diff --git a/src/etc/testcases/taskdefs/xmlproperty.xml b/src/etc/testcases/taskdefs/xmlproperty.xml index 457cfca95..6436a73a5 100644 --- a/src/etc/testcases/taskdefs/xmlproperty.xml +++ b/src/etc/testcases/taskdefs/xmlproperty.xml @@ -4,4 +4,7 @@ + + + diff --git a/src/etc/testcases/taskdefs/xmlproperty_data.dtd b/src/etc/testcases/taskdefs/xmlproperty_data.dtd new file mode 100644 index 000000000..8c17474b2 --- /dev/null +++ b/src/etc/testcases/taskdefs/xmlproperty_data.dtd @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/src/etc/testcases/taskdefs/xmlproperty_withdtd.xml b/src/etc/testcases/taskdefs/xmlproperty_withdtd.xml new file mode 100644 index 000000000..3b7f6fc2a --- /dev/null +++ b/src/etc/testcases/taskdefs/xmlproperty_withdtd.xml @@ -0,0 +1,7 @@ + + + + Text + false + ]]> + diff --git a/src/main/org/apache/tools/ant/taskdefs/XmlProperty.java b/src/main/org/apache/tools/ant/taskdefs/XmlProperty.java index 04af3d99d..729517202 100644 --- a/src/main/org/apache/tools/ant/taskdefs/XmlProperty.java +++ b/src/main/org/apache/tools/ant/taskdefs/XmlProperty.java @@ -65,6 +65,7 @@ import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.types.Path; import org.apache.tools.ant.util.FileUtils; +import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; @@ -260,23 +261,16 @@ public class XmlProperty extends org.apache.tools.ant.Task { throw new BuildException(msg); } - BufferedInputStream configurationStream = null; - try { log("Loading " + src.getAbsolutePath(), Project.MSG_VERBOSE); if (src.exists()) { - configurationStream = - new BufferedInputStream(new FileInputStream(src)); - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setValidating(validate); factory.setNamespaceAware(false); - - Element topElement - = factory.newDocumentBuilder().parse(configurationStream).getDocumentElement(); + Document document = factory.newDocumentBuilder().parse(src); + Element topElement = document.getDocumentElement(); // Keep a hashtable of attributes added by this task. // This task is allow to override its own properties @@ -312,13 +306,6 @@ public class XmlProperty extends org.apache.tools.ant.Task { } catch (IOException ioe) { // I/O error throw new BuildException(ioe); - } finally { - if (configurationStream != null) { - try { - configurationStream.close(); - } catch (Exception e) { - } - } } } diff --git a/src/testcases/org/apache/tools/ant/taskdefs/XmlPropertyTest.java b/src/testcases/org/apache/tools/ant/taskdefs/XmlPropertyTest.java index c686b37e0..01fbdb271 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/XmlPropertyTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/XmlPropertyTest.java @@ -87,13 +87,18 @@ public class XmlPropertyTest extends BuildFileTest { executeTarget("test"); assertEquals("true", getProject().getProperty("root-tag(myattr)")); assertEquals("Text", getProject().getProperty("root-tag.inner-tag")); - assertEquals("val", + assertEquals("val", getProject().getProperty("root-tag.inner-tag(someattr)")); assertEquals("false", getProject().getProperty("root-tag.a2.a3.a4")); - assertEquals("CDATA failed", + assertEquals("CDATA failed", "", getProject().getProperty("root-tag.cdatatag")); } + public void testDTD() { + executeTarget("testdtd"); + assertEquals("Text", getProject().getProperty("root-tag.inner-tag")); + } + public void testNone () { doTest("testNone", false, false, false, false, false); } @@ -144,7 +149,7 @@ public class XmlPropertyTest extends BuildFileTest { */ private void doTest(String msg, boolean keepRoot, boolean collapse, boolean semantic, boolean include, boolean localRoot) { - Enumeration iter = + Enumeration iter = getFiles(new File("src/etc/testcases/taskdefs/xmlproperty/inputs")); while (iter.hasMoreElements()) { File inputFile = (File) iter.nextElement(); @@ -162,8 +167,8 @@ public class XmlPropertyTest extends BuildFileTest { File propertyFile = getGoldfile(inputFile, keepRoot, collapse, semantic, include, localRoot); if (!propertyFile.exists()) { -// System.out.println("Skipping as " -// + propertyFile.getAbsolutePath() +// System.out.println("Skipping as " +// + propertyFile.getAbsolutePath() // + ") doesn't exist."); continue; } @@ -209,15 +214,15 @@ public class XmlPropertyTest extends BuildFileTest { * but some other properties may get set in the XmlProperty due * to generic Project/Task configuration. */ - private static void ensureProperties (String msg, File inputFile, - File workingDir, Project project, + private static void ensureProperties (String msg, File inputFile, + File workingDir, Project project, Properties properties) { Hashtable xmlproperties = project.getProperties(); // Every key identified by the Properties must have been loaded. Enumeration propertyKeyEnum = properties.propertyNames(); while(propertyKeyEnum.hasMoreElements()){ String currentKey = propertyKeyEnum.nextElement().toString(); - String assertMsg = msg + "-" + inputFile.getName() + String assertMsg = msg + "-" + inputFile.getName() + " Key=" + currentKey; String propertyValue = properties.getProperty(currentKey); @@ -238,11 +243,11 @@ public class XmlPropertyTest extends BuildFileTest { } // What is the property supposed to be? - propertyValue = + propertyValue = propertyValue.substring(3, propertyValue.length()); if (propertyValue.equals("path")) { if (!(obj instanceof Path)) { - fail(assertMsg + " Path ID is a " + fail(assertMsg + " Path ID is a " + obj.getClass().getName()); } } else { @@ -255,7 +260,7 @@ public class XmlPropertyTest extends BuildFileTest { // The property is the name of a file. We are testing // a location attribute, so we need to resolve the given // file name in the provided folder. - String fileName = + String fileName = propertyValue.substring(5, propertyValue.length()); File f = new File(workingDir, fileName); propertyValue = f.getAbsolutePath(); @@ -274,7 +279,7 @@ public class XmlPropertyTest extends BuildFileTest { Enumeration keyEnum = xmlproperties.keys(); while (keyEnum.hasMoreElements()) { String currentKey = keyEnum.nextElement().toString(); - System.out.println(currentKey + " = " + System.out.println(currentKey + " = " + xmlproperties.get(currentKey)); } } @@ -282,7 +287,7 @@ public class XmlPropertyTest extends BuildFileTest { /** * Ensure all references loaded by the project are valid. */ - private static void ensureReferences (String msg, File inputFile, + private static void ensureReferences (String msg, File inputFile, Hashtable references) { Enumeration referenceKeyEnum = references.keys(); while(referenceKeyEnum.hasMoreElements()){ @@ -293,7 +298,7 @@ public class XmlPropertyTest extends BuildFileTest { } else if (currentValue instanceof String) { } else { if( ! currentKey.startsWith("ant.") ) { - fail(msg + "-" + inputFile.getName() + " Key=" + fail(msg + "-" + inputFile.getName() + " Key=" + currentKey + " is not a recognized type."); } } @@ -304,16 +309,16 @@ public class XmlPropertyTest extends BuildFileTest { * Munge the name of the input file to find an appropriate goldfile, * based on hardwired naming conventions. */ - private static File getGoldfile (File input, boolean keepRoot, - boolean collapse, boolean semantic, + private static File getGoldfile (File input, boolean keepRoot, + boolean collapse, boolean semantic, boolean include, boolean localRoot) { // Substitute .xml with .properties String baseName = input.getName().toLowerCase(); if (baseName.endsWith(".xml")) { - baseName = baseName.substring(0, baseName.length() - 4) + baseName = baseName.substring(0, baseName.length() - 4) + ".properties"; } - + File dir = fileUtils.getParentFile(fileUtils.getParentFile(input)); String goldFileFolder = "goldfiles/";