use of javax.xml.transform.dom.DOMSource in project buck by facebook.
the class BaseRunner method writeResult.
/**
* The test result file is written as XML to avoid introducing a dependency on JSON (see class
* overview).
*/
protected void writeResult(String testClassName, List<TestResult> results) throws IOException, ParserConfigurationException, TransformerException {
// XML writer logic taken from:
// http://www.genedavis.com/library/xml/java_dom_xml_creation.jsp
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = docBuilder.newDocument();
doc.setXmlVersion("1.1");
Element root = doc.createElement("testcase");
root.setAttribute("name", testClassName);
root.setAttribute("runner_capabilities", getRunnerCapabilities());
doc.appendChild(root);
for (TestResult result : results) {
Element test = doc.createElement("test");
// suite attribute
test.setAttribute("suite", result.testClassName);
// name attribute
test.setAttribute("name", result.testMethodName);
// success attribute
boolean isSuccess = result.isSuccess();
test.setAttribute("success", Boolean.toString(isSuccess));
// type attribute
test.setAttribute("type", result.type.toString());
// time attribute
long runTime = result.runTime;
test.setAttribute("time", String.valueOf(runTime));
// Include failure details, if appropriate.
Throwable failure = result.failure;
if (failure != null) {
String message = failure.getMessage();
test.setAttribute("message", message);
String stacktrace = stackTraceToString(failure);
test.setAttribute("stacktrace", stacktrace);
}
// stdout, if non-empty.
if (result.stdOut != null) {
Element stdOutEl = doc.createElement("stdout");
stdOutEl.appendChild(doc.createTextNode(result.stdOut));
test.appendChild(stdOutEl);
}
// stderr, if non-empty.
if (result.stdErr != null) {
Element stdErrEl = doc.createElement("stderr");
stdErrEl.appendChild(doc.createTextNode(result.stdErr));
test.appendChild(stdErrEl);
}
root.appendChild(test);
}
// Create an XML transformer that pretty-prints with a 2-space indent.
// The transformer factory uses a system property to find the class to use. We need to default
// to the system default since we have the user's classpath and they may not have everything set
// up for the XSLT transform to work.
String vendor = System.getProperty("java.vm.vendor");
String factoryClass;
if ("IBM Corporation".equals(vendor)) {
// Used in the IBM JDK --- from
// https://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.aix.80.doc/user/xml/using_xml.html
factoryClass = "com.ibm.xtq.xslt.jaxp.compiler.TransformerFactoryImpl";
} else {
// Used in the OpenJDK and the Oracle JDK.
factoryClass = "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl";
}
// When we get this far, we're exiting, so no need to reset the property.
System.setProperty("javax.xml.transform.TransformerFactory", factoryClass);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer trans = transformerFactory.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
// Write the result to a file.
String testSelectorSuffix = "";
if (!testSelectorList.isEmpty()) {
testSelectorSuffix += ".test_selectors";
}
if (isDryRun) {
testSelectorSuffix += ".dry_run";
}
OutputStream output;
if (outputDirectory != null) {
File outputFile = new File(outputDirectory, testClassName + testSelectorSuffix + ".xml");
output = new BufferedOutputStream(new FileOutputStream(outputFile));
} else {
output = System.out;
}
StreamResult streamResult = new StreamResult(output);
DOMSource source = new DOMSource(doc);
trans.transform(source, streamResult);
if (outputDirectory != null) {
output.close();
}
}
use of javax.xml.transform.dom.DOMSource in project buck by facebook.
the class BuckXmlTestRunListener method addMainTestResult.
/**
* Adds one more XML element to the test_result.xml tracking the
* result of the whole process.
*/
private void addMainTestResult() {
try {
File resultFile = getResultFile(mReportDir);
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(resultFile);
Node testsuite = doc.getElementsByTagName("testsuite").item(0);
if (mRunFailureMessage != null) {
Element failureNode = doc.createElement("failure");
failureNode.setTextContent(mRunFailureMessage);
testsuite.appendChild(failureNode);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(resultFile);
transformer.transform(source, result);
}
} catch (IOException | ParserConfigurationException | SAXException | TransformerException e) {
throw new RuntimeException(e);
}
}
use of javax.xml.transform.dom.DOMSource in project tinker by Tencent.
the class JavaXmlUtil method saveDocument.
/**
* save document
*
* @param document
* @param outputFullFilename
*/
public static void saveDocument(final Document document, final String outputFullFilename) {
OutputStream outputStream = null;
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, Constant.Encoding.UTF8);
outputStream = new FileOutputStream(outputFullFilename);
StreamResult result = new StreamResult(outputStream);
transformer.transform(domSource, result);
} catch (Exception e) {
throw new JavaXmlUtilException(e);
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (Exception e) {
throw new JavaXmlUtilException(e);
}
}
}
}
use of javax.xml.transform.dom.DOMSource in project Mycat-Server by MyCATApache.
the class FirewallConfig method updateToFile.
public static synchronized void updateToFile(String host, List<UserConfig> userConfigs) throws Exception {
LOGGER.debug("set white host:" + host + "user:" + userConfigs);
String filename = SystemConfig.getHomePath() + File.separator + "conf" + File.separator + "server.xml";
//String filename = "E:\\MyProject\\Mycat-Server\\src\\main\\resources\\server.xml";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
factory.setValidating(false);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(new IgnoreDTDEntityResolver());
Document xmldoc = builder.parse(filename);
Element whitehost = (Element) xmldoc.getElementsByTagName("whitehost").item(0);
Element firewall = (Element) xmldoc.getElementsByTagName("firewall").item(0);
if (firewall == null) {
firewall = xmldoc.createElement("firewall");
Element root = xmldoc.getDocumentElement();
root.appendChild(firewall);
if (whitehost == null) {
whitehost = xmldoc.createElement("whitehost");
firewall.appendChild(whitehost);
}
}
for (UserConfig userConfig : userConfigs) {
String user = userConfig.getName();
Element hostEle = xmldoc.createElement("host");
hostEle.setAttribute("host", host);
hostEle.setAttribute("user", user);
whitehost.appendChild(hostEle);
}
TransformerFactory factory2 = TransformerFactory.newInstance();
Transformer former = factory2.newTransformer();
String systemId = xmldoc.getDoctype().getSystemId();
if (systemId != null) {
former.setOutputProperty(javax.xml.transform.OutputKeys.DOCTYPE_SYSTEM, systemId);
}
former.transform(new DOMSource(xmldoc), new StreamResult(new File(filename)));
}
use of javax.xml.transform.dom.DOMSource in project jersey by jersey.
the class WadlBeanParamTest method nodeAsString.
private String nodeAsString(final Object resourceNode) throws TransformerException {
StringWriter writer = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource((Node) resourceNode), new StreamResult(writer));
return writer.toString();
}
Aggregations