Debian Codegraph @

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