Search in sources :

Example 56 with DOMSource

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

the class SourceHttpMessageConverterTests method readDOMSourceExternal.

@Test
public void readDOMSourceExternal() throws Exception {
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes("UTF-8"));
    inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
    converter.setSupportDtd(true);
    DOMSource result = (DOMSource) converter.read(DOMSource.class, inputMessage);
    Document document = (Document) result.getNode();
    assertEquals("Invalid result", "root", document.getDocumentElement().getLocalName());
    assertNotEquals("Invalid result", "Foo Bar", document.getDocumentElement().getTextContent());
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) DOMSource(javax.xml.transform.dom.DOMSource) MediaType(org.springframework.http.MediaType) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 57 with DOMSource

use of javax.xml.transform.dom.DOMSource in project camel by apache.

the class SaxonConverterTest method convertToDOMSource.

@Test
public void convertToDOMSource() throws XPathException {
    DOMSource source = context.getTypeConverter().convertTo(DOMSource.class, exchange, doc);
    assertNotNull(source);
    String string = context.getTypeConverter().convertTo(String.class, exchange, source);
    assertEquals(CONTENT, string);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Test(org.junit.Test)

Example 58 with DOMSource

use of javax.xml.transform.dom.DOMSource in project camel by apache.

the class XAdESSignaturePropertiesTest method validateAgainstSchema.

private void validateAgainstSchema(Document doc) throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Source schema1 = new StreamSource(new File("target/test-classes/org/apache/camel/component/xmlsecurity/xades/XAdES.xsd"));
    Source schema2 = new StreamSource(new File("target/test-classes/org/apache/camel/component/xmlsecurity/xades/xmldsig-core-schema.xsd"));
    Schema schema = factory.newSchema(new Source[] { schema2, schema1 });
    Validator validator = schema.newValidator();
    validator.validate(new DOMSource(doc));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) File(java.io.File) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator)

Example 59 with DOMSource

use of javax.xml.transform.dom.DOMSource in project camel by apache.

the class EipDocumentationEnricherMojo method saveToFile.

private void saveToFile(Document document, File outputFile, Transformer transformer) throws FileNotFoundException, TransformerException {
    StreamResult result = new StreamResult(new FileOutputStream(outputFile));
    DOMSource source = new DOMSource(document);
    transformer.transform(source, result);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) FileOutputStream(java.io.FileOutputStream)

Example 60 with DOMSource

use of javax.xml.transform.dom.DOMSource in project camel by apache.

the class SpringBootStarterMojo method writeXmlFormatted.

private void writeXmlFormatted(Document pom, File destination) throws Exception {
    XPathExpression xpath = XPathFactory.newInstance().newXPath().compile("//text()[normalize-space(.) = '']");
    NodeList emptyNodes = (NodeList) xpath.evaluate(pom, XPathConstants.NODESET);
    // Remove empty text nodes
    for (int i = 0; i < emptyNodes.getLength(); i++) {
        Node emptyNode = emptyNodes.item(i);
        emptyNode.getParentNode().removeChild(emptyNode);
    }
    pom.setXmlStandalone(true);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    DOMSource source = new DOMSource(pom);
    String content;
    try (StringWriter out = new StringWriter()) {
        StreamResult result = new StreamResult(out);
        transformer.transform(source, result);
        content = out.toString();
    }
    // Fix header formatting problem
    content = content.replaceFirst("-->", "-->\n").replaceFirst("\\?><!--", "\\?>\n<!--");
    writeIfChanged(content, destination);
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) DependencyNode(org.apache.maven.shared.dependency.tree.DependencyNode)

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