use of javax.xml.transform.dom.DOMSource in project groovy by apache.
the class XmlUtil method serialize.
/**
* Return a pretty String version of the Element.
*
* @param element the Element to serialize
* @return the pretty String representation of the Element
*/
public static String serialize(Element element) {
StringWriter sw = new StringWriter();
serialize(new DOMSource(element), sw);
return sw.toString();
}
use of javax.xml.transform.dom.DOMSource in project hadoop by apache.
the class Configuration method writeXml.
/**
* Write out the non-default properties in this configuration to the
* given {@link Writer}.
*
* <li>
* When property name is not empty and the property exists in the
* configuration, this method writes the property and its attributes
* to the {@link Writer}.
* </li>
* <p>
*
* <li>
* When property name is null or empty, this method writes all the
* configuration properties and their attributes to the {@link Writer}.
* </li>
* <p>
*
* <li>
* When property name is not empty but the property doesn't exist in
* the configuration, this method throws an {@link IllegalArgumentException}.
* </li>
* <p>
* @param out the writer to write to.
*/
public void writeXml(String propertyName, Writer out) throws IOException, IllegalArgumentException {
Document doc = asXmlDocument(propertyName);
try {
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(out);
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
// Important to not hold Configuration log while writing result, since
// 'out' may be an HDFS stream which needs to lock this configuration
// from another thread.
transformer.transform(source, result);
} catch (TransformerException te) {
throw new IOException(te);
}
}
use of javax.xml.transform.dom.DOMSource in project hadoop by apache.
the class TestQueueConfigurationParser method testQueueConfigurationParser.
/**
* test xml generation
* @throws ParserConfigurationException
* @throws Exception
*/
@Test(timeout = 5000)
public void testQueueConfigurationParser() throws ParserConfigurationException, Exception {
JobQueueInfo info = new JobQueueInfo("root", "rootInfo");
JobQueueInfo infoChild1 = new JobQueueInfo("child1", "child1Info");
JobQueueInfo infoChild2 = new JobQueueInfo("child2", "child1Info");
info.addChild(infoChild1);
info.addChild(infoChild2);
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
Document document = builder.newDocument();
// test QueueConfigurationParser.getQueueElement
Element e = QueueConfigurationParser.getQueueElement(document, info);
// transform result to string for check
DOMSource domSource = new DOMSource(e);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
String str = writer.toString();
assertTrue(str.endsWith("<queue><name>root</name><properties/><state>running</state><queue><name>child1</name><properties/><state>running</state></queue><queue><name>child2</name><properties/><state>running</state></queue></queue>"));
}
use of javax.xml.transform.dom.DOMSource in project hive by apache.
the class GenHiveTemplate method writeToFile.
private void writeToFile(File template, Document document) throws Exception {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(template);
transformer.transform(source, result);
}
use of javax.xml.transform.dom.DOMSource in project tomcat by apache.
the class DefaultServlet method secureXslt.
private Source secureXslt(InputStream is) {
// Need to filter out any external entities
Source result = null;
try {
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(secureEntityResolver);
Document document = builder.parse(is);
result = new DOMSource(document);
} catch (ParserConfigurationException | SAXException | IOException e) {
if (debug > 0) {
log(e.getMessage(), e);
}
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// Ignore
}
}
}
return result;
}
Aggregations