KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > expr > StringTokenIterator


1 package net.sf.saxon.expr;
2 import net.sf.saxon.om.Item;
3 import net.sf.saxon.om.SequenceIterator;
4 import net.sf.saxon.value.StringValue;
5
6 import java.util.StringTokenizer JavaDoc;
7
8 /**
9 * StringTokenIterator: breaks a string up into tokens,
10 * and returns the tokens as a sequence of strings.
11 */

12
13 public class StringTokenIterator implements SequenceIterator {
14
15     private String JavaDoc theString;
16     private String JavaDoc delimiters; // null implies use whitespace as delimiter
17
private StringTokenizer JavaDoc tokenizer;
18     private String JavaDoc current;
19     private int position = 0;
20
21     /**
22     * Construct a StringTokenIterator that will break the supplied
23     * string into tokens at whitespace boundaries
24     */

25
26     public StringTokenIterator (String JavaDoc string) {
27         theString = string;
28         delimiters = null;
29         tokenizer = new StringTokenizer JavaDoc(string);
30     }
31
32     /**
33     * Construct a StringTokenIterator that will break the supplied
34     * string into tokens at any of the delimiter characters included in the
35     * delimiter string.
36     */

37
38     public StringTokenIterator (String JavaDoc string, String JavaDoc delimiters) {
39         theString = string;
40         this.delimiters = delimiters;
41         tokenizer = new StringTokenizer JavaDoc(string, delimiters);
42     }
43
44     public Item next() {
45         if (tokenizer.hasMoreElements()) {
46             current = (String JavaDoc)tokenizer.nextElement();
47             position++;
48             return new StringValue(current);
49         } else {
50             current = null;
51             position = -1;
52             return null;
53         }
54     }
55
56     public Item current() {
57         return (current == null ? null : new StringValue(current));
58     }
59
60     public int position() {
61         return position;
62     }
63
64     public SequenceIterator getAnother() {
65         if (delimiters==null) {
66             return new StringTokenIterator(theString);
67         } else {
68             return new StringTokenIterator(theString, delimiters);
69         }
70     }
71
72     /**
73      * Get properties of this iterator, as a bit-significant integer.
74      *
75      * @return the properties of this iterator. This will be some combination of
76      * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
77      * and {@link LOOKAHEAD}. It is always
78      * acceptable to return the value zero, indicating that there are no known special properties.
79      * It is acceptable for the properties of the iterator to change depending on its state.
80      */

81
82     public int getProperties() {
83         return 0;
84     }
85 }
86
87 //
88
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
89
// you may not use this file except in compliance with the License. You may obtain a copy of the
90
// License at http://www.mozilla.org/MPL/
91
//
92
// Software distributed under the License is distributed on an "AS IS" basis,
93
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
94
// See the License for the specific language governing rights and limitations under the License.
95
//
96
// The Original Code is: all this file.
97
//
98
// The Initial Developer of the Original Code is Michael H. Kay.
99
//
100
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
101
//
102
// Contributor(s): none.
103
//
104
Popular Tags