`
zhuwei2008
  • 浏览: 83128 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java解析ubb2html

    博客分类:
  • java
阅读更多
package com.ztx.club.util; import java.io.UnsupportedEncodingException; import java.util.Hashtable; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.ztx.util.web.ParamUtils; public class StringUtils { private static final String[] regexUBB = new String[] { "\\[/url\\]", "\\[/email\\]", "\\[/color\\]", "\\[/size\\]", "\\[/font\\]", "\\[/align\\]", "\\[b\\]", "\\[/b\\]", "\\[i\\]", "\\[/i\\]", "\\[u\\]", "\\[/u\\]", "\\[list\\]", "\\[list=1\\]", "\\[list=a\\]", "\\[list=A\\]", "\\[\\*\\]", "\\[/list\\]", "\\[indent\\]", "\\[/indent\\]", "\\[code\\]", "\\[/code\\]", "\\[quote\\]", "\\[/quote\\]", "\\[table\\]", "\\[tr\\]", "\\[td\\]", "\\[/tr\\]", "\\[/td\\]", "\\[/table\\]" }; private static final String[] replacementUBB = new String[] { "", "", "", "", "", "

", "", "", "", "", "", "", "
    ", "
      ", "
        ", "
          ", "
        1. ", "
", "
", "
", "
<textarea name="codes" id="codes" rows="12" cols="65">", "</textarea>
<input type="button" value="运行代码" onclick="RunCode()"> <input type="button" value="复制代码" onclick="CopyCode()"> <input type="button" value="另存代码" onclick="SaveCode()">  提示:您可以先修改部分代码再运行
", "
", "
", "", "", "", "", "
", "
" }; private static final String[] replacementHTML = new String[] { "[/align]", "[b]", "[/b]", "[i]", "[/i]", "[u]", "[/u]", "[list]", "[list=1]", "[list=a]", "[list=A]", "[*]", "[/list]", "[indent]", "[/indent]", "[code]", "[/code]", "[quote]", "[/quote]", "[table]", "[tr]", "[td]", "[/tr]", "[/td]", "[/table]" }; private static final String[] regexHTML = new String[] { "
", "", "", "", "", "", "", "
    ", "
      ", "
        ", "
          ", "
        1. ", "
", "
", "
", "
<textarea name="codes" id="codes" rows="12" cols="65">", "</textarea>
<input type="button" value="运行代码" onclick="RunCode\\(\\)"> <input type="button" value="复制代码" onclick="CopyCode\\(\\)"> <input type="button" value="另存代码" onclick="SaveCode\\(\\)">  提示:您可以先修改部分代码再运行
", "
", "
", "", "", "", "", "
", "
" }; public static String parsetable(String width,String bgcolor,String str) { if(width==null ) { width="90%"; } else { width = width.trim().endsWith("%")?( Integer.parseInt(width.substring(0, width.length() - 1)) <= 98 ? width :"98%"):(Integer.parseInt(width) <= 560 ? width : "98%"); } if(bgcolor==null){ bgcolor="#000"; } str = str.replaceAll("\\[tr(?:=([\\(\\)%,#\\w]+))?\\]\\s*\\[td(?:=(\\d{1,2}),(\\d{1,2})(?:,(\\d{1,4}%?))?)?\\]", ""+"" ); str = str.replaceAll("\\[\\/td\\]\\s*\\[td(?:=(\\d{1,2}),(\\d{1,2})(?:,(\\d{1,4}%?))?)?\\]", "" ); str = str.replaceAll("\\[\\/td\\]\\s*\\[\\/tr\\]", ""); return "<table" +="" "="" width=" + width + " style="\"background:"+" bgcolor+"\""="" +="" "="">" + str + ""; } public static String tableStr(String str){ if(str==null&&str.trim().equals("")) return ""; String temp=""; String width=""; String bgcolor=""; Pattern pattern=Pattern.compile("\\[table(?:=(\\d{1,4}%?)(?:,([\\(\\)%,#\\w ]+))?)?\\]\\s*([\\s\\S]+?)\\s*\\[\\/table\\]"); Matcher ma=pattern.matcher(str); while(ma.find()){ width=ma.group(1); bgcolor=ma.group(2); temp=ma.group(3); str=parsetable(width,bgcolor,temp); System.out.println(width+":\n "+bgcolor+" :\n"+temp+":\n"); } System.out.println("str----:"+str); return str; } public static String recursionU2H(String tagname,String text,String dofunction,String extraargs){ if(text.trim().equals("")) return ""; String str=""; int pos_begintag=0; do{ text=text.trim().toLowerCase(); int len=text.length(); String begintag="["+tagname; int len_begintag=begintag.length(); int searchtop=0; boolean found=false; pos_begintag=text.indexOf(begintag,searchtop); if(pos_begintag==-1) break; for(int i=pos_begintag+len_begintag;i< regexUBB.length; i++) // ubb = ubb.replaceAll(regexUBB[i], replacementUBB[i]); // // return ubb; // } // // public static String escapeHTML2UBB(String html) { // // 图片html // html = html // .replaceAll( // "按此在新窗口浏览图片", // "[img]$1://$2[/img]"); // // 链接html // html = html.replaceAll( // "(.*)", // "[url=$1://$2]$3[/url]"); // // 邮箱html // html = html // .replaceAll( // "(.*)", // "[email=$1]$2[/email]"); // // QQ号码html // html = html // .replaceAll( // "点击这里给我发消息", // "[qq=$1]$1[/qq]"); // // html = html // .replaceAll( // "(.*)(.*)(.*)(.*)(.*)", // "[$1=$2]$3[$4=$5]$6[$7=$8]$9[/$7]$10[/$4]$11[/$1]"); // // html = html // .replaceAll( // "(.*)(.*)(.*)", // "[$1=$2]$3[$4=$5]$6[/$4]$7[/$1]"); // // html = html.replaceAll("(.*)", // "[$1=$2]$3[/$1]"); // // html = html.replaceAll("\\[face=([^\\]]*)\\](.*)\\[/face\\]", // "[font=$1]$2[/font]"); // // // 文字对齐方式html // html = html.replaceAll("
", // "[align=$1]"); // // 表格html // html = html.replaceAll("
", "[table=$1]"); // // FLASH动画html // html = html // .replaceAll( // "([^']*)", // "[flash]$1[/flash]"); // html = html // .replaceAll( // "([^']*)", // "[flash=$1,$2]$3[/flash]"); // // MEDIA PLAY播放html // html = html // .replaceAll( // "", // "[wmv]$1[/wmv]"); // html = html // .replaceAll( // "", // "[wmv=$1,$2]$3[/wmv]"); // // REALPLAY 播放html // html = html // .replaceAll( // "
", // "[rm]$1[/rm]"); // html = html // .replaceAll( // "
", // "[rm=$1,$2]$3[/rm]"); // // for (int i = 0; i < regexHTML.length; i++) // html = html.replaceAll(regexHTML[i], replacementHTML[i]); // // return html; // } public static String escapeUBB2HTML(String ubb) { ubb = ParamUtils.escapeHTMLTags(ubb); ubb=ubb.trim().toLowerCase(); if(ubb.trim().equals("")){ return ""; } // 链接UBB ubb=ubb.replaceAll("\\[url\\]\\s*(www.|https?:\\/\\/|ftp:\\/\\/|gopher:\\/\\/|news:\\/\\/|telnet:\\/\\/|rtsp:\\/\\/|mms:\\/\\/|callto:\\/\\/|bctp:\\/\\/|ed2k:\\/\\/){1}([^\\[\"\']+?)\\s*\\[\\/url\\]", ""); ubb= ubb.replaceAll("\\[url=www.([^\\[\"\']+?)\\](.+?)\\[\\/url\\]", "$2"); ubb= ubb.replaceAll("\\[url=(https?|ftp|gopher|news|telnet|rtsp|mms|callto|bctp|ed2k){1}:\\/\\/([^\\[\"\']+?)\\]([\\s\\S]+?)\\[\\/url\\]", "$3"); //字体 ubb = ubb.replaceAll("\\[color=([^\\[\\<]+?)\\]", ""); ubb = ubb.replaceAll("\\[size=(\\d+?)\\]", ""); ubb = ubb.replaceAll("\\[size=(\\d+(\\.\\d+)?(px|pt|in|cm|mm|pc|em|ex|%)+?)\\]", ""); ubb = ubb.replaceAll("\\[font=([^\\[\\<]+?)\\]", ""); ubb = ubb.replaceAll("\\[align=([^\\[\\<]+?)\\]", "

"); //span ubb = ubb.replaceAll("\\[float=([^\\[\\<]+?)\\]", "
"); //table ubb = tableStr(ubb); //picture ubb = ubb.replaceAll("\\[img\\]\\s*([^\\[\\<\\r\\n]+?)\\s*\\[\\/img\\]", ""); ubb = ubb.replaceAll("\\[img=(\\d{1,4})[x|\\,](\\d{1,4})\\]\\s*([^\\[\\<\\r\\n]+?)\\s*\\[\\/img\\]", "\"\""); ubb = ubb.replaceAll("(\\[flash\\])(http://[^\\]]*(.swf))(\\[/flash\\])","$2"); ubb = ubb.replaceAll("(\\[flash=*([0-9]*),*([0-9]*)\\])(http://[^\\]]*(.swf))(\\[/flash\\])","$4"); // 替换不匹配标签 for (int i = 0; i < regexUBB.length; i++) ubb = ubb.replaceAll(regexUBB[i], replacementUBB[i]); return ubb; } public static String tabletag(String attributes) { String width = ""; String re = "width=([\"\']?)(\\d{1,4}%?)(\\1)"; Pattern pattern=Pattern.compile(re); Matcher matches=pattern.matcher(attributes); if(matches!= null) { while(matches.find()) { width = matches.group(2).trim().endsWith("%")? (Integer.parseInt(matches.group(2).substring(0,matches.group(2).length()- 1)) <= 98 ? matches.group(2) : "98%") : (Integer.parseInt(matches.group(2))<= 560 ? matches.group(2): "98%"); } } else { re = "width\\s?:\\s?(\\d{1,4})([px|%])"; Pattern pattern1=Pattern.compile(re); Matcher matches1=pattern1.matcher(attributes); while(matches1.find()){ if(matches1 != null) { width = matches1.group(2) == "%" ? (Integer.parseInt(matches1.group(1))<= 98 ? matches1.group(1) : "98%") : (Integer.parseInt(matches1.group(1)) <= 560 ? matches1.group(1) : "98%"); } } } String bgcolor =""; re ="(?:background|background-color|bgcolor)[:=]\\s*([\"\']?)((rgb\\(\\d{1,3}%?,\\s*\\d{1,3}%?,\\s*\\d{1,3}%?\\))|(#[0-9a-fA-F]{3,6})|([a-zA-Z]{1,20}))(\\1)"; Pattern pattern2=Pattern.compile(re); Matcher matches2=pattern2.matcher(attributes); while(matches2.find()){ if(matches2 != null) { bgcolor = matches2.group(2); width = (width!=null&&!width.trim().equals("")) ? width : "98%"; } } String temp=""; if(bgcolor!=null&&!bgcolor.trim().equals("")&&width!=null&&!width.trim().equals("")){ temp="[table=" + width + "," + bgcolor + "]" ; }else if(bgcolor!=null&&!bgcolor.trim().equals("")&&width==null) { temp="[table=" + bgcolor + "]" ; }else if(width!=null&&!width.trim().equals("")&&bgcolor==null) { temp="[table=" + width + "]" ; }else if(width==null&&bgcolor==null) { temp="[table]" ; } return temp; } public static String tdtag(String attributes) { int colspan = 1; int rowspan = 1; int width = 0; String re = "colspan=([\"\']?)(\\d{1,2})(\1)"; Pattern pattern=Pattern.compile(re); Matcher matches=pattern.matcher(attributes); while(matches.find()){ if(matches!= null) { colspan =Integer.parseInt( matches.group(2)); } } re ="rowspan=([\"\']?)(\\d{1,2})(\\1)"; Pattern pattern1=Pattern.compile(re); Matcher matches1=pattern1.matcher(attributes); while(matches1.find()){ if(matches1!= null) { rowspan = Integer.parseInt(matches1.group(2)); } } re = "width=([\"\']?)(\\d{1,4}%?)(\\1)"; Pattern pattern2=Pattern.compile(re); Matcher matches2=pattern2.matcher(attributes); while(matches2.find()){ if(matches2 != null) { width = Integer.parseInt(matches2.group(2)); } } return width==0 ? (colspan == 1 && rowspan == 1 ? "[td]" : "[td=" + colspan + "," + rowspan + "]") : "[td=" + colspan + "," + rowspan +"," + width + "]"; } public static String tableH2U(String html){ if(html==null&&html.trim().equals("")) return ""; Pattern pattern=Pattern.compile("<table([^>]*(width|background|background-color|bgcolor)[^>]*)>"); Matcher ma=pattern.matcher(html); String temp=""; while(ma.find()){ temp=tabletag(ma.group(1)).toString(); } html=html.replaceAll("<table([^>]*(width|background|background-color|bgcolor)[^>]*)>", temp); return html; } public static String tdH2U(String html){ if(html==null&&html.trim().equals("")) return ""; Pattern pattern=Pattern.compile("<t[dh]([^>]*(width|colspan|rowspan)[^>]*)>"); Matcher ma=pattern.matcher(html); String temp=""; while(ma.find()){ temp=tdtag(ma.group(2)).toString(); } html=html.replaceAll("<table([^>]*(width|background|background-color|bgcolor)[^>]*)>", temp); return html; } public static String imgtag(String attributes) { int width =0; int height =0; String src=""; String re = "src=([\"\']?)([\\s\\S]*?)(\\1)"; Pattern pattern=Pattern.compile(re); Matcher matches =pattern.matcher(attributes); while(matches.find()){ if(matches != null) { src= matches.group(2); } else { return ""; } } re = "width\\s?:\\s?(\\d{1,4})px"; Pattern pattern1=Pattern.compile(re); Matcher matches1 =pattern1.matcher(attributes); while(matches1.find()){ if(matches1 != null) { width = Integer.parseInt(matches1.group(1)); } } re = "height\\s?:\\s?(\\d{1,4})px"; Pattern pattern2=Pattern.compile(re); Matcher matches2 =pattern2.matcher(attributes); while(matches2.find()){ if(matches2 != null) { height = Integer.parseInt(matches2.group(1)); } } if(width==0 || height==0) { re = "width=([\"\']?)(\\d+)(\\1)"; Pattern pattern3=Pattern.compile(re); Matcher matches3 =pattern3.matcher(attributes); while(matches3.find()){ if(matches3 != null) { width = Integer.parseInt(matches3.group(2)); } } re = "height=([\"\']?)(\\d+)(\\1)"; Pattern pattern4=Pattern.compile(re); Matcher matches4 =pattern4.matcher(attributes); while(matches4.find()){ if(matches4 != null) { height = Integer.parseInt(matches4.group(2)); } } } re = "aid=([\"\']?)attach_(\\d+)(\\1)"; String imgtag = "img"; Pattern pattern5=Pattern.compile(re); Matcher matches5 =pattern5.matcher(attributes); while(matches5.find()){ if(matches5 != null) { imgtag = "localimg"; return "[attachimg]" + matches.group(2) + "[/attachimg]"; } } return width > 0 && height > 0 ?"[" + imgtag +"=" + width + "," + height + "]" + src + "[/" + imgtag + "]" :"[img]" + src + "[/img]"; } public static String imgH2U(String html){ if(html==null&&html.trim().equals("")) return ""; String re="<img([^>]*src[^>]*)>"; Pattern pattern=Pattern.compile(re); Matcher matches =pattern.matcher(html); String temp=""; while(matches.find()){ if(matches != null) { temp=imgtag(matches.group(1)); html=html.replaceFirst("<img([^>]*src[^>]*)>", temp); } } return html; } public static String objectH2U(String html){ if(html==null&&html.trim().equals("")) return ""; String re="<object([^>]*src[^>]*)>"; Pattern pattern=Pattern.compile(re); Matcher matches =pattern.matcher(html); String temp=""; while(matches.find()){ if(matches != null) { temp=imgtag(matches.group(1)); html=html.replaceAll("<object([^>]*src[^>]*)>", temp); } } return html; } public static String recursion(String tagname,String text,String dofunction,String extraargs) { if(extraargs == null) { extraargs = ""; } int beginsearchpos = 0; int tagbegin = 0; do { tagname = tagname.toLowerCase(); String open_tag = "<" + tagname; int open_tag_len = open_tag.length(); String close_tag = "<!---->"; int close_tag_len = close_tag.length(); String textlower = text.trim().toLowerCase(); tagbegin = textlower.indexOf(open_tag, beginsearchpos); int strlen = text.length(); if(tagbegin == -1) { break; } int inquote =0; boolean found = false; int tagnameend = 0; int optionend = 0; char t_char =' '; for(optionend = tagbegin; optionend < strlen; optionend++) { t_char=text.charAt(optionend) ; inquote+=1; if(t_char == '>') { found=true; break; } } if(found==false) { break; } int offset = inquote + (tagbegin); //System.out.println("tagoptions"+"offset------"+offset+"start----"+(tagbegin * 1+1)+"end---"+(tagnameend - tagbegin)); String tagoptions = text.substring(tagbegin + open_tag_len, offset); String acttagname = textlower.trim().substring(tagbegin * 1 + 1, tagbegin*1+open_tag_len); //System.out.println("acttagname-----"+acttagname+"tagname-----"+tagname); if(!acttagname.trim().equals(tagname)) { beginsearchpos = inquote + (tagbegin);; continue; } int tagend = textlower.indexOf(close_tag, optionend); if(tagend == -1) { break; } int localbegin = tagbegin + inquote; //System.out.println("tagend"+tagend+"localbegin"+localbegin+"tagend - localbegin"+(tagend - localbegin)); String localtext =""; if(dofunction.trim().equals("simpletag")){ localtext=simpletag(tagoptions, text.substring(localbegin, tagend), tagname, extraargs); }else if(dofunction.trim().equals("atag")){ localtext=atag(tagoptions, text.substring(localbegin, tagend)); //System.out.println("localtext----"+localtext); }else if(dofunction.trim().equals("fonttag")){ localtext=fonttag(tagoptions, text.substring(localbegin, tagend)); } text = text.substring(0, tagbegin) + localtext + text.substring(tagend+close_tag_len); //System.out.println("text----"+text); beginsearchpos = tagbegin + localtext.length()-1; } while(tagbegin!=-1); return text; } public static String simpletag(String options,String text,String tagname,String parseto) { if(text.trim() =="") { return ""; } text = recursion(tagname, text, "simpletag", parseto); return "[" + parseto + "]" + text + "[/" + parseto + "]"; } public static String getoptionvalue(String option,String text) { String re = option + "(\\s+?)?\\=(\\s+?)?[\"\']?(.+?)([\"\']|$|>)"; Pattern pattern=Pattern.compile(re); Matcher matches =pattern.matcher(text); String temp=""; while(matches.find()){ if(matches!= null) { temp= matches.group(3); } } if(temp.trim().equals("")){ return ""; }else{ return temp; } } public static String atag(String aoptions,String text) { if(text.trim() =="") { return ""; } String href = getoptionvalue("href", aoptions); String tag=""; if(href.length()>11&&href.substring(0, 11) == "javascript:") { return recursion("a", text, "atag","").trim(); } else if(href.length()>7&&href.substring(0, 7) == "mailto:") { tag = "email"; href = href.substring(7); } else { tag = "url"; } return "[" + tag + "=" + href + "]" + recursion("a", text, "atag","").trim()+ "[/" + tag + "]"; } public static String fonttag(String fontoptions,String text) { String prepend = ""; String append = ""; String[] tags = new String[] {"font"+" :"+ "face=", "size" +":"+ "size=", "color" +":"+ "color="}; for(int i=0;i<tags.length;i++) {="" string="" optionvalue="fetchoptionvalue(tags[i]," fontoptions);="" system.out.println("optionvalue---"+optionvalue);="" if(optionvalue!="null&amp;&amp;!optionvalue.trim().equals(&quot;&quot;))" {="" prepend="" +="[" +="" tags[i]="" +="" "=" + optionvalue + " ]";="" append="[/" +="" tags[i]="" +="" "]"="" +="" append;="" }="" }="" hashtable="" pend="parsestyle(fontoptions," prepend,="" append);="" system.out.println("prepend---"+pend.get("prepend")+"--append--"+pend.get("append"));="" return="" pend.get("prepend")="" +="" recursion("font",="" text,="" "fonttag","")="" +="" pend.get("append");="" }="" 查询字符串位置="" public="" static="" int="" strpos(string="" haystack,string="" needle,int="" offset)="" {="" int="" index="haystack.toLowerCase().indexOf(needle.toLowerCase()," offset);="" return="" index="=" -1="" ?="" -1="" :index;="" }="" public="" static="" string="" fetchoptionvalue(string="" option,string="" text)="" {="" int="" position="0;" if((="" position="strpos(text," option,0))=""> -1) { int delimiter = position + option.length(); char delimchar=' '; if(text.charAt(delimiter) == '\"') { delimchar = '\"'; } else if(text.charAt(delimiter) == '\'') { delimchar = '\''; } else { delimchar = ' '; } int delimloc = strpos(text, delimchar+"", delimiter + 1); if(delimloc ==-1) { delimloc = text.length(); } else if(delimchar == '\"' || delimchar == '\'') { delimiter++; } return text.substring(delimiter, delimloc - delimiter).trim(); } else { return ""; } } public static Hashtable parsestyle(String tagoptions,String prepend,String append) { Hashtable hash=new Hashtable(); String[][] searchlist = { {"align", "true", "text-align:\\s*(left|center|right);?"," 1"}, {"float", "true", "float:\\s*(left|right);?", "1"}, {"color","true", "^(?:\\s|)color:\\s*([^;]+);?", "1"}, {"font", "true", "font-family:\\s*([^;]+);?", "1"}, {"size","true", "font-size:\\s*(\\d+(\\.\\d+)?(px|pt|in|cm|mm|pc|em|ex|%|));?", "1"}, {"b", "false", "font-weight:\\s*(bold);?"}, {"i", "false", "font-style:\\s*(italic);?"}, {"u", "false", "text-decoration:\\s*(underline);?"} }; String style = getoptionvalue("style", tagoptions); String re = "^(?:\\s|)color:\\s*rgb\\((\\d+),\\s*(\\d+),\\s*(\\d+)\\)(;?)"; Pattern pattern=Pattern.compile(re); Matcher ma=pattern.matcher(style); while(ma.find()){ style="color:#"+Integer.parseInt(ma.group(2))+ma.group(3)+ma.group(4)+ma.group(5); } //style = style.replace(re, function($1, $2, $3, $4, $5) {return(+ parseInt($2).toString(16) + parseInt($3).toString(16) + parseInt($4).toString(16) + $5);}); int len = searchlist.length; for(int i = 0; i < len; i++) { Pattern pattern1=Pattern.compile(searchlist[i][2]); Matcher matcher=pattern1.matcher(style); while(matcher.find()) { if(matcher != null) { String opnvalue = matcher.group(Integer.parseInt(searchlist[i][3])); prepend += "[" + searchlist[i][0] + (searchlist[i][1] == "true" ? "=" + opnvalue + "]" : "]"); append = "[/" + searchlist[i][0] + "]" + append; } } } hash.put("prepend", prepend); hash.put("append", append); return hash; } public static String escapeHTML2UBB(String html) { //去标签去特殊符号 html=html.trim().toLowerCase(); String[] preg=new String[]{"<style.*?>[\\s\\S]*?<\\/style>","<noscript.*?>[\\s\\S]*?<\\/script>","<noscript.*?>[\\s\\S]*?<\\/noscript>","<select.*?>[\\s\\S]*?<\\/select>", "<!---->", "on[a-zA-Z]{3,16}\\s?=\\s?\"[\\s\\S]*?\""}; for(int i=0,size=preg.length;i<size;i++){ html="html.replaceAll(preg[i]," "");="" }="" html="html.replaceAll(&quot;(\\r\\n|\\n|\\r)&quot;," "");="" html="html.replaceAll(&quot;&amp;((#(32|127|160|173))|shy|nbsp);&quot;,&quot;&quot;).trim();" 转表格="" html="tableH2U(html);" html="html.replaceAll(&quot;" <table[^="">]*>","[table]"); html=html.replaceAll("<tr[^>]*(?:background|background-color|bgcolor|class)[:=]\\s*([\"\']?)([\\(\\)%,#\\w]+)(\\1)[^>]*>", "[tr=$3]"); html=html.replaceAll("<tr[^>]*>", "[tr]"); html=tdH2U(html); String[] tablepreg=new String[]{"<t[dh][^>]*>","<\\/t[dh]>","<\\/tr>","<\\/table>"}; String[] tableReplace=new String[]{"[td]", "[/td]", "[/tr]", "[/table]"}; for(int j=0,max=tablepreg.length;j<max;j++) {="" html="html.replaceAll(tablepreg[j]," tablereplace[j]);="" }="" 转字体大小="" html="html.replaceAll(&quot;&lt;h([0-9]+)[^">]*>(.*)<\\/h\\1>", "[size=$1]$2[/size]\\n\\n"); html = imgH2U(html); html = objectH2U(html); html = html.replaceAll("<a\\s+?name=([\"\']?)(.+?)(\\1)[\\s\\s]*?>([\\s\\S]*?)<\\/a>", "$4"); html = html.replaceAll("<br\\s+?style=([\"\']?)clear: both;?(\\1)[^\\="">]*>", ""); html = html.replaceAll("<br[^\\>]*>", "\n"); //遍历内容,替换标签。 html = recursion("b", html, "simpletag", "b"); html = recursion("htmlong", html, "simpletag", "b"); html = recursion("i", html, "simpletag", "i"); html = recursion("em", html, "simpletag", "i"); html = recursion("u", html, "simpletag", "u"); html = recursion("a", html, "atag",""); html = recursion("font", html, "fonttag",""); html = recursion("blockquote", html, "simpletag", "indent"); // FLASH动画html html = html.replaceAll("([^']*)","[flash]$1[/flash]"); html = html.replaceAll("([^']*)","[flash=$1,$2]$3[/flash]"); for (int i = 0; i < regexHTML.length; i++) html = html.replaceAll(regexHTML[i], replacementHTML[i]); html = html.replaceAll("<[\\/\\!]*?[^<>]*?>", ""); //System.out.println("html----"+html); return html; } // html代码转化 把一些html里的标签转化为特殊字符 public static String escapeHTMLTags(String input) { // Check if the string is null or zero length -- if so, return // what was sent in. if (input == null || input.length() == 0) { // return input; input = ""; } // Use a StringBuffer in lieu of String concatenation -- it is // much more efficient this way. StringBuilder buf = new StringBuilder(input.length()); char ch = ' '; for (int i = 0; i < input.length(); i++) { ch = input.charAt(i); if (ch == '<') { buf.append("<"); } else if (ch == '>') { buf.append(">"); } else if (ch == '"') { buf.append("'"); } else { buf.append(ch); } } return buf.toString(); } // 路径转换 public static String escapePath(String input) { if (input != null) { return input.replace("\\", "/"); } else { return ""; } } // 编码转换 public static String StrEnCoding(String str) { try { return new String(str.getBytes("ISO8859_1"), "gbk"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); return ""; } } // 截取字符串 public static String StrIntercept(String str, int length) { if (str.length() < length) { return str; } else { return str.substring(0, length) + "..."; } } // 判断字符串是可以转换为长整形 public static boolean isLong(String value) { try { Long.parseLong(value); return true; } catch (NumberFormatException e) { return false; } } // 发帖的时候除去某些标签 textarea script iframe public static String removeTag(String context) { if (context != null) { String content = context; content = content.trim().toLowerCase(); if (content.indexOf("onload=autoresize(this)") > -1) { content = content .replaceAll("onload=autoresize\\(this\\)", "xxxxx"); } String[] preg=new String[]{"<style.*?>[\\s\\S]*?<\\/style>","<noscript.*?>[\\s\\S]*?<\\/script>","<noscript.*?>[\\s\\S]*?<\\/noscript>","<select.*?>[\\s\\S]*?<\\/select>", "<!---->", "on[a-zA-Z]{3,16}\\s?=\\s?\"[\\s\\S]*?\""}; for(int i=0,size=preg.length;i<size;i++){ context="context.replaceAll(preg[i]," "");="" }="" if="" (content.indexof("onload")=""> -1) { content = content.replaceAll("onload", ""); } if (content.indexOf("xxxxx") > -1) { content = content.replaceAll("xxxxx", "onload=autoresize(this)"); } if (content.indexOf("meta") > -1) { content = content.replaceAll("<meta.*?>", ""); } if (content.indexOf("a") > -1) { content = content.replaceAll("", ""); content = content.replaceAll("", ""); } if (content.indexOf("script") > -1) { content = content.replaceAll("<noscript.*?>", ""); content = content.replaceAll("</noscript>", ""); } if (content.indexOf("window") > -1) { content = content.replaceAll("window", ""); } if (content.indexOf("body") > -1) { content = content.replaceAll("<body.*?>", ""); } return content; } else { return ""; } } // 去除所有html标签 public static String removeAllTag(String content) { if (content.trim().equals("") && content == null) { return ""; } else { String str = content.replaceAll("\\&[a-zA-Z]{1,10};", "") .replaceAll("<[^>]*>", ""); str = str.replaceAll("[(/>)<]", ""); return str; } } // 获取超链接的地址 public static String getHtmlA(String content) { String str = ""; if (content == null && content.trim().length() < 0) { return ""; } Pattern pattern = Pattern.compile("href=\"(.+?)\"", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(content); if (matcher.find()) { str = matcher.group(1); } return str; } // 截取社区签名的url public static String getHtmlUrl(String content) { String str = ""; if (content == null && content.trim().length() < 0) { return ""; } Pattern pattern = Pattern .compile("(http://|https://){1}[\\w\\.\\-/:]+", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(content); StringBuffer buffer = new StringBuffer(); while (matcher.find()) { buffer.append(matcher.group()); // buffer.append(","); str = buffer.toString(); } return str; } // 去除社区特定签名的标签 public static String removeURL(String content) { String str = ""; if (content == null && content.trim().length() < 0) { return ""; } Pattern pattern = Pattern.compile("\\[url.*\\](.+?)\\[/url\\]"); Matcher matcher = pattern.matcher(content); while (matcher.find()) { str = matcher.group(1); } return str; } public static void main(String[] args) { // String ubb="[img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img]"; // System.out.println("ubb-----"+ubb); // ubb= escapeUBB2HTML(ubb); // System.out.println("html-----"+ubb); String ubb=""; String html="

 引用 zuzu08 的帖子

 

 引用 熊俊 的帖子

  好看不知道好吃吗?



看起来可以,不知道怎么样


好吃就更好了
"; System.out.println("-html----"+html); System.out.println("--------------------------"); ubb=escapeHTML2UBB(html); System.out.println("ubb-----"+ubb); System.out.println("--------------------------"); ubb=escapeUBB2HTML(ubb); System.out.println("html-----"+ubb); } } 请高人指点。文中table遍历不够完美。。。。
<
0
0
分享到:
评论

相关推荐

    实现ubb代码的java类

    Java类的实现意味着这个程序会有一个解析器,它可以识别这些标记并转换为对应的HTML或其他富文本格式,以便在网页或应用程序中正确显示。 这个Java类的设计考虑到了扩展性,这意味着它不仅包含基本的UBB代码解析...

    JSP论坛源码,支持MD5加密和UBB

    本论坛源码是基于Java技术栈实现的,采用JSP(JavaServer Pages)作为视图层,结合Servlet和JavaBean进行业务逻辑处理,提供了丰富的功能,同时也支持MD5加密和UBB代码解析,非常适合开发者进行二次开发或学习。...

    JSP留言本

    同时,正则表达式也可能用于解析ubb代码,这是一种简化HTML的标记语言,使得用户可以在留言中插入格式化的文本,如加粗、斜体等。 **UBB代码和表情** UBB(UltraBB Code)是一种轻量级的标记语言,类似于BBCode,...

    G.A.N.G 留言本_ganggbook(毕设 + 课设).zip

    1. **UbbCode.class**:这是Java类文件,很可能用于解析UBB(UltraBB Code)代码。UBB是一种类似于HTML的标记语言,常用于论坛和留言本,让用户可以插入格式化文本,如加粗、斜体、链接等。 2. **FaceFilter.class*...

    CNSXML留言本

    2. **UBB解析**:接收用户输入的UBB代码,将其转换为相应的HTML显示形式。 3. **验证与过滤**:对用户输入进行安全检查,防止SQL注入或其他恶意行为,同时可能还包括对内容长度、格式的限制。 4. **显示与交互**:将...

    淘特JSP留言板 v0.1_tot-jsp-gb(毕设 + 课设).zip

    5. **XML配置**:`DOM4JConfiguration.class`可能与DOM4J库相关,这是一个强大的Java XML API,用于解析、创建和修改XML文档。在项目中,可能用来读取或写入配置文件,比如数据库连接信息。 6. **字符串处理**:`...

    JSP+JavaBean留言本_guestbook(毕设 + 课设).zip

    3. **FaceFilter.class**:可能是一个过滤器,用于解析UBB代码并将其转换为对应的HTML,以便在网页上正确显示表情。 4. **HTMLFilter.class**:可能用于过滤用户输入的HTML标签,防止XSS(跨站脚本攻击)。 5. **...

    ASP网上贴吧系统(源代码+LW).zip

    4. `ubb.asp` 和 `ubb1.asp`:UBB(Unified Bulletin Board Code)是一种论坛常用的文本格式,用于将文本格式化为HTML,这些文件可能实现了UBB代码的解析和转换功能。 5. `community.asp`:社区首页或者论坛主页面,...

    jsp+javabean留言簿

    2. **JUBB (类似UBB)**: UBB(Universal Bulletin Board)是一种简化版的HTML,常用于论坛和留言系统,允许用户输入格式化的文本。JUBB是其变体,可能提供了类似的标记语言,使用户能够添加粗体、斜体、链接等基本...

    诚信网络商店系统个人用户 正式版

    UBB代码的解析通常需要后端或前端的UBB解析库,将用户输入的UBB代码转换成HTML显示。这样可以确保用户在不熟悉HTML的情况下也能美化自己的帖子,同时也避免了直接输入HTML可能带来的安全风险。 5. **后台更稳定**:...

    TopBook v1.0

    6. **UBB代码解析**:UBB代码的解析需要后台程序来处理,将用户输入的UBB代码转化为对应的HTML元素,展示在页面上。这涉及字符串处理和正则表达式知识。 7. **权限管理**:管理员可能拥有更高的权限,可以审核、...

    ASP电子论坛系统(源代码+LW).zip

    5. `ubb.asp`:UBB(Uniform Bar Code)是一种文本编码方式,常用于论坛的富文本编辑器中,将用户输入的特殊标记转换为HTML,使得用户可以使用简单的语法来格式化文本。 6. `search_post.asp`:帖子搜索功能,允许...

    梦想年华JSP留言本_guestbook(毕设 + 课设).zip

    在这个项目中,我们可以看到多个关键组件,这些组件共同构成了一个完整的留言系统,下面将详细解析其中涉及的技术和知识点。 1. **JSP(Java Server Pages)技术**: JSP 是一种动态网页开发技术,它允许开发者在...

    涂鸦板1.00

    2. **ubbcode.asp**:UBB(Unified Bulletin Board Code)是一种类似于HTML的标记语言,常用于论坛和留言板,用于格式化文本。这个文件可能用于将用户输入的UBB代码转换为可视化的涂鸦内容。 3. **getubb.asp**:此...

    xheditor-1.1.14

    说明:若使用了ubb插件,请设置此属性为true 备注:1.0.0 beta2新添加 emots:添加自定义表情 参数:可定义多个JSON对象数组,示例如下:{qq:{name:'QQ',count:55,width:25,height:25,line:11},msn:{name:'MSN',...

    等待死亡日记本

    日记编辑器部分则可能集成WYSIWYG(所见即所得)编辑器,如TinyMCE或CKEditor,它们支持UBB代码的解析和显示。后端开发可能基于Node.js、Python的Django或Flask,或者Java的Spring Boot等技术栈,构建服务器端逻辑,...

    tuxedo

    Jolt 是 Tuxedo 中用于处理 Java 对象传输的语言。配置 Jolt 通常涉及在 UBB 文件中定义相关的服务和参数。 #### 三、程序开发 **3.1 Tuxedo CLIENT 连接不到 server 端的网络问题?** 当 Tuxedo 客户端无法连接...

    MyNewCC98:实验性

    signal-&gt;process-&gt;presentation 主要视图部分: 板... 另一个选择:到目前为止,UBB代码似乎不容易在textview中解析,但是markdown代码似乎可以使用 。用户资料直接加载到webview中;PMView /回复与主题视图类似,它

Magicbox
Global site tag (gtag.js) - Google Analytics