summaryrefslogtreecommitdiff
path: root/qadevOOo/runner/util/RegistryTools.java
blob: 72ef5e74370daa30a38a7d39e6924fed31ae6977 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

package util;

// access the implementations via names
import com.sun.star.uno.UnoRuntime;
import java.io.PrintWriter ;

import com.sun.star.registry.XRegistryKey ;
import com.sun.star.registry.XSimpleRegistry ;
import com.sun.star.registry.RegistryKeyType ;
import com.sun.star.registry.RegistryValueType ;
import com.sun.star.registry.InvalidRegistryException ;
import com.sun.star.lang.XMultiServiceFactory ;
import com.sun.star.uno.Exception;

public class RegistryTools {

    /**
    * Creates 'com.sun.star.registry.SimpleRegistry'
    * service.
    * @param xMSF Multiservice factory.
    * @return Service created.
    */
    public static XSimpleRegistry createRegistryService
        (XMultiServiceFactory xMSF) throws com.sun.star.uno.Exception {

        Object oInterface = xMSF.createInstance
            ("com.sun.star.registry.SimpleRegistry");
        return UnoRuntime.queryInterface (
            XSimpleRegistry.class, oInterface) ;
    }

    /**
    * Opens registry file for reading/writing. If file doesn't
    * exist a new one created.
    * @param file Registry file name.
    * @param xMSF Multiservice factory.
    * @return Opened registry.
    */
    public static XSimpleRegistry openRegistry
        (String file, XMultiServiceFactory xMSF)
        throws com.sun.star.uno.Exception {

        XSimpleRegistry reg = createRegistryService(xMSF) ;

        reg.open(file, false, true) ;

        return reg ;
    }

    /**
    * Compares two registry keys, their names, value
    * types and values.
    * return <code>true</code> if key names, value types
    * and values are equal, else returns <code>false</code>.
    */
    private static boolean compareKeys
        (XRegistryKey key1, XRegistryKey key2) {

        if (key1 == null || key2 == null ||
            !key1.isValid() || !key2.isValid())

            return false ;

        String keyName1 = getShortKeyName(key1.getKeyName()) ;
        String keyName2 = getShortKeyName(key2.getKeyName()) ;

        if (!keyName1.equals(keyName2)) return false ;

        try {
            if (key1.getValueType() != key2.getValueType()) return false ;
        } catch (InvalidRegistryException e) {
            return false ;
        }

        RegistryValueType type ;
        try {
            type = key1.getValueType() ;

            if (type.equals(RegistryValueType.ASCII)) {
                if (!key1.getAsciiValue().equals(key2.getAsciiValue()))
                    return false ;
            } else
            if (type.equals(RegistryValueType.STRING)) {
                if (!key1.getStringValue().equals(key2.getStringValue()))
                    return false ;
            } else
            if (type.equals(RegistryValueType.LONG)) {
                if (key1.getLongValue() != key2.getLongValue())
                    return false ;
            } else
            if (type.equals(RegistryValueType.BINARY)) {
                byte[] bin1 = key1.getBinaryValue() ;
                byte[] bin2 = key2.getBinaryValue() ;
                if (bin1.length != bin2.length)
                    return false ;
                for (int i = 0; i < bin1.length; i++)
                    if (bin1[i] != bin2[i]) return false ;
            } else
            if (type.equals(RegistryValueType.ASCIILIST)) {
                String[] list1 = key1.getAsciiListValue() ;
                String[] list2 = key2.getAsciiListValue() ;
                if (list1.length != list2.length)
                    return false ;
                for (int i = 0; i < list1.length; i++)
                    if (!list1[i].equals(list2[i])) return false ;
            } else
            if (type.equals(RegistryValueType.STRINGLIST)) {
                String[] list1 = key1.getStringListValue() ;
                String[] list2 = key2.getStringListValue() ;
                if (list1.length != list2.length)
                    return false ;
                for (int i = 0; i < list1.length; i++)
                    if (!list1[i].equals(list2[i])) return false ;
            } else
            if (type.equals(RegistryValueType.LONGLIST)) {
                int[] list1 = key1.getLongListValue() ;
                int[] list2 = key2.getLongListValue() ;
                if (list1.length != list2.length)
                    return false ;
                for (int i = 0; i < list1.length; i++)
                    if (list1[i] != list2[i]) return false ;
            }
        } catch (Exception e) {
            return false ;
        }

        return true ;
    }

    /**
    * Gets name of the key relative to its parent.
    * For example if full name of key is '/key1/subkey'
    * short key name is 'subkey'
    * @param keyName Full key name.
    * @return Short key name.
    */
    private static String getShortKeyName(String keyName) {
        if (keyName == null) return null ;
        int idx = keyName.lastIndexOf('/') ;
        if (idx < 0) return keyName ;
        else return keyName.substring(idx + 1) ;
    }

    /**
    * Compare all child keys.
    * @param compareRoot If <code>true</code> method also
    * compare root keys, if <code>false</code> it begins recursive
    * comparing from children of root keys.
    * @return <code>true</code> if keys and their sub keys are equal.
    */
    private static boolean compareKeyTrees
        (XRegistryKey tree1, XRegistryKey tree2, boolean compareRoot) {

        if (compareRoot && !compareKeys(tree1, tree2)) return false ;

        try {
            String[] keyNames1 = tree1.getKeyNames() ;
            String[] keyNames2 = tree2.getKeyNames() ;

            if (keyNames1 == null && keyNames2 == null) return true ;

            if (keyNames1 == null || keyNames2 == null ||
                keyNames2.length != keyNames1.length)
                return false ;

            for (int i = 0; i < keyNames1.length; i++) {

                String keyName = getShortKeyName(keyNames1[i]) ;
                XRegistryKey key2 = tree2.openKey(keyName) ;

                if (key2 == null)
                // key with the same name doesn't exist in the second tree
                    return false ;

                if (!tree1.getKeyType(keyName).equals(
                     tree2.getKeyType(keyName)))
                    return false ;

                if (tree1.getKeyType(keyName).equals(
                    RegistryKeyType.LINK)) {

                    if (!getShortKeyName(tree1.getLinkTarget(keyName)).equals(
                        getShortKeyName(tree2.getLinkTarget(keyName))))

                        return false ;
                } else {

                    if (!compareKeyTrees(tree1.openKey(keyName),
                          tree2.openKey(keyName), true)) return false ;
                }
            }
        } catch (InvalidRegistryException e) {
            return false ;
        }

        return true ;
    }

    /**
    * Compare keys specified and all their child keys.
    * @return <code>true</code> if keys and their sub keys are equal.
    */
    public static boolean compareKeyTrees
        (XRegistryKey tree1, XRegistryKey tree2) {

        return compareKeyTrees(tree1, tree2, false) ;
    }

    /**
    * Prints to a specified output about all keys and subkeys information
    * (key name, type, value, link target, attributes) recursively.
    * @param reg Registry for which information is needed.
    * @param out Output stream.
    */
    public static void printRegistryInfo(XSimpleRegistry reg, PrintWriter out) {
        try {
            printRegistryInfo(reg.getRootKey(), out) ;
        } catch (com.sun.star.registry.InvalidRegistryException e) {
            out.println("!!! Can't open root registry key for info printing") ;
        }
    }

    /**
    * Prints to a specified output about all keys and subkeys information
    * (key name, type, value, link target, attributes) recursively.
    * @param root Key for which subkeys (and further) information is required.
    * @param out Output stream.
    */
    public static void printRegistryInfo(XRegistryKey root, PrintWriter out) {
        if (root == null) {
            out.println("/(null)") ;
            return ;
        }

        out.println("/") ;
        try {
            printTreeInfo(root, out, "  ") ;
        } catch (com.sun.star.registry.InvalidRegistryException e) {
            out.println("Exception accessing registry :") ;
            e.printStackTrace(out) ;
        }
    }

    private static void printTreeInfo(XRegistryKey key,
        PrintWriter out, String margin)
        throws com.sun.star.registry.InvalidRegistryException {

        String[] subKeys = key.getKeyNames() ;

        if (subKeys == null || subKeys.length == 0) return ;

        for (int i = 0; i < subKeys.length; i++) {
            printKeyInfo(key, subKeys[i], out, margin) ;
            XRegistryKey subKey = key.openKey
                (getShortKeyName(subKeys[i])) ;
            printTreeInfo(subKey, out, margin + "  ") ;
            subKey.closeKey() ;
        }
    }

    private static void printKeyInfo(XRegistryKey parentKey,
        String keyName, PrintWriter out, String margin)
        throws com.sun.star.registry.InvalidRegistryException {

        out.print(margin) ;
        keyName = getShortKeyName(keyName) ;
        XRegistryKey key = parentKey.openKey(keyName) ;
        if (key != null)
            out.print("/" + getShortKeyName(key.getKeyName()) + " ") ;
        else {
            out.println("(null)") ;
            return ;
        }

        if (!key.isValid()) {
            out.println("(not valid)") ;
            return ;
        }

        if (key.isReadOnly()) {
            out.print("(read only) ") ;
        }

        if (parentKey.getKeyType(keyName) == RegistryKeyType.LINK) {
            out.println("(link to " + parentKey.getLinkTarget(keyName) + ")") ;
            return ;
        }

        RegistryValueType type ;
        try {
            type = key.getValueType() ;

            if (type.equals(RegistryValueType.ASCII)) {
                out.println("[ASCII] = '" + key.getAsciiValue() + "'") ;
            } else
            if (type.equals(RegistryValueType.STRING)) {
                out.println("[STRING] = '" + key.getStringValue() + "'") ;
            } else
            if (type.equals(RegistryValueType.LONG)) {
                out.println("[LONG] = " + key.getLongValue()) ;
            } else
            if (type.equals(RegistryValueType.BINARY)) {
                out.print("[BINARY] = {") ;
                byte[] bin = key.getBinaryValue() ;
                for (int i = 0; i < bin.length; i++)
                    out.print(bin[i] + ",") ;
                out.println("}") ;
            } else
            if (type.equals(RegistryValueType.ASCIILIST)) {
                out.print("[ASCIILIST] = {") ;
                String[] list = key.getAsciiListValue() ;
                for (int i = 0; i < list.length; i++)
                    out.print("'" + list[i] + "',") ;
                out.println("}") ;
            } else
            if (type.equals(RegistryValueType.STRINGLIST)) {
                out.print("[STRINGLIST] = {") ;
                String[] list = key.getStringListValue() ;
                for (int i = 0; i < list.length; i++)
                    out.print("'" + list[i] + "',") ;
                out.println("}") ;
            } else
            if (type.equals(RegistryValueType.LONGLIST)) {
                out.print("[LONGLIST] = {") ;
                int[] list = key.getLongListValue() ;
                for (int i = 0; i < list.length; i++)
                    out.print(list[i] + ",") ;
                out.println("}") ;
            } else {
                out.println("") ;
            }
        } catch (com.sun.star.uno.Exception e) {
            out.println("Exception occurred : ") ;
            e.printStackTrace(out) ;
        } finally {
            key.closeKey() ;
        }
    }


}