1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package util; 25 26 import com.sun.star.beans.PropertyValue; 27 import java.lang.reflect.Array; 28 import java.lang.reflect.Field; 29 import java.lang.reflect.Modifier; 30 import com.sun.star.uno.Type; 31 import com.sun.star.uno.Enum; 32 import com.sun.star.uno.XInterface; 33 import com.sun.star.uno.Any; 34 import com.sun.star.uno.AnyConverter; 35 import java.util.HashMap; 36 37 38 public class ValueComparer { 39 40 // Method to change a Value, thought for properties 41 public static boolean equalValue( Object first, Object second ) { 42 43 if (first instanceof com.sun.star.uno.Any) { 44 try { 45 first = AnyConverter.toObject(((Any) first).getType(),first); 46 } catch (com.sun.star.lang.IllegalArgumentException iae) { 47 } 48 } 49 if (second instanceof com.sun.star.uno.Any) { 50 try { 51 second = AnyConverter.toObject(((Any) second).getType(),second); 52 } catch (com.sun.star.lang.IllegalArgumentException iae) { 53 } 54 } 55 boolean eq = false; 56 try { 57 if ( (first==null) || (second == null) ) { 58 eq = (first == second); 59 } 60 else { 61 if ( util.utils.isVoid(first) && util.utils.isVoid(second) ) { 62 eq=true; 63 } else if ( util.utils.isVoid(first) || util.utils.isVoid(second) ) { 64 eq = (first == second); 65 } else { 66 eq = compareObjects(first, second); 67 } 68 } 69 } 70 catch (Exception e) { 71 System.out.println("Exception occured while comparing Objects"); 72 e.printStackTrace(); 73 } 74 return eq; 75 } // end of equalValue 76 77 static boolean compareArrayOfPropertyValue(PropertyValue[] pv1, PropertyValue[] pv2){ 78 if ( pv1.length != pv2.length) { 79 return false; 80 } 81 HashMap hm1 = new HashMap(); 82 boolean result = true; 83 int i = 0; 84 85 for (i = 0; i < pv1.length; i++){ 86 hm1.put(pv1[i].Name, pv1[i].Value); 87 } 88 89 i = 0; 90 while (i < pv2.length && result) { 91 result &= equalValue(hm1.get(pv2[i].Name),pv2[i].Value); 92 i++; 93 } 94 return result; 95 } 96 97 static boolean compareArrays(Object op1, Object op2) throws Exception { 98 99 if (op1 instanceof PropertyValue[] && op2 instanceof PropertyValue[]) { 100 return compareArrayOfPropertyValue((PropertyValue[])op1,(PropertyValue[])op2); 101 } 102 boolean result = true; 103 if((op1.getClass().getComponentType() == op2.getClass().getComponentType()) 104 && (Array.getLength(op1) == Array.getLength(op2))) 105 { 106 Class zClass = op1.getClass().getComponentType(); 107 108 for(int i = 0; i < Array.getLength(op1); ++ i) 109 result = result & compareObjects(Array.get(op1, i), Array.get(op2, i)); 110 } else { 111 result = false ; 112 } 113 114 return result; 115 } 116 117 static boolean compareInterfaces(XInterface op1, XInterface op2) { 118 return op1 == op2; 119 } 120 121 static boolean compareUntil(Class zClass, Class untilClass, Object op1, Object op2) throws Exception { 122 boolean result = true; 123 124 // write inherited members first 125 Class superClass = zClass.getSuperclass(); 126 if(superClass != null && superClass != untilClass) 127 result = result & compareUntil(superClass, untilClass, op1, op2); 128 129 Field fields[] = zClass.getDeclaredFields(); 130 131 for(int i = 0; i < fields.length && result; ++ i) { 132 if((fields[i].getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) == 0) { // neither static nor transient ? 133 Object obj1 = fields[i].get(op1); 134 Object obj2 = fields[i].get(op2); 135 if (obj1 instanceof com.sun.star.uno.Any) { 136 try { 137 if (utils.isVoid(obj1)) { 138 obj1 = null; 139 } else { 140 obj1 = AnyConverter.toObject(((Any) obj1).getType(),obj1); 141 } 142 } catch (com.sun.star.lang.IllegalArgumentException iae) { 143 } 144 } 145 if (obj2 instanceof com.sun.star.uno.Any) { 146 try { 147 if (utils.isVoid(obj2)) { 148 obj2 = null; 149 } else { 150 obj2 = AnyConverter.toObject(((Any) obj2).getType(),obj2); 151 } 152 } catch (com.sun.star.lang.IllegalArgumentException iae) { 153 } 154 } 155 156 result = result & compareObjects(obj1, obj2); 157 158 } 159 } 160 161 return result; 162 } 163 164 static boolean compareStructs(Object op1, Object op2) throws Exception { 165 boolean result = true; 166 167 if(op1.getClass() != op2.getClass()) 168 result = false; 169 else { 170 result = compareUntil(op1.getClass(), Object.class, op1, op2); 171 } 172 173 return result; 174 } 175 176 static boolean compareThrowable(Throwable op1, Throwable op2) throws Exception { 177 boolean result = true; 178 179 if(op1.getClass() != op2.getClass()) 180 result = false; 181 else { 182 result = compareUntil(op1.getClass(), Throwable.class, op1, op2); 183 184 result = result & op1.getMessage().equals(op2.getMessage()); 185 } 186 187 return result; 188 } 189 190 static boolean compareEnums(Enum en1, Enum en2) { 191 return en1.getValue() == en2.getValue(); 192 } 193 194 static boolean compareObjects(Object op1, Object op2) throws Exception { 195 boolean result = false; 196 197 if(op1 == op2) 198 result = true; 199 200 if ( (op1==null) || (op2 == null) ) { 201 result = (op1 == op2); 202 } 203 204 else if(op1.getClass().isPrimitive() && op2.getClass().isPrimitive()) 205 result = op1.equals(op2); 206 207 else if(op1.getClass() == Byte.class && op2.getClass() == Byte.class) 208 result = op1.equals(op2); 209 210 else if(op1.getClass() == Type.class && op2.getClass() == Type.class) 211 result = op1.equals(op2); 212 213 else if(op1.getClass() == Boolean.class && op2.getClass() == Boolean.class) 214 result = op1.equals(op2); 215 216 else if(op1.getClass() == Short.class && op2.getClass() == Short.class) 217 result = op1.equals(op2); 218 219 else if(Throwable.class.isAssignableFrom(op1.getClass()) && Throwable.class.isAssignableFrom(op2.getClass())) 220 result = compareThrowable((Throwable)op1, (Throwable)op2); 221 222 else if(op1.getClass() == Integer.class && op2.getClass() == Integer.class) 223 result = op1.equals(op2); 224 225 else if(op1.getClass() == Character.class && op2.getClass() == Character.class) 226 result = op1.equals(op2); 227 228 else if(op1.getClass() == Long.class && op2.getClass() == Long.class) 229 result = op1.equals(op2); 230 231 else if(op1.getClass() == Void.class && op2.getClass() == Void.class) 232 result = op1.equals(op2); 233 234 else if(op1.getClass() == Float.class && op2.getClass() == Float.class) 235 result = op1.equals(op2); 236 237 else if(op1.getClass() == Double.class && op2.getClass() == Double.class) 238 result = op1.equals(op2); 239 240 else if(op1.getClass().isArray() && op2.getClass().isArray()) 241 result = compareArrays(op1, op2); 242 243 else if(op1.getClass() == Void.class || op2.getClass() == void.class) // write nothing ? 244 result = true; 245 246 else if(XInterface.class.isAssignableFrom(op1.getClass()) && XInterface.class.isAssignableFrom(op2.getClass())) 247 compareInterfaces((XInterface)op1, (XInterface)op2); 248 249 else if(Enum.class.isAssignableFrom(op1.getClass()) && Enum.class.isAssignableFrom(op2.getClass())) 250 compareEnums((Enum)op1, (Enum)op2); 251 252 else if(op1.getClass() == String.class && op2.getClass() == String.class) // is it a String ? 253 result = ((String)op1).equals((String)op2); 254 255 else // otherwise it must be a struct 256 result = compareStructs(op1, op2); 257 258 return result; 259 } 260 261 262 } 263