1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
33 *************************************************************************/
35 import java
.util
.ArrayList
;
36 import java
.util
.List
;
38 import com
.sun
.star
.beans
.PropertyValue
;
39 import com
.sun
.star
.frame
.FrameSearchFlag
;
40 import com
.sun
.star
.frame
.XDesktop
;
41 import com
.sun
.star
.frame
.XDispatch
;
42 import com
.sun
.star
.frame
.XDispatchProvider
;
43 import com
.sun
.star
.frame
.XFrame
;
44 import com
.sun
.star
.lang
.XMultiComponentFactory
;
45 import com
.sun
.star
.lang
.XServiceInfo
;
46 import com
.sun
.star
.reflection
.TypeDescriptionSearchDepth
;
47 import com
.sun
.star
.reflection
.XServiceTypeDescription
;
48 import com
.sun
.star
.reflection
.XTypeDescription
;
49 import com
.sun
.star
.reflection
.XTypeDescriptionEnumeration
;
50 import com
.sun
.star
.reflection
.XTypeDescriptionEnumerationAccess
;
51 import com
.sun
.star
.uno
.AnyConverter
;
52 import com
.sun
.star
.uno
.Type
;
53 import com
.sun
.star
.uno
.TypeClass
;
54 import com
.sun
.star
.uno
.UnoRuntime
;
55 import com
.sun
.star
.uno
.XComponentContext
;
56 import com
.sun
.star
.util
.URL
;
57 import com
.sun
.star
.util
.XURLTransformer
;
63 private XMultiComponentFactory m_xMultiComponentFactory
;
64 private XComponentContext m_xComponentContext
;
65 private Object
[] m_oParamObjects
= null;
66 private int m_nNodeType
= XUnoNode
.nOTHERS
;
67 private Type aType
= null;
68 private String sLabel
= "";
72 /** Creates a new instance of UnoNode */
73 public UnoNode(Object _oUnoObject
) {
74 m_xComponentContext
= Introspector
.getIntrospector().getXComponentContext();
75 m_xMultiComponentFactory
= m_xComponentContext
.getServiceManager();
76 m_oUnoObject
= _oUnoObject
;
79 public UnoNode(Object _oUnoObject
, Type _aType
) {
82 m_nNodeType
= XUnoNode
.nINTERFACE
;
85 public Object
getUnoObject(){
90 protected XComponentContext
getXComponentContext(){
91 return m_xComponentContext
;
95 protected XMultiComponentFactory
getXMultiComponentFactory(){
96 return m_xMultiComponentFactory
;
100 private static XTypeDescriptionEnumerationAccess
getXTypeDescriptionEnumerationAccess(){
101 return Introspector
.getIntrospector().getXTypeDescriptionEnumerationAccess();
105 public String
getAnchor(){
109 public int getNodeType(){
113 public void setNodeType(int _nNodeType
){
114 m_nNodeType
= _nNodeType
;
117 public String
getClassName(){
118 String sClassName
= "";
119 if (m_nNodeType
== XUnoNode
.nINTERFACE
){
120 sClassName
= aType
.getTypeName();
122 else if(m_nNodeType
== XUnoNode
.nSERVICE
){
129 public Type
getUnoType(){
133 protected void setLabel(String _sLabel
){
137 public void openIdlDescription(String _sIDLUrl
, String _sClassName
, String _sAnchor
){
139 String sIDLUrl
= _sIDLUrl
;
140 String sAnchor
= ""; // TODO find out how the Anchor may be set at the html file; //_sAnchor;
141 boolean bExists
= Introspector
.getIntrospector().getXSimpleFileAccess().exists(sIDLUrl
);
142 if (sIDLUrl
.equals("") || (!bExists
)){
143 sIDLUrl
= "http://api.libreoffice.org/" + Inspector
.sIDLDOCUMENTSUBFOLDER
;
145 if (!sIDLUrl
.endsWith("/")){
148 if (_sClassName
.equals("")){
149 sIDLUrl
+= "com/sun/star/module-ix";
153 sIDLUrl
+= _sClassName
.replace('.', '/');
155 if (sAnchor
!= null){
156 if (!sAnchor
.equals("")){
157 sIDLUrl
+= "#" + sAnchor
;
161 URL openHyperlink
= getDispatchURL(".uno:OpenHyperlink");
162 PropertyValue pv
= new PropertyValue();
165 getXDispatcher(openHyperlink
).dispatch(openHyperlink
, new PropertyValue
[] {pv
});
166 } catch(Exception exception
) {
167 exception
.printStackTrace(System
.err
);
171 private com
.sun
.star
.util
.URL
getDispatchURL(String _sURL
){
173 Object oTransformer
= getXMultiComponentFactory().createInstanceWithContext("com.sun.star.util.URLTransformer", getXComponentContext());
174 XURLTransformer xTransformer
= UnoRuntime
.queryInterface(XURLTransformer
.class, oTransformer
);
175 com
.sun
.star
.util
.URL
[] oURL
= new com
.sun
.star
.util
.URL
[1];
176 oURL
[0] = new com
.sun
.star
.util
.URL();
177 oURL
[0].Complete
= _sURL
;
178 xTransformer
.parseStrict(oURL
);
180 } catch (Exception e
) {
181 e
.printStackTrace(System
.err
);
187 private XFrame
getCurrentFrame(){
189 Object oDesktop
= getXMultiComponentFactory().createInstanceWithContext("com.sun.star.frame.Desktop", getXComponentContext());
190 XDesktop xDesktop
= UnoRuntime
.queryInterface(XDesktop
.class, oDesktop
);
191 return xDesktop
.getCurrentFrame();
192 } catch (Exception e
) {
193 e
.printStackTrace(System
.err
);
198 private XDispatch
getXDispatcher(com
.sun
.star
.util
.URL oURL
) {
200 com
.sun
.star
.util
.URL
[] oURLArray
= new com
.sun
.star
.util
.URL
[1];
202 XDispatchProvider xDispatchProvider
= UnoRuntime
.queryInterface(XDispatchProvider
.class, getCurrentFrame());
203 XDispatch xDispatch
= xDispatchProvider
.queryDispatch(oURLArray
[0], "_top", FrameSearchFlag
.ALL
); // "_self"
205 } catch (Exception e
) {
206 e
.printStackTrace(System
.err
);
211 private PropertyValue
[] loadArgs(String url
) {
212 PropertyValue pv
= new PropertyValue();
215 return new PropertyValue
[] {pv
};
220 public boolean isFilterApplicable(String _sFilter
, String _sName
){
221 boolean bFilterDoesApply
= true;
222 if (_sFilter
.length() > 0){
223 if (_sName
.indexOf(_sFilter
) == -1){
224 bFilterDoesApply
= false;
227 return bFilterDoesApply
;
231 // public static String getServiceDescription(Object _oUnoObject){
232 // String sClassName = "";
233 // XServiceInfo xServiceInfo = (XServiceInfo) UnoRuntime.queryInterface(XServiceInfo.class, _oUnoObject);
234 // if (xServiceInfo != null){
235 // String[] sChildServiceNames = removeMandatoryServiceNames(xServiceInfo.getSupportedServiceNames());
236 // if (sChildServiceNames.length > 0){
237 // sClassName = sChildServiceNames[0];
240 // return sClassName;
245 private static String
[] getMandatoryServiceNames(String _sServiceName
){
246 String
[] sMandatoryServiceNames
= new String
[]{};
248 TypeClass
[] eTypeClasses
= new com
.sun
.star
.uno
.TypeClass
[1];
249 eTypeClasses
[0] = com
.sun
.star
.uno
.TypeClass
.SERVICE
;
250 XTypeDescriptionEnumeration xTDEnumeration
= getXTypeDescriptionEnumerationAccess().createTypeDescriptionEnumeration(Introspector
.getModuleName(_sServiceName
), eTypeClasses
, TypeDescriptionSearchDepth
.INFINITE
);
251 while (xTDEnumeration
.hasMoreElements()) {
252 XTypeDescription xTD
= xTDEnumeration
.nextTypeDescription();
253 if (xTD
.getName().equals(_sServiceName
)){
254 XServiceTypeDescription xServiceTypeDescription
= UnoRuntime
.queryInterface(XServiceTypeDescription
.class, xTD
);
255 XServiceTypeDescription
[] xMandatoryServiceTypeDescriptions
= xServiceTypeDescription
.getMandatoryServices();
256 int nlength
= xMandatoryServiceTypeDescriptions
.length
;
257 sMandatoryServiceNames
= new String
[nlength
];
258 for (int i
= 0; i
< nlength
; i
++){
259 sMandatoryServiceNames
[i
] = xMandatoryServiceTypeDescriptions
[i
].getName();
264 } catch ( java
.lang
.Exception e
) {
265 System
.out
.println(System
.out
);
267 return sMandatoryServiceNames
;
271 private static String
[] removeMandatoryServiceNames(String
[] _sServiceNames
){
273 List
<String
> aList
= java
.util
.Arrays
.asList(_sServiceNames
);
274 ArrayList
<String
> aVector
= new ArrayList
<String
>(aList
);
275 for (int n
= 0; n
< _sServiceNames
.length
; n
++){
276 String
[] sDelServiceNames
= getMandatoryServiceNames(_sServiceNames
[n
]);
277 for (int m
= 0; m
< sDelServiceNames
.length
; m
++){
278 if (aVector
.contains(sDelServiceNames
[m
])){
279 int nIndex
= aVector
.indexOf(sDelServiceNames
[m
]);
280 aVector
.remove(nIndex
);
284 String
[] sRetArray
= new String
[aVector
.size()];
285 aVector
.toArray(sRetArray
);
287 } catch (java
.lang
.Exception exception
) {
288 exception
.printStackTrace(System
.err
);
290 return new String
[]{};
294 public static String
getDisplayValueOfPrimitiveType(Object _objectElement
){
297 if (AnyConverter
.isString(_objectElement
)){
298 sValue
= AnyConverter
.toString(_objectElement
);
300 else if (AnyConverter
.isBoolean(_objectElement
)){
301 sValue
+= AnyConverter
.toBoolean(_objectElement
);
303 else if (AnyConverter
.isByte(_objectElement
)){
304 sValue
+= AnyConverter
.toByte(_objectElement
);
306 else if (AnyConverter
.isChar(_objectElement
)){
307 sValue
+= AnyConverter
.toChar(_objectElement
);
309 else if (AnyConverter
.isDouble(_objectElement
)){
310 sValue
+= AnyConverter
.toDouble(_objectElement
);
312 else if (AnyConverter
.isFloat(_objectElement
)){
313 sValue
+= AnyConverter
.toFloat(_objectElement
);
315 else if (AnyConverter
.isInt(_objectElement
)){
316 sValue
+= AnyConverter
.toInt(_objectElement
);
318 else if (AnyConverter
.isLong(_objectElement
)){
319 sValue
+= AnyConverter
.toLong(_objectElement
);
321 else if (AnyConverter
.isShort(_objectElement
)){
322 sValue
+= AnyConverter
.toShort(_objectElement
);
325 catch( Exception e
) {
326 System
.err
.println( e
);
331 protected static String
[] getDisplayValuesofPrimitiveArray(Object _oUnoObject
){
332 String
[] sDisplayValues
= null;
334 Type aType
= AnyConverter
.getType(_oUnoObject
);
335 TypeClass aTypeClass
= aType
.getTypeClass();
336 int nTypeValue
= aTypeClass
.getValue();
337 if (nTypeValue
== TypeClass
.SEQUENCE_value
){
338 nTypeValue
= (sequenceComponentType(aType
)).getTypeClass().getValue();
341 case TypeClass
.BOOLEAN_value
:
342 boolean[] bBooleans
= (boolean[]) AnyConverter
.toArray(_oUnoObject
);
343 sDisplayValues
= new String
[bBooleans
.length
];
344 for (int i
= 0; i
< bBooleans
.length
; i
++){
345 sDisplayValues
[i
] = Boolean
.toString(bBooleans
[i
]);
348 case TypeClass
.BYTE_value
:
349 byte[] bBytes
= (byte[]) AnyConverter
.toArray(_oUnoObject
);
350 sDisplayValues
= new String
[bBytes
.length
];
351 for (int i
= 0; i
< bBytes
.length
; i
++){
352 sDisplayValues
[i
] = "" + bBytes
[i
];
355 case TypeClass
.DOUBLE_value
:
356 double[] fdoubles
= (double[]) AnyConverter
.toArray(_oUnoObject
);
357 sDisplayValues
= new String
[fdoubles
.length
];
358 for (int i
= 0; i
< fdoubles
.length
; i
++){
359 sDisplayValues
[i
] = String
.valueOf(fdoubles
[i
]);
362 case TypeClass
.FLOAT_value
:
363 float[] ffloats
= (float[]) AnyConverter
.toArray(_oUnoObject
);
364 sDisplayValues
= new String
[ffloats
.length
];
365 for (int i
= 0; i
< ffloats
.length
; i
++){
366 sDisplayValues
[i
] = String
.valueOf(ffloats
[i
]);
369 case TypeClass
.LONG_value
:
370 int[] nints
= (int[]) AnyConverter
.toArray(_oUnoObject
);
371 sDisplayValues
= new String
[nints
.length
];
372 for (int i
= 0; i
< nints
.length
; i
++){
373 sDisplayValues
[i
] = String
.valueOf(nints
[i
]);
376 case TypeClass
.HYPER_value
:
377 long[] nlongs
= (long[]) AnyConverter
.toArray(_oUnoObject
);
378 sDisplayValues
= new String
[nlongs
.length
];
379 for (int i
= 0; i
< nlongs
.length
; i
++){
380 sDisplayValues
[i
] = String
.valueOf(nlongs
[i
]);
383 case TypeClass
.SHORT_value
:
384 short[] nShorts
= (short[]) AnyConverter
.toArray(_oUnoObject
);
385 sDisplayValues
= new String
[nShorts
.length
];
386 for (int i
= 0; i
< nShorts
.length
; i
++){
387 sDisplayValues
[i
] = "" + nShorts
[i
];
390 case TypeClass
.CHAR_value
:
393 System
.out
.println("Value could not be retrieved: " + aType
.getTypeClass().getClass().getName());
395 return sDisplayValues
;
397 catch( Exception e
) {
398 System
.err
.println( e
);
403 private static Type
sequenceComponentType(Type sequenceType
) {
404 // assert sequenceType.getTypeClass() == TypeClass.SEQUENCE;
405 String n
= sequenceType
.getTypeName();
406 final String PREFIX
= "[]";
407 // assert n.startsWith(PREFIX);
408 return new Type(n
.substring(PREFIX
.length()));
412 public static String
getNodeDescription(Object _oUnoObject
, int _nIndex
){
413 return getNodeDescription(_oUnoObject
) + "[" + (_nIndex
+ 1) + "]";
417 public static String
getNodeDescription(Object _oUnoObject
){
418 XServiceInfo xServiceInfo
= UnoRuntime
.queryInterface( XServiceInfo
.class, _oUnoObject
);
419 if ( xServiceInfo
!= null ) {
420 return xServiceInfo
.getImplementationName();
422 String sClassName
= _oUnoObject
.getClass().getName();
423 if (Introspector
.isObjectPrimitive(_oUnoObject
)){ //super.isO{sObjectClassName.equals("java.lang.String"))issClassName.equals("java.lang.String"))
424 return _oUnoObject
.toString();
427 return _oUnoObject
.getClass().getName();
431 public void setParameterObjects(Object
[] _oParamObjects
){
432 m_oParamObjects
= _oParamObjects
;
435 public Object
[] getParameterObjects(){
436 return m_oParamObjects
;