我是C#开发人员,实际上我必须在Java 1.6中为OpenOffice-PlugIn开发一些函数。 其中一个功能是获取环境的元信息,如执行OpenOffice的版本。 在谷歌我没有找到什么。我知道存在一个注册表项。但这只是一个没有价值的子项。 有谁知道,我如何获得使用Java 1.6执行OpenOffice的版本号?
编辑:
现在我有了解决方案。如果他们遇到同样的问题,我会帮助其他开发人员。它必须仅在方法中封装。
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;
答案 0 :(得分:1)
请参阅this example code了解如何使用ConfigurationProvider界面。您可能会发现this python example也有助于了解事情是如何运作的。
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;
}