Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Inspector / SourceCodeGenerator.java
blob2eae0ff44f2268fa4d378f3b637177ae62e44b40
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
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.
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);
78 if (i == 0){
79 sVariableName = sINITIALVARIABLENAME;
80 oInitialUnoNode = oUnoNode;
82 else{
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);
106 if (_brememberPath){
107 aTreepathProviders.add(_xTreepathProvider);
109 return sCompleteCode;
113 private void setLanguage(int _nLanguage){
114 XLanguageSourceCodeGenerator xLanguageSourceCodeGenerator = null;
115 switch(_nLanguage){
116 case XLanguageSourceCodeGenerator.nJAVA:
117 xLanguageSourceCodeGenerator = new JavaCodeGenerator();
118 break;
119 case XLanguageSourceCodeGenerator.nCPLUSPLUS:
120 xLanguageSourceCodeGenerator = new CPlusPlusCodeGenerator();
121 break;
122 case XLanguageSourceCodeGenerator.nBASIC:
123 xLanguageSourceCodeGenerator = new BasicCodeGenerator();
124 break;
125 default:
126 System.out.println("Unknown Sourcecode Language. Check Menus!");
128 if (xLanguageSourceCodeGenerator != null){
129 m_xLanguageSourceCodeGenerator = xLanguageSourceCodeGenerator;
133 private void resetSourceCodeGeneration(int _nLanguage){
134 aVariables.clear();
135 this.sHeaderStatements.clear();
136 setLanguage(_nLanguage);
137 sStatementCode = "";
140 private String generateVariableNameFromMethod(String _sMethodName, String _sPrefix, boolean _bConsiderAll){
141 String sReturn = "";
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)){
148 if (_bConsiderAll){
149 sReturn = _sMethodName;
151 else{
152 sReturn = _sMethodName.substring(nPrefixLength, _sMethodName.length());
157 return sReturn;
161 private String generateVariableNameFromMethod(XIdlMethod _xIdlMethod){
162 // todo: refactor this!!!
163 String sMethodName = _xIdlMethod.getName();
164 String sReturn = "";
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;
190 return sReturn;
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)));
201 return sSourceCode;
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();
225 else{
226 oParamObjects = _oUnoMethodNode.getLastParameterObjects();
229 if (oParamObjects != null){
230 oUnoObjectDefinition.addParameterObjects(oParamObjects);
232 return oUnoObjectDefinition;
236 private String combineCompleteSourceCode(boolean _bAddHeader){
237 String sCompleteCode = "";
238 if (_bAddHeader){
239 sMainMethodSignature = m_xLanguageSourceCodeGenerator.getMainMethodSignatureSourceCode(oInitialUnoNode, sINITIALVARIABLENAME);
240 m_xLanguageSourceCodeGenerator.assignqueryInterfaceHeaderSourceCode();
241 sCompleteCode += getHeaderSourceCode();
243 sCompleteCode += sMainMethodSignature;
244 sCompleteCode += sStatementCode;
245 if (_bAddHeader){
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);
263 return sStatement;
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();
272 return sStatement;
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);
293 return sStatement;
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);
305 else{
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)){
355 return true;
358 return false;
362 @Override
363 public boolean add(String _oElement){
364 if (!contains(_oElement)){
365 super.add(_oElement);
366 return true;
368 return false;
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);
380 if (bisDeclared){
381 Object oUnoObject = _oUnoObjectDefinition.getUnoObject();
382 if (Introspector.isObjectPrimitive(oUnoObject)){
383 bisDeclared = false;
385 else if (Introspector.isObjectSequence(oUnoObject)){
386 bisDeclared = false;
388 else{
389 String sCompVariableName = sVariableStemName;
390 String sUnoObjectIdentity = oUnoObject.toString();
391 boolean bleaveloop = false;
392 int a = 2;
393 while (!bleaveloop){
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;
399 if (!bleaveloop){
400 sCompVariableName = sVariableStemName + SSUFFIXSEPARATOR + a++;
403 else{
404 bleaveloop = true;
405 bisDeclared = false;
410 return bisDeclared;
414 private String addUniqueVariableName(String _sStemVariableName, UnoObjectDefinition _oUnoObjectDefinition){
415 boolean bElementexists = true;
416 int a = 2;
417 String sCompName = _sStemVariableName;
418 while (bElementexists){
419 if (! aVariables.containsKey(sCompName)){
420 aVariables.put(sCompName, _oUnoObjectDefinition);
421 break;
423 sCompName = _sStemVariableName + SSUFFIXSEPARATOR + a++;
425 return sCompName;
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();
435 break;
436 case TypeClass.BYTE_value:
437 sTypeString = m_xLanguageSourceCodeGenerator.getbyteTypeDescription();
438 break;
439 case TypeClass.CHAR_value:
440 sTypeString = m_xLanguageSourceCodeGenerator.getcharTypeDescription();
441 break;
442 case TypeClass.DOUBLE_value:
443 sTypeString = m_xLanguageSourceCodeGenerator.getdoubleTypeDescription();
444 break;
445 case TypeClass.FLOAT_value:
446 sTypeString = m_xLanguageSourceCodeGenerator.getfloatTypeDescription();
447 break;
448 case TypeClass.HYPER_value:
449 sTypeString = m_xLanguageSourceCodeGenerator.gethyperTypeDescription();
450 break;
451 case TypeClass.LONG_value:
452 sTypeString = m_xLanguageSourceCodeGenerator.getlongTypeDescription();
453 break;
454 case TypeClass.SHORT_value:
455 sTypeString = m_xLanguageSourceCodeGenerator.getshortTypeDescription();
456 break;
457 case TypeClass.STRING_value:
458 sTypeString = m_xLanguageSourceCodeGenerator.getstringTypeDescription(_bAsHeaderSourceCode);
459 break;
460 case TypeClass.UNSIGNED_HYPER_value:
461 sTypeString = m_xLanguageSourceCodeGenerator.getunsignedhyperTypeDescription();
462 break;
463 case TypeClass.UNSIGNED_LONG_value:
464 sTypeString = m_xLanguageSourceCodeGenerator.getunsignedlongTypeDescription();
465 break;
466 case TypeClass.UNSIGNED_SHORT_value:
467 sTypeString = m_xLanguageSourceCodeGenerator.getdoubleTypeDescription();
468 break;
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);
475 break;
476 case TypeClass.ANY_value:
477 sTypeString = m_xLanguageSourceCodeGenerator.getanyTypeDescription(_bAsHeaderSourceCode);
478 break;
479 case TypeClass.TYPE_value:
480 sTypeString = m_xLanguageSourceCodeGenerator.getObjectTypeDescription("com.sun.star.uno.Type", _bAsHeaderSourceCode);
481 break;
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);
489 break;
490 default:
492 return sTypeString;
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());
503 else{
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);
509 if (bIsArray){
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;
527 else{
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);
539 else{
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();
583 switch(nTypeClass){
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());
590 break;
591 case TypeClass.TYPE_value:
592 sCentralVariableStemName = SVARIABLENAME;
593 break;
594 case TypeClass.STRUCT_value:
595 sCentralVariableStemName = Introspector.getShortClassName(getTypeName());
596 break;
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);
603 default:
604 sCentralVariableStemName = SVARIABLENAME;
606 return sCentralVariableStemName;
609 /** may return null
611 public Object getUnoObject(){
612 return m_oUnoObject;
616 public TypeClass getTypeClass(){
617 return m_aTypeClass;
621 public String getTypeName(){
622 return m_sTypeName;
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();
654 switch(nTypeClass){
655 case TypeClass.BOOLEAN_value:
656 sVariableStemName = "b" + m_sCentralVariableStemName;
657 break;
658 case TypeClass.DOUBLE_value:
659 case TypeClass.FLOAT_value:
660 sVariableStemName = "f" + m_sCentralVariableStemName;
661 break;
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;
670 break;
671 case TypeClass.CHAR_value:
672 case TypeClass.STRING_value:
673 sVariableStemName = "s" + m_sCentralVariableStemName;
674 break;
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());
681 break;
682 case TypeClass.TYPE_value:
683 sVariableStemName = "a" + m_sCentralVariableStemName;
684 break;
685 case TypeClass.ANY_value:
686 sVariableStemName = "o" + m_sCentralVariableStemName;
687 break;
688 case TypeClass.STRUCT_value:
689 case TypeClass.ENUM_value:
690 sVariableStemName = "a" + m_sCentralVariableStemName;
691 break;
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);
698 default:
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("")){
719 int a = 2;
720 sVariableName = getVariableStemName();
721 boolean bleaveloop = false;
722 while (!bleaveloop){
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)))){
728 bleaveloop = true;
730 else{
731 sVariableName = getVariableStemName() + SSUFFIXSEPARATOR + a++;
734 else{
735 bleaveloop = true;
738 else{
739 bleaveloop = true;
743 return sVariableName;
748 public String getStringValueOfObject(Object _oUnoObject, TypeClass _aTypeClass){
749 String sReturn = "";
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:
755 break;
756 case TypeClass.DOUBLE_value:
757 double fdoubleValue = ((Double) _oUnoObject).doubleValue();
758 sReturn += Double.toString(fdoubleValue);
759 break;
760 case TypeClass.ENUM_value:
761 break;
762 case TypeClass.FLOAT_value:
763 float floatValue = ((Float) _oUnoObject).floatValue();
764 sReturn += Float.toString(floatValue);
765 break;
766 case TypeClass.HYPER_value:
767 long nlongValue = ((Long) _oUnoObject).longValue();
768 sReturn += Long.toString(nlongValue);
769 break;
770 case TypeClass.LONG_value:
771 int nintValue = ((Integer) _oUnoObject).intValue();
772 sReturn += Integer.toString(nintValue);
773 break;
774 case TypeClass.SHORT_value:
775 short nshortValue = ((Short) _oUnoObject).shortValue();
776 sReturn += Short.toString(nshortValue);
777 break;
778 case TypeClass.STRING_value:
779 sReturn += (String) _oUnoObject;
780 break;
781 case TypeClass.UNSIGNED_HYPER_value:
782 nlongValue = ((Long) _oUnoObject).longValue();
783 sReturn += Long.toString(nlongValue);
784 break;
785 case TypeClass.UNSIGNED_LONG_value:
786 nintValue = ((Integer) _oUnoObject).intValue();
787 sReturn += Integer.toString(nintValue);
788 break;
789 case TypeClass.UNSIGNED_SHORT_value:
790 nshortValue = ((Short) _oUnoObject).shortValue();
791 sReturn += Short.toString(nshortValue);
792 break;
793 default:
794 System.out.println("Type " + _aTypeClass.getValue() + " not yet defined in 'getStringValueOfObject()'");
796 return sReturn;
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(){
832 return ";";
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...
848 else{
849 sClassName = "";
853 else{
854 sClassName = getTypeString(_sClassName, _aTypeClass, true);
857 else if (_aTypeClass.getValue() == TypeClass.ENUM_value){
858 sClassName = _sClassName;
860 else{
861 sClassName = "";
863 if (!sClassName.equals("")){
864 sHeaderStatement = "import " + sClassName + ";\n";
867 return sHeaderStatement;
871 public String getFinalHeaderStatements(){
872 return "";
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;
888 String sReturn = "";
889 switch (_aTypeClass.getValue()){
890 case TypeClass.BOOLEAN_value:
891 sReturn = _sReturnVariableName + " = AnyConverter.toBoolean(" + _sObjectDescription + ")";
892 break;
893 case TypeClass.CHAR_value:
894 sReturn = _sReturnVariableName + " = AnyConverter.toChar(" + _sObjectDescription + ")";
895 break;
896 case TypeClass.BYTE_value:
897 sReturn = _sReturnVariableName + " = AnyConverter.toByte(" + _sObjectDescription + ")";
898 break;
899 case TypeClass.DOUBLE_value:
900 sReturn = _sReturnVariableName + " = AnyConverter.toDouble(" + _sObjectDescription + ")";
901 break;
902 case TypeClass.FLOAT_value:
903 sReturn = _sReturnVariableName + " = AnyConverter.toFloat(" + _sObjectDescription + ")";
904 break;
905 case TypeClass.UNSIGNED_HYPER_value:
906 sReturn = _sReturnVariableName + " = AnyConverter.toUnsignedLong(" + _sObjectDescription + ")";
907 break;
908 case TypeClass.HYPER_value:
909 sReturn = _sReturnVariableName + " = AnyConverter.toLong(" + _sObjectDescription + ")";
910 break;
911 case TypeClass.UNSIGNED_LONG_value:
912 sReturn = _sReturnVariableName + " = AnyConverter.toUnsignedInt(" + _sObjectDescription + ")";
913 break;
914 case TypeClass.LONG_value:
915 sReturn = _sReturnVariableName + " = AnyConverter.toInt(" + _sObjectDescription + ")";
916 break;
917 case TypeClass.SHORT_value:
918 sReturn = _sReturnVariableName + " = AnyConverter.toShort(" + _sObjectDescription + ")";
919 break;
920 case TypeClass.UNSIGNED_SHORT_value:
921 sReturn = _sReturnVariableName + " = AnyConverter.toUnsignedShort(" + _sObjectDescription + ")";
922 break;
923 case TypeClass.STRING_value:
924 sReturn = _sReturnVariableName + " = AnyConverter.toString(" + _sObjectDescription + ")";
925 break;
926 default:
927 String sShortTypeName = Introspector.getShortClassName(_sTypeName);
928 if (bIsPropertyUnoObjectDefined){
929 sReturn = "oUnoObject = " + _sObjectDescription + ";\n\t";
931 else{
932 sReturn = "Object oUnoObject = " + _sObjectDescription + ";\n\t";
933 bIsPropertyUnoObjectDefined = true;
935 sReturn += _sReturnVariableName + " = (" + sShortTypeName + ") AnyConverter.toObject(" + sShortTypeName + ".class, oUnoObject);";
936 break;
938 if (!bAddAnyConverter){
939 bAddAnyConverter = bLocAddAnyConverter;
941 return sReturn;
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){
953 sReturn += "\ntry{";
955 return sReturn;
958 public String getMethodSeparator(){
959 return ".";
962 public boolean needsqueryInterface(){
963 return true;
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);
975 sReturn += ";";
976 sReturn = "\t" + sReturn;
977 return sReturn;
981 public String getObjectTypeDescription(String _sClassName, boolean _bAsHeader){
982 String sReturn = "";
983 if (_bAsHeader){
984 sReturn = _sClassName;
986 else{
987 sReturn = Introspector.getShortClassName(_sClassName);
989 return sReturn;
993 public String getMethodTerminationSourceCode(){
994 String sReturn = "";
995 int nIndex = 1;
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){
1001 if (nIndex > 1){
1002 sExceptionName = "e"+ nIndex;
1004 else{
1005 sReturn +="\n}";
1007 sReturn += "catch (" + sCurException + " " + sExceptionName + "){\n";
1008 sReturn += "\t" + sExceptionName + ".printStackTrace(System.err);\n";
1009 sReturn += "\t" + getCommentSign() + "Enter your Code here...\n}";
1010 nIndex++;
1014 sReturn += "\n}";
1015 return sReturn;
1018 public String castLiteral(String _sExpression, TypeClass _aTypeClass){
1019 String sReturn = "";
1020 switch (_aTypeClass.getValue()){
1021 case TypeClass.BOOLEAN_value:
1022 sReturn = _sExpression;
1023 break;
1024 case TypeClass.BYTE_value:
1025 sReturn = "(byte) " + _sExpression;
1026 break;
1027 case TypeClass.CHAR_value:
1028 sReturn = "'" + _sExpression + "'";
1029 break;
1030 case TypeClass.DOUBLE_value:
1031 sReturn = "(double) " + _sExpression;
1032 break;
1033 case TypeClass.FLOAT_value:
1034 sReturn = "(float) " + _sExpression;
1035 break;
1036 case TypeClass.UNSIGNED_SHORT_value:
1037 case TypeClass.SHORT_value:
1038 sReturn = "(short) " + _sExpression;
1039 break;
1040 case TypeClass.STRING_value:
1041 sReturn = "\"" + _sExpression + "\"";
1042 break;
1043 case TypeClass.HYPER_value:
1044 case TypeClass.UNSIGNED_HYPER_value:
1045 sReturn = "(long) " + _sExpression;
1046 break;
1047 case TypeClass.LONG_value:
1048 sReturn = _sExpression;
1049 break;
1050 case TypeClass.ENUM_value:
1051 default:
1052 sReturn = _sExpression;
1053 System.out.println("Type " + _aTypeClass.getValue() + " not yet defined in 'castliteral()'");
1055 return sReturn;
1059 public String getbooleanTypeDescription(){
1060 return "boolean";
1063 public String getbyteTypeDescription(){
1064 return "byte";
1067 public String getshortTypeDescription(){
1068 return "short";
1071 public String getlongTypeDescription(){
1072 return "int";
1075 public String getunsignedlongTypeDescription(){
1076 return "int";
1079 public String gethyperTypeDescription(){
1080 return "long";
1083 public String getunsignedhyperTypeDescription(){
1084 return "long";
1087 public String getfloatTypeDescription(){
1088 return "float";
1091 public String getdoubleTypeDescription(){
1092 return "double";
1095 public String getcharTypeDescription(){
1096 return "char";
1099 public String getstringTypeDescription(boolean _bAsHeaderSourceCode){
1100 if (_bAsHeaderSourceCode){
1101 return "";
1103 else{
1104 return "String";
1108 public String getanyTypeDescription(boolean _bAsHeaderSourceCode){
1109 if (_bAsHeaderSourceCode){
1110 return "";
1112 else{
1113 return "Object";
1118 public String getStringValue(String _sValue){
1119 return _sValue;
1123 public String getVariableDeclaration(String _sTypeString, String _sVariableName, boolean _bIsArray, TypeClass _aTypeClass, boolean _bInitialize){
1124 String sReturn = "";
1125 if (_bIsArray){
1126 sReturn = _sTypeString + "[] " + _sVariableName;
1128 else{
1129 sReturn = _sTypeString + " " + _sVariableName;
1131 return sReturn;
1135 public String getCommentSign(){
1136 return "//";
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(){
1153 return "";
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(){
1162 return false;
1165 public void assignqueryInterfaceHeaderSourceCode(){
1168 public String getMethodSeparator(){
1169 return ".";
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){
1193 return "Object";
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;
1216 break;
1217 case TypeClass.CHAR_value:
1218 case TypeClass.STRING_value:
1219 sReturn = "\"" +_sExpression + "\"";
1220 break;
1221 case TypeClass.ENUM_value:
1222 default:
1223 sReturn = _sExpression;
1224 System.out.println("Type " + _aTypeClass.getValue() + " not yet defined in 'castliteral()'");
1226 return sReturn;
1231 public String getbooleanTypeDescription(){
1232 return "Boolean";
1235 public String getbyteTypeDescription(){
1236 return "Integer";
1239 public String getshortTypeDescription(){
1240 return "Integer";
1243 public String getlongTypeDescription(){
1244 return "Integer";
1247 public String getunsignedlongTypeDescription(){
1248 return "Long";
1251 public String gethyperTypeDescription(){
1252 return "Long";
1255 public String getunsignedhyperTypeDescription(){
1256 return "Long";
1259 public String getfloatTypeDescription(){
1260 return "Double";
1263 public String getdoubleTypeDescription(){
1264 return "Double";
1267 public String getcharTypeDescription(){
1268 return "String";
1271 public String getstringTypeDescription(boolean _bAsHeaderSourceCode){
1272 if (_bAsHeaderSourceCode){
1273 return "";
1275 else{
1276 return "String";
1280 public String getanyTypeDescription(boolean _bAsHeaderSourceCode){
1281 if (_bAsHeaderSourceCode){
1282 return "";
1284 else{
1285 return "Object";
1289 public String getStatementTerminationCharacter(){
1290 return "";
1294 public String getVariableDeclaration(String _sTypeString, String _sVariableName, boolean bIsArray, TypeClass _aTypeClass, boolean _bInitialize){
1295 String sReturn = "";
1296 if (bIsArray){
1297 sReturn = "Dim " + _sVariableName + "() as " + _sTypeString + "\n\t" + _sVariableName;
1299 else{
1300 sReturn = "Dim " + _sVariableName + " as " + _sTypeString + "\n\t" + _sVariableName;
1302 return sReturn;
1306 public String getStringValue(String _sValue){
1307 return _sValue;
1311 public String getCommentSign(){
1312 return "'";
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(){
1327 return "css";
1330 public String getStatementTerminationCharacter(){
1331 return ";";
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...
1347 else{
1348 sClassName = "";
1352 else{
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";
1369 return sReturn;
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"));
1390 if (bIncludeAny){
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 + " ){";
1399 int a = 0;
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){
1408 if (a++ > 0){
1409 sReturn += ", ";
1411 sReturn += getObjectTypeDescription(sCurException, false);
1417 sReturn += "{";
1418 return sReturn;
1422 public boolean needsqueryInterface(){
1423 return true;
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 = "";
1469 if (_bAsHeader){
1470 sReturn = _sClassName.replace('.', '/');
1472 else{
1473 String sModuleName = Introspector.getModuleName(_sClassName);
1474 sModuleName = Introspector.getShortClassName(sModuleName);
1475 sReturn = getCSSNameSpaceString() + "::" + sModuleName + "::" + Introspector.getShortClassName(_sClassName);
1477 return sReturn;
1481 public String getMethodTerminationSourceCode(){
1482 return "\n}";
1485 public String getMethodSeparator(){
1486 return "->";
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;
1504 break;
1505 case TypeClass.CHAR_value:
1506 sReturn = "'" + _sExpression + "'";
1507 break;
1508 case TypeClass.STRING_value:
1509 sReturn = getStringValue(_sExpression);
1510 break;
1511 case TypeClass.ENUM_value:
1512 default:
1513 sReturn = _sExpression;
1514 System.out.println("Type " + _aTypeClass.getValue() + " not yet defined in 'castliteral()'");
1516 return sReturn;
1519 public String getbooleanTypeDescription(){
1520 return "sal_Bool";
1523 public String getbyteTypeDescription(){
1524 return "sal_Int8";
1527 public String getshortTypeDescription(){
1528 return "sal_Int16";
1531 public String getlongTypeDescription(){
1532 return "sal_Int32";
1535 public String getunsignedlongTypeDescription(){
1536 return "sal_uInt32";
1539 public String gethyperTypeDescription(){
1540 return "sal_Int64";
1543 public String getunsignedhyperTypeDescription(){
1544 return "sal_uInt64";
1547 public String getfloatTypeDescription(){
1548 return "float";
1551 public String getdoubleTypeDescription(){
1552 return "double";
1555 public String getcharTypeDescription(){
1556 return "sal_Unicode";
1559 public String getstringTypeDescription(boolean _bAsHeaderSourceCode){
1560 bIncludeStringHeader = true;
1561 if (_bAsHeaderSourceCode){
1562 return "";
1564 else{
1565 return "OUString";
1569 public String getanyTypeDescription(boolean _bAsHeaderSourceCode){
1570 if (_bAsHeaderSourceCode){
1571 return "com/sun/star/uno/XInterface";
1573 else{
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 = "";
1583 if (bIsArray){
1584 bIncludeSequenceHeader = true;
1585 sReturn = getCSSNameSpaceString() + "::uno::Sequence<" + _sTypeString + "> " + _sVariableName;
1587 else{
1588 if (bIsPrimitive){
1589 sReturn = _sTypeString + " " + _sVariableName;
1590 if (_bInitialize){
1591 switch (_aTypeClass.getValue()){
1592 case TypeClass.BOOLEAN_value:
1593 sReturn = sReturn + " = false";
1594 break;
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";
1603 break;
1604 case TypeClass.DOUBLE_value:
1605 case TypeClass.FLOAT_value:
1606 sReturn = sReturn + " = 0.0";
1607 break;
1608 case TypeClass.CHAR_value:
1609 sReturn = sReturn + "'0'";
1610 break;
1611 case TypeClass.STRING_value:
1612 sReturn = _sTypeString + " " + _sVariableName;
1613 break;
1614 default:
1615 sReturn = _sTypeString + " " + _sVariableName;
1616 System.out.println("Type " + _aTypeClass.getValue() + " not yet defined in 'getVariableDeclaration()'");
1620 else{
1621 sReturn = getCSSNameSpaceString() + "::uno::Reference<" + _sTypeString + "> " +_sVariableName;
1624 return sReturn;
1627 public String getCommentSign(){
1628 return "//";