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 localName, String value) throws XMLStreamException;

Source Link

Document

Writes an attribute to the output stream without a prefix.

Usage

From source file:org.eclipse.koneki.protocols.omadm.client.basic.DMBasicSession.java

private void writeGenericAlert(final XMLStreamWriter writer, final DMGenericAlert[] genAlerts)
        throws XMLStreamException {
    for (final DMGenericAlert genericAlert : genAlerts) {
        final String cmdID = String.valueOf(this.idGenerator.nextCmdID());
        final String alertCode = "1226"; //$NON-NLS-1$
        final DMMeta meta = new DMMeta();
        meta.put(DMMeta.TYPE, genericAlert.getType());
        meta.put(DMMeta.FORMAT, genericAlert.getFormat());
        meta.put(DMMeta.MARK, genericAlert.getMark());
        this.commandSends.put(cmdID, new Object[] { alertCode, genericAlert.getCorrelator(),
                new DMItem[] { new DMItem(null, genericAlert.getSource(), meta, genericAlert.getData()) } });
        writer.writeStartElement("Alert"); //$NON-NLS-1$
        // CHECKSTYLE:OFF (imbricated blocks)
        {// w  w  w  .  j  av a 2s .co m
            writer.writeStartElement("CmdID"); //$NON-NLS-1$
            writer.writeCharacters(cmdID);
            writer.writeEndElement();
            writer.writeStartElement("Data"); //$NON-NLS-1$
            writer.writeCharacters(alertCode);
            writer.writeEndElement();
            if (genericAlert.getCorrelator() != null) {
                writer.writeStartElement("Correlator"); //$NON-NLS-1$
                writer.writeCharacters(genericAlert.getCorrelator());
                writer.writeEndElement();
            }
            writer.writeStartElement("Item"); //$NON-NLS-1$
            {
                if (genericAlert.getSource() != null) {
                    writer.writeStartElement("Source"); //$NON-NLS-1$
                    {
                        writer.writeStartElement("LocURI"); //$NON-NLS-1$
                        writer.writeCharacters(genericAlert.getSource());
                        writer.writeEndElement();
                    }
                    writer.writeEndElement();
                }
                writer.writeStartElement("Meta"); //$NON-NLS-1$
                {
                    writer.writeStartElement("Type"); //$NON-NLS-1$
                    writer.writeAttribute("xmlns", "syncml:metinf"); //$NON-NLS-1$ //$NON-NLS-2$
                    writer.writeCharacters(genericAlert.getType());
                    writer.writeEndElement();
                    writer.writeStartElement("Format"); //$NON-NLS-1$
                    writer.writeAttribute("xmlns", "syncml:metinf"); //$NON-NLS-1$ //$NON-NLS-2$
                    writer.writeCharacters(genericAlert.getFormat());
                    writer.writeEndElement();
                    writer.writeStartElement("Mark"); //$NON-NLS-1$
                    writer.writeAttribute("xmlns", "syncml:metinf"); //$NON-NLS-1$ //$NON-NLS-2$
                    writer.writeCharacters(genericAlert.getMark());
                    writer.writeEndElement();
                }
                writer.writeEndElement();
                writer.writeStartElement("Data"); //$NON-NLS-1$
                writer.writeCharacters(genericAlert.getData());
                writer.writeEndElement();
            }
            writer.writeEndElement();
        }
        writer.writeEndElement();
    }
    // CHECKSTYLE:ON
}

From source file:org.eclipse.koneki.protocols.omadm.client.basic.DMBasicSession.java

private void writeReplace(final XMLStreamWriter writer, final DMNode[] nodes) throws XMLStreamException {
    if (this.devInfoNodes.length >= 1) {
        boolean sameFormat = true;
        boolean sameType = true;
        if (this.devInfoNodes.length >= 2) {
            for (int i = 1; i < this.devInfoNodes.length; ++i) {
                if (sameFormat && !this.devInfoNodes[i].getFormat().equals(this.devInfoNodes[0].getFormat())) {
                    sameFormat = false;/*from www .j  a  v a2s  . c o  m*/
                }
                if (sameType && !this.devInfoNodes[i].getType().equals(this.devInfoNodes[0].getType())) {
                    sameType = false;
                }
            }
        }
        writer.writeStartElement("Replace"); //$NON-NLS-1$
        // CHECKSTYLE:OFF (imbricated blocks)
        {
            writer.writeStartElement("CmdID"); //$NON-NLS-1$
            writer.writeCharacters(String.valueOf(this.idGenerator.nextCmdID()));
            writer.writeEndElement();
            if (sameFormat || sameType) {
                writer.writeStartElement("Meta"); //$NON-NLS-1$
                {
                    if (sameFormat) {
                        writer.writeStartElement("Format"); //$NON-NLS-1$
                        writer.writeAttribute("xmlns", "syncml:metinf"); //$NON-NLS-1$ //$NON-NLS-2$
                        writer.writeCharacters(this.devInfoNodes[0].getFormat());
                        writer.writeEndElement();
                    }
                    if (sameType) {
                        writer.writeStartElement("Type"); //$NON-NLS-1$
                        writer.writeAttribute("xmlns", "syncml:metinf"); //$NON-NLS-1$ //$NON-NLS-2$
                        writer.writeCharacters(this.devInfoNodes[0].getType());
                        writer.writeEndElement();
                    }
                }
                writer.writeEndElement();
            }
            for (final DMNode devInfoNode : this.devInfoNodes) {
                writer.writeStartElement("Item"); //$NON-NLS-1$
                {
                    writer.writeStartElement("Source"); //$NON-NLS-1$
                    {
                        writer.writeStartElement("LocURI"); //$NON-NLS-1$
                        writer.writeCharacters(devInfoNode.getURI());
                        writer.writeEndElement();
                    }
                    writer.writeEndElement();
                    if (!sameFormat || !sameType) {
                        writer.writeStartElement("Meta"); //$NON-NLS-1$
                        {
                            if (!sameFormat) {
                                writer.writeStartElement("Format"); //$NON-NLS-1$
                                writer.writeAttribute("xmlns", "syncml:metinf"); //$NON-NLS-1$ //$NON-NLS-2$
                                writer.writeCharacters(devInfoNode.getFormat());
                                writer.writeEndElement();
                            }
                            if (!sameType) {
                                writer.writeStartElement("Type"); //$NON-NLS-1$
                                writer.writeAttribute("xmlns", "syncml:metinf"); //$NON-NLS-1$ //$NON-NLS-2$
                                writer.writeCharacters(devInfoNode.getType());
                                writer.writeEndElement();
                            }
                        }
                        writer.writeEndElement();
                    }
                    writer.writeStartElement("Data"); //$NON-NLS-1$
                    writer.writeCharacters(devInfoNode.getData());
                    writer.writeEndElement();
                }
                writer.writeEndElement();
            }
        }
        writer.writeEndElement();
        // CHECKSTYLE:ON
    }
}

From source file:org.eclipse.smila.datamodel.record.stax.RecordWriter.java

/**
 * write a record to an XML stream.//  w w w . j  a v  a  2s.  com
 *
 * @param staxWriter
 *          target XML stream
 * @param record
 *          the record to write
 * @param addNamespace
 *          add xmlns attribute
 * @throws XMLStreamException
 *           StAX error
 */
private void writeRecord(final XMLStreamWriter staxWriter, final Record record, final boolean addNamespace)
        throws XMLStreamException {
    staxWriter.setDefaultNamespace(RecordBuilder.NAMESPACE_RECORD);
    writeStartElement(staxWriter, RecordBuilder.TAG_RECORD);
    if (addNamespace) {
        staxWriter.writeDefaultNamespace(RecordBuilder.NAMESPACE_RECORD);
    }
    staxWriter.writeAttribute(RecordBuilder.ATTRIBUTE_VERSION, RecordBuilder.SCHEMA_VERSION_RECORD);
    newline(staxWriter);
    if (record.getId() != null) {
        _idWriter.writeId(staxWriter, record.getId());
        staxWriter.setDefaultNamespace(RecordBuilder.NAMESPACE_RECORD);
    }

    if (record.getMetadata() != null) {
        writeMetadata(staxWriter, record.getMetadata());
    }
    if (record.hasAttachments()) {
        final Iterator<String> attachmentNames = record.getAttachmentNames();
        while (attachmentNames.hasNext()) {
            final String attachmentName = attachmentNames.next();
            writeTextElement(staxWriter, RecordBuilder.TAG_ATTACHMENT, attachmentName);
        }
    }
    writeEndElement(staxWriter);
}

From source file:org.eclipse.smila.datamodel.record.stax.RecordWriter.java

/**
 * append Attribute to stream./*from   www . j  ava  2 s . c o  m*/
 *
 * @param staxWriter
 *          target XML stream
 * @param attribute
 *          the possibly attribute object.
 * @throws XMLStreamException
 *           StAX error
 */
private void writeAttribute(final XMLStreamWriter staxWriter, final Attribute attribute)
        throws XMLStreamException {
    writeStartElement(staxWriter, RecordBuilder.TAG_ATTRIBUTE);
    staxWriter.writeAttribute(RecordBuilder.ATTRIBUTE_NAME, attribute.getName());
    newline(staxWriter);
    if (attribute.hasAnnotations()) {
        writeAnnotations(staxWriter, attribute);
    }
    if (attribute.hasLiterals()) {
        List<Literal> annotatedLiterals = null;
        boolean unnanotatedLiteralsStarted = false;
        if (attribute.hasLiterals()) {
            for (final Literal literal : attribute.getLiterals()) {
                if (literal.hasAnnotations()) {
                    if (annotatedLiterals == null) {
                        annotatedLiterals = new ArrayList<Literal>();
                    }
                    annotatedLiterals.add(literal);
                } else {
                    if (!unnanotatedLiteralsStarted) {
                        writeStartElement(staxWriter, RecordBuilder.TAG_LITERAL);
                        newline(staxWriter);
                        unnanotatedLiteralsStarted = true;
                    }
                    writeLiteral(staxWriter, literal);
                }
            }
        }
        if (unnanotatedLiteralsStarted) {
            writeEndElement(staxWriter);
        }
        if (annotatedLiterals != null) {
            for (final Literal literal : attribute.getLiterals()) {
                writeStartElement(staxWriter, RecordBuilder.TAG_LITERAL);
                newline(staxWriter);
                writeLiteral(staxWriter, literal);
                writeEndElement(staxWriter);
            }
        }
    }
    if (attribute.hasObjects()) {
        for (final MObject object : attribute.getObjects()) {
            writeStartElement(staxWriter, RecordBuilder.TAG_OBJECT);
            if (object.getSemanticType() != null) {
                staxWriter.writeAttribute(RecordBuilder.ATTRIBUTE_SEMANTICTYPE, object.getSemanticType());
            }
            writeMetadata(staxWriter, object);
            writeEndElement(staxWriter);
        }
    }
    writeEndElement(staxWriter);
}

From source file:org.eclipse.smila.datamodel.record.stax.RecordWriter.java

/**
 * append Literal to stream./*w  w  w .  ja  v  a2  s . c o  m*/
 *
 * @param staxWriter
 *          target XML stream
 * @param literal
 *          the possibly attribute object.
 * @throws XMLStreamException
 *           StAX error
 */
private void writeLiteral(final XMLStreamWriter staxWriter, final Literal literal) throws XMLStreamException {
    if (literal.getValue() != null) {
        writeStartElement(staxWriter, RecordBuilder.TAG_VALUE);
        switch (literal.getDataType()) {
        case INT:
            staxWriter.writeAttribute(RecordBuilder.ATTRIBUTE_TYPE, "int");
            break;
        case FP:
            staxWriter.writeAttribute(RecordBuilder.ATTRIBUTE_TYPE, "fp");
            break;
        case BOOL:
            staxWriter.writeAttribute(RecordBuilder.ATTRIBUTE_TYPE, "bool");
            break;
        case DATE:
            staxWriter.writeAttribute(RecordBuilder.ATTRIBUTE_TYPE, "date");
            break;
        case TIME:
            staxWriter.writeAttribute(RecordBuilder.ATTRIBUTE_TYPE, "time");
            break;
        case DATETIME:
            staxWriter.writeAttribute(RecordBuilder.ATTRIBUTE_TYPE, "datetime");
            break;
        default: // write nothing for strings
        }
        if (literal.getSemanticType() != null) {
            staxWriter.writeAttribute(RecordBuilder.ATTRIBUTE_SEMANTICTYPE, literal.getSemanticType());
        }
        writeText(staxWriter, literal.getStringValue());
        writeEndElement(staxWriter);
        writeAnnotations(staxWriter, literal);
    }
}

From source file:org.eclipse.smila.datamodel.record.stax.RecordWriter.java

/**
 * append Annotation to stream.//w  w w. java2  s  .c  om
 *
 * @param staxWriter
 *          target XML stream
 * @param name
 *          name of annotation
 * @param annotation
 *          the annotation to append.
 * @throws XMLStreamException
 *           StAX error
 */
private void writeAnnotation(final XMLStreamWriter staxWriter, final String name, final Annotation annotation)
        throws XMLStreamException {
    writeStartElement(staxWriter, RecordBuilder.TAG_ANNOTATION);
    staxWriter.writeAttribute(RecordBuilder.ATTRIBUTE_NAME, name);
    newline(staxWriter);
    if (annotation.hasAnonValues()) {
        for (final String value : annotation.getAnonValues()) {
            writeTextElement(staxWriter, RecordBuilder.TAG_VALUE, value);
        }
    }
    if (annotation.hasNamedValues()) {
        final Iterator<String> valueNames = annotation.getValueNames();
        while (valueNames.hasNext()) {
            final String valueName = valueNames.next();
            writeStartElement(staxWriter, RecordBuilder.TAG_VALUE);
            staxWriter.writeAttribute(RecordBuilder.ATTRIBUTE_NAME, valueName);
            writeText(staxWriter, annotation.getNamedValue(valueName));
            writeEndElement(staxWriter);
        }
    }
    writeAnnotations(staxWriter, annotation);
    writeEndElement(staxWriter);
}

From source file:org.eclipse.smila.datamodel.xml.StaxRecordWriter.java

/**
 * write a record to an XML stream.//w  w  w  . jav a2s. c  o m
 * 
 * @param staxWriter
 *          target XML stream
 * @param record
 *          the record to write
 * @param addNamespace
 *          add xmlns attribute
 * @throws XMLStreamException
 *           StAX error
 */
private void writeRecord(final XMLStreamWriter staxWriter, final Record record, final boolean addNamespace)
        throws XMLStreamException {
    staxWriter.setDefaultNamespace(XmlConstants.NAMESPACE_RECORD);
    newline(staxWriter);
    writeStartElement(staxWriter, XmlConstants.TAG_RECORD);
    if (addNamespace) {
        staxWriter.writeDefaultNamespace(XmlConstants.NAMESPACE_RECORD);
    }
    staxWriter.writeAttribute(XmlConstants.ATTRIBUTE_VERSION, XmlConstants.SCHEMA_VERSION_RECORD);
    newline(staxWriter);
    if (record.getId() != null) {
        staxWriter.setDefaultNamespace(XmlConstants.NAMESPACE_RECORD);
    }

    if (record.getMetadata() != null) {
        writeMetadata(staxWriter, record.getMetadata());
    }
    if (record.hasAttachments()) {
        final Iterator<String> attachmentNames = record.getAttachmentNames();
        while (attachmentNames.hasNext()) {
            final String attachmentName = attachmentNames.next();
            writeTextElement(staxWriter, XmlConstants.TAG_ATTACHMENT, attachmentName);
        }
    }
    writeEndElement(staxWriter);
}

From source file:org.eclipse.smila.datamodel.xml.StaxRecordWriter.java

/**
 * append {@link Value} to stream.//from w w  w.ja v  a  2s  . co  m
 * 
 * @param staxWriter
 *          target XML stream
 * @param key
 *          the key
 * @param value
 *          the {@link Value} object.
 * @throws XMLStreamException
 *           StAX error
 */
private void writeValue(final XMLStreamWriter staxWriter, final String key, final Value value)
        throws XMLStreamException {
    writeStartElement(staxWriter, XmlConstants.TAG_VAL);
    if (key != null) {
        staxWriter.writeAttribute(XmlConstants.ATTRIBUTE_KEY, key);
    }
    final ValueType valueType = value.getValueType();
    if (valueType != ValueType.STRING) {
        staxWriter.writeAttribute(XmlConstants.ATTRIBUTE_TYPE, valueType.name().toLowerCase());
    }
    writeText(staxWriter, value.asString());
    writeEndElement(staxWriter);
}

From source file:org.eclipse.smila.datamodel.xml.StaxRecordWriter.java

/**
 * append {@link AnySeq} to stream./*from   www .j  av  a2  s .com*/
 * 
 * @param staxWriter
 *          target XML stream
 * @param key
 *          the key
 * @param anySeq
 *          the {@link AnySeq} object.
 * @throws XMLStreamException
 *           StAX error
 */
private void writeSeq(final XMLStreamWriter staxWriter, final String key, final AnySeq anySeq)
        throws XMLStreamException {
    writeStartElement(staxWriter, XmlConstants.TAG_SEQ);
    if (key != null) {
        staxWriter.writeAttribute(XmlConstants.ATTRIBUTE_KEY, key);
    }
    if (!anySeq.isEmpty()) {
        newline(staxWriter);
        final Iterator<Any> anyIterator = anySeq.iterator();
        while (anyIterator.hasNext()) {
            final Any any = anyIterator.next();
            if (any.isValue()) {
                writeValue(staxWriter, null, (Value) any);
            } else if (any.isSeq()) {
                writeSeq(staxWriter, null, (AnySeq) any);
            } else if (any.isMap()) {
                writeMap(staxWriter, null, (AnyMap) any);
            }
        }
    }
    writeEndElement(staxWriter);
}

From source file:org.eclipse.smila.datamodel.xml.StaxRecordWriter.java

/**
 * append {@link AnyMap} to stream./*from ww w . j  a va 2  s. com*/
 * 
 * @param staxWriter
 *          target XML stream
 * @param key
 *          the key
 * @param anyMap
 *          the {@link AnyMap} object.
 * @throws XMLStreamException
 *           StAX error
 */
private void writeMap(final XMLStreamWriter staxWriter, final String key, final AnyMap anyMap)
        throws XMLStreamException {
    writeStartElement(staxWriter, XmlConstants.TAG_MAP);
    if (key != null) {
        staxWriter.writeAttribute(XmlConstants.ATTRIBUTE_KEY, key);
    }
    if (!anyMap.isEmpty()) {
        newline(staxWriter);
        for (final Entry<String, Any> entry : anyMap.entrySet()) {
            final String entryKey = entry.getKey();
            final Any any = entry.getValue();
            if (any.isValue()) {
                writeValue(staxWriter, entryKey, (Value) any);
            } else if (any.isSeq()) {
                writeSeq(staxWriter, entryKey, (AnySeq) any);
            } else if (any.isMap()) {
                writeMap(staxWriter, entryKey, (AnyMap) any);
            }
        }
    }
    writeEndElement(staxWriter);
}