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