merge the formfield patch from ooo-build
[ooovba.git] / odk / examples / java / Inspector / SourceCodeGenerator.java
blob0069891b2984a49cb1c458a06e61feb7171f6b66
1 /*************************************************************************
3 * $RCSfile: SourceCodeGenerator.java,v $
5 * $Revision: 1.4 $
7 * last change: $Author: rt $ $Date: 2007-04-04 09:20:37 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import com.sun.star.beans.UnknownPropertyException;
42 import com.sun.star.beans.XPropertySet;
43 import com.sun.star.container.XNameAccess;
44 import com.sun.star.lang.Locale;
45 import com.sun.star.lang.WrappedTargetException;
46 import com.sun.star.lang.XServiceInfo;
47 import com.sun.star.reflection.ParamInfo;
48 import com.sun.star.reflection.XIdlClass;
49 import com.sun.star.reflection.XIdlMethod;
50 import com.sun.star.reflection.XTypeDescription;
51 import com.sun.star.text.XTextTablesSupplier;
52 import com.sun.star.uno.Any;
53 import com.sun.star.uno.AnyConverter;
54 import com.sun.star.uno.Type;
55 import com.sun.star.uno.TypeClass;
56 import com.sun.star.uno.UnoRuntime;
57 import java.util.Enumeration;
58 import java.util.Hashtable;
59 import java.util.Vector;
63 public class SourceCodeGenerator {
64 private Vector sExceptions = new Vector();
65 Vector sHeaderStatements = new HeaderStatements();
66 private XLanguageSourceCodeGenerator m_xLanguageSourceCodeGenerator;
67 private String sHeaderCode = "";
68 private String sStatementCode = "";
69 private String sMainMethodSignature = "";
71 private Hashtable aVariables = new Hashtable();
72 private final String SSUFFIXSEPARATOR = "_";
73 private final String SVARIABLENAME = "VariableName";
74 private final String SARRAYVARIABLENAME = "VariableNameList";
75 private final String SUNOOBJECTNAME = "oUnobject";
76 private final String SUNOSTRUCTNAME = "aUnoStruct";
77 private Introspector m_oIntrospector;
78 private Vector aTreepathProviders = new Vector();
79 private XTreePathProvider xTreepathProvider = null;
80 private boolean baddExceptionHandling = false;
81 private boolean bXPropertySetExceptionsAreAdded = false;
82 private XUnoNode oInitialUnoNode = null;
83 private final String sINITIALVARIABLENAME = "_oUnoEntryObject";
86 /** Creates a new instance of SourceCodeGenerator */
87 public SourceCodeGenerator(int _nLanguage) {
88 this.setLanguage(_nLanguage);
89 m_oIntrospector = Introspector.getIntrospector();
93 public String addSourceCodeOfUnoObject(XTreePathProvider _xTreepathProvider, boolean _brememberPath, boolean _bAddMethodSignature, boolean _baddHeader){
94 String sSourceCode = "";
95 String sVariableName = "";
96 if (_xTreepathProvider != null) {
97 for (int i = 0; i < _xTreepathProvider.getPathCount(); i++){
98 XUnoNode oUnoNode = _xTreepathProvider.getPathComponent(i);
99 if (i == 0){
100 sVariableName = sINITIALVARIABLENAME;
101 oInitialUnoNode = oUnoNode;
103 else{
104 if (oUnoNode instanceof XUnoMethodNode){
105 XUnoMethodNode oUnoMethodNode = (XUnoMethodNode) oUnoNode;
106 if (oUnoMethodNode.isInvoked()){
107 UnoObjectDefinition oUnoReturnObjectDefinition = getUnoObjectDefinition(_xTreepathProvider, oUnoMethodNode, i);
108 if (!isVariableDeclared(oUnoReturnObjectDefinition, this.generateVariableNameFromMethod(oUnoMethodNode.getXIdlMethod()))){
109 // sStatementCode += "\n";
110 sStatementCode += "\n" + getMethodStatementSourceCode(oUnoMethodNode, sVariableName, oUnoReturnObjectDefinition);
112 sVariableName = oUnoReturnObjectDefinition.getVariableName();
115 else if (oUnoNode instanceof XUnoPropertyNode){
116 XUnoPropertyNode oUnoPropertyNode = (XUnoPropertyNode) oUnoNode;
117 Any oReturnObject = com.sun.star.uno.Any.complete(oUnoPropertyNode.getUnoReturnObject());
118 UnoObjectDefinition oUnoReturnObjectDefinition = new UnoObjectDefinition(oReturnObject);
119 if (!isVariableDeclared(oUnoReturnObjectDefinition, oUnoPropertyNode.getProperty().Name)){
120 // sStatementCode += "\n";
121 sStatementCode += "\n" + getPropertyStatementSourceCode(oUnoPropertyNode, sVariableName, oUnoReturnObjectDefinition);
123 sVariableName = oUnoReturnObjectDefinition.getVariableName();
128 String sCompleteCode = combineCompleteSourceCode(sMainMethodSignature, _baddHeader);
129 xTreepathProvider = _xTreepathProvider;
130 if (_brememberPath){
131 aTreepathProviders.add(_xTreepathProvider);
133 return sCompleteCode;
137 private void setLanguage(int _nLanguage){
138 XLanguageSourceCodeGenerator xLanguageSourceCodeGenerator = null;
139 switch(_nLanguage){
140 case XLanguageSourceCodeGenerator.nJAVA:
141 xLanguageSourceCodeGenerator = new JavaCodeGenerator();
142 break;
143 case XLanguageSourceCodeGenerator.nCPLUSPLUS:
144 xLanguageSourceCodeGenerator = new CPlusPlusCodeGenerator();
145 break;
146 case XLanguageSourceCodeGenerator.nBASIC:
147 xLanguageSourceCodeGenerator = new BasicCodeGenerator();
148 break;
149 default:
150 System.out.println("Unknown Sourcecode Language. Check Menus!");
152 if (xLanguageSourceCodeGenerator != null){
153 m_xLanguageSourceCodeGenerator = xLanguageSourceCodeGenerator;
157 private void resetSourceCodeGeneration(int _nLanguage){
158 aVariables.clear();
159 this.sHeaderStatements.clear();
160 setLanguage(_nLanguage);
161 String sHeaderCode = "";
162 sStatementCode = "";
165 private String generateVariableNameFromMethod(String _sMethodName, String _sPrefix, boolean _bConsiderAll){
166 String sReturn = "";
167 if (_sMethodName.startsWith(_sPrefix)){
168 int nPrefixLength = _sPrefix.length();
169 if (_sMethodName.length() > nPrefixLength){
170 String sChar = _sMethodName.substring(nPrefixLength, nPrefixLength + 1);
171 String sUpperChar = sChar.toUpperCase();
172 if (sUpperChar.equals(sChar)){
173 if (_bConsiderAll){
174 sReturn = _sMethodName;
176 else{
177 sReturn = _sMethodName.substring(nPrefixLength, _sMethodName.length());
182 return sReturn;
186 private String generateVariableNameFromMethod(XIdlMethod _xIdlMethod){
187 // todo: refactor this!!!
188 String sMethodName = _xIdlMethod.getName();
189 String sReturn = "";
190 sReturn = generateVariableNameFromMethod(sMethodName, "getBy", false);
191 if (sReturn.equals("")){
192 sReturn = generateVariableNameFromMethod(sMethodName, "get", false);
194 if (sReturn.equals("")){
195 sReturn = generateVariableNameFromMethod(sMethodName, "attach", false);
197 if (sReturn.equals("")){
198 sReturn = generateVariableNameFromMethod(sMethodName, "assign", false);
200 if (sReturn.equals("")){
201 sReturn = generateVariableNameFromMethod(sMethodName, "attach", false);
203 if (sReturn.equals("")){
204 sReturn = generateVariableNameFromMethod(sMethodName, "create", false);
206 if (sReturn.equals("")){
207 sReturn = generateVariableNameFromMethod(sMethodName, "is", true);
209 if (sReturn.equals("")){
210 sReturn = generateVariableNameFromMethod(sMethodName, "has", true);
212 if (sReturn.equals("")){
213 sReturn = sMethodName;
215 return sReturn;
219 public String convertAllUnoObjects(int _nLanguage){
220 String sSourceCode = "";
221 resetSourceCodeGeneration(_nLanguage);
222 int ncount = aTreepathProviders.size();
223 for (int i=0; i< ncount; i++){
224 sSourceCode = addSourceCodeOfUnoObject((XTreePathProvider) aTreepathProviders.get(i), false, (i==0), (i == (ncount-1)));
226 return sSourceCode;
229 private UnoObjectDefinition getUnoObjectDefinition(XTreePathProvider _xTreePathProvider, XUnoMethodNode _oUnoMethodNode, int _nindex){
230 XUnoNode oUnoNode = null;
231 Object oUnoReturnObject = null;
232 Object[] oParamObjects = null;
233 XIdlClass xIdlClass = _oUnoMethodNode.getXIdlMethod().getReturnType();
234 String sTypeName = xIdlClass.getName();
235 TypeClass aTypeClass = xIdlClass.getTypeClass();
236 if (aTypeClass.getValue() != TypeClass.VOID_value){
237 if (_xTreePathProvider.getPathCount() > _nindex + 1){
238 oUnoNode = _xTreePathProvider.getPathComponent(_nindex + 1);
239 oUnoReturnObject = oUnoNode.getUnoObject();
242 if (oUnoReturnObject == null){
243 oUnoReturnObject = _oUnoMethodNode.getLastUnoReturnObject();
245 UnoObjectDefinition oUnoObjectDefinition = new UnoObjectDefinition(oUnoReturnObject, sTypeName, aTypeClass);
246 if (_oUnoMethodNode.hasParameters()){
247 if (oUnoNode != null){
248 oParamObjects = oUnoNode.getParameterObjects();
250 else{
251 oParamObjects = _oUnoMethodNode.getLastParameterObjects();
254 if (oParamObjects != null){
255 oUnoObjectDefinition.addParameterObjects(oParamObjects);
257 return oUnoObjectDefinition;
261 private String combineCompleteSourceCode(String _sMethodSignature, boolean _bAddHeader){
262 String sCompleteCode = "";
263 if (_bAddHeader){
264 sMainMethodSignature = m_xLanguageSourceCodeGenerator.getMainMethodSignatureSourceCode(oInitialUnoNode, sINITIALVARIABLENAME);
265 m_xLanguageSourceCodeGenerator.assignqueryInterfaceHeaderSourceCode();
266 sCompleteCode += getHeaderSourceCode();
268 sCompleteCode += sMainMethodSignature;
269 sCompleteCode += sStatementCode;
270 if (_bAddHeader){
271 sCompleteCode += m_xLanguageSourceCodeGenerator.getMethodTerminationSourceCode();
272 sCompleteCode += "\n" + m_xLanguageSourceCodeGenerator.getCommentSign() + "...";
274 return sCompleteCode;
278 public String getPropertyStatementSourceCode(XUnoPropertyNode _oUnoPropertyNode, String _sVariableName, UnoObjectDefinition _oUnoReturnObjectDefinition){
279 String sReturnObjectVariableDefinition = "";
280 String sStatement = "";
281 String sPropertyName = _oUnoPropertyNode.getProperty().Name;
282 UnoObjectDefinition oUnoObjectDefinition = new UnoObjectDefinition(_oUnoPropertyNode.getUnoObject(), "com.sun.star.beans.XPropertySet");
283 if (!m_xLanguageSourceCodeGenerator.needsqueryInterface() || (oUnoObjectDefinition.getTypeClass().getValue() == TypeClass.STRUCT_value)){
284 oUnoObjectDefinition.setVariableName(_sVariableName);
285 if (oUnoObjectDefinition.getTypeClass().getValue() == TypeClass.STRUCT_value){
286 sReturnObjectVariableDefinition = getVariableInitialization(_oUnoReturnObjectDefinition, false);
287 sStatement += m_xLanguageSourceCodeGenerator.getStructSourceCode(sReturnObjectVariableDefinition, oUnoObjectDefinition.getVariableName(), sPropertyName);
288 return sStatement;
291 sStatement += addQueryInterfaceSourceCode(oUnoObjectDefinition, _sVariableName, "com.sun.star.beans.XPropertySet");
292 if (_oUnoReturnObjectDefinition.getTypeClass().getValue() != TypeClass.VOID_value){
293 sReturnObjectVariableDefinition = getVariableInitialization(_oUnoReturnObjectDefinition, true);
295 sStatement += m_xLanguageSourceCodeGenerator.getPropertyValueGetterSourceCode(sPropertyName, sReturnObjectVariableDefinition, oUnoObjectDefinition.getVariableName(), _oUnoReturnObjectDefinition.getTypeClass(), _oUnoReturnObjectDefinition.getTypeName());
296 addXPropertySetRelatedExceptions();
297 return sStatement;
301 public String getMethodStatementSourceCode(XUnoMethodNode _oUnoMethodNode, String _sVariableName, UnoObjectDefinition _oUnoReturnObjectDefinition){
302 String sReturnObjectVariableDefinition = "";
303 String sStatement = "";
304 XIdlMethod xIdlMethod = _oUnoMethodNode.getXIdlMethod();
305 TypeClass aReturnTypeClass = xIdlMethod.getReturnType().getTypeClass();
306 UnoObjectDefinition oUnoObjectDefinition = new UnoObjectDefinition(_oUnoMethodNode.getUnoObject(), _oUnoMethodNode.getClassName());
307 String sVariableStemName = this.generateVariableNameFromMethod(xIdlMethod);
308 sStatement += addQueryInterfaceSourceCode(oUnoObjectDefinition, _sVariableName, oUnoObjectDefinition.getTypeName());
309 if (_oUnoReturnObjectDefinition.getTypeClass().getValue() != TypeClass.VOID_value){
310 sReturnObjectVariableDefinition = getVariableInitialization(_oUnoReturnObjectDefinition, false) + " = ";
312 Object[] oParamObjects = _oUnoReturnObjectDefinition.getParameterObjects();
313 String sParameterCode = getMethodParameterValueDescription(_oUnoMethodNode, oParamObjects, false);
314 String sSeparator = m_xLanguageSourceCodeGenerator.getMethodSeparator();
315 sStatement += "\t" + sReturnObjectVariableDefinition + oUnoObjectDefinition.getVariableName() + sSeparator + xIdlMethod.getName() + "(" + sParameterCode + ")";
316 sStatement += m_xLanguageSourceCodeGenerator.getStatementTerminationCharacter();
317 addExceptions(xIdlMethod);
318 return sStatement;
322 private String addQueryInterfaceSourceCode(UnoObjectDefinition _oUnoObjectDefinition, String _sVariableName, String _sTypeName){
323 String sLocStatement = "";
324 if (m_xLanguageSourceCodeGenerator.needsqueryInterface()){
325 if (!isVariableDeclared(_oUnoObjectDefinition, "")){
326 String sObjectVariableDefinition = getVariableDeclaration(_oUnoObjectDefinition, false, "");
327 sLocStatement += m_xLanguageSourceCodeGenerator.getqueryInterfaceSourceCode(_sTypeName, sObjectVariableDefinition, _sVariableName);
330 else{
331 _oUnoObjectDefinition.setVariableName(_sVariableName);
333 return sLocStatement;
337 private void addXPropertySetRelatedExceptions(){
338 if (!bXPropertySetExceptionsAreAdded){
339 sExceptions.add("com.sun.star.beans.UnknownPropertyException");
340 sExceptions.add("com.sun.star.lang.WrappedTargetException");
341 sExceptions.add("com.sun.star.lang.IllegalArgumentException");
342 bXPropertySetExceptionsAreAdded = true;
343 baddExceptionHandling = true;
348 private void addExceptions(XIdlMethod _xIdlMethod){
349 XIdlClass[] xIdlClasses = _xIdlMethod.getExceptionTypes();
350 for (int i = 0; i > xIdlClasses.length; i++){
351 sExceptions.add(xIdlClasses[0].getName());
352 baddExceptionHandling = true;
356 private String getRootDescription(XUnoNode _oUnoNode){
357 return "_o" + _oUnoNode.toString();
361 private String getHeaderSourceCode(){
362 Enumeration aEnumeration = aVariables.elements();
363 while(aEnumeration.hasMoreElements()){
364 UnoObjectDefinition oUnoObjectDefinition = (UnoObjectDefinition) aEnumeration.nextElement();
365 String sCurHeaderStatement = m_xLanguageSourceCodeGenerator.getHeaderSourceCode(oUnoObjectDefinition.getUnoObject(), oUnoObjectDefinition.getTypeName(), oUnoObjectDefinition.getTypeClass());
366 sHeaderStatements.add(sCurHeaderStatement);
368 String sHeaderSourcecode = "";
369 String[] sHeaderStatementArray = new String[sHeaderStatements.size()];
370 sHeaderStatements.toArray(sHeaderStatementArray);
371 java.util.Arrays.sort(sHeaderStatementArray);
372 for (int i = 0; i < sHeaderStatementArray.length; i++){
373 sHeaderSourcecode += sHeaderStatementArray[i];
375 sHeaderSourcecode += m_xLanguageSourceCodeGenerator.getFinalHeaderStatements();
376 return sHeaderSourcecode +"\n" + m_xLanguageSourceCodeGenerator.getCommentSign() + "...\n";
380 private class HeaderStatements extends Vector{
382 public boolean contains(Object _oElement){
383 String sCompName = (String) _oElement;
384 for (int i = 0; i < this.size(); i++){
385 String sElement = (String) this.get(i);
386 if (sElement.equals(sCompName)){
387 return true;
390 return false;
394 public boolean add(Object _oElement){
395 if (_oElement instanceof String){
396 if (!contains(_oElement)){
397 super.add(_oElement);
398 return true;
401 return false;
406 private boolean isVariableDeclared(UnoObjectDefinition _oUnoObjectDefinition, String _sDefaultStemName){
407 boolean bisDeclared = false;
408 if (!_sDefaultStemName.equals("")){
409 _oUnoObjectDefinition.setCentralVariableStemName(_sDefaultStemName);
411 String sVariableStemName = _oUnoObjectDefinition.getVariableStemName();
412 bisDeclared = aVariables.containsKey(sVariableStemName);
413 if (bisDeclared){
414 Object oUnoObject = _oUnoObjectDefinition.getUnoObject();
415 if (m_oIntrospector.isObjectPrimitive(oUnoObject)){
416 bisDeclared = false;
418 else if (m_oIntrospector.isObjectSequence(oUnoObject)){
419 bisDeclared = false;
421 else{
422 String sCompVariableName = sVariableStemName;
423 String sUnoObjectIdentity = oUnoObject.toString();
424 boolean bleaveloop = false;
425 int a = 2;
426 while (!bleaveloop){
427 if (aVariables.containsKey(sCompVariableName)){
428 Object oUnoCompObject = ((UnoObjectDefinition) aVariables.get(sCompVariableName)).getUnoObject();
429 String sUnoCompObjectIdentity = oUnoCompObject.toString();
430 bleaveloop = sUnoCompObjectIdentity.equals(sUnoObjectIdentity);
431 bisDeclared = bleaveloop;
432 if (!bleaveloop){
433 sCompVariableName = sVariableStemName + SSUFFIXSEPARATOR + a++;
436 else{
437 bleaveloop = true;
438 bisDeclared = false;
443 return bisDeclared;
447 private String addUniqueVariableName(String _sStemVariableName, UnoObjectDefinition _oUnoObjectDefinition){
448 boolean bElementexists = true;
449 int a = 2;
450 String sCompName = _sStemVariableName;
451 while (bElementexists){
452 if (! aVariables.containsKey(sCompName)){
453 aVariables.put(sCompName, _oUnoObjectDefinition);
454 break;
456 sCompName = _sStemVariableName + SSUFFIXSEPARATOR + a++;
458 return sCompName;
463 private String getTypeString(String _sTypeName, TypeClass _aTypeClass, boolean _bAsHeaderSourceCode){
464 String sTypeString = "";
465 switch (_aTypeClass.getValue()){
466 case TypeClass.BOOLEAN_value:
467 sTypeString = m_xLanguageSourceCodeGenerator.getbooleanTypeDescription();
468 break;
469 case TypeClass.BYTE_value:
470 sTypeString = m_xLanguageSourceCodeGenerator.getbyteTypeDescription();
471 break;
472 case TypeClass.CHAR_value:
473 sTypeString = m_xLanguageSourceCodeGenerator.getcharTypeDescription();
474 break;
475 case TypeClass.DOUBLE_value:
476 sTypeString = m_xLanguageSourceCodeGenerator.getdoubleTypeDescription();
477 break;
478 case TypeClass.FLOAT_value:
479 sTypeString = m_xLanguageSourceCodeGenerator.getfloatTypeDescription();
480 break;
481 case TypeClass.HYPER_value:
482 sTypeString = m_xLanguageSourceCodeGenerator.gethyperTypeDescription();
483 break;
484 case TypeClass.LONG_value:
485 sTypeString = m_xLanguageSourceCodeGenerator.getlongTypeDescription();
486 break;
487 case TypeClass.SHORT_value:
488 sTypeString = m_xLanguageSourceCodeGenerator.getshortTypeDescription();
489 break;
490 case TypeClass.STRING_value:
491 sTypeString = m_xLanguageSourceCodeGenerator.getstringTypeDescription(_bAsHeaderSourceCode);
492 break;
493 case TypeClass.UNSIGNED_HYPER_value:
494 sTypeString = m_xLanguageSourceCodeGenerator.getunsignedhyperTypeDescription();
495 break;
496 case TypeClass.UNSIGNED_LONG_value:
497 sTypeString = m_xLanguageSourceCodeGenerator.getunsignedlongTypeDescription();
498 break;
499 case TypeClass.UNSIGNED_SHORT_value:
500 sTypeString = m_xLanguageSourceCodeGenerator.getdoubleTypeDescription();
501 break;
502 case TypeClass.SEQUENCE_value:
503 //TODO consider mulitdimensional Arrays
504 XTypeDescription xTypeDescription = Introspector.getIntrospector().getReferencedType(_sTypeName);
505 if (xTypeDescription != null){
506 sTypeString = getTypeString(xTypeDescription.getName(), xTypeDescription.getTypeClass(), _bAsHeaderSourceCode);
508 break;
509 case TypeClass.ANY_value:
510 sTypeString = m_xLanguageSourceCodeGenerator.getanyTypeDescription(_bAsHeaderSourceCode);
511 break;
512 case TypeClass.TYPE_value:
513 sTypeString = m_xLanguageSourceCodeGenerator.getObjectTypeDescription("com.sun.star.uno.Type", _bAsHeaderSourceCode);
514 break;
515 case TypeClass.ENUM_value:
516 case TypeClass.STRUCT_value:
517 case TypeClass.INTERFACE_ATTRIBUTE_value:
518 case TypeClass.INTERFACE_METHOD_value:
519 case TypeClass.INTERFACE_value:
520 case TypeClass.PROPERTY_value:
521 sTypeString = m_xLanguageSourceCodeGenerator.getObjectTypeDescription(_sTypeName, _bAsHeaderSourceCode);
522 break;
523 default:
525 return sTypeString;
529 private String getVariableDeclaration(UnoObjectDefinition _oUnoObjectDefinition, boolean _bInitialize, String _sVariableDefaultName){
530 TypeClass aTypeClass = _oUnoObjectDefinition.getTypeClass();
531 TypeClass aLocTypeClass = aTypeClass;
532 boolean bIsArray = false;
533 if (_oUnoObjectDefinition.getUnoObject() != null){
534 bIsArray = m_oIntrospector.isObjectSequence(_oUnoObjectDefinition.getUnoObject());
536 else{
537 bIsArray = _oUnoObjectDefinition.getTypeClass().getValue() == TypeClass.SEQUENCE_value;
539 String sVariableName = _oUnoObjectDefinition.getVariableName(_sVariableDefaultName);
540 String sTypeName = _oUnoObjectDefinition.getTypeName();
541 String sTypeString = getTypeString(sTypeName, aLocTypeClass, false);
542 if (bIsArray){
543 XTypeDescription xTypeDescription = Introspector.getIntrospector().getReferencedType(sTypeName);
544 if (xTypeDescription != null){
545 aLocTypeClass = xTypeDescription.getTypeClass();
548 String sVariableDeclaration = m_xLanguageSourceCodeGenerator.getVariableDeclaration(sTypeString, sVariableName, bIsArray, aLocTypeClass, _bInitialize);
549 addUniqueVariableName(sVariableName, _oUnoObjectDefinition);
550 return sVariableDeclaration;
554 public String getVariableInitialization(UnoObjectDefinition _oUnoObjectDefinition, boolean _bInitialize){
555 String sObjectVariableDeclaration = "";
556 String sVariableName = _oUnoObjectDefinition.getVariableName();
557 if (isVariableDeclared(_oUnoObjectDefinition, "")){
558 sObjectVariableDeclaration = sVariableName;
560 else{
561 sObjectVariableDeclaration = getVariableDeclaration(_oUnoObjectDefinition, _bInitialize, "");
563 return sObjectVariableDeclaration;
568 public String getVariableNameforUnoObject(String _sShortClassName){
569 if (_sShortClassName.startsWith("X")){
570 return "x" + _sShortClassName.substring(1);
572 else{
573 return _sShortClassName;
578 class UnoObjectDefinition{
579 Object m_oUnoObject = null;
580 Type aType = null;
581 String sVariableStemName = "";
582 String m_sCentralVariableStemName = "";
583 String sVariableName = "";
584 String m_sTypeName = "";
585 TypeClass m_aTypeClass = null;
586 Object[] m_oParameterObjects = null;
589 public UnoObjectDefinition(Any _oUnoObject){
590 m_sTypeName = _oUnoObject.getType().getTypeName();
591 m_aTypeClass = _oUnoObject.getType().getTypeClass();
592 m_oUnoObject = _oUnoObject;
593 m_sCentralVariableStemName = getCentralVariableStemName(m_aTypeClass);
597 public UnoObjectDefinition(Object _oUnoObject, String _sTypeName, TypeClass _aTypeClass){
598 m_oUnoObject = _oUnoObject;
599 m_sTypeName = _sTypeName;
600 m_aTypeClass = _aTypeClass;
601 m_sCentralVariableStemName = getCentralVariableStemName(m_aTypeClass);
605 public UnoObjectDefinition(Object _oUnoObject, String _sTypeName){
606 m_oUnoObject = _oUnoObject;
607 m_sTypeName = _sTypeName;
608 m_aTypeClass = AnyConverter.getType(_oUnoObject).getTypeClass();
609 m_sCentralVariableStemName = getCentralVariableStemName(m_aTypeClass);
613 private String getCentralVariableStemName(TypeClass _aTypeClass){
614 String sCentralVariableStemName = "";
615 int nTypeClass = _aTypeClass.getValue();
616 switch(nTypeClass){
617 case TypeClass.SEQUENCE_value:
618 //TODO consider mulitdimensional Arrays
619 XTypeDescription xTypeDescription = Introspector.getIntrospector().getReferencedType(getTypeName());
620 if (xTypeDescription != null){
621 sCentralVariableStemName = getCentralVariableStemName(xTypeDescription.getTypeClass());
623 break;
624 case TypeClass.TYPE_value:
625 sCentralVariableStemName = SVARIABLENAME;
626 break;
627 case TypeClass.STRUCT_value:
628 sCentralVariableStemName = Introspector.getShortClassName(getTypeName());
629 break;
630 case TypeClass.INTERFACE_ATTRIBUTE_value:
631 case TypeClass.INTERFACE_METHOD_value:
632 case TypeClass.INTERFACE_value:
633 case TypeClass.PROPERTY_value:
634 String sShortClassName = m_oIntrospector.getShortClassName(getTypeName());
635 sCentralVariableStemName = getVariableNameforUnoObject(sShortClassName);
636 default:
637 sCentralVariableStemName = SVARIABLENAME;
639 return sCentralVariableStemName;
642 /** may return null
644 public Object getUnoObject(){
645 return m_oUnoObject;
649 public void setTypeClass(TypeClass _aTypeClass){
650 sVariableStemName = "";
651 m_aTypeClass = _aTypeClass;
655 public TypeClass getTypeClass(){
656 return m_aTypeClass;
660 public void setTypeName(String _sTypeName){
661 sVariableStemName = "";
662 m_sTypeName = _sTypeName;
666 public String getTypeName(){
667 return m_sTypeName;
671 public void setCentralVariableStemName(String _sCentralVariableStemName){
672 m_sCentralVariableStemName = _sCentralVariableStemName;
676 public String getVariableStemName(){
677 if (sVariableStemName.equals("")){
678 sVariableStemName = getVariableStemName(m_aTypeClass);
680 return sVariableStemName;
684 public void addParameterObjects(Object[] _oParameterObjects){
685 m_oParameterObjects = _oParameterObjects;
689 public Object[] getParameterObjects(){
690 return m_oParameterObjects;
694 public boolean hasParameterObjects(){
695 boolean breturn = false;
696 if (m_oParameterObjects != null){
697 breturn = m_oParameterObjects.length > 0;
699 return breturn;
703 public String getVariableStemName(TypeClass _aTypeClass){
704 int nTypeClass = _aTypeClass.getValue();
705 switch(nTypeClass){
706 case TypeClass.BOOLEAN_value:
707 sVariableStemName = "b" + m_sCentralVariableStemName;
708 break;
709 case TypeClass.DOUBLE_value:
710 case TypeClass.FLOAT_value:
711 sVariableStemName = "f" + m_sCentralVariableStemName;
712 break;
713 case TypeClass.BYTE_value:
714 case TypeClass.HYPER_value:
715 case TypeClass.LONG_value:
716 case TypeClass.UNSIGNED_HYPER_value:
717 case TypeClass.UNSIGNED_LONG_value:
718 case TypeClass.UNSIGNED_SHORT_value:
719 case TypeClass.SHORT_value:
720 sVariableStemName = "n" + m_sCentralVariableStemName;
721 break;
722 case TypeClass.CHAR_value:
723 case TypeClass.STRING_value:
724 sVariableStemName = "s" + m_sCentralVariableStemName;
725 break;
726 case TypeClass.SEQUENCE_value:
727 //TODO consider mulitdimensional Arrays
728 XTypeDescription xTypeDescription = Introspector.getIntrospector().getReferencedType(getTypeName());
729 if (xTypeDescription != null){
730 sVariableStemName = getVariableStemName(xTypeDescription.getTypeClass());
732 break;
733 case TypeClass.TYPE_value:
734 sVariableStemName = "a" + m_sCentralVariableStemName;
735 break;
736 case TypeClass.ANY_value:
737 sVariableStemName = "o" + m_sCentralVariableStemName;
738 break;
739 case TypeClass.STRUCT_value:
740 case TypeClass.ENUM_value:
741 sVariableStemName = "a" + m_sCentralVariableStemName;
742 break;
743 case TypeClass.INTERFACE_ATTRIBUTE_value:
744 case TypeClass.INTERFACE_METHOD_value:
745 case TypeClass.INTERFACE_value:
746 case TypeClass.PROPERTY_value:
747 String sShortClassName = m_oIntrospector.getShortClassName(getTypeName());
748 sVariableStemName = getVariableNameforUnoObject(sShortClassName);
749 default:
751 return sVariableStemName;
755 private void setVariableName(String _sVariableName){
756 sVariableName = _sVariableName;
760 private String getVariableName(String _sCentralVariableStemName){
761 if (!_sCentralVariableStemName.equals("")){
762 this.m_sCentralVariableStemName = _sCentralVariableStemName;
764 return getVariableName();
768 private String getVariableName() throws NullPointerException{
769 if (sVariableName.equals("")){
770 int a = 2;
771 sVariableName = getVariableStemName();
772 boolean bleaveloop = false;
773 while (!bleaveloop){
774 if (aVariables.containsKey(sVariableName)){
775 String sUnoObjectIdentity = ((UnoObjectDefinition) aVariables.get(sVariableName)).getUnoObject().toString();
776 if (m_oUnoObject != null){
777 if ((sUnoObjectIdentity.equals(m_oUnoObject.toString()) && (!m_oIntrospector.isPrimitive(this.getTypeClass())) &&
778 (! m_oIntrospector.isObjectSequence(m_oUnoObject)))){
779 bleaveloop = true;
781 else{
782 sVariableName = getVariableStemName() + SSUFFIXSEPARATOR + a++;
785 else{
786 bleaveloop = true;
789 else{
790 bleaveloop = true;
791 // throw new NullPointerException("SourceCode Variable " + _sStemVariableName + " not defined");
795 return sVariableName;
800 public String getStringValueOfObject(Object _oUnoObject, TypeClass _aTypeClass){
801 String sReturn = "";
802 switch (_aTypeClass.getValue()){
803 case TypeClass.BOOLEAN_value:
804 boolean bbooleanValue = ((Boolean) _oUnoObject).booleanValue();
805 sReturn += Boolean.toString(bbooleanValue);
806 case TypeClass.CHAR_value:
807 break;
808 case TypeClass.DOUBLE_value:
809 double fdoubleValue = ((Double) _oUnoObject).doubleValue();
810 sReturn += Double.toString(fdoubleValue);
811 break;
812 case TypeClass.ENUM_value:
813 break;
814 case TypeClass.FLOAT_value:
815 float floatValue = ((Float) _oUnoObject).floatValue();
816 sReturn += Float.toString(floatValue);
817 break;
818 case TypeClass.HYPER_value:
819 long nlongValue = ((Long) _oUnoObject).longValue();
820 sReturn += Long.toString(nlongValue);
821 break;
822 case TypeClass.LONG_value:
823 int nintValue = ((Integer) _oUnoObject).intValue();
824 sReturn += Integer.toString(nintValue);
825 break;
826 case TypeClass.SHORT_value:
827 short nshortValue = ((Short) _oUnoObject).shortValue();
828 sReturn += Short.toString(nshortValue);
829 break;
830 case TypeClass.STRING_value:
831 sReturn += (String) _oUnoObject;
832 break;
833 case TypeClass.UNSIGNED_HYPER_value:
834 nlongValue = ((Long) _oUnoObject).longValue();
835 sReturn += Long.toString(nlongValue);
836 break;
837 case TypeClass.UNSIGNED_LONG_value:
838 nintValue = ((Integer) _oUnoObject).intValue();
839 sReturn += Integer.toString(nintValue);
840 break;
841 case TypeClass.UNSIGNED_SHORT_value:
842 nshortValue = ((Short) _oUnoObject).shortValue();
843 sReturn += Short.toString(nshortValue);
844 break;
845 default:
846 System.out.println("Type " + _aTypeClass.getValue() + " not yet defined in 'getStringValueOfObject()'");
848 return sReturn;
852 public String getMethodParameterValueDescription(XUnoMethodNode _oUnoMethodNode, Object[] _oParamObjects, boolean _bIncludeParameterNames){
853 String sParamSourceCode = "";
854 ParamInfo[] aParamInfos = _oUnoMethodNode.getXIdlMethod().getParameterInfos();
855 if (_oParamObjects != null){
856 for (int i = 0; i < _oParamObjects.length; i++){
857 TypeClass aTypeClass = aParamInfos[i].aType.getTypeClass();
858 if (_bIncludeParameterNames){
859 sParamSourceCode += aParamInfos[i].aName + "=";
861 String sParamDescription = getStringValueOfObject(_oParamObjects[i], aTypeClass);
862 sParamDescription = this.m_xLanguageSourceCodeGenerator.castLiteral(sParamDescription, aTypeClass);
863 sParamSourceCode += sParamDescription;
865 if (i < _oParamObjects.length - 1){
866 sParamSourceCode += ", ";
870 return sParamSourceCode;
874 public class JavaCodeGenerator implements XLanguageSourceCodeGenerator{
875 String sStatementsCode = "";
876 boolean bAddAnyConverter = false;
877 // boolean bAddTypeImport = false;
878 boolean bIsPropertyUnoObjectDefined = false;
880 public JavaCodeGenerator(){
884 public String getStatementTerminationCharacter(){
885 return ";";
889 public String getHeaderSourceCode(Object _oUnoObject, String _sClassName, TypeClass _aTypeClass){
890 String sClassName = _sClassName;
891 String sHeaderStatement = "";
892 if (_oUnoObject != null){
893 if (!m_oIntrospector.isObjectPrimitive(_oUnoObject)){
894 if (m_oIntrospector.isObjectSequence(_oUnoObject)){
895 XTypeDescription xTypeDescription = m_oIntrospector.getReferencedType(sClassName);
896 if (xTypeDescription != null){
897 if (!m_oIntrospector.isPrimitive(xTypeDescription.getTypeClass())){
898 sClassName = getTypeString(xTypeDescription.getName(), xTypeDescription.getTypeClass(), true);
900 // primitive Types are not supposed to turn up in the import section...
901 else{
902 sClassName = "";
906 else{
907 sClassName = getTypeString(_sClassName, _aTypeClass, true);
910 else if (_aTypeClass.getValue() == TypeClass.ENUM_value){
911 sClassName = _sClassName;
913 else{
914 sClassName = "";
916 if (!sClassName.equals("")){
917 sHeaderStatement = "import " + sClassName + ";\n";
920 return sHeaderStatement;
924 public String getFinalHeaderStatements(){
925 return "";
929 public void assignqueryInterfaceHeaderSourceCode(){
930 sHeaderStatements.add("import com.sun.star.uno.UnoRuntime;\n");
931 sHeaderStatements.add("import com.sun.star.uno.XInterface;\n");
932 if (bAddAnyConverter){
933 sHeaderStatements.add("import com.sun.star.uno.AnyConverter;\n");
939 public String getConvertedSourceCodeValueOfObject(String _sReturnVariableName, String _sObjectDescription, TypeClass _aTypeClass, String _sTypeName){
940 boolean bLocAddAnyConverter = true;
941 String sReturn = "";
942 switch (_aTypeClass.getValue()){
943 case TypeClass.BOOLEAN_value:
944 sReturn = _sReturnVariableName + " = AnyConverter.toBoolean(" + _sObjectDescription + ")";
945 break;
946 case TypeClass.CHAR_value:
947 sReturn = _sReturnVariableName + " = AnyConverter.toChar(" + _sObjectDescription + ")";
948 break;
949 case TypeClass.BYTE_value:
950 sReturn = _sReturnVariableName + " = AnyConverter.toByte(" + _sObjectDescription + ")";
951 break;
952 case TypeClass.DOUBLE_value:
953 sReturn = _sReturnVariableName + " = AnyConverter.toDouble(" + _sObjectDescription + ")";
954 break;
955 case TypeClass.FLOAT_value:
956 sReturn = _sReturnVariableName + " = AnyConverter.toFloat(" + _sObjectDescription + ")";
957 break;
958 case TypeClass.UNSIGNED_HYPER_value:
959 sReturn = _sReturnVariableName + " = AnyConverter.toUnsignedLong(" + _sObjectDescription + ")";
960 break;
961 case TypeClass.HYPER_value:
962 sReturn = _sReturnVariableName + " = AnyConverter.toLong(" + _sObjectDescription + ")";
963 break;
964 case TypeClass.UNSIGNED_LONG_value:
965 sReturn = _sReturnVariableName + " = AnyConverter.toUnsignedInt(" + _sObjectDescription + ")";
966 break;
967 case TypeClass.LONG_value:
968 sReturn = _sReturnVariableName + " = AnyConverter.toInt(" + _sObjectDescription + ")";
969 break;
970 case TypeClass.SHORT_value:
971 sReturn = _sReturnVariableName + " = AnyConverter.toShort(" + _sObjectDescription + ")";
972 break;
973 case TypeClass.UNSIGNED_SHORT_value:
974 sReturn = _sReturnVariableName + " = AnyConverter.toUnsignedShort(" + _sObjectDescription + ")";
975 break;
976 case TypeClass.STRING_value:
977 sReturn = _sReturnVariableName + " = AnyConverter.toString(" + _sObjectDescription + ")";
978 break;
979 default:
980 String sShortTypeName = Introspector.getShortClassName(_sTypeName);
981 if (bIsPropertyUnoObjectDefined){
982 sReturn = "oUnoObject = " + _sObjectDescription + ";\n\t";
984 else{
985 sReturn = "Object oUnoObject = " + _sObjectDescription + ";\n\t";
986 bIsPropertyUnoObjectDefined = true;
988 sReturn += _sReturnVariableName + " = (" + sShortTypeName + ") AnyConverter.toObject(" + sShortTypeName + ".class, oUnoObject);";
989 // this.bAddTypeImport = true;
990 break;
992 if (!bAddAnyConverter){
993 bAddAnyConverter = bLocAddAnyConverter;
995 return sReturn;
999 public String getStructSourceCode(String _sReturnVariableDescription, String _sObjectDescription, String _sMember){
1000 return "\t" + _sReturnVariableDescription + " = " + _sObjectDescription + "." + _sMember + ";";
1003 public String getMainMethodSignatureSourceCode(XUnoNode _oUnoNode, String _soReturnObjectDescription){
1004 //TODO try to use + _oUnoNode.getClassName() instead of the hack
1005 String sReturn = "public void codesnippet(XInterface " + _soReturnObjectDescription + "){";
1006 if (baddExceptionHandling){
1007 sReturn += "\ntry{";
1009 return sReturn;
1012 public String getMethodSeparator(){
1013 return ".";
1016 public boolean needsqueryInterface(){
1017 return true;
1020 public String getqueryInterfaceSourceCode(String _sClassName, String _sReturnVariableName, String _sIncomingObjectName){
1021 String sShortClassName = m_oIntrospector.getShortClassName(_sClassName);
1022 return "\t" + _sReturnVariableName + " = (" + sShortClassName + ") UnoRuntime.queryInterface(" + sShortClassName + ".class, " + _sIncomingObjectName + ");\n";
1026 public String getPropertyValueGetterSourceCode(String _sPropertyName, String _sReturnVariableName, String _sIncomingObjectName, TypeClass _aTypeClass, String _sTypeName){
1027 String sObjectDescription = _sIncomingObjectName + ".getPropertyValue(\"" + _sPropertyName + "\")";
1028 String sReturn = getConvertedSourceCodeValueOfObject(_sReturnVariableName, sObjectDescription, _aTypeClass, _sTypeName);
1029 sReturn += ";";
1030 sReturn = "\t" + sReturn;
1031 return sReturn;
1032 // return "\t" + _sReturnVariableName + " = " + _sIncomingObjectName + ".getPropertyValue(\"" + _sPropertyName + "\");";
1036 public String getObjectTypeDescription(String _sClassName, boolean _bAsHeader){
1037 String sReturn = "";
1038 if (_bAsHeader){
1039 sReturn = _sClassName;
1041 else{
1042 sReturn = m_oIntrospector.getShortClassName(_sClassName);
1044 return sReturn;
1048 public String getMethodTerminationSourceCode(){
1049 String sReturn = "";
1050 int nIndex = 1;
1051 String sExceptionName = "e";
1052 if (baddExceptionHandling){
1053 for (int i = 0; i < sExceptions.size(); i++){
1054 String sCurException = (String) sExceptions.get(i);
1055 if (sReturn.indexOf(sCurException) == -1){
1056 if (nIndex > 1){
1057 sExceptionName = "e"+ nIndex;
1059 else{
1060 sReturn +="\n}";
1062 sReturn += "catch (" + sCurException + " " + sExceptionName + "){\n";
1063 sReturn += "\t" + sExceptionName + ".printStackTrace(System.out);\n";
1064 sReturn += "\t" + getCommentSign() + "Enter your Code here...\n}";
1065 nIndex++;
1069 sReturn += "\n}";
1070 return sReturn;
1073 public String castLiteral(String _sExpression, TypeClass _aTypeClass){
1074 String sReturn = "";
1075 switch (_aTypeClass.getValue()){
1076 case TypeClass.BOOLEAN_value:
1077 sReturn = _sExpression;
1078 break;
1079 case TypeClass.BYTE_value:
1080 sReturn = "(byte) " + _sExpression;
1081 break;
1082 case TypeClass.CHAR_value:
1083 sReturn = "'" + _sExpression + "'";
1084 break;
1085 case TypeClass.DOUBLE_value:
1086 sReturn = "(double) " + _sExpression;
1087 break;
1088 case TypeClass.FLOAT_value:
1089 sReturn = "(float) " + _sExpression;
1090 break;
1091 case TypeClass.UNSIGNED_SHORT_value:
1092 case TypeClass.SHORT_value:
1093 sReturn = "(short) " + _sExpression;
1094 break;
1095 case TypeClass.STRING_value:
1096 sReturn = "\"" + _sExpression + "\"";
1097 break;
1098 case TypeClass.HYPER_value:
1099 case TypeClass.UNSIGNED_HYPER_value:
1100 sReturn = "(long) " + _sExpression;
1101 break;
1102 case TypeClass.LONG_value:
1103 sReturn = _sExpression;
1104 break;
1105 case TypeClass.ENUM_value:
1106 default:
1107 sReturn = _sExpression;
1108 System.out.println("Type " + _aTypeClass.getValue() + " not yet defined in 'castliteral()'");
1110 return sReturn;
1114 public String getbooleanTypeDescription(){
1115 return "boolean";
1118 public String getbyteTypeDescription(){
1119 return "byte";
1122 public String getshortTypeDescription(){
1123 return "short";
1126 public String getunsignedshortTypeDescription(){
1127 return "short";
1130 public String getlongTypeDescription(){
1131 return "int";
1134 public String getunsignedlongTypeDescription(){
1135 return "int";
1138 public String gethyperTypeDescription(){
1139 return "long";
1142 public String getunsignedhyperTypeDescription(){
1143 return "long";
1146 public String getfloatTypeDescription(){
1147 return "float";
1150 public String getdoubleTypeDescription(){
1151 return "double";
1154 public String getcharTypeDescription(){
1155 return "char";
1158 public String getstringTypeDescription(boolean _bAsHeaderSourceCode){
1159 if (_bAsHeaderSourceCode){
1160 return "";
1162 else{
1163 return "String";
1167 public String gettypeTypeDescription(boolean _bAsHeaderSourceCode){
1168 if (_bAsHeaderSourceCode){
1169 return "com.sun.star.uno.Type";
1171 else{
1172 return "Type";
1176 public String getanyTypeDescription(boolean _bAsHeaderSourceCode){
1177 if (_bAsHeaderSourceCode){
1178 return "";
1180 else{
1181 return "Object";
1186 public String getStringValue(String _sValue){
1187 return _sValue;
1191 public String getVariableDeclaration(String _sTypeString, String _sVariableName, boolean _bIsArray, TypeClass _aTypeClass, boolean _bInitialize){
1192 String sReturn = "";
1193 if (_bIsArray){
1194 sReturn = _sTypeString + "[] " + _sVariableName;
1196 else{
1197 sReturn = _sTypeString + " " + _sVariableName;
1199 return sReturn;
1203 public String getArrayDeclaration(String _sVariableDeclaration){
1204 String sReturn = "";
1205 String[] sDeclarations = _sVariableDeclaration.split(" ");
1206 for (int i = 0; i< sDeclarations.length;i++){
1207 sReturn += sDeclarations[i];
1208 if (i == 0){
1209 sReturn += "[]";
1211 if (i < (sDeclarations.length -1)){
1212 sReturn += " ";
1215 return sReturn;
1218 public String getCommentSign(){
1219 return "//";
1224 public class BasicCodeGenerator implements XLanguageSourceCodeGenerator{
1225 String sStatementsCode = "";
1227 public BasicCodeGenerator(){
1230 public String getHeaderSourceCode(Object _oUnoObject, String _sClassName, TypeClass _aTypeClass){
1231 String sHeaderStatement = "";
1232 return sHeaderStatement;
1235 public String getFinalHeaderStatements(){
1236 return "";
1239 public String getMainMethodSignatureSourceCode(XUnoNode _oUnoNode, String _soReturnObjectDescription){
1240 //TODO try to use + _oUnoNode.getClassName() instead of the hack
1241 return "Sub Main(" + _soReturnObjectDescription + " as " + getanyTypeDescription(false) + ")";
1244 public boolean needsqueryInterface(){
1245 return false;
1248 public void assignqueryInterfaceHeaderSourceCode(){
1251 public String getMethodSeparator(){
1252 return ".";
1256 public String getqueryInterfaceSourceCode(String _sClassName, String _sReturnVariableName, String _sIncomingObjectName){
1257 return _sIncomingObjectName;
1261 public String getPropertyValueGetterSourceCode(String _sPropertyName, String _sReturnVariableName, String _sIncomingObjectName, TypeClass _aTypeClass, String _sTypeName){
1262 return "\t" + _sReturnVariableName + " = " + _sIncomingObjectName + "." + _sPropertyName;
1266 public String getStructSourceCode(String _sReturnVariableDescription, String _sObjectDescription, String _sMember){
1267 return getPropertyValueGetterSourceCode(_sMember, _sReturnVariableDescription, _sObjectDescription, null, "" );
1270 public String getConvertedSourceCodeValueOfObject(String _sReturnVariableName, String _sObjectDescription, TypeClass _aTypeClass, String _sTypeName){
1271 return _sReturnVariableName + " = " + _sObjectDescription;
1275 public String getObjectTypeDescription(String _sClassName, boolean _bAsHeader){
1276 return "Object";
1280 public String getMethodTerminationSourceCode(){
1281 return "\nEnd Sub\n";
1285 public String castLiteral(String _sExpression, TypeClass _aTypeClass){
1286 String sReturn = "";
1287 switch (_aTypeClass.getValue()){
1288 case TypeClass.BOOLEAN_value:
1289 case TypeClass.BYTE_value:
1290 case TypeClass.DOUBLE_value:
1291 case TypeClass.FLOAT_value:
1292 case TypeClass.UNSIGNED_SHORT_value:
1293 case TypeClass.SHORT_value:
1294 case TypeClass.LONG_value:
1295 case TypeClass.UNSIGNED_LONG_value:
1296 case TypeClass.HYPER_value:
1297 case TypeClass.UNSIGNED_HYPER_value:
1298 sReturn = _sExpression;
1299 break;
1300 case TypeClass.CHAR_value:
1301 case TypeClass.STRING_value:
1302 sReturn = "\"" +_sExpression + "\"";
1303 break;
1304 case TypeClass.ENUM_value:
1305 default:
1306 sReturn = _sExpression;
1307 System.out.println("Type " + _aTypeClass.getValue() + " not yet defined in 'castliteral()'");
1309 return sReturn;
1314 public String getbooleanTypeDescription(){
1315 return "Boolean";
1318 public String getbyteTypeDescription(){
1319 return "Integer";
1322 public String getshortTypeDescription(){
1323 return "Integer";
1326 public String getunsignedshortTypeDescription(){
1327 return "Integer";
1330 public String getlongTypeDescription(){
1331 return "Integer";
1334 public String getunsignedlongTypeDescription(){
1335 return "Long";
1338 public String gethyperTypeDescription(){
1339 return "Long";
1342 public String getunsignedhyperTypeDescription(){
1343 return "Long";
1346 public String getfloatTypeDescription(){
1347 return "Double";
1350 public String getdoubleTypeDescription(){
1351 return "Double";
1354 public String getcharTypeDescription(){
1355 return "String";
1358 public String getstringTypeDescription(boolean _bAsHeaderSourceCode){
1359 if (_bAsHeaderSourceCode){
1360 return "";
1362 else{
1363 return "String";
1367 public String gettypeTypeDescription(boolean _bAsHeaderSourceCode){
1368 if (_bAsHeaderSourceCode){
1369 return "";
1371 else{
1372 return "Object";
1376 public String getanyTypeDescription(boolean _bAsHeaderSourceCode){
1377 if (_bAsHeaderSourceCode){
1378 return "";
1380 else{
1381 return "Object";
1385 public String getStatementTerminationCharacter(){
1386 return "";
1390 public String getVariableDeclaration(String _sTypeString, String _sVariableName, boolean bIsArray, TypeClass _aTypeClass, boolean _bInitialize){
1391 String sReturn = "";
1392 if (bIsArray){
1393 sReturn = "Dim " + _sVariableName + "() as " + _sTypeString + "\n\t" + _sVariableName;
1395 else{
1396 sReturn = "Dim " + _sVariableName + " as " + _sTypeString + "\n\t" + _sVariableName;
1398 return sReturn;
1402 public String getStringValue(String _sValue){
1403 return _sValue;
1407 public String getArrayDeclaration(String _sVariableDeclaration){
1408 String sReturn = "";
1409 String[] sDeclarations = _sVariableDeclaration.split(" ");
1410 for (int i = 0; i< sDeclarations.length;i++){
1411 sReturn += sDeclarations[i];
1412 if (i == 0){
1413 sReturn += "[]";
1415 if (i < (sDeclarations.length -1)){
1416 sReturn += " ";
1419 return sReturn;
1422 public String getCommentSign(){
1423 return "'";
1428 public class CPlusPlusCodeGenerator implements XLanguageSourceCodeGenerator{
1429 String sStatementsCode = "";
1430 boolean bIncludeStringHeader = false;
1431 boolean bIncludeAny = false;
1432 boolean bIncludeSequenceHeader = false;
1434 public CPlusPlusCodeGenerator(){
1437 private String getCSSNameSpaceString(){
1438 return "css";
1441 public String getStatementTerminationCharacter(){
1442 return ";";
1446 public String getHeaderSourceCode(Object _oUnoObject, String _sClassName, TypeClass _aTypeClass){
1447 String sClassName = _sClassName;
1448 String sHeaderStatement = "";
1449 if (_oUnoObject != null){
1450 if (!m_oIntrospector.isObjectPrimitive(_oUnoObject)){
1451 if (m_oIntrospector.isObjectSequence(_oUnoObject)){
1452 XTypeDescription xTypeDescription = m_oIntrospector.getReferencedType(sClassName);
1453 if (xTypeDescription != null){
1454 if (!m_oIntrospector.isPrimitive(xTypeDescription.getTypeClass())){
1455 sClassName = getTypeString(xTypeDescription.getName(), xTypeDescription.getTypeClass(), true);
1457 // primitive Types are not supposed to turn up in the import section...
1458 else{
1459 sClassName = "";
1463 else{
1464 sClassName = getTypeString(_sClassName, _aTypeClass, true);
1466 if (!sClassName.equals("")){
1467 sHeaderStatement = getHeaderOfClass(sClassName);
1471 return sHeaderStatement;
1476 public String getFinalHeaderStatements(){
1477 String sReturn = "";
1478 sReturn += "\nnamespace " + getCSSNameSpaceString() + " = com::sun::star;\n";
1479 sReturn += "using namespace rtl;\n";
1480 return sReturn;
1484 private String getHeaderOfClass(String _sClassName){
1485 return "#include \"" + _sClassName.replace('.', '/') + ".hpp\"\n"; // #include <com/sun/star/uno/XComponentContext.hpp>
1490 public void assignqueryInterfaceHeaderSourceCode(){
1491 sHeaderStatements.add("#include \"sal/config.h\"\n");
1492 sHeaderStatements.add("#include \"sal/types.h\"\n");
1493 if (bIncludeStringHeader){
1494 sHeaderStatements.add("#include \"rtl/ustring.hxx\"\n");
1496 sHeaderStatements.add("#include \"com/sun/star/uno/Reference.hxx\"\n");
1497 if (bIncludeSequenceHeader){
1498 sHeaderStatements.add("#include \"com/sun/star/uno/Sequence.hxx\"\n");
1500 sHeaderStatements.add(getHeaderOfClass("com.sun.star.uno.XInterface"));
1501 if (bIncludeAny){
1502 sHeaderStatements.add(getHeaderOfClass("com.sun.star.uno.Any"));
1507 public String getMainMethodSignatureSourceCode(XUnoNode _oUnoNode, String _soReturnObjectDescription){
1508 String sReturn = "";
1509 sReturn = "void codesnippet(const " + getCSSNameSpaceString() + "::uno::Reference<" + getCSSNameSpaceString() + "::uno::XInterface>& " + _soReturnObjectDescription + " ){";
1510 int a = 0;
1511 if (!sExceptions.contains("com.sun.star.uno.RuntimeException")){
1512 sExceptions.add("com.sun.star.uno.RuntimeException");
1514 if (baddExceptionHandling){
1515 sReturn += "\n//throw ";
1516 for (int i = 0; i < sExceptions.size(); i++){
1517 String sCurException = (String) sExceptions.get(i);
1518 if (sReturn.indexOf(sCurException) == -1){
1519 if (a++ > 0){
1520 sReturn += ", ";
1522 sReturn += getObjectTypeDescription(sCurException, false);
1528 sReturn += "{";
1529 return sReturn;
1533 public boolean needsqueryInterface(){
1534 return true;
1538 public String getqueryInterfaceSourceCode(String _sClassName, String _sReturnVariableName, String _sIncomingObjectName){
1539 return "\t" + _sReturnVariableName + "( " + _sIncomingObjectName + ", " + getCSSNameSpaceString() + "::uno::UNO_QUERY_THROW);\n";
1543 public String getPropertyValueGetterSourceCode(String _sPropertyName, String _sReturnVariableName, String _sIncomingObjectName, TypeClass _aTypeClass, String _sTypeName){
1544 String sFirstLine = "\t";
1545 String sReturnVariableName = _sReturnVariableName;
1546 // e.g. uno::Any a = xPropSet->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" )) );
1547 String[] sVarDefinition = _sReturnVariableName.split("=");
1548 if (sVarDefinition.length > 0){
1549 String sVariable = sVarDefinition[0];
1550 String[] sVarDeclaration = sVariable.split(" ");
1551 if (sVarDeclaration.length > 0){
1552 sFirstLine += sReturnVariableName + ";\n";
1553 sReturnVariableName = sVarDeclaration[sVarDeclaration.length-1];
1556 String sObjectDescription = _sIncomingObjectName + "->getPropertyValue(" + getStringValue(_sPropertyName) + ")";
1557 String sSecondLine = "\t" + getConvertedSourceCodeValueOfObject(sReturnVariableName, sObjectDescription, _aTypeClass, _sTypeName) + ";";
1558 return sFirstLine + sSecondLine;
1562 public String getStructSourceCode(String _sReturnVariableDescription, String _sObjectDescription, String _sMember){
1563 return "\t" + _sReturnVariableDescription + " = " + _sObjectDescription + "->" + _sMember + ";";
1567 public String getConvertedSourceCodeValueOfObject(String _sReturnVariableName, String _sObjectDescription, TypeClass _aTypeClass, String _sTypeName){
1568 // if (m_oIntrospector.isPrimitive(_aTypeClass)){
1569 return _sObjectDescription + " >>= " + _sReturnVariableName;
1570 // }
1571 // else{
1572 // return _sReturnVariableName + " = " + _sObjectDescription;
1573 // }
1577 public String getStringValue(String _sValue){
1578 bIncludeStringHeader = true;
1579 return "OUString(RTL_CONSTASCII_USTRINGPARAM(\"" + _sValue + "\"))";
1583 public String getObjectTypeDescription(String _sClassName, boolean _bAsHeader){
1584 String sReturn = "";
1585 if (_bAsHeader){
1586 sReturn = _sClassName.replace('.', '/');
1588 else{
1589 String sModuleName = m_oIntrospector.getModuleName(_sClassName);
1590 sModuleName = m_oIntrospector.getShortClassName(sModuleName);
1591 sReturn = getCSSNameSpaceString() + "::" + sModuleName + "::" + m_oIntrospector.getShortClassName(_sClassName);
1593 return sReturn;
1597 public String getMethodTerminationSourceCode(){
1598 return "\n}";
1601 public String getMethodSeparator(){
1602 return "->";
1606 public String castLiteral(String _sExpression, TypeClass _aTypeClass){
1607 String sReturn = "";
1608 switch (_aTypeClass.getValue()){
1609 case TypeClass.BOOLEAN_value:
1610 case TypeClass.BYTE_value:
1611 case TypeClass.DOUBLE_value:
1612 case TypeClass.FLOAT_value:
1613 case TypeClass.UNSIGNED_SHORT_value:
1614 case TypeClass.SHORT_value:
1615 case TypeClass.LONG_value:
1616 case TypeClass.UNSIGNED_LONG_value:
1617 case TypeClass.HYPER_value:
1618 case TypeClass.UNSIGNED_HYPER_value:
1619 sReturn = _sExpression;
1620 break;
1621 case TypeClass.CHAR_value:
1622 sReturn = "'" + _sExpression + "'";
1623 break;
1624 case TypeClass.STRING_value:
1625 sReturn = getStringValue(_sExpression);
1626 break;
1627 case TypeClass.ENUM_value:
1628 default:
1629 sReturn = _sExpression;
1630 System.out.println("Type " + _aTypeClass.getValue() + " not yet defined in 'castliteral()'");
1632 return sReturn;
1635 public String getbooleanTypeDescription(){
1636 return "sal_Bool";
1639 public String getbyteTypeDescription(){
1640 return "sal_Int8";
1643 public String getshortTypeDescription(){
1644 return "sal_Int16";
1647 public String getunsignedshortTypeDescription(){
1648 return "sal_uInt16";
1651 public String getlongTypeDescription(){
1652 return "sal_Int32";
1655 public String getunsignedlongTypeDescription(){
1656 return "sal_uInt32";
1659 public String gethyperTypeDescription(){
1660 return "sal_Int64";
1663 public String getunsignedhyperTypeDescription(){
1664 return "sal_uInt64";
1667 public String getfloatTypeDescription(){
1668 return "float";
1671 public String getdoubleTypeDescription(){
1672 return "double";
1675 public String getcharTypeDescription(){
1676 return "sal_Unicode";
1679 public String getstringTypeDescription(boolean _bAsHeaderSourceCode){
1680 bIncludeStringHeader = true;
1681 if (_bAsHeaderSourceCode){
1682 return "";
1684 else{
1685 return "OUString";
1689 public String gettypeTypeDescription(boolean _bAsHeaderSourceCode){
1690 if (_bAsHeaderSourceCode){
1691 return "com/sun/star/uno/Type";
1693 else{
1694 return "Type";
1698 public String getanyTypeDescription(boolean _bAsHeaderSourceCode){
1699 if (_bAsHeaderSourceCode){
1700 return "com/sun/star/uno/XInterface";
1702 else{
1703 return "XInterface";
1708 public String getVariableDeclaration(String _sTypeString, String _sVariableName, boolean bIsArray, TypeClass _aTypeClass, boolean _bInitialize){
1709 boolean bIsPrimitive = m_oIntrospector.isPrimitive(_aTypeClass);
1711 // uno::Reference< frame::XDispatch > m_xDispatch
1712 String sReturn = "";
1713 if (bIsArray){
1714 bIncludeSequenceHeader = true;
1715 sReturn = getCSSNameSpaceString() + "::uno::Sequence<" + _sTypeString + "> " + _sVariableName;
1717 else{
1718 if (bIsPrimitive){
1719 sReturn = _sTypeString + " " + _sVariableName;
1720 if (_bInitialize){
1721 switch (_aTypeClass.getValue()){
1722 case TypeClass.BOOLEAN_value:
1723 sReturn = sReturn + " = false";
1724 break;
1725 case TypeClass.BYTE_value:
1726 case TypeClass.UNSIGNED_SHORT_value:
1727 case TypeClass.SHORT_value:
1728 case TypeClass.LONG_value:
1729 case TypeClass.UNSIGNED_LONG_value:
1730 case TypeClass.HYPER_value:
1731 case TypeClass.UNSIGNED_HYPER_value:
1732 sReturn = sReturn + " = 0";
1733 break;
1734 case TypeClass.DOUBLE_value:
1735 case TypeClass.FLOAT_value:
1736 sReturn = sReturn + " = 0.0";
1737 break;
1738 case TypeClass.CHAR_value:
1739 sReturn = sReturn + "'0'";
1740 break;
1741 case TypeClass.STRING_value:
1742 sReturn = _sTypeString + " " + _sVariableName;
1743 break;
1744 default:
1745 sReturn = _sTypeString + " " + _sVariableName;
1746 System.out.println("Type " + _aTypeClass.getValue() + " not yet defined in 'getVariableDeclaration()'");
1750 else{
1751 sReturn = getCSSNameSpaceString() + "::uno::Reference<" + _sTypeString + "> " +_sVariableName;
1754 return sReturn;
1757 public String getArrayDeclaration(String _sVariableDeclaration){
1758 this.bIncludeSequenceHeader = true;
1759 String sReturn = "";
1760 String[] sDeclarations = _sVariableDeclaration.split(" ");
1761 if (sDeclarations.length == 2){
1762 sReturn = getCSSNameSpaceString() +"::uno::Sequence<" + sDeclarations[1] + ">";
1764 return sReturn;
1767 public String getCommentSign(){
1768 return "//";