Author: apetrelli
Date: Sat Feb 2 08:42:11 2008
New Revision: 617828
URL: http://svn.apache.org/viewvc?rev=617828&view=rev
Log:
TILES-245
Now Definition is an extended class of BasicAttributeContext.
Added:
tiles/framework/trunk/tiles-api/src/test/java/org/apache/tiles/BasicAttributeContextTest.java
- copied, changed from r617611,
tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/context/BasicAttributeContextTest.java
Removed:
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
tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/BasicAttributeContext.java
tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/Definition.java
tiles/framework/trunk/tiles-api/src/test/java/org/apache/tiles/TestDefinition.java
tiles/framework/trunk/tiles-jsp/src/main/java/org/apache/tiles/jsp/taglib/definition/DefinitionTag.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=617828&r1=617827&r2=617828&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
Sat Feb 2 08:42:11 2008
@@ -54,6 +54,14 @@
void inheritCascadedAttributes(AttributeContext parent);
/**
+ * Copies all missing attributes from the <code>parent</code> attribute
+ * context to this one.
+ *
+ * @param parent The attribute context to copy attributes from.
+ */
+ void inherit(AttributeContext parent);
+
+ /**
* Retrieve the named attribute, either cascaded or not.
*
* @param name key name for the attribute.
Modified:
tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/BasicAttributeContext.java
URL:
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/BasicAttributeContext.java?rev=617828&r1=617827&r2=617828&view=diff
==============================================================================
---
tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/BasicAttributeContext.java
(original)
+++
tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/BasicAttributeContext.java
Sat Feb 2 08:42:11 2008
@@ -104,6 +104,42 @@
}
}
+ /** [EMAIL PROTECTED] */
+ public void inherit(AttributeContext parent) {
+ if (parent instanceof BasicAttributeContext) {
+ inherit((BasicAttributeContext) parent);
+ } else {
+ Set<String> names = parent.getCascadedAttributeNames();
+ if (names != null && !names.isEmpty()) {
+ for (String name : names) {
+ Attribute attribute = parent.getCascadedAttribute(name);
+ putAttribute(name, attribute, true);
+ }
+ }
+ names = parent.getLocalAttributeNames();
+ if (names != null && !names.isEmpty()) {
+ for (String name : names) {
+ Attribute attribute = parent.getLocalAttribute(name);
+ putAttribute(name, attribute, false);
+ }
+ }
+ }
+ }
+
+ /**
+ * Inherits the attribute context, inheriting, i.e. copying if not present,
+ * the attributes.
+ *
+ * @param parent The attribute context to inherit.
+ */
+ public void inherit(BasicAttributeContext parent) {
+ cascadedAttributes = addMissingAttributes(
+ ((BasicAttributeContext) parent).cascadedAttributes,
+ cascadedAttributes);
+ attributes = addMissingAttributes(
+ ((BasicAttributeContext) parent).attributes, attributes);
+ }
+
/**
* Add all attributes to this context.
* Copies all of the mappings from the specified attribute map to this
context.
@@ -282,5 +318,29 @@
cascadedAttributes = new HashMap<String, Attribute>(
context.cascadedAttributes);
}
+ }
+
+ /**
+ * Adds missing attributes to the destination map.
+ *
+ * @param source The source attribute map.
+ * @param destination The destination attribute map.
+ * @return The destination attribute map if not null, a new one otherwise.
+ */
+ private Map<String, Attribute> addMissingAttributes(Map<String, Attribute>
source,
+ Map<String, Attribute> destination) {
+ if (source != null && !source.isEmpty()) {
+ if (destination == null) {
+ destination = new HashMap<String, Attribute>();
+ }
+ for (Map.Entry<String, Attribute> entry : source.entrySet()) {
+ String key = entry.getKey();
+ if (!destination.containsKey(key)) {
+ destination.put(key, entry.getValue());
+ }
+ }
+ }
+
+ return destination;
}
}
Modified:
tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/Definition.java
URL:
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/Definition.java?rev=617828&r1=617827&r2=617828&view=diff
==============================================================================
---
tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/Definition.java
(original)
+++
tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/Definition.java
Sat Feb 2 08:42:11 2008
@@ -24,7 +24,6 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
-import java.util.HashMap;
import java.util.Set;
import org.apache.tiles.Attribute.AttributeType;
@@ -38,7 +37,7 @@
* @since Tiles 2.0
* @version $Rev$ $Date$
*/
-public class Definition {
+public class Definition extends BasicAttributeContext {
/**
* Extends attribute value.
*/
@@ -52,10 +51,6 @@
*/
protected String template = null;
/**
- * Attributes defined for the definition.
- */
- protected Map<String, Attribute> attributes = null;
- /**
* The roles that can render this definition.
*/
protected Set<String> roles = null;
@@ -69,7 +64,7 @@
* Constructor.
*/
public Definition() {
- attributes = new HashMap<String, Attribute>();
+ super();
}
/**
@@ -81,8 +76,7 @@
* @param definition The definition to copy.
*/
public Definition(Definition definition) {
- attributes = new HashMap<String, Attribute>(
- definition.getAttributes());
+ super(definition);
this.name = definition.name;
this.template = definition.template;
this.roles = definition.roles;
@@ -98,9 +92,9 @@
*/
public Definition(String name, String template,
Map<String, Attribute> attributes) {
+ super(attributes);
this.name = name;
this.template = template;
- this.attributes = attributes;
}
/**
@@ -199,37 +193,19 @@
}
/**
- * Access method for the attributes property.
- * If there is no attributes, return an empty map.
+ * Access method for the attributes property. If there is no attributes,
+ * return an empty map.
*
* @return the current value of the attributes property
+ * @deprecated Use [EMAIL PROTECTED]
AttributeContext#getLocalAttributeNames()} and
+ * [EMAIL PROTECTED] AttributeContext#getCascadedAttributeNames()}.
*/
+ @Deprecated
public Map<String, Attribute> getAttributes() {
return attributes;
}
/**
- * Returns the attribute for the given name, or null if no attribute of the
- * given name exists.
- *
- * @param key name of the attribute
- * @return requested attribute or null if not found
- */
- public Attribute getAttribute(String key) {
- return attributes.get(key);
- }
-
- /**
- * Put a new attribute in this definition.
- *
- * @param key String key for attribute
- * @param value Attibute value.
- */
- public void putAttribute(String key, Attribute value) {
- attributes.put(key, value);
- }
-
- /**
* Add an attribute to this definition.
* <p/>
* This method is used by Digester to load definitions.
@@ -247,18 +223,24 @@
*
* @param key The attribute key to check.
* @return <code>true</code> if the attribute has a value.
+ * @deprecated Check if the [EMAIL PROTECTED]
AttributeContext#getAttribute(String)}
+ * returns null.
*/
+ @Deprecated
public boolean hasAttributeValue(String key) {
- return attributes.containsKey(key);
+ return getAttribute(key) != null;
}
/**
- * Put an attribute in template definition.
- * Attribute can be used as content for tag get.
+ * Put an attribute in template definition. Attribute can be used as
content
+ * for tag get.
*
- * @param name Attribute name
+ * @param name Attribute name
* @param content Attribute value
+ * @deprecated Use [EMAIL PROTECTED] AttributeContext#putAttribute(String,
Attribute)}
+ * or [EMAIL PROTECTED] AttributeContext#putAttribute(String, Attribute,
boolean)}.
*/
+ @Deprecated
public void put(String name, Object content) {
put(name, content, null);
}
@@ -270,7 +252,10 @@
* @param name Attribute name
* @param content Attribute value
* @param role Determine if content is used by get tag. If user is in
role, content is used.
+ * @deprecated Use [EMAIL PROTECTED] AttributeContext#putAttribute(String,
Attribute)}
+ * or [EMAIL PROTECTED] AttributeContext#putAttribute(String, Attribute,
boolean)}.
*/
+ @Deprecated
public void put(String name, Object content, String role) {
put(name, content, null, role);
}
@@ -283,7 +268,10 @@
* @param content Attribute value
* @param type attribute type: template, string, definition
* @param role Determine if content is used by get tag. If user is in
role, content is used.
+ * @deprecated Use [EMAIL PROTECTED] AttributeContext#putAttribute(String,
Attribute)}
+ * or [EMAIL PROTECTED] AttributeContext#putAttribute(String, Attribute,
boolean)}.
*/
+ @Deprecated
public void put(String name, Object content, AttributeType type, String
role) {
// Is there a type set ?
// First check direct attribute, and translate it to a valueType.
@@ -326,6 +314,58 @@
*/
public String getExtends() {
return inherit;
+ }
+
+ /**
+ * Inherits the attribute context. If the parameter is a Definition, the
+ * other properties (roles, template, preparer) are inherited.
+ *
+ * @param parent The attribute context to inherit.
+ */
+ @Override
+ public void inherit(AttributeContext parent) {
+ if (parent instanceof Definition) {
+ inherit((Definition) parent);
+ } else {
+ super.inherit(parent);
+ }
+ }
+
+ /**
+ * Inherits the attribute context. If the parameter is a Definition, the
+ * other properties (roles, template, preparer) are inherited.
+ *
+ * @param parent The attribute context to inherit.
+ */
+ @Override
+ public void inherit(BasicAttributeContext parent) {
+ if (parent instanceof Definition) {
+ inherit((Definition) parent);
+ } else {
+ super.inherit(parent);
+ }
+ }
+
+ /**
+ * Inherits the definition, inheriting attribute, i.e. copying if not
+ * present, attributes, template, roles, preparer.
+ *
+ * @param parent The definition to inherit.
+ */
+ public void inherit(Definition parent) {
+ super.inherit(parent);
+
+ // Set template, roles and preparer if not set
+ if (template == null) {
+ template = parent.template;
+ }
+ if ((roles == null || roles.isEmpty()) && parent.roles != null
+ && !parent.roles.isEmpty()) {
+ roles = new HashSet<String>(parent.roles);
+ }
+ if (preparer == null) {
+ preparer = parent.preparer;
+ }
}
/** [EMAIL PROTECTED] */
Copied:
tiles/framework/trunk/tiles-api/src/test/java/org/apache/tiles/BasicAttributeContextTest.java
(from r617611,
tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/context/BasicAttributeContextTest.java)
URL:
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-api/src/test/java/org/apache/tiles/BasicAttributeContextTest.java?p2=tiles/framework/trunk/tiles-api/src/test/java/org/apache/tiles/BasicAttributeContextTest.java&p1=tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/context/BasicAttributeContextTest.java&r1=617611&r2=617828&rev=617828&view=diff
==============================================================================
---
tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/context/BasicAttributeContextTest.java
(original)
+++
tiles/framework/trunk/tiles-api/src/test/java/org/apache/tiles/BasicAttributeContextTest.java
Sat Feb 2 08:42:11 2008
@@ -18,16 +18,13 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.tiles.context;
+package org.apache.tiles;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
-import org.apache.tiles.Attribute;
-import org.apache.tiles.AttributeContext;
-import org.apache.tiles.BasicAttributeContext;
import org.easymock.EasyMock;
import junit.framework.TestCase;
@@ -147,6 +144,37 @@
attribute = context.getCascadedAttribute("name2");
assertNotNull("Attribute name2 not found", attribute);
assertEquals("Attribute name2 has not been set correctly", "value2",
+ attribute.getValue());
+ }
+
+ /**
+ * Tests [EMAIL PROTECTED]
BasicAttributeContext#inheritCascadedAttributes(AttributeContext)}.
+ */
+ public void testInherit() {
+ AttributeContext toCopy = new BasicAttributeContext();
+ toCopy.putAttribute("name1", new Attribute("value1"), true);
+ toCopy.putAttribute("name2", new Attribute("value2"), true);
+ toCopy.putAttribute("name3", new Attribute("value3"), false);
+ toCopy.putAttribute("name4", new Attribute("value4"), false);
+ AttributeContext context = new BasicAttributeContext();
+ toCopy.putAttribute("name1", new Attribute("newValue1"), true);
+ toCopy.putAttribute("name3", new Attribute("newValue3"), false);
+ context.inherit(toCopy);
+ Attribute attribute = context.getCascadedAttribute("name1");
+ assertNotNull("Attribute name1 not found", attribute);
+ assertEquals("Attribute name1 has not been set correctly", "newValue1",
+ attribute.getValue());
+ attribute = context.getCascadedAttribute("name2");
+ assertNotNull("Attribute name2 not found", attribute);
+ assertEquals("Attribute name2 has not been set correctly", "value2",
+ attribute.getValue());
+ attribute = context.getLocalAttribute("name3");
+ assertNotNull("Attribute name3 not found", attribute);
+ assertEquals("Attribute name3 has not been set correctly", "newValue3",
+ attribute.getValue());
+ attribute = context.getLocalAttribute("name4");
+ assertNotNull("Attribute name4 not found", attribute);
+ assertEquals("Attribute name4 has not been set correctly", "value4",
attribute.getValue());
}
Modified:
tiles/framework/trunk/tiles-api/src/test/java/org/apache/tiles/TestDefinition.java
URL:
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-api/src/test/java/org/apache/tiles/TestDefinition.java?rev=617828&r1=617827&r2=617828&view=diff
==============================================================================
---
tiles/framework/trunk/tiles-api/src/test/java/org/apache/tiles/TestDefinition.java
(original)
+++
tiles/framework/trunk/tiles-api/src/test/java/org/apache/tiles/TestDefinition.java
Sat Feb 2 08:42:11 2008
@@ -80,5 +80,62 @@
attr1.getType() == AttributeType.DEFINITION);
}
+ /**
+ * Tests the [EMAIL PROTECTED] Definition#inherit(Definition)} method.
+ */
+ public void testInherit() {
+ Definition toCopy = new Definition();
+ toCopy.putAttribute("name1", new Attribute("value1"), true);
+ toCopy.putAttribute("name2", new Attribute("value2"), true);
+ toCopy.putAttribute("name3", new Attribute("value3"), false);
+ toCopy.putAttribute("name4", new Attribute("value4"), false);
+ Definition context = new Definition();
+ toCopy.putAttribute("name1", new Attribute("newValue1"), true);
+ toCopy.putAttribute("name3", new Attribute("newValue3"), false);
+ context.inherit(toCopy);
+ Attribute attribute = context.getCascadedAttribute("name1");
+ assertNotNull("Attribute name1 not found", attribute);
+ assertEquals("Attribute name1 has not been set correctly", "newValue1",
+ attribute.getValue());
+ attribute = context.getCascadedAttribute("name2");
+ assertNotNull("Attribute name2 not found", attribute);
+ assertEquals("Attribute name2 has not been set correctly", "value2",
+ attribute.getValue());
+ attribute = context.getLocalAttribute("name3");
+ assertNotNull("Attribute name3 not found", attribute);
+ assertEquals("Attribute name3 has not been set correctly", "newValue3",
+ attribute.getValue());
+ attribute = context.getLocalAttribute("name4");
+ assertNotNull("Attribute name4 not found", attribute);
+ assertEquals("Attribute name4 has not been set correctly", "value4",
+ attribute.getValue());
+
+ toCopy = new Definition();
+ toCopy.setPreparer("ExtendedPreparer");
+ toCopy.setRole("extendedRole");
+ toCopy.setTemplate("extendedTemplate.jsp");
+ context = new Definition();
+ context.inherit(toCopy);
+ assertEquals("Preparer not inherited", "ExtendedPreparer", context
+ .getPreparer());
+ assertNotNull("Roles not inherited", context.getRoles());
+ assertEquals("Roles not inherited", context.getRoles().size(), 1);
+ assertTrue("Roles not inherited", context.getRoles().contains(
+ "extendedRole"));
+ assertEquals("Template not inherited", "extendedTemplate.jsp", context
+ .getTemplate());
+ context = new Definition();
+ context.setPreparer("LocalPreparer");
+ context.setRole("localRole");
+ context.setTemplate("localTemplate.jsp");
+ assertEquals("Preparer inherited", "LocalPreparer", context
+ .getPreparer());
+ assertNotNull("Roles not correct", context.getRoles());
+ assertEquals("Roles not correct", context.getRoles().size(), 1);
+ assertTrue("Roles inherited", context.getRoles().contains(
+ "localRole"));
+ assertEquals("Template inherited", "localTemplate.jsp", context
+ .getTemplate());
+ }
}
Modified:
tiles/framework/trunk/tiles-jsp/src/main/java/org/apache/tiles/jsp/taglib/definition/DefinitionTag.java
URL:
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-jsp/src/main/java/org/apache/tiles/jsp/taglib/definition/DefinitionTag.java?rev=617828&r1=617827&r2=617828&view=diff
==============================================================================
---
tiles/framework/trunk/tiles-jsp/src/main/java/org/apache/tiles/jsp/taglib/definition/DefinitionTag.java
(original)
+++
tiles/framework/trunk/tiles-jsp/src/main/java/org/apache/tiles/jsp/taglib/definition/DefinitionTag.java
Sat Feb 2 08:42:11 2008
@@ -212,7 +212,7 @@
d.setExtends(extend);
d.setRole(role);
d.setPreparer(preparer);
- d.getAttributes().putAll(attributes);
+ d.addAll(attributes);
try {
container.register(d, pageContext);