📄 javacodehighlighter.java
字号:
* <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>. * * @param methodStart the HTML tag to start method blocks. */ public void setMethodStart(String methodStart) { this.methodStart = methodStart; viewer.setMethodStart(methodStart); } /** * Returns the HTML tag that ends method blocks. For example, it could be * <code></font></code>. This should correspond to the start tag * for method blocks. * * @return the HTML tag to end method blocks. */ public String getMethodEnd() { return methodEnd; } /** * Sets the HTML tag that ends method blocks. For example, it could be * <code></font></code>. This should correspond to the start tag * for method blocks. * * @param methodEnd the HTML tag to end method blocks. */ public void setMethodEnd(String methodEnd) { this.methodEnd = methodEnd; viewer.setMethodEnd(methodEnd); } /** * Returns the HTML tag that starts character blocks. For example, it could be * <code><font color="navy"></code>. * * @return the HTML tag to start method blocks. */ public String getCharacterStart() { return characterStart; } /** * Sets the HTML tag that starts character blocks. For example, it could be * <code><font color="navy"></code>. * * @param characterStart the HTML tag to start method blocks. */ public void setCharacterStart(String characterStart) { this.characterStart = characterStart; viewer.setCharacterStart(characterStart); } /** * Returns the HTML tag that ends character blocks. For example, it could be * <code></font></code>. This should correspond to the start tag * for character blocks. * * @return the HTML tag to end method blocks. */ public String getCharacterEnd() { return characterEnd; } /** * Sets the HTML tag that ends character blocks. For example, it could be * <code></font></code>. This should correspond to the start tag * for character blocks. * * @param characterEnd the HTML tag to end character blocks. */ public void setCharacterEnd(String characterEnd) { this.characterEnd = characterEnd; viewer.setCharacterEnd(characterEnd); } /** * Returns the HTML tag that starts bracket blocks. For example, it could be * <code><font color="navy"></code>. * * @return the HTML tag to start bracket blocks. */ public String getBracketStart() { return bracketStart; } /** * Sets the HTML tag that starts character blocks. For example, it could be * <code><font color="navy"></code>. * * @param bracketStart the HTML tag to start bracket blocks. */ public void setBracketStart(String bracketStart) { this.bracketStart = bracketStart; viewer.setBracketStart(bracketStart); } /** * Returns the HTML tag that ends character blocks. For example, it could be * <code></font></code>. This should correspond to the start tag * for bracket blocks. * * @return the HTML tag to end bracket blocks. */ public String getBracketEnd() { return bracketEnd; } /** * Sets the HTML tag that ends character blocks. For example, it could be * <code></font></code>. This should correspond to the start tag * for bracket blocks. * * @param bracketEnd the HTML tag to end bracket blocks. */ public void setBracketEnd(String bracketEnd) { this.bracketEnd = bracketEnd; viewer.setBracketEnd(bracketEnd); } /** * Gets the string value of the string escape character * @return the string value of the string escape character */ public String getNumberStart() { return numberStart; } /** * Sets the HTML tag that starts character blocks. For example, it could be * <code><font color="navy"></code>. * * @param numberStart the HTML tag to start bracket blocks. */ public void setNumberStart(String numberStart) { this.numberStart = numberStart; viewer.setNumberStart(numberStart); } /** * Returns the HTML tag that ends character blocks. For example, it could be * <code></font></code>. This should correspond to the start tag * for bracket blocks. * * @return the HTML tag to end bracket blocks. */ public String getNumberEnd() { return numberEnd; } /** * Sets the HTML tag that ends number literals. For example, it could be * <code></font></code>. This should correspond to the start tag * for number literals. * * @param numberEnd the HTML tag to end number literals. */ public void setNumberEnd(String numberEnd) { this.numberEnd = numberEnd; viewer.setNumberEnd(numberEnd); } /** * See if method filtering is enabled. */ public boolean getFilterMethod() { return filterMethod; } /** * Enables or disables method filtering. */ public void setFilterMethod(boolean filterMethod) { this.filterMethod = filterMethod; viewer.setFilterMethod(filterMethod); } /** * See if number filtering is enabled. */ public boolean getFilterNumber() { return filterNumber; } /** * Enables or disables number filtering. */ public void setFilterNumber(boolean filterNumber) { this.filterNumber = filterNumber; viewer.setFilterNumber(filterNumber); } /** * Syntax highlights Java code that appears inside [code][/code] tags * * @param input the input string that possibly contains Java code * @return the input string, syntax highlighted with HTML */ private String highlightCode(String input, boolean doTable) { // Check if the string is null or zero length -- if so, return what was sent in. if (input == null || input.length() == 0) { return input; } else { StringBuffer codeBuf = new StringBuffer(input.length()); int i = 0; int j = 0; int oldend = 0; boolean doFilter = true; boolean noClosingTag = false; // This processes all [code][/code] tags inside an input string, iteratively. // Each iteration advances the i and j pointers to point to the positions of // the next pair of [code][/code] tags in the input, until all are processed while ((i=input.indexOf("[code]", oldend)) >= 0) { // get the first unprocessed start position // Check to see where the ending code tag is and store this position in j if ((j=input.indexOf("[/code]", i+6)) < 0) { // get the first unprocessed end position if(input.length()>"[code]".length()) { // End at end of input if no closing tag is given // which is the first character after [code] // [code]1afadsfasdfafasfasd j = input.length()-7; doFilter = true; noClosingTag = true; } else { // nothing to filter i = -1; j = i+6; //j = input.length()-6; doFilter = false; noClosingTag = true; } } //++++// if(doFilter) { //++++// // Take the string up to the code, append the string returned by JavaCodeViewer codeBuf.append(input.substring(oldend,i)); // if we don't want a code table <pre> must be appended if(!doTable) { codeBuf.append("<pre>"); } String temp = codeBuf.toString(); codeBuf = new StringBuffer(); // start looking after the initial [code] tag // [code]123[/code] String edit = ""; // String edit = input.substring(i+6,j); if(noClosingTag) { edit = input.substring(i+6,j+7); } else { edit = input.substring(i+6,j); } int editLength = edit.length(); boolean endInNewline = true; boolean startWithNewline = true; if (editLength > 0) { // if the last character is not a carriage return nor a newline if(edit.charAt(editLength-1) != '\r' && edit.charAt(editLength-1) != '\n') { endInNewline = false; } // if the first character is not a carriage return nor a newline if (edit.charAt(0) != '\r' && edit.charAt(0) != '\n') { startWithNewline = false; } String currSection = ""; if (endInNewline) { if (startWithNewline) { // get rid of initial newline and carriage return // [code]\n\r(tokens)\n\r[/code] currSection = deleteCarriages(edit.substring(2)); //codeBuf.append(temp).append(viewer.javaCodeFilter(deleteCarriages(edit.substring(2)))); if (doTable) { codeBuf.append(temp).append(surroundWithTable(viewer.javaCodeFilter(currSection), countLines(currSection))); // what a hack } else { codeBuf.append(temp).append(viewer.javaCodeFilter(currSection)); } } else { // filter normally // [code](tokens)\n\r[/code] currSection = deleteCarriages(edit); if (doTable) { codeBuf.append(temp).append(surroundWithTable(viewer.javaCodeFilter(currSection), countLines(currSection))); } else { codeBuf.append(temp).append(viewer.javaCodeFilter(currSection)); } //codeBuf.append(temp).append(viewer.javaCodeFilter(deleteCarriages(edit))); } } else { if (startWithNewline) { // get rid of initial newline and carriage return, then append an extra newline to compensate // [code]\n\r(tokens)[/code] currSection = deleteCarriages(edit.substring(2)+"\n"); if (doTable) { codeBuf.append(temp).append(surroundWithTable(viewer.javaCodeFilter(currSection), countLines(currSection))); } else { codeBuf.append(temp).append(viewer.javaCodeFilter(currSection)); } //codeBuf.append(temp).append(viewer.javaCodeFilter(deleteCarriages(edit.substring(2)+"\n"))); } else { // append an extra newline to compensate // [code](tokens)[/code] currSection = deleteCarriages(edit+"\n"); if (doTable) { codeBuf.append(temp).append(surroundWithTable(viewer.javaCodeFilter(currSection), countLines(currSection))); } else { codeBuf.append(temp).append(viewer.javaCodeFilter(currSection)); } //codeBuf.append(temp).append(viewer.javaCodeFilter(deleteCarriages(edit+"\n"))); } } } else { codeBuf.append(temp); } // escape braces to workaround TextStyle filter String code = codeBuf.toString(); code = StringUtils.replace(code, "[", "["); code = StringUtils.replace(code, "]", "]"); codeBuf = new StringBuffer().append(code); // if we don't want a code table <pre> must be appended if(!doTable) { codeBuf.append("</pre>"); } // Next time, start looking after the ending [/code] tag oldend = j+7; // the newline filter which is applied before this one // substitutes <BR> for \n\r or \n, so check to see // if a <BR> is immediately after [/code] if(input.length()>=oldend+3) { // found one, so advance pointer ahead to ignore the <BR> if(input.charAt(oldend)=='<' && input.charAt(oldend+1)=='B' && input.charAt(oldend+2)=='R' && input.charAt(oldend+3)=='>') { //input = input.substring(0,oldend)+StringUtils.replace(input.substring(oldend,oldend+1), "\n", " ")+input.substring(oldend+1); //if(input.charAt(oldend+1)=='\n' || input.charAt(oldend+1)=='\r') { //} oldend = oldend + 4; } } } oldend = j+7; } if(doFilter) { codeBuf.append(input.substring(oldend,input.length())); } return codeBuf.toString(); } } private final int countLines(String text) { int lineCount = 0; BufferedReader reader = new BufferedReader(new StringReader(text)); try { while(reader.readLine() != null) { lineCount++; } } catch(IOException ioe) { ioe.printStackTrace(); } return lineCount; } private final String deleteCarriages(String line) { char curr_char; int i=0; // lineCount = 0; // reset the line count StringBuffer buf = new StringBuffer(); while (i<line.length()) { curr_char = line.charAt(i); if (curr_char == '\r') { // waste some cpu cycles ; }/* // for printing line numbers else if(curr_char == '\n') { buf.append(curr_char); lineCount++; }*/ else { buf.append(curr_char); } i++; } return buf.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -