当前页面: 开发资料首页  → JSP 专题  → 让你的JSP支持Ubbcode  
让你的JSP支持Ubbcode  
摘要 : 让你的JSP支持Ubbcode  
    import  java.util.regex.*;//导入正则表达式包中的类     
    /**   *  使网页支持ubbcode.   * @author shanwei   * @version 1.0   */     
    public  final  class  Ubbcode {    
        private  static  final  String //定义图片路径   
        private  static  final  String  
       
        /**   
         * @description 转换代码,使之支持ubbcode.       * @param strContent 转换前的代码.       * @return result 转换后的内容.       */      public  static  String String //   
            strContent = dvHTMLEncode(strContent);  
            strContent = FilterJS(strContent);  
            String  
            //re.IgnoreCase =true   
            //re.Global=True   
            Pattern null ; //   
            Matcher null ;  
            //转换IMG   
            re = "\\[IMG\\](http|https|ftp):\\/\\/(.[^\\[]*)\\[\\/IMG\\]";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll("screen.width-333)this.width=screen.width-333\">");  
            //转换UPLOAD   
            re = "\\[UPLOAD=(gif|jpg|jpeg|bmp|png)\\](.[^\\[]*)(gif|jpg|jpeg|bmp|png)\\[\\/UPLOAD\\]";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll(" 
            re = "\\[UPLOAD=(.[^\\[]*)\\](.[^\\[]*)\\[\\/UPLOAD\\]";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll("                                         "$1.gif\" border=0> 点击浏览该文件");  
            //转换DIR   
            re = "\\[DIR=*([0-9]*),*([0-9]*)\\](.[^\\[]*)\\[\\/DIR]";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll(" 
            //转换QT   
            re = "\\[QT=*([0-9]*),*([0-9]*)\\](.[^\\[]*)\\[\\/QT]";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll(";");   
            //转换MP   
            re = "\\[MP=*([0-9]*),*([0-9]*)\\](.[^\\[]*)\\[\\/MP]";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll(" 
            //转换RM   
            re = "\\[RM=*([0-9]*),*([0-9]*)\\](.[^\\[]*)\\[\\/RM]";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll(" 
            //转换FLASH   
            re = "(\\[FLASH\\])(.[^\\[]*)(\\[\\/FLASH\\])";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll(  
                    "[全屏欣赏]$2  
            re = "(\\[FLASH=*([0-9]*),*([0-9]*)\\])(.[^\\[]*)(\\[\\/FLASH\\])";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll(  
                    "[全屏欣赏]$4  
            //转换URL   
            re = "(\\[URL\\])(.[^\\[]*)(\\[\\/URL\\])";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll("$2");  
            re = "(\\[URL=(.[^\\[]*)\\])(.[^\\[]*)(\\[\\/URL\\])";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll("$3");  
            //转换EMAIL   
            re = "(\\[EMAIL\\])(\\S+\\@.[^\\[]*)(\\[\\/EMAIL\\])";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll("
                                             ImagePath +  
                                            "email1.gif\">$2");  
            re = "(\\[EMAIL=(\\S+\\@.[^\\[]*)\\])(.[^\\[]*)(\\[\\/EMAIL\\])";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll("
                                             ImagePath +  
                                            "email1.gif\">$3");  
       
            //自动识别网址   
            re =  
                    "^((http|https|ftp|rtsp|mms):(\\/\\/|\\\\)[A-Za-z0-9\\./=\\?%\\-&_~`@':+!]+)";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll(  
                    "$1");  
            re =  
                    "((http|https|ftp|rtsp|mms):(\\/\\/|\\\\)[A-Za-z0-9\\./=\\?%\\-&_~`@':+!]+)$";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll(  
                    "$1");  
            re = "([^>=\"])((http|https|ftp|rtsp|mms):(\\/\\/|\\\\)[A-Za-z0-9\\./=\\?%\\-&_~`@':+!]+)";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll("$1$2");  
       
            //自动识别www等开头的网址   
            re = "([^(http://|http:\\\\)])((www|cn)[.](\\w)+[.]{1,}(net|com|cn|org|cc)(((\\/[\\~]*|\\[\\~]*)(\\w)+)|[.](\\w)+)*(((([?](\\w)+){1}[=]*))*((\\w)+){1}([\\&](\\w)+[\\=](\\w)+)*)*)";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll("$2");  
       
            //自动识别Email地址,如打开本功能在浏览内容很多的帖子会引起服务器停顿   
            re = "([^(=)])((\\w)+[@]{1}((\\w)+[.]){1,3}(\\w)+)";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll("$2");  
            //转换EM   
            re = "\\[em(.[^\\[]*)\\]";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll("
                                             "em$1.gif\" border=0 align=middle>");  
            //转换HTML   
            re = "\\[HTML\\](.[^\\[]*)\\[\\/HTML\\]";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll("<table width='100%' border='0' cellspacing='0' cellpadding='6' class=tableborder1><td>以下内容为程序代码:  
            //转换CODE   
            re = "\\[code\\](.[^\\[]*)\\[\\/code\\]";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll("<table width='100%' border='0' cellspacing='0' cellpadding='6' class=tableborder1><td>以下内容为程序代码:  
            //转换COLOR   
            re = "\\[color=(.[^\\[]*)\\](.[^\\[]*)\\[\\/color\\]";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll("$2 ");  
            //转换FACE   
            re = "\\[face=(.[^\\[]*)\\](.[^\\[]*)\\[\\/face\\]";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll("$2 ");  
            //转换ALIGN   
            re = "\\[align=(center|left|right)\\](.*)\\[\\/align\\]";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll("$2
");  
            //转换QUOTE   
            re = "\\[QUOTE\\](.*)\\[\\/QUOTE\\]";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll("<table style=\"width:80%\" cellpadding=5 cellspacing=1 class=tableborder1><tr><td class=tableborder1>$1</td></tr></table> 
            //转换FLY   
            re = "\\[fly\\](.*)\\[\\/fly\\]";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll(  
                    "$1 ");  
            //转换MOVE   
            re = "\\[move\\](.*)\\[\\/move\\]";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll("$1 ");  
            //转换GLOW   
            re =  
                    "\\[GLOW=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)\\](.[^\\[]*)\\[\\/GLOW]";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll(  
                    "<table width=$1 style=\"filter:glow(color=$2, strength=$3)\">$4</table>");  
            //转换SHADOW   
            re =  
                    "\\[SHADOW=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)\\](.[^\\[]*)\\[\\/SHADOW]";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll(  
                    "<table width=$1 style=\"filter:shadow(color=$2, strength=$3)\">$4</table>");  
       
            re = "\\[i\\](.[^\\[]*)\\[\\/i\\]";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll("$1 ");  
            re = "\\[u\\](.[^\\[]*)(\\[\\/u\\])";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll("$1 ");  
            re = "\\[b\\](.[^\\[]*)(\\[\\/b\\])";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll("$1 ");  
            re = "\\[size=([1-4])\\](.[^\\[]*)\\[\\/size\\]";  
            pattern = Pattern  
            matcher = pattern.matcher(strContent);  
            strContent = matcher.replaceAll("$2 ");  
            /*re ="(\\s)";   
                     matcher = pattern.matcher(strContent);                     strContent=matcher.replaceAll("$1 ");*/          re = null ;  
            return  strContent;  
        }  
       
    /**   *  过滤javascript代码.   * @param v转换前的代码.   * @return v转换后的内容.   */     
        public  static  String String  
            if  (!v.equals("null") && v != null ) {  
                String  
                re = "(javascript)";  
                t = re.replace(v, "javascript");  
                re = "(jscript:)";  
                t = re.replace(t, "jscript:");  
                re = "(js:)";  
                t = re.replace(t, "js:");  
                re = "(value)";  
                t = re.replace(t, "value");  
                re = "(about:)";  
                t = re.replace(t, "about:");  
                re = "(file:)";  
                t = re.replace(t, "file:");  
                re = "(document.cookie)";  
                t = re.replace(t, "documents.cookie");  
                re = "(vbscript:)";  
                t = re.replace(t, "vbscript:");  
                re = "(vbs:)";  
                t = re.replace(t, "vbs:");  
                re = "(on(mouse|exit|error|click|key))";  
                t = re.replace(t, "on$2");  
                re = "(&#)";  
                t = re.replace(t, "&#");  
                re = null ;  
            }  
            return  v;  
        }  
       
    /**   *  替换一些特殊符号为html语法标记.   * @param fString转换前的代码.   * @return fString转换后的内容.   */      
      public  static  String String  
            if  (!fString.equals("null") && fString != null ) {  
                fString = replace(fString, ">", ">");  
                fString = replace(fString, "<", "<");  
                fString = replace(fString, "&", "&");  
                fString = replace(fString, " ", " ");  
                fString = replace(fString, "\"", """);  
                fString = replace(fString, "\'", "'");  
                fString = replace(fString, "\r", "");  
                fString = replace(fString, "\n", " 
                fString = replace(fString, "\\", "\");  
            }  
            return  fString;  
        }  
       
    /**   *  html语法标记转换符号本身.   * @param fString转换前的代码.   * @return fString转换后的内容.   */       
      public  static  String String  
            if  (!fString.equals("null") && fString != null ) {  
                fString = replace(fString, ">", ">");  
                fString = replace(fString, "<", "<");  
                fString = replace(fString, "&", "&");  
                fString = replace(fString, " ", " ");  
                fString = replace(fString, """, "\"");  
                fString = replace(fString, "'", "\'");  
                fString = replace(fString, "\", "\\");  
                fString = replace(fString, " 
            }  
            return  fString;  
        }  
       
        public  static  String String  
            String  
            Pattern null ;  
            Matcher null ;  
            re = "(\\<.[^\\<]*\\>)";  
            pattern = Pattern  
            matcher = pattern.matcher(str);  
            str = matcher.replaceAll(" ");  
            re = "(\\<\\/[^\\<]*\\>)";  
            str = re.replace(str, " ");  
            re = null ;  
            return  str;  
        }  
       
    /**   *  分割函数.   * @param source原字符串,div将要被分割的字符串.   * @return returnStr分割后的字符串.   */       
       public  static  String String String  
            int  arynum = 0, intIdx = 0, intIdex = 0, div_length = div.length ();  
            if  (source.compareTo("") != 0) {  
                if  (source.indexOf(div) != -1) {  
                    intIdx = source.indexOf(div);  
                    for  (int  intCount = 1; ; intCount++) {  
                        if  (source.indexOf(div, intIdx + div_length) != -1) {  
                            intIdx = source.indexOf(div, intIdx + div_length);  
                            arynum = intCount;  
                        } else  {  
                            arynum += 2;  
                            break ;  
                        }  
                    }  
                } else  {  
                    arynum = 1;  
                }  
            } else  {  
                arynum = 0;  
       
            }  
            intIdx = 0;  
            intIdex = 0;  
            String new  String  
       
      if  (source.compareTo("") != 0) {  
                if  (source.indexOf(div) != -1) {  
                    intIdx = (int ) source.indexOf(div);  
                    returnStr[0] = (String  
                    for  (int  intCount = 1; ; intCount++) {  
                        if  (source.indexOf(div, intIdx + div_length) != -1) {  
                            intIdex = (int ) source.indexOf(div, intIdx + div_length);  
                            returnStr[intCount] = (String  
                                    div_length,  
                                    intIdex);  
                            intIdx = (int ) source.indexOf(div, intIdx + div_length);  
                        } else  {  
                            returnStr[intCount] = (String  
                                    div_length,  
                                    source.length ());  
                            break ;  
                        }  
                    }  
                } else  {  
                    returnStr[0] = (String length ());  
                    return  returnStr;  
                }  
            } else  {  
                return  returnStr;  
            }  
            return  returnStr;  
        }  
       
    /**   *  替换函数.   * @param str替换前的字符串,substr被替换的字符,restr替换的字符.   * @return 替换后并且经过除空处理的字符串.   */        public  static  String String String String  
            String  
            String null ;  
            if  (tmp.length  != 0) {  
                returnstr = tmp[0];  
                for  (int  i = 0; i < tmp.length  - 1; i++) {  
                    returnstr = dealNull(returnstr) + restr + tmp[i + 1];  
                }  
            }  
            return  dealNull(returnstr);  
        }  
       
    /**   *  除空处理函数.   * @param str原字符串.   * @return 处理后的字符串.   */        public  static  String String  
            String null ;  
            if  (str == null ) {  
                returnstr = "";  
            } else  {  
                returnstr = str;  
            }  
            return  returnstr;  
        }  
       
        public  static  String Character  
            Character  
            return  c.toString();  
        }  
       
    }  
 
引自:http://www.javaresearch.org/article/showarticle.jsp?column=106&thread=51550