现在的位置: 首页 > 综合 > 正文

java版的正规UBB

2013年05月14日 ⁄ 综合 ⁄ 共 8214字 ⁄ 字号 评论关闭
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
@author zsy
 
*/


public final class Ubbcode {

    
private static final String imagePath = "images/"// 定义图片路径
    private static final String strIcons = "1";
    
private static final String picUrl = "edit/images/";
    
private static final String imgName = "edit/images/face/";

    
private static final String notePath = "http://www.richmap.com.cn/richmap4/api/tools/";
    
    
private static String ubbItem(String strContent, String re, String replayStr, 
            Pattern pattern, Matcher matcher, 
boolean IgnoreCase) {
        pattern 
= Pattern.compile(re);
        matcher 
= pattern.matcher(strContent);
        strContent 
= matcher.replaceAll(replayStr);
        
if (IgnoreCase) {
            pattern 
= Pattern.compile(re.toUpperCase());
            matcher 
= pattern.matcher(strContent);
            strContent 
= matcher.replaceAll(replayStr);
        }

        
return strContent;
    }

    
    
private static String quote(String strContent, Pattern pattern, 
            Matcher matcher, 
boolean IgnoreCase) {
        String re 
= "(/[quote/])(.*?)(/[//quote/])";
        
int start = strContent.lastIndexOf("[quote]");
        
int end = strContent.indexOf("[/quote]");
        String subStr 
= null;
        String newStr 
= null;
        
while (start != -1 && end != -1{
            subStr 
= strContent.substring(start, end + 8);
            pattern 
= Pattern.compile(re);
            matcher 
= pattern.matcher(subStr);
            newStr 
= matcher.replaceAll("<TABLE style="BORDER-RIGHT: #cccccc 1px dotted; " +
                    
"TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: " +
                    
"#cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellSpacing=0 " +
                    
"cellPadding=6 width=95% align=center border=0><TBODY><TR><TD " +
                    
"style="WORD-WRAP: break-word;font-size:12px;" bgColor=#f3f3f3><FONT " +
                    
"style="FONT-WEIGHT: bold; COLOR: #990000; font-size:12px;">***以下是引" +
                    
"用***</FONT><HR width=95% style="border:1px dotted  #cccccc "><center>" +
                    
"$2</center></TD></TR></TBODY></TABLE>");
            strContent 
= strContent.replace(subStr, newStr);
            start 
= strContent.lastIndexOf("[quote]");
            end 
= strContent.indexOf("[/quote]");
        }

        
        
if (IgnoreCase) {
            re 
= "(/[QUOTE/])(.*?)(/[//QUOTE/])";
            start 
= strContent.lastIndexOf("[QUOTE]");
            end 
= strContent.indexOf("[/QUOTE]");
            
while (start != -1 && end != -1{
                subStr 
= strContent.substring(start, end + 8);
                pattern 
= Pattern.compile(re);
                matcher 
= pattern.matcher(subStr);
                newStr 
= matcher.replaceAll("<TABLE style="BORDER-RIGHT: #cccccc 1px dotted; " +
                        
"TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: " +
                        
"#cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellSpacing=0 " +
                        
"cellPadding=6 width=95% align=center border=0><TBODY><TR><TD " +
                        
"style="WORD-WRAP: break-word;font-size:12px;" bgColor=#f3f3f3><FONT " +
                        
"style="FONT-WEIGHT: bold; COLOR: #990000; font-size:12px;">***以下是引" +
                        
"用***</FONT><HR width=95% style="border:1px dotted  #cccccc "><center>" +
                        
"$2</center></TD></TR></TBODY></TABLE>");
                strContent 
= strContent.replace(subStr, newStr);
                start 
= strContent.lastIndexOf("[QUOTE]");
                end 
= strContent.indexOf("[/QUOTE]");
            }

        }

        
return strContent;
    }

    
    
/**
     * @description 转换代码,使之支持ubbcode.
     * 
@param strContent
     *            转换前的代码.
     * 
@return result 转换后的内容.
     
*/

    
public static String ubbCode(String strContent) {
        strContent 
= dvHTMLEncode(strContent);
        Pattern pattern 
= null;
        Matcher matcher 
= null;
        
        
//随意贴
        strContent = ubbItem(strContent, 
                
"/[richmapfrm=(.[^/[]*)/](.+?)/[//richmapfrm/]"
                
"<iframe src=" + notePath + "singleNote.jsp?w=600&h=480" +
                
"&noteid=$1 frameborder=0 marginheight=0 marginwidth=0 scrolling=no height=400 " +
                
"width=600 title=睿图随意贴,贴出你的故事,贴出你的心情 target=_blank><img src=" + 
                picUrl 
+ "button/richiframe.gif border=0> $2</iframe>"
                pattern, matcher, 
true);
        
        strContent 
= ubbItem(strContent, 
                
"/[richmap=(.[^/[]*)/](.+?)/[//richmap/]"
                
"<a href=" + notePath + "myNote.jsp?&noteid=$1 " +
                
"title=睿图随意贴,贴出你的故事,贴出你的心情 target=_blank><img src=" + picUrl + 
                
"button/richwindow.gif border=0>$2</a>"
                pattern, matcher, 
true);
        
        
//转换IMG
        strContent = ubbItem(strContent, 
                
"/[img/](.+?)/[//img/]"
                
"<a href="$1" target="_blank">" +
                
"<IMG SRC="$1" border="0" alt="按此在新窗口浏览图片" " +
                
"onload="javascript:if(this.width>600)this.width=600" " +
                
"galleryImg="no"></a>"
                pattern, matcher, 
true);

        
//转换附件[FILE=路径,图片]名称[/FILE]
        strContent = ubbItem(strContent, 
                
"(/*),(.[^/[]*)/])(.*?)(/[//file/])"
                
"<br><IMG SRC=$3 border=0><br> <a href=$2>$4</a>"
                pattern, matcher, 
true);
        
        
//DIR
        strContent = ubbItem(strContent, 
                
"/[dir=*([0-9]*),*([0-9]*)/](.*?)/[//dir]"
                
"<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>"
                pattern, matcher, 
true);
        
        
//QT
        strContent = ubbItem(strContent, 
                
"/[qt=*([0-9]*),*([0-9]*)/](.*?)/[//qt]"
                
"<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/>"
                pattern, matcher, 
true);
        
        
//MP
        strContent = ubbItem(strContent, 
                
"/[mp=*([0-9]*),*([0-9]*)/](.*?)/[//mp]"
                
"<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>"
                pattern, matcher, 
true);
        
        
//RM
        strContent = ubbItem(strContent, 
                
"/[rm=*([0-9]*),*([0-9]*)/](.*?)/[//rm]"
                
"<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>"
                pattern, matcher, 
true);

        
//FLASH
        strContent = ubbItem(strContent, 
                
"(/[flash/])(.*?)(/[//flash/])"
                
"<OBJECT codeBase=http:" +
                
"//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab" +

抱歉!评论已关闭.