Author: oheger
Date: Wed Feb 22 12:16:44 2006
New Revision: 379870

URL: http://svn.apache.org/viewcvs?rev=379870&view=rev
Log:
Added interpolation support to XMLBeanDeclaration

Modified:
    
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/beanutils/XMLBeanDeclaration.java
    
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/beanutils/TestXMLBeanDeclaration.java

Modified: 
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/beanutils/XMLBeanDeclaration.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/beanutils/XMLBeanDeclaration.java?rev=379870&r1=379869&r2=379870&view=diff
==============================================================================
--- 
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/beanutils/XMLBeanDeclaration.java
 (original)
+++ 
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/beanutils/XMLBeanDeclaration.java
 Wed Feb 22 12:16:44 2006
@@ -21,6 +21,7 @@
 import java.util.Map;
 
 import org.apache.commons.configuration.HierarchicalConfiguration;
+import org.apache.commons.configuration.PropertyConverter;
 import org.apache.commons.configuration.tree.ConfigurationNode;
 
 /**
@@ -36,13 +37,13 @@
  * <p>
  *
  * <pre>
- *  ...
- *  &lt;personBean config-class=&quot;my.model.PersonBean&quot;
- *      lastName=&quot;Doe&quot; firstName=&quot;John&quot;&gt;
- *      &lt;address config-class=&quot;my.model.AddressBean&quot;
- *          street=&quot;21st street 11&quot; zip=&quot;1234&quot;
- *          city=&quot;TestCity&quot;/&gt;
- *  &lt;/personBean&gt;
+ *   ...
+ *   &lt;personBean config-class=&quot;my.model.PersonBean&quot;
+ *       lastName=&quot;Doe&quot; firstName=&quot;John&quot;&gt;
+ *       &lt;address config-class=&quot;my.model.AddressBean&quot;
+ *           street=&quot;21st street 11&quot; zip=&quot;1234&quot;
+ *           city=&quot;TestCity&quot;/&gt;
+ *   &lt;/personBean&gt;
  * </pre>
  *
  * </p>
@@ -103,6 +104,9 @@
     public static final String ATTR_FACTORY_PARAM = RESERVED_PREFIX
             + "factoryParam";
 
+    /** Stores the associated configuration. */
+    private HierarchicalConfiguration configuration;
+
     /** Stores the configuration node that contains the bean declaration. */
     private ConfigurationNode node;
 
@@ -122,14 +126,10 @@
             throw new IllegalArgumentException(
                     "Configuration must not be null!");
         }
-        if (key == null)
-        {
-            node = config.getRoot();
-        }
-        else
-        {
-            node = config.configurationAt(key).getRoot();
-        }
+
+        node = (key == null) ? config.getRoot() : config.configurationAt(key)
+                .getRoot();
+        configuration = config;
     }
 
     /**
@@ -141,7 +141,7 @@
      */
     public XMLBeanDeclaration(HierarchicalConfiguration config)
     {
-        this(config, null);
+        this(config, (String) null);
     }
 
     /**
@@ -149,15 +149,34 @@
      * initializes it with the configuration node that contains the bean
      * declaration.
      *
+     * @param config the configuration
      * @param node the node with the bean declaration.
      */
-    public XMLBeanDeclaration(ConfigurationNode node)
+    public XMLBeanDeclaration(HierarchicalConfiguration config,
+            ConfigurationNode node)
     {
+        if (config == null)
+        {
+            throw new IllegalArgumentException(
+                    "Configuration must not be null!");
+        }
         if (node == null)
         {
             throw new IllegalArgumentException("Node must not be null!");
         }
+
         this.node = node;
+        configuration = config;
+    }
+
+    /**
+     * Returns the configuration object this bean declaration is based on.
+     *
+     * @return the associated configuration
+     */
+    public HierarchicalConfiguration getConfiguration()
+    {
+        return configuration;
     }
 
     /**
@@ -217,7 +236,8 @@
             ConfigurationNode attr = (ConfigurationNode) it.next();
             if (!isReservedNode(attr))
             {
-                props.put(attr.getName(), attr.getValue());
+                props.put(attr.getName(), PropertyConverter.interpolate(attr
+                        .getValue(), getConfiguration()));
             }
         }
 
@@ -239,7 +259,8 @@
             ConfigurationNode child = (ConfigurationNode) it.next();
             if (!isReservedNode(child))
             {
-                nested.put(child.getName(), new XMLBeanDeclaration(child));
+                nested.put(child.getName(), new XMLBeanDeclaration(
+                        getConfiguration(), child));
             }
         }
 

Modified: 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/beanutils/TestXMLBeanDeclaration.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/beanutils/TestXMLBeanDeclaration.java?rev=379870&r1=379869&r2=379870&view=diff
==============================================================================
--- 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/beanutils/TestXMLBeanDeclaration.java
 (original)
+++ 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/beanutils/TestXMLBeanDeclaration.java
 Wed Feb 22 12:16:44 2006
@@ -63,6 +63,9 @@
     /** Constant for the key with the bean declaration. */
     static final String KEY = "myBean";
 
+    /** Constant for the section with the variables.*/
+    static final String VARS = "variables.";
+
     /** Stores the object to be tested. */
     XMLBeanDeclaration decl;
 
@@ -74,7 +77,8 @@
     {
         try
         {
-            decl = new XMLBeanDeclaration((ConfigurationNode) null);
+            decl = new XMLBeanDeclaration(new HierarchicalConfiguration(),
+                    (ConfigurationNode) null);
             fail("Could init declaration with null node!");
         }
         catch (IllegalArgumentException iex)
@@ -262,6 +266,23 @@
         Map nested = decl.getNestedBeanDeclarations();
         assertTrue("Found nested declarations", nested == null
                 || nested.isEmpty());
+    }
+
+    /**
+     * Tests whether interpolation of bean properties works.
+     */
+    public void testGetInterpolatedBeanProperties()
+    {
+        HierarchicalConfiguration config = new HierarchicalConfiguration();
+        String[] varValues = new String[TEST_PROPS.length];
+        for(int i = 0; i < TEST_PROPS.length; i++)
+        {
+            varValues[i] = "${" + VARS + TEST_PROPS[i] + "}";
+            config.addProperty(VARS + TEST_PROPS[i], TEST_VALUES[i]);
+        }
+        setupBeanDeclaration(config, KEY, TEST_PROPS, varValues);
+        decl = new XMLBeanDeclaration(config, KEY);
+        checkProperties(decl, TEST_PROPS, TEST_VALUES);
     }
 
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to