Source Code Cross Referenced for TextUtilities.java in  » Web-Framework » jpublish » com » anthonyeden » lib » 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 » Web Framework » jpublish » com.anthonyeden.lib.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*-- 
002:
003:         Copyright (C) 2000-2003 Anthony Eden.
004:         All rights reserved.
005:         
006:         Redistribution and use in source and binary forms, with or without
007:         modification, are permitted provided that the following conditions
008:         are met:
009:         
010:         1. Redistributions of source code must retain the above copyright
011:            notice, this list of conditions, and the following disclaimer.
012:         
013:         2. Redistributions in binary form must reproduce the above copyright
014:            notice, this list of conditions, and the disclaimer that follows 
015:            these conditions in the documentation and/or other materials 
016:            provided with the distribution.
017:
018:         3. The name "EdenLib" must not be used to endorse or promote products
019:            derived from this software without prior written permission.  For
020:            written permission, please contact me@anthonyeden.com.
021:         
022:         4. Products derived from this software may not be called "EdenLib", nor
023:            may "EdenLib" appear in their name, without prior written permission
024:            from Anthony Eden (me@anthonyeden.com).
025:         
026:         In addition, I request (but do not require) that you include in the 
027:         end-user documentation provided with the redistribution and/or in the 
028:         software itself an acknowledgement equivalent to the following:
029:             "This product includes software developed by
030:              Anthony Eden (http://www.anthonyeden.com/)."
031:
032:         THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
033:         WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
034:         OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
035:         DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, 
036:         INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
037:         (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
038:         SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
039:         HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
040:         STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
041:         IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
042:         POSSIBILITY OF SUCH DAMAGE.
043:
044:         For more information on EdenLib, please see <http://edenlib.sf.net/>.
045:         
046:         */
047:
048:        package com.anthonyeden.lib.util;
049:
050:        import java.util.List;
051:        import java.util.Iterator;
052:        import java.util.ArrayList;
053:        import java.util.Collection;
054:        import java.util.StringTokenizer;
055:
056:        /** Useful text manipulation methods.
057:
058:         @author Anthony Eden
059:         */
060:
061:        public final class TextUtilities {
062:
063:            public static final String DEFAULT_DELIMITER = ",";
064:
065:            /** Constructor. */
066:
067:            private TextUtilities() {
068:                // no op
069:            }
070:
071:            /** Replace all instances of a String within another String.
072:            
073:                @param str The complete String
074:                @param searchString The search String
075:                @param replacementString The replacement String
076:             */
077:
078:            public static String replace(String str, String searchString,
079:                    String replacementString) {
080:                int offset = 0;
081:                int searchLength = searchString.length();
082:                int replaceLength = replacementString.length();
083:                int index = str.indexOf(searchString);
084:                while (index >= 0) {
085:                    StringBuffer buffer = new StringBuffer();
086:                    offset = index + replaceLength;
087:                    buffer.append(str.substring(0, index));
088:                    buffer.append(replacementString);
089:                    buffer.append(str.substring(index + searchLength));
090:                    str = buffer.toString();
091:                    index = str.indexOf(searchString);
092:                }
093:                return str;
094:            }
095:
096:            /** Remove all instances of the given characters from the given String.
097:            
098:                @param src The source String
099:                @param removeChars The characters to remove
100:                @return The result String
101:             */
102:
103:            public static String remove(String src, char[] removeChars) {
104:                StringBuffer buffer = new StringBuffer();
105:                char[] array = new char[src.length()];
106:                src.getChars(0, src.length(), array, 0);
107:                LOOP: for (int i = 0; i < array.length; i++) {
108:                    for (int j = 0; j < removeChars.length; j++) {
109:                        if (array[i] == removeChars[j]) {
110:                            continue LOOP;
111:                        }
112:                    }
113:                    buffer.append(array[i]);
114:                }
115:                return buffer.toString();
116:            }
117:
118:            /** Remove all whitespace from the given String.
119:            
120:                @param src The source String
121:                @return The resulting String
122:             */
123:
124:            public static String removeWhitespace(String src) {
125:                StringBuffer buffer = new StringBuffer();
126:                char[] array = new char[src.length()];
127:                src.getChars(0, src.length(), array, 0);
128:                for (int i = 0; i < array.length; i++) {
129:                    if (!Character.isWhitespace(array[i])) {
130:                        buffer.append(array[i]);
131:                    }
132:                }
133:                return buffer.toString();
134:            }
135:
136:            /** Returns true if the given String contains at least one non alpha-
137:                numeric character.
138:                
139:                @param str The String
140:                @return True if one or more alpha-numeric characters are found
141:             */
142:
143:            public static boolean hasNonAlphaNumericCharacters(String str) {
144:                char[] chars = str.toCharArray();
145:                for (int i = 0; i < chars.length; i++) {
146:                    if (!Character.isLetterOrDigit(chars[i])) {
147:                        return true;
148:                    }
149:                }
150:                return false;
151:            }
152:
153:            /** Returns true if the given String contains at least one non alpha-
154:                numeric character.  Characters in the ignore array are not checked.
155:                
156:                @param str The String
157:                @param ignore An array of characters to ignore
158:                @return True if one or more alpha-numeric characters are found
159:             */
160:
161:            public static boolean hasNonAlphaNumericCharacters(String str,
162:                    char[] ignore) {
163:                char[] chars = str.toCharArray();
164:                for (int i = 0; i < chars.length; i++) {
165:                    if (isIgnore(chars[i], ignore)) {
166:                        continue;
167:                    }
168:
169:                    if (!Character.isLetterOrDigit(chars[i])) {
170:                        return true;
171:                    }
172:                }
173:                return false;
174:            }
175:
176:            /** String all non alpha-numeric characters from the given String.
177:            
178:                @param str The String
179:                @return The new String
180:             */
181:
182:            public static String stripNonAlphaNumericCharacters(String str) {
183:                StringBuffer buffer = new StringBuffer();
184:
185:                char[] chars = str.toCharArray();
186:                for (int i = 0; i < chars.length; i++) {
187:                    if (Character.isLetterOrDigit(chars[i])) {
188:                        buffer.append(chars[i]);
189:                    }
190:                }
191:
192:                return buffer.toString();
193:            }
194:
195:            /** Strip all non alpha-numeric characters from the given String.  Characters
196:                contained in the ignore array will be left within the String.
197:                
198:                @param str The String
199:                @param ignore An array of characters to ignore
200:                @return The new String
201:             */
202:
203:            public static String stripNonAlphaNumericCharacters(String str,
204:                    char[] ignore) {
205:                StringBuffer buffer = new StringBuffer();
206:
207:                char[] chars = str.toCharArray();
208:                for (int i = 0; i < chars.length; i++) {
209:                    if (isIgnore(chars[i], ignore)) {
210:                        continue;
211:                    }
212:
213:                    if (Character.isLetterOrDigit(chars[i])) {
214:                        buffer.append(chars[i]);
215:                    }
216:                }
217:
218:                return buffer.toString();
219:            }
220:
221:            /** Capitalize the first letter of the given text.
222:            
223:                @param text The original text
224:                @return The capitalized text
225:             */
226:
227:            public static String capitalize(String text) {
228:                return text.substring(0, 1).toUpperCase() + text.substring(1);
229:            }
230:
231:            /** Split the given text using the given separator.
232:            
233:                @param text The original text
234:                @param separator The separator
235:                @return The result List of String objects
236:             */
237:
238:            public static List splitString(String text, String separator) {
239:                return splitString(text, separator, true);
240:            }
241:
242:            /** Split the given text using the given separator.
243:            
244:                @param text The original text
245:                @param separator The separator
246:                @return The result List of String objects
247:             */
248:
249:            public static List splitString(String text, String separator,
250:                    boolean removeWhitespace) {
251:                ArrayList elements = new ArrayList();
252:                StringTokenizer tk = new StringTokenizer(text, separator);
253:                while (tk.hasMoreTokens()) {
254:                    if (removeWhitespace) {
255:                        elements.add(removeWhitespace(tk.nextToken()));
256:                    } else {
257:                        elements.add(tk.nextToken());
258:                    }
259:                }
260:                return elements;
261:            }
262:
263:            /** Split the given String on whitespace characters.
264:            
265:                @param text The source String
266:                @return A List of String objects
267:             */
268:
269:            public static List splitStringOnWhitespace(String text) {
270:                List elements = new ArrayList();
271:                StringBuffer buffer = new StringBuffer();
272:                char[] chars = text.toCharArray();
273:                for (int i = 0; i < chars.length; i++) {
274:                    if (Character.isWhitespace(chars[i])) {
275:                        if (buffer.length() > 0) {
276:                            elements.add(buffer.toString());
277:                            buffer = new StringBuffer();
278:                        }
279:                    } else {
280:                        buffer.append(chars[i]);
281:                    }
282:                }
283:
284:                if (buffer.length() > 0) {
285:                    elements.add(buffer.toString());
286:                }
287:
288:                return elements;
289:            }
290:
291:            /** Split the String on newlines.
292:            
293:                @param text The source text
294:                @return A List of String objects
295:             */
296:
297:            public static List chompString(String text) {
298:                return splitString(text, "\n");
299:            }
300:
301:            /** Join the given Object array using the DEFAULT_DELIMITER.
302:            
303:                @param data The data array
304:                @return The resulting String
305:             */
306:
307:            public static String join(Object[] data) {
308:                return join(data, DEFAULT_DELIMITER);
309:            }
310:
311:            /** Join the given Object array using the given delimiter.  The toString()
312:                method on each Object will be invoked to retrieve a String representation
313:                of the Object.
314:                
315:                @param data The data array
316:                @param delimiter The delimiter String
317:                @return The resulting String
318:             */
319:
320:            public static String join(Object[] data, String delimiter) {
321:                StringBuffer buffer = new StringBuffer();
322:                for (int i = 0; i < data.length; i++) {
323:                    buffer.append(data.toString());
324:                    if (i < data.length - 1) {
325:                        buffer.append(delimiter);
326:                    }
327:                }
328:                return buffer.toString();
329:            }
330:
331:            /** Join the given Collection using the DEFAULT_DELIMITER.
332:            
333:                @param data The collection
334:                @return The resulting String
335:             */
336:
337:            public static String join(Collection data) {
338:                return join(data, DEFAULT_DELIMITER);
339:            }
340:
341:            /** Join the given Collection using the given delimiter.  The toString()
342:                method on each Object will be invoked to retrieve a String representation
343:                of the Object.
344:                
345:                @param data The collection
346:                @param delimiter The delimiter String
347:                @return The resulting String
348:             */
349:
350:            public static String join(Collection data, String delimiter) {
351:                StringBuffer buffer = new StringBuffer();
352:                Iterator iter = data.iterator();
353:                while (iter.hasNext()) {
354:                    buffer.append(iter.next().toString());
355:                    if (iter.hasNext()) {
356:                        buffer.append(delimiter);
357:                    }
358:                }
359:                return buffer.toString();
360:            }
361:
362:            /** Return true if the character is an ASCII character.
363:            
364:                @param c The character
365:                @return True if character is ASCII
366:             */
367:
368:            public static boolean isAscii(char c) {
369:                return (c & 0xff80) == 0;
370:            }
371:
372:            /** Return true if the character is an extended ASCII character.
373:            
374:                @param c The character
375:                @return True if character is extended ASCII
376:             */
377:
378:            public static boolean isExtendedAscii(char c) {
379:                return (c & 0xff00) == 0;
380:            }
381:
382:            /** Return true if the given character is a member of the ignore array.
383:            
384:                @param c The character
385:                @param ignore An array of characters to ignore
386:                @return True if the character is in the ignore array
387:             */
388:
389:            private static boolean isIgnore(char c, char[] ignore) {
390:                for (int i = 0; i < ignore.length; i++) {
391:                    if (c == ignore[i]) {
392:                        return true;
393:                    }
394:                }
395:                return false;
396:            }
397:
398:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.