final Transformer transformer;
try {
transformer = tranFactory.newTransformer();
} catch (final TransformerConfigurationException ex) {
log.error("Exception encountered while transcoding org.w3c.dom.Document to a string: " + ex.getMessage(), ex);
throw new InternalErrorException("Exception encountered while transcoding org.w3c.dom.Document to a string: " + ex.getMessage());
}
if (prettyFormat == true) {
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
}
final Source src = new DOMSource(document);
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
final Result dest = new StreamResult(bout);
try {
transformer.transform(src, dest);
} catch (final TransformerException ex) {
log.error("Exception encountered while transcoding org.w3c.dom.Document to a string: " + ex.getMessage(), ex);
throw new InternalErrorException("Exception encountered while transcoding org.w3c.dom.Document to a string: " + ex.getMessage());
}
final String result;
try {
result = new String(bout.toByteArray(), "UTF-8");
} catch (final UnsupportedEncodingException ex) {
log.error("Exception encountered while transcoding org.w3c.dom.Document to a string: " + ex.getMessage(), ex);
throw new InternalErrorException("Exception encountered while transcoding org.w3c.dom.Document to a string: " + ex.getMessage());
}
return result;
}