use of javax.xml.transform.TransformerFactory in project camel by apache.
the class XmlFixture method transform.
protected static Document transform(Document aDocument, String aResourcePath) throws Exception {
TransformerFactory tf = TransformerFactory.newInstance();
InputStream in = XmlFixture.class.getResourceAsStream(aResourcePath);
Source src = new StreamSource(in);
src.setSystemId(XmlFixture.class.getResource(aResourcePath).toExternalForm());
Transformer t = tf.newTransformer(src);
DOMResult result = new DOMResult();
t.transform(new DOMSource(aDocument), result);
return (Document) result.getNode();
}
use of javax.xml.transform.TransformerFactory 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.TransformerFactory 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.TransformerFactory in project cucumber-jvm by cucumber.
the class JUnitFormatter method done.
@Override
public void done() {
try {
// set up a transformer
rootElement.setAttribute("name", JUnitFormatter.class.getName());
rootElement.setAttribute("failures", String.valueOf(rootElement.getElementsByTagName("failure").getLength()));
rootElement.setAttribute("skipped", String.valueOf(rootElement.getElementsByTagName("skipped").getLength()));
rootElement.setAttribute("time", sumTimes(rootElement.getElementsByTagName("testcase")));
if (rootElement.getElementsByTagName("testcase").getLength() == 0) {
// to avoid failed Jenkins jobs
addDummyTestCase();
}
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.INDENT, "yes");
StreamResult result = new StreamResult(out);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
} catch (TransformerException e) {
throw new CucumberException("Error while transforming.", e);
}
}
use of javax.xml.transform.TransformerFactory in project che by eclipse.
the class BuildFileGenerator method documentToString.
/** Convert document to formatted XML string. */
private String documentToString(Document doc) throws TransformerException {
StringWriter writer = new StringWriter();
Source source = new DOMSource(doc);
Result result = new StreamResult(writer);
TransformerFactory factory = TransformerFactory.newInstance();
factory.setAttribute("indent-number", "4");
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
return writer.toString();
}
Aggregations