Example usage for javax.xml.stream.events Attribute getValue

List of usage examples for javax.xml.stream.events Attribute getValue

Introduction

In this page you can find the example usage for javax.xml.stream.events Attribute getValue.

Prototype

public String getValue();

Source Link

Document

Gets the normalized value of this attribute

Usage

From source file:org.apache.olingo.commons.core.serialization.AtomDeserializer.java

private void valuable(final Valuable valuable, final XMLEventReader reader, final StartElement start)
        throws XMLStreamException, EdmPrimitiveTypeException {

    final Attribute nullAttr = start.getAttributeByName(nullQName);

    final Attribute typeAttr = start.getAttributeByName(typeQName);
    final String typeAttrValue = typeAttr == null ? null : typeAttr.getValue();

    final EdmTypeInfo typeInfo = StringUtils.isBlank(typeAttrValue) ? null
            : new EdmTypeInfo.Builder().setTypeExpression(typeAttrValue).build();

    if (typeInfo != null) {
        valuable.setType(typeInfo.internal());
    }/*  w  w  w  .j  a  v a 2s  .c om*/

    final ODataPropertyType propType = typeInfo == null ? guessPropertyType(reader, typeInfo)
            : typeInfo.isCollection() ? ODataPropertyType.COLLECTION
                    : typeInfo.isPrimitiveType() ? ODataPropertyType.PRIMITIVE : ODataPropertyType.COMPLEX;

    if (nullAttr == null) {
        switch (propType) {
        case COLLECTION:
            fromCollection(valuable, reader, start, typeInfo);
            break;

        case COMPLEX:
            final Object complexValue = fromComplexOrEnum(reader, start);
            valuable.setValue(
                    complexValue instanceof LinkedComplexValue ? ValueType.LINKED_COMPLEX
                            : complexValue instanceof List<?> ? ValueType.COMPLEX : ValueType.ENUM,
                    complexValue);
            break;

        case PRIMITIVE:
            // No type specified? Defaults to Edm.String
            if (typeInfo == null) {
                valuable.setType(EdmPrimitiveTypeKind.String.getFullQualifiedName().toString());
            }
            final Object value = fromPrimitive(reader, start, typeInfo);
            valuable.setValue(value instanceof Geospatial ? ValueType.GEOSPATIAL : ValueType.PRIMITIVE, value);
            break;

        case EMPTY:
        default:
            valuable.setValue(ValueType.PRIMITIVE, StringUtils.EMPTY);
        }
    } else {
        valuable.setValue(propType == ODataPropertyType.PRIMITIVE ? ValueType.PRIMITIVE
                : propType == ODataPropertyType.ENUM ? ValueType.ENUM
                        : propType == ODataPropertyType.COMPLEX ? ValueType.COMPLEX
                                : propType == ODataPropertyType.COLLECTION ? ValueType.COLLECTION_PRIMITIVE
                                        : ValueType.PRIMITIVE,
                null);
    }
}

From source file:org.apache.olingo.commons.core.serialization.AtomDeserializer.java

private Delta delta(final XMLEventReader reader, final StartElement start)
        throws XMLStreamException, EdmPrimitiveTypeException {
    if (!Constants.QNAME_ATOM_ELEM_FEED.equals(start.getName())) {
        return null;
    }/*  w  ww.j  av a2  s.co  m*/
    final DeltaImpl delta = new DeltaImpl();
    final Attribute xmlBase = start.getAttributeByName(Constants.QNAME_ATTR_XML_BASE);
    if (xmlBase != null) {
        delta.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(), delta);
            } else if (Constants.QNAME_ATOM_ELEM_ID.equals(event.asStartElement().getName())) {
                common(reader, event.asStartElement(), delta, "id");
            } else if (Constants.QNAME_ATOM_ELEM_TITLE.equals(event.asStartElement().getName())) {
                common(reader, event.asStartElement(), delta, "title");
            } else if (Constants.QNAME_ATOM_ELEM_SUMMARY.equals(event.asStartElement().getName())) {
                common(reader, event.asStartElement(), delta, "summary");
            } else if (Constants.QNAME_ATOM_ELEM_UPDATED.equals(event.asStartElement().getName())) {
                common(reader, event.asStartElement(), delta, "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) {
                            delta.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) {
                            delta.setDeltaLink(URI.create(href.getValue()));
                        }
                    }
                }
            } else if (Constants.QNAME_ATOM_ELEM_ENTRY.equals(event.asStartElement().getName())) {
                delta.getEntities().add(entity(reader, event.asStartElement()));
            } else if (deletedEntryQName.equals(event.asStartElement().getName())) {
                final DeletedEntityImpl deletedEntity = new DeletedEntityImpl();

                final Attribute ref = event.asStartElement()
                        .getAttributeByName(QName.valueOf(Constants.ATTR_REF));
                if (ref != null) {
                    deletedEntity.setId(URI.create(ref.getValue()));
                }
                final Attribute reason = event.asStartElement().getAttributeByName(reasonQName);
                if (reason != null) {
                    deletedEntity.setReason(Reason.valueOf(reason.getValue()));
                }

                delta.getDeletedEntities().add(deletedEntity);
            } else if (linkQName.equals(event.asStartElement().getName())
                    || deletedLinkQName.equals(event.asStartElement().getName())) {

                final DeltaLinkImpl link = new DeltaLinkImpl();

                final Attribute source = event.asStartElement()
                        .getAttributeByName(QName.valueOf(Constants.ATTR_SOURCE));
                if (source != null) {
                    link.setSource(URI.create(source.getValue()));
                }
                final Attribute relationship = event.asStartElement()
                        .getAttributeByName(QName.valueOf(Constants.ATTR_RELATIONSHIP));
                if (relationship != null) {
                    link.setRelationship(relationship.getValue());
                }
                final Attribute target = event.asStartElement()
                        .getAttributeByName(QName.valueOf(Constants.ATTR_TARGET));
                if (target != null) {
                    link.setTarget(URI.create(target.getValue()));
                }

                if (linkQName.equals(event.asStartElement().getName())) {
                    delta.getAddedLinks().add(link);
                } else {
                    delta.getDeletedLinks().add(link);
                }
            }
        }

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

    return delta;
}

From source file:org.apache.olingo.commons.core.serialization.AtomDeserializer.java

private EntityImpl entityRef(final StartElement start) throws XMLStreamException {
    final EntityImpl entity = new EntityImpl();

    final Attribute entityRefId = start.getAttributeByName(Constants.QNAME_ATOM_ATTR_ID);
    if (entityRefId != null) {
        entity.setId(URI.create(entityRefId.getValue()));
    }//from   ww w. j  a v  a2s  .com

    return entity;
}

From source file:org.apache.olingo.commons.core.serialization.AtomDeserializer.java

private Entity entity(final XMLEventReader reader, final StartElement start)
        throws XMLStreamException, EdmPrimitiveTypeException {
    final EntityImpl entity;
    if (entryRefQName.equals(start.getName())) {
        entity = entityRef(start);//ww w  . j  a v a2 s  . c o m
    } else if (Constants.QNAME_ATOM_ELEM_ENTRY.equals(start.getName())) {
        entity = new EntityImpl();
        final Attribute xmlBase = start.getAttributeByName(Constants.QNAME_ATTR_XML_BASE);
        if (xmlBase != null) {
            entity.setBaseURI(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 LinkImpl link = new LinkImpl();
                    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(
                            version.getNamespace(ODataServiceVersion.NamespaceKey.NAVIGATION_LINK_REL))) {

                        entity.getNavigationLinks().add(link);
                        inline(reader, event.asStartElement(), link);
                    } else if (link.getRel().startsWith(
                            version.getNamespace(ODataServiceVersion.NamespaceKey.ASSOCIATION_LINK_REL))) {

                        entity.getAssociationLinks().add(link);
                    } else if (link.getRel().startsWith(
                            version.getNamespace(ODataServiceVersion.NamespaceKey.MEDIA_EDIT_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 ODataOperation operation = new ODataOperation();
                    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.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.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;
    }/*from  w  w w  .j av  a2  s .  co  m*/
    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

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  ww. j a va2s.  c  o 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 org.apache.olingo.fit.metadata.Property getProperty(final StartElement start)
        throws XMLStreamException {
    final org.apache.olingo.fit.metadata.Property property = new org.apache.olingo.fit.metadata.Property(
            start.getAttributeByName(new QName("Name")).getValue());

    final Attribute type = start.getAttributeByName(new QName("Type"));
    property.setType(type == null ? "Edm.String" : type.getValue());

    final Attribute nullable = start.getAttributeByName(new QName("Nullable"));
    property.setNullable(nullable == null || !"false".equals(nullable.getValue()));

    return property;
}

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

private NavigationProperty getNavigationProperty(final StartElement start) throws XMLStreamException {
    final NavigationProperty property = new NavigationProperty(
            start.getAttributeByName(new QName("Name")).getValue());

    final Attribute type = start.getAttributeByName(new QName("Type"));
    if (type != null) {
        property.setType(type.getValue());
    }/*from w  ww .  ja v a2 s.com*/

    return property;
}

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 . jav a  2  s  .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

/**
 * {@inheritDoc }/*from w w  w .  j a  va  2 s.c om*/
 */
@Override
protected NavigationLinks retrieveNavigationInfo(final String entitySetName, final InputStream is)
        throws Exception {

    final NavigationLinks links = new NavigationLinks();

    final XMLEventReader reader = getEventReader(is);

    try {
        final List<Map.Entry<String, String>> filter = new ArrayList<Map.Entry<String, String>>();
        filter.add(new AbstractMap.SimpleEntry<String, String>("type", "application/atom+xml;type=entry"));
        filter.add(new AbstractMap.SimpleEntry<String, String>("type", "application/atom+xml;type=feed"));

        int startDepth = 0;

        while (true) {
            // a. search for link with type attribute equals to "application/atom+xml;type=entry/feed"
            final Map.Entry<Integer, XmlElement> linkInfo = extractElement(reader, null,
                    Collections.<String>singletonList(LINK), filter, true, startDepth, 2, 2);
            final XmlElement link = linkInfo.getValue();
            startDepth = linkInfo.getKey();

            final String title = link.getStart().getAttributeByName(new QName("title")).getValue();

            final Attribute hrefAttr = link.getStart().getAttributeByName(new QName("href"));
            final String href = hrefAttr == null ? null : hrefAttr.getValue();

            try {
                final XmlElement inlineElement = extractElement(link.getContentReader(), null,
                        Collections.<String>singletonList(INLINE), 0, -1, -1).getValue();
                final XMLEventReader inlineReader = inlineElement.getContentReader();

                try {
                    while (true) {
                        final XmlElement entry = extractElement(inlineReader, null,
                                Collections.<String>singletonList("entry"), 0, -1, -1).getValue();
                        links.addInlines(title, entry.toStream());
                    }
                } catch (Exception e) {
                    // Reached the end of document
                }

                inlineReader.close();
            } catch (Exception ignore) {
                // inline element not found (inlines are not mondatory).
                if (StringUtils.isNotBlank(href) && entityUriPattern.matcher(href).matches()) {
                    links.addLinks(title, href.substring(href.lastIndexOf('/') + 1));
                }
            }
        }
    } catch (Exception ignore) {
        // ignore
    } finally {
        reader.close();
    }

    return links;
}