Search in sources :

Example 86 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project geode by apache.

the class Installer method streamXML.

private void streamXML(final Document doc, final OutputStream out) {
    try {
        // Use a Transformer for output
        final TransformerFactory tFactory = TransformerFactory.newInstance();
        final Transformer transformer = tFactory.newTransformer();
        if (doc.getDoctype() != null) {
            final String systemId = doc.getDoctype().getSystemId();
            final String publicId = doc.getDoctype().getPublicId();
            transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, publicId);
            transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemId);
        }
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        final DOMSource source = new DOMSource(doc);
        final StreamResult result = new StreamResult(out);
        transformer.transform(source, result);
    } catch (final Exception e) {
        e.printStackTrace();
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) UsageException(org.apache.geode.modules.session.installer.args.UsageException)

Example 87 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project spring-framework by spring-projects.

the class StaxSourceTests method setUp.

@Before
public void setUp() throws Exception {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformer = transformerFactory.newTransformer();
    inputFactory = XMLInputFactory.newInstance();
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    documentBuilder = documentBuilderFactory.newDocumentBuilder();
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Before(org.junit.Before)

Example 88 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project amos-ss17-alexa by c-i-ber.

the class XMLParser method xmlToString.

public static String xmlToString(Document doc) {
    try {
        StringWriter sw = new StringWriter();
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.transform(new DOMSource(doc), new StreamResult(sw));
        return sw.toString();
    } catch (Exception ex) {
        throw new RuntimeException("Error converting to String", ex);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) XPathExpressionException(javax.xml.xpath.XPathExpressionException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 89 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project midpoint by Evolveum.

the class CsvAccountTests method updateCsvFilePath.

/**
     * Update icfccsvfile:filePath tag value, set the correct
     * path to midpoint-flatfile.csv file
     */
public void updateCsvFilePath() {
    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(CSV_RESOURCE_XML_PATH);
        // Get the CSV filePath element by tag name directly
        Node filePathNode = doc.getElementsByTagName("icfccsvfile:filePath").item(0);
        filePathNode.setTextContent(System.getProperty("user.dir") + "/src/test/resources/mp-resources/midpoint-flatfile.csv");
        // write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(CSV_RESOURCE_XML_PATH));
        transformer.transform(source, result);
    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
    } catch (TransformerException tfe) {
        tfe.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (SAXException sae) {
        sae.printStackTrace();
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 90 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project uPortal by Jasig.

the class ILFBuilder method printNodeToDebug.

private static void printNodeToDebug(Node n, String name) throws TransformerFactoryConfigurationError {
    if (!LOG.isDebugEnabled()) {
        return;
    }
    final StringWriter writer = new StringWriter();
    try {
        final TransformerFactory transFactory = TransformerFactory.newInstance();
        final Transformer trans = transFactory.newTransformer();
        final Source xmlSource = new DOMSource(n);
        final Result transResult = new StreamResult(writer);
        trans.transform(xmlSource, transResult);
        final String xmlStr = writer.toString();
        LOG.debug(name + " DOM Tree:\n\n" + xmlStr);
    } catch (Exception e) {
        LOG.error("Error printing out " + name + " DOM Tree", e);
        final String xmlStr = writer.toString();
        LOG.debug("Partial " + name + " DOM Tree:\n\n" + xmlStr);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) AuthorizationException(org.apereo.portal.AuthorizationException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Aggregations

TransformerFactory (javax.xml.transform.TransformerFactory)257 Transformer (javax.xml.transform.Transformer)221 StreamResult (javax.xml.transform.stream.StreamResult)198 DOMSource (javax.xml.transform.dom.DOMSource)157 TransformerException (javax.xml.transform.TransformerException)86 StringWriter (java.io.StringWriter)77 StreamSource (javax.xml.transform.stream.StreamSource)77 Document (org.w3c.dom.Document)67 Source (javax.xml.transform.Source)56 IOException (java.io.IOException)55 File (java.io.File)47 DocumentBuilder (javax.xml.parsers.DocumentBuilder)43 ByteArrayOutputStream (java.io.ByteArrayOutputStream)37 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)37 Element (org.w3c.dom.Element)36 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)35 Result (javax.xml.transform.Result)32 ByteArrayInputStream (java.io.ByteArrayInputStream)29 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)29 StringReader (java.io.StringReader)28