11 changed files with 8 additions and 879 deletions
@ -1,49 +0,0 @@
@@ -1,49 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2013 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.config; |
||||
|
||||
import org.w3c.dom.Element; |
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
||||
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; |
||||
import org.springframework.beans.factory.xml.ParserContext; |
||||
import org.springframework.util.StringUtils; |
||||
|
||||
/** |
||||
* Parser for the {@code <oxm:xmlbeans-marshaller/>} element. |
||||
* |
||||
* @author Arjen Poutsma |
||||
* @since 3.0 |
||||
* @deprecated as of Spring 4.2, following the XMLBeans retirement at Apache |
||||
*/ |
||||
@Deprecated |
||||
class XmlBeansMarshallerBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { |
||||
|
||||
@Override |
||||
protected String getBeanClassName(Element element) { |
||||
return "org.springframework.oxm.xmlbeans.XmlBeansMarshaller"; |
||||
} |
||||
|
||||
@Override |
||||
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder beanDefinitionBuilder) { |
||||
String optionsName = element.getAttribute("options"); |
||||
if (StringUtils.hasText(optionsName)) { |
||||
beanDefinitionBuilder.addPropertyReference("xmlOptions", optionsName); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -1,489 +0,0 @@
@@ -1,489 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2014 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.IOException; |
||||
import java.io.InputStream; |
||||
import java.io.OutputStream; |
||||
import java.io.Reader; |
||||
import java.io.Writer; |
||||
import java.lang.ref.WeakReference; |
||||
import java.nio.CharBuffer; |
||||
import java.util.ArrayList; |
||||
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.XMLStreamValidationException; |
||||
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.Marshaller; |
||||
import org.springframework.oxm.MarshallingFailureException; |
||||
import org.springframework.oxm.UncategorizedMappingException; |
||||
import org.springframework.oxm.UnmarshallingFailureException; |
||||
import org.springframework.oxm.ValidationFailureException; |
||||
import org.springframework.oxm.XmlMappingException; |
||||
import org.springframework.oxm.support.AbstractMarshaller; |
||||
import org.springframework.util.xml.StaxUtils; |
||||
|
||||
/** |
||||
* Implementation of the {@link Marshaller} interface for Apache XMLBeans. |
||||
* |
||||
* <p>Options can be set by setting the {@code xmlOptions} property. |
||||
* The {@link XmlOptionsFactoryBean} is provided to easily set up an {@link XmlOptions} instance. |
||||
* |
||||
* <p>Unmarshalled objects can be validated by setting the {@code validating} property, |
||||
* or by calling the {@link #validate(XmlObject)} method directly. Invalid objects will |
||||
* result in an {@link ValidationFailureException}. |
||||
* |
||||
* <p><b>NOTE:</b> Due to the nature of XMLBeans, this marshaller requires |
||||
* all passed objects to be of type {@link XmlObject}. |
||||
* |
||||
* @author Arjen Poutsma |
||||
* @since 3.0 |
||||
* @see #setValidating |
||||
* @see #setXmlOptions |
||||
* @see XmlOptionsFactoryBean |
||||
* @deprecated as of Spring 4.2, following the XMLBeans retirement at Apache |
||||
*/ |
||||
@Deprecated |
||||
public class XmlBeansMarshaller extends AbstractMarshaller { |
||||
|
||||
private XmlOptions xmlOptions; |
||||
|
||||
private boolean validating = false; |
||||
|
||||
|
||||
/** |
||||
* Set the {@code XmlOptions}. |
||||
* @see XmlOptionsFactoryBean |
||||
*/ |
||||
public void setXmlOptions(XmlOptions xmlOptions) { |
||||
this.xmlOptions = xmlOptions; |
||||
} |
||||
|
||||
/** |
||||
* Return the {@code XmlOptions}. |
||||
*/ |
||||
public XmlOptions getXmlOptions() { |
||||
return this.xmlOptions; |
||||
} |
||||
|
||||
/** |
||||
* Set whether this marshaller should validate in- and outgoing documents. |
||||
* Default is {@code false}. |
||||
*/ |
||||
public void setValidating(boolean validating) { |
||||
this.validating = validating; |
||||
} |
||||
|
||||
/** |
||||
* Return whether this marshaller should validate in- and outgoing documents. |
||||
*/ |
||||
public boolean isValidating() { |
||||
return this.validating; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* This implementation returns true if the given class is an implementation of {@link XmlObject}. |
||||
*/ |
||||
@Override |
||||
public boolean supports(Class<?> clazz) { |
||||
return XmlObject.class.isAssignableFrom(clazz); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
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(getXmlOptions()); |
||||
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); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) { |
||||
ContentHandler contentHandler = StaxUtils.createContentHandler(eventWriter); |
||||
LexicalHandler lexicalHandler = null; |
||||
if (contentHandler instanceof LexicalHandler) { |
||||
lexicalHandler = (LexicalHandler) contentHandler; |
||||
} |
||||
marshalSaxHandlers(graph, contentHandler, lexicalHandler); |
||||
} |
||||
|
||||
@Override |
||||
protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException { |
||||
ContentHandler contentHandler = StaxUtils.createContentHandler(streamWriter); |
||||
LexicalHandler lexicalHandler = null; |
||||
if (contentHandler instanceof LexicalHandler) { |
||||
lexicalHandler = (LexicalHandler) contentHandler; |
||||
} |
||||
marshalSaxHandlers(graph, contentHandler, lexicalHandler); |
||||
} |
||||
|
||||
@Override |
||||
protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler) |
||||
throws XmlMappingException { |
||||
try { |
||||
((XmlObject) graph).save(contentHandler, lexicalHandler, getXmlOptions()); |
||||
} |
||||
catch (SAXException ex) { |
||||
throw convertXmlBeansException(ex, true); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
protected void marshalOutputStream(Object graph, OutputStream outputStream) |
||||
throws XmlMappingException, IOException { |
||||
|
||||
((XmlObject) graph).save(outputStream, getXmlOptions()); |
||||
} |
||||
|
||||
@Override |
||||
protected void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException { |
||||
((XmlObject) graph).save(writer, getXmlOptions()); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
protected Object unmarshalDomNode(Node node) throws XmlMappingException { |
||||
try { |
||||
XmlObject object = XmlObject.Factory.parse(node, getXmlOptions()); |
||||
validate(object); |
||||
return object; |
||||
} |
||||
catch (XmlException ex) { |
||||
throw convertXmlBeansException(ex, false); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
protected Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlMappingException { |
||||
XMLReader reader = StaxUtils.createXMLReader(eventReader); |
||||
try { |
||||
return unmarshalSaxReader(reader, new InputSource()); |
||||
} |
||||
catch (IOException ex) { |
||||
throw convertXmlBeansException(ex, false); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) throws XmlMappingException { |
||||
try { |
||||
XmlObject object = XmlObject.Factory.parse(streamReader, getXmlOptions()); |
||||
validate(object); |
||||
return object; |
||||
} |
||||
catch (XmlException ex) { |
||||
throw convertXmlBeansException(ex, false); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) |
||||
throws XmlMappingException, IOException { |
||||
|
||||
XmlSaxHandler saxHandler = XmlObject.Factory.newXmlSaxHandler(getXmlOptions()); |
||||
xmlReader.setContentHandler(saxHandler.getContentHandler()); |
||||
try { |
||||
xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", saxHandler.getLexicalHandler()); |
||||
} |
||||
catch (SAXNotRecognizedException ex) { |
||||
// ignore
|
||||
} |
||||
catch (SAXNotSupportedException ex) { |
||||
// ignore
|
||||
} |
||||
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); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException { |
||||
try { |
||||
InputStream nonClosingInputStream = new NonClosingInputStream(inputStream); |
||||
XmlObject object = XmlObject.Factory.parse(nonClosingInputStream, getXmlOptions()); |
||||
validate(object); |
||||
return object; |
||||
} |
||||
catch (XmlException ex) { |
||||
throw convertXmlBeansException(ex, false); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
protected Object unmarshalReader(Reader reader) throws XmlMappingException, IOException { |
||||
try { |
||||
Reader nonClosingReader = new NonClosingReader(reader); |
||||
XmlObject object = XmlObject.Factory.parse(nonClosingReader, getXmlOptions()); |
||||
validate(object); |
||||
return object; |
||||
} |
||||
catch (XmlException ex) { |
||||
throw convertXmlBeansException(ex, false); |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Validate the given {@code XmlObject}. |
||||
* @param object the xml object to validate |
||||
* @throws ValidationFailureException if the given object is not valid |
||||
*/ |
||||
protected void validate(XmlObject object) throws ValidationFailureException { |
||||
if (isValidating() && object != null) { |
||||
XmlOptions validateOptions = getXmlOptions(); |
||||
if (validateOptions == null) { |
||||
// Create temporary XmlOptions just for validation
|
||||
validateOptions = new XmlOptions(); |
||||
} |
||||
List<XmlError> errorsList = new ArrayList<XmlError>(); |
||||
validateOptions.setErrorListener(errorsList); |
||||
if (!object.validate(validateOptions)) { |
||||
StringBuilder sb = new StringBuilder("Failed to validate XmlObject: "); |
||||
boolean first = true; |
||||
for (XmlError error : errorsList) { |
||||
if (error instanceof XmlValidationError) { |
||||
if (!first) { |
||||
sb.append("; "); |
||||
} |
||||
sb.append(error.toString()); |
||||
first = false; |
||||
} |
||||
} |
||||
throw new ValidationFailureException("XMLBeans validation failure", |
||||
new XmlException(sb.toString(), null, errorsList)); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Convert the given XMLBeans exception to an appropriate exception from the |
||||
* {@code org.springframework.oxm} hierarchy. |
||||
* <p>A boolean flag is used to indicate whether this exception occurs during marshalling or |
||||
* unmarshalling, since 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}), |
||||
* or unmarshalling ({@code false}) |
||||
* @return the corresponding {@code XmlMappingException} |
||||
*/ |
||||
protected XmlMappingException convertXmlBeansException(Exception ex, boolean marshalling) { |
||||
if (ex instanceof XMLStreamValidationException) { |
||||
return new ValidationFailureException("XMLBeans validation exception", ex); |
||||
} |
||||
else if (ex instanceof XmlException || ex instanceof SAXException) { |
||||
if (marshalling) { |
||||
return new MarshallingFailureException("XMLBeans marshalling exception", ex); |
||||
} |
||||
else { |
||||
return new UnmarshallingFailureException("XMLBeans unmarshalling exception", ex); |
||||
} |
||||
} |
||||
else { |
||||
// fallback
|
||||
return new UncategorizedMappingException("Unknown XMLBeans exception", ex); |
||||
} |
||||
} |
||||
|
||||
|
||||
private static class NonClosingInputStream extends InputStream { |
||||
|
||||
private final WeakReference<InputStream> in; |
||||
|
||||
public NonClosingInputStream(InputStream in) { |
||||
this.in = new WeakReference<InputStream>(in); |
||||
} |
||||
|
||||
private InputStream getInputStream() { |
||||
return this.in.get(); |
||||
} |
||||
|
||||
@Override |
||||
public int read() throws IOException { |
||||
InputStream in = getInputStream(); |
||||
return (in != null ? in.read() : -1); |
||||
} |
||||
|
||||
@Override |
||||
public int read(byte[] b) throws IOException { |
||||
InputStream in = getInputStream(); |
||||
return (in != null ? in.read(b) : -1); |
||||
} |
||||
|
||||
@Override |
||||
public int read(byte[] b, int off, int len) throws IOException { |
||||
InputStream in = getInputStream(); |
||||
return (in != null ? in.read(b, off, len) : -1); |
||||
} |
||||
|
||||
@Override |
||||
public long skip(long n) throws IOException { |
||||
InputStream in = getInputStream(); |
||||
return (in != null ? in.skip(n) : 0); |
||||
} |
||||
|
||||
@Override |
||||
public boolean markSupported() { |
||||
InputStream in = getInputStream(); |
||||
return (in != null && in.markSupported()); |
||||
} |
||||
|
||||
@Override |
||||
public void mark(int readlimit) { |
||||
InputStream in = getInputStream(); |
||||
if (in != null) { |
||||
in.mark(readlimit); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void reset() throws IOException { |
||||
InputStream in = getInputStream(); |
||||
if (in != null) { |
||||
in.reset(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public int available() throws IOException { |
||||
InputStream in = getInputStream(); |
||||
return (in != null ? in.available() : 0); |
||||
} |
||||
|
||||
@Override |
||||
public void close() throws IOException { |
||||
InputStream in = getInputStream(); |
||||
if (in != null) { |
||||
this.in.clear(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
private static class NonClosingReader extends Reader { |
||||
|
||||
private final WeakReference<Reader> reader; |
||||
|
||||
public NonClosingReader(Reader reader) { |
||||
this.reader = new WeakReference<Reader>(reader); |
||||
} |
||||
|
||||
private Reader getReader() { |
||||
return this.reader.get(); |
||||
} |
||||
|
||||
@Override |
||||
public int read(CharBuffer target) throws IOException { |
||||
Reader rdr = getReader(); |
||||
return (rdr != null ? rdr.read(target) : -1); |
||||
} |
||||
|
||||
@Override |
||||
public int read() throws IOException { |
||||
Reader rdr = getReader(); |
||||
return (rdr != null ? rdr.read() : -1); |
||||
} |
||||
|
||||
@Override |
||||
public int read(char[] cbuf) throws IOException { |
||||
Reader rdr = getReader(); |
||||
return (rdr != null ? rdr.read(cbuf) : -1); |
||||
} |
||||
|
||||
@Override |
||||
public int read(char[] cbuf, int off, int len) throws IOException { |
||||
Reader rdr = getReader(); |
||||
return (rdr != null ? rdr.read(cbuf, off, len) : -1); |
||||
} |
||||
|
||||
@Override |
||||
public long skip(long n) throws IOException { |
||||
Reader rdr = getReader(); |
||||
return (rdr != null ? rdr.skip(n) : 0); |
||||
} |
||||
|
||||
@Override |
||||
public boolean ready() throws IOException { |
||||
Reader rdr = getReader(); |
||||
return (rdr != null && rdr.ready()); |
||||
} |
||||
|
||||
@Override |
||||
public boolean markSupported() { |
||||
Reader rdr = getReader(); |
||||
return (rdr != null && rdr.markSupported()); |
||||
} |
||||
|
||||
@Override |
||||
public void mark(int readAheadLimit) throws IOException { |
||||
Reader rdr = getReader(); |
||||
if (rdr != null) { |
||||
rdr.mark(readAheadLimit); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void reset() throws IOException { |
||||
Reader rdr = getReader(); |
||||
if (rdr != null) { |
||||
rdr.reset(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void close() throws IOException { |
||||
Reader rdr = getReader(); |
||||
if (rdr != null) { |
||||
this.reader.clear(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -1,77 +0,0 @@
@@ -1,77 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2014 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.Map; |
||||
|
||||
import org.apache.xmlbeans.XmlOptions; |
||||
|
||||
import org.springframework.beans.factory.FactoryBean; |
||||
|
||||
/** |
||||
* {@link FactoryBean} that configures an XMLBeans {@code XmlOptions} 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 {@link XmlBeansMarshaller}. |
||||
* |
||||
* @author Arjen Poutsma |
||||
* @author Juergen Hoeller |
||||
* @since 3.0 |
||||
* @see XmlOptions |
||||
* @see #setOptions(java.util.Map) |
||||
* @see XmlBeansMarshaller#setXmlOptions(XmlOptions) |
||||
*/ |
||||
public class XmlOptionsFactoryBean implements FactoryBean<XmlOptions> { |
||||
|
||||
private XmlOptions xmlOptions = new XmlOptions(); |
||||
|
||||
|
||||
/** |
||||
* Set options on the underlying {@code XmlOptions} object. |
||||
* <p>The keys of the supplied map should be one of the String constants |
||||
* defined in {@code XmlOptions}, the values vary per option. |
||||
* @see XmlOptions#put(Object, Object) |
||||
* @see XmlOptions#SAVE_PRETTY_PRINT |
||||
* @see XmlOptions#LOAD_STRIP_COMMENTS |
||||
*/ |
||||
public void setOptions(Map<String, ?> optionsMap) { |
||||
this.xmlOptions = new XmlOptions(); |
||||
if (optionsMap != null) { |
||||
for (Map.Entry<String, ?> option : optionsMap.entrySet()) { |
||||
this.xmlOptions.put(option.getKey(), option.getValue()); |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public XmlOptions getObject() { |
||||
return this.xmlOptions; |
||||
} |
||||
|
||||
@Override |
||||
public Class<? extends XmlOptions> getObjectType() { |
||||
return XmlOptions.class; |
||||
} |
||||
|
||||
@Override |
||||
public boolean isSingleton() { |
||||
return true; |
||||
} |
||||
|
||||
} |
||||
@ -1,5 +0,0 @@
@@ -1,5 +0,0 @@
|
||||
/** |
||||
* Package providing integration of <a href="http://xmlbeans.apache.org/">XMLBeans</a> |
||||
* with Spring's O/X Mapping support. |
||||
*/ |
||||
package org.springframework.oxm.xmlbeans; |
||||
@ -1,68 +0,0 @@
@@ -1,68 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2015 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.apache.xmlbeans.XmlObject; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.oxm.AbstractMarshallerTests; |
||||
import org.springframework.samples.flight.FlightType; |
||||
import org.springframework.samples.flight.FlightsDocument; |
||||
|
||||
import static org.junit.Assert.*; |
||||
|
||||
/** |
||||
* @author Arjen Poutsma |
||||
* @author Sam Brannen |
||||
*/ |
||||
@SuppressWarnings("deprecation") |
||||
public class XmlBeansMarshallerTests extends |
||||
AbstractMarshallerTests<org.springframework.oxm.xmlbeans.XmlBeansMarshaller> { |
||||
|
||||
@Override |
||||
protected XmlBeansMarshaller createMarshaller() throws Exception { |
||||
return new XmlBeansMarshaller(); |
||||
} |
||||
|
||||
@Override |
||||
protected Object createFlights() { |
||||
FlightsDocument flightsDocument = FlightsDocument.Factory.newInstance(); |
||||
FlightsDocument.Flights flights = flightsDocument.addNewFlights(); |
||||
FlightType flightType = flights.addNewFlight(); |
||||
flightType.setNumber(42L); |
||||
return flightsDocument; |
||||
} |
||||
|
||||
@Test(expected = ClassCastException.class) |
||||
public void marshalNonXmlObject() throws Exception { |
||||
marshaller.marshal(new Object(), new StreamResult(new ByteArrayOutputStream())); |
||||
} |
||||
|
||||
@Test |
||||
public void supports() throws Exception { |
||||
assertTrue("XmlBeansMarshaller does not support XmlObject", marshaller.supports(XmlObject.class)); |
||||
assertFalse("XmlBeansMarshaller supports other objects", marshaller.supports(Object.class)); |
||||
assertTrue("XmlBeansMarshaller does not support FlightsDocument", marshaller.supports(FlightsDocument.class)); |
||||
assertTrue("XmlBeansMarshaller does not support Flights", marshaller.supports(FlightsDocument.Flights.class)); |
||||
assertTrue("XmlBeansMarshaller does not support FlightType", marshaller.supports(FlightType.class)); |
||||
} |
||||
|
||||
} |
||||
@ -1,97 +0,0 @@
@@ -1,97 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2015 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.StringReader; |
||||
|
||||
import javax.xml.namespace.QName; |
||||
import javax.xml.stream.XMLInputFactory; |
||||
import javax.xml.stream.XMLStreamReader; |
||||
import javax.xml.transform.Source; |
||||
import javax.xml.transform.stream.StreamSource; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.oxm.AbstractUnmarshallerTests; |
||||
import org.springframework.oxm.ValidationFailureException; |
||||
import org.springframework.samples.flight.FlightDocument; |
||||
import org.springframework.samples.flight.FlightType; |
||||
import org.springframework.samples.flight.FlightsDocument; |
||||
import org.springframework.util.xml.StaxUtils; |
||||
|
||||
import static org.junit.Assert.*; |
||||
|
||||
/** |
||||
* @author Arjen Poutsma |
||||
*/ |
||||
@SuppressWarnings("deprecation") |
||||
public class XmlBeansUnmarshallerTests extends |
||||
AbstractUnmarshallerTests<org.springframework.oxm.xmlbeans.XmlBeansMarshaller> { |
||||
|
||||
@Override |
||||
protected XmlBeansMarshaller createUnmarshaller() throws Exception { |
||||
return new XmlBeansMarshaller(); |
||||
} |
||||
|
||||
@Override |
||||
protected void testFlights(Object o) { |
||||
FlightsDocument flightsDocument = (FlightsDocument) o; |
||||
assertNotNull("FlightsDocument is null", flightsDocument); |
||||
FlightsDocument.Flights flights = flightsDocument.getFlights(); |
||||
assertEquals("Invalid amount of flight elements", 1, flights.sizeOfFlightArray()); |
||||
testFlight(flights.getFlightArray(0)); |
||||
} |
||||
|
||||
@Override |
||||
protected void testFlight(Object o) { |
||||
FlightType flight = null; |
||||
if (o instanceof FlightType) { |
||||
flight = (FlightType) o; |
||||
} |
||||
else if (o instanceof FlightDocument) { |
||||
FlightDocument flightDocument = (FlightDocument) o; |
||||
flight = flightDocument.getFlight(); |
||||
} |
||||
assertNotNull("Flight is null", flight); |
||||
assertEquals("Number is invalid", 42L, flight.getNumber()); |
||||
} |
||||
|
||||
@Test |
||||
@Override |
||||
public void unmarshalPartialStaxSourceXmlStreamReader() throws Exception { |
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance(); |
||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING)); |
||||
streamReader.nextTag(); // skip to flights
|
||||
assertEquals("Invalid element", new QName("http://samples.springframework.org/flight", "flights"), |
||||
streamReader.getName()); |
||||
streamReader.nextTag(); // skip to flight
|
||||
assertEquals("Invalid element", new QName("http://samples.springframework.org/flight", "flight"), |
||||
streamReader.getName()); |
||||
Source source = StaxUtils.createStaxSource(streamReader); |
||||
Object flight = unmarshaller.unmarshal(source); |
||||
testFlight(flight); |
||||
} |
||||
|
||||
@Test(expected = ValidationFailureException.class) |
||||
public void validate() throws Exception { |
||||
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>"; |
||||
unmarshaller.unmarshal(new StreamSource(new StringReader(invalidInput))); |
||||
} |
||||
|
||||
} |
||||
@ -1,42 +0,0 @@
@@ -1,42 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 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 org.apache.xmlbeans.XmlOptions; |
||||
import org.junit.Test; |
||||
|
||||
import static org.junit.Assert.*; |
||||
|
||||
/** |
||||
* @author Arjen Poutsma |
||||
*/ |
||||
public class XmlOptionsFactoryBeanTests { |
||||
|
||||
private XmlOptionsFactoryBean factoryBean = new XmlOptionsFactoryBean(); |
||||
|
||||
@Test |
||||
public void xmlOptionsFactoryBean() throws Exception { |
||||
factoryBean.setOptions(Collections.singletonMap(XmlOptions.SAVE_PRETTY_PRINT, Boolean.TRUE)); |
||||
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)); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue