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

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

    Object value = null;//  www .  j a  v  a  2  s  . co  m

    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(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();
            if (typeInfo == null) {
                value = stringValue;
            } else {
                final EdmPrimitiveType primitiveType = (EdmPrimitiveType) typeInfo.getType();
                final Class<?> returnType = primitiveType.getDefaultType().isAssignableFrom(Calendar.class)
                        ? Timestamp.class
                        : primitiveType.getDefaultType();
                value = ((EdmPrimitiveType) typeInfo.getType()).valueOfString(stringValue, true, null,
                        Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, true, returnType);
            }
        }

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

    return value;
}

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

private Object fromComplexOrEnum(final XMLEventReader reader, final StartElement start)
        throws XMLStreamException, EdmPrimitiveTypeException {

    Object value = null;//  w  ww. j  a va 2 s  .co m

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

        if (event.isStartElement()) {
            if (value == null) {
                value = new ComplexValue();
            }

            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.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(Constants.NS_NAVIGATION_LINK_REL)) {

                    ((ComplexValue) value).getNavigationLinks().add(link);
                    inline(reader, event.asStartElement(), link);
                } else if (link.getRel().startsWith(Constants.NS_ASSOCIATION_LINK_REL)) {

                    ((Valuable) value).asComplex().getAssociationLinks().add(link);
                }
            } else {
                ((ComplexValue) value).getValue().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.client.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 = 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  . j  a v a 2  s .  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);
}

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

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

    XMLEvent child = null;//from   w  ww .  j  a v a 2s  .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 PropertyType type;
    if (child == null) {
        type = typeInfo == null || typeInfo.isPrimitiveType() ? PropertyType.PRIMITIVE : PropertyType.ENUM;
    } else {
        if (child.isStartElement()) {
            if (Constants.NS_GML.equals(child.asStartElement().getName().getNamespaceURI())) {
                type = PropertyType.PRIMITIVE;
            } else if (elementQName.equals(child.asStartElement().getName())) {
                type = PropertyType.COLLECTION;
            } else {
                type = PropertyType.COMPLEX;
            }
        } else if (child.isCharacters()) {
            type = typeInfo == null || typeInfo.isPrimitiveType() ? PropertyType.PRIMITIVE : PropertyType.ENUM;
        } else {
            type = PropertyType.EMPTY;
        }
    }

    return type;
}

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

private StartElement skipBeforeFirstStartElement(final XMLEventReader reader) throws XMLStreamException {
    StartElement startEvent = null;
    while (reader.hasNext() && startEvent == null) {
        final XMLEvent event = reader.nextEvent();
        if (event.isStartElement()) {
            startEvent = event.asStartElement();
        }// w  w w.j av a 2s .  c om
    }
    if (startEvent == null) {
        throw new IllegalArgumentException("Cannot find any XML start element");
    }

    return startEvent;
}

From source file:org.apache.olingo.client.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()) {
            object.setCommonProperty(key, event.asCharacters().getData());
        }// w w  w  .  j a  v a2  s.co  m

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

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

private void inline(final XMLEventReader reader, final StartElement start, final Link 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 = getStartElement(reader);
                if (inline != null) {
                    if (Constants.QNAME_ATOM_ELEM_ENTRY.equals(inline.getName())) {
                        link.setInlineEntity(entity(reader, inline));
                    }//from   w  ww  .  ja  v  a2s . co m
                    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.client.core.serialization.AtomDeserializer.java

private StartElement getStartElement(final XMLEventReader reader) throws XMLStreamException {
    while (reader.hasNext()) {
        final XMLEvent innerEvent = reader.peek();
        if (innerEvent.isCharacters() && innerEvent.asCharacters().isWhiteSpace()) {
            reader.nextEvent();
        } else if (innerEvent.isStartElement()) {
            return innerEvent.asStartElement();
        } else if (innerEvent.isEndElement() && inlineQName.equals(innerEvent.asEndElement().getName())) {
            return null;
        }//from   www  .  j av  a  2  s .  co m
    }
    return null;
}

From source file:org.apache.olingo.client.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 w  w  w .  j  a va 2  s .co m*/
    final Delta delta = new Delta();
    final Attribute xmlBase = start.getAttributeByName(Constants.QNAME_ATTR_XML_BASE);
    if (xmlBase != null) {
        delta.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(), 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 (Constants.NS_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 DeletedEntity deletedEntity = new DeletedEntity();

                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 DeltaLink link = new DeltaLink();

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

private void properties(final XMLEventReader reader, final StartElement start, final Entity 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>());
                }//  w  w  w .j  ava2s .c  o  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()));
        }
    }
}