use of javax.xml.transform.Transformer 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.Transformer 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.Transformer 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.Transformer in project azure-tools-for-java by Microsoft.
the class ParseXML method saveXMLDocument.
/**
* Save XML file and saves XML document.
*
* @param fileName
* @param doc
* @return boolean
* @throws Exception object
*/
static boolean saveXMLDocument(String fileName, Document doc) throws Exception {
// open output stream where XML Document will be saved
File xmlOutputFile = new File(fileName);
FileOutputStream fos = null;
Transformer transformer;
try {
fos = new FileOutputStream(xmlOutputFile);
// Use a Transformer for output
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(fos);
// transform source into result will do save
transformer.transform(source, result);
} finally {
if (fos != null) {
fos.close();
}
}
return true;
}
use of javax.xml.transform.Transformer 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