|
21 | 21 | */
|
22 | 22 | package org.exist.xquery.functions.fn;
|
23 | 23 |
|
24 |
| -import org.exist.dom.memtree.DocumentImpl; |
25 | 24 | import org.exist.dom.persistent.ExtArrayNodeSet;
|
26 | 25 | import org.exist.dom.persistent.NodeProxy;
|
27 | 26 | import org.exist.dom.QName;
|
|
47 | 46 | * @author <a href="mailto:[email protected]">Wolfgang Meier</a>
|
48 | 47 | */
|
49 | 48 | public class FunRoot extends Function {
|
50 |
| - |
51 |
| - protected static final String FUNCTION_DESCRIPTION_0_PARAM = |
52 |
| - "Returns the root of the tree to which the context item belongs. "; |
53 |
| - protected static final String FUNCTION_DESCRIPTION_1_PARAM = |
54 |
| - "Returns the root of the tree to which $arg belongs. " + |
55 |
| - "This will usually, but not necessarily, be a document node.\n\n" + |
56 |
| - "If $arg is the empty sequence, the empty sequence is returned.\n\n" + |
57 |
| - "If $arg is a document node, $arg is returned.\n\n" + |
58 |
| - " The behavior of the zero argument version of the function is " + |
59 |
| - "exactly the same as if the context item had been passed in $arg."; |
60 |
| - |
| 49 | + |
| 50 | + protected static final String FUNCTION_DESCRIPTION_0_PARAM = |
| 51 | + "Returns the root of the tree to which the context item belongs. "; |
| 52 | + protected static final String FUNCTION_DESCRIPTION_1_PARAM = |
| 53 | + "Returns the root of the tree to which $arg belongs. " + |
| 54 | + "This will usually, but not necessarily, be a document node.\n\n" + |
| 55 | + "If $arg is the empty sequence, the empty sequence is returned.\n\n" + |
| 56 | + "If $arg is a document node, $arg is returned.\n\n" + |
| 57 | + " The behavior of the zero argument version of the function is " + |
| 58 | + "exactly the same as if the context item had been passed in $arg."; |
| 59 | + |
61 | 60 | public final static FunctionSignature[] signatures = {
|
62 |
| - new FunctionSignature( |
63 |
| - new QName("root", Function.BUILTIN_FUNCTION_NS), |
64 |
| - FUNCTION_DESCRIPTION_0_PARAM, |
65 |
| - new SequenceType[0], |
66 |
| - new FunctionReturnSequenceType(Type.NODE, Cardinality.EXACTLY_ONE, "the root node of the tree to which the context node belongs") |
67 |
| - ), |
68 |
| - new FunctionSignature( |
69 |
| - new QName("root", Function.BUILTIN_FUNCTION_NS), |
70 |
| - FUNCTION_DESCRIPTION_1_PARAM, |
71 |
| - new SequenceType[] { new FunctionParameterSequenceType("arg", Type.NODE, Cardinality.ZERO_OR_ONE, "The input node") }, |
72 |
| - new FunctionReturnSequenceType(Type.NODE, Cardinality.ZERO_OR_ONE, "the root node of the tree to which $arg belongs") |
73 |
| - ) |
| 61 | + new FunctionSignature( |
| 62 | + new QName("root", Function.BUILTIN_FUNCTION_NS), |
| 63 | + FUNCTION_DESCRIPTION_0_PARAM, |
| 64 | + new SequenceType[0], |
| 65 | + new FunctionReturnSequenceType(Type.NODE, Cardinality.EXACTLY_ONE, "the root node of the tree to which the context node belongs") |
| 66 | + ), |
| 67 | + new FunctionSignature( |
| 68 | + new QName("root", Function.BUILTIN_FUNCTION_NS), |
| 69 | + FUNCTION_DESCRIPTION_1_PARAM, |
| 70 | + new SequenceType[]{new FunctionParameterSequenceType("arg", Type.NODE, Cardinality.ZERO_OR_ONE, "The input node")}, |
| 71 | + new FunctionReturnSequenceType(Type.NODE, Cardinality.ZERO_OR_ONE, "the root node of the tree to which $arg belongs") |
| 72 | + ) |
74 | 73 | };
|
75 | 74 |
|
76 |
| - public FunRoot(XQueryContext context, FunctionSignature signature) { |
| 75 | + public FunRoot(final XQueryContext context, final FunctionSignature signature) { |
77 | 76 | super(context, signature);
|
78 | 77 | }
|
79 |
| - |
80 |
| - /* (non-Javadoc) |
81 |
| - * @see org.exist.xquery.Expression#eval(org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item) |
82 |
| - */ |
83 |
| - public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException { |
| 78 | + |
| 79 | + @Override |
| 80 | + public Sequence eval(Sequence contextSequence, final Item contextItem) throws XPathException { |
84 | 81 | if (context.getProfiler().isEnabled()) {
|
85 | 82 | context.getProfiler().start(this);
|
86 | 83 | context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
|
87 |
| - if (contextSequence != null) |
88 |
| - {context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);} |
89 |
| - if (contextItem != null) |
90 |
| - {context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());} |
| 84 | + if (contextSequence != null) { |
| 85 | + context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence); |
| 86 | + } |
| 87 | + if (contextItem != null) { |
| 88 | + context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence()); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if (contextItem != null) { |
| 93 | + contextSequence = contextItem.toSequence(); |
| 94 | + } |
| 95 | + |
| 96 | + //If we have one argument, we take it into account |
| 97 | + final Sequence seq; |
| 98 | + if (getSignature().getArgumentCount() > 0) { |
| 99 | + seq = getArgument(0).eval(contextSequence, contextItem); |
| 100 | + } else { |
| 101 | + //Otherwise, we take the context sequence and we iterate over it |
| 102 | + seq = contextSequence; |
| 103 | + } |
| 104 | + |
| 105 | + if (seq == null) { |
| 106 | + throw new XPathException(this, ErrorCodes.XPDY0002, "Undefined context item"); |
91 | 107 | }
|
92 |
| - |
93 |
| - |
94 |
| - Sequence seq; |
95 |
| - Sequence result; |
96 |
| - Item item; |
97 |
| - |
98 |
| - if (contextItem != null) |
99 |
| - {contextSequence = contextItem.toSequence();} |
100 |
| - if (contextSequence == null || contextSequence.isEmpty()) |
101 |
| - {result = Sequence.EMPTY_SEQUENCE;} |
102 |
| - |
103 |
| - //If we have one argumment, we take it into account |
104 |
| - if (getSignature().getArgumentCount() > 0) |
105 |
| - {seq = getArgument(0).eval(contextSequence, contextItem);} |
106 |
| - //Otherwise, we take the context sequence and we iterate over it |
107 |
| - else |
108 |
| - {seq = contextSequence;} |
109 |
| - |
110 |
| - if (seq == null) |
111 |
| - {throw new XPathException(this, ErrorCodes.XPDY0002, "Undefined context item");} |
112 | 108 |
|
113 |
| - if (seq.isPersistentSet()) |
114 |
| - {result = new ExtArrayNodeSet(seq.getItemCount());} |
115 |
| - else |
116 |
| - {result = new ValueSequence(seq.getItemCount());} |
| 109 | + final Sequence result; |
| 110 | + if (seq.isPersistentSet()) { |
| 111 | + result = new ExtArrayNodeSet(seq.getItemCount()); |
| 112 | + } else { |
| 113 | + result = new ValueSequence(seq.getItemCount()); |
| 114 | + } |
| 115 | + |
| 116 | + for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) { |
| 117 | + final Item item = i.nextItem(); |
| 118 | + if (!Type.subTypeOf(item.getType(), Type.NODE)) { |
| 119 | + throw new XPathException(this, ErrorCodes.XPTY0004, "Item is not a node; got '" + item + "'", seq); |
| 120 | + } |
117 | 121 |
|
118 |
| - for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) { |
119 |
| - item = i.nextItem(); |
120 |
| - if (!Type.subTypeOf(item.getType(), Type.NODE)) |
121 |
| - {throw new XPathException(this, ErrorCodes.XPTY0004, "Item is not a node; got '" + item + "'", seq);} |
122 | 122 | final Sequence s = item.toSequence();
|
123 | 123 |
|
124 | 124 | if (s.isPersistentSet()) {
|
125 | 125 | final NodeProxy p = s.toNodeSet().get(0);
|
126 | 126 | result.add(new NodeProxy(p.getOwnerDocument()));
|
127 | 127 | } else {
|
128 | 128 | if (seq.hasOne() && item.getType() == Type.ATTRIBUTE) {
|
129 |
| - result.add(item); |
130 |
| - } else if(item.getType() == Type.DOCUMENT) { |
131 |
| - result.add((DocumentImpl)item); |
| 129 | + result.add(item); |
| 130 | + } else if (item.getType() == Type.DOCUMENT) { |
| 131 | + result.add(item); |
132 | 132 | } else {
|
133 |
| - result.add(((NodeImpl)item).getOwnerDocument()); |
| 133 | + result.add(((NodeImpl) item).getOwnerDocument()); |
134 | 134 | }
|
135 | 135 | }
|
136 |
| - } |
137 |
| - |
138 |
| - if (context.getProfiler().isEnabled()) |
139 |
| - {context.getProfiler().end(this, "", result);} |
140 |
| - |
| 136 | + } |
| 137 | + |
| 138 | + if (context.getProfiler().isEnabled()) { |
| 139 | + context.getProfiler().end(this, "", result); |
| 140 | + } |
| 141 | + |
141 | 142 | return result;
|
142 |
| - |
143 |
| - } |
144 |
| - |
| 143 | + } |
145 | 144 | }
|
0 commit comments