tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / qadevOOo / runner / util / RegistryTools.java
blob72ef5e74370daa30a38a7d39e6924fed31ae6977
1 /*
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 .
19 package util;
21 // access the implementations via names
22 import com.sun.star.uno.UnoRuntime;
23 import java.io.PrintWriter ;
25 import com.sun.star.registry.XRegistryKey ;
26 import com.sun.star.registry.XSimpleRegistry ;
27 import com.sun.star.registry.RegistryKeyType ;
28 import com.sun.star.registry.RegistryValueType ;
29 import com.sun.star.registry.InvalidRegistryException ;
30 import com.sun.star.lang.XMultiServiceFactory ;
31 import com.sun.star.uno.Exception;
33 public class RegistryTools {
35 /**
36 * Creates 'com.sun.star.registry.SimpleRegistry'
37 * service.
38 * @param xMSF Multiservice factory.
39 * @return Service created.
41 public static XSimpleRegistry createRegistryService
42 (XMultiServiceFactory xMSF) throws com.sun.star.uno.Exception {
44 Object oInterface = xMSF.createInstance
45 ("com.sun.star.registry.SimpleRegistry");
46 return UnoRuntime.queryInterface (
47 XSimpleRegistry.class, oInterface) ;
50 /**
51 * Opens registry file for reading/writing. If file doesn't
52 * exist a new one created.
53 * @param file Registry file name.
54 * @param xMSF Multiservice factory.
55 * @return Opened registry.
57 public static XSimpleRegistry openRegistry
58 (String file, XMultiServiceFactory xMSF)
59 throws com.sun.star.uno.Exception {
61 XSimpleRegistry reg = createRegistryService(xMSF) ;
63 reg.open(file, false, true) ;
65 return reg ;
68 /**
69 * Compares two registry keys, their names, value
70 * types and values.
71 * return <code>true</code> if key names, value types
72 * and values are equal, else returns <code>false</code>.
74 private static boolean compareKeys
75 (XRegistryKey key1, XRegistryKey key2) {
77 if (key1 == null || key2 == null ||
78 !key1.isValid() || !key2.isValid())
80 return false ;
82 String keyName1 = getShortKeyName(key1.getKeyName()) ;
83 String keyName2 = getShortKeyName(key2.getKeyName()) ;
85 if (!keyName1.equals(keyName2)) return false ;
87 try {
88 if (key1.getValueType() != key2.getValueType()) return false ;
89 } catch (InvalidRegistryException e) {
90 return false ;
93 RegistryValueType type ;
94 try {
95 type = key1.getValueType() ;
97 if (type.equals(RegistryValueType.ASCII)) {
98 if (!key1.getAsciiValue().equals(key2.getAsciiValue()))
99 return false ;
100 } else
101 if (type.equals(RegistryValueType.STRING)) {
102 if (!key1.getStringValue().equals(key2.getStringValue()))
103 return false ;
104 } else
105 if (type.equals(RegistryValueType.LONG)) {
106 if (key1.getLongValue() != key2.getLongValue())
107 return false ;
108 } else
109 if (type.equals(RegistryValueType.BINARY)) {
110 byte[] bin1 = key1.getBinaryValue() ;
111 byte[] bin2 = key2.getBinaryValue() ;
112 if (bin1.length != bin2.length)
113 return false ;
114 for (int i = 0; i < bin1.length; i++)
115 if (bin1[i] != bin2[i]) return false ;
116 } else
117 if (type.equals(RegistryValueType.ASCIILIST)) {
118 String[] list1 = key1.getAsciiListValue() ;
119 String[] list2 = key2.getAsciiListValue() ;
120 if (list1.length != list2.length)
121 return false ;
122 for (int i = 0; i < list1.length; i++)
123 if (!list1[i].equals(list2[i])) return false ;
124 } else
125 if (type.equals(RegistryValueType.STRINGLIST)) {
126 String[] list1 = key1.getStringListValue() ;
127 String[] list2 = key2.getStringListValue() ;
128 if (list1.length != list2.length)
129 return false ;
130 for (int i = 0; i < list1.length; i++)
131 if (!list1[i].equals(list2[i])) return false ;
132 } else
133 if (type.equals(RegistryValueType.LONGLIST)) {
134 int[] list1 = key1.getLongListValue() ;
135 int[] list2 = key2.getLongListValue() ;
136 if (list1.length != list2.length)
137 return false ;
138 for (int i = 0; i < list1.length; i++)
139 if (list1[i] != list2[i]) return false ;
141 } catch (Exception e) {
142 return false ;
145 return true ;
149 * Gets name of the key relative to its parent.
150 * For example if full name of key is '/key1/subkey'
151 * short key name is 'subkey'
152 * @param keyName Full key name.
153 * @return Short key name.
155 private static String getShortKeyName(String keyName) {
156 if (keyName == null) return null ;
157 int idx = keyName.lastIndexOf('/') ;
158 if (idx < 0) return keyName ;
159 else return keyName.substring(idx + 1) ;
163 * Compare all child keys.
164 * @param compareRoot If <code>true</code> method also
165 * compare root keys, if <code>false</code> it begins recursive
166 * comparing from children of root keys.
167 * @return <code>true</code> if keys and their sub keys are equal.
169 private static boolean compareKeyTrees
170 (XRegistryKey tree1, XRegistryKey tree2, boolean compareRoot) {
172 if (compareRoot && !compareKeys(tree1, tree2)) return false ;
174 try {
175 String[] keyNames1 = tree1.getKeyNames() ;
176 String[] keyNames2 = tree2.getKeyNames() ;
178 if (keyNames1 == null && keyNames2 == null) return true ;
180 if (keyNames1 == null || keyNames2 == null ||
181 keyNames2.length != keyNames1.length)
182 return false ;
184 for (int i = 0; i < keyNames1.length; i++) {
186 String keyName = getShortKeyName(keyNames1[i]) ;
187 XRegistryKey key2 = tree2.openKey(keyName) ;
189 if (key2 == null)
190 // key with the same name doesn't exist in the second tree
191 return false ;
193 if (!tree1.getKeyType(keyName).equals(
194 tree2.getKeyType(keyName)))
195 return false ;
197 if (tree1.getKeyType(keyName).equals(
198 RegistryKeyType.LINK)) {
200 if (!getShortKeyName(tree1.getLinkTarget(keyName)).equals(
201 getShortKeyName(tree2.getLinkTarget(keyName))))
203 return false ;
204 } else {
206 if (!compareKeyTrees(tree1.openKey(keyName),
207 tree2.openKey(keyName), true)) return false ;
210 } catch (InvalidRegistryException e) {
211 return false ;
214 return true ;
218 * Compare keys specified and all their child keys.
219 * @return <code>true</code> if keys and their sub keys are equal.
221 public static boolean compareKeyTrees
222 (XRegistryKey tree1, XRegistryKey tree2) {
224 return compareKeyTrees(tree1, tree2, false) ;
228 * Prints to a specified output about all keys and subkeys information
229 * (key name, type, value, link target, attributes) recursively.
230 * @param reg Registry for which information is needed.
231 * @param out Output stream.
233 public static void printRegistryInfo(XSimpleRegistry reg, PrintWriter out) {
234 try {
235 printRegistryInfo(reg.getRootKey(), out) ;
236 } catch (com.sun.star.registry.InvalidRegistryException e) {
237 out.println("!!! Can't open root registry key for info printing") ;
242 * Prints to a specified output about all keys and subkeys information
243 * (key name, type, value, link target, attributes) recursively.
244 * @param root Key for which subkeys (and further) information is required.
245 * @param out Output stream.
247 public static void printRegistryInfo(XRegistryKey root, PrintWriter out) {
248 if (root == null) {
249 out.println("/(null)") ;
250 return ;
253 out.println("/") ;
254 try {
255 printTreeInfo(root, out, " ") ;
256 } catch (com.sun.star.registry.InvalidRegistryException e) {
257 out.println("Exception accessing registry :") ;
258 e.printStackTrace(out) ;
262 private static void printTreeInfo(XRegistryKey key,
263 PrintWriter out, String margin)
264 throws com.sun.star.registry.InvalidRegistryException {
266 String[] subKeys = key.getKeyNames() ;
268 if (subKeys == null || subKeys.length == 0) return ;
270 for (int i = 0; i < subKeys.length; i++) {
271 printKeyInfo(key, subKeys[i], out, margin) ;
272 XRegistryKey subKey = key.openKey
273 (getShortKeyName(subKeys[i])) ;
274 printTreeInfo(subKey, out, margin + " ") ;
275 subKey.closeKey() ;
279 private static void printKeyInfo(XRegistryKey parentKey,
280 String keyName, PrintWriter out, String margin)
281 throws com.sun.star.registry.InvalidRegistryException {
283 out.print(margin) ;
284 keyName = getShortKeyName(keyName) ;
285 XRegistryKey key = parentKey.openKey(keyName) ;
286 if (key != null)
287 out.print("/" + getShortKeyName(key.getKeyName()) + " ") ;
288 else {
289 out.println("(null)") ;
290 return ;
293 if (!key.isValid()) {
294 out.println("(not valid)") ;
295 return ;
298 if (key.isReadOnly()) {
299 out.print("(read only) ") ;
302 if (parentKey.getKeyType(keyName) == RegistryKeyType.LINK) {
303 out.println("(link to " + parentKey.getLinkTarget(keyName) + ")") ;
304 return ;
307 RegistryValueType type ;
308 try {
309 type = key.getValueType() ;
311 if (type.equals(RegistryValueType.ASCII)) {
312 out.println("[ASCII] = '" + key.getAsciiValue() + "'") ;
313 } else
314 if (type.equals(RegistryValueType.STRING)) {
315 out.println("[STRING] = '" + key.getStringValue() + "'") ;
316 } else
317 if (type.equals(RegistryValueType.LONG)) {
318 out.println("[LONG] = " + key.getLongValue()) ;
319 } else
320 if (type.equals(RegistryValueType.BINARY)) {
321 out.print("[BINARY] = {") ;
322 byte[] bin = key.getBinaryValue() ;
323 for (int i = 0; i < bin.length; i++)
324 out.print(bin[i] + ",") ;
325 out.println("}") ;
326 } else
327 if (type.equals(RegistryValueType.ASCIILIST)) {
328 out.print("[ASCIILIST] = {") ;
329 String[] list = key.getAsciiListValue() ;
330 for (int i = 0; i < list.length; i++)
331 out.print("'" + list[i] + "',") ;
332 out.println("}") ;
333 } else
334 if (type.equals(RegistryValueType.STRINGLIST)) {
335 out.print("[STRINGLIST] = {") ;
336 String[] list = key.getStringListValue() ;
337 for (int i = 0; i < list.length; i++)
338 out.print("'" + list[i] + "',") ;
339 out.println("}") ;
340 } else
341 if (type.equals(RegistryValueType.LONGLIST)) {
342 out.print("[LONGLIST] = {") ;
343 int[] list = key.getLongListValue() ;
344 for (int i = 0; i < list.length; i++)
345 out.print(list[i] + ",") ;
346 out.println("}") ;
347 } else {
348 out.println("") ;
350 } catch (com.sun.star.uno.Exception e) {
351 out.println("Exception occurred : ") ;
352 e.printStackTrace(out) ;
353 } finally {
354 key.closeKey() ;