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: RegistryTools.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 ************************************************************************/
33 // access the implementations via names
34 import com
.sun
.star
.uno
.UnoRuntime
;
35 import java
.io
.PrintWriter
;
37 import com
.sun
.star
.registry
.XRegistryKey
;
38 import com
.sun
.star
.registry
.XSimpleRegistry
;
39 import com
.sun
.star
.registry
.RegistryKeyType
;
40 import com
.sun
.star
.registry
.RegistryValueType
;
41 import com
.sun
.star
.registry
.InvalidRegistryException
;
42 import com
.sun
.star
.lang
.XMultiServiceFactory
;
43 import com
.sun
.star
.uno
.Exception
;
45 public class RegistryTools
{
48 * Creates 'com.sun.star.registry.SimpleRegistry'
50 * @param xMSF Multiservice factory.
51 * @return Service created.
53 public static XSimpleRegistry createRegistryService
54 (XMultiServiceFactory xMSF
) throws com
.sun
.star
.uno
.Exception
{
56 Object oInterface
= xMSF
.createInstance
57 ("com.sun.star.registry.SimpleRegistry");
58 return (XSimpleRegistry
) UnoRuntime
.queryInterface (
59 XSimpleRegistry
.class, oInterface
) ;
63 * Opens registry file for reading/writing. If file doesn't
64 * exist a new one created.
65 * @param file Registry file name.
66 * @param xMSF Multiservice factory.
67 * @return Opened registry.
69 public static XSimpleRegistry openRegistry
70 (String file
, XMultiServiceFactory xMSF
)
71 throws com
.sun
.star
.uno
.Exception
{
73 XSimpleRegistry reg
= createRegistryService(xMSF
) ;
75 reg
.open(file
, false, true) ;
81 * Compares two registry keys, their names, value
83 * return <code>true</code> if key names, value types
84 * and values are equal, else returns <code>false</code>.
86 public static boolean compareKeys
87 (XRegistryKey key1
, XRegistryKey key2
) {
89 if (key1
== null || key2
== null ||
90 !key1
.isValid() || !key2
.isValid())
94 String keyName1
= getShortKeyName(key1
.getKeyName()) ;
95 String keyName2
= getShortKeyName(key2
.getKeyName()) ;
97 if (!keyName1
.equals(keyName2
)) return false ;
100 if (key1
.getValueType() != key2
.getValueType()) return false ;
101 } catch (InvalidRegistryException e
) {
105 RegistryValueType type
;
107 type
= key1
.getValueType() ;
109 if (type
.equals(RegistryValueType
.ASCII
)) {
110 if (!key1
.getAsciiValue().equals(key2
.getAsciiValue()))
113 if (type
.equals(RegistryValueType
.STRING
)) {
114 if (!key1
.getStringValue().equals(key2
.getStringValue()))
117 if (type
.equals(RegistryValueType
.LONG
)) {
118 if (key1
.getLongValue() != key2
.getLongValue())
121 if (type
.equals(RegistryValueType
.BINARY
)) {
122 byte[] bin1
= key1
.getBinaryValue() ;
123 byte[] bin2
= key2
.getBinaryValue() ;
124 if (bin1
.length
!= bin2
.length
)
126 for (int i
= 0; i
< bin1
.length
; i
++)
127 if (bin1
[i
] != bin2
[i
]) return false ;
129 if (type
.equals(RegistryValueType
.ASCIILIST
)) {
130 String
[] list1
= key1
.getAsciiListValue() ;
131 String
[] list2
= key2
.getAsciiListValue() ;
132 if (list1
.length
!= list2
.length
)
134 for (int i
= 0; i
< list1
.length
; i
++)
135 if (!list1
[i
].equals(list2
[i
])) return false ;
137 if (type
.equals(RegistryValueType
.STRINGLIST
)) {
138 String
[] list1
= key1
.getStringListValue() ;
139 String
[] list2
= key2
.getStringListValue() ;
140 if (list1
.length
!= list2
.length
)
142 for (int i
= 0; i
< list1
.length
; i
++)
143 if (!list1
[i
].equals(list2
[i
])) return false ;
145 if (type
.equals(RegistryValueType
.LONGLIST
)) {
146 int[] list1
= key1
.getLongListValue() ;
147 int[] list2
= key2
.getLongListValue() ;
148 if (list1
.length
!= list2
.length
)
150 for (int i
= 0; i
< list1
.length
; i
++)
151 if (list1
[i
] != list2
[i
]) return false ;
153 } catch (Exception e
) {
161 * Gets name of the key relative to its parent.
162 * For example if full name of key is '/key1/subkey'
163 * short key name is 'subkey'
164 * @param keyName Full key name.
165 * @return Short key name.
167 public static String
getShortKeyName(String keyName
) {
168 if (keyName
== null) return null ;
169 int idx
= keyName
.lastIndexOf("/") ;
170 if (idx
< 0) return keyName
;
171 else return keyName
.substring(idx
+ 1) ;
175 * Compare all child keys.
176 * @param compareRoot If <code>true</code> method also
177 * compare root keys, if <code>false</code> it begins recursive
178 * comparing from children of root keys.
179 * @return <code>true</code> if keys and their sub keys are equal.
181 protected static boolean compareKeyTrees
182 (XRegistryKey tree1
, XRegistryKey tree2
, boolean compareRoot
) {
184 if (compareRoot
&& !compareKeys(tree1
, tree2
)) return false ;
187 String
[] keyNames1
= tree1
.getKeyNames() ;
188 String
[] keyNames2
= tree2
.getKeyNames() ;
190 if (keyNames1
== null && keyNames2
== null) return true ;
192 if (keyNames1
== null || keyNames2
== null ||
193 keyNames2
.length
!= keyNames1
.length
)
196 for (int i
= 0; i
< keyNames1
.length
; i
++) {
198 String keyName
= getShortKeyName(keyNames1
[i
]) ;
199 XRegistryKey key2
= tree2
.openKey(keyName
) ;
202 // key with the same name doesn't exist in the second tree
205 if (!tree1
.getKeyType(keyName
).equals(
206 tree2
.getKeyType(keyName
)))
209 if (tree1
.getKeyType(keyName
).equals(
210 RegistryKeyType
.LINK
)) {
212 if (!getShortKeyName(tree1
.getLinkTarget(keyName
)).equals(
213 getShortKeyName(tree2
.getLinkTarget(keyName
))))
218 if (compareKeyTrees(tree1
.openKey(keyName
),
219 tree2
.openKey(keyName
), true) == false) return false ;
222 } catch (InvalidRegistryException e
) {
230 * Compare keys specified and all their child keys.
231 * @return <code>true</code> if keys and their sub keys are equal.
233 public static boolean compareKeyTrees
234 (XRegistryKey tree1
, XRegistryKey tree2
) {
236 return compareKeyTrees(tree1
, tree2
, false) ;
240 * Prints to a specified output about all keys and subkeys information
241 * (key name, type, value, link target, attributes) recursively.
242 * @param reg Registry for which information is needed.
243 * @param out Output stream.
245 public static void printRegistryInfo(XSimpleRegistry reg
, PrintWriter out
) {
247 printRegistryInfo(reg
.getRootKey(), out
) ;
248 } catch (com
.sun
.star
.registry
.InvalidRegistryException e
) {
249 out
.println("!!! Can't open root registry key for info printing") ;
254 * Prints to a specified output about all keys and subkeys information
255 * (key name, type, value, link target, attributes) recursively.
256 * @param root Key for which subkeys (and further) information is required.
257 * @param out Output stream.
259 public static void printRegistryInfo(XRegistryKey root
, PrintWriter out
) {
261 out
.println("/(null)") ;
267 printTreeInfo(root
, out
, " ") ;
268 } catch (com
.sun
.star
.registry
.InvalidRegistryException e
) {
269 out
.println("Exception accessing registry :") ;
270 e
.printStackTrace(out
) ;
274 private static void printTreeInfo(XRegistryKey key
,
275 PrintWriter out
, String margin
)
276 throws com
.sun
.star
.registry
.InvalidRegistryException
{
278 String
[] subKeys
= key
.getKeyNames() ;
280 if (subKeys
== null || subKeys
.length
== 0) return ;
282 for (int i
= 0; i
< subKeys
.length
; i
++) {
283 printKeyInfo(key
, subKeys
[i
], out
, margin
) ;
284 XRegistryKey subKey
= key
.openKey
285 (getShortKeyName(subKeys
[i
])) ;
286 printTreeInfo(subKey
, out
, margin
+ " ") ;
291 private static void printKeyInfo(XRegistryKey parentKey
,
292 String keyName
, PrintWriter out
, String margin
)
293 throws com
.sun
.star
.registry
.InvalidRegistryException
{
296 keyName
= getShortKeyName(keyName
) ;
297 XRegistryKey key
= parentKey
.openKey(keyName
) ;
299 out
.print("/" + getShortKeyName(key
.getKeyName()) + " ") ;
301 out
.println("(null)") ;
305 if (!key
.isValid()) {
306 out
.println("(not valid)") ;
310 if (key
.isReadOnly()) {
311 out
.print("(read only) ") ;
314 if (parentKey
.getKeyType(keyName
) == RegistryKeyType
.LINK
) {
315 out
.println("(link to " + parentKey
.getLinkTarget(keyName
) + ")") ;
319 RegistryValueType type
;
321 type
= key
.getValueType() ;
323 if (type
.equals(RegistryValueType
.ASCII
)) {
324 out
.println("[ASCII] = '" + key
.getAsciiValue() + "'") ;
326 if (type
.equals(RegistryValueType
.STRING
)) {
327 out
.println("[STRING] = '" + key
.getStringValue() + "'") ;
329 if (type
.equals(RegistryValueType
.LONG
)) {
330 out
.println("[LONG] = " + key
.getLongValue()) ;
332 if (type
.equals(RegistryValueType
.BINARY
)) {
333 out
.print("[BINARY] = {") ;
334 byte[] bin
= key
.getBinaryValue() ;
335 for (int i
= 0; i
< bin
.length
; i
++)
336 out
.print("" + bin
[i
] + ",") ;
339 if (type
.equals(RegistryValueType
.ASCIILIST
)) {
340 out
.print("[ASCIILIST] = {") ;
341 String
[] list
= key
.getAsciiListValue() ;
342 for (int i
= 0; i
< list
.length
; i
++)
343 out
.print("'" + list
[i
] + "',") ;
346 if (type
.equals(RegistryValueType
.STRINGLIST
)) {
347 out
.print("[STRINGLIST] = {") ;
348 String
[] list
= key
.getStringListValue() ;
349 for (int i
= 0; i
< list
.length
; i
++)
350 out
.print("'" + list
[i
] + "',") ;
353 if (type
.equals(RegistryValueType
.LONGLIST
)) {
354 out
.print("[LONGLIST] = {") ;
355 int[] list
= key
.getLongListValue() ;
356 for (int i
= 0; i
< list
.length
; i
++)
357 out
.print("" + list
[i
] + ",") ;
362 } catch (com
.sun
.star
.uno
.Exception e
) {
363 out
.println("Exception occured : ") ;
364 e
.printStackTrace(out
) ;
371 // public static void compareKeyTrees