1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
public void printDocument(String path, Document doc, String encoding) throws OdXmlException
{
DOMImplementation domImplementation = doc.getImplementation();
PrintWriter out = null;
File f= null;
try {
try {
if (isValidJavaEncoding(encoding)) {
f = new File(path);
out = new PrintWriter(f, encoding);
}
else {
f = new File(path);
out = new PrintWriter(f, OdXmlUtil.DEFAULT_ENCODING);
encoding = OdXmlUtil.DEFAULT_ENCODING;
}
if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) {
DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS", "3.0");
LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
DOMConfiguration domConfiguration = lsSerializer.getDomConfig();
if (domConfiguration.canSetParameter("format-pretty-print", Boolean.TRUE)) {
lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
}
if (domConfiguration.canSetParameter("element-content-whitespace", Boolean.TRUE)) {
lsSerializer.getDomConfig().setParameter("element-content-whitespace", Boolean.TRUE);
}
LSOutput lsOutput = domImplementationLS.createLSOutput();
lsOutput.setEncoding(encoding);
lsOutput.setCharacterStream(out);
lsSerializer.write(doc, lsOutput);
} else {
throw new RuntimeException("DOM 3.0 LS and/or DOM 2.0 Core not supported.");
}
out.flush();
}
finally {
if (f != null) {
f= null;
}
if (out != null) {
out.close();
}
}
}
catch (Exception e) {
...
}
} |
Partager