ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject cvs commit: jakarta-ant/src/testcases/org/apache/tools/ant/util DOMElementWriterTest.java
Date Tue, 18 Dec 2001 20:57:23 GMT
bodewig     01/12/18 12:57:23

  Modified:    src/main/org/apache/tools/ant/util DOMElementWriter.java
               src/testcases/org/apache/tools/ant/util
                        DOMElementWriterTest.java
  Log:
  Properly encode control characters.  See XML 1.0 2.2
  <http://www.w3.org/TR/1998/REC-xml-19980210#charsets>.
  
  Submitted by:	Jim White <jim@pagesmiths.com>
  
  Revision  Changes    Path
  1.6       +45 -2     jakarta-ant/src/main/org/apache/tools/ant/util/DOMElementWriter.java
  
  Index: DOMElementWriter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/util/DOMElementWriter.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- DOMElementWriter.java	2001/10/28 21:27:20	1.5
  +++ DOMElementWriter.java	2001/12/18 20:57:23	1.6
  @@ -142,7 +142,7 @@
                   
               case Node.CDATA_SECTION_NODE:
                   out.write("<![CDATA[");
  -                out.write(((Text)child).getData());
  +                out.write(encodedata(((Text)child).getData()));
                   out.write("]]>");
                   break;
   
  @@ -183,7 +183,8 @@
       }
   
       /**
  -     * Escape &lt;, &gt; &amp; &apos; and &quot; as their entities.
  +     * Escape &lt;, &gt; &amp; &apos;, &quot; and control characters
  +     * &lt; 0x20 as their entities.
        */
       public String encode(String value) {
           sb.setLength(0);
  @@ -211,8 +212,50 @@
                       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(';');
  +                } else {
  +                    sb.append(c);
  +                }
  +                break;
  +            }
  +        }
  +        return sb.toString();
  +    }
  +
  +    /**
  +     * Escape control characters &lt; x20 as their entities.
  +     *
  +     * <p>See XML 1.0 2.2 <a
  +     * href="http://www.w3.org/TR/1998/REC-xml-19980210#charsets">http://www.w3.org/TR/1998/REC-xml-19980210#charsets</a>.</p>
  +     */
  +    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(';');
  +                } else {
  +                    sb.append(c);
  +                }
                   break;
               }
           }
  
  
  
  1.4       +3 -0      jakarta-ant/src/testcases/org/apache/tools/ant/util/DOMElementWriterTest.java
  
  Index: DOMElementWriterTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/testcases/org/apache/tools/ant/util/DOMElementWriterTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DOMElementWriterTest.java	2001/10/23 10:35:14	1.3
  +++ DOMElementWriterTest.java	2001/12/18 20:57:23	1.4
  @@ -100,5 +100,8 @@
           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"));
       }
   }
  
  
  

--
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