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.html;
020:
021: import org.apache.beehive.netui.tags.AbstractClassicTag;
022: import org.apache.beehive.netui.tags.ByRef;
023: import org.apache.beehive.netui.tags.ExpressionHandling;
024: import org.apache.beehive.netui.tags.rendering.AbstractHtmlControlState;
025:
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.jsp.JspException;
028: import java.util.List;
029:
030: /**
031: * Abstract Base class adding support for the <code>dataSource</code> attribute.
032: */
033: abstract public class HtmlDataSourceTag extends HtmlFocusBaseTag {
034: protected String _dataSource;
035:
036: public HtmlDataSourceTag() {
037: super ();
038: }
039:
040: /**
041: * Sets the tag's data source (can be an expression).
042: * @param dataSource the data source
043: * @jsptagref.attributedescription <p>The <code>dataSource</code> attribute determines both
044: * (1) the source of populating data for the tag and
045: * (2) the object to which the tag submits data.
046: *
047: * <p>For example, assume that the Controller file (= JPF file) contains
048: * a Form Bean with the property foo. Then the following <netui:textBox> tag will
049: * (1) draw populating data from the Form Bean's foo property and (2)
050: * submit user defined data to the same property.
051: *
052: * <p> <code><netui:textBox dataSource="actionForm.foo" /></code>
053: *
054: * <p>The <code>dataSource</code> attribute takes either a data binding expression or
055: * the name of a Form Bean property. In the
056: * above example, <code><netui:textBox dataSource="foo" /></code>
057: * would have the exactly same behavior.
058: *
059: * <p>When the tag is used to submit data, the data binding expression must
060: * refer to a Form Bean property.
061: * In cases where the tag is not used to submit data, but is used for
062: * displaying data only, the data
063: * binding expression need not refer to a Form Bean property. For example,
064: * assume that myIterativeData is a member variable on
065: * the Controller file ( = JPF file). The following <netui-data:repeater>
066: * tag draws its data from myIterativeData.
067: *
068: * <p> <code><netui-data:repeater dataSource="pageFlow.myIterativeData"></code>
069: * @jsptagref.databindable true
070: * @jsptagref.attributesyntaxvalue <i>expression_datasource</i>
071: * @netui:attribute required="true"
072: * description="The <code>dataSource</code> attribute determines both
073: * the source of populating data for the tag and
074: * the object to which the tag submits data."
075: */
076: public void setDataSource(String dataSource) {
077: _dataSource = dataSource;
078: }
079:
080: /**
081: * Return an <code>ArrayList</code> which represents a chain of <code>INameInterceptor</code>
082: * objects. This method by default returns <code>null</code> and should be overridden
083: * by objects that support naming.
084: * @return an <code>ArrayList</code> that will contain <code>INameInterceptor</code> objects.
085: */
086: protected List getNamingChain() {
087: return AbstractClassicTag.DefaultNamingChain;
088: }
089:
090: /**
091: * Return the Object that is represented by the specified data source.
092: * @return Object
093: * @throws JspException
094: */
095: protected Object evaluateDataSource() throws JspException {
096: // @todo: at some point we need to switch the expression evaluation to not require '{'
097: ExpressionHandling expr = new ExpressionHandling(this );
098: String datasource = "{" + _dataSource + "}";
099: String ds = expr.ensureValidExpression(datasource,
100: "dataSource", "DataSourceError");
101: if (ds == null)
102: return null;
103:
104: // have a valid expression
105: return expr.evaluateExpression(datasource, "dataSource",
106: pageContext);
107: }
108:
109: /**
110: * This method will create the name of the form element (HTML Control) that has a name. The
111: * <b>name</b> attribute represent the "control name" for the control. This name is scoped
112: * into the form element. In addition, a control may have a <b>id</b> attribute which is
113: * specified by setting the <b>tagId</b>. These two value are set in this routine. The name
114: * is always the expression mapping the data to it's backing element and is conotrolled
115: * by the optional naming chain provided by the tag. The <b>tagId</b> specifies the <b>id</b>
116: * attribute. If this is present then we write out a JavaScript that allows mapping
117: * the tagId set on the tag to both the real <b>id</b> value and also the <b>name</b> value.
118: * The <b>id</b> is formed by passing the <b>tagId</b> to the URL rewritter service.
119: * @param state The tag state structure. This contains both the name and id attributes. The
120: * id attribute should be set with the initial value from the tagId.
121: * @param javaScript A ByRef element that will contain any JavaScript that should be written out
122: * by the calling tag. A value is returned only if tagId is set and there is not IScriptReporter
123: * found.
124: * @throws JspException Pass through the exception from applyNamingChain.
125: */
126: protected void nameHtmlControl(AbstractHtmlControlState state,
127: ByRef javaScript) throws JspException {
128: assert (javaScript != null) : "paramater 'javaScript' may not be null";
129: assert (state != null) : "parameter 'state' may not be null";
130: assert (_dataSource != null) : "dataSource is Null";
131:
132: // create the expression (name)
133: String datasource = "{" + _dataSource + "}";
134: state.name = applyNamingChain(datasource);
135:
136: Form parentForm = getNearestForm();
137: String idScript = renderNameAndId(
138: (HttpServletRequest) pageContext.getRequest(), state,
139: parentForm);
140: if (idScript != null)
141: javaScript.setRef(idScript);
142: }
143:
144: /**
145: * Release any acquired resources.
146: */
147: protected void localRelease() {
148: super.localRelease();
149: _dataSource = null;
150: }
151: }
|