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() < 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 < 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 < 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: }
|