Debian Codegraph @

net/sf/saxon/lib/StandardResultDocumentResolver.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.lib;
9
10 import net.sf.saxon.event.PipelineConfiguration;
11 import net.sf.saxon.event.Receiver;
12 import net.sf.saxon.expr.XPathContext;
13 import net.sf.saxon.serialize.SerializationProperties;
14 import net.sf.saxon.trans.XPathException;
15
16 import javax.xml.transform.Result;
17 import javax.xml.transform.stream.StreamResult;
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.OutputStream;
21 import java.io.Writer;
22 import java.net.*;
23
24
25 /**
26 * This class defines the default ResultDocumentResolver. This is a counterpart to the JAXP
27 * URIResolver, but is used to map the URI of a secondary result document to a Receiver object
28 * which acts as the destination for the new document.
29 */
30
31 public class StandardResultDocumentResolver implements ResultDocumentResolver {
32
33 private static StandardResultDocumentResolver theInstance = new StandardResultDocumentResolver();
34
35 /**
36 * Get a singular instance
37 *
38 * @return the singleton instance of the class
39 */
40
41 public static StandardResultDocumentResolver getInstance() {
42 return theInstance;
43 }
44
45 @Override
46 public Receiver resolve(
47 XPathContext context, String href, String baseUri, SerializationProperties properties)
48 throws XPathException {
49 StreamResult result = resolve(href, baseUri);
50 SerializerFactory factory = context.getConfiguration().getSerializerFactory();
51 PipelineConfiguration pipe = context.getController().makePipelineConfiguration();
52 return factory.getReceiver(result, properties, pipe);
53 }
54
55 /**
56 * Resolve an output URI
57 *
58 * @param href The relative URI of the output document. This corresponds to the
59 * href attribute of the xsl:result-document instruction.
60 * @param base The base URI that should be used. This is the base output URI,
61 * normally the URI of the principal output file.
62 * @return a Result object representing the destination for the XML document
63 */
64
65 public StreamResult resolve(String href, /*@Nullable*/ String base) throws XPathException {
66
67 // System.err.println("Output URI Resolver (href='" + href + "', base='" + base + "')");
68
69 String which = "base";
70 try {
71 URI absoluteURI;
72 if (href.isEmpty()) {
73 if (base == null) {
74 throw new XPathException("The system identifier of the principal output file is unknown");
75 }
76 absoluteURI = new URI(base);
77 } else {
78 which = "relative";
79 absoluteURI = new URI(href);
80 }
81 if (!absoluteURI.isAbsolute()) {
82 if (base == null) {
83 throw new XPathException("The system identifier of the principal output file is unknown");
84 }
85 which = "base";
86 URI baseURI = new URI(base);
87 which = "relative";
88 absoluteURI = baseURI.resolve(href);
89 }
90
91 return createResult(absoluteURI);
92 } catch (URISyntaxException err) {
93 throw new XPathException("Invalid syntax for " + which + " URI", err);
94 } catch (IllegalArgumentException err2) {
95 throw new XPathException("Invalid " + which + " URI syntax", err2);
96 } catch (MalformedURLException err3) {
97 throw new XPathException("Resolved URL is malformed", err3);
98 } catch (UnknownServiceException err5) {
99 throw new XPathException("Specified protocol does not allow output", err5);
100 } catch (IOException err4) {
101 throw new XPathException("Cannot open connection to specified URL", err4);
102 }
103 }
104
105 protected StreamResult createResult(URI absoluteURI) throws XPathException, IOException {
106 if ("file".equals(absoluteURI.getScheme())) {
107 return makeOutputFile(absoluteURI);
108
109 } else {
110
111 // See if the Java VM can conjure up a writable URL connection for us.
112 // This is optimistic: I have yet to discover a URL scheme that it can handle "out of the box".
113 // But it can apparently be achieved using custom-written protocol handlers.
114
115 URLConnection connection = absoluteURI.toURL().openConnection();
116 connection.setDoInput(false);
117 connection.setDoOutput(true);
118 connection.connect();
119 OutputStream stream = connection.getOutputStream();
120 StreamResult result = new StreamResult(stream);
121 result.setSystemId(absoluteURI.toASCIIString());
122 return result;
123 }
124 }
125
126 /**
127 * Create an output file (unless it already exists) and return a reference to it as a Result object
128 *
129 * @param absoluteURI the URI of the output file (which should typically use the "file" scheme)
130 * @return a Result object referencing this output file
131 * @throws XPathException if the URI is not writable
132 */
133
134 public static synchronized StreamResult makeOutputFile(URI absoluteURI) throws XPathException {
135 try {
136 return new StreamResult(new File(absoluteURI));
137 } catch (IllegalArgumentException err) {
138 throw new XPathException("Cannot write to URI " + absoluteURI + " (" + err.getMessage() + ")");
139 }
140 }
141
142 /**
143 * Signal completion of the result document. This method is called by the system
144 * when the result document has been successfully written. It allows the resolver
145 * to perform tidy-up actions such as closing output streams, or firing off
146 * processes that take this result tree as input. Note that the OutputURIResolver
147 * is stateless, so the original href is supplied to identify the document
148 * that has been completed.
149 */
150
151 public void close(Result result) throws XPathException {
152 if (result instanceof StreamResult) {
153 OutputStream stream = ((StreamResult) result).getOutputStream();
154 if (stream != null) {
155 try {
156 stream.close();
157 } catch (IOException err) {
158 throw new XPathException("Failed while closing output file", err);
159 }
160 }
161 Writer writer = ((StreamResult) result).getWriter(); // Path not used, but there for safety
162 if (writer != null) {
163 try {
164 writer.close();
165 } catch (IOException err) {
166 throw new XPathException("Failed while closing output file", err);
167 }
168 }
169 }
170 }
171
172 }
173