1 25 26 package org.nemesis.forum.util.tool; 27 28 import java.util.Hashtable ; 29 30 59 public class CodeViewer { 60 61 private static Hashtable reservedWords = new Hashtable (80); private boolean inMultiLineComment = false; 64 private String backgroundColor = "#ffffff"; 65 private String commentStart = "<font color=\"#aa0000\"><i>"; 66 private String commentEnd = "</font></i>"; 67 private String stringStart = "<font color=\"#000099\">"; 68 private String stringEnd = "</font>"; 69 private String reservedWordStart = "<b>"; 70 private String reservedWordEnd = "</b>"; 71 72 75 static { 76 loadKeywords(); 77 } 78 79 82 public String getCommentStart() { 83 return commentStart; 84 } 85 86 89 public void setCommentStart(String commentStart) { 90 this.commentStart = commentStart; 91 } 92 93 96 public String getCommentEnd() { 97 return commentEnd; 98 } 99 100 103 public void setCommentEnd(String commentEnd) { 104 this.commentEnd = commentEnd; 105 } 106 107 110 public String getStringStart() { 111 return stringStart; 112 } 113 114 117 public void setStringStart(String stringStart) { 118 this.stringStart = stringStart; 119 } 120 121 124 public String getStringEnd() { 125 return stringEnd; 126 } 127 128 131 public void setStringEnd(String stringEnd) { 132 this.stringEnd = stringEnd; 133 } 134 135 138 public String getReservedWordStart() { 139 return reservedWordStart; 140 } 141 142 145 public void setReservedWordStart(String reservedWordStart) { 146 this.reservedWordStart = reservedWordStart; 147 } 148 149 152 public String getReservedWordEnd() { 153 return reservedWordEnd; 154 } 155 156 159 public void setReservedWordEnd(String reservedWordEnd) { 160 this.reservedWordEnd = reservedWordEnd; 161 } 162 163 167 public String syntaxHighlight( String line ) { 168 return htmlFilter(line); 169 } 170 171 175 private String htmlFilter( String line ) { 176 if( line == null || line.equals("") ) { 177 return ""; 178 } 179 line = replace(line, "&", "&"); 181 182 line = replace(line, "\\\"", "\""); 184 185 line = replace(line, "\\\\", "\\" ); 188 189 line = replace(line, "<", "<"); 192 line = replace(line, ">", ">"); 195 196 return multiLineCommentFilter(line); 197 } 198 199 203 private String multiLineCommentFilter(String line) { 204 if (line == null || line.equals("")) { 205 return ""; 206 } 207 StringBuffer buf = new StringBuffer (); 208 int index; 209 if (inMultiLineComment && (index = line.indexOf("*/")) > -1 && !isInsideString(line,index)) { 211 inMultiLineComment = false; 212 buf.append(line.substring(0,index)); 213 buf.append("*/").append(commentEnd); 214 if (line.length() > index+2) { 215 buf.append(inlineCommentFilter(line.substring(index+2))); 216 } 217 return buf.toString(); 218 } 219 else if (inMultiLineComment) { 222 return line; 223 } 224 else if ((index = line.indexOf("/*")) > -1 && !isInsideString(line,index)) { 227 inMultiLineComment = true; 228 buf.append(inlineCommentFilter(line.substring(0,index))); 233 buf.append(commentStart).append("/*"); 234 buf.append(multiLineCommentFilter(line.substring(index+2))); 235 return buf.toString(); 236 } 237 else { 240 return inlineCommentFilter(line); 241 } 242 } 243 244 247 private String inlineCommentFilter(String line) { 248 if (line == null || line.equals("")) { 249 return ""; 250 } 251 StringBuffer buf = new StringBuffer (); 252 int index; 253 if ((index = line.indexOf("//")) > -1 && !isInsideString(line,index)) { 254 buf.append(stringFilter(line.substring(0,index))); 255 buf.append(commentStart); 256 buf.append(line.substring(index)); 257 buf.append(commentEnd); 258 } 259 else { 260 buf.append(stringFilter(line)); 261 } 262 return buf.toString(); 263 } 264 265 268 private String stringFilter(String line) { 269 if (line == null || line.equals("")) { 270 return ""; 271 } 272 StringBuffer buf = new StringBuffer (); 273 if (line.indexOf("\"") <= -1) { 274 return keywordFilter(line); 275 } 276 int start = 0; 277 int startStringIndex = -1; 278 int endStringIndex = -1; 279 int tempIndex; 280 while ((tempIndex = line.indexOf("\"")) > -1) { 282 if (startStringIndex == -1) { 284 startStringIndex = 0; 285 buf.append( stringFilter(line.substring(start,tempIndex)) ); 286 buf.append(stringStart).append("\""); 287 line = line.substring(tempIndex+1); 288 } 289 else { 291 startStringIndex = -1; 292 endStringIndex = tempIndex; 293 buf.append(line.substring(0,endStringIndex+1)); 294 buf.append(stringEnd); 295 line = line.substring(endStringIndex+1); 296 } 297 } 298 buf.append( keywordFilter(line) ); 299 return buf.toString(); 300 } 301 302 305 private String keywordFilter( String line ) { 306 if( line == null || line.equals("") ) { 307 return ""; 308 } 309 StringBuffer buf = new StringBuffer (); 310 Hashtable usedReservedWords = new Hashtable (); int i=0, startAt=0; 313 char ch; 314 StringBuffer temp = new StringBuffer (); 315 while( i < line.length() ) { 316 temp.setLength(0); 317 ch = line.charAt(i); 318 startAt = i; 319 while( i<line.length() && ( ( ch >= 65 && ch <= 90 ) 322 || ( ch >= 97 && ch <= 122 ) ) ) { 323 temp.append(ch); 324 i++; 325 if( i < line.length() ) { 326 ch = line.charAt(i); 327 } 328 } 329 String tempString = temp.toString(); 330 if( reservedWords.containsKey(tempString) && !usedReservedWords.containsKey(tempString)) { 331 usedReservedWords.put(tempString,tempString); 332 line = replace( line, tempString, (reservedWordStart+tempString+reservedWordEnd) ); 333 i += (reservedWordStart.length() + reservedWordEnd.length()); 334 } 335 else { 336 i++; 337 } 338 } 339 buf.append(line); 340 return buf.toString(); 341 } 342 343 346 public static final String replace( String line, String oldString, String newString ) 347 { 348 int i=0; 349 if ( ( i=line.indexOf( oldString, i ) ) >= 0 ) { 350 char [] line2 = line.toCharArray(); 351 char [] newString2 = newString.toCharArray(); 352 int oLength = oldString.length(); 353 StringBuffer buf = new StringBuffer (line2.length); 354 buf.append(line2, 0, i).append(newString2); 355 i += oLength; 356 int j = i; 357 while( ( i=line.indexOf( oldString, i ) ) > 0 ) { 358 buf.append(line2, j, i-j).append(newString2); 359 i += oLength; 360 j = i; 361 } 362 buf.append(line2, j, line2.length - j); 363 return buf.toString(); 364 } 365 return line; 366 } 367 368 372 private boolean isInsideString(String line, int position) { 373 if (line.indexOf("\"") < 0) { 374 return false; 375 } 376 int index; 377 String left = line.substring(0,position); 378 String right = line.substring(position); 379 int leftCount = 0; 380 int rightCount = 0; 381 while ((index = left.indexOf("\"")) > -1) { 382 leftCount ++; 383 left = left.substring(index+1); 384 } 385 while ((index = right.indexOf("\"")) > -1) { 386 rightCount ++; 387 right = right.substring(index+1); 388 } 389 if (rightCount % 2 != 0 && leftCount % 2 != 0) { 390 return true; 391 } 392 else { 393 return false; 394 } 395 } 396 397 401 private static void loadKeywords() { 402 reservedWords.put( "abstract", "abstract" ); 403 reservedWords.put( "boolean", "boolean" ); 404 reservedWords.put( "break", "break" ); 405 reservedWords.put( "byte", "byte" ); 406 reservedWords.put( "case", "case" ); 407 reservedWords.put( "catch", "catch" ); 408 reservedWords.put( "char", "char" ); 409 reservedWords.put( "class", "class" ); 410 reservedWords.put( "const", "const" ); 411 reservedWords.put( "continue", "continue" ); 412 reservedWords.put( "default", "default" ); 413 reservedWords.put( "do", "do" ); 414 reservedWords.put( "double", "double" ); 415 reservedWords.put( "else", "else" ); 416 reservedWords.put( "extends", "extends" ); 417 reservedWords.put( "final", "final" ); 418 reservedWords.put( "finally", "finally" ); 419 reservedWords.put( "float", "float" ); 420 reservedWords.put( "for", "for" ); 421 reservedWords.put( "goto", "goto" ); 422 reservedWords.put( "if", "if" ); 423 reservedWords.put( "implements", "implements" ); 424 reservedWords.put( "import", "import" ); 425 reservedWords.put( "instanceof", "instanceof" ); 426 reservedWords.put( "int", "int" ); 427 reservedWords.put( "interface", "interface" ); 428 reservedWords.put( "long", "long" ); 429 reservedWords.put( "native", "native" ); 430 reservedWords.put( "new", "new" ); 431 reservedWords.put( "package", "package" ); 432 reservedWords.put( "private", "private" ); 433 reservedWords.put( "protected", "protected" ); 434 reservedWords.put( "public", "public" ); 435 reservedWords.put( "return", "return" ); 436 reservedWords.put( "short", "short" ); 437 reservedWords.put( "static", "static" ); 438 reservedWords.put( "strictfp", "strictfp" ); 439 reservedWords.put( "super", "super" ); 440 reservedWords.put( "switch", "switch" ); 441 reservedWords.put( "synchronized", "synchronized" ); 442 reservedWords.put( "this", "this" ); 443 reservedWords.put( "throw", "throw" ); 444 reservedWords.put( "throws", "throws" ); 445 reservedWords.put( "transient", "transient" ); 446 reservedWords.put( "try", "try" ); 447 reservedWords.put( "void", "void" ); 448 reservedWords.put( "volatile", "volatile" ); 449 reservedWords.put( "while", "while" ); 450 } 451 } 452 453 | Popular Tags |