1ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
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 
22ef39d40dSAndrew Rist 
23cdf0e10cSrcweir package helper;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
26cdf0e10cSrcweir import com.sun.star.container.XHierarchicalNameAccess;
27cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
28cdf0e10cSrcweir import com.sun.star.container.NoSuchElementException;
29cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
30cdf0e10cSrcweir import com.sun.star.beans.PropertyState;
31cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir /**
34cdf0e10cSrcweir  * Read configuration settings.
35cdf0e10cSrcweir  */
36cdf0e10cSrcweir public class ConfigurationRead {
37cdf0e10cSrcweir 
38cdf0e10cSrcweir     XHierarchicalNameAccess root = null;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir     /**
41cdf0e10cSrcweir      * Creates new ConfigurationRead
42cdf0e10cSrcweir      * @param xMSF An instance of service
43cdf0e10cSrcweir      *      "com.sun.star.configuration.ConfigurationProvider"
44cdf0e10cSrcweir      * @param rootnode The root of the configuration nodes.
45cdf0e10cSrcweir      */
ConfigurationRead(XMultiServiceFactory xMSF, String rootnode)46cdf0e10cSrcweir     public ConfigurationRead(XMultiServiceFactory xMSF, String rootnode) {
47cdf0e10cSrcweir 
48cdf0e10cSrcweir         PropertyValue [] nodeArgs = new PropertyValue [1];
49cdf0e10cSrcweir         PropertyValue nodepath = new PropertyValue();
50cdf0e10cSrcweir         nodepath.Name = "nodepath";
51cdf0e10cSrcweir         nodepath.Value = rootnode;
52cdf0e10cSrcweir         nodepath.Handle = -1;
53cdf0e10cSrcweir         nodepath.State = PropertyState.DEFAULT_VALUE;
54cdf0e10cSrcweir         nodeArgs[0]=nodepath;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir         try {
57cdf0e10cSrcweir             Object rootObject = xMSF.createInstanceWithArguments(
58cdf0e10cSrcweir                             "com.sun.star.configuration.ConfigurationAccess",
59cdf0e10cSrcweir                             nodeArgs);
60cdf0e10cSrcweir 
61cdf0e10cSrcweir             root = (XHierarchicalNameAccess)
62cdf0e10cSrcweir                             UnoRuntime.queryInterface(
63cdf0e10cSrcweir                             XHierarchicalNameAccess.class, rootObject);
64cdf0e10cSrcweir         }
65cdf0e10cSrcweir         catch(com.sun.star.uno.Exception e) {
66cdf0e10cSrcweir             e.printStackTrace();
67cdf0e10cSrcweir         }
68cdf0e10cSrcweir     }
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     /**
71cdf0e10cSrcweir      * Creates new ConfigurationRead. This uses "org.openoffice.Setup"
72cdf0e10cSrcweir      * as default root name.
73cdf0e10cSrcweir      * @param xMSF An instance of service
74cdf0e10cSrcweir      *      "com.sun.star.configuration.ConfigurationProvider"
75cdf0e10cSrcweir      */
ConfigurationRead(XMultiServiceFactory xMSF)76cdf0e10cSrcweir     public ConfigurationRead(XMultiServiceFactory xMSF) {
77cdf0e10cSrcweir         this(xMSF, "org.openoffice.Setup");
78cdf0e10cSrcweir     }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     /**
81cdf0e10cSrcweir      * Does the node with this hierarchical name exist?
82cdf0e10cSrcweir      * @param name The hierarchical name of a subnode.
83cdf0e10cSrcweir      * @return True, if the node exists.
84cdf0e10cSrcweir      */
hasByHieracrhicalName(String name)85cdf0e10cSrcweir     public boolean hasByHieracrhicalName(String name) throws NoSuchElementException,
86cdf0e10cSrcweir                                     com.sun.star.lang.WrappedTargetException {
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         return root.hasByHierarchicalName(name);
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     /**
94cdf0e10cSrcweir      * Get the elements of the root node.
95cdf0e10cSrcweir      * @return All elements of the root node.
96cdf0e10cSrcweir      */
getRootNodeNames()97cdf0e10cSrcweir     public String[] getRootNodeNames() {
98cdf0e10cSrcweir 
99cdf0e10cSrcweir         XNameAccess xName = (XNameAccess)
100cdf0e10cSrcweir                     UnoRuntime.queryInterface(XNameAccess.class, root);
101cdf0e10cSrcweir         String[]names = xName.getElementNames();
102cdf0e10cSrcweir         return names;
103cdf0e10cSrcweir     }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     /**
106cdf0e10cSrcweir      * Get all elements of this node
107cdf0e10cSrcweir      * @param name The name of the node
108cdf0e10cSrcweir      * @return All elements of this node (as hierarchical names).
109cdf0e10cSrcweir      */
getSubNodeNames(String name)110cdf0e10cSrcweir     public String[] getSubNodeNames(String name) {
111cdf0e10cSrcweir         String[]names = null;
112cdf0e10cSrcweir         try {
113cdf0e10cSrcweir 
114cdf0e10cSrcweir             Object next = root.getByHierarchicalName(name);
115cdf0e10cSrcweir             XNameAccess x = (XNameAccess)UnoRuntime.queryInterface(
116cdf0e10cSrcweir                                                 XNameAccess.class, next);
117cdf0e10cSrcweir             names = x.getElementNames();
118cdf0e10cSrcweir             for (int i=0; i< names.length; i++) {
119cdf0e10cSrcweir                 names[i] = name + "/" + names[i];
120cdf0e10cSrcweir             }
121cdf0e10cSrcweir         }
122cdf0e10cSrcweir         catch(Exception e) {
123cdf0e10cSrcweir             //just return null, if there are no further nodes
124cdf0e10cSrcweir         }
125cdf0e10cSrcweir         return names;
126cdf0e10cSrcweir     }
127cdf0e10cSrcweir 
128cdf0e10cSrcweir     /**
129cdf0e10cSrcweir      * Get contents of a node by its hierarchical name.
130*e6b649b5SPedro Giffuni      * @param name The hierarchical name of the node.
131cdf0e10cSrcweir      * @return The contents as an object
132cdf0e10cSrcweir      */
getByHierarchicalName(String name)133cdf0e10cSrcweir     public Object getByHierarchicalName(String name) throws NoSuchElementException {
134cdf0e10cSrcweir         return root.getByHierarchicalName(name);
135cdf0e10cSrcweir     }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir }
138