KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > claros > chat > utility > Utility


1 package org.claros.chat.utility;
2
3 import java.sql.Connection JavaDoc;
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 JavaDoc {
18         return getDbConnection("file");
19     }
20
21     public static IGenericDao getTxnDbConnection() throws Exception JavaDoc {
22         return getDbConnection("file");
23     }
24
25     public static IGenericDao getDbConnection(String JavaDoc name) throws Exception JavaDoc {
26         BasicDataSource bs = (BasicDataSource)DbConfigList.getDataSourceById(name);
27         log.debug("Number active: " + bs.getNumActive());
28         log.debug("Number idle: " + bs.getNumIdle());
29         
30         Connection JavaDoc con = bs.getConnection();
31         return persistMan.getGenericDaoFactory().createDao(con);
32     }
33     
34     public static IGenericDao getTxnDbConnection(String JavaDoc name) throws Exception JavaDoc {
35         Connection JavaDoc con = DbConfigList.getDataSourceById(name).getConnection();
36         con.setAutoCommit(false);
37         return persistMan.getGenericDaoFactory().createDao(con);
38     }
39
40     public static String JavaDoc replaceAllOccurances(String JavaDoc str, String JavaDoc from, String JavaDoc 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 JavaDoc extendedTrim(String JavaDoc str, String JavaDoc 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 JavaDoc checkDecimalFormat(Object JavaDoc number) {
70         String JavaDoc str = "-";
71         try {
72             str = number.toString();
73             int posDot = str.indexOf(".");
74             int posComma = str.indexOf(",");
75
76             /*
77              * Double.parseDouble ile bu çakar. Nokta ile virgülün yer değiştirmesi lazım
78              */

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 JavaDoc e) {
89             str = "-";
90         }
91         return str;
92     }
93
94     public static String JavaDoc doCharsetCorrections(String JavaDoc str) {
95         if (str == null) return "";
96         
97         String JavaDoc extraChars[] = {"\u00FD","\u00DD","\u00FE","\u00DE","\u00F0","\u00D0"};
98         String JavaDoc 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 JavaDoc 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 JavaDoc htmlSpecialChars(String JavaDoc input) {
110         StringBuffer JavaDoc filtered;
111         try {
112             filtered = new StringBuffer JavaDoc(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("&lt;");
118                 } else if (c == '>') {
119                     filtered.append("&gt;");
120                 } else if (c == '"') {
121                     filtered.append("&quot;");
122                 } else if (c == '&') {
123                     filtered.append("&amp;");
124                 } else {
125                     filtered.append(c);
126                 }
127             }
128         } catch (Exception JavaDoc e) {
129             return input;
130         }
131         return (filtered.toString());
132     }
133
134 }
135
Popular Tags