Java Code Examples for org.springframework.oxm.XmlMappingException

The following examples show how to use org.springframework.oxm.XmlMappingException. These examples are extracted from open source projects. 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 Project: java-technology-stack   Source File: AbstractMarshaller.java    License: 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 2
Source Project: spring-analysis-note   Source File: AbstractMarshaller.java    License: 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 3
Source Project: spring-analysis-note   Source File: AbstractMarshaller.java    License: 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 4
/**
 * 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 5
Source Project: spring-analysis-note   Source File: Jaxb2Marshaller.java    License: 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 6
Source Project: spring-analysis-note   Source File: Jaxb2Marshaller.java    License: 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 7
Source Project: spring-analysis-note   Source File: MarshallingMessageConverter.java    License: 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 8
Source Project: java-technology-stack   Source File: XStreamMarshaller.java    License: 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 9
Source Project: java-technology-stack   Source File: AbstractMarshaller.java    License: 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 10
Source Project: java-technology-stack   Source File: AbstractMarshaller.java    License: 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 Project: java-technology-stack   Source File: AbstractMarshaller.java    License: 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 12
Source Project: spring-analysis-note   Source File: JibxMarshaller.java    License: 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 13
Source Project: spring-analysis-note   Source File: JibxMarshaller.java    License: MIT License 5 votes vote down vote up
@Override
protected void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException {
	try {
		IMarshallingContext marshallingContext = createMarshallingContext();
		marshallingContext.startDocument(this.encoding, this.standalone, writer);
		marshalDocument(marshallingContext, graph);
	}
	catch (JiBXException ex) {
		throw convertJibxException(ex, true);
	}
}
 
Example 14
Source Project: spring-analysis-note   Source File: JibxMarshaller.java    License: 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 15
Source Project: spring-analysis-note   Source File: JibxMarshaller.java    License: 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 16
Source Project: spring-analysis-note   Source File: JibxMarshaller.java    License: MIT License 5 votes vote down vote up
@Override
protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, @Nullable LexicalHandler lexicalHandler)
		throws XmlMappingException {
	try {
		// JiBX does not support SAX natively, so we write to a buffer first, and transform that to the handlers
		SAXResult saxResult = new SAXResult(contentHandler);
		saxResult.setLexicalHandler(lexicalHandler);
		transformAndMarshal(graph, saxResult);
	}
	catch (IOException ex) {
		throw new MarshallingFailureException("JiBX marshalling exception", ex);
	}
}
 
Example 17
Source Project: spring-analysis-note   Source File: JibxMarshaller.java    License: 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 18
Source Project: spring-analysis-note   Source File: JibxMarshaller.java    License: 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 19
Source Project: java-technology-stack   Source File: XStreamMarshaller.java    License: 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 20
Source Project: java-technology-stack   Source File: CastorMarshaller.java    License: MIT License 5 votes vote down vote up
@Override
protected Object unmarshalReader(Reader reader) throws XmlMappingException, IOException {
	try {
		return createUnmarshaller().unmarshal(new InputSource(reader));
	}
	catch (XMLException ex) {
		throw convertCastorException(ex, false);
	}
}
 
Example 21
Source Project: spring-analysis-note   Source File: AbstractMarshaller.java    License: 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 Project: spring-analysis-note   Source File: XStreamMarshaller.java    License: 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 23
Source Project: java-technology-stack   Source File: XStreamMarshaller.java    License: 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 24
Source Project: spring-analysis-note   Source File: XStreamMarshaller.java    License: 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 25
Source Project: spring-analysis-note   Source File: XStreamMarshaller.java    License: 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 26
Source Project: spring-analysis-note   Source File: XStreamMarshaller.java    License: 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 27
Source Project: spring-analysis-note   Source File: XStreamMarshaller.java    License: 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 28
Source Project: java-technology-stack   Source File: XStreamMarshaller.java    License: 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 29
Source Project: java-technology-stack   Source File: CastorMarshaller.java    License: 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 30
Source Project: java-technology-stack   Source File: JibxMarshaller.java    License: 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);
	}
}