use of javax.xml.transform.Source in project poi by apache.
the class OOXMLPrettyPrint method pretty.
private static void pretty(Document document, OutputStream outputStream, int indent) throws TransformerException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
if (indent > 0) {
// set properties to indent the resulting XML nicely
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent));
}
Result result = new StreamResult(outputStream);
Source source = new DOMSource(document);
transformer.transform(source, result);
}
use of javax.xml.transform.Source in project poi by apache.
the class StreamHelper method saveXmlInStream.
/**
* Save the document object in the specified output stream.
*
* @param xmlContent
* The XML document.
* @param outStream
* The OutputStream in which the XML document will be written.
* @return <b>true</b> if the xml is successfully written in the stream,
* else <b>false</b>.
*/
public static boolean saveXmlInStream(Document xmlContent, OutputStream outStream) {
try {
Transformer trans = getIdentityTransformer();
Source xmlSource = new DOMSource(xmlContent);
// prevent close of stream by transformer:
Result outputTarget = new StreamResult(new FilterOutputStream(outStream) {
@Override
public void write(byte[] b, int off, int len) throws IOException {
out.write(b, off, len);
}
@Override
public void close() throws IOException {
// only flush, don't close!
out.flush();
}
});
trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.setOutputProperty(OutputKeys.STANDALONE, "yes");
trans.transform(xmlSource, outputTarget);
} catch (TransformerException e) {
return false;
}
return true;
}
use of javax.xml.transform.Source in project poi by apache.
the class PkiTestUtils method toString.
static String toString(Node dom) throws TransformerException {
Source source = new DOMSource(dom);
StringWriter stringWriter = new StringWriter();
Result result = new StreamResult(stringWriter);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
/*
* We have to omit the ?xml declaration if we want to embed the
* document.
*/
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(source, result);
return stringWriter.getBuffer().toString();
}
use of javax.xml.transform.Source in project sling by apache.
the class XmlUtil method prettyPrint.
public static String prettyPrint(InputSource is) {
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
// initialize StreamResult with File object to save to file
StreamResult result = new StreamResult(new StringWriter());
Source source = new SAXSource(is);
transformer.transform(source, result);
return result.getWriter().toString();
} catch (Exception e) {
// Catch generic error as panel should still work if xml apis are
// not
// resolved
log.warn("Error occurred while transforming xml", e);
} finally {
Util.close(is);
}
return "Source not found";
}
use of javax.xml.transform.Source in project wildfly by wildfly.
the class StandardConfigsXMLValidationUnitTestCase method setUp.
@BeforeClass
public static void setUp() {
final List<Source> sources = new LinkedList<Source>();
for (File file : jbossSchemaFiles(true)) {
sources.add(new StreamSource(file));
}
SCHEMAS = sources.toArray(new StreamSource[0]);
}
Aggregations