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 Entity entity(final XMLEventReader reader, final StartElement start)
        throws XMLStreamException, EdmPrimitiveTypeException {
    final Entity entity;
    if (entryRefQName.equals(start.getName())) {
        entity = entityRef(start);//from   w ww.ja  v  a2 s.  c  o  m
    } 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_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 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 (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)) {
                        entity.getNavigationLinks().add(link);
                        inline(reader, event.asStartElement(), 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)
                            || link.getRel().startsWith(Constants.NS_MEDIA_READ_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 Operation operation = new Operation();
                    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.toContentTypeString().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.client.core.serialization.AtomDeserializer.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()));
        }//from ww w. j a v a 2 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 EntityCollection entitySet(final XMLEventReader reader, final StartElement start)
        throws XMLStreamException, EdmPrimitiveTypeException {
    if (!Constants.QNAME_ATOM_ELEM_FEED.equals(start.getName())) {
        return null;
    }/* ww w  .  j  a va2 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_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 (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()));
            } 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.client.core.serialization.AtomDeserializer.java

private ODataError error(final XMLEventReader reader, final StartElement start) throws XMLStreamException {
    final ODataError error = new ODataError();

    boolean setCode = false;
    boolean codeSet = false;
    boolean setMessage = false;
    boolean messageSet = false;
    boolean setTarget = false;
    boolean targetSet = false;

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

        if (event.isStartElement()) {
            if (errorCodeQName.equals(event.asStartElement().getName())) {
                setCode = true;//from w w w . j a  va 2  s  .  co  m
            } else if (errorMessageQName.equals(event.asStartElement().getName())) {
                setMessage = true;
            } else if (errorTargetQName.equals(event.asStartElement().getName())) {
                setTarget = true;
            }
        }

        if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) {
            if (setCode && !codeSet) {
                error.setCode(event.asCharacters().getData());
                setCode = false;
                codeSet = true;
            }
            if (setMessage && !messageSet) {
                error.setMessage(event.asCharacters().getData());
                setMessage = false;
                messageSet = true;
            }
            if (setTarget && !targetSet) {
                error.setTarget(event.asCharacters().getData());
                setTarget = false;
                targetSet = true;
            }
        }

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

    return error;
}

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

private List<Point> points(final XMLEventReader reader, final StartElement start,
        final EdmPrimitiveTypeKind type, final SRID srid) throws XMLStreamException {

    final List<Point> result = new ArrayList<Point>();

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

        if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) {
            final String[] pointInfo = event.asCharacters().getData().split(" ");

            final Point point = new Point(GeoUtils.getDimension(type), srid);
            try {
                point.setX(EdmDouble.getInstance().valueOfString(pointInfo[0], null, null,
                        Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null, Double.class));
                point.setY(EdmDouble.getInstance().valueOfString(pointInfo[1], null, null,
                        Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null, Double.class));
            } catch (EdmPrimitiveTypeException e) {
                throw new XMLStreamException("While deserializing point coordinates as double", e);
            }/*  w w w  .j  a v a  2s. co m*/
            result.add(point);
        }

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

    // handles bad input, e.g. things like <gml:pos/>
    if (result.isEmpty()) {
        result.add(new Point(GeoUtils.getDimension(type), srid));
    }

    return result;
}

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

private MultiPoint multipoint(final XMLEventReader reader, final StartElement start,
        final EdmPrimitiveTypeKind type, final SRID srid) throws XMLStreamException {

    List<Point> points = Collections.<Point>emptyList();

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

        if (event.isStartElement() && event.asStartElement().getName().equals(Constants.QNAME_POINTMEMBERS)) {
            points = points(reader, event.asStartElement(), type, null);
        }/*from   w ww .j a  v a2s .co m*/

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

    return new MultiPoint(GeoUtils.getDimension(type), srid, points);
}

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

private Polygon polygon(final XMLEventReader reader, final StartElement start, final EdmPrimitiveTypeKind type,
        final SRID srid) throws XMLStreamException {

    List<Point> extPoints = null;
    List<Point> intPoints = null;

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

        if (event.isStartElement()) {
            if (event.asStartElement().getName().equals(Constants.QNAME_POLYGON_EXTERIOR)) {
                extPoints = points(reader, event.asStartElement(), type, null);
            }/*from  w w  w.  j  a va 2 s  .  c  o  m*/
            if (event.asStartElement().getName().equals(Constants.QNAME_POLYGON_INTERIOR)) {
                intPoints = points(reader, event.asStartElement(), type, null);
            }
        }

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

    return new Polygon(GeoUtils.getDimension(type), srid, intPoints, extPoints);
}

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

private MultiLineString multiLineString(final XMLEventReader reader, final StartElement start,
        final EdmPrimitiveTypeKind type, final SRID srid) 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));
        }/*from   ww w  .  ja  v a 2  s  . c om*/

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

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

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

private MultiPolygon multiPolygon(final XMLEventReader reader, final StartElement start,
        final EdmPrimitiveTypeKind type, final SRID srid) 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   w w w.  j a  v a2s  . co m*/

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

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

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

private GeospatialCollection collection(final XMLEventReader reader, final StartElement start,
        final EdmPrimitiveTypeKind type, final SRID srid) 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())));
                }/*  w  w  w  .j av a 2  s  .  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), srid, geospatials);
}