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.apache.olingo.server.core.deserializer.xml.ODataXmlDeserializer.java

private void consumeParameters(final EdmAction edmAction, final XMLEventReader reader, final StartElement start,
        final Map<String, Parameter> parameters)
        throws DeserializerException, EdmPrimitiveTypeException, XMLStreamException {

    List<String> parameterNames = edmAction.getParameterNames();
    if (edmAction.isBound()) {
        // The binding parameter must not occur in the payload.
        parameterNames = parameterNames.subList(1, parameterNames.size());
    }//w ww  . ja  v  a  2 s.  c  om

    boolean foundEndElement = false;
    while (reader.hasNext() && !foundEndElement) {
        final XMLEvent event = reader.nextEvent();
        if (event.isStartElement()) {
            boolean found = false;
            for (String paramName : parameterNames) {
                if (paramName.equals(event.asStartElement().getName().getLocalPart())) {
                    found = true;
                    Parameter parameter = createParameter(reader, event.asStartElement(), paramName,
                            edmAction.getParameter(paramName));
                    Parameter previous = parameters.put(paramName, parameter);
                    if (previous != null) {
                        throw new DeserializerException("Duplicate property detected",
                                DeserializerException.MessageKeys.DUPLICATE_PROPERTY);
                    }
                    break; // for
                }
            }
            if (!found) {
                throw new DeserializerException(
                        "failed to read " + event.asStartElement().getName().getLocalPart(),
                        DeserializerException.MessageKeys.UNKNOWN_CONTENT);
            }
        }
        if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
            foundEndElement = true;
        }
    }
}

From source file:org.codice.ddf.spatial.ogc.wfs.transformer.handlebars.HandlebarsWfsFeatureTransformer.java

/**
 * Reads in the FeatureMember from the input stream, populating the contextMap with the XML tag
 * names and values//w  w w. j a  va  2  s  .  c  om
 *
 * @param inputStream the stream containing the FeatureMember xml document
 */
private void populateContextMap(InputStream inputStream, Map<String, String> contextMap) {
    Map<String, String> namespaces = new HashMap<>();
    boolean canHandleFeatureType = false;
    try {
        XMLEventReader xmlEventReader = getXmlEventReader(inputStream);

        String elementName = null;
        while (xmlEventReader.hasNext()) {
            XMLEvent xmlEvent = xmlEventReader.nextEvent();
            if (xmlEvent.isStartElement()) {
                StartElement startElement = xmlEvent.asStartElement();
                elementName = startElement.getName().getLocalPart();
                canHandleFeatureType |= processStartElement(xmlEventReader, startElement, namespaces,
                        contextMap, canHandleFeatureType);
                addXmlAttributesToContextMap(startElement, contextMap);
            } else if (xmlEvent.isCharacters()) {
                contextMap.put(elementName, xmlEvent.asCharacters().getData());
            }
        }
        if (!canHandleFeatureType) {
            contextMap.clear();
        }
    } catch (XMLStreamException e) {
        LOGGER.debug("Error transforming feature to metacard.", e);
    }
}

From source file:org.codice.ddf.spatial.ogc.wfs.transformer.handlebars.HandlebarsWfsFeatureTransformer.java

private void readGmlData(XMLEventReader xmlEventReader, String elementName, Map<String, String> contextMap)
        throws XMLStreamException {

    if (!xmlEventReader.peek().isStartElement()) {
        LOGGER.debug("Problem reading gml data for element: {}. Invalid xml element provided.", elementName);
        return;/*ww  w  .j av  a 2  s .  co  m*/
    }

    StringWriter stringWriter = new StringWriter();
    XMLEventWriter eventWriter = XML_OUTPUT_FACTORY.createXMLEventWriter(stringWriter);

    if (eventWriter == null) {
        LOGGER.debug("Problem reading gml data for element: {}. Event writer is null", elementName);
        return;
    }

    int count = 0;
    boolean addEvent = true;

    try {
        while (addEvent) {
            XMLEvent xmlEvent = xmlEventReader.nextEvent();

            // populate the start element with the namespaces
            if (xmlEvent.isStartElement()) {
                xmlEvent = addNamespacesToStartElement(xmlEvent.asStartElement());
                count++;
            } else if (xmlEvent.isEndElement()) {
                if (count == 0) {
                    addEvent = false;
                    eventWriter.flush();
                    LOGGER.debug("String writer: {}", stringWriter);
                    contextMap.put(elementName, stringWriter.toString());
                }
                count--;
            }

            if (addEvent) {
                eventWriter.add(xmlEvent);
            }
        }
    } finally {
        eventWriter.close();
    }
}

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

private void commonNextEventTypeTest(XMLEventReader reader) throws XMLStreamException {
    assertTrue(reader.hasNext());/*from   w ww. j  ava  2s.c om*/
    assertEquals(START_DOCUMENT, reader.nextEvent().getEventType());
    assertTrue(reader.hasNext());
    assertEquals(START_ELEMENT, reader.nextEvent().getEventType());
    assertTrue(reader.hasNext());
    assertEquals(CHARACTERS, reader.nextEvent().getEventType());
    assertTrue(reader.hasNext());
    assertEquals(END_ELEMENT, reader.nextEvent().getEventType());
    commonEndDocumentTest(reader);
}

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);//w  w w .j a  v  a  2  s.com

    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);//from w  w w. j  a  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  w w w  . j  a  va2s  .com

    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();//  w w w.j ava  2s  .co  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());/*w  w w .  j  a va  2s . co  m*/
    assertEquals(END_DOCUMENT, reader.nextEvent().getEventType());

    assertFalse(reader.hasNext());

    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());/*  ww w .  j ava  2 s.c  om*/
    reader.nextEvent();
    reader.remove();
}