1ef39d40dSAndrew Rist /************************************************************** 2ef39d40dSAndrew Rist * 3ef39d40dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4ef39d40dSAndrew Rist * or more contributor license agreements. See the NOTICE file 5ef39d40dSAndrew Rist * distributed with this work for additional information 6ef39d40dSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7ef39d40dSAndrew Rist * to you under the Apache License, Version 2.0 (the 8ef39d40dSAndrew Rist * "License"); you may not use this file except in compliance 9ef39d40dSAndrew Rist * with the License. You may obtain a copy of the License at 10ef39d40dSAndrew Rist * 11ef39d40dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12ef39d40dSAndrew Rist * 13ef39d40dSAndrew Rist * Unless required by applicable law or agreed to in writing, 14ef39d40dSAndrew Rist * software distributed under the License is distributed on an 15ef39d40dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16ef39d40dSAndrew Rist * KIND, either express or implied. See the License for the 17ef39d40dSAndrew Rist * specific language governing permissions and limitations 18ef39d40dSAndrew Rist * under the License. 19ef39d40dSAndrew Rist * 20ef39d40dSAndrew Rist *************************************************************/ 21ef39d40dSAndrew Rist 22cdf0e10cSrcweir package helper; 23cdf0e10cSrcweir 24cdf0e10cSrcweir import com.sun.star.uno.*; 25cdf0e10cSrcweir import com.sun.star.lang.*; 26cdf0e10cSrcweir import com.sun.star.container.*; 27cdf0e10cSrcweir import com.sun.star.beans.*; 28cdf0e10cSrcweir import com.sun.star.util.*; 29cdf0e10cSrcweir 30cdf0e10cSrcweir /** 31cdf0e10cSrcweir * This <CODE>ConfigHelper</CODE> makes it possible to access the 32cdf0e10cSrcweir * configuration and change their content.<P> 33cdf0e10cSrcweir * <P> 34cdf0e10cSrcweir * Example: <P> 35cdf0e10cSrcweir * Listing of the <CODE>Configuration</CODE> Views.xcu:<P> 36cdf0e10cSrcweir * <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> 37cdf0e10cSrcweir * <node oor:name="Windows"><P> 38cdf0e10cSrcweir * <<node oor:name="SplitWindow0" oor:op="replace"><P> 39cdf0e10cSrcweir * <prop oor:name="Visible" oor:type="xs:boolean"><P> 40cdf0e10cSrcweir * <value>false</value><P> 41cdf0e10cSrcweir * </prop><P> 42cdf0e10cSrcweir * <prop oor:name="WindowState" oor:type="xs:string"><P> 43cdf0e10cSrcweir * <value/><P> 44cdf0e10cSrcweir * </prop><P> 45cdf0e10cSrcweir * <node oor:name="UserData"><P> 46cdf0e10cSrcweir * <prop oor:name="UserItem" oor:op="replace" 47cdf0e10cSrcweir * oor:type="xs:string"><P> 48cdf0e10cSrcweir * <value>V1,2,0</value><P> 49cdf0e10cSrcweir * </prop><P> 50cdf0e10cSrcweir * </node><P> 51cdf0e10cSrcweir * </node><P> 52cdf0e10cSrcweir * </node><P> 53cdf0e10cSrcweir * <P> 54cdf0e10cSrcweir * <CODE>Definition</CODE><P> 55cdf0e10cSrcweir * <ul> 56cdf0e10cSrcweir * <li><CODE><node oor:name="Windows"></CODE> 57cdf0e10cSrcweir * represents a <CODE>Set</CODE> and is a <CODE>XNameContainer</CODE></LI> 58cdf0e10cSrcweir * <li><CODE><node oor:name="SplitWindow0"></CODE> 59cdf0e10cSrcweir * represents a <CODE>Group</CODE> and is a <CODE>XNameReplace</CODE></LI> 60cdf0e10cSrcweir * <li><CODE><prop oor:name="Visible"></CODE> 61cdf0e10cSrcweir * represents a pr<CODE></CODE>operty of the group</li> 62cdf0e10cSrcweir * <li><CODE><node oor:name="UserData"></CODE> 63cdf0e10cSrcweir * represents a <CODE>extensible group</CODE> and is a <CODE>XNameContainer</CODE></LI> 64cdf0e10cSrcweir * <li><CODE><prop oor:name="UserItem"></CODE> 65cdf0e10cSrcweir * represents a <CODE>property</CODE> of the extensible group</LI> 66cdf0e10cSrcweir * </UL> 67*bb6af6bcSPedro Giffuni * We assume in the following examples the existence of:<P> 68cdf0e10cSrcweir * <CODE>ConfigHelper aConfig = new ConfigHelper(xMSF, "org.openoffice.Office.Views", false);</CODE> 69cdf0e10cSrcweir * <ul> 70cdf0e10cSrcweir * <li>If you like to insert a new <CODE>Group</CODE> into the <CODE>Set</CODE> "Windows":<p> 71cdf0e10cSrcweir * <CODE>XNameReplace xMyGroup = aConfig.getOrInsertGroup("Windows", "myGroup");</CODE><P> 72cdf0e10cSrcweir * The method <CODE>getOrInsertGroup()</CODE> uses the 73cdf0e10cSrcweir * <CODE>XSingleServiceFactory</CODE> to create the skeleton of a new group. 74cdf0e10cSrcweir * 75cdf0e10cSrcweir * </li> 76cdf0e10cSrcweir * <li>If you like to change the property "WindowState" of "myGroup" 77cdf0e10cSrcweir * of the Set "Windows"<p> 78cdf0e10cSrcweir * <CODE>aConfig.updateGroupProperty( 79cdf0e10cSrcweir * "Windows","myGroup", "WindowState", "952,180,244,349;1;0,0,0,0;");</CODE> 80cdf0e10cSrcweir * </li> 81cdf0e10cSrcweir * <li>If you like to change the property "myProp" of the extensible group 82cdf0e10cSrcweir * "myExtGroup" which is an extensible group of "my2ndGroup" of the 83cdf0e10cSrcweir * Set "Windows":<p> 84cdf0e10cSrcweir * <CODE>aConfig.insertOrUpdateExtensibleGroupProperty( 85cdf0e10cSrcweir * "Windows", "my2ndGroup", "myExtGroup", "myProp","TheValue");</CODE> 86cdf0e10cSrcweir * </li> 87cdf0e10cSrcweir * </ul> 88cdf0e10cSrcweir */ 89cdf0e10cSrcweir public class ConfigHelper 90cdf0e10cSrcweir { 91cdf0e10cSrcweir private XMultiServiceFactory m_xSMGR = null; 92cdf0e10cSrcweir private XHierarchicalNameAccess m_xConfig = null; 93cdf0e10cSrcweir 94cdf0e10cSrcweir //----------------------------------------------- 95cdf0e10cSrcweir public ConfigHelper(XMultiServiceFactory xSMGR , 96cdf0e10cSrcweir String sConfigPath , 97cdf0e10cSrcweir boolean bReadOnly ) 98cdf0e10cSrcweir throws com.sun.star.uno.Exception 99cdf0e10cSrcweir { 100cdf0e10cSrcweir m_xSMGR = xSMGR; 101cdf0e10cSrcweir 102cdf0e10cSrcweir XMultiServiceFactory xConfigRoot = (XMultiServiceFactory) 103cdf0e10cSrcweir UnoRuntime.queryInterface( 104cdf0e10cSrcweir XMultiServiceFactory.class, 105cdf0e10cSrcweir m_xSMGR.createInstance( 106cdf0e10cSrcweir "com.sun.star.configuration.ConfigurationProvider")); 107cdf0e10cSrcweir 108cdf0e10cSrcweir PropertyValue[] lParams = new PropertyValue[1]; 109cdf0e10cSrcweir lParams[0] = new PropertyValue(); 110cdf0e10cSrcweir lParams[0].Name = "nodepath"; 111cdf0e10cSrcweir lParams[0].Value = sConfigPath; 112cdf0e10cSrcweir 113cdf0e10cSrcweir Object aConfig; 114cdf0e10cSrcweir if (bReadOnly) 115cdf0e10cSrcweir aConfig = xConfigRoot.createInstanceWithArguments( 116cdf0e10cSrcweir "com.sun.star.configuration.ConfigurationAccess", 117cdf0e10cSrcweir lParams); 118cdf0e10cSrcweir else 119cdf0e10cSrcweir aConfig = xConfigRoot.createInstanceWithArguments( 120cdf0e10cSrcweir "com.sun.star.configuration.ConfigurationUpdateAccess", 121cdf0e10cSrcweir lParams); 122cdf0e10cSrcweir 123cdf0e10cSrcweir m_xConfig = (XHierarchicalNameAccess)UnoRuntime.queryInterface( 124cdf0e10cSrcweir XHierarchicalNameAccess.class, 125cdf0e10cSrcweir aConfig); 126cdf0e10cSrcweir 127cdf0e10cSrcweir if (m_xConfig == null) 128cdf0e10cSrcweir throw new com.sun.star.uno.Exception("Could not open configuration \""+sConfigPath+"\""); 129cdf0e10cSrcweir } 130cdf0e10cSrcweir 131cdf0e10cSrcweir //----------------------------------------------- 132cdf0e10cSrcweir public Object readRelativeKey(String sRelPath, 133cdf0e10cSrcweir String sKey ) 134cdf0e10cSrcweir throws com.sun.star.container.NoSuchElementException 135cdf0e10cSrcweir { 136cdf0e10cSrcweir try 137cdf0e10cSrcweir { 138cdf0e10cSrcweir XPropertySet xPath = (XPropertySet)UnoRuntime.queryInterface( 139cdf0e10cSrcweir XPropertySet.class, 140cdf0e10cSrcweir m_xConfig.getByHierarchicalName(sRelPath)); 141cdf0e10cSrcweir return xPath.getPropertyValue(sKey); 142cdf0e10cSrcweir } 143cdf0e10cSrcweir catch(com.sun.star.uno.Exception ex) 144cdf0e10cSrcweir { 145cdf0e10cSrcweir throw new com.sun.star.container.NoSuchElementException(ex.getMessage()); 146cdf0e10cSrcweir } 147cdf0e10cSrcweir } 148cdf0e10cSrcweir 149cdf0e10cSrcweir //----------------------------------------------- 150cdf0e10cSrcweir public void writeRelativeKey(String sRelPath, 151cdf0e10cSrcweir String sKey , 152cdf0e10cSrcweir Object aValue ) 153cdf0e10cSrcweir throws com.sun.star.container.NoSuchElementException 154cdf0e10cSrcweir { 155cdf0e10cSrcweir try 156cdf0e10cSrcweir { 157cdf0e10cSrcweir XPropertySet xPath = (XPropertySet)UnoRuntime.queryInterface( 158cdf0e10cSrcweir XPropertySet.class, 159cdf0e10cSrcweir m_xConfig.getByHierarchicalName(sRelPath)); 160cdf0e10cSrcweir xPath.setPropertyValue(sKey, aValue); 161cdf0e10cSrcweir } 162cdf0e10cSrcweir catch(com.sun.star.uno.Exception ex) 163cdf0e10cSrcweir { 164cdf0e10cSrcweir throw new com.sun.star.container.NoSuchElementException(ex.getMessage()); 165cdf0e10cSrcweir } 166cdf0e10cSrcweir } 167cdf0e10cSrcweir 168cdf0e10cSrcweir //----------------------------------------------- 169cdf0e10cSrcweir /** 170cdf0e10cSrcweir * Updates the configuration.<p> 171cdf0e10cSrcweir * This must be called after you have changed the configuration 172cdf0e10cSrcweir * else you changes will be lost. 173cdf0e10cSrcweir */ 174cdf0e10cSrcweir public void flush() 175cdf0e10cSrcweir { 176cdf0e10cSrcweir try 177cdf0e10cSrcweir { 178cdf0e10cSrcweir XChangesBatch xBatch = (XChangesBatch)UnoRuntime.queryInterface( 179cdf0e10cSrcweir XChangesBatch.class, 180cdf0e10cSrcweir m_xConfig); 181cdf0e10cSrcweir xBatch.commitChanges(); 182cdf0e10cSrcweir } 183cdf0e10cSrcweir catch(com.sun.star.uno.Exception ex) 184cdf0e10cSrcweir {} 185cdf0e10cSrcweir } 186cdf0e10cSrcweir 187cdf0e10cSrcweir //----------------------------------------------- 188cdf0e10cSrcweir public static Object readDirectKey(XMultiServiceFactory xSMGR , 189cdf0e10cSrcweir String sConfigFile, 190cdf0e10cSrcweir String sRelPath , 191cdf0e10cSrcweir String sKey ) 192cdf0e10cSrcweir throws com.sun.star.uno.Exception 193cdf0e10cSrcweir { 194cdf0e10cSrcweir ConfigHelper aConfig = new ConfigHelper(xSMGR, sConfigFile, true); 195cdf0e10cSrcweir return aConfig.readRelativeKey(sRelPath, sKey); 196cdf0e10cSrcweir } 197cdf0e10cSrcweir 198cdf0e10cSrcweir //----------------------------------------------- 199cdf0e10cSrcweir public static void writeDirectKey(XMultiServiceFactory xSMGR , 200cdf0e10cSrcweir String sConfigFile, 201cdf0e10cSrcweir String sRelPath , 202cdf0e10cSrcweir String sKey , 203cdf0e10cSrcweir Object aValue ) 204cdf0e10cSrcweir throws com.sun.star.uno.Exception 205cdf0e10cSrcweir { 206cdf0e10cSrcweir ConfigHelper aConfig = new ConfigHelper(xSMGR, sConfigFile, false); 207cdf0e10cSrcweir aConfig.writeRelativeKey(sRelPath, sKey, aValue); 208cdf0e10cSrcweir aConfig.flush(); 209cdf0e10cSrcweir } 210cdf0e10cSrcweir 211cdf0e10cSrcweir 212cdf0e10cSrcweir /** 213cdf0e10cSrcweir * Insert a structured node (group) in a name container (set) 214cdf0e10cSrcweir * or else update it and retrun the <CODE>XNameReplace</CODE> of it.<P> 215cdf0e10cSrcweir * The <CODE>XSingleServiceFacttory</CODE> of the <CODE>set</CODE> will be used 216cdf0e10cSrcweir * to create a new group. This group is specific to its set and 217cdf0e10cSrcweir * creates defined entries. 218cdf0e10cSrcweir * @return The [inserted] group of the set 219cdf0e10cSrcweir * @param groupName The name of the goup which should be returned 220cdf0e10cSrcweir * @param setName The name of the set 221cdf0e10cSrcweir * @throws com.sun.star.uno.Exception throws 222cdf0e10cSrcweir * <CODE>com.sun.star.uno.Exeception</CODE> on any error. 223cdf0e10cSrcweir */ 224cdf0e10cSrcweir public XNameReplace getOrInsertGroup(String setName, String groupName) 225cdf0e10cSrcweir throws com.sun.star.uno.Exception 226cdf0e10cSrcweir 227cdf0e10cSrcweir { 228cdf0e10cSrcweir XNameContainer xSetCont = this.getSet(setName); 229cdf0e10cSrcweir 230cdf0e10cSrcweir XNameReplace xChildAccess = null; 231cdf0e10cSrcweir 232cdf0e10cSrcweir try { 233cdf0e10cSrcweir Object xChild=xSetCont.getByName(groupName); 234cdf0e10cSrcweir xChildAccess = (XNameReplace) UnoRuntime.queryInterface( 235cdf0e10cSrcweir XNameReplace.class,xSetCont); 236cdf0e10cSrcweir } catch(com.sun.star.container.NoSuchElementException e) { 237cdf0e10cSrcweir // proceed with inserting 238cdf0e10cSrcweir } 239cdf0e10cSrcweir 240cdf0e10cSrcweir if (xChildAccess == null) { 241cdf0e10cSrcweir XSingleServiceFactory xChildfactory = (XSingleServiceFactory) 242cdf0e10cSrcweir UnoRuntime.queryInterface(XSingleServiceFactory.class,xSetCont); 243cdf0e10cSrcweir 244cdf0e10cSrcweir Object xNewChild = xChildfactory.createInstance(); 245cdf0e10cSrcweir 246cdf0e10cSrcweir xSetCont.insertByName(groupName, xNewChild); 247cdf0e10cSrcweir 248cdf0e10cSrcweir xChildAccess = (XNameReplace) 249cdf0e10cSrcweir UnoRuntime.queryInterface(XNameContainer.class,xNewChild); 250cdf0e10cSrcweir } 251cdf0e10cSrcweir 252cdf0e10cSrcweir return xChildAccess; 253cdf0e10cSrcweir } 254cdf0e10cSrcweir 255cdf0e10cSrcweir /** 256cdf0e10cSrcweir * Update a property of a group container of a set container 257*bb6af6bcSPedro Giffuni * @param setName the name of the <CODE>set</CODE> which contains the <CODE>group</CODE> 258cdf0e10cSrcweir * @param groupName the name of the <CODE>group</CODE> which property should be changed 259cdf0e10cSrcweir * @param propName the name of the property which should be changed 260cdf0e10cSrcweir * @param propValue the value the property should get 261cdf0e10cSrcweir * @throws com.sun.star.uno.Exception throws <CODE>com.sun.star.uno.Exeception</CODE> on any error. 262cdf0e10cSrcweir */ 263cdf0e10cSrcweir public void updateGroupProperty(String setName, 264cdf0e10cSrcweir String groupName, 265cdf0e10cSrcweir String propName, 266cdf0e10cSrcweir Object propValue) 267cdf0e10cSrcweir throws com.sun.star.uno.Exception 268cdf0e10cSrcweir { 269cdf0e10cSrcweir XNameContainer xSetCont = this.getSet(setName); 270cdf0e10cSrcweir 271cdf0e10cSrcweir XPropertySet xProp = null; 272cdf0e10cSrcweir try { 273cdf0e10cSrcweir xProp = (XPropertySet)UnoRuntime.queryInterface( 274cdf0e10cSrcweir XPropertySet.class, 275cdf0e10cSrcweir xSetCont.getByName(groupName)); 276cdf0e10cSrcweir } catch (com.sun.star.container.NoSuchElementException e){ 277cdf0e10cSrcweir throw new com.sun.star.uno.Exception( 278cdf0e10cSrcweir "could not get group '" + groupName + 279cdf0e10cSrcweir "' from set '"+ setName +"':\n" + e.toString()); 280cdf0e10cSrcweir } 281cdf0e10cSrcweir try{ 282cdf0e10cSrcweir xProp.setPropertyValue(propName, propValue); 283cdf0e10cSrcweir } catch (com.sun.star.uno.Exception e) { 284cdf0e10cSrcweir throw new com.sun.star.uno.Exception( 285cdf0e10cSrcweir "could not set property '" + propName + 286cdf0e10cSrcweir "' from group '"+ groupName + 287cdf0e10cSrcweir "' from set '"+ setName +"':\n" + e.toString()); 288cdf0e10cSrcweir } 289cdf0e10cSrcweir } 290cdf0e10cSrcweir 291cdf0e10cSrcweir 292cdf0e10cSrcweir /** 293cdf0e10cSrcweir * Insert a property in an extensible group container or else update it 294*bb6af6bcSPedro Giffuni * @param setName the name of the <CODE>set</CODE> which contains the <CODE>group</CODE> 295cdf0e10cSrcweir * @param group The name of the <CODE>group</CODE> which conatins the <CODE>extensible group</CODE>. 296cdf0e10cSrcweir * @param extGroup The name of the <CODE>extensible group</CODE> which 297cdf0e10cSrcweir * [should] contain the property 298cdf0e10cSrcweir * @param propName The name of the property. 299cdf0e10cSrcweir * @param propValue The value of the property. 300cdf0e10cSrcweir * @throws com.sun.star.uno.Exception throws <CODE>com.sun.star.uno.Exeception</CODE> on any error. 301cdf0e10cSrcweir */ 302cdf0e10cSrcweir public void insertOrUpdateExtensibleGroupProperty( 303cdf0e10cSrcweir String setName, 304cdf0e10cSrcweir String group, 305cdf0e10cSrcweir String extGroup, 306cdf0e10cSrcweir String propName, 307cdf0e10cSrcweir Object propValue) 308cdf0e10cSrcweir throws com.sun.star.uno.Exception 309cdf0e10cSrcweir { 310cdf0e10cSrcweir XNameContainer xSetCont = this.getSet(setName); 311cdf0e10cSrcweir 312cdf0e10cSrcweir XNameReplace xGroupAccess = null; 313cdf0e10cSrcweir XNameContainer xExtGroupCont = null; 314cdf0e10cSrcweir 315cdf0e10cSrcweir try { 316cdf0e10cSrcweir Object xGroup=xSetCont.getByName(group); 317cdf0e10cSrcweir xGroupAccess = (XNameReplace) UnoRuntime.queryInterface( 318cdf0e10cSrcweir XNameReplace.class,xGroup); 319cdf0e10cSrcweir } catch(com.sun.star.container.NoSuchElementException e) { 320cdf0e10cSrcweir throw new com.sun.star.uno.Exception( 321cdf0e10cSrcweir "could not get group '" + group + 322cdf0e10cSrcweir "' from set '"+ setName +"':\n" + e.toString()); 323cdf0e10cSrcweir } 324cdf0e10cSrcweir 325cdf0e10cSrcweir try { 326cdf0e10cSrcweir Object xGroup=xGroupAccess.getByName(extGroup); 327cdf0e10cSrcweir xExtGroupCont = (XNameContainer) UnoRuntime.queryInterface( 328cdf0e10cSrcweir XNameContainer.class,xGroup); 329cdf0e10cSrcweir } catch(com.sun.star.container.NoSuchElementException e) { 330cdf0e10cSrcweir throw new com.sun.star.uno.Exception( 331cdf0e10cSrcweir "could not get extensilbe group '"+extGroup+ 332cdf0e10cSrcweir "' from group '"+ group + 333cdf0e10cSrcweir "' from set '"+ setName +"':\n" + e.toString()); 334cdf0e10cSrcweir } 335cdf0e10cSrcweir 336cdf0e10cSrcweir try { 337cdf0e10cSrcweir xExtGroupCont.insertByName(propName, propValue); 338cdf0e10cSrcweir } 339cdf0e10cSrcweir catch(com.sun.star.container.ElementExistException e) { 340cdf0e10cSrcweir xExtGroupCont .replaceByName(propName, propValue); 341cdf0e10cSrcweir } 342cdf0e10cSrcweir 343cdf0e10cSrcweir } 344cdf0e10cSrcweir 345cdf0e10cSrcweir 346cdf0e10cSrcweir /** 347cdf0e10cSrcweir * Returns a <CODE>XNameContainer</CODE> of the <CODE>Set</CODE> 348cdf0e10cSrcweir * of the <CODE>Configuration</CODE> 349cdf0e10cSrcweir * @param setName the name of the Set which sould be returned 350cdf0e10cSrcweir * @throws com.sun.star.uno.Exception on any error 351cdf0e10cSrcweir * @return A XNameContainer of the Set 352cdf0e10cSrcweir */ 353cdf0e10cSrcweir public XNameContainer getSet(String setName) 354cdf0e10cSrcweir throws com.sun.star.uno.Exception 355cdf0e10cSrcweir { 356cdf0e10cSrcweir XNameReplace xCont = (XNameReplace) 357cdf0e10cSrcweir UnoRuntime.queryInterface(XNameReplace.class, m_xConfig); 358cdf0e10cSrcweir 359cdf0e10cSrcweir Object oSet = xCont.getByName(setName); 360cdf0e10cSrcweir 361cdf0e10cSrcweir if (oSet == null) 362cdf0e10cSrcweir throw new com.sun.star.uno.Exception( 363cdf0e10cSrcweir "could not get set '" + setName + ": null"); 364cdf0e10cSrcweir 365cdf0e10cSrcweir return (XNameContainer) UnoRuntime.queryInterface( 366cdf0e10cSrcweir XNameContainer.class, oSet); 367cdf0e10cSrcweir 368cdf0e10cSrcweir } 369cdf0e10cSrcweir } 370