Search in sources :

Example 56 with DOMResult

use of javax.xml.transform.dom.DOMResult in project lucene-solr by apache.

the class Config method copyDoc.

private static Document copyDoc(Document doc) throws TransformerException {
    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer tx = tfactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    DOMResult result = new DOMResult();
    tx.transform(source, result);
    return (Document) result.getNode();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) Document(org.w3c.dom.Document)

Example 57 with DOMResult

use of javax.xml.transform.dom.DOMResult in project webservices-axiom by apache.

the class TestTransformerWithStylesheet method runTest.

protected void runTest() throws Throwable {
    DocumentBuilder builder = dbf.newDocumentBuilder();
    Document input = builder.parse(TestTransformerWithStylesheet.class.getResourceAsStream("input.xml"));
    Document stylesheet = builder.parse(TestTransformerWithStylesheet.class.getResourceAsStream("stylesheet.xslt"));
    Document expected = builder.parse(TestTransformerWithStylesheet.class.getResourceAsStream("output.xml"));
    Document actual = builder.newDocument();
    Transformer transformer = xsltImplementation.newTransformerFactory().newTransformer(new DOMSource(stylesheet));
    transformer.transform(new DOMSource(input), new DOMResult(actual));
    assertAbout(xml()).that(xml(Document.class, actual)).ignoringWhitespace().hasSameContentAs(xml(Document.class, expected));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Document(org.w3c.dom.Document)

Example 58 with DOMResult

use of javax.xml.transform.dom.DOMResult in project webservices-axiom by apache.

the class SecureEchoTest method runTest.

@Override
protected void runTest() throws Throwable {
    DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document requestDocument = documentBuilder.newDocument();
    Element request = requestDocument.createElementNS("urn:test", "p:Echo");
    request.setTextContent("Hello");
    Document responseDocument = documentBuilder.newDocument();
    context.getBean(WebServiceTemplate.class).sendSourceAndReceiveToResult(new DOMSource(request), new SoapActionCallback("http://www.example.com/echo"), new DOMResult(responseDocument));
    Element response = responseDocument.getDocumentElement();
    assertEquals("urn:test", response.getNamespaceURI());
    assertEquals("Echo", response.getLocalName());
    assertEquals("Hello", response.getTextContent());
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) SoapActionCallback(org.springframework.ws.soap.client.core.SoapActionCallback) DOMResult(javax.xml.transform.dom.DOMResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) WebServiceTemplate(org.springframework.ws.client.core.WebServiceTemplate)

Example 59 with DOMResult

use of javax.xml.transform.dom.DOMResult in project ddf by codice.

the class SchematronValidationService method generateReport.

private SchematronReport generateReport(String metadata, Templates validator) throws SchematronValidationException {
    XMLReader xmlReader = null;
    try {
        XMLReader xmlParser = XMLReaderFactory.createXMLReader();
        xmlParser.setFeature("http://xml.org/sax/features/external-general-entities", false);
        xmlParser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        xmlParser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        xmlReader = new XMLFilterImpl(xmlParser);
    } catch (SAXException e) {
        throw new SchematronValidationException(e);
    }
    SchematronReport report;
    try {
        Transformer transformer = validator.newTransformer();
        DOMResult schematronResult = new DOMResult();
        transformer.transform(new SAXSource(xmlReader, new InputSource(new StringReader(metadata))), schematronResult);
        report = new SvrlReport(schematronResult);
    } catch (TransformerException e) {
        throw new SchematronValidationException("Could not setup validator to perform validation.", e);
    }
    return report;
}
Also used : InputSource(org.xml.sax.InputSource) Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) SAXSource(javax.xml.transform.sax.SAXSource) XMLFilterImpl(org.xml.sax.helpers.XMLFilterImpl) StringReader(java.io.StringReader) XMLReader(org.xml.sax.XMLReader) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException)

Example 60 with DOMResult

use of javax.xml.transform.dom.DOMResult in project ddf by codice.

the class SchematronValidationService method compileSchematronRules.

private Templates compileSchematronRules(String schematronFileName) throws SchematronInitializationException {
    Templates template;
    File schematronFile = new File(schematronFileName);
    if (!schematronFile.exists()) {
        throw new SchematronInitializationException("Could not locate schematron file " + schematronFileName);
    }
    try {
        URL schUrl = schematronFile.toURI().toURL();
        Source schSource = new StreamSource(schUrl.toString());
        // Stage 1: Perform inclusion expansion on Schematron schema file
        DOMResult stage1Result = performStage(schSource, getClass().getClassLoader().getResource("iso-schematron/iso_dsdl_include.xsl"));
        DOMSource stage1Output = new DOMSource(stage1Result.getNode());
        // Stage 2: Perform abstract expansion on output file from Stage 1
        DOMResult stage2Result = performStage(stage1Output, getClass().getClassLoader().getResource("iso-schematron/iso_abstract_expand.xsl"));
        DOMSource stage2Output = new DOMSource(stage2Result.getNode());
        // Stage 3: Compile the .sch rules that have been prepocessed by Stages 1 and 2 (i.e.,
        // the output of Stage 2)
        DOMResult stage3Result = performStage(stage2Output, getClass().getClassLoader().getResource("iso-schematron/iso_svrl_for_xslt2.xsl"));
        DOMSource stage3Output = new DOMSource(stage3Result.getNode());
        // Setting the system ID let's us resolve relative paths in the schematron files.
        // We need the URL string so that the string is properly formatted (e.g. space = %20).
        stage3Output.setSystemId(schUrl.toString());
        template = transformerFactory.newTemplates(stage3Output);
    } catch (Exception e) {
        throw new SchematronInitializationException("Error trying to create SchematronValidationService using sch file " + schematronFileName, e);
    }
    return template;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMResult(javax.xml.transform.dom.DOMResult) StreamSource(javax.xml.transform.stream.StreamSource) Templates(javax.xml.transform.Templates) File(java.io.File) URL(java.net.URL) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) ValidationException(ddf.catalog.validation.ValidationException) TransformerException(javax.xml.transform.TransformerException) ExecutionException(java.util.concurrent.ExecutionException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

DOMResult (javax.xml.transform.dom.DOMResult)61 Document (org.w3c.dom.Document)33 DOMSource (javax.xml.transform.dom.DOMSource)24 Transformer (javax.xml.transform.Transformer)20 DocumentBuilder (javax.xml.parsers.DocumentBuilder)17 TransformerException (javax.xml.transform.TransformerException)14 IOException (java.io.IOException)13 InputSource (org.xml.sax.InputSource)13 StreamSource (javax.xml.transform.stream.StreamSource)12 StringReader (java.io.StringReader)11 SAXResult (javax.xml.transform.sax.SAXResult)10 Element (org.w3c.dom.Element)10 Node (org.w3c.dom.Node)10 SAXSource (javax.xml.transform.sax.SAXSource)9 StreamResult (javax.xml.transform.stream.StreamResult)9 Test (org.junit.Test)9 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)8 Source (javax.xml.transform.Source)8 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)7 InputStream (java.io.InputStream)6