📄 javacodehighlighter.java
字号:
/*
* Copyright 2003-2006 the original author or authors.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.jdon.jivejdon.model.message.output.codeviewer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import com.jdon.jivejdon.model.message.MessageRenderSpecification;
import com.jdon.jivejdon.model.message.MessageVO;
import com.jdon.util.StringUtil;
/**
* A ForumMessageFilter that syntax highlights Java code appearing between
* [code][/code] tags in the body of ForumMessage.
*/
public class JavaCodeHighlighter implements MessageRenderSpecification {
private static final String NEW_LINE = System.getProperty("line.separator");
private String commentStart;
private String commentEnd;
private String stringStart;
private String stringEnd;
private String reservedWordStart;
private String reservedWordEnd;
private String methodStart;
private String methodEnd;
private String characterStart;
private String characterEnd;
private String bracketStart;
private String bracketEnd;
private String numberStart;
private String numberEnd;
private boolean filterMethod;
private boolean filterNumber;
// for formatting code with an html table and printing line numbers
private boolean applyTableSurround; // surround code with an html table
private boolean showLineCount; // show line numbers
private static final int ALIGNLEFT = 0; // left-side alignment
private static final int ALIGNRIGHT = 1; // right-side alignment
// private int alignPosition;
private String tableBorderColor;
private String tableLinePanelBackgroundColor;
private String tableCodePanelBackgroundColor;
private String lineNumberColor;
/**
* Much of the work of this filter is done by a JavaCodeViewer object.
*/
private JavaCodeViewer viewer;
/**
* Construct a new JavaCodeHighlighter filter.
*/
public JavaCodeHighlighter() {
viewer = new JavaCodeViewer();
commentStart = viewer.getCommentStart();
commentEnd = viewer.getCommentEnd();
stringStart = viewer.getStringStart();
stringEnd = viewer.getStringEnd();
reservedWordStart = viewer.getReservedWordStart();
reservedWordEnd = viewer.getReservedWordEnd();
methodStart = viewer.getMethodStart();
methodEnd = viewer.getMethodEnd();
characterStart = viewer.getCharacterStart();
characterEnd = viewer.getCharacterEnd();
bracketStart = viewer.getBracketStart();
bracketEnd = viewer.getBracketEnd();
numberStart = viewer.getNumberStart();
numberEnd = viewer.getNumberEnd();
filterMethod = viewer.getFilterMethod();
filterNumber = viewer.getFilterNumber();
applyTableSurround = false; // default
showLineCount = false; // default
tableBorderColor = "#999999"; // default
tableLinePanelBackgroundColor = "#dddddd"; // default
tableCodePanelBackgroundColor = "#ffffff"; // default
lineNumberColor = "#555555"; // default
}
/**
* Clones a new filter that will have the same properties and that
* will wrap around the specified message.
*
* @param message the ForumMessage to wrap the new filter around.
*/
public MessageVO render(MessageVO message) {
// if(true) {
if (applyTableSurround) {
message.setBody(highlightCode(message.getBody(), true));
} else {
message.setBody(highlightCode(message.getBody(), false));
}
return message;
}
//FROM THE FORUMMESSAGE INTERFACE//
/**
* create an outter table that surrounds code.
* A benefit of this is that the table background colors
* can be specified according to whatever maybe easier on
* the eyes, especially if the existing page's background color
* does not display well with formatted code.
* Another useful feature of the code table is the ability to
* conveniently output line numbers.
* @param text code already formatted with html makeup
* @param numLines number of lines in text
* @return code surrounded with a table, possibly displaying line numbers
*/
private String surroundWithTable(String text, int numLines) {
// kludgey, non-optimized code, as the table
// code can be shared across all calls to the filter
StringBuffer buffer = new StringBuffer();
buffer.append("<table border=\"0\" cellspacing=\"1\" cellpadding=\"0\" width=\"100%\" bgcolor=\"" + tableBorderColor + "\">").append("\n");
buffer.append("<tr>");
// if line counts should be displayed
// a cool feature to implement would be to be able
// to specify left or right sides
if (showLineCount) {
buffer.append("<td width=\"1%\" align=\"left\" bgcolor=\"" + tableLinePanelBackgroundColor + "\">\n");
buffer.append("<font color=\"" + lineNumberColor + "\">");
buffer.append("<pre>\n");
buffer.append(makeLines(numLines));
buffer.append("</pre>\n");
buffer.append("</font>");
buffer.append("</td>");
}
buffer.append("<td width=\"99%\" align=\"left\" bgcolor=\"" + tableCodePanelBackgroundColor + "\">\n");
buffer.append("<pre>\n");
buffer.append(text);
buffer.append("</pre>\n");
buffer.append("</td>").append("</tr>\n");
buffer.append("</table>\n");
return buffer.toString();
}
/**
* create line numbers
* the effect is similar to an ide's line number display
*/
private String makeLines(int numLines) {
StringBuffer buffer = new StringBuffer();
for (int i = 1; i <= numLines; i++) {
/*
// saving bandwidth at the cost of computation!
if(i==numLines) {
buffer.append(i+" \n");
}
else {
buffer.append(i+"\n");
}
*/
buffer.append(" " + i + " \n");
}
return buffer.toString();
}
//OTHER METHODS//
public boolean getApplyTableSurround() {
return applyTableSurround;
}
public void setApplyTableSurround(boolean applyTableSurround) {
this.applyTableSurround = applyTableSurround;
}
public boolean getShowLineCount() {
return showLineCount;
}
public void setShowLineCount(boolean showLineCount) {
this.showLineCount = showLineCount;
}
/**
* Returns the code table's border color. For example, it could
* be <code>red</code> or <code>#123456</code>
*
* @return the code table's border color.
*/
public String getTableBorderColor() {
return tableBorderColor;
}
/**
* Sets the code table's border color. For example, it could be
* <code>red</code> or <code>#123456</code>
*
* @param tableBorderColor the code table's border color.
*/
public void setTableBorderColor(String tableBorderColor) {
this.tableBorderColor = tableBorderColor;
}
/**
* Returns the line-number panel's background color. For example, it could
* be <code>white</code> or <code>#123456</code>
*
* @return the line panel's background color.
*/
public String getTableLinePanelBackgroundColor() {
return tableLinePanelBackgroundColor;
}
/**
* Sets the line-number panel's background color. For example, it could be
* <code>white</code> or <code>#123456</code>
*
* @param tableLinePanelBackgroundColor the line panel's background color.
*/
public void setTableLinePanelBackgroundColor(String tableLinePanelBackgroundColor) {
this.tableLinePanelBackgroundColor = tableLinePanelBackgroundColor;
}
/**
* Returns the code panel's background color. For example, it could be
* <code>blue</code> or <code>#123456</code>
*
* @return the code panel's background color.
*/
public String getTableCodePanelBackgroundColor() {
return tableCodePanelBackgroundColor;
}
/**
* Sets the code panel's background color. For example, it could be
* <code>blue</code> or <code>#123456</code>
*
* @param tableCodePanelBackgroundColor the code panel's background color.
*/
public void setTableCodePanelBackgroundColor(String tableCodePanelBackgroundColor) {
this.tableCodePanelBackgroundColor = tableCodePanelBackgroundColor;
}
public String getLineNumberColor() {
return lineNumberColor;
}
public void setLineNumberColor(String lineNumberColor) {
this.lineNumberColor = lineNumberColor;
}
/**
* Returns the HTML tag that starts comment blocks. For example, it could
* be <code><i></code>.
*
* @return the HTML tag to start comment blocks.
*/
public String getCommentStart() {
return commentStart;
}
/**
* Sets the HTML tag that starts comment blocks. For example, it could be
* <code><i></code>.
*
* @param commentStart the HTML tag to start comment blocks.
*/
public void setCommentStart(String commentStart) {
this.commentStart = commentStart;
viewer.setCommentStart(commentStart);
}
/**
* Returns the HTML tag that ends comment blocks. For example, it could
* be <code></i></code>. The tag should correspond to the comment
* start tag.
*
* @return the HTML tag to end comment blocks.
*/
public String getCommentEnd() {
return commentEnd;
}
/**
* Sets the HTML tag that ends comment blocks. For example, it could be
* <code></i></code>. The tag should correspond to the comment
* start tag.
*
* @param commentEnd the HTML tag to end comment blocks.
*/
public void setCommentEnd(String commentEnd) {
this.commentEnd = commentEnd;
viewer.setCommentEnd(commentEnd);
}
/**
* Returns the HTML tag that starts string blocks. For example, it could
* be <code><font color="red"></code>.
*
* @return the HTML tag to start string blocks.
*/
public String getStringStart() {
return stringStart;
}
/**
* Sets the HTML tag that starts string blocks. For example, it could be
* <code><font color="red"></code>.
*
* @param stringStart the HTML tag to start string blocks.
*/
public void setStringStart(String stringStart) {
this.stringStart = stringStart;
viewer.setStringStart(stringStart);
}
/**
* Returns the HTML tag that ends string blocks. For example, it could be
* <code></font></code>. The tag should correspond to the string
* start tag.
*
* @return the HTML tag to end string blocks.
*/
public String getStringEnd() {
return stringEnd;
}
/**
* Sets the HTML tag that ends string blocks. For example, it could be
* <code></font></code>. The tag should correspond to the string
* end tag.
*
* @param stringEnd the HTML tag to end string blocks.
*/
public void setStringEnd(String stringEnd) {
this.stringEnd = stringEnd;
viewer.setStringEnd(stringEnd);
}
/**
* Returns the HTML tag that starts keyword blocks. For example, it could be
* <code><font color="navy"></code>.
*
* @return the HTML tag to start keyword blocks.
*/
public String getReservedWordStart() {
return reservedWordStart;
}
/**
* Sets the HTML tag that starts reserved word blocks. For example, it could be
* <code><font color="navy"></code>.
*
* @param reservedWordStart the HTML tag to start keyword blocks.
*/
public void setReservedWordStart(String reservedWordStart) {
this.reservedWordStart = reservedWordStart;
viewer.setReservedWordStart(reservedWordStart);
}
/**
* Returns the HTML tag that ends keyword blocks. For example, it could be
* <code></font></code>. This should correspond to the start tag for
* keyword blocks.
*
* @return the HTML tag to end keyword blocks.
*/
public String getReservedWordEnd() {
return reservedWordEnd;
}
/**
* Sets the HTML tag that ends keyword blocks. For example, it could be
* <code><font color="navy"></code>. This should
* correspond to the start tag for keyword blocks.
*
* @param reservedWordEnd the HTML tag to end keyword blocks.
*/
public void setReservedWordEnd(String reservedWordEnd) {
this.reservedWordEnd = reservedWordEnd;
viewer.setReservedWordEnd(reservedWordEnd);
}
/**
* Returns the HTML tag that starts method blocks. For example, it could be
* <code><font color="brown"></code>.
*
* @return the HTML tag to start method blocks.
*/
public String getMethodStart() {
return methodStart;
}
/**
* Sets the HTML tag that starts method blocks. For example, it could be
* <code><font color="brown"></code>.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -