package com.yihu.utils; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Random; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; import com.coreframework.util.AppConfig; public class StringUtil { /** * Code节点名称 */ private static final String CODE = "Code"; /** * Message节点名称 */ private static final String MESSAGE = "Message"; public static String createUUID() { return UUID.randomUUID().toString() .replaceAll("-", "").toUpperCase(); } public static String formatXML(String retStr) throws Exception { String res = null; Document doc = null; try { doc = DocumentHelper.parseText(retStr); } catch (DocumentException e) { e.printStackTrace(); throw e; } XMLWriter output = null; OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("GBK"); format.setNewLineAfterDeclaration(false); format.setExpandEmptyElements(true); format.setSuppressDeclaration(true); OutputStream outputStream = new ByteArrayOutputStream(); XMLWriter writer = new XMLWriter(outputStream, format); writer.write(doc); writer.close(); res = outputStream.toString(); return res; } // String is null or not public static boolean isEmpty(String str) { if ("".equals(str) || "null".equals(str) || "NULL".equals(str) || str == null || "undefined".equals(str)){ return true; } if ("".equals(str.trim())){ return true; } return false; } // List is null or not public static boolean isEmpty(List list) { if (list != null && list.size() > 0){ return false; } return true; } // Map is null or not public static boolean isEmpty(Map map) { if (map == null || map.size() == 0){ return true; } return false; } // Object is null or not public static boolean isEmpty(Object obj) { if (obj == null){ return true; } return false; } // to do empty string public static String chEmptyStr(String str) { if ("".equals(str) || "null".equals(str) || "NULL".equals(str) || str == null){ return ""; } return str.trim(); } public static String chMap(Map map, String key) { String res = ""; if (map != null) { return chEmptyStr(map.get(key) + ""); } return res; } // chang clob type to String // public static String clobToStr(Clob clob) throws Exception { // return (clob != null ? clob.getSubString(1, (int) clob.length()) : ""); // } public static int parseInt(Integer i) { if (i == null){ return 0; } return i; } public static int parseInt(Integer i, int v) { if (i == null){ return v; } return i; } public static boolean isNum(String str) { Pattern pattern = Pattern.compile("[0-9]*"); Matcher isNum = pattern.matcher(str); if (isNum.matches()){ return true; } return false; } public static boolean isEnNum(String str) { String first = str.substring(0, 1); String last = str.substring(str.length() - 1, str.length()); String middle = str.substring(1, str.length() - 1); if (StringUtil.isNum(middle)) { if (StringUtil.isNum(first) || Pattern.compile("(?i)[a-zA-Z]").matcher(first).find()) { if (StringUtil.isNum(last) || Pattern.compile("(?i)[a-zA-Z]").matcher(last).find()) { if (!"a".equals(first) && !"A".equals(first)) { return true; } } } } return false; } public static boolean checkDate(String date) { Date d = null; boolean res = false; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); sdf.setLenient(false); try { d = sdf.parse(date); res = true; } catch (Exception e) { } return res; } public static Integer[] getBirthYMD(String identityID) { int y = 0, m = 0, d = 0; if (identityID.length() == 15) { y = Integer.parseInt("19" + identityID.substring(6, 8)); m = Integer.parseInt(identityID.substring(8, 10)); d = Integer.parseInt(identityID.substring(10, 12)); } if (identityID.length() == 18) { y = Integer.parseInt(identityID.substring(6, 10)); m = Integer.parseInt(identityID.substring(10, 12)); d = Integer.parseInt(identityID.substring(12, 14)); } Integer[] a = new Integer[3]; a[0] = y; a[1] = m; a[2] = d; return a; } public static Integer[] getBirthYMDbyBirth(String birth) { int y = 0, m = 0, d = 0; if (!StringUtil.isEmpty(birth)) { y = Integer.parseInt(birth.substring(0, 4)); m = Integer.parseInt(birth.substring(5, 7)); d = Integer.parseInt(birth.substring(8, 10)); } Integer[] a = new Integer[3]; a[0] = y; a[1] = m; a[2] = d; return a; } public static boolean isNeedVirtual(String cardno) { if (StringUtil.isEmpty(cardno)){ return true; } if (cardno.trim().length() != 8 && cardno.trim().length() != 12){ return true; } return false; } public static String getWeek(int weekindex) { switch (weekindex) { case 0: return "星期天"; case 1: return "星期一"; case 2: return "星期二"; case 3: return "星期三"; case 4: return "星期四"; case 5: return "星期五"; case 6: return "星期六"; default: return ""; } } /** * 判断字符串是否存在 * * @param str * @param arr * 英文逗号隔开的字符串数组 * @return */ public static boolean strInArr(String str, String[] arr) { boolean b = false; if (arr != null && arr.length > 0) { for (String s : arr) { if (str.equals(s)) { b = true; break; } } } return b; } /** * 去掉数组第一个值 * * @param arr * @return */ public static String deleteFirstNode(String[] arr) { if (arr.length < 2){ return null; } String[] arrNew = new String[arr.length - 1]; for (int i = 0; i < arr.length; i++) { if (i == 0) { continue; } arrNew[i - 1] = arr[i]; } String ts = ""; for (String s : arrNew) { ts += s + ","; } return ts.substring(0, ts.length() - 1); } public static String delHtmlTagaaa(String str1) { if(StringUtil.isEmpty(str1)) { return ""; } String str = ""; str = str1; str = str.replaceAll("]+>", ""); // 剔出了的标签 str = str.replace(" ", ""); //str = str.replace(".", ""); //str = str.replace("\"", "‘"); //str = str.replace("'", "‘"); str=str.replaceAll("'", "\""); return str; } public static String editHtmlTag(String str) { if(StringUtil.isEmpty(str)) { return ""; } return str.replaceAll("'", "\""); } public static String retrim(String str) { if(StringUtil.isEmpty(str)) { return ""; } String temp=str.trim(); temp=temp.replaceAll(" ", ""); temp=temp.replaceAll(" ", ""); return temp; } public static String getPermitway(String waystatus) { String a = waystatus.substring(0,1); String b = waystatus.substring(1,2); String c = waystatus.substring(2,3); List list = new ArrayList(); if("1".equals(a)) { list.add("1"); } if("1".equals(b)) { list.add("2"); } if("1".equals(c)) { list.add("3"); } String temp=""; if(list!=null&&list.size()>0) { for(String str : list) { temp+=str+","; } temp = temp.substring(0,temp.length()-1); } return temp; } /** * to get standard dept code with four numbers * * @param sd * @return */ public static String getStandardDeptId(String sd) { if(!StringUtil.isEmpty(sd)) { String zero = ""; for(int i=0;i<4-sd.length();i++) { zero+="0"; } return zero+sd; } return ""; } public static JSONObject jsonResult(){ return jsonResult(1,"SUCCESS"); } public static JSONObject jsonResult(int code, String message){ JSONObject j=new JSONObject(); j.put(CODE, code); j.put(MESSAGE, message); return j; } public static JSONObject jsonResult(int code, String message, Object result){ JSONObject j=new JSONObject(); j.put(CODE, code); j.put(MESSAGE, message); Object rs = result; if(result instanceof com.common.json.JSONObject || result instanceof com.common.json.JSONArray){ rs = result.toString(); } j.put("Result", rs); return j; } public static void write(HttpServletResponse response,String value) { try { response.setContentType("text/html;charset=UTF-8"); response.getWriter().write(value); } catch (IOException e) { e.printStackTrace(); } } public static String getRandomNumber(int length){ Random random = new Random(); random.setSeed(System.currentTimeMillis()); String code= Integer.toString(Math.abs(random.nextInt())); if(code.length()>length){ code=code.substring(0,length); }else{ while(code.length()