| 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.Expression; |
| 11 | import net.sf.saxon.expr.FirstItemExpression; |
| 12 | import net.sf.saxon.expr.XPathContext; |
| 13 | import net.sf.saxon.om.Item; |
| 14 | import net.sf.saxon.om.Sequence; |
| 15 | import net.sf.saxon.om.ZeroOrOne; |
| 16 | import net.sf.saxon.trans.XPathException; |
| 17 | |
| 18 | /** |
| 19 | * This class implements the function fn:head(), which is a standard function in XPath 3.0 |
| 20 | */ |
| 21 | |
| 22 | public class HeadFn extends SystemFunction { |
| 23 | |
| 24 | @Override |
| 25 | public Expression makeFunctionCall(Expression[] arguments) { |
| 26 | return FirstItemExpression.makeFirstItemExpression(arguments[0]); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Evaluate the expression |
| 31 | * |
| 32 | * @param context the dynamic evaluation context |
| 33 | * @param arguments the values of the arguments, supplied as Sequences |
| 34 | * @return the result of the evaluation, in the form of a Sequence |
| 35 | * @throws net.sf.saxon.trans.XPathException |
| 36 | * if a dynamic error occurs during the evaluation of the expression |
| 37 | */ |
| 38 | public ZeroOrOne<Item<?>> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 39 | Item<?> head = arguments[0].head(); |
| 40 | return new ZeroOrOne<>(head); |
| 41 | } |
| 42 | } |