|
22 | 22 | package org.exist.xquery.functions.fn;
|
23 | 23 |
|
24 | 24 | import org.exist.dom.persistent.NodeProxy;
|
25 |
| -import org.exist.dom.QName; |
26 | 25 | import org.exist.dom.memtree.DocumentImpl;
|
27 | 26 | import org.exist.xmldb.XmldbURI;
|
28 |
| -import org.exist.xquery.Cardinality; |
29 |
| -import org.exist.xquery.Dependency; |
30 |
| -import org.exist.xquery.Function; |
31 |
| -import org.exist.xquery.FunctionSignature; |
32 |
| -import org.exist.xquery.Profiler; |
33 |
| -import org.exist.xquery.XPathException; |
34 |
| -import org.exist.xquery.XQueryContext; |
| 27 | +import org.exist.xquery.*; |
35 | 28 | import org.exist.xquery.value.AnyURIValue;
|
36 |
| -import org.exist.xquery.value.FunctionReturnSequenceType; |
37 | 29 | import org.exist.xquery.value.FunctionParameterSequenceType;
|
38 | 30 | import org.exist.xquery.value.Item;
|
39 | 31 | import org.exist.xquery.value.NodeValue;
|
40 | 32 | import org.exist.xquery.value.Sequence;
|
41 |
| -import org.exist.xquery.value.SequenceType; |
42 | 33 | import org.exist.xquery.value.Type;
|
43 | 34 |
|
| 35 | +import static org.exist.xquery.FunctionDSL.*; |
| 36 | +import static org.exist.xquery.functions.fn.FnModule.functionSignature; |
| 37 | + |
44 | 38 | /**
|
45 | 39 | * @author wolf
|
46 | 40 | */
|
47 | 41 | public class FunDocumentURI extends Function {
|
48 | 42 |
|
49 |
| - public final static FunctionSignature signature = |
50 |
| - new FunctionSignature( |
51 |
| - new QName("document-uri", Function.BUILTIN_FUNCTION_NS), |
52 |
| - "Returns the absolute URI of the resource from which the " + |
53 |
| - "document node $document-node was constructed. " + |
54 |
| - "If none such URI exists returns the empty sequence. " + |
55 |
| - "If $document-node is the empty sequence, returns the empty sequence.", |
56 |
| - new SequenceType[] { |
57 |
| - new FunctionParameterSequenceType("document-node", Type.NODE, |
58 |
| - Cardinality.ZERO_OR_ONE, "The document node") |
59 |
| - }, |
60 |
| - new FunctionReturnSequenceType(Type.ANY_URI, Cardinality.ZERO_OR_ONE, |
61 |
| - "the document URI of $document-node") |
62 |
| - ); |
63 |
| - |
64 |
| - public FunDocumentURI(XQueryContext context) { |
| 43 | + private static final FunctionParameterSequenceType FS_PARAM_NODE = optManyParam("value", Type.NODE, "The document node."); |
| 44 | + |
| 45 | + private static final String FS_DOCUMENT_URI = "document-uri"; |
| 46 | + private static final String FS_DESCRIPTION = "Returns the URI of a resource where a document can be found, if available."; |
| 47 | + |
| 48 | + private static final String FS_RETURN_DESCRIPTION = "The URI of a resource."; |
| 49 | + static final FunctionSignature FS_DOCUMENT_URI_0 = functionSignature( |
| 50 | + FS_DOCUMENT_URI, |
| 51 | + FS_DESCRIPTION, |
| 52 | + returnsOpt(Type.ANY_URI, FS_RETURN_DESCRIPTION) |
| 53 | + ); |
| 54 | + |
| 55 | + static final FunctionSignature FS_DOCUMENT_URI_1 = functionSignature( |
| 56 | + FS_DOCUMENT_URI, |
| 57 | + FS_DESCRIPTION, |
| 58 | + returnsOpt(Type.ANY_URI, FS_RETURN_DESCRIPTION), |
| 59 | + FS_PARAM_NODE |
| 60 | + ); |
| 61 | + |
| 62 | + public FunDocumentURI(final XQueryContext context, final FunctionSignature signature) { |
65 | 63 | super(context, signature);
|
66 | 64 | }
|
67 | 65 |
|
68 | 66 | /* (non-Javadoc)
|
69 | 67 | * @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)
|
70 | 68 | */
|
71 |
| - public Sequence eval(Sequence contextSequence, Item contextItem) |
72 |
| - throws XPathException { |
| 69 | + public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException { |
73 | 70 | if (context.getProfiler().isEnabled()) {
|
74 | 71 | context.getProfiler().start(this);
|
75 | 72 | context.getProfiler().message(this, Profiler.DEPENDENCIES,
|
76 |
| - "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies())); |
77 |
| - if (contextSequence != null) |
78 |
| - {context.getProfiler().message(this, Profiler.START_SEQUENCES, |
79 |
| - "CONTEXT SEQUENCE", contextSequence);} |
80 |
| - if (contextItem != null) |
81 |
| - {context.getProfiler().message(this, Profiler.START_SEQUENCES, |
82 |
| - "CONTEXT ITEM", contextItem.toSequence());} |
| 73 | + "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies())); |
| 74 | + if (contextSequence != null) { |
| 75 | + context.getProfiler().message(this, Profiler.START_SEQUENCES,"CONTEXT SEQUENCE", contextSequence); |
| 76 | + } |
| 77 | + if (contextItem != null) { |
| 78 | + context.getProfiler().message(this, Profiler.START_SEQUENCES,"CONTEXT ITEM", contextItem.toSequence()); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + final boolean isContextItem = (contextItem != null); |
| 83 | + |
| 84 | + if(!isContextItem && getArgumentCount()==0){ |
| 85 | + // Bug in eXist-db? the 0-parameter function is invoked |
| 86 | + // which effectively means that the zero-argument function is active. |
| 87 | + throw new XPathException(this, ErrorCodes.XPDY0002, "Context item is absent "); |
83 | 88 | }
|
84 |
| - final Sequence seq = getArgument(0).eval(contextSequence, contextItem); |
| 89 | + |
| 90 | + // Get sequence from contextItem or from parameter |
| 91 | + final Sequence seq = (isContextItem) |
| 92 | + ? contextItem.toSequence() |
| 93 | + : getArgument(0).eval(contextSequence, contextItem); |
| 94 | + |
| 95 | + if (isContextItem && seq.isEmpty()) { |
| 96 | + // This is the actual empty context item check |
| 97 | + throw new XPathException(this, ErrorCodes.XPDY0002, "Context item is absent."); |
| 98 | + } |
| 99 | + |
| 100 | + if (isContextItem && !Type.subTypeOf(seq.getItemType(), Type.NODE) ) { |
| 101 | + // If context item is provided, it must be a node |
| 102 | + throw new XPathException(this, ErrorCodes.XPTY0004, "Context item is not a node."); |
| 103 | + } |
| 104 | + |
85 | 105 | Sequence result = Sequence.EMPTY_SEQUENCE;
|
86 | 106 | if (!seq.isEmpty()) {
|
87 | 107 | final NodeValue value = (NodeValue) seq.itemAt(0);
|
88 |
| - if (value.getImplementationType() == NodeValue.PERSISTENT_NODE) { |
| 108 | + if (value.getImplementationType() == NodeValue.PERSISTENT_NODE) { |
89 | 109 | final NodeProxy node = (NodeProxy) value;
|
90 |
| - //Returns the empty sequence if the node is not a document node. |
| 110 | + //Returns the empty sequence if the node is not a document node. |
91 | 111 | if (node.isDocument()) {
|
92 |
| - final XmldbURI path = node.getOwnerDocument().getURI(); |
| 112 | + final XmldbURI path = node.getOwnerDocument().getURI(); |
93 | 113 | result = new AnyURIValue(path);
|
94 | 114 | }
|
| 115 | + |
95 | 116 | } else {
|
96 |
| - if (value instanceof DocumentImpl && |
97 |
| - ((DocumentImpl)value).getDocumentURI() != null) { |
| 117 | + if (value instanceof DocumentImpl && ((DocumentImpl)value).getDocumentURI() != null) { |
98 | 118 | result = new AnyURIValue(((DocumentImpl)value).getDocumentURI());
|
99 | 119 | }
|
100 | 120 | }
|
101 | 121 | }
|
102 |
| - if (context.getProfiler().isEnabled()) |
103 |
| - {context.getProfiler().end(this, "", result);} |
| 122 | + |
| 123 | + if (context.getProfiler().isEnabled()) { |
| 124 | + context.getProfiler().end(this, "", result); |
| 125 | + } |
104 | 126 | return result;
|
105 | 127 | }
|
106 | 128 | }
|
0 commit comments