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.intermine.template.xml.TemplateQueryBinding.java

/**
 * Convert a TemplateQuery to XML and write XML to given writer.
 *
 * @param template the TemplateQuery//from   ww  w.j av a2  s. c  om
 * @param writer the XMLStreamWriter to write to
 * @param version the version number of the XML format
 */
public void doMarshal(TemplateQuery template, XMLStreamWriter writer, int version) {
    if (template == null) {
        throw new NullPointerException("template must not be null");
    }
    if (writer == null) {
        throw new NullPointerException("writer must not be null");
    }
    try {
        writer.writeCharacters("\n");
        writer.writeStartElement("template");
        writer.writeAttribute("name", template.getName());
        if (template.getTitle() != null) {
            writer.writeAttribute("title", template.getTitle());
        }
        if (template.getComment() == null) {
            writer.writeAttribute("comment", "");
        } else {
            writer.writeAttribute("comment", template.getComment());
        }

        doMarshal(template, template.getName(), template.getModel().getName(), writer, version);
        writer.writeEndElement();
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.intermine.template.xml.TemplateQueryBinding.java

/**
 * {@inheritDoc}/*w w  w .j  a  v  a  2s  .  c  o  m*/
 */
@Override
public void doAdditionalConstraintStuff(PathQuery query, PathConstraint constraint, XMLStreamWriter writer)
        throws XMLStreamException {
    TemplateQuery template = (TemplateQuery) query;
    if (template.isEditable(constraint)) {
        writer.writeAttribute("editable", "true");
    } else {
        writer.writeAttribute("editable", "false");
    }
    String description = template.getConstraintDescription(constraint);
    if (description != null) {
        writer.writeAttribute("description", description);
    }
    SwitchOffAbility switchOffAbility = template.getSwitchOffAbility(constraint);
    if (SwitchOffAbility.ON.equals(switchOffAbility)) {
        writer.writeAttribute("switchable", "on");
    } else if (SwitchOffAbility.OFF.equals(switchOffAbility)) {
        writer.writeAttribute("switchable", "off");
    }
}

From source file:org.maodian.flyingcat.xmpp.codec.AbstractCodec.java

protected void writeAttributeIfNotBlank(XMLStreamWriter xmlsw, String localName, String value)
        throws XMLStreamException {
    if (StringUtils.isNotBlank(value)) {
        xmlsw.writeAttribute(localName, value);
    }// w w w  . j  a  v a 2  s . com
}

From source file:org.maodian.flyingcat.xmpp.codec.AbstractCodec.java

protected void writeRequiredAttribute(XMLStreamWriter xmlsw, String localName, String value)
        throws XMLStreamException {
    if (StringUtils.isBlank(value)) {
        throw new XmppException(StreamError.INTERNAL_SERVER_ERROR);
    }/*from   w ww.j a va  2  s .  com*/
    xmlsw.writeAttribute(localName, value);
}

From source file:org.mule.module.xml.util.XMLUtils.java

private static void writeStartElement(XMLStreamReader reader, XMLStreamWriter writer)
        throws XMLStreamException {
    String local = reader.getLocalName();
    String uri = reader.getNamespaceURI();
    String prefix = reader.getPrefix();
    if (prefix == null) {
        prefix = "";
    }// ww w  . jav a2  s.com

    //        System.out.println("STAXUTILS:writeStartElement : node name : " + local +  " namespace URI" + uri);
    boolean writeElementNS = false;
    if (uri != null) {
        String boundPrefix = writer.getPrefix(uri);
        if (boundPrefix == null || !prefix.equals(boundPrefix)) {
            writeElementNS = true;
        }
    }

    // Write out the element name
    if (uri != null) {
        if (prefix.length() == 0 && StringUtils.isEmpty(uri)) {
            writer.writeStartElement(local);
            writer.setDefaultNamespace(uri);

        } else {
            writer.writeStartElement(prefix, local, uri);
            writer.setPrefix(prefix, uri);
        }
    } else {
        writer.writeStartElement(local);
    }

    // Write out the namespaces
    for (int i = 0; i < reader.getNamespaceCount(); i++) {
        String nsURI = reader.getNamespaceURI(i);
        String nsPrefix = reader.getNamespacePrefix(i);
        if (nsPrefix == null) {
            nsPrefix = "";
        }

        if (nsPrefix.length() == 0) {
            writer.writeDefaultNamespace(nsURI);
        } else {
            writer.writeNamespace(nsPrefix, nsURI);
        }

        if (nsURI.equals(uri) && nsPrefix.equals(prefix)) {
            writeElementNS = false;
        }
    }

    // Check if the namespace still needs to be written.
    // We need this check because namespace writing works
    // different on Woodstox and the RI.
    if (writeElementNS) {
        if (prefix.length() == 0) {
            writer.writeDefaultNamespace(uri);
        } else {
            writer.writeNamespace(prefix, uri);
        }
    }

    // Write out attributes
    for (int i = 0; i < reader.getAttributeCount(); i++) {
        String ns = reader.getAttributeNamespace(i);
        String nsPrefix = reader.getAttributePrefix(i);
        if (ns == null || ns.length() == 0) {
            writer.writeAttribute(reader.getAttributeLocalName(i), reader.getAttributeValue(i));
        } else if (nsPrefix == null || nsPrefix.length() == 0) {
            writer.writeAttribute(reader.getAttributeNamespace(i), reader.getAttributeLocalName(i),
                    reader.getAttributeValue(i));
        } else {
            writer.writeAttribute(reader.getAttributePrefix(i), reader.getAttributeNamespace(i),
                    reader.getAttributeLocalName(i), reader.getAttributeValue(i));
        }

    }
}

From source file:org.opentox.toxotis.ontology.impl.MetaInfoImpl.java

private void writeMetaDatumResourceToStAX(String metaDatumNS, String metaDatumName, ResourceValue value,
        javax.xml.stream.XMLStreamWriter writer) throws XMLStreamException {
    if (value != null && value.getUri() != null && value.getUri().toString() != null
            && !(value.getUri().toString().isEmpty())) {
        String stringVal = value.getUri().toString();
        writer.writeEmptyElement(metaDatumNS + ":" + metaDatumName);
        writer.writeAttribute("rdf:resource", stringVal);
    }//from  www  .j av  a 2  s  .  c  om
}

From source file:org.ops4j.pax.exam.karaf.container.internal.DependenciesDeployer.java

/**
 * Write a feature xml structure for test dependencies specified as ProvisionOption
 * in system to the given writer/*from w  w  w  .  j av a2 s .co  m*/
 * 
 * @param writer where to write the feature xml
 * @param provisionOptions dependencies
 */
static void writeDependenciesFeature(Writer writer, ProvisionOption<?>... provisionOptions) {
    XMLOutputFactory xof = XMLOutputFactory.newInstance();
    xof.setProperty("javax.xml.stream.isRepairingNamespaces", true);
    XMLStreamWriter sw = null;
    try {
        sw = xof.createXMLStreamWriter(writer);
        sw.writeStartDocument("UTF-8", "1.0");
        sw.setDefaultNamespace(KARAF_FEATURE_NS);
        sw.writeCharacters("\n");
        sw.writeStartElement("features");
        sw.writeAttribute("name", "test-dependencies");
        sw.writeCharacters("\n");
        sw.writeStartElement("feature");
        sw.writeAttribute("name", "test-dependencies");
        sw.writeCharacters("\n");
        for (ProvisionOption<?> provisionOption : provisionOptions) {
            if (provisionOption.getURL().startsWith("link")
                    || provisionOption.getURL().startsWith("scan-features")) {
                // well those we've already handled at another location...
                continue;
            }
            sw.writeStartElement("bundle");
            if (provisionOption.getStartLevel() != null) {
                sw.writeAttribute("start-level", provisionOption.getStartLevel().toString());
            }
            sw.writeCharacters(provisionOption.getURL());
            endElement(sw);
        }
        endElement(sw);
        endElement(sw);
        sw.writeEndDocument();
    } catch (XMLStreamException e) {
        throw new RuntimeException("Error writing feature " + e.getMessage(), e);
    } finally {
        close(sw);
    }
}

From source file:org.osaf.cosmo.model.text.XhtmlCollectionFormat.java

public String format(CollectionItem collection) {
    try {//from   w  ww.j ava 2 s.co  m
        StringWriter sw = new StringWriter();
        XMLStreamWriter writer = createXmlWriter(sw);

        writer.writeStartElement("div");
        writer.writeAttribute("class", "collection");

        writer.writeCharacters("Collection: ");

        if (collection.getDisplayName() != null) {
            writer.writeStartElement("span");
            writer.writeAttribute("class", "name");
            writer.writeCharacters(collection.getDisplayName());
            writer.writeEndElement();
        }

        if (collection.getUid() != null) {
            writer.writeCharacters(" (uuid ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "uuid");
            writer.writeCharacters(collection.getUid());
            writer.writeEndElement();
            writer.writeCharacters(")");
        }

        writer.writeEndElement();
        writer.close();

        return sw.toString();
    } catch (XMLStreamException e) {
        throw new RuntimeException("Error formatting XML", e);
    }
}

From source file:org.osaf.cosmo.model.text.XhtmlPreferenceFormat.java

public String format(Preference pref) {
    try {//ww  w . ja va 2 s . c om
        StringWriter sw = new StringWriter();
        XMLStreamWriter writer = createXmlWriter(sw);

        writer.writeStartElement("div");
        writer.writeAttribute("class", "preference");

        writer.writeCharacters("Preference: ");

        if (pref.getKey() != null) {
            writer.writeStartElement("span");
            writer.writeAttribute("class", "key");
            writer.writeCharacters(pref.getKey());
            writer.writeEndElement();
        }

        writer.writeCharacters(" = ");

        if (pref.getValue() != null) {
            writer.writeStartElement("span");
            writer.writeAttribute("class", "value");
            writer.writeCharacters(pref.getValue());
            writer.writeEndElement();
        }

        writer.writeEndElement();
        writer.close();

        return sw.toString();
    } catch (XMLStreamException e) {
        throw new RuntimeException("Error formatting XML", e);
    }
}

From source file:org.osaf.cosmo.model.text.XhtmlSubscriptionFormat.java

private String format(CollectionSubscription sub, boolean isCollectionProvided, CollectionItem collection,
        boolean isTicketProvided, Ticket ticket) {
    try {/*w  w  w.  jav a2s . c  o m*/
        StringWriter sw = new StringWriter();
        XMLStreamWriter writer = createXmlWriter(sw);

        writer.writeStartElement("div");
        writer.writeAttribute("class", "local-subscription");

        if (sub.getDisplayName() != null) {
            writer.writeCharacters("Subscription: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "name");
            writer.writeCharacters(sub.getDisplayName());
            writer.writeEndElement();
        }

        if (sub.getCollectionUid() != null) {
            writer.writeStartElement("div");
            writer.writeAttribute("class", "collection");
            writer.writeCharacters("Collection: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "uuid");
            writer.writeCharacters(sub.getCollectionUid());
            writer.writeEndElement();
            if (isCollectionProvided) {
                writer.writeCharacters(" Exists? ");
                writer.writeStartElement("span");
                writer.writeAttribute("class", "exists");
                writer.writeCharacters(Boolean.valueOf(collection != null).toString());
                writer.writeEndElement();
            }
            writer.writeEndElement();
        }

        if (sub.getTicketKey() != null) {
            writer.writeStartElement("div");
            writer.writeAttribute("class", "ticket");
            writer.writeCharacters("Ticket: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "key");
            writer.writeCharacters(sub.getTicketKey());
            writer.writeEndElement();
            if (isTicketProvided) {
                writer.writeCharacters(" Exists? ");
                writer.writeStartElement("span");
                writer.writeAttribute("class", "exists");
                writer.writeCharacters(Boolean.valueOf(ticket != null).toString());
                writer.writeEndElement();
            }
            writer.writeEndElement();
        }

        writer.writeEndElement();
        writer.close();

        return sw.toString();
    } catch (XMLStreamException e) {
        throw new RuntimeException("Error formatting XML", e);
    }
}