📄 javacodehighlighter.java
字号:
package com.jivesoftware.forum.filter;
import java.io.*;
import com.jivesoftware.util.*;
import com.jivesoftware.forum.*;
import com.jivesoftware.util.JavaCodeViewer;
/**
* A ForumMessageFilter that syntax highlights Java code appearing between
* [code][/code] tags in the body of ForumMessage.
*/
public class JavaCodeHighlighter extends ForumMessageFilter {
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 ForumMessageFilter clone(ForumMessage message) {
JavaCodeHighlighter filter = new JavaCodeHighlighter();
filter.message = message;
filter.setCommentStart(commentStart);
filter.setCommentEnd(commentEnd);
filter.setStringStart(stringStart);
filter.setStringEnd(stringEnd);
filter.setReservedWordStart(reservedWordStart);
filter.setReservedWordEnd(reservedWordEnd);
filter.setMethodStart(methodStart);
filter.setMethodEnd(methodEnd);
filter.setCharacterStart(characterStart);
filter.setCharacterEnd(characterEnd);
filter.setBracketStart(bracketStart);
filter.setBracketEnd(bracketEnd);
filter.setNumberStart(numberStart);
filter.setNumberEnd(numberEnd);
filter.setFilterMethod(filterMethod);
filter.setFilterNumber(filterNumber);
filter.setApplyTableSurround(applyTableSurround);
filter.setShowLineCount(showLineCount);
filter.setTableBorderColor(tableBorderColor);
filter.setTableLinePanelBackgroundColor(tableLinePanelBackgroundColor);
filter.setTableCodePanelBackgroundColor(tableCodePanelBackgroundColor);
filter.setLineNumberColor(lineNumberColor);
return filter;
}
//FROM FORUMMESSAGEFILTER//
public boolean isCacheable() {
return true;
}
//FROM THE FORUMMESSAGE INTERFACE//
/**
* <b>Overloaded</b> to return the body of message with Java code between
* [code] [/code] tags syntax highlighted.
*/
public String getBody() {
// if(true) {
if(applyTableSurround) {
return highlightCode(message.getBody(), true);
}
else {
return highlightCode(message.getBody(), false);
}
}
/**
* 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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -