KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > controller > Block


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.core.controller;
66
67 import com.jcorporate.expresso.core.misc.StringUtil;
68 import com.jcorporate.expresso.kernel.util.FastStringBuffer;
69 import org.w3c.dom.NamedNodeMap JavaDoc;
70 import org.w3c.dom.Node JavaDoc;
71 import org.w3c.dom.NodeList JavaDoc;
72
73 import java.io.Serializable JavaDoc;
74 import java.util.Enumeration JavaDoc;
75 import java.util.HashMap JavaDoc;
76 import java.util.Map JavaDoc;
77 import java.util.Vector JavaDoc;
78
79 /**
80  * A Block is a logical grouping of Expresso Controller Elements used for nesting.
81  * <p/>
82  * <h4>Recognized Types:</h4>
83  * <p>The following types are recognized by the expresso framework and
84  * automatically rendered: You may add your own types or ignore them
85  * if you are doing your own page rendering.</p>
86  * <p/>
87  * <p>header-row: Renders each subobject in it's own cell with the appropriate
88  * jc-header class Make the cells pipe-delimited strings </p>
89  * <p/>
90  * <p>row: Renders all subobjects in one row</p>
91  * <p>borderWidth: Renders the table width a paricular border.</p>
92  * <p><i>default behavior:</i> Render as a table/nested table.</p>
93  *
94  * @author Shash Chatterjee
95  */

96 public class Block
97         extends ControllerElement
98         implements Cloneable JavaDoc,
99         Serializable JavaDoc {
100     private static final String JavaDoc thisClass = Block.class.getName() + ".";
101     private Transition formTransition = null;
102
103     private Map JavaDoc mNestedBlocks = null;
104     private Map JavaDoc mNestedTransitions = null;
105     private Map JavaDoc mNestedInputs = null;
106     private Map JavaDoc mNestedOutputs = null;
107
108     /**
109      * Instantiation Constructor. Normally not called
110      */

111     public Block() {
112     }
113
114     /**
115      * Convenience constructor - to make a new Block object
116      * with a specific name.
117      *
118      * @param newName The new name of the block
119      */

120     public Block(String JavaDoc newName) {
121         setName(newName);
122     } /* Block(String) */
123
124     /**
125      * Add an element to this Block
126      *
127      * @param element an element to be added
128      */

129     public void add(ControllerElement element) {
130         addNested(element);
131         // iterate through the 4 possibilites in order to
132
// fill the right map
133
if (element instanceof Output) {
134             if (mNestedOutputs == null) {
135                 mNestedOutputs = new HashMap JavaDoc();
136             }
137             mNestedOutputs.put(element.getName(), element);
138         } else if (element instanceof Input) {
139             if (mNestedInputs == null) {
140                 mNestedInputs = new HashMap JavaDoc();
141             }
142             mNestedInputs.put(element.getName(), element);
143         } else if (element instanceof Transition) {
144             if (mNestedTransitions == null) {
145                 mNestedTransitions = new HashMap JavaDoc();
146             }
147             mNestedTransitions.put(element.getName(), element);
148         } else if (element instanceof Block) {
149             if (mNestedBlocks == null) {
150                 mNestedBlocks = new HashMap JavaDoc();
151             }
152             mNestedBlocks.put(element.getName(), element);
153         }
154     }
155
156
157     /**
158      * Retrieve the transitions as a map so that JSTL can cope with navigating the
159      * ControllerResponse through a named fashion
160      *
161      * @return java.util.Map map of transitions which have been added
162      */

163     public Map JavaDoc getNamedTransitions() {
164         return mNestedTransitions;
165     }
166
167     /**
168      * Retrieve the outputs as a map so that JSTL can cope with navigating the
169      * ControllerResponse through a named fashion
170      *
171      * @return java.util.Map map of outputs which have been added
172      */

173     public Map JavaDoc getNamedOutputs() {
174         return mNestedOutputs;
175     }
176
177     /**
178      * Retrieve the inputs as a map so that JSTL can cope with navigating the
179      * ControllerResponse through a named fashion
180      *
181      * @return java.util.Map map of inputs which have been added
182      */

183     public Map JavaDoc getNamedInputs() {
184         return mNestedInputs;
185     }
186
187     /**
188      * Retrieve the Blocks as a map so that JSTL can cope with navigating the
189      * ControllerResponse through a named fashion
190      *
191      * @return java.util.Map map of blocks which have been added
192      */

193     public Map JavaDoc getNamedBlocks() {
194         return mNestedBlocks;
195     }
196
197     /**
198      * Remove an element from the nested elements for this item
199      *
200      * @param elementToRemove The element to be removed from the list of
201      * nested items.
202      * @throws ControllerException if there is no such item nested in this item
203      */

204     public void removeNested(ControllerElement elementToRemove)
205             throws ControllerException {
206         super.removeNested(elementToRemove);
207
208         if (elementToRemove instanceof Input) {
209             mNestedInputs.remove(elementToRemove.getName());
210         } else if (elementToRemove instanceof Output) {
211             mNestedOutputs.remove(elementToRemove.getName());
212         } else if (elementToRemove instanceof Transition) {
213             mNestedTransitions.remove(elementToRemove.getName());
214         } else if (elementToRemove instanceof Block) {
215             mNestedBlocks.remove(elementToRemove.getName());
216         }
217     } /* removeNested(ControllerElement) */
218
219     /**
220      * Generate a copy of this current Block
221      *
222      * @return an instantiated Block onbject
223      */

224     public Object JavaDoc clone()
225             throws CloneNotSupportedException JavaDoc {
226         Block b;
227
228         synchronized (this) {
229             b = (Block) super.clone();
230             b.formTransition = this.formTransition;
231             b.mNestedBlocks = this.mNestedBlocks;
232             b.mNestedTransitions = this.mNestedTransitions;
233             b.mNestedInputs = this.mNestedInputs;
234             b.mNestedOutputs = this.mNestedOutputs;
235         }
236
237         return b;
238     }
239
240     /**
241      * Return a controller element based upon the xml fragment
242      *
243      * @param n a DOM Node that we extract the internal elements
244      * @return an instantiated ControllerElement [specifically a block]
245      * @throws ControllerException if unable to build itself from the DOM node
246      */

247     public static ControllerElement fromXML(Node JavaDoc n)
248             throws ControllerException {
249
250         //If we're at the root node, then it'll be doc instead of input.
251
if (n.getNodeName().equals("#document")) {
252             return fromXML(n.getChildNodes().item(0));
253         }
254         if (!n.getNodeName().equals("block")) {
255             return null;
256         }
257
258         Block b = new Block();
259
260         //Get node attributes
261
NamedNodeMap JavaDoc transitionAttributes = n.getAttributes();
262         Node JavaDoc attributeNode = transitionAttributes.getNamedItem("name");
263
264         if (attributeNode != null) {
265             String JavaDoc value = attributeNode.getNodeValue();
266
267             if (value != null) {
268                 b.setName(value);
269             }
270         }
271
272         NodeList JavaDoc nl = n.getChildNodes();
273
274         for (int i = 0; i < nl.getLength(); i++) {
275             Node JavaDoc oneChild = nl.item(i);
276             String JavaDoc nodeName = oneChild.getNodeName();
277
278             if (nodeName.equals("controller-element")) {
279                 b = (Block) ControllerElement.fromXML(oneChild, b);
280                 break;
281             }
282         }
283
284         return b;
285     }
286
287     /**
288      * Retrieve a list of blocks contained within this Block
289      *
290      * @return java.util.Vector
291      */

292     public Vector JavaDoc getBlocks() {
293         Vector JavaDoc elements = new Vector JavaDoc(16);
294         ControllerElement t = null;
295
296         for (Enumeration JavaDoc e = getNested().elements(); e.hasMoreElements();) {
297             t = (ControllerElement) e.nextElement();
298
299             if (t instanceof Block) {
300                 elements.add(t);
301             }
302         }
303
304         return elements;
305     }
306
307     /**
308      * Retrieve a newsted Block by name. Note that for this to work correctly,
309      * all added items in the block MUST have unique names. otherwise, you may
310      * get ClassCastExceptions
311      *
312      * @param blockName the name of the block to retrieve
313      * @return Block or null if the subBlock doesn't exit
314      */

315     public Block getBlock(String JavaDoc blockName) {
316         return (Block) this.getNestedMap().get(blockName);
317     }
318
319     /**
320      * Retrieve a nested input by name. Note that for this to work correctly,
321      * all added items in the block MUST have unique names. otherwise, you may
322      * get ClassCastExceptions
323      *
324      * @param inputName the name of the block to retrieve
325      * @return Input or null if the sub input doesn't exit
326      */

327     public Input getInput(String JavaDoc inputName) {
328         return (Input) this.getNestedMap().get(inputName);
329     }
330
331     /**
332      * Retrieve a nested output by name. Note that for this to work correctly,
333      * all added items in the block MUST have unique names. otherwise, you may
334      * get ClassCastExceptions
335      *
336      * @param outputName the name of the block to retrieve
337      * @return Output or null if the sub output doesn't exit
338      */

339     public Output getOutput(String JavaDoc outputName) {
340         return (Output) this.getNestedMap().get(outputName);
341     }
342
343     /**
344      * Retrieve a nested transition by name. Note that for this to work correctly,
345      * all added items in the block MUST have unique names. otherwise, you may
346      * get ClassCastExceptions
347      *
348      * @param transitionName the name of the block to retrieve
349      * @return Transition or null if the sub Transition doesn't exit
350      */

351     public Transition getTransition(String JavaDoc transitionName) {
352         return (Transition) this.getNestedMap().get(transitionName);
353     }
354
355     /**
356      * Retrieve the form encoding attribute of this block
357      *
358      * @return java.lang.String or null
359      */

360     public String JavaDoc getFormEncoding() {
361         return getAttribute("formEncoding");
362     } /* getFormEncoding() */
363
364     /**
365      * Get the form method whether GET or POST
366      *
367      * @return java.lang.String either GET or POST
368      */

369     public String JavaDoc getFormMethod() {
370         String JavaDoc formMethod = StringUtil.notNull(getAttribute("formMethod"));
371
372         if (!formMethod.equalsIgnoreCase("get")) {
373             formMethod = "post";
374         }
375
376         return formMethod;
377     } /* getFormMethod() */
378
379     /**
380      * Get the Form Transition
381      *
382      * @return com.jcorporate.expresso.core.controller.Transition
383      */

384     public Transition getFormTransition() {
385         return formTransition;
386     } /* getFormTransition() */
387
388
389     /**
390      * Get the nested Inputs
391      *
392      * @return java.util.Vector of Inputs
393      */

394     public Vector JavaDoc getInputs() {
395         Vector JavaDoc elements = new Vector JavaDoc(16);
396         ControllerElement t = null;
397
398         for (Enumeration JavaDoc e = getNested().elements(); e.hasMoreElements();) {
399             t = (ControllerElement) e.nextElement();
400
401             if (t instanceof Input) {
402                 elements.add(t);
403             }
404         }
405
406         return elements;
407     }
408
409     /**
410      * Get the number of elements contained within the nested block
411      *
412      * @return integer describing the size of the nested contents
413      */

414     public int getNumContents() {
415         return getNested().size();
416     } /* getNumContents() */
417
418
419     /**
420      * Retrieve a list of Outputs nested wtihin this block
421      *
422      * @return java.util.Vector of nested Outputs
423      */

424     public Vector JavaDoc getOutputs() {
425         Vector JavaDoc elements = new Vector JavaDoc(16);
426         ControllerElement t = null;
427
428         for (Enumeration JavaDoc e = getNested().elements(); e.hasMoreElements();) {
429             t = (ControllerElement) e.nextElement();
430
431             if (t instanceof Output) {
432                 elements.add(t);
433             }
434         }
435
436         return elements;
437     }
438
439     /**
440      * Retrieve a list of Transitions nested wtihin this block
441      *
442      * @return java.util.Vector of nested Transitions
443      */

444     public Vector JavaDoc getTransitions() {
445         Vector JavaDoc elements = new Vector JavaDoc(16);
446         ControllerElement t = null;
447
448         for (Enumeration JavaDoc e = getNested().elements(); e.hasMoreElements();) {
449             t = (ControllerElement) e.nextElement();
450
451             if (t instanceof Transition) {
452                 elements.add(t);
453             }
454         }
455
456         return elements;
457     }
458
459     /**
460      * Return true if the form attribute for this block has been set
461      *
462      * @return boolean true if the attribute form has been set to true for this block
463      */

464     public boolean isForm() {
465         if (StringUtil.notNull(getAttribute("form")).equals("true")) {
466             return true;
467         } else {
468             return false;
469         }
470     } /* isForm() */
471
472     /**
473      * Set whether this form is true or not.
474      *
475      * @param isForm either 'true' or 'false' (lower case)
476      * @throws ControllerException if the methods are invalid
477      */

478     public void setForm(String JavaDoc isForm)
479             throws ControllerException {
480         String JavaDoc thisMethod = thisClass + "setForm";
481
482         if (!(isForm.equals("true") || isForm.equals("false"))) {
483             throw new ControllerException(thisMethod +
484                     ":parameter passed in must be \"true\" or \"false\"");
485         }
486
487         setAttribute("form", isForm);
488     } /* setForm(String) */
489
490
491     /**
492      * Set the encoding attribute for the block form
493      *
494      * @param encoding java.lang.String
495      * @throws ControllerException if the attribute is unable to be thrown
496      */

497     public void setFormEncoding(String JavaDoc encoding)
498             throws ControllerException {
499         setAttribute("formEncoding", encoding);
500     } /* setFormEncoding(String) */
501
502
503     /**
504      * Set the form method.
505      *
506      * @param method The string get or post (lower case)
507      * @throws ControllerException upon invalid argument
508      */

509     public void setFormMethod(String JavaDoc method)
510             throws ControllerException {
511         String JavaDoc thisMethod = thisClass + "setFormMethod";
512
513         if (!(method.equals("get") || method.equals("post"))) {
514             throw new ControllerException(thisMethod +
515                     ": parameter passed in must be \"get\" or \"post\"");
516         }
517
518         setAttribute("formMethod", method);
519     } /* setFormMethod(String) */
520
521
522     /**
523      * Sets the form transition
524      *
525      * @param a a Transition that is the equiv of the 'submit' button on the
526      * form.
527      * @throws ControllerException if the transition is null
528      */

529     public void setFormTransition(Transition a)
530             throws ControllerException {
531         String JavaDoc thisMethod = thisClass + "setFormTransition";
532
533         if (a == null) {
534             throw new ControllerException(thisMethod +
535                     ":Transition cannot be null");
536         }
537
538         formTransition = a;
539     } /* setFormTransition(Transition) */
540
541
542     /**
543      * Concert the object to an xml fragment.
544      *
545      * @param stream an instantiated FastStringBuffer to which we are supposed
546      * to append to.
547      * @return a fleshed out FastStringBuffer (usually the stream parameter)
548      */

549     public FastStringBuffer toXML(FastStringBuffer stream) {
550         stream.append("<block name=\"" + StringUtil.xmlEscape(this.getName()) +
551                 "\">\n");
552         stream = super.toXML(stream);
553
554         // stream = toNestedXML(stream,"nested-outputs", getOutputs());
555
// stream = toNestedXML(stream,"nested-inputs", getInputs());
556
// stream = toNestedXML(stream,"nested-transitions", getTransitions());
557
// stream = toNestedXML(stream,"nested-blocks", getBlocks());
558
stream.append("</block>");
559
560         return stream;
561     }
562
563     protected FastStringBuffer toNestedXML(FastStringBuffer stream,
564                                            String JavaDoc blockLabel, Vector JavaDoc elements) {
565         stream.append("<");
566         stream.append(blockLabel);
567         stream.append(">");
568
569         for (Enumeration JavaDoc eb = elements.elements(); eb.hasMoreElements();) {
570             ControllerElement oneControllerElement = (ControllerElement) eb.nextElement();
571             stream = oneControllerElement.toXML(stream);
572         } /* each element in this block */
573
574
575         stream.append("</");
576         stream.append(blockLabel);
577         stream.append(">");
578
579         return stream;
580     }
581 }
582
583 /* Block */
584
Popular Tags