001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.tags.template;
020:
021: import org.apache.beehive.netui.tags.AbstractClassicTag;
022:
023: import javax.servlet.ServletRequest;
024: import javax.servlet.jsp.JspException;
025: import java.util.HashMap;
026:
027: /**
028: * Set an <code>Attribute</code> value defined in a template. This tag is
029: * used in content pages to set the value of attributes defined in a template.
030: * The attribute value will override any default value defined on the
031: * <code>Attribute</code>.
032:
033: * @jsptagref.tagdescription
034: *
035: * Sets a property value in a template page.
036: *
037: * <p>The <netui-template:setAttribute> tag must have a parent
038: * {@link Template} tag.
039: *
040: * <p>The target placeholder is defined by a {@link Attribute} tag. For a value to be set
041: * in the placeholder, the <netui-template:attribute> and
042: * <netui-template:setAttribute> tags must have matching <code>name</code> attributes.
043: *
044: * <p>For example, a placeholder may be defined
045: * in the template.
046: *
047: * <p><b>In the template JSP page...</b>
048: *
049: * <pre> <head>
050: * <title>
051: * <netui-template:attribute name="title"/>
052: * </title>
053: * </head></pre>
054: *
055: * <p>Then content pages may set the value of this placeholder using the
056: * <netui-template:setAttribute> tag.
057: *
058: * <p><b>In a content JSP page...</b>
059: *
060: * <pre> <netui-template:setAttribute name="title" value="myContentPage1.jsp"/></pre>
061: *
062: * <p>The HTML rendered in the browser appears as follows.
063: *
064: * <p><b>Rendered HTML in the browser...</b>
065: *
066: * <pre> <head>
067: * <title>
068: * myContentPage1.jsp
069: * </title>
070: * </head></pre>
071: *
072: * If the <netui-template:setAttribute> tag specifies no value to be set in the placeholder, then the
073: * <netui-template:attribute> tag's <code>defaultValue</code> will be used.
074: *
075: * <pre> <netui-template:attribute name="title" <b>defaultValue="My Page"</b>/></pre>
076: *
077: * The <netui-template:attribute> tag may also be used to define placeholders within
078: * JSP and HTML tags.
079: *
080: * <p><b>In the template JSP page...</b>
081: *
082: * <pre> <td colspan="3" bgcolor="<b><netui-template:attribute name="headerColor" defaultValue="#ffffff"/></b>"></pre>
083: *
084: * @example
085: * <p>Assume a <netui-template:attribute> tag defines a value placeholder
086: * within a <td> tag</p>
087: *
088: * <pre> <td colspan="3" bgcolor="<b><netui-template:attribute name="headerColor" defaultValue="#ffffff"/></b>"></pre>
089: *
090: * <p>Now a content JSP page can control the background color of the <td>.
091: *
092: * <pre> <netui-template:setAttribute name="headerColor" value="lightgreen"/></pre>
093: *
094: * The HTML rendered in the browser will appear as follows.
095: *
096: * <pre> <td colspan="3" bgcolor="lightgreen"></pre>
097: *
098: * @netui:tag name="setAttribute"
099: * description="Use this tag to set the value of an netui-template:attribute element in a template file."
100: */
101: public class SetAttribute extends AbstractClassicTag implements
102: TemplateConstants {
103: /**
104: * The name of the attribute.
105: */
106: private String _name;
107:
108: /**
109: * The value of the attribute.
110: */
111: private String _value;
112:
113: /**
114: * Return the name of the tag. This is used by error reporting
115: * in the base class <code>AbstractBaseTag</code>.
116: */
117: public String getTagName() {
118: return "SetAttribute";
119: }
120:
121: /**
122: * Set the <code>name</code> of the attribute.
123: * @param name The name of the <code>Attribute</code> in the
124: * template for which this tags sets the value.
125: *
126: * @jsptagref.attributedescription
127: * The name of the attribute to set.
128: *
129: * @jsptagref.databindable false
130: *
131: * @jsptagref.attributesyntaxvalue <i>string_name</i>
132: *
133: * @netui:attribute required="true" rtexprvalue="true"
134: * description="The name of the attribute to set."
135: */
136: public void setName(String name) {
137: _name = name;
138: }
139:
140: /**
141: * Set the value of the <code>Attribute</code>. This attribute
142: * may be assigned a read only expression.
143: * @param value The value to use for the <code>Attribute</code>
144: * in the template.
145: *
146: * @jsptagref.attributedescription
147: * Sets the value of the attribute.
148: *
149: * @jsptagref.databindable Read Only
150: *
151: * @jsptagref.attributesyntaxvalue <i>string_or_expression_value</i>
152: *
153: * @netui:attribute required="true" rtexprvalue="true"
154: * description="Sets the value of the attribute."
155: */
156: public void setValue(String value) throws JspException {
157: _value = value;
158: }
159:
160: /**
161: * Tag Lifecycle method called when the tag is first seen. This method
162: * will add the <code>Attribute</code> value to a <code>HashMap</code>
163: * stored in the request allowing the template to access the value. If
164: * there are errors, the error text will be placed into the attribute
165: * value. Nothing is written into the <code>ServletResponse</code>
166: * @return EVAL_PAGE to continue processing the page.
167: * @throws JspException on error
168: */
169: public int doStartTag() throws JspException {
170: ServletRequest req = pageContext.getRequest();
171: HashMap atts = (HashMap) req.getAttribute(TEMPLATE_ATTRIBUTES);
172: if (atts == null) {
173: atts = new HashMap();
174: req.setAttribute(TEMPLATE_ATTRIBUTES, atts);
175: }
176: if (hasErrors()) {
177: String s = getErrorsReport();
178: atts.put(_name, s);
179: localRelease();
180: return EVAL_PAGE;
181: }
182:
183: atts.put(_name, _value);
184: localRelease();
185: return EVAL_PAGE;
186: }
187:
188: /**
189: * Reset all of the fields of the tag.
190: */
191: protected void localRelease() {
192: super.localRelease();
193: _name = null;
194: _value = null;
195: }
196: }
|