2016-09-02 3 views
-2

Je suis un développeur C# et en fait je dois développer certaines fonctions dans Java 1.6 pour un OpenOffice-PlugIn. Une de ces fonctions est d'obtenir des méta-informations d'environnement comme la version de l'exécutable OpenOffice. Sur Google, je n'ai pas trouvé quelque chose. Je sais qu'une entrée de registre existe. Mais c'est juste une sous-clé sans valeur. Est-ce que quelqu'un sait, comment je peux obtenir le numéro de version de l'exécution d'OpenOffice avec Java 1.6?Comment lire la version OpenOffice en Java (1.6)

Éditer:

Maintenant, j'ai la solution. Je vais aider les autres développeurs, s'ils ont le même problème. Il doit être seulement capsuled dans une méthode.

XComponentContext componentContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 
     XMultiComponentFactory xRemoteServiceManager = componentContext.getServiceManager(); 


     Object configProvider = xRemoteServiceManager.createInstanceWithContext("com.sun.star.configuration.ConfigurationProvider", componentContext); 
     XMultiServiceFactory xConfigProvider = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, configProvider); 

     PropertyValue[] lParams = new PropertyValue[1]; 

     lParams[0] = new PropertyValue(); 
     lParams[0].Name = "nodepath"; 
     lParams[0].Value = "/org.openoffice.Setup/Product"; 

     Object xAccess = xConfigProvider.createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess" , lParams); 

     XNameAccess xNameAccess = (com.sun.star.container.XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, xAccess); 

     String OOVersion = (String)xNameAccess.getByName("ooSetupVersion"); 
     return OOVersion; 

Répondre

1

Voir this example code comment utiliser l'interface ConfigurationProvider. Vous pouvez trouver this python example utile aussi pour comprendre comment les choses fonctionnent.

NODE_PRODUCT = "org.openoffice.Setup/Product"; 
public String getOpenOfficeVersion() { 
     try { 
      // OOo >= 2.2 returns major.minor.micro 
      return getOpenOfficeProperty(NODE_PRODUCT, "ooSetupVersionAboutBox"); 
     } catch (OpenOfficeException noSuchElementException) { 
      // OOo < 2.2 only returns major.minor 
      return getOpenOfficeProperty(NODE_PRODUCT, "ooSetupVersion"); 
     } 
} 

public String getOpenOfficeProperty(String nodePath, String node) { 
     if (!nodePath.startsWith("/")) { 
      nodePath = "/" + nodePath; 
     } 
     String property = ""; 
     // create the provider and remember it as a XMultiServiceFactory 
     try { 
      final String sProviderService = "com.sun.star.configuration.ConfigurationProvider"; 
      Object configProvider = connection.getRemoteServiceManager().createInstanceWithContext(
       sProviderService, connection.getComponentContext()); 
      XMultiServiceFactory xConfigProvider = UnoRuntime.queryInterface(
       com.sun.star.lang.XMultiServiceFactory.class, configProvider); 

      // The service name: Need only read access: 
      final String sReadOnlyView = "com.sun.star.configuration.ConfigurationAccess"; 
      // creation arguments: nodepath 
      PropertyValue aPathArgument = new PropertyValue(); 
      aPathArgument.Name = "nodepath"; 
      aPathArgument.Value = nodePath; 
      Object[] aArguments = new Object[1]; 
      aArguments[0] = aPathArgument; 

      // create the view 
      XInterface xElement = (XInterface) xConfigProvider.createInstanceWithArguments(sReadOnlyView, aArguments); 
      XNameAccess xChildAccess = UnoRuntime.queryInterface(XNameAccess.class, xElement); 

      // get the value 
      property = (String) xChildAccess.getByName(node); 
     } catch (Exception exception) { 
      throw new OpenOfficeException("Could not retrieve property", exception); 
     } 
     return property; 
} 
+0

Nous vous remercions de votre réponse. Je suis cependant à la recherche d'une méthode pour interroger la version de la version OO en cours depuis mon Addin et j'espérais ne pas avoir à utiliser une bibliothèque, mais plutôt que OO offre la possibilité à ses addins d'interroger la version. Est-ce que quelqu'un sait si c'est possible? – Andy