让你的JSP支持Ubbcode

  1. import java.util.regex.*;//导入正则表达式包中的类
  2.  
  3. /**
  4.  *  使网页支持ubbcode.
  5.  * @author shanwei
  6.  * @version 1.0
  7.  */
  8.  
  9. public final class Ubbcode {
  10.  
  11.     private static final String ImagePath = "images/"//定义图片路径
  12.     private static final String emotImagePath = "images/emot/";
  13.  
  14.     /**
  15.      * @description 转换代码,使之支持ubbcode.
  16.      * @param strContent 转换前的代码.
  17.      * @return result 转换后的内容.
  18.      */
  19.     public static String UBBCode(String strContent) { //
  20.         strContent = dvHTMLEncode(strContent);
  21.         strContent = FilterJS(strContent);
  22.         String re;
  23.         //re.IgnoreCase =true
  24.         //re.Global=True
  25.         Pattern pattern = null//
  26.         Matcher matcher = null;
  27.         //转换IMG
  28.         re = "//[IMG//](http|https|ftp)://(.[^//[]*)//[///IMG//]";
  29.         pattern = Pattern.compile(re);
  30.         matcher = pattern.matcher(strContent);
  31.         strContent = matcher.replaceAll("<a οnfοcus=this.blur() href=/"$1://$2/" target=_blank><IMG SRC=/"$1://$2/" border=0 alt=按此在新窗口浏览图片 οnlοad=/"javascript:if(this.width>screen.width-333)this.width=screen.width-333/"></a>");
  32.         //转换UPLOAD
  33.         re = "//[UPLOAD=(gif|jpg|jpeg|bmp|png)//](.[^//[]*)(gif|jpg|jpeg|bmp|png)//[///UPLOAD//]";
  34.         pattern = Pattern.compile(re);
  35.         matcher = pattern.matcher(strContent);
  36.         strContent = matcher.replaceAll("<br><IMG SRC=/"" + ImagePath + "$1.gif/" border=0>此主题相关图片如下:<br><A HREF=/"$2$1/" TARGET=_blank><IMG SRC=/"$2$1/" border=0 alt=按此在新窗口浏览图片 οnlοad=/"javascript:if(this.width>screen.width-333)this.width=screen.width-333/"></A>");
  37.         re = "//[UPLOAD=(.[^//[]*)//](.[^//[]*)//[///UPLOAD//]";
  38.         pattern = Pattern.compile(re);
  39.         matcher = pattern.matcher(strContent);
  40.         strContent = matcher.replaceAll("<br><IMG SRC=/"" + ImagePath +
  41.                                         "$1.gif/" border=0> <a href=/"$2/">点击浏览该文件</a>");
  42.         //转换DIR
  43.         re = "//[DIR=*([0-9]*),*([0-9]*)//](.[^//[]*)//[///DIR]";
  44.         pattern = Pattern.compile(re);
  45.         matcher = pattern.matcher(strContent);
  46.         strContent = matcher.replaceAll("<object classid=clsid:166B1BCA-3F9C-11CF-8075-444553540000 codebase=http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=7,0,2,0 width=$1 height=$2><param name=src value=$3><embed src=$3 pluginspage=http://www.macromedia.com/shockwave/download/ width=$1 height=$2></embed></object>");
  47.         //转换QT
  48.         re = "//[QT=*([0-9]*),*([0-9]*)//](.[^//[]*)//[///QT]";
  49.         pattern = Pattern.compile(re);
  50.         matcher = pattern.matcher(strContent);
  51.         strContent = matcher.replaceAll("<embed src=$3 width=$1 height=$2 autoplay=true loop=false controller=true playeveryframe=false cache=false scale=TOFIT bgcolor=#000000 kioskmode=false targetcache=false pluginspage=http://www.apple.com/quicktime/>;");
  52.         //转换MP
  53.         re = "//[MP=*([0-9]*),*([0-9]*)//](.[^//[]*)//[///MP]";
  54.         pattern = Pattern.compile(re);
  55.         matcher = pattern.matcher(strContent);
  56.         strContent = matcher.replaceAll("<object align=middle classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 class=OBJECT id=MediaPlayer width=$1 height=$2 ><param name=ShowStatusBar value=-1><param name=Filename value=$3><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src=$3  width=$1 height=$2></embed></object>");
  57.         //转换RM
  58.         re = "//[RM=*([0-9]*),*([0-9]*)//](.[^//[]*)//[///RM]";
  59.         pattern = Pattern.compile(re);
  60.         matcher = pattern.matcher(strContent);
  61.         strContent = matcher.replaceAll("<OBJECT classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX width=$1 height=$2><PARAM NAME=SRC VALUE=$3><PARAM NAME=CONSOLE VALUE=Clip1><PARAM NAME=CONTROLS VALUE=imagewindow><PARAM NAME=AUTOSTART VALUE=true></OBJECT><br><OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=32 id=video2 width=$1><PARAM NAME=SRC VALUE=$3><PARAM NAME=AUTOSTART VALUE=-1><PARAM NAME=CONTROLS VALUE=controlpanel><PARAM NAME=CONSOLE VALUE=Clip1></OBJECT>");
  62.         //转换FLASH
  63.         re = "(//[FLASH//])(.[^//[]*)(//[///FLASH//])";
  64.         pattern = Pattern.compile(re);
  65.         matcher = pattern.matcher(strContent);
  66.         strContent = matcher.replaceAll(
  67.                 "<a href=/"$2/" TARGET=_blank><IMG SRC=/"" + ImagePath + "swf.gif/" border=0 alt=点击开新窗口欣赏该FLASH动画! height=16 width=16>[全屏欣赏]</a><br><OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=500 height=400><PARAM NAME=movie VALUE=/"$2/"><PARAM NAME=quality VALUE=high><embed src=/"$2/" quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width=500 height=400>$2</embed></OBJECT>");
  68.         re = "(//[FLASH=*([0-9]*),*([0-9]*)//])(.[^//[]*)(//[///FLASH//])";
  69.         pattern = Pattern.compile(re);
  70.         matcher = pattern.matcher(strContent);
  71.         strContent = matcher.replaceAll(
  72.                 "<a href=/"$4/" TARGET=_blank><IMG SRC=/"" + ImagePath + "swf.gif/" border=0 alt=点击开新窗口欣赏该FLASH动画! height=16 width=16>[全屏欣赏]</a><br><OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=$2 height=$3><PARAM NAME=movie VALUE=/"$4/"><PARAM NAME=quality VALUE=high><embed src=/"$4/" quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width=$2 height=$3>$4</embed></OBJECT>");
  73.         //转换URL
  74.         re = "(//[URL//])(.[^//[]*)(//[///URL//])";
  75.         pattern = Pattern.compile(re);
  76.         matcher = pattern.matcher(strContent);
  77.         strContent = matcher.replaceAll("<A HREF=/"$2/" TARGET=_blank>$2</A>");
  78.         re = "(//[URL=(.[^//[]*)//])(.[^//[]*)(//[///URL//])";
  79.         pattern = Pattern.compile(re);
  80.         matcher = pattern.matcher(strContent);
  81.         strContent = matcher.replaceAll("<A HREF=/"$2/" TARGET=_blank>$3</A>");
  82.         //转换EMAIL
  83.         re = "(//[EMAIL//])(//S+//@.[^//[]*)(//[///EMAIL//])";
  84.         pattern = Pattern.compile(re);
  85.         matcher = pattern.matcher(strContent);
  86.         strContent = matcher.replaceAll("<img align=absmiddle src=/"" +
  87.                                         ImagePath +
  88.                                         "email1.gif/"><A HREF=/"mailto:$2/">$2</A>");
  89.         re = "(//[EMAIL=(//S+//@.[^//[]*)//])(.[^//[]*)(//[///EMAIL//])";
  90.         pattern = Pattern.compile(re);
  91.         matcher = pattern.matcher(strContent);
  92.         strContent = matcher.replaceAll("<img align=absmiddle src=/"" +
  93.                                         ImagePath +
  94.                                         "email1.gif/"><A HREF=/"mailto:$2/" TARGET=_blank>$3</A>");
  95.  
  96.         //自动识别网址
  97.         re =
  98.                 "^((http|https|ftp|rtsp|mms):(//|)[A-Za-z0-9//./=//?%//-&_~`@':+!]+)";
  99.         pattern = Pattern.compile(re);
  100.         matcher = pattern.matcher(strContent);
  101.         strContent = matcher.replaceAll(
  102.                 "<img align=absmiddle src=pic/url.gif border=0><a target=_blank href=$1>$1</a>");
  103.         re =
  104.                 "((http|https|ftp|rtsp|mms):(//|)[A-Za-z0-9//./=//?%//-&_~`@':+!]+)$";
  105.         pattern = Pattern.compile(re);
  106.         matcher = pattern.matcher(strContent);
  107.         strContent = matcher.replaceAll(
  108.                 "<img align=absmiddle src=pic/url.gif border=0><a target=_blank href=$1>$1</a>");
  109.         re = "([^>=/"])((http|https|ftp|rtsp|mms):(//|)[A-Za-z0-9//./=//?%//-&_~`@':+!]+)";
  110.         pattern = Pattern.compile(re);
  111.         matcher = pattern.matcher(strContent);
  112.         strContent = matcher.replaceAll("$1<img align=absmiddle src=pic/url.gif border=0><a target=_blank href=$2>$2</a>");
  113.  
  114.         //自动识别www等开头的网址
  115.         re = "([^(http://|http:)])((www|cn)[.](//w)+[.]{1,}(net|com|cn|org|cc)(((///[//~]*|//[//~]*)(//w)+)|[.](//w)+)*(((([?](//w)+){1}[=]*))*((//w)+){1}([//&](//w)+[//=](//w)+)*)*)";
  116.         pattern = Pattern.compile(re);
  117.         matcher = pattern.matcher(strContent);
  118.         strContent = matcher.replaceAll("<img align=absmiddle src=pic/url.gif border=0><a target=_blank href=http://$2>$2</a>");
  119.  
  120.         //自动识别Email地址,如打开本功能在浏览内容很多的帖子会引起服务器停顿
  121.         re = "([^(=)])((//w)+[@]{1}((//w)+[.]){1,3}(//w)+)";
  122.         pattern = Pattern.compile(re);
  123.         matcher = pattern.matcher(strContent);
  124.         strContent = matcher.replaceAll("<img align=absmiddle src=pic/url.gif border=0><a target=_blank href=/"mailto:$2/">$2</a>");
  125.         //转换EM
  126.         re = "//[em(.[^//[]*)//]";
  127.         pattern = Pattern.compile(re);
  128.         matcher = pattern.matcher(strContent);
  129.         strContent = matcher.replaceAll("<img src=/"" + emotImagePath +
  130.                                         "em$1.gif/" border=0 align=middle>");
  131.         //转换HTML
  132.         re = "//[HTML//](.[^//[]*)//[///HTML//]";
  133.         pattern = Pattern.compile(re);
  134.         matcher = pattern.matcher(strContent);
  135.         strContent = matcher.replaceAll("<table width='100%' border='0' cellspacing='0' cellpadding='6' class=tableborder1><td><b>以下内容为程序代码:</b><br>$1</td></table>");
  136.         //转换CODE
  137.         re = "//[code//](.[^//[]*)//[///code//]";
  138.         pattern = Pattern.compile(re);
  139.         matcher = pattern.matcher(strContent);
  140.         strContent = matcher.replaceAll("<table width='100%' border='0' cellspacing='0' cellpadding='6' class=tableborder1><td><b>以下内容为程序代码:</b><br>$1</td></table>");
  141.         //转换COLOR
  142.         re = "//[color=(.[^//[]*)//](.[^//[]*)//[///color//]";
  143.         pattern = Pattern.compile(re);
  144.         matcher = pattern.matcher(strContent);
  145.         strContent = matcher.replaceAll("<font color=$1>$2</font>");
  146.         //转换FACE
  147.         re = "//[face=(.[^//[]*)//](.[^//[]*)//[///face//]";
  148.         pattern = Pattern.compile(re);
  149.         matcher = pattern.matcher(strContent);
  150.         strContent = matcher.replaceAll("<font face=$1>$2</font>");
  151.         //转换ALIGN
  152.         re = "//[align=(center|left|right)//](.*)//[///align//]";
  153.         pattern = Pattern.compile(re);
  154.         matcher = pattern.matcher(strContent);
  155.         strContent = matcher.replaceAll("<div align=$1>$2</div>");
  156.         //转换QUOTE
  157.         re = "//[QUOTE//](.*)//[///QUOTE//]";
  158.         pattern = Pattern.compile(re);
  159.         matcher = pattern.matcher(strContent);
  160.         strContent = matcher.replaceAll("<table style=/"width:80%/" cellpadding=5 cellspacing=1 class=tableborder1><TR><TD class=tableborder1>$1</td></tr></table><br>");
  161.         //转换FLY
  162.         re = "//[fly//](.*)//[///fly//]";
  163.         pattern = Pattern.compile(re);
  164.         matcher = pattern.matcher(strContent);
  165.         strContent = matcher.replaceAll(
  166.                 "<marquee width=90% behavior=alternate scrollamount=3>$1</marquee>");
  167.         //转换MOVE
  168.         re = "//[move//](.*)//[///move//]";
  169.         pattern = Pattern.compile(re);
  170.         matcher = pattern.matcher(strContent);
  171.         strContent = matcher.replaceAll("<MARQUEE scrollamount=3>$1</marquee>");
  172.         //转换GLOW
  173.         re =
  174.                 "//[GLOW=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)//](.[^//[]*)//[///GLOW]";
  175.         pattern = Pattern.compile(re);
  176.         matcher = pattern.matcher(strContent);
  177.         strContent = matcher.replaceAll(
  178.                 "<table width=$1 style=/"filter:glow(color=$2, strength=$3)/">$4</table>");
  179.         //转换SHADOW
  180.         re =
  181.                 "//[SHADOW=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)//](.[^//[]*)//[///SHADOW]";
  182.         pattern = Pattern.compile(re);
  183.         matcher = pattern.matcher(strContent);
  184.         strContent = matcher.replaceAll(
  185.                 "<table width=$1 style=/"filter:shadow(color=$2, strength=$3)/">$4</table>");
  186.  
  187.         re = "//[i//](.[^//[]*)//[///i//]";
  188.         pattern = Pattern.compile(re);
  189.         matcher = pattern.matcher(strContent);
  190.         strContent = matcher.replaceAll("<i>$1</i>");
  191.         re = "//[u//](.[^//[]*)(//[///u//])";
  192.         pattern = Pattern.compile(re);
  193.         matcher = pattern.matcher(strContent);
  194.         strContent = matcher.replaceAll("<u>$1</u>");
  195.         re = "//[b//](.[^//[]*)(//[///b//])";
  196.         pattern = Pattern.compile(re);
  197.         matcher = pattern.matcher(strContent);
  198.         strContent = matcher.replaceAll("<b>$1</b>");
  199.         re = "//[size=([1-4])//](.[^//[]*)//[///size//]";
  200.         pattern = Pattern.compile(re);
  201.         matcher = pattern.matcher(strContent);
  202.         strContent = matcher.replaceAll("<font size=$1>$2</font>");
  203.         /*re ="(//s)";
  204.                  matcher = pattern.matcher(strContent);  
  205.                  strContent=matcher.replaceAll("<I>$1</I>");*/
  206.         re = null;
  207.         return strContent;
  208.     }
  209.  
  210. /**
  211.  *  过滤javascript代码.
  212.  * @param v转换前的代码.
  213.  * @return v转换后的内容.
  214.  */
  215.  
  216.     public static String FilterJS(String v) {
  217.         if (!v.equals("null") && v != null) {
  218.             String t, re;
  219.             re = "(javascript)";
  220.             t = re.replace(v, "javascript");
  221.             re = "(jscript:)";
  222.             t = re.replace(t, "jscript:");
  223.             re = "(js:)";
  224.             t = re.replace(t, "js:");
  225.             re = "(value)";
  226.             t = re.replace(t, "value");
  227.             re = "(about:)";
  228.             t = re.replace(t, "about:");
  229.             re = "(file:)";
  230.             t = re.replace(t, "file:");
  231.             re = "(document.cookie)";
  232.             t = re.replace(t, "documents.cookie");
  233.             re = "(vbscript:)";
  234.             t = re.replace(t, "vbscript:");
  235.             re = "(vbs:)";
  236.             t = re.replace(t, "vbs:");
  237.             re = "(on(mouse|exit|error|click|key))";
  238.             t = re.replace(t, "on$2");
  239.             re = "(&#)";
  240.             t = re.replace(t, "&#");
  241.             re = null;
  242.         }
  243.         return v;
  244.     }
  245.  
  246. /**
  247.  *  替换一些特殊符号为html语法标记.
  248.  * @param fString转换前的代码.
  249.  * @return fString转换后的内容.
  250.  */ 
  251.  
  252.   public static String dvHTMLEncode(String fString) {
  253.         if (!fString.equals("null") && fString != null) {
  254.             fString = replace(fString, ">"">");
  255.             fString = replace(fString, "<""<");
  256.             fString = replace(fString, "&""&");
  257.             fString = replace(fString, " "" ");
  258.             fString = replace(fString, "/""""");
  259.             fString = replace(fString, "/'""'");
  260.             fString = replace(fString, "/r""");
  261.             fString = replace(fString, "/n""<BR> ");
  262.             fString = replace(fString, "//", "/");
  263.         }
  264.         return fString;
  265.     }
  266.  
  267. /**
  268.  *  html语法标记转换符号本身.
  269.  * @param fString转换前的代码.
  270.  * @return fString转换后的内容.
  271.  */  
  272.  
  273.   public static String dvHTMLCode(String fString) {
  274.         if (!fString.equals("null") && fString != null) {
  275.             fString = replace(fString, ">"">");
  276.             fString = replace(fString, "<""<");
  277.             fString = replace(fString, "&""&");
  278.             fString = replace(fString, " "" ");
  279.             fString = replace(fString, """"/"");
  280.             fString = replace(fString, "'""/'");
  281.             fString = replace(fString, "/""//");
  282.             fString = replace(fString, "<BR>""/n");
  283.         }
  284.         return fString;
  285.     }
  286.  
  287.     public static String nohtml(String str) {
  288.         String re;
  289.         Pattern pattern = null;
  290.         Matcher matcher = null;
  291.         re = "(//<.[^//<]*//>)";
  292.         pattern = Pattern.compile(re);
  293.         matcher = pattern.matcher(str);
  294.         str = matcher.replaceAll(" ");
  295.         re = "(//<///[^//<]*//>)";
  296.         str = re.replace(str, " ");
  297.         re = null;
  298.         return str;
  299.     }
  300.  
  301. /**
  302.  *  分割函数.
  303.  * @param source原字符串,div将要被分割的字符串.
  304.  * @return returnStr分割后的字符串.
  305.  */  
  306.  
  307.    public static String[] split(String source, String div) {
  308.         int arynum = 0, intIdx = 0, intIdex = 0, div_length = div.length();
  309.         if (source.compareTo("") != 0) {
  310.             if (source.indexOf(div) != -1) {
  311.                 intIdx = source.indexOf(div);
  312.                 for (int intCount = 1; ; intCount++) {
  313.                     if (source.indexOf(div, intIdx + div_length) != -1) {
  314.                         intIdx = source.indexOf(div, intIdx + div_length);
  315.                         arynum = intCount;
  316.                     } else {
  317.                         arynum += 2;
  318.                         break;
  319.                     }
  320.                 }
  321.             } else {
  322.                 arynum = 1;
  323.             }
  324.         } else {
  325.             arynum = 0;
  326.  
  327.         }
  328.         intIdx = 0;
  329.         intIdex = 0;
  330.         String[] returnStr = new String[arynum];
  331.  
  332.   if (source.compareTo("") != 0) {
  333.             if (source.indexOf(div) != -1) {
  334.                 intIdx = (int) source.indexOf(div);
  335.                 returnStr[0] = (String) source.substring(0, intIdx);
  336.                 for (int intCount = 1; ; intCount++) {
  337.                     if (source.indexOf(div, intIdx + div_length) != -1) {
  338.                         intIdex = (int) source.indexOf(div, intIdx + div_length);
  339.                         returnStr[intCount] = (String) source.substring(intIdx +
  340.                                 div_length,
  341.                                 intIdex);
  342.                         intIdx = (int) source.indexOf(div, intIdx + div_length);
  343.                     } else {
  344.                         returnStr[intCount] = (String) source.substring(intIdx +
  345.                                 div_length,
  346.                                 source.length());
  347.                         break;
  348.                     }
  349.                 }
  350.             } else {
  351.                 returnStr[0] = (String) source.substring(0, source.length());
  352.                 return returnStr;
  353.             }
  354.         } else {
  355.             return returnStr;
  356.         }
  357.         return returnStr;
  358.     }
  359.  
  360. /**
  361.  *  替换函数.
  362.  * @param str替换前的字符串,substr被替换的字符,restr替换的字符.
  363.  * @return 替换后并且经过除空处理的字符串.
  364.  */  
  365.     public static String replace(String str, String substr, String restr) {
  366.         String[] tmp = split(str, substr);
  367.         String returnstr = null;
  368.         if (tmp.length != 0) {
  369.             returnstr = tmp[0];
  370.             for (int i = 0; i < tmp.length - 1; i++) {
  371.                 returnstr = dealNull(returnstr) + restr + tmp[i + 1];
  372.             }
  373.         }
  374.         return dealNull(returnstr);
  375.     }
  376.  
  377. /**
  378.  *  除空处理函数.
  379.  * @param str原字符串.
  380.  * @return 处理后的字符串.
  381.  */  
  382.     public static String dealNull(String str) {
  383.         String returnstr = null;
  384.         if (str == null) {
  385.             returnstr = "";
  386.         } else {
  387.             returnstr = str;
  388.         }
  389.         return returnstr;
  390.     }
  391.  
  392.     public static String tostring(Character i) {
  393.         Character c = i;
  394.         return c.toString();
  395.     }
  396.  
  397. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值