1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *************************************************************************/
34 
35 import com.sun.star.beans.PropertyValue;
36 import com.sun.star.frame.FrameSearchFlag;
37 import com.sun.star.frame.XDesktop;
38 import com.sun.star.frame.XDispatch;
39 import com.sun.star.frame.XDispatchProvider;
40 import com.sun.star.frame.XFrame;
41 import com.sun.star.lang.XMultiComponentFactory;
42 import com.sun.star.lang.XServiceInfo;
43 import com.sun.star.reflection.TypeDescriptionSearchDepth;
44 import com.sun.star.reflection.XServiceTypeDescription;
45 import com.sun.star.reflection.XTypeDescription;
46 import com.sun.star.reflection.XTypeDescriptionEnumeration;
47 import com.sun.star.reflection.XTypeDescriptionEnumerationAccess;
48 import com.sun.star.uno.AnyConverter;
49 import com.sun.star.uno.Type;
50 import com.sun.star.uno.TypeClass;
51 import com.sun.star.uno.UnoRuntime;
52 import com.sun.star.uno.XComponentContext;
53 import com.sun.star.util.URL;
54 import com.sun.star.util.XURLTransformer;
55 import java.util.List;
56 import java.util.Vector;
57 
58 public class UnoNode{
59 
60     String sPath = null;
61     Object m_oUnoObject;
62     private XMultiComponentFactory m_xMultiComponentFactory;
63     private XComponentContext m_xComponentContext;
64     private Object[] m_oParamObjects = null;
65     private int m_nNodeType = XUnoNode.nOTHERS;
66     private Type aType = null;
67     private String sLabel = "";
68 
69 
70 
71     /** Creates a new instance of UnoNode */
72     public UnoNode(Object _oUnoObject) {
73         m_xComponentContext = Introspector.getIntrospector().getXComponentContext();
74         m_xMultiComponentFactory = m_xComponentContext.getServiceManager();
75         m_oUnoObject = _oUnoObject;
76     }
77 
78     public UnoNode(Object _oUnoObject, Type _aType) {
79         this(_oUnoObject);
80         aType = _aType;
81         m_nNodeType = XUnoNode.nINTERFACE;
82     }
83 
84     public Object getUnoObject(){
85         return m_oUnoObject;
86     }
87 
88 
89     protected XComponentContext getXComponentContext(){
90         return m_xComponentContext;
91     }
92 
93 
94     protected XMultiComponentFactory getXMultiComponentFactory(){
95         return m_xMultiComponentFactory;
96     }
97 
98 
99     private static XTypeDescriptionEnumerationAccess getXTypeDescriptionEnumerationAccess(){
100        return Introspector.getIntrospector().getXTypeDescriptionEnumerationAccess();
101     }
102 
103 
104     public String getAnchor(){
105         return "";
106     }
107 
108     public int getNodeType(){
109         return m_nNodeType;
110     }
111 
112     public void setNodeType(int _nNodeType){
113         m_nNodeType = _nNodeType;
114     }
115 
116     public String getClassName(){
117         String sClassName = "";
118         if (m_nNodeType == XUnoNode.nINTERFACE){
119             sClassName = aType.getTypeName();
120         }
121         else if(m_nNodeType == XUnoNode.nSERVICE){
122             sClassName = sLabel;
123         }
124         return sClassName;
125     }
126 
127 
128     public Type getUnoType(){
129         return aType;
130     }
131 
132     protected void  setLabel(String _sLabel){
133         sLabel = _sLabel;
134     }
135 
136     public void openIdlDescription(String _sIDLUrl, String _sClassName, String _sAnchor){
137     try{
138         String sIDLUrl = _sIDLUrl;
139         String sAnchor = ""; // TODO find out how the Anchor may be set at the html file;  //_sAnchor;
140         boolean bExists = Introspector.getIntrospector().getXSimpleFileAccess().exists(sIDLUrl);
141         if (sIDLUrl.equals("") || (!bExists)){
142             sIDLUrl = "http://api.openoffice.org/" + Inspector.sIDLDOCUMENTSUBFOLDER;
143         }
144         if (!sIDLUrl.endsWith("/")){
145             sIDLUrl += "/";
146         }
147         if (_sClassName.equals("")){
148             sIDLUrl += "com/sun/star/module-ix";
149             sAnchor = "";
150         }
151         else{
152             sIDLUrl += _sClassName.replace('.', '/');
153         }
154         if (sAnchor != null){
155             if (!sAnchor.equals("")){
156                 sIDLUrl += "#" + sAnchor;
157             }
158         }
159         sIDLUrl += ".html";
160         URL openHyperlink = getDispatchURL(".uno:OpenHyperlink");
161         PropertyValue pv = new PropertyValue();
162         pv.Name = "URL";
163         pv.Value = sIDLUrl;
164         getXDispatcher(openHyperlink).dispatch(openHyperlink, new PropertyValue[] {pv});
165     } catch(Exception exception) {
166         exception.printStackTrace(System.out);
167     }}
168 
169 
170     private com.sun.star.util.URL getDispatchURL(String _sURL){
171         try {
172             Object oTransformer = getXMultiComponentFactory().createInstanceWithContext("com.sun.star.util.URLTransformer", getXComponentContext());
173             XURLTransformer xTransformer = (XURLTransformer) UnoRuntime.queryInterface(XURLTransformer.class, oTransformer);
174             com.sun.star.util.URL[] oURL = new com.sun.star.util.URL[1];
175             oURL[0] = new com.sun.star.util.URL();
176             oURL[0].Complete = _sURL;
177             xTransformer.parseStrict(oURL);
178         return oURL[0];
179         } catch (Exception e) {
180             e.printStackTrace(System.out);
181         }
182         return null;
183     }
184 
185 
186     private XFrame getCurrentFrame(){
187     try{
188         Object oDesktop = getXMultiComponentFactory().createInstanceWithContext("com.sun.star.frame.Desktop", getXComponentContext());
189         XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, oDesktop);
190         return xDesktop.getCurrentFrame();
191     } catch (Exception e) {
192         e.printStackTrace(System.out);
193         return null;
194     }}
195 
196 
197     private XDispatch getXDispatcher(com.sun.star.util.URL oURL) {
198     try {
199         com.sun.star.util.URL[] oURLArray = new com.sun.star.util.URL[1];
200         oURLArray[0] = oURL;
201         XDispatchProvider xDispatchProvider = (XDispatchProvider) UnoRuntime.queryInterface(XDispatchProvider.class, getCurrentFrame());
202         XDispatch xDispatch = xDispatchProvider.queryDispatch(oURLArray[0], "_top", FrameSearchFlag.ALL); // "_self"
203         return xDispatch;
204     } catch (Exception e) {
205         e.printStackTrace(System.out);
206         return null;
207     }}
208 
209 
210     private PropertyValue[] loadArgs(String url) {
211         PropertyValue pv = new PropertyValue();
212         pv.Name = "URL";
213         pv.Value = url;
214         return new PropertyValue[] {pv};
215     }
216 
217 
218 
219     public boolean isFilterApplicable(String _sFilter, String _sName){
220         boolean bFilterDoesApply = true;
221         if (_sFilter.length() > 0){
222             if (_sName.indexOf(_sFilter) == -1){
223                 bFilterDoesApply = false;
224             }
225         }
226         return bFilterDoesApply;
227     }
228 
229 
230 //    public static String getServiceDescription(Object _oUnoObject){
231 //        String sClassName = "";
232 //        XServiceInfo xServiceInfo = (XServiceInfo) UnoRuntime.queryInterface(XServiceInfo.class, _oUnoObject);
233 //        if (xServiceInfo != null){
234 //            String[] sChildServiceNames = removeMandatoryServiceNames(xServiceInfo.getSupportedServiceNames());
235 //            if (sChildServiceNames.length > 0){
236 //                sClassName = sChildServiceNames[0];
237 //            }
238 //        }
239 //        return sClassName;
240 //    }
241 
242 
243 
244     private static String[] getMandatoryServiceNames(String _sServiceName){
245     String[] sMandatoryServiceNames  = new String[]{};
246     try {
247         TypeClass[] eTypeClasses = new com.sun.star.uno.TypeClass[1];
248         eTypeClasses[0] = com.sun.star.uno.TypeClass.SERVICE;
249         XTypeDescriptionEnumeration xTDEnumeration = getXTypeDescriptionEnumerationAccess().createTypeDescriptionEnumeration(Introspector.getModuleName(_sServiceName), eTypeClasses, TypeDescriptionSearchDepth.INFINITE);
250         while (xTDEnumeration.hasMoreElements()) {
251             XTypeDescription xTD = xTDEnumeration.nextTypeDescription();
252             if (xTD.getName().equals(_sServiceName)){
253                 XServiceTypeDescription xServiceTypeDescription = (XServiceTypeDescription) UnoRuntime.queryInterface(XServiceTypeDescription.class, xTD);
254                 XServiceTypeDescription[] xMandatoryServiceTypeDescriptions =  xServiceTypeDescription.getMandatoryServices();
255                 int nlength = xMandatoryServiceTypeDescriptions.length;
256                 sMandatoryServiceNames  = new String[nlength];
257                 for (int i = 0; i < nlength; i++){
258                     sMandatoryServiceNames[i] = xMandatoryServiceTypeDescriptions[i].getName();
259                 }
260 
261             }
262         }
263     } catch ( java.lang.Exception e) {
264         System.out.println(System.out);
265     }
266         return sMandatoryServiceNames;
267     }
268 
269 
270     private static String[] removeMandatoryServiceNames(String[] _sServiceNames){
271     try{
272         List aList = java.util.Arrays.asList(_sServiceNames);
273         Vector aVector = new Vector(aList);
274         for (int n = 0; n < _sServiceNames.length; n++){
275             String[] sDelServiceNames = getMandatoryServiceNames(_sServiceNames[n]);
276             for (int m = 0; m < sDelServiceNames.length; m++){
277                 if (aVector.contains(sDelServiceNames[m])){
278                     int nIndex = aVector.indexOf(sDelServiceNames[m]);
279                     aVector.remove(nIndex);
280                 }
281             }
282         }
283         String[] sRetArray = new String[aVector.size()];
284         aVector.toArray(sRetArray);
285         return sRetArray;
286     } catch (java.lang.Exception exception) {
287         exception.printStackTrace(System.out);
288     }
289         return new String[]{};
290     }
291 
292 
293     public  static String getDisplayValueOfPrimitiveType(Object _objectElement){
294     String sValue ="";
295     try{
296         if (AnyConverter.isString(_objectElement)){
297             sValue = AnyConverter.toString(_objectElement);
298         }
299         else if (AnyConverter.isBoolean(_objectElement)){
300             sValue += AnyConverter.toBoolean(_objectElement);
301         }
302         else if (AnyConverter.isByte(_objectElement)){
303             sValue += AnyConverter.toByte(_objectElement);
304         }
305         else if (AnyConverter.isChar(_objectElement)){
306             sValue += AnyConverter.toChar(_objectElement);
307         }
308         else if (AnyConverter.isDouble(_objectElement)){
309             sValue += AnyConverter.toDouble(_objectElement);
310         }
311         else if (AnyConverter.isFloat(_objectElement)){
312             sValue += AnyConverter.toFloat(_objectElement);
313         }
314         else if (AnyConverter.isInt(_objectElement)){
315             sValue += AnyConverter.toInt(_objectElement);
316         }
317         else if (AnyConverter.isLong(_objectElement)){
318             sValue += AnyConverter.toLong(_objectElement);
319         }
320         else if (AnyConverter.isShort(_objectElement)){
321             sValue += AnyConverter.toShort(_objectElement);
322         }
323     }
324     catch( Exception e ) {
325         System.err.println( e );
326     }
327         return sValue;
328     }
329 
330     protected static String[] getDisplayValuesofPrimitiveArray(Object _oUnoObject){
331     String[] sDisplayValues = null;
332     try{
333         Type aType = AnyConverter.getType(_oUnoObject);
334         TypeClass aTypeClass = aType.getTypeClass();
335         int nTypeValue = aTypeClass.getValue();
336         if (nTypeValue == TypeClass.SEQUENCE_value){
337             nTypeValue = (sequenceComponentType(aType)).getTypeClass().getValue();
338         }
339         switch (nTypeValue){
340             case TypeClass.BOOLEAN_value:
341                 boolean[] bBooleans = (boolean[]) AnyConverter.toArray(_oUnoObject);
342                 sDisplayValues = new String[bBooleans.length];
343                 for (int i = 0; i < bBooleans.length; i++){
344                     sDisplayValues[i] = Boolean.toString(bBooleans[i]);
345                 }
346                 break;
347             case TypeClass.BYTE_value:
348                 byte[] bBytes = (byte[]) AnyConverter.toArray(_oUnoObject);
349                 sDisplayValues = new String[bBytes.length];
350                 for (int i = 0; i < bBytes.length; i++){
351                     sDisplayValues[i] = "" + bBytes[i];
352                 }
353                 break;
354             case TypeClass.DOUBLE_value:
355                 double[] fdoubles = (double[]) AnyConverter.toArray(_oUnoObject);
356                 sDisplayValues = new String[fdoubles.length];
357                 for (int i = 0; i < fdoubles.length; i++){
358                     sDisplayValues[i] = String.valueOf(fdoubles[i]);
359                 }
360                 break;
361             case TypeClass.FLOAT_value:
362                 float[] ffloats = (float[]) AnyConverter.toArray(_oUnoObject);
363                 sDisplayValues = new String[ffloats.length];
364                 for (int i = 0; i < ffloats.length; i++){
365                     sDisplayValues[i] = String.valueOf(ffloats[i]);
366                 }
367                 break;
368             case TypeClass.LONG_value:
369                 int[] nints = (int[]) AnyConverter.toArray(_oUnoObject);
370                 sDisplayValues = new String[nints.length];
371                 for (int i = 0; i < nints.length; i++){
372                     sDisplayValues[i] = String.valueOf(nints[i]);
373                 }
374                 break;
375             case TypeClass.HYPER_value:
376                 long[] nlongs = (long[]) AnyConverter.toArray(_oUnoObject);
377                 sDisplayValues = new String[nlongs.length];
378                 for (int i = 0; i < nlongs.length; i++){
379                     sDisplayValues[i] = String.valueOf(nlongs[i]);
380                 }
381                 break;
382             case TypeClass.SHORT_value:
383                 short[] nShorts = (short[]) AnyConverter.toArray(_oUnoObject);
384                 sDisplayValues = new String[nShorts.length];
385                 for (int i = 0; i < nShorts.length; i++){
386                     sDisplayValues[i] = "" +  nShorts[i];
387                 }
388                 break;
389             case TypeClass.CHAR_value:
390                 break;
391             default:
392                 System.out.println("Value could not be retrieved: " + aType.getTypeClass().getClass().getName());
393             }
394         return sDisplayValues;
395     }
396     catch( Exception e ) {
397         System.err.println( e );
398         return null;
399     }}
400 
401 
402     private static Type sequenceComponentType(Type sequenceType) {
403 //        assert sequenceType.getTypeClass() == TypeClass.SEQUENCE;
404         String n = sequenceType.getTypeName();
405         final String PREFIX = "[]";
406 //        assert n.startsWith(PREFIX);
407         return new Type(n.substring(PREFIX.length()));
408     }
409 
410 
411     public static String getNodeDescription(Object _oUnoObject, int _nIndex){
412         return getNodeDescription(_oUnoObject) + "[" + (_nIndex + 1) + "]";
413     }
414 
415 
416     public static String getNodeDescription(Object _oUnoObject){
417         XServiceInfo xServiceInfo = ( XServiceInfo ) UnoRuntime.queryInterface( XServiceInfo.class, _oUnoObject );
418         if ( xServiceInfo != null ) {
419             return xServiceInfo.getImplementationName();
420         }
421         String sClassName = _oUnoObject.getClass().getName();
422         if (Introspector.getIntrospector().isObjectPrimitive(_oUnoObject)){         //super.isO{sObjectClassName.equals("java.lang.String"))issClassName.equals("java.lang.String"))
423             return _oUnoObject.toString();
424         }
425         else{
426             return _oUnoObject.getClass().getName();
427         }
428     }
429 
430     public void setParameterObjects(Object[] _oParamObjects){
431         m_oParamObjects = _oParamObjects;
432     }
433 
434     public Object[] getParameterObjects(){
435         return m_oParamObjects;
436     }
437 }
438