| 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.regex.RegexIterator; |
| 13 | import net.sf.saxon.trans.XPathException; |
| 14 | import net.sf.saxon.value.NumericValue; |
| 15 | import net.sf.saxon.value.StringValue; |
| 16 | |
| 17 | |
| 18 | public class RegexGroup extends SystemFunction { |
| 19 | |
| 20 | /** |
| 21 | * Evaluate the expression |
| 22 | * |
| 23 | * @param context the dynamic evaluation context |
| 24 | * @param arguments the values of the arguments, supplied as Sequences |
| 25 | * @return the result of the evaluation, in the form of a Sequence |
| 26 | * @throws net.sf.saxon.trans.XPathException |
| 27 | * if a dynamic error occurs during the evaluation of the expression |
| 28 | */ |
| 29 | public StringValue call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 30 | RegexIterator iter = context.getCurrentRegexIterator(); |
| 31 | if (iter == null) { |
| 32 | return StringValue.EMPTY_STRING; |
| 33 | } |
| 34 | NumericValue gp0 = (NumericValue) arguments[0].head(); |
| 35 | String s = iter.getRegexGroup((int) gp0.longValue()); |
| 36 | return StringValue.makeStringValue(s); |
| 37 | } |
| 38 | } |
| 39 |