use of javax.xml.transform.stream.StreamResult in project graphhopper by graphhopper.
the class GraphHopperServlet method errorsToXML.
protected String errorsToXML(Collection<Throwable> list) {
if (list.isEmpty())
throw new RuntimeException("errorsToXML should not be called with an empty list");
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
Element gpxElement = doc.createElement("gpx");
gpxElement.setAttribute("creator", "GraphHopper");
gpxElement.setAttribute("version", "1.1");
doc.appendChild(gpxElement);
Element mdElement = doc.createElement("metadata");
gpxElement.appendChild(mdElement);
Element extensionsElement = doc.createElement("extensions");
mdElement.appendChild(extensionsElement);
Element messageElement = doc.createElement("message");
extensionsElement.appendChild(messageElement);
messageElement.setTextContent(list.iterator().next().getMessage());
Element hintsElement = doc.createElement("hints");
extensionsElement.appendChild(hintsElement);
for (Throwable t : list) {
Element error = doc.createElement("error");
hintsElement.appendChild(error);
error.setAttribute("message", t.getMessage());
error.setAttribute("details", t.getClass().getName());
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
return writer.toString();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
use of javax.xml.transform.stream.StreamResult in project zaproxy by zaproxy.
the class ReportGenerator method XMLToHtml.
public static File XMLToHtml(Document xmlDocument, String infilexsl, File outFile) {
File stylesheet = null;
outFile = new File(outFile.getAbsolutePath());
try {
stylesheet = new File(infilexsl);
DOMSource source = new DOMSource(xmlDocument);
// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
StreamSource stylesource = new StreamSource(stylesheet);
Transformer transformer = tFactory.newTransformer(stylesource);
// Make the transformation and write to the output file
StreamResult result = new StreamResult(outFile);
transformer.transform(source, result);
} catch (TransformerException e) {
logger.error(e.getMessage(), e);
}
return outFile;
}
use of javax.xml.transform.stream.StreamResult in project zaproxy by zaproxy.
the class ReportGenerator method getDebugXMLString.
public static String getDebugXMLString(Document doc) throws TransformerException {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
return writer.getBuffer().toString().replaceAll("\n|\r", "");
}
use of javax.xml.transform.stream.StreamResult in project zaproxy by zaproxy.
the class FileXML method saveFile.
public void saveFile(String fileName) {
// DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//factory.setNamespaceAware(true);
//factory.setValidating(true);
File file = null;
FileOutputStream outFile = null;
try {
file = new File(fileName);
outFile = new FileOutputStream(file);
// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(outFile);
//StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
} catch (TransformerException | IOException e) {
logger.error(e.getMessage(), e);
} finally {
if (outFile != null) {
try {
outFile.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
}
use of javax.xml.transform.stream.StreamResult in project crawler4j by yasserg.
the class BinaryParseData method getTransformerHandler.
/**
* Returns a transformer handler that serializes incoming SAX events to
* XHTML or HTML (depending the given method) using the given output encoding.
*
* @param encoding output encoding, or <code>null</code> for the platform default
*/
private static TransformerHandler getTransformerHandler(OutputStream out, String method, String encoding) throws TransformerConfigurationException {
TransformerHandler transformerHandler = SAX_TRANSFORMER_FACTORY.newTransformerHandler();
Transformer transformer = transformerHandler.getTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, method);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
if (encoding != null) {
transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
}
transformerHandler.setResult(new StreamResult(new PrintStream(out)));
return transformerHandler;
}
Aggregations