Skip to content

Commit 0f06aa0

Browse files
committed
revise code
1 parent 082ba9e commit 0f06aa0

File tree

1 file changed

+34
-38
lines changed

1 file changed

+34
-38
lines changed

exist-core/src/main/java/org/exist/xquery/functions/fn/FunDocumentURI.java

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,14 @@
2121
*/
2222
package org.exist.xquery.functions.fn;
2323

24-
import org.exist.dom.persistent.NodeProxy;
2524
import org.exist.dom.memtree.DocumentImpl;
25+
import org.exist.dom.persistent.NodeProxy;
2626
import org.exist.xmldb.XmldbURI;
2727
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;
3632
import static org.exist.xquery.functions.fn.FnModule.functionSignature;
3733

3834
/**
@@ -67,57 +63,57 @@ public FunDocumentURI(final XQueryContext context, final FunctionSignature signa
6763
/* (non-Javadoc)
6864
* @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)
6965
*/
70-
public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException {
66+
public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException {
7167
if (context.getProfiler().isEnabled()) {
7268
context.getProfiler().start(this);
7369
context.getProfiler().message(this, Profiler.DEPENDENCIES,
7470
"DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
7571
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);
7773
}
7874
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());
8076
}
8177
}
8278

83-
final boolean isContextItem = (contextItem != null);
79+
final boolean contextItemIsAbsent = (contextItem == null);
80+
final boolean argumentIsOmitted = (getArgumentCount() == 0);
8481

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.");
8985
}
9086

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)
9389
? contextItem.toSequence()
9490
: getArgument(0).eval(contextSequence, contextItem);
9591

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;
9995
}
10096

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)) {
10399
throw new XPathException(this, ErrorCodes.XPTY0004, "Context item is not a node.");
104100
}
105101

102+
103+
// Error condition: Returns the empty sequence if the node is not a document
106104
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());
121117
}
122118
}
123119

0 commit comments

Comments
 (0)