]>
Commit | Line | Data |
---|---|---|
1 | /** OOoRegistry.java - a Java class to handle OpenOffice.org's Registry | |
2 | * | |
3 | * Copyright (C) 2008 Dietmar Hiller, dhiller | |
4 | ||
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. | |
7 | ||
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. | |
10 | ||
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/>. | |
13 | * | |
14 | * Created on 5. Mai 2008, 21:34 | |
15 | * | |
16 | */ | |
17 | ||
18 | package name.dhiller.ooo.OOoRegistry; | |
19 | ||
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; | |
27 | ||
28 | /** Handling of the Openoffice.org registry | |
29 | * @author Dietmar Hiller (dhiller) | |
30 | * @version 080508 | |
31 | * @since OOo 2.0 | |
32 | */ | |
33 | public class OOoRegistry { | |
34 | private final XComponentContext m_xContext; | |
35 | ||
36 | /** Creates a new instance of OOoRegistry | |
37 | */ | |
38 | public OOoRegistry(XComponentContext xContext) { | |
39 | m_xContext = xContext; | |
40 | } | |
41 | ||
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 | |
46 | * @return String | |
47 | * @exception Exception returns empty string | |
48 | */ | |
49 | public String getString(String sPackageName, String sPath, String sRegKey) { | |
50 | String sString = ""; | |
51 | try { | |
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); | |
57 | } | |
58 | } | |
59 | } catch (Exception ex) { | |
60 | ex.printStackTrace(System.out); | |
61 | } | |
62 | return sString; | |
63 | } | |
64 | ||
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 | |
71 | */ | |
72 | public String getValue(String sPackageName, String sPath, String sRegKey){ | |
73 | String sUrl = ""; | |
74 | try { | |
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()); | |
83 | sUrl = sUrl.trim(); | |
84 | } catch (Exception ex) { | |
85 | return sUrl; | |
86 | // e.g. substring was not successfull, because string was not expanded | |
87 | // nevertheless, sUrl contains the correct data and can be returned | |
88 | } | |
89 | return sUrl; | |
90 | } | |
91 | ||
92 | /* get access to the registry | |
93 | * @author dhiller | |
94 | * @version 071208 | |
95 | * @param _sKeyName - key as in <Registry>.xcu | |
96 | * @return XNameAccess | |
97 | * @exception Exception | |
98 | */ | |
99 | private XNameAccess getRegistryKeyContent(String _sKeyName){ | |
100 | try { | |
101 | Object oConfigProvider; | |
102 | PropertyValue[] aNodePath = new PropertyValue[1]; | |
103 | ||
104 | XMultiComponentFactory m_xMCF = m_xContext.getServiceManager(); | |
105 | ||
106 | oConfigProvider = | |
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); | |
113 | Object oNode = | |
114 | xMSFConfig.createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess", aNodePath); | |
115 | XNameAccess xNameAccess = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, oNode); | |
116 | return xNameAccess; | |
117 | } | |
118 | catch (Exception exception) { | |
119 | exception.printStackTrace(System.out); | |
120 | return null; | |
121 | } | |
122 | } | |
123 | } |