Browse Source

Handle relative DTDs in XMLProperty

PR:	14685
Submitted by:	Kimbo Mundy


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274907 13f79535-47bb-0310-9956-ffa450edef68
master
Conor MacNeill 22 years ago
parent
commit
3b9cd024be
5 changed files with 50 additions and 34 deletions
  1. +3
    -0
      src/etc/testcases/taskdefs/xmlproperty.xml
  2. +14
    -0
      src/etc/testcases/taskdefs/xmlproperty_data.dtd
  3. +7
    -0
      src/etc/testcases/taskdefs/xmlproperty_withdtd.xml
  4. +3
    -16
      src/main/org/apache/tools/ant/taskdefs/XmlProperty.java
  5. +23
    -18
      src/testcases/org/apache/tools/ant/taskdefs/XmlPropertyTest.java

+ 3
- 0
src/etc/testcases/taskdefs/xmlproperty.xml View File

@@ -4,4 +4,7 @@
<xmlproperty file="xmlproperty_data.xml"/>
</target>

<target name="testdtd">
<xmlproperty file="xmlproperty_withdtd.xml"/>
</target>
</project>

+ 14
- 0
src/etc/testcases/taskdefs/xmlproperty_data.dtd View File

@@ -0,0 +1,14 @@
<!ELEMENT root-tag (inner-tag, a2, cdatatag)>
<!ATTLIST root-tag myattr CDATA "">

<!ELEMENT inner-tag (#PCDATA)>
<!ATTLIST inner-tag someattr CDATA "">

<!ELEMENT a2 (a3)>

<!ELEMENT a3 (a4)>

<!ELEMENT a4 (#PCDATA)>

<!ELEMENT cdatatag (#PCDATA)>


+ 7
- 0
src/etc/testcases/taskdefs/xmlproperty_withdtd.xml View File

@@ -0,0 +1,7 @@
<!DOCTYPE root-tag SYSTEM "xmlproperty_data.dtd">

<root-tag myattr="true">
<inner-tag someattr="val">Text</inner-tag>
<a2><a3><a4>false</a4></a3></a2>
<cdatatag><![CDATA[<test>]]></cdatatag>
</root-tag>

+ 3
- 16
src/main/org/apache/tools/ant/taskdefs/XmlProperty.java View File

@@ -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) {
}
}
}
}



+ 23
- 18
src/testcases/org/apache/tools/ant/taskdefs/XmlPropertyTest.java View File

@@ -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",
"<test>", 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/";


Loading…
Cancel
Save