You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Jim Clune <ji...@parasoft.com> on 2000/08/23 00:31:21 UTC

Bug report: Directory names with spaces

Summary:  Directory names with spaces are not correctly translated into
URI's,
causing parse failures.

To reproduce:
Xerces-J:  1.1.3
JDK: 1.2

Test case files:
foo.xml:
<?xml version="1.0"?>
<foo/>

SpaceBug.java:
// Begin SpaceBug.java
import java.io.*;
import javax.xml.parsers.*;
public class SpaceBug {
    public static void main(String args[]) {
        try {
            DocumentBuilderFactory factory =
                DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            builder.parse(new File("foo.xml"));
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
        System.out.println("Success!");
    }
}
// End SpaceBug.java

Test case:  Use the DOMParser to parse a file with
which references a document type of a DTD with the SYSTEM id.  Run this from
a directory which has spaces in it, such as "C:\Program files".  Run it
again in this directory.  You will get a stack trace such as:
org.xml.sax.SAXParseException: File "foo.xml" not found.
        at
org.apache.xerces.framework.XMLParser.reportError(XMLParser.java:975)

        at
org.apache.xerces.readers.DefaultEntityHandler.startReadingFromDocume
nt(DefaultEntityHandler.java:499)
        at
org.apache.xerces.framework.XMLParser.parseSomeSetup(XMLParser.java:3
03)
        at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:860)
        at
org.apache.xerces.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.
java:123)
        at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:171)
        at SpaceBug.main(SpaceBug.java:9)

Suggested (partial) solution:
It looks like DefaultEntityHandler.fixURI(String) does not properly
reconfigure path
names with spaces in them.  If you are parsing files from a directory which
has spaces in them (i.e. "C:\Program Files") you will end up with
MalformedURLExceptions because the output from fixURI is not a fixed URI.

My understanding is that spaces in directory names are to be substituted
with
"%20", a fix is to add the following immediately before "return str;" in
DefaultEntityHandler.fixURI(String):
-----------------------
       // Replace spaces with "%20".
       int spaceIndex = str.indexOf(' ');
       if (spaceIndex > 0) {
           StringBuffer buffer = new StringBuffer();
           while (spaceIndex >= 0) {
               buffer.append(str.substring(0, spaceIndex) + "%20");
               str = str.substring(spaceIndex+1);
               spaceIndex = str.indexOf(' ');
           }
           buffer.append(str);
           str = buffer.toString();
       }
-----------------------

Note: this only partially fixes the problem.  Add a dtd like this:
foo.xml:
<?xml version="1.0"?>
<!DOCTYPE foo SYSTEM "foo.dtd">
<foo/>

foo.dtd:
<!ELEMENT foo EMPTY>

Then re-run the same test case.
This causes additional problems in interpreting the %20.

Regards,

Jim Clune
jim@parasoft.com