Search in sources :

Example 6 with DOMResult

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

the class StaxSourceTests method streamReaderSourceToDOMResult.

@Test
public void streamReaderSourceToDOMResult() throws Exception {
    XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(XML));
    StaxSource source = new StaxSource(streamReader);
    assertEquals("Invalid streamReader returned", streamReader, source.getXMLStreamReader());
    assertNull("EventReader returned", source.getXMLEventReader());
    Document expected = documentBuilder.parse(new InputSource(new StringReader(XML)));
    Document result = documentBuilder.newDocument();
    transformer.transform(source, new DOMResult(result));
    assertThat("Invalid result", result, isSimilarTo(expected));
}
Also used : InputSource(org.xml.sax.InputSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) DOMResult(javax.xml.transform.dom.DOMResult) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 7 with DOMResult

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

the class JibxMarshaller method marshalDomNode.

// Unsupported marshalling
@Override
protected void marshalDomNode(Object graph, Node node) throws XmlMappingException {
    try {
        // JiBX does not support DOM natively, so we write to a buffer first, and transform that to the Node
        Result result = new DOMResult(node);
        transformAndMarshal(graph, result);
    } catch (IOException ex) {
        throw new MarshallingFailureException("JiBX marshalling exception", ex);
    }
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) MarshallingFailureException(org.springframework.oxm.MarshallingFailureException) IOException(java.io.IOException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) DOMResult(javax.xml.transform.dom.DOMResult)

Example 8 with DOMResult

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

the class AbstractMarshallerTests method marshalDOMResult.

@Test
public void marshalDOMResult() throws Exception {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
    Document result = builder.newDocument();
    DOMResult domResult = new DOMResult(result);
    marshaller.marshal(flights, domResult);
    Document expected = builder.newDocument();
    Element flightsElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flights");
    Attr namespace = expected.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:tns");
    namespace.setNodeValue("http://samples.springframework.org/flight");
    flightsElement.setAttributeNode(namespace);
    expected.appendChild(flightsElement);
    Element flightElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flight");
    flightsElement.appendChild(flightElement);
    Element numberElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:number");
    flightElement.appendChild(numberElement);
    Text text = expected.createTextNode("42");
    numberElement.appendChild(text);
    assertThat("Marshaller writes invalid DOMResult", result, isSimilarTo(expected));
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DOMResult(javax.xml.transform.dom.DOMResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr) Test(org.junit.Test)

Example 9 with DOMResult

use of javax.xml.transform.dom.DOMResult in project nokogiri by sparklemotion.

the class XsltStylesheet method tryXsltTransformation.

private DOMResult tryXsltTransformation(ThreadContext context, IRubyObject[] args, DOMSource domSource, NokogiriXsltErrorListener elistener) throws TransformerException {
    Transformer transf = sheet.newTransformer();
    transf.reset();
    transf.setErrorListener(elistener);
    if (args.length > 1) {
        addParametersToTransformer(context, transf, args[1]);
    }
    DOMResult result = new DOMResult();
    transf.transform(domSource, result);
    return result;
}
Also used : Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult)

Example 10 with DOMResult

use of javax.xml.transform.dom.DOMResult in project gocd by gocd.

the class XsltStylesheet method transform.

@JRubyMethod(rest = true, required = 1, optional = 2)
public IRubyObject transform(ThreadContext context, IRubyObject[] args) {
    Ruby runtime = context.getRuntime();
    argumentTypeCheck(runtime, args[0]);
    NokogiriXsltErrorListener elistener = new NokogiriXsltErrorListener();
    DOMSource domSource = new DOMSource(((XmlDocument) args[0]).getDocument());
    DOMResult result = null;
    String stringResult = null;
    try {
        // DOMResult
        result = tryXsltTransformation(context, args, domSource, elistener);
        if (result.getNode().getFirstChild() == null) {
            // StreamResult
            stringResult = retryXsltTransformation(context, args, domSource, elistener);
        }
    } catch (TransformerConfigurationException ex) {
        throw runtime.newRuntimeError(ex.getMessage());
    } catch (TransformerException ex) {
        throw runtime.newRuntimeError(ex.getMessage());
    } catch (IOException ex) {
        throw runtime.newRuntimeError(ex.getMessage());
    }
    switch(elistener.getErrorType()) {
        case ERROR:
        case FATAL:
            throw runtime.newRuntimeError(elistener.getErrorMessage());
        case WARNING:
        default:
    }
    if (stringResult == null) {
        return createDocumentFromDomResult(context, runtime, result);
    } else {
        return createDocumentFromString(context, runtime, stringResult);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMResult(javax.xml.transform.dom.DOMResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) RubyString(org.jruby.RubyString) IOException(java.io.IOException) Ruby(org.jruby.Ruby) TransformerException(javax.xml.transform.TransformerException) NokogiriXsltErrorListener(nokogiri.internals.NokogiriXsltErrorListener) JRubyMethod(org.jruby.anno.JRubyMethod)

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