KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > applications > StringParser


1 /*
2  * $Header$
3  * $Revision: 5379 $
4  * $Date: 2003-11-05 19:41:33 +0100 (Wed, 05 Nov 2003) $
5  *
6  * ====================================================================
7  *
8  * The Apache Software License, Version 1.1
9  *
10  * Copyright (c) 1999 The Apache Software Foundation. All rights
11  * reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in
22  * the documentation and/or other materials provided with the
23  * distribution.
24  *
25  * 3. The end-user documentation included with the redistribution, if
26  * any, must include the following acknowlegement:
27  * "This product includes software developed by the
28  * Apache Software Foundation (http://www.apache.org/)."
29  * Alternately, this acknowlegement may appear in the software itself,
30  * if and wherever such third-party acknowlegements normally appear.
31  *
32  * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
33  * Foundation" must not be used to endorse or promote products derived
34  * from this software without prior written permission. For written
35  * permission, please contact apache@apache.org.
36  *
37  * 5. Products derived from this software may not be called "Apache"
38  * nor may "Apache" appear in their names without prior written
39  * permission of the Apache Group.
40  *
41  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  * ====================================================================
54  *
55  * This software consists of voluntary contributions made by many
56  * individuals on behalf of the Apache Software Foundation. For more
57  * information on the Apache Software Foundation, please see
58  * <http://www.apache.org/>.
59  *
60  * [Additional notices, if required by prior licensing conditions]
61  *
62  */

63
64
65 package org.jahia.services.applications;
66
67
68 /**
69  * Utility class for string parsing that is higher performance than
70  * StringParser for simple delimited text cases. Parsing is performed
71  * by setting the string, and then using the <code>findXxxx()</code> and
72  * <code>skipXxxx()</code> families of methods to remember significant
73  * offsets. To retrieve the parsed substrings, call the <code>extract()</code>
74  * method with the appropriate saved offset values.
75  *
76  * @author Craig R. McClanahan
77  * @version $Revision: 5379 $ $Date: 2003-11-05 19:41:33 +0100 (Wed, 05 Nov 2003) $
78  */

79
80 public final class StringParser {
81
82
83     // ----------------------------------------------------------- Constructors
84

85
86     /**
87      * Construct a string parser with no preset string to be parsed.
88      */

89     public StringParser () {
90
91         this (null);
92
93     }
94
95
96     /**
97      * Construct a string parser that is initialized to parse the specified
98      * string.
99      *
100      * @param string The string to be parsed
101      */

102     public StringParser (String JavaDoc string) {
103
104         super ();
105         setString (string);
106
107     }
108
109
110     // ----------------------------------------------------- Instance Variables
111

112
113     /**
114      * The characters of the current string, as a character array. Stored
115      * when the string is first specified to speed up access to characters
116      * being compared during parsing.
117      */

118     private char chars[] = null;
119
120
121     /**
122      * The zero-relative index of the current point at which we are
123      * positioned within the string being parsed. <strong>NOTE</strong>:
124      * the value of this index can be one larger than the index of the last
125      * character of the string (i.e. equal to the string length) if you
126      * parse off the end of the string. This value is useful for extracting
127      * substrings that include the end of the string.
128      */

129     private int index = 0;
130
131
132     /**
133      * The length of the String we are currently parsing. Stored when the
134      * string is first specified to avoid repeated recalculations.
135      */

136     private int length = 0;
137
138
139     /** The String we are currently parsing. */
140     private String JavaDoc string = null;
141
142
143     // ------------------------------------------------------------- Properties
144

145
146     /**
147      * Return the zero-relative index of our current parsing position
148      * within the string being parsed.
149      */

150     public int getIndex () {
151
152         return (this.index);
153
154     }
155
156
157     /**
158      * Return the length of the string we are parsing.
159      */

160     public int getLength () {
161
162         return (this.length);
163
164     }
165
166
167     /**
168      * Return the String we are currently parsing.
169      */

170     public String JavaDoc getString () {
171
172         return (this.string);
173
174     }
175
176
177     /**
178      * Set the String we are currently parsing. The parser state is also reset
179      * to begin at the start of this string.
180      *
181      * @param string The string to be parsed.
182      */

183     public void setString (String JavaDoc string) {
184
185         this.string = string;
186         if (string != null) {
187             this.length = string.length ();
188             chars = this.string.toCharArray ();
189         } else {
190             this.length = 0;
191             chars = new char[0];
192         }
193         reset ();
194
195     }
196
197
198     // --------------------------------------------------------- Public Methods
199

200
201     /**
202      * Advance the current parsing position by one, if we are not already
203      * past the end of the string.
204      */

205     public void advance () {
206
207         if (index < length)
208             index++;
209
210     }
211
212
213     /**
214      * Extract and return a substring that starts at the specified position,
215      * and extends to the end of the string being parsed. If this is not
216      * possible, a zero-length string is returned.
217      *
218      * @param start Starting index, zero relative, inclusive
219      */

220     public String JavaDoc extract (int start) {
221
222         if ((start < 0) || (start >= length))
223             return ("");
224         else
225             return (string.substring (start));
226
227     }
228
229
230     /**
231      * Extract and return a substring that starts at the specified position,
232      * and ends at the character before the specified position. If this is
233      * not possible, a zero-length string is returned.
234      *
235      * @param start Starting index, zero relative, inclusive
236      * @param end Ending index, zero relative, exclusive
237      */

238     public String JavaDoc extract (int start, int end) {
239
240         if ((start < 0) || (start >= end) || (end > length))
241             return ("");
242         else
243             return (string.substring (start, end));
244
245     }
246
247
248     /**
249      * Return the index of the next occurrence of the specified character,
250      * or the index of the character after the last position of the string
251      * if no more occurrences of this character are found. The current
252      * parsing position is updated to the returned value.
253      *
254      * @param ch Character to be found
255      */

256     public int findChar (char ch) {
257
258         while ((index < length) && (ch != chars[index]))
259             index++;
260         return (index);
261
262     }
263
264
265     /**
266      * Return the index of the next occurrence of a non-whitespace character,
267      * or the index of the character after the last position of the string
268      * if no more non-whitespace characters are found. The current
269      * parsing position is updated to the returned value.
270      */

271     public int findText () {
272
273         while ((index < length) && isWhite (chars[index]))
274             index++;
275         return (index);
276
277     }
278
279
280     /**
281      * Return the index of the next occurrence of a whitespace character,
282      * or the index of the character after the last position of the string
283      * if no more whitespace characters are found. The current parsing
284      * position is updated to the returned value.
285      */

286     public int findWhite () {
287
288         while ((index < length) && !isWhite (chars[index]))
289             index++;
290         return (index);
291
292     }
293
294
295     /**
296      * Reset the current state of the parser to the beginning of the
297      * current string being parsed.
298      */

299     public void reset () {
300
301         index = 0;
302
303     }
304
305
306     /**
307      * Advance the current parsing position while it is pointing at the
308      * specified character, or until it moves past the end of the string.
309      * Return the final value.
310      *
311      * @param ch Character to be skipped
312      */

313     public int skipChar (char ch) {
314
315         while ((index < length) && (ch == chars[index]))
316             index++;
317         return (index);
318
319     }
320
321
322     /**
323      * Advance the current parsing position while it is pointing at a
324      * non-whitespace character, or until it moves past the end of the string.
325      * Return the final value.
326      */

327     public int skipText () {
328
329         while ((index < length) && !isWhite (chars[index]))
330             index++;
331         return (index);
332
333     }
334
335
336     /**
337      * Advance the current parsing position while it is pointing at a
338      * whitespace character, or until it moves past the end of the string.
339      * Return the final value.
340      */

341     public int skipWhite () {
342
343         while ((index < length) && isWhite (chars[index]))
344             index++;
345         return (index);
346
347     }
348
349
350     // ------------------------------------------------------ Protected Methods
351

352
353     /**
354      * Is the specified character considered to be whitespace?
355      *
356      * @param ch Character to be checked
357      */

358     protected boolean isWhite (char ch) {
359
360         if ((ch == ' ') || (ch == '\t') || (ch == '\r') || (ch == '\n'))
361             return (true);
362         else
363             return (false);
364
365     }
366
367
368 }
369
Popular Tags