1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.beans.IllegalTypeException;
25cdf0e10cSrcweir import com.sun.star.beans.MethodConcept;
26cdf0e10cSrcweir import com.sun.star.beans.NamedValue;
27cdf0e10cSrcweir import com.sun.star.beans.Property;
28cdf0e10cSrcweir import com.sun.star.beans.XIntrospection;
29cdf0e10cSrcweir import com.sun.star.beans.XIntrospectionAccess;
30cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
31cdf0e10cSrcweir import com.sun.star.bridge.UnoUrlResolver;
32cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
33cdf0e10cSrcweir import com.sun.star.comp.helper.Bootstrap;
34cdf0e10cSrcweir import com.sun.star.container.XEnumeration;
35cdf0e10cSrcweir import com.sun.star.container.XEnumerationAccess;
36cdf0e10cSrcweir import com.sun.star.container.XHierarchicalNameAccess;
37cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
38cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
39cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
40cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
41cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
42cdf0e10cSrcweir import com.sun.star.lang.XTypeProvider;
43cdf0e10cSrcweir import com.sun.star.lib.uno.helper.WeakBase;
44cdf0e10cSrcweir import com.sun.star.reflection.ParamInfo;
45cdf0e10cSrcweir import com.sun.star.reflection.TypeDescriptionSearchDepth;
46cdf0e10cSrcweir import com.sun.star.reflection.XConstantTypeDescription;
47cdf0e10cSrcweir import com.sun.star.reflection.XConstantsTypeDescription;
48cdf0e10cSrcweir import com.sun.star.reflection.XIdlClass;
49cdf0e10cSrcweir import com.sun.star.reflection.XIdlField;
50cdf0e10cSrcweir import com.sun.star.reflection.XIdlMethod;
51cdf0e10cSrcweir import com.sun.star.reflection.XIdlReflection;
52cdf0e10cSrcweir import com.sun.star.reflection.XIndirectTypeDescription;
53cdf0e10cSrcweir import com.sun.star.reflection.XInterfaceTypeDescription;
54cdf0e10cSrcweir import com.sun.star.reflection.XInterfaceTypeDescription2;
55cdf0e10cSrcweir import com.sun.star.reflection.XPropertyTypeDescription;
56cdf0e10cSrcweir import com.sun.star.reflection.XServiceTypeDescription;
57cdf0e10cSrcweir import com.sun.star.reflection.XTypeDescription;
58cdf0e10cSrcweir import com.sun.star.reflection.XTypeDescriptionEnumeration;
59cdf0e10cSrcweir import com.sun.star.reflection.XTypeDescriptionEnumerationAccess;
60cdf0e10cSrcweir import com.sun.star.ucb.CommandAbortedException;
61cdf0e10cSrcweir import com.sun.star.ucb.XSimpleFileAccess;
62cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
63cdf0e10cSrcweir import com.sun.star.uno.Type;
64cdf0e10cSrcweir import com.sun.star.uno.TypeClass;
65cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
66cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
67cdf0e10cSrcweir import com.sun.star.util.URL;
68cdf0e10cSrcweir import java.util.List;
69cdf0e10cSrcweir import java.util.Vector;
70cdf0e10cSrcweir import javax.swing.JOptionPane;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir public class Introspector extends WeakBase{
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     private XIntrospection m_xIntrospection;
75cdf0e10cSrcweir     private XMultiComponentFactory m_xMultiComponentFactory;
76cdf0e10cSrcweir     private XComponentContext m_xComponentContext;
77cdf0e10cSrcweir     private XTypeDescriptionEnumerationAccess m_xTDEnumerationAccess;
78cdf0e10cSrcweir     private static XComponentContext xOfficeComponentContext;
79cdf0e10cSrcweir     private XIdlReflection mxIdlReflection;
80cdf0e10cSrcweir     private URL openHyperlink;
81cdf0e10cSrcweir     private static Introspector m_oIntrospector = null;
82cdf0e10cSrcweir     private XSimpleFileAccess xSimpleFileAccess = null;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 
getIntrospector()86cdf0e10cSrcweir     public static Introspector getIntrospector(){
87cdf0e10cSrcweir         if (m_oIntrospector == null){
88cdf0e10cSrcweir             throw new NullPointerException();
89cdf0e10cSrcweir         }
90cdf0e10cSrcweir         else{
91cdf0e10cSrcweir             return m_oIntrospector;
92cdf0e10cSrcweir         }
93cdf0e10cSrcweir     }
94cdf0e10cSrcweir 
getIntrospector(XComponentContext _xComponentContext)95cdf0e10cSrcweir     public static Introspector getIntrospector(XComponentContext _xComponentContext){
96cdf0e10cSrcweir         if (m_oIntrospector == null){
97cdf0e10cSrcweir             m_oIntrospector =  new Introspector(_xComponentContext);
98cdf0e10cSrcweir         }
99cdf0e10cSrcweir         return m_oIntrospector;
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 
103cdf0e10cSrcweir     /** Creates a new instance of Introspection */
Introspector(XComponentContext _xComponentContext)104cdf0e10cSrcweir     private Introspector(XComponentContext _xComponentContext) {
105cdf0e10cSrcweir     try{
106cdf0e10cSrcweir         m_xComponentContext = _xComponentContext;
107cdf0e10cSrcweir         m_xMultiComponentFactory = m_xComponentContext.getServiceManager();
108cdf0e10cSrcweir         Object o = m_xMultiComponentFactory.createInstanceWithContext("com.sun.star.beans.Introspection", m_xComponentContext);
109cdf0e10cSrcweir         m_xIntrospection = ( XIntrospection ) UnoRuntime.queryInterface(XIntrospection.class, o );
110cdf0e10cSrcweir         Object oCoreReflection = getXMultiComponentFactory().createInstanceWithContext("com.sun.star.reflection.CoreReflection", getXComponentContext());
111cdf0e10cSrcweir         mxIdlReflection = (XIdlReflection) UnoRuntime.queryInterface(XIdlReflection.class, oCoreReflection);
112cdf0e10cSrcweir         initTypeDescriptionManager();
113cdf0e10cSrcweir     }
114cdf0e10cSrcweir     catch( Exception exception ) {
115cdf0e10cSrcweir         System.err.println( exception );
116cdf0e10cSrcweir     }}
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 
getXComponentContext()119cdf0e10cSrcweir     protected XComponentContext getXComponentContext(){
120cdf0e10cSrcweir         return m_xComponentContext;
121cdf0e10cSrcweir     }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 
getXMultiComponentFactory()124cdf0e10cSrcweir     protected XMultiComponentFactory getXMultiComponentFactory(){
125cdf0e10cSrcweir         return m_xMultiComponentFactory;
126cdf0e10cSrcweir     }
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 
getXIntrospectionAccess(Object _oUnoComponent)129cdf0e10cSrcweir     protected XIntrospectionAccess getXIntrospectionAccess(Object _oUnoComponent){
130cdf0e10cSrcweir         return m_xIntrospection.inspect(_oUnoComponent);
131cdf0e10cSrcweir     }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 
isContainer(Object _oUnoObject)134cdf0e10cSrcweir     public boolean isContainer(Object _oUnoObject){
135cdf0e10cSrcweir     boolean bIsContainer = false;
136cdf0e10cSrcweir     try {
137cdf0e10cSrcweir         XIntrospectionAccess xIntrospectionAccessObject = getXIntrospectionAccess(_oUnoObject);
138cdf0e10cSrcweir         if (xIntrospectionAccessObject != null){
139cdf0e10cSrcweir             XEnumerationAccess xEnumerationAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class, xIntrospectionAccessObject.queryAdapter( new Type( XEnumerationAccess.class ) ) );
140cdf0e10cSrcweir             if (xEnumerationAccess != null){
141cdf0e10cSrcweir                 XEnumeration xEnumeration = xEnumerationAccess.createEnumeration();
142cdf0e10cSrcweir                 bIsContainer = xEnumeration.hasMoreElements();
143cdf0e10cSrcweir             }
144cdf0e10cSrcweir             if (!bIsContainer){
145cdf0e10cSrcweir                 XIndexAccess xIndexAccess = (XIndexAccess) UnoRuntime.queryInterface( XIndexAccess.class, xIntrospectionAccessObject.queryAdapter(new Type( XIndexAccess.class )));
146cdf0e10cSrcweir                 if (xIndexAccess != null){
147cdf0e10cSrcweir                     bIsContainer = (xIndexAccess.getCount() > 0);
148cdf0e10cSrcweir                 }
149cdf0e10cSrcweir             }
150cdf0e10cSrcweir         }
151cdf0e10cSrcweir     } catch (IllegalTypeException ex) {
152cdf0e10cSrcweir         ex.printStackTrace(System.out);
153cdf0e10cSrcweir     }
154cdf0e10cSrcweir         return bIsContainer;
155cdf0e10cSrcweir     }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 
158cdf0e10cSrcweir     //  add all containers for the given object to the tree under the node
159cdf0e10cSrcweir     //  parent
getUnoObjectsOfContainer(Object _oUnoParentObject)160cdf0e10cSrcweir     public Object[] getUnoObjectsOfContainer(Object _oUnoParentObject) {
161cdf0e10cSrcweir     Object[] oRetComponents = null;
162cdf0e10cSrcweir     try {
163cdf0e10cSrcweir         Vector oRetComponentsVector = new Vector();
164cdf0e10cSrcweir         XIntrospectionAccess xIntrospectionAccessObject = getXIntrospectionAccess(_oUnoParentObject);
165cdf0e10cSrcweir         if ( xIntrospectionAccessObject != null ) {
166cdf0e10cSrcweir             XEnumerationAccess xEnumerationAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class, xIntrospectionAccessObject.queryAdapter( new Type( XEnumerationAccess.class ) ) );
167cdf0e10cSrcweir             if ( xEnumerationAccess != null ) {
168cdf0e10cSrcweir                 XEnumeration xEnumeration = xEnumerationAccess.createEnumeration();
169cdf0e10cSrcweir                 while ( xEnumeration.hasMoreElements() ) {
170cdf0e10cSrcweir                     oRetComponentsVector.add(xEnumeration.nextElement());
171cdf0e10cSrcweir                 }
172cdf0e10cSrcweir             }
173cdf0e10cSrcweir             XIndexAccess xIndexAccess = (XIndexAccess) UnoRuntime.queryInterface( XIndexAccess.class, xIntrospectionAccessObject.queryAdapter(new Type( XIndexAccess.class )));
174cdf0e10cSrcweir             if ( xIndexAccess != null ) {
175cdf0e10cSrcweir                 XIdlMethod mMethod = xIntrospectionAccessObject.getMethod("getByIndex", com.sun.star.beans.MethodConcept.INDEXCONTAINER);
176cdf0e10cSrcweir                 for ( int i = 0; i < xIndexAccess.getCount(); i++ ) {
177cdf0e10cSrcweir                     Object[][] aParamInfo = new Object[1][1];
178cdf0e10cSrcweir                     aParamInfo[0] = new Integer[] { new Integer(i) };
179cdf0e10cSrcweir                     oRetComponentsVector.add(mMethod.invoke(_oUnoParentObject, aParamInfo));
180cdf0e10cSrcweir                 }
181cdf0e10cSrcweir             }
182cdf0e10cSrcweir         }
183cdf0e10cSrcweir         if (oRetComponentsVector != null){
184cdf0e10cSrcweir             oRetComponents = new Object[oRetComponentsVector.size()];
185cdf0e10cSrcweir             oRetComponentsVector.toArray(oRetComponents);
186cdf0e10cSrcweir         }
187cdf0e10cSrcweir     }
188cdf0e10cSrcweir     catch( Exception exception ) {
189cdf0e10cSrcweir         System.err.println( exception );
190cdf0e10cSrcweir     }
191cdf0e10cSrcweir     return oRetComponents;
192cdf0e10cSrcweir     }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 
getMethodsOfInterface(Type _aType)195cdf0e10cSrcweir     protected XIdlMethod[] getMethodsOfInterface(Type _aType){
196cdf0e10cSrcweir     try{
197cdf0e10cSrcweir         XIdlClass xIdlClass = mxIdlReflection.forName(_aType.getTypeName());
198cdf0e10cSrcweir         return xIdlClass.getMethods();
199cdf0e10cSrcweir     }
200cdf0e10cSrcweir     catch( Exception e ) {
201cdf0e10cSrcweir         System.err.println( e );
202cdf0e10cSrcweir         return null;
203cdf0e10cSrcweir     }}
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 
getFieldsOfType(Type _aType)206cdf0e10cSrcweir     protected XIdlField[] getFieldsOfType(Type _aType){
207cdf0e10cSrcweir     try{
208cdf0e10cSrcweir         XIdlClass xIdlClass = mxIdlReflection.forName(_aType.getTypeName());
209cdf0e10cSrcweir         return xIdlClass.getFields();
210cdf0e10cSrcweir     }
211cdf0e10cSrcweir     catch( Exception e ) {
212cdf0e10cSrcweir         System.err.println( e );
213cdf0e10cSrcweir         return null;
214cdf0e10cSrcweir     }}
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 
hasMethods(Object _oUnoObject)217cdf0e10cSrcweir     public boolean hasMethods(Object _oUnoObject){
218cdf0e10cSrcweir         boolean bHasMethods = (getMethods(_oUnoObject).length > 0);
219cdf0e10cSrcweir         return bHasMethods;
220cdf0e10cSrcweir     }
221cdf0e10cSrcweir 
222cdf0e10cSrcweir 
223cdf0e10cSrcweir     //  add all methods for the given object to the tree under the node parent
getMethods(Object _oUnoParentObject)224cdf0e10cSrcweir     public XIdlMethod[] getMethods(Object _oUnoParentObject) {
225cdf0e10cSrcweir     try {
226cdf0e10cSrcweir         XIntrospectionAccess xIntrospectionAccess = getXIntrospectionAccess(_oUnoParentObject);
227cdf0e10cSrcweir         if (xIntrospectionAccess != null){
228cdf0e10cSrcweir             XIdlMethod[] xIdlMethods = xIntrospectionAccess.getMethods(MethodConcept.ALL - MethodConcept.DANGEROUS);
229cdf0e10cSrcweir             return xIdlMethods;
230cdf0e10cSrcweir         }
231cdf0e10cSrcweir     }
232cdf0e10cSrcweir     catch( Exception e ) {
233cdf0e10cSrcweir         System.err.println( e );
234cdf0e10cSrcweir     }
235cdf0e10cSrcweir     return null;
236cdf0e10cSrcweir     }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 
hasProperties(Object _oUnoObject)239cdf0e10cSrcweir     public boolean hasProperties(Object _oUnoObject){
240cdf0e10cSrcweir         boolean bHasProperties = (getProperties(_oUnoObject).length > 0);
241cdf0e10cSrcweir         return bHasProperties;
242cdf0e10cSrcweir     }
243cdf0e10cSrcweir 
244cdf0e10cSrcweir 
getProperties( Object _oUnoParentObject)245cdf0e10cSrcweir     protected Property[] getProperties( Object _oUnoParentObject){
246cdf0e10cSrcweir     try {
247cdf0e10cSrcweir         XIntrospectionAccess xIntrospectionAccess = getXIntrospectionAccess(_oUnoParentObject);
248cdf0e10cSrcweir         if (xIntrospectionAccess != null){
249cdf0e10cSrcweir             Property[] aProperties = xIntrospectionAccess.getProperties(com.sun.star.beans.PropertyConcept.ATTRIBUTES + com.sun.star.beans.PropertyConcept.PROPERTYSET);
250cdf0e10cSrcweir             return aProperties;
251cdf0e10cSrcweir         }
252cdf0e10cSrcweir     }
253cdf0e10cSrcweir     catch( Exception e ) {
254cdf0e10cSrcweir         System.err.println( e );
255cdf0e10cSrcweir     }
256cdf0e10cSrcweir         return null;
257cdf0e10cSrcweir     }
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 
getProperties(Object _oUnoObject, String _sServiceName)260cdf0e10cSrcweir     protected Property[] getProperties(Object _oUnoObject, String _sServiceName){
261cdf0e10cSrcweir         Property[] aProperties = getProperties(_oUnoObject);
262cdf0e10cSrcweir         List aListOfProperties = java.util.Arrays.asList(aProperties);
263cdf0e10cSrcweir         Vector aPropertiesVector = new Vector(aListOfProperties);
264cdf0e10cSrcweir         if (aProperties != null){
265cdf0e10cSrcweir             XPropertyTypeDescription[] xPropertyTypeDescriptions = getPropertyDescriptionsOfService(_sServiceName);
266cdf0e10cSrcweir             for (int i = aProperties.length - 1; i >= 0; i--){
267cdf0e10cSrcweir                 if (!hasByName(xPropertyTypeDescriptions, _sServiceName + "." + aProperties[i].Name)){
268cdf0e10cSrcweir                     aPropertiesVector.remove(i);
269cdf0e10cSrcweir                 }
270cdf0e10cSrcweir             }
271cdf0e10cSrcweir         }
272cdf0e10cSrcweir         Property[] aRetProperties = new Property[aPropertiesVector.size()];
273cdf0e10cSrcweir         aPropertiesVector.toArray(aRetProperties);
274cdf0e10cSrcweir         return aRetProperties;
275cdf0e10cSrcweir     }
276cdf0e10cSrcweir 
277cdf0e10cSrcweir 
getInterfaces(Object _oUnoObject, String _sServiceName)278cdf0e10cSrcweir     protected Type[] getInterfaces(Object _oUnoObject, String _sServiceName){
279cdf0e10cSrcweir         Type[] aTypes = getInterfaces(_oUnoObject);
280cdf0e10cSrcweir         List aListOfTypes = java.util.Arrays.asList(aTypes);
281cdf0e10cSrcweir         Vector aTypesVector = new Vector(aListOfTypes);
282cdf0e10cSrcweir         if (aTypes != null){
283cdf0e10cSrcweir             XInterfaceTypeDescription[] xInterfaceTypeDescriptions = getInterfaceDescriptionsOfService(_sServiceName);
284cdf0e10cSrcweir             for (int i = aTypes.length - 1; i >= 0; i--){
285cdf0e10cSrcweir                 if (!hasByName(xInterfaceTypeDescriptions, aTypes[i].getTypeName())){
286cdf0e10cSrcweir                     aTypesVector.remove(i);
287cdf0e10cSrcweir                 }
288cdf0e10cSrcweir             }
289cdf0e10cSrcweir         }
290cdf0e10cSrcweir         Type[] aRetTypes = new Type[aTypesVector.size()];
291cdf0e10cSrcweir         aTypesVector.toArray(aRetTypes);
292cdf0e10cSrcweir         return aRetTypes;
293cdf0e10cSrcweir     }
294cdf0e10cSrcweir 
295cdf0e10cSrcweir 
hasInterfaces(Object _oUnoObject)296cdf0e10cSrcweir     public boolean hasInterfaces(Object _oUnoObject){
297cdf0e10cSrcweir         return (getInterfaces(_oUnoObject).length > 0);
298cdf0e10cSrcweir     }
299cdf0e10cSrcweir 
300cdf0e10cSrcweir 
getInterfaces(Object _oUnoParentObject)301cdf0e10cSrcweir     protected Type[] getInterfaces(Object _oUnoParentObject){
302cdf0e10cSrcweir         Type[] aTypes = new Type[]{};
303cdf0e10cSrcweir         XTypeProvider xTypeProvider = ( XTypeProvider ) UnoRuntime.queryInterface( XTypeProvider.class, _oUnoParentObject);
304cdf0e10cSrcweir         if ( xTypeProvider != null ) {
305cdf0e10cSrcweir             aTypes = xTypeProvider.getTypes();
306cdf0e10cSrcweir         }
307cdf0e10cSrcweir         return aTypes;
308cdf0e10cSrcweir     }
309cdf0e10cSrcweir 
310cdf0e10cSrcweir 
311cdf0e10cSrcweir 
isObjectSequence(Object _oUnoObject)312cdf0e10cSrcweir     public static boolean isObjectSequence(Object _oUnoObject){
313cdf0e10cSrcweir         Type aType = AnyConverter.getType(_oUnoObject);
314cdf0e10cSrcweir         return aType.getTypeClass().getValue() == TypeClass.SEQUENCE_value;
315cdf0e10cSrcweir     }
316cdf0e10cSrcweir 
317cdf0e10cSrcweir 
isObjectPrimitive(Object _oUnoObject)318cdf0e10cSrcweir     public static boolean isObjectPrimitive(Object _oUnoObject){
319cdf0e10cSrcweir         boolean breturn = false;
320cdf0e10cSrcweir         if (_oUnoObject != null){
321cdf0e10cSrcweir             Type aType = AnyConverter.getType(_oUnoObject);
322cdf0e10cSrcweir             breturn = isObjectPrimitive(_oUnoObject.getClass(), aType.getTypeClass());
323cdf0e10cSrcweir         }
324cdf0e10cSrcweir         return breturn;
325cdf0e10cSrcweir     }
326cdf0e10cSrcweir 
327cdf0e10cSrcweir 
isPrimitive(TypeClass _typeClass)328cdf0e10cSrcweir     public static boolean isPrimitive(TypeClass _typeClass){
329cdf0e10cSrcweir                return (( _typeClass == TypeClass.BOOLEAN )
330cdf0e10cSrcweir                      || ( _typeClass == TypeClass.BYTE )
331cdf0e10cSrcweir                      || ( _typeClass == TypeClass.CHAR )
332cdf0e10cSrcweir                      || ( _typeClass == TypeClass.DOUBLE )
333cdf0e10cSrcweir                      || ( _typeClass == TypeClass.ENUM )
334cdf0e10cSrcweir                      || ( _typeClass == TypeClass.FLOAT )
335cdf0e10cSrcweir                      || ( _typeClass == TypeClass.HYPER )
336cdf0e10cSrcweir                      || ( _typeClass == TypeClass.LONG )
337cdf0e10cSrcweir                      || ( _typeClass == TypeClass.SHORT )
338cdf0e10cSrcweir                      || ( _typeClass == TypeClass.STRING )
339cdf0e10cSrcweir                      || ( _typeClass == TypeClass.UNSIGNED_HYPER )
340cdf0e10cSrcweir                      || ( _typeClass == TypeClass.UNSIGNED_LONG )
341cdf0e10cSrcweir                      || ( _typeClass == TypeClass.UNSIGNED_SHORT ));
342cdf0e10cSrcweir     }
343cdf0e10cSrcweir 
isObjectPrimitive(Class _oUnoClass, TypeClass _typeClass)344cdf0e10cSrcweir     public static boolean isObjectPrimitive(Class _oUnoClass, TypeClass _typeClass){
345cdf0e10cSrcweir         return !( ( !_oUnoClass.isPrimitive() ) && ( _typeClass != TypeClass.ARRAY )
346cdf0e10cSrcweir                                                          && ( _typeClass != TypeClass.BOOLEAN )
347cdf0e10cSrcweir                                                          && ( _typeClass != TypeClass.BYTE )
348cdf0e10cSrcweir                                                          && ( _typeClass != TypeClass.CHAR )
349cdf0e10cSrcweir                                                          && ( _typeClass != TypeClass.DOUBLE )
350cdf0e10cSrcweir                                                          && ( _typeClass != TypeClass.ENUM )
351cdf0e10cSrcweir                                                          && ( _typeClass != TypeClass.FLOAT )
352cdf0e10cSrcweir                                                          && ( _typeClass != TypeClass.HYPER )
353cdf0e10cSrcweir                                                          && ( _typeClass != TypeClass.LONG )
354cdf0e10cSrcweir                                                          && ( _typeClass != TypeClass.SHORT )
355cdf0e10cSrcweir                                                          && ( _typeClass != TypeClass.STRING )
356cdf0e10cSrcweir                                                          && ( _typeClass != TypeClass.UNSIGNED_HYPER )
357cdf0e10cSrcweir                                                          && ( _typeClass != TypeClass.UNSIGNED_LONG )
358cdf0e10cSrcweir                                                          && ( _typeClass != TypeClass.UNSIGNED_SHORT ));
359cdf0e10cSrcweir     }
360cdf0e10cSrcweir 
361cdf0e10cSrcweir 
initTypeDescriptionManager()362cdf0e10cSrcweir     protected void initTypeDescriptionManager() {
363cdf0e10cSrcweir     try {
364cdf0e10cSrcweir         Object oTypeDescriptionManager = getXComponentContext().getValueByName("/singletons/com.sun.star.reflection.theTypeDescriptionManager");
365cdf0e10cSrcweir         m_xTDEnumerationAccess = (XTypeDescriptionEnumerationAccess) UnoRuntime.queryInterface(XTypeDescriptionEnumerationAccess.class, oTypeDescriptionManager);
366cdf0e10cSrcweir     } catch ( java.lang.Exception e) {
367cdf0e10cSrcweir         System.out.println(System.out);
368cdf0e10cSrcweir     }}
369cdf0e10cSrcweir 
370cdf0e10cSrcweir 
getXTypeDescriptionEnumerationAccess()371cdf0e10cSrcweir     protected XTypeDescriptionEnumerationAccess getXTypeDescriptionEnumerationAccess(){
372cdf0e10cSrcweir         return m_xTDEnumerationAccess;
373cdf0e10cSrcweir     }
374cdf0e10cSrcweir 
375cdf0e10cSrcweir 
getFieldsOfConstantGroup(String _sTypeClass)376cdf0e10cSrcweir     protected XConstantTypeDescription[] getFieldsOfConstantGroup(String _sTypeClass){
377cdf0e10cSrcweir     XConstantTypeDescription[] xConstantTypeDescriptions = null;
378cdf0e10cSrcweir     try {
379cdf0e10cSrcweir         TypeClass[] eTypeClasses = new com.sun.star.uno.TypeClass[1];
380cdf0e10cSrcweir         eTypeClasses[0] = com.sun.star.uno.TypeClass.CONSTANTS;
381cdf0e10cSrcweir         XTypeDescriptionEnumeration xTDEnumeration = m_xTDEnumerationAccess.createTypeDescriptionEnumeration(getModuleName(_sTypeClass), eTypeClasses, TypeDescriptionSearchDepth.INFINITE);
382cdf0e10cSrcweir         while (xTDEnumeration.hasMoreElements()) {
383cdf0e10cSrcweir             XTypeDescription xTD = xTDEnumeration.nextTypeDescription();
384cdf0e10cSrcweir             if (xTD.getName().equals(_sTypeClass)){
385cdf0e10cSrcweir                 XConstantsTypeDescription xConstantsTypeDescription = (XConstantsTypeDescription) UnoRuntime.queryInterface(XConstantsTypeDescription.class, xTD);
386cdf0e10cSrcweir                 xConstantTypeDescriptions = xConstantsTypeDescription.getConstants();
387cdf0e10cSrcweir             }
388cdf0e10cSrcweir             String sName = xTD.getName();
389cdf0e10cSrcweir         }
390cdf0e10cSrcweir         return xConstantTypeDescriptions;
391cdf0e10cSrcweir     } catch ( java.lang.Exception e) {
392cdf0e10cSrcweir         System.out.println(System.out);
393cdf0e10cSrcweir     }
394cdf0e10cSrcweir         return null;
395cdf0e10cSrcweir     }
396cdf0e10cSrcweir 
getServiceTypeDescription(String _sServiceName, TypeClass _eTypeClass)397cdf0e10cSrcweir     private XServiceTypeDescription getServiceTypeDescription(String _sServiceName, TypeClass _eTypeClass){
398cdf0e10cSrcweir     try{
399cdf0e10cSrcweir         if (_sServiceName.length() > 0){
400cdf0e10cSrcweir             TypeClass[] eTypeClasses = new com.sun.star.uno.TypeClass[2];
401cdf0e10cSrcweir             eTypeClasses[0] = com.sun.star.uno.TypeClass.SERVICE;
402cdf0e10cSrcweir             eTypeClasses[1] = _eTypeClass;
403cdf0e10cSrcweir             XTypeDescriptionEnumeration xTDEnumeration = getXTypeDescriptionEnumerationAccess().createTypeDescriptionEnumeration(Introspector.getModuleName(_sServiceName), eTypeClasses, TypeDescriptionSearchDepth.INFINITE);
404cdf0e10cSrcweir             while (xTDEnumeration.hasMoreElements()) {
405cdf0e10cSrcweir                 XTypeDescription xTD = xTDEnumeration.nextTypeDescription();
406cdf0e10cSrcweir                 if (xTD.getName().equals(_sServiceName)){
407cdf0e10cSrcweir                     XServiceTypeDescription xServiceTypeDescription = (XServiceTypeDescription) UnoRuntime.queryInterface(XServiceTypeDescription.class, xTD);
408cdf0e10cSrcweir                     return xServiceTypeDescription;
409cdf0e10cSrcweir                 }
410cdf0e10cSrcweir             }
411cdf0e10cSrcweir         }
412cdf0e10cSrcweir         return null;
413cdf0e10cSrcweir     } catch (Exception ex) {
414cdf0e10cSrcweir         ex.printStackTrace(System.out);
415cdf0e10cSrcweir         return null;
416cdf0e10cSrcweir     }}
417cdf0e10cSrcweir 
418cdf0e10cSrcweir 
getPropertyDescriptionsOfService(String _sServiceName)419cdf0e10cSrcweir     public XPropertyTypeDescription[] getPropertyDescriptionsOfService(String _sServiceName){
420cdf0e10cSrcweir     try {
421cdf0e10cSrcweir         XServiceTypeDescription xServiceTypeDescription = getServiceTypeDescription(_sServiceName, com.sun.star.uno.TypeClass.PROPERTY);
422cdf0e10cSrcweir         if (xServiceTypeDescription != null){
423cdf0e10cSrcweir             XPropertyTypeDescription[] xPropertyTypeDescriptions = xServiceTypeDescription.getProperties();
424cdf0e10cSrcweir             return xPropertyTypeDescriptions;
425cdf0e10cSrcweir         }
426cdf0e10cSrcweir     } catch ( java.lang.Exception e) {
427cdf0e10cSrcweir         System.out.println(System.out);
428cdf0e10cSrcweir     }
429cdf0e10cSrcweir     return new XPropertyTypeDescription[]{};
430cdf0e10cSrcweir     }
431cdf0e10cSrcweir 
432cdf0e10cSrcweir 
getReferencedType(String _sTypeName)433cdf0e10cSrcweir     public XTypeDescription getReferencedType(String _sTypeName){
434cdf0e10cSrcweir     XTypeDescription xTypeDescription = null;
435cdf0e10cSrcweir     try{
436cdf0e10cSrcweir         XHierarchicalNameAccess xHierarchicalNameAccess = (XHierarchicalNameAccess) UnoRuntime.queryInterface(XHierarchicalNameAccess.class, m_xTDEnumerationAccess);
437cdf0e10cSrcweir         if (xHierarchicalNameAccess != null){
438cdf0e10cSrcweir             if (xHierarchicalNameAccess.hasByHierarchicalName(_sTypeName)){
439cdf0e10cSrcweir                 XIndirectTypeDescription xIndirectTypeDescription = (XIndirectTypeDescription) UnoRuntime.queryInterface(XIndirectTypeDescription.class, xHierarchicalNameAccess.getByHierarchicalName(_sTypeName));
440cdf0e10cSrcweir                 if (xIndirectTypeDescription != null){
441cdf0e10cSrcweir                     xTypeDescription = xIndirectTypeDescription.getReferencedType();
442cdf0e10cSrcweir                 }
443cdf0e10cSrcweir             }
444cdf0e10cSrcweir         }
445cdf0e10cSrcweir     } catch (Exception ex) {
446cdf0e10cSrcweir         ex.printStackTrace(System.out);
447cdf0e10cSrcweir     }
448cdf0e10cSrcweir         return xTypeDescription;
449cdf0e10cSrcweir     }
450cdf0e10cSrcweir 
451cdf0e10cSrcweir 
getInterfaceDescriptionsOfService(String _sServiceName)452cdf0e10cSrcweir     public XInterfaceTypeDescription[] getInterfaceDescriptionsOfService(String _sServiceName){
453cdf0e10cSrcweir     try {
454cdf0e10cSrcweir         XServiceTypeDescription xServiceTypeDescription = getServiceTypeDescription(_sServiceName, com.sun.star.uno.TypeClass.INTERFACE);
455cdf0e10cSrcweir         if (xServiceTypeDescription != null){
456cdf0e10cSrcweir             XInterfaceTypeDescription[] xInterfaceTypeDescriptions = xServiceTypeDescription.getMandatoryInterfaces();
457cdf0e10cSrcweir             return xInterfaceTypeDescriptions;
458cdf0e10cSrcweir         }
459cdf0e10cSrcweir     } catch ( java.lang.Exception e) {
460cdf0e10cSrcweir         System.out.println(System.out);
461cdf0e10cSrcweir     }
462cdf0e10cSrcweir         return new XInterfaceTypeDescription[]{};
463cdf0e10cSrcweir     }
464cdf0e10cSrcweir 
465cdf0e10cSrcweir 
hasByName(XTypeDescription[] _xTypeDescriptions, String _sTypeName)466cdf0e10cSrcweir     static boolean hasByName(XTypeDescription[] _xTypeDescriptions, String _sTypeName){
467cdf0e10cSrcweir         for (int i = 0; i < _xTypeDescriptions.length; i++){
468cdf0e10cSrcweir             if (_xTypeDescriptions[i].getName().equals(_sTypeName)){
469cdf0e10cSrcweir                 return true;
470cdf0e10cSrcweir             }
471cdf0e10cSrcweir         }
472cdf0e10cSrcweir         return false;
473cdf0e10cSrcweir     }
474cdf0e10cSrcweir 
475cdf0e10cSrcweir 
getModuleName(String _sTypeClass)476cdf0e10cSrcweir     public static String getModuleName(String _sTypeClass){
477cdf0e10cSrcweir         int nlastindex = _sTypeClass.lastIndexOf(".");
478cdf0e10cSrcweir         if (nlastindex > -1){
479cdf0e10cSrcweir             return _sTypeClass.substring(0, nlastindex);
480cdf0e10cSrcweir         }
481cdf0e10cSrcweir         else{
482cdf0e10cSrcweir             return "";
483cdf0e10cSrcweir         }
484cdf0e10cSrcweir     }
485cdf0e10cSrcweir 
486cdf0e10cSrcweir 
getShortClassName(String _sClassName)487cdf0e10cSrcweir     public static String getShortClassName(String _sClassName){
488cdf0e10cSrcweir         String sShortClassName = _sClassName;
489cdf0e10cSrcweir         int nindex = _sClassName.lastIndexOf(".");
490cdf0e10cSrcweir         if ((nindex < _sClassName.length()) && nindex > -1){
491cdf0e10cSrcweir             sShortClassName = _sClassName.substring(nindex + 1);
492cdf0e10cSrcweir         }
493cdf0e10cSrcweir         return sShortClassName;
494cdf0e10cSrcweir     }
495cdf0e10cSrcweir 
496cdf0e10cSrcweir 
497cdf0e10cSrcweir 
isUnoTypeObject(Object _oUnoObject)498cdf0e10cSrcweir     public static boolean isUnoTypeObject(Object _oUnoObject){
499cdf0e10cSrcweir         return isOfUnoType(_oUnoObject, "com.sun.star.uno.Type");
500cdf0e10cSrcweir     }
501cdf0e10cSrcweir 
502cdf0e10cSrcweir 
isUnoPropertyTypeObject(Object _oUnoObject)503cdf0e10cSrcweir     public static boolean isUnoPropertyTypeObject(Object _oUnoObject){
504cdf0e10cSrcweir         return isOfUnoType(_oUnoObject, "com.sun.star.beans.Property");
505cdf0e10cSrcweir     }
506cdf0e10cSrcweir 
507cdf0e10cSrcweir 
isUnoPropertyValueTypeObject(Object _oUnoObject)508cdf0e10cSrcweir     public static boolean isUnoPropertyValueTypeObject(Object _oUnoObject){
509cdf0e10cSrcweir         return isOfUnoType(_oUnoObject, "com.sun.star.beans.PropertyValue");
510cdf0e10cSrcweir     }
511cdf0e10cSrcweir 
512cdf0e10cSrcweir 
isOfUnoType(Object _oUnoObject, String _sTypeName)513cdf0e10cSrcweir     public static boolean isOfUnoType(Object _oUnoObject, String _sTypeName){
514cdf0e10cSrcweir         boolean bIsUnoObject = false;
515cdf0e10cSrcweir         if (_oUnoObject != null){
516cdf0e10cSrcweir             if (_oUnoObject.getClass().isArray()){
517cdf0e10cSrcweir                 if (!_oUnoObject.getClass().getComponentType().isPrimitive()){
518cdf0e10cSrcweir                     Object[] oUnoArray = (Object[]) _oUnoObject;
519cdf0e10cSrcweir                     if (oUnoArray.length > 0){
520cdf0e10cSrcweir                         bIsUnoObject = ( oUnoArray[0].getClass().getName().equals(_sTypeName));
521cdf0e10cSrcweir                     }
522cdf0e10cSrcweir                 }
523cdf0e10cSrcweir             }
524cdf0e10cSrcweir         }
525cdf0e10cSrcweir         else{
526cdf0e10cSrcweir             bIsUnoObject = (_oUnoObject.getClass().getName().equals(_sTypeName));
527cdf0e10cSrcweir         }
528cdf0e10cSrcweir         return bIsUnoObject;
529cdf0e10cSrcweir     }
530cdf0e10cSrcweir 
531cdf0e10cSrcweir 
getConstantDisplayString(int _nValue, XConstantTypeDescription[] _xConstantTypeDescription, String _sDisplayString)532cdf0e10cSrcweir     public String getConstantDisplayString(int _nValue, XConstantTypeDescription[] _xConstantTypeDescription, String _sDisplayString){
533cdf0e10cSrcweir         String sPrefix = "";
534cdf0e10cSrcweir         int[] nbits = new int[_xConstantTypeDescription.length];
535cdf0e10cSrcweir         for (int i = 0; i < _xConstantTypeDescription.length; i++){
536cdf0e10cSrcweir             short nConstantValue = ((Short) _xConstantTypeDescription[i].getConstantValue()).shortValue();
537cdf0e10cSrcweir             nbits[i] = _nValue & nConstantValue;
538cdf0e10cSrcweir             if (nbits[i] > 0){
539cdf0e10cSrcweir                 _sDisplayString += sPrefix + _xConstantTypeDescription[i].getName();
540cdf0e10cSrcweir                 sPrefix = " + ";
541cdf0e10cSrcweir             }
542cdf0e10cSrcweir         }
543cdf0e10cSrcweir         return _sDisplayString;
544cdf0e10cSrcweir     }
545cdf0e10cSrcweir 
546cdf0e10cSrcweir 
isValid(Object[] _oObject)547cdf0e10cSrcweir     public static boolean isValid(Object[] _oObject){
548cdf0e10cSrcweir         if (_oObject != null){
549cdf0e10cSrcweir             if (_oObject.length > 0){
550cdf0e10cSrcweir                 return true;
551cdf0e10cSrcweir             }
552cdf0e10cSrcweir         }
553cdf0e10cSrcweir         return false;
554cdf0e10cSrcweir     }
555cdf0e10cSrcweir 
556cdf0e10cSrcweir 
isValid(Object _oObject)557cdf0e10cSrcweir     public static boolean isValid(Object _oObject){
558cdf0e10cSrcweir         if (_oObject != null){
559cdf0e10cSrcweir             return (!AnyConverter.isVoid(_oObject));
560cdf0e10cSrcweir         }
561cdf0e10cSrcweir         return false;
562cdf0e10cSrcweir     }
563cdf0e10cSrcweir 
564cdf0e10cSrcweir 
isArray(Object _oObject)565cdf0e10cSrcweir     public static boolean isArray(Object _oObject){
566cdf0e10cSrcweir         return _oObject.getClass().isArray();
567cdf0e10cSrcweir     }
568cdf0e10cSrcweir 
569cdf0e10cSrcweir 
hasSupportedServices(Object _oUnoObject)570cdf0e10cSrcweir     public boolean hasSupportedServices(Object _oUnoObject){
571cdf0e10cSrcweir         boolean bHasSupportedServices = false;
572cdf0e10cSrcweir         XServiceInfo xServiceInfo = ( XServiceInfo ) UnoRuntime.queryInterface( XServiceInfo.class, _oUnoObject);
573cdf0e10cSrcweir         if ( xServiceInfo != null ){
574cdf0e10cSrcweir             String[] sSupportedServiceNames = xServiceInfo.getSupportedServiceNames();
575cdf0e10cSrcweir             bHasSupportedServices = sSupportedServiceNames.length > 0;
576cdf0e10cSrcweir         }
577cdf0e10cSrcweir         return bHasSupportedServices;
578cdf0e10cSrcweir     }
579cdf0e10cSrcweir 
580cdf0e10cSrcweir 
getValueOfText(TypeClass aTypeClass, String sText)581cdf0e10cSrcweir     public Object getValueOfText(TypeClass aTypeClass, String sText){
582cdf0e10cSrcweir         Object oReturn = null;
583cdf0e10cSrcweir         switch (aTypeClass.getValue()){
584cdf0e10cSrcweir             case TypeClass.CHAR_value:
585cdf0e10cSrcweir                 break;
586cdf0e10cSrcweir             case TypeClass.DOUBLE_value:
587cdf0e10cSrcweir                 oReturn = Double.valueOf(sText);
588cdf0e10cSrcweir                 break;
589cdf0e10cSrcweir             case TypeClass.ENUM_value:
590cdf0e10cSrcweir                 break;
591cdf0e10cSrcweir             case TypeClass.FLOAT_value:
592cdf0e10cSrcweir                 oReturn = Float.valueOf(sText);
593cdf0e10cSrcweir                 break;
594cdf0e10cSrcweir             case TypeClass.HYPER_value:
595cdf0e10cSrcweir                 oReturn = Long.valueOf(sText);
596cdf0e10cSrcweir                 break;
597cdf0e10cSrcweir             case TypeClass.LONG_value:
598cdf0e10cSrcweir                 oReturn = Integer.valueOf(sText);
599cdf0e10cSrcweir                 break;
600cdf0e10cSrcweir             case TypeClass.SHORT_value:
601cdf0e10cSrcweir                 oReturn = Byte.valueOf(sText);
602cdf0e10cSrcweir                 break;
603cdf0e10cSrcweir             case TypeClass.STRING_value:
604cdf0e10cSrcweir                 oReturn = sText;
605cdf0e10cSrcweir                 break;
606cdf0e10cSrcweir             case TypeClass.UNSIGNED_HYPER_value:
607cdf0e10cSrcweir                 oReturn = Long.valueOf(sText);
608cdf0e10cSrcweir                 break;
609cdf0e10cSrcweir             case TypeClass.UNSIGNED_LONG_value:
610cdf0e10cSrcweir                 oReturn = Integer.valueOf(sText);
611cdf0e10cSrcweir                 break;
612cdf0e10cSrcweir             case TypeClass.UNSIGNED_SHORT_value:
613cdf0e10cSrcweir                 oReturn = Byte.valueOf(sText);
614cdf0e10cSrcweir                 break;
615cdf0e10cSrcweir             default:
616cdf0e10cSrcweir         }
617cdf0e10cSrcweir         return oReturn;
618cdf0e10cSrcweir     }
619cdf0e10cSrcweir 
620cdf0e10cSrcweir 
getXSimpleFileAccess()621cdf0e10cSrcweir     public XSimpleFileAccess getXSimpleFileAccess(){
622cdf0e10cSrcweir     try {
623cdf0e10cSrcweir         if (xSimpleFileAccess == null){
624cdf0e10cSrcweir             Object oSimpleFileAccess = m_xComponentContext.getServiceManager().createInstanceWithContext("com.sun.star.ucb.SimpleFileAccess", m_xComponentContext);
625cdf0e10cSrcweir             xSimpleFileAccess = (XSimpleFileAccess) com.sun.star.uno.UnoRuntime.queryInterface(XSimpleFileAccess.class, oSimpleFileAccess);
626cdf0e10cSrcweir         }
627cdf0e10cSrcweir         return xSimpleFileAccess;
628cdf0e10cSrcweir     } catch (com.sun.star.uno.Exception ex) {
629cdf0e10cSrcweir         ex.printStackTrace(System.out);
630cdf0e10cSrcweir         return null;
631cdf0e10cSrcweir     }}
632cdf0e10cSrcweir 
633cdf0e10cSrcweir 
isValidSDKInstallationPath(String _sSDKInstallationPath)634cdf0e10cSrcweir     public boolean isValidSDKInstallationPath(String _sSDKInstallationPath){
635cdf0e10cSrcweir     boolean bIsValid = false;
636cdf0e10cSrcweir     try {
637cdf0e10cSrcweir         String sIDLFolder = Introspector.addToPath(_sSDKInstallationPath, Inspector.sIDLDOCUMENTSUBFOLDER);
638cdf0e10cSrcweir         String sIndexFile = Introspector.addToPath(_sSDKInstallationPath, "index.html");
639cdf0e10cSrcweir         if (getXSimpleFileAccess() != null){
640cdf0e10cSrcweir             bIsValid = (getXSimpleFileAccess().exists(sIDLFolder) && getXSimpleFileAccess().exists(sIndexFile));
641cdf0e10cSrcweir         }
642cdf0e10cSrcweir     } catch (com.sun.star.uno.Exception ex) {
643cdf0e10cSrcweir         ex.printStackTrace(System.out);
644cdf0e10cSrcweir     }
645cdf0e10cSrcweir         return bIsValid;
646cdf0e10cSrcweir     }
647cdf0e10cSrcweir 
648cdf0e10cSrcweir 
addToPath(String _sPath, String _sSubPath)649cdf0e10cSrcweir     public static String addToPath(String _sPath, String _sSubPath){
650cdf0e10cSrcweir         if (!_sPath.endsWith("/")){
651cdf0e10cSrcweir             _sPath += "/";
652cdf0e10cSrcweir         }
653cdf0e10cSrcweir         return _sPath + _sSubPath;
654cdf0e10cSrcweir     }
655cdf0e10cSrcweir 
656cdf0e10cSrcweir }
657