Author: apetrelli
Date: Wed May  9 01:05:25 2007
New Revision: 536454

URL: http://svn.apache.org/viewvc?view=rev&rev=536454
Log:
TILES-175
Now the attribute type is propogated correctly to extended definitions.
Added test case.

TILES-174
Attribute.attributes and all related code have been removed.

TILES-87
All performance problems have been addressed.

Modified:
    
tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/Attribute.java
    
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/context/BasicAttributeContext.java
    
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/DefinitionsImpl.java
    
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java
    
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/mgmt/DefinitionManager.java
    
tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/TestDefinitions.java

Modified: 
tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/Attribute.java
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/Attribute.java?view=diff&rev=536454&r1=536453&r2=536454
==============================================================================
--- 
tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/Attribute.java 
(original)
+++ 
tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/Attribute.java 
Wed May  9 01:05:25 2007
@@ -130,11 +130,6 @@
     private String name = null;
 
     /**
-     * The composing attributes, used to render the attribute itself.
-     */
-    private Map<String, Attribute> attributes;
-
-    /**
      * Constructor.
      *
      */
@@ -160,14 +155,6 @@
         this.role = attribute.role;
         this.type = attribute.type;
         this.value = attribute.getValue();
-        if (attribute.attributes != null) {
-            this.attributes = new HashMap<String, Attribute>();
-            for (Map.Entry<String, Attribute> entry : attribute.attributes
-                    .entrySet()) {
-                this.attributes.put(entry.getKey(), new Attribute(
-                        entry.getValue()));
-            }
-        }
     }
 
     /**
@@ -302,25 +289,6 @@
      */
     public void setName(String name) {
         this.name = name;
-    }
-
-
-    /**
-     * Returns the underlying attributes for this attribute.
-     *
-     * @return The attribute map.
-     */
-    public Map<String, Attribute> getAttributes() {
-        return attributes;
-    }
-
-    /**
-     * Sets the underlying attributes for this attribute.
-     *
-     * @param attributes The attribute map.
-     */
-    public void setAttributes(Map<String, Attribute> attributes) {
-        this.attributes = attributes;
     }
 
     /**

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?view=diff&rev=536454&r1=536453&r2=536454
==============================================================================
--- 
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 May  9 01:05:25 2007
@@ -93,6 +93,10 @@
      * @param newAttributes Attributes to add.
      */
     public void addAll(Map<String, Attribute> newAttributes) {
+        if (newAttributes == null) {
+            return;
+        }
+
         if (attributes == null) {
             attributes = new HashMap<String, Attribute>(newAttributes);
             return;

Modified: 
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/DefinitionsImpl.java
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/DefinitionsImpl.java?view=diff&rev=536454&r1=536453&r2=536454
==============================================================================
--- 
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/DefinitionsImpl.java
 (original)
+++ 
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/DefinitionsImpl.java
 Wed May  9 01:05:25 2007
@@ -24,10 +24,8 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.tiles.Attribute;
-import org.apache.tiles.Attribute.AttributeType;
 
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.Locale;
 import java.util.Map;
 
@@ -80,7 +78,6 @@
     public void addDefinitions(Map<String, Definition> defsMap)
             throws NoSuchDefinitionException {
         this.baseDefinitions.putAll(defsMap);
-        resolveAttributeDependencies(null);
         resolveInheritances();
     }
 
@@ -95,7 +92,6 @@
      */
     public void addDefinitions(Map<String, Definition> defsMap, Locale locale) 
throws NoSuchDefinitionException {
         localeSpecificDefinitions.put(locale, defsMap);
-        resolveAttributeDependencies(locale);
         resolveInheritances(locale);
     }
 
@@ -171,52 +167,6 @@
     }
 
     /**
-     * Checks if an attribute is of type "definition".
-     *
-     * @param attr The attribute to check.
-     * @return <code>true</code> if the attribute is a definition.
-     */
-    protected boolean isDefinitionType(Attribute attr) {
-        AttributeType type = attr.getType();
-
-        boolean explicit = type == AttributeType.DEFINITION;
-
-        boolean implicit = type == null && attr.getValue() != null
-                && baseDefinitions.containsKey(attr.getValue().toString());
-
-        return explicit || implicit;
-
-    }
-
-    /**
-     * Resolves attribute dependencies for the given locale. If the locale is
-     * <code>null</code>, it resolves base attribute dependencies.
-     *
-     * @param locale The locale to use.
-     */
-    protected void resolveAttributeDependencies(Locale locale) {
-        if (locale != null) {
-            resolveAttributeDependencies(null); // FIXME Is it necessary?
-        }
-        Map<String, Definition> defsMap = 
localeSpecificDefinitions.get(locale);
-        if (defsMap == null) {
-            return;
-        }
-
-        for (Definition def : defsMap.values()) {
-            Map<String, Attribute> attributes = def.getAttributes();
-            for (Attribute attr : attributes.values()) {
-                if (isDefinitionType(attr)) {
-                    Definition subDef =
-                        getDefinitionByAttribute(attr, locale);
-                    attr.setAttributes(subDef.getAttributes());
-                    attr.setType(AttributeType.DEFINITION);
-                }
-            }
-        }
-    }
-
-    /**
      * Searches for a definition specified as an attribute.
      *
      * @param attr   The attribute to use.
@@ -295,15 +245,13 @@
      * @param parent The parent definition.
      * @param child  The child that will be overloaded.
      */
+    // FIXME This is the same as DefinitionManager.overload.
     protected void overload(Definition parent,
                             Definition child) {
         // Iterate on each parent's attribute and add it if not defined in 
child.
-        Iterator<String> parentAttributes = parent.getAttributes().keySet()
-                .iterator();
-        while (parentAttributes.hasNext()) {
-            String name = parentAttributes.next();
-            if (!child.getAttributes().containsKey(name)) {
-                child.put(name, parent.getAttribute(name));
+        for (Map.Entry<String, Attribute> entry : 
parent.getAttributes().entrySet()) {
+            if (!child.hasAttributeValue(entry.getKey())) {
+                child.putAttribute(entry.getKey(), new 
Attribute(entry.getValue()));
             }
         }
         // Set template and role if not setted

Modified: 
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java?view=diff&rev=536454&r1=536453&r2=536454
==============================================================================
--- 
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java
 (original)
+++ 
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java
 Wed May  9 01:05:25 2007
@@ -431,49 +431,39 @@
     /** [EMAIL PROTECTED] */
     public void render(Attribute attr, Writer writer, Object... requestItems)
         throws TilesException, IOException {
-        AttributeContext context = getAttributeContext(requestItems);
         TilesRequestContext request = getRequestContext(requestItems);
 
-        AttributeType type = calculateType(attr, request);
-
-        if (AttributeType.OBJECT == type) {
-            throw new TilesException(
-                    "Cannot insert an attribute of 'object' type");
-        }
-
-        if (AttributeType.STRING == type) {
-            writer.write(attr.getValue().toString());
-            return;
-
+        if (attr == null) {
+            throw new TilesException("Cannot render a null attribute");
         }
 
-        Map<String, Attribute> attrs = attr.getAttributes();
-        if (attrs != null) {
-            for (Map.Entry<String, Attribute> a : attrs.entrySet()) {
-                context.putAttribute(a.getKey(), a.getValue());
-            }
+        AttributeType type = attr.getType();
+        if (type == null) {
+            type = calculateType(attr, request);
+            attr.setType(type);
         }
 
-        if (isDefinition(attr, requestItems)) {
-            render(request, attr.getValue().toString());
-        } else {
-            request.dispatch(attr.getValue().toString());
+        switch (type) {
+            case OBJECT:
+                throw new TilesException(
+                    "Cannot insert an attribute of 'object' type");
+            case STRING:
+                writer.write(attr.getValue().toString());
+                break;
+            case DEFINITION:
+                render(request, attr.getValue().toString());
+                break;
+            case TEMPLATE:
+                request.dispatch(attr.getValue().toString());
+                break;
+            default: // should not happen
+                throw new TilesException(
+                        "Unrecognized type for attribute value "
+                        + attr.getValue());
         }
     }
 
     /**
-     * Checks if an attribute contains a definition.
-     *
-     * @param attr The attribute to check.
-     * @param requestItems The request items.
-     * @return <code>true</code> if the attribute is a definition.
-     */
-    private boolean isDefinition(Attribute attr, Object... requestItems) {
-        return AttributeType.DEFINITION == attr.getType()
-                || isValidDefinition(attr.getValue().toString(), requestItems);
-    }
-
-    /**
      * Calculates the type of an attribute.
      *
      * @param attr The attribute to check.
@@ -483,26 +473,21 @@
      */
     private AttributeType calculateType(Attribute attr,
             TilesRequestContext request) throws TilesException {
-        AttributeType type = attr.getType();
-        if (type == null) {
-            Object valueContent = attr.getValue();
-            if (valueContent instanceof String) {
-                String valueString = (String) valueContent;
-                if (isValidDefinition(request, valueString)) {
-                    type = AttributeType.DEFINITION;
-                } else if (valueString.startsWith("/")) {
-                    type = AttributeType.TEMPLATE;
-                } else {
-                    type = AttributeType.STRING;
-                }
+        AttributeType type;
+        Object valueContent = attr.getValue();
+        if (valueContent instanceof String) {
+            String valueString = (String) valueContent;
+            if (isValidDefinition(request, valueString)) {
+                type = AttributeType.DEFINITION;
+            } else if (valueString.startsWith("/")) {
+                type = AttributeType.TEMPLATE;
             } else {
-                type = AttributeType.OBJECT;
-            }
-            if (type == null) {
-                throw new TilesException("Unrecognized type for attribute 
value "
-                    + attr.getValue());
+                type = AttributeType.STRING;
             }
+        } else {
+            type = AttributeType.OBJECT;
         }
+
         return type;
     }
 

Modified: 
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/mgmt/DefinitionManager.java
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/mgmt/DefinitionManager.java?view=diff&rev=536454&r1=536453&r2=536454
==============================================================================
--- 
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/mgmt/DefinitionManager.java
 (original)
+++ 
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/mgmt/DefinitionManager.java
 Wed May  9 01:05:25 2007
@@ -21,7 +21,6 @@
 package org.apache.tiles.impl.mgmt;
 
 import org.apache.tiles.Attribute;
-import org.apache.tiles.Attribute.AttributeType;
 import org.apache.tiles.context.TilesRequestContext;
 import org.apache.tiles.definition.Definition;
 import org.apache.tiles.definition.DefinitionsFactory;
@@ -138,34 +137,10 @@
             this.resolveInheritance(definition, request);
         }
 
-        for (Attribute attr : definition.getAttributes().values()) {
-            if (isDefinition(attr, request)) {
-                Definition d = getDefinition(attr.getValue().toString(), 
request);
-                attr.setAttributes(d.getAttributes());
-            }
-        }
-
         getOrCreateDefinitions(request).put(definition.getName(), definition);
     }
 
     /**
-     * Checks if an attribute is a definition.
-     *
-     * @param attribute The attribute to check.
-     * @param request The current request.
-     * @return <code>true</code> if the attribute is a definition.
-     * @throws DefinitionsFactoryException If something goes wrong during
-     * checking if the attribute is a main definition.
-     */
-    private boolean isDefinition(Attribute attribute,
-            TilesRequestContext request) throws DefinitionsFactoryException {
-        boolean explicit = AttributeType.DEFINITION == attribute.getType();
-        boolean implicit = attribute.getType() == null
-                && (getDefinition((String) attribute.getValue(), request) != 
null);
-        return explicit || implicit;
-    }
-
-    /**
      * Validates a custom definition.
      *
      * @param definition The definition to validate.
@@ -245,6 +220,7 @@
      * @param parent The parent definition.
      * @param child  The child that will be overloaded.
      */
+    // FIXME This is the same as DefinitionsImpl.overload.
     protected void overload(Definition parent,
                             Definition child) {
         // Iterate on each parent's attribute and add it if not defined in 
child.

Modified: 
tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/TestDefinitions.java
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/TestDefinitions.java?view=diff&rev=536454&r1=536453&r2=536454
==============================================================================
--- 
tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/TestDefinitions.java
 (original)
+++ 
tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/TestDefinitions.java
 Wed May  9 01:05:25 2007
@@ -77,6 +77,22 @@
         attr.setName("attr1");
         attr.setValue("value1");
         def.addAttribute(attr);
+        attr = new Attribute();
+        attr.setName("attr2");
+        attr.setValue("tiles.def1");
+        // No type set
+        def.addAttribute(attr);
+        defs.put(def.getName(), def);
+        attr = new Attribute();
+        attr.setName("attr3");
+        attr.setValue("tiles.def1");
+        attr.setType(AttributeType.STRING);
+        def.addAttribute(attr);
+        defs.put(def.getName(), def);
+
+        def = new Definition();
+        def.setName("tiles.def1");
+        def.setTemplate("/test2.jsp");
         defs.put(def.getName(), def);
 
         def = new Definition();
@@ -95,12 +111,24 @@
             fail("Test failure: " + e);
         }
 
-        assertNotNull("Couldn't get parent.",
-                definitions.getDefinition("parent.def1"));
-        assertEquals("Incorrect template value." , "/test1.jsp",
-                definitions.getDefinition("parent.def1").getTemplate());
-        assertEquals("Incorrect attr1 value", "value1",
-                
definitions.getDefinition("parent.def1").getAttribute("attr1"));
+        def = definitions.getDefinition("parent.def1");
+
+        assertNotNull("Couldn't get parent.", def);
+        assertEquals("Incorrect template value.", "/test1.jsp", def
+                .getTemplate());
+        assertEquals("Incorrect attr1 value", "value1", def
+                .getAttribute("attr1"));
+
+        attr = def.getAttributes().get("attr1");
+        assertNotNull("Dependent attribute not found.", attr);
+        attr = def.getAttributes().get("attr2");
+        assertNotNull("Dependent attribute not found.", attr);
+        attr = def.getAttributes().get("attr3");
+        assertNotNull("Dependent attribute not found.", attr);
+        assertTrue("The attribute 'attr3' should be of type STRING", attr
+                .getType() == AttributeType.STRING);
+
+        def = definitions.getDefinition("child.def1");
 
         assertNotNull("Couldn't get child.",
                 definitions.getDefinition("child.def1"));
@@ -108,6 +136,15 @@
                 definitions.getDefinition("child.def1").getTemplate());
         assertEquals("Incorrect attr1 value", "New value",
                 definitions.getDefinition("child.def1").getAttribute("attr1"));
+
+        attr = def.getAttributes().get("attr1");
+        assertNotNull("Dependent attribute not found.", attr);
+        attr = def.getAttributes().get("attr2");
+        assertNotNull("Dependent attribute not found.", attr);
+        attr = def.getAttributes().get("attr3");
+        assertNotNull("Dependent attribute not found.", attr);
+        assertTrue("The attribute 'attr3' should be of type STRING", attr
+                .getType() == AttributeType.STRING);
     }
 
     /**
@@ -256,6 +293,24 @@
         Definitions definitions = new DefinitionsImpl();
         try {
             definitions.addDefinitions(defs);
+        } catch (NoSuchDefinitionException e) {
+            fail("Test failure: " + e);
+        }
+
+        defs = new HashMap<String, Definition>(defs);
+        def = new Definition();
+        def.setName("parent.def2");
+        def.setTemplate("/test1.jsp");
+        attr = new Attribute();
+        attr.setName("attr1");
+        attr.setValue("tiles.def3");
+        def.addAttribute(attr);
+        defs.put(def.getName(), def);
+        def = new Definition();
+        def.setName("tiles.def3");
+        defs.put(def.getName(), def);
+
+        try {
             definitions.addDefinitions(defs, Locale.ITALIAN);
         } catch (NoSuchDefinitionException e) {
             fail("Test failure: " + e);
@@ -292,5 +347,11 @@
 
         assertEquals("Incorrect dependent attribute name.", "tiles.def2",
                 newAttr);
+
+        newDef = definitions.getDefinition("parent.def2", Locale.ITALIAN);
+        assertNotNull("Parent definition not found.", newDef);
+
+        attr = newDef.getAttributes().get("attr1");
+        assertNotNull("Dependent attribute not found.", attr);
     }
 }


Reply via email to