ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jim White <...@pagesmiths.com>
Subject DOMElementWriter fix for control characters.
Date Tue, 18 Dec 2001 19:42:48 GMT
Howdy Gang.

Gotta say first thing that I love Ant and it has been my primary scripting 
tool for most of this year.

Anyhow, I've started using CruiseControl and ran into a nasty problem 
caused by some user code (triggered by Jtest) that was sending ANSI control 
characters to the console.  That winds up in the log.xml generated by 
DOMElementWriter.  Unfortunately the XML file is invalid since only '\t', 
'\r', and '\n' controls are allowed, others must be coded as their 
entities, but DOMElementWriter was not doing that.  That is true for both 
TEXT and CDATA.

See: http://www.w3.org/TR/1998/REC-xml-19980210#charsets

Following is my patch to fix this problem.

jim

cvs diff -c DOMElementWriter.java
Index: DOMElementWriter.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/util/DOMElementWriter.java,v
retrieving revision 1.5
diff -c -r1.5 DOMElementWriter.java
*** DOMElementWriter.java       2001/10/28 21:27:20     1.5
--- DOMElementWriter.java       2001/12/18 18:37:21
***************
*** 142,148 ****

               case Node.CDATA_SECTION_NODE:
                   out.write("<![CDATA[");
!                 out.write(((Text)child).getData());
                   out.write("]]>");
                   break;

--- 142,148 ----

               case Node.CDATA_SECTION_NODE:
                   out.write("<![CDATA[");
!                 out.write(encodedata(((Text)child).getData()));
                   out.write("]]>");
                   break;

***************
*** 183,189 ****
       }

       /**
!      * Escape &lt;, &gt; &amp; &apos; and &quot; as their entities.
        */
       public String encode(String value) {
           sb.setLength(0);
--- 183,189 ----
       }

       /**
!      * Escape &lt;, &gt; &amp; &apos; and &quot; and < &#x20;
as their 
entities.
        */
       public String encode(String value) {
           sb.setLength(0);
***************
*** 211,217 ****
--- 211,261 ----
                       sb.append('&');
                   }
                   break;
+             case '\t':
+             case '\n':
+             case '\r':
+                 sb.append(c);
+                 break;
+
               default:
+                 if (c < 0x20) {
+                     sb.append("&#x");
+                     sb.append(Integer.toHexString(c));
+                     sb.append(';');
+                     break;
+                 }
+
+                 sb.append(c);
+                 break;
+             }
+         }
+         return sb.toString();
+     }
+
+     /**
+      * Escape < &#x20; as their entities.
+      * See XML 1.0 2.2 <http://www.w3.org/TR/1998/REC-xml-19980210#charsets>.
+      */
+     public String encodedata(final String value)
+     {
+         sb.setLength(0);
+         for (int i = 0; i < value.length(); ++i) {
+             char c = value.charAt(i);
+             switch (c) {
+             case '\t':
+             case '\n':
+             case '\r':
+                 sb.append(c);
+                 break;
+
+             default:
+                 if (c < 0x20) {
+                     sb.append("&#x");
+                     sb.append(Integer.toHexString(c));
+                     sb.append(';');
+                     break;
+                 }
+
                   sb.append(c);
                   break;
               }

*****CVS exited normally with code 1*****

cvs diff -c DOMElementWriterTest.java
Index: DOMElementWriterTest.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/testcases/org/apache/tools/ant/util/DOMElementWriterTest.java,v
retrieving revision 1.3
diff -c -r1.3 DOMElementWriterTest.java
*** DOMElementWriterTest.java   2001/10/23 10:35:14     1.3
--- DOMElementWriterTest.java   2001/12/18 19:38:26
***************
*** 100,104 ****
--- 100,107 ----
           assertEquals("&quot;", w.encode("\""));
           assertEquals("&lt;", w.encode("<"));
           assertEquals("&amp;", w.encode("&"));
+         assertEquals("&#x17;", w.encode("\u0017"));
+         assertEquals("&#20;\"20;&", w.encodedata("&#20;\"20;&"));
+         assertEquals("&#x17;", w.encodedata("\u0017"));
       }
   }

*****CVS exited normally with code 1*****


----------------------------------------------------------------
James P. White     Pagesmiths' home is http://www.pagesmiths.com
Live free http://www.ushistory.org/franklin/quotable/quote04.htm
Try Kawa, the Java-based Scheme http://www.gnu.org/software/kawa


--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message