/**

 * 

 *开发单位：长城软件与系统有限公司

 *版权所属：长城软件与系统有限公司

 *创建日期：  2016-8-15      

 *

 */

package com.tax.token.utils;



import java.net.InetAddress;

import java.net.UnknownHostException;

import java.util.Random;

import java.util.regex.Matcher;

import java.util.regex.Pattern;



/**

 * ****************************************************************************

 * 字符串操作工具类

 *

 * @author(作者)：xuyongyun	

 * @author(复审人)：	xu 2016-5-10

 * @version(版本)1.0

 * @since 1.0

 ******************************************************************************

 */



public class StringEncryptUtil {

	

	private static char[] CH_HEX = {'0', '1', '2', '3', '4', '5', '6', '7', '8',

        		'9', 'A', 'B', 'C', 'D', 'E', 'F'};



	/**

	 * 判断是不是空白字符串

	 * @param src 待处理字符串

	 * @return 空白时返回true，非空白时返回false

	 * @since           1.0

	 */

	public static boolean isBlank(String src){

		if(src==null||src.length()==0||src.trim().length()==0){

			return true;

		}else{

			return false;

		}

	}

	

	/**

	 * 处理字符串两端空白

	 * @param src 待处理字符串

	 * @param defaultValue 字符串空白时默认返回值

	 * @return src空白时返回defaultValue，非空白时进行左右的空白处理

	 * @since           1.0

	 */

	public static String trim(String src,String defaultValue){

		if(isBlank(src)){

			return defaultValue;

		}else{

			return src.trim();

		}

	}

	

	/**

	 * 处理字符串中间的回车换行及两端的空白

	 *	

	 * @param src

	 * @return

	 * @since           1.0

	 */

	public static String trimEnter(String src){

		if(!isBlank(src)){

			src = trim(src,"");

			src = src.replaceAll("\r", "");

			src = src.replaceAll("\n", "");

		}

		return src;

	}

	

	/**

     * 将字节数组转为十六进制字符串

     *

     * @param bytes

     * @return 返回16进制字符串

     */

    public static String byteArrayToHex(byte[] bytes) {

        // 一个字节占8位，一个十六进制字符占4位；十六进制字符数组的长度为字节数组长度的两倍

        char[] chars = new char[bytes.length * 2];

        int index = 0;

        for (byte b : bytes) {

            // 取字节的高4位

            chars[index++] = CH_HEX[b >>> 4 & 0xf];

            // 取字节的低4位

            chars[index++] = CH_HEX[b & 0xf];

        }

        return new String(chars);

    }

    

    public static byte[] hexToBytes(String src) {  

    	int m = 0, n = 0;  

    	int l = src.length() / 2; 

    	byte[] ret = new byte[l];  

    	for (int i = 0; i < l; i++) {  

    		m = i * 2 + 1;  

    	    n = m + 1;  

    	    ret[i] = uniteBytes(src.substring(i * 2, m), src.substring(m, n));  

    	}  

    	return ret;  

    }

    

    private static byte uniteBytes(String src0, String src1) {

    	byte b0 = Byte.decode("0x" + src0).byteValue();

    	b0 = (byte) (b0 << 4);  

    	byte b1 = Byte.decode("0x" + src1).byteValue();  

    	byte ret = (byte) (b0 | b1);  

    	return ret;  

   }

    

    /**

     * 获取字符串的长度，对双字符（包括汉字）按两位计数

     * 

     * @param value

     * @return

     */

    public static int getStrLength(String value) {

    	int length = 0;  

    	for(int i = 0; i < value.length(); i++){ 

    		int ascii = Character.codePointAt(value, i);

    		if(ascii >= 0 && ascii <=255){

    			length++;

    		}else{

    			length += 2;  

    		}

    	}

        return length;  

    }

 

    /**

     * 正则表达式匹配字符串

     *	

     * @param reg 正则表达式

     * @param string 字符串

     * @return 匹配时返回true，不匹配是返回false

     * @since           1.0

     */

    public static boolean matcher(String reg, String string) {

        boolean tem = false;

        Pattern pattern = Pattern.compile(reg);

        Matcher matcher = pattern.matcher(string);

        tem = matcher.matches();

        return tem;

    }

    

    private static String htmlEncode(char c) {

        switch(c) {

           case '&':

               return "&amp;";

           case '<':

               return "&lt;";

           case '>':

               return "&gt;";

           case '"':

               return "&quot;";

           case ' ':

               return "&nbsp;";

           default:

               return c + "";

        }

    }



     



    /** 对传入的字符串str进行Html encode转换 */

    public static String htmlEncode(String str) {

        if(str==null||str.trim().equals("")){

        	return str;

        }



        StringBuilder encodeStrBuilder = new StringBuilder();

        for (int i=0;i<str.length();i++) {

           encodeStrBuilder.append(htmlEncode(str.charAt(i)));

        }

        return encodeStrBuilder.toString();

    }

    

    /**

     * 获取一个唯一标识

     *	

     * @return

     * @since           1.0

     */

    public static String getUuid(){

    	String lsh = "";

    	//当前16进制时间戳

    	String currTime = Long.toHexString(System.currentTimeMillis());

    	

    	//获取weblogic名称

    	String serverName = String.valueOf(System.getProperty("weblogic.Name"));

    	if(serverName==null||serverName.length()==0||serverName.equals("null")){

    		serverName = "";

    	}

    	//未获取到weblogic名称，则获取机器名称

    	if(serverName.length()==0){

    		try {

    			InetAddress addr = InetAddress.getLocalHost();

    			serverName = addr.getHostName().toString(); //获取本机计算机名称   

    		} catch (UnknownHostException e) {

    			serverName = "";

    		}

    	}

    	

    	//机器名过长则截取

    	if(serverName.length()>14){

    		int nameLength = serverName.length();

    		serverName = serverName.substring(0, 7) + serverName.substring(nameLength - 7, nameLength);

    	}

    	

    	//5位随机值

    	Random random = new Random();

    	int val = random.nextInt(89999) + 10000;

    	

    	lsh = currTime + serverName + String.valueOf(val);

    	return lsh;

    }



}

