1 9 package org.infozone.tools.janalyzer; 10 11 import koala.dynamicjava.tree.*; 12 13 import java.io.File ; 14 import java.io.FileReader ; 15 import java.io.BufferedReader ; 16 import java.io.FileNotFoundException ; 17 import java.io.IOException ; 18 import java.io.PrintStream ; 19 import java.io.FileOutputStream ; 20 21 import java.util.Vector ; 22 import java.util.Collections ; 23 24 import org.apache.regexp.RE; 25 import org.apache.regexp.RESyntaxException; 26 27 28 50 public final class JavaCodeOutput extends java.lang.Object { 51 52 56 57 private int LINE_LENGTH; 58 59 private final static int LOW_SPLIT_LEVEL = 10; 60 private final static int MIDDLE_SPLIT_LEVEL = 25; 61 private final static int HIGH_SPLIT_LEVEL = 50; 62 private final static int OBLIGATION_SPLIT_LEVEL = 99; 63 64 private final static String ONE_INDENT = " "; 65 66 private final static String WRAP_INDENT = " "; 67 68 private final static String DOC_COMMENT_START = "/" + "**"; 70 71 private final static String BLOCK_COMMENT_START = "/" + "*"; 73 74 private final static String LINE_COMMENT_START = "/" + "/"; 76 77 private final static String BLOCK_COMMENT_LINE = "*"; 78 79 private final static String BLOCK_COMMENT_END = "*" + "/"; 80 81 private final static String [] DOC_COMMENT_START_LINES = {DOC_COMMENT_START}; 82 83 private final static String [] DOC_COMMENT_END_LINES = {BLOCK_COMMENT_END}; 84 85 95 private final static String SPLIT_CHARACTER = "§"; 96 97 private final static boolean PRINT_LINE_NUMBERS = true; 98 99 103 108 private int splitLevel = LOW_SPLIT_LEVEL + 1; 109 112 private boolean wrapIndentAdded = false; 113 114 private String indent = ""; 115 116 117 private int commentStart = 0; 118 119 private int commentEnd = 0; 120 121 122 private PrintStream outStream = new PrintStream ( System.out ); 123 124 127 Vector origLines = new Vector (); 128 129 130 public JavaCodeOutput( File fileIn, String filenameOut, String lineLength ) { 131 try { 132 BufferedReader br = new BufferedReader ( new FileReader ( fileIn ) ); 133 while (br.ready()) { 134 origLines.add( br.readLine() ); 135 } 136 br.close(); 137 } catch (FileNotFoundException e) { 138 printEx( "File " + fileIn.getName() + " not found!", e ); 139 } catch (IOException e) { 140 printEx( " ", e ); 141 } 142 try { 143 if (filenameOut != null) { 144 File file = new File ( filenameOut ); 145 FileOutputStream fos = new FileOutputStream ( file ); 146 outStream = new PrintStream ( fos ); 147 } 148 } catch (FileNotFoundException e) { 149 printEx( "File " + filenameOut + " not found!", e ); 150 } catch (IOException e) { 151 printEx( " ", e ); 152 } 153 try { 154 if (lineLength != null) { 155 LINE_LENGTH = Integer.parseInt( lineLength ); 156 } else { 157 LINE_LENGTH = 80; 158 } 159 } catch (NumberFormatException e) { 160 printEx( "Line length " + lineLength + " is not a valid number!", e ); 161 } 162 } 163 164 165 169 170 173 public void increaseIndent() { 174 indent += ONE_INDENT; 175 } 176 177 178 181 protected void decreaseIndent() { 182 if (indent.length() > 2) { 183 indent = indent.substring( 0, indent.length() - ONE_INDENT.length() ); 184 } 185 } 186 187 188 193 private void addWrapIndent() { 194 if (!wrapIndentAdded) { 195 wrapIndentAdded = true; 196 indent += WRAP_INDENT; 197 } 198 } 199 200 201 205 private void deleteWrapIndent() { 206 if (wrapIndentAdded) { 207 wrapIndentAdded = false; 208 indent = indent.substring( 0, indent.length() - WRAP_INDENT.length() ); 209 } 210 } 211 212 213 217 218 224 private String getLineNumber() { 225 return ""; 226 } 227 228 229 233 234 237 public String mask( String aText ) { 238 try { 239 RE regexp = new RE( SPLIT_CHARACTER ); 240 aText = regexp.subst( aText, SPLIT_CHARACTER + SPLIT_CHARACTER ); 241 } catch (RESyntaxException e) { 242 printEx( "false regexp in mask", e ); 243 } 244 return aText; 245 } 246 247 248 256 public String getSplitLevel() { 257 return SPLIT_CHARACTER + splitLevel; 258 } 259 260 261 264 public String getLowSplitLevel() { 265 return SPLIT_CHARACTER + LOW_SPLIT_LEVEL; 266 } 267 268 269 272 public String getMiddleSplitLevel() { 273 return SPLIT_CHARACTER + MIDDLE_SPLIT_LEVEL; 274 } 275 276 277 280 public String getHighSplitLevel() { 281 return SPLIT_CHARACTER + HIGH_SPLIT_LEVEL; 282 } 283 284 285 292 public String getObligateSplitLevel() { 293 return SPLIT_CHARACTER + OBLIGATION_SPLIT_LEVEL; 294 } 295 296 297 300 public String getNextSplitLevel() { 301 return SPLIT_CHARACTER + splitLevel++; 302 } 303 304 305 public void resetSplitLevel() { 306 splitLevel = LOW_SPLIT_LEVEL + 1; 307 } 308 309 310 314 315 321 public void println( String aLine ) { 322 String output = cleanOutputLine( aLine ); 323 output = getLineNumber() + indent + output; 324 if (output.length() <= LINE_LENGTH) { 326 outStream.println( output ); 328 } else { 329 int index = 0; 338 if ((index = aLine.indexOf( getObligateSplitLevel() )) != -1 && (index == 0 || index > 0 && !(aLine.charAt( 340 index - 1 ) == SPLIT_CHARACTER.charAt( 0 )))) { 341 println( aLine.substring( 0, index ) ); 343 aLine = aLine.substring( index + getObligateSplitLevel().length() ); 344 printDB( "ObligateSplitLevel found on line in if " + getLineNumber() + " index is " + index 345 + " aLine is " + aLine ); 346 addWrapIndent(); 347 while ((index = aLine.indexOf( getObligateSplitLevel() )) != -1 && (index == 0 || index > 0 349 && !(aLine.charAt( index - 1 ) == SPLIT_CHARACTER.charAt( 0 )))) { 350 println( aLine.substring( 0, index ) ); 352 aLine = aLine.substring( index + getObligateSplitLevel().length() ); 353 printDB( "ObligateSplitLevel found on line in while " + getLineNumber() + " index is " + index 354 + " aLine is " + aLine ); 355 } 356 println( aLine ); 358 deleteWrapIndent(); 359 return; 361 } 362 if ((output = breakThisLine( aLine, getHighSplitLevel() )) != null) { 364 if ((output = breakThisLine( output, getMiddleSplitLevel() )) != null) { 365 if ((output = breakThisLine( output, getLowSplitLevel() )) != null) { 366 outStream.println( getLineNumber() + indent + cleanOutputLine( output ) ); 367 deleteWrapIndent(); 368 } 369 } 370 } 371 } 372 } 373 374 375 private String breakThisLine( String aLine, String aSplitter ) { 376 int index = LINE_LENGTH + 8 * aSplitter.length(); 377 String output = null; 378 while ((index = aLine.lastIndexOf( aSplitter, 380 index )) != -1 && (index == 0 || index > 0 && !(aLine.charAt( index - 1 ) == SPLIT_CHARACTER.charAt( 381 0 )))) { 382 output = getLineNumber() + indent + cleanOutputLine( aLine.substring( 0, index ) ); 384 if (output.length() <= LINE_LENGTH) { 385 outStream.println( output ); 387 if (index + aSplitter.length() < aLine.length()) { 389 addWrapIndent(); 391 println( aLine.substring( index + aSplitter.length() ).trim() ); 392 deleteWrapIndent(); 393 } 394 return null; 395 } else { 396 --index; 399 } 400 } 401 printLog( "Line not splitable by Splitter " + aSplitter ); 404 return aLine; 406 } 407 408 409 413 private String cleanOutputLine( String aLine ) { 414 if (aLine.indexOf( SPLIT_CHARACTER ) != -1) { 415 try { 416 418 RE regexp = new RE( "[^" + SPLIT_CHARACTER + "](" + SPLIT_CHARACTER + "\\d\\d)" ); 419 RE regexptwo; 420 while (regexp.match( aLine )) { 421 regexptwo = new RE( regexp.getParen( 1 ) ); 422 aLine = regexptwo.subst( aLine, "" ); 423 } 424 425 regexp = new RE( "^(" + SPLIT_CHARACTER + "\\d\\d)" ); 427 aLine = regexp.subst( aLine, "" ); 428 429 regexp = new RE( SPLIT_CHARACTER + SPLIT_CHARACTER ); 431 aLine = regexp.subst( aLine, SPLIT_CHARACTER ); 432 } catch (Exception e) { 433 e.printStackTrace(); 434 } 435 } 436 return aLine; 437 } 438 439 440 444 445 public void setCommentStart( int start ) { 446 commentStart = start; 447 } 448 449 450 public void setCommentEnd( int end ) { 451 commentEnd = end; 452 } 453 454 455 459 public void printComment() { 460 printOrigComment( commentStart, commentEnd, 0 ); 461 } 462 463 464 468 public void printImportComment( Node aNode ) { 469 printComment(); 470 } 471 472 473 public void printPackageComment( Node aNode ) { 474 printComment(); 475 } 476 477 478 public void printClassComment( Node aNode ) { 479 printOrigComment( commentStart, commentEnd, 2 ); 480 } 481 482 483 public void printInterfaceComment( Node aNode ) { 484 printOrigComment( commentStart, commentEnd, 2 ); 485 } 486 487 488 public void printConstructorComment( Node aNode ) { 489 printOrigComment( commentStart, commentEnd, 2 ); 490 } 491 492 493 public void printMethodComment( MethodDeclaration method ) { 494 printOrigComment( commentStart, commentEnd, 2 ); 495 } 496 497 498 public void printFieldComment( Node aNode ) { 499 printComment(); 500 } 501 502 503 public void printVariableComment( Node aNode ) { 504 printComment(); 505 } 506 507 508 512 private void printComment( String aCommentLine ) { 513 outStream.println( getLineNumber() + indent + aCommentLine ); 514 } 515 516 517 521 522 527 private boolean printOrigComment( int startLine, int endLine, int insertLineCount ) { 528 529 if (startLine >= endLine) { 531 return false; 533 } 534 Vector lines = new Vector (); 535 536 boolean removeEmptyLines = false; 537 539 for (int i = 1; i < (commentEnd - commentStart); i++) { 540 String line = (String )origLines.elementAt( commentEnd - i - 1 ); 541 542 if (line.trim().length() != 0) { 543 removeEmptyLines = false; 544 lines.add( line ); 545 } else { 546 if (insertLineCount > 0) { 549 while (insertLineCount > 0) { 550 lines.add( "" ); 551 insertLineCount--; 552 } 553 removeEmptyLines = true; 554 } 555 if (!removeEmptyLines) { 556 lines.add( "" ); 557 } 558 } 559 } 560 561 while (insertLineCount > 0) { 562 lines.add( "" ); 563 insertLineCount--; 564 } 565 566 Collections.reverse( lines ); 567 568 String [] strings = new String [lines.size()]; 569 try { 570 System.arraycopy( lines.toArray(), 0, strings, 0, lines.size() ); 572 } catch (Exception e) { 573 e.printStackTrace(); 574 } 575 return printComment( strings ); 576 } 577 578 579 583 private boolean printComment( Vector someLines ) { 584 String [] lines = new String [someLines.size()]; 585 try { 586 System.arraycopy( someLines.toArray(), 0, lines, 0, someLines.size() ); 588 } catch (Exception e) { 589 e.printStackTrace(); 590 } 591 return printComment( lines ); 592 } 593 594 595 private boolean printComment( String [] lines ) { 596 String actLine; 598 boolean isDocComment = false; 600 boolean isBlockComment = false; 602 boolean commentExist = false; 604 605 for (int i = 0; i < lines.length; i++) { 607 609 actLine = lines[i].trim(); 610 if (actLine == null) { 611 actLine = ""; 612 } 613 if (actLine.startsWith( LINE_COMMENT_START )) { 615 if (!isBlockComment) { 617 printComment( actLine ); 619 continue; 620 } 621 } 622 if ((actLine.startsWith( DOC_COMMENT_START ) || actLine.startsWith( BLOCK_COMMENT_START )) 624 && actLine.endsWith( BLOCK_COMMENT_END )) { 625 printComment( actLine ); 627 commentExist = true; 628 continue; 629 } 630 if (actLine.indexOf( BLOCK_COMMENT_START ) != -1 && actLine.indexOf( BLOCK_COMMENT_END ) != -1) { 633 printComment( actLine.substring( actLine.indexOf( BLOCK_COMMENT_START ), 634 actLine.indexOf( BLOCK_COMMENT_END ) + BLOCK_COMMENT_END.length() ) ); 635 636 continue; 637 } 638 639 if (actLine.startsWith( BLOCK_COMMENT_END )) { 641 isDocComment = isBlockComment = false; 643 printComment( " " + BLOCK_COMMENT_END ); 644 continue; 645 } 646 if (actLine.indexOf( BLOCK_COMMENT_END ) != -1) { 648 isDocComment = isBlockComment = false; 649 650 int index = actLine.indexOf( BLOCK_COMMENT_END ); 651 printComment( printBlockCommentLine( actLine.substring( 0, index ) ) ); 652 printComment( " " + BLOCK_COMMENT_END ); 654 continue; 656 } 657 if (actLine.startsWith( DOC_COMMENT_START )) { 659 isDocComment = true; 660 commentExist = true; 661 662 printComment( DOC_COMMENT_START ); 663 if (actLine.length() > DOC_COMMENT_START.length()) { 665 printComment( printBlockCommentLine( actLine.substring( DOC_COMMENT_START.length() - 1 ) ) ); 666 } 667 continue; 668 } 669 if (actLine.startsWith( BLOCK_COMMENT_START )) { 670 isBlockComment = true; 673 674 printComment( BLOCK_COMMENT_START ); 675 if (actLine.length() > BLOCK_COMMENT_START.length()) { 677 printComment( printBlockCommentLine( actLine.substring( BLOCK_COMMENT_START.length() - 1 ) ) ); 678 } 679 continue; 680 } 681 if (isDocComment) { 683 printComment( printBlockCommentLine( actLine ) ); 685 continue; 686 } 687 if (isBlockComment) { 688 outStream.println( lines[i] ); 691 continue; 692 } 693 694 if (actLine.indexOf( LINE_COMMENT_START ) != -1) { 696 if (!isBlockComment) { 698 int index = actLine.indexOf( LINE_COMMENT_START ); 699 printComment( actLine.substring( index ) ); 700 } 701 continue; 702 } 703 if (actLine.length() < 1) { 705 printComment( "" ); 706 } 707 } 708 710 if (isDocComment || isBlockComment) { 712 printComment( " " + BLOCK_COMMENT_END ); 713 printLog( "Blockcomment not determinated!" ); 714 } 715 return commentExist; 716 } 717 718 719 725 private String printBlockCommentLine( String aLine ) { 726 if (aLine.startsWith( BLOCK_COMMENT_LINE )) { 727 return " " + aLine; 728 } else { 729 return " " + BLOCK_COMMENT_LINE + " " + aLine; 730 } 731 } 732 733 734 738 private void printLog( String aLine ) { 739 } 740 741 742 746 private void printDB( String aLine ) { 747 } 748 749 750 754 private void printEx( String aLine, Exception e ) { 755 System.err.println( getClass() + ": " + e ); 756 } 757 } 758 | Popular Tags |