| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class StandardOutputResolver implements OutputURIResolver { |
| 30 |
|
| 31 |
private static StandardOutputResolver theInstance = new StandardOutputResolver(); |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public static StandardOutputResolver getInstance() { |
| 40 |
return theInstance; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public StandardOutputResolver newInstance() { |
| 56 |
return this; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public Result resolve(String href, String base) throws XPathException { |
| 70 |
|
| 71 |
|
| 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 |
|
| 126 |
|
| 127 |
|
| 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 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 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(); |
| 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 |
|