use of org.w3c.dom.ls.LSOutput in project camel by apache.
the class XmlSignatureHelper method transformNonTextNodeToOutputStream.
/**
* Serializes a node using a certain character encoding.
*
* @param node
* DOM node to serialize
* @param os
* output stream, to which the node is serialized
* @param omitXmlDeclaration
* indicator whether to omit the XML declaration or not
* @param encoding
* character encoding, can be <code>null</code>, if
* <code>null</code> then "UTF-8" is used
* @throws Exception
*/
public static void transformNonTextNodeToOutputStream(Node node, OutputStream os, boolean omitXmlDeclaration, String encoding) throws Exception {
// therefore we switched to DOMImplementationLS
if (encoding == null) {
encoding = "UTF-8";
}
DOMImplementationRegistry domImplementationRegistry = DOMImplementationRegistry.newInstance();
DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementationRegistry.getDOMImplementation("LS");
LSOutput lsOutput = domImplementationLS.createLSOutput();
lsOutput.setEncoding(encoding);
lsOutput.setByteStream(os);
LSSerializer lss = domImplementationLS.createLSSerializer();
lss.getDomConfig().setParameter("xml-declaration", !omitXmlDeclaration);
lss.write(node, lsOutput);
}
use of org.w3c.dom.ls.LSOutput in project android by JetBrains.
the class ThemePreviewBuilder method printDebug.
private static void printDebug(@NotNull PrintStream out, @NotNull Document document) {
try {
DOMImplementationRegistry reg = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl = (DOMImplementationLS) reg.getDOMImplementation("LS");
LSSerializer serializer = impl.createLSSerializer();
LSOutput lsOutput = impl.createLSOutput();
lsOutput.setEncoding("UTF-8");
lsOutput.setByteStream(out);
serializer.write(document, lsOutput);
} catch (ClassNotFoundException e) {
e.printStackTrace(out);
} catch (InstantiationException e) {
e.printStackTrace(out);
} catch (IllegalAccessException e) {
e.printStackTrace(out);
}
}
use of org.w3c.dom.ls.LSOutput in project openhab1-addons by openhab.
the class Helper method documentToString.
/***
* Helper method which converts XML Document into pretty formatted string
*
* @param doc to convert
* @return converted XML as String
*/
public static String documentToString(Document doc) {
String strMsg = "";
try {
DOMImplementation domImpl = doc.getImplementation();
DOMImplementationLS domImplLS = (DOMImplementationLS) domImpl.getFeature("LS", "3.0");
LSSerializer lsSerializer = domImplLS.createLSSerializer();
lsSerializer.getDomConfig().setParameter("format-pretty-print", true);
Writer stringWriter = new StringWriter();
LSOutput lsOutput = domImplLS.createLSOutput();
lsOutput.setEncoding("UTF-8");
lsOutput.setCharacterStream(stringWriter);
lsSerializer.write(doc, lsOutput);
strMsg = stringWriter.toString();
} catch (Exception e) {
logger.warn("Error occured when converting document to string", e);
}
return strMsg;
}
use of org.w3c.dom.ls.LSOutput in project tomee by apache.
the class XmlFormatter method format.
public static String format(final String in) {
try {
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
final DocumentBuilder db = dbf.newDocumentBuilder();
final InputSource is = new InputSource(new StringReader(in));
final Document document = db.parse(is);
final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
final DOMImplementationLS impl = DOMImplementationLS.class.cast(registry.getDOMImplementation("XML 3.0 LS 3.0"));
if (impl == null) {
return in;
}
final LSSerializer serializer = impl.createLSSerializer();
if (serializer.getDomConfig().canSetParameter("format-pretty-print", Boolean.TRUE)) {
serializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
final LSOutput lsOutput = impl.createLSOutput();
lsOutput.setEncoding("UTF-8");
final StringWriter stringWriter = new StringWriter();
lsOutput.setCharacterStream(stringWriter);
serializer.write(document, lsOutput);
return stringWriter.toString().replace("\"UTF-8\"?><", "\"UTF-8\"?>\n<");
}
return in;
} catch (final Throwable t) {
// just to be more sexy so ignore it and use ugly xml
return in;
}
}
use of org.w3c.dom.ls.LSOutput in project ddf by codice.
the class XPathHelper method print.
/**
* Prints a given node as a String
*
* @param n
* - the node to print as a String
* @param encoding
* - the character encoding to use for the returned String
* @return the Node as a String, null if an exception is thrown or null is passed in.
*/
public static String print(Node n, String encoding) {
if (n == null) {
return null;
}
try {
Document document = null;
if (n instanceof Document) {
document = (Document) n;
} else {
document = n.getOwnerDocument();
}
StringWriter stringOut = new StringWriter();
DOMImplementationLS domImpl = (DOMImplementationLS) document.getImplementation();
LSSerializer serializer = domImpl.createLSSerializer();
LSOutput lsOut = domImpl.createLSOutput();
lsOut.setEncoding(encoding);
lsOut.setCharacterStream(stringOut);
serializer.write(n, lsOut);
return stringOut.toString();
} catch (DOMException | LSException e) {
LOGGER.debug(e.getMessage(), e);
}
return null;
}
Aggregations