001: /*
002: *
003: * Copyright (c) 2004 SourceTap - www.sourcetap.com
004: *
005: * The contents of this file are subject to the SourceTap Public License
006: * ("License"); You may not use this file except in compliance with the
007: * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
010: * the specific language governing rights and limitations under the License.
011: *
012: * The above copyright notice and this permission notice shall be included
013: * in all copies or substantial portions of the Software.
014: *
015: */
016:
017: package com.sourcetap.sfa.util;
018:
019: import java.text.DateFormat;
020: import java.util.ArrayList;
021: import java.util.Date;
022: import java.util.StringTokenizer;
023:
024: import org.ofbiz.base.util.Debug;
025:
026: /**
027: * DOCUMENT ME!
028: *
029: */
030: public class StringHelper {
031: public static final String module = StringHelper.class.getName();
032:
033: /**
034: * DOCUMENT ME!
035: *
036: * @param origString
037: * @param removeString
038: * @param insertString
039: *
040: * @return
041: */
042: public static String replaceAll(String origString,
043: String removeString, String insertString) {
044: Debug.logVerbose("[StringHelper.replaceAll] Args: "
045: + origString + "/" + removeString + "/" + insertString,
046: module);
047:
048: StringBuffer buf = new StringBuffer(origString);
049:
050: int loopCounter = 0;
051: int completePos = 0;
052: int replaceStartPos = buf.toString().indexOf(removeString,
053: completePos);
054: int replaceEndPos = 0;
055:
056: while (replaceStartPos > 0) {
057:
058: // Calculate the position of the first character NOT to be replaced after the replace start position.
059: replaceEndPos = replaceStartPos + removeString.length();
060:
061: // Perform the string replacement.
062: buf.replace(replaceStartPos, replaceEndPos, insertString);
063:
064: // Figure out where to start the search from next time through the loop. This will be the replace start position
065: // plus the length of the insert string.
066: completePos = replaceStartPos + insertString.length();
067:
068: // Figure out the replace start position for next time through.
069: replaceStartPos = buf.toString().indexOf(removeString,
070: completePos);
071:
072: if (loopCounter++ > 1000) {
073: Debug.logWarning(
074: "[StringHelper.replaceAll] Loop detected.",
075: module);
076:
077: return buf.toString();
078: }
079: }
080:
081: Debug.logVerbose(
082: "[StringHelper.replaceAll] String after replacement: "
083: + buf.toString(), module);
084:
085: return buf.toString();
086: }
087:
088: public static String valueOf(Object o) {
089: if (o == null)
090: return "";
091: else
092: return String.valueOf(o);
093: }
094:
095: /**
096: * Converts a Java variable name to a field description.
097: * The naming conventions used to allow for this are as follows: a Java name (ejb or field) is in all
098: * lower case letters, except the letter at the beginning of each word (for example:
099: * NeatEntityName or RandomFieldName).
100: * @param javaName The Java variable name
101: * @return The description
102: */
103: public static String javaNameToDescription(String javaName) {
104: if (javaName == null)
105: return null;
106: if (javaName.length() <= 0)
107: return "";
108: StringBuffer description = new StringBuffer();
109:
110: description.append(Character.toUpperCase(javaName.charAt(0)));
111: int namePos = 1;
112:
113: while (namePos < javaName.length()) {
114: char curChar = javaName.charAt(namePos);
115:
116: if (Character.isUpperCase(curChar))
117: description.append(' ');
118: description.append(curChar);
119: namePos++;
120: }
121:
122: return description.toString();
123: }
124:
125: /**
126: * Converts text to HTML. Replace new lines in text with BR tags
127: * and replace leading white space with html space characters
128: * @param str - String containing the plain text.
129: * @return the HTML verion of the string
130: */
131: public final static String textToHtml(String str) {
132:
133: if (str == null)
134: return str;
135:
136: StringBuffer buf = new StringBuffer();
137: int len = str.length();
138: boolean newline = true;
139: for (int i = 0; i < len; i++) {
140: char c = str.charAt(i);
141: if (c == '\n') {
142: buf.append("<br>");
143: newline = true;
144: } else if ((c == ' ') && newline) {
145: buf.append(" ");
146: } else {
147: newline = false;
148: buf.append(c);
149: }
150: }
151:
152: return buf.toString();
153:
154: }
155:
156: public static String formatDate(Date date) {
157: if (date == null)
158: return "";
159: DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
160: return df.format(date);
161:
162: }
163:
164: public static String convertDate(String inDate) {
165: StringTokenizer st = new StringTokenizer(inDate, " ");
166: if (st.countTokens() != 3)
167: st = new StringTokenizer(inDate, "/");
168:
169: if (st.countTokens() != 3)
170: return inDate;
171:
172: String month = st.nextToken();
173: String day = st.nextToken();
174: String year = st.nextToken();
175:
176: try {
177: int year4 = Integer.parseInt(year);
178:
179: if ((year4 > 4) && (year4 < 100))
180: year4 = year4 + 1900;
181: else if (year4 < 100)
182: year4 = year4 + 2000;
183:
184: year = String.valueOf(year4);
185: return month + " " + day + " " + year;
186: } catch (Exception e) {
187: return inDate;
188: }
189: }
190:
191: public static ArrayList splitCityStateZip(String input) {
192: ArrayList retList = new ArrayList();
193: retList.add(null);
194: retList.add(null);
195: retList.add(null);
196: retList.add(null);
197:
198: try {
199: ArrayList flds = new ArrayList();
200: StringTokenizer st = new StringTokenizer(input, " ");
201: while (st.hasMoreTokens()) {
202: flds.add(st.nextToken());
203: }
204: int numFlds = flds.size();
205: if (numFlds < 3) {
206: throw new IllegalArgumentException(
207: "field is not city state zip: " + input);
208: }
209: int pos = numFlds - 1;
210: String zip = (String) flds.get(pos);
211: if ((zip.length() < 5) && (pos > 0)) {
212: pos--;
213: zip = (String) flds.get(pos) + "-" + zip;
214: }
215: String s = zip.replaceAll("-", "");
216: int tstInt = Integer.parseInt(s); // if not an int, then an exception will be thrown
217:
218: String state = null;
219: if (pos > 0) {
220: pos--;
221: state = (String) flds.get(pos);
222: if (state.length() > 2) {
223: Debug.logVerbose("State is not 2 characters: "
224: + state, module);
225: throw new IllegalArgumentException(
226: "Invalid State Format");
227: }
228: }
229:
230: String city = (String) flds.get(0);
231: for (int i = 1; i < pos; i++) {
232: city = city + " " + (String) flds.get(i);
233: }
234:
235: retList.set(0, city);
236: retList.set(1, state);
237: retList.set(2, zip);
238: retList.set(3, "USA");
239: } catch (Exception e) {
240: Debug.logVerbose("unable to transform address field:"
241: + input + ": " + e.getMessage(), module);
242: retList.set(0, input);
243: }
244:
245: return retList;
246: }
247:
248: }
|