Improved Jaxb1Marshaller.supports() to use context path.

Added setContextPaths to AbstractJaxbMarshaller.
This commit is contained in:
Arjen Poutsma
2007-11-06 13:05:02 +00:00
parent 91f2133d4f
commit cf00834ffb
3 changed files with 35 additions and 4 deletions

View File

@@ -28,6 +28,8 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.oxm.XmlMappingException;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Abstract base class for implementations of the <code>Marshaller</code> and <code>Unmarshaller</code> interfaces that
@@ -64,9 +66,19 @@ public abstract class AbstractJaxbMarshaller
/** Sets the JAXB Context path. */
public void setContextPath(String contextPath) {
Assert.notNull(contextPath, "'contextPath' must not be null");
this.contextPath = contextPath;
}
/**
* Sets multiple JAXB Context paths. The given array of context paths is converted to a colon-delimited string, as
* supported by JAXB.
*/
public void setContextPaths(String[] contextPaths) {
Assert.notEmpty(contextPaths, "'contextPaths' must not be empty");
this.contextPath = StringUtils.arrayToDelimitedString(contextPaths, ":");
}
/**
* 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.

View File

@@ -22,7 +22,7 @@ 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.ClassUtils;
import org.springframework.util.StringUtils;
/**
@@ -38,7 +38,7 @@ import org.springframework.util.StringUtils;
* @see #setValidating(boolean)
* @since 1.0.0
*/
public class Jaxb1Marshaller extends AbstractJaxbMarshaller implements InitializingBean {
public class Jaxb1Marshaller extends AbstractJaxbMarshaller {
private boolean validating = false;
@@ -48,7 +48,26 @@ public class Jaxb1Marshaller extends AbstractJaxbMarshaller implements Initializ
}
public boolean supports(Class clazz) {
return Element.class.isAssignableFrom(clazz);
if (!Element.class.isAssignableFrom(clazz)) {
return false;
}
if (StringUtils.hasLength(getContextPath())) {
String className = ClassUtils.getQualifiedName(clazz);
int lastDotIndex = className.lastIndexOf('.');
if (lastDotIndex == -1) {
return false;
}
String packageName = className.substring(0, lastDotIndex);
String[] contextPaths = StringUtils.tokenizeToStringArray(getContextPath(), ":");
for (int i = 0; i < contextPaths.length; i++) {
if (contextPaths[i].equals(packageName)) {
return true;
}
}
return false;
}
return false;
}
protected final JAXBContext createJaxbContext() throws JAXBException {

View File

@@ -31,7 +31,7 @@ public class Jaxb1MarshallerTest extends AbstractJaxbMarshallerTestCase {
protected final Marshaller createMarshaller() throws Exception {
Jaxb1Marshaller marshaller = new Jaxb1Marshaller();
marshaller.setContextPath(CONTEXT_PATH);
marshaller.setContextPaths(new String[]{CONTEXT_PATH});
marshaller.afterPropertiesSet();
return marshaller;
}