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