1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package helper;
28 
29 import com.sun.star.lang.XMultiServiceFactory;
30 import com.sun.star.container.XHierarchicalNameAccess;
31 import com.sun.star.container.XNameAccess;
32 import com.sun.star.container.NoSuchElementException;
33 import com.sun.star.beans.PropertyValue;
34 import com.sun.star.beans.PropertyState;
35 import com.sun.star.uno.UnoRuntime;
36 
37 /**
38  * Read configuration settings.
39  */
40 public class ConfigurationRead {
41 
42     XHierarchicalNameAccess root = null;
43 
44     /**
45      * Creates new ConfigurationRead
46      * @param xMSF An instance of service
47      *      "com.sun.star.configuration.ConfigurationProvider"
48      * @param rootnode The root of the configuration nodes.
49      */
50     public ConfigurationRead(XMultiServiceFactory xMSF, String rootnode) {
51 
52         PropertyValue [] nodeArgs = new PropertyValue [1];
53         PropertyValue nodepath = new PropertyValue();
54         nodepath.Name = "nodepath";
55         nodepath.Value = rootnode;
56         nodepath.Handle = -1;
57         nodepath.State = PropertyState.DEFAULT_VALUE;
58         nodeArgs[0]=nodepath;
59 
60         try {
61             Object rootObject = xMSF.createInstanceWithArguments(
62                             "com.sun.star.configuration.ConfigurationAccess",
63                             nodeArgs);
64 
65             root = (XHierarchicalNameAccess)
66                             UnoRuntime.queryInterface(
67                             XHierarchicalNameAccess.class, rootObject);
68         }
69         catch(com.sun.star.uno.Exception e) {
70             e.printStackTrace();
71         }
72     }
73 
74     /**
75      * Creates new ConfigurationRead. This uses "org.openoffice.Setup"
76      * as default root name.
77      * @param xMSF An instance of service
78      *      "com.sun.star.configuration.ConfigurationProvider"
79      */
80     public ConfigurationRead(XMultiServiceFactory xMSF) {
81         this(xMSF, "org.openoffice.Setup");
82     }
83 
84     /**
85      * Does the node with this hierarchical name exist?
86      * @param name The hierarchical name of a subnode.
87      * @return True, if the node exists.
88      */
89     public boolean hasByHieracrhicalName(String name) throws NoSuchElementException,
90                                     com.sun.star.lang.WrappedTargetException {
91 
92         return root.hasByHierarchicalName(name);
93 
94     }
95 
96 
97     /**
98      * Get the elements of the root node.
99      * @return All elements of the root node.
100      */
101     public String[] getRootNodeNames() {
102 
103         XNameAccess xName = (XNameAccess)
104                     UnoRuntime.queryInterface(XNameAccess.class, root);
105         String[]names = xName.getElementNames();
106         return names;
107     }
108 
109     /**
110      * Get all elements of this node
111      * @param name The name of the node
112      * @return All elements of this node (as hierarchical names).
113      */
114     public String[] getSubNodeNames(String name) {
115         String[]names = null;
116         try {
117 
118             Object next = root.getByHierarchicalName(name);
119             XNameAccess x = (XNameAccess)UnoRuntime.queryInterface(
120                                                 XNameAccess.class, next);
121             names = x.getElementNames();
122             for (int i=0; i< names.length; i++) {
123                 names[i] = name + "/" + names[i];
124             }
125         }
126         catch(Exception e) {
127             //just return null, if there are no further nodes
128         }
129         return names;
130     }
131 
132     /**
133      * Get contents of a node by its hierarchical name.
134      * @param The hierarchical name of the node.
135      * @return The contents as an object
136      */
137     public Object getByHierarchicalName(String name) throws NoSuchElementException {
138         return root.getByHierarchicalName(name);
139     }
140 
141 }
142