1 /************************************************************************* 2 * 3 * The Contents of this file are made available subject to the terms of 4 * the BSD license. 5 * 6 * Copyright 2000, 2010 Oracle and/or its affiliates. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 *************************************************************************/ 34 35 import com.sun.star.beans.UnknownPropertyException; 36 import com.sun.star.beans.XPropertySet; 37 import com.sun.star.container.XNameAccess; 38 import com.sun.star.lang.Locale; 39 import com.sun.star.lang.WrappedTargetException; 40 import com.sun.star.lang.XServiceInfo; 41 import com.sun.star.reflection.ParamInfo; 42 import com.sun.star.reflection.XIdlClass; 43 import com.sun.star.reflection.XIdlMethod; 44 import com.sun.star.reflection.XTypeDescription; 45 import com.sun.star.text.XTextTablesSupplier; 46 import com.sun.star.uno.Any; 47 import com.sun.star.uno.AnyConverter; 48 import com.sun.star.uno.Type; 49 import com.sun.star.uno.TypeClass; 50 import com.sun.star.uno.UnoRuntime; 51 import java.util.Enumeration; 52 import java.util.Hashtable; 53 import java.util.Vector; 54 55 56 57 public class SourceCodeGenerator { 58 private Vector sExceptions = new Vector(); 59 Vector sHeaderStatements = new HeaderStatements(); 60 private XLanguageSourceCodeGenerator m_xLanguageSourceCodeGenerator; 61 private String sHeaderCode = ""; 62 private String sStatementCode = ""; 63 private String sMainMethodSignature = ""; 64 65 private Hashtable aVariables = new Hashtable(); 66 private final String SSUFFIXSEPARATOR = "_"; 67 private final String SVARIABLENAME = "VariableName"; 68 private final String SARRAYVARIABLENAME = "VariableNameList"; 69 private final String SUNOOBJECTNAME = "oUnobject"; 70 private final String SUNOSTRUCTNAME = "aUnoStruct"; 71 private Introspector m_oIntrospector; 72 private Vector aTreepathProviders = new Vector(); 73 private XTreePathProvider xTreepathProvider = null; 74 private boolean baddExceptionHandling = false; 75 private boolean bXPropertySetExceptionsAreAdded = false; 76 private XUnoNode oInitialUnoNode = null; 77 private final String sINITIALVARIABLENAME = "_oUnoEntryObject"; 78 79 80 /** Creates a new instance of SourceCodeGenerator */ 81 public SourceCodeGenerator(int _nLanguage) { 82 this.setLanguage(_nLanguage); 83 m_oIntrospector = Introspector.getIntrospector(); 84 } 85 86 87 public String addSourceCodeOfUnoObject(XTreePathProvider _xTreepathProvider, boolean _brememberPath, boolean _bAddMethodSignature, boolean _baddHeader){ 88 String sSourceCode = ""; 89 String sVariableName = ""; 90 if (_xTreepathProvider != null) { 91 for (int i = 0; i < _xTreepathProvider.getPathCount(); i++){ 92 XUnoNode oUnoNode = _xTreepathProvider.getPathComponent(i); 93 if (i == 0){ 94 sVariableName = sINITIALVARIABLENAME; 95 oInitialUnoNode = oUnoNode; 96 } 97 else{ 98 if (oUnoNode instanceof XUnoMethodNode){ 99 XUnoMethodNode oUnoMethodNode = (XUnoMethodNode) oUnoNode; 100 if (oUnoMethodNode.isInvoked()){ 101 UnoObjectDefinition oUnoReturnObjectDefinition = getUnoObjectDefinition(_xTreepathProvider, oUnoMethodNode, i); 102 if (!isVariableDeclared(oUnoReturnObjectDefinition, this.generateVariableNameFromMethod(oUnoMethodNode.getXIdlMethod()))){ 103 // sStatementCode += "\n"; 104 sStatementCode += "\n" + getMethodStatementSourceCode(oUnoMethodNode, sVariableName, oUnoReturnObjectDefinition); 105 } 106 sVariableName = oUnoReturnObjectDefinition.getVariableName(); 107 } 108 } 109 else if (oUnoNode instanceof XUnoPropertyNode){ 110 XUnoPropertyNode oUnoPropertyNode = (XUnoPropertyNode) oUnoNode; 111 Any oReturnObject = com.sun.star.uno.Any.complete(oUnoPropertyNode.getUnoReturnObject()); 112 UnoObjectDefinition oUnoReturnObjectDefinition = new UnoObjectDefinition(oReturnObject); 113 if (!isVariableDeclared(oUnoReturnObjectDefinition, oUnoPropertyNode.getProperty().Name)){ 114 // sStatementCode += "\n"; 115 sStatementCode += "\n" + getPropertyStatementSourceCode(oUnoPropertyNode, sVariableName, oUnoReturnObjectDefinition); 116 } 117 sVariableName = oUnoReturnObjectDefinition.getVariableName(); 118 } 119 } 120 } 121 } 122 String sCompleteCode = combineCompleteSourceCode(sMainMethodSignature, _baddHeader); 123 xTreepathProvider = _xTreepathProvider; 124 if (_brememberPath){ 125 aTreepathProviders.add(_xTreepathProvider); 126 } 127 return sCompleteCode; 128 } 129 130 131 private void setLanguage(int _nLanguage){ 132 XLanguageSourceCodeGenerator xLanguageSourceCodeGenerator = null; 133 switch(_nLanguage){ 134 case XLanguageSourceCodeGenerator.nJAVA: 135 xLanguageSourceCodeGenerator = new JavaCodeGenerator(); 136 break; 137 case XLanguageSourceCodeGenerator.nCPLUSPLUS: 138 xLanguageSourceCodeGenerator = new CPlusPlusCodeGenerator(); 139 break; 140 case XLanguageSourceCodeGenerator.nBASIC: 141 xLanguageSourceCodeGenerator = new BasicCodeGenerator(); 142 break; 143 default: 144 System.out.println("Unknown Sourcecode Language. Check Menus!"); 145 } 146 if (xLanguageSourceCodeGenerator != null){ 147 m_xLanguageSourceCodeGenerator = xLanguageSourceCodeGenerator; 148 } 149 } 150 151 private void resetSourceCodeGeneration(int _nLanguage){ 152 aVariables.clear(); 153 this.sHeaderStatements.clear(); 154 setLanguage(_nLanguage); 155 String sHeaderCode = ""; 156 sStatementCode = ""; 157 } 158 159 private String generateVariableNameFromMethod(String _sMethodName, String _sPrefix, boolean _bConsiderAll){ 160 String sReturn = ""; 161 if (_sMethodName.startsWith(_sPrefix)){ 162 int nPrefixLength = _sPrefix.length(); 163 if (_sMethodName.length() > nPrefixLength){ 164 String sChar = _sMethodName.substring(nPrefixLength, nPrefixLength + 1); 165 String sUpperChar = sChar.toUpperCase(); 166 if (sUpperChar.equals(sChar)){ 167 if (_bConsiderAll){ 168 sReturn = _sMethodName; 169 } 170 else{ 171 sReturn = _sMethodName.substring(nPrefixLength, _sMethodName.length()); 172 } 173 } 174 } 175 } 176 return sReturn; 177 } 178 179 180 private String generateVariableNameFromMethod(XIdlMethod _xIdlMethod){ 181 // todo: refactor this!!! 182 String sMethodName = _xIdlMethod.getName(); 183 String sReturn = ""; 184 sReturn = generateVariableNameFromMethod(sMethodName, "getBy", false); 185 if (sReturn.equals("")){ 186 sReturn = generateVariableNameFromMethod(sMethodName, "get", false); 187 } 188 if (sReturn.equals("")){ 189 sReturn = generateVariableNameFromMethod(sMethodName, "attach", false); 190 } 191 if (sReturn.equals("")){ 192 sReturn = generateVariableNameFromMethod(sMethodName, "assign", false); 193 } 194 if (sReturn.equals("")){ 195 sReturn = generateVariableNameFromMethod(sMethodName, "attach", false); 196 } 197 if (sReturn.equals("")){ 198 sReturn = generateVariableNameFromMethod(sMethodName, "create", false); 199 } 200 if (sReturn.equals("")){ 201 sReturn = generateVariableNameFromMethod(sMethodName, "is", true); 202 } 203 if (sReturn.equals("")){ 204 sReturn = generateVariableNameFromMethod(sMethodName, "has", true); 205 } 206 if (sReturn.equals("")){ 207 sReturn = sMethodName; 208 } 209 return sReturn; 210 } 211 212 213 public String convertAllUnoObjects(int _nLanguage){ 214 String sSourceCode = ""; 215 resetSourceCodeGeneration(_nLanguage); 216 int ncount = aTreepathProviders.size(); 217 for (int i=0; i< ncount; i++){ 218 sSourceCode = addSourceCodeOfUnoObject((XTreePathProvider) aTreepathProviders.get(i), false, (i==0), (i == (ncount-1))); 219 } 220 return sSourceCode; 221 } 222 223 private UnoObjectDefinition getUnoObjectDefinition(XTreePathProvider _xTreePathProvider, XUnoMethodNode _oUnoMethodNode, int _nindex){ 224 XUnoNode oUnoNode = null; 225 Object oUnoReturnObject = null; 226 Object[] oParamObjects = null; 227 XIdlClass xIdlClass = _oUnoMethodNode.getXIdlMethod().getReturnType(); 228 String sTypeName = xIdlClass.getName(); 229 TypeClass aTypeClass = xIdlClass.getTypeClass(); 230 if (aTypeClass.getValue() != TypeClass.VOID_value){ 231 if (_xTreePathProvider.getPathCount() > _nindex + 1){ 232 oUnoNode = _xTreePathProvider.getPathComponent(_nindex + 1); 233 oUnoReturnObject = oUnoNode.getUnoObject(); 234 } 235 } 236 if (oUnoReturnObject == null){ 237 oUnoReturnObject = _oUnoMethodNode.getLastUnoReturnObject(); 238 } 239 UnoObjectDefinition oUnoObjectDefinition = new UnoObjectDefinition(oUnoReturnObject, sTypeName, aTypeClass); 240 if (_oUnoMethodNode.hasParameters()){ 241 if (oUnoNode != null){ 242 oParamObjects = oUnoNode.getParameterObjects(); 243 } 244 else{ 245 oParamObjects = _oUnoMethodNode.getLastParameterObjects(); 246 } 247 } 248 if (oParamObjects != null){ 249 oUnoObjectDefinition.addParameterObjects(oParamObjects); 250 } 251 return oUnoObjectDefinition; 252 } 253 254 255 private String combineCompleteSourceCode(String _sMethodSignature, boolean _bAddHeader){ 256 String sCompleteCode = ""; 257 if (_bAddHeader){ 258 sMainMethodSignature = m_xLanguageSourceCodeGenerator.getMainMethodSignatureSourceCode(oInitialUnoNode, sINITIALVARIABLENAME); 259 m_xLanguageSourceCodeGenerator.assignqueryInterfaceHeaderSourceCode(); 260 sCompleteCode += getHeaderSourceCode(); 261 } 262 sCompleteCode += sMainMethodSignature; 263 sCompleteCode += sStatementCode; 264 if (_bAddHeader){ 265 sCompleteCode += m_xLanguageSourceCodeGenerator.getMethodTerminationSourceCode(); 266 sCompleteCode += "\n" + m_xLanguageSourceCodeGenerator.getCommentSign() + "..."; 267 } 268 return sCompleteCode; 269 } 270 271 272 public String getPropertyStatementSourceCode(XUnoPropertyNode _oUnoPropertyNode, String _sVariableName, UnoObjectDefinition _oUnoReturnObjectDefinition){ 273 String sReturnObjectVariableDefinition = ""; 274 String sStatement = ""; 275 String sPropertyName = _oUnoPropertyNode.getProperty().Name; 276 UnoObjectDefinition oUnoObjectDefinition = new UnoObjectDefinition(_oUnoPropertyNode.getUnoObject(), "com.sun.star.beans.XPropertySet"); 277 if (!m_xLanguageSourceCodeGenerator.needsqueryInterface() || (oUnoObjectDefinition.getTypeClass().getValue() == TypeClass.STRUCT_value)){ 278 oUnoObjectDefinition.setVariableName(_sVariableName); 279 if (oUnoObjectDefinition.getTypeClass().getValue() == TypeClass.STRUCT_value){ 280 sReturnObjectVariableDefinition = getVariableInitialization(_oUnoReturnObjectDefinition, false); 281 sStatement += m_xLanguageSourceCodeGenerator.getStructSourceCode(sReturnObjectVariableDefinition, oUnoObjectDefinition.getVariableName(), sPropertyName); 282 return sStatement; 283 } 284 } 285 sStatement += addQueryInterfaceSourceCode(oUnoObjectDefinition, _sVariableName, "com.sun.star.beans.XPropertySet"); 286 if (_oUnoReturnObjectDefinition.getTypeClass().getValue() != TypeClass.VOID_value){ 287 sReturnObjectVariableDefinition = getVariableInitialization(_oUnoReturnObjectDefinition, true); 288 } 289 sStatement += m_xLanguageSourceCodeGenerator.getPropertyValueGetterSourceCode(sPropertyName, sReturnObjectVariableDefinition, oUnoObjectDefinition.getVariableName(), _oUnoReturnObjectDefinition.getTypeClass(), _oUnoReturnObjectDefinition.getTypeName()); 290 addXPropertySetRelatedExceptions(); 291 return sStatement; 292 } 293 294 295 public String getMethodStatementSourceCode(XUnoMethodNode _oUnoMethodNode, String _sVariableName, UnoObjectDefinition _oUnoReturnObjectDefinition){ 296 String sReturnObjectVariableDefinition = ""; 297 String sStatement = ""; 298 XIdlMethod xIdlMethod = _oUnoMethodNode.getXIdlMethod(); 299 TypeClass aReturnTypeClass = xIdlMethod.getReturnType().getTypeClass(); 300 UnoObjectDefinition oUnoObjectDefinition = new UnoObjectDefinition(_oUnoMethodNode.getUnoObject(), _oUnoMethodNode.getClassName()); 301 String sVariableStemName = this.generateVariableNameFromMethod(xIdlMethod); 302 sStatement += addQueryInterfaceSourceCode(oUnoObjectDefinition, _sVariableName, oUnoObjectDefinition.getTypeName()); 303 if (_oUnoReturnObjectDefinition.getTypeClass().getValue() != TypeClass.VOID_value){ 304 sReturnObjectVariableDefinition = getVariableInitialization(_oUnoReturnObjectDefinition, false) + " = "; 305 } 306 Object[] oParamObjects = _oUnoReturnObjectDefinition.getParameterObjects(); 307 String sParameterCode = getMethodParameterValueDescription(_oUnoMethodNode, oParamObjects, false); 308 String sSeparator = m_xLanguageSourceCodeGenerator.getMethodSeparator(); 309 sStatement += "\t" + sReturnObjectVariableDefinition + oUnoObjectDefinition.getVariableName() + sSeparator + xIdlMethod.getName() + "(" + sParameterCode + ")"; 310 sStatement += m_xLanguageSourceCodeGenerator.getStatementTerminationCharacter(); 311 addExceptions(xIdlMethod); 312 return sStatement; 313 } 314 315 316 private String addQueryInterfaceSourceCode(UnoObjectDefinition _oUnoObjectDefinition, String _sVariableName, String _sTypeName){ 317 String sLocStatement = ""; 318 if (m_xLanguageSourceCodeGenerator.needsqueryInterface()){ 319 if (!isVariableDeclared(_oUnoObjectDefinition, "")){ 320 String sObjectVariableDefinition = getVariableDeclaration(_oUnoObjectDefinition, false, ""); 321 sLocStatement += m_xLanguageSourceCodeGenerator.getqueryInterfaceSourceCode(_sTypeName, sObjectVariableDefinition, _sVariableName); 322 } 323 } 324 else{ 325 _oUnoObjectDefinition.setVariableName(_sVariableName); 326 } 327 return sLocStatement; 328 } 329 330 331 private void addXPropertySetRelatedExceptions(){ 332 if (!bXPropertySetExceptionsAreAdded){ 333 sExceptions.add("com.sun.star.beans.UnknownPropertyException"); 334 sExceptions.add("com.sun.star.lang.WrappedTargetException"); 335 sExceptions.add("com.sun.star.lang.IllegalArgumentException"); 336 bXPropertySetExceptionsAreAdded = true; 337 baddExceptionHandling = true; 338 } 339 } 340 341 342 private void addExceptions(XIdlMethod _xIdlMethod){ 343 XIdlClass[] xIdlClasses = _xIdlMethod.getExceptionTypes(); 344 for (int i = 0; i > xIdlClasses.length; i++){ 345 sExceptions.add(xIdlClasses[0].getName()); 346 baddExceptionHandling = true; 347 } 348 } 349 350 private String getRootDescription(XUnoNode _oUnoNode){ 351 return "_o" + _oUnoNode.toString(); 352 } 353 354 355 private String getHeaderSourceCode(){ 356 Enumeration aEnumeration = aVariables.elements(); 357 while(aEnumeration.hasMoreElements()){ 358 UnoObjectDefinition oUnoObjectDefinition = (UnoObjectDefinition) aEnumeration.nextElement(); 359 String sCurHeaderStatement = m_xLanguageSourceCodeGenerator.getHeaderSourceCode(oUnoObjectDefinition.getUnoObject(), oUnoObjectDefinition.getTypeName(), oUnoObjectDefinition.getTypeClass()); 360 sHeaderStatements.add(sCurHeaderStatement); 361 } 362 String sHeaderSourcecode = ""; 363 String[] sHeaderStatementArray = new String[sHeaderStatements.size()]; 364 sHeaderStatements.toArray(sHeaderStatementArray); 365 java.util.Arrays.sort(sHeaderStatementArray); 366 for (int i = 0; i < sHeaderStatementArray.length; i++){ 367 sHeaderSourcecode += sHeaderStatementArray[i]; 368 } 369 sHeaderSourcecode += m_xLanguageSourceCodeGenerator.getFinalHeaderStatements(); 370 return sHeaderSourcecode +"\n" + m_xLanguageSourceCodeGenerator.getCommentSign() + "...\n"; 371 } 372 373 374 private class HeaderStatements extends Vector{ 375 376 public boolean contains(Object _oElement){ 377 String sCompName = (String) _oElement; 378 for (int i = 0; i < this.size(); i++){ 379 String sElement = (String) this.get(i); 380 if (sElement.equals(sCompName)){ 381 return true; 382 } 383 } 384 return false; 385 } 386 387 388 public boolean add(Object _oElement){ 389 if (_oElement instanceof String){ 390 if (!contains(_oElement)){ 391 super.add(_oElement); 392 return true; 393 } 394 } 395 return false; 396 } 397 } 398 399 400 private boolean isVariableDeclared(UnoObjectDefinition _oUnoObjectDefinition, String _sDefaultStemName){ 401 boolean bisDeclared = false; 402 if (!_sDefaultStemName.equals("")){ 403 _oUnoObjectDefinition.setCentralVariableStemName(_sDefaultStemName); 404 } 405 String sVariableStemName = _oUnoObjectDefinition.getVariableStemName(); 406 bisDeclared = aVariables.containsKey(sVariableStemName); 407 if (bisDeclared){ 408 Object oUnoObject = _oUnoObjectDefinition.getUnoObject(); 409 if (m_oIntrospector.isObjectPrimitive(oUnoObject)){ 410 bisDeclared = false; 411 } 412 else if (m_oIntrospector.isObjectSequence(oUnoObject)){ 413 bisDeclared = false; 414 } 415 else{ 416 String sCompVariableName = sVariableStemName; 417 String sUnoObjectIdentity = oUnoObject.toString(); 418 boolean bleaveloop = false; 419 int a = 2; 420 while (!bleaveloop){ 421 if (aVariables.containsKey(sCompVariableName)){ 422 Object oUnoCompObject = ((UnoObjectDefinition) aVariables.get(sCompVariableName)).getUnoObject(); 423 String sUnoCompObjectIdentity = oUnoCompObject.toString(); 424 bleaveloop = sUnoCompObjectIdentity.equals(sUnoObjectIdentity); 425 bisDeclared = bleaveloop; 426 if (!bleaveloop){ 427 sCompVariableName = sVariableStemName + SSUFFIXSEPARATOR + a++; 428 } 429 } 430 else{ 431 bleaveloop = true; 432 bisDeclared = false; 433 } 434 } 435 } 436 } 437 return bisDeclared; 438 } 439 440 441 private String addUniqueVariableName(String _sStemVariableName, UnoObjectDefinition _oUnoObjectDefinition){ 442 boolean bElementexists = true; 443 int a = 2; 444 String sCompName = _sStemVariableName; 445 while (bElementexists){ 446 if (! aVariables.containsKey(sCompName)){ 447 aVariables.put(sCompName, _oUnoObjectDefinition); 448 break; 449 } 450 sCompName = _sStemVariableName + SSUFFIXSEPARATOR + a++; 451 } 452 return sCompName; 453 } 454 455 456 457 private String getTypeString(String _sTypeName, TypeClass _aTypeClass, boolean _bAsHeaderSourceCode){ 458 String sTypeString = ""; 459 switch (_aTypeClass.getValue()){ 460 case TypeClass.BOOLEAN_value: 461 sTypeString = m_xLanguageSourceCodeGenerator.getbooleanTypeDescription(); 462 break; 463 case TypeClass.BYTE_value: 464 sTypeString = m_xLanguageSourceCodeGenerator.getbyteTypeDescription(); 465 break; 466 case TypeClass.CHAR_value: 467 sTypeString = m_xLanguageSourceCodeGenerator.getcharTypeDescription(); 468 break; 469 case TypeClass.DOUBLE_value: 470 sTypeString = m_xLanguageSourceCodeGenerator.getdoubleTypeDescription(); 471 break; 472 case TypeClass.FLOAT_value: 473 sTypeString = m_xLanguageSourceCodeGenerator.getfloatTypeDescription(); 474 break; 475 case TypeClass.HYPER_value: 476 sTypeString = m_xLanguageSourceCodeGenerator.gethyperTypeDescription(); 477 break; 478 case TypeClass.LONG_value: 479 sTypeString = m_xLanguageSourceCodeGenerator.getlongTypeDescription(); 480 break; 481 case TypeClass.SHORT_value: 482 sTypeString = m_xLanguageSourceCodeGenerator.getshortTypeDescription(); 483 break; 484 case TypeClass.STRING_value: 485 sTypeString = m_xLanguageSourceCodeGenerator.getstringTypeDescription(_bAsHeaderSourceCode); 486 break; 487 case TypeClass.UNSIGNED_HYPER_value: 488 sTypeString = m_xLanguageSourceCodeGenerator.getunsignedhyperTypeDescription(); 489 break; 490 case TypeClass.UNSIGNED_LONG_value: 491 sTypeString = m_xLanguageSourceCodeGenerator.getunsignedlongTypeDescription(); 492 break; 493 case TypeClass.UNSIGNED_SHORT_value: 494 sTypeString = m_xLanguageSourceCodeGenerator.getdoubleTypeDescription(); 495 break; 496 case TypeClass.SEQUENCE_value: 497 //TODO consider mulitdimensional Arrays 498 XTypeDescription xTypeDescription = Introspector.getIntrospector().getReferencedType(_sTypeName); 499 if (xTypeDescription != null){ 500 sTypeString = getTypeString(xTypeDescription.getName(), xTypeDescription.getTypeClass(), _bAsHeaderSourceCode); 501 } 502 break; 503 case TypeClass.ANY_value: 504 sTypeString = m_xLanguageSourceCodeGenerator.getanyTypeDescription(_bAsHeaderSourceCode); 505 break; 506 case TypeClass.TYPE_value: 507 sTypeString = m_xLanguageSourceCodeGenerator.getObjectTypeDescription("com.sun.star.uno.Type", _bAsHeaderSourceCode); 508 break; 509 case TypeClass.ENUM_value: 510 case TypeClass.STRUCT_value: 511 case TypeClass.INTERFACE_ATTRIBUTE_value: 512 case TypeClass.INTERFACE_METHOD_value: 513 case TypeClass.INTERFACE_value: 514 case TypeClass.PROPERTY_value: 515 sTypeString = m_xLanguageSourceCodeGenerator.getObjectTypeDescription(_sTypeName, _bAsHeaderSourceCode); 516 break; 517 default: 518 } 519 return sTypeString; 520 } 521 522 523 private String getVariableDeclaration(UnoObjectDefinition _oUnoObjectDefinition, boolean _bInitialize, String _sVariableDefaultName){ 524 TypeClass aTypeClass = _oUnoObjectDefinition.getTypeClass(); 525 TypeClass aLocTypeClass = aTypeClass; 526 boolean bIsArray = false; 527 if (_oUnoObjectDefinition.getUnoObject() != null){ 528 bIsArray = m_oIntrospector.isObjectSequence(_oUnoObjectDefinition.getUnoObject()); 529 } 530 else{ 531 bIsArray = _oUnoObjectDefinition.getTypeClass().getValue() == TypeClass.SEQUENCE_value; 532 } 533 String sVariableName = _oUnoObjectDefinition.getVariableName(_sVariableDefaultName); 534 String sTypeName = _oUnoObjectDefinition.getTypeName(); 535 String sTypeString = getTypeString(sTypeName, aLocTypeClass, false); 536 if (bIsArray){ 537 XTypeDescription xTypeDescription = Introspector.getIntrospector().getReferencedType(sTypeName); 538 if (xTypeDescription != null){ 539 aLocTypeClass = xTypeDescription.getTypeClass(); 540 } 541 } 542 String sVariableDeclaration = m_xLanguageSourceCodeGenerator.getVariableDeclaration(sTypeString, sVariableName, bIsArray, aLocTypeClass, _bInitialize); 543 addUniqueVariableName(sVariableName, _oUnoObjectDefinition); 544 return sVariableDeclaration; 545 } 546 547 548 public String getVariableInitialization(UnoObjectDefinition _oUnoObjectDefinition, boolean _bInitialize){ 549 String sObjectVariableDeclaration = ""; 550 String sVariableName = _oUnoObjectDefinition.getVariableName(); 551 if (isVariableDeclared(_oUnoObjectDefinition, "")){ 552 sObjectVariableDeclaration = sVariableName; 553 } 554 else{ 555 sObjectVariableDeclaration = getVariableDeclaration(_oUnoObjectDefinition, _bInitialize, ""); 556 } 557 return sObjectVariableDeclaration; 558 } 559 560 561 562 public String getVariableNameforUnoObject(String _sShortClassName){ 563 if (_sShortClassName.startsWith("X")){ 564 return "x" + _sShortClassName.substring(1); 565 } 566 else{ 567 return _sShortClassName; 568 } 569 } 570 571 572 class UnoObjectDefinition{ 573 Object m_oUnoObject = null; 574 Type aType = null; 575 String sVariableStemName = ""; 576 String m_sCentralVariableStemName = ""; 577 String sVariableName = ""; 578 String m_sTypeName = ""; 579 TypeClass m_aTypeClass = null; 580 Object[] m_oParameterObjects = null; 581 582 583 public UnoObjectDefinition(Any _oUnoObject){ 584 m_sTypeName = _oUnoObject.getType().getTypeName(); 585 m_aTypeClass = _oUnoObject.getType().getTypeClass(); 586 m_oUnoObject = _oUnoObject; 587 m_sCentralVariableStemName = getCentralVariableStemName(m_aTypeClass); 588 } 589 590 591 public UnoObjectDefinition(Object _oUnoObject, String _sTypeName, TypeClass _aTypeClass){ 592 m_oUnoObject = _oUnoObject; 593 m_sTypeName = _sTypeName; 594 m_aTypeClass = _aTypeClass; 595 m_sCentralVariableStemName = getCentralVariableStemName(m_aTypeClass); 596 } 597 598 599 public UnoObjectDefinition(Object _oUnoObject, String _sTypeName){ 600 m_oUnoObject = _oUnoObject; 601 m_sTypeName = _sTypeName; 602 m_aTypeClass = AnyConverter.getType(_oUnoObject).getTypeClass(); 603 m_sCentralVariableStemName = getCentralVariableStemName(m_aTypeClass); 604 } 605 606 607 private String getCentralVariableStemName(TypeClass _aTypeClass){ 608 String sCentralVariableStemName = ""; 609 int nTypeClass = _aTypeClass.getValue(); 610 switch(nTypeClass){ 611 case TypeClass.SEQUENCE_value: 612 //TODO consider mulitdimensional Arrays 613 XTypeDescription xTypeDescription = Introspector.getIntrospector().getReferencedType(getTypeName()); 614 if (xTypeDescription != null){ 615 sCentralVariableStemName = getCentralVariableStemName(xTypeDescription.getTypeClass()); 616 } 617 break; 618 case TypeClass.TYPE_value: 619 sCentralVariableStemName = SVARIABLENAME; 620 break; 621 case TypeClass.STRUCT_value: 622 sCentralVariableStemName = Introspector.getShortClassName(getTypeName()); 623 break; 624 case TypeClass.INTERFACE_ATTRIBUTE_value: 625 case TypeClass.INTERFACE_METHOD_value: 626 case TypeClass.INTERFACE_value: 627 case TypeClass.PROPERTY_value: 628 String sShortClassName = m_oIntrospector.getShortClassName(getTypeName()); 629 sCentralVariableStemName = getVariableNameforUnoObject(sShortClassName); 630 default: 631 sCentralVariableStemName = SVARIABLENAME; 632 } 633 return sCentralVariableStemName; 634 } 635 636 /** may return null 637 */ 638 public Object getUnoObject(){ 639 return m_oUnoObject; 640 } 641 642 643 public void setTypeClass(TypeClass _aTypeClass){ 644 sVariableStemName = ""; 645 m_aTypeClass = _aTypeClass; 646 } 647 648 649 public TypeClass getTypeClass(){ 650 return m_aTypeClass; 651 } 652 653 654 public void setTypeName(String _sTypeName){ 655 sVariableStemName = ""; 656 m_sTypeName = _sTypeName; 657 } 658 659 660 public String getTypeName(){ 661 return m_sTypeName; 662 } 663 664 665 public void setCentralVariableStemName(String _sCentralVariableStemName){ 666 m_sCentralVariableStemName = _sCentralVariableStemName; 667 } 668 669 670 public String getVariableStemName(){ 671 if (sVariableStemName.equals("")){ 672 sVariableStemName = getVariableStemName(m_aTypeClass); 673 } 674 return sVariableStemName; 675 } 676 677 678 public void addParameterObjects(Object[] _oParameterObjects){ 679 m_oParameterObjects = _oParameterObjects; 680 } 681 682 683 public Object[] getParameterObjects(){ 684 return m_oParameterObjects; 685 } 686 687 688 public boolean hasParameterObjects(){ 689 boolean breturn = false; 690 if (m_oParameterObjects != null){ 691 breturn = m_oParameterObjects.length > 0; 692 } 693 return breturn; 694 } 695 696 697 public String getVariableStemName(TypeClass _aTypeClass){ 698 int nTypeClass = _aTypeClass.getValue(); 699 switch(nTypeClass){ 700 case TypeClass.BOOLEAN_value: 701 sVariableStemName = "b" + m_sCentralVariableStemName; 702 break; 703 case TypeClass.DOUBLE_value: 704 case TypeClass.FLOAT_value: 705 sVariableStemName = "f" + m_sCentralVariableStemName; 706 break; 707 case TypeClass.BYTE_value: 708 case TypeClass.HYPER_value: 709 case TypeClass.LONG_value: 710 case TypeClass.UNSIGNED_HYPER_value: 711 case TypeClass.UNSIGNED_LONG_value: 712 case TypeClass.UNSIGNED_SHORT_value: 713 case TypeClass.SHORT_value: 714 sVariableStemName = "n" + m_sCentralVariableStemName; 715 break; 716 case TypeClass.CHAR_value: 717 case TypeClass.STRING_value: 718 sVariableStemName = "s" + m_sCentralVariableStemName; 719 break; 720 case TypeClass.SEQUENCE_value: 721 //TODO consider mulitdimensional Arrays 722 XTypeDescription xTypeDescription = Introspector.getIntrospector().getReferencedType(getTypeName()); 723 if (xTypeDescription != null){ 724 sVariableStemName = getVariableStemName(xTypeDescription.getTypeClass()); 725 } 726 break; 727 case TypeClass.TYPE_value: 728 sVariableStemName = "a" + m_sCentralVariableStemName; 729 break; 730 case TypeClass.ANY_value: 731 sVariableStemName = "o" + m_sCentralVariableStemName; 732 break; 733 case TypeClass.STRUCT_value: 734 case TypeClass.ENUM_value: 735 sVariableStemName = "a" + m_sCentralVariableStemName; 736 break; 737 case TypeClass.INTERFACE_ATTRIBUTE_value: 738 case TypeClass.INTERFACE_METHOD_value: 739 case TypeClass.INTERFACE_value: 740 case TypeClass.PROPERTY_value: 741 String sShortClassName = m_oIntrospector.getShortClassName(getTypeName()); 742 sVariableStemName = getVariableNameforUnoObject(sShortClassName); 743 default: 744 } 745 return sVariableStemName; 746 } 747 748 749 private void setVariableName(String _sVariableName){ 750 sVariableName = _sVariableName; 751 } 752 753 754 private String getVariableName(String _sCentralVariableStemName){ 755 if (!_sCentralVariableStemName.equals("")){ 756 this.m_sCentralVariableStemName = _sCentralVariableStemName; 757 } 758 return getVariableName(); 759 } 760 761 762 private String getVariableName() throws NullPointerException{ 763 if (sVariableName.equals("")){ 764 int a = 2; 765 sVariableName = getVariableStemName(); 766 boolean bleaveloop = false; 767 while (!bleaveloop){ 768 if (aVariables.containsKey(sVariableName)){ 769 String sUnoObjectIdentity = ((UnoObjectDefinition) aVariables.get(sVariableName)).getUnoObject().toString(); 770 if (m_oUnoObject != null){ 771 if ((sUnoObjectIdentity.equals(m_oUnoObject.toString()) && (!m_oIntrospector.isPrimitive(this.getTypeClass())) && 772 (! m_oIntrospector.isObjectSequence(m_oUnoObject)))){ 773 bleaveloop = true; 774 } 775 else{ 776 sVariableName = getVariableStemName() + SSUFFIXSEPARATOR + a++; 777 } 778 } 779 else{ 780 bleaveloop = true; 781 } 782 } 783 else{ 784 bleaveloop = true; 785 // throw new NullPointerException("SourceCode Variable " + _sStemVariableName + " not defined"); 786 } 787 } 788 } 789 return sVariableName; 790 } 791 } 792 793 794 public String getStringValueOfObject(Object _oUnoObject, TypeClass _aTypeClass){ 795 String sReturn = ""; 796 switch (_aTypeClass.getValue()){ 797 case TypeClass.BOOLEAN_value: 798 boolean bbooleanValue = ((Boolean) _oUnoObject).booleanValue(); 799 sReturn += Boolean.toString(bbooleanValue); 800 case TypeClass.CHAR_value: 801 break; 802 case TypeClass.DOUBLE_value: 803 double fdoubleValue = ((Double) _oUnoObject).doubleValue(); 804 sReturn += Double.toString(fdoubleValue); 805 break; 806 case TypeClass.ENUM_value: 807 break; 808 case TypeClass.FLOAT_value: 809 float floatValue = ((Float) _oUnoObject).floatValue(); 810 sReturn += Float.toString(floatValue); 811 break; 812 case TypeClass.HYPER_value: 813 long nlongValue = ((Long) _oUnoObject).longValue(); 814 sReturn += Long.toString(nlongValue); 815 break; 816 case TypeClass.LONG_value: 817 int nintValue = ((Integer) _oUnoObject).intValue(); 818 sReturn += Integer.toString(nintValue); 819 break; 820 case TypeClass.SHORT_value: 821 short nshortValue = ((Short) _oUnoObject).shortValue(); 822 sReturn += Short.toString(nshortValue); 823 break; 824 case TypeClass.STRING_value: 825 sReturn += (String) _oUnoObject; 826 break; 827 case TypeClass.UNSIGNED_HYPER_value: 828 nlongValue = ((Long) _oUnoObject).longValue(); 829 sReturn += Long.toString(nlongValue); 830 break; 831 case TypeClass.UNSIGNED_LONG_value: 832 nintValue = ((Integer) _oUnoObject).intValue(); 833 sReturn += Integer.toString(nintValue); 834 break; 835 case TypeClass.UNSIGNED_SHORT_value: 836 nshortValue = ((Short) _oUnoObject).shortValue(); 837 sReturn += Short.toString(nshortValue); 838 break; 839 default: 840 System.out.println("Type " + _aTypeClass.getValue() + " not yet defined in 'getStringValueOfObject()'"); 841 } 842 return sReturn; 843 } 844 845 846 public String getMethodParameterValueDescription(XUnoMethodNode _oUnoMethodNode, Object[] _oParamObjects, boolean _bIncludeParameterNames){ 847 String sParamSourceCode = ""; 848 ParamInfo[] aParamInfos = _oUnoMethodNode.getXIdlMethod().getParameterInfos(); 849 if (_oParamObjects != null){ 850 for (int i = 0; i < _oParamObjects.length; i++){ 851 TypeClass aTypeClass = aParamInfos[i].aType.getTypeClass(); 852 if (_bIncludeParameterNames){ 853 sParamSourceCode += aParamInfos[i].aName + "="; 854 } 855 String sParamDescription = getStringValueOfObject(_oParamObjects[i], aTypeClass); 856 sParamDescription = this.m_xLanguageSourceCodeGenerator.castLiteral(sParamDescription, aTypeClass); 857 sParamSourceCode += sParamDescription; 858 859 if (i < _oParamObjects.length - 1){ 860 sParamSourceCode += ", "; 861 } 862 } 863 } 864 return sParamSourceCode; 865 } 866 867 868 public class JavaCodeGenerator implements XLanguageSourceCodeGenerator{ 869 String sStatementsCode = ""; 870 boolean bAddAnyConverter = false; 871 // boolean bAddTypeImport = false; 872 boolean bIsPropertyUnoObjectDefined = false; 873 874 public JavaCodeGenerator(){ 875 } 876 877 878 public String getStatementTerminationCharacter(){ 879 return ";"; 880 } 881 882 883 public String getHeaderSourceCode(Object _oUnoObject, String _sClassName, TypeClass _aTypeClass){ 884 String sClassName = _sClassName; 885 String sHeaderStatement = ""; 886 if (_oUnoObject != null){ 887 if (!m_oIntrospector.isObjectPrimitive(_oUnoObject)){ 888 if (m_oIntrospector.isObjectSequence(_oUnoObject)){ 889 XTypeDescription xTypeDescription = m_oIntrospector.getReferencedType(sClassName); 890 if (xTypeDescription != null){ 891 if (!m_oIntrospector.isPrimitive(xTypeDescription.getTypeClass())){ 892 sClassName = getTypeString(xTypeDescription.getName(), xTypeDescription.getTypeClass(), true); 893 } 894 // primitive Types are not supposed to turn up in the import section... 895 else{ 896 sClassName = ""; 897 } 898 } 899 } 900 else{ 901 sClassName = getTypeString(_sClassName, _aTypeClass, true); 902 } 903 } 904 else if (_aTypeClass.getValue() == TypeClass.ENUM_value){ 905 sClassName = _sClassName; 906 } 907 else{ 908 sClassName = ""; 909 } 910 if (!sClassName.equals("")){ 911 sHeaderStatement = "import " + sClassName + ";\n"; 912 } 913 } 914 return sHeaderStatement; 915 } 916 917 918 public String getFinalHeaderStatements(){ 919 return ""; 920 } 921 922 923 public void assignqueryInterfaceHeaderSourceCode(){ 924 sHeaderStatements.add("import com.sun.star.uno.UnoRuntime;\n"); 925 sHeaderStatements.add("import com.sun.star.uno.XInterface;\n"); 926 if (bAddAnyConverter){ 927 sHeaderStatements.add("import com.sun.star.uno.AnyConverter;\n"); 928 } 929 } 930 931 932 933 public String getConvertedSourceCodeValueOfObject(String _sReturnVariableName, String _sObjectDescription, TypeClass _aTypeClass, String _sTypeName){ 934 boolean bLocAddAnyConverter = true; 935 String sReturn = ""; 936 switch (_aTypeClass.getValue()){ 937 case TypeClass.BOOLEAN_value: 938 sReturn = _sReturnVariableName + " = AnyConverter.toBoolean(" + _sObjectDescription + ")"; 939 break; 940 case TypeClass.CHAR_value: 941 sReturn = _sReturnVariableName + " = AnyConverter.toChar(" + _sObjectDescription + ")"; 942 break; 943 case TypeClass.BYTE_value: 944 sReturn = _sReturnVariableName + " = AnyConverter.toByte(" + _sObjectDescription + ")"; 945 break; 946 case TypeClass.DOUBLE_value: 947 sReturn = _sReturnVariableName + " = AnyConverter.toDouble(" + _sObjectDescription + ")"; 948 break; 949 case TypeClass.FLOAT_value: 950 sReturn = _sReturnVariableName + " = AnyConverter.toFloat(" + _sObjectDescription + ")"; 951 break; 952 case TypeClass.UNSIGNED_HYPER_value: 953 sReturn = _sReturnVariableName + " = AnyConverter.toUnsignedLong(" + _sObjectDescription + ")"; 954 break; 955 case TypeClass.HYPER_value: 956 sReturn = _sReturnVariableName + " = AnyConverter.toLong(" + _sObjectDescription + ")"; 957 break; 958 case TypeClass.UNSIGNED_LONG_value: 959 sReturn = _sReturnVariableName + " = AnyConverter.toUnsignedInt(" + _sObjectDescription + ")"; 960 break; 961 case TypeClass.LONG_value: 962 sReturn = _sReturnVariableName + " = AnyConverter.toInt(" + _sObjectDescription + ")"; 963 break; 964 case TypeClass.SHORT_value: 965 sReturn = _sReturnVariableName + " = AnyConverter.toShort(" + _sObjectDescription + ")"; 966 break; 967 case TypeClass.UNSIGNED_SHORT_value: 968 sReturn = _sReturnVariableName + " = AnyConverter.toUnsignedShort(" + _sObjectDescription + ")"; 969 break; 970 case TypeClass.STRING_value: 971 sReturn = _sReturnVariableName + " = AnyConverter.toString(" + _sObjectDescription + ")"; 972 break; 973 default: 974 String sShortTypeName = Introspector.getShortClassName(_sTypeName); 975 if (bIsPropertyUnoObjectDefined){ 976 sReturn = "oUnoObject = " + _sObjectDescription + ";\n\t"; 977 } 978 else{ 979 sReturn = "Object oUnoObject = " + _sObjectDescription + ";\n\t"; 980 bIsPropertyUnoObjectDefined = true; 981 } 982 sReturn += _sReturnVariableName + " = (" + sShortTypeName + ") AnyConverter.toObject(" + sShortTypeName + ".class, oUnoObject);"; 983 // this.bAddTypeImport = true; 984 break; 985 } 986 if (!bAddAnyConverter){ 987 bAddAnyConverter = bLocAddAnyConverter; 988 } 989 return sReturn; 990 } 991 992 993 public String getStructSourceCode(String _sReturnVariableDescription, String _sObjectDescription, String _sMember){ 994 return "\t" + _sReturnVariableDescription + " = " + _sObjectDescription + "." + _sMember + ";"; 995 } 996 997 public String getMainMethodSignatureSourceCode(XUnoNode _oUnoNode, String _soReturnObjectDescription){ 998 //TODO try to use + _oUnoNode.getClassName() instead of the hack 999 String sReturn = "public void codesnippet(XInterface " + _soReturnObjectDescription + "){"; 1000 if (baddExceptionHandling){ 1001 sReturn += "\ntry{"; 1002 } 1003 return sReturn; 1004 } 1005 1006 public String getMethodSeparator(){ 1007 return "."; 1008 } 1009 1010 public boolean needsqueryInterface(){ 1011 return true; 1012 } 1013 1014 public String getqueryInterfaceSourceCode(String _sClassName, String _sReturnVariableName, String _sIncomingObjectName){ 1015 String sShortClassName = m_oIntrospector.getShortClassName(_sClassName); 1016 return "\t" + _sReturnVariableName + " = (" + sShortClassName + ") UnoRuntime.queryInterface(" + sShortClassName + ".class, " + _sIncomingObjectName + ");\n"; 1017 } 1018 1019 1020 public String getPropertyValueGetterSourceCode(String _sPropertyName, String _sReturnVariableName, String _sIncomingObjectName, TypeClass _aTypeClass, String _sTypeName){ 1021 String sObjectDescription = _sIncomingObjectName + ".getPropertyValue(\"" + _sPropertyName + "\")"; 1022 String sReturn = getConvertedSourceCodeValueOfObject(_sReturnVariableName, sObjectDescription, _aTypeClass, _sTypeName); 1023 sReturn += ";"; 1024 sReturn = "\t" + sReturn; 1025 return sReturn; 1026 // return "\t" + _sReturnVariableName + " = " + _sIncomingObjectName + ".getPropertyValue(\"" + _sPropertyName + "\");"; 1027 } 1028 1029 1030 public String getObjectTypeDescription(String _sClassName, boolean _bAsHeader){ 1031 String sReturn = ""; 1032 if (_bAsHeader){ 1033 sReturn = _sClassName; 1034 } 1035 else{ 1036 sReturn = m_oIntrospector.getShortClassName(_sClassName); 1037 } 1038 return sReturn; 1039 } 1040 1041 1042 public String getMethodTerminationSourceCode(){ 1043 String sReturn = ""; 1044 int nIndex = 1; 1045 String sExceptionName = "e"; 1046 if (baddExceptionHandling){ 1047 for (int i = 0; i < sExceptions.size(); i++){ 1048 String sCurException = (String) sExceptions.get(i); 1049 if (sReturn.indexOf(sCurException) == -1){ 1050 if (nIndex > 1){ 1051 sExceptionName = "e"+ nIndex; 1052 } 1053 else{ 1054 sReturn +="\n}"; 1055 } 1056 sReturn += "catch (" + sCurException + " " + sExceptionName + "){\n"; 1057 sReturn += "\t" + sExceptionName + ".printStackTrace(System.out);\n"; 1058 sReturn += "\t" + getCommentSign() + "Enter your Code here...\n}"; 1059 nIndex++; 1060 } 1061 } 1062 } 1063 sReturn += "\n}"; 1064 return sReturn; 1065 } 1066 1067 public String castLiteral(String _sExpression, TypeClass _aTypeClass){ 1068 String sReturn = ""; 1069 switch (_aTypeClass.getValue()){ 1070 case TypeClass.BOOLEAN_value: 1071 sReturn = _sExpression; 1072 break; 1073 case TypeClass.BYTE_value: 1074 sReturn = "(byte) " + _sExpression; 1075 break; 1076 case TypeClass.CHAR_value: 1077 sReturn = "'" + _sExpression + "'"; 1078 break; 1079 case TypeClass.DOUBLE_value: 1080 sReturn = "(double) " + _sExpression; 1081 break; 1082 case TypeClass.FLOAT_value: 1083 sReturn = "(float) " + _sExpression; 1084 break; 1085 case TypeClass.UNSIGNED_SHORT_value: 1086 case TypeClass.SHORT_value: 1087 sReturn = "(short) " + _sExpression; 1088 break; 1089 case TypeClass.STRING_value: 1090 sReturn = "\"" + _sExpression + "\""; 1091 break; 1092 case TypeClass.HYPER_value: 1093 case TypeClass.UNSIGNED_HYPER_value: 1094 sReturn = "(long) " + _sExpression; 1095 break; 1096 case TypeClass.LONG_value: 1097 sReturn = _sExpression; 1098 break; 1099 case TypeClass.ENUM_value: 1100 default: 1101 sReturn = _sExpression; 1102 System.out.println("Type " + _aTypeClass.getValue() + " not yet defined in 'castliteral()'"); 1103 } 1104 return sReturn; 1105 } 1106 1107 1108 public String getbooleanTypeDescription(){ 1109 return "boolean"; 1110 } 1111 1112 public String getbyteTypeDescription(){ 1113 return "byte"; 1114 } 1115 1116 public String getshortTypeDescription(){ 1117 return "short"; 1118 } 1119 1120 public String getunsignedshortTypeDescription(){ 1121 return "short"; 1122 } 1123 1124 public String getlongTypeDescription(){ 1125 return "int"; 1126 } 1127 1128 public String getunsignedlongTypeDescription(){ 1129 return "int"; 1130 } 1131 1132 public String gethyperTypeDescription(){ 1133 return "long"; 1134 } 1135 1136 public String getunsignedhyperTypeDescription(){ 1137 return "long"; 1138 } 1139 1140 public String getfloatTypeDescription(){ 1141 return "float"; 1142 } 1143 1144 public String getdoubleTypeDescription(){ 1145 return "double"; 1146 } 1147 1148 public String getcharTypeDescription(){ 1149 return "char"; 1150 } 1151 1152 public String getstringTypeDescription(boolean _bAsHeaderSourceCode){ 1153 if (_bAsHeaderSourceCode){ 1154 return ""; 1155 } 1156 else{ 1157 return "String"; 1158 } 1159 } 1160 1161 public String gettypeTypeDescription(boolean _bAsHeaderSourceCode){ 1162 if (_bAsHeaderSourceCode){ 1163 return "com.sun.star.uno.Type"; 1164 } 1165 else{ 1166 return "Type"; 1167 } 1168 } 1169 1170 public String getanyTypeDescription(boolean _bAsHeaderSourceCode){ 1171 if (_bAsHeaderSourceCode){ 1172 return ""; 1173 } 1174 else{ 1175 return "Object"; 1176 } 1177 } 1178 1179 1180 public String getStringValue(String _sValue){ 1181 return _sValue; 1182 } 1183 1184 1185 public String getVariableDeclaration(String _sTypeString, String _sVariableName, boolean _bIsArray, TypeClass _aTypeClass, boolean _bInitialize){ 1186 String sReturn = ""; 1187 if (_bIsArray){ 1188 sReturn = _sTypeString + "[] " + _sVariableName; 1189 } 1190 else{ 1191 sReturn = _sTypeString + " " + _sVariableName; 1192 } 1193 return sReturn; 1194 } 1195 1196 1197 public String getArrayDeclaration(String _sVariableDeclaration){ 1198 String sReturn = ""; 1199 String[] sDeclarations = _sVariableDeclaration.split(" "); 1200 for (int i = 0; i< sDeclarations.length;i++){ 1201 sReturn += sDeclarations[i]; 1202 if (i == 0){ 1203 sReturn += "[]"; 1204 } 1205 if (i < (sDeclarations.length -1)){ 1206 sReturn += " "; 1207 } 1208 } 1209 return sReturn; 1210 } 1211 1212 public String getCommentSign(){ 1213 return "//"; 1214 } 1215 } 1216 1217 1218 public class BasicCodeGenerator implements XLanguageSourceCodeGenerator{ 1219 String sStatementsCode = ""; 1220 1221 public BasicCodeGenerator(){ 1222 } 1223 1224 public String getHeaderSourceCode(Object _oUnoObject, String _sClassName, TypeClass _aTypeClass){ 1225 String sHeaderStatement = ""; 1226 return sHeaderStatement; 1227 } 1228 1229 public String getFinalHeaderStatements(){ 1230 return ""; 1231 } 1232 1233 public String getMainMethodSignatureSourceCode(XUnoNode _oUnoNode, String _soReturnObjectDescription){ 1234 //TODO try to use + _oUnoNode.getClassName() instead of the hack 1235 return "Sub Main(" + _soReturnObjectDescription + " as " + getanyTypeDescription(false) + ")"; 1236 } 1237 1238 public boolean needsqueryInterface(){ 1239 return false; 1240 } 1241 1242 public void assignqueryInterfaceHeaderSourceCode(){ 1243 } 1244 1245 public String getMethodSeparator(){ 1246 return "."; 1247 } 1248 1249 1250 public String getqueryInterfaceSourceCode(String _sClassName, String _sReturnVariableName, String _sIncomingObjectName){ 1251 return _sIncomingObjectName; 1252 } 1253 1254 1255 public String getPropertyValueGetterSourceCode(String _sPropertyName, String _sReturnVariableName, String _sIncomingObjectName, TypeClass _aTypeClass, String _sTypeName){ 1256 return "\t" + _sReturnVariableName + " = " + _sIncomingObjectName + "." + _sPropertyName; 1257 } 1258 1259 1260 public String getStructSourceCode(String _sReturnVariableDescription, String _sObjectDescription, String _sMember){ 1261 return getPropertyValueGetterSourceCode(_sMember, _sReturnVariableDescription, _sObjectDescription, null, "" ); 1262 } 1263 1264 public String getConvertedSourceCodeValueOfObject(String _sReturnVariableName, String _sObjectDescription, TypeClass _aTypeClass, String _sTypeName){ 1265 return _sReturnVariableName + " = " + _sObjectDescription; 1266 } 1267 1268 1269 public String getObjectTypeDescription(String _sClassName, boolean _bAsHeader){ 1270 return "Object"; 1271 } 1272 1273 1274 public String getMethodTerminationSourceCode(){ 1275 return "\nEnd Sub\n"; 1276 } 1277 1278 1279 public String castLiteral(String _sExpression, TypeClass _aTypeClass){ 1280 String sReturn = ""; 1281 switch (_aTypeClass.getValue()){ 1282 case TypeClass.BOOLEAN_value: 1283 case TypeClass.BYTE_value: 1284 case TypeClass.DOUBLE_value: 1285 case TypeClass.FLOAT_value: 1286 case TypeClass.UNSIGNED_SHORT_value: 1287 case TypeClass.SHORT_value: 1288 case TypeClass.LONG_value: 1289 case TypeClass.UNSIGNED_LONG_value: 1290 case TypeClass.HYPER_value: 1291 case TypeClass.UNSIGNED_HYPER_value: 1292 sReturn = _sExpression; 1293 break; 1294 case TypeClass.CHAR_value: 1295 case TypeClass.STRING_value: 1296 sReturn = "\"" +_sExpression + "\""; 1297 break; 1298 case TypeClass.ENUM_value: 1299 default: 1300 sReturn = _sExpression; 1301 System.out.println("Type " + _aTypeClass.getValue() + " not yet defined in 'castliteral()'"); 1302 } 1303 return sReturn; 1304 } 1305 1306 1307 1308 public String getbooleanTypeDescription(){ 1309 return "Boolean"; 1310 } 1311 1312 public String getbyteTypeDescription(){ 1313 return "Integer"; 1314 } 1315 1316 public String getshortTypeDescription(){ 1317 return "Integer"; 1318 } 1319 1320 public String getunsignedshortTypeDescription(){ 1321 return "Integer"; 1322 } 1323 1324 public String getlongTypeDescription(){ 1325 return "Integer"; 1326 } 1327 1328 public String getunsignedlongTypeDescription(){ 1329 return "Long"; 1330 } 1331 1332 public String gethyperTypeDescription(){ 1333 return "Long"; 1334 } 1335 1336 public String getunsignedhyperTypeDescription(){ 1337 return "Long"; 1338 } 1339 1340 public String getfloatTypeDescription(){ 1341 return "Double"; 1342 } 1343 1344 public String getdoubleTypeDescription(){ 1345 return "Double"; 1346 } 1347 1348 public String getcharTypeDescription(){ 1349 return "String"; 1350 } 1351 1352 public String getstringTypeDescription(boolean _bAsHeaderSourceCode){ 1353 if (_bAsHeaderSourceCode){ 1354 return ""; 1355 } 1356 else{ 1357 return "String"; 1358 } 1359 } 1360 1361 public String gettypeTypeDescription(boolean _bAsHeaderSourceCode){ 1362 if (_bAsHeaderSourceCode){ 1363 return ""; 1364 } 1365 else{ 1366 return "Object"; 1367 } 1368 } 1369 1370 public String getanyTypeDescription(boolean _bAsHeaderSourceCode){ 1371 if (_bAsHeaderSourceCode){ 1372 return ""; 1373 } 1374 else{ 1375 return "Object"; 1376 } 1377 } 1378 1379 public String getStatementTerminationCharacter(){ 1380 return ""; 1381 } 1382 1383 1384 public String getVariableDeclaration(String _sTypeString, String _sVariableName, boolean bIsArray, TypeClass _aTypeClass, boolean _bInitialize){ 1385 String sReturn = ""; 1386 if (bIsArray){ 1387 sReturn = "Dim " + _sVariableName + "() as " + _sTypeString + "\n\t" + _sVariableName; 1388 } 1389 else{ 1390 sReturn = "Dim " + _sVariableName + " as " + _sTypeString + "\n\t" + _sVariableName; 1391 } 1392 return sReturn; 1393 } 1394 1395 1396 public String getStringValue(String _sValue){ 1397 return _sValue; 1398 } 1399 1400 1401 public String getArrayDeclaration(String _sVariableDeclaration){ 1402 String sReturn = ""; 1403 String[] sDeclarations = _sVariableDeclaration.split(" "); 1404 for (int i = 0; i< sDeclarations.length;i++){ 1405 sReturn += sDeclarations[i]; 1406 if (i == 0){ 1407 sReturn += "[]"; 1408 } 1409 if (i < (sDeclarations.length -1)){ 1410 sReturn += " "; 1411 } 1412 } 1413 return sReturn; 1414 } 1415 1416 public String getCommentSign(){ 1417 return "'"; 1418 } 1419 1420 } 1421 1422 public class CPlusPlusCodeGenerator implements XLanguageSourceCodeGenerator{ 1423 String sStatementsCode = ""; 1424 boolean bIncludeStringHeader = false; 1425 boolean bIncludeAny = false; 1426 boolean bIncludeSequenceHeader = false; 1427 1428 public CPlusPlusCodeGenerator(){ 1429 } 1430 1431 private String getCSSNameSpaceString(){ 1432 return "css"; 1433 } 1434 1435 public String getStatementTerminationCharacter(){ 1436 return ";"; 1437 } 1438 1439 1440 public String getHeaderSourceCode(Object _oUnoObject, String _sClassName, TypeClass _aTypeClass){ 1441 String sClassName = _sClassName; 1442 String sHeaderStatement = ""; 1443 if (_oUnoObject != null){ 1444 if (!m_oIntrospector.isObjectPrimitive(_oUnoObject)){ 1445 if (m_oIntrospector.isObjectSequence(_oUnoObject)){ 1446 XTypeDescription xTypeDescription = m_oIntrospector.getReferencedType(sClassName); 1447 if (xTypeDescription != null){ 1448 if (!m_oIntrospector.isPrimitive(xTypeDescription.getTypeClass())){ 1449 sClassName = getTypeString(xTypeDescription.getName(), xTypeDescription.getTypeClass(), true); 1450 } 1451 // primitive Types are not supposed to turn up in the import section... 1452 else{ 1453 sClassName = ""; 1454 } 1455 } 1456 } 1457 else{ 1458 sClassName = getTypeString(_sClassName, _aTypeClass, true); 1459 } 1460 if (!sClassName.equals("")){ 1461 sHeaderStatement = getHeaderOfClass(sClassName); 1462 } 1463 } 1464 } 1465 return sHeaderStatement; 1466 } 1467 1468 1469 1470 public String getFinalHeaderStatements(){ 1471 String sReturn = ""; 1472 sReturn += "\nnamespace " + getCSSNameSpaceString() + " = com::sun::star;\n"; 1473 sReturn += "using namespace rtl;\n"; 1474 return sReturn; 1475 } 1476 1477 1478 private String getHeaderOfClass(String _sClassName){ 1479 return "#include \"" + _sClassName.replace('.', '/') + ".hpp\"\n"; // #include <com/sun/star/uno/XComponentContext.hpp> 1480 } 1481 1482 1483 1484 public void assignqueryInterfaceHeaderSourceCode(){ 1485 sHeaderStatements.add("#include \"sal/config.h\"\n"); 1486 sHeaderStatements.add("#include \"sal/types.h\"\n"); 1487 if (bIncludeStringHeader){ 1488 sHeaderStatements.add("#include \"rtl/ustring.hxx\"\n"); 1489 } 1490 sHeaderStatements.add("#include \"com/sun/star/uno/Reference.hxx\"\n"); 1491 if (bIncludeSequenceHeader){ 1492 sHeaderStatements.add("#include \"com/sun/star/uno/Sequence.hxx\"\n"); 1493 } 1494 sHeaderStatements.add(getHeaderOfClass("com.sun.star.uno.XInterface")); 1495 if (bIncludeAny){ 1496 sHeaderStatements.add(getHeaderOfClass("com.sun.star.uno.Any")); 1497 } 1498 } 1499 1500 1501 public String getMainMethodSignatureSourceCode(XUnoNode _oUnoNode, String _soReturnObjectDescription){ 1502 String sReturn = ""; 1503 sReturn = "void codesnippet(const " + getCSSNameSpaceString() + "::uno::Reference<" + getCSSNameSpaceString() + "::uno::XInterface>& " + _soReturnObjectDescription + " ){"; 1504 int a = 0; 1505 if (!sExceptions.contains("com.sun.star.uno.RuntimeException")){ 1506 sExceptions.add("com.sun.star.uno.RuntimeException"); 1507 } 1508 if (baddExceptionHandling){ 1509 sReturn += "\n//throw "; 1510 for (int i = 0; i < sExceptions.size(); i++){ 1511 String sCurException = (String) sExceptions.get(i); 1512 if (sReturn.indexOf(sCurException) == -1){ 1513 if (a++ > 0){ 1514 sReturn += ", "; 1515 } 1516 sReturn += getObjectTypeDescription(sCurException, false); 1517 1518 } 1519 } 1520 1521 } 1522 sReturn += "{"; 1523 return sReturn; 1524 } 1525 1526 1527 public boolean needsqueryInterface(){ 1528 return true; 1529 } 1530 1531 1532 public String getqueryInterfaceSourceCode(String _sClassName, String _sReturnVariableName, String _sIncomingObjectName){ 1533 return "\t" + _sReturnVariableName + "( " + _sIncomingObjectName + ", " + getCSSNameSpaceString() + "::uno::UNO_QUERY_THROW);\n"; 1534 } 1535 1536 1537 public String getPropertyValueGetterSourceCode(String _sPropertyName, String _sReturnVariableName, String _sIncomingObjectName, TypeClass _aTypeClass, String _sTypeName){ 1538 String sFirstLine = "\t"; 1539 String sReturnVariableName = _sReturnVariableName; 1540 // e.g. uno::Any a = xPropSet->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" )) ); 1541 String[] sVarDefinition = _sReturnVariableName.split("="); 1542 if (sVarDefinition.length > 0){ 1543 String sVariable = sVarDefinition[0]; 1544 String[] sVarDeclaration = sVariable.split(" "); 1545 if (sVarDeclaration.length > 0){ 1546 sFirstLine += sReturnVariableName + ";\n"; 1547 sReturnVariableName = sVarDeclaration[sVarDeclaration.length-1]; 1548 } 1549 } 1550 String sObjectDescription = _sIncomingObjectName + "->getPropertyValue(" + getStringValue(_sPropertyName) + ")"; 1551 String sSecondLine = "\t" + getConvertedSourceCodeValueOfObject(sReturnVariableName, sObjectDescription, _aTypeClass, _sTypeName) + ";"; 1552 return sFirstLine + sSecondLine; 1553 } 1554 1555 1556 public String getStructSourceCode(String _sReturnVariableDescription, String _sObjectDescription, String _sMember){ 1557 return "\t" + _sReturnVariableDescription + " = " + _sObjectDescription + "->" + _sMember + ";"; 1558 } 1559 1560 1561 public String getConvertedSourceCodeValueOfObject(String _sReturnVariableName, String _sObjectDescription, TypeClass _aTypeClass, String _sTypeName){ 1562 // if (m_oIntrospector.isPrimitive(_aTypeClass)){ 1563 return _sObjectDescription + " >>= " + _sReturnVariableName; 1564 // } 1565 // else{ 1566 // return _sReturnVariableName + " = " + _sObjectDescription; 1567 // } 1568 } 1569 1570 1571 public String getStringValue(String _sValue){ 1572 bIncludeStringHeader = true; 1573 return "OUString(RTL_CONSTASCII_USTRINGPARAM(\"" + _sValue + "\"))"; 1574 } 1575 1576 1577 public String getObjectTypeDescription(String _sClassName, boolean _bAsHeader){ 1578 String sReturn = ""; 1579 if (_bAsHeader){ 1580 sReturn = _sClassName.replace('.', '/'); 1581 } 1582 else{ 1583 String sModuleName = m_oIntrospector.getModuleName(_sClassName); 1584 sModuleName = m_oIntrospector.getShortClassName(sModuleName); 1585 sReturn = getCSSNameSpaceString() + "::" + sModuleName + "::" + m_oIntrospector.getShortClassName(_sClassName); 1586 } 1587 return sReturn; 1588 } 1589 1590 1591 public String getMethodTerminationSourceCode(){ 1592 return "\n}"; 1593 } 1594 1595 public String getMethodSeparator(){ 1596 return "->"; 1597 } 1598 1599 1600 public String castLiteral(String _sExpression, TypeClass _aTypeClass){ 1601 String sReturn = ""; 1602 switch (_aTypeClass.getValue()){ 1603 case TypeClass.BOOLEAN_value: 1604 case TypeClass.BYTE_value: 1605 case TypeClass.DOUBLE_value: 1606 case TypeClass.FLOAT_value: 1607 case TypeClass.UNSIGNED_SHORT_value: 1608 case TypeClass.SHORT_value: 1609 case TypeClass.LONG_value: 1610 case TypeClass.UNSIGNED_LONG_value: 1611 case TypeClass.HYPER_value: 1612 case TypeClass.UNSIGNED_HYPER_value: 1613 sReturn = _sExpression; 1614 break; 1615 case TypeClass.CHAR_value: 1616 sReturn = "'" + _sExpression + "'"; 1617 break; 1618 case TypeClass.STRING_value: 1619 sReturn = getStringValue(_sExpression); 1620 break; 1621 case TypeClass.ENUM_value: 1622 default: 1623 sReturn = _sExpression; 1624 System.out.println("Type " + _aTypeClass.getValue() + " not yet defined in 'castliteral()'"); 1625 } 1626 return sReturn; 1627 } 1628 1629 public String getbooleanTypeDescription(){ 1630 return "sal_Bool"; 1631 } 1632 1633 public String getbyteTypeDescription(){ 1634 return "sal_Int8"; 1635 } 1636 1637 public String getshortTypeDescription(){ 1638 return "sal_Int16"; 1639 } 1640 1641 public String getunsignedshortTypeDescription(){ 1642 return "sal_uInt16"; 1643 } 1644 1645 public String getlongTypeDescription(){ 1646 return "sal_Int32"; 1647 } 1648 1649 public String getunsignedlongTypeDescription(){ 1650 return "sal_uInt32"; 1651 } 1652 1653 public String gethyperTypeDescription(){ 1654 return "sal_Int64"; 1655 } 1656 1657 public String getunsignedhyperTypeDescription(){ 1658 return "sal_uInt64"; 1659 } 1660 1661 public String getfloatTypeDescription(){ 1662 return "float"; 1663 } 1664 1665 public String getdoubleTypeDescription(){ 1666 return "double"; 1667 } 1668 1669 public String getcharTypeDescription(){ 1670 return "sal_Unicode"; 1671 } 1672 1673 public String getstringTypeDescription(boolean _bAsHeaderSourceCode){ 1674 bIncludeStringHeader = true; 1675 if (_bAsHeaderSourceCode){ 1676 return ""; 1677 } 1678 else{ 1679 return "OUString"; 1680 } 1681 } 1682 1683 public String gettypeTypeDescription(boolean _bAsHeaderSourceCode){ 1684 if (_bAsHeaderSourceCode){ 1685 return "com/sun/star/uno/Type"; 1686 } 1687 else{ 1688 return "Type"; 1689 } 1690 } 1691 1692 public String getanyTypeDescription(boolean _bAsHeaderSourceCode){ 1693 if (_bAsHeaderSourceCode){ 1694 return "com/sun/star/uno/XInterface"; 1695 } 1696 else{ 1697 return "XInterface"; 1698 } 1699 } 1700 1701 1702 public String getVariableDeclaration(String _sTypeString, String _sVariableName, boolean bIsArray, TypeClass _aTypeClass, boolean _bInitialize){ 1703 boolean bIsPrimitive = m_oIntrospector.isPrimitive(_aTypeClass); 1704 1705 // uno::Reference< frame::XDispatch > m_xDispatch 1706 String sReturn = ""; 1707 if (bIsArray){ 1708 bIncludeSequenceHeader = true; 1709 sReturn = getCSSNameSpaceString() + "::uno::Sequence<" + _sTypeString + "> " + _sVariableName; 1710 } 1711 else{ 1712 if (bIsPrimitive){ 1713 sReturn = _sTypeString + " " + _sVariableName; 1714 if (_bInitialize){ 1715 switch (_aTypeClass.getValue()){ 1716 case TypeClass.BOOLEAN_value: 1717 sReturn = sReturn + " = false"; 1718 break; 1719 case TypeClass.BYTE_value: 1720 case TypeClass.UNSIGNED_SHORT_value: 1721 case TypeClass.SHORT_value: 1722 case TypeClass.LONG_value: 1723 case TypeClass.UNSIGNED_LONG_value: 1724 case TypeClass.HYPER_value: 1725 case TypeClass.UNSIGNED_HYPER_value: 1726 sReturn = sReturn + " = 0"; 1727 break; 1728 case TypeClass.DOUBLE_value: 1729 case TypeClass.FLOAT_value: 1730 sReturn = sReturn + " = 0.0"; 1731 break; 1732 case TypeClass.CHAR_value: 1733 sReturn = sReturn + "'0'"; 1734 break; 1735 case TypeClass.STRING_value: 1736 sReturn = _sTypeString + " " + _sVariableName; 1737 break; 1738 default: 1739 sReturn = _sTypeString + " " + _sVariableName; 1740 System.out.println("Type " + _aTypeClass.getValue() + " not yet defined in 'getVariableDeclaration()'"); 1741 } 1742 } 1743 } 1744 else{ 1745 sReturn = getCSSNameSpaceString() + "::uno::Reference<" + _sTypeString + "> " +_sVariableName; 1746 } 1747 } 1748 return sReturn; 1749 } 1750 1751 public String getArrayDeclaration(String _sVariableDeclaration){ 1752 this.bIncludeSequenceHeader = true; 1753 String sReturn = ""; 1754 String[] sDeclarations = _sVariableDeclaration.split(" "); 1755 if (sDeclarations.length == 2){ 1756 sReturn = getCSSNameSpaceString() +"::uno::Sequence<" + sDeclarations[1] + ">"; 1757 } 1758 return sReturn; 1759 } 1760 1761 public String getCommentSign(){ 1762 return "//"; 1763 } 1764 1765 } 1766 } 1767