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.commons.core.serialization.AtomDeserializer.java

private ODataPropertyType guessPropertyType(final XMLEventReader reader, final EdmTypeInfo typeInfo)
        throws XMLStreamException {

    XMLEvent child = null;/*from w w w  . ja  va  2  s.  c  o  m*/
    while (reader.hasNext() && child == null) {
        final XMLEvent event = reader.peek();
        if (event.isCharacters() && event.asCharacters().isWhiteSpace()) {
            reader.nextEvent();
        } else {
            child = event;
        }
    }

    final ODataPropertyType type;
    if (child == null) {
        type = typeInfo == null || typeInfo.isPrimitiveType() ? ODataPropertyType.PRIMITIVE
                : ODataPropertyType.ENUM;
    } else {
        if (child.isStartElement()) {
            if (Constants.NS_GML.equals(child.asStartElement().getName().getNamespaceURI())) {
                type = ODataPropertyType.PRIMITIVE;
            } else if (elementQName.equals(child.asStartElement().getName())) {
                type = ODataPropertyType.COLLECTION;
            } else {
                type = ODataPropertyType.COMPLEX;
            }
        } else if (child.isCharacters()) {
            type = typeInfo == null || typeInfo.isPrimitiveType() ? ODataPropertyType.PRIMITIVE
                    : ODataPropertyType.ENUM;
        } else {
            type = ODataPropertyType.EMPTY;
        }
    }

    return type;
}

From source file:org.apache.olingo.commons.core.serialization.AtomDeserializer.java

private void common(final XMLEventReader reader, final StartElement start, final AbstractODataObject object,
        final String key) throws XMLStreamException {

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

        if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) {
            try {
                object.setCommonProperty(key, event.asCharacters().getData());
            } catch (ParseException e) {
                throw new XMLStreamException("While parsing Atom entry or feed common elements", e);
            }//from  w  w  w  . ja v a 2s . c o  m
        }

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

From source file:org.apache.olingo.commons.core.serialization.AtomDeserializer.java

private void inline(final XMLEventReader reader, final StartElement start, final LinkImpl link)
        throws XMLStreamException, EdmPrimitiveTypeException {

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

        if (event.isStartElement()) {
            if (inlineQName.equals(event.asStartElement().getName())) {
                StartElement inline = null;
                while (reader.hasNext() && inline == null) {
                    final XMLEvent innerEvent = reader.peek();
                    if (innerEvent.isCharacters() && innerEvent.asCharacters().isWhiteSpace()) {
                        reader.nextEvent();
                    } else if (innerEvent.isStartElement()) {
                        inline = innerEvent.asStartElement();
                    }/*w w w.  j  a va  2 s. c  o m*/
                }
                if (inline != null) {
                    if (Constants.QNAME_ATOM_ELEM_ENTRY.equals(inline.getName())) {
                        link.setInlineEntity(entity(reader, inline));
                    }
                    if (Constants.QNAME_ATOM_ELEM_FEED.equals(inline.getName())) {
                        link.setInlineEntitySet(entitySet(reader, inline));
                    }
                }
            } else if (annotationQName.equals(event.asStartElement().getName())) {
                link.getAnnotations().add(annotation(reader, event.asStartElement()));
            }
        }

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

From source file:org.apache.olingo.commons.core.serialization.AtomDeserializer.java

private Delta delta(final XMLEventReader reader, final StartElement start)
        throws XMLStreamException, EdmPrimitiveTypeException {
    if (!Constants.QNAME_ATOM_ELEM_FEED.equals(start.getName())) {
        return null;
    }//from  ww  w . j a va 2 s .  c  o  m
    final DeltaImpl delta = new DeltaImpl();
    final Attribute xmlBase = start.getAttributeByName(Constants.QNAME_ATTR_XML_BASE);
    if (xmlBase != null) {
        delta.setBaseURI(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(), delta);
            } else if (Constants.QNAME_ATOM_ELEM_ID.equals(event.asStartElement().getName())) {
                common(reader, event.asStartElement(), delta, "id");
            } else if (Constants.QNAME_ATOM_ELEM_TITLE.equals(event.asStartElement().getName())) {
                common(reader, event.asStartElement(), delta, "title");
            } else if (Constants.QNAME_ATOM_ELEM_SUMMARY.equals(event.asStartElement().getName())) {
                common(reader, event.asStartElement(), delta, "summary");
            } else if (Constants.QNAME_ATOM_ELEM_UPDATED.equals(event.asStartElement().getName())) {
                common(reader, event.asStartElement(), delta, "updated");
            } 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) {
                            delta.setNext(URI.create(href.getValue()));
                        }
                    }
                    if (ODataServiceVersion.V40.getNamespace(NamespaceKey.DELTA_LINK_REL)
                            .equals(rel.getValue())) {
                        final Attribute href = event.asStartElement()
                                .getAttributeByName(QName.valueOf(Constants.ATTR_HREF));
                        if (href != null) {
                            delta.setDeltaLink(URI.create(href.getValue()));
                        }
                    }
                }
            } else if (Constants.QNAME_ATOM_ELEM_ENTRY.equals(event.asStartElement().getName())) {
                delta.getEntities().add(entity(reader, event.asStartElement()));
            } else if (deletedEntryQName.equals(event.asStartElement().getName())) {
                final DeletedEntityImpl deletedEntity = new DeletedEntityImpl();

                final Attribute ref = event.asStartElement()
                        .getAttributeByName(QName.valueOf(Constants.ATTR_REF));
                if (ref != null) {
                    deletedEntity.setId(URI.create(ref.getValue()));
                }
                final Attribute reason = event.asStartElement().getAttributeByName(reasonQName);
                if (reason != null) {
                    deletedEntity.setReason(Reason.valueOf(reason.getValue()));
                }

                delta.getDeletedEntities().add(deletedEntity);
            } else if (linkQName.equals(event.asStartElement().getName())
                    || deletedLinkQName.equals(event.asStartElement().getName())) {

                final DeltaLinkImpl link = new DeltaLinkImpl();

                final Attribute source = event.asStartElement()
                        .getAttributeByName(QName.valueOf(Constants.ATTR_SOURCE));
                if (source != null) {
                    link.setSource(URI.create(source.getValue()));
                }
                final Attribute relationship = event.asStartElement()
                        .getAttributeByName(QName.valueOf(Constants.ATTR_RELATIONSHIP));
                if (relationship != null) {
                    link.setRelationship(relationship.getValue());
                }
                final Attribute target = event.asStartElement()
                        .getAttributeByName(QName.valueOf(Constants.ATTR_TARGET));
                if (target != null) {
                    link.setTarget(URI.create(target.getValue()));
                }

                if (linkQName.equals(event.asStartElement().getName())) {
                    delta.getAddedLinks().add(link);
                } else {
                    delta.getDeletedLinks().add(link);
                }
            }
        }

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

    return delta;
}

From source file:org.apache.olingo.commons.core.serialization.AtomDeserializer.java

private LinkCollection linkCollection(final XMLEventReader reader) throws XMLStreamException {

    final LinkCollectionImpl linkCollection = new LinkCollectionImpl();

    boolean isURI = false;
    boolean isNext = false;
    while (reader.hasNext()) {
        final XMLEvent event = reader.nextEvent();
        if (event.isStartElement()) {
            isURI = uriQName.equals(event.asStartElement().getName());
            isNext = nextQName.equals(event.asStartElement().getName());
        }/*from   w  ww  .ja  v  a 2 s .c  om*/

        if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) {
            if (isURI) {
                linkCollection.getLinks().add(URI.create(event.asCharacters().getData()));
                isURI = false;
            } else if (isNext) {
                linkCollection.setNext(URI.create(event.asCharacters().getData()));
                isNext = false;
            }
        }
    }

    return linkCollection;
}

From source file:org.apache.olingo.commons.core.serialization.AtomDeserializer.java

private void properties(final XMLEventReader reader, final StartElement start, final EntityImpl entity)
        throws XMLStreamException, EdmPrimitiveTypeException {

    final Map<String, List<Annotation>> annotations = new HashMap<String, List<Annotation>>();

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

        if (event.isStartElement()) {
            if (annotationQName.equals(event.asStartElement().getName())) {
                final String target = event.asStartElement()
                        .getAttributeByName(QName.valueOf(Constants.ATTR_TARGET)).getValue();
                if (!annotations.containsKey(target)) {
                    annotations.put(target, new ArrayList<Annotation>());
                }//from w  w w.  j a  va2 s  .  co m
                annotations.get(target).add(annotation(reader, event.asStartElement()));
            } else {
                entity.getProperties().add(property(reader, event.asStartElement()));
            }
        }

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

    for (Property property : entity.getProperties()) {
        if (annotations.containsKey(property.getName())) {
            property.getAnnotations().addAll(annotations.get(property.getName()));
        }
    }
}

From source file:org.apache.olingo.commons.core.serialization.AtomDeserializer.java

private Entity entity(final XMLEventReader reader, final StartElement start)
        throws XMLStreamException, EdmPrimitiveTypeException {
    final EntityImpl entity;
    if (entryRefQName.equals(start.getName())) {
        entity = entityRef(start);//from w  w  w.j a  v a2s . co m
    } else if (Constants.QNAME_ATOM_ELEM_ENTRY.equals(start.getName())) {
        entity = new EntityImpl();
        final Attribute xmlBase = start.getAttributeByName(Constants.QNAME_ATTR_XML_BASE);
        if (xmlBase != null) {
            entity.setBaseURI(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_TITLE.equals(event.asStartElement().getName())) {
                    common(reader, event.asStartElement(), entity, "title");
                } else if (Constants.QNAME_ATOM_ELEM_SUMMARY.equals(event.asStartElement().getName())) {
                    common(reader, event.asStartElement(), entity, "summary");
                } else if (Constants.QNAME_ATOM_ELEM_UPDATED.equals(event.asStartElement().getName())) {
                    common(reader, event.asStartElement(), entity, "updated");
                } 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 LinkImpl link = new LinkImpl();
                    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.setHref(href.getValue());
                    }
                    final Attribute type = event.asStartElement()
                            .getAttributeByName(QName.valueOf(Constants.ATTR_TYPE));
                    if (type != null) {
                        link.setType(type.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(
                            version.getNamespace(ODataServiceVersion.NamespaceKey.NAVIGATION_LINK_REL))) {

                        entity.getNavigationLinks().add(link);
                        inline(reader, event.asStartElement(), link);
                    } else if (link.getRel().startsWith(
                            version.getNamespace(ODataServiceVersion.NamespaceKey.ASSOCIATION_LINK_REL))) {

                        entity.getAssociationLinks().add(link);
                    } else if (link.getRel().startsWith(
                            version.getNamespace(ODataServiceVersion.NamespaceKey.MEDIA_EDIT_LINK_REL))) {

                        final Attribute metag = event.asStartElement().getAttributeByName(etagQName);
                        if (metag != null) {
                            link.setMediaETag(metag.getValue());
                        }
                        entity.getMediaEditLinks().add(link);
                    }
                } else if (actionQName.equals(event.asStartElement().getName())) {
                    final ODataOperation operation = new ODataOperation();
                    final Attribute metadata = event.asStartElement()
                            .getAttributeByName(QName.valueOf(Constants.ATTR_METADATA));
                    if (metadata != null) {
                        operation.setMetadataAnchor(metadata.getValue());
                    }
                    final Attribute title = event.asStartElement()
                            .getAttributeByName(QName.valueOf(Constants.ATTR_TITLE));
                    if (title != null) {
                        operation.setTitle(title.getValue());
                    }
                    final Attribute target = event.asStartElement()
                            .getAttributeByName(QName.valueOf(Constants.ATTR_TARGET));
                    if (target != null) {
                        operation.setTarget(URI.create(target.getValue()));
                    }

                    entity.getOperations().add(operation);
                } else if (Constants.QNAME_ATOM_ELEM_CONTENT.equals(event.asStartElement().getName())) {
                    final Attribute type = event.asStartElement()
                            .getAttributeByName(QName.valueOf(Constants.ATTR_TYPE));
                    if (type == null || ContentType.APPLICATION_XML.equals(type.getValue())) {
                        properties(reader, skipBeforeFirstStartElement(reader), entity);
                    } else {
                        entity.setMediaContentType(type.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);
                } else if (annotationQName.equals(event.asStartElement().getName())) {
                    entity.getAnnotations().add(annotation(reader, event.asStartElement()));
                }
            }

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

    return entity;
}

From source file:org.apache.olingo.commons.core.serialization.AtomDeserializer.java

private void count(final XMLEventReader reader, final StartElement start, final EntitySet 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()));
        }//from  w w w  . java2  s. c om

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

From source file:org.apache.olingo.commons.core.serialization.AtomDeserializer.java

private EntitySet entitySet(final XMLEventReader reader, final StartElement start)
        throws XMLStreamException, EdmPrimitiveTypeException {
    if (!Constants.QNAME_ATOM_ELEM_FEED.equals(start.getName())) {
        return null;
    }/*from  w  w  w.jav a2  s  .com*/
    final EntitySetImpl entitySet = new EntitySetImpl();
    final Attribute xmlBase = start.getAttributeByName(Constants.QNAME_ATTR_XML_BASE);
    if (xmlBase != null) {
        entitySet.setBaseURI(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_TITLE.equals(event.asStartElement().getName())) {
                common(reader, event.asStartElement(), entitySet, "title");
            } else if (Constants.QNAME_ATOM_ELEM_SUMMARY.equals(event.asStartElement().getName())) {
                common(reader, event.asStartElement(), entitySet, "summary");
            } else if (Constants.QNAME_ATOM_ELEM_UPDATED.equals(event.asStartElement().getName())) {
                common(reader, event.asStartElement(), entitySet, "updated");
            } 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 (ODataServiceVersion.V40.getNamespace(NamespaceKey.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()));
            } else if (entryRefQName.equals(event.asStartElement().getName())) {
                entitySet.getEntities().add(entityRef(event.asStartElement()));
            } else if (annotationQName.equals(event.asStartElement().getName())) {
                entitySet.getAnnotations().add(annotation(reader, event.asStartElement()));
            }
        }

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

    return entitySet;
}

From source file:org.apache.olingo.fit.metadata.Metadata.java

public Metadata(final InputStream is) {
    DEF_NS = Constants.get(ConstantKey.EDM_NS);
    schemas = new HashMap<String, Schema>();

    try {/*  w  w  w  .  j  a  va 2 s.  c om*/
        final XMLInputFactory ifactory = XMLInputFactory.newInstance();
        final XMLEventReader reader = ifactory.createXMLEventReader(is,
                org.apache.olingo.commons.api.Constants.UTF8);

        try {
            while (reader.hasNext()) {
                final XMLEvent event = reader.nextEvent();

                if (event.isStartElement()
                        && event.asStartElement().getName().equals(new QName(DEF_NS, "Schema"))) {
                    final Schema schema = getSchema(event.asStartElement(), reader);
                    schemas.put(schema.getNamespace(), schema);
                }
            }

        } catch (Exception ignore) {
            // ignore
        } finally {
            reader.close();
            IOUtils.closeQuietly(is);
        }
    } catch (Exception e) {
        LOG.error("Error parsing metadata", e);
    }

    for (Map.Entry<String, Schema> schemaEntry : schemas.entrySet()) {
        for (EntityType entityType : schemaEntry.getValue().getEntityTypes()) {
            for (NavigationProperty property : entityType.getNavigationProperties()) {
                property.setFeed(property.getType().startsWith("Collection("));

                final Collection<EntitySet> entitySets = schemaEntry.getValue().getContainers().iterator()
                        .next().getEntitySets(schemaEntry.getKey(), entityType.getName());

                final Iterator<EntitySet> iter = entitySets.iterator();
                boolean found = false;

                while (!found && iter.hasNext()) {
                    final EntitySet entitySet = iter.next();
                    final String target = entitySet.getTarget(property.getName());
                    if (StringUtils.isNotBlank(target)) {
                        property.setTarget(entitySet.getTarget(property.getName()));
                        found = true;
                    }
                }
            }
        }
    }
}