1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir package util; 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir // access the implementations via names 31*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 32*cdf0e10cSrcweir import java.io.PrintWriter ; 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey ; 35*cdf0e10cSrcweir import com.sun.star.registry.XSimpleRegistry ; 36*cdf0e10cSrcweir import com.sun.star.registry.RegistryKeyType ; 37*cdf0e10cSrcweir import com.sun.star.registry.RegistryValueType ; 38*cdf0e10cSrcweir import com.sun.star.registry.InvalidRegistryException ; 39*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory ; 40*cdf0e10cSrcweir import com.sun.star.uno.Exception; 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir public class RegistryTools { 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir /** 45*cdf0e10cSrcweir * Creates 'com.sun.star.registry.SimpleRegistry' 46*cdf0e10cSrcweir * service. 47*cdf0e10cSrcweir * @param xMSF Multiservice factory. 48*cdf0e10cSrcweir * @return Service created. 49*cdf0e10cSrcweir */ 50*cdf0e10cSrcweir public static XSimpleRegistry createRegistryService 51*cdf0e10cSrcweir (XMultiServiceFactory xMSF) throws com.sun.star.uno.Exception { 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir Object oInterface = xMSF.createInstance 54*cdf0e10cSrcweir ("com.sun.star.registry.SimpleRegistry"); 55*cdf0e10cSrcweir return (XSimpleRegistry) UnoRuntime.queryInterface ( 56*cdf0e10cSrcweir XSimpleRegistry.class, oInterface) ; 57*cdf0e10cSrcweir } 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir /** 60*cdf0e10cSrcweir * Opens registry file for reading/writing. If file doesn't 61*cdf0e10cSrcweir * exist a new one created. 62*cdf0e10cSrcweir * @param file Registry file name. 63*cdf0e10cSrcweir * @param xMSF Multiservice factory. 64*cdf0e10cSrcweir * @return Opened registry. 65*cdf0e10cSrcweir */ 66*cdf0e10cSrcweir public static XSimpleRegistry openRegistry 67*cdf0e10cSrcweir (String file, XMultiServiceFactory xMSF) 68*cdf0e10cSrcweir throws com.sun.star.uno.Exception { 69*cdf0e10cSrcweir 70*cdf0e10cSrcweir XSimpleRegistry reg = createRegistryService(xMSF) ; 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir reg.open(file, false, true) ; 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir return reg ; 75*cdf0e10cSrcweir } 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir /** 78*cdf0e10cSrcweir * Compares two registry keys, their names, value 79*cdf0e10cSrcweir * types and values. 80*cdf0e10cSrcweir * return <code>true</code> if key names, value types 81*cdf0e10cSrcweir * and values are equal, else returns <code>false</code>. 82*cdf0e10cSrcweir */ 83*cdf0e10cSrcweir public static boolean compareKeys 84*cdf0e10cSrcweir (XRegistryKey key1, XRegistryKey key2) { 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir if (key1 == null || key2 == null || 87*cdf0e10cSrcweir !key1.isValid() || !key2.isValid()) 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir return false ; 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir String keyName1 = getShortKeyName(key1.getKeyName()) ; 92*cdf0e10cSrcweir String keyName2 = getShortKeyName(key2.getKeyName()) ; 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir if (!keyName1.equals(keyName2)) return false ; 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir try { 97*cdf0e10cSrcweir if (key1.getValueType() != key2.getValueType()) return false ; 98*cdf0e10cSrcweir } catch (InvalidRegistryException e) { 99*cdf0e10cSrcweir return false ; 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir RegistryValueType type ; 103*cdf0e10cSrcweir try { 104*cdf0e10cSrcweir type = key1.getValueType() ; 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir if (type.equals(RegistryValueType.ASCII)) { 107*cdf0e10cSrcweir if (!key1.getAsciiValue().equals(key2.getAsciiValue())) 108*cdf0e10cSrcweir return false ; 109*cdf0e10cSrcweir } else 110*cdf0e10cSrcweir if (type.equals(RegistryValueType.STRING)) { 111*cdf0e10cSrcweir if (!key1.getStringValue().equals(key2.getStringValue())) 112*cdf0e10cSrcweir return false ; 113*cdf0e10cSrcweir } else 114*cdf0e10cSrcweir if (type.equals(RegistryValueType.LONG)) { 115*cdf0e10cSrcweir if (key1.getLongValue() != key2.getLongValue()) 116*cdf0e10cSrcweir return false ; 117*cdf0e10cSrcweir } else 118*cdf0e10cSrcweir if (type.equals(RegistryValueType.BINARY)) { 119*cdf0e10cSrcweir byte[] bin1 = key1.getBinaryValue() ; 120*cdf0e10cSrcweir byte[] bin2 = key2.getBinaryValue() ; 121*cdf0e10cSrcweir if (bin1.length != bin2.length) 122*cdf0e10cSrcweir return false ; 123*cdf0e10cSrcweir for (int i = 0; i < bin1.length; i++) 124*cdf0e10cSrcweir if (bin1[i] != bin2[i]) return false ; 125*cdf0e10cSrcweir } else 126*cdf0e10cSrcweir if (type.equals(RegistryValueType.ASCIILIST)) { 127*cdf0e10cSrcweir String[] list1 = key1.getAsciiListValue() ; 128*cdf0e10cSrcweir String[] list2 = key2.getAsciiListValue() ; 129*cdf0e10cSrcweir if (list1.length != list2.length) 130*cdf0e10cSrcweir return false ; 131*cdf0e10cSrcweir for (int i = 0; i < list1.length; i++) 132*cdf0e10cSrcweir if (!list1[i].equals(list2[i])) return false ; 133*cdf0e10cSrcweir } else 134*cdf0e10cSrcweir if (type.equals(RegistryValueType.STRINGLIST)) { 135*cdf0e10cSrcweir String[] list1 = key1.getStringListValue() ; 136*cdf0e10cSrcweir String[] list2 = key2.getStringListValue() ; 137*cdf0e10cSrcweir if (list1.length != list2.length) 138*cdf0e10cSrcweir return false ; 139*cdf0e10cSrcweir for (int i = 0; i < list1.length; i++) 140*cdf0e10cSrcweir if (!list1[i].equals(list2[i])) return false ; 141*cdf0e10cSrcweir } else 142*cdf0e10cSrcweir if (type.equals(RegistryValueType.LONGLIST)) { 143*cdf0e10cSrcweir int[] list1 = key1.getLongListValue() ; 144*cdf0e10cSrcweir int[] list2 = key2.getLongListValue() ; 145*cdf0e10cSrcweir if (list1.length != list2.length) 146*cdf0e10cSrcweir return false ; 147*cdf0e10cSrcweir for (int i = 0; i < list1.length; i++) 148*cdf0e10cSrcweir if (list1[i] != list2[i]) return false ; 149*cdf0e10cSrcweir } 150*cdf0e10cSrcweir } catch (Exception e) { 151*cdf0e10cSrcweir return false ; 152*cdf0e10cSrcweir } 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir return true ; 155*cdf0e10cSrcweir } 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir /** 158*cdf0e10cSrcweir * Gets name of the key relative to its parent. 159*cdf0e10cSrcweir * For example if full name of key is '/key1/subkey' 160*cdf0e10cSrcweir * short key name is 'subkey' 161*cdf0e10cSrcweir * @param keyName Full key name. 162*cdf0e10cSrcweir * @return Short key name. 163*cdf0e10cSrcweir */ 164*cdf0e10cSrcweir public static String getShortKeyName(String keyName) { 165*cdf0e10cSrcweir if (keyName == null) return null ; 166*cdf0e10cSrcweir int idx = keyName.lastIndexOf("/") ; 167*cdf0e10cSrcweir if (idx < 0) return keyName ; 168*cdf0e10cSrcweir else return keyName.substring(idx + 1) ; 169*cdf0e10cSrcweir } 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir /** 172*cdf0e10cSrcweir * Compare all child keys. 173*cdf0e10cSrcweir * @param compareRoot If <code>true</code> method also 174*cdf0e10cSrcweir * compare root keys, if <code>false</code> it begins recursive 175*cdf0e10cSrcweir * comparing from children of root keys. 176*cdf0e10cSrcweir * @return <code>true</code> if keys and their sub keys are equal. 177*cdf0e10cSrcweir */ 178*cdf0e10cSrcweir protected static boolean compareKeyTrees 179*cdf0e10cSrcweir (XRegistryKey tree1, XRegistryKey tree2, boolean compareRoot) { 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir if (compareRoot && !compareKeys(tree1, tree2)) return false ; 182*cdf0e10cSrcweir 183*cdf0e10cSrcweir try { 184*cdf0e10cSrcweir String[] keyNames1 = tree1.getKeyNames() ; 185*cdf0e10cSrcweir String[] keyNames2 = tree2.getKeyNames() ; 186*cdf0e10cSrcweir 187*cdf0e10cSrcweir if (keyNames1 == null && keyNames2 == null) return true ; 188*cdf0e10cSrcweir 189*cdf0e10cSrcweir if (keyNames1 == null || keyNames2 == null || 190*cdf0e10cSrcweir keyNames2.length != keyNames1.length) 191*cdf0e10cSrcweir return false ; 192*cdf0e10cSrcweir 193*cdf0e10cSrcweir for (int i = 0; i < keyNames1.length; i++) { 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir String keyName = getShortKeyName(keyNames1[i]) ; 196*cdf0e10cSrcweir XRegistryKey key2 = tree2.openKey(keyName) ; 197*cdf0e10cSrcweir 198*cdf0e10cSrcweir if (key2 == null) 199*cdf0e10cSrcweir // key with the same name doesn't exist in the second tree 200*cdf0e10cSrcweir return false ; 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir if (!tree1.getKeyType(keyName).equals( 203*cdf0e10cSrcweir tree2.getKeyType(keyName))) 204*cdf0e10cSrcweir return false ; 205*cdf0e10cSrcweir 206*cdf0e10cSrcweir if (tree1.getKeyType(keyName).equals( 207*cdf0e10cSrcweir RegistryKeyType.LINK)) { 208*cdf0e10cSrcweir 209*cdf0e10cSrcweir if (!getShortKeyName(tree1.getLinkTarget(keyName)).equals( 210*cdf0e10cSrcweir getShortKeyName(tree2.getLinkTarget(keyName)))) 211*cdf0e10cSrcweir 212*cdf0e10cSrcweir return false ; 213*cdf0e10cSrcweir } else { 214*cdf0e10cSrcweir 215*cdf0e10cSrcweir if (compareKeyTrees(tree1.openKey(keyName), 216*cdf0e10cSrcweir tree2.openKey(keyName), true) == false) return false ; 217*cdf0e10cSrcweir } 218*cdf0e10cSrcweir } 219*cdf0e10cSrcweir } catch (InvalidRegistryException e) { 220*cdf0e10cSrcweir return false ; 221*cdf0e10cSrcweir } 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir return true ; 224*cdf0e10cSrcweir } 225*cdf0e10cSrcweir 226*cdf0e10cSrcweir /** 227*cdf0e10cSrcweir * Compare keys specified and all their child keys. 228*cdf0e10cSrcweir * @return <code>true</code> if keys and their sub keys are equal. 229*cdf0e10cSrcweir */ 230*cdf0e10cSrcweir public static boolean compareKeyTrees 231*cdf0e10cSrcweir (XRegistryKey tree1, XRegistryKey tree2) { 232*cdf0e10cSrcweir 233*cdf0e10cSrcweir return compareKeyTrees(tree1, tree2, false) ; 234*cdf0e10cSrcweir } 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir /** 237*cdf0e10cSrcweir * Prints to a specified output about all keys and subkeys information 238*cdf0e10cSrcweir * (key name, type, value, link target, attributes) recursively. 239*cdf0e10cSrcweir * @param reg Registry for which information is needed. 240*cdf0e10cSrcweir * @param out Output stream. 241*cdf0e10cSrcweir */ 242*cdf0e10cSrcweir public static void printRegistryInfo(XSimpleRegistry reg, PrintWriter out) { 243*cdf0e10cSrcweir try { 244*cdf0e10cSrcweir printRegistryInfo(reg.getRootKey(), out) ; 245*cdf0e10cSrcweir } catch (com.sun.star.registry.InvalidRegistryException e) { 246*cdf0e10cSrcweir out.println("!!! Can't open root registry key for info printing") ; 247*cdf0e10cSrcweir } 248*cdf0e10cSrcweir } 249*cdf0e10cSrcweir 250*cdf0e10cSrcweir /** 251*cdf0e10cSrcweir * Prints to a specified output about all keys and subkeys information 252*cdf0e10cSrcweir * (key name, type, value, link target, attributes) recursively. 253*cdf0e10cSrcweir * @param root Key for which subkeys (and further) information is required. 254*cdf0e10cSrcweir * @param out Output stream. 255*cdf0e10cSrcweir */ 256*cdf0e10cSrcweir public static void printRegistryInfo(XRegistryKey root, PrintWriter out) { 257*cdf0e10cSrcweir if (root == null) { 258*cdf0e10cSrcweir out.println("/(null)") ; 259*cdf0e10cSrcweir return ; 260*cdf0e10cSrcweir } 261*cdf0e10cSrcweir 262*cdf0e10cSrcweir out.println("/") ; 263*cdf0e10cSrcweir try { 264*cdf0e10cSrcweir printTreeInfo(root, out, " ") ; 265*cdf0e10cSrcweir } catch (com.sun.star.registry.InvalidRegistryException e) { 266*cdf0e10cSrcweir out.println("Exception accessing registry :") ; 267*cdf0e10cSrcweir e.printStackTrace(out) ; 268*cdf0e10cSrcweir } 269*cdf0e10cSrcweir } 270*cdf0e10cSrcweir 271*cdf0e10cSrcweir private static void printTreeInfo(XRegistryKey key, 272*cdf0e10cSrcweir PrintWriter out, String margin) 273*cdf0e10cSrcweir throws com.sun.star.registry.InvalidRegistryException { 274*cdf0e10cSrcweir 275*cdf0e10cSrcweir String[] subKeys = key.getKeyNames() ; 276*cdf0e10cSrcweir 277*cdf0e10cSrcweir if (subKeys == null || subKeys.length == 0) return ; 278*cdf0e10cSrcweir 279*cdf0e10cSrcweir for (int i = 0; i < subKeys.length; i++) { 280*cdf0e10cSrcweir printKeyInfo(key, subKeys[i], out, margin) ; 281*cdf0e10cSrcweir XRegistryKey subKey = key.openKey 282*cdf0e10cSrcweir (getShortKeyName(subKeys[i])) ; 283*cdf0e10cSrcweir printTreeInfo(subKey, out, margin + " ") ; 284*cdf0e10cSrcweir subKey.closeKey() ; 285*cdf0e10cSrcweir } 286*cdf0e10cSrcweir } 287*cdf0e10cSrcweir 288*cdf0e10cSrcweir private static void printKeyInfo(XRegistryKey parentKey, 289*cdf0e10cSrcweir String keyName, PrintWriter out, String margin) 290*cdf0e10cSrcweir throws com.sun.star.registry.InvalidRegistryException { 291*cdf0e10cSrcweir 292*cdf0e10cSrcweir out.print(margin) ; 293*cdf0e10cSrcweir keyName = getShortKeyName(keyName) ; 294*cdf0e10cSrcweir XRegistryKey key = parentKey.openKey(keyName) ; 295*cdf0e10cSrcweir if (key != null) 296*cdf0e10cSrcweir out.print("/" + getShortKeyName(key.getKeyName()) + " ") ; 297*cdf0e10cSrcweir else { 298*cdf0e10cSrcweir out.println("(null)") ; 299*cdf0e10cSrcweir return ; 300*cdf0e10cSrcweir } 301*cdf0e10cSrcweir 302*cdf0e10cSrcweir if (!key.isValid()) { 303*cdf0e10cSrcweir out.println("(not valid)") ; 304*cdf0e10cSrcweir return ; 305*cdf0e10cSrcweir } 306*cdf0e10cSrcweir 307*cdf0e10cSrcweir if (key.isReadOnly()) { 308*cdf0e10cSrcweir out.print("(read only) ") ; 309*cdf0e10cSrcweir } 310*cdf0e10cSrcweir 311*cdf0e10cSrcweir if (parentKey.getKeyType(keyName) == RegistryKeyType.LINK) { 312*cdf0e10cSrcweir out.println("(link to " + parentKey.getLinkTarget(keyName) + ")") ; 313*cdf0e10cSrcweir return ; 314*cdf0e10cSrcweir } 315*cdf0e10cSrcweir 316*cdf0e10cSrcweir RegistryValueType type ; 317*cdf0e10cSrcweir try { 318*cdf0e10cSrcweir type = key.getValueType() ; 319*cdf0e10cSrcweir 320*cdf0e10cSrcweir if (type.equals(RegistryValueType.ASCII)) { 321*cdf0e10cSrcweir out.println("[ASCII] = '" + key.getAsciiValue() + "'") ; 322*cdf0e10cSrcweir } else 323*cdf0e10cSrcweir if (type.equals(RegistryValueType.STRING)) { 324*cdf0e10cSrcweir out.println("[STRING] = '" + key.getStringValue() + "'") ; 325*cdf0e10cSrcweir } else 326*cdf0e10cSrcweir if (type.equals(RegistryValueType.LONG)) { 327*cdf0e10cSrcweir out.println("[LONG] = " + key.getLongValue()) ; 328*cdf0e10cSrcweir } else 329*cdf0e10cSrcweir if (type.equals(RegistryValueType.BINARY)) { 330*cdf0e10cSrcweir out.print("[BINARY] = {") ; 331*cdf0e10cSrcweir byte[] bin = key.getBinaryValue() ; 332*cdf0e10cSrcweir for (int i = 0; i < bin.length; i++) 333*cdf0e10cSrcweir out.print("" + bin[i] + ",") ; 334*cdf0e10cSrcweir out.println("}") ; 335*cdf0e10cSrcweir } else 336*cdf0e10cSrcweir if (type.equals(RegistryValueType.ASCIILIST)) { 337*cdf0e10cSrcweir out.print("[ASCIILIST] = {") ; 338*cdf0e10cSrcweir String[] list = key.getAsciiListValue() ; 339*cdf0e10cSrcweir for (int i = 0; i < list.length; i++) 340*cdf0e10cSrcweir out.print("'" + list[i] + "',") ; 341*cdf0e10cSrcweir out.println("}") ; 342*cdf0e10cSrcweir } else 343*cdf0e10cSrcweir if (type.equals(RegistryValueType.STRINGLIST)) { 344*cdf0e10cSrcweir out.print("[STRINGLIST] = {") ; 345*cdf0e10cSrcweir String[] list = key.getStringListValue() ; 346*cdf0e10cSrcweir for (int i = 0; i < list.length; i++) 347*cdf0e10cSrcweir out.print("'" + list[i] + "',") ; 348*cdf0e10cSrcweir out.println("}") ; 349*cdf0e10cSrcweir } else 350*cdf0e10cSrcweir if (type.equals(RegistryValueType.LONGLIST)) { 351*cdf0e10cSrcweir out.print("[LONGLIST] = {") ; 352*cdf0e10cSrcweir int[] list = key.getLongListValue() ; 353*cdf0e10cSrcweir for (int i = 0; i < list.length; i++) 354*cdf0e10cSrcweir out.print("" + list[i] + ",") ; 355*cdf0e10cSrcweir out.println("}") ; 356*cdf0e10cSrcweir } else { 357*cdf0e10cSrcweir out.println("") ; 358*cdf0e10cSrcweir } 359*cdf0e10cSrcweir } catch (com.sun.star.uno.Exception e) { 360*cdf0e10cSrcweir out.println("Exception occured : ") ; 361*cdf0e10cSrcweir e.printStackTrace(out) ; 362*cdf0e10cSrcweir } finally { 363*cdf0e10cSrcweir key.closeKey() ; 364*cdf0e10cSrcweir } 365*cdf0e10cSrcweir } 366*cdf0e10cSrcweir 367*cdf0e10cSrcweir 368*cdf0e10cSrcweir // public static void compareKeyTrees 369*cdf0e10cSrcweir 370*cdf0e10cSrcweir } 371