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:
029: static public String[] splitString(String splitStr, String delim) {
030: int i, count;
031: String[] result;
032: StringTokenizer toker;
033:
034: toker = new StringTokenizer(splitStr, delim);
035:
036: count = toker.countTokens();
037:
038: result = new String[count];
039:
040: for (i = 0; i < count; ++i) {
041: try {
042: result[i] = toker.nextToken();
043: } catch (NoSuchElementException ex) {
044: result = null;
045: break;
046: }
047: }
048:
049: return result;
050: }
051:
052: public static String[] argumentSubstitution(String[] args,
053: Hashtable vars) {
054: StringBuffer argBuf = new StringBuffer();
055:
056: String[] result = new String[args.length];
057:
058: for (int aIdx = 0; aIdx < args.length; ++aIdx) {
059: String argStr = args[aIdx];
060:
061: int index = argStr.indexOf('$');
062:
063: if (index < 0) {
064: result[aIdx] = argStr;
065: continue;
066: } else {
067: result[aIdx] = StringUtilities.stringSubstitution(
068: argStr, vars);
069: }
070: }
071:
072: return result;
073: }
074:
075: public static String stringSubstitution(String argStr,
076: Hashtable vars) {
077: StringBuffer argBuf = new StringBuffer();
078:
079: for (int cIdx = 0; cIdx < argStr.length();) {
080: char ch = argStr.charAt(cIdx);
081:
082: switch (ch) {
083: case '$':
084: StringBuffer nameBuf = new StringBuffer();
085: for (++cIdx; cIdx < argStr.length(); ++cIdx) {
086: ch = argStr.charAt(cIdx);
087: if (ch == '_' || Character.isLetterOrDigit(ch))
088: nameBuf.append(ch);
089: else
090: break;
091: }
092:
093: if (nameBuf.length() > 0) {
094: String value = (String) vars
095: .get(nameBuf.toString());
096:
097: if (value != null) {
098: argBuf.append(value);
099: }
100: }
101: break;
102:
103: default:
104: argBuf.append(ch);
105: ++cIdx;
106: break;
107: }
108: }
109:
110: return argBuf.toString();
111: }
112:
113: public static String[] parseArgumentString(String argStr) {
114: String[] result = null;
115:
116: Vector vector = StringUtilities.parseArgumentVector(argStr);
117:
118: if (vector != null && vector.size() > 0) {
119: result = new String[vector.size()];
120:
121: for (int i = 0; i < result.length; ++i) {
122: result[i] = (String) vector.elementAt(i);
123: }
124: }
125:
126: return result;
127: }
128:
129: public static Vector parseArgumentVector(String argStr) {
130: Vector result = new Vector();
131: StringBuffer argBuf = new StringBuffer();
132:
133: boolean backSlash = false;
134: boolean matchSglQuote = false;
135: boolean matchDblQuote = false;
136:
137: for (int cIdx = 0; cIdx < argStr.length(); ++cIdx) {
138: char ch = argStr.charAt(cIdx);
139:
140: switch (ch) {
141: //
142: // W H I T E S P A C E
143: //
144: case ' ':
145: case '\t':
146: case '\n':
147: case '\r':
148: if (backSlash) {
149: argBuf.append(ch);
150: backSlash = false;
151: } else if (matchSglQuote || matchDblQuote) {
152: argBuf.append(ch);
153: } else if (argBuf.length() > 0) {
154: result.addElement(argBuf.toString());
155: argBuf.setLength(0);
156: }
157: break;
158:
159: case '\\':
160: if (backSlash) {
161: argBuf.append("\\");
162: }
163: backSlash = !backSlash;
164: break;
165:
166: case '\'':
167: if (backSlash) {
168: argBuf.append("'");
169: backSlash = false;
170: } else if (matchSglQuote) {
171: result.addElement(argBuf.toString());
172: argBuf.setLength(0);
173: matchSglQuote = false;
174: } else if (!matchDblQuote) {
175: matchSglQuote = true;
176: }
177: break;
178:
179: case '"':
180: if (backSlash) {
181: argBuf.append("\"");
182: backSlash = false;
183: } else if (matchDblQuote) {
184: result.addElement(argBuf.toString());
185: argBuf.setLength(0);
186: matchDblQuote = false;
187: } else if (!matchSglQuote) {
188: matchDblQuote = true;
189: }
190: break;
191:
192: default:
193: if (backSlash) {
194: switch (ch) {
195: case 'b':
196: argBuf.append('\b');
197: break;
198: case 'f':
199: argBuf.append('\f');
200: break;
201: case 'n':
202: argBuf.append('\n');
203: break;
204: case 'r':
205: argBuf.append('\r');
206: break;
207: case 't':
208: argBuf.append('\t');
209: break;
210:
211: default:
212: char ch2 = argStr.charAt(cIdx + 1);
213: char ch3 = argStr.charAt(cIdx + 2);
214: if ((ch >= '0' && ch <= '7')
215: && (ch2 >= '0' && ch2 <= '7')
216: && (ch3 >= '0' && ch3 <= '7')) {
217: int octal = (((ch - '0') * 64)
218: + ((ch2 - '0') * 8) + (ch3 - '0'));
219: argBuf.append((char) octal);
220: cIdx += 2;
221: } else if (ch == '0') {
222: argBuf.append('\0');
223: } else {
224: argBuf.append(ch);
225: }
226: break;
227: }
228: } else {
229: argBuf.append(ch);
230: }
231:
232: backSlash = false;
233: break;
234: }
235: }
236:
237: if (argBuf.length() > 0) {
238: result.addElement(argBuf.toString());
239: }
240:
241: return result;
242: }
243:
244: }
|