001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.core.controller;
066:
067: import com.jcorporate.expresso.core.cache.Cacheable;
068: import com.jcorporate.expresso.core.misc.StringUtil;
069: import com.jcorporate.expresso.kernel.util.FastStringBuffer;
070: import org.w3c.dom.NamedNodeMap;
071: import org.w3c.dom.Node;
072: import org.w3c.dom.NodeList;
073:
074: import java.util.Enumeration;
075:
076: /**
077: * An item returned from a controller to provide output/feedback to the
078: * client.<br>
079: * An Output object is one of the three types of object that can be returned
080: * by a Controller moving to a new state. The other two are Input and
081: * Action items.<br>
082: * An Output item can be a single item, or a list of items, using the "nested"
083: * methods in the ControllerElement class.
084: */
085: public class Output extends ControllerElement implements Cloneable,
086: Cacheable, java.io.Serializable {
087:
088: /**
089: * The string contents of this Output
090: */
091: private String content;
092:
093: /**
094: * Suggested "style" of this Output item
095: */
096: private String style;
097:
098: /**
099: * Suggested alignment of this Output item
100: */
101: private String alignment;
102: private long usedCount = 0;
103:
104: /**
105: * Key used when this output is used as a Cacheable object
106: */
107: private String key = null;
108:
109: /**
110: * Default constructor
111: */
112: public Output() {
113: super ();
114: } /* Output */
115:
116: /**
117: * Convenience constructor for creating an output with a specific
118: * string as it's contents in one step
119: *
120: * @param c The string contents of this Output item
121: */
122: public Output(String c) {
123: super ();
124: setContent(c);
125: } /* Output(String) */
126:
127: /**
128: * Convenience constructor for creating an output with a specific
129: * string as it's contents and a name in one step
130: *
131: * @param name The name of this new Output item
132: * @param c The string contents of the Output item
133: */
134: public Output(String name, String c) {
135: setName(name);
136: setContent(c);
137: } /* Output(String, String) */
138:
139: /**
140: * Add a new nested output item to this item
141: *
142: * @param o The new Output item to be nested into this one
143: */
144: public void addNested(Output o) {
145: super .addNested(o);
146: } /* addNested(Output) */
147:
148: /**
149: * Clears the used count
150: */
151: public void clearUsedCount() {
152: usedCount = 0;
153: } /* clearUsedCount() */
154:
155: /**
156: * Deep copy clone the object
157: *
158: * @return Copy of this current output
159: */
160: public Object clone() throws CloneNotSupportedException {
161: Output newOutput;
162:
163: synchronized (this ) {
164: newOutput = (Output) super .clone();
165: newOutput.alignment = alignment;
166: newOutput.content = content;
167: newOutput.key = key;
168: newOutput.usedCount = usedCount;
169: }
170:
171: return newOutput;
172: } /* clone() */
173:
174: /**
175: * Get the suggested alignment for this item
176: *
177: * @return The suggested alignment for this Output item
178: */
179: public String getAlignment() {
180: return alignment;
181: } /* getAlignment() */
182:
183: /**
184: * Get the "contents" of this Output, the string value that this
185: * Output object represents
186: *
187: * @return A String value for the contents of this Output
188: */
189: public String getContent() {
190: return StringUtil.notNull(content);
191: } /* getContent() */
192:
193: /**
194: * Get the name of this output element
195: *
196: * @return the key of the output Element
197: */
198: public String getKey() {
199: return key;
200: } /* getKey() */
201:
202: /**
203: * /**
204: * Return the suggested "style" for displaying this Output item
205: *
206: * @return A "style" string as suggested by the Controller
207: */
208: public String getStyle() {
209: return style;
210: } /* getStyle() */
211:
212: /**
213: * @return the number of times this output has been used
214: */
215: public long getUsedCount() {
216: return usedCount;
217: } /* getUsedCount() */
218:
219: /**
220: * Does this output have a nested item with the given name?
221: *
222: * @param nestedName The name to check against
223: * @return true if there is such an element, else false
224: * @throws ControllerException upon error
225: */
226: public boolean hasNested(String nestedName)
227: throws ControllerException {
228: Output oneElement = null;
229:
230: for (Enumeration e = getNested().elements(); e
231: .hasMoreElements();) {
232: oneElement = (Output) e.nextElement();
233:
234: if (oneElement.getName().equals(nestedName)) {
235: return true;
236: }
237: } /* for each element nested */
238:
239: return false;
240: } /* hasNested(String) */
241:
242: /**
243: *
244: */
245: public synchronized void incrementUsedCount() {
246: usedCount++;
247: } /* incrementUsedCount() */
248:
249: /**
250: * Method used by the Controller to specify a suggested alignment
251: * for this Output item
252: *
253: * @param newAlignment A String suggesting an alignment to the client
254: * when displaying this Output item
255: */
256: public synchronized void setAlignment(String newAlignment) {
257: alignment = newAlignment;
258: } /* setAlignment(String) */
259:
260: /**
261: * Used by the Controller to specify the contents string of this Output
262: *
263: * @param newContent The new contents string
264: */
265: public synchronized void setContent(String newContent) {
266: content = newContent;
267: } /* setContent(String) */
268:
269: /**
270: * @param newKey The new Key for the output
271: */
272: public synchronized void setKey(String newKey) {
273: key = newKey;
274: } /* setKey(String) */
275:
276: /**
277: * Used by the Controller to set a suggested "style" for displaying this
278: * Output item
279: *
280: * @param newStyle The new "style" to set for this output object
281: */
282: public synchronized void setStyle(String newStyle) {
283: style = newStyle;
284: } /* setStyle(String) */
285:
286: /**
287: * Concert the object to an xml fragment.
288: *
289: * @param stream The fastStringBuffer to append to
290: * @return a FastStingBuffer object that represents this output in an
291: * xml fragment
292: */
293: public FastStringBuffer toXML(FastStringBuffer stream) {
294: stream.append("<output");
295:
296: if (this .getName() != null && this .getName().length() > 0) {
297: stream.append(" name=\"");
298: stream.append(StringUtil.xmlEscape(this .getName()));
299: stream.append("\"");
300: }
301: if (this .alignment != null && this .alignment.length() > 0) {
302: stream.append(" alignment=\"");
303: stream.append(StringUtil.xmlEscape(this .alignment));
304: stream.append("\"");
305: }
306: if (this .key != null && this .key.length() > 0) {
307: stream.append(" key=\"");
308: stream.append(StringUtil.xmlEscape(this .key));
309: stream.append("\"");
310: }
311: if (this .usedCount > 0) {
312: stream.append(" usedCount =\"");
313: stream.append(Long.toString(this .usedCount));
314: stream.append("\"");
315: }
316:
317: stream.append(">\n");
318: stream.append("<content>");
319: stream.append(StringUtil.xmlEscape(getContent()));
320: stream.append("</content>\n");
321: stream = super .toXML(stream);
322: stream.append("</output>\n");
323:
324: return stream;
325: }
326:
327: /**
328: * Return a controller element based upon the xml fragment
329: *
330: * @param n a DOM node
331: * @return a built ControllerElement
332: * @throws ControllerException if the nodes do not match what we expect
333: */
334: public static ControllerElement fromXML(Node n)
335: throws ControllerException {
336:
337: //If we're at the root node, then it'll be doc instead of input.
338: if (n.getNodeName().equals("#document")) {
339: return fromXML(n.getChildNodes().item(0));
340: }
341: if (!n.getNodeName().equals("output")) {
342: return null;
343: }
344:
345: Output o = new Output();
346:
347: //Get node attributes
348: NamedNodeMap outputAttributes = n.getAttributes();
349: Node attributeNode = outputAttributes.getNamedItem("name");
350:
351: if (attributeNode != null) {
352: String value = attributeNode.getNodeValue();
353:
354: if (value != null) {
355: o.setName(value);
356: }
357: }
358:
359: attributeNode = outputAttributes.getNamedItem("alignment");
360:
361: if (attributeNode != null) {
362: String value = attributeNode.getNodeValue();
363:
364: if (value != null) {
365: o.alignment = value;
366: }
367: }
368:
369: attributeNode = outputAttributes.getNamedItem("key");
370:
371: if (attributeNode != null) {
372: String value = attributeNode.getNodeValue();
373:
374: if (value != null) {
375: o.key = value;
376: }
377: }
378:
379: attributeNode = outputAttributes.getNamedItem("usedCount");
380:
381: if (attributeNode != null) {
382: String value = attributeNode.getNodeValue();
383:
384: if (value != null) {
385: try {
386: o.usedCount = Long.parseLong(value);
387: } catch (NumberFormatException nfe) {
388: }
389: }
390: }
391:
392: NodeList nl = n.getChildNodes();
393:
394: for (int i = 0; i < nl.getLength(); i++) {
395: Node oneChild = nl.item(i);
396: String nodeName = oneChild.getNodeName();
397:
398: if (nodeName.equals("content")) {
399: Node contentNode = oneChild.getFirstChild();
400:
401: if (contentNode != null) {
402: o.content = contentNode.getNodeValue();
403: }
404: } else if (nodeName.equals("controller-element")) {
405: o = (Output) ControllerElement.fromXML(oneChild, o);
406: }
407: }
408:
409: return o;
410: }
411:
412: /**
413: * set contents; convenience method to unify Input/Output method names
414: *
415: * @param newContent The new contents string
416: */
417: public synchronized void setDefaultValue(String newContent) {
418: content = newContent;
419: }
420:
421: /**
422: * Return contents; convenience method to unify Input/Output method names
423: *
424: * @return A String content
425: */
426: public String getDefaultValue() {
427: return content;
428: } /* getDefaultValue() */
429:
430: } /* Output */
|