EncodeString.java
上传用户:u_thks
上传日期:2022-07-31
资源大小:1910k
文件大小:20k
- /*
- * Made in GamVan
- */
- package com.gamvan.tools;
- import java.io.UnsupportedEncodingException;
- import java.security.MessageDigest;
- import sun.misc.BASE64Encoder;
- import java.util.regex.*;
- import com.gamvan.tools.MD5;
- /**
- * 字符串操作集合类
- * @author GamVan by 我容易么我
- * Powered by GamVan.com
- */
- public class EncodeString{
- private boolean ubbUrl = false;
- private boolean ubbImg = false;
- /**
- * 考虑到中文的字符串长度判断
- * @param str
- * @return
- */
- public static int Glength(String str){
- int i = 0;
- String s = "";
- try {
- s = new String(str.getBytes(),"UTF-8");
- i = s.length();
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- return i;
- }
-
-
- /**
- * 中英文混和字符串按特定长度截取
- * @param res 中英文混和字符。
- * @param start 截取起始位置。
- * @param length 截取长度。
- * @return res中,从start起length长度的子字符串。
- */
- public static String Gsubstring(String res, int start, int length ,String charSet) {
- if(start<1){
- start=1;
- }
- int i_Start,i_Len ,i,j,ilenByte,i_S,i_L;
- i_Start= 0;
- i_S =0;
- i_Len=0;
- i_L =0;
- try {
- byte[] resBytes = res.getBytes(charSet);
- ilenByte =resBytes.length ;
-
- for ( i = 0; i < ilenByte; i++) {
- i_Start = i_Start+1;
- if (i_Start <=start) {i_S = i_S+1;}
-
- if (i_Start >=start){
- i_Len=i_Len+1;
- i_L = i_L +1;
- }
-
- if (resBytes[i] <0 ) {
- i = i+2;
- i_Start = i_Start +1;
- if (i_Start <start) {i_S = i_S+2;}
- if (i_Start >=start){
- i_Len=i_Len+1;
- i_L=i_L+2;
- }
- }
-
- if (i_Len >=length) i =ilenByte;
- }
-
- byte[] dest = new byte[i_L];
-
- for ( j = 0; j < i_L;j++) {
- dest[j] = resBytes[i_S + j-1];
- }
-
- return new String(dest, charSet);
-
- } catch (Exception e) {
- e.printStackTrace();
- return "";
- }
-
- }
- public static String encodeString(String codeType, String txt){
- if(codeType.equals("md5of16")){
- MD5 m = new MD5();
- return m.getMD5ofStr16(txt);
- }else if(codeType.equals("md5of32")){
- MD5 m = new MD5();
- return m.getMD5ofStr(txt);
- }else{
- try {
- MessageDigest gv = MessageDigest.getInstance(codeType);
- gv.update(txt.getBytes());
- return new BASE64Encoder().encode(gv.digest());
- }
- catch(java.security.NoSuchAlgorithmException e) {
- e.printStackTrace();//打印错误信息;
- return null;
- }
- }
- }
-
-
-
- //codeType 加密方式, txt加密字符串
- private String replaces(String src, String fnd, String rep){
- if(src==null || src.equals("")){
- return "";
- }
- if (src == null || src.equals("")){
- return "";
- }
- String dst = src;
- int idx = dst.indexOf(fnd);
- while (idx >= 0){
- dst = dst.substring(0, idx) + rep + dst.substring(idx + fnd.length(), dst.length());
- idx = dst.indexOf(fnd, idx + rep.length());
- }
- return dst;
- }
-
-
-
- public static String codeColor(String str){
- if(str==null) return "";
- str = str.replaceAll(" ","");
- str = ubbPattern(str, ""(.*?)"","<span style=color:#2A00FF;>"$1"</span>");
- str = ubbPattern(str, "\/\*(.*?)\*/","<span style=color:#3F7F5F;>/*$1*/</span>");
-
- //str = ubbPattern(str, "http://(.*?)<br/>","<span style=color:#3F7F5F;>http://$1</span><br/>");
- str = ubbPattern(str, "\/\/(.*?)\<br/>","<span style=color:#3F7F5F;>//$1</span><br/>");
-
- //str = ubbPattern(str, "<(.*?)>","<span style=color:#006600;><$1></span>");
- //str = ubbPattern(str, "<(.*?)/>","<span style=color:#006600;><$1/></span>");
- str = ubbPattern(str,"private ","<span style="color:#7F0055; font-weight: bold;">private </span>");
- str = ubbPattern(str,"protected ","<span style="color:#7F0055; font-weight: bold;">protected </span>");
- str = ubbPattern(str,"public ","<span style="color:#7F0055; font-weight: bold;">public </span>");
- str = ubbPattern(str,"static ","<span style="color:#7F0055; font-weight: bold;">static </span>");
- str = ubbPattern(str,"return ","<span style="color:#7F0055; font-weight: bold;">return </span>");
- str = ubbPattern(str,"try","<span style="color:#7F0055; font-weight: bold;">try</span>");
- str = ubbPattern(str,"catch","<span style="color:#7F0055; font-weight: bold;">catch</span>");
- str = ubbPattern(str,"finally","<span style="color:#7F0055; font-weight: bold;">finally</span>");
- str = ubbPattern(str,"package","<span style="color:#7F0055; font-weight: bold;">package</span>");
-
-
- str = ubbPattern(str,"null","<span style="color:#7F0055; font-weight: bold;">null</span>");
- str = ubbPattern(str,"short ","<span style="color:#7F0055; font-weight: bold;">short </span>");
- str = ubbPattern(str,"int ","<span style="color:#7F0055; font-weight: bold;">int </span>");
- str = ubbPattern(str,"double ","<span style="color:#7F0055; font-weight: bold;">double </span>");
- str = ubbPattern(str,"byte ","<span style="color:#7F0055; font-weight: bold;">byte </span>");
- str = ubbPattern(str,"long ","<span style="color:#7F0055; font-weight: bold;">long </span>");
- str = ubbPattern(str,"final ","<span style="color:#7F0055; font-weight: bold;">final </span>");
- str = ubbPattern(str,"new ","<span style="color:#7F0055; font-weight: bold;">new </span>");
- str = ubbPattern(str,"false","<span style="color:#7F0055; font-weight: bold;">false</span>");
- str = ubbPattern(str,"true","<span style="color:#7F0055; font-weight: bold;">true</span>");
-
-
-
- str = ubbPattern(str,"while\(","<span style="color:#7F0055; font-weight: bold;">while</span>(");
- str = ubbPattern(str,"for\(","<span style="color:#7F0055; font-weight: bold;">for</span>(");
- str = ubbPattern(str,"switch","<span style="color:#7F0055; font-weight: bold;">switch</span>");
- str = ubbPattern(str,"if\(","<span style="color:#7F0055; font-weight: bold;">if</span>(");
- str = ubbPattern(str,"else","<span style="color:#7F0055; font-weight: bold;">else</span>");
- str = ubbPattern(str,"this\.","<span style="color:#7F0055; font-weight: bold;">this</span>.");
- str = ubbPattern(str,"function\(","<span style="color:#7F0055; font-weight: bold;">function</span>(");
- str = ubbPattern(str,"document.","<span style="color:#7F0055; font-weight: bold;">document</span>.");
-
-
- return str;
- }
-
-
- public static void main(String[] str){
- P.rintl(EncodeString.htmlRun("今晚23[code]public[/code]"));
- }
-
-
-
- /**
- * 标记可运行代码
- * @param src
- * @return
- */
- private static String htmlRun(String src){
- if(src==null) return "";
- String str = "";
- //str = matcherStr(src, "\[html\](.*?)\[/html]", "GVCODE903");
- //str = unHtmlEncoder(src);
- str = matcherCodeColor(src, "\[code\](.*?)\[/code]");
- str = ubbPattern(str, "\[code\](.*?)\[/code]","<table align=center bgcolor="#BAD5EF" width="98%" border=0 cellpadding=4 cellspacing=1>"
- +"<form><tr><td bgcolor="#E6EEF7" height="25" style="font-size: 12px">程序代码:</td></tr><tr><td bgcolor="#ffffff" style="font-size: 12px">"
- //"<textarea cols="60" rows="10" id="gv123">"
- + ("$1")
- +"</td></tr></form></table>");
- return str;
- }
-
-
-
- //运行html代码
- //用于HTML字符串反向替换
- public static String unHtmlEncoder(String src) {
- if (src == null || src.equals("")){
- return "";
- }
- String unHtmlEncode = new String(src);
- unHtmlEncode = unHtmlEncode.replaceAll("<","<");
- unHtmlEncode = unHtmlEncode.replaceAll(">",">");
- unHtmlEncode = unHtmlEncode.replaceAll(""",""");
- unHtmlEncode = unHtmlEncode.replaceAll("'","'");
- unHtmlEncode = unHtmlEncode.replaceAll(" "," ");
- unHtmlEncode = unHtmlEncode.replaceAll("<br/>","rn");
- unHtmlEncode = unHtmlEncode.replaceAll("<br/>","r");
- unHtmlEncode = unHtmlEncode.replaceAll("<br/>","n");
- return unHtmlEncode;
- }
- //用于HTML字符串替换
- public static String htmlEncoder(String src) {
- if (src == null || src.equals("")){
- return "";
- }
- String htmlencode = new String(src);
- htmlencode = htmlencode.replaceAll("<","<");
- htmlencode = htmlencode.replaceAll(">",">");
- htmlencode = htmlencode.replaceAll(""",""");
- htmlencode = htmlencode.replaceAll("'","'");
- htmlencode = htmlencode.replaceAll(" "," ");
- htmlencode = htmlencode.replaceAll("rn","<br/>");
- htmlencode = htmlencode.replaceAll("r","<br/>");
- htmlencode = htmlencode.replaceAll("n","<br/>");
- return htmlencode;
- }
- /**
- * 转换为XML编码.<br>
- */
- public String xmlEncoder(String src){
- if (src == null || src.equals("")){
- return "";
- }
- String xmlencode = src;
- xmlencode = replaces(xmlencode, "&", "&");
- xmlencode = replaces(xmlencode, "<", "<");
- xmlencode = replaces(xmlencode, ">", ">");
- xmlencode = replaces(xmlencode, """, """);
- xmlencode = replaces(xmlencode, "'", "´");
- return xmlencode;
- }
-
-
- public String ubbEncoder(String str){
- if(str==null || str.equals("")){
- return "";
- }
- str = ubbPattern(str,"\[center\](.*?)\[/center\]","<center>$1</center>");
- str = ubbPattern(str,"\[b\](.*?)\[/b\]","<strong>$1</strong>");
- str = ubbPattern(str,"\[i\](.*?)\[/i\]","<emg>$1</em>");
- str = ubbPattern(str, "\[color=(.*?)\](.*?)\[/color\]","<span style=color:$1>$2</span>");
- str = ubbPattern(str, "\[GamVanFace_(\d*)\]","<img src="GVimgs/GamVanFace/$1.gif">");
- str = ubbPattern(str, "\[img\](.*?)\[/img\]","<a href="$1" target="_blank"><img " +
- " onload="javascript:if(this.width > screen.width-350){this.width = screen.width-350};" " +
- " onerror="javascript:this.src='GVimgs/imgErr.gif'" src="$1" border="0" /></a>");
- str = ubbPattern(str, "\[url\](.*?)\[/url\]","<a href="$1" target="_blank">$1</a>");
- str = ubbPattern(str, "\[url=(.*?)\](.*?)\[/url\]","<a href="$1" target="_blank">$2</a>");
- str = ubbPattern(str, "\[size=*([\d]*)\](.*?)\[/size\]","<span style="font-size: $1px;">$2</span>");
- str = ubbPattern(str,"\[quote\](.*?)\[/quote\]","<table align=center bgcolor="#BAD5EF" width="98%" border=0 cellpadding=4 cellspacing=1>" +
- "<tr><td bgcolor="#E6EEF7" height="25" style="font-size: 12px"><strong>以下内容为引用内容</strong>:</td></tr><tr><td bgcolor="#ffffff" style="font-size: 12px">$1</td></tr></table>");
-
- str = ubbPattern(str,"\[quote=(.*?)\](.*?)\[/quote\]","<table align=center bgcolor="#BAD5EF" width="98%" border=0 cellpadding=4 cellspacing=1>" +
- "<tr><td bgcolor="#E6EEF7" height="25" style="font-size: 12px"><strong>$1 说:</strong></td></tr><tr><td bgcolor="#ffffff" style="font-size: 12px">$2</td></tr></table>");
-
-
-
- StringBuffer sb = new StringBuffer("");
- sb.append("<object codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" );
- sb.append(" height=400 width=500 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000>");
- sb.append("<param name="movie" value="$1">");
- sb.append("<param name="menu" value="false">");
- sb.append("<embed src="$1" menu=false quality=high ");
- sb.append(" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"" );
- sb.append(" type="application/x-shockwave-flash" width="500" height="400"></embed></object>");
- //UBB多媒体部分开始
- str = ubbPattern(str, "\[flash\](.*?)\[/flash\]","<a href="$1" TARGET=_blank>[全屏欣赏]</a><br>" + sb.toString());
-
- sb.delete(0,sb.length());
-
- sb.append("<object codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" );
- sb.append(" height=$2 width=$1 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000>");
- sb.append("<param name="movie" value="$3">");
- sb.append("<param name="menu" value="false">");
- sb.append("<embed src="$3" menu=false quality=high ");
- sb.append(" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"" );
- sb.append(" type="application/x-shockwave-flash" width="$1" height="$2"></embed></object>");
-
- str = ubbPattern(str, "\[flash=*([\d]*),*([\d]*)\](.*?)\[/flash\]","<a href="$3" TARGET=_blank>[全屏欣赏]</a><br>" + sb.toString());
- sb.delete(0,sb.length());
-
- str = ubbPattern(str, "\[real\](.*?)\[/real\]","<OBJECT classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX width="450" height="350"><PARAM NAME=SRC VALUE=$1><PARAM NAME=CONSOLE VALUE=Clip1><PARAM NAME=CONTROLS VALUE=imagewindow><PARAM NAME=AUTOSTART VALUE=true></OBJECT><br><OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=35 id=video2 width=450><PARAM NAME=SRC VALUE=$1><PARAM NAME=AUTOSTART VALUE=-1><PARAM NAME=CONTROLS VALUE=controlpanel><PARAM NAME=CONSOLE VALUE=Clip1></OBJECT>");
- str = ubbPattern(str, "\[real=*([\d]*),*([\d]*)\](.*?)\[/real\]","<OBJECT classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX width="$1" height="$2"><PARAM NAME=SRC VALUE=$3><PARAM NAME=CONSOLE VALUE=Clip1><PARAM NAME=CONTROLS VALUE=imagewindow><PARAM NAME=AUTOSTART VALUE=true></OBJECT><br><OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=35 id=video2 width=450><PARAM NAME=SRC VALUE=$3><PARAM NAME=AUTOSTART VALUE=-1><PARAM NAME=CONTROLS VALUE=controlpanel><PARAM NAME=CONSOLE VALUE=Clip1></OBJECT>");
- str = ubbPattern(str, "\[video\](.*?)\[/video\]","<object align=middle classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 class=OBJECT id=MediaPlayer width="450" height="300"><param name="loop" value="true"><param name=ShowStatusBar value=-1><param name=Filename value="$1"><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename="mp" src="$1" width="450" height="300"></embed></object>");
- str = ubbPattern(str, "\[video=*([\d]*),*([\d]*)\](.*?)\[/video\]","<object align=middle classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 class=OBJECT id=MediaPlayer width="$1" height="$2"><param name="loop" value="true"><param name=ShowStatusBar value=-1><param name=Filename value="$3"><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename="mp" src="$3" width="$1" height="$2"></embed></object>");
- // UBB多媒体部分结束
-
- if(ubbImg){
- str = ubbPattern(str,"((http|https|ftp|rtsp|mms):(//|\\)([\w\.\:/\?\=%&_-]+(gif|jpg|jpeg|bmp|png)))",
- "<a href="$1" target="_blank"><img onload="javascript:if(this.width > screen.width-350){this.width = screen.width-350};" " +
- " onerror="javascript:this.src='GVimgs/imgErr.gif'" src="$1" border="0"></a>");
- }else{
- if(ubbUrl){
- str = ubbPattern(str,"((http|https|ftp|rtsp|mms):(//|\\)([\w\.\:/\?\=%&_-]+))",
- "<a target="_blank" href="$1">$1</a>");
- //str = ubbPattern(str,"^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$" ,
- //"<a target="_blank" href="$1">$1</a>");
- }
- }
-
- str = htmlRun(str);
- return str;
- }
-
-
-
-
- /**
- * 代码内容的色彩替换
- * @param str
- * @param cp
- * @param s
- * @return
- */
- public static String matcherCodeColor(String str, String cp){
- if(str==null || str.equals("")){
- return "";
- }
- String txt = new String();
- if(str!=null && !str.equals("")){
- txt = str;
- Pattern p = Pattern.compile(cp,2); //参数2表示大小写不区分
- Matcher m = p.matcher(txt);
- StringBuffer sb = new StringBuffer();
- boolean result = m.find();
- String temp = "";
- //使用循环将句子里所有匹配的内容找出并替换再将内容加到sb里
- while(result) {
- temp = m.group();
- temp = codeColor(temp);
- m.appendReplacement(sb,temp);
- //继续查找下一个匹配对象
- result = m.find();
- }
- //最后调用appendTail()方法将最后一次匹配后的剩余字符串加到sb里;
- m.appendTail(sb);
- txt = String.valueOf(sb);
- }else{
- txt = "";
- }
- return txt;
- }
-
-
-
-
-
-
- /**
- * 正则匹配并完成替换过程
- * @param str 要检索的字符串
- * @param cp 正则匹配条件字符串
- * @param mc 要替换成的字符串
- * @return
- */
- public static String ubbPattern(String str, String cp, String mc){
- if(str==null || str.equals("")){
- return "";
- }
- String txt = new String();
- txt = str;
- if(str!=null && !str.equals("")){
- txt = str;
- Pattern p = Pattern.compile(cp,2); //参数2表示大小写不区分
- Matcher m = p.matcher(txt);
- txt = m.replaceAll(mc);
- }else{
- txt = "";
- }
- return txt;
- }
-
-
-
-
- public static String matcherStr(String str, String cp, String s){
- if(str==null || str.equals("")){
- return "";
- }
- String txt = new String();
- txt = str;
- if(str!=null && !str.equals("")){
- txt = str;
- Pattern p = Pattern.compile(cp,2); //参数2表示大小写不区分
- Matcher m = p.matcher(txt);
- StringBuffer sb = new StringBuffer();
- int i=0;
- boolean result = m.find();
- //使用循环将句子里所有匹配的内容找出并替换再将内容加到sb里
- while(result) {
- i++;
- sb.append(m.group());
- sb.append(s);
- //继续查找下一个匹配对象
- result = m.find();
- }
- txt = String.valueOf(sb);
- }else{
- txt = "";
- }
- return txt;
- }
-
-
- public void setUbbImg(boolean ubbImg){
- this.ubbImg = ubbImg;
- }
- public void setUbbUrl(boolean ubbUrl){
- this.ubbUrl = ubbUrl;
- }
- }