KickJava   Java API By Example, From Geeks To Geeks.

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


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.tags.AbstractClassicTag;
21 import org.apache.beehive.netui.tags.ByRef;
22 import org.apache.beehive.netui.tags.ExpressionHandling;
23 import org.apache.beehive.netui.tags.rendering.AbstractHtmlControlState;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.jsp.JspException JavaDoc;
27 import java.util.List JavaDoc;
28
29 /**
30  * Abstract Base class adding support for the <code>dataSource</code> attribute.
31  */

32 abstract public class HtmlDataSourceTag
33         extends HtmlFocusBaseTag
34 {
35     protected String JavaDoc _dataSource;
36
37     public HtmlDataSourceTag()
38     {
39         super();
40     }
41
42     /**
43      * Sets the tag's data source (can be an expression).
44      * @param dataSource the data source
45      * @jsptagref.attributedescription <p>The <code>dataSource</code> attribute determines both
46      * (1) the source of populating data for the tag and
47      * (2) the object to which the tag submits data.
48      *
49      * <p>For example, assume that the Controller file (= JPF file) contains
50      * a Form Bean with the property foo. Then the following &lt;netui:textBox> tag will
51      * (1) draw populating data from the Form Bean's foo property and (2)
52      * submit user defined data to the same property.
53      *
54      * <p>&nbsp;&nbsp;&nbsp;&nbsp;<code>&lt;netui:textBox dataSource="actionForm.foo" /></code>
55      *
56      * <p>The <code>dataSource</code> attribute takes either a data binding expression or
57      * the name of a Form Bean property. In the
58      * above example, <code>&lt;netui:textBox dataSource="foo" /></code>
59      * would have the exactly same behavior.
60      *
61      * <p>When the tag is used to submit data, the data binding expression must
62      * refer to a Form Bean property.
63      * In cases where the tag is not used to submit data, but is used for
64      * displaying data only, the data
65      * binding expression need not refer to a Form Bean property. For example,
66      * assume that myIterativeData is a member variable on
67      * the Controller file ( = JPF file). The following &lt;netui-data:repeater>
68      * tag draws its data from myIterativeData.
69      *
70      * <p>&nbsp;&nbsp;&nbsp;&nbsp;<code>&lt;netui-data:repeater dataSource="pageFlow.myIterativeData"></code>
71      * @jsptagref.databindable true
72      * @jsptagref.attributesyntaxvalue <i>expression_datasource</i>
73      * @netui:attribute required="true"
74      * description="The <code>dataSource</code> attribute determines both
75      * the source of populating data for the tag and
76      * the object to which the tag submits data."
77      */

78     public void setDataSource(String JavaDoc dataSource)
79     {
80         _dataSource = dataSource;
81     }
82
83     /**
84      * Return an <code>ArrayList</code> which represents a chain of <code>INameInterceptor</code>
85      * objects. This method by default returns <code>null</code> and should be overridden
86      * by objects that support naming.
87      * @return an <code>ArrayList</code> that will contain <code>INameInterceptor</code> objects.
88      */

89     protected List JavaDoc getNamingChain()
90     {
91         return AbstractClassicTag.DefaultNamingChain;
92     }
93
94     /**
95      * Return the Object that is represented by the specified data source.
96      * @return Object
97      * @throws JspException
98      */

99     protected Object JavaDoc evaluateDataSource()
100             throws JspException JavaDoc
101     {
102         // @todo: at some point we need to switch the expression evaluation to not require '{'
103
ExpressionHandling expr = new ExpressionHandling(this);
104         String JavaDoc datasource = "{" + _dataSource + "}";
105         String JavaDoc ds = expr.ensureValidExpression(datasource, "dataSource", "DataSourceError");
106         if (ds == null)
107             return null;
108
109         // have a valid expression
110
return expr.evaluateExpression(datasource, "dataSource", pageContext);
111     }
112
113     /**
114      * This method will create the name of the form element (HTML Control) that has a name. The
115      * <b>name</b> attribute represent the "control name" for the control. This name is scoped
116      * into the form element. In addition, a control may have a <b>id</b> attribute which is
117      * specified by setting the <b>tagId</b>. These two value are set in this routine. The name
118      * is always the expression mapping the data to it's backing element and is conotrolled
119      * by the optional naming chain provided by the tag. The <b>tagId</b> specifies the <b>id</b>
120      * attribute. If this is present then we write out a JavaScript that allows mapping
121      * the tagId set on the tag to both the real <b>id</b> value and also the <b>name</b> value.
122      * The <b>id</b> is formed by passing the <b>tagId</b> to the URL rewritter service.
123      * @param state The tag state structure. This contains both the name and id attributes. The
124      * id attribute should be set with the initial value from the tagId.
125      * @param javaScript A ByRef element that will contain any JavaScript that should be written out
126      * by the calling tag. A value is returned only if tagId is set and there is not IScriptReporter
127      * found.
128      * @throws JspException Pass through the exception from applyNamingChain.
129      */

130     protected void nameHtmlControl(AbstractHtmlControlState state, ByRef javaScript)
131             throws JspException JavaDoc
132     {
133         assert (javaScript != null) : "paramater 'javaScript' may not be null";
134         assert (state != null) : "parameter 'state' may not be null";
135         assert (_dataSource != null) : "dataSource is Null";
136
137         // create the expression (name)
138
String JavaDoc datasource = "{" + _dataSource + "}";
139         state.name = applyNamingChain(datasource);
140
141         Form parentForm = getNearestForm();
142         String JavaDoc idScript = renderNameAndId((HttpServletRequest JavaDoc) pageContext.getRequest(), state, parentForm);
143         if (idScript != null)
144             javaScript.setRef(idScript);
145     }
146
147     /**
148      * Release any acquired resources.
149      */

150     protected void localRelease()
151     {
152         super.localRelease();
153         _dataSource = null;
154     }
155 }
156
Popular Tags