| 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.expr; |
| 9 | |
| 10 | import net.sf.saxon.om.Item; |
| 11 | import net.sf.saxon.om.SequenceIterator; |
| 12 | import net.sf.saxon.regex.RegexIterator; |
| 13 | import net.sf.saxon.trans.XPathException; |
| 14 | import net.sf.saxon.tree.iter.EmptyIterator; |
| 15 | |
| 16 | /** |
| 17 | * Mapping function that maps the sequence of matching/non-matching strings to the |
| 18 | * sequence delivered by applying the matching-substring and non-matching-substring |
| 19 | * expressions respectively to each such string |
| 20 | */ |
| 21 | |
| 22 | public class AnalyzeMappingFunction implements ContextMappingFunction<Item> { |
| 23 | |
| 24 | private RegexIterator base; |
| 25 | private XPathContext c2; |
| 26 | private Expression nonMatchExpr; |
| 27 | private Expression matchingExpr; |
| 28 | |
| 29 | public AnalyzeMappingFunction(RegexIterator base, XPathContext c2, Expression nonMatchExpr, Expression matchingExpr) { |
| 30 | this.base = base; |
| 31 | this.c2 = c2; |
| 32 | this.nonMatchExpr = nonMatchExpr; |
| 33 | this.matchingExpr = matchingExpr; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Map one item to a sequence. |
| 38 | * |
| 39 | * @param context The processing context. Some mapping functions use this because they require |
| 40 | * context information. Some mapping functions modify the context by maintaining the context item |
| 41 | * and position. In other cases, the context may be null. |
| 42 | * @return either (a) a SequenceIterator over the sequence of items that the supplied input |
| 43 | * item maps to, or (b) an Item if it maps to a single item, or (c) null if it maps to an empty |
| 44 | * sequence. |
| 45 | */ |
| 46 | |
| 47 | public SequenceIterator<?> map(XPathContext context) throws XPathException { |
| 48 | if (base.isMatching()) { |
| 49 | if (matchingExpr != null) { |
| 50 | return matchingExpr.iterate(c2); |
| 51 | } |
| 52 | } else { |
| 53 | if (nonMatchExpr != null) { |
| 54 | return nonMatchExpr.iterate(c2); |
| 55 | } |
| 56 | } |
| 57 | return EmptyIterator.getInstance(); |
| 58 | } |
| 59 | } |
| 60 |