|
1 | 1 | /*
|
2 |
| - * eXist-db Open Source Native XML Database |
3 |
| - * Copyright (C) 2001 The eXist-db Authors |
| 2 | + * Elemental |
| 3 | + * Copyright (C) 2024, Evolved Binary Ltd |
4 | 4 | *
|
5 |
| - |
6 |
| - * http://www.exist-db.org |
| 5 | + |
| 6 | + * https://www.evolvedbinary.com | https://www.elemental.xyz |
7 | 7 | *
|
8 | 8 | * This library is free software; you can redistribute it and/or
|
9 | 9 | * modify it under the terms of the GNU Lesser General Public
|
10 |
| - * License as published by the Free Software Foundation; either |
11 |
| - * version 2.1 of the License, or (at your option) any later version. |
| 10 | + * License as published by the Free Software Foundation; version 2.1. |
12 | 11 | *
|
13 | 12 | * This library is distributed in the hope that it will be useful,
|
14 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
21 | 20 | */
|
22 | 21 | package org.exist.xquery.functions.fn;
|
23 | 22 |
|
24 |
| -import org.apache.logging.log4j.LogManager; |
25 |
| -import org.apache.logging.log4j.Logger; |
26 |
| - |
27 |
| -import org.exist.dom.QName; |
28 |
| -import org.exist.xquery.*; |
29 |
| -import org.exist.xquery.functions.xmldb.XMLDBModule; |
| 23 | +import org.exist.xquery.BasicFunction; |
| 24 | +import org.exist.xquery.ErrorCodes; |
| 25 | +import org.exist.xquery.FunctionSignature; |
| 26 | +import org.exist.xquery.XPathException; |
| 27 | +import org.exist.xquery.XQueryContext; |
30 | 28 | import org.exist.xquery.util.DocUtils;
|
31 | 29 | import org.exist.xquery.value.BooleanValue;
|
32 |
| -import org.exist.xquery.value.FunctionReturnSequenceType; |
33 |
| -import org.exist.xquery.value.FunctionParameterSequenceType; |
34 |
| -import org.exist.xquery.value.Item; |
35 | 30 | import org.exist.xquery.value.Sequence;
|
36 |
| -import org.exist.xquery.value.SequenceType; |
37 | 31 | import org.exist.xquery.value.Type;
|
38 | 32 |
|
39 | 33 | import java.net.URI;
|
40 | 34 | import java.net.URISyntaxException;
|
41 | 35 |
|
| 36 | +import static org.exist.xquery.FunctionDSL.optParam; |
| 37 | +import static org.exist.xquery.FunctionDSL.returns; |
| 38 | +import static org.exist.xquery.functions.fn.FnModule.functionSignature; |
| 39 | + |
42 | 40 | /**
|
43 |
| - * Implements the XQuery's fn:doc-available() function. |
| 41 | + * Implementation of the XPath fn:doc-available() function. |
| 42 | + * See <a href="https://www.w3.org/TR/xpath-functions-31/#func-doc-available">14.6.2 fn:doc-available</a> in the |
| 43 | + * W3C XPath and XQuery Functions and Operators 3.1 specification. |
44 | 44 | *
|
45 |
| - * @author <a href="mailto:[email protected]">Pierrick Brihaye</a> |
46 |
| - * @author wolf |
| 45 | + * @author <a href="mailto:[email protected]">Adam Retter</a> |
47 | 46 | */
|
48 |
| -public class FunDocAvailable extends Function { |
| 47 | +public class FunDocAvailable extends BasicFunction { |
49 | 48 |
|
50 |
| - protected static final Logger logger = LogManager.getLogger(FunDocAvailable.class); |
| 49 | + public static final FunctionSignature FS_DOC_AVAILABLE = functionSignature( |
| 50 | + "doc-available", |
| 51 | + "The function returns true if and only if the function call fn:doc($uri) would return a document node.", |
| 52 | + returns(Type.BOOLEAN, "If a call on fn:doc($uri) would return a document node, this function returns true. In all other cases this function returns false."), |
| 53 | + optParam("uri", Type.STRING, "The URI to check for a document.") |
| 54 | + ); |
51 | 55 |
|
52 |
| - public static final FunctionSignature signature = |
53 |
| - new FunctionSignature( |
54 |
| - new QName("doc-available", Function.BUILTIN_FUNCTION_NS), |
55 |
| - "Returns whether or not the document, $document-uri, " + |
56 |
| - "specified in the input sequence is available. " + |
57 |
| - XMLDBModule.ANY_URI, |
58 |
| - new SequenceType[]{ |
59 |
| - new FunctionParameterSequenceType("document-uri", Type.STRING, |
60 |
| - Cardinality.ZERO_OR_ONE, "The document URI") |
61 |
| - }, |
62 |
| - new FunctionReturnSequenceType(Type.BOOLEAN, Cardinality.EXACTLY_ONE, |
63 |
| - "true() if the document is available, false() otherwise")); |
64 |
| - |
65 |
| - public FunDocAvailable(final XQueryContext context) { |
| 56 | + public FunDocAvailable(final XQueryContext context, final FunctionSignature signature) { |
66 | 57 | super(context, signature);
|
67 | 58 | }
|
68 | 59 |
|
69 | 60 | @Override
|
70 |
| - public int getDependencies() { |
71 |
| - return Dependency.CONTEXT_SET; |
72 |
| - } |
73 |
| - |
74 |
| - @Override |
75 |
| - public Sequence eval(final Sequence contextSequence, final Item contextItem) |
76 |
| - throws XPathException { |
77 |
| - if (context.getProfiler().isEnabled()) { |
78 |
| - context.getProfiler().start(this); |
79 |
| - context.getProfiler().message(this, Profiler.DEPENDENCIES, |
80 |
| - "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies())); |
81 |
| - if (contextSequence != null) { |
82 |
| - context.getProfiler().message(this, Profiler.START_SEQUENCES, |
83 |
| - "CONTEXT SEQUENCE", contextSequence); |
84 |
| - } |
85 |
| - if (contextItem != null) { |
86 |
| - context.getProfiler().message(this, Profiler.START_SEQUENCES, |
87 |
| - "CONTEXT ITEM", contextItem.toSequence()); |
88 |
| - } |
| 61 | + public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException { |
| 62 | + if (args.length == 0) { |
| 63 | + return BooleanValue.FALSE; |
89 | 64 | }
|
90 | 65 |
|
91 |
| - Sequence result = BooleanValue.FALSE; |
92 |
| - final Sequence arg = getArgument(0).eval(contextSequence, contextItem); |
93 |
| - if (!arg.isEmpty()) { |
94 |
| - final String path = arg.itemAt(0).getStringValue(); |
95 |
| - |
96 |
| - try { |
97 |
| - new URI(path); |
98 |
| - } catch (final URISyntaxException e) { |
99 |
| - throw new XPathException(this, ErrorCodes.FODC0005, e.getMessage(), arg, e); |
100 |
| - } |
101 |
| - |
102 |
| - try { |
103 |
| - result = BooleanValue.valueOf(DocUtils.isDocumentAvailable(this.context, path, this)); |
104 |
| - } catch (final XPathException e) { |
105 |
| - result = BooleanValue.FALSE; |
| 66 | + final String uri = args[0].getStringValue(); |
| 67 | + try { |
| 68 | + new URI(uri); |
| 69 | + } catch (final URISyntaxException e) { |
| 70 | + if (context.getXQueryVersion() == 31) { |
| 71 | + // XPath 3.1 |
| 72 | + return BooleanValue.FALSE; |
| 73 | + } else { |
| 74 | + // XPath 2.0 and 3.0 |
| 75 | + throw new XPathException(this, ErrorCodes.FODC0005, e.getMessage(), args[0], e); |
106 | 76 | }
|
107 | 77 | }
|
108 | 78 |
|
109 |
| - if (context.getProfiler().isEnabled()) { |
110 |
| - context.getProfiler().end(this, "", result); |
| 79 | + try { |
| 80 | + return BooleanValue.valueOf(DocUtils.isDocumentAvailable(this.context, uri, this)); |
| 81 | + } catch (final XPathException e) { |
| 82 | + return BooleanValue.FALSE; |
111 | 83 | }
|
112 |
| - |
113 |
| - return result; |
114 |
| - } |
115 |
| - |
116 |
| - @Override |
117 |
| - public void resetState(final boolean postOptimization) { |
118 |
| - super.resetState(postOptimization); |
119 |
| - getArgument(0).resetState(postOptimization); |
120 | 84 | }
|
121 | 85 | }
|
0 commit comments