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