1 30 31 package com.Yasna.codeviewer; 32 33 import java.util.*; 34 35 64 public class CodeViewer { 65 66 private static Hashtable reservedWords = new Hashtable(80); private boolean inMultiLineComment = false; 69 private String backgroundColor = "#ffffff"; 70 private String commentStart = "<font color=\"#aa0000\"><i>"; 71 private String commentEnd = "</font></i>"; 72 private String stringStart = "<font color=\"#000099\">"; 73 private String stringEnd = "</font>"; 74 private String reservedWordStart = "<b>"; 75 private String reservedWordEnd = "</b>"; 76 77 80 static { 81 loadKeywords(); 82 } 83 84 87 public String getCommentStart() { 88 return commentStart; 89 } 90 91 94 public void setCommentStart(String commentStart) { 95 this.commentStart = commentStart; 96 } 97 98 101 public String getCommentEnd() { 102 return commentEnd; 103 } 104 105 108 public void setCommentEnd(String commentEnd) { 109 this.commentEnd = commentEnd; 110 } 111 112 115 public String getStringStart() { 116 return stringStart; 117 } 118 119 122 public void setStringStart(String stringStart) { 123 this.stringStart = stringStart; 124 } 125 126 129 public String getStringEnd() { 130 return stringEnd; 131 } 132 133 136 public void setStringEnd(String stringEnd) { 137 this.stringEnd = stringEnd; 138 } 139 140 143 public String getReservedWordStart() { 144 return reservedWordStart; 145 } 146 147 150 public void setReservedWordStart(String reservedWordStart) { 151 this.reservedWordStart = reservedWordStart; 152 } 153 154 157 public String getReservedWordEnd() { 158 return reservedWordEnd; 159 } 160 161 164 public void setReservedWordEnd(String reservedWordEnd) { 165 this.reservedWordEnd = reservedWordEnd; 166 } 167 168 172 public String syntaxHighlight( String line ) { 173 return htmlFilter(line); 174 } 175 176 180 private String htmlFilter( String line ) { 181 if( line == null || line.equals("") ) { 182 return ""; 183 } 184 line = replace(line, "&", "&"); 186 187 line = replace(line, "\\\"", "\""); 189 190 line = replace(line, "\\\\", "\\" ); 193 194 line = replace(line, "<", "<"); 197 line = replace(line, ">", ">"); 200 201 return multiLineCommentFilter(line); 202 } 203 204 208 private String multiLineCommentFilter(String line) { 209 if (line == null || line.equals("")) { 210 return ""; 211 } 212 StringBuffer buf = new StringBuffer (); 213 int index; 214 if (inMultiLineComment && (index = line.indexOf("*/")) > -1 && !isInsideString(line,index)) { 216 inMultiLineComment = false; 217 buf.append(line.substring(0,index)); 218 buf.append("*/").append(commentEnd); 219 if (line.length() > index+2) { 220 buf.append(inlineCommentFilter(line.substring(index+2))); 221 } 222 return buf.toString(); 223 } 224 else if (inMultiLineComment) { 227 return line; 228 } 229 else if ((index = line.indexOf("/*")) > -1 && !isInsideString(line,index)) { 232 inMultiLineComment = true; 233 buf.append(inlineCommentFilter(line.substring(0,index))); 238 buf.append(commentStart).append("/*"); 239 buf.append(multiLineCommentFilter(line.substring(index+2))); 240 return buf.toString(); 241 } 242 else { 245 return inlineCommentFilter(line); 246 } 247 } 248 249 252 private String inlineCommentFilter(String line) { 253 if (line == null || line.equals("")) { 254 return ""; 255 } 256 StringBuffer buf = new StringBuffer (); 257 int index; 258 if ((index = line.indexOf("//")) > -1 && !isInsideString(line,index)) { 259 buf.append(stringFilter(line.substring(0,index))); 260 buf.append(commentStart); 261 buf.append(line.substring(index)); 262 buf.append(commentEnd); 263 } 264 else { 265 buf.append(stringFilter(line)); 266 } 267 return buf.toString(); 268 } 269 270 273 private String stringFilter(String line) { 274 if (line == null || line.equals("")) { 275 return ""; 276 } 277 StringBuffer buf = new StringBuffer (); 278 if (line.indexOf("\"") <= -1) { 279 return keywordFilter(line); 280 } 281 int start = 0; 282 int startStringIndex = -1; 283 int endStringIndex = -1; 284 int tempIndex; 285 while ((tempIndex = line.indexOf("\"")) > -1) { 287 if (startStringIndex == -1) { 289 startStringIndex = 0; 290 buf.append( stringFilter(line.substring(start,tempIndex)) ); 291 buf.append(stringStart).append("\""); 292 line = line.substring(tempIndex+1); 293 } 294 else { 296 startStringIndex = -1; 297 endStringIndex = tempIndex; 298 buf.append(line.substring(0,endStringIndex+1)); 299 buf.append(stringEnd); 300 line = line.substring(endStringIndex+1); 301 } 302 } 303 buf.append( keywordFilter(line) ); 304 return buf.toString(); 305 } 306 307 310 private String keywordFilter( String line ) { 311 if( line == null || line.equals("") ) { 312 return ""; 313 } 314 StringBuffer buf = new StringBuffer (); 315 Hashtable usedReservedWords = new Hashtable(); int i=0, startAt=0; 318 char ch; 319 StringBuffer temp = new StringBuffer (); 320 while( i < line.length() ) { 321 temp.setLength(0); 322 ch = line.charAt(i); 323 startAt = i; 324 while( i<line.length() && ( ( ch >= 65 && ch <= 90 ) 327 || ( ch >= 97 && ch <= 122 ) ) ) { 328 temp.append(ch); 329 i++; 330 if( i < line.length() ) { 331 ch = line.charAt(i); 332 } 333 } 334 String tempString = temp.toString(); 335 if( reservedWords.containsKey(tempString) && !usedReservedWords.containsKey(tempString)) { 336 usedReservedWords.put(tempString,tempString); 337 line = replace( line, tempString, (reservedWordStart+tempString+reservedWordEnd) ); 338 i += (reservedWordStart.length() + reservedWordEnd.length()); 339 } 340 else { 341 i++; 342 } 343 } 344 buf.append(line); 345 return buf.toString(); 346 } 347 348 351 public static final String replace( String line, String oldString, String newString ) 352 { 353 int i=0; 354 if ( ( i=line.indexOf( oldString, i ) ) >= 0 ) { 355 char [] line2 = line.toCharArray(); 356 char [] newString2 = newString.toCharArray(); 357 int oLength = oldString.length(); 358 StringBuffer buf = new StringBuffer (line2.length); 359 buf.append(line2, 0, i).append(newString2); 360 i += oLength; 361 int j = i; 362 while( ( i=line.indexOf( oldString, i ) ) > 0 ) { 363 buf.append(line2, j, i-j).append(newString2); 364 i += oLength; 365 j = i; 366 } 367 buf.append(line2, j, line2.length - j); 368 return buf.toString(); 369 } 370 return line; 371 } 372 373 377 private boolean isInsideString(String line, int position) { 378 if (line.indexOf("\"") < 0) { 379 return false; 380 } 381 int index; 382 String left = line.substring(0,position); 383 String right = line.substring(position); 384 int leftCount = 0; 385 int rightCount = 0; 386 while ((index = left.indexOf("\"")) > -1) { 387 leftCount ++; 388 left = left.substring(index+1); 389 } 390 while ((index = right.indexOf("\"")) > -1) { 391 rightCount ++; 392 right = right.substring(index+1); 393 } 394 if (rightCount % 2 != 0 && leftCount % 2 != 0) { 395 return true; 396 } 397 else { 398 return false; 399 } 400 } 401 402 406 private static void loadKeywords() { 407 reservedWords.put( "abstract", "abstract" ); 408 reservedWords.put( "boolean", "boolean" ); 409 reservedWords.put( "break", "break" ); 410 reservedWords.put( "byte", "byte" ); 411 reservedWords.put( "case", "case" ); 412 reservedWords.put( "catch", "catch" ); 413 reservedWords.put( "char", "char" ); 414 reservedWords.put( "class", "class" ); 415 reservedWords.put( "const", "const" ); 416 reservedWords.put( "continue", "continue" ); 417 reservedWords.put( "default", "default" ); 418 reservedWords.put( "do", "do" ); 419 reservedWords.put( "double", "double" ); 420 reservedWords.put( "else", "else" ); 421 reservedWords.put( "extends", "extends" ); 422 reservedWords.put( "final", "final" ); 423 reservedWords.put( "finally", "finally" ); 424 reservedWords.put( "float", "float" ); 425 reservedWords.put( "for", "for" ); 426 reservedWords.put( "goto", "goto" ); 427 reservedWords.put( "if", "if" ); 428 reservedWords.put( "implements", "implements" ); 429 reservedWords.put( "import", "import" ); 430 reservedWords.put( "instanceof", "instanceof" ); 431 reservedWords.put( "int", "int" ); 432 reservedWords.put( "interface", "interface" ); 433 reservedWords.put( "long", "long" ); 434 reservedWords.put( "native", "native" ); 435 reservedWords.put( "new", "new" ); 436 reservedWords.put( "package", "package" ); 437 reservedWords.put( "private", "private" ); 438 reservedWords.put( "protected", "protected" ); 439 reservedWords.put( "public", "public" ); 440 reservedWords.put( "return", "return" ); 441 reservedWords.put( "short", "short" ); 442 reservedWords.put( "static", "static" ); 443 reservedWords.put( "strictfp", "strictfp" ); 444 reservedWords.put( "super", "super" ); 445 reservedWords.put( "switch", "switch" ); 446 reservedWords.put( "synchronized", "synchronized" ); 447 reservedWords.put( "this", "this" ); 448 reservedWords.put( "throw", "throw" ); 449 reservedWords.put( "throws", "throws" ); 450 reservedWords.put( "transient", "transient" ); 451 reservedWords.put( "try", "try" ); 452 reservedWords.put( "void", "void" ); 453 reservedWords.put( "volatile", "volatile" ); 454 reservedWords.put( "while", "while" ); 455 } 456 } 457 458 | Popular Tags |