KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > webwork > view > taglib > PrintTag


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 /**
27  * Print a value from the ValueStack.
28  */

29 package net.killingar.webwork.view.taglib;
30
31 import webwork.util.BeanUtil;
32 import webwork.util.TextUtil;
33
34 import javax.servlet.jsp.JspException JavaDoc;
35 import javax.servlet.jsp.JspTagException JavaDoc;
36 import java.io.Reader JavaDoc;
37 import java.io.Writer JavaDoc;
38 import java.util.Iterator JavaDoc;
39
40 public class PrintTag extends webwork.view.taglib.WebWorkTagSupport
41 {
42     // Attributes ----------------------------------------------------
43
protected String JavaDoc valueAttr;
44     protected Boolean JavaDoc escape;
45
46     // Public --------------------------------------------------------
47
public void setValue(String JavaDoc inName)
48     {
49         valueAttr = inName;
50     }
51
52     public void setEscape(boolean inEscape)
53     {
54         escape = new Boolean JavaDoc(inEscape);
55     }
56
57     // BodyTag implementation ----------------------------------------
58
public int doStartTag() throws JspException JavaDoc
59     {
60         Object JavaDoc value = findValue(valueAttr);
61         if (escape == null)
62             escape = new Boolean JavaDoc(false);
63
64         try
65         {
66             if (value != null)
67             {
68                 if (value instanceof Iterator JavaDoc)
69                 {
70                     Iterator JavaDoc i = (Iterator JavaDoc) value;
71                     if (i.hasNext())
72                         write(i.next());
73                 }
74                 else if (value instanceof char[])
75                     write(String.valueOf((char[]) value));
76                 else if (value instanceof Reader JavaDoc)
77                 {
78                     Reader JavaDoc in = (Reader JavaDoc)value;
79                     Writer JavaDoc out = pageContext.getOut();
80
81                     char buf[] = new char[524288];
82                     int read, totalRead = 0;
83
84                     while(true)
85                     {
86                         read = in.read(buf);
87                         if (read == -1)break;
88                         out.write(buf, 0, read);
89                         totalRead += read;
90                     }
91                 }
92                 /*else if (value instanceof java.beans.PropertyDescriptor)
93                 {
94                     java.beans.PropertyDescriptor desc = (java.beans.PropertyDescriptor)value;
95
96                     java.lang.reflect.Method m = desc.getReadMethod();
97
98                     write(m.invoke(findValue("../.."), new Object[]{}));
99                 }*/

100                 else
101                     write(value);
102             }
103             else
104                 pageContext.getOut().write(""); // Printing out null gives no output
105
}
106         catch (Exception JavaDoc e)
107         {
108             e.printStackTrace();
109             throw new JspTagException JavaDoc("Could not show value " + valueAttr + ":" + e);
110         }
111
112         return SKIP_BODY;
113     }
114
115     private void write(Object JavaDoc output) throws java.io.IOException JavaDoc
116     {
117         String JavaDoc s = BeanUtil.toStringValue(output);
118         if (escape.booleanValue())
119             s = TextUtil.escapeHTML(s);
120
121         pageContext.getOut().write(s);
122     }
123 }
Popular Tags