Example usage for javax.xml.stream XMLStreamWriter writeEndElement

List of usage examples for javax.xml.stream XMLStreamWriter writeEndElement

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamWriter writeEndElement.

Prototype

public void writeEndElement() throws XMLStreamException;

Source Link

Document

Writes an end tag to the output relying on the internal state of the writer to determine the prefix and local name of the event.

Usage

From source file:org.apache.olingo.commons.core.data.AtomSerializer.java

private void feed(final XMLStreamWriter writer, final Feed feed) throws XMLStreamException {
    if (feed.getBaseURI() != null) {
        writer.writeAttribute(XMLConstants.XML_NS_URI, Constants.ATTR_XML_BASE,
                feed.getBaseURI().toASCIIString());
    }/*from  w w  w  .j a va 2  s. c om*/

    if (feed.getCount() != null) {
        writer.writeStartElement(version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA),
                Constants.ATOM_ELEM_COUNT);
        writer.writeCharacters(Integer.toString(feed.getCount()));
        writer.writeEndElement();
    }

    if (StringUtils.isNotBlank(feed.getId())) {
        writer.writeStartElement(Constants.ATOM_ELEM_ID);
        writer.writeCharacters(feed.getId());
        writer.writeEndElement();
    }

    if (feed instanceof AbstractAtomObject) {
        common(writer, (AbstractAtomObject) feed);
    }

    for (Entry entry : feed.getEntries()) {
        writer.writeStartElement(Constants.ATOM_ELEM_ENTRY);
        entry(writer, entry);
        writer.writeEndElement();
    }

    if (feed.getNext() != null) {
        final LinkImpl next = new LinkImpl();
        next.setRel(Constants.NEXT_LINK_REL);
        next.setHref(feed.getNext().toASCIIString());

        links(writer, Collections.<Link>singletonList(next));
    }
}

From source file:org.apache.olingo.commons.core.data.AtomSerializer.java

private void feed(final Writer outWriter, final Feed feed) throws XMLStreamException {
    final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter);

    startDocument(writer, Constants.ATOM_ELEM_FEED);

    feed(writer, feed);/*from w ww  .  ja  va2 s.c  o  m*/

    writer.writeEndElement();
    writer.writeEndDocument();
    writer.flush();
}

From source file:org.apache.olingo.commons.core.data.AtomSerializer.java

private void link(final Writer outWriter, final Link link) throws XMLStreamException {
    final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter);

    writer.writeStartDocument();/*from   ww  w .  j a  v a 2s.  c  o m*/

    writer.writeStartElement(Constants.ELEM_LINKS);
    writer.writeDefaultNamespace(version.getNamespaceMap().get(ODataServiceVersion.NS_DATASERVICES));

    writer.writeStartElement(Constants.ELEM_URI);
    writer.writeCharacters(link.getHref());
    writer.writeEndElement();

    writer.writeEndElement();

    writer.writeEndDocument();
    writer.flush();
}

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

private void collection(final XMLStreamWriter writer, final ValueType valueType,
        final EdmPrimitiveTypeKind kind, final List<?> value)
        throws XMLStreamException, EdmPrimitiveTypeException {
    for (Object item : value) {
        if (version.compareTo(ODataServiceVersion.V40) < 0) {
            writer.writeStartElement(Constants.PREFIX_DATASERVICES, Constants.ELEM_ELEMENT, namespaceData);
        } else {/*from  w  ww .j av  a 2s . c om*/
            writer.writeStartElement(Constants.PREFIX_METADATA, Constants.ELEM_ELEMENT, namespaceMetadata);
        }
        value(writer, valueType, kind, item);
        writer.writeEndElement();
    }
}

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

public void property(final XMLStreamWriter writer, final Property property, final boolean standalone)
        throws XMLStreamException, EdmPrimitiveTypeException {

    if (version.compareTo(ODataServiceVersion.V40) >= 0 && standalone) {
        writer.writeStartElement(Constants.PREFIX_METADATA, Constants.VALUE, namespaceData);
    } else {/*w  ww  .j  a v a  2 s  .  c o m*/
        writer.writeStartElement(Constants.PREFIX_DATASERVICES, property.getName(), namespaceData);
    }

    if (standalone) {
        namespaces(writer);
    }

    EdmTypeInfo typeInfo = null;
    if (StringUtils.isNotBlank(property.getType())) {
        typeInfo = new EdmTypeInfo.Builder().setTypeExpression(property.getType()).build();
        if (!EdmPrimitiveTypeKind.String.getFullQualifiedName().toString().equals(typeInfo.internal())) {
            writer.writeAttribute(Constants.PREFIX_METADATA, namespaceMetadata, Constants.ATTR_TYPE,
                    typeInfo.external(version));
        }
    }

    value(writer, property.getValueType(), typeInfo == null ? null : typeInfo.getPrimitiveTypeKind(),
            property.getValue());
    if (!property.isNull() && property.isLinkedComplex()) {
        links(writer, property.asLinkedComplex().getAssociationLinks());
        links(writer, property.asLinkedComplex().getNavigationLinks());
    }

    writer.writeEndElement();

    for (Annotation annotation : property.getAnnotations()) {
        annotation(writer, annotation, property.getName());
    }
}

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

private void links(final XMLStreamWriter writer, final List<Link> links)
        throws XMLStreamException, EdmPrimitiveTypeException {
    for (Link link : links) {
        writer.writeStartElement(Constants.ATOM_ELEM_LINK);

        if (StringUtils.isNotBlank(link.getRel())) {
            writer.writeAttribute(Constants.ATTR_REL, link.getRel());
        }//from  w  ww  .jav a  2s  .  co m
        if (StringUtils.isNotBlank(link.getTitle())) {
            writer.writeAttribute(Constants.ATTR_TITLE, link.getTitle());
        }
        if (StringUtils.isNotBlank(link.getHref())) {
            writer.writeAttribute(Constants.ATTR_HREF, link.getHref());
        }
        if (StringUtils.isNotBlank(link.getType())) {
            writer.writeAttribute(Constants.ATTR_TYPE, link.getType());
        }

        if (link.getInlineEntity() != null || link.getInlineEntitySet() != null) {
            writer.writeStartElement(Constants.PREFIX_METADATA, Constants.ATOM_ELEM_INLINE, namespaceMetadata);

            if (link.getInlineEntity() != null) {
                writer.writeStartElement(Constants.ATOM_ELEM_ENTRY);
                entity(writer, link.getInlineEntity());
                writer.writeEndElement();
            }
            if (link.getInlineEntitySet() != null) {
                writer.writeStartElement(Constants.ATOM_ELEM_FEED);
                entitySet(writer, link.getInlineEntitySet());
                writer.writeEndElement();
            }

            writer.writeEndElement();
        }

        for (Annotation annotation : link.getAnnotations()) {
            annotation(writer, annotation, null);
        }

        writer.writeEndElement();
    }
}

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

private void annotation(final XMLStreamWriter writer, final Annotation annotation, final String target)
        throws XMLStreamException, EdmPrimitiveTypeException {

    writer.writeStartElement(Constants.PREFIX_METADATA, Constants.ANNOTATION, namespaceMetadata);

    writer.writeAttribute(Constants.ATOM_ATTR_TERM, annotation.getTerm());

    if (target != null) {
        writer.writeAttribute(Constants.ATTR_TARGET, target);
    }/*from ww w.  j  a v  a2 s . com*/

    EdmTypeInfo typeInfo = null;
    if (StringUtils.isNotBlank(annotation.getType())) {
        typeInfo = new EdmTypeInfo.Builder().setTypeExpression(annotation.getType()).build();
        if (!EdmPrimitiveTypeKind.String.getFullQualifiedName().toString().equals(typeInfo.internal())) {
            writer.writeAttribute(Constants.PREFIX_METADATA, namespaceMetadata, Constants.ATTR_TYPE,
                    typeInfo.external(version));
        }
    }

    value(writer, annotation.getValueType(), typeInfo == null ? null : typeInfo.getPrimitiveTypeKind(),
            annotation.getValue());

    writer.writeEndElement();
}

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

private void entity(final XMLStreamWriter writer, final Entity entity)
        throws XMLStreamException, EdmPrimitiveTypeException {
    if (entity.getBaseURI() != null) {
        writer.writeAttribute(XMLConstants.XML_NS_URI, Constants.ATTR_XML_BASE,
                entity.getBaseURI().toASCIIString());
    }//from www .  j  a va2 s  . c  om

    if (serverMode && StringUtils.isNotBlank(entity.getETag())) {
        writer.writeAttribute(namespaceMetadata, Constants.ATOM_ATTR_ETAG, entity.getETag());
    }

    if (entity.getId() != null) {
        writer.writeStartElement(Constants.ATOM_ELEM_ID);
        writer.writeCharacters(entity.getId().toASCIIString());
        writer.writeEndElement();
    }

    writer.writeStartElement(Constants.ATOM_ELEM_CATEGORY);
    writer.writeAttribute(Constants.ATOM_ATTR_SCHEME,
            version.getNamespace(ODataServiceVersion.NamespaceKey.SCHEME));
    if (StringUtils.isNotBlank(entity.getType())) {
        writer.writeAttribute(Constants.ATOM_ATTR_TERM,
                new EdmTypeInfo.Builder().setTypeExpression(entity.getType()).build().external(version));
    }
    writer.writeEndElement();

    if (entity instanceof AbstractODataObject) {
        common(writer, (AbstractODataObject) entity);
    }

    if (serverMode) {
        if (entity.getEditLink() != null) {
            links(writer, Collections.singletonList(entity.getEditLink()));
        }

        if (entity.getSelfLink() != null) {
            links(writer, Collections.singletonList(entity.getSelfLink()));
        }
    }

    links(writer, entity.getAssociationLinks());
    links(writer, entity.getNavigationLinks());
    links(writer, entity.getMediaEditLinks());

    if (serverMode) {
        for (ODataOperation operation : entity.getOperations()) {
            writer.writeStartElement(namespaceMetadata, Constants.ATOM_ELEM_ACTION);
            writer.writeAttribute(Constants.ATTR_METADATA, operation.getMetadataAnchor());
            writer.writeAttribute(Constants.ATTR_TITLE, operation.getTitle());
            writer.writeAttribute(Constants.ATTR_TARGET, operation.getTarget().toASCIIString());
            writer.writeEndElement();
        }
    }

    writer.writeStartElement(Constants.ATOM_ELEM_CONTENT);
    if (entity.isMediaEntity()) {
        if (StringUtils.isNotBlank(entity.getMediaContentType())) {
            writer.writeAttribute(Constants.ATTR_TYPE, entity.getMediaContentType());
        }
        if (entity.getMediaContentSource() != null) {
            writer.writeAttribute(Constants.ATOM_ATTR_SRC, entity.getMediaContentSource().toASCIIString());
        }
        writer.writeEndElement();

        writer.writeStartElement(namespaceMetadata, Constants.PROPERTIES);
        properties(writer, entity.getProperties());
    } else {
        writer.writeAttribute(Constants.ATTR_TYPE, ContentType.APPLICATION_XML.toContentTypeString());
        writer.writeStartElement(namespaceMetadata, Constants.PROPERTIES);
        properties(writer, entity.getProperties());
        writer.writeEndElement();
    }
    writer.writeEndElement();

    for (Annotation annotation : entity.getAnnotations()) {
        annotation(writer, annotation, null);
    }
}

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

private void entity(final Writer outWriter, final Entity entity)
        throws XMLStreamException, EdmPrimitiveTypeException {
    final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter);

    if (entity.getType() == null && entity.getProperties().isEmpty()) {
        writer.writeStartDocument();/*from  w ww .  j a  v a  2 s. co m*/
        writer.setDefaultNamespace(namespaceMetadata);

        entityRef(writer, entity);
    } else {
        startDocument(writer, Constants.ATOM_ELEM_ENTRY);

        entity(writer, entity);
    }

    writer.writeEndElement();
    writer.writeEndDocument();
    writer.flush();
}

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

private void entitySet(final XMLStreamWriter writer, final EntitySet entitySet)
        throws XMLStreamException, EdmPrimitiveTypeException {
    if (entitySet.getBaseURI() != null) {
        writer.writeAttribute(XMLConstants.XML_NS_URI, Constants.ATTR_XML_BASE,
                entitySet.getBaseURI().toASCIIString());
    }/*from w  w  w.j  ava 2s.  c o  m*/

    if (entitySet.getCount() != null) {
        writer.writeStartElement(namespaceMetadata, Constants.ATOM_ELEM_COUNT);
        writer.writeCharacters(Integer.toString(entitySet.getCount()));
        writer.writeEndElement();
    }

    if (entitySet.getId() != null) {
        writer.writeStartElement(Constants.ATOM_ELEM_ID);
        writer.writeCharacters(entitySet.getId().toASCIIString());
        writer.writeEndElement();
    }

    if (entitySet instanceof AbstractODataObject) {
        common(writer, (AbstractODataObject) entitySet);
    }

    for (Entity entity : entitySet.getEntities()) {
        if (entity.getType() == null && entity.getProperties().isEmpty()) {
            entityRef(writer, entity);
            writer.writeEndElement();
        } else {
            writer.writeStartElement(Constants.ATOM_ELEM_ENTRY);
            entity(writer, entity);
            writer.writeEndElement();
        }
    }

    if (serverMode) {
        if (entitySet.getNext() != null) {
            final LinkImpl next = new LinkImpl();
            next.setRel(Constants.NEXT_LINK_REL);
            next.setHref(entitySet.getNext().toASCIIString());

            links(writer, Collections.<Link>singletonList(next));
        }
        if (entitySet.getDeltaLink() != null) {
            final LinkImpl next = new LinkImpl();
            next.setRel(ODataServiceVersion.V40.getNamespace(NamespaceKey.DELTA_LINK_REL));
            next.setHref(entitySet.getDeltaLink().toASCIIString());

            links(writer, Collections.<Link>singletonList(next));
        }
    }
}