Example usage for javax.xml.stream XMLStreamWriter writeCharacters

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

Introduction

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

Prototype

public void writeCharacters(String text) throws XMLStreamException;

Source Link

Document

Write text to the output

Usage

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

private void entitySet(final XMLStreamWriter writer, final EntityCollection 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  a v a2s .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();
    }

    common(writer, entitySet);

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

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

            links(writer, Collections.<Link>singletonList(next));
        }
        if (entitySet.getDeltaLink() != null) {
            final Link next = new Link();
            next.setRel(Constants.NS_DELTA_LINK_REL);
            next.setHref(entitySet.getDeltaLink().toASCIIString());

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

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

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

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

    writer.writeStartElement(Constants.ELEM_LINKS);
    writer.writeDefaultNamespace(namespaceData);

    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.data.AtomPropertySerializer.java

private void value(final XMLStreamWriter writer, final Value value) throws XMLStreamException {
    if (value.isSimple()) {
        writer.writeCharacters(value.asSimple().get());
    } else if (value.isGeospatial()) {
        this.geoSerializer.serialize(writer, value.asGeospatial().get());
    } else if (value.isCollection()) {
        collection(writer, value.asCollection());
    } else if (value.isComplex()) {
        for (Property property : value.asComplex().get()) {
            property(writer, property, false);
        }/*  w  w w .  j a v  a2  s.  c  o  m*/
    }
}

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

private void common(final XMLStreamWriter writer, final AbstractAtomObject object) throws XMLStreamException {
    if (StringUtils.isNotBlank(object.getTitle())) {
        writer.writeStartElement(Constants.ATOM_ELEM_TITLE);
        writer.writeAttribute(Constants.ATTR_TYPE, TYPE_TEXT);
        writer.writeCharacters(object.getTitle());
        writer.writeEndElement();/*w ww.  j a  va  2s  .  c  om*/
    }

    if (StringUtils.isNotBlank(object.getSummary())) {
        writer.writeStartElement(Constants.ATOM_ELEM_SUMMARY);
        writer.writeAttribute(Constants.ATTR_TYPE, "text");
        writer.writeCharacters(object.getSummary());
        writer.writeEndElement();
    }
}

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

private void entry(final XMLStreamWriter writer, final Entry entry) throws XMLStreamException {
    if (entry.getBaseURI() != null) {
        writer.writeAttribute(XMLConstants.XML_NS_URI, Constants.ATTR_XML_BASE,
                entry.getBaseURI().toASCIIString());
    }/* w  w w .ja  v a 2 s.  c o  m*/

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

    writer.writeStartElement(Constants.ATOM_ELEM_CATEGORY);
    writer.writeAttribute(Constants.ATOM_ATTR_SCHEME,
            version.getNamespaceMap().get(ODataServiceVersion.NS_SCHEME));
    writer.writeAttribute(Constants.ATOM_ATTR_TERM, entry.getType());
    writer.writeEndElement();

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

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

    writer.writeStartElement(Constants.ATOM_ELEM_CONTENT);
    if (entry.isMediaEntry()) {
        if (StringUtils.isNotBlank(entry.getMediaContentType())) {
            writer.writeAttribute(Constants.ATTR_TYPE, entry.getMediaContentType());
        }
        if (StringUtils.isNotBlank(entry.getMediaContentSource())) {
            writer.writeAttribute(Constants.ATOM_ATTR_SRC, entry.getMediaContentSource());
        }
        writer.writeEndElement();

        writer.writeStartElement(version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA),
                Constants.PROPERTIES);
        properties(writer, entry.getProperties());
    } else {
        writer.writeAttribute(Constants.ATTR_TYPE, ContentType.APPLICATION_XML);
        writer.writeStartElement(version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA),
                Constants.PROPERTIES);
        properties(writer, entry.getProperties());
        writer.writeEndElement();
    }
    writer.writeEndElement();
}

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 ww  w.j av a 2s.  c o m*/

    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 link(final Writer outWriter, final Link link) throws XMLStreamException {
    final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter);

    writer.writeStartDocument();/*from w  w w  .j av  a2 s  .  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

@SuppressWarnings("unchecked")
private void value(final XMLStreamWriter writer, final ValueType valueType, final EdmPrimitiveTypeKind kind,
        final Object value) throws XMLStreamException, EdmPrimitiveTypeException {
    if (value == null) {
        writer.writeAttribute(Constants.PREFIX_METADATA, namespaceMetadata, Constants.ATTR_NULL,
                Boolean.TRUE.toString());
        return;//from   ww  w. ja v  a2  s  .  c o m
    }
    switch (valueType) {
    case PRIMITIVE:
        writer.writeCharacters(kind == null ? value.toString()
                : EdmPrimitiveTypeFactory.getInstance(kind) // TODO: add facets
                        .valueToString(value, null, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE,
                                null));
        break;
    case ENUM:
        writer.writeCharacters(value.toString());
        break;
    case GEOSPATIAL:
        geoSerializer.serialize(writer, (Geospatial) value);
        break;
    case COLLECTION_PRIMITIVE:
    case COLLECTION_GEOSPATIAL:
    case COLLECTION_ENUM:
    case COLLECTION_COMPLEX:
    case COLLECTION_LINKED_COMPLEX:
        collection(writer, valueType.getBaseType(), kind, (List<?>) value);
        break;
    case LINKED_COMPLEX:
        for (Property property : ((LinkedComplexValue) value).getValue()) {
            property(writer, property, false);
        }
        break;
    case COMPLEX:
        for (Property property : (List<Property>) value) {
            property(writer, property, false);
        }
        break;
    }
}

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 ww w  .  j  a  va  2 s.c  o  m

    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 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  ww w .  j av  a 2  s . com

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