Author: oheger Date: Sun Nov 25 14:57:18 2012 New Revision: 1413357 URL: http://svn.apache.org/viewvc?rev=1413357&view=rev Log: Improved Javadocs and added a constructor for specifying a default bean class.
Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/XMLBeanDeclaration.java commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestXMLBeanDeclaration.java Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/XMLBeanDeclaration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/XMLBeanDeclaration.java?rev=1413357&r1=1413356&r2=1413357&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/XMLBeanDeclaration.java (original) +++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/XMLBeanDeclaration.java Sun Nov 25 14:57:18 2012 @@ -159,15 +159,20 @@ public class XMLBeanDeclaration implemen /** Stores the configuration node that contains the bean declaration. */ private final ConfigurationNode node; + /** The name of the default bean class. */ + private final String defaultBeanClassName; + /** - * Creates a new instance of {@code XMLBeanDeclaration} and - * initializes it from the given configuration. The passed in key points to - * the bean declaration. + * Creates a new instance of {@code XMLBeanDeclaration} and initializes it + * from the given configuration. The passed in key points to the bean + * declaration. * - * @param config the configuration + * @param config the configuration (must not be <b>null</b>) * @param key the key to the bean declaration (this key must point to - * exactly one bean declaration or a {@code IllegalArgumentException} - * exception will be thrown) + * exactly one bean declaration or a {@code IllegalArgumentException} + * exception will be thrown) + * @throws IllegalArgumentException if required information is missing to + * construct the bean declaration */ public XMLBeanDeclaration(HierarchicalConfiguration config, String key) { @@ -175,24 +180,49 @@ public class XMLBeanDeclaration implemen } /** - * Creates a new instance of {@code XMLBeanDeclaration} and - * initializes it from the given configuration. The passed in key points to - * the bean declaration. If the key does not exist and the boolean argument - * is <b>true</b>, the declaration is initialized with an empty - * configuration. It is possible to create objects from such an empty - * declaration if a default class is provided. If the key on the other hand - * has multiple values or is undefined and the boolean argument is <b>false</b>, - * a {@code IllegalArgumentException} exception will be thrown. + * Creates a new instance of {@code XMLBeanDeclaration} and initializes it + * from the given configuration supporting optional declarations. * - * @param config the configuration + * @param config the configuration (must not be <b>null</b>) * @param key the key to the bean declaration * @param optional a flag whether this declaration is optional; if set to - * <b>true</b>, no exception will be thrown if the passed in key is - * undefined + * <b>true</b>, no exception will be thrown if the passed in key is + * undefined + * @throws IllegalArgumentException if required information is missing to + * construct the bean declaration */ public XMLBeanDeclaration(HierarchicalConfiguration config, String key, boolean optional) { + this(config, key, optional, null); + } + + /** + * Creates a new instance of {@code XMLBeanDeclaration} and initializes it + * from the given configuration supporting optional declarations and a + * default bean class name. The passed in key points to the bean + * declaration. If the key does not exist and the boolean argument is + * <b>true</b>, the declaration is initialized with an empty configuration. + * It is possible to create objects from such an empty declaration if a + * default class is provided. If the key on the other hand has multiple + * values or is undefined and the boolean argument is <b>false</b>, a + * {@code IllegalArgumentException} exception will be thrown. It is possible + * to set a default bean class name; this name is used if the configuration + * does not contain a bean class. + * + * @param config the configuration (must not be <b>null</b>) + * @param key the key to the bean declaration + * @param optional a flag whether this declaration is optional; if set to + * <b>true</b>, no exception will be thrown if the passed in key is + * undefined + * @param defBeanClsName a default bean class name + * @throws IllegalArgumentException if required information is missing to + * construct the bean declaration + * @since 2.0 + */ + public XMLBeanDeclaration(HierarchicalConfiguration config, String key, + boolean optional, String defBeanClsName) + { if (config == null) { throw new IllegalArgumentException( @@ -218,6 +248,7 @@ public class XMLBeanDeclaration implemen } this.node = tmpnode; this.configuration = tmpconfiguration; + defaultBeanClassName = defBeanClsName; initSubnodeConfiguration(getConfiguration()); } @@ -256,6 +287,7 @@ public class XMLBeanDeclaration implemen this.node = node; configuration = config; + defaultBeanClassName = null; initSubnodeConfiguration(config); } @@ -270,6 +302,19 @@ public class XMLBeanDeclaration implemen } /** + * Returns the name of the default bean class. This class is used if no bean + * class is specified in the configuration. It may be <b>null</b> if no + * default class was set. + * + * @return the default bean class name + * @since 2.0 + */ + public String getDefaultBeanClassName() + { + return defaultBeanClassName; + } + + /** * Returns the node that contains the bean declaration. * * @return the configuration node this bean declaration is based on @@ -309,7 +354,7 @@ public class XMLBeanDeclaration implemen */ public String getBeanClassName() { - return getConfiguration().getString(ATTR_BEAN_CLASS); + return getConfiguration().getString(ATTR_BEAN_CLASS, getDefaultBeanClassName()); } /** Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestXMLBeanDeclaration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestXMLBeanDeclaration.java?rev=1413357&r1=1413356&r2=1413357&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestXMLBeanDeclaration.java (original) +++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestXMLBeanDeclaration.java Sun Nov 25 14:57:18 2012 @@ -157,6 +157,38 @@ public class TestXMLBeanDeclaration } /** + * Tests whether a default bean class name is taken into account. + */ + @Test + public void testGetBeanClassNameFromDefault() + { + BaseHierarchicalConfiguration config = + new BaseHierarchicalConfiguration(); + config.addProperty(KEY + "[@someProperty]", Boolean.TRUE); + XMLBeanDeclaration decl = + new XMLBeanDeclaration(config, KEY, false, getClass().getName()); + assertEquals("Wrong class name", getClass().getName(), + decl.getBeanClassName()); + } + + /** + * Tests whether a default bean class name is overridden by a value in the + * configuration. + */ + @Test + public void tetGetBeanClassNameDefaultOverride() + { + BaseHierarchicalConfiguration config = + new BaseHierarchicalConfiguration(); + config.addProperty(KEY + "[@config-class]", getClass().getName()); + XMLBeanDeclaration decl = + new XMLBeanDeclaration(config, KEY, false, + "someDefaultClassName"); + assertEquals("Wrong class name", getClass().getName(), + decl.getBeanClassName()); + } + + /** * Tests fetching the name of the bean factory. */ @Test