ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r958857 - in /ant/core/trunk/src: main/org/apache/tools/ant/util/DOMElementWriter.java tests/junit/org/apache/tools/ant/util/DOMElementWriterTest.java
Date Tue, 29 Jun 2010 07:46:07 GMT
Author: bodewig
Date: Tue Jun 29 07:46:06 2010
New Revision: 958857

URL: http://svn.apache.org/viewvc?rev=958857&view=rev
Log:
properly encode whitespace in attribute values

Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/util/DOMElementWriter.java
    ant/core/trunk/src/tests/junit/org/apache/tools/ant/util/DOMElementWriterTest.java

Modified: ant/core/trunk/src/main/org/apache/tools/ant/util/DOMElementWriter.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/util/DOMElementWriter.java?rev=958857&r1=958856&r2=958857&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/util/DOMElementWriter.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/util/DOMElementWriter.java Tue Jun 29 07:46:06
2010
@@ -332,7 +332,7 @@ public class DOMElementWriter {
             }
             out.write(attr.getName());
             out.write("=\"");
-            out.write(encode(attr.getValue()));
+            out.write(encodeAttributeValue(attr.getValue()));
             out.write("\"");
         }
 
@@ -411,6 +411,21 @@ public class DOMElementWriter {
      * @return the encoded string.
      */
     public String encode(String value) {
+        return encode(value, false);
+    }
+
+    /**
+     * Escape <, > & ', " as their entities, \n,
+     * \r and \t as numeric entities and drop characters that are
+     * illegal in XML documents.
+     * @param value the string to encode.
+     * @return the encoded string.
+     */
+    public String encodeAttributeValue(String value) {
+        return encode(value, true);
+    }
+
+    private String encode(String value, boolean encodeWhitespace) {
         int len = value.length();
         StringBuffer sb = new StringBuffer(len);
         for (int i = 0; i < len; i++) {
@@ -431,6 +446,15 @@ public class DOMElementWriter {
             case '&':
                 sb.append("&amp;");
                 break;
+            case '\r':
+            case '\n':
+            case '\t':
+                if (encodeWhitespace) {
+                    sb.append("&#x").append(Integer.toHexString(c)).append(";");
+                } else {
+                    sb.append(c);
+                }
+                break;
             default:
                 if (isLegalCharacter(c)) {
                     sb.append(c);

Modified: ant/core/trunk/src/tests/junit/org/apache/tools/ant/util/DOMElementWriterTest.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/junit/org/apache/tools/ant/util/DOMElementWriterTest.java?rev=958857&r1=958856&r2=958857&view=diff
==============================================================================
--- ant/core/trunk/src/tests/junit/org/apache/tools/ant/util/DOMElementWriterTest.java (original)
+++ ant/core/trunk/src/tests/junit/org/apache/tools/ant/util/DOMElementWriterTest.java Tue
Jun 29 07:46:06 2010
@@ -68,6 +68,40 @@ public class DOMElementWriterTest extend
         assertEquals("&lt;", w.encode("<"));
         assertEquals("&amp;", w.encode("&"));
         assertEquals("", w.encode("\u0017"));
+        assertEquals("\r\n\t", w.encode("\r\n\t"));
+    }
+
+    public void testEncodeAttributeValue() {
+        assertEquals("&amp;#20;", w.encodeAttributeValue("&#20;"));
+        assertEquals("&amp;#x20;", w.encodeAttributeValue("&#x20;"));
+        assertEquals("&amp;#xA0;", w.encodeAttributeValue("&#xA0;"));
+        assertEquals("&amp;#A0;", w.encodeAttributeValue("&#A0;"));
+        assertEquals("20;", w.encodeAttributeValue("20;"));
+        assertEquals("&amp;#20", w.encodeAttributeValue("&#20"));
+        assertEquals("&amp;quot;", w.encodeAttributeValue("&quot;"));
+        assertEquals("&amp;apos;", w.encodeAttributeValue("&apos;"));
+        assertEquals("&amp;gt;", w.encodeAttributeValue("&gt;"));
+        assertEquals("&amp;lt;", w.encodeAttributeValue("&lt;"));
+        assertEquals("&amp;amp;", w.encodeAttributeValue("&amp;"));
+        assertEquals("&quot;", w.encodeAttributeValue("\""));
+        assertEquals("&lt;", w.encodeAttributeValue("<"));
+        assertEquals("&amp;", w.encodeAttributeValue("&"));
+        assertEquals("", w.encodeAttributeValue("\u0017"));
+        assertEquals("&#xd;&#xa;&#x9;", w.encodeAttributeValue("\r\n\t"));
+    }
+
+    public void testAttributeWithWhitespace() throws IOException {
+        Document d = DOMUtils.newDocument();
+        Element root = d.createElement("root");
+        root.setAttribute("foo", "bar\nbaz");
+        StringWriter sw = new StringWriter();
+        DOMElementWriter w = new DOMElementWriter();
+        w.write(root, sw, 0, "  ");
+        assertEquals("<root foo=\"bar&#xa;baz\" />" + StringUtils.LINE_SEP,
+                     sw.toString());
+    }
+
+    public void testEncodeData() {
         assertEquals("&#20;\"20;&", w.encodedata("&#20;\"20;&"));
         assertEquals("", w.encodedata("\u0017"));
     }



Mime
View raw message