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
.HashMap
;
38 import com
.sun
.star
.reflection
.ParamInfo
;
39 import com
.sun
.star
.reflection
.XIdlClass
;
40 import com
.sun
.star
.reflection
.XIdlMethod
;
41 import com
.sun
.star
.reflection
.XTypeDescription
;
42 import com
.sun
.star
.uno
.Any
;
43 import com
.sun
.star
.uno
.AnyConverter
;
44 import com
.sun
.star
.uno
.TypeClass
;
48 public class SourceCodeGenerator
{
49 private ArrayList
<String
> sExceptions
= new ArrayList
<String
>();
50 private ArrayList
<String
> sHeaderStatements
= new HeaderStatements();
51 private XLanguageSourceCodeGenerator m_xLanguageSourceCodeGenerator
;
52 private String sStatementCode
= "";
53 private String sMainMethodSignature
= "";
55 private HashMap
<String
, UnoObjectDefinition
> aVariables
= new HashMap
<String
, UnoObjectDefinition
>();
56 private final String SSUFFIXSEPARATOR
= "_";
57 private final String SVARIABLENAME
= "VariableName";
58 private Introspector m_oIntrospector
;
59 private ArrayList
<XTreePathProvider
> aTreepathProviders
= new ArrayList
<XTreePathProvider
>();
60 private boolean baddExceptionHandling
= false;
61 private boolean bXPropertySetExceptionsAreAdded
= false;
62 private XUnoNode oInitialUnoNode
= null;
63 private final String sINITIALVARIABLENAME
= "_oUnoEntryObject";
66 /** Creates a new instance of SourceCodeGenerator */
67 public SourceCodeGenerator(int _nLanguage
) {
68 this.setLanguage(_nLanguage
);
69 m_oIntrospector
= Introspector
.getIntrospector();
73 public String
addSourceCodeOfUnoObject(XTreePathProvider _xTreepathProvider
, boolean _brememberPath
, boolean _baddHeader
){
74 String sVariableName
= "";
75 if (_xTreepathProvider
!= null) {
76 for (int i
= 0; i
< _xTreepathProvider
.getPathCount(); i
++){
77 XUnoNode oUnoNode
= _xTreepathProvider
.getPathComponent(i
);
79 sVariableName
= sINITIALVARIABLENAME
;
80 oInitialUnoNode
= oUnoNode
;
83 if (oUnoNode
instanceof XUnoMethodNode
){
84 XUnoMethodNode oUnoMethodNode
= (XUnoMethodNode
) oUnoNode
;
85 if (oUnoMethodNode
.isInvoked()){
86 UnoObjectDefinition oUnoReturnObjectDefinition
= getUnoObjectDefinition(_xTreepathProvider
, oUnoMethodNode
, i
);
87 if (!isVariableDeclared(oUnoReturnObjectDefinition
, this.generateVariableNameFromMethod(oUnoMethodNode
.getXIdlMethod()))){
88 sStatementCode
+= "\n" + getMethodStatementSourceCode(oUnoMethodNode
, sVariableName
, oUnoReturnObjectDefinition
);
90 sVariableName
= oUnoReturnObjectDefinition
.getVariableName();
93 else if (oUnoNode
instanceof XUnoPropertyNode
){
94 XUnoPropertyNode oUnoPropertyNode
= (XUnoPropertyNode
) oUnoNode
;
95 Any oReturnObject
= com
.sun
.star
.uno
.Any
.complete(oUnoPropertyNode
.getUnoReturnObject());
96 UnoObjectDefinition oUnoReturnObjectDefinition
= new UnoObjectDefinition(oReturnObject
);
97 if (!isVariableDeclared(oUnoReturnObjectDefinition
, oUnoPropertyNode
.getProperty().Name
)){
98 sStatementCode
+= "\n" + getPropertyStatementSourceCode(oUnoPropertyNode
, sVariableName
, oUnoReturnObjectDefinition
);
100 sVariableName
= oUnoReturnObjectDefinition
.getVariableName();
105 String sCompleteCode
= combineCompleteSourceCode(_baddHeader
);
107 aTreepathProviders
.add(_xTreepathProvider
);
109 return sCompleteCode
;
113 private void setLanguage(int _nLanguage
){
114 XLanguageSourceCodeGenerator xLanguageSourceCodeGenerator
= null;
116 case XLanguageSourceCodeGenerator
.nJAVA
:
117 xLanguageSourceCodeGenerator
= new JavaCodeGenerator();
119 case XLanguageSourceCodeGenerator
.nCPLUSPLUS
:
120 xLanguageSourceCodeGenerator
= new CPlusPlusCodeGenerator();
122 case XLanguageSourceCodeGenerator
.nBASIC
:
123 xLanguageSourceCodeGenerator
= new BasicCodeGenerator();
126 System
.out
.println("Unknown Sourcecode Language. Check Menus!");
128 if (xLanguageSourceCodeGenerator
!= null){
129 m_xLanguageSourceCodeGenerator
= xLanguageSourceCodeGenerator
;
133 private void resetSourceCodeGeneration(int _nLanguage
){
135 this.sHeaderStatements
.clear();
136 setLanguage(_nLanguage
);
140 private String
generateVariableNameFromMethod(String _sMethodName
, String _sPrefix
, boolean _bConsiderAll
){
142 if (_sMethodName
.startsWith(_sPrefix
)){
143 int nPrefixLength
= _sPrefix
.length();
144 if (_sMethodName
.length() > nPrefixLength
){
145 String sChar
= _sMethodName
.substring(nPrefixLength
, nPrefixLength
+ 1);
146 String sUpperChar
= sChar
.toUpperCase();
147 if (sUpperChar
.equals(sChar
)){
149 sReturn
= _sMethodName
;
152 sReturn
= _sMethodName
.substring(nPrefixLength
, _sMethodName
.length());
161 private String
generateVariableNameFromMethod(XIdlMethod _xIdlMethod
){
162 // todo: refactor this!!!
163 String sMethodName
= _xIdlMethod
.getName();
165 sReturn
= generateVariableNameFromMethod(sMethodName
, "getBy", false);
166 if (sReturn
.equals("")){
167 sReturn
= generateVariableNameFromMethod(sMethodName
, "get", false);
169 if (sReturn
.equals("")){
170 sReturn
= generateVariableNameFromMethod(sMethodName
, "attach", false);
172 if (sReturn
.equals("")){
173 sReturn
= generateVariableNameFromMethod(sMethodName
, "assign", false);
175 if (sReturn
.equals("")){
176 sReturn
= generateVariableNameFromMethod(sMethodName
, "attach", false);
178 if (sReturn
.equals("")){
179 sReturn
= generateVariableNameFromMethod(sMethodName
, "create", false);
181 if (sReturn
.equals("")){
182 sReturn
= generateVariableNameFromMethod(sMethodName
, "is", true);
184 if (sReturn
.equals("")){
185 sReturn
= generateVariableNameFromMethod(sMethodName
, "has", true);
187 if (sReturn
.equals("")){
188 sReturn
= sMethodName
;
194 public String
convertAllUnoObjects(int _nLanguage
){
195 String sSourceCode
= "";
196 resetSourceCodeGeneration(_nLanguage
);
197 int ncount
= aTreepathProviders
.size();
198 for (int i
=0; i
< ncount
; i
++){
199 sSourceCode
= addSourceCodeOfUnoObject(aTreepathProviders
.get(i
), false, (i
== (ncount
-1)));
204 private UnoObjectDefinition
getUnoObjectDefinition(XTreePathProvider _xTreePathProvider
, XUnoMethodNode _oUnoMethodNode
, int _nindex
){
205 XUnoNode oUnoNode
= null;
206 Object oUnoReturnObject
= null;
207 Object
[] oParamObjects
= null;
208 XIdlClass xIdlClass
= _oUnoMethodNode
.getXIdlMethod().getReturnType();
209 String sTypeName
= xIdlClass
.getName();
210 TypeClass aTypeClass
= xIdlClass
.getTypeClass();
211 if (aTypeClass
.getValue() != TypeClass
.VOID_value
){
212 if (_xTreePathProvider
.getPathCount() > _nindex
+ 1){
213 oUnoNode
= _xTreePathProvider
.getPathComponent(_nindex
+ 1);
214 oUnoReturnObject
= oUnoNode
.getUnoObject();
217 if (oUnoReturnObject
== null){
218 oUnoReturnObject
= _oUnoMethodNode
.getLastUnoReturnObject();
220 UnoObjectDefinition oUnoObjectDefinition
= new UnoObjectDefinition(oUnoReturnObject
, sTypeName
, aTypeClass
);
221 if (_oUnoMethodNode
.hasParameters()){
222 if (oUnoNode
!= null){
223 oParamObjects
= oUnoNode
.getParameterObjects();
226 oParamObjects
= _oUnoMethodNode
.getLastParameterObjects();
229 if (oParamObjects
!= null){
230 oUnoObjectDefinition
.addParameterObjects(oParamObjects
);
232 return oUnoObjectDefinition
;
236 private String
combineCompleteSourceCode(boolean _bAddHeader
){
237 String sCompleteCode
= "";
239 sMainMethodSignature
= m_xLanguageSourceCodeGenerator
.getMainMethodSignatureSourceCode(oInitialUnoNode
, sINITIALVARIABLENAME
);
240 m_xLanguageSourceCodeGenerator
.assignqueryInterfaceHeaderSourceCode();
241 sCompleteCode
+= getHeaderSourceCode();
243 sCompleteCode
+= sMainMethodSignature
;
244 sCompleteCode
+= sStatementCode
;
246 sCompleteCode
+= m_xLanguageSourceCodeGenerator
.getMethodTerminationSourceCode();
247 sCompleteCode
+= "\n" + m_xLanguageSourceCodeGenerator
.getCommentSign() + "...";
249 return sCompleteCode
;
253 private String
getPropertyStatementSourceCode(XUnoPropertyNode _oUnoPropertyNode
, String _sVariableName
, UnoObjectDefinition _oUnoReturnObjectDefinition
){
254 String sReturnObjectVariableDefinition
= "";
255 String sStatement
= "";
256 String sPropertyName
= _oUnoPropertyNode
.getProperty().Name
;
257 UnoObjectDefinition oUnoObjectDefinition
= new UnoObjectDefinition(_oUnoPropertyNode
.getUnoObject(), "com.sun.star.beans.XPropertySet");
258 if (!m_xLanguageSourceCodeGenerator
.needsqueryInterface() || (oUnoObjectDefinition
.getTypeClass().getValue() == TypeClass
.STRUCT_value
)){
259 oUnoObjectDefinition
.setVariableName(_sVariableName
);
260 if (oUnoObjectDefinition
.getTypeClass().getValue() == TypeClass
.STRUCT_value
){
261 sReturnObjectVariableDefinition
= getVariableInitialization(_oUnoReturnObjectDefinition
, false);
262 sStatement
+= m_xLanguageSourceCodeGenerator
.getStructSourceCode(sReturnObjectVariableDefinition
, oUnoObjectDefinition
.getVariableName(), sPropertyName
);
266 sStatement
+= addQueryInterfaceSourceCode(oUnoObjectDefinition
, _sVariableName
, "com.sun.star.beans.XPropertySet");
267 if (_oUnoReturnObjectDefinition
.getTypeClass().getValue() != TypeClass
.VOID_value
){
268 sReturnObjectVariableDefinition
= getVariableInitialization(_oUnoReturnObjectDefinition
, true);
270 sStatement
+= m_xLanguageSourceCodeGenerator
.getPropertyValueGetterSourceCode(sPropertyName
, sReturnObjectVariableDefinition
, oUnoObjectDefinition
.getVariableName(), _oUnoReturnObjectDefinition
.getTypeClass(), _oUnoReturnObjectDefinition
.getTypeName());
271 addXPropertySetRelatedExceptions();
276 private String
getMethodStatementSourceCode(XUnoMethodNode _oUnoMethodNode
, String _sVariableName
, UnoObjectDefinition _oUnoReturnObjectDefinition
){
277 String sReturnObjectVariableDefinition
= "";
278 String sStatement
= "";
279 XIdlMethod xIdlMethod
= _oUnoMethodNode
.getXIdlMethod();
280 TypeClass aReturnTypeClass
= xIdlMethod
.getReturnType().getTypeClass();
281 UnoObjectDefinition oUnoObjectDefinition
= new UnoObjectDefinition(_oUnoMethodNode
.getUnoObject(), _oUnoMethodNode
.getClassName());
282 String sVariableStemName
= this.generateVariableNameFromMethod(xIdlMethod
);
283 sStatement
+= addQueryInterfaceSourceCode(oUnoObjectDefinition
, _sVariableName
, oUnoObjectDefinition
.getTypeName());
284 if (_oUnoReturnObjectDefinition
.getTypeClass().getValue() != TypeClass
.VOID_value
){
285 sReturnObjectVariableDefinition
= getVariableInitialization(_oUnoReturnObjectDefinition
, false) + " = ";
287 Object
[] oParamObjects
= _oUnoReturnObjectDefinition
.getParameterObjects();
288 String sParameterCode
= getMethodParameterValueDescription(_oUnoMethodNode
, oParamObjects
, false);
289 String sSeparator
= m_xLanguageSourceCodeGenerator
.getMethodSeparator();
290 sStatement
+= "\t" + sReturnObjectVariableDefinition
+ oUnoObjectDefinition
.getVariableName() + sSeparator
+ xIdlMethod
.getName() + "(" + sParameterCode
+ ")";
291 sStatement
+= m_xLanguageSourceCodeGenerator
.getStatementTerminationCharacter();
292 addExceptions(xIdlMethod
);
297 private String
addQueryInterfaceSourceCode(UnoObjectDefinition _oUnoObjectDefinition
, String _sVariableName
, String _sTypeName
){
298 String sLocStatement
= "";
299 if (m_xLanguageSourceCodeGenerator
.needsqueryInterface()){
300 if (!isVariableDeclared(_oUnoObjectDefinition
, "")){
301 String sObjectVariableDefinition
= getVariableDeclaration(_oUnoObjectDefinition
, false, "");
302 sLocStatement
+= m_xLanguageSourceCodeGenerator
.getqueryInterfaceSourceCode(_sTypeName
, sObjectVariableDefinition
, _sVariableName
);
306 _oUnoObjectDefinition
.setVariableName(_sVariableName
);
308 return sLocStatement
;
312 private void addXPropertySetRelatedExceptions(){
313 if (!bXPropertySetExceptionsAreAdded
){
314 sExceptions
.add("com.sun.star.beans.UnknownPropertyException");
315 sExceptions
.add("com.sun.star.lang.WrappedTargetException");
316 sExceptions
.add("com.sun.star.lang.IllegalArgumentException");
317 bXPropertySetExceptionsAreAdded
= true;
318 baddExceptionHandling
= true;
323 private void addExceptions(XIdlMethod _xIdlMethod
){
324 XIdlClass
[] xIdlClasses
= _xIdlMethod
.getExceptionTypes();
325 for (int i
= 0; i
> xIdlClasses
.length
; i
++){
326 sExceptions
.add(xIdlClasses
[0].getName());
327 baddExceptionHandling
= true;
331 private String
getHeaderSourceCode(){
332 for(UnoObjectDefinition oUnoObjectDefinition
: aVariables
.values()){
333 String sCurHeaderStatement
= m_xLanguageSourceCodeGenerator
.getHeaderSourceCode(oUnoObjectDefinition
.getUnoObject(), oUnoObjectDefinition
.getTypeName(), oUnoObjectDefinition
.getTypeClass());
334 sHeaderStatements
.add(sCurHeaderStatement
);
336 String sHeaderSourcecode
= "";
337 String
[] sHeaderStatementArray
= new String
[sHeaderStatements
.size()];
338 sHeaderStatements
.toArray(sHeaderStatementArray
);
339 java
.util
.Arrays
.sort(sHeaderStatementArray
);
340 for (int i
= 0; i
< sHeaderStatementArray
.length
; i
++){
341 sHeaderSourcecode
+= sHeaderStatementArray
[i
];
343 sHeaderSourcecode
+= m_xLanguageSourceCodeGenerator
.getFinalHeaderStatements();
344 return sHeaderSourcecode
+"\n" + m_xLanguageSourceCodeGenerator
.getCommentSign() + "...\n";
348 private class HeaderStatements
extends ArrayList
<String
> {
350 public boolean contains(String _oElement
){
351 String sCompName
= _oElement
;
352 for (int i
= 0; i
< this.size(); i
++){
353 String sElement
= this.get(i
);
354 if (sElement
.equals(sCompName
)){
363 public boolean add(String _oElement
){
364 if (!contains(_oElement
)){
365 super.add(_oElement
);
373 private boolean isVariableDeclared(UnoObjectDefinition _oUnoObjectDefinition
, String _sDefaultStemName
){
374 boolean bisDeclared
= false;
375 if (!_sDefaultStemName
.equals("")){
376 _oUnoObjectDefinition
.setCentralVariableStemName(_sDefaultStemName
);
378 String sVariableStemName
= _oUnoObjectDefinition
.getVariableStemName();
379 bisDeclared
= aVariables
.containsKey(sVariableStemName
);
381 Object oUnoObject
= _oUnoObjectDefinition
.getUnoObject();
382 if (Introspector
.isObjectPrimitive(oUnoObject
)){
385 else if (Introspector
.isObjectSequence(oUnoObject
)){
389 String sCompVariableName
= sVariableStemName
;
390 String sUnoObjectIdentity
= oUnoObject
.toString();
391 boolean bleaveloop
= false;
394 if (aVariables
.containsKey(sCompVariableName
)){
395 Object oUnoCompObject
= aVariables
.get(sCompVariableName
).getUnoObject();
396 String sUnoCompObjectIdentity
= oUnoCompObject
.toString();
397 bleaveloop
= sUnoCompObjectIdentity
.equals(sUnoObjectIdentity
);
398 bisDeclared
= bleaveloop
;
400 sCompVariableName
= sVariableStemName
+ SSUFFIXSEPARATOR
+ a
++;
414 private String
addUniqueVariableName(String _sStemVariableName
, UnoObjectDefinition _oUnoObjectDefinition
){
415 boolean bElementexists
= true;
417 String sCompName
= _sStemVariableName
;
418 while (bElementexists
){
419 if (! aVariables
.containsKey(sCompName
)){
420 aVariables
.put(sCompName
, _oUnoObjectDefinition
);
423 sCompName
= _sStemVariableName
+ SSUFFIXSEPARATOR
+ a
++;
430 private String
getTypeString(String _sTypeName
, TypeClass _aTypeClass
, boolean _bAsHeaderSourceCode
){
431 String sTypeString
= "";
432 switch (_aTypeClass
.getValue()){
433 case TypeClass
.BOOLEAN_value
:
434 sTypeString
= m_xLanguageSourceCodeGenerator
.getbooleanTypeDescription();
436 case TypeClass
.BYTE_value
:
437 sTypeString
= m_xLanguageSourceCodeGenerator
.getbyteTypeDescription();
439 case TypeClass
.CHAR_value
:
440 sTypeString
= m_xLanguageSourceCodeGenerator
.getcharTypeDescription();
442 case TypeClass
.DOUBLE_value
:
443 sTypeString
= m_xLanguageSourceCodeGenerator
.getdoubleTypeDescription();
445 case TypeClass
.FLOAT_value
:
446 sTypeString
= m_xLanguageSourceCodeGenerator
.getfloatTypeDescription();
448 case TypeClass
.HYPER_value
:
449 sTypeString
= m_xLanguageSourceCodeGenerator
.gethyperTypeDescription();
451 case TypeClass
.LONG_value
:
452 sTypeString
= m_xLanguageSourceCodeGenerator
.getlongTypeDescription();
454 case TypeClass
.SHORT_value
:
455 sTypeString
= m_xLanguageSourceCodeGenerator
.getshortTypeDescription();
457 case TypeClass
.STRING_value
:
458 sTypeString
= m_xLanguageSourceCodeGenerator
.getstringTypeDescription(_bAsHeaderSourceCode
);
460 case TypeClass
.UNSIGNED_HYPER_value
:
461 sTypeString
= m_xLanguageSourceCodeGenerator
.getunsignedhyperTypeDescription();
463 case TypeClass
.UNSIGNED_LONG_value
:
464 sTypeString
= m_xLanguageSourceCodeGenerator
.getunsignedlongTypeDescription();
466 case TypeClass
.UNSIGNED_SHORT_value
:
467 sTypeString
= m_xLanguageSourceCodeGenerator
.getdoubleTypeDescription();
469 case TypeClass
.SEQUENCE_value
:
470 //TODO consider mulitdimensional Arrays
471 XTypeDescription xTypeDescription
= Introspector
.getIntrospector().getReferencedType(_sTypeName
);
472 if (xTypeDescription
!= null){
473 sTypeString
= getTypeString(xTypeDescription
.getName(), xTypeDescription
.getTypeClass(), _bAsHeaderSourceCode
);
476 case TypeClass
.ANY_value
:
477 sTypeString
= m_xLanguageSourceCodeGenerator
.getanyTypeDescription(_bAsHeaderSourceCode
);
479 case TypeClass
.TYPE_value
:
480 sTypeString
= m_xLanguageSourceCodeGenerator
.getObjectTypeDescription("com.sun.star.uno.Type", _bAsHeaderSourceCode
);
482 case TypeClass
.ENUM_value
:
483 case TypeClass
.STRUCT_value
:
484 case TypeClass
.INTERFACE_ATTRIBUTE_value
:
485 case TypeClass
.INTERFACE_METHOD_value
:
486 case TypeClass
.INTERFACE_value
:
487 case TypeClass
.PROPERTY_value
:
488 sTypeString
= m_xLanguageSourceCodeGenerator
.getObjectTypeDescription(_sTypeName
, _bAsHeaderSourceCode
);
496 private String
getVariableDeclaration(UnoObjectDefinition _oUnoObjectDefinition
, boolean _bInitialize
, String _sVariableDefaultName
){
497 TypeClass aTypeClass
= _oUnoObjectDefinition
.getTypeClass();
498 TypeClass aLocTypeClass
= aTypeClass
;
499 boolean bIsArray
= false;
500 if (_oUnoObjectDefinition
.getUnoObject() != null){
501 bIsArray
= Introspector
.isObjectSequence(_oUnoObjectDefinition
.getUnoObject());
504 bIsArray
= _oUnoObjectDefinition
.getTypeClass().getValue() == TypeClass
.SEQUENCE_value
;
506 String sVariableName
= _oUnoObjectDefinition
.getVariableName(_sVariableDefaultName
);
507 String sTypeName
= _oUnoObjectDefinition
.getTypeName();
508 String sTypeString
= getTypeString(sTypeName
, aLocTypeClass
, false);
510 XTypeDescription xTypeDescription
= Introspector
.getIntrospector().getReferencedType(sTypeName
);
511 if (xTypeDescription
!= null){
512 aLocTypeClass
= xTypeDescription
.getTypeClass();
515 String sVariableDeclaration
= m_xLanguageSourceCodeGenerator
.getVariableDeclaration(sTypeString
, sVariableName
, bIsArray
, aLocTypeClass
, _bInitialize
);
516 addUniqueVariableName(sVariableName
, _oUnoObjectDefinition
);
517 return sVariableDeclaration
;
521 private String
getVariableInitialization(UnoObjectDefinition _oUnoObjectDefinition
, boolean _bInitialize
){
522 String sObjectVariableDeclaration
= "";
523 String sVariableName
= _oUnoObjectDefinition
.getVariableName();
524 if (isVariableDeclared(_oUnoObjectDefinition
, "")){
525 sObjectVariableDeclaration
= sVariableName
;
528 sObjectVariableDeclaration
= getVariableDeclaration(_oUnoObjectDefinition
, _bInitialize
, "");
530 return sObjectVariableDeclaration
;
535 public String
getVariableNameforUnoObject(String _sShortClassName
){
536 if (_sShortClassName
.startsWith("X")){
537 return "x" + _sShortClassName
.substring(1);
540 return _sShortClassName
;
545 private class UnoObjectDefinition
{
546 private Object m_oUnoObject
= null;
548 private String sVariableStemName
= "";
549 private String m_sCentralVariableStemName
= "";
550 private String sVariableName
= "";
551 private String m_sTypeName
= "";
552 private TypeClass m_aTypeClass
= null;
553 private Object
[] m_oParameterObjects
= null;
556 private UnoObjectDefinition(Any _oUnoObject
){
557 m_sTypeName
= _oUnoObject
.getType().getTypeName();
558 m_aTypeClass
= _oUnoObject
.getType().getTypeClass();
559 m_oUnoObject
= _oUnoObject
;
560 m_sCentralVariableStemName
= getCentralVariableStemName(m_aTypeClass
);
564 private UnoObjectDefinition(Object _oUnoObject
, String _sTypeName
, TypeClass _aTypeClass
){
565 m_oUnoObject
= _oUnoObject
;
566 m_sTypeName
= _sTypeName
;
567 m_aTypeClass
= _aTypeClass
;
568 m_sCentralVariableStemName
= getCentralVariableStemName(m_aTypeClass
);
572 private UnoObjectDefinition(Object _oUnoObject
, String _sTypeName
){
573 m_oUnoObject
= _oUnoObject
;
574 m_sTypeName
= _sTypeName
;
575 m_aTypeClass
= AnyConverter
.getType(_oUnoObject
).getTypeClass();
576 m_sCentralVariableStemName
= getCentralVariableStemName(m_aTypeClass
);
580 private String
getCentralVariableStemName(TypeClass _aTypeClass
){
581 String sCentralVariableStemName
= "";
582 int nTypeClass
= _aTypeClass
.getValue();
584 case TypeClass
.SEQUENCE_value
:
585 //TODO consider mulitdimensional Arrays
586 XTypeDescription xTypeDescription
= Introspector
.getIntrospector().getReferencedType(getTypeName());
587 if (xTypeDescription
!= null){
588 sCentralVariableStemName
= getCentralVariableStemName(xTypeDescription
.getTypeClass());
591 case TypeClass
.TYPE_value
:
592 sCentralVariableStemName
= SVARIABLENAME
;
594 case TypeClass
.STRUCT_value
:
595 sCentralVariableStemName
= Introspector
.getShortClassName(getTypeName());
597 case TypeClass
.INTERFACE_ATTRIBUTE_value
:
598 case TypeClass
.INTERFACE_METHOD_value
:
599 case TypeClass
.INTERFACE_value
:
600 case TypeClass
.PROPERTY_value
:
601 String sShortClassName
= Introspector
.getShortClassName(getTypeName());
602 sCentralVariableStemName
= getVariableNameforUnoObject(sShortClassName
);
604 sCentralVariableStemName
= SVARIABLENAME
;
606 return sCentralVariableStemName
;
611 public Object
getUnoObject(){
616 public TypeClass
getTypeClass(){
621 public String
getTypeName(){
626 public void setCentralVariableStemName(String _sCentralVariableStemName
){
627 m_sCentralVariableStemName
= _sCentralVariableStemName
;
631 public String
getVariableStemName(){
632 if (sVariableStemName
.equals("")){
633 sVariableStemName
= getVariableStemName(m_aTypeClass
);
635 return sVariableStemName
;
639 private void addParameterObjects(Object
[] _oParameterObjects
){
640 m_oParameterObjects
= _oParameterObjects
;
644 public Object
[] getParameterObjects(){
645 return m_oParameterObjects
;
652 private String
getVariableStemName(TypeClass _aTypeClass
){
653 int nTypeClass
= _aTypeClass
.getValue();
655 case TypeClass
.BOOLEAN_value
:
656 sVariableStemName
= "b" + m_sCentralVariableStemName
;
658 case TypeClass
.DOUBLE_value
:
659 case TypeClass
.FLOAT_value
:
660 sVariableStemName
= "f" + m_sCentralVariableStemName
;
662 case TypeClass
.BYTE_value
:
663 case TypeClass
.HYPER_value
:
664 case TypeClass
.LONG_value
:
665 case TypeClass
.UNSIGNED_HYPER_value
:
666 case TypeClass
.UNSIGNED_LONG_value
:
667 case TypeClass
.UNSIGNED_SHORT_value
:
668 case TypeClass
.SHORT_value
:
669 sVariableStemName
= "n" + m_sCentralVariableStemName
;
671 case TypeClass
.CHAR_value
:
672 case TypeClass
.STRING_value
:
673 sVariableStemName
= "s" + m_sCentralVariableStemName
;
675 case TypeClass
.SEQUENCE_value
:
676 //TODO consider mulitdimensional Arrays
677 XTypeDescription xTypeDescription
= Introspector
.getIntrospector().getReferencedType(getTypeName());
678 if (xTypeDescription
!= null){
679 sVariableStemName
= getVariableStemName(xTypeDescription
.getTypeClass());
682 case TypeClass
.TYPE_value
:
683 sVariableStemName
= "a" + m_sCentralVariableStemName
;
685 case TypeClass
.ANY_value
:
686 sVariableStemName
= "o" + m_sCentralVariableStemName
;
688 case TypeClass
.STRUCT_value
:
689 case TypeClass
.ENUM_value
:
690 sVariableStemName
= "a" + m_sCentralVariableStemName
;
692 case TypeClass
.INTERFACE_ATTRIBUTE_value
:
693 case TypeClass
.INTERFACE_METHOD_value
:
694 case TypeClass
.INTERFACE_value
:
695 case TypeClass
.PROPERTY_value
:
696 String sShortClassName
= Introspector
.getShortClassName(getTypeName());
697 sVariableStemName
= getVariableNameforUnoObject(sShortClassName
);
700 return sVariableStemName
;
704 private void setVariableName(String _sVariableName
){
705 sVariableName
= _sVariableName
;
709 private String
getVariableName(String _sCentralVariableStemName
){
710 if (!_sCentralVariableStemName
.equals("")){
711 this.m_sCentralVariableStemName
= _sCentralVariableStemName
;
713 return getVariableName();
717 private String
getVariableName() throws NullPointerException
{
718 if (sVariableName
.equals("")){
720 sVariableName
= getVariableStemName();
721 boolean bleaveloop
= false;
723 if (aVariables
.containsKey(sVariableName
)){
724 String sUnoObjectIdentity
= aVariables
.get(sVariableName
).getUnoObject().toString();
725 if (m_oUnoObject
!= null){
726 if ((sUnoObjectIdentity
.equals(m_oUnoObject
.toString()) && (!Introspector
.isPrimitive(this.getTypeClass())) &&
727 (! Introspector
.isObjectSequence(m_oUnoObject
)))){
731 sVariableName
= getVariableStemName() + SSUFFIXSEPARATOR
+ a
++;
743 return sVariableName
;
748 public String
getStringValueOfObject(Object _oUnoObject
, TypeClass _aTypeClass
){
750 switch (_aTypeClass
.getValue()){
751 case TypeClass
.BOOLEAN_value
:
752 boolean bbooleanValue
= ((Boolean
) _oUnoObject
).booleanValue();
753 sReturn
+= Boolean
.toString(bbooleanValue
);
754 case TypeClass
.CHAR_value
:
756 case TypeClass
.DOUBLE_value
:
757 double fdoubleValue
= ((Double
) _oUnoObject
).doubleValue();
758 sReturn
+= Double
.toString(fdoubleValue
);
760 case TypeClass
.ENUM_value
:
762 case TypeClass
.FLOAT_value
:
763 float floatValue
= ((Float
) _oUnoObject
).floatValue();
764 sReturn
+= Float
.toString(floatValue
);
766 case TypeClass
.HYPER_value
:
767 long nlongValue
= ((Long
) _oUnoObject
).longValue();
768 sReturn
+= Long
.toString(nlongValue
);
770 case TypeClass
.LONG_value
:
771 int nintValue
= ((Integer
) _oUnoObject
).intValue();
772 sReturn
+= Integer
.toString(nintValue
);
774 case TypeClass
.SHORT_value
:
775 short nshortValue
= ((Short
) _oUnoObject
).shortValue();
776 sReturn
+= Short
.toString(nshortValue
);
778 case TypeClass
.STRING_value
:
779 sReturn
+= (String
) _oUnoObject
;
781 case TypeClass
.UNSIGNED_HYPER_value
:
782 nlongValue
= ((Long
) _oUnoObject
).longValue();
783 sReturn
+= Long
.toString(nlongValue
);
785 case TypeClass
.UNSIGNED_LONG_value
:
786 nintValue
= ((Integer
) _oUnoObject
).intValue();
787 sReturn
+= Integer
.toString(nintValue
);
789 case TypeClass
.UNSIGNED_SHORT_value
:
790 nshortValue
= ((Short
) _oUnoObject
).shortValue();
791 sReturn
+= Short
.toString(nshortValue
);
794 System
.out
.println("Type " + _aTypeClass
.getValue() + " not yet defined in 'getStringValueOfObject()'");
800 public String
getMethodParameterValueDescription(XUnoMethodNode _oUnoMethodNode
, Object
[] _oParamObjects
, boolean _bIncludeParameterNames
){
801 String sParamSourceCode
= "";
802 ParamInfo
[] aParamInfos
= _oUnoMethodNode
.getXIdlMethod().getParameterInfos();
803 if (_oParamObjects
!= null){
804 for (int i
= 0; i
< _oParamObjects
.length
; i
++){
805 TypeClass aTypeClass
= aParamInfos
[i
].aType
.getTypeClass();
806 if (_bIncludeParameterNames
){
807 sParamSourceCode
+= aParamInfos
[i
].aName
+ "=";
809 String sParamDescription
= getStringValueOfObject(_oParamObjects
[i
], aTypeClass
);
810 sParamDescription
= this.m_xLanguageSourceCodeGenerator
.castLiteral(sParamDescription
, aTypeClass
);
811 sParamSourceCode
+= sParamDescription
;
813 if (i
< _oParamObjects
.length
- 1){
814 sParamSourceCode
+= ", ";
818 return sParamSourceCode
;
822 private class JavaCodeGenerator
implements XLanguageSourceCodeGenerator
{
824 private boolean bAddAnyConverter
= false;
825 private boolean bIsPropertyUnoObjectDefined
= false;
827 public JavaCodeGenerator(){
831 public String
getStatementTerminationCharacter(){
836 public String
getHeaderSourceCode(Object _oUnoObject
, String _sClassName
, TypeClass _aTypeClass
){
837 String sClassName
= _sClassName
;
838 String sHeaderStatement
= "";
839 if (_oUnoObject
!= null){
840 if (!Introspector
.isObjectPrimitive(_oUnoObject
)){
841 if (Introspector
.isObjectSequence(_oUnoObject
)){
842 XTypeDescription xTypeDescription
= m_oIntrospector
.getReferencedType(sClassName
);
843 if (xTypeDescription
!= null){
844 if (!Introspector
.isPrimitive(xTypeDescription
.getTypeClass())){
845 sClassName
= getTypeString(xTypeDescription
.getName(), xTypeDescription
.getTypeClass(), true);
847 // primitive Types are not supposed to turn up in the import section...
854 sClassName
= getTypeString(_sClassName
, _aTypeClass
, true);
857 else if (_aTypeClass
.getValue() == TypeClass
.ENUM_value
){
858 sClassName
= _sClassName
;
863 if (!sClassName
.equals("")){
864 sHeaderStatement
= "import " + sClassName
+ ";\n";
867 return sHeaderStatement
;
871 public String
getFinalHeaderStatements(){
876 public void assignqueryInterfaceHeaderSourceCode(){
877 sHeaderStatements
.add("import com.sun.star.uno.UnoRuntime;\n");
878 sHeaderStatements
.add("import com.sun.star.uno.XInterface;\n");
879 if (bAddAnyConverter
){
880 sHeaderStatements
.add("import com.sun.star.uno.AnyConverter;\n");
886 public String
getConvertedSourceCodeValueOfObject(String _sReturnVariableName
, String _sObjectDescription
, TypeClass _aTypeClass
, String _sTypeName
){
887 boolean bLocAddAnyConverter
= true;
889 switch (_aTypeClass
.getValue()){
890 case TypeClass
.BOOLEAN_value
:
891 sReturn
= _sReturnVariableName
+ " = AnyConverter.toBoolean(" + _sObjectDescription
+ ")";
893 case TypeClass
.CHAR_value
:
894 sReturn
= _sReturnVariableName
+ " = AnyConverter.toChar(" + _sObjectDescription
+ ")";
896 case TypeClass
.BYTE_value
:
897 sReturn
= _sReturnVariableName
+ " = AnyConverter.toByte(" + _sObjectDescription
+ ")";
899 case TypeClass
.DOUBLE_value
:
900 sReturn
= _sReturnVariableName
+ " = AnyConverter.toDouble(" + _sObjectDescription
+ ")";
902 case TypeClass
.FLOAT_value
:
903 sReturn
= _sReturnVariableName
+ " = AnyConverter.toFloat(" + _sObjectDescription
+ ")";
905 case TypeClass
.UNSIGNED_HYPER_value
:
906 sReturn
= _sReturnVariableName
+ " = AnyConverter.toUnsignedLong(" + _sObjectDescription
+ ")";
908 case TypeClass
.HYPER_value
:
909 sReturn
= _sReturnVariableName
+ " = AnyConverter.toLong(" + _sObjectDescription
+ ")";
911 case TypeClass
.UNSIGNED_LONG_value
:
912 sReturn
= _sReturnVariableName
+ " = AnyConverter.toUnsignedInt(" + _sObjectDescription
+ ")";
914 case TypeClass
.LONG_value
:
915 sReturn
= _sReturnVariableName
+ " = AnyConverter.toInt(" + _sObjectDescription
+ ")";
917 case TypeClass
.SHORT_value
:
918 sReturn
= _sReturnVariableName
+ " = AnyConverter.toShort(" + _sObjectDescription
+ ")";
920 case TypeClass
.UNSIGNED_SHORT_value
:
921 sReturn
= _sReturnVariableName
+ " = AnyConverter.toUnsignedShort(" + _sObjectDescription
+ ")";
923 case TypeClass
.STRING_value
:
924 sReturn
= _sReturnVariableName
+ " = AnyConverter.toString(" + _sObjectDescription
+ ")";
927 String sShortTypeName
= Introspector
.getShortClassName(_sTypeName
);
928 if (bIsPropertyUnoObjectDefined
){
929 sReturn
= "oUnoObject = " + _sObjectDescription
+ ";\n\t";
932 sReturn
= "Object oUnoObject = " + _sObjectDescription
+ ";\n\t";
933 bIsPropertyUnoObjectDefined
= true;
935 sReturn
+= _sReturnVariableName
+ " = (" + sShortTypeName
+ ") AnyConverter.toObject(" + sShortTypeName
+ ".class, oUnoObject);";
938 if (!bAddAnyConverter
){
939 bAddAnyConverter
= bLocAddAnyConverter
;
945 public String
getStructSourceCode(String _sReturnVariableDescription
, String _sObjectDescription
, String _sMember
){
946 return "\t" + _sReturnVariableDescription
+ " = " + _sObjectDescription
+ "." + _sMember
+ ";";
949 public String
getMainMethodSignatureSourceCode(XUnoNode _oUnoNode
, String _soReturnObjectDescription
){
950 //TODO try to use + _oUnoNode.getClassName() instead of the hack
951 String sReturn
= "public void codesnippet(XInterface " + _soReturnObjectDescription
+ "){";
952 if (baddExceptionHandling
){
958 public String
getMethodSeparator(){
962 public boolean needsqueryInterface(){
966 public String
getqueryInterfaceSourceCode(String _sClassName
, String _sReturnVariableName
, String _sIncomingObjectName
){
967 String sShortClassName
= Introspector
.getShortClassName(_sClassName
);
968 return "\t" + _sReturnVariableName
+ " = (" + sShortClassName
+ ") UnoRuntime.queryInterface(" + sShortClassName
+ ".class, " + _sIncomingObjectName
+ ");\n";
972 public String
getPropertyValueGetterSourceCode(String _sPropertyName
, String _sReturnVariableName
, String _sIncomingObjectName
, TypeClass _aTypeClass
, String _sTypeName
){
973 String sObjectDescription
= _sIncomingObjectName
+ ".getPropertyValue(\"" + _sPropertyName
+ "\")";
974 String sReturn
= getConvertedSourceCodeValueOfObject(_sReturnVariableName
, sObjectDescription
, _aTypeClass
, _sTypeName
);
976 sReturn
= "\t" + sReturn
;
981 public String
getObjectTypeDescription(String _sClassName
, boolean _bAsHeader
){
984 sReturn
= _sClassName
;
987 sReturn
= Introspector
.getShortClassName(_sClassName
);
993 public String
getMethodTerminationSourceCode(){
996 String sExceptionName
= "e";
997 if (baddExceptionHandling
){
998 for (int i
= 0; i
< sExceptions
.size(); i
++){
999 String sCurException
= sExceptions
.get(i
);
1000 if (sReturn
.indexOf(sCurException
) == -1){
1002 sExceptionName
= "e"+ nIndex
;
1007 sReturn
+= "catch (" + sCurException
+ " " + sExceptionName
+ "){\n";
1008 sReturn
+= "\t" + sExceptionName
+ ".printStackTrace(System.err);\n";
1009 sReturn
+= "\t" + getCommentSign() + "Enter your Code here...\n}";
1018 public String
castLiteral(String _sExpression
, TypeClass _aTypeClass
){
1019 String sReturn
= "";
1020 switch (_aTypeClass
.getValue()){
1021 case TypeClass
.BOOLEAN_value
:
1022 sReturn
= _sExpression
;
1024 case TypeClass
.BYTE_value
:
1025 sReturn
= "(byte) " + _sExpression
;
1027 case TypeClass
.CHAR_value
:
1028 sReturn
= "'" + _sExpression
+ "'";
1030 case TypeClass
.DOUBLE_value
:
1031 sReturn
= "(double) " + _sExpression
;
1033 case TypeClass
.FLOAT_value
:
1034 sReturn
= "(float) " + _sExpression
;
1036 case TypeClass
.UNSIGNED_SHORT_value
:
1037 case TypeClass
.SHORT_value
:
1038 sReturn
= "(short) " + _sExpression
;
1040 case TypeClass
.STRING_value
:
1041 sReturn
= "\"" + _sExpression
+ "\"";
1043 case TypeClass
.HYPER_value
:
1044 case TypeClass
.UNSIGNED_HYPER_value
:
1045 sReturn
= "(long) " + _sExpression
;
1047 case TypeClass
.LONG_value
:
1048 sReturn
= _sExpression
;
1050 case TypeClass
.ENUM_value
:
1052 sReturn
= _sExpression
;
1053 System
.out
.println("Type " + _aTypeClass
.getValue() + " not yet defined in 'castliteral()'");
1059 public String
getbooleanTypeDescription(){
1063 public String
getbyteTypeDescription(){
1067 public String
getshortTypeDescription(){
1071 public String
getlongTypeDescription(){
1075 public String
getunsignedlongTypeDescription(){
1079 public String
gethyperTypeDescription(){
1083 public String
getunsignedhyperTypeDescription(){
1087 public String
getfloatTypeDescription(){
1091 public String
getdoubleTypeDescription(){
1095 public String
getcharTypeDescription(){
1099 public String
getstringTypeDescription(boolean _bAsHeaderSourceCode
){
1100 if (_bAsHeaderSourceCode
){
1108 public String
getanyTypeDescription(boolean _bAsHeaderSourceCode
){
1109 if (_bAsHeaderSourceCode
){
1118 public String
getStringValue(String _sValue
){
1123 public String
getVariableDeclaration(String _sTypeString
, String _sVariableName
, boolean _bIsArray
, TypeClass _aTypeClass
, boolean _bInitialize
){
1124 String sReturn
= "";
1126 sReturn
= _sTypeString
+ "[] " + _sVariableName
;
1129 sReturn
= _sTypeString
+ " " + _sVariableName
;
1135 public String
getCommentSign(){
1141 private class BasicCodeGenerator
implements XLanguageSourceCodeGenerator
{
1144 public BasicCodeGenerator(){
1147 public String
getHeaderSourceCode(Object _oUnoObject
, String _sClassName
, TypeClass _aTypeClass
){
1148 String sHeaderStatement
= "";
1149 return sHeaderStatement
;
1152 public String
getFinalHeaderStatements(){
1156 public String
getMainMethodSignatureSourceCode(XUnoNode _oUnoNode
, String _soReturnObjectDescription
){
1157 //TODO try to use + _oUnoNode.getClassName() instead of the hack
1158 return "Sub Main(" + _soReturnObjectDescription
+ " as " + getanyTypeDescription(false) + ")";
1161 public boolean needsqueryInterface(){
1165 public void assignqueryInterfaceHeaderSourceCode(){
1168 public String
getMethodSeparator(){
1173 public String
getqueryInterfaceSourceCode(String _sClassName
, String _sReturnVariableName
, String _sIncomingObjectName
){
1174 return _sIncomingObjectName
;
1178 public String
getPropertyValueGetterSourceCode(String _sPropertyName
, String _sReturnVariableName
, String _sIncomingObjectName
, TypeClass _aTypeClass
, String _sTypeName
){
1179 return "\t" + _sReturnVariableName
+ " = " + _sIncomingObjectName
+ "." + _sPropertyName
;
1183 public String
getStructSourceCode(String _sReturnVariableDescription
, String _sObjectDescription
, String _sMember
){
1184 return getPropertyValueGetterSourceCode(_sMember
, _sReturnVariableDescription
, _sObjectDescription
, null, "" );
1187 public String
getConvertedSourceCodeValueOfObject(String _sReturnVariableName
, String _sObjectDescription
, TypeClass _aTypeClass
, String _sTypeName
){
1188 return _sReturnVariableName
+ " = " + _sObjectDescription
;
1192 public String
getObjectTypeDescription(String _sClassName
, boolean _bAsHeader
){
1197 public String
getMethodTerminationSourceCode(){
1198 return "\nEnd Sub\n";
1202 public String
castLiteral(String _sExpression
, TypeClass _aTypeClass
){
1203 String sReturn
= "";
1204 switch (_aTypeClass
.getValue()){
1205 case TypeClass
.BOOLEAN_value
:
1206 case TypeClass
.BYTE_value
:
1207 case TypeClass
.DOUBLE_value
:
1208 case TypeClass
.FLOAT_value
:
1209 case TypeClass
.UNSIGNED_SHORT_value
:
1210 case TypeClass
.SHORT_value
:
1211 case TypeClass
.LONG_value
:
1212 case TypeClass
.UNSIGNED_LONG_value
:
1213 case TypeClass
.HYPER_value
:
1214 case TypeClass
.UNSIGNED_HYPER_value
:
1215 sReturn
= _sExpression
;
1217 case TypeClass
.CHAR_value
:
1218 case TypeClass
.STRING_value
:
1219 sReturn
= "\"" +_sExpression
+ "\"";
1221 case TypeClass
.ENUM_value
:
1223 sReturn
= _sExpression
;
1224 System
.out
.println("Type " + _aTypeClass
.getValue() + " not yet defined in 'castliteral()'");
1231 public String
getbooleanTypeDescription(){
1235 public String
getbyteTypeDescription(){
1239 public String
getshortTypeDescription(){
1243 public String
getlongTypeDescription(){
1247 public String
getunsignedlongTypeDescription(){
1251 public String
gethyperTypeDescription(){
1255 public String
getunsignedhyperTypeDescription(){
1259 public String
getfloatTypeDescription(){
1263 public String
getdoubleTypeDescription(){
1267 public String
getcharTypeDescription(){
1271 public String
getstringTypeDescription(boolean _bAsHeaderSourceCode
){
1272 if (_bAsHeaderSourceCode
){
1280 public String
getanyTypeDescription(boolean _bAsHeaderSourceCode
){
1281 if (_bAsHeaderSourceCode
){
1289 public String
getStatementTerminationCharacter(){
1294 public String
getVariableDeclaration(String _sTypeString
, String _sVariableName
, boolean bIsArray
, TypeClass _aTypeClass
, boolean _bInitialize
){
1295 String sReturn
= "";
1297 sReturn
= "Dim " + _sVariableName
+ "() as " + _sTypeString
+ "\n\t" + _sVariableName
;
1300 sReturn
= "Dim " + _sVariableName
+ " as " + _sTypeString
+ "\n\t" + _sVariableName
;
1306 public String
getStringValue(String _sValue
){
1311 public String
getCommentSign(){
1317 private class CPlusPlusCodeGenerator
implements XLanguageSourceCodeGenerator
{
1319 private boolean bIncludeStringHeader
= false;
1320 private boolean bIncludeAny
= false;
1321 private boolean bIncludeSequenceHeader
= false;
1323 public CPlusPlusCodeGenerator(){
1326 private String
getCSSNameSpaceString(){
1330 public String
getStatementTerminationCharacter(){
1335 public String
getHeaderSourceCode(Object _oUnoObject
, String _sClassName
, TypeClass _aTypeClass
){
1336 String sClassName
= _sClassName
;
1337 String sHeaderStatement
= "";
1338 if (_oUnoObject
!= null){
1339 if (!Introspector
.isObjectPrimitive(_oUnoObject
)){
1340 if (Introspector
.isObjectSequence(_oUnoObject
)){
1341 XTypeDescription xTypeDescription
= m_oIntrospector
.getReferencedType(sClassName
);
1342 if (xTypeDescription
!= null){
1343 if (!Introspector
.isPrimitive(xTypeDescription
.getTypeClass())){
1344 sClassName
= getTypeString(xTypeDescription
.getName(), xTypeDescription
.getTypeClass(), true);
1346 // primitive Types are not supposed to turn up in the import section...
1353 sClassName
= getTypeString(_sClassName
, _aTypeClass
, true);
1355 if (!sClassName
.equals("")){
1356 sHeaderStatement
= getHeaderOfClass(sClassName
);
1360 return sHeaderStatement
;
1365 public String
getFinalHeaderStatements(){
1366 String sReturn
= "";
1367 sReturn
+= "\nnamespace " + getCSSNameSpaceString() + " = com::sun::star;\n";
1368 sReturn
+= "using ::rtl::OUString;\n";
1373 private String
getHeaderOfClass(String _sClassName
){
1374 return "#include \"" + _sClassName
.replace('.', '/') + ".hpp\"\n";
1379 public void assignqueryInterfaceHeaderSourceCode(){
1380 sHeaderStatements
.add("#include \"sal/config.h\"\n");
1381 sHeaderStatements
.add("#include \"sal/types.h\"\n");
1382 if (bIncludeStringHeader
){
1383 sHeaderStatements
.add("#include \"rtl/ustring.hxx\"\n");
1385 sHeaderStatements
.add("#include \"com/sun/star/uno/Reference.hxx\"\n");
1386 if (bIncludeSequenceHeader
){
1387 sHeaderStatements
.add("#include \"com/sun/star/uno/Sequence.hxx\"\n");
1389 sHeaderStatements
.add(getHeaderOfClass("com.sun.star.uno.XInterface"));
1391 sHeaderStatements
.add(getHeaderOfClass("com.sun.star.uno.Any"));
1396 public String
getMainMethodSignatureSourceCode(XUnoNode _oUnoNode
, String _soReturnObjectDescription
){
1397 String sReturn
= "";
1398 sReturn
= "void codesnippet(const " + getCSSNameSpaceString() + "::uno::Reference<" + getCSSNameSpaceString() + "::uno::XInterface>& " + _soReturnObjectDescription
+ " ){";
1400 if (!sExceptions
.contains("com.sun.star.uno.RuntimeException")){
1401 sExceptions
.add("com.sun.star.uno.RuntimeException");
1403 if (baddExceptionHandling
){
1404 sReturn
+= "\n//throw ";
1405 for (int i
= 0; i
< sExceptions
.size(); i
++){
1406 String sCurException
= sExceptions
.get(i
);
1407 if (sReturn
.indexOf(sCurException
) == -1){
1411 sReturn
+= getObjectTypeDescription(sCurException
, false);
1422 public boolean needsqueryInterface(){
1427 public String
getqueryInterfaceSourceCode(String _sClassName
, String _sReturnVariableName
, String _sIncomingObjectName
){
1428 return "\t" + _sReturnVariableName
+ "( " + _sIncomingObjectName
+ ", " + getCSSNameSpaceString() + "::uno::UNO_QUERY_THROW);\n";
1432 public String
getPropertyValueGetterSourceCode(String _sPropertyName
, String _sReturnVariableName
, String _sIncomingObjectName
, TypeClass _aTypeClass
, String _sTypeName
){
1433 String sFirstLine
= "\t";
1434 String sReturnVariableName
= _sReturnVariableName
;
1435 // e.g. uno::Any a = xPropSet->getPropertyValue( rtl::OUString( "DefaultContext" ) );
1436 String
[] sVarDefinition
= _sReturnVariableName
.split("=");
1437 if (sVarDefinition
.length
> 0){
1438 String sVariable
= sVarDefinition
[0];
1439 String
[] sVarDeclaration
= sVariable
.split(" ");
1440 if (sVarDeclaration
.length
> 0){
1441 sFirstLine
+= sReturnVariableName
+ ";\n";
1442 sReturnVariableName
= sVarDeclaration
[sVarDeclaration
.length
-1];
1445 String sObjectDescription
= _sIncomingObjectName
+ "->getPropertyValue(" + getStringValue(_sPropertyName
) + ")";
1446 String sSecondLine
= "\t" + getConvertedSourceCodeValueOfObject(sReturnVariableName
, sObjectDescription
, _aTypeClass
, _sTypeName
) + ";";
1447 return sFirstLine
+ sSecondLine
;
1451 public String
getStructSourceCode(String _sReturnVariableDescription
, String _sObjectDescription
, String _sMember
){
1452 return "\t" + _sReturnVariableDescription
+ " = " + _sObjectDescription
+ "->" + _sMember
+ ";";
1456 public String
getConvertedSourceCodeValueOfObject(String _sReturnVariableName
, String _sObjectDescription
, TypeClass _aTypeClass
, String _sTypeName
){
1457 return _sObjectDescription
+ " >>= " + _sReturnVariableName
;
1461 public String
getStringValue(String _sValue
){
1462 bIncludeStringHeader
= true;
1463 return "OUString(\"" + _sValue
+ "\")";
1467 public String
getObjectTypeDescription(String _sClassName
, boolean _bAsHeader
){
1468 String sReturn
= "";
1470 sReturn
= _sClassName
.replace('.', '/');
1473 String sModuleName
= Introspector
.getModuleName(_sClassName
);
1474 sModuleName
= Introspector
.getShortClassName(sModuleName
);
1475 sReturn
= getCSSNameSpaceString() + "::" + sModuleName
+ "::" + Introspector
.getShortClassName(_sClassName
);
1481 public String
getMethodTerminationSourceCode(){
1485 public String
getMethodSeparator(){
1490 public String
castLiteral(String _sExpression
, TypeClass _aTypeClass
){
1491 String sReturn
= "";
1492 switch (_aTypeClass
.getValue()){
1493 case TypeClass
.BOOLEAN_value
:
1494 case TypeClass
.BYTE_value
:
1495 case TypeClass
.DOUBLE_value
:
1496 case TypeClass
.FLOAT_value
:
1497 case TypeClass
.UNSIGNED_SHORT_value
:
1498 case TypeClass
.SHORT_value
:
1499 case TypeClass
.LONG_value
:
1500 case TypeClass
.UNSIGNED_LONG_value
:
1501 case TypeClass
.HYPER_value
:
1502 case TypeClass
.UNSIGNED_HYPER_value
:
1503 sReturn
= _sExpression
;
1505 case TypeClass
.CHAR_value
:
1506 sReturn
= "'" + _sExpression
+ "'";
1508 case TypeClass
.STRING_value
:
1509 sReturn
= getStringValue(_sExpression
);
1511 case TypeClass
.ENUM_value
:
1513 sReturn
= _sExpression
;
1514 System
.out
.println("Type " + _aTypeClass
.getValue() + " not yet defined in 'castliteral()'");
1519 public String
getbooleanTypeDescription(){
1523 public String
getbyteTypeDescription(){
1527 public String
getshortTypeDescription(){
1531 public String
getlongTypeDescription(){
1535 public String
getunsignedlongTypeDescription(){
1536 return "sal_uInt32";
1539 public String
gethyperTypeDescription(){
1543 public String
getunsignedhyperTypeDescription(){
1544 return "sal_uInt64";
1547 public String
getfloatTypeDescription(){
1551 public String
getdoubleTypeDescription(){
1555 public String
getcharTypeDescription(){
1556 return "sal_Unicode";
1559 public String
getstringTypeDescription(boolean _bAsHeaderSourceCode
){
1560 bIncludeStringHeader
= true;
1561 if (_bAsHeaderSourceCode
){
1569 public String
getanyTypeDescription(boolean _bAsHeaderSourceCode
){
1570 if (_bAsHeaderSourceCode
){
1571 return "com/sun/star/uno/XInterface";
1574 return "XInterface";
1579 public String
getVariableDeclaration(String _sTypeString
, String _sVariableName
, boolean bIsArray
, TypeClass _aTypeClass
, boolean _bInitialize
){
1580 boolean bIsPrimitive
= Introspector
.isPrimitive(_aTypeClass
);
1582 String sReturn
= "";
1584 bIncludeSequenceHeader
= true;
1585 sReturn
= getCSSNameSpaceString() + "::uno::Sequence<" + _sTypeString
+ "> " + _sVariableName
;
1589 sReturn
= _sTypeString
+ " " + _sVariableName
;
1591 switch (_aTypeClass
.getValue()){
1592 case TypeClass
.BOOLEAN_value
:
1593 sReturn
= sReturn
+ " = false";
1595 case TypeClass
.BYTE_value
:
1596 case TypeClass
.UNSIGNED_SHORT_value
:
1597 case TypeClass
.SHORT_value
:
1598 case TypeClass
.LONG_value
:
1599 case TypeClass
.UNSIGNED_LONG_value
:
1600 case TypeClass
.HYPER_value
:
1601 case TypeClass
.UNSIGNED_HYPER_value
:
1602 sReturn
= sReturn
+ " = 0";
1604 case TypeClass
.DOUBLE_value
:
1605 case TypeClass
.FLOAT_value
:
1606 sReturn
= sReturn
+ " = 0.0";
1608 case TypeClass
.CHAR_value
:
1609 sReturn
= sReturn
+ "'0'";
1611 case TypeClass
.STRING_value
:
1612 sReturn
= _sTypeString
+ " " + _sVariableName
;
1615 sReturn
= _sTypeString
+ " " + _sVariableName
;
1616 System
.out
.println("Type " + _aTypeClass
.getValue() + " not yet defined in 'getVariableDeclaration()'");
1621 sReturn
= getCSSNameSpaceString() + "::uno::Reference<" + _sTypeString
+ "> " +_sVariableName
;
1627 public String
getCommentSign(){