1 16 17 package org.apache.taglibs.standard.tag.common.core; 18 19 import java.io.IOException ; 20 import java.io.Reader ; 21 22 import javax.servlet.jsp.JspException ; 23 import javax.servlet.jsp.JspWriter ; 24 import javax.servlet.jsp.PageContext ; 25 import javax.servlet.jsp.tagext.BodyTagSupport ; 26 27 36 public class OutSupport extends BodyTagSupport { 37 38 42 43 46 protected Object value; protected String def; protected boolean escapeXml; private boolean needBody; 51 54 59 public OutSupport() { 60 super(); 61 init(); 62 } 63 64 private void init() { 66 value = def = null; 67 escapeXml = true; 68 needBody = false; 69 } 70 71 public void release() { 73 super.release(); 74 init(); 75 } 76 77 78 81 public int doStartTag() throws JspException { 83 84 needBody = false; this.bodyContent = null; 87 try { 88 if (value != null) { 90 out(pageContext, escapeXml, value); 91 return SKIP_BODY; 92 } else { 93 if (def == null) { 95 needBody = true; 96 return EVAL_BODY_BUFFERED; 97 } 98 99 if (def != null) { 101 out(pageContext, escapeXml, def); 103 } 104 return SKIP_BODY; 105 } 106 } catch (IOException ex) { 107 throw new JspException (ex.toString(), ex); 108 } 109 } 110 111 public int doEndTag() throws JspException { 113 try { 114 if (!needBody) 115 return EVAL_PAGE; 117 if (bodyContent != null && bodyContent.getString() != null) 119 out(pageContext, escapeXml, bodyContent.getString().trim()); 120 return EVAL_PAGE; 121 } catch (IOException ex) { 122 throw new JspException (ex.toString(), ex); 123 } 124 } 125 126 127 130 143 public static void out(PageContext pageContext, 144 boolean escapeXml, 145 Object obj) throws IOException { 146 JspWriter w = pageContext.getOut(); 147 if (!escapeXml) { 148 if (obj instanceof Reader ) { 150 Reader reader = (Reader )obj; 151 char[] buf = new char[4096]; 152 int count; 153 while ((count=reader.read(buf, 0, 4096)) != -1) { 154 w.write(buf, 0, count); 155 } 156 } else { 157 w.write(obj.toString()); 158 } 159 } else { 160 if (obj instanceof Reader ) { 162 Reader reader = (Reader )obj; 163 char[] buf = new char[4096]; 164 int count; 165 while ((count = reader.read(buf, 0, 4096)) != -1) { 166 writeEscapedXml(buf, count, w); 167 } 168 } else { 169 String text = obj.toString(); 170 writeEscapedXml(text.toCharArray(), text.length(), w); 171 } 172 } 173 } 174 175 181 private static void writeEscapedXml(char[] buffer, int length, JspWriter w) throws IOException { 182 int start = 0; 183 184 for (int i = 0; i < length; i++) { 185 char c = buffer[i]; 186 if (c <= Util.HIGHEST_SPECIAL) { 187 char[] escaped = Util.specialCharactersRepresentation[c]; 188 if (escaped != null) { 189 if (start < i) { 191 w.write(buffer,start,i-start); 192 } 193 w.write(escaped); 195 start = i + 1; 196 } 197 } 198 } 199 if (start < length) { 201 w.write(buffer,start,length-start); 202 } 203 } 204 } 205 | Popular Tags |