Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5004112

SAXParser doesn't call XMLReader.resolveEntity to resolve location of DTD file

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Won't Fix
    • Affects Version/s: 1.4.2
    • Fix Version/s: 1.4.2
    • Component/s: xml
    • Labels:

      Description

      Name: gm110360 Date: 02/26/2004


      FULL PRODUCT VERSION :
      java version "1.4.1_03"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_03-b02)
      Java HotSpot(TM) Client VM (build 1.4.1_03-b02, mixed mode)


      FULL OS VERSION :
      SunOS [hostname] 5.7 Generic_106541-11 sun4u sparc SUNW,Ultra-5_10

      A DESCRIPTION OF THE PROBLEM :
      The parse() method of class javax.xml.parsers.SAXParser is failing to use the resolveEntity() method of its embedded XMLReader object, to resolve a referenced DTD file.
      Instead, SAXParser.parse() throws a SAXParseException, indicating "Relative URI "rfml.dtd"; can not be resolved without a base URI."
      Among the various JDK's that we test to (AIX, Windows, OS/400, Linux), this bug is unique to the Solaris JDK.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See executable test case below.
      You will also need the following 2 files in your current directory (where you invoke "java SaxTest"):

      [ File "testFile.rfml": ]

      <?xml version="1.0" encoding="UTF-8" standalone="no"?>
      <!DOCTYPE rfml SYSTEM "rfml.dtd">
      <rfml version="1.0">
      <recordformat name="format1"/>
      </rfml>



      [ File "rfml.dtd": ]

      ELEMENT rfml (recordformat)+
      ATTLIST rfml
                version CDATA #FIXED "4.0"


      ELEMENT recordformat (data)*
      ATTLIST recordformat
                name ID #REQUIRED
                description CDATA #IMPLIED


      ELEMENT data EMPTY
      ATTLIST data
                name CDATA #REQUIRED
                length CDATA #IMPLIED



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The testcase should run to successful completion, with the message "The parse succeeded."
      ACTUAL -
      The testcase fails with a SAXParseException. See "Error Messages" section below.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      $ java SaxTest
      org.xml.sax.SAXParseException: Relative URI "rfml.dtd"; can not be resolved without a base URI.
              at org.apache.crimson.parser.Parser2.fatal(Parser2.java:3232)
              at org.apache.crimson.parser.Parser2.fatal(Parser2.java:3226)
              at org.apache.crimson.parser.Parser2.resolveURI(Parser2.java:2808)
              at org.apache.crimson.parser.Parser2.maybeExternalID(Parser2.java:2780)
              at org.apache.crimson.parser.Parser2.maybeDoctypeDecl(Parser2.java:1174)

              at org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:523)
              at org.apache.crimson.parser.Parser2.parse(Parser2.java:318)
              at org.apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:442)

              at javax.xml.parsers.SAXParser.parse(SAXParser.java:345)
              at SaxTest.main(SaxTest.java:40)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.xml.parsers.SAXParserFactory;
      import javax.xml.parsers.SAXParser;
      import org.xml.sax.EntityResolver;
      import org.xml.sax.helpers.DefaultHandler;
      import org.xml.sax.InputSource;
      import org.xml.sax.SAXException;
      import org.xml.sax.SAXParseException;
      import org.xml.sax.XMLReader;
      import java.io.FileInputStream;
      import java.io.FileNotFoundException;

      public class SaxTest extends DefaultHandler implements EntityResolver {

        public SaxTest() {}
        public static void main ( String args[] ) throws Exception {

          String fileName = "testFile.rfml";
          FileInputStream inStream = new FileInputStream(fileName);
          SaxTest tester = new SaxTest();

          SAXParserFactory factory = SAXParserFactory.newInstance();
          factory.setValidating(true);
          factory.setNamespaceAware(false);
          SAXParser parser = factory.newSAXParser();

          try {
            XMLReader reader = parser.getXMLReader();
            reader.setEntityResolver(tester);
            parser.parse(new InputSource(inStream), tester);
            System.out.println("The parse succeeded.");
          }
          catch (SAXException e) {
            e.printStackTrace();
          }

          System.exit(0);
        }

        public InputSource resolveEntity (String publicId, String systemId)
        throws SAXException
        {
          System.out.println("DEBUG RfmlSAXParser.resolveEntity(|"+publicId+"|, |"+systemId+"|)");
          if (systemId != null && systemId.length() > 0)
          {
            int finalSlashPos = systemId.lastIndexOf("/");
            String sysFile;

            if (finalSlashPos == -1) {
              sysFile = systemId;
            }
            else {
              sysFile = systemId.substring(finalSlashPos+1);
            }

            if (sysFile.equals("rfml.dtd"))
            {
              System.out.println("DEBUG RfmlSAXParser.resolveEntity(|"+publicId+"|, |"+systemId+"|) : Returning an InputSource.");
              FileInputStream inStream = null;
              try {
                inStream = new FileInputStream("./rfml.dtd");
              }
              catch (FileNotFoundException e) { e.printStackTrace(); }
              return new InputSource(inStream);
            }
          }

          return super.resolveEntity(publicId, systemId);
        }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use a different XML parser (e.g. the Xerces parser).
      (Incident Review ID: 192267)
      ======================================================================
      ###@###.### 2004-03-03

        Attachments

          Activity

            People

            Assignee:
            jsuttorsunw Jeff Suttor (Inactive)
            Reporter:
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: