Debian Codegraph @

net/sf/saxon/resource/CatalogCollection.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.resource;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.expr.XPathContext;
12 import net.sf.saxon.functions.DocumentFn;
13 import net.sf.saxon.functions.URIQueryParameters;
14 import net.sf.saxon.lib.ParseOptions;
15 import net.sf.saxon.lib.Resource;
16 import net.sf.saxon.lib.Validation;
17 import net.sf.saxon.om.AxisInfo;
18 import net.sf.saxon.om.NodeInfo;
19 import net.sf.saxon.om.SpaceStrippingRule;
20 import net.sf.saxon.om.TreeInfo;
21 import net.sf.saxon.pattern.NodeKindTest;
22 import net.sf.saxon.trans.XPathException;
23 import net.sf.saxon.tree.iter.AxisIterator;
24 import net.sf.saxon.tree.jiter.MappingJavaIterator;
25
26 import javax.xml.transform.Source;
27 import java.io.BufferedReader;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.net.URI;
32 import java.net.URISyntaxException;
33 import java.nio.charset.Charset;
34 import java.util.ArrayList;
35 import java.util.Iterator;
36 import java.util.List;
37
38
39 public class CatalogCollection extends AbstractResourceCollection {
40
41 private boolean stable;
42 private SpaceStrippingRule whitespaceRules;
43
44 //TODO we might know the catalog File already
45 public CatalogCollection(Configuration config, String collectionURI) {
46 super(config);
47 this.collectionURI = collectionURI;
48 }
49
50
51 public Iterator<String> getResourceURIs(XPathContext context) throws XPathException {
52 StandardCollectionFinder.checkNotNull(collectionURI, context);
53 return catalogContents(collectionURI, context);
54 }
55
56
57 public Iterator<Resource> getResources(final XPathContext context) throws XPathException {
58
59 StandardCollectionFinder.checkNotNull(collectionURI, context);
60
61 Iterator<String> resourceURIs = getResourceURIs(context);
62
63 return new MappingJavaIterator<>(resourceURIs, in -> {
64 try {
65 if (in.startsWith("data:")) {
66 try {
67 Resource basicResource = DataURIScheme.decode(new URI(in));
68 return makeTypedResource(context.getConfiguration(), basicResource);
69 } catch (URISyntaxException | IllegalArgumentException e) {
70 throw new XPathException(e);
71 }
72 } else {
73 InputDetails id = getInputDetails(in);
74 id.parseOptions = new ParseOptions(context.getConfiguration().getParseOptions());
75 id.parseOptions.setSpaceStrippingRule(whitespaceRules);
76 return makeResource(context.getConfiguration(), id, in);
77 }
78 } catch (XPathException e) {
79 int onError = params == null ? URIQueryParameters.ON_ERROR_FAIL : params.getOnError();
80 if (onError == URIQueryParameters.ON_ERROR_FAIL) {
81 return new FailedResource(in, e);
82 } else if (onError == URIQueryParameters.ON_ERROR_WARNING) {
83 context.getController().warning("collection(): failed to parse " + in + ": " + e.getMessage(), e.getErrorCodeLocalPart(), null);
84 return null;
85 } else {
86 return null;
87 }
88 }
89 });
90
91 }
92
93
94 @Override
95 public boolean isStable(XPathContext context) {
96 return stable;
97 }
98
99 /**
100 * Return a StringBuilder initialized to the contents of an InputStream
101 *
102 * @param in the input stream (which is consumed by this method)
103 * @return the StringBuilder, initialized to the contents of this InputStream
104 * @throws IOException if an error occurs reading the resource
105 */
106
107 public static StringBuilder makeStringBuilderFromStream(InputStream in, String encoding) throws IOException {
108 InputStreamReader is = new InputStreamReader(in, Charset.forName(encoding));
109 StringBuilder sb = new StringBuilder();
110 BufferedReader br = new BufferedReader(is);
111 String read = br.readLine();
112
113 while (read != null) {
114 sb.append(read);
115 read = br.readLine();
116 }
117 br.close();
118 return sb;
119 }
120
121
122 /**
123 * Return a collection defined as a list of URIs in a catalog file
124 *
125 * @param href the absolute URI of the catalog file
126 * @param context the dynamic evaluation context
127 * @return an iterator over the documents in the collection
128 * @throws XPathException if any failures occur
129 */
130
131 protected Iterator<String> catalogContents(String href, final XPathContext context)
132 throws XPathException {
133
134 Source source = DocumentFn.resolveURI(href, null, null, context);
135 ParseOptions options = new ParseOptions();
136 options.setSchemaValidationMode(Validation.SKIP);
137 TreeInfo catalog = context.getConfiguration().buildDocumentTree(source, options);
138 if (catalog == null) {
139 // we failed to read the catalogue
140 XPathException err = new XPathException("Failed to load collection catalog " + href);
141 err.setErrorCode("FODC0004");
142 err.setXPathContext(context);
143 throw err;
144 }
145
146 // Now return an iterator over the documents that it refers to
147
148 AxisIterator iter =
149 catalog.getRootNode().iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT);
150 NodeInfo top = iter.next();
151 if (top == null || !("collection".equals(top.getLocalPart()) && top.getURI().isEmpty())) {
152 String message;
153 if (top == null) {
154 message = "No outermost element found in collection catalog";
155 } else {
156 message = "Outermost element of collection catalog should be Q{}collection " +
157 "(found Q{" + top.getURI() + "}" + top.getLocalPart() + ")";
158 }
159 XPathException err = new XPathException(message);
160 err.setErrorCode("FODC0004");
161 err.setXPathContext(context);
162 throw err;
163 }
164 iter.close();
165
166 String stableAtt = top.getAttributeValue("", "stable");
167 if (stableAtt != null) {
168 if ("true".equals(stableAtt)) {
169 stable = true;
170 } else if ("false".equals(stableAtt)) {
171 stable = false;
172 } else {
173 XPathException err = new XPathException(
174 "The 'stable' attribute of element <collection> must be true or false");
175 err.setErrorCode("FODC0004");
176 err.setXPathContext(context);
177 throw err;
178 }
179 }
180
181 AxisIterator documents = top.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT);
182 List<String> result = new ArrayList<>();
183 NodeInfo item;
184 while ((item = documents.next()) != null) {
185
186 if (!("doc".equals(item.getLocalPart()) &&
187 item.getURI().isEmpty())) {
188 XPathException err = new XPathException("Children of <collection> element must be <doc> elements");
189 err.setErrorCode("FODC0004");
190 err.setXPathContext(context);
191 throw err;
192 }
193 String hrefAtt = item.getAttributeValue("", "href");
194 if (hrefAtt == null) {
195 XPathException err = new XPathException("A <doc> element in the collection catalog has no @href attribute");
196 err.setErrorCode("FODC0004");
197 err.setXPathContext(context);
198 throw err;
199 }
200 String uri;
201 try {
202 uri = new URI(item.getBaseURI()).resolve(hrefAtt).toString();
203 } catch (URISyntaxException e) {
204 XPathException err = new XPathException("Invalid base URI or href URI in collection catalog: ("
205 + item.getBaseURI() + ", " + hrefAtt + ")");
206 err.setErrorCode("FODC0004");
207 err.setXPathContext(context);
208 throw err;
209 }
210 result.add(uri);
211
212 }
213
214 return result.iterator();
215 }
216
217 // TODO: provide control over error recovery (etc) through options in the catalog file.
218
219
220 }