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

StAX peek() with EventFilter advances wrapped XMLEventReader

    Details

      Description

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

      ADDITIONAL OS VERSION INFORMATION :
      Darwin air.local 16.4.0 Darwin Kernel Version 16.4.0: Thu Dec 22 22:53:21 PST 2016; root:xnu-3789.41.3~3/RELEASE_X86_64 x86_64

      A DESCRIPTION OF THE PROBLEM :
      When a filtered XMLEventReader is used to look ahead with the peek method and the next matching element is farther than one away the state of the underlying reader is advanced, such that it is positioned right before the element returned by the call to peek.

      This might be an ambiguity in the spec. It is implemented differently on IBM JDK, where the reader position is not altered by a call to peek.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      javac StaxFilterPeekTest.java && java -ea StaxFilterPeekTest

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      expected output:
      a
      c
      a

      rational:
      a call to peek() should not advance the reader
      ACTUAL -
      a
      c
      c
      Exception in thread "main" java.lang.AssertionError
      at StaxFilterPeekTest.main(StaxFilterPeekTest.java:26)

      explanation:
      the call to peek() on the filtered reader caused the wrapped reader to be advanced

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.xml.stream.EventFilter;
      import javax.xml.stream.XMLEventReader;
      import javax.xml.stream.XMLInputFactory;
      import javax.xml.stream.events.XMLEvent;

      public class StaxFilterPeekTest {
          public static void main(String[] args) throws Exception {
              XMLInputFactory staxFactory = XMLInputFactory.newInstance();
              XMLEventReader staxReader = staxFactory.createXMLEventReader(new java.io.StringReader("<a><b/><c/></a>"));
              EventFilter eventFilter = event -> event.isStartElement() && event.asStartElement().getName().getLocalPart().equals("c");
              XMLEventReader staxFilteredReader = staxFactory.createFilteredReader(staxReader, eventFilter);

              XMLEvent nextEvent = staxReader.nextEvent();
              assert nextEvent.isStartDocument();

              XMLEvent peek1 = staxReader.peek();
              System.out.println(peek1.asStartElement().getName());
              assert peek1.asStartElement().getName().getLocalPart().equals("a");

              XMLEvent filteredPeek = staxFilteredReader.peek();
              System.out.println(filteredPeek.asStartElement().getName());
              assert filteredPeek.asStartElement().getName().getLocalPart().equals("c");

              XMLEvent peek2 = staxReader.peek();
              System.out.println(peek2.asStartElement().getName());
              assert peek2.asStartElement().getName().getLocalPart().equals("a");
          }
      }
      ---------- END SOURCE ----------

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                • Created:
                  Updated: