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

Processing instruction present in DTD not accepted by SAX parser

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 5.0
    • xml

      FULL PRODUCT VERSION :
      java version "1.5.0_01"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_01-b08)
      Java HotSpot(TM) Client VM (build 1.5.0_01-b08, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]


      A DESCRIPTION OF THE PROBLEM :
      There is a problem parsing XML files linking to a DTD at URL http://java.sun.com/dtd/, in the specific case http://java.sun.com/dtd/properties.dtd.

      This link is automatically inserted by the Properties.storeToXML() method.

      The parser gets a fatal error pointing to the DTD file:
      The processing instruction target matching "[xX][mM][lL]" is not allowed., row=5, col=6
      org.xml.sax.SAXParseException: The processing instruction target matching "[xX][mM][lL]" is not allowed.
      at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
      at javax.xml.parsers.SAXParser.parse(Unknown Source)
      at javax.xml.parsers.SAXParser.parse(Unknown Source)
      at lab.ExampleHandler.main(ExampleHandler.java:228)

      Comparing the DTD with DTD:s from W3C reveals that the SUN DTD contains the line :
          <?xml version="1.0" encoding="UTF-8"?>
      at line 5, which is the line for which the parser complains.

      Creating a local copy of the file with the above mentioned line removed results in a successful parsing.

      My opinion (although it may be wrong) is that the DTD file is incorrect, and this may apply to all DTD files, not only the properties.dtd file. If the <?xml directive is permitted in the DTD file then it is likely to be a bug in the parser. (Unless I have missed some extra option needed to parse the XML files)


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create XML file from properties, or use file provided in source, compile java source below and perform a test run.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      It should be possible to parse generated XML property files without any extra tricks.

      ACTUAL -
        From the provided program:
      ----
      start document
      resolveEntity:target=null, data=http://java.sun.com/dtd/properties.dtd
      fatalError:e=The processing instruction target matching "[xX][mM][lL]" is not allowed., row=5, col=6
      org.xml.sax.SAXParseException: The processing instruction target matching "[xX][mM][lL]" is not allowed.
      at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
      at javax.xml.parsers.SAXParser.parse(Unknown Source)
      at javax.xml.parsers.SAXParser.parse(Unknown Source)
      at lab.ExampleHandler2.main(ExampleHandler2.java:89)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      Create the following XML file (props.xml):
      ------
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
      <properties>
      <comment>Testing XML properties</comment>
      <entry key="str2">String 2</entry>
      <entry key="str1">String 1</entry>
      </properties>
      ------

      Use the following program to parse the file:
      ------
      package lab;
      import java.io.IOException;
      import java.util.Hashtable;

      import javax.xml.parsers.ParserConfigurationException;
      import javax.xml.parsers.SAXParser;
      import javax.xml.parsers.SAXParserFactory;

      import org.xml.sax.Attributes;
      import org.xml.sax.InputSource;
      import org.xml.sax.SAXException;
      import org.xml.sax.SAXParseException;
      import org.xml.sax.helpers.DefaultHandler;

      public class ExampleHandler2 extends DefaultHandler
      {
          private final static String padStr=" ";
          private Hashtable<String, String> h1=new Hashtable<String, String>();

          public ExampleHandler2()
          {
              super();
          }

          public void startDocument() throws SAXException
          {
              System.out.println("start document");
          }

          @Override public void startElement(String uri,
                  String localName,
                  String qName,
                  Attributes attributes) throws SAXException
          {
              int n=this.h1.size();
              String pad=padStr.substring(0,4*n);
              System.out.println(pad+"<"+qName+">");
          }

          @Override public InputSource resolveEntity(String publicId, String systemId) throws IOException, SAXException
          {
              System.out.println("resolveEntity:target="+publicId+", data="+systemId);
              return super.resolveEntity(publicId, systemId);
          }

          @Override public void fatalError(SAXParseException e) throws SAXException
          {
              System.out.println("fatalError:e="+e.getMessage()+", row="+e.getLineNumber()+", col="+e.getColumnNumber());
          }

          @Override public void endElement(String uri,
                  String localName,
                  String qName) throws SAXException
          {
              this.h1.remove(qName);
              int n=this.h1.size();
              String pad=padStr.substring(0,4*n);
              System.out.println(pad+"</"+qName+">");
              System.out.println("---");
          }

          public void endDocument() throws SAXException
          {
              System.out.println("end document");
          }

          public static void main(String[] args)
          {
              SAXParserFactory factory = SAXParserFactory.newInstance();
              SAXParser parser=null;

              try
              {
                  parser = factory.newSAXParser();
              } catch (ParserConfigurationException e)
              {
                  e.printStackTrace();
              } catch (SAXException e)
              {
                  e.printStackTrace();
              }

              if (parser != null)
              {
                  ExampleHandler2 eh=null;
                  try
                  {
                      eh=new ExampleHandler2();
                      parser.parse("props.xml", eh);
                  } catch (SAXException e)
                  {
                      e.printStackTrace();
                  } catch (IOException e)
                  {
                      e.printStackTrace();
                  }
              }
          }
      }
      ------

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

      CUSTOMER SUBMITTED WORKAROUND :
      Edit the XML file to use a local DTD without the offending <?xml line.

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: