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.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 w  w.  j  ava2s  .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.  j  ava2s .c om*/
        }
    }

    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());
    }/*w  w  w . j  a  v  a 2s  . co  m*/
    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  w  ww  .  j  a v a  2s  .  c om*/
        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. j  ava  2 s .  c  o  m*/
    }

    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);//w  w  w . jav a2 s  . c  o  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;//w  w w  . j  ava 2 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));
}

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

public InputStream addAtomInlinecount(final InputStream feed, final int count, final Accept accept)
        throws Exception {
    final XMLEventReader reader = getEventReader(feed);

    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final XMLEventWriter writer = getEventWriter(bos);

    try {//w  w  w  . jav  a 2s. co  m

        final XmlElement feedElement = extractElement(reader, writer, Collections.<String>singletonList("feed"),
                0, 1, 1).getValue();

        writer.add(feedElement.getStart());
        addAtomElement(IOUtils.toInputStream(String.format("<m:count>%d</m:count>", count)), writer);
        writer.add(feedElement.getContentReader());
        writer.add(feedElement.getEnd());

        while (reader.hasNext()) {
            writer.add(reader.nextEvent());
        }

    } finally {
        writer.flush();
        writer.close();
        reader.close();
        IOUtils.closeQuietly(feed);
    }

    return new ByteArrayInputStream(bos.toByteArray());
}

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

@Override
public InputStream selectEntity(final InputStream entity, final String[] propertyNames) throws Exception {
    final XMLEventReader reader = getEventReader(entity);

    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final XMLEventWriter writer = getEventWriter(bos);

    final List<String> found = new ArrayList<String>(Arrays.asList(propertyNames));

    boolean inProperties = false;
    boolean writeCurrent = true;
    Boolean writeNext = null;/*from   www  . ja v a  2  s .c  o  m*/
    String currentName = null;

    final List<String> fieldToBeSaved = new ArrayList<String>(Arrays.asList(propertyNames));

    while (reader.hasNext()) {
        final XMLEvent event = reader.nextEvent();
        if (event.getEventType() == XMLStreamConstants.START_ELEMENT
                && LINK.equals(event.asStartElement().getName().getLocalPart())
                && !fieldToBeSaved
                        .contains(event.asStartElement().getAttributeByName(new QName("title")).getValue())
                && !"edit".equals(event.asStartElement().getAttributeByName(new QName("rel")).getValue())) {
            writeCurrent = false;
        } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT
                && LINK.equals(event.asEndElement().getName().getLocalPart())) {
            writeNext = true;
        } else if (event.getEventType() == XMLStreamConstants.START_ELEMENT
                && (PROPERTIES).equals(event.asStartElement().getName().getLocalPart())) {
            writeCurrent = true;
            writeNext = false;
            inProperties = true;
        } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT
                && (PROPERTIES).equals(event.asEndElement().getName().getLocalPart())) {
            writeCurrent = true;
        } else if (inProperties) {
            if (event.getEventType() == XMLStreamConstants.START_ELEMENT) {
                final String elementName = event.asStartElement().getName().getLocalPart();

                for (String propertyName : propertyNames) {
                    if ((ATOM_PROPERTY_PREFIX + propertyName.trim()).equals(elementName)) {
                        writeCurrent = true;
                        found.remove(propertyName);
                        currentName = propertyName;
                    }
                }

            } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT
                    && StringUtils.isNotBlank(currentName) && (ATOM_PROPERTY_PREFIX + currentName.trim())
                            .equals(event.asEndElement().getName().getLocalPart())) {
                writeNext = false;
                currentName = null;
            }

        }

        if (writeCurrent) {
            writer.add(event);
        }

        if (writeNext != null) {
            writeCurrent = writeNext;
            writeNext = null;
        }
    }

    writer.flush();
    writer.close();
    reader.close();
    IOUtils.closeQuietly(entity);

    // Do not raise any exception in order to support FC properties as well
    // if (!found.isEmpty()) {
    //     throw new Exception(String.format("Could not find a properties '%s'", found));
    // }

    return new ByteArrayInputStream(bos.toByteArray());
}

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

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

    final XMLElement res = new XMLElement();
    res.setStart(start);/*w  w  w  .  j  a va 2 s . c  om*/

    final Charset encoding = Charset.forName(org.apache.olingo.commons.api.Constants.UTF8);
    final ByteArrayOutputStream content = new ByteArrayOutputStream();
    final OutputStreamWriter writer = new OutputStreamWriter(content, encoding);

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

    writer.flush();
    writer.close();

    res.setContent(new ByteArrayInputStream(content.toByteArray()));

    return res;
}