KickJava   Java API By Example, From Geeks To Geeks.

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


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  * * 6. No product derived from this software may compete in the same
35  * market space, i.e. framework, without prior written permission
36  * of Jcorporate Ltd. For written permission, please contact
37  * partners@jcorporate.com.
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
43  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
45  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This software consists of voluntary contributions made by many
54  * individuals on behalf of the Jcorporate Ltd. Contributions back
55  * to the project(s) are encouraged when you make modifications.
56  * Please send them to support@jcorporate.com. For more information
57  * on Jcorporate Ltd. and its products, please see
58  * <http://www.jcorporate.com/>.
59  *
60  * Portions of this software are based upon other open source
61  * products and are subject to their respective licenses.
62  */

63
64 package com.jcorporate.expresso.ext.taglib;
65
66 /**
67  * ElementCollection.java
68  *
69  * Copyright 1999, 2000, 2001 Jcorporate Ltd.
70  */

71
72 import com.jcorporate.expresso.core.controller.Block;
73 import com.jcorporate.expresso.core.controller.ControllerElement;
74 import org.apache.log4j.Logger;
75
76 import javax.servlet.jsp.JspTagException JavaDoc;
77 import javax.servlet.jsp.tagext.Tag JavaDoc;
78 import java.util.Enumeration JavaDoc;
79 import java.util.Vector JavaDoc;
80
81
82 /**
83  * A custom tag that iterates through nested controller elements.
84  * This tag provides an Enumeration object to an ElementIterator tag,
85  * providing access to the desired ControllerElements. The
86  * ElementIterator may iterate through all the elements found in the
87  * ElementCollection.
88  * <p/>
89  * <p/>
90  * tag atteribute type should be set
91  * <p/>
92  * <ul>
93  * <li>"input" - iterates through all <code>Input</code> controller elements.</li>
94  * <li>"output" - iterates through all <code>Output</code> controller elements.</li>
95  * <li>"transition" - iterates through all <code>Transition</code> controller elements.</li>
96  * <li>"block" - iterates through all <code>Block</code> controller elements.</li>
97  * <li>"nested" - iterates through <em>all</em> controller elements generically.</li>
98  * </ul>
99  * </p>
100  * <p/>
101  * <p> Documentation Peter Pilgrim Wed Dec 04 14:45:44 GMT 2002
102  * </p>
103  *
104  * @author Shash Chatterjee
105  * @see com.jcorporate.expresso.core.controller.ControllerElement
106  * @see com.jcorporate.expresso.core.controller.Input
107  * @see com.jcorporate.expresso.core.controller.Output
108  * @see com.jcorporate.expresso.core.controller.Transition
109  * @see com.jcorporate.expresso.core.controller.Block
110  */

111 public class ElementCollection
112         extends ExpressoTagSupport {
113     protected Enumeration JavaDoc elements = null;
114     private java.lang.String JavaDoc type = null;
115     private static Logger log = Logger.getLogger(ElementCollection.class);
116
117     public ElementCollection() {
118         super();
119     }
120
121     public int doEndTag()
122             throws javax.servlet.jsp.JspTagException JavaDoc {
123         return EVAL_PAGE;
124     }
125
126     public int doStartTag()
127             throws javax.servlet.jsp.JspTagException JavaDoc {
128         getControllerResponse();
129
130         Vector JavaDoc holding = null;
131
132         try {
133             BlockTag ancestorBlock = (BlockTag) getAncestor("com.jcorporate.expresso.ext.taglib.BlockTag");
134 /* turgay */
135             Tag JavaDoc cont = getContainer();
136             ControllerElement container = null;
137             if (cont instanceof ElementIterator) {
138                 container = ((ElementIterator) cont).getElement();
139             }
140
141             if (container != null) {
142                 if (getType().equalsIgnoreCase("block")) {
143                     holding = ((Block) container).getBlocks();
144                     if (holding != null) {
145                         elements = holding.elements();
146                     }
147                 } else if (getType().equalsIgnoreCase("input")) {
148                     holding = ((Block) container).getInputs();
149                     if (holding != null) {
150                         elements = holding.elements();
151                     }
152                 } else if (getType().equalsIgnoreCase("output")) {
153                     holding = ((Block) container).getOutputs();
154                     if (holding != null) {
155                         elements = holding.elements();
156                     }
157                 } else if (getType().equalsIgnoreCase("transition")) {
158                     holding = ((Block) container).getTransitions();
159                     if (holding != null) {
160                         elements = holding.elements();
161                     }
162 /*turgay*/
163                 } else if (getType().equalsIgnoreCase("nested")) {
164                     // *PP* Allow ElementCollectionTag to be generic controller elements
165
// Wed Dec 04 14:38:31 GMT 2002
166
holding = ((Block) container).getNested();
167                     if (holding != null) {
168                         elements = holding.elements();
169                     }
170                 }
171             } else if (ancestorBlock != null) {
172                 Block block = ancestorBlock.getBlock();
173
174                 if (getType().equalsIgnoreCase("block")) {
175                     holding = block.getBlocks();
176
177                     if (holding != null) {
178                         elements = holding.elements();
179                     }
180                 } else if (getType().equalsIgnoreCase("input")) {
181                     holding = block.getInputs();
182
183                     if (holding != null) {
184                         elements = holding.elements();
185                     }
186                 } else if (getType().equalsIgnoreCase("output")) {
187                     holding = block.getOutputs();
188
189                     if (holding != null) {
190                         elements = holding.elements();
191                     }
192                 } else if (getType().equalsIgnoreCase("transition")) {
193                     holding = block.getTransitions();
194
195                     if (holding != null) {
196                         elements = holding.elements();
197                     }
198                 } else if (getType().equalsIgnoreCase("nested")) {
199                     // *PP* Allow ElementCollectionTag to be generic controller elements
200
// Wed Dec 04 14:38:31 GMT 2002
201
holding = block.getNested();
202                     if (holding != null) {
203                         elements = holding.elements();
204                     }
205                 }
206             } else {
207                 if (getType().equalsIgnoreCase("block")) {
208                     holding = ctlrResp.getBlocks();
209
210                     if (holding != null) {
211                         elements = holding.elements();
212                     }
213                 } else if (getType().equalsIgnoreCase("input")) {
214                     holding = ctlrResp.getInputs();
215
216                     if (holding != null) {
217                         elements = holding.elements();
218                     }
219                 } else if (getType().equalsIgnoreCase("output")) {
220                     holding = ctlrResp.getOutputs();
221
222                     if (holding != null) {
223                         elements = holding.elements();
224                     }
225                 } else if (getType().equalsIgnoreCase("transition")) {
226                     holding = ctlrResp.getTransitions();
227
228                     if (holding != null) {
229                         elements = holding.elements();
230                     }
231                 } else if (getType().equalsIgnoreCase("nested")) {
232                     // *PP* Allow ElementCollectionTag to be generic controller elements
233
// Wed Dec 04 14:38:31 GMT 2002
234
//
235
// The controller response does not have a "getNested()" call so
236
// I simulate it here, to get every single type of controller element
237
holding = new Vector JavaDoc();
238                     Vector JavaDoc tmp = ctlrResp.getInputs();
239                     if (tmp == null) {
240                         holding.addAll(tmp);
241                     }
242                     tmp = ctlrResp.getOutputs();
243                     if (tmp == null) {
244                         holding.addAll(tmp);
245                     }
246                     tmp = ctlrResp.getTransitions();
247                     if (tmp == null) {
248                         holding.addAll(tmp);
249                     }
250                     tmp = ctlrResp.getBlocks();
251                     if (tmp == null) {
252                         holding.addAll(tmp);
253                     }
254                     elements = holding.elements();
255                 }
256             }
257         } catch (Exception JavaDoc e) {
258             log.error("Error from ElementCollection Tag:", e);
259             throw new JspTagException JavaDoc("ElementCollection Tag Error: " +
260                     e.getMessage());
261         }
262         if (elements == null || !elements.hasMoreElements()) {
263             return SKIP_BODY;
264         } else {
265             return EVAL_BODY_INCLUDE;
266         }
267     }
268
269     protected Enumeration JavaDoc getElements() {
270         return elements;
271     }
272
273     public java.lang.String JavaDoc getType() {
274         return type;
275     }
276
277     public void setType(java.lang.String JavaDoc newType) {
278         type = newType;
279     }
280
281 } /* ElementCollection */
282
283 /* ElementCollection */
284
Popular Tags