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 import com.sun.star.beans.PropertyValue;
25 import com.sun.star.frame.FrameSearchFlag;
26 import com.sun.star.frame.XDesktop;
27 import com.sun.star.frame.XDispatch;
28 import com.sun.star.frame.XDispatchProvider;
29 import com.sun.star.frame.XFrame;
30 import com.sun.star.lang.XMultiComponentFactory;
31 import com.sun.star.lang.XServiceInfo;
32 import com.sun.star.reflection.TypeDescriptionSearchDepth;
33 import com.sun.star.reflection.XServiceTypeDescription;
34 import com.sun.star.reflection.XTypeDescription;
35 import com.sun.star.reflection.XTypeDescriptionEnumeration;
36 import com.sun.star.reflection.XTypeDescriptionEnumerationAccess;
37 import com.sun.star.uno.AnyConverter;
38 import com.sun.star.uno.Type;
39 import com.sun.star.uno.TypeClass;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.uno.XComponentContext;
42 import com.sun.star.util.URL;
43 import com.sun.star.util.XURLTransformer;
44 import java.util.List;
45 import java.util.Vector;
46 
47 public class UnoNode{
48 
49     String sPath = null;
50     Object m_oUnoObject;
51     private XMultiComponentFactory m_xMultiComponentFactory;
52     private XComponentContext m_xComponentContext;
53     private Object[] m_oParamObjects = null;
54     private int m_nNodeType = XUnoNode.nOTHERS;
55     private Type aType = null;
56     private String sLabel = "";
57 
58 
59 
60     /** Creates a new instance of UnoNode */
UnoNode(Object _oUnoObject)61     public UnoNode(Object _oUnoObject) {
62         m_xComponentContext = Introspector.getIntrospector().getXComponentContext();
63         m_xMultiComponentFactory = m_xComponentContext.getServiceManager();
64         m_oUnoObject = _oUnoObject;
65     }
66 
UnoNode(Object _oUnoObject, Type _aType)67     public UnoNode(Object _oUnoObject, Type _aType) {
68         this(_oUnoObject);
69         aType = _aType;
70         m_nNodeType = XUnoNode.nINTERFACE;
71     }
72 
getUnoObject()73     public Object getUnoObject(){
74         return m_oUnoObject;
75     }
76 
77 
getXComponentContext()78     protected XComponentContext getXComponentContext(){
79         return m_xComponentContext;
80     }
81 
82 
getXMultiComponentFactory()83     protected XMultiComponentFactory getXMultiComponentFactory(){
84         return m_xMultiComponentFactory;
85     }
86 
87 
getXTypeDescriptionEnumerationAccess()88     private static XTypeDescriptionEnumerationAccess getXTypeDescriptionEnumerationAccess(){
89        return Introspector.getIntrospector().getXTypeDescriptionEnumerationAccess();
90     }
91 
92 
getAnchor()93     public String getAnchor(){
94         return "";
95     }
96 
getNodeType()97     public int getNodeType(){
98         return m_nNodeType;
99     }
100 
setNodeType(int _nNodeType)101     public void setNodeType(int _nNodeType){
102         m_nNodeType = _nNodeType;
103     }
104 
getClassName()105     public String getClassName(){
106         String sClassName = "";
107         if (m_nNodeType == XUnoNode.nINTERFACE){
108             sClassName = aType.getTypeName();
109         }
110         else if(m_nNodeType == XUnoNode.nSERVICE){
111             sClassName = sLabel;
112         }
113         return sClassName;
114     }
115 
116 
getUnoType()117     public Type getUnoType(){
118         return aType;
119     }
120 
setLabel(String _sLabel)121     protected void  setLabel(String _sLabel){
122         sLabel = _sLabel;
123     }
124 
openIdlDescription(String _sIDLUrl, String _sClassName, String _sAnchor)125     public void openIdlDescription(String _sIDLUrl, String _sClassName, String _sAnchor){
126     try{
127         String sIDLUrl = _sIDLUrl;
128         String sAnchor = ""; // TODO find out how the Anchor may be set at the html file;  //_sAnchor;
129         boolean bExists = Introspector.getIntrospector().getXSimpleFileAccess().exists(sIDLUrl);
130         if (sIDLUrl.equals("") || (!bExists)){
131             sIDLUrl = "http://api.openoffice.org/" + Inspector.sIDLDOCUMENTSUBFOLDER;
132         }
133         if (!sIDLUrl.endsWith("/")){
134             sIDLUrl += "/";
135         }
136         if (_sClassName.equals("")){
137             sIDLUrl += "com/sun/star/module-ix";
138             sAnchor = "";
139         }
140         else{
141             sIDLUrl += _sClassName.replace('.', '/');
142         }
143         if (sAnchor != null){
144             if (!sAnchor.equals("")){
145                 sIDLUrl += "#" + sAnchor;
146             }
147         }
148         sIDLUrl += ".html";
149         URL openHyperlink = getDispatchURL(".uno:OpenHyperlink");
150         PropertyValue pv = new PropertyValue();
151         pv.Name = "URL";
152         pv.Value = sIDLUrl;
153         getXDispatcher(openHyperlink).dispatch(openHyperlink, new PropertyValue[] {pv});
154     } catch(Exception exception) {
155         exception.printStackTrace(System.out);
156     }}
157 
158 
getDispatchURL(String _sURL)159     private com.sun.star.util.URL getDispatchURL(String _sURL){
160         try {
161             Object oTransformer = getXMultiComponentFactory().createInstanceWithContext("com.sun.star.util.URLTransformer", getXComponentContext());
162             XURLTransformer xTransformer = (XURLTransformer) UnoRuntime.queryInterface(XURLTransformer.class, oTransformer);
163             com.sun.star.util.URL[] oURL = new com.sun.star.util.URL[1];
164             oURL[0] = new com.sun.star.util.URL();
165             oURL[0].Complete = _sURL;
166             xTransformer.parseStrict(oURL);
167         return oURL[0];
168         } catch (Exception e) {
169             e.printStackTrace(System.out);
170         }
171         return null;
172     }
173 
174 
getCurrentFrame()175     private XFrame getCurrentFrame(){
176     try{
177         Object oDesktop = getXMultiComponentFactory().createInstanceWithContext("com.sun.star.frame.Desktop", getXComponentContext());
178         XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, oDesktop);
179         return xDesktop.getCurrentFrame();
180     } catch (Exception e) {
181         e.printStackTrace(System.out);
182         return null;
183     }}
184 
185 
getXDispatcher(com.sun.star.util.URL oURL)186     private XDispatch getXDispatcher(com.sun.star.util.URL oURL) {
187     try {
188         com.sun.star.util.URL[] oURLArray = new com.sun.star.util.URL[1];
189         oURLArray[0] = oURL;
190         XDispatchProvider xDispatchProvider = (XDispatchProvider) UnoRuntime.queryInterface(XDispatchProvider.class, getCurrentFrame());
191         XDispatch xDispatch = xDispatchProvider.queryDispatch(oURLArray[0], "_top", FrameSearchFlag.ALL); // "_self"
192         return xDispatch;
193     } catch (Exception e) {
194         e.printStackTrace(System.out);
195         return null;
196     }}
197 
198 
loadArgs(String url)199     private PropertyValue[] loadArgs(String url) {
200         PropertyValue pv = new PropertyValue();
201         pv.Name = "URL";
202         pv.Value = url;
203         return new PropertyValue[] {pv};
204     }
205 
206 
207 
isFilterApplicable(String _sFilter, String _sName)208     public boolean isFilterApplicable(String _sFilter, String _sName){
209         boolean bFilterDoesApply = true;
210         if (_sFilter.length() > 0){
211             if (_sName.indexOf(_sFilter) == -1){
212                 bFilterDoesApply = false;
213             }
214         }
215         return bFilterDoesApply;
216     }
217 
218 
219 //    public static String getServiceDescription(Object _oUnoObject){
220 //        String sClassName = "";
221 //        XServiceInfo xServiceInfo = (XServiceInfo) UnoRuntime.queryInterface(XServiceInfo.class, _oUnoObject);
222 //        if (xServiceInfo != null){
223 //            String[] sChildServiceNames = removeMandatoryServiceNames(xServiceInfo.getSupportedServiceNames());
224 //            if (sChildServiceNames.length > 0){
225 //                sClassName = sChildServiceNames[0];
226 //            }
227 //        }
228 //        return sClassName;
229 //    }
230 
231 
232 
getMandatoryServiceNames(String _sServiceName)233     private static String[] getMandatoryServiceNames(String _sServiceName){
234     String[] sMandatoryServiceNames  = new String[]{};
235     try {
236         TypeClass[] eTypeClasses = new com.sun.star.uno.TypeClass[1];
237         eTypeClasses[0] = com.sun.star.uno.TypeClass.SERVICE;
238         XTypeDescriptionEnumeration xTDEnumeration = getXTypeDescriptionEnumerationAccess().createTypeDescriptionEnumeration(Introspector.getModuleName(_sServiceName), eTypeClasses, TypeDescriptionSearchDepth.INFINITE);
239         while (xTDEnumeration.hasMoreElements()) {
240             XTypeDescription xTD = xTDEnumeration.nextTypeDescription();
241             if (xTD.getName().equals(_sServiceName)){
242                 XServiceTypeDescription xServiceTypeDescription = (XServiceTypeDescription) UnoRuntime.queryInterface(XServiceTypeDescription.class, xTD);
243                 XServiceTypeDescription[] xMandatoryServiceTypeDescriptions =  xServiceTypeDescription.getMandatoryServices();
244                 int nlength = xMandatoryServiceTypeDescriptions.length;
245                 sMandatoryServiceNames  = new String[nlength];
246                 for (int i = 0; i < nlength; i++){
247                     sMandatoryServiceNames[i] = xMandatoryServiceTypeDescriptions[i].getName();
248                 }
249 
250             }
251         }
252     } catch ( java.lang.Exception e) {
253         System.out.println(System.out);
254     }
255         return sMandatoryServiceNames;
256     }
257 
258 
removeMandatoryServiceNames(String[] _sServiceNames)259     private static String[] removeMandatoryServiceNames(String[] _sServiceNames){
260     try{
261         List aList = java.util.Arrays.asList(_sServiceNames);
262         Vector aVector = new Vector(aList);
263         for (int n = 0; n < _sServiceNames.length; n++){
264             String[] sDelServiceNames = getMandatoryServiceNames(_sServiceNames[n]);
265             for (int m = 0; m < sDelServiceNames.length; m++){
266                 if (aVector.contains(sDelServiceNames[m])){
267                     int nIndex = aVector.indexOf(sDelServiceNames[m]);
268                     aVector.remove(nIndex);
269                 }
270             }
271         }
272         String[] sRetArray = new String[aVector.size()];
273         aVector.toArray(sRetArray);
274         return sRetArray;
275     } catch (java.lang.Exception exception) {
276         exception.printStackTrace(System.out);
277     }
278         return new String[]{};
279     }
280 
281 
getDisplayValueOfPrimitiveType(Object _objectElement)282     public  static String getDisplayValueOfPrimitiveType(Object _objectElement){
283     String sValue ="";
284     try{
285         if (AnyConverter.isString(_objectElement)){
286             sValue = AnyConverter.toString(_objectElement);
287         }
288         else if (AnyConverter.isBoolean(_objectElement)){
289             sValue += AnyConverter.toBoolean(_objectElement);
290         }
291         else if (AnyConverter.isByte(_objectElement)){
292             sValue += AnyConverter.toByte(_objectElement);
293         }
294         else if (AnyConverter.isChar(_objectElement)){
295             sValue += AnyConverter.toChar(_objectElement);
296         }
297         else if (AnyConverter.isDouble(_objectElement)){
298             sValue += AnyConverter.toDouble(_objectElement);
299         }
300         else if (AnyConverter.isFloat(_objectElement)){
301             sValue += AnyConverter.toFloat(_objectElement);
302         }
303         else if (AnyConverter.isInt(_objectElement)){
304             sValue += AnyConverter.toInt(_objectElement);
305         }
306         else if (AnyConverter.isLong(_objectElement)){
307             sValue += AnyConverter.toLong(_objectElement);
308         }
309         else if (AnyConverter.isShort(_objectElement)){
310             sValue += AnyConverter.toShort(_objectElement);
311         }
312     }
313     catch( Exception e ) {
314         System.err.println( e );
315     }
316         return sValue;
317     }
318 
getDisplayValuesofPrimitiveArray(Object _oUnoObject)319     protected static String[] getDisplayValuesofPrimitiveArray(Object _oUnoObject){
320     String[] sDisplayValues = null;
321     try{
322         Type aType = AnyConverter.getType(_oUnoObject);
323         TypeClass aTypeClass = aType.getTypeClass();
324         int nTypeValue = aTypeClass.getValue();
325         if (nTypeValue == TypeClass.SEQUENCE_value){
326             nTypeValue = (sequenceComponentType(aType)).getTypeClass().getValue();
327         }
328         switch (nTypeValue){
329             case TypeClass.BOOLEAN_value:
330                 boolean[] bBooleans = (boolean[]) AnyConverter.toArray(_oUnoObject);
331                 sDisplayValues = new String[bBooleans.length];
332                 for (int i = 0; i < bBooleans.length; i++){
333                     sDisplayValues[i] = Boolean.toString(bBooleans[i]);
334                 }
335                 break;
336             case TypeClass.BYTE_value:
337                 byte[] bBytes = (byte[]) AnyConverter.toArray(_oUnoObject);
338                 sDisplayValues = new String[bBytes.length];
339                 for (int i = 0; i < bBytes.length; i++){
340                     sDisplayValues[i] = "" + bBytes[i];
341                 }
342                 break;
343             case TypeClass.DOUBLE_value:
344                 double[] fdoubles = (double[]) AnyConverter.toArray(_oUnoObject);
345                 sDisplayValues = new String[fdoubles.length];
346                 for (int i = 0; i < fdoubles.length; i++){
347                     sDisplayValues[i] = String.valueOf(fdoubles[i]);
348                 }
349                 break;
350             case TypeClass.FLOAT_value:
351                 float[] ffloats = (float[]) AnyConverter.toArray(_oUnoObject);
352                 sDisplayValues = new String[ffloats.length];
353                 for (int i = 0; i < ffloats.length; i++){
354                     sDisplayValues[i] = String.valueOf(ffloats[i]);
355                 }
356                 break;
357             case TypeClass.LONG_value:
358                 int[] nints = (int[]) AnyConverter.toArray(_oUnoObject);
359                 sDisplayValues = new String[nints.length];
360                 for (int i = 0; i < nints.length; i++){
361                     sDisplayValues[i] = String.valueOf(nints[i]);
362                 }
363                 break;
364             case TypeClass.HYPER_value:
365                 long[] nlongs = (long[]) AnyConverter.toArray(_oUnoObject);
366                 sDisplayValues = new String[nlongs.length];
367                 for (int i = 0; i < nlongs.length; i++){
368                     sDisplayValues[i] = String.valueOf(nlongs[i]);
369                 }
370                 break;
371             case TypeClass.SHORT_value:
372                 short[] nShorts = (short[]) AnyConverter.toArray(_oUnoObject);
373                 sDisplayValues = new String[nShorts.length];
374                 for (int i = 0; i < nShorts.length; i++){
375                     sDisplayValues[i] = "" +  nShorts[i];
376                 }
377                 break;
378             case TypeClass.CHAR_value:
379                 break;
380             default:
381                 System.out.println("Value could not be retrieved: " + aType.getTypeClass().getClass().getName());
382             }
383         return sDisplayValues;
384     }
385     catch( Exception e ) {
386         System.err.println( e );
387         return null;
388     }}
389 
390 
sequenceComponentType(Type sequenceType)391     private static Type sequenceComponentType(Type sequenceType) {
392 //        assert sequenceType.getTypeClass() == TypeClass.SEQUENCE;
393         String n = sequenceType.getTypeName();
394         final String PREFIX = "[]";
395 //        assert n.startsWith(PREFIX);
396         return new Type(n.substring(PREFIX.length()));
397     }
398 
399 
getNodeDescription(Object _oUnoObject, int _nIndex)400     public static String getNodeDescription(Object _oUnoObject, int _nIndex){
401         return getNodeDescription(_oUnoObject) + "[" + (_nIndex + 1) + "]";
402     }
403 
404 
getNodeDescription(Object _oUnoObject)405     public static String getNodeDescription(Object _oUnoObject){
406         XServiceInfo xServiceInfo = ( XServiceInfo ) UnoRuntime.queryInterface( XServiceInfo.class, _oUnoObject );
407         if ( xServiceInfo != null ) {
408             return xServiceInfo.getImplementationName();
409         }
410         String sClassName = _oUnoObject.getClass().getName();
411         if (Introspector.getIntrospector().isObjectPrimitive(_oUnoObject)){         //super.isO{sObjectClassName.equals("java.lang.String"))issClassName.equals("java.lang.String"))
412             return _oUnoObject.toString();
413         }
414         else{
415             return _oUnoObject.getClass().getName();
416         }
417     }
418 
setParameterObjects(Object[] _oParamObjects)419     public void setParameterObjects(Object[] _oParamObjects){
420         m_oParamObjects = _oParamObjects;
421     }
422 
getParameterObjects()423     public Object[] getParameterObjects(){
424         return m_oParamObjects;
425     }
426 }
427