Search in sources :

Example 86 with Transformer

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());
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 87 with Transformer

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;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) XSOMParser(com.sun.xml.xsom.parser.XSOMParser) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SAXException(org.xml.sax.SAXException) ByteArrayInputStream(java.io.ByteArrayInputStream) TransformerException(javax.xml.transform.TransformerException)

Example 88 with Transformer

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();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SystemException(com.evolveum.midpoint.util.exception.SystemException) StreamResult(javax.xml.transform.stream.StreamResult) TransformerException(javax.xml.transform.TransformerException)

Example 89 with Transformer

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;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) TransformerFactory(javax.xml.transform.TransformerFactory) StreamResult(javax.xml.transform.stream.StreamResult) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 90 with Transformer

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;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) TransformerFactory(javax.xml.transform.TransformerFactory) StreamResult(javax.xml.transform.stream.StreamResult) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Aggregations

Transformer (javax.xml.transform.Transformer)449 StreamResult (javax.xml.transform.stream.StreamResult)354 DOMSource (javax.xml.transform.dom.DOMSource)272 TransformerFactory (javax.xml.transform.TransformerFactory)222 TransformerException (javax.xml.transform.TransformerException)175 StringWriter (java.io.StringWriter)153 Document (org.w3c.dom.Document)114 IOException (java.io.IOException)105 StreamSource (javax.xml.transform.stream.StreamSource)98 Source (javax.xml.transform.Source)97 DocumentBuilder (javax.xml.parsers.DocumentBuilder)70 File (java.io.File)66 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)64 SAXException (org.xml.sax.SAXException)62 Element (org.w3c.dom.Element)59 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)57 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)57 ByteArrayOutputStream (java.io.ByteArrayOutputStream)53 StringReader (java.io.StringReader)52 Result (javax.xml.transform.Result)52