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