Author: apetrelli
Date: Tue Jan 13 07:26:38 2009
New Revision: 734153
URL: http://svn.apache.org/viewvc?rev=734153&view=rev
Log:
TILES-336
Added default values attributes to <tiles:insertAttribute> tag.
Added Selenium testcase.
Modified:
tiles/framework/trunk/tiles-jsp/src/main/java/org/apache/tiles/jsp/taglib/InsertAttributeTag.java
tiles/framework/trunk/tiles-jsp/src/main/resources/META-INF/tld/tiles-jsp.tld
tiles/framework/trunk/tiles-test/src/main/webapp/WEB-INF/tiles-defs.xml
tiles/framework/trunk/tiles-test/src/main/webapp/index.jsp
tiles/framework/trunk/tiles-test/src/test/selenium/TestSuite.html
Modified:
tiles/framework/trunk/tiles-jsp/src/main/java/org/apache/tiles/jsp/taglib/InsertAttributeTag.java
URL:
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-jsp/src/main/java/org/apache/tiles/jsp/taglib/InsertAttributeTag.java?rev=734153&r1=734152&r2=734153&view=diff
==============================================================================
---
tiles/framework/trunk/tiles-jsp/src/main/java/org/apache/tiles/jsp/taglib/InsertAttributeTag.java
(original)
+++
tiles/framework/trunk/tiles-jsp/src/main/java/org/apache/tiles/jsp/taglib/InsertAttributeTag.java
Tue Jan 13 07:26:38 2009
@@ -49,6 +49,30 @@
protected Object value = null;
/**
+ * This value is evaluated only if <code>value</code> is null and the
+ * attribute with the associated <code>name</code> is null.
+ *
+ * @since 2.1.2
+ */
+ protected Object defaultValue;
+
+ /**
+ * The type of the {...@link #defaultValue}, if it is a string.
+ *
+ * @since 2.1.2
+ */
+ protected String defaultValueType;
+
+ /**
+ * The role to check for the default value. If the user is in the specified
+ * role, the default value is taken into account; otherwise, it is ignored
+ * (skipped).
+ *
+ * @since 2.1.2
+ */
+ protected String defaultValueRole;
+
+ /**
* The evaluated attribute.
*
* @since 2.1.0
@@ -91,12 +115,77 @@
this.value = value;
}
+ /**
+ * Returns the default value, that is evaluated only if <code>value</code>
+ * is null and the attribute with the associated <code>name</code> is null.
+ *
+ * @return The default value.
+ */
+ public Object getDefaultValue() {
+ return defaultValue;
+ }
+
+ /**
+ * Sets the default value, that is evaluated only if <code>value</code> is
+ * null and the attribute with the associated <code>name</code> is null.
+ *
+ * @param defaultValue The default value to set.
+ */
+ public void setDefaultValue(Object defaultValue) {
+ this.defaultValue = defaultValue;
+ }
+
+ /**
+ * Returns the default value type. It will be used only if
+ * {...@link #getDefaultValue()} is a string.
+ *
+ * @return The default value type.
+ */
+ public String getDefaultValueType() {
+ return defaultValueType;
+ }
+
+ /**
+ * Sets the default value type. To be used in conjunction with
+ * {...@link #setDefaultValue(Object)} when passing a string.
+ *
+ * @param defaultValueType The default value type.
+ */
+ public void setDefaultValueType(String defaultValueType) {
+ this.defaultValueType = defaultValueType;
+ }
+
+ /**
+ * Returns the role to check for the default value. If the user is in the
specified
+ * role, the default value is taken into account; otherwise, it is ignored
+ * (skipped).
+ *
+ * @return The default value role.
+ */
+ public String getDefaultValueRole() {
+ return defaultValueRole;
+ }
+
+ /**
+ * Sets the role to check for the default value. If the user is in the
specified
+ * role, the default value is taken into account; otherwise, it is ignored
+ * (skipped).
+ *
+ * @param defaultValueRole The default value role.
+ */
+ public void setDefaultValueRole(String defaultValueRole) {
+ this.defaultValueRole = defaultValueRole;
+ }
+
/** {...@inheritdoc} */
@Override
protected void reset() {
super.reset();
this.name = null;
this.value = null;
+ this.defaultValue = null;
+ this.defaultValueType = null;
+ this.defaultValueType = null;
this.attribute = null;
}
@@ -129,17 +218,7 @@
container.prepare(preparer, context);
}
- attribute = (Attribute) value;
-
- if (attribute == null) {
- AttributeContext evaluatingContext = container
- .getAttributeContext(context);
- attribute = evaluatingContext.getAttribute(name);
- if (attribute == null && !ignore) {
- throw new NoSuchAttributeException("Attribute '" + name
- + "' not found.");
- }
- }
+ attribute = computeAttribute(context);
super.startContext(context);
}
@@ -154,4 +233,46 @@
protected void render(Attribute attr) throws IOException {
container.render(attr, pageContext.getOut(), pageContext);
}
+
+ /**
+ * Computes the attribute to render, evaluating the various tag attributes.
+ *
+ * @param context The page context.
+ * @return The computed attribute.
+ */
+ private Attribute computeAttribute(PageContext context) {
+ Attribute attribute = (Attribute) value;
+
+ if (attribute == null) {
+ AttributeContext evaluatingContext = container
+ .getAttributeContext(context);
+ attribute = evaluatingContext.getAttribute(name);
+ if (attribute == null) {
+ attribute = computeDefaultAttribute();
+ if (attribute == null && !ignore) {
+ throw new NoSuchAttributeException("Attribute '" + name
+ + "' not found.");
+ }
+ }
+ }
+ return attribute;
+ }
+
+ /**
+ * Computes the default attribute.
+ *
+ * @return The default attribute.
+ */
+ private Attribute computeDefaultAttribute() {
+ Attribute attribute = null;
+ if (defaultValue != null) {
+ if (defaultValue instanceof Attribute) {
+ attribute = (Attribute) defaultValue;
+ } else if (defaultValue instanceof String) {
+ attribute = new Attribute(defaultValue,
+ defaultValueRole, defaultValueType);
+ }
+ }
+ return attribute;
+ }
}
Modified:
tiles/framework/trunk/tiles-jsp/src/main/resources/META-INF/tld/tiles-jsp.tld
URL:
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-jsp/src/main/resources/META-INF/tld/tiles-jsp.tld?rev=734153&r1=734152&r2=734153&view=diff
==============================================================================
---
tiles/framework/trunk/tiles-jsp/src/main/resources/META-INF/tld/tiles-jsp.tld
(original)
+++
tiles/framework/trunk/tiles-jsp/src/main/resources/META-INF/tld/tiles-jsp.tld
Tue Jan 13 07:26:38 2009
@@ -317,6 +317,44 @@
The fully qualified name of the preparer.
</description>
</attribute>
+ <attribute>
+ <name>defaultValue</name>
+ <required>false</required>
+ <rtexprvalue>true</rtexprvalue>
+ <type>java.lang.Object</type>
+ <description>
+ <![CDATA[
+ <p>This value is evaluated only if <code>value</code> is null and the
+ attribute with the associated <code>name</code> is null.</p>
+ ]]>
+ </description>
+ </attribute>
+ <attribute>
+ <name>defaultValueType</name>
+ <required>false</required>
+ <rtexprvalue>true</rtexprvalue>
+ <type>java.lang.Object</type>
+ <description>
+ <![CDATA[
+ <p>The type of the <code>defaultValue</code>, if it is a string.
+ To be used in conjunction with <code>defaultValue</code>
attribute.</p>
+ ]]>
+ </description>
+ </attribute>
+ <attribute>
+ <name>defaultValueRole</name>
+ <required>false</required>
+ <rtexprvalue>true</rtexprvalue>
+ <type>java.lang.Object</type>
+ <description>
+ <![CDATA[
+ <p>The role to check for the default value. If the user is in the
specified
+ role, the default value is taken into account; otherwise, it is
ignored
+ (skipped).
+ To be used in conjunction with <code>defaultValue</code>
attribute.</p>
+ ]]>
+ </description>
+ </attribute>
</tag>
<tag>
<name>definition</name>
Modified:
tiles/framework/trunk/tiles-test/src/main/webapp/WEB-INF/tiles-defs.xml
URL:
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-test/src/main/webapp/WEB-INF/tiles-defs.xml?rev=734153&r1=734152&r2=734153&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-test/src/main/webapp/WEB-INF/tiles-defs.xml
(original)
+++ tiles/framework/trunk/tiles-test/src/main/webapp/WEB-INF/tiles-defs.xml Tue
Jan 13 07:26:38 2009
@@ -237,4 +237,8 @@
<put-attribute name="header" value="/header.jsp"/>
<put-attribute name="body" value="/layout.jsp" />
</definition>
+
+ <definition name="test.defaultvalues.definition"
template="/layout_default.jsp">
+ <put-attribute name="title" value="This is the title."/>
+ </definition>
</tiles-definitions>
Modified: tiles/framework/trunk/tiles-test/src/main/webapp/index.jsp
URL:
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-test/src/main/webapp/index.jsp?rev=734153&r1=734152&r2=734153&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-test/src/main/webapp/index.jsp (original)
+++ tiles/framework/trunk/tiles-test/src/main/webapp/index.jsp Tue Jan 13
07:26:38 2009
@@ -46,6 +46,7 @@
<a href="testinsertdefinition_exception.jsp">Test Insert Configured
Definition with an exception in an attribute page</a><br/>
<a href="testinsertdefinition_freemarker.jsp">Test Insert Configured
Definition with FreeMarker</a><br/>
<a href="testinsertdefinition_openbody.jsp">Test Insert Configured
Definition with Open Body</a><br/>
+ <a href="testinsertdefinition_defaultvalues.jsp">Test Insert Configured
Definition with Default Values</a><br/>
<a href="testput.jsp">Test Put Tag</a><br/>
<a href="testput_flush.jsp">Test Put Tag with Flush</a><br/>
<a href="testput_el.jsp">Test Put Tag using EL</a><br/>
Modified: tiles/framework/trunk/tiles-test/src/test/selenium/TestSuite.html
URL:
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-test/src/test/selenium/TestSuite.html?rev=734153&r1=734152&r2=734153&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-test/src/test/selenium/TestSuite.html (original)
+++ tiles/framework/trunk/tiles-test/src/test/selenium/TestSuite.html Tue Jan
13 07:26:38 2009
@@ -106,6 +106,9 @@
<td><a href="ConfiguredDefinitionOpenBodyTest.html">Configured
Definition with Open Body Test</a></td>
</tr>
<tr>
+ <td><a href="ConfiguredDefinitionDefaultValuesTest.html">Configured
Definition with Default Values Test</a></td>
+ </tr>
+ <tr>
<td><a href="ConfiguredDefinitionWildcardTest.html">Configured
Definition Wildcard Test</a></td>
</tr>
<tr>