001: package net.sourceforge.squirrel_sql.fw.util;
002:
003: /*
004: * Copyright (C) 2001-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: /**
025: * String handling utilities.
026: *
027: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
028: */
029: public class StringUtilities {
030: /**
031: * Return <tt>true</tt> if the passed string is <tt>null</tt> or empty.
032: *
033: * @param str String to be tested.
034: *
035: * @return <tt>true</tt> if the passed string is <tt>null</tt> or empty.
036: */
037: public static boolean isEmpty(String str) {
038: return str == null || str.length() == 0;
039: }
040:
041: /**
042: * Return whether the 2 passed strings are equal. This function
043: * allows for <TT>null</TT> strings. If <TT>s1</TT> and <TT>s1</TT> are
044: * both <TT>null</TT> they are considered equal.
045: *
046: * @param str1 First string to check.
047: * @param str2 Second string to check.
048: */
049: public static boolean areStringsEqual(String str1, String str2) {
050: if (str1 == null && str2 == null) {
051: return true;
052: }
053: if (str1 != null) {
054: return str1.equals(str2);
055: }
056: return str2.equals(str1);
057: }
058:
059: /**
060: * Clean the passed string. Replace whitespace characters with a single
061: * space. If a <TT>null</TT> string passed return an empty string. E.G.
062: * replace
063: *
064: * [pre]
065: * \t\tselect\t* from\t\ttab01
066: * [/pre]
067: *
068: * with
069: *
070: * [pre]
071: * select * from tab01
072: * [/pre]
073: *
074: * @param str String to be cleaned.
075: *
076: * @return Cleaned string.
077: */
078: public static String cleanString(String str) {
079: final StringBuffer buf = new StringBuffer(str.length());
080: char prevCh = ' ';
081:
082: for (int i = 0, limit = str.length(); i < limit; ++i) {
083: char ch = str.charAt(i);
084: if (Character.isWhitespace(ch)) {
085: if (!Character.isWhitespace(prevCh)) {
086: buf.append(' ');
087: }
088: } else {
089: buf.append(ch);
090: }
091: prevCh = ch;
092: }
093:
094: return buf.toString();
095: }
096:
097: /**
098: * Return the number of occurences of a character in a string.
099: *
100: * @param str The string to check.
101: * @param ch The character check for.
102: *
103: * @return The number of times <tt>ch</tt> occurs in <tt>str</tt>.
104: */
105: public static int countOccurences(String str, int ch) {
106: if (isEmpty(str)) {
107: return 0;
108: }
109:
110: int count = 0;
111: int idx = -1;
112: do {
113: idx = str.indexOf(ch, ++idx);
114: if (idx != -1) {
115: ++count;
116: }
117: } while (idx != -1);
118: return count;
119: }
120:
121: /**
122: * Split a string based on the given delimiter, but don't remove
123: * empty elements.
124: *
125: * @param str The string to be split.
126: * @param delimiter Split string based on this delimiter.
127: *
128: * @return Array of split strings. Guaranteeded to be not null.
129: */
130: public static String[] split(String str, char delimiter) {
131: return split(str, delimiter, false);
132: }
133:
134: /**
135: * Split a string based on the given delimiter, optionally removing
136: * empty elements.
137: *
138: * @param str The string to be split.
139: * @param delimiter Split string based on this delimiter.
140: * @param removeEmpty If <tt>true</tt> then remove empty elements.
141: *
142: * @return Array of split strings. Guaranteeded to be not null.
143: */
144: public static String[] split(String str, char delimiter,
145: boolean removeEmpty) {
146: // Return empty list if source string is empty.
147: final int len = (str == null) ? 0 : str.length();
148: if (len == 0) {
149: return new String[0];
150: }
151:
152: final List<String> result = new ArrayList<String>();
153: String elem = null;
154: int i = 0, j = 0;
155: while (j != -1 && j < len) {
156: j = str.indexOf(delimiter, i);
157: elem = (j != -1) ? str.substring(i, j) : str.substring(i);
158: i = j + 1;
159: if (!removeEmpty || !(elem == null || elem.length() == 0)) {
160: result.add(elem);
161: }
162: }
163: return result.toArray(new String[result.size()]);
164: }
165:
166: /**
167: * Joins the specified parts separating each from one another with the
168: * specified delimiter. If delim is null, then this merely returns the
169: * concatenation of all the parts.
170: *
171: * @param parts the strings to be joined
172: * @param delim the char(s) that should separate the parts in the result
173: * @return a string representing the joined parts.
174: */
175: public static String join(String[] parts, String delim) {
176: StringBuilder result = new StringBuilder();
177:
178: for (int i = 0; i < parts.length; i++) {
179: String part = parts[i];
180: result.append(part);
181: if (delim != null && i < parts.length - 1) {
182: result.append(delim);
183: }
184: }
185: return result.toString();
186: }
187:
188: public static String[] segment(String source, int maxSegmentSize) {
189: ArrayList<String> tmp = new ArrayList<String>();
190: if (source.length() <= maxSegmentSize) {
191: return new String[] { source };
192: }
193: boolean done = false;
194: int currBeginIdx = 0;
195: int currEndIdx = maxSegmentSize;
196: while (!done) {
197: String segment = source.substring(currBeginIdx, currEndIdx);
198: tmp.add(segment);
199: if (currEndIdx >= source.length()) {
200: done = true;
201: continue;
202: }
203: currBeginIdx = currEndIdx;
204: currEndIdx += maxSegmentSize;
205: if (currEndIdx > source.length()) {
206: currEndIdx = source.length();
207: }
208: }
209: return tmp.toArray(new String[tmp.size()]);
210: }
211:
212: public static int getTokenBeginIndex(String selectSQL, String token) {
213: String lowerSel = selectSQL.toLowerCase();
214: String lowerToken = token.toLowerCase().trim();
215:
216: int curPos = 0;
217: int count = 0;
218: while (-1 != curPos) {
219: curPos = lowerSel.indexOf(lowerToken, curPos
220: + lowerToken.length());
221:
222: if (-1 < curPos
223: && (0 == curPos || Character.isWhitespace(lowerSel
224: .charAt(curPos - 1)))
225: && (lowerSel.length() == curPos
226: + lowerToken.length() || Character
227: .isWhitespace(lowerSel.charAt(curPos
228: + lowerToken.length())))) {
229: return curPos;
230: }
231: // If we've loop through one time for each character in the string,
232: // then something must be wrong. Get out!
233: if (count++ > selectSQL.length()) {
234: break;
235: }
236: }
237:
238: return curPos;
239: }
240:
241: public static Byte[] getByteArray(byte[] bytes) {
242: if (bytes == null || bytes.length == 0) {
243: return new Byte[0];
244: }
245: Byte[] result = new Byte[bytes.length];
246: for (int i = 0; i < bytes.length; i++) {
247: result[i] = Byte.valueOf(bytes[i]);
248: }
249:
250: return result;
251: }
252:
253: /**
254: * Chops off the very last character of the given string.
255: *
256: * @param aString a string to chop
257: * @return the specified string minus it's last character, or null for null
258: * or empty string for a string with length == 0|1.
259: */
260: public static String chop(String aString) {
261: if (aString == null) {
262: return null;
263: }
264: if (aString.length() == 0) {
265: return "";
266: }
267: if (aString.length() == 1) {
268: return "";
269: }
270: return aString.substring(0, aString.length() - 1);
271: }
272:
273: }
|