| 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.StaticProperty; |
| 12 | import net.sf.saxon.expr.XPathContext; |
| 13 | import net.sf.saxon.om.NodeInfo; |
| 14 | import net.sf.saxon.om.Sequence; |
| 15 | import net.sf.saxon.om.ZeroOrOne; |
| 16 | import net.sf.saxon.trans.XPathException; |
| 17 | |
| 18 | /** |
| 19 | * Implement the XPath 2.0 root() function with one argument |
| 20 | */ |
| 21 | |
| 22 | |
| 23 | public class Root_1 extends SystemFunction { |
| 24 | |
| 25 | /** |
| 26 | * Get the static properties of this expression (other than its type). The result is |
| 27 | * bit-significant. These properties are used for optimizations. In general, if |
| 28 | * property bit is set, it is true, but if it is unset, the value is unknown. |
| 29 | * @param arguments the actual arguments to the function call |
| 30 | */ |
| 31 | |
| 32 | public int getSpecialProperties(Expression[] arguments) { |
| 33 | int prop = StaticProperty.ORDERED_NODESET | |
| 34 | StaticProperty.SINGLE_DOCUMENT_NODESET | |
| 35 | StaticProperty.NO_NODES_NEWLY_CREATED; |
| 36 | if ((getArity() == 0) || |
| 37 | (arguments[0].getSpecialProperties() & StaticProperty.CONTEXT_DOCUMENT_NODESET) != 0) { |
| 38 | prop |= StaticProperty.CONTEXT_DOCUMENT_NODESET; |
| 39 | } |
| 40 | return prop; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Evaluate the expression |
| 45 | * |
| 46 | * @param context the dynamic evaluation context |
| 47 | * @param arguments the values of the arguments, supplied as Sequences |
| 48 | * @return the result of the evaluation, in the form of a Sequence |
| 49 | * @throws net.sf.saxon.trans.XPathException |
| 50 | * if a dynamic error occurs during the evaluation of the expression |
| 51 | */ |
| 52 | public ZeroOrOne<NodeInfo> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 53 | NodeInfo node = (NodeInfo) arguments[0].head(); |
| 54 | if (node == null) { |
| 55 | return ZeroOrOne.empty(); |
| 56 | } else { |
| 57 | return new ZeroOrOne<>(node.getRoot()); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | public String getStreamerName() { |
| 62 | return "Root"; |
| 63 | } |
| 64 | |
| 65 | public String getCompilerName() { |
| 66 | return "RootFunctionCompiler"; |
| 67 | } |
| 68 | |
| 69 | } |
| 70 |