Java Code Examples for org.springframework.oxm.MarshallingFailureException

Following code examples demonstrate how to use org.springframework.oxm.MarshallingFailureExceptionfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.oxm.MarshallingFailureExceptionand various code implementation of this class.

        catch(MarshallingFailureException e) {
            if (e.getMostSpecificCause() instanceof SAXParseException) {
                throw new XmlValideringException("Kunne ikke validere generert Manifest XML. Sjekk at alle pÄkrevde input er satt og ikke er null",
                        SendException.AntattSkyldig.KLIENT, (SAXParseException) e.getMostSpecificCause());
            }

            throw e;
        } 


	public void marshal(Object graph, Result result) throws XmlMappingException, IOException {
		RewardConfirmation rewardConfirmation = (RewardConfirmation) graph;
		Assert.hasText(rewardConfirmation.getDiningTransactionId(),
			"'diningTransactionId' is missing from reward confirmation");
		
		Document doc;
		try {
			doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
		} catch (Throwable e) {
			throw new MarshallingFailureException("Can't create new Document", e);
		}
		Element elConfirmation = (Element) doc.appendChild(doc.createElement("reward-confirmation"));
		elConfirmation.setAttribute("dining-transaction-id", rewardConfirmation.getDiningTransactionId());
		
		if (result instanceof DOMResult) {
			((DOMResult) result).setNode(doc);
		} else {
			throw new IllegalArgumentException("Got instance of "
				+ result.getClass().getSimpleName()
				+ " which is not supported");
		}

	} 

    public Manifest createManifest(JOB job, Sender sender) {
        Object xmlManifest = buildXmlManifest(job, sender);

        try (ByteArrayOutputStream manifestStream = new ByteArrayOutputStream()) {
            marshal(xmlManifest, manifestStream);
            return new Manifest(manifestStream.toByteArray());
        } catch (MarshallingFailureException e) {
            if (e.getMostSpecificCause() instanceof SAXParseException) {
                throw new XmlValidationException("Unable to validate generated Manifest XML. " +
                        "This typically happens if one or more values are not in accordance with the XSD. " +
                        "You may inspect the cause (by calling getCause()) to see which constraint has been violated.", (SAXParseException) e.getMostSpecificCause());
            }
            throw e;
        } catch (IOException e) {
            throw new RuntimeIOException(e);
        }
    } 

	public static 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 {
			return new UncategorizedMappingException("Unknown XStream exception", ex);
		}
	} 

Advertisement
Javadoc
Exception thrown on marshalling failure. @author Arjen Poutsma @since 3.0
Advertisement