1 package codeswitcher; 2 3 72 73 import java.io.File ; 74 import java.io.FileReader ; 75 import java.io.FileWriter ; 76 import java.io.IOException ; 77 import java.io.LineNumberReader ; 78 import java.util.Vector ; 79 80 88 110 public class CodeSwitcher { 111 112 private static final String ls = System.getProperty("line.separator", 113 "\n"); 114 private Vector vList; 115 private Vector vSwitchOn; 116 private Vector vSwitchOff; 117 private Vector vSwitches; 118 private boolean bAdd, bRemove; 119 private static final int MAX_LINELENGTH = 82; 120 private static boolean quiet; 121 123 129 public static void main(String a[]) { 130 131 CodeSwitcher s = new CodeSwitcher(); 132 133 if (a.length == 0) { 134 showUsage(); 135 136 return; 137 } 138 139 boolean path = false; 140 141 for (int i = 0; i < a.length; i++) { 142 String p = a[i]; 143 if (p.equalsIgnoreCase("/strip")) { 144 System.err.println("WARNING: /STRIP is deprecated, now always strips. "); 145 } 147 else if (p.startsWith("#")) { 148 String opt = p.substring(1); 149 if(opt.equalsIgnoreCase("strip")) { 150 System.err.println("WARNING: #STRIP is deprecated, now always strips. "); 151 } 153 else if(opt.equalsIgnoreCase("quiet")) { 154 quiet = true; 155 } 156 } 157 else if (p.startsWith("+")) { 158 if (p.length() == 1) { 159 s.bAdd = true; 160 } else { 161 s.vSwitchOn.addElement(p.substring(1)); 162 } 163 } else if (p.startsWith("-")) { 164 if (p.length() == 1) { 165 s.bRemove = true; 166 } else { 167 s.vSwitchOff.addElement(p.substring(1)); 168 } 169 } else { 170 s.addDir(p); 171 172 path = true; 173 } 174 } 175 176 if (!path) { 177 printError("no path specified"); 178 showUsage(); 179 } 180 181 s.process(); 182 183 if (s.vSwitchOff.size() == 0 && s.vSwitchOn.size() == 0) { 184 s.printSwitches(); 185 } 186 } 187 188 192 static void showUsage() { 193 194 System.out.print("Usage: java CodeSwitcher [paths] [labels] [+][-]\n" 195 + "If no labels are specified then all used\n" 196 + "labels in the source code are shown.\n" 197 + "Use +MODE to switch on the things labeld MODE\n" 198 + "Use -MODE to switch off the things labeld MODE\n" 199 + "Path: Any number of path or files may be\n" 200 + "specified. Use . for the current directory\n" 201 + "(including sub-directories).\n" 202 + "Example: java CodeSwitcher +JAVA2 .\n" 203 + "This example switches on code labeled JAVA2\n" 204 + "in all *.java files in the current directory\n" 205 + "and all subdirectories.\n" 206 + "java CodeSwitcher + .\n" 207 + "Adds test code to the code.\n" 208 + "java CodeSwitcher - .\n" 209 + "Removed test code from the code.\n"); 210 } 211 212 216 CodeSwitcher() { 217 218 vList = new Vector (); 219 vSwitchOn = new Vector (); 220 vSwitchOff = new Vector (); 221 vSwitches = new Vector (); 222 } 223 224 228 void process() { 229 230 int len = vList.size(); 231 232 for (int i = 0; i < len; i++) { 233 235 String file = (String ) vList.elementAt(i); 236 237 if (bAdd || bRemove) { 238 int maxlen = testFile(file); 239 240 if (bAdd &&!bRemove) { 241 addTest(file, maxlen); 242 } else { 243 removeTest(file); 244 } 245 } else { 246 if (!processFile(file)) { 247 printError("in file " + file + " !"); 248 } 249 } 250 } 251 252 System.out.println(""); 253 } 254 255 259 void printSwitches() { 260 261 System.out.println("Used labels:"); 262 263 for (int i = 0; i < vSwitches.size(); i++) { 264 System.out.println((String ) (vSwitches.elementAt(i))); 265 } 266 } 267 268 274 void addDir(String path) { 275 File f = new File (path); 276 277 if (f.isFile() && path.endsWith(".java")) { 278 vList.addElement(path); 279 } else if (f.isDirectory()) { 280 String list[] = f.list(); 281 282 for (int i = 0; i < list.length; i++) { 283 addDir(path + File.separatorChar + list[i]); 284 } 285 } 286 } 287 288 294 void removeTest(String name) { 295 296 File f = new File (name); 297 File fnew = new File (name + ".new"); 298 299 LineNumberReader read = null; 300 FileWriter write = null; 301 302 try { 303 read = new LineNumberReader (new FileReader (f)); 304 write = new FileWriter (fnew); 305 306 while (true) { 307 String line = read.readLine(); 308 309 if (line == null) { 310 break; 311 } 312 313 if (line.startsWith("Profile.visit(")) { 314 int s = line.indexOf(';'); 315 316 line = line.substring(s + 1); 317 } 318 319 write.write(line + ls); 320 } 321 322 read.close(); 323 read = null; 324 write.flush(); 325 write.close(); 326 write = null; 327 328 File fbak = new File (name + ".bak"); 329 330 fbak.delete(); 331 f.renameTo(fbak); 332 333 File fcopy = new File (name); 334 335 fnew.renameTo(fcopy); 336 fbak.delete(); 337 } catch (Exception e) { 338 printError(e.getMessage()); 339 } 340 finally { 341 if(read != null) { 342 try { 343 read.close(); 344 } catch (IOException e1) { 345 } 346 } 347 if(write != null) { 348 try { 349 write.close(); 350 } catch (IOException e1) { 351 } 352 } 353 } 354 } 355 356 363 void addTest(String name, int maxline) { 364 365 File f = new File (name); 366 File fnew = new File (name + ".new"); 367 String key = name; 368 369 key = key.replace('\\', '.'); 370 LineNumberReader read = null; 371 FileWriter write = null; 372 373 try { 374 read = new LineNumberReader (new FileReader (f)); 375 write = new FileWriter (fnew); 376 int l = 0; 377 boolean longline = false; 378 379 while (true) { 380 String line = read.readLine(); 381 382 if (line == null) { 383 break; 384 } 385 386 if (line.startsWith(" ")) { 387 int spaces = 0; 388 389 for (; spaces < line.length(); spaces++) { 390 if (line.charAt(spaces) != ' ') { 391 break; 392 } 393 } 394 395 if (spaces > 3 && testLine(line) &&!longline) { 396 line = "org.hsqldb.test.Profile.visit(\"" + key 397 + "\"," + l + "," + maxline + ");" + line; 398 399 l++; 400 } else if (isLongline(line)) { 401 longline = true; 402 } else { 403 longline = false; 404 } 405 } 406 407 write.write(line + ls); 408 } 409 410 read.close(); 411 read = null; 412 write.flush(); 413 write.close(); 414 write = null; 415 416 File fbak = new File (name + ".bak"); 417 418 fbak.delete(); 419 f.renameTo(fbak); 420 421 File fcopy = new File (name); 422 423 fnew.renameTo(fcopy); 424 fbak.delete(); 425 } catch (Exception e) { 426 printError(e.getMessage()); 427 } 428 finally { 429 430 if(read != null) { 431 try { 432 read.close(); 433 } catch (IOException e1) { 434 } 435 } 436 if(write != null) { 437 try { 438 write.close(); 439 } catch (IOException e1) { 440 } 441 } 442 } 443 } 444 445 453 int testFile(String name) { 454 455 File f = new File (name); 456 LineNumberReader read = null; 457 458 try { 459 read = new LineNumberReader (new FileReader (f)); 460 int l = 1, 461 maxline = 0; 462 boolean longline = false; 463 464 while (true) { 465 String line = read.readLine(); 466 467 if (line == null) { 468 break; 469 } 470 471 if (line.length() > MAX_LINELENGTH 472 &&!line.startsWith("org.hsqldb.test.Profile.")) { 473 System.out.println("long line in " + name + " at line " 474 + l); 475 } 476 477 if (line.startsWith(" ")) { 478 int spaces = 0; 479 480 for (; spaces < line.length(); spaces++) { 481 if (line.charAt(spaces) != ' ') { 482 break; 483 } 484 } 485 486 if (spaces > 3 && testLine(line) &&!longline) { 487 maxline++; 488 } else if (isLongline(line)) { 489 longline = true; 490 } else { 491 longline = false; 492 } 493 494 String s = line.substring(spaces); 495 496 if (s.startsWith("if(")) { 497 if (!s.endsWith(" {")) { 498 System.out.println("if( without { in " + name 499 + " at line " + l); 500 } 501 } else if (s.startsWith("} else if(")) { 502 if (!s.endsWith(" {")) { 503 System.out.println("} else if without { in " 504 + name + " at line " + l); 505 } 506 } else if (s.startsWith("while(")) { 507 if (!s.endsWith(" {")) { 508 System.out.println("while( without { in " + name 509 + " at line " + l); 510 } 511 } else if (s.startsWith("switch(")) { 512 if (!s.endsWith(" {")) { 513 System.out.println("switch( without { in " + name 514 + " at line " + l); 515 } 516 } else if (s.startsWith("do ")) { 517 if (!s.endsWith(" {")) { 518 System.out.println("do without { in " + name 519 + " at line " + l); 520 } 521 } 522 } 523 524 l++; 525 } 526 527 return maxline; 528 } catch (Exception e) { 529 printError(e.getMessage()); 530 } 531 finally { 532 533 if(read != null) { 534 try { 535 read.close(); 536 } catch (IOException e1) { 537 } 538 } 539 } 540 541 return -1; 542 } 543 544 552 boolean testLine(String line) { 553 554 if (!line.endsWith(";")) { 555 return false; 556 } 557 558 if (line.trim().startsWith("super(")) { 559 return false; 560 } 561 562 return true; 563 } 564 565 573 boolean isLongline(String s) { 574 575 char c = s.charAt(s.length() - 1); 576 577 if (",(+-&|".indexOf(c) >= 0) { 578 return true; 579 } 580 581 return false; 582 } 583 584 592 boolean processFile(String name) { 593 594 File f = new File (name); 595 File fnew = new File (name + ".new"); 596 int state = 0; boolean switchoff = false; 598 boolean working = false; 599 int removeFrom = -1; 600 601 try { 602 Vector v = getFileLines(f); 603 Vector v1 = new Vector (v.size()); 604 605 for (int i = 0; i < v.size(); i++) { 606 v1.addElement(v.elementAt(i)); 607 } 608 609 for (int i = 0; i < v.size(); i++) { 610 String line = (String ) v.elementAt(i); 611 612 if (line == null) { 613 break; 614 } 615 616 String lineTrimmed = trimBoth(line); 617 String lineStripped = stripSpaces(line); 618 619 if (working) { 620 if (lineTrimmed.equals("/*") || lineTrimmed.equals("*/")) { 621 v.removeElementAt(i--); 622 continue; 623 } 624 else if(lineTrimmed.startsWith("*")) { 625 int idx = line.indexOf('*'); 626 v.setElementAt(line.substring(0, idx) + line.substring(idx + 1), i); 627 } 628 629 } 630 631 if (lineStripped.indexOf("//#") != -1) { 632 if (lineStripped.startsWith("//#ifdef")) { 633 if (state != 0) { 634 printError( 635 "'#ifdef' not allowed inside '#ifdef'"); 636 637 return false; 638 } 639 640 state = 1; 641 removeFrom = -1; 642 643 String s = lineStripped.substring(8); 644 645 if ( vSwitchOn.indexOf(s) != -1) { 646 647 v.remove(i--); 649 printMessage(f, i, "Including " + s); 651 working = true; 652 switchoff = false; 653 } else if (vSwitchOff.indexOf(s) != -1) { 654 printMessage(f, i, "Excluding " + s); 655 working = true; 656 removeFrom = i; 661 663 switchoff = true; 664 } 665 666 if (vSwitches.indexOf(s) == -1) { 667 vSwitches.addElement(s); 668 } 669 } else if (lineStripped.startsWith("//#else")) { 670 if (state != 1) { 671 printError("'#else' without '#ifdef'"); 672 673 return false; 674 } 675 676 state = 2; 677 678 if (!working) {} 679 else if (switchoff) { 680 if(removeFrom != -1) { 691 for(int j = i ; j >= removeFrom; j--) { 692 v.remove(j); 693 i--; 694 } 695 removeFrom = -1; 696 } 697 699 switchoff = false; 700 } else { 701 printMessage(f, i, "Will remove from " + i); 706 removeFrom = i; 707 709 switchoff = true; 710 } 711 } else if (lineStripped.startsWith("//#endif")) { 712 if (state == 0) { 713 printError("'#endif' without '#ifdef'"); 714 715 return false; 716 } 717 718 state = 0; 719 720 if (working) { 721 if(switchoff) { 722 if (v.elementAt(i - 1).equals("")) { 723 v.insertElementAt("*/", i - 1); 725 i++; 726 } else { 728 v.insertElementAt("*/", i++); 730 } 732 733 if(removeFrom != -1) { 734 printMessage(f, removeFrom, "Removing " + ( i - removeFrom) + " lines"); 735 for(int j = i ; j >= removeFrom; j--) { 736 printMessage(f, i, "Removing '" + v.remove(j) + "'"); 737 i--; 738 } 739 removeFrom = -1; 740 } 741 } 742 else { 743 v.removeElementAt(i--); 745 } 747 } 748 749 working = false; 750 switchoff = false; 751 } else {} 752 } 753 } 754 755 if (state != 0) { 756 printError("'#endif' missing"); 757 758 return false; 759 } 760 761 boolean filechanged = false; 762 763 for (int i = 0; i < v.size(); i++) { 764 if (!v1.elementAt(i).equals(v.elementAt(i))) { 765 filechanged = true; 766 767 break; 768 } 769 } 770 771 if (!filechanged) { 772 return true; 773 } 774 775 writeFileLines(v, fnew); 776 777 File fbak = new File (name + ".bak"); 778 779 fbak.delete(); 780 f.renameTo(fbak); 781 782 File fcopy = new File (name); 783 784 fnew.renameTo(fcopy); 785 fbak.delete(); 786 787 return true; 788 } catch (Exception e) { 789 printError(e.getMessage()); 790 791 return false; 792 } 793 } 794 795 static Vector getFileLines(File f) throws IOException { 796 797 LineNumberReader read = null ; 798 Vector v = new Vector (); 799 try { 800 read = new LineNumberReader (new FileReader (f)); 801 802 for (;;) { 803 String line = read.readLine(); 804 805 if (line == null) { 806 break; 807 } 808 809 v.addElement(line); 810 } 811 } 812 finally { 813 if(read != null) { 814 read.close(); 815 } 816 } 817 818 return v; 819 } 820 821 static String trimBoth(String text) { 822 text = text.trim(); 823 int s = text.length(); 824 char ch; 825 int i; 826 for(i = 0 ; i < s && Character.isWhitespace((ch = text.charAt(i))); i++); 827 return i < s ? text.substring(i) : text; 828 } 829 830 static String stripSpaces(String text) { 831 StringBuffer buf = new StringBuffer (); 832 int s = text.length(); 833 char ch; 834 for(int i = 0 ; i < s; i++) { 835 ch = text.charAt(i); 836 if(!Character.isWhitespace(ch)) { 837 buf.append(ch); 838 } 839 } 840 return buf.toString(); 841 } 842 843 static void writeFileLines(Vector v, File f) throws IOException { 844 845 FileWriter write = null; 846 try { 847 write = new FileWriter (f); 848 849 for (int i = 0; i < v.size(); i++) { 850 write.write((String ) v.elementAt(i)); 851 write.write(ls); 852 } 853 854 write.flush(); 855 } 856 finally { 857 if(write != null ) { 858 write.close(); 859 860 } 861 } 862 } 863 864 870 static void printError(String error) { 871 System.out.println("ERROR: " + error); 872 } 873 874 880 static void printMessage(File f, int line, String message) { 881 if(!quiet) 882 System.out.println("MSG: " + f.getName() + "[" + line + "] " + message); 883 } 884 885 public static void Xmain(String args[]) { 886 System.out.println(stripSpaces(trimBoth("//#ifdef XTRA"))); 887 System.out.println(stripSpaces(trimBoth("\t\t \t//#ifdef XTRA"))); 888 System.out.println(stripSpaces(trimBoth("\t\t \t//# ifdef XTRA"))); 889 System.out.println(stripSpaces(trimBoth("\t\t \t// # ifdef XTRA"))); 890 System.out.println(stripSpaces(trimBoth("//# ifdef XTRA"))); 891 System.out.println(stripSpaces(trimBoth("// # ifdef\t \t XTRA"))); 892 System.out.println(stripSpaces(trimBoth("// # ifdef XTRA"))); 893 } 894 } 895 | Popular Tags |