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 import com.sun.star.beans.PropertyValue; 31*cdf0e10cSrcweir import java.lang.reflect.Array; 32*cdf0e10cSrcweir import java.lang.reflect.Field; 33*cdf0e10cSrcweir import java.lang.reflect.Modifier; 34*cdf0e10cSrcweir import com.sun.star.uno.Type; 35*cdf0e10cSrcweir import com.sun.star.uno.Enum; 36*cdf0e10cSrcweir import com.sun.star.uno.XInterface; 37*cdf0e10cSrcweir import com.sun.star.uno.Any; 38*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter; 39*cdf0e10cSrcweir import java.util.HashMap; 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir public class ValueComparer { 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir // Method to change a Value, thought for properties 45*cdf0e10cSrcweir public static boolean equalValue( Object first, Object second ) { 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir if (first instanceof com.sun.star.uno.Any) { 48*cdf0e10cSrcweir try { 49*cdf0e10cSrcweir first = AnyConverter.toObject(((Any) first).getType(),first); 50*cdf0e10cSrcweir } catch (com.sun.star.lang.IllegalArgumentException iae) { 51*cdf0e10cSrcweir } 52*cdf0e10cSrcweir } 53*cdf0e10cSrcweir if (second instanceof com.sun.star.uno.Any) { 54*cdf0e10cSrcweir try { 55*cdf0e10cSrcweir second = AnyConverter.toObject(((Any) second).getType(),second); 56*cdf0e10cSrcweir } catch (com.sun.star.lang.IllegalArgumentException iae) { 57*cdf0e10cSrcweir } 58*cdf0e10cSrcweir } 59*cdf0e10cSrcweir boolean eq = false; 60*cdf0e10cSrcweir try { 61*cdf0e10cSrcweir if ( (first==null) || (second == null) ) { 62*cdf0e10cSrcweir eq = (first == second); 63*cdf0e10cSrcweir } 64*cdf0e10cSrcweir else { 65*cdf0e10cSrcweir if ( util.utils.isVoid(first) && util.utils.isVoid(second) ) { 66*cdf0e10cSrcweir eq=true; 67*cdf0e10cSrcweir } else if ( util.utils.isVoid(first) || util.utils.isVoid(second) ) { 68*cdf0e10cSrcweir eq = (first == second); 69*cdf0e10cSrcweir } else { 70*cdf0e10cSrcweir eq = compareObjects(first, second); 71*cdf0e10cSrcweir } 72*cdf0e10cSrcweir } 73*cdf0e10cSrcweir } 74*cdf0e10cSrcweir catch (Exception e) { 75*cdf0e10cSrcweir System.out.println("Exception occured while comparing Objects"); 76*cdf0e10cSrcweir e.printStackTrace(); 77*cdf0e10cSrcweir } 78*cdf0e10cSrcweir return eq; 79*cdf0e10cSrcweir } // end of equalValue 80*cdf0e10cSrcweir 81*cdf0e10cSrcweir static boolean compareArrayOfPropertyValue(PropertyValue[] pv1, PropertyValue[] pv2){ 82*cdf0e10cSrcweir if ( pv1.length != pv2.length) { 83*cdf0e10cSrcweir return false; 84*cdf0e10cSrcweir } 85*cdf0e10cSrcweir HashMap hm1 = new HashMap(); 86*cdf0e10cSrcweir boolean result = true; 87*cdf0e10cSrcweir int i = 0; 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir for (i = 0; i < pv1.length; i++){ 90*cdf0e10cSrcweir hm1.put(pv1[i].Name, pv1[i].Value); 91*cdf0e10cSrcweir } 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir i = 0; 94*cdf0e10cSrcweir while (i < pv2.length && result) { 95*cdf0e10cSrcweir result &= equalValue(hm1.get(pv2[i].Name),pv2[i].Value); 96*cdf0e10cSrcweir i++; 97*cdf0e10cSrcweir } 98*cdf0e10cSrcweir return result; 99*cdf0e10cSrcweir } 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir static boolean compareArrays(Object op1, Object op2) throws Exception { 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir if (op1 instanceof PropertyValue[] && op2 instanceof PropertyValue[]) { 104*cdf0e10cSrcweir return compareArrayOfPropertyValue((PropertyValue[])op1,(PropertyValue[])op2); 105*cdf0e10cSrcweir } 106*cdf0e10cSrcweir boolean result = true; 107*cdf0e10cSrcweir if((op1.getClass().getComponentType() == op2.getClass().getComponentType()) 108*cdf0e10cSrcweir && (Array.getLength(op1) == Array.getLength(op2))) 109*cdf0e10cSrcweir { 110*cdf0e10cSrcweir Class zClass = op1.getClass().getComponentType(); 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir for(int i = 0; i < Array.getLength(op1); ++ i) 113*cdf0e10cSrcweir result = result & compareObjects(Array.get(op1, i), Array.get(op2, i)); 114*cdf0e10cSrcweir } else { 115*cdf0e10cSrcweir result = false ; 116*cdf0e10cSrcweir } 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir return result; 119*cdf0e10cSrcweir } 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir static boolean compareInterfaces(XInterface op1, XInterface op2) { 122*cdf0e10cSrcweir return op1 == op2; 123*cdf0e10cSrcweir } 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir static boolean compareUntil(Class zClass, Class untilClass, Object op1, Object op2) throws Exception { 126*cdf0e10cSrcweir boolean result = true; 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir // write inherited members first 129*cdf0e10cSrcweir Class superClass = zClass.getSuperclass(); 130*cdf0e10cSrcweir if(superClass != null && superClass != untilClass) 131*cdf0e10cSrcweir result = result & compareUntil(superClass, untilClass, op1, op2); 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir Field fields[] = zClass.getDeclaredFields(); 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir for(int i = 0; i < fields.length && result; ++ i) { 136*cdf0e10cSrcweir if((fields[i].getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) == 0) { // neither static nor transient ? 137*cdf0e10cSrcweir Object obj1 = fields[i].get(op1); 138*cdf0e10cSrcweir Object obj2 = fields[i].get(op2); 139*cdf0e10cSrcweir if (obj1 instanceof com.sun.star.uno.Any) { 140*cdf0e10cSrcweir try { 141*cdf0e10cSrcweir if (utils.isVoid(obj1)) { 142*cdf0e10cSrcweir obj1 = null; 143*cdf0e10cSrcweir } else { 144*cdf0e10cSrcweir obj1 = AnyConverter.toObject(((Any) obj1).getType(),obj1); 145*cdf0e10cSrcweir } 146*cdf0e10cSrcweir } catch (com.sun.star.lang.IllegalArgumentException iae) { 147*cdf0e10cSrcweir } 148*cdf0e10cSrcweir } 149*cdf0e10cSrcweir if (obj2 instanceof com.sun.star.uno.Any) { 150*cdf0e10cSrcweir try { 151*cdf0e10cSrcweir if (utils.isVoid(obj2)) { 152*cdf0e10cSrcweir obj2 = null; 153*cdf0e10cSrcweir } else { 154*cdf0e10cSrcweir obj2 = AnyConverter.toObject(((Any) obj2).getType(),obj2); 155*cdf0e10cSrcweir } 156*cdf0e10cSrcweir } catch (com.sun.star.lang.IllegalArgumentException iae) { 157*cdf0e10cSrcweir } 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir result = result & compareObjects(obj1, obj2); 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir } 163*cdf0e10cSrcweir } 164*cdf0e10cSrcweir 165*cdf0e10cSrcweir return result; 166*cdf0e10cSrcweir } 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir static boolean compareStructs(Object op1, Object op2) throws Exception { 169*cdf0e10cSrcweir boolean result = true; 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir if(op1.getClass() != op2.getClass()) 172*cdf0e10cSrcweir result = false; 173*cdf0e10cSrcweir else { 174*cdf0e10cSrcweir result = compareUntil(op1.getClass(), Object.class, op1, op2); 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir 177*cdf0e10cSrcweir return result; 178*cdf0e10cSrcweir } 179*cdf0e10cSrcweir 180*cdf0e10cSrcweir static boolean compareThrowable(Throwable op1, Throwable op2) throws Exception { 181*cdf0e10cSrcweir boolean result = true; 182*cdf0e10cSrcweir 183*cdf0e10cSrcweir if(op1.getClass() != op2.getClass()) 184*cdf0e10cSrcweir result = false; 185*cdf0e10cSrcweir else { 186*cdf0e10cSrcweir result = compareUntil(op1.getClass(), Throwable.class, op1, op2); 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir result = result & op1.getMessage().equals(op2.getMessage()); 189*cdf0e10cSrcweir } 190*cdf0e10cSrcweir 191*cdf0e10cSrcweir return result; 192*cdf0e10cSrcweir } 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir static boolean compareEnums(Enum en1, Enum en2) { 195*cdf0e10cSrcweir return en1.getValue() == en2.getValue(); 196*cdf0e10cSrcweir } 197*cdf0e10cSrcweir 198*cdf0e10cSrcweir static boolean compareObjects(Object op1, Object op2) throws Exception { 199*cdf0e10cSrcweir boolean result = false; 200*cdf0e10cSrcweir 201*cdf0e10cSrcweir if(op1 == op2) 202*cdf0e10cSrcweir result = true; 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir if ( (op1==null) || (op2 == null) ) { 205*cdf0e10cSrcweir result = (op1 == op2); 206*cdf0e10cSrcweir } 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir else if(op1.getClass().isPrimitive() && op2.getClass().isPrimitive()) 209*cdf0e10cSrcweir result = op1.equals(op2); 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir else if(op1.getClass() == Byte.class && op2.getClass() == Byte.class) 212*cdf0e10cSrcweir result = op1.equals(op2); 213*cdf0e10cSrcweir 214*cdf0e10cSrcweir else if(op1.getClass() == Type.class && op2.getClass() == Type.class) 215*cdf0e10cSrcweir result = op1.equals(op2); 216*cdf0e10cSrcweir 217*cdf0e10cSrcweir else if(op1.getClass() == Boolean.class && op2.getClass() == Boolean.class) 218*cdf0e10cSrcweir result = op1.equals(op2); 219*cdf0e10cSrcweir 220*cdf0e10cSrcweir else if(op1.getClass() == Short.class && op2.getClass() == Short.class) 221*cdf0e10cSrcweir result = op1.equals(op2); 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir else if(Throwable.class.isAssignableFrom(op1.getClass()) && Throwable.class.isAssignableFrom(op2.getClass())) 224*cdf0e10cSrcweir result = compareThrowable((Throwable)op1, (Throwable)op2); 225*cdf0e10cSrcweir 226*cdf0e10cSrcweir else if(op1.getClass() == Integer.class && op2.getClass() == Integer.class) 227*cdf0e10cSrcweir result = op1.equals(op2); 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir else if(op1.getClass() == Character.class && op2.getClass() == Character.class) 230*cdf0e10cSrcweir result = op1.equals(op2); 231*cdf0e10cSrcweir 232*cdf0e10cSrcweir else if(op1.getClass() == Long.class && op2.getClass() == Long.class) 233*cdf0e10cSrcweir result = op1.equals(op2); 234*cdf0e10cSrcweir 235*cdf0e10cSrcweir else if(op1.getClass() == Void.class && op2.getClass() == Void.class) 236*cdf0e10cSrcweir result = op1.equals(op2); 237*cdf0e10cSrcweir 238*cdf0e10cSrcweir else if(op1.getClass() == Float.class && op2.getClass() == Float.class) 239*cdf0e10cSrcweir result = op1.equals(op2); 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir else if(op1.getClass() == Double.class && op2.getClass() == Double.class) 242*cdf0e10cSrcweir result = op1.equals(op2); 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir else if(op1.getClass().isArray() && op2.getClass().isArray()) 245*cdf0e10cSrcweir result = compareArrays(op1, op2); 246*cdf0e10cSrcweir 247*cdf0e10cSrcweir else if(op1.getClass() == Void.class || op2.getClass() == void.class) // write nothing ? 248*cdf0e10cSrcweir result = true; 249*cdf0e10cSrcweir 250*cdf0e10cSrcweir else if(XInterface.class.isAssignableFrom(op1.getClass()) && XInterface.class.isAssignableFrom(op2.getClass())) 251*cdf0e10cSrcweir compareInterfaces((XInterface)op1, (XInterface)op2); 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir else if(Enum.class.isAssignableFrom(op1.getClass()) && Enum.class.isAssignableFrom(op2.getClass())) 254*cdf0e10cSrcweir compareEnums((Enum)op1, (Enum)op2); 255*cdf0e10cSrcweir 256*cdf0e10cSrcweir else if(op1.getClass() == String.class && op2.getClass() == String.class) // is it a String ? 257*cdf0e10cSrcweir result = ((String)op1).equals((String)op2); 258*cdf0e10cSrcweir 259*cdf0e10cSrcweir else // otherwise it must be a struct 260*cdf0e10cSrcweir result = compareStructs(op1, op2); 261*cdf0e10cSrcweir 262*cdf0e10cSrcweir return result; 263*cdf0e10cSrcweir } 264*cdf0e10cSrcweir 265*cdf0e10cSrcweir 266*cdf0e10cSrcweir } 267