Example usage for javax.xml.stream XMLStreamWriter writeAttribute

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

Introduction

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

Prototype


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

Source Link

Document

Writes an attribute to the output stream

Usage

From source file:org.apache.axis2.policy.model.MTOM11Assertion.java

public void serialize(XMLStreamWriter writer) throws XMLStreamException {
    String prefix = writer.getPrefix(NS);

    if (prefix == null) {
        prefix = PREFIX;/*from  w  w  w  .  j  a  va2  s.  co  m*/
        writer.setPrefix(PREFIX, NS);
    }

    writer.writeStartElement(PREFIX, MTOM_LN, NS);

    if (optional) {
        writer.writeAttribute(Constants.ATTR_WSP, null, Constants.Q_ELEM_OPTIONAL_ATTR.getLocalPart(), "true");
    }

    writer.writeNamespace(PREFIX, NS);
    writer.writeEndElement();

}

From source file:org.apache.flex.compiler.config.Configuration.java

/**
 * Write a map of values to rdf/xml//w  w  w . j a  va 2s  . co  m
 * 
 * @param writer
 * @param namespaceURI
 * @param localName
 * @param mapData
 * @throws XMLStreamException
 */
private void writeMap(XMLStreamWriter writer, String namespaceURI, String localName,
        Map<String, String> mapData) throws XMLStreamException {
    if (mapData.size() > 0) {
        writer.writeStartElement(namespaceURI, localName);
        if ((mapData.size() == 1) && (mapData.get("x-default") != null)) {
            String data = mapData.get("x-default");
            writer.writeCharacters(data);
        } else {
            writer.writeStartElement(RDF_URI, "Alt");
            for (final String key : mapData.keySet()) {
                final String value = mapData.get(key);
                writer.writeStartElement(RDF_URI, "li");
                writer.writeAttribute("xml", "", "lang", key);
                writer.writeCharacters(value);
                writer.writeEndElement();
            }
            writer.writeEndElement();
        }

        writer.writeEndElement();
    }

}

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

private void value(final XMLStreamWriter writer, final ValueType valueType, final EdmPrimitiveTypeKind kind,
        final Object value) throws XMLStreamException, EdmPrimitiveTypeException {
    if (value == null || (valueType == ValueType.COMPLEX && ((ComplexValue) value).getValue().isEmpty())) {
        writer.writeAttribute(Constants.PREFIX_METADATA, namespaceMetadata, Constants.ATTR_NULL,
                Boolean.TRUE.toString());
        return;//from  ww w  .j av a  2  s.  com
    }
    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:
        collection(writer, valueType.getBaseType(), kind, (List<?>) value);
        break;
    case COMPLEX:
        for (Property property : ((ComplexValue) value).getValue()) {
            property(writer, property, false);
        }
        break;
    case ENTITY:
    case COLLECTION_ENTITY:
        throw new ODataRuntimeException("Entities cannot appear in this payload");
    }
}

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);/*from   w w  w .j  a v a  2  s .c  om*/
    } 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 reference(final Writer outWriter, final ResWrap<URI> container) throws XMLStreamException {
    final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter);

    writer.writeStartDocument();/*from   www. j  ava2 s. c om*/

    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

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);/*  w w w. j a  v a  2 s .co  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

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

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 {//from   w  w w  .  java2  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.synapse.commons.json.JsonDataSource.java

public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException {
    XMLStreamReader reader = getReader();
    xmlWriter.writeStartDocument();//from   w  ww .jav a2  s .co  m
    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();
}

From source file:org.deegree.services.sos.SOSController.java

private void doGetFeatureOfInterest(GetFeatureOfInterest foi, HttpResponseBuffer response)
        throws IOException, XMLStreamException {
    XMLStreamWriter xmlWriter = response.getXMLWriter();

    List<String> foiIDs = Arrays.asList(foi.getFoiID());

    xmlWriter.writeStartElement(SA_PREFIX, "SamplingFeatureCollection", SA_NS);
    xmlWriter.writeNamespace(SA_PREFIX, SA_NS);
    xmlWriter.writeNamespace(XSI_PREFIX, XSINS);
    xmlWriter.writeNamespace(XLINK_PREFIX, XLNNS);
    xmlWriter.writeNamespace(GML_PREFIX, GMLNS);

    xmlWriter.writeAttribute(XSI_PREFIX, XSINS, "schemaLocation",
            "http://www.opengis.net/sampling/1.0 http://schemas.opengis.net/sampling/1.0.0/sampling.xsd");

    // TODO a url should be specified in the xlink:href of sampledFeature
    xmlWriter.writeEmptyElement(SA_PREFIX, "sampledFeature", SA_NS);

    for (Offering offering : sosService.getAllOfferings()) {
        for (Procedure procedure : offering.getProcedures()) {
            if (foiIDs.contains(procedure.getFeatureOfInterestHref())) {
                Geometry procGeometry = procedure.getLocation();
                if (procGeometry instanceof Point) { // TODO check if the procedure can have some other geometries
                    // and if so,
                    // handle them

                    xmlWriter.writeStartElement(SA_PREFIX, "member", SA_NS);

                    xmlWriter.writeStartElement(SA_PREFIX, "SamplingPoint", SA_NS);
                    xmlWriter.writeStartElement(GML_PREFIX, "name", GMLNS);
                    xmlWriter.writeCharacters(procedure.getFeatureOfInterestHref());
                    // TODO if the GetFeatureOfInterest does not provide a foi but a location instead, search
                    // for all
                    // sensors
                    // inside that BBOX
                    xmlWriter.writeEndElement();

                    // TODO a url should be specified in the xlink:href of sampledFeature
                    xmlWriter.writeEmptyElement(SA_PREFIX, "sampledFeature", SA_NS);

                    xmlWriter.writeStartElement(SA_PREFIX, "position", SA_NS);
                    // exporting a gml:Point TODO use GML encoder
                    xmlWriter.writeStartElement(GML_PREFIX, "Point", GMLNS);
                    // have the last part of the foiID as the Point id attribute
                    String[] foiParts = procedure.getFeatureOfInterestHref().split(":");
                    xmlWriter.writeAttribute(GML_PREFIX, GMLNS, "id", foiParts[foiParts.length - 1]);

                    xmlWriter.writeStartElement(GML_PREFIX, "pos", GMLNS);
                    ICRS foiCRS = null;//  w  w  w  . j  a va 2  s . c om
                    foiCRS = procGeometry.getCoordinateSystem();
                    xmlWriter.writeAttribute("srsName", foiCRS.getCode().toString());

                    Point p = (Point) procGeometry;
                    xmlWriter.writeCharacters(p.get0() + " " + p.get1());
                    xmlWriter.writeEndElement(); // gml:pos
                    xmlWriter.writeEndElement(); // gml:Point
                    xmlWriter.writeEndElement(); // gml:position
                    xmlWriter.writeEndElement(); // sa:SamplingPoint
                    xmlWriter.writeEndElement(); // sa:member
                }
            }
        }
    }

    xmlWriter.writeEndElement(); // sa:SamplingFeatureCollection
    xmlWriter.writeEndDocument();
    xmlWriter.flush();
}