用户: 密码: 答案:   我要注册   忘记密码

加入收藏  设为首页

开发文档

CNJM首页

业界新闻

手机软件

终端应用

资源下载

EclipseME

CNJM论坛

                 

频道列表

J2ME开发 188篇
服务器端开发 33篇
JAVA语言 79篇
游戏与图形 101篇
WindowsMobile开发 6篇
Symbian开发 62篇
Brew开发 36篇
其它开发平台 8篇

热点文章

cookie和session机... 11401次
介绍一篇关于sessi... 9130次
[J2EE新手入门]使...  8141次
一个可扩展的高速U... 8095次
使用Java实现UBBCo... 7802次
JSP安全性初探 7612次
Eclipse 3.4 (Gany... 7511次
所有支持WAP手机上... 7451次
在Win2K+resin中配... 7378次
Apache下 配置WAP ... 7152次
JSP应用程序开发中... 6806次
J2EE应用性能问题...  6797次

文章搜索

搜 索
按 照
频 道
  
使用Java实现UBBCode的转换
编辑:rocks    审核:rocks    文章来源:IT狐
关键词:ubb    发表日期:2006-03-03 10:09:58    浏览次数:7803次
本文版权归原作者,中国JAVA手机网收录本文的目的是让更多人阅读到此文章。转载请注明出处为中国JAVA手机网<www.cnjm.net>

[本文章最后由 rocks 在2006-03-23 15:23:40编辑过]

来自:http://www.cnjm.net/tech/article1072.html

[转载于IT狐]

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
JAVA手机网[www.cnjm.net]

JAVA手机网[www.cnjm.net]
===========================
JAVA手机网[www.cnjm.net]
   Resource Link
JAVA手机网[www.cnjm.net]
===========================
It is based on xml & RegularExpressions tech.
JAVA手机网[www.cnjm.net]
about xml & regularExpression u can find them here:

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

========================
it used :
JAVA手机网[www.cnjm.net]
   jakarta-regexp-1.2              (download form [url]http://jakarta.apache.org[/url])
   dom4j-0.6(over 0.6 version)     (download form [url]http://sourceforge.net[/url])
   jdk(over 1.2 version)           (download form [url]http://java.sun.com[/url])

==========================
       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
JAVA手机网[www.cnjm.net]
*
* Created on 2001年9月3日, 下午4:01
*/

package com.wacos.util.ubb;

import org.dom4j.*;
import org.dom4j.io.*;
JAVA手机网[www.cnjm.net]
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() {
   }
JAVA手机网[www.cnjm.net]
   
   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);
JAVA手机网[www.cnjm.net]
           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){
JAVA手机网[www.cnjm.net]
               doc=xml2Document();
JAVA手机网[www.cnjm.net]
           }
           Node node = doc.selectSingleNode("//map[@ubb-code='"+ubbCode+"']");
           return node.valueOf( "@map-to" );
       }catch(Exception err){
           System.out.println(err);
           return err.toString();
       }
   }
   /**
JAVA手机网[www.cnjm.net]
    * get the <map ubb-code="..." map-to=".."> ubb-code List
JAVA手机网[www.cnjm.net]
   **/
   private static List getUBBCodeList(){
       try{
JAVA手机网[www.cnjm.net]
           if(doc==null){
               doc=xml2Document();
           }
           return doc.selectNodes( "//map/@ubb-code" );
       }catch(Exception err){
           System.out.println(err);
JAVA手机网[www.cnjm.net]
           return null;
       }
   }
   /**
JAVA手机网[www.cnjm.net]
    * get the <map ubb-code="..." map-to=".."> map-to List
   **/
   private static List getUBBMapList(){
JAVA手机网[www.cnjm.net]
       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
JAVA手机网[www.cnjm.net]
   **/
   public static String replace(String pattern1,String pattern2,String inStr){
       try {
           RE re = new RE(pattern1);
           int point=0;
JAVA手机网[www.cnjm.net]
           while(re.match(inStr)){
               RE re2 = new RE("\$([0-9])");
               while(re2.match(pattern2)){
JAVA手机网[www.cnjm.net]
                   point = Integer.parseInt(re2.getParen(1));
                   pattern2=re2.subst(pattern2,re.getParen(point),RE.REPLACE_FIRSTONLY);
               }
JAVA手机网[www.cnjm.net]
               inStr = re.subst(inStr,pattern2);
           }
           return inStr;
       }
       catch (Exception e) {
           System.out.println(e);
           return e.toString();
JAVA手机网[www.cnjm.net]
       }  
   }

}


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

public class UBBTest
{
   public static void main(String[] args)
   {
       try{
           String test ="a[b]test[/b][h1]H1 Font[/h1]rn [url href=[url]http://www.21cn.com[/url]]测试hehe..";

           System.out.println(UBB.parse(test));
       }catch(Exception err){
           System.out.println(err);
       }
   }
}
来自:http://www.cnjm.net/tech/article1072.html

相关文章
    一个可扩展的高速UBB标签转换引擎  [2006-03-23]
最新评论
网站简介  |  关于版权  |  广告服务  |  网站地图  |  联系我们
Copyright © www.CNJM.net, All rights reserved
中国JAVA手机网 版权所有 京ICP备041452
本站带宽由互联互通提供