package com.meiliwan.emall.commons.util;

import java.io.PrintStream;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;

public class StringUtil
{
private static String[] BATCHAPPEND = { "000000", "00000", "0000", "000", "00", "0", "" };

private static final Whitelist whitelist = new Whitelist().addTags(new String[] { "div", "span", "br", "p", "i", "b", "em", "u", "strong", "img", "embed", "font", "blockquote" }).addAttributes("i", new String[] { "class" }).addAttributes("img", new String[] { "src", "alt", "width" }).addProtocols("img", "src", new String[] { "http" }).addAttributes("span", new String[] { "class", "name", "style" }).addAttributes("font", new String[] { "color", "size", "face" }).addAttributes("a", new String[] { "href" }).addAttributes("embed", new String[] { "src", "height", "width", "align", "type", "wmode" }).addProtocols("a", "href", new String[] { "http" }).addProtocols("embed", "src", new String[] { "http" });

public static boolean checkNull(String target)
{
if ((target == null) || ("".equals(target.trim())) || ("null".equals(target.trim()))) {
return true;
}
return false;
}

public static boolean checkNull(Object target)
{
if ((target == null) || ("".equals(target.toString().trim())) || ("null".equals(target.toString().trim())))
{
return true;
}
return false;
}

public static Timestamp convertTimestamp(Date date)
{
if (date == null)
return null;
return new Timestamp(date.getTime());
}

public static Timestamp convertTimestamp(Date date, String time)
{
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = format.format(date);
String timeStr = new StringBuilder().append(dateStr).append(" ").append(time).toString();
Timestamp ts = Timestamp.valueOf(timeStr);
return ts;
}

public static String date2String(Date date, String patten)
{
DateFormat format = new SimpleDateFormat(patten);
String dateStr = format.format(date);

return dateStr;
}

public static Double getDouble(Map<String, Object> map, String key)
{
if ((map != null) && (StringUtils.isNotEmpty(key)) &&
(map.get(key) != null)) {
return (Double)map.get(key);
}

return null;
}

public static BigDecimal getBigDecimal(Map<String, Object> map, String key)
{
if ((map != null) && (StringUtils.isNotEmpty(key)) &&
(map.get(key) != null)) {
return (BigDecimal)map.get(key);
}

return null;
}

public static String getBigDecimalStr(Map<String, Object> map, String key)
{
String dateStr = "";
if ((map != null) && (StringUtils.isNotEmpty(key)) &&
(map.get(key) != null)) {
dateStr = ((BigDecimal)map.get(key)).toString();
}

return dateStr;
}

public static String getString(Map<String, Object> map, String key)
{
String retStr = "";
if ((map != null) && (StringUtils.isNotEmpty(key)) &&
(map.get(key) != null) && (StringUtils.isNotEmpty((String)map.get(key)))) {
retStr = ((String)map.get(key)).trim();
}

return retStr;
}

public static String getDateStr(Map<String, Object> map, String key)
{
String dateStr = "";
if ((map != null) && (StringUtils.isNotEmpty(key)) &&
(map.get(key) != null)) {
dateStr = ((Date)map.get(key)).toString();
}

return dateStr;
}

public static Date getDate(Map<String, Object> map, String key)
{
if ((map != null) && (StringUtils.isNotEmpty(key)) &&
(map.get(key) != null)) {
return (Date)map.get(key);
}

return null;
}

public static String getIntegerStr(Map<String, Object> map, String key)
{
String dateStr = "";
if ((map != null) && (StringUtils.isNotEmpty(key)) &&
(map.get(key) != null)) {
dateStr = ((Integer)map.get(key)).toString();
}

return dateStr;
}

public static Integer getInteger(Map<String, Object> map, String key)
{
if ((map != null) && (StringUtils.isNotEmpty(key)) &&
(map.get(key) != null)) {
return (Integer)map.get(key);
}

return null;
}

public static boolean isNumber(String str)
{
return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
}

public static String getNumbers(String content)
{
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
return matcher.group(0);
}
return "";
}

public static Double parseDoubleByString(String str) throws NumberFormatException {
if ((str != null) && (!"".equals(str.trim()))) {
return Double.valueOf(str);
}
return null;
}

public static Integer parseIntegerByString(String str) throws NumberFormatException {
if ((str != null) && (!"".equals(str.trim()))) {
return Integer.valueOf(str);
}
return null;
}

public static String subString(String str, int subBytes)
{
int bytes = 0;
for (int i = 0; i < str.length(); i++) {
if (bytes == subBytes) {
return str.substring(0, i);
}
char c = str.charAt(i);
if (c < 'Ā')
{
bytes++;
}
else {
bytes += 2;
if (bytes - subBytes == 1) {
return str.substring(0, i);
}
}
}
return str;
}

public static String subBatch(String batch)
{
if (checkNull(batch)) {
return "";
}
int temp = Integer.parseInt(batch);
return String.valueOf(temp);
}

public static void main(String[] args) {
System.out.println(getCheckCode());
System.out.println(checkEmail("xxxxx@sina.cn"));
System.out.println(checkEmail("xxxx@qq.com"));
System.out.println(checkEmail("xxxx@qq.com"));
System.out.println(checkEmail("xx@126.com"));
System.out.println(checkEmail("xxxx@opi-corp.com"));
}

public static String appendBatch(String omsBatch)
{
if (omsBatch.length() > 6) {
return omsBatch;
}
return new StringBuilder().append(BATCHAPPEND[omsBatch.length()]).append(omsBatch).toString();
}

public static String getCheckCode()
{
String nowTime = String.valueOf(System.currentTimeMillis());
return nowTime.substring(nowTime.length() - 4);
}

public static boolean checkEmail(String email)
{
boolean flag = false;
try
{
String check = "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$";
Pattern regex = Pattern.compile(check);
Matcher matcher = regex.matcher(email);
flag = matcher.matches();
} catch (Exception e) {
flag = false;
}
return flag;
}

public static boolean checkPhone(String phone)
{
boolean flag = false;
try {
Pattern p = Pattern.compile("^[0-9]{1,15}$");
Matcher m = p.matcher(phone);
flag = m.matches();
} catch (Exception e) {
flag = false;
}
return flag;
}

public static String nullToString(String str)
{
return str == null ? "" : str;
}

public static String cleanHTML(String body)
{
return Jsoup.clean(body, whitelist);
}

public static String specialFilter(String str)
throws PatternSyntaxException
{
String regEx = "[`~!@#$%^&*()+=|{}':;',//[//]<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.replaceAll("").trim();
}

public static String stringHide(String str)
{
if (checkEmail(str)) {
int index = str.indexOf(64);
String prex = str.substring(0, index);
String sufx = str.substring(index);
StringBuilder strBuilder = new StringBuilder();
if (prex.length() < 3) {
strBuilder.append("***").append(sufx);
} else {
strBuilder.append(prex.charAt(0));
for (int i = 1; i < prex.length() - 1; i++) {
strBuilder.append("*");
}
strBuilder.append(prex.charAt(prex.length() - 1));
strBuilder.append(sufx);
}
return strBuilder.toString();
}if (checkPhone(str)) {
int begin = 3;
int end = 3;
StringBuilder strBuilder = new StringBuilder();
if (str.length() > 5) {
for (int i = 0; i < begin; i++) {
strBuilder.append(str.charAt(i));
}
for (int i = begin; i < str.length() - end; i++) {
strBuilder.append("*");
}
for (int i = str.length() - end; i < str.length(); i++)
strBuilder.append(str.charAt(i));
}
else {
for (int i = 0; i < str.length(); i++) {
if ((i == 0) || (i == str.length() - 1))
strBuilder.append(str.charAt(i));
else {
strBuilder.append("*");
}
}
}
return strBuilder.toString();
}
return str;
}
}