1 /** OOoRegistry.java - a Java class to handle OpenOffice.org's Registry
3 * Copyright (C) 2008 Dietmar Hiller, dhiller
5 This program is free software; you can redistribute it and/or modify it under the Terms of the GNU Lesser General Public License
6 as published by the Free Software Foundation; version 3 of the License.
8 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
11 You should have received a copy of the GNU Lesser General Public License along with this program; if not,
12 see <http://www.gnu.org/licenses/>.
14 * Created on 5. Mai 2008, 21:34
18 package name
.dhiller
.ooo
.OOoRegistry
;
20 import com
.sun
.star
.lang
.XMultiComponentFactory
;
21 import com
.sun
.star
.lang
.XMultiServiceFactory
;
22 import com
.sun
.star
.uno
.UnoRuntime
;
23 import com
.sun
.star
.uno
.XComponentContext
;
24 import com
.sun
.star
.container
.XNameAccess
;
25 import com
.sun
.star
.util
.XMacroExpander
;
26 import com
.sun
.star
.beans
.PropertyValue
;
28 /** Handling of the Openoffice.org registry
29 * @author Dietmar Hiller (dhiller)
33 public class OOoRegistry
{
34 private final XComponentContext m_xContext
;
36 /** Creates a new instance of OOoRegistry
38 public OOoRegistry(XComponentContext xContext
) {
39 m_xContext
= xContext
;
42 /** get String from registry
43 * @param sPackageName name of package (e.g. org.openoffice.my_extension_name)
44 * @param sPath the path a registryNode
45 * @param sRegKey the name of the registry key
47 * @exception Exception returns empty string
49 public String
getString(String sPackageName
, String sPath
, String sRegKey
) {
52 // retrive the configuration node of the extension #####
53 XNameAccess xNameAccess
= getRegistryKeyContent(sPackageName
+"."+sPath
);
54 if (xNameAccess
!= null){
55 if (xNameAccess
.hasByName(sRegKey
)){
56 sString
= (String
) xNameAccess
.getByName(sRegKey
);
59 } catch (Exception ex
) {
60 ex
.printStackTrace(System
.out
);
65 /** get the value of a path, expand %origin% if available
66 * @param sPackageName name of package (e.g. org.openoffice.my_extension_name)
67 * @param sPath the path a registryNode
68 * @param sRegKey the name of the registry key
69 * @return local path to <sPath>
70 * @exception Exception e.g. substring was not successfull, because string was not expanded; nevertheless, sUrl contains the correct data and can be returned
72 public String
getValue(String sPackageName
, String sPath
, String sRegKey
){
75 // get the Url and process the Url by the macroexpander...
76 sUrl
= getString(sPackageName
, sPath
, sRegKey
);
77 Object oMacroExpander
=
78 this.m_xContext
.getValueByName("/singletons/com.sun.star.util.theMacroExpander");
79 XMacroExpander xMacroExpander
= (XMacroExpander
)
80 UnoRuntime
.queryInterface(XMacroExpander
.class, oMacroExpander
);
81 sUrl
= xMacroExpander
.expandMacros(sUrl
);
82 sUrl
= sUrl
.substring(new String("vnd.sun.star.expand:").length(), sUrl
.length());
84 } catch (Exception ex
) {
86 // e.g. substring was not successfull, because string was not expanded
87 // nevertheless, sUrl contains the correct data and can be returned
92 /* get access to the registry
95 * @param _sKeyName - key as in <Registry>.xcu
97 * @exception Exception
99 private XNameAccess
getRegistryKeyContent(String _sKeyName
){
101 Object oConfigProvider
;
102 PropertyValue
[] aNodePath
= new PropertyValue
[1];
104 XMultiComponentFactory m_xMCF
= m_xContext
.getServiceManager();
107 m_xMCF
.createInstanceWithContext("com.sun.star.configuration.ConfigurationProvider", this.m_xContext
);
108 aNodePath
[0] = new PropertyValue();
109 aNodePath
[0].Name
= "nodepath";
110 aNodePath
[0].Value
= _sKeyName
;
111 XMultiServiceFactory xMSFConfig
= (XMultiServiceFactory
)
112 UnoRuntime
.queryInterface(XMultiServiceFactory
.class, oConfigProvider
);
114 xMSFConfig
.createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess", aNodePath
);
115 XNameAccess xNameAccess
= (XNameAccess
) UnoRuntime
.queryInterface(XNameAccess
.class, oNode
);
118 catch (Exception exception
) {
119 exception
.printStackTrace(System
.out
);