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

private String entityRef(final XMLEventReader reader, final StartElement start) throws XMLStreamException {
    boolean foundEndElement = false;
    final Attribute entityRefId = start.getAttributeByName(Constants.QNAME_ATOM_ATTR_ID);
    while (reader.hasNext() && !foundEndElement) {
        final XMLEvent event = reader.nextEvent();
        if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
            foundEndElement = true;//from w ww . j  a v  a 2 s. c o  m
        }
    }
    return entityRefId.getValue();
}

From source file:org.apache.olingo.server.core.deserializer.xml.ODataXmlDeserializer.java

private void properties(final XMLEventReader reader, final StartElement start, final Entity entity,
        final EdmEntityType edmEntityType)
        throws XMLStreamException, EdmPrimitiveTypeException, DeserializerException {

    boolean foundEndProperties = false;
    while (reader.hasNext() && !foundEndProperties) {
        final XMLEvent event = reader.nextEvent();

        if (event.isStartElement()) {
            String propertyName = event.asStartElement().getName().getLocalPart();
            EdmProperty edmProperty = (EdmProperty) edmEntityType.getProperty(propertyName);
            if (edmProperty == null) {
                throw new DeserializerException("Invalid Property in payload with name: " + propertyName,
                        DeserializerException.MessageKeys.UNKNOWN_CONTENT, propertyName);
            }//from  w  ww . ja v a  2 s.  c o  m
            entity.getProperties()
                    .add(property(reader, event.asStartElement(), edmProperty.getType(),
                            edmProperty.isNullable(), edmProperty.getMaxLength(), edmProperty.getPrecision(),
                            edmProperty.getScale(), edmProperty.isUnicode(), edmProperty.isCollection()));
        }

        if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
            foundEndProperties = true;
        }
    }
}

From source file:org.apache.olingo.server.core.deserializer.xml.ODataXmlDeserializer.java

private Entity entity(final XMLEventReader reader, final StartElement start, final EdmEntityType edmEntityType)
        throws XMLStreamException, EdmPrimitiveTypeException, DeserializerException {
    Entity entity = null;/*from   ww w.jav a 2s. c  om*/
    if (entryRefQName.equals(start.getName())) {
        entity = entityRef(start);
    } else if (Constants.QNAME_ATOM_ELEM_ENTRY.equals(start.getName())) {
        entity = new Entity();
        final Attribute xmlBase = start.getAttributeByName(Constants.QNAME_ATTR_XML_BASE);
        if (xmlBase != null) {
            entity.setBaseURI(URI.create(xmlBase.getValue()));
        }

        final Attribute etag = start.getAttributeByName(etagQName);
        if (etag != null) {
            entity.setETag(etag.getValue());
        }

        boolean foundEndEntry = false;
        while (reader.hasNext() && !foundEndEntry) {
            final XMLEvent event = reader.nextEvent();

            if (event.isStartElement()) {
                if (Constants.QNAME_ATOM_ELEM_ID.equals(event.asStartElement().getName())) {
                    common(reader, event.asStartElement(), entity, "id");
                } else if (Constants.QNAME_ATOM_ELEM_CATEGORY.equals(event.asStartElement().getName())) {
                    final Attribute term = event.asStartElement()
                            .getAttributeByName(QName.valueOf(Constants.ATOM_ATTR_TERM));
                    if (term != null) {
                        entity.setType(new EdmTypeInfo.Builder().setTypeExpression(term.getValue()).build()
                                .internal());
                    }
                } else if (Constants.QNAME_ATOM_ELEM_LINK.equals(event.asStartElement().getName())) {
                    final Link link = new Link();
                    final Attribute rel = event.asStartElement()
                            .getAttributeByName(QName.valueOf(Constants.ATTR_REL));
                    if (rel != null) {
                        link.setRel(rel.getValue());
                    }
                    final Attribute title = event.asStartElement()
                            .getAttributeByName(QName.valueOf(Constants.ATTR_TITLE));
                    if (title != null) {
                        link.setTitle(title.getValue());
                    }
                    final Attribute href = event.asStartElement()
                            .getAttributeByName(QName.valueOf(Constants.ATTR_HREF));
                    if (href != null) {
                        link.setBindingLink(href.getValue());
                    }
                    final Attribute linktype = event.asStartElement()
                            .getAttributeByName(QName.valueOf(Constants.ATTR_TYPE));
                    if (linktype != null) {
                        link.setType(linktype.getValue());
                    }
                    if (Constants.SELF_LINK_REL.equals(link.getRel())) {
                        entity.setSelfLink(link);
                    } else if (Constants.EDIT_LINK_REL.equals(link.getRel())) {
                        entity.setEditLink(link);
                    } else if (Constants.EDITMEDIA_LINK_REL.equals(link.getRel())) {
                        final Attribute mediaETag = event.asStartElement().getAttributeByName(etagQName);
                        if (mediaETag != null) {
                            entity.setMediaETag(mediaETag.getValue());
                        }
                    } else if (link.getRel().startsWith(Constants.NS_NAVIGATION_LINK_REL)) {
                        inline(reader, event.asStartElement(), link, edmEntityType);
                        if (link.getInlineEntity() == null && link.getInlineEntitySet() == null) {
                            entity.getNavigationBindings().add(link);
                        } else {
                            if (link.getInlineEntitySet() != null) {
                                List<String> bindings = new ArrayList<String>();
                                List<Entity> entities = link.getInlineEntitySet().getEntities();

                                for (Entity inlineEntity : entities) {
                                    // check if this is reference
                                    if (inlineEntity.getId() != null
                                            && inlineEntity.getProperties().isEmpty()) {
                                        bindings.add(inlineEntity.getId().toASCIIString());
                                    }
                                }
                                if (!bindings.isEmpty()) {
                                    link.setInlineEntitySet(null);
                                    link.setBindingLinks(bindings);
                                    entity.getNavigationBindings().add(link);
                                } else {
                                    entity.getNavigationLinks().add(link);
                                }
                            } else {
                                // add link
                                entity.getNavigationLinks().add(link);
                            }
                        }
                    } else if (link.getRel().startsWith(Constants.NS_ASSOCIATION_LINK_REL)) {
                        entity.getAssociationLinks().add(link);
                    } else if (link.getRel().startsWith(Constants.NS_MEDIA_EDIT_LINK_REL)) {
                        final Attribute metag = event.asStartElement().getAttributeByName(etagQName);
                        if (metag != null) {
                            link.setMediaETag(metag.getValue());
                        }
                        entity.getMediaEditLinks().add(link);
                    }
                } else if (Constants.QNAME_ATOM_ELEM_CONTENT.equals(event.asStartElement().getName())) {
                    final Attribute contenttype = event.asStartElement()
                            .getAttributeByName(QName.valueOf(Constants.ATTR_TYPE));
                    if (contenttype == null || ContentType.APPLICATION_XML.toContentTypeString()
                            .equals(contenttype.getValue())) {
                        properties(reader, skipBeforeFirstStartElement(reader), entity, edmEntityType);
                    } else {
                        entity.setMediaContentType(contenttype.getValue());
                        final Attribute src = event.asStartElement()
                                .getAttributeByName(QName.valueOf(Constants.ATOM_ATTR_SRC));
                        if (src != null) {
                            entity.setMediaContentSource(URI.create(src.getValue()));
                        }
                    }
                } else if (propertiesQName.equals(event.asStartElement().getName())) {
                    properties(reader, event.asStartElement(), entity, edmEntityType);
                }
            }

            if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
                foundEndEntry = true;
            }
        }
    }
    return entity;
}

From source file:org.apache.olingo.server.core.deserializer.xml.ODataXmlDeserializer.java

private void count(final XMLEventReader reader, final StartElement start, final EntityCollection entitySet)
        throws XMLStreamException {

    boolean foundEndElement = false;
    while (reader.hasNext() && !foundEndElement) {
        final XMLEvent event = reader.nextEvent();
        if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) {
            entitySet.setCount(Integer.valueOf(event.asCharacters().getData()));
        }/* w ww  .j  av  a2 s. c  o m*/
        if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
            foundEndElement = true;
        }
    }
}

From source file:org.apache.olingo.server.core.deserializer.xml.ODataXmlDeserializer.java

private EntityCollection entitySet(final XMLEventReader reader, final StartElement start,
        final EdmEntityType edmEntityType)
        throws XMLStreamException, EdmPrimitiveTypeException, DeserializerException {
    if (!Constants.QNAME_ATOM_ELEM_FEED.equals(start.getName())) {
        return null;
    }/*from w  w  w  .  j  ava  2  s . c o m*/
    final EntityCollection entitySet = new EntityCollection();
    final Attribute xmlBase = start.getAttributeByName(Constants.QNAME_ATTR_XML_BASE);
    if (xmlBase != null) {
        entitySet.setBaseURI(URI.create(xmlBase.getValue()));
    }

    boolean foundEndFeed = false;
    while (reader.hasNext() && !foundEndFeed) {
        final XMLEvent event = reader.nextEvent();
        if (event.isStartElement()) {
            if (countQName.equals(event.asStartElement().getName())) {
                count(reader, event.asStartElement(), entitySet);
            } else if (Constants.QNAME_ATOM_ELEM_ID.equals(event.asStartElement().getName())) {
                common(reader, event.asStartElement(), entitySet, "id");
            } else if (Constants.QNAME_ATOM_ELEM_LINK.equals(event.asStartElement().getName())) {
                final Attribute rel = event.asStartElement()
                        .getAttributeByName(QName.valueOf(Constants.ATTR_REL));
                if (rel != null) {
                    if (Constants.NEXT_LINK_REL.equals(rel.getValue())) {
                        final Attribute href = event.asStartElement()
                                .getAttributeByName(QName.valueOf(Constants.ATTR_HREF));
                        if (href != null) {
                            entitySet.setNext(URI.create(href.getValue()));
                        }
                    }
                    if (Constants.NS_DELTA_LINK_REL.equals(rel.getValue())) {
                        final Attribute href = event.asStartElement()
                                .getAttributeByName(QName.valueOf(Constants.ATTR_HREF));
                        if (href != null) {
                            entitySet.setDeltaLink(URI.create(href.getValue()));
                        }
                    }
                }
            } else if (Constants.QNAME_ATOM_ELEM_ENTRY.equals(event.asStartElement().getName())) {
                entitySet.getEntities().add(entity(reader, event.asStartElement(), edmEntityType));
            } else if (entryRefQName.equals(event.asStartElement().getName())) {
                entitySet.getEntities().add(entityRef(event.asStartElement()));
            }
        }

        if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
            foundEndFeed = true;
        }
    }
    return entitySet;
}

From source file:org.apache.olingo.server.core.deserializer.xml.ODataXmlDeserializer.java

@Override
public DeserializerResult entityReferences(final InputStream stream) throws DeserializerException {
    try {/* w w  w.j a v a  2s  .  c  o  m*/
        XMLEventReader reader = getReader(stream);
        ArrayList<URI> references = new ArrayList<URI>();

        while (reader.hasNext()) {
            final XMLEvent event = reader.nextEvent();
            if (event.isStartElement()) {
                StartElement start = event.asStartElement();
                if (entryRefQName.equals(start.getName())) {
                    Attribute context = start.getAttributeByName(Constants.QNAME_ATOM_ATTR_ID);
                    URI uri = URI.create(context.getValue());
                    references.add(uri);
                }
            }
        }
        return DeserializerResultImpl.with().entityReferences(references).build();
    } catch (XMLStreamException e) {
        throw new DeserializerException(e.getMessage(), e, DeserializerException.MessageKeys.IO_EXCEPTION);
    }
}

From source file:org.apache.olingo.server.core.deserializer.xml.ODataXmlDeserializer.java

@Override
public DeserializerResult actionParameters(final InputStream stream, final EdmAction edmAction)
        throws DeserializerException {
    Map<String, Parameter> parameters = new LinkedHashMap<String, Parameter>();
    if (edmAction.getParameterNames() == null || edmAction.getParameterNames().isEmpty()
            || edmAction.isBound() && edmAction.getParameterNames().size() == 1) {
        return DeserializerResultImpl.with().actionParameters(parameters).build();
    }/*from ww w  .j  ava2 s.c om*/

    try {
        final XMLEventReader reader = getReader(stream);
        while (reader.hasNext()) {
            final XMLEvent event = reader.nextEvent();
            if (event.isStartElement() && parametersQName.equals(event.asStartElement().getName())) {
                consumeParameters(edmAction, reader, event.asStartElement(), parameters);
            }
        }
        // EDM checks.
        for (final String param : edmAction.getParameterNames()) {
            Parameter parameter = parameters.get(param);
            if (parameter == null) {
                final EdmParameter edmParameter = edmAction.getParameter(param);
                if (!edmParameter.isNullable()) {
                    throw new DeserializerException("Non-nullable parameter not present or null: " + param,
                            MessageKeys.INVALID_NULL_PARAMETER, param);
                }
                if (edmParameter.isCollection()) {
                    throw new DeserializerException("Collection must not be null for parameter: " + param,
                            MessageKeys.INVALID_NULL_PARAMETER, param);
                }
                // NULL fill for missing parameters.
                parameter = new Parameter();
                parameter.setName(param);
                parameter.setValue(ValueType.PRIMITIVE, null);
                parameters.put(param, parameter);
            }
        }
        return DeserializerResultImpl.with().actionParameters(parameters).build();
    } catch (XMLStreamException e) {
        throw new DeserializerException(e.getMessage(), e, DeserializerException.MessageKeys.IO_EXCEPTION);
    } catch (final EdmPrimitiveTypeException e) {
        throw new DeserializerException(e.getMessage(), e,
                DeserializerException.MessageKeys.INVALID_VALUE_FOR_PROPERTY);
    }
}

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());
    }//from w  w w  .  j  av a2s.  c o m

    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/*from  w  ww  .  j av a 2  s. com*/
 *
 * @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.dataconservancy.model.builder.WrappingEventReaderTest.java

private void commonNextEventTypeTest(XMLEventReader reader) throws XMLStreamException {
    assertTrue(reader.hasNext());
    assertEquals(START_DOCUMENT, reader.nextEvent().getEventType());
    assertTrue(reader.hasNext());/* ww  w .j a  va  2s. c  om*/
    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);
}