codetag.java

来自「weblogic应用全实例」· Java 代码 · 共 165 行

JAVA
165
字号
//定义本类所在的包
package examples.jsp.tagext.quote;
//定义本类引入的类
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

/**
 * 这个类定义一个定制的JSP标签,枚举当前的会话内容
 */

public class CodeTag extends BodyTagSupport {
  //显示的颜色
  String commentColor = "red";
  String quoteColor = "green";
  String defaultFont = "face=\"Courier New\" color=navy";
  
  public void setCommentColor(String col) { commentColor = col; }
  public String getCommentColor() { return commentColor; }
  
  public void setQuoteColor(String col) { quoteColor = col; }
  public String getQuoteColor() { return quoteColor; }
  
  public void setDefaultFont(String col) { defaultFont = col; }
  public String getDefaultFont() { return defaultFont; }
  //开始
  public int doStartTag() throws javax.servlet.jsp.JspException {
      return BodyTag.EVAL_BODY_TAG;
  }
  //结束
  public int doAfterBody() throws javax.servlet.jsp.JspException {
    // 获取body内容
    String body = getBodyContent().getString();
         
    // 重新个格式body
    String output = processCode(body);
    
    // 写客户端
    try {
      getPreviousOut().print("<pre><font "+defaultFont+" >"+
                             output+"</font></pre>");
    } catch (IOException ioe) {}
    
    return(SKIP_BODY);
  }
  
  
  /**
   * 处理给定的字符串产生格式化的高亮的HTML输出 
   */
  private String processCode(String body) {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    PrintWriter out = new PrintWriter(buffer);
    int index = 0;

    boolean line_comment = false;
    boolean reg_comment = false;
    boolean in_quotes = false;
    boolean in_single_quote = false;
    
    int len = body.length();

    while(index < len)
    {
      switch(body.charAt(index)) {
    case '<':
        out.write("&lt;");
        break;
    case '>':
        out.write("&gt;");
        break;
    case '&':
        out.write("&amp;");
        break;
    case '/':
        if (!in_single_quote && !in_quotes && !line_comment && !reg_comment) {
          if (body.charAt(index+1) == '/') {
            out.write("<font color=\""+commentColor+"\">//");
            line_comment = true;
            index++;
          } else if (body.charAt(index+1) == '*') {
            out.write("<font color=\""+commentColor+"\">/*");
            reg_comment = true;
            index++;
          } else {
            out.write('/');
          }
        } else {
          out.write('/');
        }
        break;
    case '\n':
        if (line_comment) {
          out.write("</font>");
          line_comment = false;
        }
        out.write('\n');
        break;
    case '*':
        if (!line_comment && reg_comment &&
            (body.charAt(index+1) == '/'))
        {
          out.write("*/</font>");
          index++;
          reg_comment = false;
        } else {
          out.write('*');
        }
        break;
    case '\'':
        if (!in_quotes  && !line_comment && !reg_comment) {
          if (!in_single_quote) {
            out.write("<font color=\""+quoteColor+"\">'");
            in_single_quote = true;
          } else {
            out.write("'");
            // Make sure this is not an escaped single quote
            // Make sure this is not an escaped quote
            if ( (index==1) || (body.charAt(index-1)!='\\') || 
                 // and that this escape character isn't escaped !
                 ((index > 1) && (body.charAt(index-2)=='\\')))
            {
              out.write("</font>");
              in_single_quote = false;
            }
          }
        } else {
          out.write('\'');
        }
        break;
    case '"':
        if (!in_single_quote && !line_comment && !reg_comment) {
          if (!in_quotes) {
            out.write("<font color=\""+quoteColor+"\">\"");
            in_quotes = true;
          } else {
            out.write('"');
            // Make sure this is not an escaped quote
            if ( (index==1) || (body.charAt(index-1)!='\\') || 
                 // and that this escape character isn't escaped !
                 ((index > 1) && (body.charAt(index-2)=='\\')) )
            {
              out.write("</font>");
              in_quotes = false;
            }
          }
        } else {
          out.write('"');
        }
        break;
    default:
        out.write(body.charAt(index));
      }
      index++;
    }
    
    out.flush();
    out.close();
    
    return(buffer.toString());
  }
  
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?