KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > util > StringListParser


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.util;
13
14 import java.util.Map JavaDoc;
15
16 /**
17  * This class is a simple lexical analyzer for a list of Strings, ints and
18  * doubles separated by a delim character. It is much faster than
19  * StringTokenizer for lists of numbers etc as it does not create a String
20  * for each token.
21  */

22 public final class StringListParser {
23
24     public static final char DEFAULT_DELIM = ',';
25
26     private String JavaDoc s;
27     private int slen;
28     private char delim;
29     private int pos;
30
31     public StringListParser(char delim) {
32         this.delim = delim;
33     }
34
35     public StringListParser(String JavaDoc s, char delim) {
36         this.delim = delim;
37         setString(s);
38     }
39
40     public StringListParser(String JavaDoc s) {
41         this(s, DEFAULT_DELIM);
42     }
43
44     public StringListParser() {
45         this(DEFAULT_DELIM);
46     }
47
48     /**
49      * Set the String we are parsing.
50      */

51     public void setString(String JavaDoc s) {
52         this.s = s;
53         pos = 0;
54         slen = s.length();
55     }
56
57     /**
58      * Get the String we are parsing.
59      */

60     public String JavaDoc getString() {
61         return s;
62     }
63
64     /**
65      * Get the next String from the string.
66      *
67      * @throws IllegalStateException if there are no more or invalid format
68      */

69     public String JavaDoc nextString() throws IllegalStateException JavaDoc {
70         if (!hasNext()) {
71             throw new IllegalStateException JavaDoc("Expected String at end: " + s);
72         }
73         int start = pos;
74         for (; pos < slen;) {
75             char c = s.charAt(pos++);
76             if (c == delim) return s.substring(start, pos - 1);
77         }
78         return s.substring(start);
79     }
80
81     /**
82      * Get the next double quoted String from the string.
83      *
84      * @throws IllegalStateException if there are no more or invalid format
85      */

86     public String JavaDoc nextQuotedString() throws IllegalStateException JavaDoc {
87         if (!hasNext()) {
88             throw new IllegalStateException JavaDoc(
89                     "Expected quoted String at end: " + s);
90         }
91         if (s.charAt(pos) == '-') {
92             if (++pos < slen) ++pos; // skip the delim
93
return null;
94         }
95         StringBuffer JavaDoc a = new StringBuffer JavaDoc();
96         ++pos; // skip the opening double quote
97
for (; pos < slen;) {
98             char c = s.charAt(pos++);
99             if (c == '"') {
100                 if (pos >= slen) break;
101                 c = s.charAt(pos++);
102                 if (c != '"') break;
103             }
104             a.append(c);
105         }
106         return a.toString();
107     }
108
109     /**
110      * Get the next Class from the string.
111      *
112      * @throws IllegalStateException if there are no more or invalid format
113      */

114     public Class JavaDoc nextClass(ClassLoader JavaDoc cl)
115             throws IllegalStateException JavaDoc, ClassNotFoundException JavaDoc {
116         if (!hasNext()) {
117             throw new IllegalStateException JavaDoc("Expected class name at end: " + s);
118         }
119         if (s.charAt(pos) == '-') {
120             if (++pos < slen) ++pos; // skip the delim
121
return null;
122         }
123         int start = pos;
124         for (; pos < slen;) {
125             char c = s.charAt(pos++);
126             if (c == delim) break;
127         }
128         String JavaDoc name = s.substring(start, pos - 1);
129         return BeanUtils.loadClass(name, true, cl);
130     }
131
132     /**
133      * Get the next int from the string.
134      *
135      * @throws IllegalStateException if there are no more or invalid format
136      */

137     public int nextInt() throws IllegalStateException JavaDoc {
138         if (!hasNext()) {
139             throw new IllegalStateException JavaDoc("Expected int at end: " + s);
140         }
141         int ibuf = 0;
142         boolean neg = false;
143         if (pos < slen && s.charAt(pos) == '-') {
144             neg = true;
145             pos++;
146         }
147         for (; pos < slen;) {
148             char c = s.charAt(pos++);
149             if (c >= '0' && c <= '9') {
150                 ibuf = ibuf * 10 + (c - '0');
151                 //cat.debug("c = '" + c + "' ibuf " + ibuf);
152
} else if (c == delim) {
153                 break;
154             } else {
155                 throw new IllegalStateException JavaDoc(
156                         "Expected int at pos " + (pos - 1) + ": " + s);
157             }
158         }
159         return neg ? -ibuf : ibuf;
160     }
161
162     /**
163      * Get the next boolean from the string.
164      *
165      * @throws IllegalStateException if there are no more or invalid format
166      */

167     public boolean nextBoolean() {
168         if (!hasNext()) {
169             throw new IllegalStateException JavaDoc("Expected boolean at end: " + s);
170         }
171         char c = s.charAt(pos++);
172         if (pos < slen) pos++; // skip the delim
173
if (c == 'Y') {
174             return true;
175         } else if (c == 'N') return false;
176         throw new IllegalStateException JavaDoc("Invalid boolean character '" + c +
177                 "' in " + s);
178     }
179
180     /**
181      * Get the next double from the string.
182      *
183      * @throws IllegalStateException if there are no more or invalid format
184      */

185     public double nextDouble() throws IllegalStateException JavaDoc {
186         if (!hasNext()) {
187             throw new IllegalStateException JavaDoc("Expected double at end: " + s);
188         }
189         double dbuf = 0.0;
190         boolean neg = false;
191         if (pos < slen && s.charAt(pos) == '-') {
192             neg = true;
193             pos++;
194         }
195         for (; pos < slen;) {
196             char c = s.charAt(pos++);
197             if (c >= '0' && c <= '9') {
198                 dbuf = dbuf * 10.0 + (c - '0');
199             } else if (c == delim) {
200                 return neg ? -dbuf : dbuf;
201             } else if (c == '.') {
202                 break;
203             } else {
204                 throw new IllegalStateException JavaDoc(
205                         "Expected double at pos " + (pos - 1) + ": " + s);
206             }
207         }
208         double m = 10.0;
209         double f = 0.0;
210         for (; pos < slen;) {
211             char c = s.charAt(pos++);
212             if (c >= '0' && c <= '9') {
213                 f = f * 10.0 + (c - '0');
214                 m = m * 10.0;
215             } else if (c == delim) {
216                 break;
217             } else {
218                 throw new IllegalStateException JavaDoc(
219                         "Expected double at pos " + (pos - 1) + ": " + s);
220             }
221         }
222         dbuf = dbuf + f / m;
223         return neg ? -dbuf : dbuf;
224     }
225
226     /**
227      * Read the rest of the String as key,value pairs.
228      */

229     public void nextProperties(Map JavaDoc map) {
230         for (; hasNext();) {
231             String JavaDoc key = nextString();
232             String JavaDoc value = nextQuotedString();
233             map.put(key, value);
234         }
235     }
236
237     /**
238      * Are there more Strings, ints or doubles?
239      */

240     public final boolean hasNext() {
241         return pos < slen;
242     }
243
244     public void nextProperties(IntObjectHashMap map) {
245         for (; hasNext();) {
246             int key = nextInt();
247             String JavaDoc value = nextQuotedString();
248             map.put(key, value);
249         }
250     }
251 }
252
Popular Tags