站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题让你的JSP支持Ubbcode

让你的JSP支持Ubbcode

摘要: 让你的JSP支持Ubbcode
  1. import java.util.regex.*;//导入正则表达式包中的类
  2. /**
  3. * 使网页支持ubbcode.
  4. * @author shanwei
  5. * @version 1.0
  6. */
  7. public final class Ubbcode {
  8. private static final String ImagePath = "images/"; //定义图片路径
  9. private static final String emotImagePath = "images/emot/";
  10. /**
  11. * @description 转换代码,使之支持ubbcode.
  12. * @param strContent 转换前的代码.
  13. * @return result 转换后的内容.
  14. */
  15. public static String UBBCode(String strContent) { //
  16. strContent = dvHTMLEncode(strContent);
  17. strContent = FilterJS(strContent);
  18. String re;
  19. //re.IgnoreCase =true
  20. //re.Global=True
  21. Pattern pattern = null; //
  22. Matcher matcher = null;
  23. //转换IMG
  24. re = "\\[IMG\\](http|https|ftp):\\/\\/(.[^\\[]*)\\[\\/IMG\\]";
  25. pattern = Pattern.compile(re);
  26. matcher = pattern.matcher(strContent);
  27. strContent = matcher.replaceAll("screen.width-333)this.width=screen.width-333\">");
  28. //转换UPLOAD
  29. re = "\\[UPLOAD=(gif|jpg|jpeg|bmp|png)\\](.[^\\[]*)(gif|jpg|jpeg|bmp|png)\\[\\/UPLOAD\\]";
  30. pattern = Pattern.compile(re);
  31. matcher = pattern.matcher(strContent);
  32. strContent = matcher.replaceAll("
    此主题相关图片如下:
    screen.width-333)this.width=screen.width-333\">");
  33. re = "\\[UPLOAD=(.[^\\[]*)\\](.[^\\[]*)\\[\\/UPLOAD\\]";
  34. pattern = Pattern.compile(re);
  35. matcher = pattern.matcher(strContent);
  36. strContent = matcher.replaceAll("
  37. "$1.gif\" border=0> 点击浏览该文件");
  38. //转换DIR
  39. re = "\\[DIR=*([0-9]*),*([0-9]*)\\](.[^\\[]*)\\[\\/DIR]";
  40. pattern = Pattern.compile(re);
  41. matcher = pattern.matcher(strContent);
  42. strContent = matcher.replaceAll("");
  43. //转换QT
  44. re = "\\[QT=*([0-9]*),*([0-9]*)\\](.[^\\[]*)\\[\\/QT]";
  45. pattern = Pattern.compile(re);
  46. matcher = pattern.matcher(strContent);
  47. strContent = matcher.replaceAll(";");
  48. //转换MP
  49. re = "\\[MP=*([0-9]*),*([0-9]*)\\](.[^\\[]*)\\[\\/MP]";
  50. pattern = Pattern.compile(re);
  51. matcher = pattern.matcher(strContent);
  52. strContent = matcher.replaceAll("");
  53. //转换RM
  54. re = "\\[RM=*([0-9]*),*([0-9]*)\\](.[^\\[]*)\\[\\/RM]";
  55. pattern = Pattern.compile(re);
  56. matcher = pattern.matcher(strContent);
  57. strContent = matcher.replaceAll("
    ");
  58. //转换FLASH
  59. re = "(\\[FLASH\\])(.[^\\[]*)(\\[\\/FLASH\\])";
  60. pattern = Pattern.compile(re);
  61. matcher = pattern.matcher(strContent);
  62. strContent = matcher.replaceAll(
  63. "[全屏欣赏]
    $2");
  64. re = "(\\[FLASH=*([0-9]*),*([0-9]*)\\])(.[^\\[]*)(\\[\\/FLASH\\])";
  65. pattern = Pattern.compile(re);
  66. matcher = pattern.matcher(strContent);
  67. strContent = matcher.replaceAll(
  68. "[全屏欣赏]
    $4");
  69. //转换URL
  70. re = "(\\[URL\\])(.[^\\[]*)(\\[\\/URL\\])";
  71. pattern = Pattern.compile(re);
  72. matcher = pattern.matcher(strContent);
  73. strContent = matcher.replaceAll("$2");
  74. re = "(\\[URL=(.[^\\[]*)\\])(.[^\\[]*)(\\[\\/URL\\])";
  75. pattern = Pattern.compile(re);
  76. matcher = pattern.matcher(strContent);
  77. strContent = matcher.replaceAll("$3");
  78. //转换EMAIL
  79. re = "(\\[EMAIL\\])(\\S+\\@.[^\\[]*)(\\[\\/EMAIL\\])";
  80. pattern = Pattern.compile(re);
  81. matcher = pattern.matcher(strContent);
  82. strContent = matcher.replaceAll("
  83. ImagePath +
  84. "email1.gif\">$2");
  85. re = "(\\[EMAIL=(\\S+\\@.[^\\[]*)\\])(.[^\\[]*)(\\[\\/EMAIL\\])";
  86. pattern = Pattern.compile(re);
  87. matcher = pattern.matcher(strContent);
  88. strContent = matcher.replaceAll("
  89. ImagePath +
  90. "email1.gif\">$3");
  91. //自动识别网址
  92. re =
  93. "^((http|https|ftp|rtsp|mms):(\\/\\/|\\\\)[A-Za-z0-9\\./=\\?%\\-&_~`@':+!]+)";
  94. pattern = Pattern.compile(re);
  95. matcher = pattern.matcher(strContent);
  96. strContent = matcher.replaceAll(
  97. "$1");
  98. re =
  99. "((http|https|ftp|rtsp|mms):(\\/\\/|\\\\)[A-Za-z0-9\\./=\\?%\\-&_~`@':+!]+)$";
  100. pattern = Pattern.compile(re);
  101. matcher = pattern.matcher(strContent);
  102. strContent = matcher.replaceAll(
  103. "$1");
  104. re = "([^>=\"])((http|https|ftp|rtsp|mms):(\\/\\/|\\\\)[A-Za-z0-9\\./=\\?%\\-&_~`@':+!]+)";
  105. pattern = Pattern.compile(re);
  106. matcher = pattern.matcher(strContent);
  107. strContent = matcher.replaceAll("$1$2");
  108. //自动识别www等开头的网址
  109. re = "([^(http://|http:\\\\)])((www|cn)[.](\\w)+[.]{1,}(net|com|cn|org|cc)(((\\/[\\~]*|\\[\\~]*)(\\w)+)|[.](\\w)+)*(((([?](\\w)+){1}[=]*))*((\\w)+){1}([\\&](\\w)+[\\=](\\w)+)*)*)";
  110. pattern = Pattern.compile(re);
  111. matcher = pattern.matcher(strContent);
  112. strContent = matcher.replaceAll("$2");
  113. //自动识别Email地址,如打开本功能在浏览内容很多的帖子会引起服务器停顿
  114. re = "([^(=)])((\\w)+[@]{1}((\\w)+[.]){1,3}(\\w)+)";
  115. pattern = Pattern.compile(re);
  116. matcher = pattern.matcher(strContent);
  117. strContent = matcher.replaceAll("$2");
  118. //转换EM
  119. re = "\\[em(.[^\\[]*)\\]";
  120. pattern = Pattern.compile(re);
  121. matcher = pattern.matcher(strContent);
  122. strContent = matcher.replaceAll("
  123. "em$1.gif\" border=0 align=middle>");
  124. //转换HTML
  125. re = "\\[HTML\\](.[^\\[]*)\\[\\/HTML\\]";
  126. pattern = Pattern.compile(re);
  127. matcher = pattern.matcher(strContent);
  128. strContent = matcher.replaceAll("<table width='100%' border='0' cellspacing='0' cellpadding='6' class=tableborder1><td>以下内容为程序代码:
    $1</td></table>");
  129. //转换CODE
  130. re = "\\[code\\](.[^\\[]*)\\[\\/code\\]";
  131. pattern = Pattern.compile(re);
  132. matcher = pattern.matcher(strContent);
  133. strContent = matcher.replaceAll("<table width='100%' border='0' cellspacing='0' cellpadding='6' class=tableborder1><td>以下内容为程序代码:
    $1</td></table>");
  134. //转换COLOR
  135. re = "\\[color=(.[^\\[]*)\\](.[^\\[]*)\\[\\/color\\]";
  136. pattern = Pattern.compile(re);
  137. matcher = pattern.matcher(strContent);
  138. strContent = matcher.replaceAll("$2");
  139. //转换FACE
  140. re = "\\[face=(.[^\\[]*)\\](.[^\\[]*)\\[\\/face\\]";
  141. pattern = Pattern.compile(re);
  142. matcher = pattern.matcher(strContent);
  143. strContent = matcher.replaceAll("$2");
  144. //转换ALIGN
  145. re = "\\[align=(center|left|right)\\](.*)\\[\\/align\\]";
  146. pattern = Pattern.compile(re);
  147. matcher = pattern.matcher(strContent);
  148. strContent = matcher.replaceAll("
    $2
    ");
  149. //转换QUOTE
  150. re = "\\[QUOTE\\](.*)\\[\\/QUOTE\\]";
  151. pattern = Pattern.compile(re);
  152. matcher = pattern.matcher(strContent);
  153. strContent = matcher.replaceAll("<table style=\"width:80%\" cellpadding=5 cellspacing=1 class=tableborder1><tr><td class=tableborder1>$1</td></tr></table>
    ");
  154. //转换FLY
  155. re = "\\[fly\\](.*)\\[\\/fly\\]";
  156. pattern = Pattern.compile(re);
  157. matcher = pattern.matcher(strContent);
  158. strContent = matcher.replaceAll(
  159. "$1");
  160. //转换MOVE
  161. re = "\\[move\\](.*)\\[\\/move\\]";
  162. pattern = Pattern.compile(re);
  163. matcher = pattern.matcher(strContent);
  164. strContent = matcher.replaceAll("$1");
  165. //转换GLOW
  166. re =
  167. "\\[GLOW=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)\\](.[^\\[]*)\\[\\/GLOW]";
  168. pattern = Pattern.compile(re);
  169. matcher = pattern.matcher(strContent);
  170. strContent = matcher.replaceAll(
  171. "<table width=$1 style=\"filter:glow(color=$2, strength=$3)\">$4</table>");
  172. //转换SHADOW
  173. re =
  174. "\\[SHADOW=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)\\](.[^\\[]*)\\[\\/SHADOW]";
  175. pattern = Pattern.compile(re);
  176. matcher = pattern.matcher(strContent);
  177. strContent = matcher.replaceAll(
  178. "<table width=$1 style=\"filter:shadow(color=$2, strength=$3)\">$4</table>");
  179. re = "\\[i\\](.[^\\[]*)\\[\\/i\\]";
  180. pattern = Pattern.compile(re);
  181. matcher = pattern.matcher(strContent);
  182. strContent = matcher.replaceAll("$1");
  183. re = "\\[u\\](.[^\\[]*)(\\[\\/u\\])";
  184. pattern = Pattern.compile(re);
  185. matcher = pattern.matcher(strContent);
  186. strContent = matcher.replaceAll("$1");
  187. re = "\\[b\\](.[^\\[]*)(\\[\\/b\\])";
  188. pattern = Pattern.compile(re);
  189. matcher = pattern.matcher(strContent);
  190. strContent = matcher.replaceAll("$1");
  191. re = "\\[size=([1-4])\\](.[^\\[]*)\\[\\/size\\]";
  192. pattern = Pattern.compile(re);
  193. matcher = pattern.matcher(strContent);
  194. strContent = matcher.replaceAll("$2");
  195. /*re ="(\\s)";
  196. matcher = pattern.matcher(strContent);
  197. strContent=matcher.replaceAll("$1");*/
  198. re = null;
  199. return strContent;
  200. }
  201. /**
  202. * 过滤javascript代码.
  203. * @param v转换前的代码.
  204. * @return v转换后的内容.
  205. */
  206. public static String FilterJS(String v) {
  207. if (!v.equals("null") && v != null) {
  208. String t, re;
  209. re = "(javascript)";
  210. t = re.replace(v, "javascript");
  211. re = "(jscript:)";
  212. t = re.replace(t, "jscript:");
  213. re = "(js:)";
  214. t = re.replace(t, "js:");
  215. re = "(value)";
  216. t = re.replace(t, "value");
  217. re = "(about:)";
  218. t = re.replace(t, "about:");
  219. re = "(file:)";
  220. t = re.replace(t, "file:");
  221. re = "(document.cookie)";
  222. t = re.replace(t, "documents.cookie");
  223. re = "(vbscript:)";
  224. t = re.replace(t, "vbscript:");
  225. re = "(vbs:)";
  226. t = re.replace(t, "vbs:");
  227. re = "(on(mouse|exit|error|click|key))";
  228. t = re.replace(t, "on$2");
  229. re = "(&#)";
  230. t = re.replace(t, "&#");
  231. re = null;
  232. }
  233. return v;
  234. }
  235. /**
  236. * 替换一些特殊符号为html语法标记.
  237. * @param fString转换前的代码.
  238. * @return fString转换后的内容.
  239. */
  240.  public static String dvHTMLEncode(String fString) {
  241. if (!fString.equals("null") && fString != null) {
  242. fString = replace(fString, ">", ">");
  243. fString = replace(fString, "<", "<");
  244. fString = replace(fString, "&", "&");
  245. fString = replace(fString, " ", " ");
  246. fString = replace(fString, "\"", """);
  247. fString = replace(fString, "\'", "'");
  248. fString = replace(fString, "\r", "");
  249. fString = replace(fString, "\n", "
    ");
  250. fString = replace(fString, "\\", "\");
  251. }
  252. return fString;
  253. }
  254. /**
  255. * html语法标记转换符号本身.
  256. * @param fString转换前的代码.
  257. * @return fString转换后的内容.
  258. */
  259. public static String dvHTMLCode(String fString) {
  260. if (!fString.equals("null") && fString != null) {
  261. fString = replace(fString, ">", ">");
  262. fString = replace(fString, "<", "<");
  263. fString = replace(fString, "&", "&");
  264. fString = replace(fString, " ", " ");
  265. fString = replace(fString, """, "\"");
  266. fString = replace(fString, "'", "\'");
  267. fString = replace(fString, "\", "\\");
  268. fString = replace(fString, "
    ", "\n");
  269. }
  270. return fString;
  271. }
  272. public static String nohtml(String str) {
  273. String re;
  274. Pattern pattern = null;
  275. Matcher matcher = null;
  276. re = "(\\<.[^\\<]*\\>)";
  277. pattern = Pattern.compile(re);
  278. matcher = pattern.matcher(str);
  279. str = matcher.replaceAll(" ");
  280. re = "(\\<\\/[^\\<]*\\>)";
  281. str = re.replace(str, " ");
  282. re = null;
  283. return str;
  284. }
  285. /**
  286. * 分割函数.
  287. * @param source原字符串,div将要被分割的字符串.
  288. * @return returnStr分割后的字符串.
  289. */
  290. public static String[] split(String source, String div) {
  291. int arynum = 0, intIdx = 0, intIdex = 0, div_length = div.length();
  292. if (source.compareTo("") != 0) {
  293. if (source.indexOf(div) != -1) {
  294. intIdx = source.indexOf(div);
  295. for (int intCount = 1; ; intCount++) {
  296. if (source.indexOf(div, intIdx + div_length) != -1) {
  297. intIdx = source.indexOf(div, intIdx + div_length);
  298. arynum = intCount;
  299. } else {
  300. arynum += 2;
  301. break;
  302. }
  303. }
  304. } else {
  305. arynum = 1;
  306. }
  307. } else {
  308. arynum = 0;
  309. }
  310. intIdx = 0;
  311. intIdex = 0;
  312. String[] returnStr = new String[arynum];
  313.   if (source.compareTo("") != 0) {
  314. if (source.indexOf(div) != -1) {
  315. intIdx = (int) source.indexOf(div);
  316. returnStr[0] = (String) source.substring(0, intIdx);
  317. for (int intCount = 1; ; intCount++) {
  318. if (source.indexOf(div, intIdx + div_length) != -1) {
  319. intIdex = (int) source.indexOf(div, intIdx + div_length);
  320. returnStr[intCount] = (String) source.substring(intIdx +
  321. div_length,
  322. intIdex);
  323. intIdx = (int) source.indexOf(div, intIdx + div_length);
  324. } else {
  325. returnStr[intCount] = (String) source.substring(intIdx +
  326. div_length,
  327. source.length());
  328. break;
  329. }
  330. }
  331. } else {
  332. returnStr[0] = (String) source.substring(0, source.length());
  333. return returnStr;
  334. }
  335. } else {
  336. return returnStr;
  337. }
  338. return returnStr;
  339. }
  340. /**
  341. * 替换函数.
  342. * @param str替换前的字符串,substr被替换的字符,restr替换的字符.
  343. * @return 替换后并且经过除空处理的字符串.
  344. */
  345. public static String replace(String str, String substr, String restr) {
  346. String[] tmp = split(str, substr);
  347. String returnstr = null;
  348. if (tmp.length != 0) {
  349. returnstr = tmp[0];
  350. for (int i = 0; i < tmp.length - 1; i++) {
  351. returnstr = dealNull(returnstr) + restr + tmp[i + 1];
  352. }
  353. }
  354. return dealNull(returnstr);
  355. }
  356. /**
  357. * 除空处理函数.
  358. * @param str原字符串.
  359. * @return 处理后的字符串.
  360. */
  361. public static String dealNull(String str) {
  362. String returnstr = null;
  363. if (str == null) {
  364. returnstr = "";
  365. } else {
  366. returnstr = str;
  367. }
  368. return returnstr;
  369. }
  370. public static String tostring(Character i) {
  371. Character c = i;
  372. return c.toString();
  373. }
  374. }

引自:http://www.javaresearch.org/article/showarticle.jsp?column=106&thread=51550



↑返回目录
前一篇: JSP白皮书
后一篇: JSP环境基于Session的在线用户统计深入分析