1 package org.claros.chat.utility; 2 3 import java.sql.Connection ; 4 5 import org.apache.commons.dbcp.BasicDataSource; 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.claros.commons.db.DbConfigList; 9 10 import com.jenkov.mrpersister.PersistenceManager; 11 import com.jenkov.mrpersister.itf.IGenericDao; 12 13 public class Utility { 14 public static PersistenceManager persistMan = new PersistenceManager(); 15 private static Log log = LogFactory.getLog(Utility.class); 16 17 public static IGenericDao getDbConnection() throws Exception { 18 return getDbConnection("file"); 19 } 20 21 public static IGenericDao getTxnDbConnection() throws Exception { 22 return getDbConnection("file"); 23 } 24 25 public static IGenericDao getDbConnection(String name) throws Exception { 26 BasicDataSource bs = (BasicDataSource)DbConfigList.getDataSourceById(name); 27 log.debug("Number active: " + bs.getNumActive()); 28 log.debug("Number idle: " + bs.getNumIdle()); 29 30 Connection con = bs.getConnection(); 31 return persistMan.getGenericDaoFactory().createDao(con); 32 } 33 34 public static IGenericDao getTxnDbConnection(String name) throws Exception { 35 Connection con = DbConfigList.getDataSourceById(name).getConnection(); 36 con.setAutoCommit(false); 37 return persistMan.getGenericDaoFactory().createDao(con); 38 } 39 40 public static String replaceAllOccurances(String str, String from, String to) { 41 if (str == null || str.length() == 0) { 42 return str; 43 } else if (str.length() == 1 && str.equals(from)) { 44 return to; 45 } else if (str.length() == 1 && !str.equals(from)) { 46 return str; 47 } 48 int j = -1; 49 while ((j = str.indexOf(from)) >= 0) { 50 str = str.substring(0, j) + (char)5 + str.substring(j + from.length()); 51 } 52 53 int i = -1; 54 while ((i = str.indexOf((char)5)) >= 0) { 55 str = str.substring(0, i) + to + str.substring(i + 1); 56 } 57 58 return str; 59 } 60 61 public static String extendedTrim(String str, String trimStr) { 62 if (str == null || str.length() == 0) 63 return str; 64 for (str = str.trim(); str.startsWith(trimStr); str = str.substring(trimStr.length()).trim()); 65 for (; str.endsWith(trimStr); str = str.substring(0, str.length() - trimStr.length()).trim()); 66 return str; 67 } 68 69 public static Object checkDecimalFormat(Object number) { 70 String str = "-"; 71 try { 72 str = number.toString(); 73 int posDot = str.indexOf("."); 74 int posComma = str.indexOf(","); 75 76 79 if (posComma > posDot) { 80 str = Utility.replaceAllOccurances(str, ".", ""); 81 str = Utility.replaceAllOccurances(str, ",", "."); 82 } else if (posComma == -1 && posDot > 0) { 83 int lastPosDot = str.lastIndexOf("."); 84 if (posDot != lastPosDot) { 85 str = Utility.replaceAllOccurances(str, ".", ""); 86 } 87 } 88 } catch (Exception e) { 89 str = "-"; 90 } 91 return str; 92 } 93 94 public static String doCharsetCorrections(String str) { 95 if (str == null) return ""; 96 97 String extraChars[] = {"\u00FD","\u00DD","\u00FE","\u00DE","\u00F0","\u00D0"}; 98 String unicode[] = {"\u0131", "\u0130", "\u015F", "\u015E", "\u011F", "\u011E"}; 99 for (int i=0;i<extraChars.length;i++) { 100 while (str.indexOf(extraChars[i]) != -1) { 101 String tmp = str; 102 str = tmp.substring(0, tmp.indexOf(extraChars[i])) 103 + unicode[i] + tmp.substring (tmp.indexOf(extraChars[i])+1, tmp.length()); 104 } 105 } 106 return str; 107 } 108 109 public static String htmlSpecialChars(String input) { 110 StringBuffer filtered; 111 try { 112 filtered = new StringBuffer (input.length()); 113 char c; 114 for (int i = 0; i < input.length(); i++) { 115 c = input.charAt(i); 116 if (c == '<') { 117 filtered.append("<"); 118 } else if (c == '>') { 119 filtered.append(">"); 120 } else if (c == '"') { 121 filtered.append("""); 122 } else if (c == '&') { 123 filtered.append("&"); 124 } else { 125 filtered.append(c); 126 } 127 } 128 } catch (Exception e) { 129 return input; 130 } 131 return (filtered.toString()); 132 } 133 134 } 135 | Popular Tags |