|
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 |
| - * This library is free software; you can redistribute it and/or |
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. |
| 8 | + * Use of this software is governed by the Business Source License 1.1 |
| 9 | + * included in the LICENSE file and at www.mariadb.com/bsl11. |
12 | 10 | *
|
13 |
| - * This library is distributed in the hope that it will be useful, |
14 |
| - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
| - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 |
| - * Lesser General Public License for more details. |
| 11 | + * Change Date: 2028-04-27 |
17 | 12 | *
|
18 |
| - * You should have received a copy of the GNU Lesser General Public |
19 |
| - * License along with this library; if not, write to the Free Software |
20 |
| - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 13 | + * On the date above, in accordance with the Business Source License, use |
| 14 | + * of this software will be governed by the Apache License, Version 2.0. |
| 15 | + * |
| 16 | + * Additional Use Grant: Production use of the Licensed Work for a permitted |
| 17 | + * purpose. A Permitted Purpose is any purpose other than a Competing Use. |
| 18 | + * A Competing Use means making the Software available to others in a commercial |
| 19 | + * product or service that: substitutes for the Software; substitutes for any |
| 20 | + * other product or service we offer using the Software that exists as of the |
| 21 | + * date we make the Software available; or offers the same or substantially |
| 22 | + * similar functionality as the Software. |
21 | 23 | */
|
22 | 24 | package org.exist.xquery.functions.fn;
|
23 | 25 |
|
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; |
| 26 | +import org.exist.xquery.BasicFunction; |
| 27 | +import org.exist.xquery.ErrorCodes; |
| 28 | +import org.exist.xquery.FunctionSignature; |
| 29 | +import org.exist.xquery.XPathException; |
| 30 | +import org.exist.xquery.XQueryContext; |
30 | 31 | import org.exist.xquery.util.DocUtils;
|
31 | 32 | 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 | 33 | import org.exist.xquery.value.Sequence;
|
36 |
| -import org.exist.xquery.value.SequenceType; |
37 | 34 | import org.exist.xquery.value.Type;
|
38 | 35 |
|
39 | 36 | import java.net.URI;
|
40 | 37 | import java.net.URISyntaxException;
|
41 | 38 |
|
| 39 | +import static org.exist.xquery.FunctionDSL.optParam; |
| 40 | +import static org.exist.xquery.FunctionDSL.returns; |
| 41 | +import static org.exist.xquery.functions.fn.FnModule.functionSignature; |
| 42 | + |
42 | 43 | /**
|
43 |
| - * Implements the XQuery's fn:doc-available() function. |
| 44 | + * Implementation of the XPath fn:doc-available() function. |
| 45 | + * {@see https://www.w3.org/TR/xpath-functions-31/#func-doc-available}. |
44 | 46 | *
|
45 |
| - * @author <a href="mailto:[email protected]">Pierrick Brihaye</a> |
46 |
| - * @author wolf |
| 47 | + * @author <a href="mailto:[email protected]">Adam Retter</a> |
47 | 48 | */
|
48 |
| -public class FunDocAvailable extends Function { |
49 |
| - |
50 |
| - protected static final Logger logger = LogManager.getLogger(FunDocAvailable.class); |
| 49 | +public class FunDocAvailable extends BasicFunction { |
51 | 50 |
|
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")); |
| 51 | + public static final FunctionSignature FS_DOC_AVAILABLE = functionSignature( |
| 52 | + "doc-available", |
| 53 | + "The function returns true if and only if the function call fn:doc($uri) would return a document node.", |
| 54 | + 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."), |
| 55 | + optParam("uri", Type.STRING, "The URI to check for a document.") |
| 56 | + ); |
64 | 57 |
|
65 |
| - public FunDocAvailable(final XQueryContext context) { |
| 58 | + public FunDocAvailable(final XQueryContext context, final FunctionSignature signature) { |
66 | 59 | super(context, signature);
|
67 | 60 | }
|
68 | 61 |
|
69 | 62 | @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 |
| - } |
| 63 | + public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException { |
| 64 | + if (args.length == 0) { |
| 65 | + return BooleanValue.FALSE; |
89 | 66 | }
|
90 | 67 |
|
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; |
| 68 | + final String uri = args[0].getStringValue(); |
| 69 | + try { |
| 70 | + new URI(uri); |
| 71 | + } catch (final URISyntaxException e) { |
| 72 | + if (context.getXQueryVersion() == 31) { |
| 73 | + // XPath 3.1 |
| 74 | + return BooleanValue.FALSE; |
| 75 | + } else { |
| 76 | + // XPath 2.0 and 3.0 |
| 77 | + throw new XPathException(this, ErrorCodes.FODC0005, e.getMessage(), args[0], e); |
106 | 78 | }
|
107 | 79 | }
|
108 | 80 |
|
109 |
| - if (context.getProfiler().isEnabled()) { |
110 |
| - context.getProfiler().end(this, "", result); |
| 81 | + try { |
| 82 | + return BooleanValue.valueOf(DocUtils.isDocumentAvailable(this.context, uri, this)); |
| 83 | + } catch (final XPathException e) { |
| 84 | + return BooleanValue.FALSE; |
111 | 85 | }
|
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 | 86 | }
|
121 | 87 | }
|
0 commit comments