jakarta-taglibs-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pier...@apache.org
Subject cvs commit: jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/core OutSupport.java
Date Mon, 31 Mar 2003 05:47:22 GMT
pierred     2003/03/30 21:47:22

  Modified:    standard/examples/web/elsupport Out.jsp
               standard/src/org/apache/taglibs/standard/tag/common/core
                        OutSupport.java
  Log:
  <c:out> now has special processing for Reader objects: read the content and
  then write to current JspWriter instead of simply writing reader.toString().
  
  Revision  Changes    Path
  1.4       +10 -0     jakarta-taglibs/standard/examples/web/elsupport/Out.jsp
  
  Index: Out.jsp
  ===================================================================
  RCS file: /home/cvs/jakarta-taglibs/standard/examples/web/elsupport/Out.jsp,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Out.jsp	25 Mar 2003 19:43:22 -0000	1.3
  +++ Out.jsp	31 Mar 2003 05:47:21 -0000	1.4
  @@ -20,5 +20,15 @@
       </tr>
     </c:forEach>
   </table>
  +
  +<h4>&lt;c:out&gt; with Reader object</h4>
  +<%
  +java.io.Reader reader1 = new java.io.StringReader("<foo>Text for a Reader!</foo>");
  +pageContext.setAttribute("myReader1", reader1);
  +java.io.Reader reader2 = new java.io.StringReader("<foo>Text for a Reader!</foo>");
  +pageContext.setAttribute("myReader2", reader2);
  +%>
  +Reader1 (escapeXml=true) : <c:out value="${myReader1}"/><br>
  +Reader2 (escapeXml=false): <c:out value="${myReader2}" escapeXml="false"/><br>
   </body>
   </html>
  
  
  
  1.9       +45 -21    jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/core/OutSupport.java
  
  Index: OutSupport.java
  ===================================================================
  RCS file: /home/cvs/jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/core/OutSupport.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- OutSupport.java	25 Mar 2003 19:43:25 -0000	1.8
  +++ OutSupport.java	31 Mar 2003 05:47:21 -0000	1.9
  @@ -124,7 +124,7 @@
         try {
   	// print value if available; otherwise, try 'default'
   	if (value != null) {
  -            out(pageContext, escapeXml, value.toString());
  +            out(pageContext, escapeXml, value);
   	    return SKIP_BODY;
   	} else {
   	    // if we don't have a 'default' attribute, just go to the body
  @@ -136,7 +136,7 @@
   	    // if we do have 'default', print it
   	    if (def != null) {
   		// good 'default'
  -                out(pageContext, escapeXml, def.toString());
  +                out(pageContext, escapeXml, def);
   	    }
   	    return SKIP_BODY;
   	}
  @@ -179,27 +179,51 @@
        */
       public static void out(PageContext pageContext,
                              boolean escapeXml,
  -                           String text) throws IOException {
  +                           Object obj) throws IOException {
           JspWriter w = pageContext.getOut();
  -	if (!escapeXml)
  -            w.print(text);
  -        else {
  -            // avoid needless double-buffering (is this really more efficient?)
  -            for (int i = 0; i < text.length(); i++) {
  -                char c = text.charAt(i);
  -                if (c == '&')
  -                    w.print("&amp;");
  -                else if (c == '<')
  -                    w.print("&lt;");
  -                else if (c == '>')
  -                    w.print("&gt;");
  -                else if (c == '"')
  -                    w.print("&#034;");
  -                else if (c == '\'')
  -                    w.print("&#039;");
  -                else
  -                    w.print(c);
  +	if (!escapeXml) {
  +            // write chars as is
  +            if (obj instanceof Reader) {
  +                Reader reader = (Reader)obj;
  +                char[] buf = new char[4096];
  +                int count;
  +                while ((count=reader.read(buf, 0, 4096)) != -1) {
  +                    w.write(buf, 0, count);
  +                }
  +            } else {
  +                w.print(obj.toString());
  +            }
  +        } else {
  +            // escape XML chars
  +            if (obj instanceof Reader) {
  +                Reader reader = (Reader)obj;
  +                int c;
  +                while ((c=reader.read()) != -1) {
  +                    escapeChar((char)c, w);
  +                } 
  +            } else {
  +                String text = obj.toString();
  +                // avoid needless double-buffering (is this really more efficient?)
  +                for (int i = 0; i < text.length(); i++) {
  +                    char c = text.charAt(i);
  +                    escapeChar(c, w);
  +                }
               }
           }
  +    }
  +    
  +    private static void escapeChar(char c, JspWriter w) throws IOException {
  +        if (c == '&')
  +            w.print("&amp;");
  +        else if (c == '<')
  +            w.print("&lt;");
  +        else if (c == '>')
  +            w.print("&gt;");
  +        else if (c == '"')
  +            w.print("&#034;");
  +        else if (c == '\'')
  +            w.print("&#039;");
  +        else
  +            w.print(c);
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-dev-help@jakarta.apache.org


Mime
View raw message