KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > html > Hidden


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.html;
19
20 import org.apache.beehive.netui.util.internal.InternalStringBuilder;
21
22 import org.apache.beehive.netui.tags.ByRef;
23 import org.apache.beehive.netui.tags.HtmlUtils;
24 import org.apache.beehive.netui.tags.rendering.*;
25 import org.apache.beehive.netui.util.Bundle;
26
27 import javax.servlet.ServletRequest JavaDoc;
28 import javax.servlet.jsp.JspException JavaDoc;
29
30
31 /**
32  * Generates a hidden tag with a given value. Hidden ignores its body content.
33  * @jsptagref.tagdescription Generates an HTML hidden tag with a given value.
34  *
35  * <p>The <code>dataInput</code> attribute overrides the <code>dataSource</code> attribute
36  * for the input of the value.
37  * It allows a &lt;netui:hidden> tag to read it's value from one place (whatever is referenced by
38  * the <code>dataInput</code> attribute) and submit the value to a new destination
39  * (whatever is referenced by the <code>dataSource</code> attribute).
40  * The <code>dataInput</code> attribute may take a String literal or a data binding expression.
41  * @example In this sample, the Hidden tag is written using the value from the form bean's status property.
42  * <pre>&lt;netui:hidden dataSource="actionForm.status" /></pre>
43  * @netui:tag name="hidden" description="Generates a hidden tag with a given value."
44  */

45 public class Hidden
46         extends HtmlDataSourceTag
47 {
48     private InputHiddenTag.State _state = new InputHiddenTag.State();
49
50     private String JavaDoc _value;
51     private Object JavaDoc _dataInput;
52
53     /**
54      * Return the name of the Tag.
55      */

56     public String JavaDoc getTagName()
57     {
58         return "Hidden";
59     }
60
61     /**
62      * This method will return the state associated with the tag. This is used by this
63      * base class to access the individual state objects created by the tags.
64      * @return a subclass of the <code>AbstractHtmlState</code> class.
65      */

66     protected AbstractHtmlState getState()
67     {
68         return _state;
69     }
70
71     /**
72      * Base support for the attribute tag. This is overridden to prevent setting the <code>type</code>
73      * or <code>value</code> attributes.
74      * @param name The name of the attribute
75      * @param value The value of the attribute
76      */

77     public void setAttribute(String JavaDoc name, String JavaDoc value, String JavaDoc facet)
78             throws JspException JavaDoc
79     {
80         if (name == null)
81             return;
82
83         if (name.equals(TYPE) || name.equals(VALUE)) {
84             String JavaDoc s = Bundle.getString("Tags_AttributeMayNotBeSet", new Object JavaDoc[]{name});
85             registerTagError(s, null);
86         }
87         super.setAttribute(name, value, facet);
88     }
89
90     /**
91      * Set the data input. This value will override the <code>
92      * dataSource</code> and provide the input value on the select box.
93      * @param dataInput the value of the input to the page. This value
94      * may contain an expression.
95      * @jsptagref.attributedescription The dataInput attribute overrides the dataSource attribute for the input of the value.
96      * It allows a &lt;netui:hidden> tag to read it's value from one place (whatever is referenced by
97      * the <code>dataInput</code> attribute) and return the value to a new destination (whatever is
98      * referenced by the <code>dataSource</code> attribute).
99      * This attribute may take a String literal or a data binding expression.
100      * @jsptagref.databindable true
101      * @jsptagref.attributesyntaxvalue <i>string_or_expression_dataInput</i>
102      * @netui:attribute required="false" rtexprvalue="true" type="java.lang.Object"
103      * description="The dataInput attribute overrides the dataSource attribute for the input of the value. "
104      */

105     public void setDataInput(Object JavaDoc dataInput)
106     {
107         _dataInput = dataInput;
108     }
109
110     /**
111      * Render the Hidden tag
112      * @throws JspException if a JSP exception has occurred
113      */

114     public int doStartTag() throws JspException JavaDoc
115     {
116         return EVAL_BODY_BUFFERED;
117     }
118
119     /**
120      * Save the body content of the Hidden.
121      * @throws JspException if a JSP exception has occurred
122      */

123     public int doAfterBody() throws JspException JavaDoc
124     {
125
126         if (bodyContent != null) {
127             bodyContent.clearBody();
128         }
129         return SKIP_BODY;
130     }
131
132     /**
133      * Generate the hidden input tag.
134      * @throws JspException if a JSP exception has occurred
135      */

136     public int doEndTag() throws JspException JavaDoc
137     {
138         Object JavaDoc val = evaluateDataSource();
139
140         ServletRequest JavaDoc req = pageContext.getRequest();
141
142         if (_dataInput != null) {
143             val = _dataInput.toString();
144         }
145
146         // if there were expression errors report them
147
if (hasErrors())
148             return reportAndExit(SKIP_BODY);
149
150         if (val != null) {
151             _value = val.toString();
152         }
153
154         // Create an appropriate "input" element based on our parameters
155
ByRef ref = new ByRef();
156         nameHtmlControl(_state, ref);
157
158         if (_value != null) {
159             InternalStringBuilder sb = new InternalStringBuilder(_value.length() + 16);
160             StringBuilderRenderAppender sbAppend = new StringBuilderRenderAppender(sb);
161             HtmlUtils.filter(_value, sbAppend);
162             _state.value = sb.toString();
163         }
164
165         // correct for null text here
166
if (_state.value == null)
167             _state.value = "";
168
169         WriteRenderAppender writer = new WriteRenderAppender(pageContext);
170         TagRenderingBase hiddenTag = TagRenderingBase.Factory.getRendering(TagRenderingBase.INPUT_HIDDEN_TAG, req);
171
172         hiddenTag.doStartTag(writer, _state);
173         hiddenTag.doEndTag(writer);
174
175         if (!ref.isNull())
176             write((String JavaDoc) ref.getRef());
177
178         // Continue processing this page
179
localRelease();
180         return SKIP_BODY;
181     }
182
183     /**
184      * Release any acquired resources.
185      */

186     protected void localRelease()
187     {
188         super.localRelease();
189
190         _state.clear();
191         _value = null;
192         _dataInput = null;
193     }
194
195     /* ==================================================================
196      *
197      * This tag's publically exposed HTML, CSS, and JavaScript attributes
198      *
199      * ==================================================================
200      */

201     /**
202      * Sets the tabIndex of the rendered html tag.
203      * @param tabindex the tab index.
204      * @jsptagref.attributedescription The tabIndex of the rendered HTML tag. This attribute determines the position of the
205      * rendered HTML tag in the sequence of tags that the user may advance through by pressing the TAB key.
206      * @jsptagref.databindable false
207      * @jsptagref.attributesyntaxvalue <i>string_tabIndex</i>
208      * @netui:attribute required="false" rtexprvalue="true" type="int"
209      * description="The tabIndex of the rendered HTML tag. This attribute determines the position of the
210      * rendered HTML tag in the sequence of tags that the user may advance through by pressing the TAB key."
211      */

212     public void setTabindex(int tabindex)
213     {
214         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, HtmlConstants.TABINDEX, Integer.toString(tabindex));
215     }
216 }
217
Popular Tags