From 0dd43ab10734de73b841a83744f06cbb85d2af4f Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Mon, 4 Apr 2011 08:50:15 +0000 Subject: [PATCH] SPR-7805 - Add support for package binding in the JibxMashaller git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4169 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../oxm/jibx/JibxMarshaller.java | 54 ++++++++++++++----- .../oxm/jibx/JibxMarshallerTests.java | 11 ++-- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/org.springframework.oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java b/org.springframework.oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java index 5d6a053effb..f4e3336b4aa 100644 --- a/org.springframework.oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java +++ b/org.springframework.oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 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. @@ -82,8 +82,12 @@ import org.springframework.util.xml.StaxUtils; */ public class JibxMarshaller extends AbstractMarshaller implements InitializingBean { + private static final String DEFAULT_BINDING_NAME = "binding"; + private Class targetClass; + private String targetPackage; + private String bindingName; private int indent = -1; @@ -106,12 +110,24 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe /** - * Set the target class for this instance. This property is required. + * 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) { this.targetClass = targetClass; } + /** + * 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) { + this.targetPackage = targetPackage; + } /** * Set the optional binding name for this instance. */ @@ -185,24 +201,38 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe } public void afterPropertiesSet() throws JiBXException { - Assert.notNull(this.targetClass, "'targetClass' is required"); - if (StringUtils.hasLength(this.bindingName)) { - if (logger.isInfoEnabled()) { - logger.info("Configured for target class [" + this.targetClass + "] using binding [" + this.bindingName + "]"); + if (this.targetClass != null) { + if (StringUtils.hasLength(this.bindingName)) { + if (logger.isInfoEnabled()) { + logger.info("Configured for target class [" + this.targetClass + "] using binding [" + this.bindingName + "]"); + } + this.bindingFactory = BindingDirectory.getFactory(this.bindingName, this.targetClass); } - this.bindingFactory = BindingDirectory.getFactory(this.bindingName, this.targetClass); - } - else { - if (logger.isInfoEnabled()) { - logger.info("Configured for target class [" + this.targetClass + "]"); + else { + if (logger.isInfoEnabled()) { + logger.info("Configured for target class [" + this.targetClass + "]"); + } + this.bindingFactory = BindingDirectory.getFactory(this.targetClass); } - this.bindingFactory = BindingDirectory.getFactory(this.targetClass); + } 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 + "]"); + } + this.bindingFactory = BindingDirectory.getFactory(bindingName, targetPackage); + } else { + throw new IllegalArgumentException("either 'targetClass' or 'targetPackage' is required"); } } public boolean supports(Class clazz) { Assert.notNull(clazz, "'clazz' must not be null"); + if (this.targetClass != null) { + return this.targetClass.equals(clazz); + } String[] mappedClasses = this.bindingFactory.getMappedClasses(); String className = clazz.getName(); for (String mappedClass : mappedClasses) { diff --git a/org.springframework.oxm/src/test/java/org/springframework/oxm/jibx/JibxMarshallerTests.java b/org.springframework.oxm/src/test/java/org/springframework/oxm/jibx/JibxMarshallerTests.java index b69a9b03256..7d43dcde8c2 100644 --- a/org.springframework.oxm/src/test/java/org/springframework/oxm/jibx/JibxMarshallerTests.java +++ b/org.springframework.oxm/src/test/java/org/springframework/oxm/jibx/JibxMarshallerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2011 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. @@ -19,21 +19,22 @@ package org.springframework.oxm.jibx; import java.io.StringWriter; import javax.xml.transform.stream.StreamResult; -import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; import org.custommonkey.xmlunit.XMLUnit; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; import org.junit.Test; import org.springframework.oxm.AbstractMarshallerTests; import org.springframework.oxm.Marshaller; +import static org.custommonkey.xmlunit.XMLAssert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + public class JibxMarshallerTests extends AbstractMarshallerTests { @Override protected Marshaller createMarshaller() throws Exception { JibxMarshaller marshaller = new JibxMarshaller(); - marshaller.setTargetClass(Flights.class); + marshaller.setTargetPackage("org.springframework.oxm.jibx"); marshaller.afterPropertiesSet(); return marshaller; }