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.configuration.PropertyFile;
009: import org.claros.commons.db.DbConfigList;
010:
011: import com.jenkov.mrpersister.PersistenceManager;
012: import com.jenkov.mrpersister.itf.IGenericDao;
013:
014: public class Utility {
015: public static PersistenceManager persistMan = new PersistenceManager();
016: private static Log log = LogFactory.getLog(Utility.class);
017: public static String msnTransport = "";
018: public static String yahooTransport = "";
019: public static String icqTransport = "";
020: public static String aolTransport = "";
021:
022: static {
023: try {
024: msnTransport = PropertyFile.getConfiguration(
025: "/config/config.xml").getString(
026: "chat.msn-transport");
027: } catch (Exception e) {
028: log.debug("unable to get msn chat domain", e);
029: }
030: try {
031: yahooTransport = PropertyFile.getConfiguration(
032: "/config/config.xml").getString(
033: "chat.yahoo-transport");
034: } catch (Exception e) {
035: log.debug("unable to get yahoo chat domain", e);
036: }
037: try {
038: icqTransport = PropertyFile.getConfiguration(
039: "/config/config.xml").getString(
040: "chat.icq-transport");
041: } catch (Exception e) {
042: log.debug("unable to get icq chat domain", e);
043: }
044: try {
045: aolTransport = PropertyFile.getConfiguration(
046: "/config/config.xml").getString(
047: "chat.aol-transport");
048: } catch (Exception e) {
049: log.debug("unable to get aol chat domain", e);
050: }
051: }
052:
053: public static IGenericDao getDbConnection() throws Exception {
054: return getDbConnection("file");
055: }
056:
057: public static IGenericDao getTxnDbConnection() throws Exception {
058: return getDbConnection("file");
059: }
060:
061: public static IGenericDao getDbConnection(String name)
062: throws Exception {
063: BasicDataSource bs = (BasicDataSource) DbConfigList
064: .getDataSourceById(name);
065: // log.debug("Number active: " + bs.getNumActive());
066: // log.debug("Number idle: " + bs.getNumIdle());
067:
068: Connection con = bs.getConnection();
069: return persistMan.getGenericDaoFactory().createDao(con);
070: }
071:
072: public static IGenericDao getTxnDbConnection(String name)
073: throws Exception {
074: Connection con = DbConfigList.getDataSourceById(name)
075: .getConnection();
076: con.setAutoCommit(false);
077: return persistMan.getGenericDaoFactory().createDao(con);
078: }
079:
080: public static String replaceAllOccurances(String str, String from,
081: String to) {
082: if (str == null || str.length() == 0) {
083: return str;
084: } else if (str.length() == 1 && str.equals(from)) {
085: return to;
086: } else if (str.length() == 1 && !str.equals(from)) {
087: return str;
088: }
089: int j = -1;
090: while ((j = str.indexOf(from)) >= 0) {
091: str = str.substring(0, j) + (char) 5
092: + str.substring(j + from.length());
093: }
094:
095: int i = -1;
096: while ((i = str.indexOf((char) 5)) >= 0) {
097: str = str.substring(0, i) + to + str.substring(i + 1);
098: }
099:
100: return str;
101: }
102:
103: public static String extendedTrim(String str, String trimStr) {
104: if (str == null || str.length() == 0)
105: return str;
106: for (str = str.trim(); str.startsWith(trimStr); str = str
107: .substring(trimStr.length()).trim())
108: ;
109: for (; str.endsWith(trimStr); str = str.substring(0,
110: str.length() - trimStr.length()).trim())
111: ;
112: return str;
113: }
114:
115: public static Object checkDecimalFormat(Object number) {
116: String str = "-";
117: try {
118: str = number.toString();
119: int posDot = str.indexOf(".");
120: int posComma = str.indexOf(",");
121:
122: /*
123: * Double.parseDouble ile bu çakar. Nokta ile virgülün yer değiştirmesi lazım
124: */
125: if (posComma > posDot) {
126: str = Utility.replaceAllOccurances(str, ".", "");
127: str = Utility.replaceAllOccurances(str, ",", ".");
128: } else if (posComma == -1 && posDot > 0) {
129: int lastPosDot = str.lastIndexOf(".");
130: if (posDot != lastPosDot) {
131: str = Utility.replaceAllOccurances(str, ".", "");
132: }
133: }
134: } catch (Exception e) {
135: str = "-";
136: }
137: return str;
138: }
139:
140: public static String doCharsetCorrections(String str) {
141: if (str == null)
142: return "";
143:
144: String extraChars[] = { "\u00FD", "\u00DD", "\u00FE", "\u00DE",
145: "\u00F0", "\u00D0" };
146: String unicode[] = { "\u0131", "\u0130", "\u015F", "\u015E",
147: "\u011F", "\u011E" };
148: for (int i = 0; i < extraChars.length; i++) {
149: while (str.indexOf(extraChars[i]) != -1) {
150: String tmp = str;
151: str = tmp.substring(0, tmp.indexOf(extraChars[i]))
152: + unicode[i]
153: + tmp.substring(tmp.indexOf(extraChars[i]) + 1,
154: tmp.length());
155: }
156: }
157: return str;
158: }
159:
160: public static String htmlSpecialChars(String input) {
161: StringBuffer filtered;
162: try {
163: filtered = new StringBuffer(input.length());
164: char c;
165: for (int i = 0; i < input.length(); i++) {
166: c = input.charAt(i);
167: if (c == '<') {
168: filtered.append("<");
169: } else if (c == '>') {
170: filtered.append(">");
171: } else if (c == '"') {
172: filtered.append(""");
173: } else if (c == '&') {
174: filtered.append("&");
175: } else {
176: filtered.append(c);
177: }
178: }
179: } catch (Exception e) {
180: return input;
181: }
182: return (filtered.toString());
183: }
184:
185: }
|