From 299a7d5929bc956bf865bb1c86e5bfce49ca7df1 Mon Sep 17 00:00:00 2001 From: Antoine Levy-Lambert Date: Tue, 9 Dec 2003 01:14:31 +0000 Subject: [PATCH] Fix URIs for DTDs entered with a full path location under Windows PR: 23913 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@275759 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/ant/types/XMLCatalog.java | 21 +++++++++++++++-- .../tools/ant/types/XMLCatalogTest.java | 23 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/main/org/apache/tools/ant/types/XMLCatalog.java b/src/main/org/apache/tools/ant/types/XMLCatalog.java index 39cb4ddf5..7626bb720 100644 --- a/src/main/org/apache/tools/ant/types/XMLCatalog.java +++ b/src/main/org/apache/tools/ant/types/XMLCatalog.java @@ -74,6 +74,7 @@ import javax.xml.transform.sax.SAXSource; import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; +import org.apache.tools.ant.taskdefs.condition.Os; import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.JAXPUtils; import org.xml.sax.EntityResolver; @@ -678,6 +679,8 @@ public class XMLCatalog extends DataType private InputSource filesystemLookup(ResourceLocation matchingEntry) { String uri = matchingEntry.getLocation(); + // the following line seems to be necessary on Windows under JDK 1.2 + uri = uri.replace(File.separatorChar, '/'); URL baseURL = null; // @@ -697,11 +700,25 @@ public class XMLCatalog extends DataType InputSource source = null; URL url = null; - try { url = new URL(baseURL, uri); } catch (MalformedURLException ex) { - // ignore + // this processing is useful under Windows when the location of the DTD has been given as an absolute path + // see Bugzilla Report 23913 + File testFile = new File(uri); + if (testFile.exists() && testFile.canRead()) { + log("uri : '" + + uri + "' matches a readable file", Project.MSG_DEBUG); + try { + url = fileUtils.getFileURL(testFile); + } catch (MalformedURLException ex1) { + throw new BuildException("could not find an URL for :" + testFile.getAbsolutePath()); + } + } else { + log("uri : '" + + uri + "' does not match a readable file", Project.MSG_DEBUG); + + } } if (url != null) { diff --git a/src/testcases/org/apache/tools/ant/types/XMLCatalogTest.java b/src/testcases/org/apache/tools/ant/types/XMLCatalogTest.java index 26fe950f6..dcdee7c5b 100644 --- a/src/testcases/org/apache/tools/ant/types/XMLCatalogTest.java +++ b/src/testcases/org/apache/tools/ant/types/XMLCatalogTest.java @@ -238,6 +238,29 @@ public class XMLCatalogTest extends TestCase { fail("resolveEntity() failed!" + e.toString()); } } + // inspired by Bugzilla Report 23913 + // a problem used to happen under Windows when the location of the DTD was given as an absolute path + // possibly with a mixture of file separators + public void testAbsolutePath() { + ResourceLocation dtd = new ResourceLocation(); + dtd.setPublicId("-//stevo//DTD doc 1.0//EN"); + + String sysid = System.getProperty("user.dir") + File.separator + "src/etc/testcases/taskdefs/optional/xml/doc.dtd"; + dtd.setLocation(sysid); + catalog.addDTD(dtd); + File dtdFile = project.resolveFile(sysid); + + try { + InputSource result = catalog.resolveEntity("-//stevo//DTD doc 1.0//EN", + "nap:chemical+brothers"); + assertNotNull(result); + assertEquals(toURLString(dtdFile), + result.getSystemId()); + } catch (Exception e) { + fail("resolveEntity() failed!" + e.toString()); + } + + } public void testSimpleEntry() {