Source Code Cross Referenced for TextUtils.java in  » Web-Services » xins » org » xins » common » text » 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 Services » xins » org.xins.common.text 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: TextUtils.java,v 1.29 2007/05/22 09:36:09 agoubard Exp $
003:         *
004:         * Copyright 2003-2007 Orange Nederland Breedband B.V.
005:         * See the COPYRIGHT file for redistribution and use restrictions.
006:         */
007:        package org.xins.common.text;
008:
009:        import java.util.Enumeration;
010:        import java.util.Properties;
011:        import org.xins.common.MandatoryArgumentChecker;
012:
013:        /**
014:         * Text-related utility functions.
015:         *
016:         * @version $Revision: 1.29 $ $Date: 2007/05/22 09:36:09 $
017:         * @author <a href="mailto:ernst@ernstdehaan.com">Ernst de Haan</a>
018:         *
019:         * @since XINS 1.0.0
020:         */
021:        public final class TextUtils {
022:
023:            /**
024:             * Constructs a new <code>TextUtils</code> object.
025:             */
026:            private TextUtils() {
027:                // empty
028:            }
029:
030:            /**
031:             * Quotes the specified string, or returns <code>"(null)"</code> if it is
032:             * <code>null</code>.
033:             *
034:             * @param s
035:             *    the input string, or <code>null</code>.
036:             *
037:             * @return
038:             *    if <code>s != null</code> the quoted string, otherwise the string
039:             *    <code>"(null)"</code>.
040:             */
041:            public static String quote(String s) {
042:                if (s != null) {
043:                    String quoted = '"' + s + '"';
044:                    return quoted;
045:                } else {
046:                    return "(null)";
047:                }
048:            }
049:
050:            /**
051:             * Quotes the textual presentation (returned by <code>toString()</code>) of
052:             * the specified object, or returns <code>"(null)"</code> if the object is
053:             * <code>null</code>.
054:             *
055:             * @param o
056:             *    the object, or <code>null</code>.
057:             *
058:             * @return
059:             *    if <code>o != null</code> then <code>o.toString()</code> quoted,
060:             *    otherwise the string <code>"(null)"</code>.
061:             *
062:             * @since XINS 1.0.1
063:             */
064:            public static String quote(Object o) {
065:                String s = (o == null) ? null : o.toString();
066:                return quote(s);
067:            }
068:
069:            /**
070:             * Determines if the specified string is <code>null</code> or an empty
071:             * string.
072:             *
073:             * @param s
074:             *    the string, or <code>null</code>.
075:             *
076:             * @return
077:             *    <code>true</code> if <code>s == null || s.length() &lt; 1</code>.
078:             *
079:             * @since XINS 1.0.1
080:             */
081:            public static boolean isEmpty(String s) {
082:                return (s == null) || (s.length() == 0);
083:            }
084:
085:            /**
086:             * Trims the specified string, or returns an empty string if the argument
087:             * is <code>null</code>.
088:             *
089:             * @param s
090:             *    the string, or <code>null</code>.
091:             *
092:             * @param ifEmpty
093:             *    the string to return if
094:             *    <code>s == null || s.trim().length &lt; 1</code>.
095:             *
096:             * @return
097:             *    the trimmed version of the string (see {@link String#trim()}) or
098:             *    <code>ifEmpty</code> if
099:             *    <code>s == null</code> or <code>s.trim().length &lt; 1</code>.
100:             *
101:             * @since XINS 1.3.0
102:             */
103:            public static String trim(String s, String ifEmpty) {
104:
105:                if (s != null) {
106:                    s = s.trim();
107:                    if (s.length() >= 1) {
108:                        return s;
109:                    }
110:                }
111:
112:                return ifEmpty;
113:            }
114:
115:            /**
116:             * Replaces substrings in a string. The substrings to be replaced are
117:             * passed in a {@link Properties} object. A prefix and a suffix can be
118:             * specified. These are prepended/appended to each of the search keys.
119:             *
120:             * <p />Example: If you have a string <code>"Hello ${name}"</code> and you
121:             * would like to replace <code>"${name}"</code> with <code>"John"</code>
122:             * and you would like to replace <code>${surname}</code> with
123:             * <code>"Doe"</code>, use the following code:
124:             *
125:             * <p /><blockquote><code>String s = "Hello ${name}";
126:             * <br />Properties replacements = new Properties();
127:             * <br />replacements.put("name", "John");
128:             * <br />replacements.put("surname", "Doe");
129:             * <br />
130:             * <br />StringUtils.replace(s, replacements, "${", "}");</code></blockquote>
131:             *
132:             * <p />The result string will be <code>"Hello John"</code>.
133:             *
134:             * @param s
135:             *    the text string to which replacements should be applied, not <code>null</code>.
136:             *
137:             * @param replacements
138:             *    the replacements to apply, not <code>null</code>.
139:             *
140:             * @param prefix
141:             *    the optional prefix for the search keys, or <code>null</code>.
142:             *
143:             * @param suffix
144:             *    the optional prefix for the search keys, or <code>null</code>.
145:             *
146:             * @return the String with the replacements.
147:             *
148:             * @throws IllegalArgumentException
149:             *    if one of the mandatory arguments is missing.
150:             *
151:             * @since XINS 1.4.0.
152:             */
153:            public static String replace(String s, Properties replacements,
154:                    String prefix, String suffix)
155:                    throws IllegalArgumentException {
156:
157:                // Check preconditions
158:                MandatoryArgumentChecker.check("s", s, "replacements",
159:                        replacements);
160:
161:                // Make sure prefix and suffix are not null
162:                prefix = (prefix == null) ? "" : prefix;
163:                suffix = (suffix == null) ? "" : suffix;
164:
165:                Enumeration keys = replacements.propertyNames();
166:                while (keys.hasMoreElements()) {
167:                    String key = (String) keys.nextElement();
168:                    String search = prefix + key + suffix;
169:                    int index = s.indexOf(search);
170:                    while (index >= 0) {
171:                        String replacement = replacements.getProperty(key);
172:                        s = s.substring(0, index) + replacement
173:                                + s.substring(index + search.length());
174:                        index = s.indexOf(search);
175:                    }
176:                }
177:
178:                return s;
179:            }
180:
181:            /**
182:             * Tranforms the given <code>String</code> to the similar <code>String</code>,
183:             * but starting with an uppercase.
184:             *
185:             * @param text
186:             *    the text to transform, can be <code>null</code>
187:             *
188:             * @return
189:             *    the transformed text, the return value will start with an uppercase.
190:             *    <code>null</code> is returned if the text was <code>null</code>.
191:             *
192:             * @since XINS 2.0.
193:             */
194:            public static String firstCharUpper(String text) {
195:                if (text == null) {
196:                    return null;
197:                } else if (text.length() == 0) {
198:                    return text;
199:                } else if (!Character.isLowerCase(text.charAt(0))) {
200:                    return text;
201:                } else if (text.length() == 1) {
202:                    return text.toUpperCase();
203:                } else {
204:                    return text.substring(0, 1).toUpperCase()
205:                            + text.substring(1);
206:                }
207:
208:            }
209:
210:            /**
211:             * Tranforms the given <code>String</code> to the similar <code>String</code>,
212:             * but starting with a lowercase.
213:             *
214:             * @param text
215:             *    the text to transform, can be <code>null</code>
216:             *
217:             * @return
218:             *    the transformed text, the return value will start with a lowercase.
219:             *    <code>null</code> is returned if the text was <code>null</code>.
220:             *
221:             * @since XINS 2.0.
222:             */
223:            public static String firstCharLower(String text) {
224:                if (text == null) {
225:                    return null;
226:                } else if (text.length() == 0) {
227:                    return text;
228:                } else if (!Character.isUpperCase(text.charAt(0))) {
229:                    return text;
230:                } else if (text.length() == 1) {
231:                    return text.toLowerCase();
232:                } else {
233:                    return text.substring(0, 1).toLowerCase()
234:                            + text.substring(1);
235:                }
236:            }
237:
238:            /**
239:             * Removes the given character from the given <code>String</code>.
240:             *
241:             * @param charToRemove
242:             *     the character that should be removed from the String.
243:             *
244:             * @param text
245:             *     the text from which the charecter should be removed, can be <code>null</code>.
246:             *
247:             * @return
248:             *    the text without the character or <code>null</code> if the input text is <code>null</code>.
249:             *
250:             * @since XINS 2.0.
251:             */
252:            public static String removeCharacter(char charToRemove, String text) {
253:                if (text == null) {
254:                    return null;
255:                }
256:                if (text.indexOf(charToRemove) == -1) {
257:                    return text;
258:                }
259:                char[] inputText = text.toCharArray();
260:                StringBuffer result = new StringBuffer(inputText.length);
261:                for (int i = 0; i < inputText.length; i++) {
262:                    if (inputText[i] != charToRemove) {
263:                        result.append(inputText[i]);
264:                    }
265:                }
266:                if (result.length() == inputText.length) {
267:                    return text;
268:                } else {
269:                    return result.toString();
270:                }
271:            }
272:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.