⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simpletaghandler.java

📁 ubb标签转换的程序 用于jsp开发中.
💻 JAVA
字号:
package util;

/**
 * 版权所有 2006(C) 中国Java手机网(www.cnjm.net)。所有版权保留。
 * 作者开放本代码的目的是希望更多人使用它,任何人只要遵从以下条款,就可以传播、使用或修改本代码: 
 * 1. 再发布本代码必须完整保留此版权声明及条款,不得删除。
 * 2. 使用本代码开发的软件必须在版权信息中声明: “本软件在开发中使用了中国Java手机网(www.cnjm.net)编写的UBB转换引擎”。
 * 3. 使用本代码开发的软件应在文档中加入中国Java手机网(www.cnjm.net)的链接。
 * 4. 使用者必须独自承担使用本代码的风险,中国Java手机网不为本代码对其软件或系统可能造成的任何损害承担责任。
 */
public class SimpleTagHandler implements UBBTagHandler {
        
//    [b]文字加粗体效果[/b]
//    [i]文字加倾斜效果[/i]
//    [u]文字加下划线效果[/u]
//    [size=4]改变文字大小[/size]
//    [color=red]改变文字颜色[/color]
//    [quote]这个标签是用来做为引用所设置的,如果你有什么内容是引用自别的地方,请加上这个标签![/quote]  
//    [url]http://www.cnjm.net[/url]
//    [url=http://www.cnjm.net]JAVA手机网[/url]
//    [email=webmaster@cnjm.net]写信给我[/email] 
//    [email]webmaster@cnjm.net[/email]
//    [img]http://www.cnjm.net/myimages/mainlogo.gif[/img]    
    
    public SimpleTagHandler() { }

    public String[] parseTag(String s, boolean isEmpty) {
        if (isEmpty) { // 本处理器不支持空标签
            return null;
        }
        // 如果标签中有'='号就把标签分为UBB标记和属性两部分,否则属性为null
        String tag = s, attr = null;
        int idx = s.indexOf('=');
        if (idx >= 0) {
            tag = s.substring(0, idx);
            attr = s.substring(idx + 1);
        }
        String tmp = tag.toLowerCase(); // 大小写不敏感
        // 只有下面的标记是本处理器支持的
        if ("b".equals(tmp) || 
            "i".equals(tmp) ||
            "u".equals(tmp) ||
            "size".equals(tmp) ||
            "color".equals(tmp) ||
            "quote".equals(tmp) ||
            "url".equals(tmp) ||
            "email".equals(tmp) ||
            "img".equals(tmp)) { 
            return new String[] { tag, attr };
        }
        // 不是一个合法的UBB标签,作为普通文本处理
        return null;
    }

    public String compose(String tag, String attr, String data, boolean isEmpty) {
        // 针对不同标记进行组合工作
        String tmp = tag;
        if ("b".equals(tmp) ||
            "i".equals(tmp) ||
            "u".equals(tmp)) {
            return "<" + tag + ">" + data + "</" + tag + ">";
        } else if ("size".equals(tmp) ||
            "color".equals(tmp)) {
            return "<font " + tag + "='" + attr + "'>" + data + "</font>";
        } else if ("quote".equals(tmp)) {
            return "<table cellpadding=0 cellspacing=0 width=94% bgcolor=#000000 align=center style='table-layout:fixed'><tr><td><table width=100% cellpadding=5 cellspacing=1 style='table-layout:fixed'><tr><td bgcolor=#FFFFFF style='left: 0px; width: 100%; word-wrap: break-word'>"
                    + data + "</td></tr></table></td></tr></table>";
        } else if ("url".equals(tmp)) {
            String url = attr != null ? attr : data;
            return "<a href='" + url + "' target=_blank>" + data + "</a>";
        } else if ("email".equals(tmp)) {
            String email = attr != null ? attr : data;
            return "<a href='mailto:" + email + "'>" + data + "</a>";
        } else if ("img".equals(tmp)) {
            return "<img src='" + data + "' border=0>";
        }
        return data;
    }
    
    // 测试代码,可以运行这个类,并把包含UBB标签的文本作为参数传入来测试
    // 比如java util.SimpleTagHandler "[color=red]你[color=blue]好[/color]啊[/color]"
    public static void main(String[] args) throws Exception {
        System.out.println(">>>>" + args[0]);
        // 下面采用了忽略模式来容错,你也可以用MODE_CLOSE试验一下关闭模式的容错效果
        System.out.println("=========================\n" 
                + UBBDecoder.decode(args[0], new SimpleTagHandler(), UBBDecoder.MODE_IGNORE));
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -