Example usage for javax.xml.stream XMLStreamWriter writeStartElement

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

Introduction

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

Prototype

public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException;

Source Link

Document

Writes a start tag to the output

Usage

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

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

    if (standalone) {
        writer.writeStartElement(Constants.PREFIX_METADATA, Constants.VALUE, namespaceData);
        namespaces(writer);//w  w w. j  av a  2s.  c  o  m
    } else {
        writer.writeStartElement(Constants.PREFIX_DATASERVICES, property.getName(), namespaceData);
    }

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

    value(writer, property.getValueType(), typeInfo == null ? null : typeInfo.getPrimitiveTypeKind(),
            property.getValue());
    if (!property.isNull() && property.isComplex() && !property.isCollection()) {
        links(writer, property.asComplex().getAssociationLinks());
        if (serverMode) {
            links(writer, property.asComplex().getNavigationLinks());
        } else {
            writeNavigationLinks(writer, property.asComplex().getNavigationLinks());
        }
    }

    writer.writeEndElement();

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

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

private void writeNavigationLinks(final XMLStreamWriter writer, final List<Link> links)
        throws XMLStreamException, EdmPrimitiveTypeException {
    final Map<String, List<String>> entitySetLinks = new HashMap<String, List<String>>();

    for (Link link : links) {

        if (link.getInlineEntity() != null || link.getInlineEntitySet() != null) {
            writeLink(writer, link, new ExtraContent() {
                @Override//from  w w  w. j a  v  a 2  s  .  c o m
                public void write(XMLStreamWriter writer, Link link)
                        throws XMLStreamException, EdmPrimitiveTypeException {
                    writer.writeStartElement(Constants.PREFIX_METADATA, Constants.ATOM_ELEM_INLINE,
                            namespaceMetadata);
                    if (link.getInlineEntity() != null) {
                        writer.writeStartElement(namespaceAtom, Constants.ATOM_ELEM_ENTRY);
                        entity(writer, link.getInlineEntity());
                        writer.writeEndElement();
                    }
                    if (link.getInlineEntitySet() != null) {
                        writer.writeStartElement(namespaceAtom, Constants.ATOM_ELEM_FEED);
                        entitySet(writer, link.getInlineEntitySet());
                        writer.writeEndElement();
                    }
                    writer.writeEndElement(); // inline    
                }
            });

        } else if (link.getBindingLink() != null) {
            writeLink(writer, link, new ExtraContent() {
                @Override
                public void write(XMLStreamWriter writer, Link link)
                        throws XMLStreamException, EdmPrimitiveTypeException {
                    writer.writeAttribute(Constants.ATTR_HREF, link.getBindingLink());
                }
            });
        } else if (link.getBindingLinks() != null && !link.getBindingLinks().isEmpty()) {
            writeLink(writer, link, new ExtraContent() {
                @Override
                public void write(XMLStreamWriter writer, Link link)
                        throws XMLStreamException, EdmPrimitiveTypeException {
                    writer.writeStartElement(Constants.PREFIX_METADATA, Constants.ATOM_ELEM_INLINE,
                            namespaceMetadata);
                    writer.writeStartElement(namespaceAtom, Constants.ATOM_ELEM_FEED);
                    for (String binding : link.getBindingLinks()) {
                        Entity entity = new Entity();
                        entity.setId(URI.create(binding));
                        inlineEntityRef(writer, entity);
                    }
                    writer.writeEndElement(); //feed            
                    writer.writeEndElement(); //inline
                }
            });
        } else {
            if (isEntitySetNavigation(link)) {
                final List<String> uris;
                if (entitySetLinks.containsKey(link.getTitle())) {
                    uris = entitySetLinks.get(link.getTitle());
                } else {
                    uris = new ArrayList<String>();
                    entitySetLinks.put(link.getTitle(), uris);
                }
                if (StringUtils.isNotBlank(link.getHref())) {
                    uris.add(link.getHref());
                }
            } else {
                writeLink(writer, link, new ExtraContent() {
                    @Override
                    public void write(XMLStreamWriter writer, Link link)
                            throws XMLStreamException, EdmPrimitiveTypeException {
                    }
                });
            }
        }
    }
    for (String title : entitySetLinks.keySet()) {
        final List<String> entitySetLink = entitySetLinks.get(title);
        if (!entitySetLink.isEmpty()) {
            Link link = new Link();
            link.setTitle(title);
            link.setType(Constants.ENTITY_SET_NAVIGATION_LINK_TYPE);
            link.setRel(Constants.NS_NAVIGATION_LINK_REL + title);

            writeLink(writer, link, new ExtraContent() {
                @Override
                public void write(XMLStreamWriter writer, Link link)
                        throws XMLStreamException, EdmPrimitiveTypeException {
                    writer.writeStartElement(Constants.PREFIX_METADATA, Constants.ATOM_ELEM_INLINE,
                            namespaceMetadata);
                    writer.writeStartElement(namespaceAtom, Constants.ATOM_ELEM_FEED);
                    for (String binding : entitySetLink) {
                        Entity entity = new Entity();
                        entity.setId(URI.create(binding));
                        inlineEntityRef(writer, entity);
                    }
                    writer.writeEndElement();
                    writer.writeEndElement();
                }
            });
        }
    }
}

From source file:org.apache.olingo.client.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);
    }//  w w w .j  a v a2s .c  om

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

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

    writer.writeEndElement();
}

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

private void reference(final Writer outWriter, final ResWrap<URI> container) throws XMLStreamException {
    final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter);

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

    writer.writeStartElement(Constants.ATTR_METADATA, Constants.ATTR_REF, Constants.NS_METADATA);
    writer.writeNamespace(Constants.ATTR_METADATA, Constants.NS_METADATA);
    writer.writeAttribute(Constants.ATTR_METADATA, Constants.NS_METADATA, Constants.CONTEXT,
            container.getContextURL().toASCIIString());
    writer.writeAttribute(Constants.ATOM_ATTR_ID, container.getPayload().toASCIIString());
    writer.writeEndElement();

    writer.writeEndDocument();
}

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

private void collection(final XMLStreamWriter writer, final CollectionValue value) throws XMLStreamException {
    for (Value item : value.get()) {
        writer.writeStartElement(Constants.PREFIX_DATASERVICES, Constants.ELEM_ELEMENT,
                version.getNamespaceMap().get(ODataServiceVersion.NS_DATASERVICES));
        value(writer, item);//ww w .jav a 2  s  .c  om
        writer.writeEndElement();
    }
}

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

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

    writer.writeStartElement(Constants.PREFIX_DATASERVICES, property.getName(),
            version.getNamespaceMap().get(ODataServiceVersion.NS_DATASERVICES));
    if (standalone) {
        namespaces(writer);/*from   w w w .  java  2 s  .  c  o m*/
    }
    if (StringUtils.isNotBlank(property.getType())) {
        writer.writeAttribute(Constants.PREFIX_METADATA,
                version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA), Constants.ATTR_TYPE,
                property.getType());
    }

    if (property.getValue().isNull()) {
        writer.writeAttribute(Constants.PREFIX_METADATA,
                version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA), Constants.ATTR_NULL,
                Boolean.TRUE.toString());
    } else {
        value(writer, property.getValue());
    }

    writer.writeEndElement();
}

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  .  ja  v  a  2 s .  com
            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 {// ww  w. j  a va  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 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);
    }//w w w .  j av  a 2  s  .c o m

    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.synapse.commons.json.JsonDataSource.java

public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException {
    XMLStreamReader reader = getReader();
    xmlWriter.writeStartDocument();//w  w w.java2  s . com
    while (reader.hasNext()) {
        int x = reader.next();
        switch (x) {
        case XMLStreamConstants.START_ELEMENT:
            xmlWriter.writeStartElement(reader.getPrefix(), reader.getLocalName(), reader.getNamespaceURI());
            int namespaceCount = reader.getNamespaceCount();
            for (int i = namespaceCount - 1; i >= 0; i--) {
                xmlWriter.writeNamespace(reader.getNamespacePrefix(i), reader.getNamespaceURI(i));
            }
            int attributeCount = reader.getAttributeCount();
            for (int i = 0; i < attributeCount; i++) {
                xmlWriter.writeAttribute(reader.getAttributePrefix(i), reader.getAttributeNamespace(i),
                        reader.getAttributeLocalName(i), reader.getAttributeValue(i));
            }
            break;
        case XMLStreamConstants.START_DOCUMENT:
            break;
        case XMLStreamConstants.CHARACTERS:
            xmlWriter.writeCharacters(reader.getText());
            break;
        case XMLStreamConstants.CDATA:
            xmlWriter.writeCData(reader.getText());
            break;
        case XMLStreamConstants.END_ELEMENT:
            xmlWriter.writeEndElement();
            break;
        case XMLStreamConstants.END_DOCUMENT:
            xmlWriter.writeEndDocument();
            break;
        case XMLStreamConstants.SPACE:
            break;
        case XMLStreamConstants.COMMENT:
            xmlWriter.writeComment(reader.getText());
            break;
        case XMLStreamConstants.DTD:
            xmlWriter.writeDTD(reader.getText());
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            xmlWriter.writeProcessingInstruction(reader.getPITarget(), reader.getPIData());
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            xmlWriter.writeEntityRef(reader.getLocalName());
            break;
        default:
            throw new OMException();
        }
    }
    xmlWriter.writeEndDocument();
    xmlWriter.flush();
    xmlWriter.close();
}