1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package helper;
24 
25 import com.sun.star.lang.XMultiServiceFactory;
26 import com.sun.star.container.XHierarchicalNameAccess;
27 import com.sun.star.container.XNameAccess;
28 import com.sun.star.container.NoSuchElementException;
29 import com.sun.star.beans.PropertyValue;
30 import com.sun.star.beans.PropertyState;
31 import com.sun.star.uno.UnoRuntime;
32 
33 /**
34  * Read configuration settings.
35  */
36 public class ConfigurationRead {
37 
38     XHierarchicalNameAccess root = null;
39 
40     /**
41      * Creates new ConfigurationRead
42      * @param xMSF An instance of service
43      *      "com.sun.star.configuration.ConfigurationProvider"
44      * @param rootnode The root of the configuration nodes.
45      */
ConfigurationRead(XMultiServiceFactory xMSF, String rootnode)46     public ConfigurationRead(XMultiServiceFactory xMSF, String rootnode) {
47 
48         PropertyValue [] nodeArgs = new PropertyValue [1];
49         PropertyValue nodepath = new PropertyValue();
50         nodepath.Name = "nodepath";
51         nodepath.Value = rootnode;
52         nodepath.Handle = -1;
53         nodepath.State = PropertyState.DEFAULT_VALUE;
54         nodeArgs[0]=nodepath;
55 
56         try {
57             Object rootObject = xMSF.createInstanceWithArguments(
58                             "com.sun.star.configuration.ConfigurationAccess",
59                             nodeArgs);
60 
61             root = (XHierarchicalNameAccess)
62                             UnoRuntime.queryInterface(
63                             XHierarchicalNameAccess.class, rootObject);
64         }
65         catch(com.sun.star.uno.Exception e) {
66             e.printStackTrace();
67         }
68     }
69 
70     /**
71      * Creates new ConfigurationRead. This uses "org.openoffice.Setup"
72      * as default root name.
73      * @param xMSF An instance of service
74      *      "com.sun.star.configuration.ConfigurationProvider"
75      */
ConfigurationRead(XMultiServiceFactory xMSF)76     public ConfigurationRead(XMultiServiceFactory xMSF) {
77         this(xMSF, "org.openoffice.Setup");
78     }
79 
80     /**
81      * Does the node with this hierarchical name exist?
82      * @param name The hierarchical name of a subnode.
83      * @return True, if the node exists.
84      */
hasByHieracrhicalName(String name)85     public boolean hasByHieracrhicalName(String name) throws NoSuchElementException,
86                                     com.sun.star.lang.WrappedTargetException {
87 
88         return root.hasByHierarchicalName(name);
89 
90     }
91 
92 
93     /**
94      * Get the elements of the root node.
95      * @return All elements of the root node.
96      */
getRootNodeNames()97     public String[] getRootNodeNames() {
98 
99         XNameAccess xName = (XNameAccess)
100                     UnoRuntime.queryInterface(XNameAccess.class, root);
101         String[]names = xName.getElementNames();
102         return names;
103     }
104 
105     /**
106      * Get all elements of this node
107      * @param name The name of the node
108      * @return All elements of this node (as hierarchical names).
109      */
getSubNodeNames(String name)110     public String[] getSubNodeNames(String name) {
111         String[]names = null;
112         try {
113 
114             Object next = root.getByHierarchicalName(name);
115             XNameAccess x = (XNameAccess)UnoRuntime.queryInterface(
116                                                 XNameAccess.class, next);
117             names = x.getElementNames();
118             for (int i=0; i< names.length; i++) {
119                 names[i] = name + "/" + names[i];
120             }
121         }
122         catch(Exception e) {
123             //just return null, if there are no further nodes
124         }
125         return names;
126     }
127 
128     /**
129      * Get contents of a node by its hierarchical name.
130      * @param name The hierarchical name of the node.
131      * @return The contents as an object
132      */
getByHierarchicalName(String name)133     public Object getByHierarchicalName(String name) throws NoSuchElementException {
134         return root.getByHierarchicalName(name);
135     }
136 
137 }
138