Search in sources :

Example 86 with Source

use of javax.xml.transform.Source in project cloudstack by apache.

the class PaloAltoResource method prettyFormat.

// pretty printing of xml strings
private String prettyFormat(String input) {
    int indent = 4;
    try {
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Throwable e) {
        try {
            Source xmlInput = new StreamSource(new StringReader(input));
            StringWriter stringWriter = new StringWriter();
            StreamResult xmlOutput = new StreamResult(stringWriter);
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.transform(xmlInput, xmlOutput);
            return xmlOutput.getWriter().toString();
        } catch (Throwable t) {
            return input;
        }
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource)

Example 87 with Source

use of javax.xml.transform.Source in project jackrabbit by apache.

the class ClientSession method exportDocumentView.

/**
     * Exports the XML document view of the specified repository location
     * to the given XML content handler. This method first requests the
     * raw XML data from the remote session, and then uses an identity
     * transformation to feed the data to the given XML content handler.
     * Possible IO and transformer exceptions are thrown as SAXExceptions.
     *
     * {@inheritDoc}
     */
public void exportDocumentView(String path, ContentHandler handler, boolean binaryAsLink, boolean noRecurse) throws SAXException, RepositoryException {
    try {
        byte[] xml = remote.exportDocumentView(path, binaryAsLink, noRecurse);
        Source source = new StreamSource(new ByteArrayInputStream(xml));
        Result result = new SAXResult(handler);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.transform(source, result);
    } catch (RemoteException ex) {
        throw new RemoteRepositoryException(ex);
    } catch (IOException ex) {
        throw new SAXException(ex);
    } catch (TransformerConfigurationException ex) {
        throw new SAXException(ex);
    } catch (TransformerException ex) {
        throw new SAXException(ex);
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) SAXException(org.xml.sax.SAXException) SAXResult(javax.xml.transform.sax.SAXResult) ByteArrayInputStream(java.io.ByteArrayInputStream) RemoteException(java.rmi.RemoteException) TransformerException(javax.xml.transform.TransformerException)

Example 88 with Source

use of javax.xml.transform.Source in project jackrabbit by apache.

the class AbstractImportXmlTest method serialize.

public void serialize(Document document) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
    try {
        // disable pretty printing/default line wrapping!
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        t.setOutputProperty(OutputKeys.INDENT, "no");
        Source s = new DOMSource(document);
        Result r = new StreamResult(bos);
        t.transform(s, r);
    } catch (TransformerException te) {
        throw (IOException) new IOException(te.getMessage()).initCause(te);
    } finally {
        bos.close();
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 89 with Source

use of javax.xml.transform.Source in project jena by apache.

the class DOM2Model method load.

/**
     * Parse a DOM Node with the RDF/XML parser, loading the triples into the
     * associated Model. Known not to work with Java 1.4.1.
     * 
     * @param document
     */
public void load(Node document) {
    Source input = new DOMSource(document);
    // Make a SAXResult object using this handler
    SAXResult output = new SAXResult(this);
    output.setLexicalHandler(this);
    // Run transform
    TransformerFactory xformFactory = TransformerFactory.newInstance();
    try {
        Transformer idTransform = xformFactory.newTransformer();
        idTransform.transform(input, output);
    } catch (FatalParsingErrorException e) {
        // Old code ignored this,
        // given difficult bug report, don't be silent.
        logger.error("Unexpected exception in DOM2Model", e);
    } catch (RuntimeException rte) {
        throw rte;
    } catch (Exception nrte) {
        throw new JenaException(nrte);
    } finally {
        close();
    }
}
Also used : JenaException(org.apache.jena.shared.JenaException) DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) SAXResult(javax.xml.transform.sax.SAXResult) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) SAXParseException(org.xml.sax.SAXParseException) JenaException(org.apache.jena.shared.JenaException)

Example 90 with Source

use of javax.xml.transform.Source in project logging-log4j2 by apache.

the class XmlCompactFileAsyncAppenderValidationTest method validateXmlSchema.

private void validateXmlSchema(final File file) throws SAXException, IOException {
    final URL schemaFile = this.getClass().getClassLoader().getResource("Log4j-events.xsd");
    final Source xmlFile = new StreamSource(file);
    final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    final Schema schema = schemaFactory.newSchema(schemaFile);
    final Validator validator = schema.newValidator();
    validator.validate(xmlFile);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator)

Aggregations

Source (javax.xml.transform.Source)238 StreamSource (javax.xml.transform.stream.StreamSource)161 DOMSource (javax.xml.transform.dom.DOMSource)108 Transformer (javax.xml.transform.Transformer)76 StreamResult (javax.xml.transform.stream.StreamResult)74 InputSource (org.xml.sax.InputSource)67 SAXSource (javax.xml.transform.sax.SAXSource)56 StringReader (java.io.StringReader)52 IOException (java.io.IOException)46 TransformerException (javax.xml.transform.TransformerException)45 Result (javax.xml.transform.Result)42 TransformerFactory (javax.xml.transform.TransformerFactory)41 Test (org.junit.Test)39 StringWriter (java.io.StringWriter)35 InputStream (java.io.InputStream)32 SAXException (org.xml.sax.SAXException)32 Schema (javax.xml.validation.Schema)29 Validator (javax.xml.validation.Validator)29 SchemaFactory (javax.xml.validation.SchemaFactory)28 ByteArrayInputStream (java.io.ByteArrayInputStream)26