EncodeString.java
上传用户:u_thks
上传日期:2022-07-31
资源大小:1910k
文件大小:20k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

Java

  1. /* 
  2.  * Made in GamVan
  3.  */
  4. package com.gamvan.tools;
  5. import java.io.UnsupportedEncodingException;
  6. import java.security.MessageDigest;
  7. import sun.misc.BASE64Encoder;
  8. import java.util.regex.*;
  9. import com.gamvan.tools.MD5;
  10. /**
  11.  * 字符串操作集合类
  12.  * @author GamVan by 我容易么我
  13.  * Powered by GamVan.com
  14.  */
  15. public class EncodeString{
  16.     private boolean ubbUrl = false;
  17.     private boolean ubbImg = false;
  18.     /**
  19.      * 考虑到中文的字符串长度判断
  20.      * @param str
  21.      * @return
  22.      */
  23.     public static int Glength(String str){
  24.         int i = 0;
  25.         String s = "";
  26.         try {
  27.             s = new String(str.getBytes(),"UTF-8");
  28.             i = s.length();
  29.         } catch (UnsupportedEncodingException e) {
  30.             e.printStackTrace();
  31.         }
  32.         return i;
  33.     }
  34.     
  35.     
  36.     /**
  37.     * 中英文混和字符串按特定长度截取
  38.     * @param res 中英文混和字符。
  39.     * @param start 截取起始位置。
  40.     * @param length 截取长度。
  41.     * @return res中,从start起length长度的子字符串。
  42.     */
  43.     public static String Gsubstring(String res, int start, int length ,String charSet) {
  44.         if(start<1){
  45.             start=1;
  46.         }
  47.         int i_Start,i_Len ,i,j,ilenByte,i_S,i_L;
  48.         i_Start= 0;
  49.         i_S =0;
  50.         i_Len=0;
  51.         i_L =0;
  52.         try {
  53.             byte[] resBytes = res.getBytes(charSet);
  54.             ilenByte =resBytes.length ;
  55.         
  56.             for ( i = 0; i < ilenByte; i++) {
  57.                 i_Start = i_Start+1;
  58.                 if (i_Start <=start) {i_S = i_S+1;}
  59.                 
  60.                 if (i_Start >=start){
  61.                     i_Len=i_Len+1; 
  62.                     i_L = i_L +1;
  63.                 } 
  64.                 
  65.                 if (resBytes[i] <0 ) {
  66.                     i = i+2;
  67.                     i_Start = i_Start +1;
  68.                     if (i_Start <start) {i_S = i_S+2;}
  69.                     if (i_Start >=start){
  70.                             i_Len=i_Len+1;
  71.                             i_L=i_L+2;
  72.                     }
  73.                 }
  74.             
  75.                 if (i_Len >=length) i =ilenByte;
  76.             }
  77.     
  78.         byte[] dest = new byte[i_L];
  79.     
  80.         for ( j = 0; j < i_L;j++) {
  81.             dest[j] = resBytes[i_S + j-1];
  82.         }
  83.     
  84.         return new String(dest, charSet);
  85.     
  86.         } catch (Exception e) {
  87.             e.printStackTrace();
  88.             return "";
  89.         }
  90.         
  91.     }
  92.     public static String encodeString(String codeType, String txt){
  93.         if(codeType.equals("md5of16")){
  94.             MD5 m = new MD5();
  95.             return m.getMD5ofStr16(txt);
  96.         }else if(codeType.equals("md5of32")){  
  97.             MD5 m = new MD5();
  98.             return m.getMD5ofStr(txt);
  99.         }else{
  100.             try {
  101.                 MessageDigest gv = MessageDigest.getInstance(codeType);
  102.                 gv.update(txt.getBytes());
  103.                 return new BASE64Encoder().encode(gv.digest());
  104.             }
  105.             catch(java.security.NoSuchAlgorithmException e) {
  106.                 e.printStackTrace();//打印错误信息;
  107.                 return null;
  108.             }
  109.         }
  110.     }
  111.     
  112.     
  113.     
  114.     //codeType 加密方式, txt加密字符串
  115.     private  String replaces(String src, String fnd, String rep){
  116.         if(src==null || src.equals("")){
  117.             return "";
  118.         }
  119.         if (src == null || src.equals("")){
  120.             return "";
  121.         }
  122.         String dst = src;
  123.         int idx = dst.indexOf(fnd);
  124.         while (idx >= 0){
  125.             dst = dst.substring(0, idx) + rep + dst.substring(idx + fnd.length(), dst.length());
  126.             idx = dst.indexOf(fnd, idx + rep.length());
  127.         }
  128.         return dst;
  129.     }
  130.     
  131.     
  132.     
  133.     public static String codeColor(String str){
  134.         if(str==null) return "";
  135.         str = str.replaceAll(" ","");
  136.         str = ubbPattern(str, "&quot;(.*?)&quot;","<span style=color:#2A00FF;>&quot;$1&quot;</span>");
  137.         str = ubbPattern(str, "\/\*(.*?)\*/","<span style=color:#3F7F5F;>/*$1*/</span>");
  138.         
  139.         //str = ubbPattern(str, "http://(.*?)<br/>","<span style=color:#3F7F5F;>http://$1</span><br/>");
  140.         str = ubbPattern(str, "\/\/(.*?)\<br/>","<span style=color:#3F7F5F;>//$1</span><br/>");
  141.         
  142.         //str = ubbPattern(str, "&lt;(.*?)&gt;","<span style=color:#006600;>&lt;$1&gt;</span>");
  143.         //str = ubbPattern(str, "&lt;(.*?)/&gt;","<span style=color:#006600;>&lt;$1/&gt;</span>");
  144.         str = ubbPattern(str,"private&nbsp;","<span style="color:#7F0055; font-weight: bold;">private&nbsp;</span>");
  145.         str = ubbPattern(str,"protected&nbsp;","<span style="color:#7F0055; font-weight: bold;">protected&nbsp;</span>");
  146.         str = ubbPattern(str,"public&nbsp;","<span style="color:#7F0055; font-weight: bold;">public&nbsp;</span>");
  147.         str = ubbPattern(str,"static&nbsp;","<span style="color:#7F0055; font-weight: bold;">static&nbsp;</span>");
  148.         str = ubbPattern(str,"return&nbsp;","<span style="color:#7F0055; font-weight: bold;">return&nbsp;</span>");
  149.         str = ubbPattern(str,"try","<span style="color:#7F0055; font-weight: bold;">try</span>");
  150.         str = ubbPattern(str,"catch","<span style="color:#7F0055; font-weight: bold;">catch</span>");
  151.         str = ubbPattern(str,"finally","<span style="color:#7F0055; font-weight: bold;">finally</span>");
  152.         str = ubbPattern(str,"package","<span style="color:#7F0055; font-weight: bold;">package</span>");
  153.          
  154.         
  155.         str = ubbPattern(str,"null","<span style="color:#7F0055; font-weight: bold;">null</span>");
  156.         str = ubbPattern(str,"short&nbsp;","<span style="color:#7F0055; font-weight: bold;">short&nbsp;</span>");
  157.         str = ubbPattern(str,"int&nbsp;","<span style="color:#7F0055; font-weight: bold;">int&nbsp;</span>");
  158.         str = ubbPattern(str,"double&nbsp;","<span style="color:#7F0055; font-weight: bold;">double&nbsp;</span>");
  159.         str = ubbPattern(str,"byte&nbsp;","<span style="color:#7F0055; font-weight: bold;">byte&nbsp;</span>");
  160.         str = ubbPattern(str,"long&nbsp;","<span style="color:#7F0055; font-weight: bold;">long&nbsp;</span>");
  161.         str = ubbPattern(str,"final&nbsp;","<span style="color:#7F0055; font-weight: bold;">final&nbsp;</span>");
  162.         str = ubbPattern(str,"new&nbsp;","<span style="color:#7F0055; font-weight: bold;">new&nbsp;</span>");
  163.         str = ubbPattern(str,"false","<span style="color:#7F0055; font-weight: bold;">false</span>");
  164.         str = ubbPattern(str,"true","<span style="color:#7F0055; font-weight: bold;">true</span>");
  165.         
  166.         
  167.         
  168.         str = ubbPattern(str,"while\(","<span style="color:#7F0055; font-weight: bold;">while</span>(");
  169.         str = ubbPattern(str,"for\(","<span style="color:#7F0055; font-weight: bold;">for</span>(");
  170.         str = ubbPattern(str,"switch","<span style="color:#7F0055; font-weight: bold;">switch</span>");
  171.         str = ubbPattern(str,"if\(","<span style="color:#7F0055; font-weight: bold;">if</span>(");
  172.         str = ubbPattern(str,"else","<span style="color:#7F0055; font-weight: bold;">else</span>");
  173.         str = ubbPattern(str,"this\.","<span style="color:#7F0055; font-weight: bold;">this</span>.");
  174.         str = ubbPattern(str,"function\(","<span style="color:#7F0055; font-weight: bold;">function</span>(");
  175.         str = ubbPattern(str,"document.","<span style="color:#7F0055; font-weight: bold;">document</span>.");
  176.         
  177.         
  178.         return str; 
  179.     }
  180.     
  181.     
  182.     public static void main(String[] str){
  183.         P.rintl(EncodeString.htmlRun("今晚23[code]public[/code]"));
  184.     }
  185.     
  186.  
  187.     
  188.     /**
  189.      * 标记可运行代码
  190.      * @param src
  191.      * @return
  192.      */
  193.     private static String htmlRun(String src){
  194.         if(src==null) return "";
  195.         String str = "";
  196.         //str = matcherStr(src, "\[html\](.*?)\[/html]", "GVCODE903");
  197.         //str = unHtmlEncoder(src);        
  198.         str = matcherCodeColor(src, "\[code\](.*?)\[/code]");
  199.         str = ubbPattern(str, "\[code\](.*?)\[/code]","<table align=center bgcolor="#BAD5EF" width="98%" border=0 cellpadding=4 cellspacing=1>"
  200.         +"<form><tr><td bgcolor="#E6EEF7" height="25"  style="font-size: 12px">程序代码:</td></tr><tr><td bgcolor="#ffffff" style="font-size: 12px">"
  201.         //"<textarea cols="60" rows="10" id="gv123">"
  202.         + ("$1")
  203.         +"</td></tr></form></table>");
  204.         return str;
  205.     }
  206.     
  207.     
  208.     
  209.     //运行html代码
  210.     //用于HTML字符串反向替换
  211.     public  static String unHtmlEncoder(String src) {
  212.         if (src == null || src.equals("")){
  213.             return "";
  214.         }
  215.         String unHtmlEncode = new String(src);
  216.         unHtmlEncode = unHtmlEncode.replaceAll("&lt;","<");
  217.         unHtmlEncode = unHtmlEncode.replaceAll("&gt;",">"); 
  218.         unHtmlEncode = unHtmlEncode.replaceAll("&quot;",""");   
  219.         unHtmlEncode = unHtmlEncode.replaceAll("&#039;","'");
  220.         unHtmlEncode = unHtmlEncode.replaceAll("&nbsp;"," ");       
  221.         unHtmlEncode = unHtmlEncode.replaceAll("<br/>","rn");
  222.         unHtmlEncode = unHtmlEncode.replaceAll("<br/>","r");       
  223.         unHtmlEncode = unHtmlEncode.replaceAll("<br/>","n");
  224.         return unHtmlEncode;
  225.     }
  226.     //用于HTML字符串替换
  227.     public static String htmlEncoder(String src) {
  228.         if (src == null || src.equals("")){
  229.             return "";
  230.         }
  231.         String htmlencode = new String(src);
  232.         htmlencode = htmlencode.replaceAll("<","&lt;");
  233.         htmlencode = htmlencode.replaceAll(">","&gt;"); 
  234.         htmlencode = htmlencode.replaceAll(""","&quot;");   
  235.         htmlencode = htmlencode.replaceAll("'","&#039;");
  236.         htmlencode = htmlencode.replaceAll(" "," &nbsp;");       
  237.         htmlencode = htmlencode.replaceAll("rn","<br/>");
  238.         htmlencode = htmlencode.replaceAll("r","<br/>");       
  239.         htmlencode = htmlencode.replaceAll("n","<br/>");       
  240.         return htmlencode;
  241.     }
  242.    /**
  243.      * 转换为XML编码.<br>
  244.      */
  245.     public String xmlEncoder(String src){
  246.         if (src == null || src.equals("")){
  247.             return "";
  248.         }
  249.         String xmlencode = src;
  250.         xmlencode = replaces(xmlencode, "&",  "&amp;");
  251.         xmlencode = replaces(xmlencode, "<",  "&lt;");
  252.         xmlencode = replaces(xmlencode, ">",  "&gt;");
  253.         xmlencode = replaces(xmlencode, """, "&quot;");
  254.         xmlencode = replaces(xmlencode, "'", "&acute;");
  255.         return xmlencode;
  256.     }
  257.     
  258.     
  259.     public String ubbEncoder(String str){
  260.         if(str==null || str.equals("")){
  261.             return "";
  262.         }
  263.         str = ubbPattern(str,"\[center\](.*?)\[/center\]","<center>$1</center>");
  264.         str = ubbPattern(str,"\[b\](.*?)\[/b\]","<strong>$1</strong>");
  265.         str = ubbPattern(str,"\[i\](.*?)\[/i\]","<emg>$1</em>");
  266.         str = ubbPattern(str, "\[color=(.*?)\](.*?)\[/color\]","<span style=color:$1>$2</span>");
  267.         str = ubbPattern(str, "\[GamVanFace_(\d*)\]","<img src="GVimgs/GamVanFace/$1.gif">");
  268.         str = ubbPattern(str, "\[img\](.*?)\[/img\]","<a href="$1" target="_blank"><img " +
  269.             " onload="javascript:if(this.width > screen.width-350){this.width = screen.width-350};" " + 
  270.             " onerror="javascript:this.src='GVimgs/imgErr.gif'" src="$1" border="0" /></a>");
  271.         str = ubbPattern(str, "\[url\](.*?)\[/url\]","<a href="$1" target="_blank">$1</a>");
  272.         str = ubbPattern(str, "\[url=(.*?)\](.*?)\[/url\]","<a href="$1" target="_blank">$2</a>");
  273.         str = ubbPattern(str, "\[size=*([\d]*)\](.*?)\[/size\]","<span style="font-size: $1px;">$2</span>");
  274.         str = ubbPattern(str,"\[quote\](.*?)\[/quote\]","<table align=center bgcolor="#BAD5EF" width="98%" border=0 cellpadding=4 cellspacing=1>" +
  275.                                 "<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>");
  276.         
  277.         str = ubbPattern(str,"\[quote=(.*?)\](.*?)\[/quote\]","<table align=center bgcolor="#BAD5EF" width="98%" border=0 cellpadding=4 cellspacing=1>" +
  278.         "<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>");
  279.         
  280.         
  281.         
  282.         StringBuffer sb = new StringBuffer("");
  283.         sb.append("<object codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" );
  284.         sb.append(" height=400 width=500 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000>");
  285.         sb.append("<param name="movie" value="$1">");
  286.         sb.append("<param name="menu" value="false">");
  287.         sb.append("<embed src="$1" menu=false quality=high ");
  288.         sb.append(" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"" );
  289.         sb.append(" type="application/x-shockwave-flash" width="500" height="400"></embed></object>");
  290.         //UBB多媒体部分开始
  291.         str = ubbPattern(str, "\[flash\](.*?)\[/flash\]","<a href="$1" TARGET=_blank>[全屏欣赏]</a><br>" + sb.toString());
  292.        
  293.         sb.delete(0,sb.length());
  294.         
  295.         sb.append("<object codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" );
  296.         sb.append(" height=$2 width=$1 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000>");
  297.         sb.append("<param name="movie" value="$3">");
  298.         sb.append("<param name="menu" value="false">");
  299.         sb.append("<embed src="$3" menu=false quality=high ");
  300.         sb.append(" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"" );
  301.         sb.append(" type="application/x-shockwave-flash" width="$1" height="$2"></embed></object>");
  302.         
  303.         str = ubbPattern(str, "\[flash=*([\d]*),*([\d]*)\](.*?)\[/flash\]","<a href="$3" TARGET=_blank>[全屏欣赏]</a><br>" + sb.toString());
  304.         sb.delete(0,sb.length());  
  305.         
  306.         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>");
  307.         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>");
  308.         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>");
  309.         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>");
  310.         // UBB多媒体部分结束
  311.         
  312.         if(ubbImg){
  313.             str = ubbPattern(str,"((http|https|ftp|rtsp|mms):(//|\\)([\w\.\:/\?\=%&_-]+(gif|jpg|jpeg|bmp|png)))",
  314.             "<a href="$1" target="_blank"><img onload="javascript:if(this.width > screen.width-350){this.width = screen.width-350};" " +
  315.             " onerror="javascript:this.src='GVimgs/imgErr.gif'" src="$1" border="0"></a>");
  316.         }else{ 
  317.             if(ubbUrl){
  318.                 str = ubbPattern(str,"((http|https|ftp|rtsp|mms):(//|\\)([\w\.\:/\?\=%&_-]+))",
  319.                 "<a target="_blank" href="$1">$1</a>");
  320.                 //str = ubbPattern(str,"^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$" ,
  321.                         //"<a target="_blank" href="$1">$1</a>");
  322.             }
  323.         }
  324.         
  325.         str = htmlRun(str);
  326.         return str;
  327.     }
  328.     
  329.     
  330.     
  331.     
  332.     /**
  333.      * 代码内容的色彩替换
  334.      * @param str
  335.      * @param cp
  336.      * @param s
  337.      * @return
  338.      */    
  339.     public static String matcherCodeColor(String str, String cp){
  340.         if(str==null || str.equals("")){
  341.             return "";
  342.         }
  343.         String txt = new String();
  344.         if(str!=null && !str.equals("")){
  345.             txt = str;
  346.             Pattern p = Pattern.compile(cp,2); //参数2表示大小写不区分
  347.             Matcher m = p.matcher(txt);
  348.             StringBuffer sb = new StringBuffer();
  349.             boolean result = m.find();
  350.             String temp = "";
  351.             //使用循环将句子里所有匹配的内容找出并替换再将内容加到sb里
  352.             while(result) {
  353.                 temp = m.group();
  354.                 temp = codeColor(temp);
  355.                 m.appendReplacement(sb,temp);
  356.                 //继续查找下一个匹配对象
  357.                 result = m.find();
  358.             }
  359.             //最后调用appendTail()方法将最后一次匹配后的剩余字符串加到sb里;
  360.             m.appendTail(sb);
  361.             txt = String.valueOf(sb);
  362.         }else{
  363.             txt = "";
  364.         }
  365.         return txt;       
  366.     }
  367.     
  368.     
  369.     
  370.     
  371.     
  372.     
  373.     /**
  374.      * 正则匹配并完成替换过程
  375.      * @param str 要检索的字符串
  376.      * @param cp 正则匹配条件字符串
  377.      * @param mc 要替换成的字符串
  378.      * @return
  379.      */
  380.     public static String ubbPattern(String str, String cp, String mc){
  381.         if(str==null || str.equals("")){
  382.             return "";
  383.         }
  384.         String txt = new String();
  385.         txt = str;
  386.         if(str!=null && !str.equals("")){
  387.             txt = str;
  388.             Pattern p = Pattern.compile(cp,2); //参数2表示大小写不区分
  389.             Matcher m = p.matcher(txt);
  390.             txt = m.replaceAll(mc);
  391.         }else{
  392.             txt = "";
  393.         }
  394.         return txt;       
  395.     }
  396.     
  397.     
  398.  
  399.     
  400.     public static String matcherStr(String str, String cp, String s){
  401.         if(str==null || str.equals("")){
  402.             return "";
  403.         }
  404.         String txt = new String();
  405.         txt = str;
  406.         if(str!=null && !str.equals("")){
  407.             txt = str;
  408.             Pattern p = Pattern.compile(cp,2); //参数2表示大小写不区分
  409.             Matcher m = p.matcher(txt);
  410.             StringBuffer sb = new StringBuffer();
  411.             int i=0;
  412.             boolean result = m.find();
  413.             //使用循环将句子里所有匹配的内容找出并替换再将内容加到sb里
  414.             while(result) {
  415.                 i++;
  416.                 sb.append(m.group());
  417.                 sb.append(s);
  418.                 //继续查找下一个匹配对象
  419.                 result = m.find();
  420.             }
  421.             txt = String.valueOf(sb);
  422.         }else{
  423.             txt = "";
  424.         }
  425.         return txt;       
  426.     }
  427.     
  428.     
  429.     public void setUbbImg(boolean ubbImg){
  430.         this.ubbImg = ubbImg;
  431.     }
  432.     public void setUbbUrl(boolean ubbUrl){
  433.         this.ubbUrl = ubbUrl;
  434.     }
  435. }