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.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. j ava  2 s .co  m

        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;
    }/*  ww  w  .j av a2s  .  c om*/
    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 {/*from   w  w  w .j av  a  2  s  .  c o  m*/
        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;
                    }
                }
            }
        }
    }
}

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

private Schema getSchema(final StartElement start, final XMLEventReader reader) throws XMLStreamException {
    final Schema schema = new Schema(start.getAttributeByName(new QName("Namespace")).getValue());

    boolean completed = false;

    while (!completed && reader.hasNext()) {
        XMLEvent event = reader.nextEvent();

        if (event.isStartElement() && event.asStartElement().getName().equals(new QName(DEF_NS, "EntityType"))
                || event.isStartElement()
                        && event.asStartElement().getName().equals(new QName(DEF_NS, "ComplexType"))) {
            final EntityType entityType = getEntityType(event.asStartElement(), reader);
            schema.addEntityType(entityType.getName(), entityType);
        } else if (event.isStartElement()
                && event.asStartElement().getName().equals(new QName(DEF_NS, "EntityContainer"))) {
            final org.apache.olingo.fit.metadata.Container container = getContainer(event.asStartElement(),
                    reader);/*from   w ww . j a v a2  s. c  o m*/
            schema.addContainer(container.getName(), container);
        } else if (event.isEndElement() && event.asEndElement().getName().equals(start.getName())) {
            completed = true;
        }
    }

    return schema;
}

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

private org.apache.olingo.fit.metadata.Container getContainer(final StartElement start,
        final XMLEventReader reader) throws XMLStreamException {
    final org.apache.olingo.fit.metadata.Container container = new org.apache.olingo.fit.metadata.Container(
            start.getAttributeByName(new QName("Name")).getValue());

    boolean completed = false;

    while (!completed && reader.hasNext()) {
        XMLEvent event = reader.nextEvent();

        if (event.isStartElement() && (event.asStartElement().getName().equals(new QName(DEF_NS, "EntitySet"))
                || event.asStartElement().getName().equals(new QName(DEF_NS, "Singleton")))) {
            final EntitySet entitySet = getEntitySet(event.asStartElement(), reader);
            container.addEntitySet(entitySet.getName(), entitySet);
        } else if (event.isEndElement() && event.asEndElement().getName().equals(start.getName())) {
            completed = true;// w  w  w.  ja  va 2  s . c  o  m
        }
    }

    return container;
}

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

private EntityType getEntityType(final StartElement start, final XMLEventReader reader)
        throws XMLStreamException {
    final EntityType entityType = new EntityType(start.getAttributeByName(new QName("Name")).getValue());
    final Attribute baseType = start.getAttributeByName(new QName("BaseType"));
    if (baseType != null) {
        entityType.setBaseType(baseType.getValue());
    }//from ww  w.  ja  v  a  2  s.com
    final Attribute openType = start.getAttributeByName(new QName("OpenType"));
    if (openType != null) {
        entityType.setOpenType(BooleanUtils.toBoolean(openType.getValue()));
    }

    boolean completed = false;

    while (!completed && reader.hasNext()) {
        XMLEvent event = reader.nextEvent();

        if (event.isStartElement() && event.asStartElement().getName().equals(new QName(DEF_NS, "Property"))) {
            final org.apache.olingo.fit.metadata.Property property = getProperty(event.asStartElement());
            entityType.addProperty(property.getName(), property);
        } else if (event.isStartElement()
                && event.asStartElement().getName().equals(new QName(DEF_NS, "NavigationProperty"))) {
            final NavigationProperty property = getNavigationProperty(event.asStartElement());
            entityType.addNavigationProperty(property.getName(), property);
        } else if (event.isEndElement() && event.asEndElement().getName().equals(start.getName())) {
            completed = true;
        }
    }

    return entityType;
}

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

private EntitySet getEntitySet(final StartElement start, final XMLEventReader reader)
        throws XMLStreamException {
    final EntitySet entitySet = "Singleton".equals(start.getName().getLocalPart())
            ? new EntitySet(start.getAttributeByName(new QName("Name")).getValue(), true)
            : new EntitySet(start.getAttributeByName(new QName("Name")).getValue());

    Attribute type = start.getAttributeByName(new QName("EntityType"));
    if (type == null) {
        type = start.getAttributeByName(new QName("Type"));
        entitySet.setType(type == null ? null : type.getValue());
    } else {/*from   www  .  ja v  a2  s  .co  m*/
        entitySet.setType(type.getValue());
    }

    boolean completed = false;

    while (!completed && reader.hasNext()) {
        XMLEvent event = reader.nextEvent();

        if (event.isStartElement()
                && event.asStartElement().getName().equals(new QName(DEF_NS, "NavigationPropertyBinding"))) {
            final String path = event.asStartElement().getAttributeByName(new QName("Path")).getValue();
            final String target = event.asStartElement().getAttributeByName(new QName("Target")).getValue();
            entitySet.addBinding(path, target);
        } else if (event.isEndElement() && event.asEndElement().getName().equals(start.getName())) {
            completed = true;
        }
    }

    return entitySet;
}

From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.java

private void skipElement(final StartElement start, final XMLEventReader reader, final XMLEventWriter writer,
        final boolean excludeStart) throws Exception {

    if (!excludeStart) {
        writeEvent(start, writer);//from   www .ja v  a 2s . com
    }

    int depth = 1;
    boolean found = false;

    while (reader.hasNext() && !found) {
        final XMLEvent event = reader.nextEvent();

        writeEvent(event, writer);

        if (event.getEventType() == XMLStreamConstants.START_ELEMENT) {
            depth++;
        } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT) {
            depth--;
            found = depth == 0 && start.getName().equals(event.asEndElement().getName());
        }
    }
}

From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.java

public XmlElement getXmlElement(final StartElement start, final XMLEventReader reader) throws Exception {

    final XmlElement res = new XmlElement();
    res.setStart(start);/*from  w  ww  .j  a va2s .  co m*/

    StringWriter content = new StringWriter();

    int depth = 1;

    while (reader.hasNext() && depth > 0) {
        final XMLEvent event = reader.nextEvent();

        if (event.getEventType() == XMLStreamConstants.START_ELEMENT) {
            depth++;
        } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT) {
            depth--;
        }

        if (depth == 0) {
            res.setEnd(event.asEndElement());
        } else {
            event.writeAsEncodedUnicode(content);
        }
    }

    content.flush();
    content.close();

    res.setContent(new ByteArrayInputStream(content.toString().getBytes()));

    return res;
}

From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.java

public Map.Entry<Integer, XmlElement> extractElement(final XMLEventReader reader, final XMLEventWriter writer,
        final List<String> path, final Collection<Map.Entry<String, String>> filter, final boolean filterInOr,
        final int startPathPos, final int minPathPos, final int maxPathPos) throws Exception {

    StartElement start = null;/* www  . j  a  v a2  s.  c om*/
    int searchFor = 0;
    int depth = startPathPos;

    // Current inspected element
    String current = null;

    // set defaults
    final List<String> pathElementNames = path == null ? Collections.<String>emptyList() : path;
    final Collection<Map.Entry<String, String>> filterAttrs = filter == null
            ? Collections.<Map.Entry<String, String>>emptySet()
            : filter;

    while (reader.hasNext() && start == null) {
        final XMLEvent event = reader.nextEvent();

        if (event.getEventType() == XMLStreamConstants.START_ELEMENT) {
            depth++;

            if (current != null
                    || ((minPathPos < 0 || minPathPos <= depth) && (maxPathPos < 0 || depth <= maxPathPos))) {
                if (pathElementNames.isEmpty() || pathElementNames.get(searchFor).trim()
                        .equals(event.asStartElement().getName().getLocalPart())) {

                    if (searchFor < pathElementNames.size() - 1) {
                        // path exploring not completed
                        writeEvent(event, writer);
                        current = pathElementNames.get(searchFor).trim();
                        searchFor++;
                    } else {

                        // path exploring completed ... evaluate filter about path element name attribute
                        boolean match = filterAttrs.isEmpty() || !filterInOr;

                        for (Map.Entry<String, String> filterAttr : filterAttrs) {
                            final Attribute attr = event.asStartElement()
                                    .getAttributeByName(new QName(filterAttr.getKey().trim()));

                            if (attr == null || !filterAttr.getValue().trim().equals(attr.getValue())) {
                                match = filterInOr ? match : false;
                            } else {
                                match = filterInOr ? true : match;
                            }
                        }

                        if (match) {
                            // found searched element
                            start = event.asStartElement();
                        } else {
                            skipElement(event.asStartElement(), reader, writer, false);
                            depth--;
                        }
                    }
                } else if (current == null) {
                    writeEvent(event, writer);
                } else {
                    // skip element
                    skipElement(event.asStartElement(), reader, writer, false);
                    depth--;
                }
            } else {
                writeEvent(event, writer);
            }

        } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT) {
            depth--;

            writeEvent(event, writer);

            if (event.asEndElement().getName().getLocalPart().equals(current)) {
                // back step ....
                searchFor--;
                current = searchFor > 0 ? pathElementNames.get(searchFor - 1).trim() : null;
            }
        } else {
            writeEvent(event, writer);
        }
    }

    if (start == null) {
        throw new NotFoundException();
    }

    return new SimpleEntry<Integer, XmlElement>(Integer.valueOf(depth - 1), getXmlElement(start, reader));
}