| 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.trans.XPathException; |
| 12 | import net.sf.saxon.value.StringValue; |
| 13 | |
| 14 | |
| 15 | /** |
| 16 | * This interface defines an iterator that supports the evaluation of xsl:analyze-string. |
| 17 | * It returns all the matching and non-matching substrings in an input string, and |
| 18 | * provides access to their captured groups |
| 19 | */ |
| 20 | public interface RegexIterator extends SequenceIterator<StringValue> { |
| 21 | |
| 22 | /** |
| 23 | * Determine whether the current item in the sequence is a matching item or a non-matching item |
| 24 | * |
| 25 | * @return true if the current item is a matching item |
| 26 | */ |
| 27 | |
| 28 | boolean isMatching(); |
| 29 | |
| 30 | /** |
| 31 | * Get the number of captured groups in the current matching item |
| 32 | */ |
| 33 | |
| 34 | int getNumberOfGroups(); |
| 35 | |
| 36 | /** |
| 37 | * Get a substring that matches a parenthesised group within the regular expression |
| 38 | * |
| 39 | * @param number the number of the group to be obtained |
| 40 | * @return the substring of the current item that matches the n'th parenthesized group |
| 41 | * within the regular expression |
| 42 | */ |
| 43 | |
| 44 | /*@Nullable*/ |
| 45 | String getRegexGroup(int number); |
| 46 | |
| 47 | /** |
| 48 | * Process a matching substring, performing specified actions at the start and end of each matching |
| 49 | * group |
| 50 | */ |
| 51 | |
| 52 | void processMatchingSubstring(MatchHandler action) throws XPathException; |
| 53 | |
| 54 | |
| 55 | /** |
| 56 | * Interface defining a call-back action for processing captured groups |
| 57 | */ |
| 58 | |
| 59 | interface MatchHandler { |
| 60 | |
| 61 | /** |
| 62 | * Method to be called with each fragment of text in a matching substring |
| 63 | * @param s a matching substring, or part thereof that falls within a specific group |
| 64 | */ |
| 65 | |
| 66 | void characters(CharSequence s) throws XPathException; |
| 67 | |
| 68 | /** |
| 69 | * Method to be called when the start of a captured group is encountered |
| 70 | * |
| 71 | * @param groupNumber the group number of the captured group |
| 72 | */ |
| 73 | |
| 74 | void onGroupStart(int groupNumber) throws XPathException; |
| 75 | |
| 76 | /** |
| 77 | * Method to be called when the end of a captured group is encountered |
| 78 | * |
| 79 | * @param groupNumber the group number of the captured group |
| 80 | */ |
| 81 | |
| 82 | void onGroupEnd(int groupNumber) throws XPathException; |
| 83 | } |
| 84 | |
| 85 | } |
| 86 |