Debian Codegraph @

net/sf/saxon/resource/XmlResource.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.URIQueryParameters;
13 import net.sf.saxon.lib.ParseOptions;
14 import net.sf.saxon.lib.Resource;
15 import net.sf.saxon.lib.ResourceFactory;
16 import net.sf.saxon.om.Item;
17 import net.sf.saxon.om.NodeInfo;
18 import net.sf.saxon.trans.XPathException;
19
20 import javax.xml.transform.Source;
21 import javax.xml.transform.stream.StreamSource;
22 import java.io.IOException;
23
24 /**
25 * The class is an implementation of the generic Resource object (typically an item in a collection)
26 * representing an XML document
27 */
28
29 public class XmlResource implements Resource {
30 private Source source;
31 private ParseOptions options;
32 private NodeInfo doc;
33 private Configuration config;
34 private int onError = URIQueryParameters.ON_ERROR_FAIL;
35
36 public final static ResourceFactory FACTORY = new ResourceFactory() {
37 public Resource makeResource(Configuration config, String resourceURI, String contentType, AbstractResourceCollection.InputDetails details) throws XPathException {
38 Source source = new StreamSource(details.inputStream, resourceURI);
39 ParseOptions options = details.parseOptions;
40 if (options == null) {
41 options = config.getParseOptions();
42 }
43 return new XmlResource(config, source, options, details.onError);
44 }
45 };
46
47 /**
48 * Create an XML resource using a specific node
49 *
50 * @param doc the node in question (usually but not necessarily a document node)
51 */
52
53 public XmlResource(NodeInfo doc) {
54 this.config = doc.getConfiguration();
55 this.doc = doc;
56 }
57
58 /**
59 * Create an XML resource using a specific node. (Method retained for backwards compatibility).
60 * @param config the Saxon Configuration. This must be the configuration to which the node
61 * belongs.
62 * @param doc the node in question (usually but not necessarily a document node)
63 */
64
65 public XmlResource(Configuration config, NodeInfo doc) {
66 this.config = config;
67 this.doc = doc;
68 if (config != doc.getConfiguration()) {
69 throw new IllegalArgumentException("Supplied node belongs to wrong configuration");
70 }
71 }
72
73 /**
74 * Create an XML resource using a JAXP Source object
75 *
76 * @param config the Saxon Configuration
77 * @param source the JAXP Source object
78 * @param options options for parsing the XML if it needs to be parsed
79 * @param onError flag indicating what should happen on a parsing error: one of
80 * {@link URIQueryParameters#ON_ERROR_FAIL}, {@link URIQueryParameters#ON_ERROR_WARNING},
81 * or {@link URIQueryParameters#ON_ERROR_IGNORE}
82 */
83
84 public XmlResource(Configuration config, Source source, ParseOptions options, int onError) {
85 this.config = config;
86 this.source = source;
87 this.options = options;
88 this.onError = onError;
89 }
90
91 public String getResourceURI() {
92 if (doc == null) {
93 return source.getSystemId();
94 } else {
95 return doc.getSystemId();
96 }
97 }
98
99 /**
100 * Get an item representing the resource: in this case a document node for the XML document.
101 * @param context the XPath evaluation context
102 * @return the document; or null if there is an error and the error is to be ignored
103 * @throws XPathException if (for example) XML parsing fails
104 */
105
106 public Item getItem(XPathContext context) throws XPathException {
107 if (doc == null) {
108 try {
109 doc = config.buildDocumentTree(source, options).getRootNode();
110 } catch (XPathException e) {
111 if (onError == URIQueryParameters.ON_ERROR_FAIL) {
112 XPathException e2 = new XPathException("collection(): failed to parse XML file " + source.getSystemId() + ": " + e.getMessage(),
113 e.getErrorCodeLocalPart());
114 throw e2;
115 } else if (onError == URIQueryParameters.ON_ERROR_WARNING) {
116 context.getController().warning("collection(): failed to parse XML file " + source.getSystemId() + ": " + e.getMessage(), e.getErrorCodeLocalPart(), null);
117 } else {
118 return null;
119 }
120 }
121 if (source instanceof StreamSource && ((StreamSource)source).getInputStream() != null) {
122 try {
123 ((StreamSource) source).getInputStream().close();
124 } catch (IOException e) {
125 // ignore the failure
126 }
127 }
128 }
129 return doc;
130 }
131
132 /**
133 * Get the media type (MIME type) of the resource if known
134 *
135 * @return the string "application/xml"
136 */
137
138 public String getContentType() {
139 return "application/xml";
140 }
141
142
143 }