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 javax.servlet.ServletRequest;
022: import javax.servlet.jsp.JspException;
023: import javax.servlet.jsp.tagext.TagSupport;
024: import java.io.IOException;
025: import java.io.Writer;
026: import java.util.HashMap;
027:
028: /**
029: * The Attribute tag defines an attribute within a template that may be set
030: * from a content page. For example, the page's title may be defined as an
031: * attribute in the template and then provided by each content page using the
032: * template. The attribute has a name and default value. If the content
033: * page specifies a value for the attribute it will be used, otherwise
034: * the default value is used.
035:
036: * @jsptagref.tagdescription
037: *
038: * Defines a property placeholder within a template. The
039: * value of these placeholders may be set
040: * from a content page.
041: *
042: * <p>For example, a title placeholder may be defined
043: * in the template.
044: *
045: * <p><b>In the template JSP page...</b>
046: *
047: * <pre> <head>
048: * <title>
049: * <netui-template:attribute name="title"/>
050: * </title>
051: * </head></pre>
052: *
053: * <p>Then content pages may set the value of this placeholder using the
054: * {@link SetAttribute} tag.
055: *
056: * <p><b>In a content JSP page...</b>
057: *
058: * <pre> <netui-template:setAttribute name="title" value="myContentPage1.jsp"/></pre>
059: *
060: * <p>The HTML rendered in the browser appears as follows.
061: *
062: * <p><b>Rendered HTML in the browser...</b>
063: *
064: * <pre> <head>
065: * <title>
066: * myContentPage1.jsp
067: * </title>
068: * </head></pre>
069: *
070: * If the <netui-template:setAttribute> tag specifies no value to be set in the
071: * placeholder, then the
072: * {@link Attribute} tag's <code>defaultValue</code> will be used.
073: *
074: * <pre> <netui-template:attribute name="title" <b>defaultValue="My Page"</b>/></pre>
075: *
076: * The <netui-template:attribute> tag may also be used to define placeholders within
077: * JSP and HTML tags.
078: *
079: * <p><b>In the template JSP page...</b>
080: *
081: * <pre> <td colspan="3" bgcolor="<b><netui-template:attribute name="headerColor" defaultValue="#ffffff"/></b>"></pre>
082: *
083: * @example
084: * <p>In this sample, a <netui-template:attribute> tag defines a value placeholder
085: * within a <td> tag</p>
086: *
087: * <pre> <td colspan="3" bgcolor="<b><netui-template:attribute name="headerColor" defaultValue="#ffffff"/></b>"></pre>
088: *
089: * <p>Now a content JSP page can control the background color of the <td>.
090: *
091: * <pre> <netui-template:setAttribute name="headerColor" value="lightgreen"/></pre>
092: *
093: * The HTML rendered in the browser will appear as follows.
094: *
095: * <pre> <td colspan="3" bgcolor="lightgreen"></pre>
096: *
097: * @netui:tag name="attribute" description="Place this tag in a template file, and then set its value with the netui-template:setAttribute tag."
098: */
099: public class Attribute extends TagSupport implements TemplateConstants {
100: /**
101: * The name of the attribute.
102: */
103: private String _name;
104:
105: /**
106: * Default value
107: */
108: private String _defaultValue;
109:
110: /**
111: * Sets the <code>name</code> for the <code>Attribute</code>. An
112: * attribute may be used more than once in a template page.
113: * @param name The name of the attribute. The name does
114: * not need to be unique because it may be used more than once
115: * on the page.
116: *
117: * @jsptagref.attributedescription
118: * The <code>name</code> for the <netui-template:attribute> placeholder. The <code>name</code>
119: * may be used more than once in a template page.
120: *
121: * @jsptagref.databindable false
122: *
123: * @jsptagref.attributesyntaxvalue <i>string_name</i>
124: *
125: * @netui:attribute required="true" rtexprvalue="true"
126: * description="The name for the <netui-template:attribute> placeholder. The name
127: * may be used more than once in a template page."
128: */
129: public void setName(String name) {
130: _name = name;
131: }
132:
133: /**
134: * Sets the <code>defaultValue</code> for the <code>Attribute</code>.
135: * If the content page does not define a value for this attribute
136: * through the <code>SetAttribute</code> tag, then the
137: * <code>defaultValue</code> will be used.
138: * If neither a value nor <code>defaultValue</code> is set, then the
139: * empty String "" will be output.
140: * @param defaultValue The value to set the defaultValue property.
141: *
142: * @jsptagref.attributedescription
143: * The default value for <netui-template:attribute> placeholder.
144: * If a content page does not define a value for the placeholder
145: * through its <netui-template:setAttribute> tag, then the
146: * <code>defaultValue</code> will be used.
147: * If neither a value nor <code>defaultValue</code> is set, then the
148: * empty String "" will be output.
149: *
150: * @jsptagref.databindable false
151: *
152: * @jsptagref.attributesyntaxvalue <i>string_defaultValue</i>
153: *
154: * @netui:attribute required="false" rtexprvalue="true"
155: * description="The default value for <netui-template:attribute> placeholder."
156: */
157: public void setDefaultValue(String defaultValue) {
158: _defaultValue = defaultValue;
159: }
160:
161: /**
162: * Renders the content of the attribute.
163: * @return EVAL_PAGE to continue evaluation of the page.
164: * @throws JspException If there is any failure in the tag.
165: */
166: public int doStartTag() throws JspException {
167: ServletRequest req = pageContext.getRequest();
168: HashMap atts = (HashMap) req.getAttribute(TEMPLATE_ATTRIBUTES);
169: try {
170: if (atts != null) {
171: String val = (String) atts.get(_name);
172: if (val != null) {
173: Writer out = pageContext.getOut();
174: out.write(val);
175: } else {
176: Writer out = pageContext.getOut();
177: if (_defaultValue != null)
178: out.write(_defaultValue);
179: else
180: out.write("");
181: }
182: } else {
183: Writer out = pageContext.getOut();
184: if (_defaultValue != null)
185: out.write(_defaultValue);
186: else
187: out.write("");
188: }
189: } catch (IOException e) {
190: localRelease();
191: JspException jspException = new JspException(
192: "Caught IO Exception:" + e.getMessage(), e);
193: // todo: future cleanup
194: // The 2.5 Servlet api will set the initCause in the Throwable superclass during construction,
195: // this will cause an IllegalStateException on the following call.
196: if (jspException.getCause() == null) {
197: jspException.initCause(e);
198: }
199: throw jspException;
200: }
201: localRelease();
202: return EVAL_PAGE;
203: }
204:
205: /**
206: * Resets all of the fields of the tag.
207: */
208: // this is the root because this tag doesn't extend AbstractBaseTag
209: protected void localRelease() {
210: _name = null;
211: _defaultValue = null;
212: }
213: }
|