2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import com
.sun
.star
.uno
.*;
22 import com
.sun
.star
.lang
.*;
23 import com
.sun
.star
.container
.*;
24 import com
.sun
.star
.beans
.*;
25 import com
.sun
.star
.util
.*;
28 * This <CODE>ConfigHelper</CODE> makes it possible to access the
29 * configuration and change their content.<P>
32 * Listing of the <CODE>Configuration</CODE> Views.xcu:<P>
33 * <oor:component-data xmlns:oor="http://openoffice.org/2001/registry" xmlns:xs="http://www.w3.org/2001/XMLSchema" oor:name="Views" oor:package="org.openoffice.Office"><p>
34 * <node oor:name="Windows"><P>
35 * <<node oor:name="SplitWindow0" oor:op="replace"><P>
36 * <prop oor:name="Visible" oor:type="xs:boolean"><P>
37 * <value>false</value><P>
39 * <prop oor:name="WindowState" oor:type="xs:string"><P>
42 * <node oor:name="UserData"><P>
43 * <prop oor:name="UserItem" oor:op="replace"
44 * oor:type="xs:string"><P>
45 * <value>V1,2,0</value><P>
51 * <CODE>Definition</CODE><P>
53 * <li><CODE><node oor:name="Windows"></CODE>
54 * represents a <CODE>Set</CODE> and is a <CODE>XNameContainer</CODE></LI>
55 * <li><CODE><node oor:name="SplitWindow0"></CODE>
56 * represents a <CODE>Group</CODE> and is a <CODE>XNameReplace</CODE></LI>
57 * <li><CODE><prop oor:name="Visible"></CODE>
58 * represents a pr<CODE></CODE>operty of the group</li>
59 * <li><CODE><node oor:name="UserData"></CODE>
60 * represents a <CODE>extensible group</CODE> and is a <CODE>XNameContainer</CODE></LI>
61 * <li><CODE><prop oor:name="UserItem"></CODE>
62 * represents a <CODE>property</CODE> of the extensible group</LI>
64 * We assume in the following examples the existance of:<P>
65 * <CODE>ConfigHelper aConfig = new ConfigHelper(xMSF, "org.openoffice.Office.Views", false);</CODE>
67 * <li>If you like to insert a new <CODE>Group</CODE> into the <CODE>Set</CODE> "Windows":<p>
68 * <CODE>XNameReplace xMyGroup = aConfig.getOrInsertGroup("Windows", "myGroup");</CODE><P>
69 * The method <CODE>getOrInsertGroup()</CODE> uses the
70 * <CODE>XSingleServiceFactory</CODE> to create the skeleton of a new group.
73 * <li>If you like to change the property "WindowState" of "myGroup"
74 * of the Set "Windows"<p>
75 * <CODE>aConfig.updateGroupProperty(
76 * "Windows","myGroup", "WindowState", "952,180,244,349;1;0,0,0,0;");</CODE>
78 * <li>If you like to change the property "myProp" of the extensible group
79 * "myExtGroup" which is an extensible group of "my2ndGroup" of the
81 * <CODE>aConfig.insertOrUpdateExtensibleGroupProperty(
82 * "Windows", "my2ndGroup", "myExtGroup", "myProp","TheValue");</CODE>
86 public class ConfigHelper
88 private XMultiServiceFactory m_xSMGR
= null;
89 private XHierarchicalNameAccess m_xConfig
= null;
91 //-----------------------------------------------
92 public ConfigHelper(XMultiServiceFactory xSMGR
,
95 throws com
.sun
.star
.uno
.Exception
99 XMultiServiceFactory xConfigRoot
= UnoRuntime
.queryInterface(
100 XMultiServiceFactory
.class,
101 m_xSMGR
.createInstance(
102 "com.sun.star.configuration.ConfigurationProvider"));
104 PropertyValue
[] lParams
= new PropertyValue
[1];
105 lParams
[0] = new PropertyValue();
106 lParams
[0].Name
= "nodepath";
107 lParams
[0].Value
= sConfigPath
;
111 aConfig
= xConfigRoot
.createInstanceWithArguments(
112 "com.sun.star.configuration.ConfigurationAccess",
115 aConfig
= xConfigRoot
.createInstanceWithArguments(
116 "com.sun.star.configuration.ConfigurationUpdateAccess",
119 m_xConfig
= UnoRuntime
.queryInterface(
120 XHierarchicalNameAccess
.class,
123 if (m_xConfig
== null)
124 throw new com
.sun
.star
.uno
.Exception("Could not open configuration \""+sConfigPath
+"\"");
127 //-----------------------------------------------
128 public Object
readRelativeKey(String sRelPath
,
130 throws com
.sun
.star
.container
.NoSuchElementException
134 XPropertySet xPath
= UnoRuntime
.queryInterface(
136 m_xConfig
.getByHierarchicalName(sRelPath
));
137 return xPath
.getPropertyValue(sKey
);
139 catch(com
.sun
.star
.uno
.Exception ex
)
141 throw new com
.sun
.star
.container
.NoSuchElementException(ex
.getMessage());
145 //-----------------------------------------------
146 public void writeRelativeKey(String sRelPath
,
149 throws com
.sun
.star
.container
.NoSuchElementException
153 XPropertySet xPath
= UnoRuntime
.queryInterface(
155 m_xConfig
.getByHierarchicalName(sRelPath
));
156 xPath
.setPropertyValue(sKey
, aValue
);
158 catch(com
.sun
.star
.uno
.Exception ex
)
160 throw new com
.sun
.star
.container
.NoSuchElementException(ex
.getMessage());
164 //-----------------------------------------------
166 * Updates the configuration.<p>
167 * This must be called after you have changed the configuration
168 * else you changes will be lost.
174 XChangesBatch xBatch
= UnoRuntime
.queryInterface(
177 xBatch
.commitChanges();
179 catch(com
.sun
.star
.uno
.Exception ex
)
183 //-----------------------------------------------
184 public static Object
readDirectKey(XMultiServiceFactory xSMGR
,
188 throws com
.sun
.star
.uno
.Exception
190 ConfigHelper aConfig
= new ConfigHelper(xSMGR
, sConfigFile
, true);
191 return aConfig
.readRelativeKey(sRelPath
, sKey
);
194 //-----------------------------------------------
195 public static void writeDirectKey(XMultiServiceFactory xSMGR
,
200 throws com
.sun
.star
.uno
.Exception
202 ConfigHelper aConfig
= new ConfigHelper(xSMGR
, sConfigFile
, false);
203 aConfig
.writeRelativeKey(sRelPath
, sKey
, aValue
);
209 * Insert a structured node (group) in a name container (set)
210 * or else update it and retrun the <CODE>XNameReplace</CODE> of it.<P>
211 * The <CODE>XSingleServiceFacttory</CODE> of the <CODE>set</CODE> will be used
212 * to create a new group. This group is specific to its set and
213 * creates defined entries.
214 * @return The [inserted] group of the set
215 * @param groupName The name of the goup which should be returned
216 * @param setName The name of the set
217 * @throws com.sun.star.uno.Exception throws
218 * <CODE>com.sun.star.uno.Exeception</CODE> on any error.
220 public XNameReplace
getOrInsertGroup(String setName
, String groupName
)
221 throws com
.sun
.star
.uno
.Exception
224 XNameContainer xSetCont
= this.getSet(setName
);
226 XNameReplace xChildAccess
= null;
229 xSetCont
.getByName(groupName
);
230 xChildAccess
= UnoRuntime
.queryInterface(
231 XNameReplace
.class,xSetCont
);
232 } catch(com
.sun
.star
.container
.NoSuchElementException e
) {
233 // proceed with inserting
236 if (xChildAccess
== null) {
237 XSingleServiceFactory xChildfactory
= UnoRuntime
.queryInterface(XSingleServiceFactory
.class,xSetCont
);
239 Object xNewChild
= xChildfactory
.createInstance();
241 xSetCont
.insertByName(groupName
, xNewChild
);
243 xChildAccess
= UnoRuntime
.queryInterface(XNameContainer
.class,xNewChild
);
250 * Update a property of a group container of a set container
251 * @param setName the name of the <CODE>set</CODE> which containes the <CODE>group</CODE>
252 * @param groupName the name of the <CODE>group</CODE> which property should be changed
253 * @param propName the name of the property which should be changed
254 * @param propValue the value the property should get
255 * @throws com.sun.star.uno.Exception throws <CODE>com.sun.star.uno.Exeception</CODE> on any error.
257 public void updateGroupProperty(String setName
,
261 throws com
.sun
.star
.uno
.Exception
263 XNameContainer xSetCont
= this.getSet(setName
);
265 XPropertySet xProp
= null;
267 xProp
= UnoRuntime
.queryInterface(
269 xSetCont
.getByName(groupName
));
270 } catch (com
.sun
.star
.container
.NoSuchElementException e
){
271 throw new com
.sun
.star
.uno
.Exception(
272 "could not get group '" + groupName
+
273 "' from set '"+ setName
+"':\n" + e
.toString());
276 xProp
.setPropertyValue(propName
, propValue
);
277 } catch (com
.sun
.star
.uno
.Exception e
) {
278 throw new com
.sun
.star
.uno
.Exception(
279 "could not set property '" + propName
+
280 "' from group '"+ groupName
+
281 "' from set '"+ setName
+"':\n" + e
.toString());
287 * Insert a property in an extensible group container or else update it
288 * @param setName the name of the <CODE>set</CODE> which containes the <CODE>group</CODE>
289 * @param group The name of the <CODE>group</CODE> which conatins the <CODE>extensible group</CODE>.
290 * @param extGroup The name of the <CODE>extensible group</CODE> which
291 * [should] contain the property
292 * @param propName The name of the property.
293 * @param propValue The value of the property.
294 * @throws com.sun.star.uno.Exception throws <CODE>com.sun.star.uno.Exeception</CODE> on any error.
296 public void insertOrUpdateExtensibleGroupProperty(
302 throws com
.sun
.star
.uno
.Exception
304 XNameContainer xSetCont
= this.getSet(setName
);
306 XNameReplace xGroupAccess
= null;
307 XNameContainer xExtGroupCont
= null;
310 Object xGroup
=xSetCont
.getByName(group
);
311 xGroupAccess
= UnoRuntime
.queryInterface(
312 XNameReplace
.class,xGroup
);
313 } catch(com
.sun
.star
.container
.NoSuchElementException e
) {
314 throw new com
.sun
.star
.uno
.Exception(
315 "could not get group '" + group
+
316 "' from set '"+ setName
+"':\n" + e
.toString());
320 Object xGroup
=xGroupAccess
.getByName(extGroup
);
321 xExtGroupCont
= UnoRuntime
.queryInterface(
322 XNameContainer
.class,xGroup
);
323 } catch(com
.sun
.star
.container
.NoSuchElementException e
) {
324 throw new com
.sun
.star
.uno
.Exception(
325 "could not get extensilbe group '"+extGroup
+
326 "' from group '"+ group
+
327 "' from set '"+ setName
+"':\n" + e
.toString());
331 xExtGroupCont
.insertByName(propName
, propValue
);
333 catch(com
.sun
.star
.container
.ElementExistException e
) {
334 xExtGroupCont
.replaceByName(propName
, propValue
);
341 * Returns a <CODE>XNameContainer</CODE> of the <CODE>Set</CODE>
342 * of the <CODE>Configuration</CODE>
343 * @param setName the name of the Set which sould be returned
344 * @throws com.sun.star.uno.Exception on any error
345 * @return A XNameContainer of the Set
347 public XNameContainer
getSet(String setName
)
348 throws com
.sun
.star
.uno
.Exception
350 XNameReplace xCont
= UnoRuntime
.queryInterface(XNameReplace
.class, m_xConfig
);
352 Object oSet
= xCont
.getByName(setName
);
355 throw new com
.sun
.star
.uno
.Exception(
356 "could not get set '" + setName
+ ": null");
358 return UnoRuntime
.queryInterface(
359 XNameContainer
.class, oSet
);