-2

我是一名 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;
4

1 回答 1

1

请参阅如何使用ConfigurationProvider接口的示例代码。你可能会发现这个 python 示例也有助于理解事情是如何工作的。

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;
}
于 2016-09-02T10:50:42.050 回答