encoding problem fixed

James Tauber jtauber at jtauber.com
Fri Jul 30 03:47:10 BST 1999


I fixed the encoding problem after trying this morning an idea I had last
night while trying to sleep (isn't it always the way).

In short, I changed in my CommandLine (the class run when FOP takes a FOT
file as input) code the line:

   SAXProcessor fop = new SAXProcessor(parser, new InputSource(new
FileReader(args[0])));

to the line:

   SAXProcessor fop = new SAXProcessor(parser,
com.jclark.xsl.sax.Driver.fileInputSource(args[0]));

In other words, rather than creating an InputSource using a FileReader, I
used James Clark's "fileInputSource" method in XT to make a URL out of a
file and create the InputSource from the URL string.

I've included James Clark's code below.

Ironically, using exactly the same code in my XTCommandLine (the class run
when FOP takes XML+XSL and calls XT from within) code a few weeks ago fixed
a completely different bug.

I now think I realise the error of my ways: I used java.io.FileReader. To
quote Java in a Nutshell "If you want to read Unicode characters from a file
that uses some encoding other than the default encoding for the locale, you
must explicitly create your own InputStreamReader to perform the
byte-to-character conversion".

The culprit is FileReader. It is the one doing the strange "read UTF-8 as
Windows code page".

James Tauber



------
Code extract from com.jclark.xsl.sax.Driver:

  /**
   * Generates an <code>InputSource</code> from a file name.
   */
  static public InputSource fileInputSource(String str) {
    return fileInputSource(new File(str));
  }

  static public InputSource fileInputSource(File file) {
    String path = file.getAbsolutePath();
    String fSep = System.getProperty("file.separator");
    if (fSep != null && fSep.length() == 1)
      path = path.replace(fSep.charAt(0), '/');
    if (path.length() > 0 && path.charAt(0) != '/')
      path = '/' + path;
    try {
      return new InputSource(new URL("file", null, path).toString());
    }
    catch (java.net.MalformedURLException e) {
      /* According to the spec this could only happen if the file
  protocol were not recognized. */
      throw new Error("unexpected MalformedURLException");
    }
  }



xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev at ic.ac.uk
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/ and on CD-ROM/ISBN 981-02-3594-1
To (un)subscribe, mailto:majordomo at ic.ac.uk the following message;
(un)subscribe xml-dev
To subscribe to the digests, mailto:majordomo at ic.ac.uk the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa at ic.ac.uk)





More information about the Xml-dev mailing list