/* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ package helper; import com.sun.star.uno.*; import com.sun.star.lang.*; import com.sun.star.container.*; import com.sun.star.beans.*; import com.sun.star.util.*; /** * This ConfigHelper makes it possible to access the * configuration and change their content.

*

* Example:

* Listing of the Configuration Views.xcu:

* <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">

* <node oor:name="Windows">

* <<node oor:name="SplitWindow0" oor:op="replace">

* <prop oor:name="Visible" oor:type="xs:boolean">

* <value>false</value>

* </prop>

* <prop oor:name="WindowState" oor:type="xs:string">

* <value/>

* </prop>

* <node oor:name="UserData">

* <prop oor:name="UserItem" oor:op="replace" * oor:type="xs:string">

* <value>V1,2,0</value>

* </prop>

* </node>

* </node>

* </node>

*

* Definition

*

* We assume in the following examples the existence of:

* ConfigHelper aConfig = new ConfigHelper(xMSF, "org.openoffice.Office.Views", false); *

*/ public class ConfigHelper { private final XHierarchicalNameAccess m_xConfig; public ConfigHelper(XMultiServiceFactory xSMGR , String sConfigPath , boolean bReadOnly ) throws com.sun.star.uno.Exception { XMultiServiceFactory xConfigRoot = UnoRuntime.queryInterface( XMultiServiceFactory.class, xSMGR.createInstance("com.sun.star.configuration.ConfigurationProvider")); PropertyValue[] lParams = new PropertyValue[1]; lParams[0] = new PropertyValue(); lParams[0].Name = "nodepath"; lParams[0].Value = sConfigPath; Object aConfig; if (bReadOnly) aConfig = xConfigRoot.createInstanceWithArguments( "com.sun.star.configuration.ConfigurationAccess", lParams); else aConfig = xConfigRoot.createInstanceWithArguments( "com.sun.star.configuration.ConfigurationUpdateAccess", lParams); m_xConfig = UnoRuntime.queryInterface( XHierarchicalNameAccess.class, aConfig); if (m_xConfig == null) throw new com.sun.star.uno.Exception("Could not open configuration \""+sConfigPath+"\""); } /** * Updates the configuration.

* This must be called after you have changed the configuration * else you changes will be lost. */ public void flush() { try { XChangesBatch xBatch = UnoRuntime.queryInterface( XChangesBatch.class, m_xConfig); xBatch.commitChanges(); } catch(com.sun.star.uno.Exception ex) {} } /** * Insert a structured node (group) in a name container (set) * or else update it and return the XNameReplace of it.

* The XSingleServiceFacttory of the set will be used * to create a new group. This group is specific to its set and * creates defined entries. * @return The [inserted] group of the set * @param groupName The name of the goup which should be returned * @param setName The name of the set * @throws com.sun.star.uno.Exception throws * com.sun.star.uno.Exception on any error. */ public XNameReplace getOrInsertGroup(String setName, String groupName) throws com.sun.star.uno.Exception { XNameContainer xSetCont = this.getSet(setName); XNameReplace xChildAccess = null; try { xSetCont.getByName(groupName); xChildAccess = UnoRuntime.queryInterface( XNameReplace.class,xSetCont); } catch(com.sun.star.container.NoSuchElementException e) { // proceed with inserting } if (xChildAccess == null) { XSingleServiceFactory xChildfactory = UnoRuntime.queryInterface(XSingleServiceFactory.class,xSetCont); Object xNewChild = xChildfactory.createInstance(); xSetCont.insertByName(groupName, xNewChild); xChildAccess = UnoRuntime.queryInterface(XNameContainer.class,xNewChild); } return xChildAccess; } /** * Update a property of a group container of a set container * @param setName the name of the set which contains the group * @param groupName the name of the group which property should be changed * @param propName the name of the property which should be changed * @param propValue the value the property should get * @throws com.sun.star.uno.Exception throws com.sun.star.uno.Exception on any error. */ public void updateGroupProperty(String setName, String groupName, String propName, Object propValue) throws com.sun.star.uno.Exception { XNameContainer xSetCont = this.getSet(setName); XPropertySet xProp = null; try { xProp = UnoRuntime.queryInterface( XPropertySet.class, xSetCont.getByName(groupName)); } catch (com.sun.star.container.NoSuchElementException ex){ throw new com.sun.star.uno.Exception(ex, "could not get group '" + groupName + "' from set '"+ setName +"'"); } try{ xProp.setPropertyValue(propName, propValue); } catch (com.sun.star.uno.Exception ex) { throw new com.sun.star.uno.Exception(ex, "could not set property '" + propName + "' from group '"+ groupName + "' from set '"+ setName +"'"); } } /** * Insert a property in an extensible group container or else update it * @param setName the name of the set which contains the group * @param group The name of the group which contains the extensible group. * @param extGroup The name of the extensible group which * [should] contain the property * @param propName The name of the property. * @param propValue The value of the property. * @throws com.sun.star.uno.Exception throws com.sun.star.uno.Exception on any error. */ public void insertOrUpdateExtensibleGroupProperty( String setName, String group, String extGroup, String propName, Object propValue) throws com.sun.star.uno.Exception { XNameContainer xSetCont = this.getSet(setName); XNameReplace xGroupAccess = null; XNameContainer xExtGroupCont = null; try { Object xGroup=xSetCont.getByName(group); xGroupAccess = UnoRuntime.queryInterface( XNameReplace.class,xGroup); } catch(com.sun.star.container.NoSuchElementException ex) { throw new com.sun.star.uno.Exception(ex, "could not get group '" + group + "' from set '"+ setName +"'"); } try { Object xGroup=xGroupAccess.getByName(extGroup); xExtGroupCont = UnoRuntime.queryInterface( XNameContainer.class,xGroup); } catch(com.sun.star.container.NoSuchElementException ex) { throw new com.sun.star.uno.Exception(ex, "could not get extensible group '"+extGroup+ "' from group '"+ group + "' from set '"+ setName +"'"); } try { xExtGroupCont.insertByName(propName, propValue); } catch(com.sun.star.container.ElementExistException e) { xExtGroupCont.replaceByName(propName, propValue); } } /** * Returns a XNameContainer of the Set * of the Configuration * @param setName the name of the Set which should be returned * @throws com.sun.star.uno.Exception on any error * @return A XNameContainer of the Set */ private XNameContainer getSet(String setName) throws com.sun.star.uno.Exception { XNameReplace xCont = UnoRuntime.queryInterface(XNameReplace.class, m_xConfig); Object oSet = xCont.getByName(setName); if (oSet == null) throw new com.sun.star.uno.Exception( "could not get set '" + setName + ": null"); return UnoRuntime.queryInterface( XNameContainer.class, oSet); } }