001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.util;
028:
029: import java.util.Vector;
030:
031: /** Utility knife for efficiently parsing stuff out of strings.
032: **/
033:
034: public class StringUtility {
035:
036: /** an empty String instance **/
037: public static final String emptyString = "";
038:
039: /** @return an array of strings which represents the comma-separated-
040: * values list from the range of characters in buf.
041: * @deprecated Use {@link CSVUtility#parse(String)}
042: **/
043: public static Vector parseCSV(String buf, int start, int end,
044: char comma) {
045: Vector tmp = new Vector();
046:
047: StringBuffer sb = new StringBuffer(); // temp storage
048:
049: int i = start;
050: int lc = -1; // last comma index
051: boolean white = true;
052: for (; i < end; i++) {
053: char c = buf.charAt(i);
054: if (c == comma) {
055: tmp.addElement(trimStringBufferRight(sb));
056: sb.setLength(0);
057: lc = i;
058: white = true;
059: } else if (c == '\\') {
060: i++;
061: c = buf.charAt(i);
062: sb.append(c);
063: white = false;
064: } else if (!white) {
065: sb.append(c);
066: } else if (!Character.isWhitespace(c)) {
067: sb.append(c);
068: white = false;
069: } else {
070: // white and still reading initial white space
071: }
072: }
073: if (sb.length() > 0) {
074: // still chars in the buffer?
075: tmp.addElement(trimStringBufferRight(sb));
076: } else if (lc > -1 && i > lc) {
077: tmp.addElement(emptyString);
078: }
079:
080: return tmp;
081: }
082:
083: public static String trimStringBufferRight(StringBuffer sb) {
084: int l = sb.length();
085:
086: // if empty, return an empty string
087: if (l == 0)
088: return emptyString;
089:
090: // skip trailing whitespace
091: int j;
092: for (j = (l - 1); j >= 0; j--) {
093: if (!Character.isWhitespace(sb.charAt(j))) {
094: break;
095: }
096: }
097:
098: // if only whitespace return empty
099: if (j < 0)
100: return emptyString;
101:
102: sb.setLength(j + 1);
103:
104: return sb.toString();
105: }
106:
107: static Vector toVector(String[] ss) {
108: Vector r = new Vector(ss.length);
109: for (int i = 0; i < ss.length; i++) {
110: r.add(ss[i]);
111: }
112: return r;
113: }
114:
115: /** @deprecated Use {@link CSVUtility#parse(String)} **/
116: public static Vector parseCSV(String buf) {
117: return toVector(CSVUtility.parse(buf));
118: }
119:
120: /** @deprecated Use {@link CSVUtility#parse(String)} **/
121: public static Vector parseCSV(String buf, int start) {
122: return toVector(CSVUtility.parse(buf.substring(start)));
123: }
124:
125: /** @deprecated Use {@link CSVUtility#parse(String)} **/
126: public static Vector parseCSV(String buf, int start, int end) {
127: return toVector(CSVUtility.parse(buf.substring(start, end)));
128: }
129:
130: /** @deprecated Use {@link CSVUtility#parse(String)} or {@link String#split(String)} **/
131: public static Vector parseCSV(String buf, char comma) {
132: if (comma == ',') {
133: return parseCSV(buf);
134: } else {
135: return toVector(buf.split("\\s*" + comma + "\\s*"));
136: }
137: }
138:
139: /** @deprecated Use {@link String#split(String)} **/
140: public static Vector explode(String s) {
141: Vector v = new Vector();
142: int k = 0; // char after last white
143: int l = s.length();
144: int i = 0;
145: while (i < l) {
146: if (Character.isWhitespace(s.charAt(i))) {
147: // is white - what do we do?
148: if (i == k) { // skipping contiguous white
149: k++;
150: } else { // last char wasn't white - word boundary!
151: v.addElement(s.substring(k, i));
152: k = i + 1;
153: }
154: } else { // nonwhite
155: // let it advance
156: }
157: i++;
158: }
159: if (k != i) { // leftover non-white chars
160: v.addElement(s.substring(k, i));
161: }
162: return v;
163: }
164:
165: /**
166: * Prefix all lines of "s" with "prefix", e.g.<br>
167: * <code>prefixLines("X: ", "a\nb")</code><br>
168: * returns<br>
169: * <code>"X: a\nA: b"</code><br>
170: */
171: public static String prefixLines(String prefix, String s) {
172: StringBuffer sb = new StringBuffer();
173: int j = s.indexOf('\n');
174: if (j < 0) {
175: sb.append(prefix).append(s);
176: } else {
177: int i = 0;
178: do {
179: sb.append(prefix).append(s.substring(i, ++j));
180: i = j;
181: j = s.indexOf('\n', i);
182: } while (j >= 0);
183: sb.append(prefix).append(s.substring(i));
184: }
185: return sb.toString();
186: }
187:
188: public static String arrayToString(Object[] array) {
189: return appendArray(new StringBuffer(), array).substring(0);
190: }
191:
192: public static StringBuffer appendArray(StringBuffer buf,
193: Object[] array) {
194: buf.append('[');
195: for (int i = 0; i < array.length; i++) {
196: if (i > 0)
197: buf.append(',');
198: buf.append(array[i]);
199: }
200: buf.append(']');
201: return buf;
202: }
203: }
|