package com.sino.publicclass.util;

import java.util.StringTokenizer;

public class StringTools {
	public static boolean ValidateStr(String value) {
		if (value == null)
			return true;
		else if (value.trim().length() == 0)
			return true;
		else
			return false;
	}

	/**
	 * 把""转成&nbsp;
	 * 
	 * @param string
	 *            String
	 * @return String
	 */
	public static String getEmpty(String string) {
		String temp = "&nbsp;";
		if (string.trim().equals("")) {
			return temp;
		}
		return string;
	}

	public static String convertForHtml(String s) {
		if (s == null) {
			return s;
		}
		if (s.equals("")) {
			return s;
		}
		int i = s.length();
		StringBuffer stringbuffer = new StringBuffer();
		for (int j = 0; j < i; j++) {
			switch (s.charAt(j)) {
			case 60: // '<'
				stringbuffer.append("&lt;");
				break;
			case 62: // '>'
				stringbuffer.append("&gt;");
				break;
			case 34: // '"'
				stringbuffer.append("&quot;");
				break;
			// case 32: // ' '
			// stringbuffer.append("&nbsp;");
			// break;
			// case 10: // '\n'
			// stringbuffer.append("<br>");
			// break;
			default:
				stringbuffer.append(s.charAt(j));
				break;
			}
		}
		return stringbuffer.toString();
	}

	public static String convertForTextArea(String s) {
		if (s == null || s.equals("")) {
			return "&nbsp;";
		}
		int i = s.length();
		StringBuffer stringbuffer = new StringBuffer();
		for (int j = 0; j < i; j++) {
			switch (s.charAt(j)) {
			case 60: // '<'
				stringbuffer.append("&lt;");
				break;
			case 62: // '>'
				stringbuffer.append("&gt;");
				break;
			case 34: // '"'
				stringbuffer.append("&quot;");
				break;
			case 32: // ' '
				stringbuffer.append("&nbsp;");
				break;
			case 10: // '\n'
				stringbuffer.append("<br>");
				break;
			default:
				stringbuffer.append(s.charAt(j));
				break;
			}
		}
		return stringbuffer.toString();
	}

	public static String convertForWrap(String s) {
		if (s == null || s.equals("")) {
			return "";
		}
		int i = s.length();
		StringBuffer stringbuffer = new StringBuffer();
		for (int j = 0; j < i; j++) {
			if ((j + 1) < i && s.charAt(j) == 13 && s.charAt(j + 1) == 10) {
				stringbuffer.append("$wrap$");
			} else if (s.charAt(j) != 13 && s.charAt(j ) != 10) {
				stringbuffer.append(s.charAt(j));
			}
		}
		return stringbuffer.toString();
	}

	/***************************************************************************
	 * 功能/function: 该方法使用转义字符"\" 转换SQL语句Like串中的 "% _" 转义后SQL语句后加" ESCAPE '\\' "
	 * 参数/parameter: 输入String 返回/return: 转换后的String 步骤/steps : 修改记录/Revision 日期
	 * 修改人 描述
	 * 
	 **************************************************************************/
	public static String convertLikeStr(String s) {
		if (s == null) {
			return "";
		}
		// 转义SQL语句Like串中的"\"
		String s1 = s, s2 = "";
		int i = 0;
		while (i >= 0) {
			i = s1.indexOf("\\");
			if (i >= 0) {
				s2 += s1.substring(0, i) + "\\\\";
				s1 = s1.substring(i + 1, s1.length());
			}
		}
		s2 += s1;
		// 转义SQL语句Like串中的"%"
		s1 = s2;
		s2 = "";
		i = 0;
		while (i >= 0) {
			i = s1.indexOf("%");
			if (i >= 0) {
				s2 += s1.substring(0, i) + "\\%";
				s1 = s1.substring(i + 1, s1.length());
			}
		}
		s2 += s1;
		// 转义SQL语句Like串中的"_"
		s1 = s2;
		s2 = "";
		i = 0;
		while (i >= 0) {
			i = s1.indexOf("_");
			if (i >= 0) {
				s2 += s1.substring(0, i) + "\\_";
				s1 = s1.substring(i + 1, s1.length());
			}
		}
		s2 += s1;
		s2 = "'%" + s2 + "%'";
		return s2;
	}

	public static String convertSqlStr(String s) {
		if (s == null) {
			return "";
		}
		// 转义SQL语句的"'"
		String s1 = s, s2 = "";
		int i = 0;
		while (i >= 0) {
			i = s1.indexOf("'");
			if (i >= 0) {
				s2 += s1.substring(0, i) + "''";
				s1 = s1.substring(i + 1, s1.length());
			}
		}
		s2 += s1;
		return "'" + s2 + "'";
	}

	public static String[] separate(String str, String separator) {
		StringTokenizer st = new StringTokenizer(str, separator);
		String[] result = new String[st.countTokens()];
		int i = 0;
		while (st.hasMoreTokens()) {
			result[i] = st.nextToken();
			i++;
		}
		return result;
	}

	public static String toGBK(String str) {
		try {
			if (str == null) {
				return null;
			} else {
				str = new String(str.getBytes("ISO8859_1"), "GBK");
				return str;
			}
		} catch (Exception e) {
			return null;
		}
	}

	public static String convertTime(long time) {
		String value;
		int h, m, s;
		h = (int) (time / 3600);
		m = (int) ((time % 3600) / 60);
		s = (int) ((time % 3600) % 60);
		value = s + "秒";
		value = m + "分" + value;
		value = h + "小时" + value;
		return value;
	}
	
	public static String convertToMS(long time) {
		String rt="";
		if(time>60) {
			int m,s;
			m=(int)time/60;
			s=(int)time%60;
			return m+"分"+s+"秒";
		}else {
			return time+"秒";
		}
	}
	
	/**
	 * 把 null 字符串对象转换为 空串 "",非 null 对象直接返回
	 * @param src
	 * @return
	 */
	public static String convertNullToEmpty(String src){
		if (null != src){
			return src;
		} else {
			return "";
		}
	}
	
	public static String getJavascript(String message){
		return "<script>" + message + "</script>";
	}
	
	public static String getJavascriptAlert(String message){
		return "<script>alert(\"" + message.replaceAll("\"", "'").replaceAll("\\n", "") + "\");</script>";
	}
	
	public static String getJavascriptAlertAndClose(String message){
		return "<script>alert(\"" + message.replaceAll("\"", "'").replaceAll("\\n", "") + "\");window.close();</script>";
	}
	
/*
	public static String convertStartToEnd(String start, String end) {
		String value = "";
		start = DateTime.reFormatTime(start, DateTime.PATTERN_0, DateTime.PATTERN_1);
		end = DateTime.reFormatTime(end, DateTime.PATTERN_0, DateTime.PATTERN_1);
		if (start.substring(0, 10).equals(end.substring(0, 10))) {
			value = start + " — " + end.substring(11);
		} else {
			value = start + " — " + end;
		}
		return value;
	}
	*/
}
