Moved Spring-WS to separate dir.
This commit is contained in:
6
oxm/src/etc/test-resources/log4j.properties
Normal file
6
oxm/src/etc/test-resources/log4j.properties
Normal file
@@ -0,0 +1,6 @@
|
||||
log4j.rootCategory=INFO, stdout
|
||||
log4j.logger.org.springframework.oxm=DEBUG
|
||||
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
|
||||
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0"?>
|
||||
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN" "http://castor.org/mapping.dtd">
|
||||
<mapping>
|
||||
<description>Castor generated mapping file</description>
|
||||
<class name="org.springframework.oxm.castor.Flights">
|
||||
<description>
|
||||
Default mapping for class
|
||||
org.springframework.oxm.castor.Flights
|
||||
</description>
|
||||
<map-to xml="flights"
|
||||
ns-uri="http://samples.springframework.org/flight" ns-prefix="tns"/>
|
||||
<field name="flight"
|
||||
type="org.springframework.oxm.castor.Flight"
|
||||
required="true" collection="array">
|
||||
<bind-xml name="tns:flight" node="element" QName-prefix="tns"
|
||||
xmlns:tns="http://samples.springframework.org/flight"/>
|
||||
</field>
|
||||
</class>
|
||||
<class name="org.springframework.oxm.castor.Flight">
|
||||
<description>
|
||||
Default mapping for class
|
||||
org.springframework.oxm.castor.Flight
|
||||
</description>
|
||||
<map-to xml="flight"
|
||||
ns-uri="http://samples.springframework.org/flight" ns-prefix="tns"/>
|
||||
<field name="number" type="long" required="true">
|
||||
<bind-xml name="tns:number" node="element"
|
||||
xmlns:tns="http://samples.springframework.org/flight"/>
|
||||
</field>
|
||||
</class>
|
||||
</mapping>
|
||||
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
|
||||
targetNamespace="http://samples.springframework.org/flight"
|
||||
xmlns:tns="http://samples.springframework.org/flight">
|
||||
<element name="flights">
|
||||
<complexType>
|
||||
<sequence>
|
||||
<element name="flight" type="tns:flightType"
|
||||
maxOccurs="unbounded">
|
||||
</element>
|
||||
</sequence>
|
||||
</complexType>
|
||||
</element>
|
||||
<element name="flight" type="tns:flightType"/>
|
||||
<complexType name="flightType">
|
||||
<sequence>
|
||||
<element name="number" type="long"/>
|
||||
</sequence>
|
||||
</complexType>
|
||||
</schema>
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<binding>
|
||||
<mapping name="flights" class="org.springframework.oxm.jibx.Flights">
|
||||
<namespace uri="http://samples.springframework.org/flight" default="elements"/>
|
||||
<collection field="flightList">
|
||||
<structure map-as="org.springframework.oxm.jibx.FlightType"/>
|
||||
</collection>
|
||||
</mapping>
|
||||
<mapping name="flight" class="org.springframework.oxm.jibx.FlightType">
|
||||
<namespace uri="http://samples.springframework.org/flight" default="elements"/>
|
||||
<value name="number" field="number" usage="required"/>
|
||||
</mapping>
|
||||
</binding>
|
||||
@@ -0,0 +1,504 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
import javax.xml.stream.XMLEventWriter;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.sax.SAXResult;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.xml.transform.StaxResult;
|
||||
import org.springframework.xml.transform.StaxSource;
|
||||
import org.w3c.dom.Node;
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.ext.LexicalHandler;
|
||||
import org.xml.sax.helpers.XMLReaderFactory;
|
||||
|
||||
/**
|
||||
* Abstract implementation of the <code>Marshaller</code> and <code>Unmarshaller</code> interface. This implementation
|
||||
* inspects the given <code>Source</code> or <code>Result</code>, and defers further handling to overridable template
|
||||
* methods.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public abstract class AbstractMarshaller implements Marshaller, Unmarshaller {
|
||||
|
||||
/**
|
||||
* Logger available to subclasses.
|
||||
*/
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private boolean validating = false;
|
||||
|
||||
private boolean namespaceAware = true;
|
||||
|
||||
private DocumentBuilderFactory documentBuilderFactory;
|
||||
|
||||
/**
|
||||
* Set whether or not the XML parser should be XML namespace aware. Default is <code>true</code>.
|
||||
*/
|
||||
public void setNamespaceAware(boolean namespaceAware) {
|
||||
this.namespaceAware = namespaceAware;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if the XML parser should validate the document. Default is <code>false</code>.
|
||||
*/
|
||||
public void setValidating(boolean validating) {
|
||||
this.validating = validating;
|
||||
}
|
||||
|
||||
/**
|
||||
* Marshals the object graph with the given root into the provided <code>javax.xml.transform.Result</code>.
|
||||
* <p/>
|
||||
* This implementation inspects the given result, and calls <code>marshalDomResult</code>,
|
||||
* <code>marshalSaxResult</code>, or <code>marshalStreamResult</code>.
|
||||
*
|
||||
* @param graph the root of the object graph to marshal
|
||||
* @param result the result to marshal to
|
||||
* @throws XmlMappingException if the given object cannot be marshalled to the result
|
||||
* @throws IOException if an I/O exception occurs
|
||||
* @throws IllegalArgumentException if <code>result</code> if neither a <code>DOMResult</code>,
|
||||
* <code>SAXResult</code>, <code>StreamResult</code>
|
||||
* @see #marshalDomResult(Object, javax.xml.transform.dom.DOMResult)
|
||||
* @see #marshalSaxResult(Object, javax.xml.transform.sax.SAXResult)
|
||||
* @see #marshalStreamResult(Object, javax.xml.transform.stream.StreamResult)
|
||||
*/
|
||||
public final void marshal(Object graph, Result result) throws XmlMappingException, IOException {
|
||||
if (result instanceof DOMResult) {
|
||||
marshalDomResult(graph, (DOMResult) result);
|
||||
}
|
||||
else if (result instanceof StaxResult) {
|
||||
marshalStaxResult(graph, (StaxResult) 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());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmarshals the given provided <code>javax.xml.transform.Source</code> into an object graph.
|
||||
* <p/>
|
||||
* This implementation inspects the given result, and calls <code>unmarshalDomSource</code>,
|
||||
* <code>unmarshalSaxSource</code>, or <code>unmarshalStreamSource</code>.
|
||||
*
|
||||
* @param source the source to marshal from
|
||||
* @return the object graph
|
||||
* @throws XmlMappingException if the given source cannot be mapped to an object
|
||||
* @throws IOException if an I/O Exception occurs
|
||||
* @throws IllegalArgumentException if <code>source</code> is neither a <code>DOMSource</code>, a
|
||||
* <code>SAXSource</code>, nor a <code>StreamSource</code>
|
||||
* @see #unmarshalDomSource(javax.xml.transform.dom.DOMSource)
|
||||
* @see #unmarshalSaxSource(javax.xml.transform.sax.SAXSource)
|
||||
* @see #unmarshalStreamSource(javax.xml.transform.stream.StreamSource)
|
||||
*/
|
||||
public final Object unmarshal(Source source) throws XmlMappingException, IOException {
|
||||
if (source instanceof DOMSource) {
|
||||
return unmarshalDomSource((DOMSource) source);
|
||||
}
|
||||
else if (source instanceof StaxSource) {
|
||||
return unmarshalStaxSource((StaxSource) 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());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a <code>DocumentBuilder</code> that this marshaller will use for creating DOM documents when passed an
|
||||
* empty <code>DOMSource</code>. Can be overridden in subclasses, adding further initialization of the builder.
|
||||
*
|
||||
* @param factory the <code>DocumentBuilderFactory</code> that the DocumentBuilder should be created with
|
||||
* @return the <code>DocumentBuilder</code>
|
||||
* @throws javax.xml.parsers.ParserConfigurationException
|
||||
* if thrown by JAXP methods
|
||||
*/
|
||||
protected DocumentBuilder createDocumentBuilder(DocumentBuilderFactory factory)
|
||||
throws ParserConfigurationException {
|
||||
return factory.newDocumentBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a <code>DocumentBuilder</code> that this marshaller will use for creating DOM documents when passed an
|
||||
* empty <code>DOMSource</code>. The resulting <code>DocumentBuilderFactory</code> is cached, so this method will
|
||||
* only be called once.
|
||||
*
|
||||
* @return the DocumentBuilderFactory
|
||||
* @throws ParserConfigurationException if thrown by JAXP methods
|
||||
*/
|
||||
protected DocumentBuilderFactory createDocumentBuilderFactory() throws ParserConfigurationException {
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
factory.setValidating(validating);
|
||||
factory.setNamespaceAware(namespaceAware);
|
||||
return factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a <code>XMLReader</code> that this marshaller will when passed an empty <code>SAXSource</code>.
|
||||
*
|
||||
* @return the XMLReader
|
||||
* @throws SAXException if thrown by JAXP methods
|
||||
*/
|
||||
protected XMLReader createXmlReader() throws SAXException {
|
||||
return XMLReaderFactory.createXMLReader();
|
||||
}
|
||||
|
||||
//
|
||||
// Marshalling
|
||||
//
|
||||
|
||||
/**
|
||||
* Template method for handling <code>DOMResult</code>s. This implementation defers to <code>marshalDomNode</code>.
|
||||
*
|
||||
* @param graph the root of the object graph to marshal
|
||||
* @param domResult the <code>DOMResult</code>
|
||||
* @throws XmlMappingException if the given object cannot be marshalled to the result
|
||||
* @throws IllegalArgumentException if the <code>domResult</code> is empty
|
||||
* @see #marshalDomNode(Object,org.w3c.dom.Node)
|
||||
*/
|
||||
protected void marshalDomResult(Object graph, DOMResult domResult) throws XmlMappingException {
|
||||
Assert.notNull(domResult.getNode(), "DOMResult does not contain Node");
|
||||
marshalDomNode(graph, domResult.getNode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Template method for handling <code>StaxResult</code>s. This implementation defers to
|
||||
* <code>marshalXMLSteamWriter</code>, or <code>marshalXMLEventConsumer</code>, depending on what is contained in
|
||||
* the <code>StaxResult</code>.
|
||||
*
|
||||
* @param graph the root of the object graph to marshal
|
||||
* @param staxResult the <code>StaxResult</code>
|
||||
* @throws XmlMappingException if the given object cannot be marshalled to the result
|
||||
* @throws IllegalArgumentException if the <code>domResult</code> is empty
|
||||
* @see #marshalDomNode(Object,org.w3c.dom.Node)
|
||||
*/
|
||||
protected void marshalStaxResult(Object graph, StaxResult staxResult) throws XmlMappingException {
|
||||
if (staxResult.getXMLStreamWriter() != null) {
|
||||
marshalXmlStreamWriter(graph, staxResult.getXMLStreamWriter());
|
||||
}
|
||||
else if (staxResult.getXMLEventWriter() != null) {
|
||||
marshalXmlEventWriter(graph, staxResult.getXMLEventWriter());
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("StaxResult contains neither XMLStreamWriter nor XMLEventConsumer");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Template method for handling <code>SAXResult</code>s. This implementation defers to
|
||||
* <code>marshalSaxHandlers</code>.
|
||||
*
|
||||
* @param graph the root of the object graph to marshal
|
||||
* @param saxResult the <code>SAXResult</code>
|
||||
* @throws XmlMappingException if the given object cannot be marshalled to the result
|
||||
* @see #marshalSaxHandlers(Object, org.xml.sax.ContentHandler, org.xml.sax.ext.LexicalHandler)
|
||||
*/
|
||||
protected void marshalSaxResult(Object graph, SAXResult saxResult) throws XmlMappingException {
|
||||
ContentHandler contentHandler = saxResult.getHandler();
|
||||
Assert.notNull(contentHandler, "ContentHandler not set on SAXResult");
|
||||
LexicalHandler lexicalHandler = saxResult.getLexicalHandler();
|
||||
marshalSaxHandlers(graph, contentHandler, lexicalHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Template method for handling <code>StreamResult</code>s. This implementation defers to
|
||||
* <code>marshalOutputStream</code>, or <code>marshalWriter</code>, depending on what is contained in the
|
||||
* <code>StreamResult</code>
|
||||
*
|
||||
* @param graph the root of the object graph to marshal
|
||||
* @param streamResult the <code>StreamResult</code>
|
||||
* @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</code> contains neither <code>OutputStream</code> nor
|
||||
* <code>Writer</code>.
|
||||
*/
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Unmarshalling
|
||||
//
|
||||
|
||||
/**
|
||||
* Template method for handling <code>DOMSource</code>s. This implementation defers to
|
||||
* <code>unmarshalDomNode</code>. If the given source is empty, an empty source <code>Document</code> will be
|
||||
* created as a placeholder.
|
||||
*
|
||||
* @param domSource the <code>DOMSource</code>
|
||||
* @return the object graph
|
||||
* @throws IllegalArgumentException if the <code>domSource</code> is empty
|
||||
* @throws XmlMappingException if the given source cannot be mapped to an object
|
||||
* @see #unmarshalDomNode(org.w3c.dom.Node)
|
||||
*/
|
||||
protected Object unmarshalDomSource(DOMSource domSource) throws XmlMappingException {
|
||||
if (domSource.getNode() == null) {
|
||||
try {
|
||||
if (documentBuilderFactory == null) {
|
||||
documentBuilderFactory = createDocumentBuilderFactory();
|
||||
}
|
||||
DocumentBuilder documentBuilder = createDocumentBuilder(documentBuilderFactory);
|
||||
domSource.setNode(documentBuilder.newDocument());
|
||||
}
|
||||
catch (ParserConfigurationException ex) {
|
||||
throw new UnmarshallingFailureException(
|
||||
"Could not create document placeholder for DOMSource: " + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
return unmarshalDomNode(domSource.getNode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Template method for handling <code>StaxSource</code>s. This implementation defers to
|
||||
* <code>unmarshalXmlStreamReader</code>, or <code>unmarshalXmlEventReader</code>.
|
||||
*
|
||||
* @param staxSource the <code>StaxSource</code>
|
||||
* @return the object graph
|
||||
* @throws XmlMappingException if the given source cannot be mapped to an object
|
||||
*/
|
||||
protected Object unmarshalStaxSource(StaxSource staxSource) throws XmlMappingException {
|
||||
if (staxSource.getXMLStreamReader() != null) {
|
||||
return unmarshalXmlStreamReader(staxSource.getXMLStreamReader());
|
||||
}
|
||||
else if (staxSource.getXMLEventReader() != null) {
|
||||
return unmarshalXmlEventReader(staxSource.getXMLEventReader());
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("StaxSource contains neither XMLStreamReader nor XMLEventReader");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Template method for handling <code>SAXSource</code>s. This implementation defers to
|
||||
* <code>unmarshalSaxReader</code>.
|
||||
*
|
||||
* @param saxSource the <code>SAXSource</code>
|
||||
* @return the object graph
|
||||
* @throws XmlMappingException if the given source cannot be mapped to an object
|
||||
* @throws IOException if an I/O Exception occurs
|
||||
* @see #unmarshalSaxReader(org.xml.sax.XMLReader, org.xml.sax.InputSource)
|
||||
*/
|
||||
protected Object unmarshalSaxSource(SAXSource saxSource) throws XmlMappingException, IOException {
|
||||
if (saxSource.getXMLReader() == null) {
|
||||
try {
|
||||
saxSource.setXMLReader(createXmlReader());
|
||||
}
|
||||
catch (SAXException ex) {
|
||||
throw new UnmarshallingFailureException("Could not create XMLReader for SAXSource: " + ex.getMessage(),
|
||||
ex);
|
||||
}
|
||||
}
|
||||
if (saxSource.getInputSource() == null) {
|
||||
saxSource.setInputSource(new InputSource());
|
||||
}
|
||||
return unmarshalSaxReader(saxSource.getXMLReader(), saxSource.getInputSource());
|
||||
}
|
||||
|
||||
/**
|
||||
* Template method for handling <code>StreamSource</code>s. This implementation defers to
|
||||
* <code>unmarshalInputStream</code>, or <code>unmarshalReader</code>.
|
||||
*
|
||||
* @param streamSource the <code>StreamSource</code>
|
||||
* @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) {
|
||||
return unmarshalInputStream(streamSource.getInputStream());
|
||||
}
|
||||
else if (streamSource.getReader() != null) {
|
||||
return unmarshalReader(streamSource.getReader());
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("StreamSource contains neither InputStream nor Reader");
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Abstract template methods
|
||||
//
|
||||
|
||||
/**
|
||||
* Abstract template method for marshalling the given object graph to a DOM <code>Node</code>.
|
||||
* <p/>
|
||||
* In practice, node is be a <code>Document</code> node, a <code>DocumentFragment</code> node, or a
|
||||
* <code>Element</code> node. In other words, a node that accepts children.
|
||||
*
|
||||
* @param graph the root of the object graph to marshal
|
||||
* @param node The DOM node that will contain the result tree
|
||||
* @throws XmlMappingException if the given object cannot be marshalled to the DOM node
|
||||
* @see org.w3c.dom.Document
|
||||
* @see org.w3c.dom.DocumentFragment
|
||||
* @see org.w3c.dom.Element
|
||||
*/
|
||||
protected abstract void marshalDomNode(Object graph, Node node) throws XmlMappingException;
|
||||
|
||||
/**
|
||||
* Abstract template method for marshalling the given object to a StAX <code>XMLEventWriter</code>.
|
||||
*
|
||||
* @param graph the root of the object graph to marshal
|
||||
* @param eventWriter the <code>XMLEventWriter</code> to write to
|
||||
* @throws XmlMappingException if the given object cannot be marshalled to the DOM node
|
||||
*/
|
||||
protected abstract void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) throws XmlMappingException;
|
||||
|
||||
/**
|
||||
* Abstract template method for marshalling the given object to a StAX <code>XMLStreamWriter</code>.
|
||||
*
|
||||
* @param graph the root of the object graph to marshal
|
||||
* @param streamWriter the <code>XMLStreamWriter</code> to write to
|
||||
* @throws XmlMappingException if the given object cannot be marshalled to the DOM node
|
||||
*/
|
||||
protected abstract void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter)
|
||||
throws XmlMappingException;
|
||||
|
||||
/**
|
||||
* Abstract template method for marshalling the given object graph to a <code>OutputStream</code>.
|
||||
*
|
||||
* @param graph the root of the object graph to marshal
|
||||
* @param outputStream the <code>OutputStream</code> to write to
|
||||
* @throws XmlMappingException if the given object cannot be marshalled to the writer
|
||||
* @throws IOException if an I/O exception occurs
|
||||
*/
|
||||
protected abstract void marshalOutputStream(Object graph, OutputStream outputStream)
|
||||
throws XmlMappingException, IOException;
|
||||
|
||||
/**
|
||||
* Abstract template method for marshalling the given object graph to a SAX <code>ContentHandler</code>.
|
||||
*
|
||||
* @param graph the root of the object graph to marshal
|
||||
* @param contentHandler the SAX <code>ContentHandler</code>
|
||||
* @param lexicalHandler the SAX2 <code>LexicalHandler</code>. Can be <code>null</code>.
|
||||
* @throws XmlMappingException if the given object cannot be marshalled to the handlers
|
||||
*/
|
||||
protected abstract void marshalSaxHandlers(Object graph,
|
||||
ContentHandler contentHandler,
|
||||
LexicalHandler lexicalHandler) throws XmlMappingException;
|
||||
|
||||
/**
|
||||
* Abstract template method for marshalling the given object graph to a <code>Writer</code>.
|
||||
*
|
||||
* @param graph the root of the object graph to marshal
|
||||
* @param writer the <code>Writer</code> to write to
|
||||
* @throws XmlMappingException if the given object cannot be marshalled to the writer
|
||||
* @throws IOException if an I/O exception occurs
|
||||
*/
|
||||
protected abstract void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException;
|
||||
|
||||
/**
|
||||
* Abstract template method for unmarshalling from a given DOM <code>Node</code>.
|
||||
*
|
||||
* @param node The DOM node that contains the objects to be unmarshalled
|
||||
* @return the object graph
|
||||
* @throws XmlMappingException if the given DOM node cannot be mapped to an object
|
||||
*/
|
||||
protected abstract Object unmarshalDomNode(Node node) throws XmlMappingException;
|
||||
|
||||
/**
|
||||
* Abstract template method for unmarshalling from a given Stax <code>XMLEventReader</code>.
|
||||
*
|
||||
* @param eventReader The <code>XMLEventReader</code> to read from
|
||||
* @return the object graph
|
||||
* @throws XmlMappingException if the given event reader cannot be converted to an object
|
||||
*/
|
||||
protected abstract Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlMappingException;
|
||||
|
||||
/**
|
||||
* Abstract template method for unmarshalling from a given Stax <code>XMLStreamReader</code>.
|
||||
*
|
||||
* @param streamReader The <code>XMLStreamReader</code> to read from
|
||||
* @return the object graph
|
||||
* @throws XmlMappingException if the given stream reader cannot be converted to an object
|
||||
*/
|
||||
protected abstract Object unmarshalXmlStreamReader(XMLStreamReader streamReader) throws XmlMappingException;
|
||||
|
||||
/**
|
||||
* Abstract template method for unmarshalling from a given <code>InputStream</code>.
|
||||
*
|
||||
* @param inputStream the <code>InputStreamStream</code> to read from
|
||||
* @return the object graph
|
||||
* @throws XmlMappingException if the given stream cannot be converted to an object
|
||||
* @throws IOException if an I/O exception occurs
|
||||
*/
|
||||
protected abstract Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException;
|
||||
|
||||
/**
|
||||
* Abstract template method for unmarshalling from a given <code>Reader</code>.
|
||||
*
|
||||
* @param reader the <code>Reader</code> to read from
|
||||
* @return the object graph
|
||||
* @throws XmlMappingException if the given reader cannot be converted to an object
|
||||
* @throws IOException if an I/O exception occurs
|
||||
*/
|
||||
protected abstract Object unmarshalReader(Reader reader) throws XmlMappingException, IOException;
|
||||
|
||||
/**
|
||||
* Abstract template method for unmarshalling using a given SAX <code>XMLReader</code> and
|
||||
* <code>InputSource</code>.
|
||||
*
|
||||
* @param xmlReader the SAX <code>XMLReader</code> to parse with
|
||||
* @param inputSource the input source to parse from
|
||||
* @return the object graph
|
||||
* @throws XmlMappingException if the given reader and input source cannot be converted to an object
|
||||
*/
|
||||
protected abstract Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource)
|
||||
throws XmlMappingException, IOException;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm;
|
||||
|
||||
|
||||
/**
|
||||
* Base class for exception thrown when a marshalling or unmarshalling error occurs.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see MarshallingFailureException
|
||||
* @see UnmarshallingFailureException
|
||||
*/
|
||||
public abstract class GenericMarshallingFailureException extends XmlMappingException {
|
||||
|
||||
/**
|
||||
* Constructor for <code>GenericMarshallingFailureException</code>.
|
||||
*/
|
||||
public GenericMarshallingFailureException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for <code>GenericMarshallingFailureException</code>.
|
||||
*/
|
||||
public GenericMarshallingFailureException(String msg, Throwable ex) {
|
||||
super(msg, ex);
|
||||
}
|
||||
|
||||
}
|
||||
44
oxm/src/main/java/org/springframework/oxm/Marshaller.java
Normal file
44
oxm/src/main/java/org/springframework/oxm/Marshaller.java
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.xml.transform.Result;
|
||||
|
||||
/**
|
||||
* Defines the contract for Object XML Mapping Marshallers. Implementations of this interface can serialize a given
|
||||
* Object to a XML Stream.
|
||||
* <p>
|
||||
* Although the <code>marshal</code> method accepts a <code>java.lang.Object</code> as its first parameter,
|
||||
* most <code>Marshaller</code> implementations cannot handle arbitrary <code>java.lang.Object</code>. Instead, a
|
||||
* object class must be registered with the marshaller, or have a common base class.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public interface Marshaller {
|
||||
|
||||
/**
|
||||
* Marshals the object graph with the given root into the provided <code>javax.xml.transform.Result</code>.
|
||||
*
|
||||
* @param graph the root of the object graph to marshal
|
||||
* @param result the result to marshal to
|
||||
* @throws XmlMappingException if the given object cannot be marshalled to the result
|
||||
* @throws IOException if an I/O exception occurs
|
||||
*/
|
||||
void marshal(Object graph, Result result) throws XmlMappingException, IOException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm;
|
||||
|
||||
|
||||
/**
|
||||
* Exception thrown on marshalling failure.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class MarshallingFailureException extends GenericMarshallingFailureException {
|
||||
/**
|
||||
* Construct a <code>MarshallingFailureException</code> with the specified detail message.
|
||||
*
|
||||
* @param msg the detail message
|
||||
*/
|
||||
public MarshallingFailureException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a <code>MarshallingFailureException</code> with the specified detail message and nested exception.
|
||||
*
|
||||
* @param msg the detail message
|
||||
* @param ex the nested exception
|
||||
*/
|
||||
public MarshallingFailureException(String msg, Throwable ex) {
|
||||
super(msg, ex);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm;
|
||||
|
||||
|
||||
/**
|
||||
* Superclass for exceptions that cannot be distinguished further.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public abstract class UncategorizedXmlMappingException extends XmlMappingException {
|
||||
|
||||
/**
|
||||
* Constructor for <code>UncategorizedXmlMappingException</code>.
|
||||
*/
|
||||
protected UncategorizedXmlMappingException(String msg, Throwable ex) {
|
||||
super(msg, ex);
|
||||
}
|
||||
}
|
||||
39
oxm/src/main/java/org/springframework/oxm/Unmarshaller.java
Normal file
39
oxm/src/main/java/org/springframework/oxm/Unmarshaller.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.xml.transform.Source;
|
||||
|
||||
/**
|
||||
* Defines the contract for Object XML Mapping unmarshallers. Implementations of this interface can deserialize a given
|
||||
* XML Stream to an Object graph.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public interface Unmarshaller {
|
||||
|
||||
/**
|
||||
* Unmarshals the given provided <code>javax.xml.transform.Source</code> into an object graph.
|
||||
*
|
||||
* @param source the source to marshal from
|
||||
* @return the object graph
|
||||
* @throws XmlMappingException if the given source cannot be mapped to an object
|
||||
* @throws IOException if an I/O Exception occurs
|
||||
*/
|
||||
Object unmarshal(Source source) throws XmlMappingException, IOException;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm;
|
||||
|
||||
/**
|
||||
* Exception thrown on unmarshalling failure.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class UnmarshallingFailureException extends GenericMarshallingFailureException {
|
||||
|
||||
/**
|
||||
* Constructor for <code>UnmarshallingFailureException</code>.
|
||||
*/
|
||||
public UnmarshallingFailureException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for <code>UnmarshallingFailureException</code>.
|
||||
*/
|
||||
public UnmarshallingFailureException(String msg, Throwable ex) {
|
||||
super(msg, ex);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm;
|
||||
|
||||
/**
|
||||
* Exception thrown on marshalling validation failure.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class ValidationFailureException extends XmlMappingException {
|
||||
|
||||
/**
|
||||
* Constructor for <code>ValidationFailureException</code>.
|
||||
*/
|
||||
public ValidationFailureException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for <code>ValidationFailureException</code>.
|
||||
*/
|
||||
public ValidationFailureException(String msg, Throwable ex) {
|
||||
super(msg, ex);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm;
|
||||
|
||||
import org.springframework.core.NestedRuntimeException;
|
||||
|
||||
/**
|
||||
* Root of the hierarchy of Object XML Mapping exceptions.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public abstract class XmlMappingException extends NestedRuntimeException {
|
||||
/**
|
||||
* Constructor for <code>XmlMappingException</code>.
|
||||
*/
|
||||
public XmlMappingException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for <code>XmlMappingException</code>.
|
||||
*/
|
||||
public XmlMappingException(String msg, Throwable ex) {
|
||||
super(msg, ex);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.castor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
import javax.xml.stream.XMLEventWriter;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
|
||||
import org.castor.mapping.BindingType;
|
||||
import org.castor.mapping.MappingUnmarshaller;
|
||||
import org.exolab.castor.mapping.Mapping;
|
||||
import org.exolab.castor.mapping.MappingException;
|
||||
import org.exolab.castor.mapping.MappingLoader;
|
||||
import org.exolab.castor.xml.ClassDescriptorResolverFactory;
|
||||
import org.exolab.castor.xml.MarshalException;
|
||||
import org.exolab.castor.xml.Marshaller;
|
||||
import org.exolab.castor.xml.UnmarshalHandler;
|
||||
import org.exolab.castor.xml.Unmarshaller;
|
||||
import org.exolab.castor.xml.XMLClassDescriptorResolver;
|
||||
import org.exolab.castor.xml.XMLException;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.oxm.AbstractMarshaller;
|
||||
import org.springframework.oxm.XmlMappingException;
|
||||
import org.springframework.xml.stream.StaxEventContentHandler;
|
||||
import org.springframework.xml.stream.StaxEventXmlReader;
|
||||
import org.springframework.xml.stream.StaxStreamContentHandler;
|
||||
import org.springframework.xml.stream.StaxStreamXmlReader;
|
||||
import org.w3c.dom.Node;
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.ext.LexicalHandler;
|
||||
|
||||
/**
|
||||
* Implementation of the <code>Marshaller</code> interface for Castor. By default, Castor does not require any further
|
||||
* configuration, though setting a target class or providing a mapping file can be used to have more control over the
|
||||
* behavior of Castor.
|
||||
* <p/>
|
||||
* If a target class is specified using <code>setTargetClass</code>, the <code>CastorMarshaller</code> can only be used
|
||||
* to unmarshall XML that represents that specific class. If you want to unmarshall multiple classes, you have to
|
||||
* provide a mapping file using <code>setMappingLocation</code>.
|
||||
* <p/>
|
||||
* Due to Castor's API, it is required to set the encoding used for writing to output streams. It defaults to
|
||||
* <code>UTF-8</code>.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see #setEncoding(String)
|
||||
* @see #setTargetClass(Class)
|
||||
* @see #setMappingLocation(org.springframework.core.io.Resource)
|
||||
*/
|
||||
public class CastorMarshaller extends AbstractMarshaller implements InitializingBean {
|
||||
|
||||
/**
|
||||
* The default encoding used for stream access.
|
||||
*/
|
||||
public static final String DEFAULT_ENCODING = "UTF-8";
|
||||
|
||||
private Resource mappingLocation;
|
||||
|
||||
private String encoding;
|
||||
|
||||
private Class targetClass;
|
||||
|
||||
private XMLClassDescriptorResolver classDescriptorResolver;
|
||||
|
||||
/**
|
||||
* Returns the encoding to be used for stream access. If this property is not set, the default encoding is used.
|
||||
*
|
||||
* @see #DEFAULT_ENCODING
|
||||
*/
|
||||
private String getEncoding() {
|
||||
return encoding != null ? encoding : DEFAULT_ENCODING;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the encoding to be used for stream access. If this property is not set, the default encoding is used.
|
||||
*
|
||||
* @see #DEFAULT_ENCODING
|
||||
*/
|
||||
public void setEncoding(String encoding) {
|
||||
this.encoding = encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Castor target class. If this property is set, this <code>CastorMarshaller</code> is tied to this one
|
||||
* specific class. Use a mapping file for unmarshalling multiple classes.
|
||||
* <p/>
|
||||
* You cannot set both this property and the mapping (location).
|
||||
*/
|
||||
public void setTargetClass(Class targetClass) {
|
||||
this.targetClass = targetClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the location of the Castor XML Mapping file.
|
||||
*/
|
||||
public void setMappingLocation(Resource mappingLocation) {
|
||||
this.mappingLocation = mappingLocation;
|
||||
}
|
||||
|
||||
public final void afterPropertiesSet() throws IOException {
|
||||
if (mappingLocation != null && targetClass != null) {
|
||||
throw new IllegalArgumentException("Cannot set both the 'mappingLocation' and 'targetClass' property. " +
|
||||
"Set targetClass for unmarshalling a single class, and 'mappingLocation' for multiple classes'");
|
||||
}
|
||||
if (logger.isInfoEnabled()) {
|
||||
if (mappingLocation != null) {
|
||||
logger.info("Configured using " + mappingLocation);
|
||||
}
|
||||
else if (targetClass != null) {
|
||||
logger.info("Configured for target class [" + targetClass.getName() + "]");
|
||||
}
|
||||
else {
|
||||
logger.info("Using default configuration");
|
||||
}
|
||||
}
|
||||
try {
|
||||
createClassDescriptorResolver();
|
||||
}
|
||||
catch (MappingException ex) {
|
||||
throw new CastorSystemException("Could not load Castor mapping: " + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void createClassDescriptorResolver() throws MappingException, IOException {
|
||||
classDescriptorResolver = (XMLClassDescriptorResolver) ClassDescriptorResolverFactory
|
||||
.createClassDescriptorResolver(BindingType.XML);
|
||||
if (mappingLocation != null) {
|
||||
Mapping mapping = new Mapping();
|
||||
mapping.loadMapping(new InputSource(mappingLocation.getInputStream()));
|
||||
MappingUnmarshaller mappingUnmarshaller = new MappingUnmarshaller();
|
||||
MappingLoader mappingLoader = mappingUnmarshaller.getMappingLoader(mapping, BindingType.XML);
|
||||
classDescriptorResolver.setMappingLoader(mappingLoader);
|
||||
classDescriptorResolver.setClassLoader(mapping.getClassLoader());
|
||||
}
|
||||
else if (targetClass != null) {
|
||||
classDescriptorResolver.setClassLoader(targetClass.getClassLoader());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given <code>CastorException</code> to an appropriate exception from the
|
||||
* <code>org.springframework.oxm</code> hierarchy.
|
||||
* <p/>
|
||||
* The default implementation delegates to <code>CastorUtils</code>. Can be overridden in subclasses.
|
||||
* <p/>
|
||||
* A boolean flag is used to indicate whether this exception occurs during marshalling or unmarshalling, since
|
||||
* Castor itself does not make this distinction in its exception hierarchy.
|
||||
*
|
||||
* @param ex Castor <code>XMLException</code> that occured
|
||||
* @param marshalling indicates whether the exception occurs during marshalling (<code>true</code>), or
|
||||
* unmarshalling (<code>false</code>)
|
||||
* @return the corresponding <code>XmlMappingException</code>
|
||||
* @see CastorUtils#convertXmlException
|
||||
*/
|
||||
public XmlMappingException convertCastorException(XMLException ex, boolean marshalling) {
|
||||
return CastorUtils.convertXmlException(ex, marshalling);
|
||||
}
|
||||
|
||||
private Unmarshaller createUnmarshaller() {
|
||||
Unmarshaller unmarshaller = null;
|
||||
if (targetClass != null) {
|
||||
unmarshaller = new Unmarshaller(targetClass);
|
||||
}
|
||||
else {
|
||||
unmarshaller = new Unmarshaller();
|
||||
}
|
||||
unmarshaller.setResolver(classDescriptorResolver);
|
||||
return unmarshaller;
|
||||
}
|
||||
|
||||
private void marshal(Object graph, Marshaller marshaller) {
|
||||
try {
|
||||
marshaller.setResolver(classDescriptorResolver);
|
||||
marshaller.marshal(graph);
|
||||
}
|
||||
catch (XMLException ex) {
|
||||
throw convertCastorException(ex, true);
|
||||
}
|
||||
}
|
||||
|
||||
protected void marshalDomNode(Object graph, Node node) throws XmlMappingException {
|
||||
Marshaller marshaller = new Marshaller(node);
|
||||
marshal(graph, marshaller);
|
||||
}
|
||||
|
||||
protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) throws XmlMappingException {
|
||||
ContentHandler contentHandler = new StaxEventContentHandler(eventWriter);
|
||||
marshalSaxHandlers(graph, contentHandler, null);
|
||||
}
|
||||
|
||||
protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException {
|
||||
ContentHandler contentHandler = new StaxStreamContentHandler(streamWriter);
|
||||
marshalSaxHandlers(graph, contentHandler, null);
|
||||
}
|
||||
|
||||
protected void marshalOutputStream(Object graph, OutputStream outputStream)
|
||||
throws XmlMappingException, IOException {
|
||||
OutputStreamWriter writer = new OutputStreamWriter(outputStream, getEncoding());
|
||||
marshalWriter(graph, writer);
|
||||
}
|
||||
|
||||
protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler)
|
||||
throws XmlMappingException {
|
||||
try {
|
||||
Marshaller marshaller = new Marshaller(contentHandler);
|
||||
marshal(graph, marshaller);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new CastorSystemException("Could not construct Castor ContentHandler Marshaller", ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException {
|
||||
Marshaller marshaller = new Marshaller(writer);
|
||||
marshal(graph, marshaller);
|
||||
}
|
||||
|
||||
protected Object unmarshalDomNode(Node node) throws XmlMappingException {
|
||||
try {
|
||||
return createUnmarshaller().unmarshal(node);
|
||||
}
|
||||
catch (XMLException ex) {
|
||||
throw convertCastorException(ex, false);
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalXmlEventReader(XMLEventReader eventReader) {
|
||||
XMLReader reader = new StaxEventXmlReader(eventReader);
|
||||
try {
|
||||
return unmarshalSaxReader(reader, new InputSource());
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new CastorUnmarshallingFailureException(new MarshalException(ex));
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) {
|
||||
XMLReader reader = new StaxStreamXmlReader(streamReader);
|
||||
try {
|
||||
return unmarshalSaxReader(reader, new InputSource());
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new CastorUnmarshallingFailureException(new MarshalException(ex));
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource)
|
||||
throws XmlMappingException, IOException {
|
||||
UnmarshalHandler unmarshalHandler = createUnmarshaller().createHandler();
|
||||
try {
|
||||
ContentHandler contentHandler = Unmarshaller.getContentHandler(unmarshalHandler);
|
||||
xmlReader.setContentHandler(contentHandler);
|
||||
xmlReader.parse(inputSource);
|
||||
return unmarshalHandler.getObject();
|
||||
}
|
||||
catch (SAXException ex) {
|
||||
throw new CastorUnmarshallingFailureException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException {
|
||||
try {
|
||||
return createUnmarshaller().unmarshal(new InputSource(inputStream));
|
||||
}
|
||||
catch (XMLException ex) {
|
||||
throw convertCastorException(ex, false);
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalReader(Reader reader) throws XmlMappingException, IOException {
|
||||
try {
|
||||
return createUnmarshaller().unmarshal(new InputSource(reader));
|
||||
}
|
||||
catch (XMLException ex) {
|
||||
throw convertCastorException(ex, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.castor;
|
||||
|
||||
import org.exolab.castor.xml.MarshalException;
|
||||
|
||||
import org.springframework.oxm.MarshallingFailureException;
|
||||
|
||||
/**
|
||||
* Castor-specific subclass of <code>MarshallingFailureException</code>.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see CastorUtils#convertXmlException
|
||||
*/
|
||||
public class CastorMarshallingFailureException extends MarshallingFailureException {
|
||||
|
||||
public CastorMarshallingFailureException(MarshalException ex) {
|
||||
super("Castor marshalling exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.castor;
|
||||
|
||||
import org.springframework.oxm.UncategorizedXmlMappingException;
|
||||
|
||||
/**
|
||||
* Castor-specific subclass of <code>UncategorizedXmlMappingException</code>, for Castor exceptions that cannot be
|
||||
* distinguished further.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class CastorSystemException extends UncategorizedXmlMappingException {
|
||||
|
||||
public CastorSystemException(String msg, Throwable ex) {
|
||||
super(msg, ex);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.castor;
|
||||
|
||||
import org.exolab.castor.xml.MarshalException;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import org.springframework.oxm.UnmarshallingFailureException;
|
||||
|
||||
/**
|
||||
* Castor-specific subclass of <code>UnmarshallingFailureException</code>.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see CastorUtils#convertXmlException
|
||||
*/
|
||||
public class CastorUnmarshallingFailureException extends UnmarshallingFailureException {
|
||||
|
||||
public CastorUnmarshallingFailureException(MarshalException ex) {
|
||||
super("Castor unmarshalling exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
public CastorUnmarshallingFailureException(SAXException ex) {
|
||||
super("Castor unmarshalling exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.castor;
|
||||
|
||||
import org.exolab.castor.xml.MarshalException;
|
||||
import org.exolab.castor.xml.ValidationException;
|
||||
import org.exolab.castor.xml.XMLException;
|
||||
|
||||
import org.springframework.oxm.XmlMappingException;
|
||||
|
||||
/**
|
||||
* Generic utility methods for working with Castor. Mainly for internal use within the framework.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class CastorUtils {
|
||||
|
||||
/**
|
||||
* Converts the given <code>XMLException</code> to an appropriate exception from the
|
||||
* <code>org.springframework.oxm</code> hierarchy.
|
||||
* <p/>
|
||||
* A boolean flag is used to indicate whether this exception occurs during marshalling or unmarshalling, since
|
||||
* Castor itself does not make this distinction in its exception hierarchy.
|
||||
*
|
||||
* @param ex Castor <code>XMLException</code> that occured
|
||||
* @param marshalling indicates whether the exception occurs during marshalling (<code>true</code>), or
|
||||
* unmarshalling (<code>false</code>)
|
||||
* @return the corresponding <code>XmlMappingException</code>
|
||||
*/
|
||||
public static XmlMappingException convertXmlException(XMLException ex, boolean marshalling) {
|
||||
if (ex instanceof MarshalException) {
|
||||
MarshalException marshalException = (MarshalException) ex;
|
||||
if (marshalling) {
|
||||
return new CastorMarshallingFailureException(marshalException);
|
||||
}
|
||||
else {
|
||||
return new CastorUnmarshallingFailureException(marshalException);
|
||||
}
|
||||
}
|
||||
else if (ex instanceof ValidationException) {
|
||||
return new CastorValidationFailureException((ValidationException) ex);
|
||||
}
|
||||
// fallback
|
||||
return new CastorSystemException("Unknown Castor exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.castor;
|
||||
|
||||
import org.exolab.castor.xml.ValidationException;
|
||||
|
||||
import org.springframework.oxm.ValidationFailureException;
|
||||
|
||||
/**
|
||||
* Castor-specific subclass of <code>MarshallingFailureException</code>.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see CastorUtils#convertXmlException
|
||||
*/
|
||||
public class CastorValidationFailureException extends ValidationFailureException {
|
||||
|
||||
public CastorValidationFailureException(ValidationException ex) {
|
||||
super("Castor validation exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<html>
|
||||
<body>
|
||||
Package providing integration of <a href="http://www.castor.org/xml-mapping.html">Castor</a> within Springs O/X Mapping
|
||||
support.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,204 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.jaxb;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import javax.xml.bind.ValidationEventHandler;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.oxm.XmlMappingException;
|
||||
|
||||
/**
|
||||
* Abstract base class for implementations of the <code>Marshaller</code> and <code>Unmarshaller</code> interfaces that
|
||||
* use JAXB. This base class is responsible for creating JAXB marshallers from a <code>JAXBContext</code>.
|
||||
* <p/>
|
||||
* JAXB 2.0 added breaking API changes, so specific subclasses must be used for JAXB 1.0 and 2.0
|
||||
* (<code>Jaxb1Marshaller</code> and <code>Jaxb2Marshaller</code> respectivaly).
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see Jaxb1Marshaller
|
||||
* @see Jaxb2Marshaller
|
||||
*/
|
||||
public abstract class AbstractJaxbMarshaller
|
||||
implements org.springframework.oxm.Marshaller, org.springframework.oxm.Unmarshaller, InitializingBean {
|
||||
|
||||
/**
|
||||
* Logger available to subclasses.
|
||||
*/
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private String contextPath;
|
||||
|
||||
private Map marshallerProperties;
|
||||
|
||||
private Map unmarshallerProperties;
|
||||
|
||||
private JAXBContext jaxbContext;
|
||||
|
||||
private ValidationEventHandler validationEventHandler;
|
||||
|
||||
/**
|
||||
* Returns the JAXB Context path.
|
||||
*/
|
||||
protected String getContextPath() {
|
||||
return contextPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the JAXB Context path.
|
||||
*/
|
||||
public void setContextPath(String contextPath) {
|
||||
this.contextPath = contextPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the JAXB <code>Marshaller</code> properties. These properties will be set on the underlying JAXB
|
||||
* <code>Marshaller</code>, and allow for features such as indentation.
|
||||
*
|
||||
* @param properties the properties
|
||||
* @see javax.xml.bind.Marshaller#setProperty(String, Object)
|
||||
* @see javax.xml.bind.Marshaller#JAXB_ENCODING
|
||||
* @see javax.xml.bind.Marshaller#JAXB_FORMATTED_OUTPUT
|
||||
* @see javax.xml.bind.Marshaller#JAXB_NO_NAMESPACE_SCHEMA_LOCATION
|
||||
* @see javax.xml.bind.Marshaller#JAXB_SCHEMA_LOCATION
|
||||
*/
|
||||
public void setMarshallerProperties(Map properties) {
|
||||
this.marshallerProperties = properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the JAXB <code>Unmarshaller</code> properties. These properties will be set on the underlying JAXB
|
||||
* <code>Unmarshaller</code>.
|
||||
*
|
||||
* @param properties the properties
|
||||
* @see javax.xml.bind.Unmarshaller#setProperty(String, Object)
|
||||
*/
|
||||
public void setUnmarshallerProperties(Map properties) {
|
||||
this.unmarshallerProperties = properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the JAXB validation event handler. This event handler will be called by JAXB if any validation errors are
|
||||
* encountered during calls to any of the marshal API's.
|
||||
*
|
||||
* @param validationEventHandler the event handler
|
||||
*/
|
||||
public void setValidationEventHandler(ValidationEventHandler validationEventHandler) {
|
||||
this.validationEventHandler = validationEventHandler;
|
||||
}
|
||||
|
||||
public final void afterPropertiesSet() throws Exception {
|
||||
try {
|
||||
jaxbContext = createJaxbContext();
|
||||
}
|
||||
catch (JAXBException ex) {
|
||||
throw convertJaxbException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the given <code>JAXBException</code> to an appropriate exception from the
|
||||
* <code>org.springframework.oxm</code> hierarchy.
|
||||
* <p/>
|
||||
* The default implementation delegates to <code>JaxbUtils</code>. Can be overridden in subclasses.
|
||||
*
|
||||
* @param ex <code>JAXBException</code> that occured
|
||||
* @return the corresponding <code>XmlMappingException</code> instance
|
||||
* @see JaxbUtils#convertJaxbException
|
||||
*/
|
||||
protected XmlMappingException convertJaxbException(JAXBException ex) {
|
||||
return JaxbUtils.convertJaxbException(ex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a newly created JAXB marshaller. JAXB marshallers are not necessarily thread safe.
|
||||
*/
|
||||
protected Marshaller createMarshaller() {
|
||||
try {
|
||||
Marshaller marshaller = jaxbContext.createMarshaller();
|
||||
if (marshallerProperties != null) {
|
||||
for (Iterator iterator = marshallerProperties.keySet().iterator(); iterator.hasNext();) {
|
||||
String name = (String) iterator.next();
|
||||
marshaller.setProperty(name, marshallerProperties.get(name));
|
||||
}
|
||||
}
|
||||
if (validationEventHandler != null) {
|
||||
marshaller.setEventHandler(validationEventHandler);
|
||||
}
|
||||
initJaxbMarshaller(marshaller);
|
||||
return marshaller;
|
||||
}
|
||||
catch (JAXBException ex) {
|
||||
throw convertJaxbException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a newly created JAXB unmarshaller. JAXB unmarshallers are not necessarily thread safe.
|
||||
*/
|
||||
protected Unmarshaller createUnmarshaller() {
|
||||
try {
|
||||
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
|
||||
if (unmarshallerProperties != null) {
|
||||
for (Iterator iterator = unmarshallerProperties.keySet().iterator(); iterator.hasNext();) {
|
||||
String name = (String) iterator.next();
|
||||
unmarshaller.setProperty(name, unmarshallerProperties.get(name));
|
||||
}
|
||||
}
|
||||
if (validationEventHandler != null) {
|
||||
unmarshaller.setEventHandler(validationEventHandler);
|
||||
}
|
||||
initJaxbUnmarshaller(unmarshaller);
|
||||
return unmarshaller;
|
||||
}
|
||||
catch (JAXBException ex) {
|
||||
throw convertJaxbException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior. Gets
|
||||
* called after creation of JAXB <code>Marshaller</code>, and after the respective properties have been set.
|
||||
* <p/>
|
||||
* Default implementation does nothing.
|
||||
*/
|
||||
protected void initJaxbMarshaller(Marshaller marshaller) throws JAXBException {
|
||||
}
|
||||
|
||||
/**
|
||||
* Template method that can overridden by concrete JAXB marshallers for custom initialization behavior. Gets called
|
||||
* after creation of JAXB <code>Unmarshaller</code>, and after the respective properties have been set.
|
||||
* <p/>
|
||||
* Default implementation does nothing.
|
||||
*/
|
||||
protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBException {
|
||||
}
|
||||
|
||||
/**
|
||||
* Template method that returns a newly created JAXB context. Called from <code>afterPropertiesSet()</code>.
|
||||
*/
|
||||
protected abstract JAXBContext createJaxbContext() throws Exception;
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.jaxb;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.Source;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Implementation of the <code>Marshaller</code> interface for JAXB 1.0.
|
||||
* <p/>
|
||||
* The typical usage will be to set the <code>contextPath</code> property on this bean, possibly customize the
|
||||
* marshaller and unmarshaller by setting properties, and validations, and to refer to it.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see #setContextPath(String)
|
||||
* @see #setMarshallerProperties(java.util.Map)
|
||||
* @see #setUnmarshallerProperties(java.util.Map)
|
||||
* @see #setValidating(boolean)
|
||||
*/
|
||||
public class Jaxb1Marshaller extends AbstractJaxbMarshaller implements InitializingBean {
|
||||
|
||||
private boolean validating = false;
|
||||
|
||||
/**
|
||||
* Set if the JAXB <code>Unmarshaller</code> should validate the incoming document. Default is <code>false</code>.
|
||||
*/
|
||||
public void setValidating(boolean validating) {
|
||||
this.validating = validating;
|
||||
}
|
||||
|
||||
protected final JAXBContext createJaxbContext() throws JAXBException {
|
||||
if (!StringUtils.hasLength(getContextPath())) {
|
||||
throw new IllegalArgumentException("contextPath is required");
|
||||
}
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("Creating JAXBContext with context path [" + getContextPath() + "]");
|
||||
}
|
||||
return JAXBContext.newInstance(getContextPath());
|
||||
}
|
||||
|
||||
protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBException {
|
||||
unmarshaller.setValidating(validating);
|
||||
}
|
||||
|
||||
public void marshal(Object graph, Result result) {
|
||||
try {
|
||||
createMarshaller().marshal(graph, result);
|
||||
}
|
||||
catch (JAXBException ex) {
|
||||
throw convertJaxbException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unmarshal(Source source) {
|
||||
try {
|
||||
return createUnmarshaller().unmarshal(source);
|
||||
}
|
||||
catch (JAXBException ex) {
|
||||
throw convertJaxbException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.jaxb;
|
||||
|
||||
import javax.xml.bind.MarshalException;
|
||||
|
||||
import org.springframework.oxm.MarshallingFailureException;
|
||||
|
||||
/**
|
||||
* JAXB-specific subclass of <code>MarshallingFailureException</code>.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see JaxbUtils#convertJaxbException
|
||||
*/
|
||||
public class JaxbMarshallingFailureException extends MarshallingFailureException {
|
||||
|
||||
public JaxbMarshallingFailureException(MarshalException ex) {
|
||||
super("JAXB marshalling exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.jaxb;
|
||||
|
||||
import javax.xml.bind.JAXBException;
|
||||
|
||||
import org.springframework.oxm.UncategorizedXmlMappingException;
|
||||
|
||||
/**
|
||||
* JAXB-specific subclass of <code>UncategorizedXmlMappingException</code>, for <code>JAXBException</code>s that cannot
|
||||
* be distinguished further.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see JaxbUtils#convertJaxbException(javax.xml.bind.JAXBException)
|
||||
*/
|
||||
public class JaxbSystemException extends UncategorizedXmlMappingException {
|
||||
|
||||
public JaxbSystemException(JAXBException ex) {
|
||||
super(ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.jaxb;
|
||||
|
||||
import javax.xml.bind.UnmarshalException;
|
||||
|
||||
import org.springframework.oxm.UnmarshallingFailureException;
|
||||
|
||||
/**
|
||||
* JAXB-specific subclass of <code>UnmarshallingFailureException</code>.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class JaxbUnmarshallingFailureException extends UnmarshallingFailureException {
|
||||
|
||||
public JaxbUnmarshallingFailureException(UnmarshalException ex) {
|
||||
super("JAXB unmarshalling exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.jaxb;
|
||||
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.MarshalException;
|
||||
import javax.xml.bind.UnmarshalException;
|
||||
import javax.xml.bind.ValidationException;
|
||||
|
||||
import org.springframework.oxm.XmlMappingException;
|
||||
|
||||
/**
|
||||
* Generic utility methods for working with JAXB. Mainly for internal use within the framework.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public abstract class JaxbUtils {
|
||||
|
||||
public static final int JAXB_1 = 0;
|
||||
|
||||
public static final int JAXB_2 = 1;
|
||||
|
||||
private static final String JAXB_2_CLASS_NAME = "javax.xml.bind.Binder";
|
||||
|
||||
private static int jaxbVersion = JAXB_1;
|
||||
|
||||
static {
|
||||
try {
|
||||
Class.forName(JAXB_2_CLASS_NAME);
|
||||
jaxbVersion = JAXB_2;
|
||||
}
|
||||
catch (ClassNotFoundException ex1) {
|
||||
// leave JAXB 1 as default
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the major JAXB version. This means we can do things like if <code>(getJaxbVersion() <= JAXB_2)</code>.
|
||||
*
|
||||
* @return a code comparable to the JAXP_XX codes in this class
|
||||
* @see #JAXB_1
|
||||
* @see #JAXB_2
|
||||
*/
|
||||
public static int getJaxbVersion() {
|
||||
return jaxbVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given <code>JAXBException</code> to an appropriate exception from the
|
||||
* <code>org.springframework.oxm</code> hierarchy.
|
||||
*
|
||||
* @param ex <code>JAXBException</code> that occured
|
||||
* @return the corresponding <code>XmlMappingException</code>
|
||||
*/
|
||||
public static XmlMappingException convertJaxbException(JAXBException ex) {
|
||||
if (ex instanceof MarshalException) {
|
||||
return new JaxbMarshallingFailureException((MarshalException) ex);
|
||||
}
|
||||
else if (ex instanceof UnmarshalException) {
|
||||
return new JaxbUnmarshallingFailureException((UnmarshalException) ex);
|
||||
}
|
||||
else if (ex instanceof ValidationException) {
|
||||
return new JaxbValidationFailureException((ValidationException) ex);
|
||||
}
|
||||
// fallback
|
||||
return new JaxbSystemException(ex);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.jaxb;
|
||||
|
||||
import javax.xml.bind.ValidationException;
|
||||
|
||||
import org.springframework.oxm.ValidationFailureException;
|
||||
|
||||
/**
|
||||
* JAXB-specific subclass of <code>ValidationFailureException</code>.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see JaxbUtils#convertJaxbException
|
||||
*/
|
||||
public class JaxbValidationFailureException extends ValidationFailureException {
|
||||
|
||||
public JaxbValidationFailureException(ValidationException ex) {
|
||||
super("JAXB validation exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<html>
|
||||
<body>
|
||||
Package providing integration of <a href="http://java.sun.com/webservices/jaxb/">JAXB</a> with Springs O/X Mapping
|
||||
support.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,289 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.jibx;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
import javax.xml.stream.XMLEventWriter;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.sax.SAXResult;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.jibx.runtime.BindingDirectory;
|
||||
import org.jibx.runtime.IBindingFactory;
|
||||
import org.jibx.runtime.IMarshallingContext;
|
||||
import org.jibx.runtime.IUnmarshallingContext;
|
||||
import org.jibx.runtime.IXMLReader;
|
||||
import org.jibx.runtime.IXMLWriter;
|
||||
import org.jibx.runtime.JiBXException;
|
||||
import org.jibx.runtime.impl.MarshallingContext;
|
||||
import org.jibx.runtime.impl.StAXReaderWrapper;
|
||||
import org.jibx.runtime.impl.StAXWriter;
|
||||
import org.jibx.runtime.impl.UnmarshallingContext;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.oxm.AbstractMarshaller;
|
||||
import org.springframework.oxm.XmlMappingException;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.xml.stream.StaxEventContentHandler;
|
||||
import org.springframework.xml.stream.XmlEventStreamReader;
|
||||
import org.w3c.dom.Node;
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.ext.LexicalHandler;
|
||||
|
||||
/**
|
||||
* Implementation of the <code>Marshaller</code> and <code>Unmarshaller</code> interfaces for JiBX.
|
||||
* <p/>
|
||||
* The typical usage will be to set the <code>targetClass</code> and optionally the <code>bindingName</code> property on
|
||||
* this bean, and to refer to it.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see org.jibx.runtime.IMarshallingContext
|
||||
* @see org.jibx.runtime.IUnmarshallingContext
|
||||
*/
|
||||
public class JibxMarshaller extends AbstractMarshaller implements InitializingBean {
|
||||
|
||||
private Class targetClass;
|
||||
|
||||
private String bindingName;
|
||||
|
||||
private IBindingFactory bindingFactory;
|
||||
|
||||
private TransformerFactory transfomerFactory;
|
||||
|
||||
/**
|
||||
* Sets the optional binding name for this instance.
|
||||
*/
|
||||
public void setBindingName(String bindingName) {
|
||||
this.bindingName = bindingName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the target class for this instance. This property is required.
|
||||
*/
|
||||
public void setTargetClass(Class targetClass) {
|
||||
this.targetClass = targetClass;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.notNull(targetClass, "targetClass is required");
|
||||
if (logger.isInfoEnabled()) {
|
||||
if (StringUtils.hasLength(bindingName)) {
|
||||
logger.info("Configured for target class [" + targetClass + "] using binding [" + bindingName + "]");
|
||||
}
|
||||
else {
|
||||
logger.info("Configured for target class [" + targetClass + "]");
|
||||
}
|
||||
}
|
||||
try {
|
||||
if (StringUtils.hasLength(bindingName)) {
|
||||
bindingFactory = BindingDirectory.getFactory(bindingName, targetClass);
|
||||
}
|
||||
else {
|
||||
bindingFactory = BindingDirectory.getFactory(targetClass);
|
||||
}
|
||||
}
|
||||
catch (JiBXException ex) {
|
||||
throw new JibxSystemException(ex);
|
||||
}
|
||||
transfomerFactory = TransformerFactory.newInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the given <code>JiBXException</code> to an appropriate exception from the
|
||||
* <code>org.springframework.oxm</code> hierarchy.
|
||||
* <p/>
|
||||
* The default implementation delegates to <code>JibxUtils</code>. Can be overridden in subclasses.
|
||||
* <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</code> that occured
|
||||
* @param marshalling indicates whether the exception occurs during marshalling (<code>true</code>), or
|
||||
* unmarshalling (<code>false</code>)
|
||||
* @return the corresponding <code>XmlMappingException</code> instance
|
||||
* @see JibxUtils#convertJibxException(org.jibx.runtime.JiBXException, boolean)
|
||||
*/
|
||||
public XmlMappingException convertJibxException(JiBXException ex, boolean marshalling) {
|
||||
return JibxUtils.convertJibxException(ex, marshalling);
|
||||
}
|
||||
|
||||
protected void marshalDomNode(Object graph, Node node) throws XmlMappingException {
|
||||
try {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
marshalOutputStream(graph, os);
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||
Transformer transformer = transfomerFactory.newTransformer();
|
||||
transformer.transform(new StreamSource(is), new DOMResult(node));
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new JibxSystemException(ex);
|
||||
}
|
||||
catch (TransformerException ex) {
|
||||
throw new JibxSystemException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected void marshalOutputStream(Object graph, OutputStream outputStream)
|
||||
throws XmlMappingException, IOException {
|
||||
try {
|
||||
IMarshallingContext marshallingContext = bindingFactory.createMarshallingContext();
|
||||
marshallingContext.marshalDocument(graph, null, null, outputStream);
|
||||
}
|
||||
catch (JiBXException ex) {
|
||||
throw convertJibxException(ex, true);
|
||||
}
|
||||
}
|
||||
|
||||
protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler)
|
||||
throws XmlMappingException {
|
||||
try {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
marshalOutputStream(graph, os);
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||
Transformer transformer = transfomerFactory.newTransformer();
|
||||
transformer.transform(new StreamSource(is), new SAXResult(contentHandler));
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new JibxSystemException(ex);
|
||||
}
|
||||
catch (TransformerException ex) {
|
||||
throw new JibxSystemException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException {
|
||||
try {
|
||||
IMarshallingContext marshallingContext = bindingFactory.createMarshallingContext();
|
||||
marshallingContext.marshalDocument(graph, null, null, writer);
|
||||
}
|
||||
catch (JiBXException ex) {
|
||||
throw convertJibxException(ex, true);
|
||||
}
|
||||
}
|
||||
|
||||
protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) {
|
||||
|
||||
ContentHandler contentHandler = new StaxEventContentHandler(eventWriter);
|
||||
marshalSaxHandlers(graph, contentHandler, null);
|
||||
}
|
||||
|
||||
protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException {
|
||||
try {
|
||||
MarshallingContext marshallingContext = (MarshallingContext) bindingFactory.createMarshallingContext();
|
||||
IXMLWriter xmlWriter = new StAXWriter(marshallingContext.getNamespaces(), streamWriter);
|
||||
marshallingContext.setXmlWriter(xmlWriter);
|
||||
marshallingContext.marshalDocument(graph);
|
||||
}
|
||||
catch (JiBXException ex) {
|
||||
throw convertJibxException(ex, false);
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalDomNode(Node node) throws XmlMappingException {
|
||||
try {
|
||||
Transformer transformer = transfomerFactory.newTransformer();
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
transformer.transform(new DOMSource(node), new StreamResult(os));
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||
return unmarshalInputStream(is);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new JibxSystemException(ex);
|
||||
}
|
||||
catch (TransformerException ex) {
|
||||
throw new JibxSystemException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException {
|
||||
try {
|
||||
IUnmarshallingContext unmarshallingContext = bindingFactory.createUnmarshallingContext();
|
||||
return unmarshallingContext.unmarshalDocument(inputStream, null);
|
||||
}
|
||||
catch (JiBXException ex) {
|
||||
throw convertJibxException(ex, false);
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalReader(Reader reader) throws XmlMappingException, IOException {
|
||||
try {
|
||||
IUnmarshallingContext unmarshallingContext = bindingFactory.createUnmarshallingContext();
|
||||
return unmarshallingContext.unmarshalDocument(reader);
|
||||
}
|
||||
catch (JiBXException ex) {
|
||||
throw convertJibxException(ex, false);
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource)
|
||||
throws XmlMappingException, IOException {
|
||||
try {
|
||||
Transformer transformer = transfomerFactory.newTransformer();
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
transformer.transform(new SAXSource(xmlReader, inputSource), new StreamResult(os));
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||
return unmarshalInputStream(is);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new JibxSystemException(ex);
|
||||
}
|
||||
catch (TransformerException ex) {
|
||||
throw new JibxSystemException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalXmlEventReader(XMLEventReader eventReader) {
|
||||
try {
|
||||
XMLStreamReader streamReader = new XmlEventStreamReader(eventReader);
|
||||
return unmarshalXmlStreamReader(streamReader);
|
||||
}
|
||||
catch (XMLStreamException ex) {
|
||||
throw new JibxSystemException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) {
|
||||
try {
|
||||
UnmarshallingContext unmarshallingContext =
|
||||
(UnmarshallingContext) bindingFactory.createUnmarshallingContext();
|
||||
IXMLReader xmlReader = new StAXReaderWrapper(streamReader, null, true);
|
||||
unmarshallingContext.setDocument(xmlReader);
|
||||
return unmarshallingContext.unmarshalElement();
|
||||
}
|
||||
catch (JiBXException ex) {
|
||||
throw convertJibxException(ex, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.jibx;
|
||||
|
||||
import org.jibx.runtime.JiBXException;
|
||||
|
||||
import org.springframework.oxm.MarshallingFailureException;
|
||||
|
||||
/**
|
||||
* JiXB-specific subclass of <code>MarshallingFailureException</code>.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see JibxUtils#convertJibxException(org.jibx.runtime.JiBXException, boolean)
|
||||
*/
|
||||
public class JibxMarshallingFailureException extends MarshallingFailureException {
|
||||
|
||||
public JibxMarshallingFailureException(JiBXException ex) {
|
||||
super("JiBX marshalling exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.jibx;
|
||||
|
||||
import org.springframework.oxm.UncategorizedXmlMappingException;
|
||||
|
||||
/**
|
||||
* JiBX-specific subclass of <code>UncategorizedXmlMappingException</code>, for <code>JiBXBException</code>s that cannot
|
||||
* be distinguished further.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see JibxUtils#convertJibxException(org.jibx.runtime.JiBXException, boolean)
|
||||
*/
|
||||
public class JibxSystemException extends UncategorizedXmlMappingException {
|
||||
|
||||
public JibxSystemException(Exception ex) {
|
||||
super(ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.jibx;
|
||||
|
||||
import org.jibx.runtime.JiBXException;
|
||||
|
||||
import org.springframework.oxm.UnmarshallingFailureException;
|
||||
|
||||
/**
|
||||
* JiXB-specific subclass of <code>UnmarshallingFailureException</code>.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see JibxUtils#convertJibxException(org.jibx.runtime.JiBXException, boolean)
|
||||
*/
|
||||
public class JibxUnmarshallingFailureException extends UnmarshallingFailureException {
|
||||
|
||||
public JibxUnmarshallingFailureException(JiBXException ex) {
|
||||
super("JiBX unmarshalling exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.jibx;
|
||||
|
||||
import org.jibx.runtime.JiBXException;
|
||||
import org.jibx.runtime.ValidationException;
|
||||
|
||||
import org.springframework.oxm.XmlMappingException;
|
||||
|
||||
/**
|
||||
* Generic utility methods for working with JiBX. Mainly for internal use within the framework.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public abstract class JibxUtils {
|
||||
|
||||
/**
|
||||
* Converts the given <code>JiBXException</code> to an appropriate exception from the
|
||||
* <code>org.springframework.oxm</code> 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</code> that occured
|
||||
* @param marshalling indicates whether the exception occurs during marshalling (<code>true</code>), or
|
||||
* unmarshalling (<code>false</code>)
|
||||
* @return the corresponding <code>XmlMappingException</code>
|
||||
*/
|
||||
public static XmlMappingException convertJibxException(JiBXException ex, boolean marshalling) {
|
||||
if (ex instanceof ValidationException) {
|
||||
return new JibxValidationFailureException((ValidationException) ex);
|
||||
}
|
||||
else {
|
||||
if (marshalling) {
|
||||
return new JibxMarshallingFailureException(ex);
|
||||
}
|
||||
else {
|
||||
return new JibxUnmarshallingFailureException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.jibx;
|
||||
|
||||
import org.jibx.runtime.ValidationException;
|
||||
|
||||
import org.springframework.oxm.ValidationFailureException;
|
||||
|
||||
/**
|
||||
* JAXB-specific subclass of <code>ValidationFailureException</code>.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see JibxUtils#convertJibxException(org.jibx.runtime.JiBXException, boolean)
|
||||
*/
|
||||
public class JibxValidationFailureException extends ValidationFailureException {
|
||||
|
||||
public JibxValidationFailureException(ValidationException ex) {
|
||||
super("JiBX validation exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<html>
|
||||
<body>
|
||||
Package providing integration of <a href="http://jibx.sourceforge.net/">JiBX</a> with Springs O/X Mapping
|
||||
support.
|
||||
</body>
|
||||
</html>
|
||||
6
oxm/src/main/java/org/springframework/oxm/package.html
Normal file
6
oxm/src/main/java/org/springframework/oxm/package.html
Normal file
@@ -0,0 +1,6 @@
|
||||
<html>
|
||||
<body>
|
||||
Root package for Spring's O/X Mapping integration classes. Contains generic Marshaller and Unmarshaller interfaces,
|
||||
and XmlMappingExceptions related to O/X Mapping.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,243 @@
|
||||
package org.springframework.oxm.xmlbeans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
import javax.xml.stream.XMLEventWriter;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
|
||||
import org.apache.xmlbeans.XmlError;
|
||||
import org.apache.xmlbeans.XmlException;
|
||||
import org.apache.xmlbeans.XmlObject;
|
||||
import org.apache.xmlbeans.XmlOptions;
|
||||
import org.apache.xmlbeans.XmlSaxHandler;
|
||||
import org.apache.xmlbeans.XmlValidationError;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.SAXNotRecognizedException;
|
||||
import org.xml.sax.SAXNotSupportedException;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.ext.LexicalHandler;
|
||||
|
||||
import org.springframework.oxm.AbstractMarshaller;
|
||||
import org.springframework.oxm.XmlMappingException;
|
||||
import org.springframework.xml.stream.StaxEventContentHandler;
|
||||
import org.springframework.xml.stream.StaxEventXmlReader;
|
||||
import org.springframework.xml.stream.StaxStreamContentHandler;
|
||||
|
||||
/**
|
||||
* Implementation of the <code>Marshaller</code> interface for XMLBeans. Further options can be set by setting the
|
||||
* <code>xmlOptions</code> property. A <code>XmlOptionsFactoryBean</code> is provided to easily wire up
|
||||
* <code>XmlObjects</code> instances.
|
||||
* <p/>
|
||||
* Unmarshalled objects can be validated by setting the <code>validating</code> property, or by calling the
|
||||
* <code>validiate()</code> method directly. Invalid objects will result in an <code>XmlBeansValidationFailureException</code>.
|
||||
* <p/>
|
||||
* <strong>Note</strong> that due to the nature of XMLBeans, this marshaller requires all passed objects to be of type
|
||||
* <code>XmlObject</code>.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see #setXmlOptions(org.apache.xmlbeans.XmlOptions)
|
||||
* @see XmlOptionsFactoryBean
|
||||
* @see #setValidating(boolean)
|
||||
* @see org.apache.xmlbeans.XmlObject
|
||||
*/
|
||||
public class XmlBeansMarshaller extends AbstractMarshaller {
|
||||
|
||||
private XmlOptions xmlOptions;
|
||||
|
||||
private boolean validating = false;
|
||||
|
||||
/**
|
||||
* Set if this marshaller should validate the incoming document. Default is <code>false</code>.
|
||||
*/
|
||||
public void setValidating(boolean validating) {
|
||||
this.validating = validating;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the <code>XmlOptions</code>.
|
||||
*
|
||||
* @param xmlOptions the xml options
|
||||
*/
|
||||
public void setXmlOptions(XmlOptions xmlOptions) {
|
||||
this.xmlOptions = xmlOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given XMLBeans exception to an appropriate exception from the <code>org.springframework.oxm</code>
|
||||
* hierarchy.
|
||||
* <p/>
|
||||
* The default implementation delegates to <code>XmlBeansUtils</code>. Can be overridden in subclasses.
|
||||
* <p/>
|
||||
* A boolean flag is used to indicate whether this exception occurs during marshalling or unmarshalling, since
|
||||
* XMLBeans itself does not make this distinction in its exception hierarchy.
|
||||
*
|
||||
* @param ex XMLBeans Exception that occured
|
||||
* @param marshalling indicates whether the exception occurs during marshalling (<code>true</code>), or
|
||||
* unmarshalling (<code>false</code>)
|
||||
* @return the corresponding <code>XmlMappingException</code>
|
||||
* @see XmlBeansUtils#convertXmlBeansException(Exception, boolean)
|
||||
*/
|
||||
public XmlMappingException convertXmlBeansException(Exception ex, boolean marshalling) {
|
||||
return XmlBeansUtils.convertXmlBeansException(ex, marshalling);
|
||||
}
|
||||
|
||||
protected void marshalDomNode(Object graph, Node node) throws XmlMappingException {
|
||||
Document document = node.getNodeType() == Node.DOCUMENT_NODE ? (Document) node : node.getOwnerDocument();
|
||||
Node xmlBeansNode = ((XmlObject) graph).newDomNode(xmlOptions);
|
||||
NodeList xmlBeansChildNodes = xmlBeansNode.getChildNodes();
|
||||
for (int i = 0; i < xmlBeansChildNodes.getLength(); i++) {
|
||||
Node xmlBeansChildNode = xmlBeansChildNodes.item(i);
|
||||
Node importedNode = document.importNode(xmlBeansChildNode, true);
|
||||
node.appendChild(importedNode);
|
||||
}
|
||||
}
|
||||
|
||||
protected void marshalOutputStream(Object graph, OutputStream outputStream)
|
||||
throws XmlMappingException, IOException {
|
||||
((XmlObject) graph).save(outputStream, xmlOptions);
|
||||
}
|
||||
|
||||
protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler)
|
||||
throws XmlMappingException {
|
||||
try {
|
||||
((XmlObject) graph).save(contentHandler, lexicalHandler, xmlOptions);
|
||||
}
|
||||
catch (SAXException ex) {
|
||||
throw convertXmlBeansException(ex, true);
|
||||
}
|
||||
}
|
||||
|
||||
protected void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException {
|
||||
((XmlObject) graph).save(writer, xmlOptions);
|
||||
}
|
||||
|
||||
protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) {
|
||||
ContentHandler contentHandler = new StaxEventContentHandler(eventWriter);
|
||||
marshalSaxHandlers(graph, contentHandler, null);
|
||||
}
|
||||
|
||||
protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException {
|
||||
ContentHandler contentHandler = new StaxStreamContentHandler(streamWriter);
|
||||
marshalSaxHandlers(graph, contentHandler, null);
|
||||
}
|
||||
|
||||
protected Object unmarshalDomNode(Node node) throws XmlMappingException {
|
||||
try {
|
||||
XmlObject object = XmlObject.Factory.parse(node, xmlOptions);
|
||||
validate(object);
|
||||
return object;
|
||||
}
|
||||
catch (XmlException ex) {
|
||||
throw convertXmlBeansException(ex, false);
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException {
|
||||
try {
|
||||
XmlObject object = XmlObject.Factory.parse(inputStream, xmlOptions);
|
||||
validate(object);
|
||||
return object;
|
||||
}
|
||||
catch (XmlException ex) {
|
||||
throw convertXmlBeansException(ex, false);
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalReader(Reader reader) throws XmlMappingException, IOException {
|
||||
try {
|
||||
XmlObject object = XmlObject.Factory.parse(reader, xmlOptions);
|
||||
validate(object);
|
||||
return object;
|
||||
}
|
||||
catch (XmlException ex) {
|
||||
throw convertXmlBeansException(ex, false);
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource)
|
||||
throws XmlMappingException, IOException {
|
||||
XmlSaxHandler saxHandler = XmlObject.Factory.newXmlSaxHandler(xmlOptions);
|
||||
xmlReader.setContentHandler(saxHandler.getContentHandler());
|
||||
try {
|
||||
xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", saxHandler.getLexicalHandler());
|
||||
}
|
||||
catch (SAXNotRecognizedException e) {
|
||||
}
|
||||
catch (SAXNotSupportedException e) {
|
||||
}
|
||||
try {
|
||||
xmlReader.parse(inputSource);
|
||||
XmlObject object = saxHandler.getObject();
|
||||
validate(object);
|
||||
return object;
|
||||
}
|
||||
catch (SAXException ex) {
|
||||
throw convertXmlBeansException(ex, false);
|
||||
}
|
||||
catch (XmlException ex) {
|
||||
throw convertXmlBeansException(ex, false);
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlMappingException {
|
||||
XMLReader reader = new StaxEventXmlReader(eventReader);
|
||||
try {
|
||||
return unmarshalSaxReader(reader, new InputSource());
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw convertXmlBeansException(ex, false);
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) throws XmlMappingException {
|
||||
try {
|
||||
XmlObject object = XmlObject.Factory.parse(streamReader, xmlOptions);
|
||||
validate(object);
|
||||
return object;
|
||||
}
|
||||
catch (XmlException ex) {
|
||||
throw convertXmlBeansException(ex, false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the given <code>XmlObject</code>.
|
||||
*
|
||||
* @param object the xml object to validate
|
||||
* @throws XmlBeansValidationFailureException
|
||||
* if the given object is not valid
|
||||
*/
|
||||
public void validate(XmlObject object) throws XmlBeansValidationFailureException {
|
||||
if (validating && object != null) {
|
||||
// create a temporary xmlOptions just for validation
|
||||
XmlOptions validateOptions = (xmlOptions != null) ? xmlOptions : new XmlOptions();
|
||||
List errorsList = new ArrayList();
|
||||
validateOptions.setErrorListener(errorsList);
|
||||
if (!object.validate(validateOptions)) {
|
||||
StringBuffer buffer = new StringBuffer("Could not validate XmlObject :");
|
||||
for (Iterator iterator = errorsList.iterator(); iterator.hasNext();) {
|
||||
XmlError xmlError = (XmlError) iterator.next();
|
||||
if (xmlError instanceof XmlValidationError) {
|
||||
buffer.append(xmlError.toString());
|
||||
}
|
||||
}
|
||||
XmlException ex = new XmlException(buffer.toString(), null, errorsList);
|
||||
throw new XmlBeansValidationFailureException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.xmlbeans;
|
||||
|
||||
import org.apache.xmlbeans.XmlException;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import org.springframework.oxm.MarshallingFailureException;
|
||||
|
||||
/**
|
||||
* XMLBeans-specific subclass of <code>MarshallingFailureException</code>.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see XmlBeansUtils#convertXmlBeansException(Exception, boolean)
|
||||
*/
|
||||
public class XmlBeansMarshallingFailureException extends MarshallingFailureException {
|
||||
|
||||
public XmlBeansMarshallingFailureException(XmlException ex) {
|
||||
super("XMLBeans marshalling exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
public XmlBeansMarshallingFailureException(SAXException ex) {
|
||||
super("XMLBeans marshalling exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.xmlbeans;
|
||||
|
||||
import org.springframework.oxm.UncategorizedXmlMappingException;
|
||||
|
||||
/**
|
||||
* XMLBeans-specific subclass of <code>UncategorizedXmlMappingException</code>, for XMLBeans exceptions that cannot
|
||||
* be distinguished further.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class XmlBeansSystemException extends UncategorizedXmlMappingException {
|
||||
|
||||
public XmlBeansSystemException(Exception e) {
|
||||
super(e.getMessage(), e);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.xmlbeans;
|
||||
|
||||
import org.apache.xmlbeans.XmlException;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import org.springframework.oxm.UnmarshallingFailureException;
|
||||
|
||||
/**
|
||||
* XMLBeans-specific subclass of <code>UnmarshallingFailureException</code>.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see XmlBeansUtils#convertXmlBeansException(Exception, boolean)
|
||||
*/
|
||||
public class XmlBeansUnmarshallingFailureException extends UnmarshallingFailureException {
|
||||
|
||||
public XmlBeansUnmarshallingFailureException(XmlException ex) {
|
||||
super("XMLBeans unmarshalling exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
public XmlBeansUnmarshallingFailureException(SAXException ex) {
|
||||
super("XMLBeans unmarshalling exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.xmlbeans;
|
||||
|
||||
import org.apache.xmlbeans.XMLStreamValidationException;
|
||||
import org.apache.xmlbeans.XmlException;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import org.springframework.oxm.XmlMappingException;
|
||||
|
||||
/**
|
||||
* Generic utility methods for working with XMLBeans. Mainly for internal use within the framework.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class XmlBeansUtils {
|
||||
|
||||
/**
|
||||
* Converts the given XMLBeans exception to an appropriate exception from the <code>org.springframework.oxm</code>
|
||||
* hierarchy.
|
||||
* <p/>
|
||||
* A boolean flag is used to indicate whether this exception occurs during marshalling or unmarshalling, since
|
||||
* XMLBeans itself does not make this distinction in its exception hierarchy.
|
||||
*
|
||||
* @param ex XMLBeans Exception that occured
|
||||
* @param marshalling indicates whether the exception occurs during marshalling (<code>true</code>), or
|
||||
* unmarshalling (<code>false</code>)
|
||||
* @return the corresponding <code>XmlMappingException</code>
|
||||
*/
|
||||
public static XmlMappingException convertXmlBeansException(Exception ex, boolean marshalling) {
|
||||
if (ex instanceof XMLStreamValidationException) {
|
||||
return new XmlBeansValidationFailureException((XMLStreamValidationException) ex);
|
||||
}
|
||||
else if (ex instanceof XmlException) {
|
||||
XmlException xmlException = (XmlException) ex;
|
||||
if (marshalling) {
|
||||
return new XmlBeansMarshallingFailureException(xmlException);
|
||||
}
|
||||
else {
|
||||
return new XmlBeansUnmarshallingFailureException(xmlException);
|
||||
}
|
||||
}
|
||||
else if (ex instanceof SAXException) {
|
||||
SAXException saxException = (SAXException) ex;
|
||||
if (marshalling) {
|
||||
return new XmlBeansMarshallingFailureException(saxException);
|
||||
}
|
||||
else {
|
||||
return new XmlBeansUnmarshallingFailureException(saxException);
|
||||
}
|
||||
}
|
||||
// fallback
|
||||
return new XmlBeansSystemException(ex);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.xmlbeans;
|
||||
|
||||
import org.apache.xmlbeans.XMLStreamValidationException;
|
||||
import org.apache.xmlbeans.XmlException;
|
||||
|
||||
import org.springframework.oxm.ValidationFailureException;
|
||||
|
||||
/**
|
||||
* XMLBeans-specific subclass of <code>ValidationFailureException</code>.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see org.springframework.oxm.xmlbeans.XmlBeansUtils#convertXmlBeansException
|
||||
*/
|
||||
public class XmlBeansValidationFailureException extends ValidationFailureException {
|
||||
|
||||
public XmlBeansValidationFailureException(XMLStreamValidationException ex) {
|
||||
super("XmlBeans validation exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
public XmlBeansValidationFailureException(XmlException ex) {
|
||||
super("XmlBeans validation exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.xmlbeans;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.xmlbeans.XmlOptions;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
/**
|
||||
* Factory bean that configures an XMLBeans <code>XmlOptions</code> object and provides it as a bean reference.
|
||||
* <p/>
|
||||
* Typical usage will be to set XMLBeans options on this bean, and refer to it in the <code>XmlBeansMarshaller</code>.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see XmlOptions
|
||||
* @see #setOptions(java.util.Map)
|
||||
* @see XmlBeansMarshaller#setXmlOptions(org.apache.xmlbeans.XmlOptions)
|
||||
*/
|
||||
public class XmlOptionsFactoryBean implements FactoryBean, InitializingBean {
|
||||
|
||||
private XmlOptions xmlOptions;
|
||||
|
||||
private Map options;
|
||||
|
||||
/**
|
||||
* Returns the singleton <code>XmlOptions</code>.
|
||||
*/
|
||||
public Object getObject() throws Exception {
|
||||
return xmlOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the class of <code>XmlOptions</code>.
|
||||
*/
|
||||
public Class getObjectType() {
|
||||
return XmlOptions.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>true</code>.
|
||||
*/
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets options on the underlying <code>XmlOptions</code> object. The keys of the supplied map should be one of the
|
||||
* string constants defined in <code>XmlOptions</code>, the values vary per option.
|
||||
*
|
||||
* @see XmlOptions#put(Object, Object)
|
||||
* @see XmlOptions#SAVE_PRETTY_PRINT
|
||||
* @see XmlOptions#LOAD_STRIP_COMMENTS
|
||||
*/
|
||||
public void setOptions(Map options) {
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
xmlOptions = new XmlOptions();
|
||||
if (options != null) {
|
||||
for (Iterator iterator = options.keySet().iterator(); iterator.hasNext();) {
|
||||
Object option = iterator.next();
|
||||
xmlOptions.put(option, options.get(option));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Package providing integration of <a href="http://xmlbeans.apache.org/">XMLBeans</a> with Springs O/X Mapping support.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,281 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.xstream;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
import javax.xml.stream.XMLEventWriter;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import com.thoughtworks.xstream.converters.Converter;
|
||||
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
|
||||
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
|
||||
import com.thoughtworks.xstream.io.xml.CompactWriter;
|
||||
import com.thoughtworks.xstream.io.xml.DomReader;
|
||||
import com.thoughtworks.xstream.io.xml.DomWriter;
|
||||
import com.thoughtworks.xstream.io.xml.QNameMap;
|
||||
import com.thoughtworks.xstream.io.xml.SaxWriter;
|
||||
import com.thoughtworks.xstream.io.xml.StaxReader;
|
||||
import com.thoughtworks.xstream.io.xml.StaxWriter;
|
||||
import com.thoughtworks.xstream.io.xml.XppReader;
|
||||
import org.springframework.beans.propertyeditors.ClassEditor;
|
||||
import org.springframework.oxm.AbstractMarshaller;
|
||||
import org.springframework.oxm.XmlMappingException;
|
||||
import org.springframework.xml.stream.StaxEventContentHandler;
|
||||
import org.springframework.xml.stream.XmlEventStreamReader;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.ext.LexicalHandler;
|
||||
|
||||
/**
|
||||
* Implementation of the <code>Marshaller</code> interface for XStream. By default, XStream does not require any further
|
||||
* configuration, though class aliases can be used to have more control over the behavior of XStream.
|
||||
* <p/>
|
||||
* Due to XStream's API, it is required to set the encoding used for writing to outputstreams. It defaults to
|
||||
* <code>UTF-8</code>.
|
||||
* <p/>
|
||||
* <b>Note</b> that XStream is an XML serialization library, not a data binding library. Therefore, it has limited
|
||||
* namespace support. As such, it is rather unsuitable for usage within Web services.
|
||||
*
|
||||
* @author Peter Meijer
|
||||
* @author Arjen Poutsma
|
||||
* @see #setEncoding(String)
|
||||
* @see #DEFAULT_ENCODING
|
||||
* @see #setAliases(java.util.Map)
|
||||
* @see #setConverters(com.thoughtworks.xstream.converters.Converter[])
|
||||
*/
|
||||
public class XStreamMarshaller extends AbstractMarshaller {
|
||||
|
||||
/**
|
||||
* The default encoding used for stream access.
|
||||
*/
|
||||
public static final String DEFAULT_ENCODING = "UTF-8";
|
||||
|
||||
private XStream xstream = new XStream();
|
||||
|
||||
private String encoding;
|
||||
|
||||
/**
|
||||
* Returns the encoding to be used for stream access. If this property is not set, the default encoding is used.
|
||||
*
|
||||
* @see #DEFAULT_ENCODING
|
||||
*/
|
||||
public String getEncoding() {
|
||||
return encoding != null ? encoding : DEFAULT_ENCODING;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the encoding to be used for stream access. If this property is not set, the default encoding is used.
|
||||
*
|
||||
* @see #DEFAULT_ENCODING
|
||||
*/
|
||||
public void setEncoding(String encoding) {
|
||||
this.encoding = encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the XStream mode.
|
||||
*
|
||||
* @see XStream#XPATH_REFERENCES
|
||||
* @see XStream#ID_REFERENCES
|
||||
* @see XStream#NO_REFERENCES
|
||||
*/
|
||||
public void setMode(int mode) {
|
||||
xstream.setMode(mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the <code>Converters</code> to be registered with the <code>XStream</code> instance.
|
||||
*/
|
||||
public void setConverters(Converter[] converters) {
|
||||
for (int i = 0; i < converters.length; i++) {
|
||||
xstream.registerConverter(converters[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a alias/type map, consisting of string aliases mapped to <code>Class</code> instances (or Strings to be
|
||||
* converted to <code>Class</code> instances).
|
||||
*
|
||||
* @see ClassEditor
|
||||
*/
|
||||
public void setAliases(Map aliases) {
|
||||
for (Iterator iterator = aliases.keySet().iterator(); iterator.hasNext();) {
|
||||
String name = (String) iterator.next();
|
||||
Object value = aliases.get(name);
|
||||
// Check whether we need to convert from String to Class.
|
||||
Class type = null;
|
||||
if (value instanceof Class) {
|
||||
type = (Class) value;
|
||||
}
|
||||
else {
|
||||
ClassEditor editor = new ClassEditor();
|
||||
editor.setAsText(String.valueOf(value));
|
||||
type = (Class) editor.getValue();
|
||||
}
|
||||
addAlias(name, type);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an alias for the given type.
|
||||
*
|
||||
* @param name alias to be used for the type
|
||||
* @param type the type to be aliased
|
||||
*/
|
||||
public void addAlias(String name, Class type) {
|
||||
xstream.alias(name, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the given XStream exception to an appropriate exception from the <code>org.springframework.oxm</code>
|
||||
* hierarchy.
|
||||
* <p/>
|
||||
* The default implementation delegates to <code>XStreamUtils</code>. Can be overridden in subclasses.
|
||||
*
|
||||
* @param ex exception that occured
|
||||
* @param marshalling indicates whether the exception occurs during marshalling (<code>true</code>), or
|
||||
* unmarshalling (<code>false</code>)
|
||||
* @return the corresponding <code>XmlMappingException</code> instance
|
||||
* @see XStreamUtils#convertXStreamException(Exception, boolean)
|
||||
*/
|
||||
public XmlMappingException convertXStreamException(Exception ex, boolean marshalling) {
|
||||
return XStreamUtils.convertXStreamException(ex, marshalling);
|
||||
}
|
||||
|
||||
/**
|
||||
* Marshals the given graph to the given XStream HierarchicalStreamWriter. Converts exceptions using
|
||||
* <code>convertXStreamException</code>.
|
||||
*/
|
||||
private void marshal(Object graph, HierarchicalStreamWriter streamWriter) {
|
||||
try {
|
||||
xstream.marshal(graph, streamWriter);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw convertXStreamException(ex, true);
|
||||
}
|
||||
}
|
||||
|
||||
protected void marshalDomNode(Object graph, Node node) throws XmlMappingException {
|
||||
HierarchicalStreamWriter streamWriter = null;
|
||||
if (node instanceof Document) {
|
||||
streamWriter = new DomWriter((Document) node);
|
||||
}
|
||||
else if (node instanceof Element) {
|
||||
streamWriter = new DomWriter((Element) node);
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("DOMResult contains neither Document nor Element");
|
||||
}
|
||||
marshal(graph, streamWriter);
|
||||
}
|
||||
|
||||
protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) throws XmlMappingException {
|
||||
ContentHandler contentHandler = new StaxEventContentHandler(eventWriter);
|
||||
marshalSaxHandlers(graph, contentHandler, null);
|
||||
}
|
||||
|
||||
protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException {
|
||||
try {
|
||||
marshal(graph, new StaxWriter(new QNameMap(), streamWriter));
|
||||
}
|
||||
catch (XMLStreamException ex) {
|
||||
throw convertXStreamException(ex, true);
|
||||
}
|
||||
}
|
||||
|
||||
protected void marshalOutputStream(Object graph, OutputStream outputStream)
|
||||
throws XmlMappingException, IOException {
|
||||
marshalWriter(graph, new OutputStreamWriter(outputStream, getEncoding()));
|
||||
}
|
||||
|
||||
protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler)
|
||||
throws XmlMappingException {
|
||||
SaxWriter saxWriter = new SaxWriter();
|
||||
saxWriter.setContentHandler(contentHandler);
|
||||
marshal(graph, saxWriter);
|
||||
}
|
||||
|
||||
protected void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException {
|
||||
marshal(graph, new CompactWriter(writer));
|
||||
}
|
||||
|
||||
private Object unmarshal(HierarchicalStreamReader streamReader) {
|
||||
try {
|
||||
return xstream.unmarshal(streamReader);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw convertXStreamException(ex, false);
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalDomNode(Node node) throws XmlMappingException {
|
||||
HierarchicalStreamReader streamReader = null;
|
||||
if (node instanceof Document) {
|
||||
streamReader = new DomReader((Document) node);
|
||||
}
|
||||
else if (node instanceof Element) {
|
||||
streamReader = new DomReader((Element) node);
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("DOMSource contains neither Document nor Element");
|
||||
}
|
||||
return unmarshal(streamReader);
|
||||
}
|
||||
|
||||
protected Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlMappingException {
|
||||
try {
|
||||
XMLStreamReader streamReader = new XmlEventStreamReader(eventReader);
|
||||
return unmarshalXmlStreamReader(streamReader);
|
||||
}
|
||||
catch (XMLStreamException ex) {
|
||||
throw convertXStreamException(ex, false);
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) throws XmlMappingException {
|
||||
return unmarshal(new StaxReader(new QNameMap(), streamReader));
|
||||
}
|
||||
|
||||
protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException {
|
||||
return unmarshalReader(new InputStreamReader(inputStream, getEncoding()));
|
||||
}
|
||||
|
||||
protected Object unmarshalReader(Reader reader) throws XmlMappingException, IOException {
|
||||
return unmarshal(new XppReader(reader));
|
||||
}
|
||||
|
||||
protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource)
|
||||
throws XmlMappingException, IOException {
|
||||
throw new UnsupportedOperationException("XStreamMarshaller does not support unmarshalling using SAX XMLReaders");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.xstream;
|
||||
|
||||
import com.thoughtworks.xstream.alias.CannotResolveClassException;
|
||||
import com.thoughtworks.xstream.converters.ConversionException;
|
||||
import com.thoughtworks.xstream.io.StreamException;
|
||||
|
||||
import org.springframework.oxm.MarshallingFailureException;
|
||||
|
||||
/**
|
||||
* XStream-specific subclass of <code>MarshallingFailureException</code>.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class XStreamMarshallingFailureException extends MarshallingFailureException {
|
||||
|
||||
public XStreamMarshallingFailureException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
public XStreamMarshallingFailureException(StreamException ex) {
|
||||
super("XStream marshalling exception: " + ex.getMessage(), ex);
|
||||
|
||||
}
|
||||
|
||||
public XStreamMarshallingFailureException(CannotResolveClassException ex) {
|
||||
super("XStream resolving exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
public XStreamMarshallingFailureException(ConversionException ex) {
|
||||
super("XStream conversion exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.xstream;
|
||||
|
||||
import org.springframework.oxm.UncategorizedXmlMappingException;
|
||||
|
||||
/**
|
||||
* XStream-specific subclass of <code>UncategorizedXmlMappingException</code>, for XStream exceptions that cannot be
|
||||
* distinguished further.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class XStreamSystemException extends UncategorizedXmlMappingException {
|
||||
|
||||
public XStreamSystemException(String msg, Throwable ex) {
|
||||
super(msg, ex);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.xstream;
|
||||
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
|
||||
import com.thoughtworks.xstream.alias.CannotResolveClassException;
|
||||
import com.thoughtworks.xstream.converters.ConversionException;
|
||||
import com.thoughtworks.xstream.io.StreamException;
|
||||
|
||||
import org.springframework.oxm.UnmarshallingFailureException;
|
||||
|
||||
/**
|
||||
* XStream-specific subclass of <code>UnmarshallingFailureException</code>.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class XStreamUnmarshallingFailureException extends UnmarshallingFailureException {
|
||||
|
||||
public XStreamUnmarshallingFailureException(StreamException ex) {
|
||||
super("XStream unmarshalling exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
public XStreamUnmarshallingFailureException(CannotResolveClassException ex) {
|
||||
super("XStream resolving exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
public XStreamUnmarshallingFailureException(ConversionException ex) {
|
||||
super("XStream conversion exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
public XStreamUnmarshallingFailureException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
public XStreamUnmarshallingFailureException(String msg, XMLStreamException ex) {
|
||||
super(msg, ex);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.xstream;
|
||||
|
||||
import com.thoughtworks.xstream.alias.CannotResolveClassException;
|
||||
import com.thoughtworks.xstream.converters.ConversionException;
|
||||
import com.thoughtworks.xstream.io.StreamException;
|
||||
|
||||
import org.springframework.oxm.XmlMappingException;
|
||||
|
||||
/**
|
||||
* Generic utility methods for working with XStream. Mainly for internal use within the framework.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public abstract class XStreamUtils {
|
||||
|
||||
/**
|
||||
* Converts the given XStream exception to an appropriate exception from the <code>org.springframework.oxm</code>
|
||||
* 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 XStream exception that occured
|
||||
* @param marshalling indicates whether the exception occurs during marshalling (<code>true</code>), or
|
||||
* unmarshalling (<code>false</code>)
|
||||
* @return the corresponding <code>XmlMappingException</code>
|
||||
*/
|
||||
public static XmlMappingException convertXStreamException(Exception ex, boolean marshalling) {
|
||||
if (ex instanceof StreamException) {
|
||||
if (marshalling) {
|
||||
return new XStreamMarshallingFailureException((StreamException) ex);
|
||||
}
|
||||
else {
|
||||
return new XStreamUnmarshallingFailureException((StreamException) ex);
|
||||
}
|
||||
}
|
||||
else if (ex instanceof CannotResolveClassException) {
|
||||
if (marshalling) {
|
||||
return new XStreamMarshallingFailureException((CannotResolveClassException) ex);
|
||||
}
|
||||
else {
|
||||
return new XStreamUnmarshallingFailureException((CannotResolveClassException) ex);
|
||||
}
|
||||
}
|
||||
else if (ex instanceof ConversionException) {
|
||||
if (marshalling) {
|
||||
return new XStreamMarshallingFailureException((ConversionException) ex);
|
||||
}
|
||||
else {
|
||||
return new XStreamUnmarshallingFailureException((ConversionException) ex);
|
||||
}
|
||||
}
|
||||
// fallback
|
||||
return new XStreamSystemException("Unknown XStream exception: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Package providing integration of <a href="http://xstream.codehaus.org/">XStream</a> with Springs O/X Mapping support.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.StringWriter;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.stream.XMLEventWriter;
|
||||
import javax.xml.stream.XMLOutputFactory;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.custommonkey.xmlunit.XMLTestCase;
|
||||
import org.custommonkey.xmlunit.XMLUnit;
|
||||
import org.springframework.xml.transform.StaxResult;
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Text;
|
||||
|
||||
public abstract class AbstractMarshallerTestCase extends XMLTestCase {
|
||||
|
||||
protected Marshaller marshaller;
|
||||
|
||||
protected Object flights;
|
||||
|
||||
protected static final String EXPECTED_STRING =
|
||||
"<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
|
||||
"<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";
|
||||
|
||||
protected final void setUp() throws Exception {
|
||||
marshaller = createMarshaller();
|
||||
flights = createFlights();
|
||||
XMLUnit.setIgnoreWhitespace(true);
|
||||
}
|
||||
|
||||
protected abstract Marshaller createMarshaller() throws Exception;
|
||||
|
||||
protected abstract Object createFlights();
|
||||
|
||||
public void testMarshalDOMResult() throws Exception {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
documentBuilderFactory.setNamespaceAware(true);
|
||||
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
|
||||
Document result = builder.newDocument();
|
||||
DOMResult domResult = new DOMResult(result);
|
||||
marshaller.marshal(flights, domResult);
|
||||
Document expected = builder.newDocument();
|
||||
Element flightsElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flights");
|
||||
expected.appendChild(flightsElement);
|
||||
Element flightElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flight");
|
||||
flightsElement.appendChild(flightElement);
|
||||
Element numberElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:number");
|
||||
flightElement.appendChild(numberElement);
|
||||
Text text = expected.createTextNode("42");
|
||||
numberElement.appendChild(text);
|
||||
Transformer transformer = TransformerFactory.newInstance().newTransformer();
|
||||
StringResult stringResult = new StringResult();
|
||||
transformer.transform(new DOMSource(result), stringResult);
|
||||
StringResult stringExpected = new StringResult();
|
||||
transformer.transform(new DOMSource(expected), stringExpected);
|
||||
assertXMLEqual("Marshaller writes invalid DOMResult", stringExpected.toString(), stringResult.toString());
|
||||
}
|
||||
|
||||
public void testMarshalStreamResultWriter() throws Exception {
|
||||
StringWriter writer = new StringWriter();
|
||||
StreamResult result = new StreamResult(writer);
|
||||
marshaller.marshal(flights, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
public void testMarshalStreamResultOutputStream() throws Exception {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
StreamResult result = new StreamResult(os);
|
||||
marshaller.marshal(flights, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult",
|
||||
EXPECTED_STRING,
|
||||
new String(os.toByteArray(), "UTF-8"));
|
||||
}
|
||||
|
||||
public void testMarshalStaxResultXMLStreamWriter() throws Exception {
|
||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
||||
StringWriter writer = new StringWriter();
|
||||
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
|
||||
StaxResult result = new StaxResult(streamWriter);
|
||||
marshaller.marshal(flights, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
public void testMarshalStaxResultXMLEventWriter() throws Exception {
|
||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
||||
StringWriter writer = new StringWriter();
|
||||
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
|
||||
StaxResult result = new StaxResult(eventWriter);
|
||||
marshaller.marshal(flights, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.StringReader;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Text;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.helpers.XMLReaderFactory;
|
||||
|
||||
import org.springframework.xml.transform.StaxSource;
|
||||
|
||||
public abstract class AbstractUnmarshallerTestCase extends TestCase {
|
||||
|
||||
protected Unmarshaller unmarshaller;
|
||||
|
||||
protected static final String INPUT_STRING =
|
||||
"<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
|
||||
"<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";
|
||||
|
||||
protected final void setUp() throws Exception {
|
||||
unmarshaller = createUnmarshaller();
|
||||
}
|
||||
|
||||
protected abstract Unmarshaller createUnmarshaller() throws Exception;
|
||||
|
||||
protected abstract void testFlights(Object o);
|
||||
|
||||
public void testUnmarshalDomSource() throws Exception {
|
||||
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
Document document = builder.newDocument();
|
||||
Element flightsElement = document.createElementNS("http://samples.springframework.org/flight", "tns:flights");
|
||||
document.appendChild(flightsElement);
|
||||
Element flightElement = document.createElementNS("http://samples.springframework.org/flight", "tns:flight");
|
||||
flightsElement.appendChild(flightElement);
|
||||
Element numberElement = document.createElementNS("http://samples.springframework.org/flight", "tns:number");
|
||||
flightElement.appendChild(numberElement);
|
||||
Text text = document.createTextNode("42");
|
||||
numberElement.appendChild(text);
|
||||
DOMSource source = new DOMSource(document);
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalStreamSourceReader() throws Exception {
|
||||
StreamSource source = new StreamSource(new StringReader(INPUT_STRING));
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalStreamSourceInputStream() throws Exception {
|
||||
StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalSAXSource() throws Exception {
|
||||
XMLReader reader = XMLReaderFactory.createXMLReader();
|
||||
SAXSource source = new SAXSource(reader, new InputSource(new StringReader(INPUT_STRING)));
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalStaxSourceXmlStreamReader() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
|
||||
StaxSource source = new StaxSource(streamReader);
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalStaxSourceXmlEventReader() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING));
|
||||
StaxSource source = new StaxSource(eventReader);
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.castor;
|
||||
|
||||
import javax.xml.transform.sax.SAXResult;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.oxm.AbstractMarshallerTestCase;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
import org.xml.sax.ContentHandler;
|
||||
|
||||
public class CastorMarshallerTest extends AbstractMarshallerTestCase {
|
||||
|
||||
protected Marshaller createMarshaller() throws Exception {
|
||||
CastorMarshaller marshaller = new CastorMarshaller();
|
||||
ClassPathResource mappingLocation = new ClassPathResource("mapping.xml", CastorMarshaller.class);
|
||||
marshaller.setMappingLocation(mappingLocation);
|
||||
marshaller.afterPropertiesSet();
|
||||
return marshaller;
|
||||
}
|
||||
|
||||
protected Object createFlights() {
|
||||
Flight flight = new Flight();
|
||||
flight.setNumber(42L);
|
||||
Flights flights = new Flights();
|
||||
flights.addFlight(flight);
|
||||
return flights;
|
||||
}
|
||||
|
||||
public void testMarshalSaxResult() throws Exception {
|
||||
MockControl handlerControl = MockControl.createControl(ContentHandler.class);
|
||||
ContentHandler handlerMock = (ContentHandler) handlerControl.getMock();
|
||||
handlerMock.startDocument();
|
||||
handlerMock.startPrefixMapping("tns", "http://samples.springframework.org/flight");
|
||||
handlerMock.startElement("http://samples.springframework.org/flight", "flights", "tns:flights", null);
|
||||
handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
handlerMock.startElement("http://samples.springframework.org/flight", "flight", "tns:flight", null);
|
||||
handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
handlerMock.startElement("http://samples.springframework.org/flight", "number", "tns:number", null);
|
||||
handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
handlerMock.characters(new char[]{'4', '2'}, 0, 2);
|
||||
handlerControl.setMatcher(MockControl.ARRAY_MATCHER);
|
||||
handlerMock.endElement("http://samples.springframework.org/flight", "number", "tns:number");
|
||||
handlerMock.endElement("http://samples.springframework.org/flight", "flight", "tns:flight");
|
||||
handlerMock.endElement("http://samples.springframework.org/flight", "flights", "tns:flights");
|
||||
handlerMock.endPrefixMapping("tns");
|
||||
handlerMock.endDocument();
|
||||
|
||||
handlerControl.replay();
|
||||
SAXResult result = new SAXResult(handlerMock);
|
||||
marshaller.marshal(flights, result);
|
||||
handlerControl.verify();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.castor;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.oxm.AbstractUnmarshallerTestCase;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
|
||||
public class CastorUnmarshallerTest extends AbstractUnmarshallerTestCase {
|
||||
|
||||
protected void testFlights(Object o) {
|
||||
Flights flights = (Flights) o;
|
||||
assertNotNull("Flights is null", flights);
|
||||
assertEquals("Invalid amount of flight elements", 1, flights.getFlightCount());
|
||||
Flight flight = flights.getFlight()[0];
|
||||
assertNotNull("Flight is null", flight);
|
||||
assertEquals("Number is invalid", 42L, flight.getNumber());
|
||||
}
|
||||
|
||||
protected Unmarshaller createUnmarshaller() throws Exception {
|
||||
CastorMarshaller marshaller = new CastorMarshaller();
|
||||
ClassPathResource mappingLocation = new ClassPathResource("mapping.xml", CastorMarshaller.class);
|
||||
marshaller.setMappingLocation(mappingLocation);
|
||||
marshaller.afterPropertiesSet();
|
||||
return marshaller;
|
||||
}
|
||||
|
||||
public void testUnmarshalTargetClass() throws Exception {
|
||||
CastorMarshaller unmarshaller = new CastorMarshaller();
|
||||
unmarshaller.setTargetClass(Flights.class);
|
||||
unmarshaller.afterPropertiesSet();
|
||||
StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testSetBothTargetClassAndMapping() throws IOException {
|
||||
try {
|
||||
CastorMarshaller marshaller = new CastorMarshaller();
|
||||
marshaller.setMappingLocation(new ClassPathResource("mapping.xml", CastorMarshaller.class));
|
||||
marshaller.setTargetClass(getClass());
|
||||
marshaller.afterPropertiesSet();
|
||||
fail("IllegalArgumentException expected");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.castor;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.exolab.castor.xml.MarshalException;
|
||||
import org.exolab.castor.xml.ValidationException;
|
||||
import org.exolab.castor.xml.XMLException;
|
||||
|
||||
public class CastorUtilsTest extends TestCase {
|
||||
|
||||
public void testConvertMarshalException() {
|
||||
assertTrue("Invalid exception conversion", CastorUtils
|
||||
.convertXmlException(new MarshalException(""), true) instanceof CastorMarshallingFailureException);
|
||||
assertTrue("Invalid exception conversion", CastorUtils
|
||||
.convertXmlException(new MarshalException(""), false) instanceof CastorUnmarshallingFailureException);
|
||||
}
|
||||
|
||||
public void testConvertValidationException() {
|
||||
assertTrue("Invalid exception conversion", CastorUtils
|
||||
.convertXmlException(new ValidationException(""), false) instanceof CastorValidationFailureException);
|
||||
}
|
||||
|
||||
public void testConvertXMLException() {
|
||||
assertTrue("Invalid exception conversion",
|
||||
CastorUtils.convertXmlException(new XMLException(""), false) instanceof CastorSystemException);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.jaxb;
|
||||
|
||||
import javax.xml.transform.sax.SAXResult;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.xml.sax.ContentHandler;
|
||||
|
||||
import org.springframework.oxm.AbstractMarshallerTestCase;
|
||||
|
||||
public abstract class AbstractJaxbMarshallerTestCase extends AbstractMarshallerTestCase {
|
||||
|
||||
public void testMarshalSaxResult() throws Exception {
|
||||
MockControl handlerControl = MockControl.createStrictControl(ContentHandler.class);
|
||||
ContentHandler handlerMock = (ContentHandler) handlerControl.getMock();
|
||||
handlerMock.setDocumentLocator(null);
|
||||
handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
handlerMock.startDocument();
|
||||
handlerMock.startPrefixMapping("", "http://samples.springframework.org/flight");
|
||||
handlerMock.startElement("http://samples.springframework.org/flight", "flights", "flights", null);
|
||||
handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
handlerMock.startElement("http://samples.springframework.org/flight", "flight", "flight", null);
|
||||
handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
handlerMock.startElement("http://samples.springframework.org/flight", "number", "number", null);
|
||||
handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
handlerMock.characters(new char[]{'4', '2'}, 0, 2);
|
||||
handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
handlerMock.endElement("http://samples.springframework.org/flight", "number", "number");
|
||||
handlerMock.endElement("http://samples.springframework.org/flight", "flight", "flight");
|
||||
handlerMock.endElement("http://samples.springframework.org/flight", "flights", "flights");
|
||||
handlerMock.endPrefixMapping("");
|
||||
handlerMock.endDocument();
|
||||
|
||||
handlerControl.replay();
|
||||
SAXResult result = new SAXResult(handlerMock);
|
||||
marshaller.marshal(flights, result);
|
||||
handlerControl.verify();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.jaxb;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.springframework.oxm.Marshaller;
|
||||
import org.springframework.oxm.XmlMappingException;
|
||||
import org.springframework.oxm.jaxb1.FlightType;
|
||||
import org.springframework.oxm.jaxb1.Flights;
|
||||
import org.springframework.oxm.jaxb1.impl.FlightTypeImpl;
|
||||
import org.springframework.oxm.jaxb1.impl.FlightsImpl;
|
||||
|
||||
public class Jaxb1MarshallerTest extends AbstractJaxbMarshallerTestCase {
|
||||
|
||||
private static final String CONTEXT_PATH = "org.springframework.oxm.jaxb1";
|
||||
|
||||
protected final Marshaller createMarshaller() throws Exception {
|
||||
Jaxb1Marshaller marshaller = new Jaxb1Marshaller();
|
||||
marshaller.setContextPath(CONTEXT_PATH);
|
||||
marshaller.afterPropertiesSet();
|
||||
return marshaller;
|
||||
}
|
||||
|
||||
protected Object createFlights() {
|
||||
FlightType flight = new FlightTypeImpl();
|
||||
flight.setNumber(42L);
|
||||
Flights flights = new FlightsImpl();
|
||||
flights.getFlight().add(flight);
|
||||
return flights;
|
||||
}
|
||||
|
||||
public void testProperties() throws Exception {
|
||||
Jaxb1Marshaller marshaller = new Jaxb1Marshaller();
|
||||
marshaller.setContextPath(CONTEXT_PATH);
|
||||
marshaller.setMarshallerProperties(
|
||||
Collections.singletonMap(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE));
|
||||
marshaller.afterPropertiesSet();
|
||||
}
|
||||
|
||||
public void testNoContextPath() throws Exception {
|
||||
try {
|
||||
Jaxb1Marshaller marshaller = new Jaxb1Marshaller();
|
||||
marshaller.afterPropertiesSet();
|
||||
fail("Should have thrown an IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testInvalidContextPath() throws Exception {
|
||||
try {
|
||||
Jaxb1Marshaller marshaller = new Jaxb1Marshaller();
|
||||
marshaller.setContextPath("ab");
|
||||
marshaller.afterPropertiesSet();
|
||||
fail("Should have thrown an XmlMappingException");
|
||||
}
|
||||
catch (XmlMappingException ex) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.jaxb;
|
||||
|
||||
import org.springframework.oxm.AbstractUnmarshallerTestCase;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
import org.springframework.oxm.jaxb1.FlightType;
|
||||
import org.springframework.oxm.jaxb1.Flights;
|
||||
|
||||
public class Jaxb1UnmarshallerTest extends AbstractUnmarshallerTestCase {
|
||||
|
||||
protected Unmarshaller createUnmarshaller() throws Exception {
|
||||
Jaxb1Marshaller marshaller = new Jaxb1Marshaller();
|
||||
marshaller.setContextPath("org.springframework.oxm.jaxb1");
|
||||
marshaller.setValidating(true);
|
||||
marshaller.afterPropertiesSet();
|
||||
return marshaller;
|
||||
}
|
||||
|
||||
protected void testFlights(Object o) {
|
||||
Flights flights = (Flights) o;
|
||||
assertNotNull("Flights is null", flights);
|
||||
assertEquals("Invalid amount of flight elements", 1, flights.getFlight().size());
|
||||
FlightType flight = (FlightType) flights.getFlight().get(0);
|
||||
assertNotNull("Flight is null", flight);
|
||||
assertEquals("Number is invalid", 42L, flight.getNumber());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.jaxb;
|
||||
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.MarshalException;
|
||||
import javax.xml.bind.UnmarshalException;
|
||||
import javax.xml.bind.ValidationException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class JaxbUtilsTest extends TestCase {
|
||||
|
||||
public void testConvertMarshallingException() {
|
||||
assertTrue("Invalid exception conversion",
|
||||
JaxbUtils.convertJaxbException(new MarshalException("")) instanceof JaxbMarshallingFailureException);
|
||||
}
|
||||
|
||||
public void testConvertUnmarshallingException() {
|
||||
assertTrue("Invalid exception conversion", JaxbUtils
|
||||
.convertJaxbException(new UnmarshalException("")) instanceof JaxbUnmarshallingFailureException);
|
||||
}
|
||||
|
||||
public void testConvertValidationException() {
|
||||
assertTrue("Invalid exception conversion",
|
||||
JaxbUtils.convertJaxbException(new ValidationException("")) instanceof JaxbValidationFailureException);
|
||||
}
|
||||
|
||||
public void testConvertJAXBException() {
|
||||
assertTrue("Invalid exception conversion",
|
||||
JaxbUtils.convertJaxbException(new JAXBException("")) instanceof JaxbSystemException);
|
||||
}
|
||||
|
||||
public void testGetJaxbVersion() throws Exception {
|
||||
assertEquals("Invalid JAXB version", JaxbUtils.JAXB_1, JaxbUtils.getJaxbVersion());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.jibx;
|
||||
|
||||
public class FlightType {
|
||||
|
||||
protected long number;
|
||||
|
||||
public long getNumber() {
|
||||
return this.number;
|
||||
}
|
||||
|
||||
public void setNumber(long number) {
|
||||
this.number = number;
|
||||
}
|
||||
}
|
||||
36
oxm/src/test/java/org/springframework/oxm/jibx/Flights.java
Normal file
36
oxm/src/test/java/org/springframework/oxm/jibx/Flights.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.jibx;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Flights {
|
||||
|
||||
protected ArrayList flightList = new ArrayList();
|
||||
|
||||
public void addFlight(FlightType flight) {
|
||||
flightList.add(flight);
|
||||
}
|
||||
|
||||
public FlightType getFlight(int index) {
|
||||
return (FlightType) flightList.get(index);
|
||||
}
|
||||
|
||||
public int sizeFlightList() {
|
||||
return flightList.size();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.jibx;
|
||||
|
||||
import org.springframework.oxm.AbstractMarshallerTestCase;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
|
||||
public class JibxMarshallerTest extends AbstractMarshallerTestCase {
|
||||
|
||||
protected Marshaller createMarshaller() throws Exception {
|
||||
JibxMarshaller marshaller = new JibxMarshaller();
|
||||
marshaller.setTargetClass(Flights.class);
|
||||
marshaller.afterPropertiesSet();
|
||||
return marshaller;
|
||||
}
|
||||
|
||||
protected Object createFlights() {
|
||||
Flights flights = new Flights();
|
||||
FlightType flight = new FlightType();
|
||||
flight.setNumber(42L);
|
||||
flights.addFlight(flight);
|
||||
return flights;
|
||||
}
|
||||
|
||||
public void testAfterPropertiesSetNoContextPath() throws Exception {
|
||||
try {
|
||||
JibxMarshaller marshaller = new JibxMarshaller();
|
||||
marshaller.afterPropertiesSet();
|
||||
fail("Should have thrown an IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.jibx;
|
||||
|
||||
import org.springframework.oxm.AbstractUnmarshallerTestCase;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
|
||||
public class JibxUnmarshallerTest extends AbstractUnmarshallerTestCase {
|
||||
|
||||
protected Unmarshaller createUnmarshaller() throws Exception {
|
||||
JibxMarshaller unmarshaller = new JibxMarshaller();
|
||||
unmarshaller.setTargetClass(Flights.class);
|
||||
unmarshaller.afterPropertiesSet();
|
||||
return unmarshaller;
|
||||
}
|
||||
|
||||
protected void testFlights(Object o) {
|
||||
Flights flights = (Flights) o;
|
||||
assertNotNull("Flights is null", flights);
|
||||
assertEquals("Invalid amount of flight elements", 1, flights.sizeFlightList());
|
||||
FlightType flight = (FlightType) flights.getFlight(0);
|
||||
assertNotNull("Flight is null", flight);
|
||||
assertEquals("Number is invalid", 42L, flight.getNumber());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.jibx;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.jibx.runtime.JiBXException;
|
||||
import org.jibx.runtime.ValidationException;
|
||||
|
||||
public class JibxUtilsTest extends TestCase {
|
||||
|
||||
public void testConvertMarshallingException() throws Exception {
|
||||
assertTrue("Invalid exception conversion",
|
||||
JibxUtils.convertJibxException(new JiBXException(""), true) instanceof JibxMarshallingFailureException);
|
||||
}
|
||||
|
||||
public void testConvertUnmarshallingException() throws Exception {
|
||||
assertTrue("Invalid exception conversion", JibxUtils
|
||||
.convertJibxException(new JiBXException(""), false) instanceof JibxUnmarshallingFailureException);
|
||||
}
|
||||
|
||||
public void testConvertValidationException() throws Exception {
|
||||
assertTrue("Invalid exception conversion", JibxUtils
|
||||
.convertJibxException(new ValidationException(""), true) instanceof JibxValidationFailureException);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.xmlbeans;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.springframework.oxm.AbstractMarshallerTestCase;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
import org.springframework.samples.flight.FlightType;
|
||||
import org.springframework.samples.flight.FlightsDocument;
|
||||
import org.springframework.samples.flight.FlightsDocument.Flights;
|
||||
|
||||
public class XmlBeansMarshallerTest extends AbstractMarshallerTestCase {
|
||||
|
||||
protected Marshaller createMarshaller() throws Exception {
|
||||
return new XmlBeansMarshaller();
|
||||
}
|
||||
|
||||
public void testMarshalNonXmlObject() throws Exception {
|
||||
try {
|
||||
this.marshaller.marshal(new Object(), new StreamResult(new ByteArrayOutputStream()));
|
||||
fail("XmlBeansMarshaller did not throw ClassCastException for non-XmlObject");
|
||||
}
|
||||
catch (ClassCastException e) {
|
||||
// Expected behavior
|
||||
}
|
||||
}
|
||||
|
||||
protected Object createFlights() {
|
||||
FlightsDocument flightsDocument = FlightsDocument.Factory.newInstance();
|
||||
Flights flights = flightsDocument.addNewFlights();
|
||||
FlightType flightType = flights.addNewFlight();
|
||||
flightType.setNumber(42L);
|
||||
return flightsDocument;
|
||||
}
|
||||
|
||||
public void testMarshalStaxResultXMLStreamWriter() throws Exception {
|
||||
// Unfortu
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.xmlbeans;
|
||||
|
||||
import org.springframework.oxm.AbstractUnmarshallerTestCase;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
import org.springframework.samples.flight.FlightType;
|
||||
import org.springframework.samples.flight.FlightsDocument;
|
||||
import org.springframework.samples.flight.FlightsDocument.Flights;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
|
||||
public class XmlBeansUnmarshallerTest extends AbstractUnmarshallerTestCase {
|
||||
|
||||
protected Unmarshaller createUnmarshaller() throws Exception {
|
||||
return new XmlBeansMarshaller();
|
||||
}
|
||||
|
||||
protected void testFlights(Object o) {
|
||||
FlightsDocument flightsDocument = (FlightsDocument) o;
|
||||
assertNotNull("FlightsDocument is null", flightsDocument);
|
||||
Flights flights = flightsDocument.getFlights();
|
||||
assertEquals("Invalid amount of flight elements", 1, flights.sizeOfFlightArray());
|
||||
FlightType flight = flights.getFlightArray(0);
|
||||
assertNotNull("Flight is null", flight);
|
||||
assertEquals("Number is invalid", 42L, flight.getNumber());
|
||||
}
|
||||
|
||||
public void testValidate() throws Exception {
|
||||
((XmlBeansMarshaller) unmarshaller).setValidating(true);
|
||||
String invalidInput = "<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
|
||||
"<tns:flight><tns:number>abc</tns:number></tns:flight></tns:flights>";
|
||||
|
||||
try {
|
||||
unmarshaller.unmarshal(new StringSource(invalidInput));
|
||||
fail("Expected a XmlBeansValidationFailureException");
|
||||
}
|
||||
catch (XmlBeansValidationFailureException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.oxm.xmlbeans;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.xmlbeans.XMLStreamValidationException;
|
||||
import org.apache.xmlbeans.XmlError;
|
||||
import org.apache.xmlbeans.XmlException;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
public class XmlBeansUtilsTest extends TestCase {
|
||||
|
||||
public void testConvertXMLStreamValidationException() {
|
||||
assertTrue("Invalid exception conversion", XmlBeansUtils.convertXmlBeansException(
|
||||
new XMLStreamValidationException(XmlError.forMessage("")),
|
||||
true) instanceof XmlBeansValidationFailureException);
|
||||
|
||||
}
|
||||
|
||||
public void testConvertXmlException() {
|
||||
assertTrue("Invalid exception conversion", XmlBeansUtils
|
||||
.convertXmlBeansException(new XmlException(""), true) instanceof XmlBeansMarshallingFailureException);
|
||||
assertTrue("Invalid exception conversion", XmlBeansUtils.convertXmlBeansException(new XmlException(""),
|
||||
false) instanceof XmlBeansUnmarshallingFailureException);
|
||||
}
|
||||
|
||||
public void testConvertSAXException() {
|
||||
assertTrue("Invalid exception conversion", XmlBeansUtils
|
||||
.convertXmlBeansException(new SAXException(""), true) instanceof XmlBeansMarshallingFailureException);
|
||||
assertTrue("Invalid exception conversion", XmlBeansUtils.convertXmlBeansException(new SAXException(""),
|
||||
false) instanceof XmlBeansUnmarshallingFailureException);
|
||||
}
|
||||
|
||||
public void testFallbackException() {
|
||||
assertTrue("Invalid exception conversion",
|
||||
XmlBeansUtils.convertXmlBeansException(new Exception(""), false) instanceof XmlBeansSystemException);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.xmlbeans;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.xmlbeans.XmlOptions;
|
||||
|
||||
public class XmlOptionsFactoryBeanTest extends TestCase {
|
||||
|
||||
private XmlOptionsFactoryBean factoryBean;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
factoryBean = new XmlOptionsFactoryBean();
|
||||
}
|
||||
|
||||
public void testXmlOptionsFactoryBean() throws Exception {
|
||||
factoryBean.setOptions(Collections.singletonMap(XmlOptions.SAVE_PRETTY_PRINT, Boolean.TRUE));
|
||||
factoryBean.afterPropertiesSet();
|
||||
XmlOptions xmlOptions = (XmlOptions) factoryBean.getObject();
|
||||
assertNotNull("No XmlOptions returned", xmlOptions);
|
||||
assertTrue("Option not set", xmlOptions.hasOption(XmlOptions.SAVE_PRETTY_PRINT));
|
||||
assertFalse("Invalid option set", xmlOptions.hasOption(XmlOptions.LOAD_LINE_NUMBERS));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.xstream;
|
||||
|
||||
public class Flight {
|
||||
|
||||
private long flightNumber;
|
||||
|
||||
public long getFlightNumber() {
|
||||
return flightNumber;
|
||||
}
|
||||
|
||||
public void setFlightNumber(long number) {
|
||||
this.flightNumber = number;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.xstream;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Arrays;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.stream.XMLEventWriter;
|
||||
import javax.xml.stream.XMLOutputFactory;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.sax.SAXResult;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import com.thoughtworks.xstream.converters.Converter;
|
||||
import com.thoughtworks.xstream.converters.extended.EncodedByteArrayConverter;
|
||||
import org.custommonkey.xmlunit.XMLTestCase;
|
||||
import org.easymock.MockControl;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Text;
|
||||
import org.xml.sax.ContentHandler;
|
||||
|
||||
import org.springframework.xml.transform.StaxResult;
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
|
||||
public class XStreamMarshallerTest extends XMLTestCase {
|
||||
|
||||
private static final String EXPECTED_STRING = "<flight><flightNumber>42</flightNumber></flight>";
|
||||
|
||||
private XStreamMarshaller marshaller;
|
||||
|
||||
private Flight flight;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
marshaller = new XStreamMarshaller();
|
||||
Properties aliases = new Properties();
|
||||
aliases.setProperty("flight", Flight.class.getName());
|
||||
marshaller.setAliases(aliases);
|
||||
flight = new Flight();
|
||||
flight.setFlightNumber(42L);
|
||||
}
|
||||
|
||||
public void testMarshalDOMResult() throws Exception {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
|
||||
Document document = builder.newDocument();
|
||||
DOMResult domResult = new DOMResult(document);
|
||||
marshaller.marshal(flight, domResult);
|
||||
Document expected = builder.newDocument();
|
||||
Element flightElement = expected.createElement("flight");
|
||||
expected.appendChild(flightElement);
|
||||
Element numberElement = expected.createElement("flightNumber");
|
||||
flightElement.appendChild(numberElement);
|
||||
Text text = expected.createTextNode("42");
|
||||
numberElement.appendChild(text);
|
||||
assertXMLEqual("Marshaller writes invalid DOMResult", expected, document);
|
||||
}
|
||||
|
||||
public void testMarshalStreamResultWriter() throws Exception {
|
||||
StringWriter writer = new StringWriter();
|
||||
StreamResult result = new StreamResult(writer);
|
||||
marshaller.marshal(flight, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
public void testMarshalStreamResultOutputStream() throws Exception {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
StreamResult result = new StreamResult(os);
|
||||
marshaller.marshal(flight, result);
|
||||
String s = new String(os.toByteArray(), "UTF-8");
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, s);
|
||||
}
|
||||
|
||||
public void testMarshalSaxResult() throws Exception {
|
||||
MockControl handlerControl = MockControl.createStrictControl(ContentHandler.class);
|
||||
handlerControl.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
|
||||
ContentHandler handlerMock = (ContentHandler) handlerControl.getMock();
|
||||
handlerMock.startDocument();
|
||||
handlerMock.startElement("", "flight", "flight", null);
|
||||
handlerMock.startElement("", "number", "number", null);
|
||||
handlerMock.characters(new char[]{'4', '2'}, 0, 2);
|
||||
handlerMock.endElement("", "number", "number");
|
||||
handlerMock.endElement("", "flight", "flight");
|
||||
handlerMock.endDocument();
|
||||
|
||||
handlerControl.replay();
|
||||
SAXResult result = new SAXResult(handlerMock);
|
||||
marshaller.marshal(flight, result);
|
||||
handlerControl.verify();
|
||||
}
|
||||
|
||||
public void testMarshalStaxResultXMLStreamWriter() throws Exception {
|
||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
||||
StringWriter writer = new StringWriter();
|
||||
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
|
||||
StaxResult result = new StaxResult(streamWriter);
|
||||
marshaller.marshal(flight, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
public void testMarshalStaxResultXMLEventWriter() throws Exception {
|
||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
||||
StringWriter writer = new StringWriter();
|
||||
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
|
||||
StaxResult result = new StaxResult(eventWriter);
|
||||
marshaller.marshal(flight, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
public void testConverters() throws Exception {
|
||||
marshaller.setConverters(new Converter[]{new EncodedByteArrayConverter()});
|
||||
byte[] buf = new byte[]{0x1, 0x2};
|
||||
StringResult result = new StringResult();
|
||||
marshaller.marshal(buf, result);
|
||||
assertXMLEqual("<byte-array>AQI=</byte-array>", result.toString());
|
||||
StringSource source = new StringSource(result.toString());
|
||||
byte[] bufResult = (byte[]) marshaller.unmarshal(source);
|
||||
assertTrue("Invalid result", Arrays.equals(buf, bufResult));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.xstream;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.StringReader;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import org.springframework.xml.transform.StaxSource;
|
||||
|
||||
public class XStreamUnmarshallerTest extends TestCase {
|
||||
|
||||
protected static final String INPUT_STRING = "<flight><flightNumber>42</flightNumber></flight>";
|
||||
|
||||
private XStreamMarshaller unmarshaller;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
unmarshaller = new XStreamMarshaller();
|
||||
Properties aliases = new Properties();
|
||||
aliases.setProperty("flight", Flight.class.getName());
|
||||
unmarshaller.setAliases(aliases);
|
||||
}
|
||||
|
||||
private void testFlight(Object o) {
|
||||
assertTrue("Unmarshalled object is not Flights", o instanceof Flight);
|
||||
Flight flight = (Flight) o;
|
||||
assertNotNull("Flight is null", flight);
|
||||
assertEquals("Number is invalid", 42L, flight.getFlightNumber());
|
||||
}
|
||||
|
||||
public void testUnmarshalDomSource() throws Exception {
|
||||
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
Document document = builder.parse(new InputSource(new StringReader(INPUT_STRING)));
|
||||
DOMSource source = new DOMSource(document);
|
||||
Object flight = unmarshaller.unmarshal(source);
|
||||
testFlight(flight);
|
||||
}
|
||||
|
||||
public void testUnmarshalStaxSourceXmlStreamReader() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
|
||||
StaxSource source = new StaxSource(streamReader);
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlight(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalStreamSourceInputStream() throws Exception {
|
||||
StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlight(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalStreamSourceReader() throws Exception {
|
||||
StreamSource source = new StreamSource(new StringReader(INPUT_STRING));
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlight(flights);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.oxm.xstream;
|
||||
|
||||
import com.thoughtworks.xstream.alias.CannotResolveClassException;
|
||||
import com.thoughtworks.xstream.io.StreamException;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class XStreamUtilsTest extends TestCase {
|
||||
|
||||
public void testConvertStreamException() {
|
||||
assertTrue("Invalid exception conversion", XStreamUtils.convertXStreamException(
|
||||
new StreamException(new Exception()), true) instanceof XStreamMarshallingFailureException);
|
||||
assertTrue("Invalid exception conversion", XStreamUtils.convertXStreamException(
|
||||
new StreamException(new Exception()), false) instanceof XStreamUnmarshallingFailureException);
|
||||
}
|
||||
|
||||
public void testConvertCannotResolveClassException() {
|
||||
assertTrue("Invalid exception conversion", XStreamUtils.convertXStreamException(
|
||||
new CannotResolveClassException(""), true) instanceof XStreamMarshallingFailureException);
|
||||
assertTrue("Invalid exception conversion", XStreamUtils.convertXStreamException(
|
||||
new CannotResolveClassException(""), false) instanceof XStreamUnmarshallingFailureException);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user