Search in sources :

Example 66 with DOMSource

use of javax.xml.transform.dom.DOMSource 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 DOMSource

use of javax.xml.transform.dom.DOMSource 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 68 with DOMSource

use of javax.xml.transform.dom.DOMSource 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 69 with DOMSource

use of javax.xml.transform.dom.DOMSource 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)

Example 70 with DOMSource

use of javax.xml.transform.dom.DOMSource in project gephi by gephi.

the class LayoutPresetPersistence method savePreset.

public void savePreset(String name, Layout layout) {
    Preset preset = addPreset(new Preset(name, layout));
    FileOutputStream fos = null;
    try {
        //Create file if dont exist
        FileObject folder = FileUtil.getConfigFile("layoutpresets");
        if (folder == null) {
            folder = FileUtil.getConfigRoot().createFolder("layoutpresets");
        }
        File presetFile = new File(FileUtil.toFile(folder), name + ".xml");
        //Create doc
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = factory.newDocumentBuilder();
        final Document document = documentBuilder.newDocument();
        document.setXmlVersion("1.0");
        document.setXmlStandalone(true);
        //Write doc
        preset.writeXML(document);
        //Write XML file
        fos = new FileOutputStream(presetFile);
        Source source = new DOMSource(document);
        Result result = new StreamResult(fos);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.transform(source, result);
    } catch (Exception e) {
        Logger.getLogger("").log(Level.SEVERE, "Error while writing preset file", e);
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException ex) {
            }
        }
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) IOException(java.io.IOException) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) IOException(java.io.IOException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileOutputStream(java.io.FileOutputStream) FileObject(org.openide.filesystems.FileObject) File(java.io.File)

Aggregations

DOMSource (javax.xml.transform.dom.DOMSource)392 StreamResult (javax.xml.transform.stream.StreamResult)231 Transformer (javax.xml.transform.Transformer)204 Document (org.w3c.dom.Document)161 TransformerFactory (javax.xml.transform.TransformerFactory)112 TransformerException (javax.xml.transform.TransformerException)107 DocumentBuilder (javax.xml.parsers.DocumentBuilder)102 StringWriter (java.io.StringWriter)97 IOException (java.io.IOException)93 Element (org.w3c.dom.Element)81 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)77 Source (javax.xml.transform.Source)67 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)62 SAXException (org.xml.sax.SAXException)56 File (java.io.File)55 InputSource (org.xml.sax.InputSource)50 StreamSource (javax.xml.transform.stream.StreamSource)47 Node (org.w3c.dom.Node)45 InputStream (java.io.InputStream)35 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)35