summaryrefslogtreecommitdiff
path: root/qadevOOo/runner/util/dbg.java
blob: 99b0929bfbb4d8cfc461b1583610ef9fb43b3523 (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
/*
 * 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.uno.XInterface;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.Type;
import com.sun.star.beans.XPropertySet;
import com.sun.star.beans.XPropertySetInfo;
import com.sun.star.beans.Property;
import com.sun.star.beans.PropertyAttribute;
import com.sun.star.beans.PropertyValue;
import com.sun.star.lang.XTypeProvider;
import com.sun.star.lang.XServiceInfo;
import java.io.PrintWriter;
import java.lang.reflect.Method;

/**
 * This class accumulates all kinds of methods for accessing debug information
 * from UNO implementations.
 */
public class dbg {

    /**
     * Prints information about the supported interfaces of an implementation
     * to standard out.
     * @param xTarget The implementation which should be analysed.
     * @see com.sun.star.uno.XInterface
     */
    public static void printInterfaces(XInterface xTarget) {
        printInterfaces(xTarget, false);
    }

    /**
     * Prints information about the supported interfaces of an implementation
     * to standard out. Extended information can be printed.
     * @param xTarget The implementation which should be analysed.
     * @param extendedInfo Should extended information be printed?
     * @see com.sun.star.uno.XInterface
     */
    private static void printInterfaces(XInterface xTarget,
                                                    boolean extendedInfo){
        Type[] types = getInterfaceTypes(xTarget);
        if( null != types ) {
            int nLen = types.length;
            for( int i = 0; i < nLen ; i++ ) {
                System.out.println(types[i].getTypeName());
                if (extendedInfo) {
                    printInterfaceInfo(types[i]);
                    System.out.println();
                }
            }
        }
    }

    /**
     * Returns all interface types of an implementation as a type array.
     * @param xTarget The implementation which should be analyzed.
     * @return An array with all interface types; null if there are none.
     * @see com.sun.star.uno.XInterface
     */
    private static Type[] getInterfaceTypes(XInterface xTarget) {
        Type[] types = null;
        XTypeProvider xTypeProvider = UnoRuntime.queryInterface( XTypeProvider.class, xTarget);
        if( xTypeProvider != null )
            types = xTypeProvider.getTypes();
        return types;
    }

    /**
     * Returns true if a specified target implements the interface with the
     * given name. Note that the comparison is not case sensitive.
     * @param xTarget The implementation which should be analysed.
     * @param ifcName The name of the interface that is tested. The name can
     * be full qualified, such as 'com.sun.star.io.XInputStream', or only
     * consist of the interface name, such as 'XText'.
     * @return True, if xTarget implements the interface named ifcType
     * @see com.sun.star.uno.XInterface
     */
    public static boolean implementsInterface(
                                    XInterface xTarget, String ifcName) {
        Type[] types = getInterfaceTypes(xTarget);
        if( null != types ) {
            int nLen = types.length;
            for( int i = 0; i < nLen ; i++ ) {
                if(types[i].getTypeName().toLowerCase().endsWith(
                                                    ifcName.toLowerCase()))
                    return true;
            }
        }
        return false;
    }

    /**
     * Prints information about an interface type.
     *
     * @param aType The type of the given interface.
     * @see com.sun.star.uno.Type
     */
    private static void printInterfaceInfo(Type aType) {
        try {
            Class<?> zClass = aType.getZClass();
            Method[] methods = zClass.getDeclaredMethods();
            for (int i=0; i<methods.length; i++) {
                System.out.println("\t" + methods[i].getReturnType().getName()
                    + " " + methods[i].getName() + "()");
            }
        }
        catch (Exception ex) {
            System.out.println("Exception occurred while printing InterfaceInfo");
            ex.printStackTrace();
        }
    }

    /**
     * Prints a string array to standard out.
     *
     * @param entries : The array to be printed.
     */
    public static void printArray( String [] entries ) {
            for ( int i=0; i< entries.length;i++ ) {
                    System.out.println(entries[i]);
            }
    }

    /**
     * Print all information about the property <code>name</code> from
     * the property set <code>PS</code> to standard out.
     * @param PS The property set which should contain a property called
     *           <code>name</code>.
     * @param name The name of the property.
     * @see com.sun.star.beans.XPropertySet
     */
    public static void printPropertyInfo(XPropertySet PS, String name) {
            printPropertyInfo(PS, name, new PrintWriter(System.out)) ;
    }

    /**
     * Print all information about the property <code>name</code> from
     * the property set <code>PS</code> to a print writer.
     * @param PS The property set which should contain a property called
     *           <code>name</code>.
     * @param name The name of the property.
     * @param out The print writer which is used as output.
     * @see com.sun.star.beans.XPropertySet
     */
    public static void printPropertyInfo(XPropertySet PS, String name,
                                                        PrintWriter out) {
        try {
            XPropertySetInfo PSI = PS.getPropertySetInfo();
            PSI.getProperties();
            Property prop = PSI.getPropertyByName(name);
            out.println("Property name is " + prop.Name);
            out.println("Property handle is " + prop.Handle);
            out.println("Property type is " + prop.Type.getTypeName());
            out.println("Property current value is " +
                                                    PS.getPropertyValue(name));
            out.println("Attributes :");
            short attr = prop.Attributes;

            if ((attr & PropertyAttribute.BOUND) != 0)
                    out.println("\t-BOUND");

            if ((attr & PropertyAttribute.CONSTRAINED) != 0)
                    out.println("\t-CONSTRAINED");

            if ((attr & PropertyAttribute.MAYBEAMBIGUOUS) != 0)
                    out.println("\t-MAYBEAMBIGUOUS");

            if ((attr & PropertyAttribute.MAYBEDEFAULT) != 0)
                    out.println("\t-MAYBEDEFAULT");

            if ((attr & PropertyAttribute.MAYBEVOID) != 0)
                    out.println("\t-MAYBEVOID");

            if ((attr & PropertyAttribute.READONLY) != 0)
                    out.println("\t-READONLY");

            if ((attr & PropertyAttribute.REMOVABLE) != 0)
                    out.println("\t-REMOVABLE");

            if ((attr & PropertyAttribute.TRANSIENT) != 0)
                    out.println("\t-TRANSIENT");
        } catch(com.sun.star.uno.Exception e) {
                out.println("Exception!!!!");
            e.printStackTrace(out);
        }
    }

    /**
     * Print the names and the values of a sequnze of <code>PropertyValue</code>
     * to to standard out.
     * @param ps The property which should displayed
     * @see com.sun.star.beans.PropertyValue
     */

    public static void printProperyValueSequenzePairs(PropertyValue[] ps){
        for( int i = 0; i < ps.length; i++){
            printProperyValuePairs(ps[i], new PrintWriter(System.out));
        }
    }

    /**
     * Print the names and the values of a sequenze of <code>PropertyValue</code>
     * to a print writer.
     * @param ps The property which should displayed
     * @param out The print writer which is used as output.
     * @see com.sun.star.beans.PropertyValue
     */
    private static void printProperyValueSequenzePairs(PropertyValue[] ps, PrintWriter out){
        for( int i = 0; i < ps.length; i++){
            printProperyValuePairs(ps[i], out);
        }
    }

    /**
     * Print the name and the value of a <code>PropertyValue</code> to to standard out.
     * @param ps The property which should displayed
     * @see com.sun.star.beans.PropertyValue
     */
    public static void printProperyValuePairs(PropertyValue ps){
        printProperyValuePairs(ps, new PrintWriter(System.out));
    }

    /**
     * Print the name and the value of a <code>PropertyValue</code> to a print writer.
     * @param ps The property which should displayed
     * @param out The print writer which is used as output.
     * @see com.sun.star.beans.PropertyValue
     */
    private static void printProperyValuePairs(PropertyValue ps, PrintWriter out){

        if (ps.Value instanceof String[] ){
            String[] values = (String[]) ps.Value;
            String oneValue = "value is an empty String[]";
            if (values.length > 0){
                oneValue = "['";
                for( int i=0; i < values.length; i++){
                    oneValue += values[i];
                    if (i+1 < values.length) oneValue += "';'";
                }
                oneValue += "']";
            }
            out.println("--------");
            out.println("   Name: '" + ps.Name + "' contains String[]:");
            out.println(oneValue);
            out.println("--------");

        } else if (ps.Value instanceof PropertyValue){
            out.println("--------");
            out.println("   Name: '" + ps.Name + "' contains PropertyValue:");
            printProperyValuePairs((PropertyValue)ps.Value, out);
            out.println("--------");

        } else if (ps.Value instanceof PropertyValue[]){
            out.println("--------");
            out.println("   Name: '" + ps.Name + "' contains PropertyValue[]:");
            printProperyValueSequenzePairs((PropertyValue[])ps.Value, out);
            out.println("--------");

        } else {
            out.println("Name: '" + ps.Name + "' Value: '" + ps.Value.toString() + "'");
        }
    }

    /**
     * Print the names of all properties inside this property set
     * @param ps The property set which is printed.
     * @see com.sun.star.beans.XPropertySet
     */
    public static void printPropertiesNames(XPropertySet ps) {
            XPropertySetInfo psi = ps.getPropertySetInfo();
            Property[] props = psi.getProperties();
            for (int i = 0; i < props.length; i++)
                    System.out.println(i + ".  " + props[i].Name);
    }

    /**
     * Print the supported services of a UNO object.
     * @param aObject A UNO object.
     */
    public static void getSuppServices (Object aObject) {
        XServiceInfo xSI = UnoRuntime.queryInterface(XServiceInfo.class,aObject);
        printArray(xSI.getSupportedServiceNames());
        String str="Therein not Supported Service";
        boolean notSupportedServices = false;
        for (int i=0;i<xSI.getSupportedServiceNames().length;i++) {
            if (! xSI.supportsService(xSI.getSupportedServiceNames()[i])) {
                notSupportedServices = true;
                str+="\n" + xSI.getSupportedServiceNames()[i];
            }
        }
        if (notSupportedServices)
            System.out.println(str);
    }
}