KickJava   Java API By Example, From Geeks To Geeks.

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


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.cache.Cacheable;
68 import com.jcorporate.expresso.core.misc.StringUtil;
69 import com.jcorporate.expresso.kernel.util.FastStringBuffer;
70 import org.w3c.dom.NamedNodeMap JavaDoc;
71 import org.w3c.dom.Node JavaDoc;
72 import org.w3c.dom.NodeList JavaDoc;
73
74 import java.util.Enumeration JavaDoc;
75
76
77 /**
78  * An item returned from a controller to provide output/feedback to the
79  * client.<br>
80  * An Output object is one of the three types of object that can be returned
81  * by a Controller moving to a new state. The other two are Input and
82  * Action items.<br>
83  * An Output item can be a single item, or a list of items, using the "nested"
84  * methods in the ControllerElement class.
85  */

86 public class Output
87         extends ControllerElement
88         implements Cloneable JavaDoc,
89         Cacheable,
90         java.io.Serializable JavaDoc {
91
92     /**
93      * The string contents of this Output
94      */

95     private String JavaDoc content;
96
97     /**
98      * Suggested "style" of this Output item
99      */

100     private String JavaDoc style;
101
102     /**
103      * Suggested alignment of this Output item
104      */

105     private String JavaDoc alignment;
106     private long usedCount = 0;
107
108     /**
109      * Key used when this output is used as a Cacheable object
110      */

111     private String JavaDoc key = null;
112
113     /**
114      * Default constructor
115      */

116     public Output() {
117         super();
118     } /* Output */
119
120     /**
121      * Convenience constructor for creating an output with a specific
122      * string as it's contents in one step
123      *
124      * @param c The string contents of this Output item
125      */

126     public Output(String JavaDoc c) {
127         super();
128         setContent(c);
129     } /* Output(String) */
130
131     /**
132      * Convenience constructor for creating an output with a specific
133      * string as it's contents and a name in one step
134      *
135      * @param name The name of this new Output item
136      * @param c The string contents of the Output item
137      */

138     public Output(String JavaDoc name, String JavaDoc c) {
139         setName(name);
140         setContent(c);
141     } /* Output(String, String) */
142
143     /**
144      * Add a new nested output item to this item
145      *
146      * @param o The new Output item to be nested into this one
147      */

148     public void addNested(Output o) {
149         super.addNested(o);
150     } /* addNested(Output) */
151
152     /**
153      * Clears the used count
154      */

155     public void clearUsedCount() {
156         usedCount = 0;
157     } /* clearUsedCount() */
158
159     /**
160      * Deep copy clone the object
161      *
162      * @return Copy of this current output
163      */

164     public Object JavaDoc clone()
165             throws CloneNotSupportedException JavaDoc {
166         Output newOutput;
167
168         synchronized (this) {
169             newOutput = (Output) super.clone();
170             newOutput.alignment = alignment;
171             newOutput.content = content;
172             newOutput.key = key;
173             newOutput.usedCount = usedCount;
174         }
175
176         return newOutput;
177     } /* clone() */
178
179
180     /**
181      * Get the suggested alignment for this item
182      *
183      * @return The suggested alignment for this Output item
184      */

185     public String JavaDoc getAlignment() {
186         return alignment;
187     } /* getAlignment() */
188
189     /**
190      * Get the "contents" of this Output, the string value that this
191      * Output object represents
192      *
193      * @return A String value for the contents of this Output
194      */

195     public String JavaDoc getContent() {
196         return StringUtil.notNull(content);
197     } /* getContent() */
198
199     /**
200      * Get the name of this output element
201      *
202      * @return the key of the output Element
203      */

204     public String JavaDoc getKey() {
205         return key;
206     } /* getKey() */
207
208     /**
209      * /**
210      * Return the suggested "style" for displaying this Output item
211      *
212      * @return A "style" string as suggested by the Controller
213      */

214     public String JavaDoc getStyle() {
215         return style;
216     } /* getStyle() */
217
218     /**
219      * @return the number of times this output has been used
220      */

221     public long getUsedCount() {
222         return usedCount;
223     } /* getUsedCount() */
224
225     /**
226      * Does this output have a nested item with the given name?
227      *
228      * @param nestedName The name to check against
229      * @return true if there is such an element, else false
230      * @throws ControllerException upon error
231      */

232     public boolean hasNested(String JavaDoc nestedName)
233             throws ControllerException {
234         Output oneElement = null;
235
236         for (Enumeration JavaDoc e = getNested().elements(); e.hasMoreElements();) {
237             oneElement = (Output) e.nextElement();
238
239             if (oneElement.getName().equals(nestedName)) {
240                 return true;
241             }
242         } /* for each element nested */
243
244
245         return false;
246     } /* hasNested(String) */
247
248
249     /**
250      *
251      */

252     public synchronized void incrementUsedCount() {
253         usedCount++;
254     } /* incrementUsedCount() */
255
256     /**
257      * Method used by the Controller to specify a suggested alignment
258      * for this Output item
259      *
260      * @param newAlignment A String suggesting an alignment to the client
261      * when displaying this Output item
262      */

263     public synchronized void setAlignment(String JavaDoc newAlignment) {
264         alignment = newAlignment;
265     } /* setAlignment(String) */
266
267     /**
268      * Used by the Controller to specify the contents string of this Output
269      *
270      * @param newContent The new contents string
271      */

272     public synchronized void setContent(String JavaDoc newContent) {
273         content = newContent;
274     } /* setContent(String) */
275
276     /**
277      * @param newKey The new Key for the output
278      */

279     public synchronized void setKey(String JavaDoc newKey) {
280         key = newKey;
281     } /* setKey(String) */
282
283     /**
284      * Used by the Controller to set a suggested "style" for displaying this
285      * Output item
286      *
287      * @param newStyle The new "style" to set for this output object
288      */

289     public synchronized void setStyle(String JavaDoc newStyle) {
290         style = newStyle;
291     } /* setStyle(String) */
292
293     /**
294      * Concert the object to an xml fragment.
295      *
296      * @param stream The fastStringBuffer to append to
297      * @return a FastStingBuffer object that represents this output in an
298      * xml fragment
299      */

300     public FastStringBuffer toXML(FastStringBuffer stream) {
301         stream.append("<output");
302
303         if (this.getName() != null && this.getName().length() > 0) {
304             stream.append(" name=\"");
305             stream.append(StringUtil.xmlEscape(this.getName()));
306             stream.append("\"");
307         }
308         if (this.alignment != null && this.alignment.length() > 0) {
309             stream.append(" alignment=\"");
310             stream.append(StringUtil.xmlEscape(this.alignment));
311             stream.append("\"");
312         }
313         if (this.key != null && this.key.length() > 0) {
314             stream.append(" key=\"");
315             stream.append(StringUtil.xmlEscape(this.key));
316             stream.append("\"");
317         }
318         if (this.usedCount > 0) {
319             stream.append(" usedCount =\"");
320             stream.append(Long.toString(this.usedCount));
321             stream.append("\"");
322         }
323
324         stream.append(">\n");
325         stream.append("<content>");
326         stream.append(StringUtil.xmlEscape(getContent()));
327         stream.append("</content>\n");
328         stream = super.toXML(stream);
329         stream.append("</output>\n");
330
331         return stream;
332     }
333
334     /**
335      * Return a controller element based upon the xml fragment
336      *
337      * @param n a DOM node
338      * @return a built ControllerElement
339      * @throws ControllerException if the nodes do not match what we expect
340      */

341     public static ControllerElement fromXML(Node JavaDoc n)
342             throws ControllerException {
343
344         //If we're at the root node, then it'll be doc instead of input.
345
if (n.getNodeName().equals("#document")) {
346             return fromXML(n.getChildNodes().item(0));
347         }
348         if (!n.getNodeName().equals("output")) {
349             return null;
350         }
351
352         Output o = new Output();
353
354         //Get node attributes
355
NamedNodeMap JavaDoc outputAttributes = n.getAttributes();
356         Node JavaDoc attributeNode = outputAttributes.getNamedItem("name");
357
358         if (attributeNode != null) {
359             String JavaDoc value = attributeNode.getNodeValue();
360
361             if (value != null) {
362                 o.setName(value);
363             }
364         }
365
366         attributeNode = outputAttributes.getNamedItem("alignment");
367
368         if (attributeNode != null) {
369             String JavaDoc value = attributeNode.getNodeValue();
370
371             if (value != null) {
372                 o.alignment = value;
373             }
374         }
375
376         attributeNode = outputAttributes.getNamedItem("key");
377
378         if (attributeNode != null) {
379             String JavaDoc value = attributeNode.getNodeValue();
380
381             if (value != null) {
382                 o.key = value;
383             }
384         }
385
386         attributeNode = outputAttributes.getNamedItem("usedCount");
387
388         if (attributeNode != null) {
389             String JavaDoc value = attributeNode.getNodeValue();
390
391             if (value != null) {
392                 try {
393                     o.usedCount = Long.parseLong(value);
394                 } catch (NumberFormatException JavaDoc nfe) {
395                 }
396             }
397         }
398
399         NodeList JavaDoc nl = n.getChildNodes();
400
401         for (int i = 0; i < nl.getLength(); i++) {
402             Node JavaDoc oneChild = nl.item(i);
403             String JavaDoc nodeName = oneChild.getNodeName();
404
405             if (nodeName.equals("content")) {
406                 Node JavaDoc contentNode = oneChild.getFirstChild();
407
408                 if (contentNode != null) {
409                     o.content = contentNode.getNodeValue();
410                 }
411             } else if (nodeName.equals("controller-element")) {
412                 o = (Output) ControllerElement.fromXML(oneChild, o);
413             }
414         }
415
416         return o;
417     }
418
419     /**
420      * set contents; convenience method to unify Input/Output method names
421      *
422      * @param newContent The new contents string
423      */

424     public synchronized void setDefaultValue(String JavaDoc newContent) {
425         content = newContent;
426     }
427
428     /**
429      * Return contents; convenience method to unify Input/Output method names
430      *
431      * @return A String content
432      */

433     public String JavaDoc getDefaultValue() {
434         return content;
435     } /* getDefaultValue() */
436
437 } /* Output */
438
439
Popular Tags