use of javax.xml.transform.stream.StreamResult in project jdk8u_jdk by JetBrains.
the class XPathNegativeZero method xform.
private static String xform(final String xml, final String xsl) throws Exception {
final TransformerFactory tf = TransformerFactory.newInstance();
final Source xslsrc = new StreamSource(new File(xsl));
final Templates tmpl = tf.newTemplates(xslsrc);
final Transformer t = tmpl.newTransformer();
StringWriter writer = new StringWriter();
final Source src = new StreamSource(new File(xml));
final Result res = new StreamResult(writer);
t.transform(src, res);
return writer.toString();
}
use of javax.xml.transform.stream.StreamResult in project jdk8u_jdk by JetBrains.
the class Test method toString.
private static String toString(Source response) throws TransformerException, IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(response, new StreamResult(bos));
bos.close();
return new String(bos.toByteArray());
}
use of javax.xml.transform.stream.StreamResult 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.stream.StreamResult 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();
}
use of javax.xml.transform.stream.StreamResult in project azure-tools-for-java by Microsoft.
the class ParserXMLUtility method saveXMLFile.
/**
* save XML file and saves XML document.
*
* @param fileName
* @param doc
* @return XML document or <B>null</B> if error occurred
* @throws IOException
* @throws Exception
*/
public static boolean saveXMLFile(String fileName, Document doc) throws Exception {
File xmlFile = null;
FileOutputStream fos = null;
Transformer transformer;
try {
xmlFile = new File(fileName);
fos = new FileOutputStream(xmlFile);
TransformerFactory transFactory = TransformerFactory.newInstance();
transformer = transFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult destination = new StreamResult(fos);
// transform source into result will do save
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(source, destination);
} finally {
if (fos != null) {
fos.close();
}
}
return true;
}
Aggregations