You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tiles.apache.org by ap...@apache.org on 2008/01/30 19:19:33 UTC

svn commit: r616829 - in /tiles/framework/trunk: tiles-api/src/main/java/org/apache/tiles/ tiles-core/src/main/java/org/apache/tiles/context/ tiles-core/src/test/java/org/apache/tiles/context/

Author: apetrelli
Date: Wed Jan 30 10:19:31 2008
New Revision: 616829

URL: http://svn.apache.org/viewvc?rev=616829&view=rev
Log:
TILES-208
Added "inheritCascadedAttributes" method in AttributeContext.

Modified:
    tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/AttributeContext.java
    tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/BasicAttributeContext.java
    tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/context/BasicAttributeContextTest.java

Modified: tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/AttributeContext.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/AttributeContext.java?rev=616829&r1=616828&r2=616829&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/AttributeContext.java (original)
+++ tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/AttributeContext.java Wed Jan 30 10:19:31 2008
@@ -47,6 +47,13 @@
     void addMissing(Map<String, Attribute> defaultAttributes);
 
     /**
+     * Copies the cascaded attributes to this attribute context.
+     *
+     * @param parent The parent context to be used.
+     */
+    void inheritCascadedAttributes(AttributeContext parent);
+
+    /**
      * Retrieve the named attribute, either cascaded or not.
      *
      * @param name key name for the attribute.

Modified: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/BasicAttributeContext.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/BasicAttributeContext.java?rev=616829&r1=616828&r2=616829&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/BasicAttributeContext.java (original)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/BasicAttributeContext.java Wed Jan 30 10:19:31 2008
@@ -75,7 +75,6 @@
         }
     }
 
-
     /**
      * Copy constructor.
      *
@@ -86,14 +85,10 @@
             copyBasicAttributeContext((BasicAttributeContext) context);
         } else {
             this.attributes = new HashMap<String, Attribute>();
-            this.cascadedAttributes = new HashMap<String, Attribute>();
             for (String name : context.getLocalAttributeNames()) {
                 attributes.put(name, context.getLocalAttribute(name));
             }
-            for (String name : context.getCascadedAttributeNames()) {
-                cascadedAttributes
-                        .put(name, context.getCascadedAttribute(name));
-            }
+            inheritCascadedAttributes(context);
         }
     }
 
@@ -106,6 +101,19 @@
         copyBasicAttributeContext(context);
     }
 
+    /** {@inheritDoc} */
+    public void inheritCascadedAttributes(AttributeContext context) {
+        if (context instanceof BasicAttributeContext) {
+            copyCascadedAttributes((BasicAttributeContext) context);
+        } else {
+            this.cascadedAttributes = new HashMap<String, Attribute>();
+            for (String name : context.getCascadedAttributeNames()) {
+                cascadedAttributes
+                        .put(name, context.getCascadedAttribute(name));
+            }
+        }
+    }
+
     /**
      * Add all attributes to this context.
      * Copies all of the mappings from the specified attribute map to this context.
@@ -338,6 +346,15 @@
         if (context.attributes != null && !context.attributes.isEmpty()) {
             attributes = new HashMap<String, Attribute>(context.attributes);
         }
+        copyCascadedAttributes(context);
+    }
+
+    /**
+     * Copies the cascaded attributes to the current context.
+     *
+     * @param context The context to copy from.
+     */
+    private void copyCascadedAttributes(BasicAttributeContext context) {
         if (context.cascadedAttributes != null
                 && !context.cascadedAttributes.isEmpty()) {
             cascadedAttributes = new HashMap<String, Attribute>(

Modified: tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/context/BasicAttributeContextTest.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/context/BasicAttributeContextTest.java?rev=616829&r1=616828&r2=616829&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/context/BasicAttributeContextTest.java (original)
+++ tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/context/BasicAttributeContextTest.java Wed Jan 30 10:19:31 2008
@@ -133,6 +133,23 @@
     }
 
     /**
+     * Tests {@link BasicAttributeContext#inheritCascadedAttributes(AttributeContext)}.
+     */
+    public void testInheritCascadedAttributes() {
+        AttributeContext toCopy = new BasicAttributeContext();
+        toCopy.putAttribute("name1", new Attribute("value1"), false);
+        toCopy.putAttribute("name2", new Attribute("value2"), true);
+        AttributeContext context = new BasicAttributeContext();
+        context.inheritCascadedAttributes(toCopy);
+        Attribute attribute = context.getLocalAttribute("name1");
+        assertNull("Attribute name1 found", attribute);
+        attribute = context.getCascadedAttribute("name2");
+        assertNotNull("Attribute name2 not found", attribute);
+        assertEquals("Attribute name2 has not been set correctly", "value2",
+                attribute.getValue());
+    }
+
+    /**
      * Tests {@link BasicAttributeContext#addAll(Map)}.
      */
     public void testAddAll() {