Debian Codegraph @

net/sf/saxon/functions/Doc_2.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.event.Builder;
13 import net.sf.saxon.event.Sender;
14 import net.sf.saxon.expr.*;
15 import net.sf.saxon.expr.accum.Accumulator;
16 import net.sf.saxon.expr.accum.AccumulatorRegistry;
17 import net.sf.saxon.lib.ParseOptions;
18 import net.sf.saxon.lib.Validation;
19 import net.sf.saxon.ma.map.MapItem;
20 import net.sf.saxon.om.*;
21 import net.sf.saxon.style.StylesheetPackage;
22 import net.sf.saxon.trans.XPathException;
23 import net.sf.saxon.trans.XsltController;
24 import net.sf.saxon.tree.tiny.TinyBuilder;
25 import net.sf.saxon.type.BuiltInAtomicType;
26 import net.sf.saxon.value.AtomicValue;
27 import net.sf.saxon.value.BooleanValue;
28 import net.sf.saxon.value.QNameValue;
29 import net.sf.saxon.value.SequenceType;
30
31 import javax.xml.transform.Source;
32 import javax.xml.transform.dom.DOMSource;
33 import javax.xml.transform.stream.StreamSource;
34 import java.net.URI;
35 import java.net.URISyntaxException;
36 import java.util.HashSet;
37 import java.util.Map;
38 import java.util.Set;
39
40 /**
41 * Implement the saxon:doc() function - a variant of the fn:doc function with
42 * a second argument to supply option parameters. Used to underpin the xsl:source-document
43 * instruction with streamable="no".
44 */
45
46 public class Doc_2 extends SystemFunction implements Callable {
47
48 public Doc_2() {
49 }
50
51 public static OptionsParameter makeOptionsParameter() {
52 SequenceType listOfQNames = SequenceType.makeSequenceType(BuiltInAtomicType.QNAME, StaticProperty.ALLOWS_ZERO_OR_MORE);
53 OptionsParameter op = new OptionsParameter();
54 op.addAllowedOption("validation", SequenceType.SINGLE_STRING);
55 op.setAllowedValues("validation", "SXZZ0001", "strict", "lax", "preserve", "skip");
56 op.addAllowedOption("type", SequenceType.SINGLE_QNAME);
57 op.addAllowedOption("strip-space", SequenceType.SINGLE_STRING);
58 op.setAllowedValues("strip-space", "SXZZ0001", "none", "all", "package-defined");
59 op.addAllowedOption("stable", SequenceType.SINGLE_BOOLEAN);
60 op.addAllowedOption("dtd-validation", SequenceType.SINGLE_BOOLEAN);
61 op.addAllowedOption("accumulators", listOfQNames);
62 op.addAllowedOption("use-xsi-schema-location", SequenceType.SINGLE_BOOLEAN);
63 return op;
64 }
65
66
67 private ParseOptions setParseOptions(
68 Map<String, Sequence<?>> checkedOptions, XPathContext context) throws XPathException {
69 ParseOptions result = new ParseOptions(context.getConfiguration().getParseOptions());
70
71 Sequence<?> value = checkedOptions.get("validation");
72 if (value != null) {
73 String valStr = value.head().getStringValue();
74 if ("skip".equals(valStr)) {
75 valStr = "strip";
76 }
77 int v = Validation.getCode(valStr);
78 if (v == Validation.INVALID) {
79 throw new XPathException("Invalid validation value " + valStr, "SXZZ0002");
80 }
81 result.setSchemaValidationMode(v);
82 }
83 value = checkedOptions.get("type");
84 if (value != null) {
85 QNameValue qval = (QNameValue) value.head();
86 result.setTopLevelType(context.getConfiguration().getSchemaType(qval.getStructuredQName()));
87 result.setSchemaValidationMode(Validation.BY_TYPE);
88 }
89 value = checkedOptions.get("strip-space");
90 if (value != null) {
91 String s = value.head().getStringValue();
92 switch (s) {
93 case "all":
94 result.setSpaceStrippingRule(AllElementsSpaceStrippingRule.getInstance());
95 break;
96 case "none":
97 result.setSpaceStrippingRule(NoElementsSpaceStrippingRule.getInstance());
98 break;
99 case "package-defined":
100 PackageData data = getRetainedStaticContext().getPackageData();
101 if (data instanceof StylesheetPackage) {
102 result.setSpaceStrippingRule(((StylesheetPackage) data).getSpaceStrippingRule());
103 }
104 break;
105 }
106 }
107 value = checkedOptions.get("dtd-validation");
108 if (value != null) {
109 result.setDTDValidationMode(((BooleanValue)value.head()).getBooleanValue() ? Validation.STRICT : Validation.SKIP);
110 }
111 value = checkedOptions.get("accumulators");
112 if (value != null) {
113 AccumulatorRegistry reg = getRetainedStaticContext().getPackageData().getAccumulatorRegistry();
114 Set<Accumulator> accumulators = new HashSet<>();
115 SequenceIterator iter = value.iterate();
116
117 Item it;
118 while ((it = iter.next()) != null) {
119 QNameValue name = (QNameValue)it;
120 Accumulator acc = reg.getAccumulator(name.getStructuredQName());
121 accumulators.add(acc);
122 }
123 result.setApplicableAccumulators(accumulators);
124 }
125 value = checkedOptions.get("use-xsi-schema-location");
126 if (value != null) {
127 result.setUseXsiSchemaLocation(((BooleanValue) value.head()).getBooleanValue());
128 }
129 return result;
130 }
131
132
133 /**
134 * Evaluate the expression
135 *
136 * @param context the dynamic evaluation context
137 * @param arguments the values of the arguments, supplied as SequenceIterators
138 * @return the result of the evaluation, in the form of a SequenceIterator
139 * @throws XPathException if a dynamic error occurs during the evaluation of the expression
140 */
141 public ZeroOrOne<NodeInfo> call(XPathContext context, Sequence[] arguments) throws XPathException {
142 AtomicValue hrefVal = (AtomicValue) arguments[0].head();
143 if (hrefVal == null) {
144 return ZeroOrOne.empty();
145 }
146 String href = hrefVal.getStringValue();
147 Item param = arguments[1].head();
148 Map<String, Sequence<?>> checkedOptions =
149 getDetails().optionDetails.processSuppliedOptions((MapItem) param, context);
150 ParseOptions parseOptions = setParseOptions(checkedOptions, context);
151
152 NodeInfo item = fetch(href, parseOptions, context).getRootNode();
153 if (item == null) {
154 // we failed to read the document
155 throw new XPathException("Failed to load document " + href, "FODC0002", context);
156 }
157 Controller controller = context.getController();
158 if (parseOptions != null && controller instanceof XsltController) {
159 ((XsltController) controller).getAccumulatorManager().setApplicableAccumulators(
160 item.getTreeInfo(), parseOptions.getApplicableAccumulators()
161 );
162 }
163 return new ZeroOrOne<>(item);
164 }
165
166 private TreeInfo fetch(String href, ParseOptions options, XPathContext context) throws XPathException {
167 // Get a Source from the URIResolver
168 Configuration config = context.getConfiguration();
169 Controller controller = context.getController();
170 URI abs;
171 try {
172 abs = ResolveURI.makeAbsolute(href, getStaticBaseUriString());
173 } catch (URISyntaxException e) {
174 throw new XPathException("Invalid URI supplied to saxon:doc - " + e.getMessage(), "FODC0002");
175 }
176 Source source = config.getSourceResolver().resolveSource(new StreamSource(abs.toASCIIString()), config);
177
178 TreeInfo newdoc;
179 if (source instanceof NodeInfo || source instanceof DOMSource) {
180 NodeInfo startNode = controller.prepareInputTree(source);
181 newdoc = startNode.getTreeInfo();
182 } else {
183 Builder b = controller.makeBuilder();
184 if (b instanceof TinyBuilder) {
185 ((TinyBuilder) b).setStatistics(config.getTreeStatistics().SOURCE_DOCUMENT_STATISTICS);
186 }
187
188 b.setPipelineConfiguration(b.getPipelineConfiguration());
189 try {
190 Sender.send(source, b, options);
191 newdoc = b.getCurrentRoot().getTreeInfo();
192 b.reset();
193 } finally {
194 if (options.isPleaseCloseAfterUse()) {
195 ParseOptions.close(source);
196 }
197 }
198 }
199 return newdoc;
200 }
201
202 /**
203 * Get the static properties of this expression (other than its type). The result is
204 * bit-signficant. These properties are used for optimizations. In general, if
205 * property bit is set, it is true, but if it is unset, the value is unknown.
206 *
207 * @param arguments the expressions supplied as arguments to the function
208 */
209
210 public int getSpecialProperties(Expression[] arguments) {
211 return StaticProperty.ORDERED_NODESET |
212 StaticProperty.PEER_NODESET |
213 StaticProperty.NO_NODES_NEWLY_CREATED |
214 StaticProperty.SINGLE_DOCUMENT_NODESET;
215 // Declaring it as a peer node-set expression avoids sorting of expressions such as
216 // doc(XXX)/a/b/c
217 // The doc() function might appear to be creative: but it isn't, because multiple calls
218 // with the same arguments will produce identical results.
219 }
220
221
222 }
223
224