1 5 import java.io.BufferedReader ; 6 import java.io.File ; 7 import java.io.FileFilter ; 8 import java.io.FileInputStream ; 9 import java.io.FileOutputStream ; 10 import java.io.InputStreamReader ; 11 import java.io.OutputStreamWriter ; 12 import java.util.HashSet ; 13 14 public class Colapi { 15 16 public static final String VERSION = "1.2"; 17 18 private static final String USAGE = "\n" 19 + "Code Colorer for Java API - Version " + VERSION + "\n" 20 + "Tool to colorize/format code between [code]...[/code]\n" + "\n" 21 + "Usage:" + " java -jar file.html [properties]\n" 22 + " (color one html file)\n" 23 + " or java -jar directory [properties]\n" 24 + " (color all html files in directory)\n" + "\n" 25 + "where properties include:\n" 26 + " -Dencoding=<value> File encoding (default \"UTF-8\")\n" 27 + " -DkeywordColor=<value> Keyword color (default \"#7F0055\")\n" 28 + " -DcommentColor=<value> Comment color (default \"#3F7F5F\")\n" 29 + " -DstringColor=<value> String color (default \"#0000A0\")\n" 30 + "\n"; 31 32 private static final String ENCODING = System.getProperty("encoding", 33 "UTF-8"); 34 35 private static final String KEYWORD_COLOR = System.getProperty( 36 "keywordColor", "#7F0055"); 37 38 private static final String COMMENT_COLOR = System.getProperty( 39 "commentColor", "#3F7F5F"); 40 41 private static final String STRING_COLOR = System.getProperty( 42 "stringColor", "#0000A0"); 43 44 private static int Processed = 0; 45 46 private static int Modified = 0; 47 48 public static void main(String [] args) throws Exception { 49 if (args.length == 0) { 50 System.out.println(USAGE); 51 return; 52 } 53 File file = new File (args[0]); 54 if (!file.exists()) { 55 System.err.println(args[0] + " is not a file or directory."); 56 return; 57 } 58 if (file.isDirectory()) { 59 processDirectory(file); 60 } else { 61 processFile(file); 62 } 63 System.out.println("Colapi processed " + Colapi.Processed 64 + " files and modified " + Colapi.Modified); 65 } 66 67 private static void processDirectory(File dir) throws Exception { 68 File [] files = dir.listFiles(new FileFilter () { 69 public boolean accept(File pathname) { 70 return pathname.isFile() 71 && pathname.getName().endsWith(".html"); 72 } 73 }); 74 for (int i = 0; i < files.length; i++) { 75 processFile(files[i]); 76 } 77 File [] dirs = dir.listFiles(new FileFilter () { 78 public boolean accept(File pathname) { 79 return pathname.isDirectory(); 80 } 81 }); 82 for (int i = 0; i < dirs.length; i++) { 83 processDirectory(dirs[i]); 84 } 85 } 86 87 private static void processFile(File file) throws Exception { 88 Colapi.Processed++; 89 BufferedReader in = new BufferedReader (new InputStreamReader ( 90 new FileInputStream (file), ENCODING)); 91 StringBuffer doc = new StringBuffer (10000); 92 int start = -1; 93 int state = DATA; 94 boolean modified = false; 95 for (int read = in.read(); read != -1; read = in.read()) { 96 97 if (state != DATA) { 99 if (read == '<') { 100 doc.append("<"); 101 } else if (read == '>') { 102 doc.append(">"); 103 } else if (read == '&') { 104 doc.append("&"); 105 } else { 106 doc.append((char) read); 107 } 108 } else { 109 doc.append((char) read); 110 } 111 switch (state) { 112 case DATA: 113 if ((read == ']') 114 && (doc.length() > 5) 115 && doc.substring(doc.length() - 6) 116 .equalsIgnoreCase("[code]")) { 117 doc.setLength(doc.length() - 6); 118 doc.append("<code><pre>"); 119 modified = true; 120 state = CODE; 121 } 122 break; 123 124 case CODE: 125 if (Character.isJavaIdentifierPart((char)read)) { 126 state = IDENTIFIER; 127 start = doc.length() - 1; 128 } else if (read == '"') { 129 state = STRING_LITERAL; 130 doc.insert(doc.length() - 1, "<font color=\"" 131 + STRING_COLOR + "\">"); 132 } else if ((read == '/') 133 && (doc.charAt(doc.length() - 2) == '/')) { 134 state = COMMENT; 135 doc.insert(doc.length() - 2, "<font color=\"" 136 + COMMENT_COLOR + "\">"); 137 } 138 break; 139 140 case STRING_LITERAL: 141 if ((read == '"') && (doc.charAt(doc.length() - 2) != '\\')) { 142 doc.append("</font>"); 143 state = CODE; 144 } 145 break; 146 147 case IDENTIFIER: 148 if ((read == ']') 149 && doc.substring(doc.length() - 7).equalsIgnoreCase( 151 "[/code]")) { 152 doc.setLength(doc.length() - 7); 153 doc.append("</pre></code>"); 154 state = DATA; 155 } else if (!Character.isJavaIdentifierPart((char)read)) { String name = doc.substring(start, doc.length() - 1); 157 if (IDENTIFIERS.contains(name)) { doc.insert(start + name.length(), "</b></font>"); 159 doc.insert(start, "<font color=\"" + KEYWORD_COLOR 160 + "\"><b>"); 161 } 162 state = CODE; 163 } 164 break; 165 166 case COMMENT: 167 if ((read == '\n') || (read == '\r')) { 168 doc.insert(doc.length() - 1, "</font>"); 169 state = CODE; 170 } 171 break; 172 } 173 } 174 in.close(); 175 176 if (modified) { 177 Colapi.Modified++; 178 OutputStreamWriter out = new OutputStreamWriter ( 179 new FileOutputStream (file), ENCODING); 180 out.write(doc.toString()); 181 out.close(); 182 } 183 } 184 185 private static final int DATA = 0; 186 187 private static final int CODE = 1; 188 189 private static final int IDENTIFIER = 2; 190 191 private static final int COMMENT = 3; 193 private static final int STRING_LITERAL = 4; 194 195 private static final String [] KEYWORDS = { "abstract", "continue", "for", 196 "new", "switch", "assert", "default", "if", "package", 197 "synchronized", "boolean", "do", "goto", "private", "this", 198 "break", "double", "implements", "protected", "throw", "byte", 199 "else", "import", "public", "throws", "case", "enum", "instanceof", 200 "return", "transient", "catch", "extends", "int", "short", "try", 201 "char", "final", "interface", "static", "void", "class", "finally", 202 "long", "strictfp", "volatile", "const", "float", "native", 203 "super", "while" }; 204 205 private static final HashSet IDENTIFIERS = new HashSet (); 206 static { 207 for (int i = 0; i < KEYWORDS.length; i++) { 208 IDENTIFIERS.add(KEYWORDS[i]); 209 } 210 } 211 }
| Popular Tags
|