org.springframework.oxm.XmlMappingException Java Examples

The following examples show how to use org.springframework.oxm.XmlMappingException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractMarshaller.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Template method for handling {@code StaxSource}s.
 * <p>This implementation delegates to {@code unmarshalXmlStreamReader} or
 * {@code unmarshalXmlEventReader}.
 * @param staxSource the {@code StaxSource}
 * @return the object graph
 * @throws XmlMappingException if the given source cannot be mapped to an object
 */
protected Object unmarshalStaxSource(Source staxSource) throws XmlMappingException {
	XMLStreamReader streamReader = StaxUtils.getXMLStreamReader(staxSource);
	if (streamReader != null) {
		return unmarshalXmlStreamReader(streamReader);
	}
	else {
		XMLEventReader eventReader = StaxUtils.getXMLEventReader(staxSource);
		if (eventReader != null) {
			return unmarshalXmlEventReader(eventReader);
		}
		else {
			throw new IllegalArgumentException("StaxSource contains neither XMLStreamReader nor XMLEventReader");
		}
	}
}
 
Example #2
Source File: MarshallingMessageConverter.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * This implementation marshals the given object to a {@link javax.jms.TextMessage} or
 * {@link javax.jms.BytesMessage}. The desired message type can be defined by setting
 * the {@link #setTargetType "marshalTo"} property.
 * @see #marshalToTextMessage
 * @see #marshalToBytesMessage
 */
@Override
public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
	Assert.state(this.marshaller != null, "No Marshaller set");
	try {
		switch (this.targetType) {
			case TEXT:
				return marshalToTextMessage(object, session, this.marshaller);
			case BYTES:
				return marshalToBytesMessage(object, session, this.marshaller);
			default:
				return marshalToMessage(object, session, this.marshaller, this.targetType);
		}
	}
	catch (XmlMappingException | IOException ex) {
		throw new MessageConversionException("Could not marshal [" + object + "]", ex);
	}
}
 
Example #3
Source File: Jaxb2Marshaller.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void marshal(Object graph, Result result, @Nullable MimeContainer mimeContainer) throws XmlMappingException {
	try {
		Marshaller marshaller = createMarshaller();
		if (this.mtomEnabled && mimeContainer != null) {
			marshaller.setAttachmentMarshaller(new Jaxb2AttachmentMarshaller(mimeContainer));
		}
		if (StaxUtils.isStaxResult(result)) {
			marshalStaxResult(marshaller, graph, result);
		}
		else {
			marshaller.marshal(graph, result);
		}
	}
	catch (JAXBException ex) {
		throw convertJaxbException(ex);
	}
}
 
Example #4
Source File: Jaxb2Marshaller.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Convert the given {@code JAXBException} to an appropriate exception
 * from the {@code org.springframework.oxm} hierarchy.
 * @param ex {@code JAXBException} that occurred
 * @return the corresponding {@code XmlMappingException}
 */
protected XmlMappingException convertJaxbException(JAXBException ex) {
	if (ex instanceof ValidationException) {
		return new ValidationFailureException("JAXB validation exception", ex);
	}
	else if (ex instanceof MarshalException) {
		return new MarshallingFailureException("JAXB marshalling exception", ex);
	}
	else if (ex instanceof UnmarshalException) {
		return new UnmarshallingFailureException("JAXB unmarshalling exception", ex);
	}
	else {
		// fallback
		return new UncategorizedMappingException("Unknown JAXB exception", ex);
	}
}
 
Example #5
Source File: AbstractMarshaller.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Template method for handling {@code StreamSource}s.
 * <p>This implementation delegates to {@code unmarshalInputStream} or {@code unmarshalReader}.
 * @param streamSource the {@code StreamSource}
 * @return the object graph
 * @throws IOException if an I/O exception occurs
 * @throws XmlMappingException if the given source cannot be mapped to an object
 */
protected Object unmarshalStreamSource(StreamSource streamSource) throws XmlMappingException, IOException {
	if (streamSource.getInputStream() != null) {
		if (isProcessExternalEntities() && isSupportDtd()) {
			return unmarshalInputStream(streamSource.getInputStream());
		}
		else {
			InputSource inputSource = new InputSource(streamSource.getInputStream());
			inputSource.setEncoding(getDefaultEncoding());
			return unmarshalSaxSource(new SAXSource(inputSource));
		}
	}
	else if (streamSource.getReader() != null) {
		if (isProcessExternalEntities() && isSupportDtd()) {
			return unmarshalReader(streamSource.getReader());
		}
		else {
			return unmarshalSaxSource(new SAXSource(new InputSource(streamSource.getReader())));
		}
	}
	else {
		return unmarshalSaxSource(new SAXSource(new InputSource(streamSource.getSystemId())));
	}
}
 
Example #6
Source File: AbstractMarshaller.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Template method for handling {@code DOMSource}s.
 * <p>This implementation delegates to {@code unmarshalDomNode}.
 * If the given source is empty, an empty source {@code Document}
 * will be created as a placeholder.
 * @param domSource the {@code DOMSource}
 * @return the object graph
 * @throws XmlMappingException if the given source cannot be mapped to an object
 * @throws IllegalArgumentException if the {@code domSource} is empty
 * @see #unmarshalDomNode(org.w3c.dom.Node)
 */
protected Object unmarshalDomSource(DOMSource domSource) throws XmlMappingException {
	if (domSource.getNode() == null) {
		domSource.setNode(buildDocument());
	}
	try {
		return unmarshalDomNode(domSource.getNode());
	}
	catch (NullPointerException ex) {
		if (!isSupportDtd()) {
			throw new UnmarshallingFailureException("NPE while unmarshalling. " +
					"This can happen on JDK 1.6 due to the presence of DTD " +
					"declarations, which are disabled.", ex);
		}
		throw ex;
	}
}
 
Example #7
Source File: AbstractMarshaller.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Template method for handling {@code StreamSource}s.
 * <p>This implementation delegates to {@code unmarshalInputStream} or {@code unmarshalReader}.
 * @param streamSource the {@code StreamSource}
 * @return the object graph
 * @throws IOException if an I/O exception occurs
 * @throws XmlMappingException if the given source cannot be mapped to an object
 */
protected Object unmarshalStreamSource(StreamSource streamSource) throws XmlMappingException, IOException {
	if (streamSource.getInputStream() != null) {
		if (isProcessExternalEntities() && isSupportDtd()) {
			return unmarshalInputStream(streamSource.getInputStream());
		}
		else {
			InputSource inputSource = new InputSource(streamSource.getInputStream());
			inputSource.setEncoding(getDefaultEncoding());
			return unmarshalSaxSource(new SAXSource(inputSource));
		}
	}
	else if (streamSource.getReader() != null) {
		if (isProcessExternalEntities() && isSupportDtd()) {
			return unmarshalReader(streamSource.getReader());
		}
		else {
			return unmarshalSaxSource(new SAXSource(new InputSource(streamSource.getReader())));
		}
	}
	else {
		return unmarshalSaxSource(new SAXSource(new InputSource(streamSource.getSystemId())));
	}
}
 
Example #8
Source File: MarshallingMessageConverter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * This implementation marshals the given object to a {@link javax.jms.TextMessage} or
 * {@link javax.jms.BytesMessage}. The desired message type can be defined by setting
 * the {@link #setTargetType "marshalTo"} property.
 * @see #marshalToTextMessage
 * @see #marshalToBytesMessage
 */
@Override
public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
	Assert.state(this.marshaller != null, "No Marshaller set");
	try {
		switch (this.targetType) {
			case TEXT:
				return marshalToTextMessage(object, session, this.marshaller);
			case BYTES:
				return marshalToBytesMessage(object, session, this.marshaller);
			default:
				return marshalToMessage(object, session, this.marshaller, this.targetType);
		}
	}
	catch (XmlMappingException | IOException ex) {
		throw new MessageConversionException("Could not marshal [" + object + "]", ex);
	}
}
 
Example #9
Source File: XStreamMarshaller.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Convert the given XStream exception to an appropriate exception from the
 * {@code org.springframework.oxm} hierarchy.
 * <p>A boolean flag is used to indicate whether this exception occurs during marshalling or
 * unmarshalling, since XStream itself does not make this distinction in its exception hierarchy.
 * @param ex the XStream exception that occurred
 * @param marshalling indicates whether the exception occurs during marshalling ({@code true}),
 * or unmarshalling ({@code false})
 * @return the corresponding {@code XmlMappingException}
 */
protected XmlMappingException convertXStreamException(Exception ex, boolean marshalling) {
	if (ex instanceof StreamException || ex instanceof CannotResolveClassException ||
			ex instanceof ConversionException) {
		if (marshalling) {
			return new MarshallingFailureException("XStream marshalling exception",  ex);
		}
		else {
			return new UnmarshallingFailureException("XStream unmarshalling exception", ex);
		}
	}
	else {
		// fallback
		return new UncategorizedMappingException("Unknown XStream exception", ex);
	}
}
 
Example #10
Source File: AbstractMarshaller.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Template method for handling {@code DOMSource}s.
 * <p>This implementation delegates to {@code unmarshalDomNode}.
 * If the given source is empty, an empty source {@code Document}
 * will be created as a placeholder.
 * @param domSource the {@code DOMSource}
 * @return the object graph
 * @throws XmlMappingException if the given source cannot be mapped to an object
 * @throws IllegalArgumentException if the {@code domSource} is empty
 * @see #unmarshalDomNode(org.w3c.dom.Node)
 */
protected Object unmarshalDomSource(DOMSource domSource) throws XmlMappingException {
	if (domSource.getNode() == null) {
		domSource.setNode(buildDocument());
	}
	try {
		return unmarshalDomNode(domSource.getNode());
	}
	catch (NullPointerException ex) {
		if (!isSupportDtd()) {
			throw new UnmarshallingFailureException("NPE while unmarshalling. " +
					"This can happen on JDK 1.6 due to the presence of DTD " +
					"declarations, which are disabled.", ex);
		}
		throw ex;
	}
}
 
Example #11
Source File: AbstractMarshaller.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Marshals the object graph with the given root into the provided {@code javax.xml.transform.Result}.
 * <p>This implementation inspects the given result, and calls {@code marshalDomResult},
 * {@code marshalSaxResult}, or {@code marshalStreamResult}.
 * @param graph the root of the object graph to marshal
 * @param result the result to marshal to
 * @throws IOException if an I/O exception occurs
 * @throws XmlMappingException if the given object cannot be marshalled to the result
 * @throws IllegalArgumentException if {@code result} if neither a {@code DOMResult},
 * a {@code SAXResult}, nor a {@code StreamResult}
 * @see #marshalDomResult(Object, javax.xml.transform.dom.DOMResult)
 * @see #marshalSaxResult(Object, javax.xml.transform.sax.SAXResult)
 * @see #marshalStreamResult(Object, javax.xml.transform.stream.StreamResult)
 */
@Override
public final void marshal(Object graph, Result result) throws IOException, XmlMappingException {
	if (result instanceof DOMResult) {
		marshalDomResult(graph, (DOMResult) result);
	}
	else if (StaxUtils.isStaxResult(result)) {
		marshalStaxResult(graph, result);
	}
	else if (result instanceof SAXResult) {
		marshalSaxResult(graph, (SAXResult) result);
	}
	else if (result instanceof StreamResult) {
		marshalStreamResult(graph, (StreamResult) result);
	}
	else {
		throw new IllegalArgumentException("Unknown Result type: " + result.getClass());
	}
}
 
Example #12
Source File: XStreamMarshaller.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public Object unmarshalInputStream(InputStream inputStream, @Nullable DataHolder dataHolder) throws XmlMappingException, IOException {
	if (this.streamDriver != null) {
		return doUnmarshal(this.streamDriver.createReader(inputStream), dataHolder);
	}
	else {
		return unmarshalReader(new InputStreamReader(inputStream, this.encoding), dataHolder);
	}
}
 
Example #13
Source File: JibxMarshaller.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException {
	try {
		IUnmarshallingContext unmarshallingContext = createUnmarshallingContext();
		return unmarshallingContext.unmarshalDocument(inputStream, this.encoding);
	}
	catch (JiBXException ex) {
		throw convertJibxException(ex, false);
	}
}
 
Example #14
Source File: XStreamMarshaller.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public void marshalOutputStream(Object graph, OutputStream outputStream, @Nullable DataHolder dataHolder)
		throws XmlMappingException, IOException {

	if (this.streamDriver != null) {
		doMarshal(graph, this.streamDriver.createWriter(outputStream), dataHolder);
	}
	else {
		marshalWriter(graph, new OutputStreamWriter(outputStream, this.encoding), dataHolder);
	}
}
 
Example #15
Source File: XStreamMarshaller.java    From java-technology-stack with MIT License 5 votes vote down vote up
public Object unmarshalInputStream(InputStream inputStream, @Nullable DataHolder dataHolder) throws XmlMappingException, IOException {
	if (this.streamDriver != null) {
		return doUnmarshal(this.streamDriver.createReader(inputStream), dataHolder);
	}
	else {
		return unmarshalReader(new InputStreamReader(inputStream, this.encoding), dataHolder);
	}
}
 
Example #16
Source File: XStreamMarshaller.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, @Nullable LexicalHandler lexicalHandler)
		throws XmlMappingException {

	SaxWriter saxWriter = new SaxWriter(this.nameCoder);
	saxWriter.setContentHandler(contentHandler);
	doMarshal(graph, saxWriter, null);
}
 
Example #17
Source File: XStreamMarshaller.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException {
	try {
		doMarshal(graph, new StaxWriter(new QNameMap(), streamWriter, this.nameCoder), null);
	}
	catch (XMLStreamException ex) {
		throw convertXStreamException(ex, true);
	}
}
 
Example #18
Source File: JibxMarshaller.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected void marshalOutputStream(Object graph, OutputStream outputStream)
		throws XmlMappingException, IOException {
	try {
		IMarshallingContext marshallingContext = createMarshallingContext();
		marshallingContext.startDocument(this.encoding, this.standalone, outputStream);
		marshalDocument(marshallingContext, graph);
	}
	catch (JiBXException ex) {
		throw convertJibxException(ex, true);
	}
}
 
Example #19
Source File: XStreamMarshaller.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public void marshalWriter(Object graph, Writer writer, @Nullable DataHolder dataHolder)
		throws XmlMappingException, IOException {

	if (this.streamDriver != null) {
		doMarshal(graph, this.streamDriver.createWriter(writer), dataHolder);
	}
	else {
		doMarshal(graph, new CompactWriter(writer), dataHolder);
	}
}
 
Example #20
Source File: XStreamMarshaller.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource)
		throws XmlMappingException, IOException {

	throw new UnsupportedOperationException(
			"XStreamMarshaller does not support unmarshalling using SAX XMLReaders");
}
 
Example #21
Source File: AbstractMarshaller.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Template method for handling {@code StreamResult}s.
 * <p>This implementation delegates to {@code marshalOutputStream} or {@code marshalWriter},
 * depending on what is contained in the {@code StreamResult}
 * @param graph the root of the object graph to marshal
 * @param streamResult the {@code StreamResult}
 * @throws IOException if an I/O Exception occurs
 * @throws XmlMappingException if the given object cannot be marshalled to the result
 * @throws IllegalArgumentException if {@code streamResult} does neither
 * contain an {@code OutputStream} nor a {@code Writer}
 */
protected void marshalStreamResult(Object graph, StreamResult streamResult)
		throws XmlMappingException, IOException {

	if (streamResult.getOutputStream() != null) {
		marshalOutputStream(graph, streamResult.getOutputStream());
	}
	else if (streamResult.getWriter() != null) {
		marshalWriter(graph, streamResult.getWriter());
	}
	else {
		throw new IllegalArgumentException("StreamResult contains neither OutputStream nor Writer");
	}
}
 
Example #22
Source File: JibxMarshaller.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected Object unmarshalDomNode(Node node) throws XmlMappingException {
	try {
		return transformAndUnmarshal(new DOMSource(node), null);
	}
	catch (IOException ex) {
		throw new UnmarshallingFailureException("JiBX unmarshalling exception", ex);
	}
}
 
Example #23
Source File: XStreamMarshaller.java    From java-technology-stack with MIT License 5 votes vote down vote up
public void marshalOutputStream(Object graph, OutputStream outputStream, @Nullable DataHolder dataHolder)
		throws XmlMappingException, IOException {

	if (this.streamDriver != null) {
		doMarshal(graph, this.streamDriver.createWriter(outputStream), dataHolder);
	}
	else {
		marshalWriter(graph, new OutputStreamWriter(outputStream, this.encoding), dataHolder);
	}
}
 
Example #24
Source File: XStreamMarshaller.java    From java-technology-stack with MIT License 5 votes vote down vote up
public void marshalWriter(Object graph, Writer writer, @Nullable DataHolder dataHolder)
		throws XmlMappingException, IOException {

	if (this.streamDriver != null) {
		doMarshal(graph, this.streamDriver.createWriter(writer), dataHolder);
	}
	else {
		doMarshal(graph, new CompactWriter(writer), dataHolder);
	}
}
 
Example #25
Source File: JibxMarshaller.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Convert the given {@code JiBXException} to an appropriate exception from the
 * {@code org.springframework.oxm} hierarchy.
 * <p>A boolean flag is used to indicate whether this exception occurs during marshalling or
 * unmarshalling, since JiBX itself does not make this distinction in its exception hierarchy.
 * @param ex {@code JiBXException} that occurred
 * @param marshalling indicates whether the exception occurs during marshalling ({@code true}),
 * or unmarshalling ({@code false})
 * @return the corresponding {@code XmlMappingException}
 */
public XmlMappingException convertJibxException(JiBXException ex, boolean marshalling) {
	if (ex instanceof ValidationException) {
		return new ValidationFailureException("JiBX validation exception", ex);
	}
	else {
		if (marshalling) {
			return new MarshallingFailureException("JiBX marshalling exception", ex);
		}
		else {
			return new UnmarshallingFailureException("JiBX unmarshalling exception", ex);
		}
	}
}
 
Example #26
Source File: CastorMarshaller.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException {
	try {
		return createUnmarshaller().unmarshal(new InputSource(inputStream));
	}
	catch (XMLException ex) {
		throw convertCastorException(ex, false);
	}
}
 
Example #27
Source File: JibxMarshaller.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected void marshalOutputStream(Object graph, OutputStream outputStream)
		throws XmlMappingException, IOException {
	try {
		IMarshallingContext marshallingContext = createMarshallingContext();
		marshallingContext.startDocument(this.encoding, this.standalone, outputStream);
		marshalDocument(marshallingContext, graph);
	}
	catch (JiBXException ex) {
		throw convertJibxException(ex, true);
	}
}
 
Example #28
Source File: JibxMarshaller.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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);
	}
}
 
Example #29
Source File: JibxMarshaller.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException {
	try {
		MarshallingContext marshallingContext = (MarshallingContext) createMarshallingContext();
		IXMLWriter xmlWriter = new StAXWriter(marshallingContext.getNamespaces(), streamWriter);
		marshallingContext.setXmlWriter(xmlWriter);
		marshallingContext.marshalDocument(graph);
	}
	catch (JiBXException ex) {
		throw convertJibxException(ex, false);
	}
}
 
Example #30
Source File: JibxMarshaller.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException {
	try {
		IUnmarshallingContext unmarshallingContext = createUnmarshallingContext();
		return unmarshallingContext.unmarshalDocument(inputStream, this.encoding);
	}
	catch (JiBXException ex) {
		throw convertJibxException(ex, false);
	}
}