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