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.utils.XMLUtilities.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;//from w  ww  . ja v a  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.XMLUtilities.java

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

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

    try {/*from www .  ja  v 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), Constants.ENCODING),
                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.XMLUtilities.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;/*w w w  . j  a 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
                && Constants.get(ConstantKey.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
                && Constants.get(ConstantKey.LINK).equals(event.asEndElement().getName().getLocalPart())) {
            writeNext = true;
        } else if (event.getEventType() == XMLStreamConstants.START_ELEMENT
                && (Constants.get(ConstantKey.PROPERTIES))
                        .equals(event.asStartElement().getName().getLocalPart())) {
            writeCurrent = true;
            writeNext = false;
            inProperties = true;
        } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT
                && (Constants.get(ConstantKey.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 ((Constants.get(ConstantKey.ATOM_PROPERTY_PREFIX) + propertyName.trim())
                            .equals(elementName)) {
                        writeCurrent = true;
                        found.remove(propertyName);
                        currentName = propertyName;
                    }
                }

            } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT
                    && StringUtils.isNotBlank(currentName)
                    && (Constants.get(ConstantKey.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

@Override
public InputStream replaceProperty(final InputStream src, final InputStream replacement,
        final List<String> path, final boolean justValue) throws Exception {

    final List<String> pathElements = new ArrayList<String>();

    for (String element : path) {
        pathElements.add(Constants.get(ConstantKey.ATOM_PROPERTY_PREFIX) + element);
    }// w ww .j a v  a2  s  . c  o  m

    final XMLEventReader reader = getEventReader(src);

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

    final Map.Entry<Integer, XMLElement> element = extractElement(reader, writer, pathElements, 0, 3, 4);

    if (justValue) {
        writer.add(element.getValue().getStart());
    }

    final XMLEventReader changesReader = new XMLEventReaderWrapper(replacement);

    while (changesReader.hasNext()) {
        final XMLEvent event = changesReader.nextEvent();
        if (event.isStartElement()
                && event.asStartElement().getName().equals(element.getValue().getStart().getName())) {
            writer.add(element.getValue().getStart());
            writer.add(changesReader);
        } else {
            writer.add(event);
        }
    }

    changesReader.close();
    IOUtils.closeQuietly(replacement);

    if (justValue) {
        writer.add(element.getValue().getEnd());
    }

    writer.add(reader);

    reader.close();
    IOUtils.closeQuietly(src);

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

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

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

private Object primitive(final XMLEventReader reader, final StartElement start, final EdmType type,
        final boolean isNullable, final Integer maxLength, final Integer precision, final Integer scale,
        final boolean isUnicode) throws XMLStreamException, EdmPrimitiveTypeException, DeserializerException {

    Object value = null;/* ww  w .j  ava2  s  . co m*/

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

        if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) {
            if (type instanceof AbstractGeospatialType<?>) {
                throw new DeserializerException("geo types support not implemented",
                        DeserializerException.MessageKeys.NOT_IMPLEMENTED);
            }
            final EdmPrimitiveType primitiveType = (EdmPrimitiveType) type;
            final String stringValue = event.asCharacters().getData();
            value = primitiveType.valueOfString(stringValue, isNullable, maxLength, precision, scale, isUnicode,
                    primitiveType.getDefaultType());
        }

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

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

private Object complex(final XMLEventReader reader, final StartElement start, final EdmComplexType edmComplex)
        throws XMLStreamException, EdmPrimitiveTypeException, DeserializerException {
    ComplexValue value = new ComplexValue();
    boolean foundEndProperty = false;
    while (reader.hasNext() && !foundEndProperty) {
        final XMLEvent event = reader.nextEvent();
        if (event.isStartElement()) {
            StartElement se = event.asStartElement();
            EdmProperty p = (EdmProperty) edmComplex.getProperty(se.getName().getLocalPart());
            value.getValue().add(property(reader, se, p.getType(), p.isNullable(), p.getMaxLength(),
                    p.getPrecision(), p.getScale(), p.isUnicode(), p.isCollection()));
        }/*from   w w w.  ja  va 2s.  c o  m*/
        if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
            foundEndProperty = true;
        }
    }
    return value;
}

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

private void collection(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)
        throws XMLStreamException, EdmPrimitiveTypeException, DeserializerException {

    List<Object> values = new ArrayList<Object>();

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

        if (event.isStartElement()) {
            if (edmType instanceof EdmPrimitiveType) {
                values.add(primitive(reader, event.asStartElement(), edmType, isNullable, maxLength, precision,
                        scale, isUnicode));
            } else if (edmType instanceof EdmComplexType) {
                values.add(complex(reader, event.asStartElement(), (EdmComplexType) edmType));
            }//from  w w  w  .  j  a v a 2s . com
            // do not add null or empty values
        }

        if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
            foundEndProperty = true;
        }
    }
    valuable.setValue(getValueType(edmType, true), values);
}

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;
            }/*w  w  w.  j a  v  a  2 s  . com*/
        }
        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());
                        }//  www . ja v a2 s .co m

                        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()));
        }/*  w w  w.  jav a2 s . c o  m*/

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