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

private void valuable(final Valuable valuable, final XMLEventReader reader, final StartElement start,
        final EdmType edmType, final boolean isNullable, final Integer maxLength, final Integer precision,
        final Integer scale, final boolean isUnicode, final boolean isCollection)
        throws XMLStreamException, EdmPrimitiveTypeException, DeserializerException {

    final Attribute nullAttr = start.getAttributeByName(nullQName);
    if (nullAttr != null) {
        // found null
        boolean foundEndProperty = false;
        while (reader.hasNext() && !foundEndProperty) {
            final XMLEvent event = reader.nextEvent();
            if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
                foundEndProperty = true;
            }//from  w w  w. j  a  va2s .c o  m
        }
        valuable.setValue(getValueType(edmType, false), null);
        return;
    }

    final String typeName = edmType.getFullQualifiedName().getFullQualifiedNameAsString();
    valuable.setType(isCollection ? ("Collection(" + typeName + ")") : typeName);
    if (isCollection) {
        collection(valuable, reader, start, edmType, isNullable, maxLength, precision, scale, isUnicode);
    } else if (edmType instanceof EdmPrimitiveType) {
        valuable.setValue(getValueType(edmType, false),
                primitive(reader, start, edmType, isNullable, maxLength, precision, scale, isUnicode));
    } else if (edmType instanceof EdmComplexType) {
        valuable.setValue(ValueType.COMPLEX, complex(reader, start, (EdmComplexType) edmType));
    } else if (edmType instanceof EdmEntityType) {
        valuable.setValue(ValueType.ENTITY, entity(reader, start, (EdmEntityType) edmType));
    }
    // do not add null or empty values
}

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

private void inline(final XMLEventReader reader, final StartElement start, final Link link,
        final EdmEntityType edmEntityType)
        throws XMLStreamException, EdmPrimitiveTypeException, DeserializerException {

    boolean foundEndElement = false;
    EdmNavigationProperty navigationProperty = edmEntityType.getNavigationProperty(link.getTitle());
    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())) {
                        if (navigationProperty.isCollection()) {
                            throw new DeserializerException(
                                    "Navigation Property " + link.getTitle() + " must be collection entities",
                                    DeserializerException.MessageKeys.INVALID_ANNOTATION_TYPE, link.getTitle());
                        }//from   www  . jav  a 2s . c  om

                        link.setInlineEntity(entity(reader, inline, navigationProperty.getType()));
                    }
                    if (Constants.QNAME_ATOM_ELEM_FEED.equals(inline.getName())) {
                        if (!navigationProperty.isCollection()) {
                            throw new DeserializerException(
                                    "Navigation Property " + link.getTitle() + " must be single entity",
                                    DeserializerException.MessageKeys.INVALID_ANNOTATION_TYPE, link.getTitle());
                        }
                        link.setInlineEntitySet(entitySet(reader, inline, navigationProperty.getType()));
                    }
                }
            } else if (entryRefQName.equals(event.asStartElement().getName())) {
                if (navigationProperty.isCollection()) {
                    throw new DeserializerException(
                            "Binding annotation: " + link.getTitle()
                                    + " must be collection of entity references",
                            DeserializerException.MessageKeys.INVALID_ANNOTATION_TYPE, link.getTitle());
                }
                link.setBindingLink(entityRef(reader, event.asStartElement()));
                link.setType(Constants.ENTITY_BINDING_LINK_TYPE);
            } else if (Constants.QNAME_ATOM_ELEM_FEED.equals(event.asStartElement().getName())) {
                if (navigationProperty.isCollection()) {
                    throw new DeserializerException(
                            "Binding annotation: " + link.getTitle() + " must be single entity references",
                            DeserializerException.MessageKeys.INVALID_ANNOTATION_TYPE, link.getTitle());
                }
                link.setBindingLinks(entityRefCollection(reader, event.asStartElement()));
                link.setType(Constants.ENTITY_COLLECTION_BINDING_LINK_TYPE);
            }
        }

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

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

private List<String> entityRefCollection(final XMLEventReader reader, final StartElement start)
        throws XMLStreamException {
    boolean foundEndElement = false;
    ArrayList<String> references = new ArrayList<String>();
    while (reader.hasNext() && !foundEndElement) {
        final XMLEvent event = reader.nextEvent();

        if (event.isStartElement() && entryRefQName.equals(event.asStartElement().getName())) {
            references.add(entityRef(reader, event.asStartElement()));
        }/*from   w  w w .j  a v  a 2s. c  o  m*/

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

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;/*w w w .j  a  va2  s .  c om*/
        }
    }
    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);
            }/*  w  w  w  .ja  va 2 s .  co  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;// w  w w . j  a  v a 2s.co  m
    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()));
        }//from  www .  j a  v  a 2s .  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. ja  va2 s  .c  om
    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 {/*from ww w  .  j av  a 2  s  .co 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 w w  w .ja va  2s  .  com

    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);
    }
}