Debian Codegraph @

net/sf/saxon/functions/DocAvailable.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.functions;
9
10 import net.sf.saxon.expr.PackageData;
11 import net.sf.saxon.expr.XPathContext;
12 import net.sf.saxon.om.*;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.value.AtomicValue;
15 import net.sf.saxon.value.BooleanValue;
16
17 /**
18 * Implement the fn:doc-available() function
19 */
20
21 public class DocAvailable extends SystemFunction {
22
23 private boolean isDocAvailable(AtomicValue hrefVal, XPathContext context) throws XPathException {
24 if (hrefVal == null) {
25 return false;
26 }
27 String href = hrefVal.getStringValue();
28 return docAvailable(href, context);
29 }
30
31 /**
32 * Evaluate the expression
33 *
34 * @param context the dynamic evaluation context
35 * @param arguments the values of the arguments, supplied as Sequences
36 * @return the result of the evaluation, in the form of a Sequence
37 * @throws net.sf.saxon.trans.XPathException
38 * if a dynamic error occurs during the evaluation of the expression
39 */
40 public BooleanValue call(XPathContext context, Sequence[] arguments) throws XPathException {
41 return BooleanValue.get(isDocAvailable((AtomicValue) arguments[0].head(), context));
42 }
43
44 public boolean docAvailable(String href, XPathContext context) {
45 try {
46 PackageData packageData = getRetainedStaticContext().getPackageData();
47 DocumentURI documentKey = DocumentFn.computeDocumentKey(href, getStaticBaseUriString(), packageData, context);
48 DocumentPool pool = context.getController().getDocumentPool();
49 if (pool.isMarkedUnavailable(documentKey)) {
50 return false;
51 }
52 TreeInfo doc = pool.find(documentKey);
53 if (doc != null) {
54 return true;
55 }
56 Item item = DocumentFn.makeDoc(href, getStaticBaseUriString(), packageData, null, context, null, true);
57 if (item != null) {
58 return true;
59 } else {
60 // The document does not exist; ensure that this remains the case
61 pool.markUnavailable(documentKey);
62 return false;
63 }
64 } catch (XPathException e) {
65 return false;
66 }
67 }
68
69
70 }
71