| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.serialize; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.lib.SaxonOutputKeys; |
| 12 |
import net.sf.saxon.serialize.charcode.CharacterSet; |
| 13 |
import net.sf.saxon.serialize.charcode.UTF16CharacterSet; |
| 14 |
import net.sf.saxon.serialize.charcode.UTF8CharacterSet; |
| 15 |
import net.sf.saxon.trans.SaxonErrorCode; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
|
| 18 |
import javax.xml.transform.OutputKeys; |
| 19 |
import javax.xml.transform.stream.StreamResult; |
| 20 |
import java.io.*; |
| 21 |
import java.net.URI; |
| 22 |
import java.net.URISyntaxException; |
| 23 |
import java.util.Properties; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class ExpandedStreamResult { |
| 32 |
|
| 33 |
private Configuration config; |
| 34 |
private Properties outputProperties; |
| 35 |
private String systemId; |
| 36 |
private Writer writer; |
| 37 |
private OutputStream outputStream; |
| 38 |
private CharacterSet characterSet; |
| 39 |
private String encoding; |
| 40 |
private boolean mustClose; |
| 41 |
private boolean allCharactersEncodable; |
| 42 |
|
| 43 |
public ExpandedStreamResult(Configuration config, StreamResult result, Properties outputProperties) throws XPathException { |
| 44 |
this.config = config; |
| 45 |
this.systemId = result.getSystemId(); |
| 46 |
this.writer = result.getWriter(); |
| 47 |
this.outputStream = result.getOutputStream(); |
| 48 |
this.encoding = outputProperties.getProperty(OutputKeys.ENCODING); |
| 49 |
if (encoding == null) { |
| 50 |
encoding = "UTF8"; |
| 51 |
allCharactersEncodable = true; |
| 52 |
} else if (encoding.equalsIgnoreCase("UTF-8")) { |
| 53 |
encoding = "UTF8"; |
| 54 |
allCharactersEncodable = true; |
| 55 |
} else if (encoding.equalsIgnoreCase("UTF-16")) { |
| 56 |
encoding = "UTF16"; |
| 57 |
} |
| 58 |
|
| 59 |
if (characterSet == null) { |
| 60 |
characterSet = config.getCharacterSetFactory().getCharacterSet(encoding); |
| 61 |
} |
| 62 |
|
| 63 |
String byteOrderMark = outputProperties.getProperty(SaxonOutputKeys.BYTE_ORDER_MARK); |
| 64 |
if ("no".equals(byteOrderMark) && "UTF16".equals(encoding)) { |
| 65 |
|
| 66 |
encoding = "UTF-16BE"; |
| 67 |
} else if (!(characterSet instanceof UTF8CharacterSet)) { |
| 68 |
|
| 69 |
|
| 70 |
encoding = characterSet.getCanonicalName(); |
| 71 |
} |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public Writer obtainWriter() throws XPathException { |
| 82 |
if (writer != null) { |
| 83 |
return writer; |
| 84 |
} else { |
| 85 |
OutputStream os = obtainOutputStream(); |
| 86 |
writer = makeWriterFromOutputStream(os); |
| 87 |
return writer; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
protected OutputStream obtainOutputStream() throws XPathException { |
| 92 |
if (outputStream != null) { |
| 93 |
return outputStream; |
| 94 |
} |
| 95 |
String uriString = systemId; |
| 96 |
if (uriString == null) { |
| 97 |
throw new XPathException("Result has no system ID, writer, or output stream defined"); |
| 98 |
} |
| 99 |
|
| 100 |
try { |
| 101 |
File file = makeWritableOutputFile(uriString); |
| 102 |
outputStream = new FileOutputStream(file); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
mustClose = true; |
| 107 |
} catch (FileNotFoundException | URISyntaxException | IllegalArgumentException fnf) { |
| 108 |
|
| 109 |
throw new XPathException(fnf); |
| 110 |
} |
| 111 |
|
| 112 |
return outputStream; |
| 113 |
} |
| 114 |
|
| 115 |
public static File makeWritableOutputFile(String uriString) throws URISyntaxException, XPathException { |
| 116 |
URI uri = new URI(uriString); |
| 117 |
if (!uri.isAbsolute()) { |
| 118 |
try { |
| 119 |
uri = new File(uriString).getAbsoluteFile().toURI(); |
| 120 |
} catch (Exception e) { |
| 121 |
|
| 122 |
} |
| 123 |
} |
| 124 |
File file = new File(uri); |
| 125 |
try { |
| 126 |
if ("file".equals(uri.getScheme()) && !file.exists()) { |
| 127 |
File directory = file.getParentFile(); |
| 128 |
if (directory != null && !directory.exists()) { |
| 129 |
directory.mkdirs(); |
| 130 |
} |
| 131 |
file.createNewFile(); |
| 132 |
} |
| 133 |
if (file.isDirectory()) { |
| 134 |
throw new XPathException("Cannot write to a directory: " + uriString, SaxonErrorCode.SXRD0004); |
| 135 |
} |
| 136 |
if (!file.canWrite()) { |
| 137 |
throw new XPathException("Cannot write to URI " + uriString, SaxonErrorCode.SXRD0004); |
| 138 |
} |
| 139 |
} catch (IOException err) { |
| 140 |
throw new XPathException("Failed to create output file " + uri, err); |
| 141 |
} |
| 142 |
return file; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public boolean usesWriter() { |
| 154 |
return true; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public void setWriter(Writer writer) throws XPathException { |
| 165 |
this.writer = writer; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
if (writer instanceof OutputStreamWriter && outputProperties != null) { |
| 172 |
String enc = ((OutputStreamWriter) writer).getEncoding(); |
| 173 |
outputProperties.setProperty(OutputKeys.ENCODING, enc); |
| 174 |
characterSet = config.getCharacterSetFactory().getCharacterSet(outputProperties); |
| 175 |
allCharactersEncodable = characterSet instanceof UTF8CharacterSet || |
| 176 |
characterSet instanceof UTF16CharacterSet; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public Writer getWriter() { |
| 187 |
return writer; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
private Writer makeWriterFromOutputStream(OutputStream stream) throws XPathException { |
| 200 |
outputStream = stream; |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
try { |
| 212 |
String javaEncoding = encoding; |
| 213 |
if (encoding.equalsIgnoreCase("iso-646") || encoding.equalsIgnoreCase("iso646")) { |
| 214 |
javaEncoding = "US-ASCII"; |
| 215 |
} |
| 216 |
if (encoding.equalsIgnoreCase("UTF8")) { |
| 217 |
writer = new UTF8Writer(outputStream); |
| 218 |
} else { |
| 219 |
writer = new BufferedWriter( |
| 220 |
new OutputStreamWriter( |
| 221 |
outputStream, javaEncoding)); |
| 222 |
} |
| 223 |
return writer; |
| 224 |
|
| 225 |
} catch (Exception err) { |
| 226 |
if (encoding.equalsIgnoreCase("UTF8")) { |
| 227 |
throw new XPathException("Failed to create a UTF8 output writer"); |
| 228 |
} |
| 229 |
throw new XPathException("Encoding " + encoding + " is not supported", "SESU0007"); |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
public OutputStream getOutputStream() { |
| 250 |
return outputStream; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
public CharacterSet getCharacterSet() { |
| 258 |
return characterSet; |
| 259 |
} |
| 260 |
|
| 261 |
} |
| 262 |
|