bump product version to 4.1.6.2
[LibreOffice.git] / qadevOOo / runner / helper / ConfigHelper.java
blobbbef1cff96468e5f6b76d41081fe1625ff016c15
1 /*
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 .
19 package helper;
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.*;
27 /**
28 * This <CODE>ConfigHelper</CODE> makes it possible to access the
29 * configuration and change their content.<P>
30 * <P>
31 * Example: <P>
32 * Listing of the <CODE>Configuration</CODE> Views.xcu:<P>
33 * &lt;oor:component-data xmlns:oor=&quot;http://openoffice.org/2001/registry&quot; xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot; oor:name=&quot;Views&quot; oor:package=&quot;org.openoffice.Office&quot;&gt;<p>
34 * &lt;node oor:name=&quot;Windows&quot;&gt;<P>
35 * <&lt;node oor:name=&quot;SplitWindow0&quot; oor:op=&quot;replace&quot;&gt;<P>
36 * &lt;prop oor:name=&quot;Visible&quot; oor:type=&quot;xs:boolean&quot;&gt;<P>
37 * &lt;value&gt;false&lt;/value&gt;<P>
38 * &lt;/prop&gt;<P>
39 * &lt;prop oor:name=&quot;WindowState&quot; oor:type=&quot;xs:string&quot;&gt;<P>
40 * &lt;value/&gt;<P>
41 * &lt;/prop&gt;<P>
42 * &lt;node oor:name=&quot;UserData&quot;&gt;<P>
43 * &lt;prop oor:name=&quot;UserItem&quot; oor:op=&quot;replace&quot;
44 * oor:type=&quot;xs:string&quot;&gt;<P>
45 * &lt;value&gt;V1,2,0&lt;/value&gt;<P>
46 * &lt;/prop&gt;<P>
47 * &lt;/node&gt;<P>
48 * &lt;/node&gt;<P>
49 * &lt;/node&gt;<P>
50 * <P>
51 * <CODE>Definition</CODE><P>
52 * <ul>
53 * <li><CODE>&lt;node oor:name=&quot;Windows&quot;&gt;</CODE>
54 * represents a <CODE>Set</CODE> and is a <CODE>XNameContainer</CODE></LI>
55 * <li><CODE>&lt;node oor:name=&quot;SplitWindow0&quot;&gt;</CODE>
56 * represents a <CODE>Group</CODE> and is a <CODE>XNameReplace</CODE></LI>
57 * <li><CODE>&lt;prop oor:name=&quot;Visible&quot;&gt;</CODE>
58 * represents a pr<CODE></CODE>operty of the group</li>
59 * <li><CODE>&lt;node oor:name=&quot;UserData&quot;&gt;</CODE>
60 * represents a <CODE>extensible group</CODE> and is a <CODE>XNameContainer</CODE></LI>
61 * <li><CODE>&lt;prop oor:name=&quot;UserItem&quot;&gt;</CODE>
62 * represents a <CODE>property</CODE> of the extensible group</LI>
63 * </UL>
64 * We assume in the following examples the existance of:<P>
65 * <CODE>ConfigHelper aConfig = new ConfigHelper(xMSF, "org.openoffice.Office.Views", false);</CODE>
66 * <ul>
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.
72 * </li>
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>
77 * </li>
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
80 * Set "Windows":<p>
81 * <CODE>aConfig.insertOrUpdateExtensibleGroupProperty(
82 * "Windows", "my2ndGroup", "myExtGroup", "myProp","TheValue");</CODE>
83 * </li>
84 * </ul>
86 public class ConfigHelper
88 private XMultiServiceFactory m_xSMGR = null;
89 private XHierarchicalNameAccess m_xConfig = null;
91 //-----------------------------------------------
92 public ConfigHelper(XMultiServiceFactory xSMGR ,
93 String sConfigPath ,
94 boolean bReadOnly )
95 throws com.sun.star.uno.Exception
97 m_xSMGR = xSMGR;
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;
109 Object aConfig;
110 if (bReadOnly)
111 aConfig = xConfigRoot.createInstanceWithArguments(
112 "com.sun.star.configuration.ConfigurationAccess",
113 lParams);
114 else
115 aConfig = xConfigRoot.createInstanceWithArguments(
116 "com.sun.star.configuration.ConfigurationUpdateAccess",
117 lParams);
119 m_xConfig = UnoRuntime.queryInterface(
120 XHierarchicalNameAccess.class,
121 aConfig);
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,
129 String sKey )
130 throws com.sun.star.container.NoSuchElementException
134 XPropertySet xPath = UnoRuntime.queryInterface(
135 XPropertySet.class,
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,
147 String sKey ,
148 Object aValue )
149 throws com.sun.star.container.NoSuchElementException
153 XPropertySet xPath = UnoRuntime.queryInterface(
154 XPropertySet.class,
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.
170 public void flush()
174 XChangesBatch xBatch = UnoRuntime.queryInterface(
175 XChangesBatch.class,
176 m_xConfig);
177 xBatch.commitChanges();
179 catch(com.sun.star.uno.Exception ex)
183 //-----------------------------------------------
184 public static Object readDirectKey(XMultiServiceFactory xSMGR ,
185 String sConfigFile,
186 String sRelPath ,
187 String sKey )
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 ,
196 String sConfigFile,
197 String sRelPath ,
198 String sKey ,
199 Object aValue )
200 throws com.sun.star.uno.Exception
202 ConfigHelper aConfig = new ConfigHelper(xSMGR, sConfigFile, false);
203 aConfig.writeRelativeKey(sRelPath, sKey, aValue);
204 aConfig.flush();
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;
228 try {
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);
246 return xChildAccess;
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,
258 String groupName,
259 String propName,
260 Object propValue)
261 throws com.sun.star.uno.Exception
263 XNameContainer xSetCont = this.getSet(setName);
265 XPropertySet xProp = null;
266 try {
267 xProp = UnoRuntime.queryInterface(
268 XPropertySet.class,
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());
275 try{
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(
297 String setName,
298 String group,
299 String extGroup,
300 String propName,
301 Object propValue)
302 throws com.sun.star.uno.Exception
304 XNameContainer xSetCont = this.getSet(setName);
306 XNameReplace xGroupAccess = null;
307 XNameContainer xExtGroupCont = null;
309 try {
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());
319 try {
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());
330 try {
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);
354 if (oSet == null)
355 throw new com.sun.star.uno.Exception(
356 "could not get set '" + setName + ": null");
358 return UnoRuntime.queryInterface(
359 XNameContainer.class, oSet);