1 16 17 package org.apache.taglibs.standard.examples.taglib; 18 19 import java.io.IOException ; 20 import java.io.Reader ; 21 import java.io.Writer ; 22 23 import javax.servlet.jsp.JspException ; 24 import javax.servlet.jsp.JspTagException ; 25 import javax.servlet.jsp.tagext.BodyTagSupport ; 26 27 import org.apache.taglibs.standard.examples.util.Util; 28 29 35 public class EscapeHtmlTag extends BodyTagSupport { 36 37 40 private Object reader; 41 private Object writer; 42 43 46 public EscapeHtmlTag() { 47 super(); 48 init(); 49 } 50 51 private void init() { 52 reader = null; 53 writer = null; 54 } 55 56 57 60 63 public void setReader(Object reader) { 64 this.reader = reader; 65 } 66 67 70 public void setWriter(Object writer) { 71 this.writer = writer; 72 } 73 74 77 public int doEndTag() throws JspException { 78 Reader in; 79 Writer out; 80 81 if (reader == null) { 82 String bcs = getBodyContent().getString().trim(); 83 if (bcs == null || bcs.equals("")) { 84 throw new JspTagException ("In <escapeHtml>, 'reader' " + 85 "not specified and no non-whitespace content inside the tag."); 86 } 87 in = Util.castToReader(bcs); 88 } else { 89 in = Util.castToReader(reader); 90 } 91 92 if (writer == null) { 93 out = pageContext.getOut(); 94 } else { 95 out = Util.castToWriter(writer); 96 } 97 98 transform(in, out); 99 return EVAL_PAGE; 100 } 101 102 105 public void release() { 106 super.release(); 107 init(); 108 } 109 110 113 116 public void transform(Reader reader, Writer writer) 117 throws JspException { 118 int c; 119 try { 120 writer.write("<pre>"); 121 while ((c = reader.read()) != -1) { 122 if (c == '<') { 123 writer.write("<"); 124 } else if (c == '>') { 125 writer.write(">"); 126 } else { 127 writer.write(c); 128 } 129 } 130 writer.write("</pre>"); 131 } catch (IOException ex) { 132 throw new JspException ("EscapeHtml: " + 133 "error copying chars", ex); 134 } 135 } 136 } 137 | Popular Tags |