use of javax.xml.transform.dom.DOMSource 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.dom.DOMSource 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.dom.DOMSource 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.dom.DOMSource in project midpoint by Evolveum.
the class DomToSchemaProcessor method parseSchema.
private XSSchemaSet parseSchema(Element schema) throws SchemaException {
// Make sure that the schema parser sees all the namespace declarations
DOMUtil.fixNamespaceDeclarations(schema);
XSSchemaSet xss = null;
try {
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(schema);
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamResult result = new StreamResult(out);
trans.transform(source, result);
XSOMParser parser = createSchemaParser();
InputSource inSource = new InputSource(new ByteArrayInputStream(out.toByteArray()));
// XXX: hack: it's here to make entity resolver work...
inSource.setSystemId("SystemId");
// XXX: end hack
inSource.setEncoding("utf-8");
parser.parse(inSource);
xss = parser.getResult();
} catch (SAXException e) {
throw new SchemaException("XML error during XSD schema parsing: " + e.getMessage() + "(embedded exception " + e.getException() + ") in " + shortDescription, e);
} catch (TransformerException e) {
throw new SchemaException("XML transformer error during XSD schema parsing: " + e.getMessage() + "(locator: " + e.getLocator() + ", embedded exception:" + e.getException() + ") in " + shortDescription, e);
} catch (RuntimeException e) {
// This sometimes happens, e.g. NPEs in Saxon
if (LOGGER.isErrorEnabled()) {
LOGGER.error("Unexpected error {} during parsing of schema:\n{}", e.getMessage(), DOMUtil.serializeDOMToString(schema));
}
throw new SchemaException("XML error during XSD schema parsing: " + e.getMessage() + " in " + shortDescription, e);
}
return xss;
}
use of javax.xml.transform.dom.DOMSource in project midpoint by Evolveum.
the class DOMUtil method printDom.
public static StringBuffer printDom(Node node, boolean indent, boolean omitXmlDeclaration) {
StringWriter writer = new StringWriter();
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans;
try {
trans = transfac.newTransformer();
} catch (TransformerConfigurationException e) {
throw new SystemException("Error in XML configuration: " + e.getMessage(), e);
}
trans.setOutputProperty(OutputKeys.INDENT, (indent ? "yes" : "no"));
// XALAN-specific
trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
trans.setParameter(OutputKeys.ENCODING, "utf-8");
// Note: serialized XML does not contain xml declaration
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, (omitXmlDeclaration ? "yes" : "no"));
DOMSource source = new DOMSource(node);
try {
trans.transform(source, new StreamResult(writer));
} catch (TransformerException e) {
throw new SystemException("Error in XML transformation: " + e.getMessage(), e);
}
return writer.getBuffer();
}
Aggregations