001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.lib;
032:
033: import java.lang.reflect.Array;
034:
035: /** Provides a collection of convenience methods for processing and
036: * creating objects with <code>String</code> value components.
037: *
038: * @author fredt@users
039: * @author boucherb@users
040: * @version 1.7.2
041: * @since 1.7.0
042: */
043: public class StringUtil {
044:
045: /**
046: * Returns a string with non alphanumeric chars converted to the
047: * substitute character. A digit first character is also converted.
048: * By sqlbob@users
049: * @param source string to convert
050: * @param substitute character to use
051: * @return converted string
052: */
053: public static String toLowerSubset(String source, char substitute) {
054:
055: int len = source.length();
056: StringBuffer src = new StringBuffer(len);
057: char ch;
058:
059: for (int i = 0; i < len; i++) {
060: ch = source.charAt(i);
061:
062: if (!Character.isLetterOrDigit(ch)) {
063: src.append(substitute);
064: } else if ((i == 0) && Character.isDigit(ch)) {
065: src.append(substitute);
066: } else {
067: src.append(Character.toLowerCase(ch));
068: }
069: }
070:
071: return src.toString();
072: }
073:
074: /**
075: * Builds a bracketed CSV list from the array
076: * @param array an array of Objects
077: * @return string
078: */
079: public static String arrayToString(Object array) {
080:
081: int len = Array.getLength(array);
082: int last = len - 1;
083: StringBuffer sb = new StringBuffer(2 * (len + 1));
084:
085: sb.append('{');
086:
087: for (int i = 0; i < len; i++) {
088: sb.append(Array.get(array, i));
089:
090: if (i != last) {
091: sb.append(',');
092: }
093: }
094:
095: sb.append('}');
096:
097: return sb.toString();
098: }
099:
100: /**
101: * Builds a CSV list from the specified String[], separator string and
102: * quote string. <p>
103: *
104: * <ul>
105: * <li>All arguments are assumed to be non-null.
106: * <li>Separates each list element with the value of the
107: * <code>separator</code> argument.
108: * <li>Prepends and appends each element with the value of the
109: * <code>quote</code> argument.
110: * <li> No attempt is made to escape the quote character sequence if it is
111: * found internal to a list element.
112: * <ul>
113: * @return a CSV list
114: * @param separator the <code>String</code> to use as the list element separator
115: * @param quote the <code>String</code> with which to quote the list elements
116: * @param s array of <code>String</code> objects
117: */
118: public static String getList(String[] s, String separator,
119: String quote) {
120:
121: int len = s.length;
122: StringBuffer b = new StringBuffer(len * 16);
123:
124: for (int i = 0; i < len; i++) {
125: b.append(quote);
126: b.append(s[i]);
127: b.append(quote);
128:
129: if (i + 1 < len) {
130: b.append(separator);
131: }
132: }
133:
134: return b.toString();
135: }
136:
137: public static String getList(Object[] s, String separator,
138: String quote) {
139:
140: int len = s.length;
141: StringBuffer b = new StringBuffer(len * 16);
142:
143: for (int i = 0; i < len; i++) {
144: b.append(quote);
145: b.append(s[i]);
146: b.append(quote);
147:
148: if (i + 1 < len) {
149: b.append(separator);
150: }
151: }
152:
153: return b.toString();
154: }
155:
156: /**
157: * Builds a CSV list from the specified int[], <code>separator</code>
158: * <code>String</code> and <code>quote</code> <code>String</code>. <p>
159: *
160: * <ul>
161: * <li>All arguments are assumed to be non-null.
162: * <li>Separates each list element with the value of the
163: * <code>separator</code> argument.
164: * <li>Prepends and appends each element with the value of the
165: * <code>quote</code> argument.
166: * <ul>
167: * @return a CSV list
168: * @param s the array of int values
169: * @param separator the <code>String</code> to use as the separator
170: * @param quote the <code>String</code> with which to quote the list elements
171: */
172: public static String getList(int[] s, String separator, String quote) {
173:
174: int len = s.length;
175: StringBuffer b = new StringBuffer(len * 8);
176:
177: for (int i = 0; i < len; i++) {
178: b.append(quote);
179: b.append(s[i]);
180: b.append(quote);
181:
182: if (i + 1 < len) {
183: b.append(separator);
184: }
185: }
186:
187: return b.toString();
188: }
189:
190: /**
191: * Builds a CSV list from the specified String[][], separator string and
192: * quote string. <p>
193: *
194: * <ul>
195: * <li>All arguments are assumed to be non-null.
196: * <li>Uses only the first element in each subarray.
197: * <li>Separates each list element with the value of the
198: * <code>separator</code> argument.
199: * <li>Prepends and appends each element with the value of the
200: * <code>quote</code> argument.
201: * <li> No attempt is made to escape the quote character sequence if it is
202: * found internal to a list element.
203: * <ul>
204: * @return a CSV list
205: * @param separator the <code>String</code> to use as the list element separator
206: * @param quote the <code>String</code> with which to quote the list elements
207: * @param s the array of <code>String</code> array objects
208: */
209: public static String getList(String[][] s, String separator,
210: String quote) {
211:
212: int len = s.length;
213: StringBuffer b = new StringBuffer(len * 16);
214:
215: for (int i = 0; i < len; i++) {
216: b.append(quote);
217: b.append(s[i][0]);
218: b.append(quote);
219:
220: if (i + 1 < len) {
221: b.append(separator);
222: }
223: }
224:
225: return b.toString();
226: }
227:
228: /**
229: * Appends a pair of string to the string buffer, using the separator between
230: * and terminator at the end
231: * @param b the buffer
232: * @param s1 first string
233: * @param s2 second string
234: * @param separator separator string
235: * @param terminator terminator string
236: */
237: public static void appendPair(StringBuffer b, String s1, String s2,
238: String separator, String terminator) {
239:
240: b.append(s1);
241: b.append(separator);
242: b.append(s2);
243: b.append(terminator);
244: }
245:
246: /**
247: * Checks if text is empty (characters <= space)
248: * @author: Nitin Chauhan
249: * @return boolean true if text is null or empty, false otherwise
250: * @param s java.lang.String
251: */
252: public static boolean isEmpty(String s) {
253:
254: int i = s == null ? 0 : s.length();
255:
256: while (i > 0) {
257: if (s.charAt(--i) > ' ') {
258: return false;
259: }
260: }
261:
262: return true;
263: }
264:
265: /**
266: * Returns the size of substring that does not contain ane trailing spaces
267: * @param s the string
268: * @return trimmed size
269: */
270: public static int rTrimSize(String s) {
271:
272: int i = s.length();
273:
274: while (i > 0) {
275: i--;
276:
277: if (s.charAt(i) != ' ') {
278: return i + 1;
279: }
280: }
281:
282: return 0;
283: }
284:
285: /**
286: * Skips any spaces at or after start and returns the index of first
287: * non-space character;
288: * @param s the string
289: * @param start index to start
290: * @return index of first non-space
291: */
292: public static int skipSpaces(String s, int start) {
293:
294: int limit = s.length();
295: int i = start;
296:
297: for (; i < limit; i++) {
298: if (s.charAt(i) != ' ') {
299: break;
300: }
301: }
302:
303: return i;
304: }
305:
306: /**
307: * Splits the string into an array, using the separator. If separator is
308: * not found in the string, the whole string is returned in the array.
309: *
310: * @param s the string
311: * @param separator the separator
312: * @return array of strings
313: */
314: public static String[] split(String s, String separator) {
315:
316: HsqlArrayList list = new HsqlArrayList();
317: int currindex = 0;
318:
319: for (boolean more = true; more;) {
320: int nextindex = s.indexOf(separator, currindex);
321:
322: if (nextindex == -1) {
323: nextindex = s.length();
324: more = false;
325: }
326:
327: list.add(s.substring(currindex, nextindex));
328:
329: currindex = nextindex + separator.length();
330: }
331:
332: return (String[]) list.toArray(new String[list.size()]);
333: }
334: }
|