AcaiFilter.java
上传用户:yuyunping
上传日期:2013-03-21
资源大小:1844k
文件大小:6k
源码类别:

Java书籍

开发平台:

Java

  1. package net.acai.filter;
  2. /**
  3.  * Title:        清清网络
  4.  * Description:
  5.  * Copyright:    Copyright (c) 2002
  6.  * Company:     http://xqingqing.yeah.net
  7.  * @author  qingqing
  8.  * @version 1.0
  9.  */
  10. import net.acai.codeviewer.*;
  11. import net.acai.util.*;
  12. import java.io.*;
  13. public class AcaiFilter{
  14.    boolean newWindowEnabled=true;
  15.    private static final String NEW_LINE = System.getProperty("line.separator");
  16.     private String commentStart;
  17.     private String commentEnd;
  18.     private String stringStart;
  19.     private String stringEnd;
  20.     private String reservedWordStart;
  21.     private String reservedWordEnd;
  22.     
  23.     
  24.     private CodeViewer viewer;
  25.     
  26.     
  27.    public AcaiFilter(){
  28.     viewer = new CodeViewer();
  29.         commentStart = viewer.getCommentStart();
  30.         commentEnd = viewer.getCommentEnd();
  31.         stringStart = viewer.getStringStart();
  32.         stringEnd = viewer.getStringEnd();
  33.         reservedWordStart = viewer.getReservedWordStart();
  34.         reservedWordEnd = viewer.getReservedWordEnd();
  35. }
  36.    public  String convertURL(String input) {
  37.         // 检查字符串是否为空,如果为空,则返回
  38.         // 送进一些什么?
  39.         if (input == null || input.length() == 0) {
  40.             return input;
  41.         }
  42.         else {
  43.             StringBuffer buf = new StringBuffer(input.length() + 25);
  44.             char[] chars = input.toCharArray();
  45.             int i = 0, j = 0, oldend = 0;
  46.             int len = input.length();
  47.             char cur;
  48.             // 搜索字符串中含有的http://
  49.             while ( ( i=input.indexOf("http://", oldend) ) >= 0 ) {
  50.                 j=i+7;
  51.                 cur = chars[j];
  52.                 while (j < len) {
  53.                     // 空格
  54.                     if (cur == ' ') break;
  55.                     // html
  56.                     if (cur == '<') break;
  57.                     // 换行
  58.                     if (cur == 'n') break;
  59.                     // Unix的换行
  60.                     if (cur == 'r' && j<len-1 && chars[j+1] == 'n') break;
  61.                     j++;
  62.                     if (j<len) {
  63.                         cur = chars[j];
  64.                     }
  65.                 }
  66.                 // 检查终结字符'.'或者 ','
  67.                 // 移出.
  68.                 if (chars[j-1] == '.' || chars[j-1] == ',' || chars[j-1] == ')')
  69.                 {
  70.                     j--;
  71.                 }
  72.                 buf.append(chars, oldend, i-oldend);
  73.                 buf.append("<a href="");
  74.                 buf.append(chars, i, j-i);
  75.                 buf.append(""");
  76.                 if (newWindowEnabled) {
  77.                     buf.append(" target="_blank">");
  78.                 }
  79.                 else {
  80.                     buf.append(">");
  81.                 }
  82.                 buf.append(chars, i, j-i);
  83.                 buf.append("</a>");
  84.                 oldend = j;
  85.             }
  86.             buf.append(chars, j, len-j);
  87.             return buf.toString();
  88.         }
  89.     }
  90.     public String highlightCode(String input) {
  91.         if (input == null || input.length() == 0) {
  92.             return input;
  93.         }
  94.         else {
  95.             StringBuffer buf = new StringBuffer(input.length()+50);
  96.             int i = 0, j = 0, oldend = 0;
  97.     while ((i=input.indexOf("[code]", oldend)) >= 0) {
  98.                 //
  99.                 if ((j=input.indexOf("[/code]", i+6)) < 0)  {
  100.                     //
  101.                     j = input.length()-7;
  102.                 }
  103.                 // 
  104.                 
  105.      
  106.                 buf.append(StringUtils.escapeHTMLTags(input.substring(oldend,i)));
  107.                 buf.append("<table width="550" border="1" cellspacing="0" cellpadding="0" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center">");
  108.      buf.append("<tr>");
  109.           buf.append("<td bgcolor="#e6e6e6" class="code">");
  110.             
  111.                 //buf.append("<pre>");
  112.                 //
  113.                 StringBuffer codeBuf = new StringBuffer(input.length()/2);
  114.                 //
  115.                 // i
  116.                 if (i+6+1 < input.length() && (input.charAt(i+6+1) == 'n')) {
  117.                     i++;
  118.                     if (i+6+1 < input.length() && (input.charAt(i+6+1) == 'r')) {
  119.                         i++;
  120.                     }
  121.                 }
  122. buf.append(StringUtils.escapeHTMLTags(input.substring(i+6, j)));
  123.                /* BufferedReader reader = new BufferedReader(new StringReader(input.substring(i+6, j)));
  124.                 String line;
  125.                 try {
  126.                     while ((line = reader.readLine()) != null) {
  127.                         // u
  128.                         line = StringUtils.replace(line, "&lt;", "<");
  129.                         line = StringUtils.replace(line, "&gt;", ">");
  130.                         codeBuf.append(viewer.syntaxHighlight(line));
  131.                         codeBuf.append(NEW_LINE);
  132.                     }
  133.                 }
  134.                 catch (IOException ioe) {
  135.                     ioe.printStackTrace();
  136.                 }
  137.                 String code = codeBuf.toString();
  138.                 // 
  139.                 code = StringUtils.replace(code, "[", "&#91;");
  140.                 code = StringUtils.replace(code, "]", "&#93;");
  141. */
  142.                 //buf.append(code);
  143.                 //buf.append("</pre>");
  144.                 buf.append("</td>");
  145.    buf.append("</tr>");
  146. buf.append("</table>");
  147.                 // 
  148.                 oldend = j+7;
  149.             }
  150.             buf.append(StringUtils.escapeHTMLTags(input.substring(oldend,input.length())));
  151.             
  152.             return StringUtils.convertNewlines(buf.toString());
  153.         }
  154.     }
  155.     public static void main(String args[]){
  156. }
  157. }