📄 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 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=\"3\" "); buffer.append("bgcolor=\"").append(tableBorderColor).append("\">"); 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\" "); buffer.append(" bgcolor=\"").append(tableLinePanelBackgroundColor); buffer.append("\" valign=\"top\">"); buffer.append("<font color=\"").append(lineNumberColor).append("\">"); buffer.append("<pre>"); buffer.append(makeLines(numLines)); buffer.append("</pre>"); buffer.append("</font>"); buffer.append("</td>"); } buffer.append("<td align=\"left\" "); buffer.append("bgcolor=\"").append(tableCodePanelBackgroundColor); buffer.append("\" valign=\"top\">"); buffer.append("<pre>"); buffer.append(text); buffer.append("</pre>"); buffer.append("</td>").append("</tr>"); buffer.append("</table>"); 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 + "<br>"); } 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -