Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / odk / examples / java / Inspector / SourceCodeGenerator.java
blob9ff6ad7933dd64dc7085df8383b1607cd88c49df
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 import java.util.ArrayList;
37 import java.util.HashMap;
39 import com.sun.star.reflection.ParamInfo;
40 import com.sun.star.reflection.XIdlClass;
41 import com.sun.star.reflection.XIdlMethod;
42 import com.sun.star.reflection.XTypeDescription;
43 import com.sun.star.uno.Any;
44 import com.sun.star.uno.AnyConverter;
45 import com.sun.star.uno.TypeClass;
49 public class SourceCodeGenerator {
50 private ArrayList<String> sExceptions = new ArrayList<String>();
51 private ArrayList<String> sHeaderStatements = new HeaderStatements();
52 private XLanguageSourceCodeGenerator m_xLanguageSourceCodeGenerator;
53 private String sStatementCode = "";
54 private String sMainMethodSignature = "";
56 private HashMap<String, UnoObjectDefinition> aVariables = new HashMap<String, UnoObjectDefinition>();
57 private final String SSUFFIXSEPARATOR = "_";
58 private final String SVARIABLENAME = "VariableName";
59 private Introspector m_oIntrospector;
60 private ArrayList<XTreePathProvider> aTreepathProviders = new ArrayList<XTreePathProvider>();
61 private boolean baddExceptionHandling = false;
62 private boolean bXPropertySetExceptionsAreAdded = false;
63 private XUnoNode oInitialUnoNode = null;
64 private final String sINITIALVARIABLENAME = "_oUnoEntryObject";
67 /** Creates a new instance of SourceCodeGenerator */
68 public SourceCodeGenerator(int _nLanguage) {
69 this.setLanguage(_nLanguage);
70 m_oIntrospector = Introspector.getIntrospector();
74 public String addSourceCodeOfUnoObject(XTreePathProvider _xTreepathProvider, boolean _brememberPath, boolean _baddHeader){
75 String sVariableName = "";
76 if (_xTreepathProvider != null) {
77 for (int i = 0; i < _xTreepathProvider.getPathCount(); i++){
78 XUnoNode oUnoNode = _xTreepathProvider.getPathComponent(i);
79 if (i == 0){
80 sVariableName = sINITIALVARIABLENAME;
81 oInitialUnoNode = oUnoNode;
83 else{
84 if (oUnoNode instanceof XUnoMethodNode){
85 XUnoMethodNode oUnoMethodNode = (XUnoMethodNode) oUnoNode;
86 if (oUnoMethodNode.isInvoked()){
87 UnoObjectDefinition oUnoReturnObjectDefinition = getUnoObjectDefinition(_xTreepathProvider, oUnoMethodNode, i);
88 if (!isVariableDeclared(oUnoReturnObjectDefinition, this.generateVariableNameFromMethod(oUnoMethodNode.getXIdlMethod()))){
89 sStatementCode += "\n" + getMethodStatementSourceCode(oUnoMethodNode, sVariableName, oUnoReturnObjectDefinition);
91 sVariableName = oUnoReturnObjectDefinition.getVariableName();
94 else if (oUnoNode instanceof XUnoPropertyNode){
95 XUnoPropertyNode oUnoPropertyNode = (XUnoPropertyNode) oUnoNode;
96 Any oReturnObject = com.sun.star.uno.Any.complete(oUnoPropertyNode.getUnoReturnObject());
97 UnoObjectDefinition oUnoReturnObjectDefinition = new UnoObjectDefinition(oReturnObject);
98 if (!isVariableDeclared(oUnoReturnObjectDefinition, oUnoPropertyNode.getProperty().Name)){
99 sStatementCode += "\n" + getPropertyStatementSourceCode(oUnoPropertyNode, sVariableName, oUnoReturnObjectDefinition);
101 sVariableName = oUnoReturnObjectDefinition.getVariableName();
106 String sCompleteCode = combineCompleteSourceCode(_baddHeader);
107 if (_brememberPath){
108 aTreepathProviders.add(_xTreepathProvider);
110 return sCompleteCode;
114 private void setLanguage(int _nLanguage){
115 XLanguageSourceCodeGenerator xLanguageSourceCodeGenerator = null;
116 switch(_nLanguage){
117 case XLanguageSourceCodeGenerator.nJAVA:
118 xLanguageSourceCodeGenerator = new JavaCodeGenerator();
119 break;
120 case XLanguageSourceCodeGenerator.nCPLUSPLUS:
121 xLanguageSourceCodeGenerator = new CPlusPlusCodeGenerator();
122 break;
123 case XLanguageSourceCodeGenerator.nBASIC:
124 xLanguageSourceCodeGenerator = new BasicCodeGenerator();
125 break;
126 default:
127 System.out.println("Unknown Sourcecode Language. Check Menus!");
129 if (xLanguageSourceCodeGenerator != null){
130 m_xLanguageSourceCodeGenerator = xLanguageSourceCodeGenerator;
134 private void resetSourceCodeGeneration(int _nLanguage){
135 aVariables.clear();
136 this.sHeaderStatements.clear();
137 setLanguage(_nLanguage);
138 sStatementCode = "";
141 private String generateVariableNameFromMethod(String _sMethodName, String _sPrefix, boolean _bConsiderAll){
142 String sReturn = "";
143 if (_sMethodName.startsWith(_sPrefix)){
144 int nPrefixLength = _sPrefix.length();
145 if (_sMethodName.length() > nPrefixLength){
146 String sChar = _sMethodName.substring(nPrefixLength, nPrefixLength + 1);
147 String sUpperChar = sChar.toUpperCase();
148 if (sUpperChar.equals(sChar)){
149 if (_bConsiderAll){
150 sReturn = _sMethodName;
152 else{
153 sReturn = _sMethodName.substring(nPrefixLength, _sMethodName.length());
158 return sReturn;
162 private String generateVariableNameFromMethod(XIdlMethod _xIdlMethod){
163 // todo: refactor this!!!
164 String sMethodName = _xIdlMethod.getName();
165 String sReturn = "";
166 sReturn = generateVariableNameFromMethod(sMethodName, "getBy", false);
167 if (sReturn.equals("")){
168 sReturn = generateVariableNameFromMethod(sMethodName, "get", false);
170 if (sReturn.equals("")){
171 sReturn = generateVariableNameFromMethod(sMethodName, "attach", false);
173 if (sReturn.equals("")){
174 sReturn = generateVariableNameFromMethod(sMethodName, "assign", false);
176 if (sReturn.equals("")){
177 sReturn = generateVariableNameFromMethod(sMethodName, "attach", false);
179 if (sReturn.equals("")){
180 sReturn = generateVariableNameFromMethod(sMethodName, "create", false);
182 if (sReturn.equals("")){
183 sReturn = generateVariableNameFromMethod(sMethodName, "is", true);
185 if (sReturn.equals("")){
186 sReturn = generateVariableNameFromMethod(sMethodName, "has", true);
188 if (sReturn.equals("")){
189 sReturn = sMethodName;
191 return sReturn;
195 public String convertAllUnoObjects(int _nLanguage){
196 String sSourceCode = "";
197 resetSourceCodeGeneration(_nLanguage);
198 int ncount = aTreepathProviders.size();
199 for (int i=0; i< ncount; i++){
200 sSourceCode = addSourceCodeOfUnoObject(aTreepathProviders.get(i), false, (i == (ncount-1)));
202 return sSourceCode;
205 private UnoObjectDefinition getUnoObjectDefinition(XTreePathProvider _xTreePathProvider, XUnoMethodNode _oUnoMethodNode, int _nindex){
206 XUnoNode oUnoNode = null;
207 Object oUnoReturnObject = null;
208 Object[] oParamObjects = null;
209 XIdlClass xIdlClass = _oUnoMethodNode.getXIdlMethod().getReturnType();
210 String sTypeName = xIdlClass.getName();
211 TypeClass aTypeClass = xIdlClass.getTypeClass();
212 if (aTypeClass.getValue() != TypeClass.VOID_value){
213 if (_xTreePathProvider.getPathCount() > _nindex + 1){
214 oUnoNode = _xTreePathProvider.getPathComponent(_nindex + 1);
215 oUnoReturnObject = oUnoNode.getUnoObject();
218 if (oUnoReturnObject == null){
219 oUnoReturnObject = _oUnoMethodNode.getLastUnoReturnObject();
221 UnoObjectDefinition oUnoObjectDefinition = new UnoObjectDefinition(oUnoReturnObject, sTypeName, aTypeClass);
222 if (_oUnoMethodNode.hasParameters()){
223 if (oUnoNode != null){
224 oParamObjects = oUnoNode.getParameterObjects();
226 else{
227 oParamObjects = _oUnoMethodNode.getLastParameterObjects();
230 if (oParamObjects != null){
231 oUnoObjectDefinition.addParameterObjects(oParamObjects);
233 return oUnoObjectDefinition;
237 private String combineCompleteSourceCode(boolean _bAddHeader){
238 String sCompleteCode = "";
239 if (_bAddHeader){
240 sMainMethodSignature = m_xLanguageSourceCodeGenerator.getMainMethodSignatureSourceCode(oInitialUnoNode, sINITIALVARIABLENAME);
241 m_xLanguageSourceCodeGenerator.assignqueryInterfaceHeaderSourceCode();
242 sCompleteCode += getHeaderSourceCode();
244 sCompleteCode += sMainMethodSignature;
245 sCompleteCode += sStatementCode;
246 if (_bAddHeader){
247 sCompleteCode += m_xLanguageSourceCodeGenerator.getMethodTerminationSourceCode();
248 sCompleteCode += "\n" + m_xLanguageSourceCodeGenerator.getCommentSign() + "...";
250 return sCompleteCode;
254 private String getPropertyStatementSourceCode(XUnoPropertyNode _oUnoPropertyNode, String _sVariableName, UnoObjectDefinition _oUnoReturnObjectDefinition){
255 String sReturnObjectVariableDefinition = "";
256 String sStatement = "";
257 String sPropertyName = _oUnoPropertyNode.getProperty().Name;
258 UnoObjectDefinition oUnoObjectDefinition = new UnoObjectDefinition(_oUnoPropertyNode.getUnoObject(), "com.sun.star.beans.XPropertySet");
259 if (!m_xLanguageSourceCodeGenerator.needsqueryInterface() || (oUnoObjectDefinition.getTypeClass().getValue() == TypeClass.STRUCT_value)){
260 oUnoObjectDefinition.setVariableName(_sVariableName);
261 if (oUnoObjectDefinition.getTypeClass().getValue() == TypeClass.STRUCT_value){
262 sReturnObjectVariableDefinition = getVariableInitialization(_oUnoReturnObjectDefinition, false);
263 sStatement += m_xLanguageSourceCodeGenerator.getStructSourceCode(sReturnObjectVariableDefinition, oUnoObjectDefinition.getVariableName(), sPropertyName);
264 return sStatement;
267 sStatement += addQueryInterfaceSourceCode(oUnoObjectDefinition, _sVariableName, "com.sun.star.beans.XPropertySet");
268 if (_oUnoReturnObjectDefinition.getTypeClass().getValue() != TypeClass.VOID_value){
269 sReturnObjectVariableDefinition = getVariableInitialization(_oUnoReturnObjectDefinition, true);
271 sStatement += m_xLanguageSourceCodeGenerator.getPropertyValueGetterSourceCode(sPropertyName, sReturnObjectVariableDefinition, oUnoObjectDefinition.getVariableName(), _oUnoReturnObjectDefinition.getTypeClass(), _oUnoReturnObjectDefinition.getTypeName());
272 addXPropertySetRelatedExceptions();
273 return sStatement;
277 private String getMethodStatementSourceCode(XUnoMethodNode _oUnoMethodNode, String _sVariableName, UnoObjectDefinition _oUnoReturnObjectDefinition){
278 String sReturnObjectVariableDefinition = "";
279 String sStatement = "";
280 XIdlMethod xIdlMethod = _oUnoMethodNode.getXIdlMethod();
281 TypeClass aReturnTypeClass = xIdlMethod.getReturnType().getTypeClass();
282 UnoObjectDefinition oUnoObjectDefinition = new UnoObjectDefinition(_oUnoMethodNode.getUnoObject(), _oUnoMethodNode.getClassName());
283 String sVariableStemName = this.generateVariableNameFromMethod(xIdlMethod);
284 sStatement += addQueryInterfaceSourceCode(oUnoObjectDefinition, _sVariableName, oUnoObjectDefinition.getTypeName());
285 if (_oUnoReturnObjectDefinition.getTypeClass().getValue() != TypeClass.VOID_value){
286 sReturnObjectVariableDefinition = getVariableInitialization(_oUnoReturnObjectDefinition, false) + " = ";
288 Object[] oParamObjects = _oUnoReturnObjectDefinition.getParameterObjects();
289 String sParameterCode = getMethodParameterValueDescription(_oUnoMethodNode, oParamObjects, false);
290 String sSeparator = m_xLanguageSourceCodeGenerator.getMethodSeparator();
291 sStatement += "\t" + sReturnObjectVariableDefinition + oUnoObjectDefinition.getVariableName() + sSeparator + xIdlMethod.getName() + "(" + sParameterCode + ")";
292 sStatement += m_xLanguageSourceCodeGenerator.getStatementTerminationCharacter();
293 addExceptions(xIdlMethod);
294 return sStatement;
298 private String addQueryInterfaceSourceCode(UnoObjectDefinition _oUnoObjectDefinition, String _sVariableName, String _sTypeName){
299 String sLocStatement = "";
300 if (m_xLanguageSourceCodeGenerator.needsqueryInterface()){
301 if (!isVariableDeclared(_oUnoObjectDefinition, "")){
302 String sObjectVariableDefinition = getVariableDeclaration(_oUnoObjectDefinition, false, "");
303 sLocStatement += m_xLanguageSourceCodeGenerator.getqueryInterfaceSourceCode(_sTypeName, sObjectVariableDefinition, _sVariableName);
306 else{
307 _oUnoObjectDefinition.setVariableName(_sVariableName);
309 return sLocStatement;
313 private void addXPropertySetRelatedExceptions(){
314 if (!bXPropertySetExceptionsAreAdded){
315 sExceptions.add("com.sun.star.beans.UnknownPropertyException");
316 sExceptions.add("com.sun.star.lang.WrappedTargetException");
317 sExceptions.add("com.sun.star.lang.IllegalArgumentException");
318 bXPropertySetExceptionsAreAdded = true;
319 baddExceptionHandling = true;
324 private void addExceptions(XIdlMethod _xIdlMethod){
325 XIdlClass[] xIdlClasses = _xIdlMethod.getExceptionTypes();
326 for (int i = 0; i > xIdlClasses.length; i++){
327 sExceptions.add(xIdlClasses[0].getName());
328 baddExceptionHandling = true;
332 private String getHeaderSourceCode(){
333 for(UnoObjectDefinition oUnoObjectDefinition : aVariables.values()){
334 String sCurHeaderStatement = m_xLanguageSourceCodeGenerator.getHeaderSourceCode(oUnoObjectDefinition.getUnoObject(), oUnoObjectDefinition.getTypeName(), oUnoObjectDefinition.getTypeClass());
335 sHeaderStatements.add(sCurHeaderStatement);
337 String sHeaderSourcecode = "";
338 String[] sHeaderStatementArray = new String[sHeaderStatements.size()];
339 sHeaderStatements.toArray(sHeaderStatementArray);
340 java.util.Arrays.sort(sHeaderStatementArray);
341 for (int i = 0; i < sHeaderStatementArray.length; i++){
342 sHeaderSourcecode += sHeaderStatementArray[i];
344 sHeaderSourcecode += m_xLanguageSourceCodeGenerator.getFinalHeaderStatements();
345 return sHeaderSourcecode +"\n" + m_xLanguageSourceCodeGenerator.getCommentSign() + "...\n";
349 private class HeaderStatements extends ArrayList<String> {
351 public boolean contains(String _oElement){
352 String sCompName = _oElement;
353 for (int i = 0; i < this.size(); i++){
354 String sElement = this.get(i);
355 if (sElement.equals(sCompName)){
356 return true;
359 return false;
363 @Override
364 public boolean add(String _oElement){
365 if (!contains(_oElement)){
366 super.add(_oElement);
367 return true;
369 return false;
374 private boolean isVariableDeclared(UnoObjectDefinition _oUnoObjectDefinition, String _sDefaultStemName){
375 boolean bisDeclared = false;
376 if (!_sDefaultStemName.equals("")){
377 _oUnoObjectDefinition.setCentralVariableStemName(_sDefaultStemName);
379 String sVariableStemName = _oUnoObjectDefinition.getVariableStemName();
380 bisDeclared = aVariables.containsKey(sVariableStemName);
381 if (bisDeclared){
382 Object oUnoObject = _oUnoObjectDefinition.getUnoObject();
383 if (Introspector.isObjectPrimitive(oUnoObject)){
384 bisDeclared = false;
386 else if (Introspector.isObjectSequence(oUnoObject)){
387 bisDeclared = false;
389 else{
390 String sCompVariableName = sVariableStemName;
391 String sUnoObjectIdentity = oUnoObject.toString();
392 boolean bleaveloop = false;
393 int a = 2;
394 while (!bleaveloop){
395 if (aVariables.containsKey(sCompVariableName)){
396 Object oUnoCompObject = aVariables.get(sCompVariableName).getUnoObject();
397 String sUnoCompObjectIdentity = oUnoCompObject.toString();
398 bleaveloop = sUnoCompObjectIdentity.equals(sUnoObjectIdentity);
399 bisDeclared = bleaveloop;
400 if (!bleaveloop){
401 sCompVariableName = sVariableStemName + SSUFFIXSEPARATOR + a++;
404 else{
405 bleaveloop = true;
406 bisDeclared = false;
411 return bisDeclared;
415 private String addUniqueVariableName(String _sStemVariableName, UnoObjectDefinition _oUnoObjectDefinition){
416 boolean bElementexists = true;
417 int a = 2;
418 String sCompName = _sStemVariableName;
419 while (bElementexists){
420 if (! aVariables.containsKey(sCompName)){
421 aVariables.put(sCompName, _oUnoObjectDefinition);
422 break;
424 sCompName = _sStemVariableName + SSUFFIXSEPARATOR + a++;
426 return sCompName;
431 private String getTypeString(String _sTypeName, TypeClass _aTypeClass, boolean _bAsHeaderSourceCode){
432 String sTypeString = "";
433 switch (_aTypeClass.getValue()){
434 case TypeClass.BOOLEAN_value:
435 sTypeString = m_xLanguageSourceCodeGenerator.getbooleanTypeDescription();
436 break;
437 case TypeClass.BYTE_value:
438 sTypeString = m_xLanguageSourceCodeGenerator.getbyteTypeDescription();
439 break;
440 case TypeClass.CHAR_value:
441 sTypeString = m_xLanguageSourceCodeGenerator.getcharTypeDescription();
442 break;
443 case TypeClass.DOUBLE_value:
444 sTypeString = m_xLanguageSourceCodeGenerator.getdoubleTypeDescription();
445 break;
446 case TypeClass.FLOAT_value:
447 sTypeString = m_xLanguageSourceCodeGenerator.getfloatTypeDescription();
448 break;
449 case TypeClass.HYPER_value:
450 sTypeString = m_xLanguageSourceCodeGenerator.gethyperTypeDescription();
451 break;
452 case TypeClass.LONG_value:
453 sTypeString = m_xLanguageSourceCodeGenerator.getlongTypeDescription();
454 break;
455 case TypeClass.SHORT_value:
456 sTypeString = m_xLanguageSourceCodeGenerator.getshortTypeDescription();
457 break;
458 case TypeClass.STRING_value:
459 sTypeString = m_xLanguageSourceCodeGenerator.getstringTypeDescription(_bAsHeaderSourceCode);
460 break;
461 case TypeClass.UNSIGNED_HYPER_value:
462 sTypeString = m_xLanguageSourceCodeGenerator.getunsignedhyperTypeDescription();
463 break;
464 case TypeClass.UNSIGNED_LONG_value:
465 sTypeString = m_xLanguageSourceCodeGenerator.getunsignedlongTypeDescription();
466 break;
467 case TypeClass.UNSIGNED_SHORT_value:
468 sTypeString = m_xLanguageSourceCodeGenerator.getdoubleTypeDescription();
469 break;
470 case TypeClass.SEQUENCE_value:
471 //TODO consider mulitdimensional Arrays
472 XTypeDescription xTypeDescription = Introspector.getIntrospector().getReferencedType(_sTypeName);
473 if (xTypeDescription != null){
474 sTypeString = getTypeString(xTypeDescription.getName(), xTypeDescription.getTypeClass(), _bAsHeaderSourceCode);
476 break;
477 case TypeClass.ANY_value:
478 sTypeString = m_xLanguageSourceCodeGenerator.getanyTypeDescription(_bAsHeaderSourceCode);
479 break;
480 case TypeClass.TYPE_value:
481 sTypeString = m_xLanguageSourceCodeGenerator.getObjectTypeDescription("com.sun.star.uno.Type", _bAsHeaderSourceCode);
482 break;
483 case TypeClass.ENUM_value:
484 case TypeClass.STRUCT_value:
485 case TypeClass.INTERFACE_ATTRIBUTE_value:
486 case TypeClass.INTERFACE_METHOD_value:
487 case TypeClass.INTERFACE_value:
488 case TypeClass.PROPERTY_value:
489 sTypeString = m_xLanguageSourceCodeGenerator.getObjectTypeDescription(_sTypeName, _bAsHeaderSourceCode);
490 break;
491 default:
493 return sTypeString;
497 private String getVariableDeclaration(UnoObjectDefinition _oUnoObjectDefinition, boolean _bInitialize, String _sVariableDefaultName){
498 TypeClass aTypeClass = _oUnoObjectDefinition.getTypeClass();
499 TypeClass aLocTypeClass = aTypeClass;
500 boolean bIsArray = false;
501 if (_oUnoObjectDefinition.getUnoObject() != null){
502 bIsArray = Introspector.isObjectSequence(_oUnoObjectDefinition.getUnoObject());
504 else{
505 bIsArray = _oUnoObjectDefinition.getTypeClass().getValue() == TypeClass.SEQUENCE_value;
507 String sVariableName = _oUnoObjectDefinition.getVariableName(_sVariableDefaultName);
508 String sTypeName = _oUnoObjectDefinition.getTypeName();
509 String sTypeString = getTypeString(sTypeName, aLocTypeClass, false);
510 if (bIsArray){
511 XTypeDescription xTypeDescription = Introspector.getIntrospector().getReferencedType(sTypeName);
512 if (xTypeDescription != null){
513 aLocTypeClass = xTypeDescription.getTypeClass();
516 String sVariableDeclaration = m_xLanguageSourceCodeGenerator.getVariableDeclaration(sTypeString, sVariableName, bIsArray, aLocTypeClass, _bInitialize);
517 addUniqueVariableName(sVariableName, _oUnoObjectDefinition);
518 return sVariableDeclaration;
522 private String getVariableInitialization(UnoObjectDefinition _oUnoObjectDefinition, boolean _bInitialize){
523 String sObjectVariableDeclaration = "";
524 String sVariableName = _oUnoObjectDefinition.getVariableName();
525 if (isVariableDeclared(_oUnoObjectDefinition, "")){
526 sObjectVariableDeclaration = sVariableName;
528 else{
529 sObjectVariableDeclaration = getVariableDeclaration(_oUnoObjectDefinition, _bInitialize, "");
531 return sObjectVariableDeclaration;
536 public String getVariableNameforUnoObject(String _sShortClassName){
537 if (_sShortClassName.startsWith("X")){
538 return "x" + _sShortClassName.substring(1);
540 else{
541 return _sShortClassName;
546 private class UnoObjectDefinition{
547 private Object m_oUnoObject = null;
549 private String sVariableStemName = "";
550 private String m_sCentralVariableStemName = "";
551 private String sVariableName = "";
552 private String m_sTypeName = "";
553 private TypeClass m_aTypeClass = null;
554 private Object[] m_oParameterObjects = null;
557 private UnoObjectDefinition(Any _oUnoObject){
558 m_sTypeName = _oUnoObject.getType().getTypeName();
559 m_aTypeClass = _oUnoObject.getType().getTypeClass();
560 m_oUnoObject = _oUnoObject;
561 m_sCentralVariableStemName = getCentralVariableStemName(m_aTypeClass);
565 private UnoObjectDefinition(Object _oUnoObject, String _sTypeName, TypeClass _aTypeClass){
566 m_oUnoObject = _oUnoObject;
567 m_sTypeName = _sTypeName;
568 m_aTypeClass = _aTypeClass;
569 m_sCentralVariableStemName = getCentralVariableStemName(m_aTypeClass);
573 private UnoObjectDefinition(Object _oUnoObject, String _sTypeName){
574 m_oUnoObject = _oUnoObject;
575 m_sTypeName = _sTypeName;
576 m_aTypeClass = AnyConverter.getType(_oUnoObject).getTypeClass();
577 m_sCentralVariableStemName = getCentralVariableStemName(m_aTypeClass);
581 private String getCentralVariableStemName(TypeClass _aTypeClass){
582 String sCentralVariableStemName = "";
583 int nTypeClass = _aTypeClass.getValue();
584 switch(nTypeClass){
585 case TypeClass.SEQUENCE_value:
586 //TODO consider mulitdimensional Arrays
587 XTypeDescription xTypeDescription = Introspector.getIntrospector().getReferencedType(getTypeName());
588 if (xTypeDescription != null){
589 sCentralVariableStemName = getCentralVariableStemName(xTypeDescription.getTypeClass());
591 break;
592 case TypeClass.TYPE_value:
593 sCentralVariableStemName = SVARIABLENAME;
594 break;
595 case TypeClass.STRUCT_value:
596 sCentralVariableStemName = Introspector.getShortClassName(getTypeName());
597 break;
598 case TypeClass.INTERFACE_ATTRIBUTE_value:
599 case TypeClass.INTERFACE_METHOD_value:
600 case TypeClass.INTERFACE_value:
601 case TypeClass.PROPERTY_value:
602 String sShortClassName = Introspector.getShortClassName(getTypeName());
603 sCentralVariableStemName = getVariableNameforUnoObject(sShortClassName);
604 default:
605 sCentralVariableStemName = SVARIABLENAME;
607 return sCentralVariableStemName;
610 /** may return null
612 public Object getUnoObject(){
613 return m_oUnoObject;
617 public TypeClass getTypeClass(){
618 return m_aTypeClass;
622 public String getTypeName(){
623 return m_sTypeName;
627 public void setCentralVariableStemName(String _sCentralVariableStemName){
628 m_sCentralVariableStemName = _sCentralVariableStemName;
632 public String getVariableStemName(){
633 if (sVariableStemName.equals("")){
634 sVariableStemName = getVariableStemName(m_aTypeClass);
636 return sVariableStemName;
640 private void addParameterObjects(Object[] _oParameterObjects){
641 m_oParameterObjects = _oParameterObjects;
645 public Object[] getParameterObjects(){
646 return m_oParameterObjects;
653 private String getVariableStemName(TypeClass _aTypeClass){
654 int nTypeClass = _aTypeClass.getValue();
655 switch(nTypeClass){
656 case TypeClass.BOOLEAN_value:
657 sVariableStemName = "b" + m_sCentralVariableStemName;
658 break;
659 case TypeClass.DOUBLE_value:
660 case TypeClass.FLOAT_value:
661 sVariableStemName = "f" + m_sCentralVariableStemName;
662 break;
663 case TypeClass.BYTE_value:
664 case TypeClass.HYPER_value:
665 case TypeClass.LONG_value:
666 case TypeClass.UNSIGNED_HYPER_value:
667 case TypeClass.UNSIGNED_LONG_value:
668 case TypeClass.UNSIGNED_SHORT_value:
669 case TypeClass.SHORT_value:
670 sVariableStemName = "n" + m_sCentralVariableStemName;
671 break;
672 case TypeClass.CHAR_value:
673 case TypeClass.STRING_value:
674 sVariableStemName = "s" + m_sCentralVariableStemName;
675 break;
676 case TypeClass.SEQUENCE_value:
677 //TODO consider mulitdimensional Arrays
678 XTypeDescription xTypeDescription = Introspector.getIntrospector().getReferencedType(getTypeName());
679 if (xTypeDescription != null){
680 sVariableStemName = getVariableStemName(xTypeDescription.getTypeClass());
682 break;
683 case TypeClass.TYPE_value:
684 sVariableStemName = "a" + m_sCentralVariableStemName;
685 break;
686 case TypeClass.ANY_value:
687 sVariableStemName = "o" + m_sCentralVariableStemName;
688 break;
689 case TypeClass.STRUCT_value:
690 case TypeClass.ENUM_value:
691 sVariableStemName = "a" + m_sCentralVariableStemName;
692 break;
693 case TypeClass.INTERFACE_ATTRIBUTE_value:
694 case TypeClass.INTERFACE_METHOD_value:
695 case TypeClass.INTERFACE_value:
696 case TypeClass.PROPERTY_value:
697 String sShortClassName = Introspector.getShortClassName(getTypeName());
698 sVariableStemName = getVariableNameforUnoObject(sShortClassName);
699 default:
701 return sVariableStemName;
705 private void setVariableName(String _sVariableName){
706 sVariableName = _sVariableName;
710 private String getVariableName(String _sCentralVariableStemName){
711 if (!_sCentralVariableStemName.equals("")){
712 this.m_sCentralVariableStemName = _sCentralVariableStemName;
714 return getVariableName();
718 private String getVariableName() throws NullPointerException{
719 if (sVariableName.equals("")){
720 int a = 2;
721 sVariableName = getVariableStemName();
722 boolean bleaveloop = false;
723 while (!bleaveloop){
724 if (aVariables.containsKey(sVariableName)){
725 String sUnoObjectIdentity = aVariables.get(sVariableName).getUnoObject().toString();
726 if (m_oUnoObject != null){
727 if ((sUnoObjectIdentity.equals(m_oUnoObject.toString()) && (!Introspector.isPrimitive(this.getTypeClass())) &&
728 (! Introspector.isObjectSequence(m_oUnoObject)))){
729 bleaveloop = true;
731 else{
732 sVariableName = getVariableStemName() + SSUFFIXSEPARATOR + a++;
735 else{
736 bleaveloop = true;
739 else{
740 bleaveloop = true;
744 return sVariableName;
749 public String getStringValueOfObject(Object _oUnoObject, TypeClass _aTypeClass){
750 String sReturn = "";
751 switch (_aTypeClass.getValue()){
752 case TypeClass.BOOLEAN_value:
753 boolean bbooleanValue = ((Boolean) _oUnoObject).booleanValue();
754 sReturn += Boolean.toString(bbooleanValue);
755 case TypeClass.CHAR_value:
756 break;
757 case TypeClass.DOUBLE_value:
758 double fdoubleValue = ((Double) _oUnoObject).doubleValue();
759 sReturn += Double.toString(fdoubleValue);
760 break;
761 case TypeClass.ENUM_value:
762 break;
763 case TypeClass.FLOAT_value:
764 float floatValue = ((Float) _oUnoObject).floatValue();
765 sReturn += Float.toString(floatValue);
766 break;
767 case TypeClass.HYPER_value:
768 long nlongValue = ((Long) _oUnoObject).longValue();
769 sReturn += Long.toString(nlongValue);
770 break;
771 case TypeClass.LONG_value:
772 int nintValue = ((Integer) _oUnoObject).intValue();
773 sReturn += Integer.toString(nintValue);
774 break;
775 case TypeClass.SHORT_value:
776 short nshortValue = ((Short) _oUnoObject).shortValue();
777 sReturn += Short.toString(nshortValue);
778 break;
779 case TypeClass.STRING_value:
780 sReturn += (String) _oUnoObject;
781 break;
782 case TypeClass.UNSIGNED_HYPER_value:
783 nlongValue = ((Long) _oUnoObject).longValue();
784 sReturn += Long.toString(nlongValue);
785 break;
786 case TypeClass.UNSIGNED_LONG_value:
787 nintValue = ((Integer) _oUnoObject).intValue();
788 sReturn += Integer.toString(nintValue);
789 break;
790 case TypeClass.UNSIGNED_SHORT_value:
791 nshortValue = ((Short) _oUnoObject).shortValue();
792 sReturn += Short.toString(nshortValue);
793 break;
794 default:
795 System.out.println("Type " + _aTypeClass.getValue() + " not yet defined in 'getStringValueOfObject()'");
797 return sReturn;
801 public String getMethodParameterValueDescription(XUnoMethodNode _oUnoMethodNode, Object[] _oParamObjects, boolean _bIncludeParameterNames){
802 String sParamSourceCode = "";
803 ParamInfo[] aParamInfos = _oUnoMethodNode.getXIdlMethod().getParameterInfos();
804 if (_oParamObjects != null){
805 for (int i = 0; i < _oParamObjects.length; i++){
806 TypeClass aTypeClass = aParamInfos[i].aType.getTypeClass();
807 if (_bIncludeParameterNames){
808 sParamSourceCode += aParamInfos[i].aName + "=";
810 String sParamDescription = getStringValueOfObject(_oParamObjects[i], aTypeClass);
811 sParamDescription = this.m_xLanguageSourceCodeGenerator.castLiteral(sParamDescription, aTypeClass);
812 sParamSourceCode += sParamDescription;
814 if (i < _oParamObjects.length - 1){
815 sParamSourceCode += ", ";
819 return sParamSourceCode;
823 private class JavaCodeGenerator implements XLanguageSourceCodeGenerator{
825 private boolean bAddAnyConverter = false;
826 private boolean bIsPropertyUnoObjectDefined = false;
828 public JavaCodeGenerator(){
832 public String getStatementTerminationCharacter(){
833 return ";";
837 public String getHeaderSourceCode(Object _oUnoObject, String _sClassName, TypeClass _aTypeClass){
838 String sClassName = _sClassName;
839 String sHeaderStatement = "";
840 if (_oUnoObject != null){
841 if (!Introspector.isObjectPrimitive(_oUnoObject)){
842 if (Introspector.isObjectSequence(_oUnoObject)){
843 XTypeDescription xTypeDescription = m_oIntrospector.getReferencedType(sClassName);
844 if (xTypeDescription != null){
845 if (!Introspector.isPrimitive(xTypeDescription.getTypeClass())){
846 sClassName = getTypeString(xTypeDescription.getName(), xTypeDescription.getTypeClass(), true);
848 // primitive Types are not supposed to turn up in the import section...
849 else{
850 sClassName = "";
854 else{
855 sClassName = getTypeString(_sClassName, _aTypeClass, true);
858 else if (_aTypeClass.getValue() == TypeClass.ENUM_value){
859 sClassName = _sClassName;
861 else{
862 sClassName = "";
864 if (!sClassName.equals("")){
865 sHeaderStatement = "import " + sClassName + ";\n";
868 return sHeaderStatement;
872 public String getFinalHeaderStatements(){
873 return "";
877 public void assignqueryInterfaceHeaderSourceCode(){
878 sHeaderStatements.add("import com.sun.star.uno.UnoRuntime;\n");
879 sHeaderStatements.add("import com.sun.star.uno.XInterface;\n");
880 if (bAddAnyConverter){
881 sHeaderStatements.add("import com.sun.star.uno.AnyConverter;\n");
887 public String getConvertedSourceCodeValueOfObject(String _sReturnVariableName, String _sObjectDescription, TypeClass _aTypeClass, String _sTypeName){
888 boolean bLocAddAnyConverter = true;
889 String sReturn = "";
890 switch (_aTypeClass.getValue()){
891 case TypeClass.BOOLEAN_value:
892 sReturn = _sReturnVariableName + " = AnyConverter.toBoolean(" + _sObjectDescription + ")";
893 break;
894 case TypeClass.CHAR_value:
895 sReturn = _sReturnVariableName + " = AnyConverter.toChar(" + _sObjectDescription + ")";
896 break;
897 case TypeClass.BYTE_value:
898 sReturn = _sReturnVariableName + " = AnyConverter.toByte(" + _sObjectDescription + ")";
899 break;
900 case TypeClass.DOUBLE_value:
901 sReturn = _sReturnVariableName + " = AnyConverter.toDouble(" + _sObjectDescription + ")";
902 break;
903 case TypeClass.FLOAT_value:
904 sReturn = _sReturnVariableName + " = AnyConverter.toFloat(" + _sObjectDescription + ")";
905 break;
906 case TypeClass.UNSIGNED_HYPER_value:
907 sReturn = _sReturnVariableName + " = AnyConverter.toUnsignedLong(" + _sObjectDescription + ")";
908 break;
909 case TypeClass.HYPER_value:
910 sReturn = _sReturnVariableName + " = AnyConverter.toLong(" + _sObjectDescription + ")";
911 break;
912 case TypeClass.UNSIGNED_LONG_value:
913 sReturn = _sReturnVariableName + " = AnyConverter.toUnsignedInt(" + _sObjectDescription + ")";
914 break;
915 case TypeClass.LONG_value:
916 sReturn = _sReturnVariableName + " = AnyConverter.toInt(" + _sObjectDescription + ")";
917 break;
918 case TypeClass.SHORT_value:
919 sReturn = _sReturnVariableName + " = AnyConverter.toShort(" + _sObjectDescription + ")";
920 break;
921 case TypeClass.UNSIGNED_SHORT_value:
922 sReturn = _sReturnVariableName + " = AnyConverter.toUnsignedShort(" + _sObjectDescription + ")";
923 break;
924 case TypeClass.STRING_value:
925 sReturn = _sReturnVariableName + " = AnyConverter.toString(" + _sObjectDescription + ")";
926 break;
927 default:
928 String sShortTypeName = Introspector.getShortClassName(_sTypeName);
929 if (bIsPropertyUnoObjectDefined){
930 sReturn = "oUnoObject = " + _sObjectDescription + ";\n\t";
932 else{
933 sReturn = "Object oUnoObject = " + _sObjectDescription + ";\n\t";
934 bIsPropertyUnoObjectDefined = true;
936 sReturn += _sReturnVariableName + " = (" + sShortTypeName + ") AnyConverter.toObject(" + sShortTypeName + ".class, oUnoObject);";
937 break;
939 if (!bAddAnyConverter){
940 bAddAnyConverter = bLocAddAnyConverter;
942 return sReturn;
946 public String getStructSourceCode(String _sReturnVariableDescription, String _sObjectDescription, String _sMember){
947 return "\t" + _sReturnVariableDescription + " = " + _sObjectDescription + "." + _sMember + ";";
950 public String getMainMethodSignatureSourceCode(XUnoNode _oUnoNode, String _soReturnObjectDescription){
951 //TODO try to use + _oUnoNode.getClassName() instead of the hack
952 String sReturn = "public void codesnippet(XInterface " + _soReturnObjectDescription + "){";
953 if (baddExceptionHandling){
954 sReturn += "\ntry{";
956 return sReturn;
959 public String getMethodSeparator(){
960 return ".";
963 public boolean needsqueryInterface(){
964 return true;
967 public String getqueryInterfaceSourceCode(String _sClassName, String _sReturnVariableName, String _sIncomingObjectName){
968 String sShortClassName = Introspector.getShortClassName(_sClassName);
969 return "\t" + _sReturnVariableName + " = (" + sShortClassName + ") UnoRuntime.queryInterface(" + sShortClassName + ".class, " + _sIncomingObjectName + ");\n";
973 public String getPropertyValueGetterSourceCode(String _sPropertyName, String _sReturnVariableName, String _sIncomingObjectName, TypeClass _aTypeClass, String _sTypeName){
974 String sObjectDescription = _sIncomingObjectName + ".getPropertyValue(\"" + _sPropertyName + "\")";
975 String sReturn = getConvertedSourceCodeValueOfObject(_sReturnVariableName, sObjectDescription, _aTypeClass, _sTypeName);
976 sReturn += ";";
977 sReturn = "\t" + sReturn;
978 return sReturn;
982 public String getObjectTypeDescription(String _sClassName, boolean _bAsHeader){
983 String sReturn = "";
984 if (_bAsHeader){
985 sReturn = _sClassName;
987 else{
988 sReturn = Introspector.getShortClassName(_sClassName);
990 return sReturn;
994 public String getMethodTerminationSourceCode(){
995 String sReturn = "";
996 int nIndex = 1;
997 String sExceptionName = "e";
998 if (baddExceptionHandling){
999 for (int i = 0; i < sExceptions.size(); i++){
1000 String sCurException = sExceptions.get(i);
1001 if (sReturn.indexOf(sCurException) == -1){
1002 if (nIndex > 1){
1003 sExceptionName = "e"+ nIndex;
1005 else{
1006 sReturn +="\n}";
1008 sReturn += "catch (" + sCurException + " " + sExceptionName + "){\n";
1009 sReturn += "\t" + sExceptionName + ".printStackTrace(System.err);\n";
1010 sReturn += "\t" + getCommentSign() + "Enter your Code here...\n}";
1011 nIndex++;
1015 sReturn += "\n}";
1016 return sReturn;
1019 public String castLiteral(String _sExpression, TypeClass _aTypeClass){
1020 String sReturn = "";
1021 switch (_aTypeClass.getValue()){
1022 case TypeClass.BOOLEAN_value:
1023 sReturn = _sExpression;
1024 break;
1025 case TypeClass.BYTE_value:
1026 sReturn = "(byte) " + _sExpression;
1027 break;
1028 case TypeClass.CHAR_value:
1029 sReturn = "'" + _sExpression + "'";
1030 break;
1031 case TypeClass.DOUBLE_value:
1032 sReturn = "(double) " + _sExpression;
1033 break;
1034 case TypeClass.FLOAT_value:
1035 sReturn = "(float) " + _sExpression;
1036 break;
1037 case TypeClass.UNSIGNED_SHORT_value:
1038 case TypeClass.SHORT_value:
1039 sReturn = "(short) " + _sExpression;
1040 break;
1041 case TypeClass.STRING_value:
1042 sReturn = "\"" + _sExpression + "\"";
1043 break;
1044 case TypeClass.HYPER_value:
1045 case TypeClass.UNSIGNED_HYPER_value:
1046 sReturn = "(long) " + _sExpression;
1047 break;
1048 case TypeClass.LONG_value:
1049 sReturn = _sExpression;
1050 break;
1051 case TypeClass.ENUM_value:
1052 default:
1053 sReturn = _sExpression;
1054 System.out.println("Type " + _aTypeClass.getValue() + " not yet defined in 'castliteral()'");
1056 return sReturn;
1060 public String getbooleanTypeDescription(){
1061 return "boolean";
1064 public String getbyteTypeDescription(){
1065 return "byte";
1068 public String getshortTypeDescription(){
1069 return "short";
1072 public String getlongTypeDescription(){
1073 return "int";
1076 public String getunsignedlongTypeDescription(){
1077 return "int";
1080 public String gethyperTypeDescription(){
1081 return "long";
1084 public String getunsignedhyperTypeDescription(){
1085 return "long";
1088 public String getfloatTypeDescription(){
1089 return "float";
1092 public String getdoubleTypeDescription(){
1093 return "double";
1096 public String getcharTypeDescription(){
1097 return "char";
1100 public String getstringTypeDescription(boolean _bAsHeaderSourceCode){
1101 if (_bAsHeaderSourceCode){
1102 return "";
1104 else{
1105 return "String";
1109 public String getanyTypeDescription(boolean _bAsHeaderSourceCode){
1110 if (_bAsHeaderSourceCode){
1111 return "";
1113 else{
1114 return "Object";
1119 public String getStringValue(String _sValue){
1120 return _sValue;
1124 public String getVariableDeclaration(String _sTypeString, String _sVariableName, boolean _bIsArray, TypeClass _aTypeClass, boolean _bInitialize){
1125 String sReturn = "";
1126 if (_bIsArray){
1127 sReturn = _sTypeString + "[] " + _sVariableName;
1129 else{
1130 sReturn = _sTypeString + " " + _sVariableName;
1132 return sReturn;
1136 public String getCommentSign(){
1137 return "//";
1142 private class BasicCodeGenerator implements XLanguageSourceCodeGenerator{
1145 public BasicCodeGenerator(){
1148 public String getHeaderSourceCode(Object _oUnoObject, String _sClassName, TypeClass _aTypeClass){
1149 String sHeaderStatement = "";
1150 return sHeaderStatement;
1153 public String getFinalHeaderStatements(){
1154 return "";
1157 public String getMainMethodSignatureSourceCode(XUnoNode _oUnoNode, String _soReturnObjectDescription){
1158 //TODO try to use + _oUnoNode.getClassName() instead of the hack
1159 return "Sub Main(" + _soReturnObjectDescription + " as " + getanyTypeDescription(false) + ")";
1162 public boolean needsqueryInterface(){
1163 return false;
1166 public void assignqueryInterfaceHeaderSourceCode(){
1169 public String getMethodSeparator(){
1170 return ".";
1174 public String getqueryInterfaceSourceCode(String _sClassName, String _sReturnVariableName, String _sIncomingObjectName){
1175 return _sIncomingObjectName;
1179 public String getPropertyValueGetterSourceCode(String _sPropertyName, String _sReturnVariableName, String _sIncomingObjectName, TypeClass _aTypeClass, String _sTypeName){
1180 return "\t" + _sReturnVariableName + " = " + _sIncomingObjectName + "." + _sPropertyName;
1184 public String getStructSourceCode(String _sReturnVariableDescription, String _sObjectDescription, String _sMember){
1185 return getPropertyValueGetterSourceCode(_sMember, _sReturnVariableDescription, _sObjectDescription, null, "" );
1188 public String getConvertedSourceCodeValueOfObject(String _sReturnVariableName, String _sObjectDescription, TypeClass _aTypeClass, String _sTypeName){
1189 return _sReturnVariableName + " = " + _sObjectDescription;
1193 public String getObjectTypeDescription(String _sClassName, boolean _bAsHeader){
1194 return "Object";
1198 public String getMethodTerminationSourceCode(){
1199 return "\nEnd Sub\n";
1203 public String castLiteral(String _sExpression, TypeClass _aTypeClass){
1204 String sReturn = "";
1205 switch (_aTypeClass.getValue()){
1206 case TypeClass.BOOLEAN_value:
1207 case TypeClass.BYTE_value:
1208 case TypeClass.DOUBLE_value:
1209 case TypeClass.FLOAT_value:
1210 case TypeClass.UNSIGNED_SHORT_value:
1211 case TypeClass.SHORT_value:
1212 case TypeClass.LONG_value:
1213 case TypeClass.UNSIGNED_LONG_value:
1214 case TypeClass.HYPER_value:
1215 case TypeClass.UNSIGNED_HYPER_value:
1216 sReturn = _sExpression;
1217 break;
1218 case TypeClass.CHAR_value:
1219 case TypeClass.STRING_value:
1220 sReturn = "\"" +_sExpression + "\"";
1221 break;
1222 case TypeClass.ENUM_value:
1223 default:
1224 sReturn = _sExpression;
1225 System.out.println("Type " + _aTypeClass.getValue() + " not yet defined in 'castliteral()'");
1227 return sReturn;
1232 public String getbooleanTypeDescription(){
1233 return "Boolean";
1236 public String getbyteTypeDescription(){
1237 return "Integer";
1240 public String getshortTypeDescription(){
1241 return "Integer";
1244 public String getlongTypeDescription(){
1245 return "Integer";
1248 public String getunsignedlongTypeDescription(){
1249 return "Long";
1252 public String gethyperTypeDescription(){
1253 return "Long";
1256 public String getunsignedhyperTypeDescription(){
1257 return "Long";
1260 public String getfloatTypeDescription(){
1261 return "Double";
1264 public String getdoubleTypeDescription(){
1265 return "Double";
1268 public String getcharTypeDescription(){
1269 return "String";
1272 public String getstringTypeDescription(boolean _bAsHeaderSourceCode){
1273 if (_bAsHeaderSourceCode){
1274 return "";
1276 else{
1277 return "String";
1281 public String getanyTypeDescription(boolean _bAsHeaderSourceCode){
1282 if (_bAsHeaderSourceCode){
1283 return "";
1285 else{
1286 return "Object";
1290 public String getStatementTerminationCharacter(){
1291 return "";
1295 public String getVariableDeclaration(String _sTypeString, String _sVariableName, boolean bIsArray, TypeClass _aTypeClass, boolean _bInitialize){
1296 String sReturn = "";
1297 if (bIsArray){
1298 sReturn = "Dim " + _sVariableName + "() as " + _sTypeString + "\n\t" + _sVariableName;
1300 else{
1301 sReturn = "Dim " + _sVariableName + " as " + _sTypeString + "\n\t" + _sVariableName;
1303 return sReturn;
1307 public String getStringValue(String _sValue){
1308 return _sValue;
1312 public String getCommentSign(){
1313 return "'";
1318 private class CPlusPlusCodeGenerator implements XLanguageSourceCodeGenerator{
1320 private boolean bIncludeStringHeader = false;
1321 private boolean bIncludeAny = false;
1322 private boolean bIncludeSequenceHeader = false;
1324 public CPlusPlusCodeGenerator(){
1327 private String getCSSNameSpaceString(){
1328 return "css";
1331 public String getStatementTerminationCharacter(){
1332 return ";";
1336 public String getHeaderSourceCode(Object _oUnoObject, String _sClassName, TypeClass _aTypeClass){
1337 String sClassName = _sClassName;
1338 String sHeaderStatement = "";
1339 if (_oUnoObject != null){
1340 if (!Introspector.isObjectPrimitive(_oUnoObject)){
1341 if (Introspector.isObjectSequence(_oUnoObject)){
1342 XTypeDescription xTypeDescription = m_oIntrospector.getReferencedType(sClassName);
1343 if (xTypeDescription != null){
1344 if (!Introspector.isPrimitive(xTypeDescription.getTypeClass())){
1345 sClassName = getTypeString(xTypeDescription.getName(), xTypeDescription.getTypeClass(), true);
1347 // primitive Types are not supposed to turn up in the import section...
1348 else{
1349 sClassName = "";
1353 else{
1354 sClassName = getTypeString(_sClassName, _aTypeClass, true);
1356 if (!sClassName.equals("")){
1357 sHeaderStatement = getHeaderOfClass(sClassName);
1361 return sHeaderStatement;
1366 public String getFinalHeaderStatements(){
1367 String sReturn = "";
1368 sReturn += "\nnamespace " + getCSSNameSpaceString() + " = com::sun::star;\n";
1369 sReturn += "using ::rtl::OUString;\n";
1370 return sReturn;
1374 private String getHeaderOfClass(String _sClassName){
1375 return "#include \"" + _sClassName.replace('.', '/') + ".hpp\"\n";
1380 public void assignqueryInterfaceHeaderSourceCode(){
1381 sHeaderStatements.add("#include \"sal/config.h\"\n");
1382 sHeaderStatements.add("#include \"sal/types.h\"\n");
1383 if (bIncludeStringHeader){
1384 sHeaderStatements.add("#include \"rtl/ustring.hxx\"\n");
1386 sHeaderStatements.add("#include \"com/sun/star/uno/Reference.hxx\"\n");
1387 if (bIncludeSequenceHeader){
1388 sHeaderStatements.add("#include \"com/sun/star/uno/Sequence.hxx\"\n");
1390 sHeaderStatements.add(getHeaderOfClass("com.sun.star.uno.XInterface"));
1391 if (bIncludeAny){
1392 sHeaderStatements.add(getHeaderOfClass("com.sun.star.uno.Any"));
1397 public String getMainMethodSignatureSourceCode(XUnoNode _oUnoNode, String _soReturnObjectDescription){
1398 String sReturn = "";
1399 sReturn = "void codesnippet(const " + getCSSNameSpaceString() + "::uno::Reference<" + getCSSNameSpaceString() + "::uno::XInterface>& " + _soReturnObjectDescription + " ){";
1400 int a = 0;
1401 if (!sExceptions.contains("com.sun.star.uno.RuntimeException")){
1402 sExceptions.add("com.sun.star.uno.RuntimeException");
1404 if (baddExceptionHandling){
1405 sReturn += "\n//throw ";
1406 for (int i = 0; i < sExceptions.size(); i++){
1407 String sCurException = sExceptions.get(i);
1408 if (sReturn.indexOf(sCurException) == -1){
1409 if (a++ > 0){
1410 sReturn += ", ";
1412 sReturn += getObjectTypeDescription(sCurException, false);
1418 sReturn += "{";
1419 return sReturn;
1423 public boolean needsqueryInterface(){
1424 return true;
1428 public String getqueryInterfaceSourceCode(String _sClassName, String _sReturnVariableName, String _sIncomingObjectName){
1429 return "\t" + _sReturnVariableName + "( " + _sIncomingObjectName + ", " + getCSSNameSpaceString() + "::uno::UNO_QUERY_THROW);\n";
1433 public String getPropertyValueGetterSourceCode(String _sPropertyName, String _sReturnVariableName, String _sIncomingObjectName, TypeClass _aTypeClass, String _sTypeName){
1434 String sFirstLine = "\t";
1435 String sReturnVariableName = _sReturnVariableName;
1436 // e.g. uno::Any a = xPropSet->getPropertyValue( rtl::OUString( "DefaultContext" ) );
1437 String[] sVarDefinition = _sReturnVariableName.split("=");
1438 if (sVarDefinition.length > 0){
1439 String sVariable = sVarDefinition[0];
1440 String[] sVarDeclaration = sVariable.split(" ");
1441 if (sVarDeclaration.length > 0){
1442 sFirstLine += sReturnVariableName + ";\n";
1443 sReturnVariableName = sVarDeclaration[sVarDeclaration.length-1];
1446 String sObjectDescription = _sIncomingObjectName + "->getPropertyValue(" + getStringValue(_sPropertyName) + ")";
1447 String sSecondLine = "\t" + getConvertedSourceCodeValueOfObject(sReturnVariableName, sObjectDescription, _aTypeClass, _sTypeName) + ";";
1448 return sFirstLine + sSecondLine;
1452 public String getStructSourceCode(String _sReturnVariableDescription, String _sObjectDescription, String _sMember){
1453 return "\t" + _sReturnVariableDescription + " = " + _sObjectDescription + "->" + _sMember + ";";
1457 public String getConvertedSourceCodeValueOfObject(String _sReturnVariableName, String _sObjectDescription, TypeClass _aTypeClass, String _sTypeName){
1458 return _sObjectDescription + " >>= " + _sReturnVariableName;
1462 public String getStringValue(String _sValue){
1463 bIncludeStringHeader = true;
1464 return "OUString(\"" + _sValue + "\")";
1468 public String getObjectTypeDescription(String _sClassName, boolean _bAsHeader){
1469 String sReturn = "";
1470 if (_bAsHeader){
1471 sReturn = _sClassName.replace('.', '/');
1473 else{
1474 String sModuleName = Introspector.getModuleName(_sClassName);
1475 sModuleName = Introspector.getShortClassName(sModuleName);
1476 sReturn = getCSSNameSpaceString() + "::" + sModuleName + "::" + Introspector.getShortClassName(_sClassName);
1478 return sReturn;
1482 public String getMethodTerminationSourceCode(){
1483 return "\n}";
1486 public String getMethodSeparator(){
1487 return "->";
1491 public String castLiteral(String _sExpression, TypeClass _aTypeClass){
1492 String sReturn = "";
1493 switch (_aTypeClass.getValue()){
1494 case TypeClass.BOOLEAN_value:
1495 case TypeClass.BYTE_value:
1496 case TypeClass.DOUBLE_value:
1497 case TypeClass.FLOAT_value:
1498 case TypeClass.UNSIGNED_SHORT_value:
1499 case TypeClass.SHORT_value:
1500 case TypeClass.LONG_value:
1501 case TypeClass.UNSIGNED_LONG_value:
1502 case TypeClass.HYPER_value:
1503 case TypeClass.UNSIGNED_HYPER_value:
1504 sReturn = _sExpression;
1505 break;
1506 case TypeClass.CHAR_value:
1507 sReturn = "'" + _sExpression + "'";
1508 break;
1509 case TypeClass.STRING_value:
1510 sReturn = getStringValue(_sExpression);
1511 break;
1512 case TypeClass.ENUM_value:
1513 default:
1514 sReturn = _sExpression;
1515 System.out.println("Type " + _aTypeClass.getValue() + " not yet defined in 'castliteral()'");
1517 return sReturn;
1520 public String getbooleanTypeDescription(){
1521 return "sal_Bool";
1524 public String getbyteTypeDescription(){
1525 return "sal_Int8";
1528 public String getshortTypeDescription(){
1529 return "sal_Int16";
1532 public String getlongTypeDescription(){
1533 return "sal_Int32";
1536 public String getunsignedlongTypeDescription(){
1537 return "sal_uInt32";
1540 public String gethyperTypeDescription(){
1541 return "sal_Int64";
1544 public String getunsignedhyperTypeDescription(){
1545 return "sal_uInt64";
1548 public String getfloatTypeDescription(){
1549 return "float";
1552 public String getdoubleTypeDescription(){
1553 return "double";
1556 public String getcharTypeDescription(){
1557 return "sal_Unicode";
1560 public String getstringTypeDescription(boolean _bAsHeaderSourceCode){
1561 bIncludeStringHeader = true;
1562 if (_bAsHeaderSourceCode){
1563 return "";
1565 else{
1566 return "OUString";
1570 public String getanyTypeDescription(boolean _bAsHeaderSourceCode){
1571 if (_bAsHeaderSourceCode){
1572 return "com/sun/star/uno/XInterface";
1574 else{
1575 return "XInterface";
1580 public String getVariableDeclaration(String _sTypeString, String _sVariableName, boolean bIsArray, TypeClass _aTypeClass, boolean _bInitialize){
1581 boolean bIsPrimitive = Introspector.isPrimitive(_aTypeClass);
1583 String sReturn = "";
1584 if (bIsArray){
1585 bIncludeSequenceHeader = true;
1586 sReturn = getCSSNameSpaceString() + "::uno::Sequence<" + _sTypeString + "> " + _sVariableName;
1588 else{
1589 if (bIsPrimitive){
1590 sReturn = _sTypeString + " " + _sVariableName;
1591 if (_bInitialize){
1592 switch (_aTypeClass.getValue()){
1593 case TypeClass.BOOLEAN_value:
1594 sReturn = sReturn + " = false";
1595 break;
1596 case TypeClass.BYTE_value:
1597 case TypeClass.UNSIGNED_SHORT_value:
1598 case TypeClass.SHORT_value:
1599 case TypeClass.LONG_value:
1600 case TypeClass.UNSIGNED_LONG_value:
1601 case TypeClass.HYPER_value:
1602 case TypeClass.UNSIGNED_HYPER_value:
1603 sReturn = sReturn + " = 0";
1604 break;
1605 case TypeClass.DOUBLE_value:
1606 case TypeClass.FLOAT_value:
1607 sReturn = sReturn + " = 0.0";
1608 break;
1609 case TypeClass.CHAR_value:
1610 sReturn = sReturn + "'0'";
1611 break;
1612 case TypeClass.STRING_value:
1613 sReturn = _sTypeString + " " + _sVariableName;
1614 break;
1615 default:
1616 sReturn = _sTypeString + " " + _sVariableName;
1617 System.out.println("Type " + _aTypeClass.getValue() + " not yet defined in 'getVariableDeclaration()'");
1621 else{
1622 sReturn = getCSSNameSpaceString() + "::uno::Reference<" + _sTypeString + "> " +_sVariableName;
1625 return sReturn;
1628 public String getCommentSign(){
1629 return "//";
1635 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */