001: /*
002: ** Tim Endres' utilities package.
003: ** Copyright (c) 1997 by Tim Endres
004: **
005: ** This program is free software.
006: **
007: ** You may redistribute it and/or modify it under the terms of the GNU
008: ** General Public License as published by the Free Software Foundation.
009: ** Version 2 of the license should be included with this distribution in
010: ** the file LICENSE, as well as License.html. If the license is not
011: ** included with this distribution, you may find a copy at the FSF web
012: ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
013: ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
014: **
015: ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
016: ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
017: ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
018: ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
019: ** REDISTRIBUTION OF THIS SOFTWARE.
020: **
021: */
022:
023: package com.ice.util;
024:
025: import java.util.*;
026:
027: public class StringUtilities {
028: static public String[] vectorToStringArray(Vector sV) {
029: int sz = sV.size();
030: String[] result = new String[sz];
031:
032: for (int i = 0; i < sz; ++i) {
033: result[i] = (String) sV.elementAt(i);
034: }
035:
036: return result;
037: }
038:
039: /**
040: * Split a string into a string array containing the substrings
041: * between the delimiters.
042: *
043: * NOTE This method WILL <strong>NOT</strong> return an empty
044: * token at the end of the array that is returned, if the string
045: * ends with the delimiter. If you wish to have a property string
046: * array that ends with the delimiter return an empty string at
047: * the end of the array, use <code>vectorString()</code>.
048: */
049:
050: static public String[] splitString(String splitStr, String delim) {
051: int i, count;
052: String[] result;
053: StringTokenizer toker;
054:
055: toker = new StringTokenizer(splitStr, delim);
056:
057: count = toker.countTokens();
058:
059: result = new String[count];
060:
061: for (i = 0; i < count; ++i) {
062: try {
063: result[i] = toker.nextToken();
064: } catch (NoSuchElementException ex) {
065: result = null;
066: break;
067: }
068: }
069:
070: return result;
071: }
072:
073: /**
074: * Split a string into a string Vector containing the substrings
075: * between the delimiters.
076: *
077: * NOTE This method WILL return an empty
078: * token at the end of the array that is returned, if the string
079: * ends with the delimiter.
080: */
081:
082: static public Vector vectorString(String splitStr, String delim) {
083: boolean tokeWasDelim = false;
084: int i, count;
085: StringTokenizer toker;
086:
087: Vector result = new Vector();
088:
089: toker = new StringTokenizer(splitStr, delim, true);
090: count = toker.countTokens();
091:
092: for (i = 0; i < count; ++i) {
093: String toke;
094:
095: try {
096: toke = toker.nextToken();
097: } catch (NoSuchElementException ex) {
098: break;
099: }
100:
101: if (toke.equals(delim)) {
102: if (tokeWasDelim)
103: result.addElement("");
104: tokeWasDelim = true;
105: } else {
106: result.addElement(toke);
107: tokeWasDelim = false;
108: }
109: }
110:
111: if (tokeWasDelim)
112: result.addElement("");
113:
114: return result;
115: }
116:
117: public static String join(String[] strings, String sep) {
118: StringBuffer result = new StringBuffer();
119:
120: for (int i = 0; strings != null && i < strings.length; ++i) {
121: if (i > 0)
122: result.append(sep);
123: result.append(strings[i]);
124: }
125:
126: return result.toString();
127: }
128:
129: public static String[] argumentSubstitution(String[] args,
130: Hashtable vars) {
131: StringBuffer argBuf = new StringBuffer();
132:
133: String[] result = new String[args.length];
134:
135: for (int aIdx = 0; aIdx < args.length; ++aIdx) {
136: String argStr = args[aIdx];
137:
138: int index = argStr.indexOf('$');
139:
140: if (index < 0) {
141: result[aIdx] = argStr;
142: continue;
143: } else {
144: result[aIdx] = StringUtilities.stringSubstitution(
145: argStr, vars);
146: }
147: }
148:
149: return result;
150: }
151:
152: public static String stringSubstitution(String argStr,
153: Hashtable vars) {
154: StringBuffer argBuf = new StringBuffer();
155:
156: for (int cIdx = 0; cIdx < argStr.length();) {
157: char ch = argStr.charAt(cIdx);
158:
159: switch (ch) {
160: case '$':
161: StringBuffer nameBuf = new StringBuffer();
162: for (++cIdx; cIdx < argStr.length(); ++cIdx) {
163: ch = argStr.charAt(cIdx);
164: if (ch == '_' || Character.isLetterOrDigit(ch))
165: nameBuf.append(ch);
166: else
167: break;
168: }
169:
170: if (nameBuf.length() > 0) {
171: String value = (String) vars
172: .get(nameBuf.toString());
173:
174: if (value != null) {
175: argBuf.append(value);
176: }
177: }
178: break;
179:
180: default:
181: argBuf.append(ch);
182: ++cIdx;
183: break;
184: }
185: }
186:
187: return argBuf.toString();
188: }
189:
190: public static String[] parseArgumentString(String argStr) {
191: String[] result = null;
192:
193: Vector vector = StringUtilities.parseArgumentVector(argStr);
194:
195: if (vector != null && vector.size() > 0) {
196: result = new String[vector.size()];
197: vector.copyInto(result);
198: }
199:
200: return result;
201: }
202:
203: public static Vector parseArgumentVector(String argStr) {
204: Vector result = new Vector();
205: StringBuffer argBuf = new StringBuffer();
206:
207: boolean backSlash = false;
208: boolean matchSglQuote = false;
209: boolean matchDblQuote = false;
210:
211: for (int cIdx = 0; cIdx < argStr.length(); ++cIdx) {
212: char ch = argStr.charAt(cIdx);
213:
214: switch (ch) {
215: //
216: // W H I T E S P A C E
217: //
218: case ' ':
219: case '\t':
220: case '\n':
221: case '\r':
222: if (backSlash) {
223: argBuf.append(ch);
224: backSlash = false;
225: } else if (matchSglQuote || matchDblQuote) {
226: argBuf.append(ch);
227: } else if (argBuf.length() > 0) {
228: result.addElement(argBuf.toString());
229: argBuf.setLength(0);
230: }
231: break;
232:
233: case '\\':
234: if (backSlash) {
235: argBuf.append("\\");
236: }
237: backSlash = !backSlash;
238: break;
239:
240: case '\'':
241: if (backSlash) {
242: argBuf.append("'");
243: backSlash = false;
244: } else if (matchSglQuote) {
245: result.addElement(argBuf.toString());
246: argBuf.setLength(0);
247: matchSglQuote = false;
248: } else if (!matchDblQuote) {
249: matchSglQuote = true;
250: }
251: break;
252:
253: case '"':
254: if (backSlash) {
255: argBuf.append("\"");
256: backSlash = false;
257: } else if (matchDblQuote) {
258: result.addElement(argBuf.toString());
259: argBuf.setLength(0);
260: matchDblQuote = false;
261: } else if (!matchSglQuote) {
262: matchDblQuote = true;
263: }
264: break;
265:
266: default:
267: if (backSlash) {
268: switch (ch) {
269: case 'b':
270: argBuf.append('\b');
271: break;
272: case 'f':
273: argBuf.append('\f');
274: break;
275: case 'n':
276: argBuf.append('\n');
277: break;
278: case 'r':
279: argBuf.append('\r');
280: break;
281: case 't':
282: argBuf.append('\t');
283: break;
284:
285: default:
286: char ch2 = argStr.charAt(cIdx + 1);
287: char ch3 = argStr.charAt(cIdx + 2);
288: if ((ch >= '0' && ch <= '7')
289: && (ch2 >= '0' && ch2 <= '7')
290: && (ch3 >= '0' && ch3 <= '7')) {
291: int octal = (((ch - '0') * 64)
292: + ((ch2 - '0') * 8) + (ch3 - '0'));
293: argBuf.append((char) octal);
294: cIdx += 2;
295: } else if (ch == '0') {
296: argBuf.append('\0');
297: } else {
298: argBuf.append(ch);
299: }
300: break;
301: }
302: } else {
303: argBuf.append(ch);
304: }
305:
306: backSlash = false;
307: break;
308: }
309: }
310:
311: if (argBuf.length() > 0) {
312: result.addElement(argBuf.toString());
313: }
314:
315: return result;
316: }
317:
318: }
|