de.sekmi.li2b2.hive.DOMUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of li2b2-xml Show documentation
Show all versions of li2b2-xml Show documentation
Message structures and code for parsing.
Used by client and server.
package de.sekmi.li2b2.hive;
import java.io.OutputStream;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class DOMUtils {
public static void stripWhitespace(Element node) throws XPathExpressionException{
XPathFactory xf = XPathFactory.newInstance();
// XPath to find empty text nodes.
XPathExpression xe = xf.newXPath().compile("//text()[normalize-space(.) = '']");
NodeList nl = (NodeList)xe.evaluate(node, XPathConstants.NODESET);
// Remove each empty text node from document.
for (int i = 0; i < nl.getLength(); i++) {
Node empty = nl.item(i);
empty.getParentNode().removeChild(empty);
}
}
public static void printDOM(Node node, OutputStream out){
try {
printDOM(node, out, "UTF-8");
} catch (TransformerException e) {
throw new RuntimeException(e);
}
}
public static void printDOM(HiveMessage message, OutputStream out){
printDOM(message.dom, out);
}
public static void printDOM(Node node, OutputStream out, String encoding) throws TransformerException{
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer;
transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.transform(new DOMSource(node),
new StreamResult(out));
}
}