Source Code Cross Referenced for StringHelper.java in  » ERP-CRM-Financial » SourceTap-CRM » com » sourcetap » sfa » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ERP CRM Financial » SourceTap CRM » com.sourcetap.sfa.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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("&nbsp;");
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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.