xref: /AOO41X/main/qadevOOo/runner/util/dbg.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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.uno.XInterface;
31*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
32*cdf0e10cSrcweir import com.sun.star.uno.Type;
33*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
34*cdf0e10cSrcweir import com.sun.star.beans.XPropertySetInfo;
35*cdf0e10cSrcweir import com.sun.star.beans.Property;
36*cdf0e10cSrcweir import com.sun.star.beans.PropertyAttribute;
37*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
38*cdf0e10cSrcweir import com.sun.star.lang.XTypeProvider;
39*cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
40*cdf0e10cSrcweir import java.io.PrintWriter;
41*cdf0e10cSrcweir import java.lang.reflect.Method;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir /**
44*cdf0e10cSrcweir  * This class accumulates all kinds of methods for accessing debug information
45*cdf0e10cSrcweir  * from UNO implementations.
46*cdf0e10cSrcweir  */
47*cdf0e10cSrcweir public class dbg {
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir     /**
50*cdf0e10cSrcweir      * Prints information about the supported interfaces of an implementation
51*cdf0e10cSrcweir      * to standard out.
52*cdf0e10cSrcweir      * @param xTarget The implementation which should be analysed.
53*cdf0e10cSrcweir      * @see com.sun.star.uno.XInterface
54*cdf0e10cSrcweir      */
55*cdf0e10cSrcweir     public static void printInterfaces(XInterface xTarget) {
56*cdf0e10cSrcweir         printInterfaces(xTarget, false);
57*cdf0e10cSrcweir     }
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir     /**
60*cdf0e10cSrcweir      * Prints information about the supported interfaces of an implementation
61*cdf0e10cSrcweir      * to standard out. Extended information can be printed.
62*cdf0e10cSrcweir      * @param xTarget The implementation which should be analysed.
63*cdf0e10cSrcweir      * @param extendedInfo Should extended information be printed?
64*cdf0e10cSrcweir      * @see com.sun.star.uno.XInterface
65*cdf0e10cSrcweir      */
66*cdf0e10cSrcweir     public static void printInterfaces(XInterface xTarget,
67*cdf0e10cSrcweir                                                     boolean extendedInfo){
68*cdf0e10cSrcweir         Type[] types = getInterfaceTypes(xTarget);
69*cdf0e10cSrcweir         if( null != types ) {
70*cdf0e10cSrcweir             int nLen = types.length;
71*cdf0e10cSrcweir             for( int i = 0; i < nLen ; i++ ) {
72*cdf0e10cSrcweir                 System.out.println(types[i].getTypeName());
73*cdf0e10cSrcweir                 if (extendedInfo) {
74*cdf0e10cSrcweir                     printInterfaceInfo(types[i]);
75*cdf0e10cSrcweir                     System.out.println();
76*cdf0e10cSrcweir                 }
77*cdf0e10cSrcweir             }
78*cdf0e10cSrcweir         }
79*cdf0e10cSrcweir     }
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir     /**
82*cdf0e10cSrcweir      * Returns all interface types of an implementation as a type array.
83*cdf0e10cSrcweir      * @param xTarget The implementation which should be analyzed.
84*cdf0e10cSrcweir      * @return An array with all interface types; null if there are none.
85*cdf0e10cSrcweir      * @see com.sun.star.uno.XInterface
86*cdf0e10cSrcweir      */
87*cdf0e10cSrcweir     public static Type[] getInterfaceTypes(XInterface xTarget) {
88*cdf0e10cSrcweir         Type[] types = null;
89*cdf0e10cSrcweir         XTypeProvider xTypeProvider = (XTypeProvider)
90*cdf0e10cSrcweir                 UnoRuntime.queryInterface( XTypeProvider.class, xTarget);
91*cdf0e10cSrcweir         if( xTypeProvider != null )
92*cdf0e10cSrcweir             types = xTypeProvider.getTypes();
93*cdf0e10cSrcweir         return types;
94*cdf0e10cSrcweir     }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir     /**
97*cdf0e10cSrcweir      * Returns true if a specified target implements the interface with the
98*cdf0e10cSrcweir      * given name. Note that the comparison is not case sensitive.
99*cdf0e10cSrcweir      * @param xTarget The implementation which should be analysed.
100*cdf0e10cSrcweir      * @param ifcName The name of the interface that is tested. The name can
101*cdf0e10cSrcweir      * be full qualified, such as 'com.sun.star.io.XInputStream', or only
102*cdf0e10cSrcweir      * consist of the interface name, such as 'XText'.
103*cdf0e10cSrcweir      * @return True, if xTarget implements the interface named ifcType
104*cdf0e10cSrcweir      * @see com.sun.star.uno.XInterface
105*cdf0e10cSrcweir      */
106*cdf0e10cSrcweir     public static boolean implementsInterface(
107*cdf0e10cSrcweir                                     XInterface xTarget, String ifcName) {
108*cdf0e10cSrcweir         Type[] types = getInterfaceTypes(xTarget);
109*cdf0e10cSrcweir         if( null != types ) {
110*cdf0e10cSrcweir             int nLen = types.length;
111*cdf0e10cSrcweir             for( int i = 0; i < nLen ; i++ ) {
112*cdf0e10cSrcweir                 if(types[i].getTypeName().toLowerCase().endsWith(
113*cdf0e10cSrcweir                                                     ifcName.toLowerCase()))
114*cdf0e10cSrcweir                     return true;
115*cdf0e10cSrcweir             }
116*cdf0e10cSrcweir         }
117*cdf0e10cSrcweir         return false;
118*cdf0e10cSrcweir     }
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir     /**
121*cdf0e10cSrcweir      * Prints information about an interface type.
122*cdf0e10cSrcweir      *
123*cdf0e10cSrcweir      * @param aType The type of the given interface.
124*cdf0e10cSrcweir      * @see com.sun.star.uno.Type
125*cdf0e10cSrcweir      */
126*cdf0e10cSrcweir     public static void printInterfaceInfo(Type aType) {
127*cdf0e10cSrcweir         try {
128*cdf0e10cSrcweir             Class zClass = aType.getZClass();
129*cdf0e10cSrcweir             Method[] methods = zClass.getDeclaredMethods();
130*cdf0e10cSrcweir             for (int i=0; i<methods.length; i++) {
131*cdf0e10cSrcweir                 System.out.println("\t" + methods[i].getReturnType().getName()
132*cdf0e10cSrcweir                     + " " + methods[i].getName() + "()");
133*cdf0e10cSrcweir             }
134*cdf0e10cSrcweir         }
135*cdf0e10cSrcweir         catch (Exception ex) {
136*cdf0e10cSrcweir             System.out.println("Exception occured while printing InterfaceInfo");
137*cdf0e10cSrcweir             ex.printStackTrace();
138*cdf0e10cSrcweir         }
139*cdf0e10cSrcweir     }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir     /**
142*cdf0e10cSrcweir      * Prints a string array to standard out.
143*cdf0e10cSrcweir      *
144*cdf0e10cSrcweir      * @param entries : The array to be printed.
145*cdf0e10cSrcweir      */
146*cdf0e10cSrcweir     public static void printArray( String [] entries ) {
147*cdf0e10cSrcweir             for ( int i=0; i< entries.length;i++ ) {
148*cdf0e10cSrcweir                     System.out.println(entries[i]);
149*cdf0e10cSrcweir             }
150*cdf0e10cSrcweir     }
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir     /**
153*cdf0e10cSrcweir      * Print all information about the property <code>name</code> from
154*cdf0e10cSrcweir      * the property set <code>PS</code> to standard out.
155*cdf0e10cSrcweir      * @param PS The property set which should contain a property called
156*cdf0e10cSrcweir      * 			 <code>name</code>.
157*cdf0e10cSrcweir      * @param name The name of the property.
158*cdf0e10cSrcweir      * @see com.sun.star.beans.XPropertySet
159*cdf0e10cSrcweir      */
160*cdf0e10cSrcweir     public static void printPropertyInfo(XPropertySet PS, String name) {
161*cdf0e10cSrcweir             printPropertyInfo(PS, name, new PrintWriter(System.out)) ;
162*cdf0e10cSrcweir     }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir     /**
165*cdf0e10cSrcweir      * Print all information about the property <code>name</code> from
166*cdf0e10cSrcweir      * the property set <code>PS</code> to a print writer.
167*cdf0e10cSrcweir      * @param PS The property set which should contain a property called
168*cdf0e10cSrcweir      * 			 <code>name</code>.
169*cdf0e10cSrcweir      * @param name The name of the property.
170*cdf0e10cSrcweir      * @param out The print writer which is used as output.
171*cdf0e10cSrcweir      * @see com.sun.star.beans.XPropertySet
172*cdf0e10cSrcweir      */
173*cdf0e10cSrcweir     public static void printPropertyInfo(XPropertySet PS, String name,
174*cdf0e10cSrcweir                                                         PrintWriter out) {
175*cdf0e10cSrcweir         try {
176*cdf0e10cSrcweir             XPropertySetInfo PSI = PS.getPropertySetInfo();
177*cdf0e10cSrcweir             Property[] props = PSI.getProperties();
178*cdf0e10cSrcweir             Property prop = PSI.getPropertyByName(name);
179*cdf0e10cSrcweir             out.println("Property name is " + prop.Name);
180*cdf0e10cSrcweir             out.println("Property handle is " + prop.Handle);
181*cdf0e10cSrcweir             out.println("Property type is " + prop.Type.getTypeName());
182*cdf0e10cSrcweir             out.println("Property current value is " +
183*cdf0e10cSrcweir                                                     PS.getPropertyValue(name));
184*cdf0e10cSrcweir             out.println("Attributes :");
185*cdf0e10cSrcweir             short attr = prop.Attributes;
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir             if ((attr & PropertyAttribute.BOUND) != 0)
188*cdf0e10cSrcweir                     out.println("\t-BOUND");
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir             if ((attr & PropertyAttribute.CONSTRAINED) != 0)
191*cdf0e10cSrcweir                     out.println("\t-CONSTRAINED");
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir             if ((attr & PropertyAttribute.MAYBEAMBIGUOUS) != 0)
194*cdf0e10cSrcweir                     out.println("\t-MAYBEAMBIGUOUS");
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir             if ((attr & PropertyAttribute.MAYBEDEFAULT) != 0)
197*cdf0e10cSrcweir                     out.println("\t-MAYBEDEFAULT");
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir             if ((attr & PropertyAttribute.MAYBEVOID) != 0)
200*cdf0e10cSrcweir                     out.println("\t-MAYBEVOID");
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir             if ((attr & PropertyAttribute.READONLY) != 0)
203*cdf0e10cSrcweir                     out.println("\t-READONLY");
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir             if ((attr & PropertyAttribute.REMOVEABLE) != 0)
206*cdf0e10cSrcweir                     out.println("\t-REMOVEABLE");
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir             if ((attr & PropertyAttribute.TRANSIENT) != 0)
209*cdf0e10cSrcweir                     out.println("\t-TRANSIENT");
210*cdf0e10cSrcweir         } catch(com.sun.star.uno.Exception e) {
211*cdf0e10cSrcweir                 out.println("Exception!!!!");
212*cdf0e10cSrcweir             e.printStackTrace(out);
213*cdf0e10cSrcweir         }
214*cdf0e10cSrcweir     }
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir     /**
217*cdf0e10cSrcweir      * Print the names and the values of a sequnze of <code>PropertyValue</code>
218*cdf0e10cSrcweir      * to to standard out.
219*cdf0e10cSrcweir      * @param ps The property which should displayed
220*cdf0e10cSrcweir      * @see com.sun.star.beans.PropertyValue
221*cdf0e10cSrcweir      */
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir     public static void printProperyValueSequenzePairs(PropertyValue[] ps){
224*cdf0e10cSrcweir         for( int i = 0; i < ps.length; i++){
225*cdf0e10cSrcweir             printProperyValuePairs(ps[i], new PrintWriter(System.out));
226*cdf0e10cSrcweir         }
227*cdf0e10cSrcweir     }
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir     /**
230*cdf0e10cSrcweir      * Print the names and the values of a sequenze of <code>PropertyValue</code>
231*cdf0e10cSrcweir      * to a print writer.
232*cdf0e10cSrcweir      * @param ps The property which should displayed
233*cdf0e10cSrcweir      * @param out The print writer which is used as output.
234*cdf0e10cSrcweir      * @see com.sun.star.beans.PropertyValue
235*cdf0e10cSrcweir      */
236*cdf0e10cSrcweir     public static void printProperyValueSequenzePairs(PropertyValue[] ps, PrintWriter out){
237*cdf0e10cSrcweir         for( int i = 0; i < ps.length; i++){
238*cdf0e10cSrcweir             printProperyValuePairs(ps[i], out);
239*cdf0e10cSrcweir         }
240*cdf0e10cSrcweir     }
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir     /**
243*cdf0e10cSrcweir      * Print the name and the value of a <code>PropertyValue</code> to to standard out.
244*cdf0e10cSrcweir      * @param ps The property which should displayed
245*cdf0e10cSrcweir      * @see com.sun.star.beans.PropertyValue
246*cdf0e10cSrcweir      */
247*cdf0e10cSrcweir     public static void printProperyValuePairs(PropertyValue ps){
248*cdf0e10cSrcweir         printProperyValuePairs(ps, new PrintWriter(System.out));
249*cdf0e10cSrcweir     }
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir     /**
252*cdf0e10cSrcweir      * Print the name and the value of a <code>PropertyValue</code> to a print writer.
253*cdf0e10cSrcweir      * @param ps The property which should displayed
254*cdf0e10cSrcweir      * @param out The print writer which is used as output.
255*cdf0e10cSrcweir      * @see com.sun.star.beans.PropertyValue
256*cdf0e10cSrcweir      */
257*cdf0e10cSrcweir     public static void printProperyValuePairs(PropertyValue ps, PrintWriter out){
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir         if (ps.Value instanceof String[] ){
260*cdf0e10cSrcweir             String[] values = (String[]) ps.Value;
261*cdf0e10cSrcweir             String oneValue = "value is an empty String[]";
262*cdf0e10cSrcweir             if (values.length > 0){
263*cdf0e10cSrcweir                 oneValue = "['";
264*cdf0e10cSrcweir                 for( int i=0; i < values.length; i++){
265*cdf0e10cSrcweir                     oneValue += values[i];
266*cdf0e10cSrcweir                     if (i+1 < values.length) oneValue += "';'";
267*cdf0e10cSrcweir                 }
268*cdf0e10cSrcweir                 oneValue += "']";
269*cdf0e10cSrcweir             }
270*cdf0e10cSrcweir             out.println("--------");
271*cdf0e10cSrcweir             out.println("   Name: '" + ps.Name + "' contains String[]:");
272*cdf0e10cSrcweir             out.println(oneValue);
273*cdf0e10cSrcweir             out.println("--------");
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir         } else if (ps.Value instanceof PropertyValue){
276*cdf0e10cSrcweir             out.println("--------");
277*cdf0e10cSrcweir             out.println("   Name: '" + ps.Name + "' contains PropertyValue:");
278*cdf0e10cSrcweir             printProperyValuePairs((PropertyValue)ps.Value, out);
279*cdf0e10cSrcweir             out.println("--------");
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir         } else if (ps.Value instanceof PropertyValue[]){
282*cdf0e10cSrcweir             out.println("--------");
283*cdf0e10cSrcweir             out.println("   Name: '" + ps.Name + "' contains PropertyValue[]:");
284*cdf0e10cSrcweir             printProperyValueSequenzePairs((PropertyValue[])ps.Value, out);
285*cdf0e10cSrcweir             out.println("--------");
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir         } else {
288*cdf0e10cSrcweir             out.println("Name: '" + ps.Name + "' Value: '" + ps.Value.toString() + "'");
289*cdf0e10cSrcweir         }
290*cdf0e10cSrcweir     }
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir     /**
293*cdf0e10cSrcweir      * Print the names of all properties inside this property set
294*cdf0e10cSrcweir      * @param ps The property set which is printed.
295*cdf0e10cSrcweir      * @see com.sun.star.beans.XPropertySet
296*cdf0e10cSrcweir      */
297*cdf0e10cSrcweir     public static void printPropertiesNames(XPropertySet ps) {
298*cdf0e10cSrcweir             XPropertySetInfo psi = ps.getPropertySetInfo();
299*cdf0e10cSrcweir             Property[] props = psi.getProperties();
300*cdf0e10cSrcweir             for (int i = 0; i < props.length; i++)
301*cdf0e10cSrcweir                     System.out.println(i + ".  " + props[i].Name);
302*cdf0e10cSrcweir     }
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir     /**
305*cdf0e10cSrcweir      * Print the supported services of a UNO object.
306*cdf0e10cSrcweir      * @param aObject A UNO object.
307*cdf0e10cSrcweir      */
308*cdf0e10cSrcweir     public static void getSuppServices (Object aObject) {
309*cdf0e10cSrcweir         XServiceInfo xSI = (XServiceInfo)
310*cdf0e10cSrcweir                 UnoRuntime.queryInterface(XServiceInfo.class,aObject);
311*cdf0e10cSrcweir         printArray(xSI.getSupportedServiceNames());
312*cdf0e10cSrcweir         String str="Therein not Supported Service";
313*cdf0e10cSrcweir 		boolean notSupportedServices = false;
314*cdf0e10cSrcweir         for (int i=0;i<xSI.getSupportedServiceNames().length;i++) {
315*cdf0e10cSrcweir             if (! xSI.supportsService(xSI.getSupportedServiceNames()[i])) {
316*cdf0e10cSrcweir 				notSupportedServices = true;
317*cdf0e10cSrcweir                 str+="\n" + xSI.getSupportedServiceNames()[i];
318*cdf0e10cSrcweir             }
319*cdf0e10cSrcweir         }
320*cdf0e10cSrcweir 		if (notSupportedServices)
321*cdf0e10cSrcweir 			System.out.println(str);
322*cdf0e10cSrcweir     }
323*cdf0e10cSrcweir 
324*cdf0e10cSrcweir     /**
325*cdf0e10cSrcweir      * Get the unique implementation id of a UNO object.
326*cdf0e10cSrcweir      * @param xTarget An implementation of a UNO object.
327*cdf0e10cSrcweir      * @return The implementation id.
328*cdf0e10cSrcweir      */
329*cdf0e10cSrcweir     public static String getImplID( XInterface xTarget ) {
330*cdf0e10cSrcweir     String str = "";
331*cdf0e10cSrcweir     XTypeProvider xTypeProvider = (XTypeProvider)
332*cdf0e10cSrcweir                 UnoRuntime.queryInterface( XTypeProvider.class, xTarget);
333*cdf0e10cSrcweir             if( xTypeProvider != null ) {
334*cdf0e10cSrcweir         byte[] id = xTypeProvider.getImplementationId();
335*cdf0e10cSrcweir         str = "ImplementationID: ";
336*cdf0e10cSrcweir         for (int i=0; i<id.length;i++) {
337*cdf0e10cSrcweir             Byte b = new Byte(id[i]);
338*cdf0e10cSrcweir             str += b.intValue();
339*cdf0e10cSrcweir         }
340*cdf0e10cSrcweir             } else {
341*cdf0e10cSrcweir         str = "No Implementation ID available";
342*cdf0e10cSrcweir     }
343*cdf0e10cSrcweir     return str;
344*cdf0e10cSrcweir     }
345*cdf0e10cSrcweir 
346*cdf0e10cSrcweir 
347*cdf0e10cSrcweir }
348