diff --git a/spring-oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java b/spring-oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java index 131b782c87..f708c4ca9d 100644 --- a/spring-oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java +++ b/spring-oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java @@ -170,30 +170,32 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing } /** - * Set the locations of the Castor XML Mapping files. + * Set the locations of the Castor XML mapping files. */ public void setMappingLocations(Resource[] mappingLocations) { this.mappingLocations = mappingLocations; } /** - * Set the Castor target class. Alternative means of configuring {@code CastorMarshaller} for unmarshalling - * multiple classes include use of mapping files, and specifying packages with Castor descriptor classes. + * Set the Castor target class. + * @see #setTargetPackage + * @see #setMappingLocation */ public void setTargetClass(Class targetClass) { this.targetClasses = new Class[]{targetClass}; } /** - * Set the Castor target classes. Alternative means of configuring {@code CastorMarshaller} for unmarshalling - * multiple classes include use of mapping files, and specifying packages with Castor descriptor classes. + * Set the Castor target classes. + * @see #setTargetPackages + * @see #setMappingLocations */ public void setTargetClasses(Class[] targetClasses) { this.targetClasses = targetClasses; } /** - * Set the names of package with the Castor descriptor classes. + * Set the name of a package with the Castor descriptor classes. */ public void setTargetPackage(String targetPackage) { this.targetPackages = new String[] {targetPackage}; diff --git a/spring-oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java b/spring-oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java index 2ded691c8b..1faf73e06b 100644 --- a/spring-oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java +++ b/spring-oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * 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. @@ -84,6 +84,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe private static final String DEFAULT_BINDING_NAME = "binding"; + private Class> targetClass; private String targetPackage; @@ -106,13 +107,12 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe private IBindingFactory bindingFactory; - private TransformerFactory transformerFactory = TransformerFactory.newInstance(); + private final TransformerFactory transformerFactory = TransformerFactory.newInstance(); /** * Set the target class for this instance. Setting either this property or the * {@link #setTargetPackage(String) targetPackage} property is required. - * *
If this property is set, {@link #setTargetPackage(String) targetPackage} is ignored. */ public void setTargetClass(Class> targetClass) { @@ -122,7 +122,6 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe /** * Set the target package for this instance. Setting either this property or the * {@link #setTargetClass(Class) targetClass} property is required. - * *
If {@link #setTargetClass(Class) targetClass} is set, this property is ignored. */ public void setTargetPackage(String targetPackage) { @@ -157,10 +156,9 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe } /** - * Sets the root element name for the DTD declaration written when marshalling. By default, this is - * {@code null} (i.e. no DTD declaration is written). If set to a value, the system ID or public ID also need to - * be set. - * + * Set the root element name for the DTD declaration written when marshalling. + * By default, this is {@code null} (i.e. no DTD declaration is written). + *
If set to a value, the system ID or public ID also need to be set. * @see #setDocTypeSystemId(String) * @see #setDocTypePublicId(String) */ @@ -169,10 +167,9 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe } /** - * Sets the system Id for the DTD declaration written when marshalling. By default, this is - * {@code null}. Only used when the root element also has been set. Set either this property or - * {@code docTypePublicId}, not both. - * + * Set the system id for the DTD declaration written when marshalling. + * By default, this is {@code null}. Only used when the root element also has been set. + *
Set either this property or {@code docTypePublicId}, not both. * @see #setDocTypeRootElementName(String) */ public void setDocTypeSystemId(String docTypeSystemId) { @@ -180,10 +177,9 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe } /** - * Sets the public Id for the DTD declaration written when marshalling. By default, this is - * {@code null}. Only used when the root element also has been set. Set either this property or - * {@code docTypeSystemId}, not both. - * + * Set the public id for the DTD declaration written when marshalling. + * By default, this is {@code null}. Only used when the root element also has been set. + *
Set either this property or {@code docTypeSystemId}, not both. * @see #setDocTypeRootElementName(String) */ public void setDocTypePublicId(String docTypePublicId) { @@ -191,15 +187,15 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe } /** - * Sets the internal subset Id for the DTD declaration written when marshalling. By default, this is - * {@code null}. Only used when the root element also has been set. - * + * Set the internal subset Id for the DTD declaration written when marshalling. + * By default, this is {@code null}. Only used when the root element also has been set. * @see #setDocTypeRootElementName(String) */ public void setDocTypeInternalSubset(String docTypeInternalSubset) { this.docTypeInternalSubset = docTypeInternalSubset; } + public void afterPropertiesSet() throws JiBXException { if (this.targetClass != null) { if (StringUtils.hasLength(this.bindingName)) { @@ -214,16 +210,18 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe } this.bindingFactory = BindingDirectory.getFactory(this.targetClass); } - } else if (this.targetPackage != null) { + } + else if (this.targetPackage != null) { if (!StringUtils.hasLength(bindingName)) { bindingName = DEFAULT_BINDING_NAME; } if (logger.isInfoEnabled()) { - logger.info("Configured for target package [" + targetPackage + "] using binding [" + bindingName + "]"); + logger.info("Configured for target package [" + this.targetPackage + "] using binding [" + this.bindingName + "]"); } - this.bindingFactory = BindingDirectory.getFactory(bindingName, targetPackage); - } else { - throw new IllegalArgumentException("either 'targetClass' or 'targetPackage' is required"); + this.bindingFactory = BindingDirectory.getFactory(this.bindingName, this.targetPackage); + } + else { + throw new IllegalArgumentException("Either 'targetClass' or 'targetPackage' is required"); } } @@ -244,7 +242,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe } - // Supported Marshalling + // Supported marshalling @Override protected void marshalOutputStream(Object graph, OutputStream outputStream) @@ -271,8 +269,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe } } - private void marshalDocument(IMarshallingContext marshallingContext, Object graph) throws IOException, - JiBXException { + private void marshalDocument(IMarshallingContext marshallingContext, Object graph) throws IOException, JiBXException { if (StringUtils.hasLength(docTypeRootElementName)) { IXMLWriter xmlWriter = marshallingContext.getXmlWriter(); xmlWriter.writeDocType(docTypeRootElementName, docTypeSystemId, docTypePublicId, docTypeInternalSubset); @@ -280,6 +277,27 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe marshallingContext.marshalDocument(graph); } + + // Unsupported marshalling + + @Override + protected void marshalDomNode(Object graph, Node node) throws XmlMappingException { + try { + // JiBX does not support DOM natively, so we write to a buffer first, and transform that to the Node + Result result = new DOMResult(node); + transformAndMarshal(graph, result); + } + catch (IOException ex) { + throw new MarshallingFailureException("JiBX marshalling exception", ex); + } + } + + @Override + protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) { + XMLStreamWriter streamWriter = StaxUtils.createEventStreamWriter(eventWriter); + marshalXmlStreamWriter(graph, streamWriter); + } + @Override protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException { try { @@ -293,20 +311,6 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe } } - // Unsupported Marshalling - - @Override - protected void marshalDomNode(Object graph, Node node) throws XmlMappingException { - try { - // JiBX does not support DOM natively, so we write to a buffer first, and transform that to the Node - Result result = new DOMResult(node); - transformAndMarshal(graph, result); - } - catch (IOException ex) { - throw new MarshallingFailureException("JiBX marshalling exception", ex); - } - } - @Override protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler) throws XmlMappingException { @@ -336,15 +340,33 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe } - @Override - protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) { - XMLStreamWriter streamWriter = StaxUtils.createEventStreamWriter(eventWriter); - marshalXmlStreamWriter(graph, streamWriter); - } - // Unmarshalling + @Override + protected Object unmarshalXmlEventReader(XMLEventReader eventReader) { + try { + XMLStreamReader streamReader = StaxUtils.createEventStreamReader(eventReader); + return unmarshalXmlStreamReader(streamReader); + } + catch (XMLStreamException ex) { + return new UnmarshallingFailureException("JiBX unmarshalling exception", ex); + } + } + + @Override + protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) { + try { + UnmarshallingContext unmarshallingContext = (UnmarshallingContext) createUnmarshallingContext(); + IXMLReader xmlReader = new StAXReaderWrapper(streamReader, null, true); + unmarshallingContext.setDocument(xmlReader); + return unmarshallingContext.unmarshalElement(); + } + catch (JiBXException ex) { + throw convertJibxException(ex, false); + } + } + @Override protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException { try { @@ -367,30 +389,6 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe } } - @Override - protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) { - try { - UnmarshallingContext unmarshallingContext = (UnmarshallingContext) createUnmarshallingContext(); - IXMLReader xmlReader = new StAXReaderWrapper(streamReader, null, true); - unmarshallingContext.setDocument(xmlReader); - return unmarshallingContext.unmarshalElement(); - } - catch (JiBXException ex) { - throw convertJibxException(ex, false); - } - } - - @Override - protected Object unmarshalXmlEventReader(XMLEventReader eventReader) { - try { - XMLStreamReader streamReader = StaxUtils.createEventStreamReader(eventReader); - return unmarshalXmlStreamReader(streamReader); - } - catch (XMLStreamException ex) { - return new UnmarshallingFailureException("JiBX unmarshalling exception", ex); - } - } - // Unsupported Unmarshalling @@ -407,12 +405,13 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe @Override protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) throws XmlMappingException, IOException { + return transformAndUnmarshal(new SAXSource(xmlReader, inputSource)); } private Object transformAndUnmarshal(Source source) throws IOException { try { - Transformer transformer = transformerFactory.newTransformer(); + Transformer transformer = this.transformerFactory.newTransformer(); ByteArrayOutputStream os = new ByteArrayOutputStream(); transformer.transform(source, new StreamResult(os)); ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());