| 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.functions; |
| 9 | |
| 10 | import net.sf.saxon.expr.XPathContext; |
| 11 | import net.sf.saxon.om.Sequence; |
| 12 | import net.sf.saxon.om.SequenceTool; |
| 13 | import net.sf.saxon.regex.RegularExpression; |
| 14 | import net.sf.saxon.trans.XPathException; |
| 15 | import net.sf.saxon.value.AtomicValue; |
| 16 | import net.sf.saxon.value.EmptySequence; |
| 17 | |
| 18 | |
| 19 | /** |
| 20 | * This class implements the 3-argument tokenize() function for regular expression matching. This returns a |
| 21 | * sequence of strings representing the unmatched substrings: the separators which match the |
| 22 | * regular expression are not returned. |
| 23 | */ |
| 24 | |
| 25 | public class Tokenize_3 extends RegexFunction { |
| 26 | |
| 27 | protected boolean allowRegexMatchingEmptyString() { |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Evaluate the expression dynamically |
| 33 | * |
| 34 | * @param context the dynamic evaluation context |
| 35 | * @param arguments the values of the arguments, supplied as SequenceIterators |
| 36 | * @return the result of the evaluation, in the form of a SequenceIterator |
| 37 | * @throws net.sf.saxon.trans.XPathException |
| 38 | * if a dynamic error occurs during the evaluation of the expression |
| 39 | */ |
| 40 | public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 41 | AtomicValue sv = (AtomicValue) arguments[0].head(); |
| 42 | if (sv == null) { |
| 43 | return EmptySequence.getInstance(); |
| 44 | } |
| 45 | CharSequence input = sv.getStringValueCS(); |
| 46 | if (input.length() == 0) { |
| 47 | return EmptySequence.getInstance(); |
| 48 | } |
| 49 | RegularExpression re = getRegularExpression(arguments); |
| 50 | return SequenceTool.toLazySequence(re.tokenize(input)); |
| 51 | } |
| 52 | } |
| 53 |