📄 simpletaghandler.java
字号:
package com.yeqiangwei.club.view.util;
/**
* this code form www.cnjm.net
* Last modified by gamvan
*/
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) ||
"url".equals(tmp) ||
"email".equals(tmp) ||
"bg".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 ("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 "<a href=\""+data+"\" target=\"_blank\"><img onload=\"javascript:if(this.width > screen.width-350){this.width = screen.width-350};\" onerror=\"javascript:this.src='/club/images/imgErr.gif'\" src=\""+data+"\" alt=\"\"/></a>";
}
else if("bg".equals(tmp)){
return "<span style=\"background:"+attr+"\">"+data+"</span>";
}
return data;
}
// 测试代码,可以运行这个类,并把包含UBB标签的文本作为参数传入来测试
// 比如java util.SimpleTagHandler "[color=red]你[color=blue]好[/color]啊[/color]"
public static void main(String[] args) throws Exception {
// 下面采用了忽略模式来容错,你也可以用MODE_CLOSE试验一下关闭模式的容错效果
System.out.println("=========================\n"
+ UBBDecoder.decode("[quote=ccc站在7楼说:][quote=蓝色咖啡坐在板凳上说:]歪了[/quote][/quote]", new SimpleTagHandler(), UBBDecoder.MODE_IGNORE));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -