Example usage for org.springframework.util.xml StaxUtils isStaxSource

List of usage examples for org.springframework.util.xml StaxUtils isStaxSource

Introduction

In this page you can find the example usage for org.springframework.util.xml StaxUtils isStaxSource.

Prototype

public static boolean isStaxSource(Source source) 

Source Link

Document

Indicate whether the given Source is a JAXP 1.4 StAX Source or custom StAX Source.

Usage

From source file:org.springframework.oxm.jaxb.Jaxb2Marshaller.java

@Override
public Object unmarshal(Source source, @Nullable MimeContainer mimeContainer) throws XmlMappingException {
    source = processSource(source);// ww w  . j a  v  a  2  s.c  o  m

    try {
        Unmarshaller unmarshaller = createUnmarshaller();
        if (this.mtomEnabled && mimeContainer != null) {
            unmarshaller.setAttachmentUnmarshaller(new Jaxb2AttachmentUnmarshaller(mimeContainer));
        }
        if (StaxUtils.isStaxSource(source)) {
            return unmarshalStaxSource(unmarshaller, source);
        } else if (this.mappedClass != null) {
            return unmarshaller.unmarshal(source, this.mappedClass).getValue();
        } else {
            return unmarshaller.unmarshal(source);
        }
    } catch (NullPointerException ex) {
        if (!isSupportDtd()) {
            throw new UnmarshallingFailureException(
                    "NPE while unmarshalling: "
                            + "This can happen due to the presence of DTD declarations which are disabled.",
                    ex);
        }
        throw ex;
    } catch (JAXBException ex) {
        throw convertJaxbException(ex);
    }
}

From source file:org.springframework.oxm.jaxb.Jaxb2Marshaller.java

@SuppressWarnings("deprecation") // on JDK 9
private Source processSource(Source source) {
    if (StaxUtils.isStaxSource(source) || source instanceof DOMSource) {
        return source;
    }/*from  w w w  .j a  v a 2  s.  c  o m*/

    XMLReader xmlReader = null;
    InputSource inputSource = null;

    if (source instanceof SAXSource) {
        SAXSource saxSource = (SAXSource) source;
        xmlReader = saxSource.getXMLReader();
        inputSource = saxSource.getInputSource();
    } else if (source instanceof StreamSource) {
        StreamSource streamSource = (StreamSource) source;
        if (streamSource.getInputStream() != null) {
            inputSource = new InputSource(streamSource.getInputStream());
        } else if (streamSource.getReader() != null) {
            inputSource = new InputSource(streamSource.getReader());
        } else {
            inputSource = new InputSource(streamSource.getSystemId());
        }
    }

    try {
        if (xmlReader == null) {
            xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
        }
        xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
        String name = "http://xml.org/sax/features/external-general-entities";
        xmlReader.setFeature(name, isProcessExternalEntities());
        if (!isProcessExternalEntities()) {
            xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER);
        }
        return new SAXSource(xmlReader, inputSource);
    } catch (SAXException ex) {
        logger.warn("Processing of external entities could not be disabled", ex);
        return source;
    }
}

From source file:org.springframework.oxm.support.AbstractMarshaller.java

/**
 * Unmarshals the given provided {@code javax.xml.transform.Source} into an object graph.
 * <p>This implementation inspects the given result, and calls {@code unmarshalDomSource},
 * {@code unmarshalSaxSource}, or {@code unmarshalStreamSource}.
 * @param source the source to marshal from
 * @return the object graph/*www  .j av  a2 s.  c  o m*/
 * @throws IOException if an I/O Exception occurs
 * @throws XmlMappingException if the given source cannot be mapped to an object
 * @throws IllegalArgumentException if {@code source} is neither a {@code DOMSource},
 * a {@code SAXSource}, nor a {@code StreamSource}
 * @see #unmarshalDomSource(javax.xml.transform.dom.DOMSource)
 * @see #unmarshalSaxSource(javax.xml.transform.sax.SAXSource)
 * @see #unmarshalStreamSource(javax.xml.transform.stream.StreamSource)
 */
@Override
public final Object unmarshal(Source source) throws IOException, XmlMappingException {
    if (source instanceof DOMSource) {
        return unmarshalDomSource((DOMSource) source);
    } else if (StaxUtils.isStaxSource(source)) {
        return unmarshalStaxSource(source);
    } else if (source instanceof SAXSource) {
        return unmarshalSaxSource((SAXSource) source);
    } else if (source instanceof StreamSource) {
        return unmarshalStreamSource((StreamSource) source);
    } else {
        throw new IllegalArgumentException("Unknown Source type: " + source.getClass());
    }
}