| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class StandardResultDocumentResolver implements ResultDocumentResolver { |
| 32 |
|
| 33 |
private static StandardResultDocumentResolver theInstance = new StandardResultDocumentResolver(); |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 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 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public StreamResult resolve(String href, String base) throws XPathException { |
| 66 |
|
| 67 |
|
| 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 |
|
| 112 |
|
| 113 |
|
| 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 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 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 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 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(); |
| 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 |
|