Source Code Cross Referenced for StringUtil.java in  » ERP-CRM-Financial » SourceTap-CRM » org » ofbiz » base » 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 » org.ofbiz.base.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: StringUtil.java,v 1.5 2003/11/17 21:20:27 ajzeneski Exp $
003:         *
004:         *  Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005:         *
006:         *  Permission is hereby granted, free of charge, to any person obtaining a
007:         *  copy of this software and associated documentation files (the "Software"),
008:         *  to deal in the Software without restriction, including without limitation
009:         *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
010:         *  and/or sell copies of the Software, and to permit persons to whom the
011:         *  Software is furnished to do so, subject to the following conditions:
012:         *
013:         *  The above copyright notice and this permission notice shall be included
014:         *  in all copies or substantial portions of the Software.
015:         *
016:         *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017:         *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018:         *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019:         *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020:         *  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021:         *  OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022:         *  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023:         */
024:        package org.ofbiz.base.util;
025:
026:        import java.io.UnsupportedEncodingException;
027:        import java.net.URLDecoder;
028:        import java.net.URLEncoder;
029:        import java.util.ArrayList;
030:        import java.util.HashMap;
031:        import java.util.Iterator;
032:        import java.util.List;
033:        import java.util.Map;
034:        import java.util.Set;
035:        import java.util.StringTokenizer;
036:
037:        /**
038:         * Misc String Utility Functions
039:         *
040:         * @author     <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
041:         * @version    $Revision: 1.5 $
042:         * @since      2.0
043:         */
044:        public class StringUtil {
045:
046:            public static final String module = StringUtil.class.getName();
047:
048:            /** 
049:             * Replaces all occurances of oldString in mainString with newString
050:             * @param mainString The original string
051:             * @param oldString The string to replace
052:             * @param newString The string to insert in place of the old
053:             * @return mainString with all occurances of oldString replaced by newString
054:             */
055:            public static String replaceString(String mainString,
056:                    String oldString, String newString) {
057:                if (mainString == null) {
058:                    return null;
059:                }
060:                if (oldString == null || oldString.length() == 0) {
061:                    return mainString;
062:                }
063:                if (newString == null) {
064:                    newString = "";
065:                }
066:
067:                int i = mainString.lastIndexOf(oldString);
068:
069:                if (i < 0)
070:                    return mainString;
071:
072:                StringBuffer mainSb = new StringBuffer(mainString);
073:
074:                while (i >= 0) {
075:                    mainSb.replace(i, i + oldString.length(), newString);
076:                    i = mainString.lastIndexOf(oldString, i - 1);
077:                }
078:                return mainSb.toString();
079:            }
080:
081:            /**
082:             * Creates a single string from a List of strings seperated by a delimiter.
083:             * @param list a list of strings to join
084:             * @param delim the delimiter character(s) to use. (null value will join with no delimiter)
085:             * @return a String of all values in the list seperated by the delimiter
086:             */
087:            public static String join(List list, String delim) {
088:                if (list == null || list.size() < 1)
089:                    return null;
090:                StringBuffer buf = new StringBuffer();
091:                Iterator i = list.iterator();
092:
093:                while (i.hasNext()) {
094:                    buf.append((String) i.next());
095:                    if (i.hasNext())
096:                        buf.append(delim);
097:                }
098:                return buf.toString();
099:            }
100:
101:            /**
102:             * Splits a String on a delimiter into a List of Strings.
103:             * @param str the String to split
104:             * @param delim the delimiter character(s) to join on (null will split on whitespace)
105:             * @return a list of Strings
106:             */
107:            public static List split(String str, String delim) {
108:                List splitList = null;
109:                StringTokenizer st = null;
110:
111:                if (str == null)
112:                    return splitList;
113:
114:                if (delim != null)
115:                    st = new StringTokenizer(str, delim);
116:                else
117:                    st = new StringTokenizer(str);
118:
119:                if (st != null && st.hasMoreTokens()) {
120:                    splitList = new ArrayList();
121:
122:                    while (st.hasMoreTokens())
123:                        splitList.add(st.nextToken());
124:                }
125:                return splitList;
126:            }
127:
128:            /**
129:             * Encloses each of a List of Strings in quotes.
130:             * @param list List of String(s) to quote.
131:             */
132:            public static List quoteStrList(List list) {
133:                List tmpList = list;
134:
135:                list = new ArrayList();
136:                Iterator i = tmpList.iterator();
137:
138:                while (i.hasNext()) {
139:                    String str = (String) i.next();
140:
141:                    str = "'" + str + "''";
142:                    list.add(str);
143:                }
144:                return list;
145:            }
146:
147:            /**
148:             * Creates a Map from an encoded name/value pair string
149:             * @param str The string to decode and format
150:             * @param trim Trim whitespace off fields
151:             * @return a Map of name/value pairs
152:             */
153:            public static Map strToMap(String str, boolean trim) {
154:                if (str == null)
155:                    return null;
156:                Map decodedMap = new HashMap();
157:                List elements = split(str, "|");
158:                Iterator i = elements.iterator();
159:
160:                while (i.hasNext()) {
161:                    String s = (String) i.next();
162:                    List e = split(s, "=");
163:
164:                    if (e.size() != 2) {
165:                        continue;
166:                    }
167:                    String name = (String) e.get(0);
168:                    String value = (String) e.get(1);
169:                    if (trim) {
170:                        if (name != null) {
171:                            name = name.trim();
172:                        }
173:                        if (value != null) {
174:                            value = value.trim();
175:                        }
176:                    }
177:
178:                    try {
179:                        decodedMap.put(URLDecoder.decode(name, "UTF-8"),
180:                                URLDecoder.decode(value, "UTF-8"));
181:                    } catch (UnsupportedEncodingException e1) {
182:                        Debug.logError(e1, module);
183:                    }
184:                }
185:                return decodedMap;
186:            }
187:
188:            /**
189:             * Creates a Map from an encoded name/value pair string
190:             * @param str The string to decode and format
191:             * @return a Map of name/value pairs
192:             */
193:            public static Map strToMap(String str) {
194:                return strToMap(str, false);
195:            }
196:
197:            /**
198:             * Creates an encoded String from a Map of name/value pairs (MUST BE STRINGS!)
199:             * @param map The Map of name/value pairs
200:             * @return String The encoded String
201:             */
202:            public static String mapToStr(Map map) {
203:                if (map == null)
204:                    return null;
205:                StringBuffer buf = new StringBuffer();
206:                Set keySet = map.keySet();
207:                Iterator i = keySet.iterator();
208:                boolean first = true;
209:
210:                while (i.hasNext()) {
211:                    Object key = i.next();
212:                    Object value = map.get(key);
213:
214:                    if (!(key instanceof  String) || !(value instanceof  String))
215:                        continue;
216:                    String encodedName = null;
217:                    try {
218:                        encodedName = URLEncoder.encode((String) key, "UTF-8");
219:                    } catch (UnsupportedEncodingException e) {
220:                        Debug.logError(e, module);
221:                    }
222:                    String encodedValue = null;
223:                    try {
224:                        encodedValue = URLEncoder.encode((String) value,
225:                                "UTF-8");
226:                    } catch (UnsupportedEncodingException e) {
227:                        Debug.logError(e, module);
228:                    }
229:
230:                    if (first)
231:                        first = false;
232:                    else
233:                        buf.append("|");
234:
235:                    buf.append(encodedName);
236:                    buf.append("=");
237:                    buf.append(encodedValue);
238:                }
239:                return buf.toString();
240:            }
241:
242:            /**
243:             * Create a Map from a List of keys and a List of values
244:             * @param keys List of keys
245:             * @param values List of values
246:             * @return Map of combined lists
247:             * @throws IllegalArgumentException When either List is null or the sizes do not equal
248:             */
249:            public static Map createMap(List keys, List values) {
250:                if (keys == null || values == null
251:                        || keys.size() != values.size()) {
252:                    throw new IllegalArgumentException(
253:                            "Keys and Values cannot be null and must be the same size");
254:                }
255:                Map newMap = new HashMap();
256:                for (int i = 0; i < keys.size(); i++) {
257:                    newMap.put(keys.get(i), values.get(i));
258:                }
259:                return newMap;
260:            }
261:
262:            /** Make sure the string starts with a forward slash but does not end with one; converts back-slashes to forward-slashes; if in String is null or empty, returns zero length string. */
263:            public static String cleanUpPathPrefix(String prefix) {
264:                if (prefix == null || prefix.length() == 0)
265:                    return "";
266:
267:                StringBuffer cppBuff = new StringBuffer(prefix.replace('\\',
268:                        '/'));
269:
270:                if (cppBuff.charAt(0) != '/') {
271:                    cppBuff.insert(0, '/');
272:                }
273:                if (cppBuff.charAt(cppBuff.length() - 1) == '/') {
274:                    cppBuff.deleteCharAt(cppBuff.length() - 1);
275:                }
276:                return cppBuff.toString();
277:            }
278:
279:            /** Removes all spaces from a string */
280:            public static String removeSpaces(String str) {
281:                StringBuffer newString = new StringBuffer();
282:                for (int i = 0; i < str.length(); i++) {
283:                    if (str.charAt(i) != ' ')
284:                        newString.append(str.charAt(i));
285:                }
286:                return newString.toString();
287:            }
288:
289:            public static String toHexString(byte[] bytes) {
290:                StringBuffer buf = new StringBuffer(bytes.length * 2);
291:                for (int i = 0; i < bytes.length; i++) {
292:                    buf.append(hexChar[(bytes[i] & 0xf0) >>> 4]);
293:                    buf.append(hexChar[bytes[i] & 0x0f]);
294:                }
295:                return buf.toString();
296:
297:            }
298:
299:            public static String cleanHexString(String str) {
300:                StringBuffer buf = new StringBuffer();
301:                for (int i = 0; i < str.length(); i++) {
302:                    if (str.charAt(i) != (int) 32 && str.charAt(i) != ':') {
303:                        buf.append(str.charAt(i));
304:                    }
305:                }
306:                return buf.toString();
307:            }
308:
309:            public static byte[] fromHexString(String str) {
310:                str = cleanHexString(str);
311:                int stringLength = str.length();
312:                if ((stringLength & 0x1) != 0) {
313:                    throw new IllegalArgumentException(
314:                            "fromHexStringÊrequiresÊanÊevenÊnumberÊofÊhexÊcharacters");
315:                }
316:                byte[] b = new byte[stringLength / 2];
317:
318:                for (int i = 0, j = 0; i < stringLength; i += 2, j++) {
319:                    int high = convertChar(str.charAt(i));
320:                    int low = convertChar(str.charAt(i + 1));
321:                    b[j] = (byte) ((high << 4) | low);
322:                }
323:                return b;
324:            }
325:
326:            private static char[] hexChar = { '0', '1', '2', '3', '4', '5',
327:                    '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
328:
329:            private static int convertChar(char c) {
330:                if ('0' <= c && c <= '9') {
331:                    return c - '0';
332:                } else if ('a' <= c && c <= 'f') {
333:                    return c - 'a' + 0xa;
334:                } else if ('A' <= c && c <= 'F') {
335:                    return c - 'A' + 0xa;
336:                } else {
337:                    throw new IllegalArgumentException(
338:                            "InvalidÊhexÊcharacter:Ê" + c);
339:                }
340:            }
341:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.