2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 import com
.sun
.star
.lang
.XMultiServiceFactory
;
21 import com
.sun
.star
.container
.XHierarchicalNameAccess
;
22 import com
.sun
.star
.container
.XNameAccess
;
23 import com
.sun
.star
.container
.NoSuchElementException
;
24 import com
.sun
.star
.beans
.PropertyValue
;
25 import com
.sun
.star
.beans
.PropertyState
;
26 import com
.sun
.star
.uno
.UnoRuntime
;
29 * Read configuration settings.
31 public class ConfigurationRead
{
33 XHierarchicalNameAccess root
= null;
36 * Creates new ConfigurationRead
37 * @param xMSF An instance of service
38 * "com.sun.star.configuration.ConfigurationProvider"
39 * @param rootnode The root of the configuration nodes.
41 public ConfigurationRead(XMultiServiceFactory xMSF
, String rootnode
) {
43 PropertyValue
[] nodeArgs
= new PropertyValue
[1];
44 PropertyValue nodepath
= new PropertyValue();
45 nodepath
.Name
= "nodepath";
46 nodepath
.Value
= rootnode
;
48 nodepath
.State
= PropertyState
.DEFAULT_VALUE
;
52 Object rootObject
= xMSF
.createInstanceWithArguments(
53 "com.sun.star.configuration.ConfigurationAccess",
56 root
= UnoRuntime
.queryInterface(
57 XHierarchicalNameAccess
.class, rootObject
);
59 catch(com
.sun
.star
.uno
.Exception e
) {
65 * Creates new ConfigurationRead. This uses "org.openoffice.Setup"
66 * as default root name.
67 * @param xMSF An instance of service
68 * "com.sun.star.configuration.ConfigurationProvider"
70 public ConfigurationRead(XMultiServiceFactory xMSF
) {
71 this(xMSF
, "org.openoffice.Setup");
75 * Does the node with this hierarchical name exist?
76 * @param name The hierarchical name of a subnode.
77 * @return True, if the node exists.
79 public boolean hasByHieracrhicalName(String name
) throws NoSuchElementException
,
80 com
.sun
.star
.lang
.WrappedTargetException
{
82 return root
.hasByHierarchicalName(name
);
88 * Get the elements of the root node.
89 * @return All elements of the root node.
91 public String
[] getRootNodeNames() {
93 XNameAccess xName
= UnoRuntime
.queryInterface(XNameAccess
.class, root
);
94 String
[]names
= xName
.getElementNames();
99 * Get all elements of this node
100 * @param name The name of the node
101 * @return All elements of this node (as hierarchical names).
103 public String
[] getSubNodeNames(String name
) {
104 String
[]names
= null;
107 Object next
= root
.getByHierarchicalName(name
);
108 XNameAccess x
= UnoRuntime
.queryInterface(
109 XNameAccess
.class, next
);
110 names
= x
.getElementNames();
111 for (int i
=0; i
< names
.length
; i
++) {
112 names
[i
] = name
+ "/" + names
[i
];
116 //just return null, if there are no further nodes
122 * Get contents of a node by its hierarchical name.
123 * @param name The hierarchical name of the node.
124 * @return The contents as an object
126 public Object
getByHierarchicalName(String name
) throws NoSuchElementException
{
127 return root
.getByHierarchicalName(name
);