Debian Codegraph @

net/sf/saxon/functions/Doc.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.Configuration;
11 import net.sf.saxon.Controller;
12 import net.sf.saxon.expr.*;
13 import net.sf.saxon.expr.parser.ExpressionTool;
14 import net.sf.saxon.expr.parser.ExpressionVisitor;
15 import net.sf.saxon.functions.registry.BuiltInFunctionSet;
16 import net.sf.saxon.lib.Feature;
17 import net.sf.saxon.lib.ParseOptions;
18 import net.sf.saxon.om.*;
19 import net.sf.saxon.trans.XPathException;
20 import net.sf.saxon.trans.XsltController;
21 import net.sf.saxon.value.AtomicValue;
22
23 /**
24 * Implement the fn:doc() function - a simplified form of the Document function
25 */
26
27 public class Doc extends SystemFunction implements Callable {
28
29 private ParseOptions parseOptions;
30
31 /**
32 * Get the parsing options set via setParseOptions()
33 * @return the parsing options
34 */
35
36 public ParseOptions getParseOptions() {
37 return parseOptions;
38 }
39
40 /**
41 * Set options to be used for the parsing operation. Defaults to the parsing options set in the Configuration
42 * @param parseOptions the parsing options to be used. Currently only affects the behaviour of the sendDocument()
43 * method (used in streamed merging)
44 */
45
46 public void setParseOptions(ParseOptions parseOptions) {
47 this.parseOptions = parseOptions;
48 }
49
50 public int getCardinality(Expression[] arguments) {
51 return arguments[0].getCardinality() & ~StaticProperty.ALLOWS_MANY;
52 }
53
54 @Override
55 public Expression makeFunctionCall(Expression... arguments) {
56 Expression expr = maybePreEvaluate(this, arguments);
57 return expr == null ? super.makeFunctionCall(arguments) : expr;
58 }
59
60 public static Expression maybePreEvaluate(final SystemFunction sf, final Expression[] arguments) {
61 if (arguments.length > 1 ||
62 !sf.getRetainedStaticContext().getConfiguration().getBooleanProperty(Feature.PRE_EVALUATE_DOC_FUNCTION)) {
63 sf.getDetails().properties = sf.getDetails().properties | BuiltInFunctionSet.LATE;
64 return null;
65
66 } else {
67 // allow early evaluation
68 return new SystemFunctionCall(sf, arguments) {
69 @Override
70 public Expression preEvaluate(ExpressionVisitor visitor) {
71 Configuration config = visitor.getConfiguration();
72 try {
73 GroundedValue firstArg = ((Literal)getArg(0)).getValue();
74 if (firstArg.getLength() == 0) {
75 return null;
76 } else if (firstArg.getLength() > 1) {
77 return this;
78 }
79 String href = firstArg.head().getStringValue();
80 if (href.indexOf('#') >= 0) {
81 return this;
82 }
83 NodeInfo item = DocumentFn.preLoadDoc(href, sf.getStaticBaseUriString(), config, getLocation());
84 if (item != null) {
85 Expression constant = Literal.makeLiteral(item);
86 ExpressionTool.copyLocationInfo(getArg(0), constant);
87 return constant;
88 }
89 } catch (Exception err) {
90 // ignore the exception and try again at run-time
91 return this;
92 }
93 return this;
94 }
95 };
96
97 }
98
99 }
100
101
102
103 /**
104 * Evaluate the expression
105 *
106 * @param context the dynamic evaluation context
107 * @param arguments the values of the arguments, supplied as SequenceIterators
108 * @return the result of the evaluation, in the form of a SequenceIterator
109 * @throws net.sf.saxon.trans.XPathException
110 * if a dynamic error occurs during the evaluation of the expression
111 */
112 public ZeroOrOne<NodeInfo> call(XPathContext context, Sequence[] arguments) throws XPathException {
113 AtomicValue hrefVal = (AtomicValue) arguments[0].head();
114 if (hrefVal == null) {
115 return ZeroOrOne.empty();
116 }
117 String href = hrefVal.getStringValue();
118 PackageData packageData = getRetainedStaticContext().getPackageData();
119 NodeInfo item = DocumentFn.makeDoc(href, getRetainedStaticContext().getStaticBaseUriString(), packageData, getParseOptions(), context, null, false);
120 if (item == null) {
121 // we failed to read the document
122 throw new XPathException("Failed to load document " + href, "FODC0002", context);
123 }
124 Controller controller = context.getController();
125 if (parseOptions != null && controller instanceof XsltController) {
126 ((XsltController)controller).getAccumulatorManager().setApplicableAccumulators(
127 item.getTreeInfo(), parseOptions.getApplicableAccumulators()
128 );
129 }
130 return new ZeroOrOne<>(item);
131 }
132
133 /**
134 * Get the static properties of this expression (other than its type). The result is
135 * bit-signficant. These properties are used for optimizations. In general, if
136 * property bit is set, it is true, but if it is unset, the value is unknown.
137 * @param arguments the expressions supplied as arguments to the function
138 */
139
140 public int getSpecialProperties(Expression[] arguments) {
141 return StaticProperty.ORDERED_NODESET |
142 StaticProperty.PEER_NODESET |
143 StaticProperty.NO_NODES_NEWLY_CREATED |
144 StaticProperty.SINGLE_DOCUMENT_NODESET;
145 // Declaring it as a peer node-set expression avoids sorting of expressions such as
146 // doc(XXX)/a/b/c
147 // The doc() function might appear to be creative: but it isn't, because multiple calls
148 // with the same arguments will produce identical results.
149 }
150
151
152 }
153