PR: 6351 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@272312 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -225,6 +225,10 @@ for all test cases. Custom formatters that need to implement | |||||
| <code>org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter</code> | <code>org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter</code> | ||||
| can be specified.</p> | 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"> | <table border="1" cellpadding="2" cellspacing="0"> | ||||
| <tr> | <tr> | ||||
| <td width="12%" valign="top"><b>Attribute</b></td> | <td width="12%" valign="top"><b>Attribute</b></td> | ||||
| @@ -231,7 +231,7 @@ public class XMLResultAggregator extends Task implements XMLConstants { | |||||
| try { | try { | ||||
| out = new FileOutputStream( file ); | out = new FileOutputStream( file ); | ||||
| wri = new PrintWriter(new OutputStreamWriter(out, "UTF8")); | 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, " "); | (new DOMElementWriter()).write(doc.getDocumentElement(), wri, 0, " "); | ||||
| wri.flush(); | wri.flush(); | ||||
| // writers do not throw exceptions, so check for them. | // writers do not throw exceptions, so check for them. | ||||
| @@ -201,8 +201,8 @@ public class DOMElementWriter { | |||||
| } | } | ||||
| /** | /** | ||||
| * Escape <, > & ', " and control characters | |||||
| * < 0x20 as their entities. | |||||
| * Escape <, > & ', " as their entities and | |||||
| * drop characters that are illegal in XML documents. | |||||
| */ | */ | ||||
| public String encode(String value) { | public String encode(String value) { | ||||
| sb.setLength(0); | sb.setLength(0); | ||||
| @@ -230,17 +230,8 @@ public class DOMElementWriter { | |||||
| sb.append('&'); | sb.append('&'); | ||||
| } | } | ||||
| break; | break; | ||||
| case '\t': | |||||
| case '\n': | |||||
| case '\r': | |||||
| sb.append(c); | |||||
| break; | |||||
| default: | default: | ||||
| if (c < 0x20) { | |||||
| sb.append("&#x"); | |||||
| sb.append(Integer.toHexString(c)); | |||||
| sb.append(';'); | |||||
| } else { | |||||
| if (isLegalCharacter(c)) { | |||||
| sb.append(c); | sb.append(c); | ||||
| } | } | ||||
| break; | break; | ||||
| @@ -250,7 +241,7 @@ public class DOMElementWriter { | |||||
| } | } | ||||
| /** | /** | ||||
| * Escape control characters < x20 as their entities. | |||||
| * Drop characters that are illegal in XML documents. | |||||
| * | * | ||||
| * <p>See XML 1.0 2.2 <a | * <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> | * 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 @@ public class DOMElementWriter { | |||||
| sb.setLength(0); | sb.setLength(0); | ||||
| for (int i = 0; i < value.length(); ++i) { | for (int i = 0; i < value.length(); ++i) { | ||||
| char c = value.charAt(i); | char c = value.charAt(i); | ||||
| switch (c) { | |||||
| case '\t': | |||||
| case '\n': | |||||
| case '\r': | |||||
| if (isLegalCharacter(c)) { | |||||
| sb.append(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(); | return sb.toString(); | ||||
| @@ -314,4 +291,27 @@ public class DOMElementWriter { | |||||
| } | } | ||||
| return false; | 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,7 +1,7 @@ | |||||
| /* | /* | ||||
| * The Apache Software License, Version 1.1 | * 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. | * reserved. | ||||
| * | * | ||||
| * Redistribution and use in source and binary forms, with or without | * Redistribution and use in source and binary forms, with or without | ||||
| @@ -100,8 +100,23 @@ public class DOMElementWriterTest extends TestCase { | |||||
| assertEquals(""", w.encode("\"")); | assertEquals(""", w.encode("\"")); | ||||
| assertEquals("<", w.encode("<")); | assertEquals("<", w.encode("<")); | ||||
| assertEquals("&", w.encode("&")); | assertEquals("&", w.encode("&")); | ||||
| assertEquals("", w.encode("\u0017")); | |||||
| assertEquals("", w.encode("\u0017")); | |||||
| assertEquals("\"20;&", w.encodedata("\"20;&")); | assertEquals("\"20;&", w.encodedata("\"20;&")); | ||||
| assertEquals("", 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')); | |||||
| } | } | ||||
| } | } | ||||