Example usage for javax.xml.stream XMLEventReader nextEvent

List of usage examples for javax.xml.stream XMLEventReader nextEvent

Introduction

In this page you can find the example usage for javax.xml.stream XMLEventReader nextEvent.

Prototype

public XMLEvent nextEvent() throws XMLStreamException;

Source Link

Document

Gets the next XMLEvent.

Usage

From source file:org.dataconservancy.model.builder.WrappingEventReaderTest.java

@Test(expected = UnsupportedOperationException.class)
public void testRemoveWithWrapperEventReader() throws Exception {
    final XMLEventReader reader = new WrappingEventReader(
            readerFactory.createXMLEventReader(IOUtils.toInputStream(SINGLE_ELEMENT_DOC_WHITESPACE)));
    assertTrue(reader.hasNext());/*from   w w  w  .  j  a va  2s .  c  o  m*/
    reader.nextEvent();
    reader.remove();
}

From source file:org.dataconservancy.model.builder.WrappingEventReaderTest.java

@Test
public void testPeekWithDefaultEventReader() throws Exception {
    // The input XML is:
    // <File><doc> </doc></File>

    final XMLEventReader reader = readerFactory
            .createXMLEventReader(IOUtils.toInputStream(SINGLE_ELEMENT_DOC_WHITESPACE_1));

    // START_DOCUMENT
    assertEquals(START_DOCUMENT, reader.peek().getEventType());
    reader.nextEvent();

    commonPeekFileTest(reader);//from   w ww .  j a  v a 2  s  .c o m

    // END_DOCUMENT
    assertEquals(END_DOCUMENT, reader.peek().getEventType());

    commonEndDocumentTest(reader);
}

From source file:org.dataconservancy.model.builder.WrappingEventReaderTest.java

@Test
public void testPeekWithWrapperEventReader() throws Exception {
    // The input XML is:
    // <File><doc> </doc></File>
    // But the wrapper should wrap it to:
    // <dcp xmlns="http://dataconservancy.org/schemas/dcm/1.0"><Files><File><doc> </doc></File></Files></dcp>

    final XMLEventReader reader = new WrappingEventReader(
            readerFactory.createXMLEventReader(IOUtils.toInputStream(SINGLE_ELEMENT_DOC_WHITESPACE_1)));

    // START_DOCUMENT
    assertEquals(START_DOCUMENT, reader.peek().getEventType());
    // idempotent
    assertEquals(START_DOCUMENT, reader.peek().getEventType());
    assertEquals(START_DOCUMENT, reader.nextEvent().getEventType());

    // <dcp>
    assertEquals(START_ELEMENT, reader.peek().getEventType());
    // idempotent
    assertEquals(START_ELEMENT, reader.peek().getEventType());
    assertEquals(START_ELEMENT, reader.nextEvent().getEventType());

    // <Files>
    assertEquals(START_ELEMENT, reader.peek().getEventType());
    // idempotent
    assertEquals(START_ELEMENT, reader.peek().getEventType());
    assertEquals(START_ELEMENT, reader.nextEvent().getEventType());

    commonPeekFileTest(reader);/*  w w w  . j  a  va2s.  c om*/

    // </Files>
    assertEquals(END_ELEMENT, reader.peek().getEventType());
    // idempotent
    assertEquals(END_ELEMENT, reader.peek().getEventType());
    assertEquals(END_ELEMENT, reader.nextEvent().getEventType());

    // </dcp>
    assertEquals(END_ELEMENT, reader.peek().getEventType());
    // idempotent
    assertEquals(END_ELEMENT, reader.peek().getEventType());
    assertEquals(END_ELEMENT, reader.nextEvent().getEventType());

    // END_DOCUMENT
    assertEquals(END_DOCUMENT, reader.peek().getEventType());
    // idempotent
    assertEquals(END_DOCUMENT, reader.peek().getEventType());

    commonEndDocumentTest(reader);
}

From source file:org.dataconservancy.model.builder.WrappingEventReaderTest.java

private void commonPeekFileTest(XMLEventReader reader) throws XMLStreamException {
    // The input XML is:
    // <File><doc> </doc></File>

    // <File>
    XMLEvent peekEvent = reader.peek();
    assertEquals(START_ELEMENT, peekEvent.getEventType());
    assertEquals(E_FILE, peekEvent.asStartElement().getName().getLocalPart());
    // idempotent
    assertEquals(peekEvent, reader.peek());
    XMLEvent actualEvent = reader.nextEvent();
    assertEquals(peekEvent, actualEvent);

    // <doc>
    peekEvent = reader.peek();/*from ww w  .j ava  2 s  .  c o  m*/
    assertEquals(START_ELEMENT, peekEvent.getEventType());
    assertEquals("doc", peekEvent.asStartElement().getName().getLocalPart());
    // idempotent
    assertEquals(peekEvent, reader.peek());
    actualEvent = reader.nextEvent();
    assertEquals(peekEvent, actualEvent);

    peekEvent = reader.peek();
    assertEquals(CHARACTERS, peekEvent.getEventType());
    // idempotent
    assertEquals(peekEvent, reader.peek());
    actualEvent = reader.nextEvent();
    assertEquals(peekEvent, actualEvent);

    // </doc>
    peekEvent = reader.peek();
    assertEquals(END_ELEMENT, peekEvent.getEventType());
    assertEquals("doc", peekEvent.asEndElement().getName().getLocalPart());
    // idempotent
    assertEquals(peekEvent, reader.peek());
    actualEvent = reader.nextEvent();
    assertEquals(peekEvent, actualEvent);

    // </File>
    peekEvent = reader.peek();
    assertEquals(END_ELEMENT, peekEvent.getEventType());
    assertEquals(E_FILE, peekEvent.asEndElement().getName().getLocalPart());
    // idempotent
    assertEquals(peekEvent, reader.peek());
    actualEvent = reader.nextEvent();
    // fails, bc we are preserving namespaces? assertEquals(peekEvent, actualEvent);
    // do this instead
    assertEquals(peekEvent.asEndElement().getName().getLocalPart(),
            actualEvent.asEndElement().getName().getLocalPart());
}

From source file:org.dataconservancy.model.builder.WrappingEventReaderTest.java

private void commonStartDocumentNextTagTest(XMLEventReader reader) throws XMLStreamException {
    try {//from w w w. j ava  2  s  . c  o  m
        reader.nextTag();
        fail("Expected XMLStreamException");
    } catch (XMLStreamException e) {
        // expected
    }

    // bug in default impl?  I would expect a call to nextTag() to not change the
    // state in the underlying reader if an exception is thrown.
    //assertEquals(START_DOCUMENT, reader.nextEvent().getEventType());

    // but instead we have this behavior
    assertEquals(START_ELEMENT, reader.nextEvent().getEventType());
}

From source file:org.dataconservancy.model.builder.WrappingEventReaderTest.java

private void commonCloseTest(XMLEventReader reader) throws XMLStreamException {
    // The input XML is EITHER:
    // <File><doc> </doc></File>
    // OR/*from ww w.ja  va2 s  . c  om*/
    // <dcp xmlns="http://dataconservancy.org/schemas/dcm/1.0"><Files><File><doc> </doc></File></Files></dcp>
    // This test works with either case.
    reader.nextEvent(); // START_DOC
    reader.nextEvent(); // START_ELEMENT

    reader.close(); // no-op

    reader.nextEvent(); // START_ELEMENT
}

From source file:org.dataconservancy.model.builder.WrappingEventReaderTest.java

@Test
public void testDeliverableUnitWithWrappingEventReader() throws XMLStreamException {
    final XMLEventReader reader = new WrappingEventReader(
            readerFactory.createXMLEventReader(IOUtils.toInputStream(DU_XML)));

    XMLEvent e = reader.nextEvent();
    assertEquals(START_DOCUMENT, e.getEventType());

    e = reader.nextEvent();/*w w  w .j  ava 2  s. c o m*/
    assertEquals(COMMENT, e.getEventType());

    e = reader.nextEvent();
    assertEquals(START_ELEMENT, e.getEventType());
    assertEquals(Q_DCP, e.asStartElement().getName());

    e = reader.nextEvent();
    assertEquals(START_ELEMENT, e.getEventType());
    assertEquals(Q_DUS, e.asStartElement().getName());

    e = reader.nextEvent();
    assertEquals(START_ELEMENT, e.getEventType());
    assertEquals(Q_DU, e.asStartElement().getName());

    e = reader.nextEvent();
    assertEquals(CHARACTERS, e.getEventType());

    e = reader.nextEvent();
    assertEquals(START_ELEMENT, e.getEventType());
    assertEquals(new QName(NS_URI, "collection"), e.asStartElement().getName());

    // Skip to the <metadata> element
    while ((e = reader.nextTag()) != null) {
        if (e.isStartElement() && e.asStartElement().getName().getLocalPart().equals("metadata")) {
            break;
        }
    }

    e = reader.nextTag();
    assertEquals(START_ELEMENT, e.getEventType());
    assertEquals(new QName("http://www.amazon.com", "md"), e.asStartElement().getName());

    e = reader.nextTag();
    assertEquals(START_ELEMENT, e.getEventType());
    assertEquals(new QName("http://www.amazon.com", "origReleaseDate"), e.asStartElement().getName());
}

From source file:org.eclipse.smila.utils.xml.stax.XmlSnippetSplitter.java

/**
 * Parse for xml snippets. If a snippet is found the registered XmlSnippethandler is called.
 * //from  w  ww. ja  v  a2  s  .  c  o m
 * @param eventReader
 *          the XMLEventReader
 * @throws XMLStreamException
 *           StAX error.
 */
private void parse(final XMLEventReader eventReader) throws XMLStreamException {
    ByteArrayOutputStream outputStream = null;
    XMLEventWriter eventWriter = null;
    try {
        while (eventReader.hasNext()) {
            final XMLEvent event = eventReader.nextEvent();
            if (isSnippetBegin(event)) {
                // begin of snippet
                outputStream = new ByteArrayOutputStream();
                eventWriter = _outputFactory.createXMLEventWriter(outputStream);
                eventWriter.add(event);
            } else if (isSnippetEnd(event)) {
                // end of snippet
                if (eventWriter != null) {
                    eventWriter.add(event);
                    eventWriter.close();
                    _snippetHandler.handleSnippet(outputStream.toByteArray());
                    // reset eventWriter and outputStream
                    eventWriter = null;
                    outputStream = null;
                } // if
            } else if (eventWriter != null) {
                eventWriter.add(event);
            }
        } // while
    } finally {
        if (eventWriter != null) {
            eventWriter.close();
        }
    }
}

From source file:org.emonocot.job.io.StaxEventItemReader.java

/**
 * Responsible for moving the cursor before the StartElement of the fragment
 * root./*  ww  w . ja v  a 2  s .  co m*/
 *
 * This implementation simply looks for the next corresponding element, it
 * does not care about element nesting. You will need to override this
 * method to correctly handle composite fragments.
 * @param xmlEventReader Set the XML event reader
 * @return <code>true</code> if next fragment was found, <code>false</code>
 *         otherwise.
 */
protected final boolean moveCursorToNextFragment(final XMLEventReader xmlEventReader) {
    try {
        while (true) {
            while (xmlEventReader.peek() != null && !xmlEventReader.peek().isStartElement()) {
                xmlEventReader.nextEvent();
            }
            if (xmlEventReader.peek() == null) {
                return false;
            }
            QName startElementName = ((StartElement) xmlEventReader.peek()).getName();
            if (startElementName.getLocalPart().equals(fragmentRootElementName)) {
                if (fragmentRootElementNameSpace == null
                        || startElementName.getNamespaceURI().equals(fragmentRootElementNameSpace)) {
                    return true;
                }
            }
            xmlEventReader.nextEvent();

        }
    } catch (XMLStreamException e) {
        throw new DataAccessResourceFailureException("Error while reading from event reader", e);
    }
}

From source file:org.geoserver.backuprestore.reader.CatalogFileReader.java

/**
 * Responsible for moving the cursor before the StartElement of the fragment root.
 * /*from w w w. ja va  2s .c  o  m*/
 * This implementation simply looks for the next corresponding element, it does not care about element nesting. You will need to override this
 * method to correctly handle composite fragments.
 * 
 * @return <code>true</code> if next fragment was found, <code>false</code> otherwise.
 * 
 * @throws NonTransientResourceException if the cursor could not be moved. This will be treated as fatal and subsequent calls to read will return
 *         null.
 */
protected boolean moveCursorToNextFragment(XMLEventReader reader) {
    try {
        while (true) {
            while (reader.peek() != null && !reader.peek().isStartElement()) {
                reader.nextEvent();
            }
            if (reader.peek() == null) {
                return false;
            }
            QName startElementName = ((StartElement) reader.peek()).getName();
            if (isFragmentRootElementName(startElementName)) {
                return true;
            }
            reader.nextEvent();
        }
    } catch (XMLStreamException e) {
        return logValidationExceptions((T) null,
                new NonTransientResourceException("Error while reading from event reader", e));
    }
}