Search in sources :

Example 66 with StreamResult

use of javax.xml.transform.stream.StreamResult in project cas by apereo.

the class SamlUtils method transformSamlObject.

/**
     * Transform saml object to String.
     *
     * @param configBean the config bean
     * @param samlObject the saml object
     * @return the string
     * @throws SamlException the saml exception
     */
public static StringWriter transformSamlObject(final OpenSamlConfigBean configBean, final XMLObject samlObject) throws SamlException {
    final StringWriter writer = new StringWriter();
    try {
        final Marshaller marshaller = configBean.getMarshallerFactory().getMarshaller(samlObject.getElementQName());
        if (marshaller != null) {
            final Element element = marshaller.marshall(samlObject);
            final DOMSource domSource = new DOMSource(element);
            final StreamResult result = new StreamResult(writer);
            final TransformerFactory tf = TransformerFactory.newInstance();
            final Transformer transformer = tf.newTransformer();
            transformer.transform(domSource, result);
        }
    } catch (final Exception e) {
        throw new SamlException(e.getMessage(), e);
    }
    return writer;
}
Also used : Marshaller(org.opensaml.core.xml.io.Marshaller) 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) Element(org.w3c.dom.Element)

Example 67 with StreamResult

use of javax.xml.transform.stream.StreamResult in project asciidoctor-fopub by asciidoctor.

the class InputHandler method transformTo.

/**
     * In contrast to render(Fop) this method only performs the XSLT stage and saves the
     * intermediate XSL-FO file to the output file.
     * @param out OutputStream to write the transformation result to.
     * @throws FOPException in case of an error during processing
     */
public void transformTo(OutputStream out) throws FOPException {
    Result res = new StreamResult(out);
    transformTo(res);
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult)

Example 68 with StreamResult

use of javax.xml.transform.stream.StreamResult in project cas by apereo.

the class DefaultRelyingPartyTokenProducer method serializeRelyingPartyToken.

private String serializeRelyingPartyToken(final Element rpToken) {
    try {
        final StringWriter sw = new StringWriter();
        final Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, BooleanUtils.toStringYesNo(Boolean.TRUE));
        t.transform(new DOMSource(rpToken), new StreamResult(sw));
        return sw.toString();
    } catch (final TransformerException e) {
        throw Throwables.propagate(e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) TransformerException(javax.xml.transform.TransformerException)

Example 69 with StreamResult

use of javax.xml.transform.stream.StreamResult in project bazel by bazelbuild.

the class ManifestMergerAction method removePermissions.

private static Path removePermissions(Path manifest, Path outputDir) throws IOException, ParserConfigurationException, TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError, SAXException {
    DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = docBuilder.parse(manifest.toFile());
    for (String tag : PERMISSION_TAGS) {
        NodeList permissions = doc.getElementsByTagName(tag);
        if (permissions != null) {
            for (int i = permissions.getLength() - 1; i >= 0; i--) {
                Node permission = permissions.item(i);
                permission.getParentNode().removeChild(permission);
            }
        }
    }
    // Write resulting manifest to the output directory, maintaining full path to prevent collisions
    Path output = outputDir.resolve(manifest.toString().replaceFirst("^/", ""));
    Files.createDirectories(output.getParent());
    TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), new StreamResult(output.toFile()));
    return output;
}
Also used : Path(java.nio.file.Path) DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document)

Example 70 with StreamResult

use of javax.xml.transform.stream.StreamResult in project gwt-test-utils by gwt-test-utils.

the class XMLParserTest method convertXMLFileToString.

private String convertXMLFileToString(String fileName) {
    try {
        InputStream inputStream = this.getClass().getResourceAsStream(fileName);
        org.w3c.dom.Document doc = XmlUtils.newDocumentBuilder().parse(inputStream);
        StringWriter stw = new StringWriter();
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.transform(new DOMSource(doc), new StreamResult(stw));
        return stw.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) InputStream(java.io.InputStream)

Aggregations

StreamResult (javax.xml.transform.stream.StreamResult)448 Transformer (javax.xml.transform.Transformer)267 DOMSource (javax.xml.transform.dom.DOMSource)234 StringWriter (java.io.StringWriter)206 TransformerFactory (javax.xml.transform.TransformerFactory)138 TransformerException (javax.xml.transform.TransformerException)125 Document (org.w3c.dom.Document)103 IOException (java.io.IOException)94 StreamSource (javax.xml.transform.stream.StreamSource)88 Source (javax.xml.transform.Source)74 Test (org.junit.Test)73 DocumentBuilder (javax.xml.parsers.DocumentBuilder)65 ByteArrayOutputStream (java.io.ByteArrayOutputStream)64 Element (org.w3c.dom.Element)59 File (java.io.File)58 Result (javax.xml.transform.Result)57 StringReader (java.io.StringReader)56 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)53 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)50 ByteArrayInputStream (java.io.ByteArrayInputStream)44