| 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.NamespaceBinding; |
| 12 | import net.sf.saxon.om.NodeInfo; |
| 13 | import net.sf.saxon.om.Sequence; |
| 14 | import net.sf.saxon.om.SequenceTool; |
| 15 | import net.sf.saxon.trans.XPathException; |
| 16 | import net.sf.saxon.tree.iter.UnfailingIterator; |
| 17 | import net.sf.saxon.tree.util.NamespaceIterator; |
| 18 | import net.sf.saxon.type.BuiltInAtomicType; |
| 19 | import net.sf.saxon.value.StringValue; |
| 20 | |
| 21 | import java.util.Iterator; |
| 22 | |
| 23 | /** |
| 24 | * This class implements the XPath 2.0 function fn:in-scope-prefixes() |
| 25 | */ |
| 26 | |
| 27 | public class InScopePrefixes extends SystemFunction { |
| 28 | |
| 29 | /** |
| 30 | * Given an element node, return an iterator over the namespace |
| 31 | * prefixes, taking into account special rules for the XML namespace and the default namespace |
| 32 | * |
| 33 | * @param element an element |
| 34 | * @return an iterator over the prefixes. |
| 35 | */ |
| 36 | |
| 37 | |
| 38 | private UnfailingIterator<StringValue> iteratePrefixes(final NodeInfo element) { |
| 39 | final Iterator<NamespaceBinding> iter = NamespaceIterator.iterateNamespaces(element); |
| 40 | return new UnfailingIterator<StringValue>() { |
| 41 | private int position = 0; |
| 42 | |
| 43 | public StringValue next() { |
| 44 | if (position == 0) { |
| 45 | position++; |
| 46 | return new StringValue("xml"); |
| 47 | } else if (iter.hasNext()) { |
| 48 | String prefix = iter.next().getPrefix(); |
| 49 | StringValue current; |
| 50 | if (prefix.isEmpty()) { |
| 51 | current = StringValue.EMPTY_STRING; |
| 52 | } else { |
| 53 | current = new StringValue(prefix, BuiltInAtomicType.NCNAME); |
| 54 | } |
| 55 | position++; |
| 56 | return current; |
| 57 | } else { |
| 58 | position = -1; |
| 59 | return null; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | }; |
| 64 | |
| 65 | } |
| 66 | |
| 67 | |
| 68 | /** |
| 69 | * Dynamic function call of in-scope-prefixes |
| 70 | * @param context the dynamic evaluation context |
| 71 | * @param arguments the values of the arguments, supplied as Sequences. |
| 72 | * <p>Generally it is advisable, if calling iterate() to process a supplied sequence, to |
| 73 | * call it only once; if the value is required more than once, it should first be converted |
| 74 | * to a {@link net.sf.saxon.om.GroundedValue} by calling the utility methd |
| 75 | * SequenceTool.toGroundedValue().</p> |
| 76 | * <p>If the expected value is a single item, the item should be obtained by calling |
| 77 | * Sequence.head(): it cannot be assumed that the item will be passed as an instance of |
| 78 | * {@link net.sf.saxon.om.Item} or {@link net.sf.saxon.value.AtomicValue}.</p> |
| 79 | * <p>It is the caller's responsibility to perform any type conversions required |
| 80 | * to convert arguments to the type expected by the callee. An exception is where |
| 81 | * this Callable is explicitly an argument-converting wrapper around the original |
| 82 | * Callable.</p> |
| 83 | * @return the sequence of prefixes |
| 84 | * @throws XPathException if a dynamic error occurs |
| 85 | */ |
| 86 | |
| 87 | public Sequence<? extends StringValue> call(final XPathContext context, Sequence[] arguments) throws XPathException { |
| 88 | final NodeInfo element = (NodeInfo) arguments[0].head(); |
| 89 | return SequenceTool.toLazySequence(iteratePrefixes(element)); |
| 90 | } |
| 91 | |
| 92 | } |
| 93 |