KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Colapi


1 /*
2  * Colapi - Code Colorer for Java API
3  * Public Domain (latest version at http://javolution.org/colapi.jar)
4  */

5 import java.io.BufferedReader JavaDoc;
6 import java.io.File JavaDoc;
7 import java.io.FileFilter JavaDoc;
8 import java.io.FileInputStream JavaDoc;
9 import java.io.FileOutputStream JavaDoc;
10 import java.io.InputStreamReader JavaDoc;
11 import java.io.OutputStreamWriter JavaDoc;
12 import java.util.HashSet JavaDoc;
13
14 public class Colapi {
15
16     public static final String JavaDoc VERSION = "1.2";
17
18     private static final String JavaDoc 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 JavaDoc ENCODING = System.getProperty("encoding",
33             "UTF-8");
34
35     private static final String JavaDoc KEYWORD_COLOR = System.getProperty(
36             "keywordColor", "#7F0055");
37
38     private static final String JavaDoc COMMENT_COLOR = System.getProperty(
39             "commentColor", "#3F7F5F");
40
41     private static final String JavaDoc 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 JavaDoc[] args) throws Exception JavaDoc {
49         if (args.length == 0) {
50             System.out.println(USAGE);
51             return;
52         }
53         File JavaDoc file = new File JavaDoc(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 JavaDoc dir) throws Exception JavaDoc {
68         File JavaDoc[] files = dir.listFiles(new FileFilter JavaDoc() {
69             public boolean accept(File JavaDoc 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 JavaDoc[] dirs = dir.listFiles(new FileFilter JavaDoc() {
78             public boolean accept(File JavaDoc 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 JavaDoc file) throws Exception JavaDoc {
88         Colapi.Processed++;
89         BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
90                 new FileInputStream JavaDoc(file), ENCODING));
91         StringBuffer JavaDoc doc = new StringBuffer JavaDoc(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             // Escape substitution.
98
if (state != DATA) {
99                 if (read == '<') {
100                     doc.append("&lt;");
101                 } else if (read == '>') {
102                     doc.append("&gt;");
103                 } else if (read == '&') {
104                     doc.append("&amp;");
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                             && // code identifier.
150
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)) { // End of identifier.
156
String JavaDoc name = doc.substring(start, doc.length() - 1);
157                         if (IDENTIFIERS.contains(name)) { // Identifier found.
158
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 JavaDoc out = new OutputStreamWriter JavaDoc(
179                     new FileOutputStream JavaDoc(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; // Can only be end of line comments.
192

193     private static final int STRING_LITERAL = 4;
194
195     private static final String JavaDoc[] 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 JavaDoc IDENTIFIERS = new HashSet JavaDoc();
206     static {
207         for (int i = 0; i < KEYWORDS.length; i++) {
208             IDENTIFIERS.add(KEYWORDS[i]);
209         }
210     }
211 }
Free Books   Free Magazines  
Popular Tags