001: package org.claros.chat.utility;
002:
003: import java.sql.Connection;
004:
005: import org.apache.commons.dbcp.BasicDataSource;
006: import org.apache.commons.logging.Log;
007: import org.apache.commons.logging.LogFactory;
008: import org.claros.commons.db.DbConfigList;
009:
010: import com.jenkov.mrpersister.PersistenceManager;
011: import com.jenkov.mrpersister.itf.IGenericDao;
012:
013: public class Utility {
014: public static PersistenceManager persistMan = new PersistenceManager();
015: private static Log log = LogFactory.getLog(Utility.class);
016:
017: public static IGenericDao getDbConnection() throws Exception {
018: return getDbConnection("file");
019: }
020:
021: public static IGenericDao getTxnDbConnection() throws Exception {
022: return getDbConnection("file");
023: }
024:
025: public static IGenericDao getDbConnection(String name)
026: throws Exception {
027: BasicDataSource bs = (BasicDataSource) DbConfigList
028: .getDataSourceById(name);
029: log.debug("Number active: " + bs.getNumActive());
030: log.debug("Number idle: " + bs.getNumIdle());
031:
032: Connection con = bs.getConnection();
033: return persistMan.getGenericDaoFactory().createDao(con);
034: }
035:
036: public static IGenericDao getTxnDbConnection(String name)
037: throws Exception {
038: Connection con = DbConfigList.getDataSourceById(name)
039: .getConnection();
040: con.setAutoCommit(false);
041: return persistMan.getGenericDaoFactory().createDao(con);
042: }
043:
044: public static String replaceAllOccurances(String str, String from,
045: String to) {
046: if (str == null || str.length() == 0) {
047: return str;
048: } else if (str.length() == 1 && str.equals(from)) {
049: return to;
050: } else if (str.length() == 1 && !str.equals(from)) {
051: return str;
052: }
053: int j = -1;
054: while ((j = str.indexOf(from)) >= 0) {
055: str = str.substring(0, j) + (char) 5
056: + str.substring(j + from.length());
057: }
058:
059: int i = -1;
060: while ((i = str.indexOf((char) 5)) >= 0) {
061: str = str.substring(0, i) + to + str.substring(i + 1);
062: }
063:
064: return str;
065: }
066:
067: public static String extendedTrim(String str, String trimStr) {
068: if (str == null || str.length() == 0)
069: return str;
070: for (str = str.trim(); str.startsWith(trimStr); str = str
071: .substring(trimStr.length()).trim())
072: ;
073: for (; str.endsWith(trimStr); str = str.substring(0,
074: str.length() - trimStr.length()).trim())
075: ;
076: return str;
077: }
078:
079: public static Object checkDecimalFormat(Object number) {
080: String str = "-";
081: try {
082: str = number.toString();
083: int posDot = str.indexOf(".");
084: int posComma = str.indexOf(",");
085:
086: /*
087: * Double.parseDouble ile bu çakar. Nokta ile virgülün yer değiştirmesi lazım
088: */
089: if (posComma > posDot) {
090: str = Utility.replaceAllOccurances(str, ".", "");
091: str = Utility.replaceAllOccurances(str, ",", ".");
092: } else if (posComma == -1 && posDot > 0) {
093: int lastPosDot = str.lastIndexOf(".");
094: if (posDot != lastPosDot) {
095: str = Utility.replaceAllOccurances(str, ".", "");
096: }
097: }
098: } catch (Exception e) {
099: str = "-";
100: }
101: return str;
102: }
103:
104: public static String doCharsetCorrections(String str) {
105: if (str == null)
106: return "";
107:
108: String extraChars[] = { "\u00FD", "\u00DD", "\u00FE", "\u00DE",
109: "\u00F0", "\u00D0" };
110: String unicode[] = { "\u0131", "\u0130", "\u015F", "\u015E",
111: "\u011F", "\u011E" };
112: for (int i = 0; i < extraChars.length; i++) {
113: while (str.indexOf(extraChars[i]) != -1) {
114: String tmp = str;
115: str = tmp.substring(0, tmp.indexOf(extraChars[i]))
116: + unicode[i]
117: + tmp.substring(tmp.indexOf(extraChars[i]) + 1,
118: tmp.length());
119: }
120: }
121: return str;
122: }
123:
124: public static String htmlSpecialChars(String input) {
125: StringBuffer filtered;
126: try {
127: filtered = new StringBuffer(input.length());
128: char c;
129: for (int i = 0; i < input.length(); i++) {
130: c = input.charAt(i);
131: if (c == '<') {
132: filtered.append("<");
133: } else if (c == '>') {
134: filtered.append(">");
135: } else if (c == '"') {
136: filtered.append(""");
137: } else if (c == '&') {
138: filtered.append("&");
139: } else {
140: filtered.append(c);
141: }
142: }
143: } catch (Exception e) {
144: return input;
145: }
146: return (filtered.toString());
147: }
148:
149: }
|