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 ************************************************************************/
30 // access the implementations via names
31 import com
.sun
.star
.uno
.UnoRuntime
;
32 import java
.io
.PrintWriter
;
34 import com
.sun
.star
.registry
.XRegistryKey
;
35 import com
.sun
.star
.registry
.XSimpleRegistry
;
36 import com
.sun
.star
.registry
.RegistryKeyType
;
37 import com
.sun
.star
.registry
.RegistryValueType
;
38 import com
.sun
.star
.registry
.InvalidRegistryException
;
39 import com
.sun
.star
.lang
.XMultiServiceFactory
;
40 import com
.sun
.star
.uno
.Exception
;
42 public class RegistryTools
{
45 * Creates 'com.sun.star.registry.SimpleRegistry'
47 * @param xMSF Multiservice factory.
48 * @return Service created.
50 public static XSimpleRegistry createRegistryService
51 (XMultiServiceFactory xMSF
) throws com
.sun
.star
.uno
.Exception
{
53 Object oInterface
= xMSF
.createInstance
54 ("com.sun.star.registry.SimpleRegistry");
55 return (XSimpleRegistry
) UnoRuntime
.queryInterface (
56 XSimpleRegistry
.class, oInterface
) ;
60 * Opens registry file for reading/writing. If file doesn't
61 * exist a new one created.
62 * @param file Registry file name.
63 * @param xMSF Multiservice factory.
64 * @return Opened registry.
66 public static XSimpleRegistry openRegistry
67 (String file
, XMultiServiceFactory xMSF
)
68 throws com
.sun
.star
.uno
.Exception
{
70 XSimpleRegistry reg
= createRegistryService(xMSF
) ;
72 reg
.open(file
, false, true) ;
78 * Compares two registry keys, their names, value
80 * return <code>true</code> if key names, value types
81 * and values are equal, else returns <code>false</code>.
83 public static boolean compareKeys
84 (XRegistryKey key1
, XRegistryKey key2
) {
86 if (key1
== null || key2
== null ||
87 !key1
.isValid() || !key2
.isValid())
91 String keyName1
= getShortKeyName(key1
.getKeyName()) ;
92 String keyName2
= getShortKeyName(key2
.getKeyName()) ;
94 if (!keyName1
.equals(keyName2
)) return false ;
97 if (key1
.getValueType() != key2
.getValueType()) return false ;
98 } catch (InvalidRegistryException e
) {
102 RegistryValueType type
;
104 type
= key1
.getValueType() ;
106 if (type
.equals(RegistryValueType
.ASCII
)) {
107 if (!key1
.getAsciiValue().equals(key2
.getAsciiValue()))
110 if (type
.equals(RegistryValueType
.STRING
)) {
111 if (!key1
.getStringValue().equals(key2
.getStringValue()))
114 if (type
.equals(RegistryValueType
.LONG
)) {
115 if (key1
.getLongValue() != key2
.getLongValue())
118 if (type
.equals(RegistryValueType
.BINARY
)) {
119 byte[] bin1
= key1
.getBinaryValue() ;
120 byte[] bin2
= key2
.getBinaryValue() ;
121 if (bin1
.length
!= bin2
.length
)
123 for (int i
= 0; i
< bin1
.length
; i
++)
124 if (bin1
[i
] != bin2
[i
]) return false ;
126 if (type
.equals(RegistryValueType
.ASCIILIST
)) {
127 String
[] list1
= key1
.getAsciiListValue() ;
128 String
[] list2
= key2
.getAsciiListValue() ;
129 if (list1
.length
!= list2
.length
)
131 for (int i
= 0; i
< list1
.length
; i
++)
132 if (!list1
[i
].equals(list2
[i
])) return false ;
134 if (type
.equals(RegistryValueType
.STRINGLIST
)) {
135 String
[] list1
= key1
.getStringListValue() ;
136 String
[] list2
= key2
.getStringListValue() ;
137 if (list1
.length
!= list2
.length
)
139 for (int i
= 0; i
< list1
.length
; i
++)
140 if (!list1
[i
].equals(list2
[i
])) return false ;
142 if (type
.equals(RegistryValueType
.LONGLIST
)) {
143 int[] list1
= key1
.getLongListValue() ;
144 int[] list2
= key2
.getLongListValue() ;
145 if (list1
.length
!= list2
.length
)
147 for (int i
= 0; i
< list1
.length
; i
++)
148 if (list1
[i
] != list2
[i
]) return false ;
150 } catch (Exception e
) {
158 * Gets name of the key relative to its parent.
159 * For example if full name of key is '/key1/subkey'
160 * short key name is 'subkey'
161 * @param keyName Full key name.
162 * @return Short key name.
164 public static String
getShortKeyName(String keyName
) {
165 if (keyName
== null) return null ;
166 int idx
= keyName
.lastIndexOf("/") ;
167 if (idx
< 0) return keyName
;
168 else return keyName
.substring(idx
+ 1) ;
172 * Compare all child keys.
173 * @param compareRoot If <code>true</code> method also
174 * compare root keys, if <code>false</code> it begins recursive
175 * comparing from children of root keys.
176 * @return <code>true</code> if keys and their sub keys are equal.
178 protected static boolean compareKeyTrees
179 (XRegistryKey tree1
, XRegistryKey tree2
, boolean compareRoot
) {
181 if (compareRoot
&& !compareKeys(tree1
, tree2
)) return false ;
184 String
[] keyNames1
= tree1
.getKeyNames() ;
185 String
[] keyNames2
= tree2
.getKeyNames() ;
187 if (keyNames1
== null && keyNames2
== null) return true ;
189 if (keyNames1
== null || keyNames2
== null ||
190 keyNames2
.length
!= keyNames1
.length
)
193 for (int i
= 0; i
< keyNames1
.length
; i
++) {
195 String keyName
= getShortKeyName(keyNames1
[i
]) ;
196 XRegistryKey key2
= tree2
.openKey(keyName
) ;
199 // key with the same name doesn't exist in the second tree
202 if (!tree1
.getKeyType(keyName
).equals(
203 tree2
.getKeyType(keyName
)))
206 if (tree1
.getKeyType(keyName
).equals(
207 RegistryKeyType
.LINK
)) {
209 if (!getShortKeyName(tree1
.getLinkTarget(keyName
)).equals(
210 getShortKeyName(tree2
.getLinkTarget(keyName
))))
215 if (compareKeyTrees(tree1
.openKey(keyName
),
216 tree2
.openKey(keyName
), true) == false) return false ;
219 } catch (InvalidRegistryException e
) {
227 * Compare keys specified and all their child keys.
228 * @return <code>true</code> if keys and their sub keys are equal.
230 public static boolean compareKeyTrees
231 (XRegistryKey tree1
, XRegistryKey tree2
) {
233 return compareKeyTrees(tree1
, tree2
, false) ;
237 * Prints to a specified output about all keys and subkeys information
238 * (key name, type, value, link target, attributes) recursively.
239 * @param reg Registry for which information is needed.
240 * @param out Output stream.
242 public static void printRegistryInfo(XSimpleRegistry reg
, PrintWriter out
) {
244 printRegistryInfo(reg
.getRootKey(), out
) ;
245 } catch (com
.sun
.star
.registry
.InvalidRegistryException e
) {
246 out
.println("!!! Can't open root registry key for info printing") ;
251 * Prints to a specified output about all keys and subkeys information
252 * (key name, type, value, link target, attributes) recursively.
253 * @param root Key for which subkeys (and further) information is required.
254 * @param out Output stream.
256 public static void printRegistryInfo(XRegistryKey root
, PrintWriter out
) {
258 out
.println("/(null)") ;
264 printTreeInfo(root
, out
, " ") ;
265 } catch (com
.sun
.star
.registry
.InvalidRegistryException e
) {
266 out
.println("Exception accessing registry :") ;
267 e
.printStackTrace(out
) ;
271 private static void printTreeInfo(XRegistryKey key
,
272 PrintWriter out
, String margin
)
273 throws com
.sun
.star
.registry
.InvalidRegistryException
{
275 String
[] subKeys
= key
.getKeyNames() ;
277 if (subKeys
== null || subKeys
.length
== 0) return ;
279 for (int i
= 0; i
< subKeys
.length
; i
++) {
280 printKeyInfo(key
, subKeys
[i
], out
, margin
) ;
281 XRegistryKey subKey
= key
.openKey
282 (getShortKeyName(subKeys
[i
])) ;
283 printTreeInfo(subKey
, out
, margin
+ " ") ;
288 private static void printKeyInfo(XRegistryKey parentKey
,
289 String keyName
, PrintWriter out
, String margin
)
290 throws com
.sun
.star
.registry
.InvalidRegistryException
{
293 keyName
= getShortKeyName(keyName
) ;
294 XRegistryKey key
= parentKey
.openKey(keyName
) ;
296 out
.print("/" + getShortKeyName(key
.getKeyName()) + " ") ;
298 out
.println("(null)") ;
302 if (!key
.isValid()) {
303 out
.println("(not valid)") ;
307 if (key
.isReadOnly()) {
308 out
.print("(read only) ") ;
311 if (parentKey
.getKeyType(keyName
) == RegistryKeyType
.LINK
) {
312 out
.println("(link to " + parentKey
.getLinkTarget(keyName
) + ")") ;
316 RegistryValueType type
;
318 type
= key
.getValueType() ;
320 if (type
.equals(RegistryValueType
.ASCII
)) {
321 out
.println("[ASCII] = '" + key
.getAsciiValue() + "'") ;
323 if (type
.equals(RegistryValueType
.STRING
)) {
324 out
.println("[STRING] = '" + key
.getStringValue() + "'") ;
326 if (type
.equals(RegistryValueType
.LONG
)) {
327 out
.println("[LONG] = " + key
.getLongValue()) ;
329 if (type
.equals(RegistryValueType
.BINARY
)) {
330 out
.print("[BINARY] = {") ;
331 byte[] bin
= key
.getBinaryValue() ;
332 for (int i
= 0; i
< bin
.length
; i
++)
333 out
.print("" + bin
[i
] + ",") ;
336 if (type
.equals(RegistryValueType
.ASCIILIST
)) {
337 out
.print("[ASCIILIST] = {") ;
338 String
[] list
= key
.getAsciiListValue() ;
339 for (int i
= 0; i
< list
.length
; i
++)
340 out
.print("'" + list
[i
] + "',") ;
343 if (type
.equals(RegistryValueType
.STRINGLIST
)) {
344 out
.print("[STRINGLIST] = {") ;
345 String
[] list
= key
.getStringListValue() ;
346 for (int i
= 0; i
< list
.length
; i
++)
347 out
.print("'" + list
[i
] + "',") ;
350 if (type
.equals(RegistryValueType
.LONGLIST
)) {
351 out
.print("[LONGLIST] = {") ;
352 int[] list
= key
.getLongListValue() ;
353 for (int i
= 0; i
< list
.length
; i
++)
354 out
.print("" + list
[i
] + ",") ;
359 } catch (com
.sun
.star
.uno
.Exception e
) {
360 out
.println("Exception occurred : ") ;
361 e
.printStackTrace(out
) ;
368 // public static void compareKeyTrees