1 19 20 package org.netbeans.modules.schema2beansdev.gen; 21 22 import java.util.*; 23 import java.io.*; 24 25 public class JavaWriter extends IndentingWriter { 26 public int HEADER_SECTION = 0; 29 public int DECL_SECTION = 1; 30 public int CONSTRUCTOR_SECTION = 2; 31 public int BODY_SECTION = 3; 32 public int TRAILER_SECTION = 4; 33 static final protected int defaultSectionCount = 5; 34 35 static public final int PUBLIC = 0x0; 36 static public final int PROTECTED = 0x1; 37 static public final int PACKAGE_LEVEL = 0x2; 38 static public final int PRIVATE = 0x3; 39 static public final int ACCESS_MASK = 0x3; 40 41 static public final int STATIC = 0x10; 42 static public final int FINAL = 0x20; 43 44 static public final int BEANINFO = 0x100; 45 static public final int IO = 0x200; 46 static public final int UNSUPPORTED = 0x400; 47 static public final int METHOD_SEMANTIC_MASK = 0xf00; 48 49 protected boolean newlineBeforeCurlyBrace = false; 50 public boolean storeMethods = true; 51 52 public JavaWriter() { 53 super(defaultSectionCount); 54 privateInit(); 55 } 56 57 public JavaWriter(JavaWriter source) { 58 super(source); 59 HEADER_SECTION = source.HEADER_SECTION; 60 DECL_SECTION = source.DECL_SECTION; 61 CONSTRUCTOR_SECTION = source.CONSTRUCTOR_SECTION; 62 BODY_SECTION = source.BODY_SECTION; 63 TRAILER_SECTION = source.TRAILER_SECTION; 64 newlineBeforeCurlyBrace = source.newlineBeforeCurlyBrace; 65 storeMethods = source.storeMethods; 66 methods = new LinkedHashMap(); 67 } 68 69 75 public int insertSectionAfter(int sectionNum) { 76 insertAdditionalBuffers(sectionNum, 1); 77 if (sectionNum < HEADER_SECTION) ++HEADER_SECTION; 78 if (sectionNum < DECL_SECTION) ++DECL_SECTION; 79 if (sectionNum < CONSTRUCTOR_SECTION) ++CONSTRUCTOR_SECTION; 80 if (sectionNum < BODY_SECTION) ++BODY_SECTION; 81 if (sectionNum < TRAILER_SECTION) ++TRAILER_SECTION; 82 return sectionNum + 1; 83 } 84 85 public void reset() { 86 super.reset(); 87 privateInit(); 88 } 89 90 private void privateInit() { 91 for (int i = 0; i < bufferCount; i++) { 92 if (i == HEADER_SECTION) 93 indentLevel[i] = 0; 94 else 95 indentLevel[i] = 1; 96 } 97 methods = new LinkedHashMap(); 98 } 99 100 104 public void writeTo(Writer out) throws IOException { 105 Writer n2aout = new BufferedWriter(new JavaUtil.N2AFilter(out)); 106 super.writeTo(n2aout); 107 n2aout.flush(); 108 } 109 110 114 public void writeTo(OutputStream out) throws IOException { 115 Writer w = new OutputStreamWriter(out); 116 writeTo(w); 117 w.flush(); 118 } 119 120 public void writeTo(GenBuffer o) { 121 super.writeTo(o); 122 if (o instanceof JavaWriter) { 123 JavaWriter out = (JavaWriter) o; 124 if (storeMethods) { 125 out.methods.putAll(methods); 126 } 127 } 128 } 129 130 public boolean writeOptions(int options) throws IOException { 131 boolean needSpace = writeAccess(options); 132 if ((options & STATIC) == STATIC) { 133 if (needSpace) 134 write(" "); 135 write("static"); 136 needSpace = true; 137 } 138 if ((options & FINAL) == FINAL) { 139 if (needSpace) 140 write(" "); 141 write("final"); 142 needSpace = true; 143 } 144 return needSpace; 145 } 146 147 public boolean writeAccess(int accessLevel) throws IOException { 148 switch (accessLevel & ACCESS_MASK) { 149 case PUBLIC: 150 write("public"); 151 return true; 152 case PROTECTED: 153 write("protected"); 154 return true; 155 case PACKAGE_LEVEL: 156 return false; 158 case PRIVATE: 159 write("private"); 160 return true; 161 } 162 return false; 163 } 164 165 168 public void writeClassDecl(String name, String extendsStatement, 169 String implementsStatement, int options) throws IOException { 170 pushSelect(HEADER_SECTION); 171 try { 172 if (writeOptions(options)) 173 write(" "); 174 write("class ", name, " "); 175 if (extendsStatement != null) { 176 write("extends ", extendsStatement, " "); 177 } 178 if (implementsStatement != null) { 179 write("implements ", implementsStatement, " "); 180 } 181 begin(); 182 popSelect(); 183 pushSelect(TRAILER_SECTION); 184 end(); 185 } finally { 186 popSelect(); 187 } 188 } 189 190 public void beginMethod(String name) throws IOException { 191 beginMethod(name, "", null); 192 } 193 194 public void beginMethod(String name, String parameters) throws IOException { 195 beginMethod(name, parameters, null); 196 } 197 198 public void beginMethod(String name, String parameters, String exceptions) throws IOException { 199 beginMethod(name, parameters, exceptions, "void", PUBLIC); 200 } 201 202 public void beginMethod(String name, String parameters, String exceptions, 203 String returnType) throws IOException { 204 beginMethod(name, parameters, exceptions, returnType, PUBLIC); 205 } 206 207 public void beginMethod(String name, String parameters, String exceptions, 208 String returnType, int options) throws IOException { 209 writeMethod(name, parameters, exceptions, returnType, options); 210 write(" "); 211 begin(); 212 } 213 214 public void endMethod() throws IOException { 215 end(); 216 cr(); 217 } 218 219 public void writeMethod(String name, String parameters, String exceptions, 220 String returnType, int options) throws IOException { 221 String nameParameters = name+"("+parameters+")"; 222 if (storeMethods) { 223 addToMethodStore(name, parameters, exceptions, returnType, options); 224 } 225 if (writeOptions(options)) 226 write(" "); 227 write(returnType); 228 write(" "); 229 write(nameParameters); 230 if (exceptions != null) 231 write(" throws ", exceptions); 232 } 233 234 private Map methods; public static class Method implements Comparable { 236 private String name; 237 private String parameters; 238 private String exceptions; 239 private String returnType; 240 private int options; 241 242 public Method(String name, String parameters, String exceptions, 243 String returnType, int options) { 244 this.name = name; 245 this.parameters = parameters.trim(); 246 this.exceptions = exceptions; 247 this.returnType = returnType; 248 this.options = options; 249 } 250 251 public void beginMethod(JavaWriter out) throws IOException { 252 out.beginMethod(name, parameters, exceptions, returnType, options); 253 } 254 255 public void writeMethod(JavaWriter out) throws IOException { 256 out.writeMethod(name, parameters, exceptions, returnType, options); 257 } 258 259 public String getNameParameters() { 260 return name+"("+parameters+")"; 261 } 262 263 public String getName() { 264 return name; 265 } 266 267 public String getReturnType() { 268 return returnType; 269 } 270 271 public String getParameters() { 272 return parameters; 273 } 274 275 public String getExceptions() { 276 return exceptions; 277 } 278 279 public int getOptions() { 280 return options; 281 } 282 283 public boolean isStatic() { 284 return (options & STATIC) == STATIC; 285 } 286 287 public boolean isPublic() { 288 return (options & ACCESS_MASK) == PUBLIC; 289 } 290 291 public boolean isBeanInfo() { 292 return (options & BEANINFO) == BEANINFO; 293 } 294 295 public boolean isUnsupported() { 296 return (options & UNSUPPORTED) == UNSUPPORTED; 297 } 298 299 public boolean isConstructor() { 300 return "".equals(getReturnType()); 301 } 302 303 public void writeCall(JavaWriter out) throws IOException { 304 out.write(name); 305 out.write("("); 306 writeParametersNoTypes(out); 307 out.write(")"); 308 } 309 310 public void writeParametersNoTypes(JavaWriter out) throws IOException { 311 boolean writeIt = false; 312 for (int pos = 0; pos < parameters.length(); ++pos) { 313 char c = parameters.charAt(pos); 314 int endFinal = pos + 6; 316 if ((parameters.length() >= endFinal) && 317 parameters.substring(pos, endFinal).equals("final ")) { 318 pos = endFinal; 319 c = parameters.charAt(pos); 320 } 321 322 if (writeIt) 323 out.write(c); 324 boolean skipWS = false; 325 if (Character.isWhitespace(c)) { 326 writeIt = true; 327 skipWS = true; 328 } else if (c == ',') { 329 writeIt = false; 330 skipWS = true; 331 } 332 if (skipWS) { 333 while (pos+1 < parameters.length() && 334 Character.isWhitespace(parameters.charAt(pos+1))) 335 ++pos; 336 } 337 } 338 } 339 340 public int compareTo(Object o) { 341 Method otherMethod = (Method) o; 342 return getNameParameters().compareTo(otherMethod.getNameParameters()); 343 } 344 } 345 346 public void addToMethodStore(String name, String parameters, String exceptions, 347 String returnType) { 348 addToMethodStore(name, parameters, exceptions, returnType, PUBLIC); 349 } 350 351 public void addToMethodStore(String name, String parameters, String exceptions, 352 String returnType, int options) { 353 Method method = new Method(name, parameters, exceptions, returnType, options); 354 methods.put(method.getNameParameters(), method); 355 } 356 357 public Collection getStoredMethods() { 358 return methods.values(); 359 } 360 361 public void beginConstructor(String name) throws IOException { 362 beginConstructor(name, "", null, PUBLIC); 363 } 364 365 public void beginConstructor(String name, String parameters) throws IOException { 366 beginConstructor(name, parameters, null, PUBLIC); 367 } 368 369 public void beginConstructor(String name, String parameters, 370 String exceptions, int options) throws IOException { 371 select(CONSTRUCTOR_SECTION); 372 if (writeOptions(options)) 373 write(" "); 374 write(name); 375 write("(", parameters, ") "); 376 if (exceptions != null) 377 write("throws ", exceptions, " "); 378 begin(); 379 addToMethodStore(name, parameters, exceptions, "", options); 380 } 381 382 public void writePackage(String pkg) throws IOException { 383 pushSelect(HEADER_SECTION); 384 try { 385 writecr("package " , pkg, ";"); 386 } finally { 387 popSelect(); 388 } 389 } 390 391 public void writeImport(String pkg) throws IOException { 392 pushSelect(HEADER_SECTION); 393 try { 394 writecr("import ", pkg, ";"); 395 } finally { 396 popSelect(); 397 } 398 } 399 400 public void begin() throws IOException { 401 if (newlineBeforeCurlyBrace) 402 cr(); 403 writecr("{"); 404 indentRight(); 405 } 406 407 public void end() throws IOException { 408 end(true); 409 } 410 411 public void end(boolean useCr) throws IOException { 412 indentLeft(); 413 write("}"); 414 if (useCr) 415 cr(); 416 } 417 418 public void eol() throws IOException { 419 eol(true); 420 } 421 422 public void eol(boolean useCr) throws IOException { 423 write(";"); 424 if (useCr) 425 cr(); 426 } 427 428 public void writeEol(String s) throws IOException { 429 write(s); 430 eol(); 431 } 432 433 public void writeEol(String s1, String s2) throws IOException { 434 write(s1, s2); 435 eol(); 436 } 437 438 public void writeEol(String s1, String s2, String s3) throws IOException { 439 write(s1, s2, s3); 440 eol(); 441 } 442 443 public void writeEol(String s1, String s2, String s3, String s4) throws IOException { 444 write(s1, s2, s3, s4); 445 eol(); 446 } 447 448 public void noI18N() throws IOException { 449 writecr(" // NOI18N"); 450 } 451 452 public void eolNoI18N() throws IOException { 453 write(";"); 454 noI18N(); 455 } 456 457 public void writeEolNoI18N(String s) throws IOException { 458 write(s); 459 write(";"); 460 noI18N(); 461 } 462 463 public void writeEolNoI18N(String s1, String s2) throws IOException { 464 write(s1, s2); 465 write(";"); 466 noI18N(); 467 } 468 469 public void writeEolNoI18N(String s1, String s2, String s3) throws IOException { 470 write(s1, s2, s3); 471 write(";"); 472 noI18N(); 473 } 474 475 public void writeEolNoI18N(String s1, String s2, String s3, String s4) throws IOException { 476 write(s1, s2, s3, s4); 477 write(";"); 478 noI18N(); 479 } 480 481 public void beginTry() throws IOException { 482 write("try "); 483 begin(); 484 } 485 486 public void endCatch(String param) throws IOException { 487 end(false); 488 write(" catch (", param, ") "); 489 begin(); 490 } 491 492 public void endFinallyBegin() throws IOException { 493 end(false); 494 write(" finally "); 495 begin(); 496 } 497 498 public void beginIf(String predicate) throws IOException { 499 write("if (", predicate, ") "); 500 begin(); 501 } 502 503 public void beginIf(String predicate1, String predicate2) throws IOException { 504 write("if (", predicate1, predicate2, ") "); 505 begin(); 506 } 507 508 public void beginIf(String predicate1, String predicate2, String predicate3) throws IOException { 509 write("if (", predicate1, predicate2, predicate3); 510 write(") "); 511 begin(); 512 } 513 514 public void endElse() throws IOException { 515 end(false); 516 write(" else "); 517 } 518 519 public void endElseBegin() throws IOException { 520 end(false); 521 write(" else "); 522 begin(); 523 } 524 525 public void endElseBeginIf(String predicate) throws IOException { 526 end(false); 527 write(" else "); 528 beginIf(predicate); 529 } 530 531 public final static int rightMarginColumn = 76; 532 public void beginFor(String init, String predicate, String next) throws IOException { 533 int indentLength; 534 if ("\t".equals(indentString)) 535 indentLength = 4; 536 else 537 indentLength = indentString.length(); 538 int horizPosition = indentLength * indentLevel[curOut]; 539 write("for ("); 540 horizPosition += 5; 541 write(init, "; "); 542 horizPosition += init.length() + 2; 543 int nextHorizPosition = horizPosition + predicate.length() + 2; 544 if (nextHorizPosition >= rightMarginColumn) { 545 cr(); 546 indentOneLevel(); 547 horizPosition = indentLength * indentLevel[curOut]; 548 nextHorizPosition = horizPosition + predicate.length() + 2; 549 } 550 write(predicate, "; "); 551 horizPosition = nextHorizPosition; 552 nextHorizPosition = horizPosition + next.length() + 2; 553 if (nextHorizPosition >= rightMarginColumn) { 554 cr(); 555 indentOneLevel(); 556 } 557 write(next, ") "); 558 horizPosition = nextHorizPosition; 559 begin(); 560 } 561 562 public void beginWhile(String predicate) throws IOException { 563 write("while ("); 564 write(predicate); 565 write(") "); 566 begin(); 567 } 568 569 public void writeAssert(String predicate) throws IOException { 570 write("assert "); 571 write(predicate); 572 eol(); 573 } 574 575 public void comment(String msg) throws IOException { 576 write("// ", msg); 577 cr(); 578 } 579 580 public void bigComment(String msg) throws IOException { 581 writecr("/**"); 582 write(" * "); 584 int length = msg.length(); 585 for (int i = 0; i < length; ++i) { 586 char c = msg.charAt(i); 587 if (c == '\n') { 588 cr(); 589 write(" * "); 590 } else if (c == '*' && i+1 < length && msg.charAt(i+1) == '/') { 591 write("* /"); 592 ++i; 593 } else { 594 write(c); 595 } 596 } 597 cr(); 598 writecr(" */"); 599 } 600 } 601 | Popular Tags |