【Java开源代码栏目提醒】:网学会员Java开源代码为您提供CodeViewer.java参考,解决您在CodeViewer.java学习中工作中的难题,参考学习。
/**
* $RCSfile: CodeViewer.java,v $
* $Revision: 1.7 $
* $Date: 2001/07/31 05:39:19 $
*
* Copyright (C) 1999-2001 CoolServlets, Inc. All rights reserved.
*
* This software is the proprietary information of CoolServlets, Inc.
* Use is subject to license terms.
*/
package net.acai.codeviewer;
import java.util.*;
/**
* A class that syntax highlights Java code into html.
* <p>
* A CodeViewer object is created and then keeps state as
* lines are passed in. Each line passed in as java text, is returned as syntax
* highlighted html text.
* <p>
* Users of the class can set how the java code will be highlighted with
* setter methods.
* <p>
* Only valid java lines should be passed in since the object maintains
* state and may not handle illegal code gracefully.
* <p>
* The actual system is implemented as a series of filters that deal with
* specific portions of the java code. The filters are as follows:
* <p>
* <pre>
* htmlFilter
* |__
* multiLineCommentFilter
* |___
* inlineCommentFilter
* |___
* stringFilter
* |__
* keywordFilter
* </pre>
*
*/
public class CodeViewer {
private static HashMap reservedWords = new HashMap(150); // >= Java2 only (also, not thread-safe)
//private static Hashtable reservedWords = new Hashtable(150); // < Java2 (thread-safe)
private boolean inMultiLineComment = false;
private String backgroundColor = "#ffffff";
private String commentStart = "<font color=\"#aa0000\"><i>";
private String commentEnd = "</font></i>";
private String stringStart = "<font color=\"#000099\">";
private String stringEnd = "</font>";
private String reservedWordStart = "<b>";
private String reservedWordEnd = "</b>";
/**
* Load all keywords at class loading time.
*/
static {
loadKeywords();
}
/**
* Gets the html for the start of a comment block.
*/
public String getCommentStart() {
return commentStart;
}
/**
* Sets the html for the start of a comment block.
*/
public void setCommentStart(String commentStart) {
this.commentStart = commentStart;
}
/**
* Gets the html for the end of a comment block.
*/
public String getCommentEnd() {
return commentEnd;
}
/**
* Sets the html for the end of a comment block.
*/
public void setCommentEnd(String commentEnd) {
this.commentEnd = commentEnd;
}
/**
* Gets the html for the start of a String.
*/
public String getStringStart() {
return stringStart;
}
/**
* Sets the html for the start of a String.
*/
public void setStringStart(String stringStart) {
this.stringStart = stringStart;
}
/**
* Gets the html for the end of a String.
*/
public String getStringEnd() {
return stringEnd;
}
/**
* Sets the html for the end of a String.
*/
public void setStringEnd(String stringEnd) {
this.stringEnd = stringEnd;
}
/**
* Gets the html for the start of a reserved word.
*/
public String getReservedWordStart() {
return reservedWordStart;
}
/**
* Sets the html for the start of a reserved word.
*/
public void setReservedWordStart(String reservedWordStart) {
this.reservedWordStart = reservedWordStart;
}
/**
* Gets the html for the end of a reserved word.
*/
public String getReservedWordEnd() {
return reservedWordEnd;
}
/**
* Sets the html for the end of a reserved word.
*/
public void setReservedWordEnd(String reservedWordEnd) {
this.reservedWordEnd = reservedWordEnd;
}
/**
* Passes off each line to the first filter.
* @param line The line of Java code to be highlighted.
*/
public String syntaxHighlight( String line ) {
return htmlFilter(line);
}
/*
* Filter html tags that appear in the java source into more benign text
* that won't disrupt the output.
*/
private String htmlFilter( String line ) {
if( line == null || line.equals("") ) {
return "";
}
// replace ampersands with HTML escape sequence for ampersand;
line = replace(line, "&", "&");
// replace \" sequences with HTML escape sequences;
line = replace(line, "\\\"", "\"");
// replace the \\ with HTML escape sequences. fixes a problem when
// backslashes preceed quotes.
line = replace(line, "\\\\", "\\" );
// replace less-than signs which might be confused
// by HTML as tag angle-brackets;
line = replace(line, "<", "<");
// replace greater-than signs which might be confused
// by HTML as tag angle-brackets;
line = replace(line, ">", ">");
return multiLineCommentFilter(line);
}
/*
* Filter out multiLine comments. State is kept with a private boolean
* variable.
*/
private String multiLineCommentFilter(String line) {
if (line == null || line.equals("")) {
return "";
}
StringBuffer buf = new StringBuffer();
int index;
//First, check for the end of a multi-line comment.
if (inMultiLineComment && (index = line.indexOf("*/")) > -1 && !isInsideString(line,index)) {
inMultiLineComment = false;
buf.append(line.substring(0,index));
buf.append("*/").append(commentEnd);
if (line.length() > index+2) {
buf.append(inlineCommentFilter(line.substring(index+2)));
}
return buf.toString();
}
//If there was no end detected and we're currently in a multi-line
//comment, we don't want to do anymore work, so return line.
else if (inMultiLineComment) {
return line;
}
//We're not currently in a comment, so check to see if the start
//of a multi-line comment is in this line.
else if ((index = line.indexOf("/*")) > -1 && !isInsideString(line,index)) {
inMultiLineComment = true;
//Return result of other filters + everything after the start
//of the multiline comment. We need to pass the through the
//to the multiLineComment filter again in case the comment ends
//on the same line.
buf.append(inlineCommentFilter(line.substring(0,index)));
buf.append(commentStart).append("/*");
buf.append(multiLineCommentFilter(line.substring(index+2)));
return buf.toString();
}
//Otherwise, no useful multi-line comment information was found so
//pass the line down to the next filter for processesing.
else {
return inlineCommentFilter(line);
}
}
/*
* Filter inline comments from a line and formats them properly.
*/
private String inlineCommentFilter(String line) {
if (line == null || line.equals("")) {
return "";
}
StringBuffer buf = new StringBuffer();
int index;
if ((index = line.indexOf("//")) > -1 && !isInsideString(line,index)) {
buf.append(stringFilter(line.substring(0,index)));
buf.append(commentStart);
buf.append(line.substring(index));
buf.append(commentEnd);
}
else {
buf.append(stringFilter(line));
}
return buf.toString();
}
/*
* Filters strings from a line of text and formats them properly.
*/
private String stringFilter(String line) {
if (line == null || line.equals("")) {
return "";
}
StringBuffer buf = new StringBuffer();
if (line.indexOf("\"") <= -1) {
return keywordFilter(line);
}
int start = 0;
int startStringIndex = -
上一篇:
CodeType.java
下一篇:
论“十二五”期间的行政体制改革