| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.regex; |
| 9 | |
| 10 | import net.sf.saxon.om.SequenceIterator; |
| 11 | import net.sf.saxon.value.StringValue; |
| 12 | |
| 13 | import java.util.regex.Matcher; |
| 14 | import java.util.regex.Pattern; |
| 15 | |
| 16 | //import java.util.regex.Matcher; |
| 17 | //import java.util.regex.Pattern; |
| 18 | |
| 19 | /** |
| 20 | * A JTokenIterator is an iterator over the strings that result from tokenizing a string using |
| 21 | * a regular expression, in this case a regular expression evaluated using the JDK regex engine |
| 22 | */ |
| 23 | |
| 24 | public class JTokenIterator implements SequenceIterator<StringValue> { |
| 25 | |
| 26 | private CharSequence input; |
| 27 | private Pattern pattern; |
| 28 | private Matcher matcher; |
| 29 | /*@Nullable*/ private CharSequence current; |
| 30 | private int prevEnd = 0; |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * Construct a JTokenIterator. |
| 35 | */ |
| 36 | |
| 37 | public JTokenIterator(CharSequence input, Pattern pattern) { |
| 38 | this.input = input; |
| 39 | this.pattern = pattern; |
| 40 | matcher = pattern.matcher(input); |
| 41 | prevEnd = 0; |
| 42 | } |
| 43 | |
| 44 | public StringValue next() { |
| 45 | if (prevEnd < 0) { |
| 46 | current = null; |
| 47 | return null; |
| 48 | } |
| 49 | |
| 50 | if (matcher.find()) { |
| 51 | current = input.subSequence(prevEnd, matcher.start()); |
| 52 | prevEnd = matcher.end(); |
| 53 | } else { |
| 54 | current = input.subSequence(prevEnd, input.length()); |
| 55 | prevEnd = -1; |
| 56 | } |
| 57 | return StringValue.makeStringValue(current); |
| 58 | } |
| 59 | |
| 60 | public void close() { |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get properties of this iterator, as a bit-significant integer. |
| 65 | * |
| 66 | * @return the properties of this iterator. This will be some combination of |
| 67 | * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER}, |
| 68 | * and {@link #LOOKAHEAD}. It is always |
| 69 | * acceptable to return the value zero, indicating that there are no known special properties. |
| 70 | * It is acceptable for the properties of the iterator to change depending on its state. |
| 71 | */ |
| 72 | |
| 73 | public int getProperties() { |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | } |
| 78 |