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

ClassCastException in internal API class StAXEvent2SAX

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: 8, 9
    • Fix Version/s: None
    • Component/s: xml
    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      linux_ubuntu

      Description

      FULL PRODUCT VERSION :
      every JDK version 6,7,8,9,10

      A DESCRIPTION OF THE PROBLEM :
      Inside com.sun.org.apache.xalan.internal.xsltc.trax.StAXEvent2SAX.handleEndElement(EndElement event)

      The iterator from event.getNamespaces() returns `Namespace` objects, like in the function under it, except that the code tries to cast them to String.

      The valid example is in the `handleStartElement` function below.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      The default implementation never has namespaces on the events, so you'll have to use org.codehaus.woodstox:woodstox-core-asl:4.3.0 to detect it


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      ERROR: 'org.codehaus.stax2.ri.evt.NamespaceEventImpl cannot be cast to java.lang.String'
      javax.xml.transform.TransformerException: java.lang.ClassCastException: org.codehaus.stax2.ri.evt.NamespaceEventImpl cannot be cast to java.lang.String
      at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:746)
      at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:351)
      at javabugapplication.JavaBugApplication.main(JavaBugApplication.java:45)
      Caused by: java.lang.ClassCastException: org.codehaus.stax2.ri.evt.NamespaceEventImpl cannot be cast to java.lang.String
      at com.sun.org.apache.xalan.internal.xsltc.trax.StAXEvent2SAX.handleEndElement(StAXEvent2SAX.java:341)
      at com.sun.org.apache.xalan.internal.xsltc.trax.StAXEvent2SAX.bridge(StAXEvent2SAX.java:198)
      at com.sun.org.apache.xalan.internal.xsltc.trax.StAXEvent2SAX.parse(StAXEvent2SAX.java:115)
      at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:682)
      at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:737)
      ... 2 more
      ---------
      java.lang.ClassCastException: org.codehaus.stax2.ri.evt.NamespaceEventImpl cannot be cast to java.lang.String
      at com.sun.org.apache.xalan.internal.xsltc.trax.StAXEvent2SAX.handleEndElement(StAXEvent2SAX.java:341)
      at com.sun.org.apache.xalan.internal.xsltc.trax.StAXEvent2SAX.bridge(StAXEvent2SAX.java:198)
      at com.sun.org.apache.xalan.internal.xsltc.trax.StAXEvent2SAX.parse(StAXEvent2SAX.java:115)
      at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:682)
      at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:737)
      at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:351)
      at javabugapplication.JavaBugApplication.main(JavaBugApplication.java:45)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package javabugapplication;

      import java.io.ByteArrayInputStream;
      import java.io.InputStream;
      import java.nio.charset.StandardCharsets;
      import javax.xml.stream.XMLEventReader;
      import javax.xml.stream.XMLInputFactory;
      import javax.xml.transform.Transformer;
      import javax.xml.transform.TransformerFactory;
      import javax.xml.transform.dom.DOMResult;
      import javax.xml.transform.stax.StAXSource;

      import org.w3c.dom.Document;

      public class JavaBugApplication {

      static String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
      "<root xmlns:h=\"http://www.w3.org/TR/html4/\" xmlns:f=\"https://www.w3schools.com/furniture\">\n" +
      " <h:table>\n" +
      " <h:tr>\n" +
      " <h:td>Apples</h:td>\n" +
      " <h:td>Bananas</h:td>\n" +
      " </h:tr>\n" +
      "</h:table>\n" +
      "\n" +
      "<f:table>\n" +
      " <f:name>African Coffee Table</f:name>\n" +
      " <f:width>80</f:width>\n" +
      " <f:length>120</f:length>\n" +
      "</f:table>" +
      "</root>";
      public static void main(String[] args) {
      Document theDoc = null;
      try (InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))) {
      XMLInputFactory xif = XMLInputFactory.newInstance();
      XMLEventReader eventReader = xif.createXMLEventReader( stream );
      TransformerFactory tf = TransformerFactory.newInstance();
      StAXSource src = new StAXSource(eventReader);
      Transformer transformer = tf.newTransformer();
      DOMResult result = new DOMResult();
      transformer.transform(src, result);
      theDoc = (Document) result.getNode();
      }
      catch (Throwable t) {
      t.printStackTrace();
      }
      assert(theDoc != null);
      }

      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Using different implementation of either factory

        Attachments

          Issue Links

            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: