Skip to Content.
Sympa Menu

xom-interest - [XOM-interest] spaces in file names

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Elliotte Rusty Harold <elharo AT metalab.unc.edu>
  • To: xom-interest <xom-interest AT lists.ibiblio.org>
  • Subject: [XOM-interest] spaces in file names
  • Date: Fri, 17 Jan 2003 18:56:12 -0500

Here's my first pass at fixing the bug where File objects
for files whose names contain spaces can't be parsed:

public Document build(File in)
throws ParseException, ValidityException, IOException {

InputStream fin = new FileInputStream(in);
// Java's toURL method doesn't properly escape file
// names so we have to do it manually
String absolute = in.getAbsolutePath();
StringBuffer url = new StringBuffer("file://");
for (int i = 0; i < absolute.length(); i++) {
char c = absolute.charAt(i);
if (c == File.separatorChar) url.append('/');
else url.append(URLEncoder.encode(String.valueOf(c)));
}

String base = url.toString();
parser.setEntityResolver(new BaseRelativeResolver(base));
return build(fin, base);

}


The problem turned out to be that File.toURL() does not escape
illegal characters properly, so I had to write my own code
to create file URLs. In my initial tests this works, but I
haven't tried anything too nasty yet (like a file name that
contains Greek letters or some such.)

Anyone see any major issues? The biggest one is that this
uses the platform default encoding, but I think for a file
URL that should be OK, and probably what's needed.




  • [XOM-interest] spaces in file names, Elliotte Rusty Harold, 01/17/2003

Archive powered by MHonArc 2.6.24.

Top of Page