KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > ext > taglib > AttributeTag


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.ext.taglib;
66
67 import com.jcorporate.expresso.core.controller.ControllerElement;
68
69 import javax.servlet.jsp.JspTagException JavaDoc;
70 import javax.servlet.jsp.JspWriter JavaDoc;
71
72
73 /**
74  * The AttributeTag can be used to display attributes from Input
75  * or Output objects. The AttributeTag can either be used
76  * stand-alone, in which case all the arguments are required,
77  * or nested within and OutputTag or InputTag, in which case only
78  * the attribute name must be specified.
79  * <p/>
80  * Note: Arguments specified in the tag override the arguments of
81  * any parent class.
82  * </p><p>
83  * Usage example when tag is not nested:
84  * </p><p>
85  * <expresso:AttributeTag controllerElement="product" name="product_desc" type="OUTPUT" /><br/>
86  * <br/>
87  * Usage example when nested within an output:<br/>
88  * <expresso:OutputTag name="product"><br/>
89  * Content Area: <expresso:ContentTag /></br>
90  * Attribute: <expresso:AttributeTag name="product_desc"/><br/>
91  * Nested Output: <expresso:OutputTag name="nestoutput"><br/>
92  * <expresso:ContentTag /><br/>
93  * </expresso:OutputTag><br/>
94  * </expresso:OutputTag><br/>
95  * </p>
96  *
97  * @author Adam Rossi, PlatinumSolutions
98  */

99 public class AttributeTag
100         extends ExpressoTagSupport {
101     private java.lang.String JavaDoc controllerElement = null;
102     private java.lang.String JavaDoc name = null;
103     private java.lang.String JavaDoc type = null;
104     private java.lang.String JavaDoc controllerElementToUse = null;
105     private java.lang.String JavaDoc nameToUse = null;
106     private java.lang.String JavaDoc typeToUse = null;
107     private ControllerElement oneElement = null;
108     private String JavaDoc attributeContent = null;
109
110     public AttributeTag() {
111         super();
112     }
113
114     public int doStartTag() throws javax.servlet.jsp.JspException JavaDoc {
115         controllerElementToUse = null;
116         nameToUse = null;
117         typeToUse = null;
118         oneElement = null;
119         attributeContent = null;
120         return SKIP_BODY;
121     }
122
123
124     /**
125      * Standard doEndTag.
126      *
127      * @return int
128      */

129     public int doEndTag()
130             throws javax.servlet.jsp.JspTagException JavaDoc {
131         nameToUse = name;
132         typeToUse = type;
133         controllerElementToUse = controllerElement;
134         getControllerResponse();
135
136         try {
137
138             //If the controllerElement and type have been specified, this takes precedence over parent.
139
if (type != null && !"".equals(type) &&
140                     controllerElementToUse != null &&
141                     !"".equals(controllerElementToUse)) {
142                 if ("INPUT".equals(typeToUse)) {
143                     oneElement = ctlrResp.getInput(controllerElementToUse);
144                 } else {
145                     oneElement = ctlrResp.getOutput(controllerElementToUse);
146                 }
147             }
148             if (oneElement == null) {
149
150                 //we did not have attributes specified in the tag.
151
//Check to see if we have an ancestor
152
InputTag ancestorInput = (InputTag) getAncestor(
153                         com.jcorporate.expresso.ext.taglib.InputTag.class.getName());
154                 OutputTag ancestorOutput = (OutputTag) getAncestor(
155                         com.jcorporate.expresso.ext.taglib.OutputTag.class.getName());
156                 boolean ancestorFoundFlag = false;
157
158                 if (ancestorInput != null) {
159                     ancestorFoundFlag = true;
160                     typeToUse = "INPUT";
161                 } else if (ancestorOutput != null) {
162                     ancestorFoundFlag = true;
163                     typeToUse = "OUTPUT";
164                 }
165                 if (ancestorFoundFlag) {
166                     if ("INPUT".equals(typeToUse)) {
167
168                         // controllerElementToUse = ancestorInput.getName();
169
oneElement = ancestorInput.getInput();
170                     } else {
171
172                         //controllerElementToUse = ancestorOutput.getName();
173
oneElement = ancestorOutput.getOutput();
174                     }
175                 }
176             }
177             if (oneElement != null) {
178                 attributeContent = oneElement.getAttribute(nameToUse);
179             } else {
180                 attributeContent = "ATTRIBUTE NOT FOUND";
181             }
182
183             JspWriter JavaDoc writer = pageContext.getOut();
184             writer.print(attributeContent);
185         } catch (Exception JavaDoc e) {
186             throw new JspTagException JavaDoc("AttributeTag Error: " + e.getMessage());
187         }
188
189         return EVAL_PAGE;
190     } /* doEndTag() */
191
192
193     /**
194      * GET / SET controllerElement
195      * Retrieve the controller element name for the attribute tag
196      *
197      * @return java.lang.String
198      */

199     public java.lang.String JavaDoc getControllerElement() {
200         return controllerElementToUse;
201     }
202
203     public java.lang.String JavaDoc getName() {
204         return nameToUse;
205     }
206
207     public java.lang.String JavaDoc getType() {
208         return typeToUse;
209     }
210
211     public void setControllerElement(java.lang.String JavaDoc newControllerElement) {
212         controllerElement = newControllerElement;
213     }
214
215     /**
216      * GET / SET Name
217      *
218      * @param newName the new name of the attribute
219      */

220     public void setName(java.lang.String JavaDoc newName) {
221         name = newName;
222     }
223
224     /**
225      * GET / SET type
226      *
227      * @param newType the new type of the attribute
228      */

229     public void setType(java.lang.String JavaDoc newType) {
230         type = newType;
231     }
232
233 }
Popular Tags