1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ConfigurationRead.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 import com
.sun
.star
.lang
.XMultiServiceFactory
;
33 import com
.sun
.star
.container
.XHierarchicalNameAccess
;
34 import com
.sun
.star
.container
.XNameAccess
;
35 import com
.sun
.star
.container
.NoSuchElementException
;
36 import com
.sun
.star
.beans
.PropertyValue
;
37 import com
.sun
.star
.beans
.PropertyState
;
38 import com
.sun
.star
.uno
.UnoRuntime
;
41 * Read configuration settings.
43 public class ConfigurationRead
{
45 XHierarchicalNameAccess root
= null;
48 * Creates new ConfigurationRead
49 * @param xMSF An instance of service
50 * "com.sun.star.configuration.ConfigurationProvider"
51 * @param rootnode The root of the configuration nodes.
53 public ConfigurationRead(XMultiServiceFactory xMSF
, String rootnode
) {
55 PropertyValue
[] nodeArgs
= new PropertyValue
[1];
56 PropertyValue nodepath
= new PropertyValue();
57 nodepath
.Name
= "nodepath";
58 nodepath
.Value
= rootnode
;
60 nodepath
.State
= PropertyState
.DEFAULT_VALUE
;
64 Object rootObject
= xMSF
.createInstanceWithArguments(
65 "com.sun.star.configuration.ConfigurationAccess",
68 root
= (XHierarchicalNameAccess
)
69 UnoRuntime
.queryInterface(
70 XHierarchicalNameAccess
.class, rootObject
);
72 catch(com
.sun
.star
.uno
.Exception e
) {
78 * Creates new ConfigurationRead. This uses "org.openoffice.Setup"
79 * as default root name.
80 * @param xMSF An instance of service
81 * "com.sun.star.configuration.ConfigurationProvider"
83 public ConfigurationRead(XMultiServiceFactory xMSF
) {
84 this(xMSF
, "org.openoffice.Setup");
88 * Does the node with this hierarchical name exist?
89 * @param name The hierarchical name of a subnode.
90 * @return True, if the node exists.
92 public boolean hasByHieracrhicalName(String name
) throws NoSuchElementException
,
93 com
.sun
.star
.lang
.WrappedTargetException
{
95 return root
.hasByHierarchicalName(name
);
101 * Get the elements of the root node.
102 * @return All elements of the root node.
104 public String
[] getRootNodeNames() {
106 XNameAccess xName
= (XNameAccess
)
107 UnoRuntime
.queryInterface(XNameAccess
.class, root
);
108 String
[]names
= xName
.getElementNames();
113 * Get all elements of this node
114 * @param name The name of the node
115 * @return All elements of this node (as hierarchical names).
117 public String
[] getSubNodeNames(String name
) {
118 String
[]names
= null;
121 Object next
= root
.getByHierarchicalName(name
);
122 XNameAccess x
= (XNameAccess
)UnoRuntime
.queryInterface(
123 XNameAccess
.class, next
);
124 names
= x
.getElementNames();
125 for (int i
=0; i
< names
.length
; i
++) {
126 names
[i
] = name
+ "/" + names
[i
];
130 //just return null, if there are no further nodes
136 * Get contents of a node by its hierarchical name.
137 * @param The hierarchical name of the node.
138 * @return The contents as an object
140 public Object
getByHierarchicalName(String name
) throws NoSuchElementException
{
141 return root
.getByHierarchicalName(name
);