001: package org.claros.commons.utility;
002:
003: import java.net.URLDecoder;
004: import java.util.Locale;
005:
006: public class Utility {
007:
008: private final static char trChars[] = { '\u0131', '\u0130',
009: '\u015F', '\u015E', '\u011F', '\u011E', '\u00fd', '\u00dd',
010: '\u00fe', '\u00de', '\u00f0', '\u00d0', '\u00E7', '\u00C7',
011: '\u00FC', '\u00DC', '\u00F6', '\u00D6' };
012: private final static char enChars[] = { 'i', 'I', 's', 'S', 'g',
013: 'G', 'i', 'I', 's', 'S', 'g', 'G', 'c', 'C', 'u', 'U', 'o',
014: 'O' };
015: private final static char enLowerCaseChars[] = { 'i', 'i', 's',
016: 's', 'g', 'g', 'i', 'i', 's', 's', 'g', 'g', 'c', 'c', 'u',
017: 'u', 'o', 'o' };
018: private final static String trUnicodes[] = { "ı", "İ",
019: "ş", "Ş", "ğ", "Ğ", "ı", "İ",
020: "ş", "Ş", "ğ", "Ğ", "ç", "Ç",
021: "ü", "Ü", "ö", "Ö" };
022:
023: private final static char trDirtyChars[] = { '\u00fd', '\u00dd',
024: '\u00fe', '\u00de', '\u00f0', '\u00d0' };
025: private final static char trCleanChars[] = { '\u0131', '\u0130',
026: '\u015F', '\u015E', '\u011F', '\u011E' };
027:
028: /**
029: *
030: * @param str
031: * @param from
032: * @param to
033: * @return
034: */
035: public static String replaceAllOccurances(String str, String from,
036: String to) {
037: if (str == null || str.length() == 0) {
038: return str;
039: } else if (str.length() == 1 && str.equals(from)) {
040: return to;
041: } else if (str.length() == 1 && !str.equals(from)) {
042: return str;
043: }
044: int j = -1;
045: while ((j = str.indexOf(from)) >= 0) {
046: str = str.substring(0, j) + (char) 5
047: + str.substring(j + from.length());
048: }
049:
050: int i = -1;
051: while ((i = str.indexOf((char) 5)) >= 0) {
052: str = str.substring(0, i) + to + str.substring(i + 1);
053: }
054:
055: return str;
056: }
057:
058: /**
059: *
060: * @param str
061: * @param trimStr
062: * @return
063: */
064: public static String extendedTrim(String str, String trimStr) {
065: if (str == null || str.length() == 0)
066: return str;
067: for (str = str.trim(); str.startsWith(trimStr); str = str
068: .substring(trimStr.length()).trim())
069: ;
070: for (; str.endsWith(trimStr); str = str.substring(0,
071: str.length() - trimStr.length()).trim())
072: ;
073: return str;
074: }
075:
076: /**
077: *
078: * @param number
079: * @return
080: */
081: public static Object checkDecimalFormat(Object number) {
082: String str = "-";
083: try {
084: str = number.toString();
085: int posDot = str.indexOf(".");
086: int posComma = str.indexOf(",");
087:
088: if (posComma > posDot) {
089: str = Utility.replaceAllOccurances(str, ".", "");
090: str = Utility.replaceAllOccurances(str, ",", ".");
091: } else if (posComma == -1 && posDot > 0) {
092: int lastPosDot = str.lastIndexOf(".");
093: if (posDot != lastPosDot) {
094: str = Utility.replaceAllOccurances(str, ".", "");
095: }
096: }
097: } catch (Exception e) {
098: str = "-";
099: }
100: return str;
101: }
102:
103: /**
104: *
105: * @param str
106: * @return
107: */
108: public static String doCharsetCorrections(String str) {
109: if (str == null)
110: return "";
111:
112: String extraChars[] = { "\u00FD", "\u00DD", "\u00FE", "\u00DE",
113: "\u00F0", "\u00D0" };
114: String unicode[] = { "\u0131", "\u0130", "\u015F", "\u015E",
115: "\u011F", "\u011E" };
116: for (int i = 0; i < extraChars.length; i++) {
117: while (str.indexOf(extraChars[i]) != -1) {
118: String tmp = str;
119: str = tmp.substring(0, tmp.indexOf(extraChars[i]))
120: + unicode[i]
121: + tmp.substring(tmp.indexOf(extraChars[i]) + 1,
122: tmp.length());
123: }
124: }
125: return str;
126: }
127:
128: /**
129: *
130: * @param input
131: * @return
132: */
133: public static String htmlSpecialChars(String input) {
134: StringBuffer filtered;
135: try {
136: filtered = new StringBuffer(input.length());
137: char c;
138: for (int i = 0; i < input.length(); i++) {
139: c = input.charAt(i);
140: if (c == '<') {
141: filtered.append("<");
142: } else if (c == '>') {
143: filtered.append(">");
144: } else if (c == '"') {
145: filtered.append(""");
146: } else if (c == '&') {
147: filtered.append("&");
148: } else {
149: filtered.append(c);
150: }
151: }
152: } catch (Exception e) {
153: return input;
154: }
155: return (filtered.toString());
156: }
157:
158: /**
159: *
160: * @param a
161: * @return
162: */
163: public static String convertTRCharsToHtmlSafe(String str) {
164: if ((str == null) || (str.length() == 0))
165: return "";
166:
167: int pos = -1;
168: for (int i = 0; i < trChars.length; i++) {
169: while ((pos = str.indexOf(trChars[i])) != -1) {
170: str = str.substring(0, pos) + trUnicodes[i]
171: + str.substring(pos + 1, str.length());
172: }
173: }
174: return str;
175: }
176:
177: /**
178: *
179: * @param a
180: * @return
181: */
182: public static String updateTRChars(String str) {
183: if ((str == null) || (str.length() == 0))
184: return "";
185: String ret = "";
186: try {
187: ret = javax.mail.internet.MimeUtility.decodeText(str);
188: } catch (Exception e) {
189: }
190: String strLowerCase = ret.toLowerCase(new Locale("en", "US"));
191: if (strLowerCase.startsWith("=?iso-8859-9?q?")) {
192: ret = ret.substring(15);
193: if (strLowerCase.endsWith("?=")) {
194: ret = ret.substring(0, ret.length() - 2);
195: } else {
196: int pos = -1;
197: while ((pos = ret.indexOf("?=")) != -1) {
198: ret = ret.substring(0, pos)
199: + ret.substring(pos + 2, ret.length());
200: }
201: }
202: try {
203: ret = ret.replace('=', '%');
204: ret = URLDecoder.decode(ret, "iso-8859-9");
205: } catch (Exception ex) {
206: }
207: }
208: for (int i = 0; i < trDirtyChars.length; i++) {
209: int pos = -1;
210: while ((pos = ret.indexOf(trDirtyChars[i])) != -1) {
211: ret = ret.substring(0, pos) + trCleanChars[i]
212: + ret.substring(pos + 1, ret.length());
213: }
214: }
215: return ret;
216: }
217:
218: /**
219: *
220: * @param a
221: * @return
222: */
223: public static String convertTRCharsToENChars(String str) {
224: if ((str == null) || (str.length() == 0))
225: return "";
226:
227: int pos = -1;
228: for (int i = 0; i < trChars.length; i++) {
229: while ((pos = str.indexOf(trChars[i])) != -1) {
230: str = str.substring(0, pos) + enChars[i]
231: + str.substring(pos + 1, str.length());
232: }
233: }
234: return str;
235: }
236:
237: /**
238: *
239: * @param a
240: * @return
241: */
242: public static String convertTRCharsToENLowerCaseChars(String str) {
243: if ((str == null) || (str.length() == 0))
244: return "";
245:
246: int pos = -1;
247: for (int i = 0; i < trChars.length; i++) {
248: while ((pos = str.indexOf(trChars[i])) != -1) {
249: str = str.substring(0, pos) + enLowerCaseChars[i]
250: + str.substring(pos + 1, str.length());
251: }
252: }
253: return str;
254: }
255: }
|