001: package net.sf.saxon.expr;
002:
003: import net.sf.saxon.om.Item;
004: import net.sf.saxon.om.SequenceIterator;
005: import net.sf.saxon.value.StringValue;
006:
007: import java.util.StringTokenizer;
008:
009: /**
010: * StringTokenIterator: breaks a string up into tokens,
011: * and returns the tokens as a sequence of strings.
012: */
013:
014: public class StringTokenIterator implements SequenceIterator {
015:
016: private String theString;
017: private String delimiters; // null implies use whitespace as delimiter
018: private StringTokenizer tokenizer;
019: private String current;
020: private int position = 0;
021:
022: /**
023: * Construct a StringTokenIterator that will break the supplied
024: * string into tokens at whitespace boundaries
025: */
026:
027: public StringTokenIterator(String string) {
028: theString = string;
029: delimiters = null;
030: tokenizer = new StringTokenizer(string);
031: }
032:
033: /**
034: * Construct a StringTokenIterator that will break the supplied
035: * string into tokens at any of the delimiter characters included in the
036: * delimiter string.
037: */
038:
039: public StringTokenIterator(String string, String delimiters) {
040: theString = string;
041: this .delimiters = delimiters;
042: tokenizer = new StringTokenizer(string, delimiters);
043: }
044:
045: public Item next() {
046: if (tokenizer.hasMoreElements()) {
047: current = (String) tokenizer.nextElement();
048: position++;
049: return new StringValue(current);
050: } else {
051: current = null;
052: position = -1;
053: return null;
054: }
055: }
056:
057: public Item current() {
058: return (current == null ? null : new StringValue(current));
059: }
060:
061: public int position() {
062: return position;
063: }
064:
065: public SequenceIterator getAnother() {
066: if (delimiters == null) {
067: return new StringTokenIterator(theString);
068: } else {
069: return new StringTokenIterator(theString, delimiters);
070: }
071: }
072:
073: /**
074: * Get properties of this iterator, as a bit-significant integer.
075: *
076: * @return the properties of this iterator. This will be some combination of
077: * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
078: * and {@link LOOKAHEAD}. It is always
079: * acceptable to return the value zero, indicating that there are no known special properties.
080: * It is acceptable for the properties of the iterator to change depending on its state.
081: */
082:
083: public int getProperties() {
084: return 0;
085: }
086: }
087:
088: //
089: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
090: // you may not use this file except in compliance with the License. You may obtain a copy of the
091: // License at http://www.mozilla.org/MPL/
092: //
093: // Software distributed under the License is distributed on an "AS IS" basis,
094: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
095: // See the License for the specific language governing rights and limitations under the License.
096: //
097: // The Original Code is: all this file.
098: //
099: // The Initial Developer of the Original Code is Michael H. Kay.
100: //
101: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
102: //
103: // Contributor(s): none.
104: //
|