KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > util > CharScanner


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.util;
30
31 /**
32  * A scanner for simple delimiter based parsing.
33  */

34 public class CharScanner {
35   private char []delimiters;
36
37   /**
38    * Creates a compiled character scanner.
39    *
40    * @param delimiters string containing the delimiters
41    */

42   public CharScanner(String JavaDoc delimiters)
43   {
44     this.delimiters = delimiters.toCharArray();
45   }
46
47   /**
48    * Skips characters from the cursor until one matches a delimiter.
49    *
50    * @param cursor CharCursor to scan.
51    *
52    * @return the last character read
53    */

54   public char skip(CharCursor cursor)
55   {
56     char ch;
57     char []delim = delimiters;
58     int len = delim.length;
59
60   loop:
61     for (ch = cursor.current();
62          ch != cursor.DONE;
63          ch = cursor.next()) {
64       for (int i = 0; i < len; i++) {
65         if (delim[i] == ch)
66           continue loop;
67       }
68       return ch;
69     }
70
71     return ch;
72   }
73
74   /**
75    * Scans characters from a cursor, filling a char buffer.
76    *
77    * @param cursor CharCursor to scan.
78    * @param buf CharBuffer to fill.
79    *
80    * @return the last character read
81    */

82   public char scan(CharCursor cursor, CharBuffer buf)
83   {
84     char ch;
85     char []delim = delimiters;
86     int len = delim.length;
87
88     for (ch = cursor.current();
89          ch != cursor.DONE;
90          ch = cursor.next()) {
91       for (int i = 0; i < len; i++) {
92         if (delim[i] == ch)
93           return ch;
94       }
95
96       buf.append(ch);
97     }
98
99     return ch;
100   }
101
102   /**
103    * Scans characters from a cursor, until reaching the delimiter
104    *
105    * @param cursor CharCursor to scan.
106    *
107    * @return the last character read
108    */

109   public char scan(CharCursor cursor)
110   {
111     char ch;
112     char []delim = delimiters;
113     int len = delim.length;
114
115     for (ch = cursor.current();
116          ch != cursor.DONE;
117          ch = cursor.next()) {
118       for (int i = 0; i < len; i++) {
119         if (delim[i] == ch)
120           return ch;
121       }
122     }
123
124     return ch;
125   }
126 }
127
Popular Tags