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.data.AtomGeoValueDeserializer.java

private MultiLineString multiLineString(final XMLEventReader reader, final StartElement start,
        final EdmPrimitiveTypeKind type, final String crs) throws XMLStreamException {

    final List<LineString> lineStrings = new ArrayList<LineString>();

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

        if (event.isStartElement() && event.asStartElement().getName().equals(Constants.QNAME_LINESTRING)) {
            lineStrings.add(lineString(reader, event.asStartElement(), type, null));
        }/* w ww.  j  a v  a2 s .c  o m*/

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

    return new MultiLineString(GeoUtils.getDimension(type), crs, lineStrings);
}

From source file:org.apache.olingo.commons.core.data.AtomGeoValueDeserializer.java

private MultiPolygon multiPolygon(final XMLEventReader reader, final StartElement start,
        final EdmPrimitiveTypeKind type, final String crs) throws XMLStreamException {

    final List<Polygon> polygons = new ArrayList<Polygon>();

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

        if (event.isStartElement() && event.asStartElement().getName().equals(Constants.QNAME_POLYGON)) {
            polygons.add(polygon(reader, event.asStartElement(), type, null));
        }//from   ww  w .  jav  a 2s  . co m

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

    return new MultiPolygon(GeoUtils.getDimension(type), crs, polygons);
}

From source file:org.apache.olingo.commons.core.data.AtomGeoValueDeserializer.java

private GeospatialCollection collection(final XMLEventReader reader, final StartElement start,
        final EdmPrimitiveTypeKind type, final String crs) throws XMLStreamException {

    final List<Geospatial> geospatials = new ArrayList<Geospatial>();

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

        if (event.isStartElement() && event.asStartElement().getName().equals(Constants.QNAME_GEOMEMBERS)) {
            boolean foundEndMembers = false;
            while (reader.hasNext() && !foundEndMembers) {
                final XMLEvent subevent = reader.nextEvent();

                if (subevent.isStartElement()) {
                    geospatials.add(deserialize(reader, subevent.asStartElement(), GeoUtils.getType(
                            GeoUtils.getDimension(type), subevent.asStartElement().getName().getLocalPart())));
                }//from  w  ww  .  j  ava 2s .  c o  m

                if (subevent.isEndElement()
                        && Constants.QNAME_GEOMEMBERS.equals(subevent.asEndElement().getName())) {
                    foundEndMembers = true;
                }
            }
        }

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

    return new GeospatialCollection(GeoUtils.getDimension(type), crs, geospatials);
}

From source file:org.apache.olingo.commons.core.data.AtomPropertyDeserializer.java

private Value fromPrimitive(final XMLEventReader reader, final StartElement start, final EdmTypeInfo typeInfo)
        throws XMLStreamException {

    Value value = null;//from  ww w. j ava  2s  .  com

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

        if (event.isStartElement() && typeInfo != null && typeInfo.getPrimitiveTypeKind().isGeospatial()) {
            final EdmPrimitiveTypeKind geoType = EdmPrimitiveTypeKind.valueOfFQN(version,
                    typeInfo.getFullQualifiedName().toString());
            value = new GeospatialValueImpl(
                    this.geoDeserializer.deserialize(reader, event.asStartElement(), geoType));
        }

        if (event.isCharacters() && !event.asCharacters().isWhiteSpace()
                && (typeInfo == null || !typeInfo.getPrimitiveTypeKind().isGeospatial())) {

            value = new PrimitiveValueImpl(event.asCharacters().getData());
        }

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

    return value;
}

From source file:org.apache.olingo.commons.core.data.AtomPropertyDeserializer.java

private ComplexValue fromComplex(final XMLEventReader reader, final StartElement start)
        throws XMLStreamException {

    final ComplexValue value = new ComplexValueImpl();

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

        if (event.isStartElement()) {
            value.get().add(deserialize(reader, event.asStartElement()));
        }//from w w  w.j a  v  a  2  s  .  c o m

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

    return value;
}

From source file:org.apache.olingo.commons.core.data.AtomPropertyDeserializer.java

private CollectionValue fromCollection(final XMLEventReader reader, final StartElement start,
        final EdmTypeInfo typeInfo) throws XMLStreamException {

    final CollectionValueImpl value = new CollectionValueImpl();

    final EdmTypeInfo type = typeInfo == null ? null
            : new EdmTypeInfo.Builder().setTypeExpression(typeInfo.getFullQualifiedName().toString()).build();

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

        if (event.isStartElement()) {
            switch (guessPropertyType(reader)) {
            case COMPLEX:
                value.get().add(fromComplex(reader, event.asStartElement()));
                break;

            case PRIMITIVE:
                value.get().add(fromPrimitive(reader, event.asStartElement(), type));
                break;

            default:
                // do not add null or empty values
            }/*from  w  w w.j  a  v a  2  s.com*/
        }

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

    return value;
}

From source file:org.apache.olingo.commons.core.data.AtomPropertyDeserializer.java

private ODataPropertyType guessPropertyType(final XMLEventReader reader) throws XMLStreamException {
    XMLEvent child = null;/*from  w w  w.j  av  a2  s  . c om*/
    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 = ODataPropertyType.PRIMITIVE;
    } 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 = ODataPropertyType.PRIMITIVE;
        } else {
            type = ODataPropertyType.EMPTY;
        }
    }

    return type;
}

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

private Object fromPrimitive(final XMLEventReader reader, final StartElement start, final EdmTypeInfo typeInfo)
        throws XMLStreamException, EdmPrimitiveTypeException {

    Object value = null;/*  w  w  w .  j  ava  2  s.  com*/

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

        if (event.isStartElement() && typeInfo != null && typeInfo.getPrimitiveTypeKind().isGeospatial()) {
            final EdmPrimitiveTypeKind geoType = EdmPrimitiveTypeKind.valueOfFQN(version,
                    typeInfo.getFullQualifiedName().toString());
            value = geoDeserializer.deserialize(reader, event.asStartElement(), geoType);
        }

        if (event.isCharacters() && !event.asCharacters().isWhiteSpace()
                && (typeInfo == null || !typeInfo.getPrimitiveTypeKind().isGeospatial())) {

            final String stringValue = event.asCharacters().getData();
            value = typeInfo == null ? stringValue : // TODO: add facets
                    ((EdmPrimitiveType) typeInfo.getType()).valueOfString(stringValue, true, null,
                            Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, true,
                            ((EdmPrimitiveType) typeInfo.getType()).getDefaultType());
        }

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

    return value;
}

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

@SuppressWarnings("unchecked")
private Object fromComplexOrEnum(final XMLEventReader reader, final StartElement start)
        throws XMLStreamException, EdmPrimitiveTypeException {

    Object value = null;/*  w  w w.j av a 2 s .c  o m*/

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

        if (event.isStartElement()) {
            if (value == null) {
                value = version.compareTo(ODataServiceVersion.V40) < 0 ? new ArrayList<Property>()
                        : new LinkedComplexValueImpl();
            }

            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 (link.getRel().startsWith(
                        version.getNamespace(ODataServiceVersion.NamespaceKey.NAVIGATION_LINK_REL))) {

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

                    ((Valuable) value).asLinkedComplex().getAssociationLinks().add(link);
                }
            } else {
                (value instanceof LinkedComplexValue ? ((LinkedComplexValue) value).getValue()
                        : (List<Property>) value).add(property(reader, event.asStartElement()));
            }
        }

        if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) {
            value = event.asCharacters().getData();
        }

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

    return value;
}

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

private void fromCollection(final Valuable valuable, final XMLEventReader reader, final StartElement start,
        final EdmTypeInfo typeInfo) throws XMLStreamException, EdmPrimitiveTypeException {

    List<Object> values = new ArrayList<Object>();
    ValueType valueType = ValueType.COLLECTION_PRIMITIVE;

    final EdmTypeInfo type = typeInfo == null ? null
            : new EdmTypeInfo.Builder().setTypeExpression(typeInfo.getFullQualifiedName().toString()).build();

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

        if (event.isStartElement()) {
            switch (guessPropertyType(reader, typeInfo)) {
            case COMPLEX:
                final Object complexValue = fromComplexOrEnum(reader, event.asStartElement());
                valueType = complexValue instanceof LinkedComplexValue ? ValueType.COLLECTION_LINKED_COMPLEX
                        : ValueType.COLLECTION_COMPLEX;
                values.add(complexValue);
                break;

            case ENUM:
                valueType = ValueType.COLLECTION_ENUM;
                values.add(fromComplexOrEnum(reader, event.asStartElement()));
                break;

            case PRIMITIVE:
                final Object value = fromPrimitive(reader, event.asStartElement(), type);
                valueType = value instanceof Geospatial ? ValueType.COLLECTION_GEOSPATIAL
                        : ValueType.COLLECTION_PRIMITIVE;
                values.add(value);/*from w  w w .  jav  a 2s . c om*/
                break;

            default:
                // do not add null or empty values
            }
        }

        if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
            foundEndProperty = true;
        }
    }
    valuable.setValue(valueType, values);
}