1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
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
;
38 * Read configuration settings.
40 public class ConfigurationRead
{
42 XHierarchicalNameAccess root
= null;
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.
50 public ConfigurationRead(XMultiServiceFactory xMSF
, String rootnode
) {
52 PropertyValue
[] nodeArgs
= new PropertyValue
[1];
53 PropertyValue nodepath
= new PropertyValue();
54 nodepath
.Name
= "nodepath";
55 nodepath
.Value
= rootnode
;
57 nodepath
.State
= PropertyState
.DEFAULT_VALUE
;
61 Object rootObject
= xMSF
.createInstanceWithArguments(
62 "com.sun.star.configuration.ConfigurationAccess",
65 root
= (XHierarchicalNameAccess
)
66 UnoRuntime
.queryInterface(
67 XHierarchicalNameAccess
.class, rootObject
);
69 catch(com
.sun
.star
.uno
.Exception e
) {
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"
80 public ConfigurationRead(XMultiServiceFactory xMSF
) {
81 this(xMSF
, "org.openoffice.Setup");
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.
89 public boolean hasByHieracrhicalName(String name
) throws NoSuchElementException
,
90 com
.sun
.star
.lang
.WrappedTargetException
{
92 return root
.hasByHierarchicalName(name
);
98 * Get the elements of the root node.
99 * @return All elements of the root node.
101 public String
[] getRootNodeNames() {
103 XNameAccess xName
= (XNameAccess
)
104 UnoRuntime
.queryInterface(XNameAccess
.class, root
);
105 String
[]names
= xName
.getElementNames();
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).
114 public String
[] getSubNodeNames(String name
) {
115 String
[]names
= null;
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
];
127 //just return null, if there are no further nodes
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
137 public Object
getByHierarchicalName(String name
) throws NoSuchElementException
{
138 return root
.getByHierarchicalName(name
);