Example usage for javax.xml.stream XMLEventReader nextTag

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

Introduction

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

Prototype

public XMLEvent nextTag() throws XMLStreamException;

Source Link

Document

Skips any insignificant space events until a START_ELEMENT or END_ELEMENT is reached.

Usage

From source file:Main.java

/** 
 * Calls xml.nextTag() and unchecks the exception.
 * //w w w .  ja  va 2  s  . co  m
 * @param xml
 * @return xml.nextTag(), or throws a RuntimeException.
 */
public static XMLEvent nextTag(XMLEventReader xml) {
    try {
        return xml.nextTag();
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Reads the next tag and throws an exception if the next tag out of the 
 * reader is not an end tag corresponding to the given start tag. 
 * /*from   ww w.  j a va2s .  co  m*/
 * @param start
 * @param xml
 */
public static void assertClosedTag(StartElement start, XMLEventReader xml) {
    try {
        assertEnd(localNameOf(start), xml.nextTag());
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }
}

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

@Test
@Ignore("FIXME: currently failing: java.lang.ClassCastException: com.sun.xml.internal.stream.events.StartElementEvent cannot be cast to javax.xml.stream.events.EndElement "
        + "at org.dataconservancy.model.builder.WrappingEventReaderTest.testNextTagWithWrappingEventReader(WrappingEventReaderTest.java:460)")
public void testNextTagWithWrappingEventReader() 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)));

    commonStartDocumentNextTagTest(reader);

    // <dcp>
    //        assertEquals(START_ELEMENT, reader.nextTag().getEventType());

    // <Files>
    final XMLEvent filesStartEvent = reader.nextTag();
    assertEquals(Q_FILES, ((StartElement) filesStartEvent).getName());
    assertEquals(START_ELEMENT, filesStartEvent.getEventType());

    // <File>
    final XMLEvent fileStartEvent = reader.nextTag();
    assertEquals(Q_FILE.getLocalPart(), ((StartElement) fileStartEvent).getName().getLocalPart());
    assertEquals(START_ELEMENT, fileStartEvent.getEventType());

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

    // </Files>
    final XMLEvent filesEndEvent = reader.nextTag();
    assertEquals(Q_FILES, ((EndElement) filesEndEvent).getName());
    assertEquals(END_ELEMENT, filesEndEvent.getEventType());

    // </dcp>
    final XMLEvent dcpEndEvent = reader.nextTag();
    assertEquals(Q_DCP, ((EndElement) dcpEndEvent).getName());
    assertEquals(END_ELEMENT, dcpEndEvent.getEventType());

    commonEndDocumentNextTagTest(reader);
}

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

private void commonStartDocumentNextTagTest(XMLEventReader reader) throws XMLStreamException {
    try {/*from w w  w  .  j av  a 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 commonEndDocumentNextTagTest(XMLEventReader reader) {
    try {//from  ww  w . java2 s .co m
        reader.nextTag();
        fail("Expected XMLStreamException");
    } catch (XMLStreamException e) {
        // expected
    }
}

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

private void commonNextTagTest(XMLEventReader reader) throws XMLStreamException {
    // <File>
    //        assertEquals(START_ELEMENT, reader.nextTag().getEventType());
    // <doc>
    assertEquals(START_ELEMENT, reader.nextTag().getEventType());

    // </doc>
    assertEquals(END_ELEMENT, reader.nextTag().getEventType());
    // </File>
    assertEquals(END_ELEMENT, reader.nextTag().getEventType());
}

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();//from   w  w  w .  j  av  a2  s.  com
    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.qi4j.valueserialization.stax.StaxValueDeserializer.java

@Override
protected Object readPlainValue(XMLEventReader input) throws Exception {
    if (!input.hasNext()) {
        return null;
    }//from  w ww  . j  a v a2 s  . co  m
    XMLEvent nextEvent = input.nextEvent();
    if (nextEvent.getEventType() == XMLEvent.START_ELEMENT
            && "null".equals(nextEvent.asStartElement().getName().getLocalPart())) {
        input.nextTag();
        return null;
    }
    if (nextEvent.getEventType() != XMLEvent.CHARACTERS) {
        throw new ValueSerializationException("Expected characters but got: " + nextEvent);
    }
    String stringValue = nextEvent.asCharacters().getData();
    return detectAndConvertStringValue(stringValue);
}

From source file:org.qi4j.valueserialization.stax.StaxValueDeserializer.java

@Override
protected <T> Collection<T> readArrayInCollection(XMLEventReader input,
        Function<XMLEventReader, T> deserializer, Collection<T> collection) throws Exception {
    if (!input.hasNext()) {
        return null;
    }/*from w w  w .j av a2s .com*/
    XMLEvent nextTag = input.nextTag();
    if (nextTag.isStartElement() && "null".equals(nextTag.asStartElement().getName().getLocalPart())) {
        input.nextTag();
        return null;
    }
    if (!nextTag.isStartElement() || !"array".equals(nextTag.asStartElement().getName().getLocalPart())) {
        throw new ValueSerializationException("Expected an <array/> but got: " + nextTag);
    }
    WHILE: while (input.hasNext()) {
        XMLEvent currentTag = input.nextTag();
        if (currentTag.isEndElement()) {
            String endElementName = currentTag.asEndElement().getName().getLocalPart();
            switch (endElementName) {
            case "array":
                break WHILE;
            case "value":
                continue;
            }
        }
        if (!"value".equals(currentTag.asStartElement().getName().getLocalPart())) {
            throw new ValueSerializationException("Expected a <value/> but got: " + currentTag);
        }
        T item = deserializer.map(input);
        collection.add(item);
    }
    return collection;
}

From source file:org.qi4j.valueserialization.stax.StaxValueDeserializer.java

@Override
protected <K, V> Map<K, V> readMapInMap(XMLEventReader input, Function<XMLEventReader, K> keyDeserializer,
        Function<XMLEventReader, V> valueDeserializer, Map<K, V> map) throws Exception {
    if (!input.hasNext()) {
        return null;
    }/*w  ww . jav  a2s .c o  m*/
    XMLEvent nextTag = input.nextTag();
    if (nextTag.isStartElement() && "null".equals(nextTag.asStartElement().getName().getLocalPart())) {
        input.nextTag();
        return null;
    }
    if (!nextTag.isStartElement() || !"array".equals(nextTag.asStartElement().getName().getLocalPart())) {
        throw new ValueSerializationException("Expected an <array/> but got: " + nextTag);
    }
    XMLEvent currentTag = input.nextTag(); // <object>
    while (!currentTag.isEndElement() || !"array".equals(currentTag.asEndElement().getName().getLocalPart())) {
        if (!currentTag.isStartElement()
                || !"object".equals(currentTag.asStartElement().getName().getLocalPart())) {
            throw new ValueSerializationException("Expected an <object/> but got: " + nextTag);
        }
        currentTag = input.nextTag(); // <field>
        K key = null;
        V value = null;
        while (!currentTag.isEndElement()
                || !"object".equals(currentTag.asEndElement().getName().getLocalPart())) {
            input.nextTag(); // <name>
            String keyOrValue = input.nextEvent().asCharacters().getData();
            input.nextTag(); // </name>
            input.nextTag(); // <value>
            switch (keyOrValue) {
            case "key":
                key = keyDeserializer.map(input);
                break;
            case "value":
                value = valueDeserializer.map(input);
                break;
            default:
                readObjectTree(input);
                break;
            }
            input.nextTag(); // </value>
            input.nextTag(); // </field>
            currentTag = input.nextTag();
        }
        if (key != null) {
            map.put(key, value);
        }
        currentTag = input.nextTag();
    }
    return map;
}