Search in sources :

Example 16 with DOMResult

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

the class SoapCreateSubject method createSubject.

public String createSubject(SubjectBean subjectBean, StudyBean studyBean, Date enrollmentDate, String secondaryId) {
    Element requestElement = prepareRequest(subjectBean, studyBean);
    DOMSource source = new DOMSource(requestElement);
    DOMResult result = new DOMResult();
    getWebServiceTemplate().sendSourceAndReceiveToResult(source, result);
    return processResponse(result.getNode());
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMResult(javax.xml.transform.dom.DOMResult) Element(org.w3c.dom.Element)

Example 17 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 18 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 19 with DOMResult

use of javax.xml.transform.dom.DOMResult in project voltdb by VoltDB.

the class JDBCSQLXML method createDOMSource.

/**
     * Retrieves a new DOMSource for reading the XML value designated by this
     * SQLXML instance. <p>
     *
     * @param sourceClass The class of the source
     * @throws java.sql.SQLException if there is an error processing the XML
     *      value or if the given <tt>sourceClass</tt> is not supported.
     * @return a new DOMSource for reading the XML value designated by this
     *      SQLXML instance
     */
@SuppressWarnings("unchecked")
protected <T extends Source> T createDOMSource(Class<T> sourceClass) throws SQLException {
    DOMSource source = null;
    try {
        source = (sourceClass == null) ? new DOMSource() : (DOMSource) sourceClass.newInstance();
    } catch (SecurityException ex) {
        throw Exceptions.sourceInstantiation(ex);
    } catch (IllegalAccessException ex) {
        throw Exceptions.sourceInstantiation(ex);
    } catch (InstantiationException ex) {
        throw Exceptions.sourceInstantiation(ex);
    } catch (ClassCastException ex) {
        throw Exceptions.sourceInstantiation(ex);
    }
    Transformer transformer = JDBCSQLXML.getIdentityTransformer();
    InputStream inputStream = this.getBinaryStreamImpl();
    StreamSource streamSource = new StreamSource();
    DOMResult domResult = new DOMResult();
    streamSource.setInputStream(inputStream);
    try {
        transformer.transform(streamSource, domResult);
    } catch (TransformerException ex) {
        throw Exceptions.transformFailed(ex);
    }
    source.setNode(domResult.getNode());
    return (T) source;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) TransformerException(javax.xml.transform.TransformerException)

Example 20 with DOMResult

use of javax.xml.transform.dom.DOMResult in project jackrabbit by apache.

the class ExportDocViewTest method readDocument.

/**
     * Reads a DOM document from the given XML stream.
     *
     * @param xml XML stream
     * @return DOM document
     * @throws RepositoryException if the document could not be read
     */
private Document readDocument(InputStream xml) throws RepositoryException {
    try {
        StreamSource source = new StreamSource(xml);
        DOMResult result = new DOMResult();
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.transform(source, result);
        return (Document) result.getNode();
    } catch (TransformerException e) {
        throw new RepositoryException("Unable to read xml file", e);
    }
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamSource(javax.xml.transform.stream.StreamSource) RepositoryException(javax.jcr.RepositoryException) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException)

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