Search in sources :

Example 46 with DOMResult

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

the class XStreamMarshallerTests method marshalDOMResult.

@Test
public void marshalDOMResult() throws Exception {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
    Document document = builder.newDocument();
    DOMResult domResult = new DOMResult(document);
    marshaller.marshal(flight, domResult);
    Document expected = builder.newDocument();
    Element flightElement = expected.createElement("flight");
    expected.appendChild(flightElement);
    Element numberElement = expected.createElement("flightNumber");
    flightElement.appendChild(numberElement);
    Text text = expected.createTextNode("42");
    numberElement.appendChild(text);
    assertThat("Marshaller writes invalid DOMResult", document, 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) Test(org.junit.Test)

Example 47 with DOMResult

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

the class XStreamMarshallerTests method marshalDOMResultToExistentDocument.

// see SWS-392
@Test
public void marshalDOMResultToExistentDocument() throws Exception {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
    Document existent = builder.newDocument();
    Element rootElement = existent.createElement("root");
    Element flightsElement = existent.createElement("flights");
    rootElement.appendChild(flightsElement);
    existent.appendChild(rootElement);
    // marshall into the existent document
    DOMResult domResult = new DOMResult(flightsElement);
    marshaller.marshal(flight, domResult);
    Document expected = builder.newDocument();
    Element eRootElement = expected.createElement("root");
    Element eFlightsElement = expected.createElement("flights");
    Element eFlightElement = expected.createElement("flight");
    eRootElement.appendChild(eFlightsElement);
    eFlightsElement.appendChild(eFlightElement);
    expected.appendChild(eRootElement);
    Element eNumberElement = expected.createElement("flightNumber");
    eFlightElement.appendChild(eNumberElement);
    Text text = expected.createTextNode("42");
    eNumberElement.appendChild(text);
    assertThat("Marshaller writes invalid DOMResult", existent, 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) Test(org.junit.Test)

Example 48 with DOMResult

use of javax.xml.transform.dom.DOMResult in project OpenAM by OpenRock.

the class MarshallerImpl method marshal.

public void marshal(Object obj, Result result) throws JAXBException {
    //XMLSerializable so = Util.toXMLSerializable(obj);
    XMLSerializable so = context.getGrammarInfo().castToXMLSerializable(obj);
    if (so == null)
        throw new MarshalException(Messages.format(Messages.NOT_MARSHALLABLE));
    if (result instanceof SAXResult) {
        write(so, ((SAXResult) result).getHandler());
        return;
    }
    if (result instanceof DOMResult) {
        Node node = ((DOMResult) result).getNode();
        if (node == null) {
            try {
                DocumentBuilder db = XMLUtils.getSafeDocumentBuilder(false);
                Document doc = db.newDocument();
                ((DOMResult) result).setNode(doc);
                write(so, new SAX2DOMEx(doc));
            } catch (ParserConfigurationException pce) {
                throw new JAXBAssertionError(pce);
            }
        } else {
            write(so, new SAX2DOMEx(node));
        }
        return;
    }
    if (result instanceof StreamResult) {
        StreamResult sr = (StreamResult) result;
        XMLWriter w = null;
        if (sr.getWriter() != null)
            w = createWriter(sr.getWriter());
        else if (sr.getOutputStream() != null)
            w = createWriter(sr.getOutputStream());
        else if (sr.getSystemId() != null) {
            String fileURL = sr.getSystemId();
            if (fileURL.startsWith("file:///")) {
                if (fileURL.substring(8).indexOf(":") > 0)
                    fileURL = fileURL.substring(8);
                else
                    fileURL = fileURL.substring(7);
            }
            try {
                w = createWriter(new FileOutputStream(fileURL));
            } catch (IOException e) {
                throw new MarshalException(e);
            }
        }
        if (w == null)
            throw new IllegalArgumentException();
        write(so, w);
        return;
    }
    // unsupported parameter type
    throw new MarshalException(Messages.format(Messages.UNSUPPORTED_RESULT));
}
Also used : MarshalException(javax.xml.bind.MarshalException) DOMResult(javax.xml.transform.dom.DOMResult) StreamResult(javax.xml.transform.stream.StreamResult) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) XMLWriter(com.sun.xml.bind.marshaller.XMLWriter) SAXResult(javax.xml.transform.sax.SAXResult) JAXBAssertionError(com.sun.xml.bind.JAXBAssertionError) DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileOutputStream(java.io.FileOutputStream) SAX2DOMEx(com.sun.xml.bind.marshaller.SAX2DOMEx) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 49 with DOMResult

use of javax.xml.transform.dom.DOMResult in project OpenAM by OpenRock.

the class MarshallerImpl method marshal.

public void marshal(Object obj, Result result) throws JAXBException {
    //XMLSerializable so = Util.toXMLSerializable(obj);
    XMLSerializable so = context.getGrammarInfo().castToXMLSerializable(obj);
    if (so == null)
        throw new MarshalException(Messages.format(Messages.NOT_MARSHALLABLE));
    if (result instanceof SAXResult) {
        write(so, ((SAXResult) result).getHandler());
        return;
    }
    if (result instanceof DOMResult) {
        Node node = ((DOMResult) result).getNode();
        if (node == null) {
            try {
                DocumentBuilder db = XMLUtils.getSafeDocumentBuilder(false);
                Document doc = db.newDocument();
                ((DOMResult) result).setNode(doc);
                write(so, new SAX2DOMEx(doc));
            } catch (ParserConfigurationException pce) {
                throw new JAXBAssertionError(pce);
            }
        } else {
            write(so, new SAX2DOMEx(node));
        }
        return;
    }
    if (result instanceof StreamResult) {
        StreamResult sr = (StreamResult) result;
        XMLWriter w = null;
        if (sr.getWriter() != null)
            w = createWriter(sr.getWriter());
        else if (sr.getOutputStream() != null)
            w = createWriter(sr.getOutputStream());
        else if (sr.getSystemId() != null) {
            String fileURL = sr.getSystemId();
            if (fileURL.startsWith("file:///")) {
                if (fileURL.substring(8).indexOf(":") > 0)
                    fileURL = fileURL.substring(8);
                else
                    fileURL = fileURL.substring(7);
            }
            try {
                w = createWriter(new FileOutputStream(fileURL));
            } catch (IOException e) {
                throw new MarshalException(e);
            }
        }
        if (w == null)
            throw new IllegalArgumentException();
        write(so, w);
        return;
    }
    // unsupported parameter type
    throw new MarshalException(Messages.format(Messages.UNSUPPORTED_RESULT));
}
Also used : MarshalException(javax.xml.bind.MarshalException) DOMResult(javax.xml.transform.dom.DOMResult) StreamResult(javax.xml.transform.stream.StreamResult) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) XMLWriter(com.sun.xml.bind.marshaller.XMLWriter) SAXResult(javax.xml.transform.sax.SAXResult) JAXBAssertionError(com.sun.xml.bind.JAXBAssertionError) DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileOutputStream(java.io.FileOutputStream) SAX2DOMEx(com.sun.xml.bind.marshaller.SAX2DOMEx) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 50 with DOMResult

use of javax.xml.transform.dom.DOMResult in project uPortal by Jasig.

the class FragmentDefinitionImporter method unmarshal.

/* (non-Javadoc)
     * @see org.springframework.oxm.Unmarshaller#unmarshal(javax.xml.transform.Source)
     */
@Override
public Object unmarshal(Source source) throws IOException, XmlMappingException {
    if (source instanceof StAXSource) {
        source = fixStAXSource((StAXSource) source);
    }
    final Transformer identityTransformer;
    try {
        identityTransformer = this.xmlUtilities.getIdentityTransformer();
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException("Failed to create identity Transformer", e);
    }
    final DOMResult domResult = new DOMResult();
    try {
        identityTransformer.transform(source, domResult);
    } catch (TransformerException e) {
        throw new RuntimeException("Failed to transform " + source + " into Document", e);
    }
    final Document resultDoc = (Document) domResult.getNode();
    return new Tuple<String, Document>(source.getSystemId(), resultDoc);
}
Also used : Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StAXSource(javax.xml.transform.stax.StAXSource) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException) Tuple(org.apereo.portal.utils.Tuple)

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