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