1 39 40 43 44 45 import java.io.*; 46 import java.util.*; 47 48 79 public class CodeViewer { 80 81 private static HashMap reservedWords = new HashMap(); private boolean inMultiLineComment = false; 84 private String backgroundColor = "#ffffff"; 85 private String commentStart = "</font><font size=2 color=\"#0000aa\"><i>"; 86 private String commentEnd = "</font></i><font size=2 color=black>"; 87 private String stringStart = "</font><font size=2 color=\"#00bb00\">"; 88 private String stringEnd = "</font><font size=2 color=black>"; 89 private String reservedWordStart = "<b>"; 90 private String reservedWordEnd = "</b>"; 91 92 static { 93 loadHash(); 94 } 95 96 public CodeViewer() { 97 } 98 99 public void setCommentStart(String commentStart) { 100 this.commentStart = commentStart; 101 } 102 public void setCommentEnd(String commentEnd) { 103 this.commentEnd = commentEnd; 104 } 105 public void setStringStart(String stringStart) { 106 this.stringStart = stringStart; 107 } 108 public void setStringEnd(String stringEnd) { 109 this.stringEnd = stringEnd; 110 } 111 public void setReservedWordStart(String reservedWordStart) { 112 this.reservedWordStart = reservedWordStart; 113 } 114 public void setReservedWordEnd(String reservedWordEnd) { 115 this.reservedWordEnd = reservedWordEnd; 116 } 117 118 public String getCommentStart() { 119 return commentStart; 120 } 121 public String getCommentEnd() { 122 return commentEnd; 123 } 124 public String getStringStart() { 125 return stringStart; 126 } 127 public String getStringEnd() { 128 return stringEnd; 129 } 130 public String getReservedWordStart() { 131 return reservedWordStart; 132 } 133 public String getReservedWordEnd() { 134 return reservedWordEnd; 135 } 136 137 141 public String syntaxHighlight( String line ) { 142 return htmlFilter(line); 143 } 144 145 148 private String htmlFilter( String line ) { 149 if( line == null || line.equals("") ) { 150 return ""; 151 } 152 153 line = replace(line, "&", "&"); 155 156 line = replace(line, "\\\\", "\\" ); 159 160 line = replace(line, "" + (char)92 + (char)34, "\""); 162 163 line = replace(line, "<", "<"); 166 line = replace(line, ">", ">"); 169 170 return multiLineCommentFilter(line); 171 } 172 173 177 private String multiLineCommentFilter(String line) { 178 if (line == null || line.equals("")) { 179 return ""; 180 } 181 StringBuffer buf = new StringBuffer (); 182 int index; 183 if (inMultiLineComment && (index = line.indexOf("*/")) > -1 && !isInsideString(line,index)) { 185 inMultiLineComment = false; 186 buf.append(line.substring(0,index)); 187 buf.append("*/").append(commentEnd); 188 if (line.length() > index+2) { 189 buf.append(inlineCommentFilter(line.substring(index+2))); 190 } 191 return buf.toString(); 192 } 193 else if (inMultiLineComment) { 196 return line; 197 } 198 else if ((index = line.indexOf("/*")) > -1 && !isInsideString(line,index)) { 201 inMultiLineComment = true; 202 buf.append(inlineCommentFilter(line.substring(0,index))); 207 buf.append(commentStart).append("/*"); 208 buf.append(multiLineCommentFilter(line.substring(index+2))); 209 return buf.toString(); 210 } 211 else { 214 return inlineCommentFilter(line); 215 } 216 } 217 218 221 private String inlineCommentFilter(String line) { 222 if (line == null || line.equals("")) { 223 return ""; 224 } 225 StringBuffer buf = new StringBuffer (); 226 int index; 227 if ((index = line.indexOf("//")) > -1 && !isInsideString(line,index)) { 228 buf.append(stringFilter(line.substring(0,index))); 229 buf.append(commentStart); 230 buf.append(line.substring(index)); 231 buf.append(commentEnd); 232 } 233 else { 234 buf.append(stringFilter(line)); 235 } 236 return buf.toString(); 237 } 238 239 242 private String stringFilter(String line) { 243 if (line == null || line.equals("")) { 244 return ""; 245 } 246 StringBuffer buf = new StringBuffer (); 247 if (line.indexOf("\"") <= -1) { 248 return keywordFilter(line); 249 } 250 int start = 0; 251 int startStringIndex = -1; 252 int endStringIndex = -1; 253 int tempIndex; 254 while ((tempIndex = line.indexOf("\"")) > -1) { 256 if (startStringIndex == -1) { 258 startStringIndex = 0; 259 buf.append( stringFilter(line.substring(start,tempIndex)) ); 260 buf.append(stringStart).append("\""); 261 line = line.substring(tempIndex+1); 262 } 263 else { 265 startStringIndex = -1; 266 endStringIndex = tempIndex; 267 buf.append(line.substring(0,endStringIndex+1)); 268 buf.append(stringEnd); 269 line = line.substring(endStringIndex+1); 270 } 271 } 272 273 buf.append( keywordFilter(line) ); 274 275 return buf.toString(); 276 } 277 278 281 private String keywordFilter( String line ) { 282 if( line == null || line.equals("") ) { 283 return ""; 284 } 285 StringBuffer buf = new StringBuffer (); 286 HashMap usedReservedWords = new HashMap(); int i=0, startAt=0; 289 char ch; 290 StringBuffer temp = new StringBuffer (); 291 while( i < line.length() ) { 292 temp.setLength(0); 293 ch = line.charAt(i); 294 startAt = i; 295 while( i<line.length() && ( ( ch >= 65 && ch <= 90 ) 298 || ( ch >= 97 && ch <= 122 ) ) ) { 299 temp.append(ch); 300 i++; 301 if( i < line.length() ) { 302 ch = line.charAt(i); 303 } 304 } 305 String tempString = temp.toString(); 306 if( reservedWords.containsKey(tempString) && !usedReservedWords.containsKey(tempString)) { 307 usedReservedWords.put(tempString,tempString); 308 line = replace( line, tempString, (reservedWordStart+tempString+reservedWordEnd) ); 309 i += (reservedWordStart.length() + reservedWordEnd.length()); 310 } 311 else { 312 i++; 313 } 314 } 315 buf.append(line); 316 return buf.toString(); 317 } 318 319 323 private String replace( String line, String oldString, String newString ) { 324 int i=0; 325 while( ( i=line.indexOf( oldString, i ) ) >= 0 ) { 326 line = (new StringBuffer ().append(line.substring(0,i)).append(newString).append(line.substring(i+oldString.length()))).toString(); 327 i += newString.length(); 328 } 329 return line; 330 } 331 332 336 private boolean isInsideString(String line, int position) { 337 if (line.indexOf("\"") < 0) { 338 return false; 339 } 340 int index; 341 String left = line.substring(0,position); 342 String right = line.substring(position); 343 int leftCount = 0; 344 int rightCount = 0; 345 while ((index = left.indexOf("\"")) > -1) { 346 leftCount ++; 347 left = left.substring(index+1); 348 } 349 while ((index = right.indexOf("\"")) > -1) { 350 rightCount ++; 351 right = right.substring(index+1); 352 } 353 if (rightCount % 2 != 0 && leftCount % 2 != 0) { 354 return true; 355 } 356 else { 357 return false; 358 } 359 } 360 361 364 private static void loadHash() { 365 reservedWords.put( "abstract", "abstract" ); 366 reservedWords.put( "do", "do" ); 367 reservedWords.put( "inner", "inner" ); 368 reservedWords.put( "public", "public" ); 369 reservedWords.put( "var", "var" ); 370 reservedWords.put( "boolean", "boolean" ); 371 reservedWords.put( "continue", "continue" ); 372 reservedWords.put( "int", "int" ); 373 reservedWords.put( "return", "return" ); 374 reservedWords.put( "void", "void" ); 375 reservedWords.put( "break", "break" ); 376 reservedWords.put( "else", "else" ); 377 reservedWords.put( "interface", "interface" ); 378 reservedWords.put( "short", "short" ); 379 reservedWords.put( "volatile", "volatile" ); 380 reservedWords.put( "byvalue", "byvalue" ); 381 reservedWords.put( "extends", "extends" ); 382 reservedWords.put( "long", "long" ); 383 reservedWords.put( "static", "static" ); 384 reservedWords.put( "while", "while" ); 385 reservedWords.put( "case", "case" ); 386 reservedWords.put( "final", "final" ); 387 reservedWords.put( "naive", "naive" ); 388 reservedWords.put( "super", "super" ); 389 reservedWords.put( "transient", "transient" ); 390 reservedWords.put( "cast", "cast" ); 391 reservedWords.put( "float", "float" ); 392 reservedWords.put( "new", "new" ); 393 reservedWords.put( "rest", "rest" ); 394 reservedWords.put( "catch", "catch" ); 395 reservedWords.put( "for", "for" ); 396 reservedWords.put( "null", "null" ); 397 reservedWords.put( "synchronized", "synchronized" ); 398 reservedWords.put( "char", "char" ); 399 reservedWords.put( "finally", "finally" ); 400 reservedWords.put( "operator", "operator" ); 401 reservedWords.put( "this", "this" ); 402 reservedWords.put( "class", "class" ); 403 reservedWords.put( "generic", "generic" ); 404 reservedWords.put( "outer", "outer" ); 405 reservedWords.put( "switch", "switch" ); 406 reservedWords.put( "const", "const" ); 407 reservedWords.put( "goto", "goto" ); 408 reservedWords.put( "package", "package" ); 409 reservedWords.put( "throw", "throw" ); 410 reservedWords.put( "double", "double" ); 411 reservedWords.put( "if", "if" ); 412 reservedWords.put( "private", "private" ); 413 reservedWords.put( "true", "true" ); 414 reservedWords.put( "default", "default" ); 415 reservedWords.put( "import", "import" ); 416 reservedWords.put( "protected", "protected" ); 417 reservedWords.put( "try", "try" ); 418 } 419 } 420 | Popular Tags |