From dd8a9cce053f957ee43837a4470cae7d81b2fb24 Mon Sep 17 00:00:00 2001
From: Arjen Poutsma If a target class is specified using Default is Default is Default is Default is
+ * Default is Default
+ * is Default is Default is
+ * Marshaller interface for Castor. By default, Castor does not require any further
- * configuration, though setting target classes, target packages or providing a mapping file can be used to have more control over the
- * behavior of Castor.
+ * configuration, though setting target classes, target packages or providing a mapping file can be used to have more
+ * control over the behavior of Castor.
*
* setTargetClass, the CastorMarshaller can only be
* used to unmarshall XML that represents that specific class. If you want to unmarshall multiple classes, you have to
@@ -72,6 +72,7 @@ import org.springframework.util.xml.StaxUtils;
* defaults to UTF-8.
*
* @author Arjen Poutsma
+ * @author Jakub Narloch
* @see #setEncoding(String)
* @see #setTargetClass(Class)
* @see #setTargetPackages(String[])
@@ -86,7 +87,6 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
*/
public static final String DEFAULT_ENCODING = "UTF-8";
-
private Resource[] mappingLocations;
private String encoding = DEFAULT_ENCODING;
@@ -111,9 +111,23 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
private boolean suppressXsiType = false;
+ private boolean marshalAsDocument = true;
+
+ private String rootElement;
+
+ private boolean marshalExtendedType = true;
+
+ private String noNamespaceSchemaLocation;
+
+ private String schemaLocation;
+
+ private boolean useXSITypeAtRoot = false;
+
+ private MapCastorMarshaller for unmarshalling multiple classes include
- * use of mapping files, and specifying packages with Castor descriptor classes.
+ * Set the Castor target class. Alternative means of configuring CastorMarshaller for unmarshalling
+ * multiple classes include use of mapping files, and specifying packages with Castor descriptor classes.
*/
public void setTargetClass(Class targetClass) {
this.targetClasses = new Class[]{targetClass};
}
/**
- * Set the Castor target classes. Alternative means of configuring
- * CastorMarshaller for unmarshalling multiple classes include
- * use of mapping files, and specifying packages with Castor descriptor classes.
+ * Set the Castor target classes. Alternative means of configuring CastorMarshaller for unmarshalling
+ * multiple classes include use of mapping files, and specifying packages with Castor descriptor classes.
*/
public void setTargetClasses(Class[] targetClasses) {
this.targetClasses = targetClasses;
@@ -160,8 +172,8 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
}
/**
- * Set whether this marshaller should validate in- and outgoing documents.
- * false.
+ * Set whether this marshaller should validate in- and outgoing documents. false.
+ *
* @see Marshaller#setValidation(boolean)
*/
public void setValidating(boolean validating) {
@@ -169,8 +181,9 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
}
/**
- * Set whether the Castor {@link Unmarshaller} should preserve "ignorable" whitespace.
- * false.
+ * Set whether the Castor {@link Unmarshaller} should preserve "ignorable" whitespace. false.
+ *
* @see org.exolab.castor.xml.Unmarshaller#setWhitespacePreserve(boolean)
*/
public void setWhitespacePreserve(boolean whitespacePreserve) {
@@ -178,8 +191,9 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
}
/**
- * Set whether the Castor {@link Unmarshaller} should ignore attributes that do not match a specific field.
- * true: extra attributes are ignored.
+ * Set whether the Castor {@link Unmarshaller} should ignore attributes that do not match a specific field. true: extra attributes are ignored.
+ *
* @see org.exolab.castor.xml.Unmarshaller#setIgnoreExtraAttributes(boolean)
*/
public void setIgnoreExtraAttributes(boolean ignoreExtraAttributes) {
@@ -187,8 +201,9 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
}
/**
- * Set whether the Castor {@link Unmarshaller} should ignore elements that do not match a specific field.
- * false, extra attributes are flagged as an error.
+ * Set whether the Castor {@link Unmarshaller} should ignore elements that do not match a specific field. false, extra attributes are flagged as an error.
+ *
* @see org.exolab.castor.xml.Unmarshaller#setIgnoreExtraElements(boolean)
*/
public void setIgnoreExtraElements(boolean ignoreExtraElements) {
@@ -197,20 +212,22 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
/**
* Set the namespace mappings. Property names are interpreted as namespace prefixes; values are namespace URIs.
+ *
* @see org.exolab.castor.xml.Marshaller#setNamespaceMapping(String, String)
*/
public void setNamespaceMappings(Map
XMLContext. Subclasses can override this to create a custom context.
- *
- * The default implementation loads mapping files if defined, or the target class or packages if defined.
+ * Create the Castor XMLContext. Subclasses can override this to create a custom context.
The default + * implementation loads mapping files if defined, or the target class or packages if defined. * * @return the created resolver * @throws MappingException when the mapping file cannot be loaded - * @throws IOException in case of I/O errors + * @throws IOException in case of I/O errors * @see XMLContext#addMapping(org.exolab.castor.mapping.Mapping) * @see XMLContext#addClass(Class) */ @@ -351,15 +439,31 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing } /** - * Template method that allows for customizing of the given Castor {@link Marshaller}. - *
The default implementation invokes {@link Marshaller#setValidation(boolean)} - * with the property set on this marshaller, and calls {@link Marshaller#setNamespaceMapping(String, String)} - * with the {@linkplain #setNamespaceMappings(java.util.Map) namespace mappings}. + * Template method that allows for customizing of the given Castor {@link Marshaller}.
The default implementation + * invokes {@link Marshaller#setValidation(boolean)}, {@link Marshaller#setSuppressNamespaces(boolean)}, {@link + * Marshaller#setSuppressXSIType(boolean)}, {@link Marshaller#setMarshalAsDocument(boolean)}, {@link + * Marshaller#setRootElement(String)}, {@link Marshaller#setMarshalExtendedType(boolean)}, {@link + * Marshaller#setNoNamespaceSchemaLocation(String)}, {@link Marshaller#setSchemaLocation(String)} and {@link + * Marshaller#setUseXSITypeAtRoot(boolean)}, with the property set on this marshaller, it also calls {@link + * Marshaller#setNamespaceMapping(String, String)} with the {@linkplain #setNamespaceMappings(java.util.Map) namespace + * mappings} and {@link Marshaller#addProcessingInstruction(String, String)} with the {@linkplain + * #setProcessingInstructions(java.util.Map) processing instructions}. */ protected void customizeMarshaller(Marshaller marshaller) { marshaller.setValidation(this.validating); marshaller.setSuppressNamespaces(isSuppressNamespaces()); marshaller.setSuppressXSIType(isSuppressXsiType()); + marshaller.setMarshalAsDocument(marshalAsDocument); + marshaller.setRootElement(rootElement); + marshaller.setMarshalExtendedType(marshalExtendedType); + marshaller.setNoNamespaceSchemaLocation(noNamespaceSchemaLocation); + marshaller.setSchemaLocation(schemaLocation); + marshaller.setUseXSITypeAtRoot(useXSITypeAtRoot); + if (processingInstructions != null) { + for (Map.Entry- * The default implementation invokes - * {@link Unmarshaller#setValidation(boolean)}, - * {@link Unmarshaller#setWhitespacePreserve(boolean)}, - * {@link Unmarshaller#setIgnoreExtraAttributes(boolean)}, and - * {@link Unmarshaller#setIgnoreExtraElements(boolean)} with the properties - * set on this marshaller. + * Template method that allows for customizing of the given Castor {@link Unmarshaller}.
The default + * implementation invokes {@link Unmarshaller#setValidation(boolean)}, {@link Unmarshaller#setWhitespacePreserve(boolean)}, + * {@link Unmarshaller#setIgnoreExtraAttributes(boolean)}, {@link Unmarshaller#setIgnoreExtraElements(boolean)}, {@link + * Unmarshaller#setClassLoader(ClassLoader)}, {@link Unmarshaller#setObject(Object)}, {@link + * Unmarshaller#setReuseObjects(boolean)} and {@link Unmarshaller#setClearCollections(boolean)} with the properties set + * on this marshaller, it also calls {@link Unmarshaller#addNamespaceToPackageMapping(String, String)} with the + * {@linkplain #setNamespaceMappings(java.util.Map) namespace to package mapping}. */ protected void customizeUnmarshaller(Unmarshaller unmarshaller) { unmarshaller.setValidation(this.validating); @@ -463,16 +564,13 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing } /** - * Convert the givenXMLException to an appropriate exception
- * from the org.springframework.oxm hierarchy.
- *
- * 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.
+ * Convert the given XMLException to an appropriate exception from the
+ * org.springframework.oxm hierarchy.
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 XMLException that occured
- * @param marshalling indicates whether the exception occurs during
- * marshalling (true), or unmarshalling (false)
+ * @param ex Castor XMLException that occured
+ * @param marshalling indicates whether the exception occurs during marshalling (true), or unmarshalling
+ * (false)
* @return the corresponding XmlMappingException
*/
protected XmlMappingException convertCastorException(XMLException ex, boolean marshalling) {