KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > util > StringParser


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.catalina.util;
20
21
22 /**
23  * Utility class for string parsing that is higher performance than
24  * StringParser for simple delimited text cases. Parsing is performed
25  * by setting the string, and then using the <code>findXxxx()</code> and
26  * <code>skipXxxx()</code> families of methods to remember significant
27  * offsets. To retrieve the parsed substrings, call the <code>extract()</code>
28  * method with the appropriate saved offset values.
29  *
30  * @author Craig R. McClanahan
31  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
32  */

33
34 public final class StringParser {
35
36
37     // ----------------------------------------------------------- Constructors
38

39
40     /**
41      * Construct a string parser with no preset string to be parsed.
42      */

43     public StringParser() {
44
45         this(null);
46
47     }
48
49
50     /**
51      * Construct a string parser that is initialized to parse the specified
52      * string.
53      *
54      * @param string The string to be parsed
55      */

56     public StringParser(String JavaDoc string) {
57
58         super();
59         setString(string);
60
61     }
62
63
64     // ----------------------------------------------------- Instance Variables
65

66
67     /**
68      * The characters of the current string, as a character array. Stored
69      * when the string is first specified to speed up access to characters
70      * being compared during parsing.
71      */

72     private char chars[] = null;
73
74
75     /**
76      * The zero-relative index of the current point at which we are
77      * positioned within the string being parsed. <strong>NOTE</strong>:
78      * the value of this index can be one larger than the index of the last
79      * character of the string (i.e. equal to the string length) if you
80      * parse off the end of the string. This value is useful for extracting
81      * substrings that include the end of the string.
82      */

83     private int index = 0;
84
85
86     /**
87      * The length of the String we are currently parsing. Stored when the
88      * string is first specified to avoid repeated recalculations.
89      */

90     private int length = 0;
91
92
93     /**
94      * The String we are currently parsing.
95      */

96     private String JavaDoc string = null;
97
98
99     // ------------------------------------------------------------- Properties
100

101
102     /**
103      * Return the zero-relative index of our current parsing position
104      * within the string being parsed.
105      */

106     public int getIndex() {
107
108         return (this.index);
109
110     }
111
112
113     /**
114      * Return the length of the string we are parsing.
115      */

116     public int getLength() {
117
118         return (this.length);
119
120     }
121
122
123     /**
124      * Return the String we are currently parsing.
125      */

126     public String JavaDoc getString() {
127
128         return (this.string);
129
130     }
131
132
133     /**
134      * Set the String we are currently parsing. The parser state is also reset
135      * to begin at the start of this string.
136      *
137      * @param string The string to be parsed.
138      */

139     public void setString(String JavaDoc string) {
140
141         this.string = string;
142         if (string != null) {
143             this.length = string.length();
144             chars = this.string.toCharArray();
145         } else {
146             this.length = 0;
147             chars = new char[0];
148         }
149         reset();
150
151     }
152
153
154     // --------------------------------------------------------- Public Methods
155

156
157     /**
158      * Advance the current parsing position by one, if we are not already
159      * past the end of the string.
160      */

161     public void advance() {
162
163         if (index < length)
164             index++;
165
166     }
167
168
169     /**
170      * Extract and return a substring that starts at the specified position,
171      * and extends to the end of the string being parsed. If this is not
172      * possible, a zero-length string is returned.
173      *
174      * @param start Starting index, zero relative, inclusive
175      */

176     public String JavaDoc extract(int start) {
177
178         if ((start < 0) || (start >= length))
179             return ("");
180         else
181             return (string.substring(start));
182
183     }
184
185
186     /**
187      * Extract and return a substring that starts at the specified position,
188      * and ends at the character before the specified position. If this is
189      * not possible, a zero-length string is returned.
190      *
191      * @param start Starting index, zero relative, inclusive
192      * @param end Ending index, zero relative, exclusive
193      */

194     public String JavaDoc extract(int start, int end) {
195
196         if ((start < 0) || (start >= end) || (end > length))
197             return ("");
198         else
199             return (string.substring(start, end));
200
201     }
202
203
204     /**
205      * Return the index of the next occurrence of the specified character,
206      * or the index of the character after the last position of the string
207      * if no more occurrences of this character are found. The current
208      * parsing position is updated to the returned value.
209      *
210      * @param ch Character to be found
211      */

212     public int findChar(char ch) {
213
214         while ((index < length) && (ch != chars[index]))
215             index++;
216         return (index);
217
218     }
219
220
221     /**
222      * Return the index of the next occurrence of a non-whitespace character,
223      * or the index of the character after the last position of the string
224      * if no more non-whitespace characters are found. The current
225      * parsing position is updated to the returned value.
226      */

227     public int findText() {
228
229         while ((index < length) && isWhite(chars[index]))
230             index++;
231         return (index);
232
233     }
234
235
236     /**
237      * Return the index of the next occurrence of a whitespace character,
238      * or the index of the character after the last position of the string
239      * if no more whitespace characters are found. The current parsing
240      * position is updated to the returned value.
241      */

242     public int findWhite() {
243
244         while ((index < length) && !isWhite(chars[index]))
245             index++;
246         return (index);
247
248     }
249
250
251     /**
252      * Reset the current state of the parser to the beginning of the
253      * current string being parsed.
254      */

255     public void reset() {
256
257         index = 0;
258
259     }
260
261
262     /**
263      * Advance the current parsing position while it is pointing at the
264      * specified character, or until it moves past the end of the string.
265      * Return the final value.
266      *
267      * @param ch Character to be skipped
268      */

269     public int skipChar(char ch) {
270
271         while ((index < length) && (ch == chars[index]))
272             index++;
273         return (index);
274
275     }
276
277
278     /**
279      * Advance the current parsing position while it is pointing at a
280      * non-whitespace character, or until it moves past the end of the string.
281      * Return the final value.
282      */

283     public int skipText() {
284
285         while ((index < length) && !isWhite(chars[index]))
286             index++;
287         return (index);
288
289     }
290
291
292     /**
293      * Advance the current parsing position while it is pointing at a
294      * whitespace character, or until it moves past the end of the string.
295      * Return the final value.
296      */

297     public int skipWhite() {
298
299         while ((index < length) && isWhite(chars[index]))
300             index++;
301         return (index);
302
303     }
304
305
306     // ------------------------------------------------------ Protected Methods
307

308
309     /**
310      * Is the specified character considered to be whitespace?
311      *
312      * @param ch Character to be checked
313      */

314     protected boolean isWhite(char ch) {
315
316         if ((ch == ' ') || (ch == '\t') || (ch == '\r') || (ch == '\n'))
317             return (true);
318         else
319             return (false);
320
321     }
322
323
324 }
325
Popular Tags