实用的工具类

 

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;

/**
 * 编码工具类,包括: <br>
 * 1)MD5/SHA编码 <br>
 * 2)Base64编码 <br>
 * 3)DES/ThreeDES编码 <br>
 * 4)GBK/UTF-8/ISO-8859-1/UNICODE转换 <br>
 */
public class CoderUtil 
{
    public static final String ALGORITHM_MD5 = "MD5";
    
    /**
     * MD5 编码,返回本地编码字符串
     * 
     * @param src 源串
     * @param encoding 编码
     * @return 目标串
     */
     public static String md5Encoder(String src, String encoding) 
     {
         if (src == null)
             return null;
         
         byte[] destBytes = md5EncoderByte(src, encoding);
         String dest = "";
         for (int i = 0; i < 16; i++) 
         {
             dest += byteToHEX(destBytes[i]);
         }
         
         return dest;
     }
     
    /**
     * MD5 编码,返回本地编码字符串
     * 
     * @param src 源串 默认使用UTF-8编码
     * @return 目标串
     */
     public static String md5Encoder(String src) 
     {
         return md5Encoder(src, "UTF-8");
     }
     
       /**
      * MD5编码,返回byte数组,本地编码
      * 
      * @param src 源串 默认使用UTF-8编码
      * @return 目标编码
      */
     public static byte[] md5EncoderByte(String src)
     {
         return md5EncoderByte(src, "UTF-8");
     }
     
     /**
      * MD5编码,返回byte数组
      * 
      * @param src 源串
      * @param encoding 编码 
      * @return 目标编码
      */
     public static byte[] md5EncoderByte(String src, String encoding)
     {
         if (src == null)
             return null;
         
        try
        {
            if (encoding == null)
                return md5EncoderByte(src.getBytes());
            else
                return md5EncoderByte(src.getBytes(encoding));
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
            return null;
        }
     }

     /**
      * MD5编码,返回byte数组
      * 
      * @param buf 源byte数组
      * @return 目标编码
      */
     public static byte[] md5EncoderByte(byte[] buf)
     {
         if (buf == null)
             return null;
         
         try
         {
             MessageDigest md5Temp = MessageDigest.getInstance(ALGORITHM_MD5);
             return md5Temp.digest(buf);
         }
         catch(Exception e)
         {
             e.printStackTrace();
             return null;
         }
    }
     
    /**
     * Base64 编码
     * 
     * @param src 源串
     * @return 目标串
     */
    public static String base64Encode(String src) 
    {
        if (src == null)
            return null;
        
        return base64Encode(src.getBytes());
    }
    
    /**
     * Base64 编码
     * 
     * @param src 源串
     * @return 目标串
     */
    public static String base64Encode(byte[] src) 
    {
        if (src == null)
            return null;
        
        return new String(Base64.encodeBase64(src));
    }

       /**
     * Base64 编码
     * 
     * @param src 源串
     * @return 目标串
     */
    public static String base64Encode(String src, String encoding) 
    {
        if (src == null)
            return null;
        
        try
        {
            byte[] dest = Base64.encodeBase64(src.getBytes(encoding));
            
            return new String(dest, encoding);
        }
        catch (UnsupportedEncodingException e)
        {
            throw new IllegalArgumentException(e);
        }
    }

    /**
     * Base64 解码
     * 
     * @param dest 目标串
     * @return 源编码
     */
    public static byte[] base64DecodeBytes(String dest) 
    {
        if (dest == null)
            return null;
        
        return Base64.decodeBase64(dest.getBytes());
    }
    
    /**
     * Base64 解码
     * 
     * @param dest 目标串
     * @return 源串
     */
    public static String base64Decode(String dest) 
    {
        if (dest == null)
            return null;
        
        byte[] b = Base64.decodeBase64(dest.getBytes());
        return new String(b);
    }

    /**
     * Base64 解码
     * 
     * @param dest 目标串
     * @return 源串
     */
    public static String base64Decode(String dest, String encoding) 
    {
        if (dest == null)
            return null;
        try
        {
            byte[] b = Base64.decodeBase64(dest.getBytes(encoding));
            return new String(b, encoding);
        }
        catch (UnsupportedEncodingException e)
        {
            throw new IllegalArgumentException(e);
        }
    }

    /**
     * Base64 解码
     * 
     * @param dest 目标串
     * @return 源编码
     */
    public static byte[] base64DecodeBytes(String dest, String encoding) 
    {
        if (dest == null)
            return null;
        
        try
        {
            return Base64.decodeBase64(dest.getBytes(encoding));
        }
        catch (UnsupportedEncodingException e)
        {
            throw new IllegalArgumentException(e);
        }
    }
    
    /**
     * 把字节转换为16进制字符
     * 
     * @param ib
     * @return
     */
    public static String byteToHEX(byte ib) 
    {
        char[] Digit = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        char [] ob = new char[2];
        ob[0] = Digit[(ib >>> 4) & 0X0F];
        ob[1] = Digit[ib & 0x0F];
        String s = new String(ob);
        return s;
    }
  
    /**
     * 给定一个url,和key,取得value
     * 此方法仅供参考,不作解释
     * @param url
     * @param key
     * @return String 给定一个url,和key,取得value
     */
    public static String getURLValue(String url,String key)
    {
        if(isEmpty(url) || isEmpty(key))
            return "";

        int p = url.indexOf("?");
        if (p != -1)
            url = url.substring(p);
        
        if (!url.startsWith("?"))
            url = "?" + url;
        
        int index = url.indexOf(key);
        if(index == -1)
            return "";
        
        if ((url.charAt(index-1) != '?' && url.charAt(index-1) != '&') 
            || index+key.length()>=url.length() || url.charAt(index+key.length()) != '=')
            return "";

        int point = index + key.length();
        int valueIndex = url.indexOf('&',point);
        if (valueIndex == -1)
            return url.substring(point+1,url.length());
        
        return url.substring(point+1,valueIndex);
    }
    
    /** 检查字符串是否为空 */
    private static boolean isEmpty(String s)
    {
        return ((s == null) || (s.trim().length() == 0));
    }
    
    /**
     * 读取XML中字段的值,允许没有field时返回空字符串
     * 此方法仅供参考,不作解释
     * @param xml XML字符串
     * @param field 字段名
     * @return 返回XML中字段中的值
     * @throws Exception 如果字段内容不符合要求,则抛出此异常
     */
    public static String getXmlField(String xml, String field) throws Exception
    {
        if (xml.length() < field.length() * 2 + 5)
        {
            throw new Exception("xml tag #"+field+"# format error");
        }
        
        String lField = "<"+field+">";
        String rField = "</"+field+">";
        int iLeft = xml.indexOf(lField);
        int iRight = xml.indexOf(rField);
        if (iLeft == -1 && iRight == -1)
            return "";
        
        if ((iLeft == -1 && iRight != -1) || (iLeft != -1 && iRight == -1))
        {
            throw new Exception("xml tag #"+field+"# format error");
        }
        
        int valueLen = iRight - iLeft - lField.length();
        if (valueLen <= 0)
            return "";
        
        return xml.substring(iLeft + lField.length(), iRight);
    }
    
    /**
     * 读取XML中字段的int值 
     * 此方法仅供参考,不作解释
     * @param xml XML字符串
     * @param field 字段名
     * @return int 返回XML中字段中的int值
     * @throws Exception 如果字段不是int或格式不正确,则抛出此异常
     */
    public static int getXmlFieldInt(String xml, String field) throws Exception
    {
        if (xml.length() < field.length() * 2 + 5)
        {
            throw new Exception("xml tag #"+field+"# format error");
        }

        String lField = "<"+field+">";
        String rField = "</"+field+">";
        int iLeft = xml.indexOf(lField);
        int iRight = xml.indexOf(rField);
        if (iLeft == -1 || iRight == -1)
        {
            throw new Exception("xml tag #"+field+"# format error");
        }
        
        int valueLen = iRight - iLeft;
        if (valueLen < 1)//nubmer不允许为空
        {
            throw new Exception("xml tag #"+field+"# format error");
        }
        
        String valueInt = xml.substring(iLeft + lField.length(), iRight);
        if (!isInteger(valueInt))
        {
            throw new Exception("xml tag #"+field+"# format error");
        }
        
        return Integer.parseInt(valueInt);
    }
    
    /** 检查是否是整型 */
    private static boolean isInteger(String s)
    {
        if (isEmpty(s))
            return false;

        // 逐个检查,如果出现一个字符不是数字则返回false
        for (int i = 0; i < s.length(); i++)
        {
            if ("0123456789".indexOf(s.charAt(i)) == -1)
                return false;
        }

        return true;
    }
}

你可能感兴趣的:(java,xml)