All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.sekmi.li2b2.services.XMLUtils Maven / Gradle / Ivy

Go to download

This project emulates the core components of an i2b2 server backend. Basic functionality of PM, CRC, ONT and WORK cells allows the official i2b2 webclient to connect ot this emulated server.

There is a newer version: 0.8
Show newest version
package de.sekmi.li2b2.services;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.StringWriter;

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 org.w3c.dom.Node;

public class XMLUtils {
	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, encoding);
		transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
		
		transformer.transform(new DOMSource(node), 
				new StreamResult(out));
	}

	public static void printDOM(Node node, OutputStream out){
		try {
		printDOM(node, out, "UTF-8");
		} catch (TransformerException e) {
			throw new RuntimeException(e);
		}
	}
	public static byte[] formatDOM(Node node, String encoding){
		ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
		printDOM(node, out);
		return out.toByteArray();
	}
	public static String formatDOM(Node node) throws TransformerException {
		StringWriter writer = new StringWriter();
		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");
		// encoding doesn't matter since we stick with strings
		transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
		
		transformer.transform(new DOMSource(node), new StreamResult(writer));
		return writer.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy