use of org.w3c.dom.ls.DOMImplementationLS in project wildfly by wildfly.
the class UsernameTokenCallbackHandler method toString.
private String toString(Node node) {
String str = null;
if (node != null) {
DOMImplementationLS lsImpl = (DOMImplementationLS) node.getOwnerDocument().getImplementation().getFeature("LS", "3.0");
LSSerializer serializer = lsImpl.createLSSerializer();
// by default its true, so set it to false to get String without xml-declaration
serializer.getDomConfig().setParameter("xml-declaration", false);
str = serializer.writeToString(node);
}
return str;
}
use of org.w3c.dom.ls.DOMImplementationLS 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.DOMImplementationLS 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 lsSerializer = domImpl.createLSSerializer();
lsSerializer.getDomConfig().setParameter(XML_DECLARATION, false);
LSOutput lsout = domImpl.createLSOutput();
lsout.setEncoding(encoding);
lsout.setCharacterStream(stringOut);
/*DDF-4382 Serializer doesn't serialize attribute nodes - attribute node's child node is a
NODE.TEXT_NODE version with the same information. If Attribute node, get the child
NODE.TEXT_NODE version and then use that node for serialization*/
if (n.getNodeType() == Node.ATTRIBUTE_NODE && n.hasChildNodes()) {
n = n.getFirstChild();
}
lsSerializer.write(n, lsout);
return stringOut.toString();
} catch (DOMException | LSException e) {
LOGGER.debug(e.getMessage(), e);
}
return null;
}
use of org.w3c.dom.ls.DOMImplementationLS in project Payara by payara.
the class CombinedXPath method writeXML.
private static synchronized void writeXML(final Node node, final Writer writer) {
try {
if (lsSerializer == null) {
final DOMImplementation domImpl = DOMImplementationRegistry.newInstance().getDOMImplementation("");
final DOMImplementationLS domLS = (DOMImplementationLS) domImpl.getFeature("LS", "3.0");
lsOutput = domLS.createLSOutput();
lsOutput.setEncoding("UTF-8");
lsSerializer = domLS.createLSSerializer();
}
lsOutput.setCharacterStream(writer);
lsSerializer.write(node, lsOutput);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
use of org.w3c.dom.ls.DOMImplementationLS in project iaf by ibissource.
the class Utils method dom2String2.
public static String dom2String2(Document document) {
DOMImplementationRegistry registry;
try {
registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS domImplLS = (DOMImplementationLS) registry.getDOMImplementation("LS");
// Create a serializer for the DOM
LSSerializer ser = domImplLS.createLSSerializer();
LSOutput out = domImplLS.createLSOutput();
// Writer will be a String
StringWriter stringOut = new StringWriter();
out.setCharacterStream(stringOut);
// Serialize the DOM
ser.write(document, out);
System.out.println("STRXML = " + // Spit out the DOM as a String
stringOut.toString());
return stringOut.toString();
} catch (Exception e) {
System.err.println("Cannot create registry: " + e.getMessage());
}
return null;
}
Aggregations