summaryrefslogtreecommitdiffstats
path: root/qadevOOo/runner/util/ValueComparer.java
blob: a581ad3cbb01d99caab09ed57c266331ecfcf1f5 (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
/*
 * 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;

import com.sun.star.beans.PropertyValue;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import com.sun.star.uno.Type;
import com.sun.star.uno.Enum;
import com.sun.star.uno.XInterface;
import com.sun.star.uno.Any;
import com.sun.star.uno.AnyConverter;
import java.util.HashMap;


public class ValueComparer {

    // Method to change a Value, thought for properties
    public static boolean equalValue( Object first, Object second ) {

        if (first instanceof com.sun.star.uno.Any) {
            try {
                first = AnyConverter.toObject(((Any) first).getType(),first);
            } catch (com.sun.star.lang.IllegalArgumentException iae) {
            }
        }
        if (second instanceof com.sun.star.uno.Any) {
            try {
                second = AnyConverter.toObject(((Any) second).getType(),second);
            } catch (com.sun.star.lang.IllegalArgumentException iae) {
            }
        }
        boolean eq = false;
        try {
            if ( (first==null) || (second == null) ) {
                eq = (first == second);
            }
            else {
                if ( util.utils.isVoid(first) && util.utils.isVoid(second) ) {
                    eq=true;
                } else if ( util.utils.isVoid(first) || util.utils.isVoid(second) ) {
                    eq = (first == second);
                } else {
                    eq = compareObjects(first, second);
                }
            }
        }
        catch (Exception e) {
            System.out.println("Exception occurred while comparing Objects");
            e.printStackTrace();
        }
        return eq;
    } // end of equalValue

    private static boolean compareArrayOfPropertyValue(PropertyValue[] pv1, PropertyValue[] pv2){
        if ( pv1.length != pv2.length) {
            return  false;
        }
        HashMap<String, Object> hm1 = new HashMap<String, Object>();
        boolean result = true;
        int i = 0;

        for (i = 0; i < pv1.length; i++){
            hm1.put(pv1[i].Name, pv1[i].Value);
        }

        i = 0;
        while (i < pv2.length && result) {
            result &= equalValue(hm1.get(pv2[i].Name),pv2[i].Value);
            i++;
        }
        return result;
    }

    private static boolean compareArrays(Object op1, Object op2) throws Exception {

        if (op1 instanceof PropertyValue[] && op2 instanceof PropertyValue[]) {
           return compareArrayOfPropertyValue((PropertyValue[])op1,(PropertyValue[])op2);
       }
        boolean result = true;
        if((op1.getClass().getComponentType() == op2.getClass().getComponentType())
           && (Array.getLength(op1) == Array.getLength(op2)))
        {
            for(int i = 0; i < Array.getLength(op1); ++ i)
                result = result & compareObjects(Array.get(op1, i), Array.get(op2, i));
        } else {
            result = false ;
        }

        return result;
    }

    private static boolean compareInterfaces(XInterface op1, XInterface op2) {
        return op1 == op2;
    }

    private static boolean compareUntil(Class<?> zClass, Class<?> untilClass, Object op1, Object op2) throws Exception {
        boolean result = true;

        // write inherited members first
        Class<?> superClass = zClass.getSuperclass();
        if(superClass != null && superClass != untilClass)
            result = result & compareUntil(superClass, untilClass, op1, op2);

        Field fields[] = zClass.getDeclaredFields();

        for(int i = 0; i < fields.length && result; ++ i) {
            if((fields[i].getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) == 0) { // neither static nor transient ?
                Object obj1 = fields[i].get(op1);
                Object obj2 = fields[i].get(op2);
                if (obj1 instanceof com.sun.star.uno.Any) {
                    try {
                        if (utils.isVoid(obj1)) {
                            obj1 = null;
                        } else {
                            obj1 = AnyConverter.toObject(((Any) obj1).getType(),obj1);
                        }
                    } catch (com.sun.star.lang.IllegalArgumentException iae) {
                    }
                }
                if (obj2 instanceof com.sun.star.uno.Any) {
                    try {
                        if (utils.isVoid(obj2)) {
                            obj2 = null;
                        } else {
                            obj2 = AnyConverter.toObject(((Any) obj2).getType(),obj2);
                        }
                    } catch (com.sun.star.lang.IllegalArgumentException iae) {
                    }
                }

                result = result & compareObjects(obj1, obj2);

            }
        }

        return result;
    }

    private static boolean compareStructs(Object op1, Object op2) throws Exception {
        boolean result = true;

        if(op1.getClass() != op2.getClass())
            result = false;
        else {
            result = compareUntil(op1.getClass(), Object.class, op1, op2);
        }

        return result;
    }

    private static boolean compareThrowable(Throwable op1, Throwable op2) throws Exception {
        boolean result = true;

        if(op1.getClass() != op2.getClass())
            result = false;
        else {
            result = compareUntil(op1.getClass(), Throwable.class, op1, op2);

            result = result & op1.getMessage().equals(op2.getMessage());
        }

        return result;
    }

    private static boolean compareEnums(Enum en1, Enum en2) {
        return en1.getValue() == en2.getValue();
    }

    private static boolean compareObjects(Object op1, Object op2) throws Exception {
        if(op1 == op2)
            return true;
        else if(op1==null || op2 == null)
            return op1 == op2;
        else if(op1.getClass().isPrimitive() && op2.getClass().isPrimitive())
            return op1.equals(op2);
        else if(op1.getClass() == Byte.class && op2.getClass() == Byte.class)
            return op1.equals(op2);
        else if(op1.getClass() == Type.class && op2.getClass() == Type.class)
            return op1.equals(op2);
        else if(op1.getClass() == Boolean.class && op2.getClass() == Boolean.class)
            return op1.equals(op2);
        else if(op1.getClass() == Short.class && op2.getClass() == Short.class)
            return op1.equals(op2);
        else if(Throwable.class.isAssignableFrom(op1.getClass()) && Throwable.class.isAssignableFrom(op2.getClass()))
            return compareThrowable((Throwable)op1, (Throwable)op2);
        else if(op1.getClass() == Integer.class && op2.getClass() == Integer.class)
            return op1.equals(op2);
        else if(op1.getClass() == Character.class && op2.getClass() == Character.class)
            return op1.equals(op2);
        else if(op1.getClass() == Long.class && op2.getClass() == Long.class)
            return op1.equals(op2);
        else if(op1.getClass() == Void.class && op2.getClass() == Void.class)
            return op1.equals(op2);
        else if(op1.getClass() == Float.class && op2.getClass() == Float.class)
            return op1.equals(op2);
        else if(op1.getClass() == Double.class && op2.getClass() == Double.class)
            return op1.equals(op2);
        else if(op1.getClass().isArray() && op2.getClass().isArray())
            return compareArrays(op1, op2);
        else if(op1.getClass() == Void.class || op2.getClass() == void.class) // write nothing ?
            return true;
        else if(XInterface.class.isAssignableFrom(op1.getClass()) && XInterface.class.isAssignableFrom(op2.getClass()))
            return compareInterfaces((XInterface)op1, (XInterface)op2);
        else if(Enum.class.isAssignableFrom(op1.getClass()) && Enum.class.isAssignableFrom(op2.getClass()))
            return compareEnums((Enum)op1, (Enum)op2);
        else if(op1.getClass() == String.class && op2.getClass() == String.class) // is it a String ?
            return ((String)op1).equals(op2);
        else // otherwise it must be a struct
            return compareStructs(op1, op2);
    }


}