1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
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
;
44 * This class accumulates all kinds of methods for accessing debug information
45 * from UNO implementations.
50 * Prints information about the supported interfaces of an implementation
52 * @param xTarget The implementation which should be analysed.
53 * @see com.sun.star.uno.XInterface
55 public static void printInterfaces(XInterface xTarget
) {
56 printInterfaces(xTarget
, false);
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
66 public static void printInterfaces(XInterface xTarget
,
67 boolean extendedInfo
){
68 Type
[] types
= getInterfaceTypes(xTarget
);
70 int nLen
= types
.length
;
71 for( int i
= 0; i
< nLen
; i
++ ) {
72 System
.out
.println(types
[i
].getTypeName());
74 printInterfaceInfo(types
[i
]);
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
87 public static Type
[] getInterfaceTypes(XInterface xTarget
) {
89 XTypeProvider xTypeProvider
= (XTypeProvider
)
90 UnoRuntime
.queryInterface( XTypeProvider
.class, xTarget
);
91 if( xTypeProvider
!= null )
92 types
= xTypeProvider
.getTypes();
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
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()))
121 * Prints information about an interface type.
123 * @param aType The type of the given interface.
124 * @see com.sun.star.uno.Type
126 public static void printInterfaceInfo(Type aType
) {
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() + "()");
135 catch (Exception ex
) {
136 System
.out
.println("Exception occurred while printing InterfaceInfo");
137 ex
.printStackTrace();
142 * Prints a string array to standard out.
144 * @param entries : The array to be printed.
146 public static void printArray( String
[] entries
) {
147 for ( int i
=0; i
< entries
.length
;i
++ ) {
148 System
.out
.println(entries
[i
]);
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
157 * @param name The name of the property.
158 * @see com.sun.star.beans.XPropertySet
160 public static void printPropertyInfo(XPropertySet PS
, String name
) {
161 printPropertyInfo(PS
, name
, new PrintWriter(System
.out
)) ;
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
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
173 public static void printPropertyInfo(XPropertySet PS
, String name
,
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
;
187 if ((attr
& PropertyAttribute
.BOUND
) != 0)
188 out
.println("\t-BOUND");
190 if ((attr
& PropertyAttribute
.CONSTRAINED
) != 0)
191 out
.println("\t-CONSTRAINED");
193 if ((attr
& PropertyAttribute
.MAYBEAMBIGUOUS
) != 0)
194 out
.println("\t-MAYBEAMBIGUOUS");
196 if ((attr
& PropertyAttribute
.MAYBEDEFAULT
) != 0)
197 out
.println("\t-MAYBEDEFAULT");
199 if ((attr
& PropertyAttribute
.MAYBEVOID
) != 0)
200 out
.println("\t-MAYBEVOID");
202 if ((attr
& PropertyAttribute
.READONLY
) != 0)
203 out
.println("\t-READONLY");
205 if ((attr
& PropertyAttribute
.REMOVEABLE
) != 0)
206 out
.println("\t-REMOVEABLE");
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
);
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
223 public static void printProperyValueSequenzePairs(PropertyValue
[] ps
){
224 for( int i
= 0; i
< ps
.length
; i
++){
225 printProperyValuePairs(ps
[i
], new PrintWriter(System
.out
));
230 * Print the names and the values of a sequenze of <code>PropertyValue</code>
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
236 public static void printProperyValueSequenzePairs(PropertyValue
[] ps
, PrintWriter out
){
237 for( int i
= 0; i
< ps
.length
; i
++){
238 printProperyValuePairs(ps
[i
], out
);
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
247 public static void printProperyValuePairs(PropertyValue ps
){
248 printProperyValuePairs(ps
, new PrintWriter(System
.out
));
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
257 public static void printProperyValuePairs(PropertyValue ps
, PrintWriter out
){
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){
264 for( int i
=0; i
< values
.length
; i
++){
265 oneValue
+= values
[i
];
266 if (i
+1 < values
.length
) oneValue
+= "';'";
270 out
.println("--------");
271 out
.println(" Name: '" + ps
.Name
+ "' contains String[]:");
272 out
.println(oneValue
);
273 out
.println("--------");
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("--------");
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("--------");
288 out
.println("Name: '" + ps
.Name
+ "' Value: '" + ps
.Value
.toString() + "'");
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
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
);
305 * Print the supported services of a UNO object.
306 * @param aObject A UNO object.
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
];
320 if (notSupportedServices
)
321 System
.out
.println(str
);
325 * Get the unique implementation id of a UNO object.
326 * @param xTarget An implementation of a UNO object.
327 * @return The implementation id.
329 public static String
getImplID( XInterface xTarget
) {
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
]);
341 str
= "No Implementation ID available";