1 55 56 package org.apache.bsf.util; 57 58 import java.io.StringReader ; 59 import java.io.StringWriter ; 60 import java.io.PrintWriter ; 61 import java.util.Hashtable ; 62 import java.util.Stack ; 63 import java.util.Vector ; 64 65 import org.apache.bsf.util.cf.CodeFormatter; 66 67 73 public class CodeBuffer 74 { 75 private StringWriter fieldDeclSW = new StringWriter (), 76 methodDeclSW = new StringWriter (), 77 initializerSW = new StringWriter (), 78 constructorSW = new StringWriter (), 79 serviceMethodSW = new StringWriter (); 80 81 private PrintWriter fieldDeclPW = new PrintWriter (fieldDeclSW), 82 methodDeclPW = new PrintWriter (methodDeclSW), 83 initializerPW = new PrintWriter (initializerSW), 84 constructorPW = new PrintWriter (constructorSW), 85 serviceMethodPW = new PrintWriter (serviceMethodSW); 86 87 private Stack symbolTableStack = new Stack (); 88 private Hashtable symbolTable = new Hashtable (), 89 usedSymbolIndices = new Hashtable (); 90 91 private ObjInfo finalStatementInfo; 92 private CodeBuffer parent; 93 94 95 { 96 symbolTableStack.push(symbolTable); 97 } 98 99 private Vector imports = new Vector (), 101 constructorArguments = new Vector (), 102 constructorExceptions = new Vector (), 103 serviceMethodExceptions = new Vector (), 104 implementsVector = new Vector (); 105 private String packageName = null, 106 className = "Test", 107 serviceMethodName = "exec", 108 extendsName = null; 109 private Class serviceMethodReturnType = void.class; 110 111 public CodeBuffer() 112 { 113 } 114 public CodeBuffer(CodeBuffer parent) 115 { 116 this.parent = parent; 117 } 118 public void addConstructorArgument(ObjInfo arg) 119 { 120 constructorArguments.addElement(arg); 121 } 122 public void addConstructorException(String exceptionName) 123 { 124 if (!constructorExceptions.contains(exceptionName)) 125 { 126 constructorExceptions.addElement(exceptionName); 127 } 128 } 129 public void addConstructorStatement(String statement) 130 { 131 constructorPW.println(statement); 132 } 133 public void addFieldDeclaration(String statement) 134 { 135 fieldDeclPW.println(statement); 136 } 137 public void addImplements(String importName) 138 { 139 if (!implementsVector.contains(importName)) 140 { 141 implementsVector.addElement(importName); 142 } 143 } 144 public void addImport(String importName) 145 { 146 if (!imports.contains(importName)) 147 { 148 imports.addElement(importName); 149 } 150 } 151 public void addInitializerStatement(String statement) 152 { 153 initializerPW.println(statement); 154 } 155 public void addMethodDeclaration(String statement) 156 { 157 methodDeclPW.println(statement); 158 } 159 public void addServiceMethodException(String exceptionName) 160 { 161 if (!serviceMethodExceptions.contains(exceptionName)) 162 { 163 serviceMethodExceptions.addElement(exceptionName); 164 } 165 } 166 public void addServiceMethodStatement(String statement) 167 { 168 serviceMethodPW.println(statement); 169 } 170 private void appendIfNecessary(PrintWriter pw, StringBuffer buf) 172 { 173 if (buf.length() > 0) 174 { 175 pw.print(buf.toString()); 176 } 177 } 178 public String buildNewSymbol(String prefix) 179 { 180 Integer nextNum = getSymbolIndex(prefix); 181 182 if (nextNum == null) 183 { 184 nextNum = new Integer (0); 185 } 186 187 int iNextNum = nextNum.intValue(); 188 String symbol = prefix + "_" + iNextNum; 189 190 while (getSymbol(symbol) != null) 191 { 192 iNextNum++; 193 symbol = prefix + "_" + iNextNum; 194 } 195 196 putSymbolIndex(prefix, new Integer (iNextNum + 1)); 197 198 return symbol; 199 } 200 public void clearSymbolTable() 201 { 202 symbolTable = new Hashtable (); 203 symbolTableStack = new Stack (); 204 symbolTableStack.push(symbolTable); 205 206 usedSymbolIndices = new Hashtable (); 207 } 208 public String getClassName() 209 { 210 return className; 211 } 212 public Vector getConstructorArguments() 213 { 214 return constructorArguments; 215 } 216 public StringBuffer getConstructorBuffer() 217 { 218 constructorPW.flush(); 219 220 return constructorSW.getBuffer(); 221 } 222 public Vector getConstructorExceptions() 223 { 224 return constructorExceptions; 225 } 226 public String getExtends() 227 { 228 return extendsName; 229 } 230 public StringBuffer getFieldBuffer() 231 { 232 fieldDeclPW.flush(); 233 234 return fieldDeclSW.getBuffer(); 235 } 236 public ObjInfo getFinalServiceMethodStatement() 237 { 238 return finalStatementInfo; 239 } 240 public Vector getImplements() 241 { 242 return implementsVector; 243 } 244 public Vector getImports() 245 { 246 return imports; 247 } 248 public StringBuffer getInitializerBuffer() 249 { 250 initializerPW.flush(); 251 252 return initializerSW.getBuffer(); 253 } 254 public StringBuffer getMethodBuffer() 255 { 256 methodDeclPW.flush(); 257 258 return methodDeclSW.getBuffer(); 259 } 260 public String getPackageName() 261 { 262 return packageName; 263 } 264 public StringBuffer getServiceMethodBuffer() 265 { 266 serviceMethodPW.flush(); 267 268 return serviceMethodSW.getBuffer(); 269 } 270 public Vector getServiceMethodExceptions() 271 { 272 return serviceMethodExceptions; 273 } 274 public String getServiceMethodName() 275 { 276 return serviceMethodName; 277 } 278 public Class getServiceMethodReturnType() 279 { 280 if (finalStatementInfo != null) 281 { 282 return finalStatementInfo.objClass; 283 } 284 else if (serviceMethodReturnType != null) 285 { 286 return serviceMethodReturnType; 287 } 288 else 289 { 290 return void.class; 291 } 292 } 293 public ObjInfo getSymbol(String symbol) 294 { 295 ObjInfo ret = (ObjInfo)symbolTable.get(symbol); 296 297 if (ret == null && parent != null) 298 ret = parent.getSymbol(symbol); 299 300 return ret; 301 } 302 Integer getSymbolIndex(String prefix) 303 { 304 if (parent != null) 305 { 306 return parent.getSymbolIndex(prefix); 307 } 308 else 309 { 310 return (Integer )usedSymbolIndices.get(prefix); 311 } 312 } 313 public Hashtable getSymbolTable() 314 { 315 return symbolTable; 316 } 317 public void merge(CodeBuffer otherCB) 318 { 319 Vector otherImports = otherCB.getImports(); 320 321 for (int i = 0; i < otherImports.size(); i++) 322 { 323 addImport((String )otherImports.elementAt(i)); 324 } 325 326 appendIfNecessary(fieldDeclPW, otherCB.getFieldBuffer()); 327 appendIfNecessary(methodDeclPW, otherCB.getMethodBuffer()); 328 appendIfNecessary(initializerPW, otherCB.getInitializerBuffer()); 329 appendIfNecessary(constructorPW, otherCB.getConstructorBuffer()); 330 appendIfNecessary(serviceMethodPW, otherCB.getServiceMethodBuffer()); 331 332 ObjInfo oldRet = getFinalServiceMethodStatement(); 333 334 if (oldRet != null && oldRet.isExecutable()) 335 { 336 addServiceMethodStatement(oldRet.objName + ";"); 337 } 338 339 setFinalServiceMethodStatement(otherCB.getFinalServiceMethodStatement()); 340 } 341 public void popSymbolTable() 342 { 343 symbolTableStack.pop(); 344 symbolTable = (Hashtable )symbolTableStack.peek(); 345 } 346 public void print(PrintWriter out, boolean formatOutput) 347 { 348 if (formatOutput) 349 { 350 new CodeFormatter().formatCode(new StringReader (toString()), out); 351 } 352 else 353 { 354 out.print(toString()); 355 } 356 357 out.flush(); 358 } 359 public void pushSymbolTable() 360 { 361 symbolTable = (Hashtable )symbolTableStack.push(new ScriptSymbolTable(symbolTable)); 362 } 363 public void putSymbol(String symbol, ObjInfo obj) 364 { 365 symbolTable.put(symbol, obj); 366 } 367 void putSymbolIndex(String prefix, Integer index) 368 { 369 if (parent != null) 370 { 371 parent.putSymbolIndex(prefix, index); 372 } 373 else 374 { 375 usedSymbolIndices.put(prefix, index); 376 } 377 } 378 public void setClassName(String className) 379 { 380 this.className = className; 381 } 382 public void setExtends(String extendsName) 383 { 384 this.extendsName = extendsName; 385 } 386 public void setFinalServiceMethodStatement(ObjInfo finalStatementInfo) 387 { 388 this.finalStatementInfo = finalStatementInfo; 389 } 390 public void setPackageName(String packageName) 391 { 392 this.packageName = packageName; 393 } 394 public void setServiceMethodName(String serviceMethodName) 395 { 396 this.serviceMethodName = serviceMethodName; 397 } 398 public void setServiceMethodReturnType(Class serviceMethodReturnType) 399 { 400 this.serviceMethodReturnType = serviceMethodReturnType; 401 } 402 public void setSymbolTable(Hashtable symbolTable) 403 { 404 this.symbolTable = symbolTable; 405 } 406 public boolean symbolTableIsStacked() 407 { 408 return (symbolTable instanceof ScriptSymbolTable); 409 } 410 public String toString() 411 { 412 StringWriter sw = new StringWriter (); 413 PrintWriter pw = new PrintWriter (sw); 414 ObjInfo ret = finalStatementInfo; 415 416 if (packageName != null && !packageName.equals("")) 417 { 418 pw.println("package " + packageName + ";"); 419 pw.println(); 420 } 421 422 if (imports.size() > 0) 423 { 424 for (int i = 0; i < imports.size(); i++) 425 { 426 pw.println("import " + imports.elementAt(i) + ";"); 427 } 428 429 pw.println(); 430 } 431 432 pw.println("public class " + className + 433 (extendsName != null && !extendsName.equals("") 434 ? " extends " + extendsName 435 : "") + 436 (implementsVector.size() > 0 437 ? " implements " + 438 StringUtils.getCommaListFromVector(implementsVector) 439 : "") 440 ); 441 pw.println("{"); 442 443 pw.print(getFieldBuffer().toString()); 444 445 StringBuffer buf = getInitializerBuffer(); 446 447 if (buf.length() > 0) 448 { 449 pw.println(); 450 pw.println("{"); 451 pw.print(buf.toString()); 452 pw.println("}"); 453 } 454 455 buf = getConstructorBuffer(); 456 457 if (buf.length() > 0) 458 { 459 pw.println(); 460 pw.println("public " + className + "(" + 461 (constructorArguments.size() > 0 462 ? StringUtils.getCommaListFromVector(constructorArguments) 463 : "" 464 ) + ")" + 465 (constructorExceptions.size() > 0 466 ? " throws " + 467 StringUtils.getCommaListFromVector(constructorExceptions) 468 : "" 469 ) 470 ); 471 pw.println("{"); 472 pw.print(buf.toString()); 473 pw.println("}"); 474 } 475 476 buf = getServiceMethodBuffer(); 477 478 if (buf.length() > 0 || ret != null) 479 { 480 pw.println(); 481 pw.println("public " + 482 StringUtils.getClassName(getServiceMethodReturnType()) + " " + 483 serviceMethodName + "()" + 484 (serviceMethodExceptions.size() > 0 485 ? " throws " + 486 StringUtils.getCommaListFromVector(serviceMethodExceptions) 487 : "" 488 ) 489 ); 490 pw.println("{"); 491 492 pw.print(buf.toString()); 493 494 if (ret != null) 495 { 496 if (ret.isValueReturning()) 497 { 498 pw.println(); 499 pw.println("return " + ret.objName + ";"); 500 } 501 else if (ret.isExecutable()) 502 { 503 pw.println(ret.objName + ";"); 504 } 505 } 506 507 pw.println("}"); 508 } 509 510 pw.print(getMethodBuffer().toString()); 511 512 pw.println("}"); 513 514 pw.flush(); 515 516 return sw.toString(); 517 } 518 } 519 | Popular Tags |