欢迎光临
我们一直在努力

Use Java to implement the UBB future.-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

ubb & java
    —-use java to implement the ubb future.

[author] pizer.chen — iceant — 陈鹏
[email ] iceant@21cn.com
[icq   ] 77777369

these days,i write some publish system,just like bbs/news etc.
the client asked me ,whether they can insert the image and href url to the plain text when they published them.
so i write this.
i use xml file for dynamic ubb extends.
u can make the ubb rule of yourself.
the only thing that u need to do is modify the ubb.xml config file.

best regards
===============
                                                                    pizer.chen/2001-9-4

===========================
    resource link
===========================
it is based on xml & regularexpressions tech.
about xml & regularexpression u can find them here:

http://www.w3.org/xml/     
http://xml.apache.org
http://www.meurrens.org/ip-links/java/regex/navi.html
http://www.savarese.org/oro/
http://jakarta.apache.org/oro
about ubb u can find them here:
http://www.ubbdesign.com

========================
it used :
    jakarta-regexp-1.2              (download form http://jakarta.apache.org)
    dom4j-0.6(over 0.6 version)     (download form http://sourceforge.net)
    jdk(over 1.2 version)           (download form http://java.sun.com)

==========================
        ubb.xml
it must be stored in classpath
==========================
the regularexpression & replace string map file.
<!–============================–>

<?xml version="1.0" encoding="utf-8"?>
<ubb-map>
    <map ubb-code="\[b\](.+?)\[\/b\]" map-to="<b>$1</b>"/>         <!–<b>$1</b>–>
    <map ubb-code="\[i\](.+?)\[\/i\]" map-to="<i>$1</i>"/>         <!–<i>$1</i>–>
    <map ubb-code="\[h1\](.+?)\[\/h1\]" map-to="<h1>$1</h1>"/>     <!–<h1>$1</h1>–>
    <map ubb-code="\(.+?)\[\/url\]" map-to="<a href="$1" target="_blank">$2</a>"/>        <!–<a href="$1" target="_blank">$2</a>–>
    <!–…–>
</ubb-map>

=========================

        ubb.java

=========================

/*
* ubb.java
*
* created on 2001年9月3日, 下午4:01
*/

package com.wacos.util.ubb;

import org.dom4j.*;
import org.dom4j.io.*;
import java.io.*;
import java.util.*;
import org.apache.regexp.*;
/**
*
* @author  pizer.chen — iceant — 陈鹏
* @version 0.1
*/
public class ubb {
    
    private static final string xml_config_file = "ubb.xml";
    private static org.dom4j.document doc = null;
     
    /** creates new ubb */
    public ubb() {
    }
    
    public static string parse(string instr){
        try{
            list list = getubbcodelist();
            string ubbcode="";
            string mapstr="";
            attribute attribute=null;
            for (iterator iter = list.iterator(); iter.hasnext(); ) {
                attribute = (attribute) iter.next();
                ubbcode = attribute.getvalue();
                mapstr= getmapvalue(ubbcode);
                instr=rereplace.replace(ubbcode,mapstr,instr);
            }
            return instr;
        }catch(exception err){
            system.out.println(err);
            return err.tostring();
        }
    }

    /**
     * parse the xml file to document object
    **/
    private static document xml2document(){
        try{
            inputstream is = thread.currentthread().getcontextclassloader()
                            .getresourceasstream(xml_config_file);
            saxreader reader = new saxreader();
            document document = reader.read(is);
            return document;
        }catch(exception err){
            system.out.println(err);
            return null;
        }
    }

    /**
     * use xpath to get the map-to value of the ubb-code.
    **/
    private static string getmapvalue(string ubbcode){
        try{
            if(doc==null){
                doc=xml2document();
            }
            node node = doc.selectsinglenode("//map[@ubb-code="+ubbcode+"]");
            return node.valueof( "@map-to" );
        }catch(exception err){
            system.out.println(err);
            return err.tostring();
        }
    }
    /**
     * get the <map ubb-code="…" map-to=".."> ubb-code list
    **/
    private static list getubbcodelist(){
        try{
            if(doc==null){
                doc=xml2document();
            }
            return doc.selectnodes( "//map/@ubb-code" );
        }catch(exception err){
            system.out.println(err);
            return null;
        }
    }
    /**
     * get the <map ubb-code="…" map-to=".."> map-to list
    **/
    private static list getubbmaplist(){
        try {
            if(doc==null){
                doc=xml2document();
            }
            return doc.selectnodes("//map//@map-to");
        }
        catch (exception e) {
            system.out.println(e);
            return null;
        }
    }
}

========================

     rereplace.java

========================
package com.wacos.util.ubb;

import java.io.*;
import java.util.*;
import org.apache.regexp.*;

public class rereplace
{
    /**
     * replace the instr with pattern1 & pattern2
    **/
    public static string replace(string pattern1,string pattern2,string instr){
        try {
            re re = new re(pattern1);
            int point=0;
            while(re.match(instr)){
                re re2 = new re("\\$([0-9])");
                while(re2.match(pattern2)){
                    point = integer.parseint(re2.getparen(1));
                    pattern2=re2.subst(pattern2,re.getparen(point),re.replace_firstonly);
                }
                instr = re.subst(instr,pattern2);
            }
            return instr;
        }
        catch (exception e) {
            system.out.println(e);
            return e.tostring();
        }  
    }

}

=============================
        ubbtest
=============================
package com.wacos.util.ubb;

public class ubbtest
{
    public static void main(string[] args)
    {
        try{
            string test ="atest

h1 font

\r\n [url href=http://www.21cn.com]测试hehe..";

            system.out.println(ubb.parse(test));
        }catch(exception err){
            system.out.println(err);
        }
    }
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Use Java to implement the UBB future.-JSP教程,Java技巧及代码
分享到: 更多 (0)