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.databinding.pageinput;
020:
021: import java.util.Map;
022: import javax.servlet.jsp.JspException;
023: import javax.servlet.jsp.PageContext;
024: import javax.servlet.http.HttpServletRequest;
025:
026: import org.apache.beehive.netui.pageflow.internal.InternalUtils;
027: import org.apache.beehive.netui.util.Bundle;
028: import org.apache.beehive.netui.util.logging.Logger;
029: import org.apache.beehive.netui.tags.AbstractSimpleTag;
030:
031: /**
032: * <p>
033: * The DeclarePageInput tag is used to declare variables that are passed as outputs of Page Flow actions to a
034: * JSP. This allows pages to declare a data contract that invoking actions must satisfy in order to
035: * successfully render a page; in essence, this is a simple method signature for the JSP.
036: * </p>
037: * <p>
038: * Page inputs are added to a Page Flow's {@link org.apache.beehive.netui.pageflow.Forward} class via the
039: * {@link org.apache.beehive.netui.pageflow.Forward#addActionOutput(String, Object)} method. From the page's
040: * perspective, the action outputs are known as <i>page inputs</i> and are available via the JSP EL
041: * implicit object <code>pageInput</code> using the name given them on the
042: * {@link org.apache.beehive.netui.pageflow.Forward} and set on this tag via {@link #setName(String)}.
043: * </p>
044: * <p>
045: * A page input can be declared to be required; if required, the page input must be available in
046: * the map of action outputs passed to the page.
047: * </p>
048: * <p>
049: * For example, to add an actiout output called <code>profile</code> to a {@link org.apache.beehive.netui.pageflow.Forward}
050: * an action would contain code like:<br/>
051: * <pre>
052: * forward.addActionOutput("profile", yourProfile);
053: * </pre>
054: * In order to declare this as a page input in a JSP, the page would contain a tag as:
055: * <pre>
056: * <netui-data:declarePageInput name="profile" type="org.someprofile.ProfileBean"/>
057: * </pre>
058: * and the <code>profile</code> object could be referenced in the JSP as:
059: * <pre>
060: * ${pageInput.profile}
061: * </pre>
062: * </p>
063: *
064: * @jsptagref.tagdescription
065: * <p>
066: * The DeclarePageInput tag is used to declare variables that are passed as outputs of Page Flow actions to a
067: * JSP. This allows pages to declare a data contract that invoking actions must satisfy in order to
068: * successfully render a page; in essence, this is a simple method signature for the JSP.
069: * </p>
070: * <p>
071: * Page inputs are added to a Page Flow's {@link org.apache.beehive.netui.pageflow.Forward} class via the
072: * {@link org.apache.beehive.netui.pageflow.Forward#addActionOutput(String, Object)} method. From the page's
073: * perspective, the action outputs are known as <i>page inputs</i> and are available via the JSP EL
074: * implicit object <code>pageInput</code> using the name given them on the
075: * {@link org.apache.beehive.netui.pageflow.Forward} and set on this tag via {@link #setName(String)}.
076: * </p>
077: * <p>
078: * A page input can be declared to be required; if required, the page input must be available in
079: * the map of action outputs passed to the page.
080: * </p>
081: * <p>
082: * For example, to add an actiout output called <code>profile</code> to a {@link org.apache.beehive.netui.pageflow.Forward}
083: * an action would contain code like:<br/>
084: * <pre>
085: * forward.addActionOutput("profile", yourProfile);
086: * </pre>
087: * In order to declare this as a page input in a JSP, the page would contain a tag as:
088: * <pre>
089: * <netui-data:declarePageInput name="profile" type="org.someprofile.ProfileBean"/>
090: * </pre>
091: * and the <code>profile</code> object could be referenced in the JSP as:
092: * <pre>
093: * ${pageInput.profile}
094: * </pre>
095: * </p>
096: *
097: * @netui:tag name="declarePageInput" body-content="scriptless"
098: * description="Use this tag to declare a page input variable that is available in the pageInput databinding context."
099: */
100: public class DeclarePageInput extends AbstractSimpleTag {
101:
102: private static final Logger LOGGER = Logger
103: .getInstance(DeclarePageInput.class);
104: private static final String EMPTY_STRING = "";
105:
106: private String _type = null;
107: private String _name = null;
108: private boolean _required = true;
109:
110: /**
111: * Get the name of this tag. This is used to identify the type of this tag
112: * for reporting tag errors.
113: *
114: * @return a constant String representing the name of this tag.
115: */
116: public String getTagName() {
117: return "DeclarePageInput";
118: }
119:
120: /**
121: * Set the name of a variable that can be referecned using the page
122: * input data binding context.
123: *
124: * @param name the name of the variable
125: * @jsptagref.attributedescription The name of the variable to reference.
126: * @jsptagref.attributesyntaxvalue <i>string_name</i>
127: * @netui:attribute required="true"
128: */
129: public void setName(String name) {
130: _name = name;
131: }
132:
133: /**
134: * Set a flag that declares whether this page intput is required or optiona. If a Page Input is required,
135: * the tag will report an error if the page input key does not appear in the set of page inputs for a page.
136: *
137: * @param required whether to require the page input for the page
138: * @jsptagref.attributedescription
139: * Set a flag that declares whether this page intput is required or optiona. If a Page Input is required,
140: * the tag will report an error if the page input key does not appear in the set of page inputs for a page.
141: * @jsptagref.attributesyntaxvalue <i>string_name</i>
142: * @netui:attribute required="false"
143: */
144: public void setRequired(boolean required) {
145: _required = required;
146: }
147:
148: /**
149: * Set the String classname variable that represents the expected type of the page input.
150: *
151: * @param type the type of the variable that is referenced
152: * @jsptagref.attributedescription
153: * Set the String classname variable that represents the expected type of the page input.
154: * @jsptagref.attributesyntaxvalue <i>string_type</i>
155: * @netui:attribute required="true"
156: */
157: public void setType(String type) {
158: _type = type;
159: }
160:
161: /**
162: *
163: */
164: public void doTag() throws JspException {
165:
166: LOGGER.debug("Added a page input named \"" + _name
167: + "\" of type \"" + _type + "\"");
168:
169: if (_name.equals(EMPTY_STRING)) {
170: String msg = Bundle
171: .getErrorString("Tags_DeclarePageInput_EmptyName");
172: registerTagError(msg, null);
173: }
174:
175: if (_type.equals(EMPTY_STRING)) {
176: String msg = Bundle
177: .getErrorString("Tags_DeclarePageInput_EmptyType");
178: registerTagError(msg, null);
179: }
180:
181: if (_required) {
182: assert getJspContext() instanceof PageContext;
183: assert ((PageContext) getJspContext()).getRequest() instanceof HttpServletRequest;
184:
185: HttpServletRequest request = (HttpServletRequest) ((PageContext) getJspContext())
186: .getRequest();
187: Map actionOutputMap = InternalUtils
188: .getPageInputMap(request);
189: if (actionOutputMap == null
190: || !actionOutputMap.containsKey(_name)) {
191: String msg = Bundle.getErrorString(
192: "Tags_DeclarePageInput_Required",
193: new Object[] { _name });
194: registerTagError(msg, null);
195: }
196: }
197:
198: if (hasErrors())
199: reportErrors();
200: }
201: }
|