diff --git a/src/main/org/apache/tools/ant/types/CharSet.java b/src/main/org/apache/tools/ant/types/CharSet.java index ed4123019..d55af40c8 100644 --- a/src/main/org/apache/tools/ant/types/CharSet.java +++ b/src/main/org/apache/tools/ant/types/CharSet.java @@ -18,7 +18,9 @@ package org.apache.tools.ant.types; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; @@ -59,6 +61,30 @@ public class CharSet extends EnumeratedAttribute { return new CharSet(Charset.defaultCharset().name()); } + /** + * Convenience methood: get US-ASCII CharSet. + * @return the default value. + */ + public static CharSet getAscii() { + return new CharSet(StandardCharsets.US_ASCII.name()); + } + + /** + * Convenience method: get UTF-8 CharSet. + * @return the default value. + */ + public static CharSet getUtf8() { + return new CharSet(StandardCharsets.UTF_8.name()); + } + + /** + * Tell if CharSet values are aliases. + * @return true if CharSet values are aliases. + */ + public boolean equivalent(CharSet cs) { + return getCharset().name().equals(cs.getCharset().name()); + } + /** * Convert this enumerated type to a Charset. * @return a Charset object. @@ -75,4 +101,25 @@ public class CharSet extends EnumeratedAttribute { public String[] getValues() { return VALUES.toArray(new String[0]); } + + /** + * Accept additional values for backwards compatibility + * (some java.io encoding names not available in java.nio) + * @param value the String value of the attribute + */ + @Override + public final void setValue(final String value) { + String realValue = value; + if (value == null || value.isEmpty()) { + realValue = Charset.defaultCharset().name(); + } else { + for (String v : Arrays.asList(value, value.toLowerCase(), value.toUpperCase())) { + if (VALUES.contains(v)) { + realValue = v; + break; + } + } + } + super.setValue(realValue); + } } diff --git a/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java b/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java index dac396615..80884c360 100644 --- a/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java +++ b/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java @@ -88,7 +88,7 @@ public abstract class EnumeratedAttribute { * @param value the String value of the attribute * @throws BuildException if the value is not valid for the attribute */ - public final void setValue(String value) throws BuildException { + public void setValue(String value) throws BuildException { int idx = indexOfValue(value); if (idx == -1) { throw new BuildException(value + " is not a legal value for this attribute"); diff --git a/src/tests/junit/org/apache/tools/ant/types/CharSetTest.java b/src/tests/junit/org/apache/tools/ant/types/CharSetTest.java index 47d8021f2..16dda9fbc 100644 --- a/src/tests/junit/org/apache/tools/ant/types/CharSetTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/CharSetTest.java @@ -27,7 +27,10 @@ import java.util.Arrays; import java.util.Collection; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.equalToIgnoringCase; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; @RunWith(Enclosed.class) public class CharSetTest { @@ -37,7 +40,10 @@ public class CharSetTest { // requires JUnit 4.12 @Parameterized.Parameters(name = "legal argument: |{0}|") public static Collection data() { - return Arrays.asList("UTF-8", "ISO-8859-1", "037", "us", "IBM500"); + return Arrays.asList("UTF-8", "ISO-8859-1", "037", "us", "IBM500", + // some java.io encodings are not provided as aliases in java.nio.charset + // so, for backwards compatibility, the case should not matter + "ascii", "utf-8", "Cp1252"); } @Parameterized.Parameter @@ -46,7 +52,7 @@ public class CharSetTest { @Test public void testCorrectNames() { CharSet cs = new CharSet(argument); - assertThat(argument, equalTo(cs.getValue())); + assertThat(argument, equalToIgnoringCase(cs.getValue())); } } @@ -63,7 +69,41 @@ public class CharSetTest { @Test(expected = BuildException.class) public void testNonExistentNames() { - new CharSet().setValue(argument); + new CharSet(argument); + } + } + + @RunWith(Parameterized.class) + public static class LegalEquivalenceTest { + // requires JUnit 4.12 + @Parameterized.Parameters(name = "equivalent argument: |{0}|") + public static Collection data() { + return Arrays.asList("UTF8", "unicode-1-1-utf-8"); + } + + @Parameterized.Parameter + public String argument; + + @Test + public void testCorrectNames() { + assertTrue(new CharSet(argument).equivalent(CharSet.getUtf8())); + } + } + + @RunWith(Parameterized.class) + public static class IncorrectEquivalenceTest { + // requires JUnit 4.12 + @Parameterized.Parameters(name = "non-equivalent argument: |{0}|") + public static Collection data() { + return Arrays.asList("us", "ISO-8859-1", "default"); + } + + @Parameterized.Parameter + public String argument; + + @Test + public void testIncorrectNames() { + assertFalse(new CharSet(argument).equivalent(CharSet.getUtf8())); } }