Java tutorial
/* (c) 2018 Open Source Geospatial Foundation - all rights reserved * This code is licensed under the GPL 2.0 license, available at the root * application directory. */ package org.geoserver.test.onlineTest; import java.io.IOException; import java.io.StringWriter; import java.nio.charset.StandardCharsets; 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.apache.commons.io.IOUtils; import org.w3c.dom.Document; /** Tests Resources Utils */ public final class Resources { public static final String TEST_DATA_DIR = "test-data"; public static String resourceToString(String path) { String result = null; try { result = IOUtils.toString(Resources.class.getClassLoader().getResourceAsStream(path), StandardCharsets.UTF_8); } catch (IOException e) { throw new RuntimeException(e); } return result; } public static String xmlDocToString(Document document) { String output = ""; try { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(writer)); output = writer.getBuffer().toString(); } catch (TransformerException e) { throw new RuntimeException(e); } return output; } private Resources() { } }