Debian Codegraph @

net/sf/saxon/functions/StreamAvailable.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.event.PipelineConfiguration;
11 import net.sf.saxon.event.ProxyReceiver;
12 import net.sf.saxon.event.Receiver;
13 import net.sf.saxon.event.Sink;
14 import net.sf.saxon.expr.Callable;
15 import net.sf.saxon.expr.XPathContext;
16 import net.sf.saxon.expr.parser.Location;
17 import net.sf.saxon.lib.ParseOptions;
18 import net.sf.saxon.om.NodeName;
19 import net.sf.saxon.om.Sequence;
20 import net.sf.saxon.trans.QuitParsingException;
21 import net.sf.saxon.trans.XPathException;
22 import net.sf.saxon.type.SchemaType;
23 import net.sf.saxon.value.BooleanValue;
24
25 /**
26 * This class implements the XSLT 3.0 function stream-available()
27 */
28
29
30 public class StreamAvailable extends SystemFunction implements Callable {
31
32 /**
33 * Call the Callable.
34 *
35 * @param context the dynamic evaluation context
36 * @param arguments the values of the arguments, supplied as Sequences.
37 * <p>Generally it is advisable, if calling iterate() to process a supplied sequence, to
38 * call it only once; if the value is required more than once, it should first be converted
39 * to a {@link net.sf.saxon.om.GroundedValue} by calling the utility methd
40 * SequenceTool.toGroundedValue().</p>
41 * <p>If the expected value is a single item, the item should be obtained by calling
42 * Sequence.head(): it cannot be assumed that the item will be passed as an instance of
43 * {@link net.sf.saxon.om.Item} or {@link net.sf.saxon.value.AtomicValue}.</p>
44 * <p>It is the caller's responsibility to perform any type conversions required
45 * to convert arguments to the type expected by the callee. An exception is where
46 * this Callable is explicitly an argument-converting wrapper around the original
47 * Callable.</p>
48 * @return the result of the evaluation, in the form of a Sequence. It is the responsibility
49 * of the callee to ensure that the type of result conforms to the expected result type.
50 * @throws net.sf.saxon.trans.XPathException
51 * if a dynamic error occurs during the evaluation of the argument
52 */
53 public BooleanValue call(XPathContext context, Sequence[] arguments) throws XPathException {
54 boolean result = isAvailable(arguments[0].head().getStringValue(), context);
55 return BooleanValue.get(result);
56 }
57
58 /**
59 * Do the work.
60 * @param uri the URI to be tested
61 * @param context the dynamic evaluation context
62 * @return boolean true if the stream is available as defined by the spec
63 */
64
65 private boolean isAvailable(String uri, XPathContext context) {
66 try {
67 Receiver tester = new StreamTester(context.getConfiguration().makePipelineConfiguration());
68 DocumentFn.sendDoc(uri, getRetainedStaticContext().getStaticBaseUriString(), context, null, tester, new ParseOptions());
69 } catch (QuitParsingException e) {
70 // Indicates that the first element was reported and the parse was then aborted
71 return true;
72 } catch (XPathException e) {
73 return false;
74 }
75 return false;
76 }
77
78 /**
79 * An implementation of Receiver which does nothing other than throwing a QuitParsingException
80 * when the first startElement event is notified
81 */
82
83 private static class StreamTester extends ProxyReceiver {
84
85 public StreamTester(PipelineConfiguration pipe) {
86 super(new Sink(pipe));
87 }
88
89 @Override
90 public void startElement(NodeName nameCode, SchemaType typeCode, Location location, int properties) throws XPathException {
91 throw new QuitParsingException(false);
92 }
93
94 }
95
96 }