Example usage for javax.xml.stream XMLEventReader hasNext

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

Introduction

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

Prototype

@Override
public boolean hasNext();

Source Link

Document

Check if there are more events.

Usage

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

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

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

    assertTrue(reader.hasNext()); // START_DOC
    assertEquals(START_DOCUMENT, reader.nextEvent().getEventType());

    commonFileWrappingTest(reader);/*from ww  w .  j a va 2 s  .c o m*/

    commonEndDocumentTest(reader);
}

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

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

    final InputStream xmlIn = IOUtils.toInputStream(SINGLE_ELEMENT_DOC_WHITESPACE_2);
    final XMLEventReader reader = readerFactory.createXMLEventReader(xmlIn);

    assertTrue(reader.hasNext()); // START_DOC
    assertEquals(START_DOCUMENT, reader.nextEvent().getEventType());

    commonFilesWrappingTest(reader);/* w w  w . ja v a  2 s  . c o  m*/

    commonEndDocumentTest(reader);
}

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

private void commonFilesWrappingTest(XMLEventReader reader) throws XMLStreamException {
    assertTrue(reader.hasNext()); // START_ELEMENT Files
    assertEquals(START_ELEMENT, reader.nextEvent().getEventType());

    commonFileWrappingTest(reader);//from  ww  w  . j  a  va 2 s.co  m

    assertTrue(reader.hasNext()); // END_ELEMENT Files
    assertEquals(END_ELEMENT, reader.nextEvent().getEventType());
}

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

private void commonFileWrappingTest(XMLEventReader reader) throws XMLStreamException {
    assertTrue(reader.hasNext()); // START_ELEMENT File
    XMLEvent e = reader.nextEvent();
    assertEquals(START_ELEMENT, e.getEventType());
    assertEquals(E_FILE, e.asStartElement().getName().getLocalPart());

    assertTrue(reader.hasNext()); // START_ELEMENT doc
    e = reader.nextEvent();/*ww w  . ja  v  a2  s. c  o m*/
    assertEquals(START_ELEMENT, e.getEventType());
    assertEquals("doc", e.asStartElement().getName().getLocalPart());

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

    assertTrue(reader.hasNext()); // END_ELEMENT doc
    e = reader.nextEvent();
    assertEquals(END_ELEMENT, e.getEventType());
    assertEquals("doc", e.asEndElement().getName().getLocalPart());

    assertTrue(reader.hasNext()); // END_ELEMENT File
    e = reader.nextEvent();
    assertEquals(END_ELEMENT, e.getEventType());
    assertEquals(E_FILE, e.asEndElement().getName().getLocalPart());
}

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

private void commonEndDocumentTest(XMLEventReader reader) throws XMLStreamException {
    assertTrue(reader.hasNext());
    assertEquals(END_DOCUMENT, reader.nextEvent().getEventType());

    assertFalse(reader.hasNext());/*from  w w  w . j  a  v a  2 s  .c  o  m*/

    try {
        reader.nextEvent();
        fail("Expected NoSuchElementException to be thrown.");
    } catch (NoSuchElementException e) {
        // expected
    }
}

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

@Test(expected = UnsupportedOperationException.class)
public void testRemoveWithDefaultEventReader() throws Exception {
    final XMLEventReader reader = readerFactory
            .createXMLEventReader(IOUtils.toInputStream(SINGLE_ELEMENT_DOC_WHITESPACE));
    assertTrue(reader.hasNext());
    reader.nextEvent();/*from  ww w .  ja  v  a2 s  .c o  m*/
    reader.remove();
}

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());
    reader.nextEvent();/*from www .  j a v a 2  s  .c om*/
    reader.remove();
}

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   ww w  .  j  av a 2s.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.jonross.coercion.v2.BinderSpec.java

private Block parse(XMLEventReader reader, StartElement start) throws XMLStreamException {
    Block block = new Block(start.getName(), blockId++);
    String lastText = "";

    while (reader.hasNext()) {
        XMLEvent e = reader.nextEvent();
        switch (e.getEventType()) {
        case XMLEvent.START_ELEMENT:
            if (block.children.size() == 0)
                block.setPrologue(lastText);
            block.children.add(parse(reader, e.asStartElement()));
            lastText = "";
            break;

        case XMLEvent.CHARACTERS:
            lastText += e.asCharacters().getData();
            break;

        case XMLEvent.END_ELEMENT:
            block.setEpilogue(lastText);
            if (log.isDebugEnabled())
                log.debug("Completed " + block);
            return block;

        default:/*from  w  ww  .ja va  2s.c  om*/
            lastText = "";
            break;
        }
    }

    // Shouldn't get here, END_ELEMENT above should return.
    throw new IllegalStateException("Coercer.parse impossible state, probably malformed XML");
}

From source file:org.jonross.coercion.v2.BinderSpec.java

private StartElement nextStartElement(XMLEventReader reader) throws XMLStreamException {
    while (reader.hasNext()) {
        XMLEvent e = reader.nextEvent();
        if (e.getEventType() == XMLEvent.START_ELEMENT)
            return e.asStartElement();
    }/* www  . ja v a  2 s  .  co  m*/
    return null;
}