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, 09 Apr 2002 12:13:25 GMT
bodewig     02/04/09 05:13:25

  Modified:    docs/manual/OptionalTasks junit.html
               src/main/org/apache/tools/ant/taskdefs/optional/junit
                        XMLResultAggregator.java
               src/main/org/apache/tools/ant/util DOMElementWriter.java
               src/testcases/org/apache/tools/ant/util
                        DOMElementWriterTest.java
  Log:
  swallow characters that are illegal in XML documents.
  
  PR: 6351
  
  Revision  Changes    Path
  1.17      +4 -0      jakarta-ant/docs/manual/OptionalTasks/junit.html
  
  Index: junit.html
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/docs/manual/OptionalTasks/junit.html,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- junit.html	20 Mar 2002 00:02:15 -0000	1.16
  +++ junit.html	9 Apr 2002 12:13:25 -0000	1.17
  @@ -225,6 +225,10 @@
   <code>org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter</code>
   can be specified.</p>
   
  +<p>If you use the XML formatter, it may not include the same output
  +that your tests have written as some characters are illegal in XML
  +documents and will be dropped.</p>
  +
   <table border="1" cellpadding="2" cellspacing="0">
   <tr>
     <td width="12%" valign="top"><b>Attribute</b></td>
  
  
  
  1.15      +1 -1      jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregator.java
  
  Index: XMLResultAggregator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregator.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- XMLResultAggregator.java	3 Apr 2002 13:18:03 -0000	1.14
  +++ XMLResultAggregator.java	9 Apr 2002 12:13:25 -0000	1.15
  @@ -231,7 +231,7 @@
           try {
               out = new FileOutputStream( file );
               wri = new PrintWriter(new OutputStreamWriter(out, "UTF8"));
  -            wri.write("<?xml version=\"1.0\"?>\n");
  +            wri.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
               (new DOMElementWriter()).write(doc.getDocumentElement(), wri, 0, "  ");
               wri.flush();
               // writers do not throw exceptions, so check for them.
  
  
  
  1.10      +28 -28    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.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- DOMElementWriter.java	14 Feb 2002 09:30:30 -0000	1.9
  +++ DOMElementWriter.java	9 Apr 2002 12:13:25 -0000	1.10
  @@ -201,8 +201,8 @@
       }
   
       /**
  -     * Escape &lt;, &gt; &amp; &apos;, &quot; and control characters
  -     * &lt; 0x20 as their entities.
  +     * Escape &lt;, &gt; &amp; &apos;, &quot; as their entities and
  +     * drop characters that are illegal in XML documents.
        */
       public String encode(String value) {
           sb.setLength(0);
  @@ -230,17 +230,8 @@
                       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 {
  +                if (isLegalCharacter(c)) {
                       sb.append(c);
                   }
                   break;
  @@ -250,7 +241,7 @@
       }
   
       /**
  -     * Escape control characters &lt; x20 as their entities.
  +     * Drop characters that are illegal in XML documents.
        *
        * <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>
  @@ -259,22 +250,8 @@
           sb.setLength(0);
           for (int i = 0; i < value.length(); ++i) {
               char c = value.charAt(i);
  -            switch (c) {
  -            case '\t':
  -            case '\n':
  -            case '\r':
  +            if (isLegalCharacter(c)) {
                   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();
  @@ -311,6 +288,29 @@
               if (name.equals(knownEntities[i])) {
                   return true;
               }
  +        }
  +        return false;
  +    }
  +
  +    /**
  +     * Is the given character allowed inside an XML document?
  +     *
  +     * <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>
  +     *
  +     * @since 1.10, Ant 1.5
  +     */
  +    public boolean isLegalCharacter(char c) {
  +        if (c == 0x9 || c == 0xA || c == 0xD) {
  +            return true;
  +        } else if (c < 0x20) {
  +            return false;
  +        } else if (c <= 0xD7FF) {
  +            return true;
  +        } else if (c < 0xE000) {
  +            return false;
  +        } else if (c <= 0xFFFD) {
  +            return true;
           }
           return false;
       }
  
  
  
  1.6       +18 -3     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.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- DOMElementWriterTest.java	10 Jan 2002 10:13:13 -0000	1.5
  +++ DOMElementWriterTest.java	9 Apr 2002 12:13:25 -0000	1.6
  @@ -1,7 +1,7 @@
   /*
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
  + * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -100,8 +100,23 @@
           assertEquals("&quot;", w.encode("\""));
           assertEquals("&lt;", w.encode("<"));
           assertEquals("&amp;", w.encode("&"));
  -        assertEquals("&#x17;", w.encode("\u0017"));
  +        assertEquals("", w.encode("\u0017"));
           assertEquals("&#20;\"20;&", w.encodedata("&#20;\"20;&"));
  -        assertEquals("&#x17;", w.encodedata("\u0017"));
  +        assertEquals("", w.encodedata("\u0017"));
  +    }
  +
  +    public void testIsLegalCharacter() {
  +        assertTrue("0x00", !w.isLegalCharacter('\u0000'));
  +        assertTrue("0x09", w.isLegalCharacter('\t'));
  +        assertTrue("0x0A", w.isLegalCharacter('\n'));
  +        assertTrue("0x0C", w.isLegalCharacter('\r'));
  +        assertTrue("0x1F", !w.isLegalCharacter('\u001F'));
  +        assertTrue("0x20", w.isLegalCharacter('\u0020'));
  +        assertTrue("0xD7FF", w.isLegalCharacter('\uD7FF'));
  +        assertTrue("0xD800", !w.isLegalCharacter('\uD800'));
  +        assertTrue("0xDFFF", !w.isLegalCharacter('\uDFFF'));
  +        assertTrue("0xE000", w.isLegalCharacter('\uE000'));
  +        assertTrue("0xFFFD", w.isLegalCharacter('\uFFFD'));
  +        assertTrue("0xFFFE", !w.isLegalCharacter('\uFFFE'));
       }
   }
  
  
  

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