1 25 26 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 ; 35 import javax.servlet.jsp.JspTagException ; 36 import java.io.Reader ; 37 import java.io.Writer ; 38 import java.util.Iterator ; 39 40 public class PrintTag extends webwork.view.taglib.WebWorkTagSupport 41 { 42 protected String valueAttr; 44 protected Boolean escape; 45 46 public void setValue(String inName) 48 { 49 valueAttr = inName; 50 } 51 52 public void setEscape(boolean inEscape) 53 { 54 escape = new Boolean (inEscape); 55 } 56 57 public int doStartTag() throws JspException 59 { 60 Object value = findValue(valueAttr); 61 if (escape == null) 62 escape = new Boolean (false); 63 64 try 65 { 66 if (value != null) 67 { 68 if (value instanceof Iterator ) 69 { 70 Iterator i = (Iterator ) 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 ) 77 { 78 Reader in = (Reader )value; 79 Writer 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 100 else 101 write(value); 102 } 103 else 104 pageContext.getOut().write(""); } 106 catch (Exception e) 107 { 108 e.printStackTrace(); 109 throw new JspTagException ("Could not show value " + valueAttr + ":" + e); 110 } 111 112 return SKIP_BODY; 113 } 114 115 private void write(Object output) throws java.io.IOException 116 { 117 String s = BeanUtil.toStringValue(output); 118 if (escape.booleanValue()) 119 s = TextUtil.escapeHTML(s); 120 121 pageContext.getOut().write(s); 122 } 123 } | Popular Tags |