3 import com
.sun
.star
.uno
.*;
4 import com
.sun
.star
.lang
.*;
5 import com
.sun
.star
.container
.*;
6 import com
.sun
.star
.beans
.*;
7 import com
.sun
.star
.util
.*;
10 * This <CODE>ConfigHelper</CODE> makes it possible to access the
11 * configuration and change their content.<P>
14 * Listing of the <CODE>Configuration</CODE> Views.xcu:<P>
15 * <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>
16 * <node oor:name="Windows"><P>
17 * <<node oor:name="SplitWindow0" oor:op="replace"><P>
18 * <prop oor:name="Visible" oor:type="xs:boolean"><P>
19 * <value>false</value><P>
21 * <prop oor:name="WindowState" oor:type="xs:string"><P>
24 * <node oor:name="UserData"><P>
25 * <prop oor:name="UserItem" oor:op="replace"
26 * oor:type="xs:string"><P>
27 * <value>V1,2,0</value><P>
33 * <CODE>Definition</CODE><P>
35 * <li><CODE><node oor:name="Windows"></CODE>
36 * represents a <CODE>Set</CODE> and is a <CODE>XNameContainer</CODE></LI>
37 * <li><CODE><node oor:name="SplitWindow0"></CODE>
38 * represents a <CODE>Group</CODE> and is a <CODE>XNameReplace</CODE></LI>
39 * <li><CODE><prop oor:name="Visible"></CODE>
40 * represents a pr<CODE></CODE>operty of the group</li>
41 * <li><CODE><node oor:name="UserData"></CODE>
42 * represents a <CODE>extensible group</CODE> and is a <CODE>XNameContainer</CODE></LI>
43 * <li><CODE><prop oor:name="UserItem"></CODE>
44 * represents a <CODE>property</CODE> of the extensible group</LI>
46 * We assume in the following examples the existance of:<P>
47 * <CODE>ConfigHelper aConfig = new ConfigHelper(xMSF, "org.openoffice.Office.Views", false);</CODE>
49 * <li>If you like to insert a new <CODE>Group</CODE> into the <CODE>Set</CODE> "Windows":<p>
50 * <CODE>XNameReplace xMyGroup = aConfig.getOrInsertGroup("Windows", "myGroup");</CODE><P>
51 * The method <CODE>getOrInsertGroup()</CODE> uses the
52 * <CODE>XSingleServiceFactory</CODE> to create the skeleton of a new group.
55 * <li>If you like to change the property "WindowState" of "myGroup"
56 * of the Set "Windows"<p>
57 * <CODE>aConfig.updateGroupProperty(
58 * "Windows","myGroup", "WindowState", "952,180,244,349;1;0,0,0,0;");</CODE>
60 * <li>If you like to change the property "myProp" of the extensible group
61 * "myExtGroup" which is an extensible group of "my2ndGroup" of the
63 * <CODE>aConfig.insertOrUpdateExtensibleGroupProperty(
64 * "Windows", "my2ndGroup", "myExtGroup", "myProp","TheValue");</CODE>
68 public class ConfigHelper
70 private XMultiServiceFactory m_xSMGR
= null;
71 private XHierarchicalNameAccess m_xConfig
= null;
73 //-----------------------------------------------
74 public ConfigHelper(XMultiServiceFactory xSMGR
,
77 throws com
.sun
.star
.uno
.Exception
81 XMultiServiceFactory xConfigRoot
= (XMultiServiceFactory
)
82 UnoRuntime
.queryInterface(
83 XMultiServiceFactory
.class,
84 m_xSMGR
.createInstance(
85 "com.sun.star.configuration.ConfigurationProvider"));
87 PropertyValue
[] lParams
= new PropertyValue
[1];
88 lParams
[0] = new PropertyValue();
89 lParams
[0].Name
= "nodepath";
90 lParams
[0].Value
= sConfigPath
;
94 aConfig
= xConfigRoot
.createInstanceWithArguments(
95 "com.sun.star.configuration.ConfigurationAccess",
98 aConfig
= xConfigRoot
.createInstanceWithArguments(
99 "com.sun.star.configuration.ConfigurationUpdateAccess",
102 m_xConfig
= (XHierarchicalNameAccess
)UnoRuntime
.queryInterface(
103 XHierarchicalNameAccess
.class,
106 if (m_xConfig
== null)
107 throw new com
.sun
.star
.uno
.Exception("Could not open configuration \""+sConfigPath
+"\"");
110 //-----------------------------------------------
111 public Object
readRelativeKey(String sRelPath
,
113 throws com
.sun
.star
.container
.NoSuchElementException
117 XPropertySet xPath
= (XPropertySet
)UnoRuntime
.queryInterface(
119 m_xConfig
.getByHierarchicalName(sRelPath
));
120 return xPath
.getPropertyValue(sKey
);
122 catch(com
.sun
.star
.uno
.Exception ex
)
124 throw new com
.sun
.star
.container
.NoSuchElementException(ex
.getMessage());
128 //-----------------------------------------------
129 public void writeRelativeKey(String sRelPath
,
132 throws com
.sun
.star
.container
.NoSuchElementException
136 XPropertySet xPath
= (XPropertySet
)UnoRuntime
.queryInterface(
138 m_xConfig
.getByHierarchicalName(sRelPath
));
139 xPath
.setPropertyValue(sKey
, aValue
);
141 catch(com
.sun
.star
.uno
.Exception ex
)
143 throw new com
.sun
.star
.container
.NoSuchElementException(ex
.getMessage());
147 //-----------------------------------------------
149 * Updates the configuration.<p>
150 * This must be called after you have changed the configuration
151 * else you changes will be lost.
157 XChangesBatch xBatch
= (XChangesBatch
)UnoRuntime
.queryInterface(
160 xBatch
.commitChanges();
162 catch(com
.sun
.star
.uno
.Exception ex
)
166 //-----------------------------------------------
167 public static Object
readDirectKey(XMultiServiceFactory xSMGR
,
171 throws com
.sun
.star
.uno
.Exception
173 ConfigHelper aConfig
= new ConfigHelper(xSMGR
, sConfigFile
, true);
174 return aConfig
.readRelativeKey(sRelPath
, sKey
);
177 //-----------------------------------------------
178 public static void writeDirectKey(XMultiServiceFactory xSMGR
,
183 throws com
.sun
.star
.uno
.Exception
185 ConfigHelper aConfig
= new ConfigHelper(xSMGR
, sConfigFile
, false);
186 aConfig
.writeRelativeKey(sRelPath
, sKey
, aValue
);
192 * Insert a structured node (group) in a name container (set)
193 * or else update it and retrun the <CODE>XNameReplace</CODE> of it.<P>
194 * The <CODE>XSingleServiceFacttory</CODE> of the <CODE>set</CODE> will be used
195 * to create a new group. This group is specific to its set and
196 * creates defined entries.
197 * @return The [inserted] group of the set
198 * @param groupName The name of the goup which should be returned
199 * @param setName The name of the set
200 * @throws com.sun.star.uno.Exception throws
201 * <CODE>com.sun.star.uno.Exeception</CODE> on any error.
203 public XNameReplace
getOrInsertGroup(String setName
, String groupName
)
204 throws com
.sun
.star
.uno
.Exception
207 XNameContainer xSetCont
= this.getSet(setName
);
209 XNameReplace xChildAccess
= null;
212 Object xChild
=xSetCont
.getByName(groupName
);
213 xChildAccess
= (XNameReplace
) UnoRuntime
.queryInterface(
214 XNameReplace
.class,xSetCont
);
215 } catch(com
.sun
.star
.container
.NoSuchElementException e
) {
216 // proceed with inserting
219 if (xChildAccess
== null) {
220 XSingleServiceFactory xChildfactory
= (XSingleServiceFactory
)
221 UnoRuntime
.queryInterface(XSingleServiceFactory
.class,xSetCont
);
223 Object xNewChild
= xChildfactory
.createInstance();
225 xSetCont
.insertByName(groupName
, xNewChild
);
227 xChildAccess
= (XNameReplace
)
228 UnoRuntime
.queryInterface(XNameContainer
.class,xNewChild
);
235 * Update a property of a group container of a set container
236 * @param setName the name of the <CODE>set</CODE> which containes the <CODE>group</CODE>
237 * @param groupName the name of the <CODE>group</CODE> which property should be changed
238 * @param propName the name of the property which should be changed
239 * @param propValue the value the property should get
240 * @throws com.sun.star.uno.Exception throws <CODE>com.sun.star.uno.Exeception</CODE> on any error.
242 public void updateGroupProperty(String setName
,
246 throws com
.sun
.star
.uno
.Exception
248 XNameContainer xSetCont
= this.getSet(setName
);
250 XPropertySet xProp
= null;
252 xProp
= (XPropertySet
)UnoRuntime
.queryInterface(
254 xSetCont
.getByName(groupName
));
255 } catch (com
.sun
.star
.container
.NoSuchElementException e
){
256 throw new com
.sun
.star
.uno
.Exception(
257 "could not get group '" + groupName
+
258 "' from set '"+ setName
+"':\n" + e
.toString());
261 xProp
.setPropertyValue(propName
, propValue
);
262 } catch (com
.sun
.star
.uno
.Exception e
) {
263 throw new com
.sun
.star
.uno
.Exception(
264 "could not set property '" + propName
+
265 "' from group '"+ groupName
+
266 "' from set '"+ setName
+"':\n" + e
.toString());
272 * Insert a property in an extensible group container or else update it
273 * @param setName the name of the <CODE>set</CODE> which containes the <CODE>group</CODE>
274 * @param group The name of the <CODE>group</CODE> which conatins the <CODE>extensible group</CODE>.
275 * @param extGroup The name of the <CODE>extensible group</CODE> which
276 * [should] contain the property
277 * @param propName The name of the property.
278 * @param propValue The value of the property.
279 * @throws com.sun.star.uno.Exception throws <CODE>com.sun.star.uno.Exeception</CODE> on any error.
281 public void insertOrUpdateExtensibleGroupProperty(
287 throws com
.sun
.star
.uno
.Exception
289 XNameContainer xSetCont
= this.getSet(setName
);
291 XNameReplace xGroupAccess
= null;
292 XNameContainer xExtGroupCont
= null;
295 Object xGroup
=xSetCont
.getByName(group
);
296 xGroupAccess
= (XNameReplace
) UnoRuntime
.queryInterface(
297 XNameReplace
.class,xGroup
);
298 } catch(com
.sun
.star
.container
.NoSuchElementException e
) {
299 throw new com
.sun
.star
.uno
.Exception(
300 "could not get group '" + group
+
301 "' from set '"+ setName
+"':\n" + e
.toString());
305 Object xGroup
=xGroupAccess
.getByName(extGroup
);
306 xExtGroupCont
= (XNameContainer
) UnoRuntime
.queryInterface(
307 XNameContainer
.class,xGroup
);
308 } catch(com
.sun
.star
.container
.NoSuchElementException e
) {
309 throw new com
.sun
.star
.uno
.Exception(
310 "could not get extensilbe group '"+extGroup
+
311 "' from group '"+ group
+
312 "' from set '"+ setName
+"':\n" + e
.toString());
316 xExtGroupCont
.insertByName(propName
, propValue
);
318 catch(com
.sun
.star
.container
.ElementExistException e
) {
319 xExtGroupCont
.replaceByName(propName
, propValue
);
326 * Returns a <CODE>XNameContainer</CODE> of the <CODE>Set</CODE>
327 * of the <CODE>Configuration</CODE>
328 * @param setName the name of the Set which sould be returned
329 * @throws com.sun.star.uno.Exception on any error
330 * @return A XNameContainer of the Set
332 public XNameContainer
getSet(String setName
)
333 throws com
.sun
.star
.uno
.Exception
335 XNameReplace xCont
= (XNameReplace
)
336 UnoRuntime
.queryInterface(XNameReplace
.class, m_xConfig
);
338 Object oSet
= xCont
.getByName(setName
);
341 throw new com
.sun
.star
.uno
.Exception(
342 "could not get set '" + setName
+ ": null");
344 return (XNameContainer
) UnoRuntime
.queryInterface(
345 XNameContainer
.class, oSet
);