|
21 | 21 | */
|
22 | 22 | package org.exist.xquery.functions.fn;
|
23 | 23 |
|
24 |
| -import org.exist.dom.persistent.NodeProxy; |
25 | 24 | import org.exist.dom.memtree.DocumentImpl;
|
| 25 | +import org.exist.dom.persistent.NodeProxy; |
26 | 26 | import org.exist.xmldb.XmldbURI;
|
27 | 27 | import org.exist.xquery.*;
|
28 |
| -import org.exist.xquery.value.AnyURIValue; |
29 |
| -import org.exist.xquery.value.FunctionParameterSequenceType; |
30 |
| -import org.exist.xquery.value.Item; |
31 |
| -import org.exist.xquery.value.NodeValue; |
32 |
| -import org.exist.xquery.value.Sequence; |
33 |
| -import org.exist.xquery.value.Type; |
34 |
| - |
35 |
| -import static org.exist.xquery.FunctionDSL.*; |
| 28 | +import org.exist.xquery.value.*; |
| 29 | + |
| 30 | +import static org.exist.xquery.FunctionDSL.optManyParam; |
| 31 | +import static org.exist.xquery.FunctionDSL.returnsOpt; |
36 | 32 | import static org.exist.xquery.functions.fn.FnModule.functionSignature;
|
37 | 33 |
|
38 | 34 | /**
|
@@ -67,57 +63,57 @@ public FunDocumentURI(final XQueryContext context, final FunctionSignature signa
|
67 | 63 | /* (non-Javadoc)
|
68 | 64 | * @see org.exist.xquery.Expression#eval(org.exist.xquery.StaticContext, org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
|
69 | 65 | */
|
70 |
| - public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException { |
| 66 | + public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException { |
71 | 67 | if (context.getProfiler().isEnabled()) {
|
72 | 68 | context.getProfiler().start(this);
|
73 | 69 | context.getProfiler().message(this, Profiler.DEPENDENCIES,
|
74 | 70 | "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
|
75 | 71 | if (contextSequence != null) {
|
76 |
| - context.getProfiler().message(this, Profiler.START_SEQUENCES,"CONTEXT SEQUENCE", contextSequence); |
| 72 | + context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence); |
77 | 73 | }
|
78 | 74 | if (contextItem != null) {
|
79 |
| - context.getProfiler().message(this, Profiler.START_SEQUENCES,"CONTEXT ITEM", contextItem.toSequence()); |
| 75 | + context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence()); |
80 | 76 | }
|
81 | 77 | }
|
82 | 78 |
|
83 |
| - final boolean isContextItem = (contextItem != null); |
| 79 | + final boolean contextItemIsAbsent = (contextItem == null); |
| 80 | + final boolean argumentIsOmitted = (getArgumentCount() == 0); |
84 | 81 |
|
85 |
| - if(!isContextItem && getArgumentCount()==0){ |
86 |
| - // Bug in eXist-db? the 0-parameter function is invoked |
87 |
| - // which effectively means that the zero-argument function is active. |
88 |
| - throw new XPathException(this, ErrorCodes.XPDY0002, "Context item is absent "); |
| 82 | + // Error condition |
| 83 | + if(argumentIsOmitted && contextItemIsAbsent){ |
| 84 | + throw new XPathException(this, ErrorCodes.XPDY0002, "Context item is absent."); |
89 | 85 | }
|
90 | 86 |
|
91 |
| - // Get sequence from contextItem or from parameter |
92 |
| - final Sequence seq = (isContextItem) |
| 87 | + // Get sequence from contextItem or from context Sequence |
| 88 | + final Sequence seq = (argumentIsOmitted) |
93 | 89 | ? contextItem.toSequence()
|
94 | 90 | : getArgument(0).eval(contextSequence, contextItem);
|
95 | 91 |
|
96 |
| - if (isContextItem && seq.isEmpty()) { |
97 |
| - // This is the actual empty context item check |
98 |
| - throw new XPathException(this, ErrorCodes.XPDY0002, "Context item is absent."); |
| 92 | + // Rule 1: If $arg is the empty sequence, the function returns the empty sequence. |
| 93 | + if(seq.isEmpty()){ |
| 94 | + return Sequence.EMPTY_SEQUENCE; |
99 | 95 | }
|
100 | 96 |
|
101 |
| - if (isContextItem && !Type.subTypeOf(seq.getItemType(), Type.NODE) ) { |
102 |
| - // If context item is provided, it must be a node |
| 97 | + // Error condition |
| 98 | + if (argumentIsOmitted && !Type.subTypeOf(seq.getItemType(), Type.NODE)) { |
103 | 99 | throw new XPathException(this, ErrorCodes.XPTY0004, "Context item is not a node.");
|
104 | 100 | }
|
105 | 101 |
|
| 102 | + |
| 103 | + // Error condition: Returns the empty sequence if the node is not a document |
106 | 104 | Sequence result = Sequence.EMPTY_SEQUENCE;
|
107 |
| - if (!seq.isEmpty()) { |
108 |
| - final NodeValue value = (NodeValue) seq.itemAt(0); |
109 |
| - if (value.getImplementationType() == NodeValue.PERSISTENT_NODE) { |
110 |
| - final NodeProxy node = (NodeProxy) value; |
111 |
| - //Returns the empty sequence if the node is not a document node. |
112 |
| - if (node.isDocument()) { |
113 |
| - final XmldbURI path = node.getOwnerDocument().getURI(); |
114 |
| - result = new AnyURIValue(path); |
115 |
| - } |
116 |
| - |
117 |
| - } else { |
118 |
| - if (value instanceof DocumentImpl && ((DocumentImpl)value).getDocumentURI() != null) { |
119 |
| - result = new AnyURIValue(((DocumentImpl)value).getDocumentURI()); |
120 |
| - } |
| 105 | + |
| 106 | + final NodeValue value = (NodeValue) seq.itemAt(0); |
| 107 | + if (value.getImplementationType() == NodeValue.PERSISTENT_NODE) { |
| 108 | + final NodeProxy node = (NodeProxy) value; |
| 109 | + if (node.isDocument()) { |
| 110 | + final XmldbURI path = node.getOwnerDocument().getURI(); |
| 111 | + result = new AnyURIValue(path); |
| 112 | + } |
| 113 | + |
| 114 | + } else { |
| 115 | + if (value instanceof DocumentImpl && ((DocumentImpl) value).getDocumentURI() != null) { |
| 116 | + result = new AnyURIValue(((DocumentImpl) value).getDocumentURI()); |
121 | 117 | }
|
122 | 118 | }
|
123 | 119 |
|
|
0 commit comments