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

Errors in XSLT stylesheet are not dispatched correctly to ErrorListener

    XMLWordPrintable

    Details

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0_66"
      Java(TM) SE Runtime Environment (build 1.8.0_66-b18)
      Java HotSpot(TM) 64-Bit Server VM (build 25.66-b18, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 10.0.10586]

      A DESCRIPTION OF THE PROBLEM :
      When I create a Transformer from an invalid XSLT stylesheet, I do not expect that output is sent directly to the console. Errors should only reported through the registered ErrorListener implementation.

      I ran the following example program:

      import java.io.ByteArrayInputStream;
      import java.io.InputStream;

      import javax.xml.transform.ErrorListener;
      import javax.xml.transform.TransformerConfigurationException;
      import javax.xml.transform.TransformerException;
      import javax.xml.transform.TransformerFactory;
      import javax.xml.transform.sax.SAXSource;

      import org.xml.sax.InputSource;


      public class Main implements ErrorListener
      {
          static final private String INVALID_STYLESHEET = "xxx";
          static final private String SYSTEM_ID = "http://myurl.com/myDirectory/myFile.xsl";

          static public void main(String[] argv) {
              new Main().run();
          }

          void run() {
              System.out.println("This tool reveals JDK errors related to XSLT parsing");
              InputStream is = new ByteArrayInputStream(INVALID_STYLESHEET.getBytes());
              InputSource source = new InputSource(is);
              source.setSystemId(SYSTEM_ID);

              TransformerFactory factory = TransformerFactory.newInstance();
              factory.setErrorListener(this);
              try {
                  factory.newTransformer(new SAXSource(source));
              }
              catch(TransformerConfigurationException e) {
                  System.out.println("We catch a final TransformerConfigurationException");
              }
          }

          @Override
          public void error(TransformerException exception)
                  throws TransformerException
          {
              System.out.println("Correctly handled error: " + exception.getMessage());
          }

          @Override
          public void fatalError(TransformerException exception)
                  throws TransformerException
          {
              System.out.println("Correctly handled fatal: " + exception.getMessage());
          }

          @Override
          public void warning(TransformerException exception)
                  throws TransformerException
          {
              System.out.println("Correctly handled warning: " + exception.getMessage());
          }
      }

      The output is:

      This tool reveals JDK errors related to XSLT parsing
      [Fatal Error] myFile.xsl:1:1: Content is not allowed in prolog.
      Correctly handled error: Could not compile stylesheet
      Correctly handled fatal: Content is not allowed in prolog.
      We catch a final TransformerConfigurationException

      The second line, which starts with "[Fatal Error]" is written by JAXP and does not come from my ErrorListener implementation. I want control over the output I show to my users and therefore this extra output is a problem for me.

      One last note: I observed this problem in my own project, but I found a post on the internet (probably stackoverflow) that confirmed that this is a JDK problem. I hope the discoverer of this bug does not feel offended by me submitting it.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the program listed in the description.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      This tool reveals JDK errors related to XSLT parsing
      Correctly handled error: Could not compile stylesheet
      Correctly handled fatal: Content is not allowed in prolog.

      ACTUAL -
      This tool reveals JDK errors related to XSLT parsing
      [Fatal Error] myFile.xsl:1:1: Content is not allowed in prolog.
      Correctly handled error: Could not compile stylesheet
      Correctly handled fatal: Content is not allowed in prolog.
      We catch a final TransformerConfigurationException


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.ByteArrayInputStream;
      import java.io.InputStream;

      import javax.xml.transform.ErrorListener;
      import javax.xml.transform.TransformerConfigurationException;
      import javax.xml.transform.TransformerException;
      import javax.xml.transform.TransformerFactory;
      import javax.xml.transform.sax.SAXSource;

      import org.xml.sax.InputSource;


      public class Main implements ErrorListener
      {
          static final private String INVALID_STYLESHEET = "xxx";
          static final private String SYSTEM_ID = "http://myurl.com/myDirectory/myFile.xsl";

          static public void main(String[] argv) {
              new Main().run();
          }

          void run() {
              System.out.println("This tool reveals JDK errors related to XSLT parsing");
              InputStream is = new ByteArrayInputStream(INVALID_STYLESHEET.getBytes());
              InputSource source = new InputSource(is);
              source.setSystemId(SYSTEM_ID);

              TransformerFactory factory = TransformerFactory.newInstance();
              factory.setErrorListener(this);
              try {
                  factory.newTransformer(new SAXSource(source));
              }
              catch(TransformerConfigurationException e) {
                  System.out.println("We catch a final TransformerConfigurationException");
              }
          }

          @Override
          public void error(TransformerException exception)
                  throws TransformerException
          {
              System.out.println("Correctly handled error: " + exception.getMessage());
          }

          @Override
          public void fatalError(TransformerException exception)
                  throws TransformerException
          {
              System.out.println("Correctly handled fatal: " + exception.getMessage());
          }

          @Override
          public void warning(TransformerException exception)
                  throws TransformerException
          {
              System.out.println("Correctly handled warning: " + exception.getMessage());
          }
      }

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

        Attachments

          Activity

            People

            Assignee:
            joehw Joe Wang
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: