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.activiti.designer.export.bpmn20.export.FormPropertiesExport.java

public static void createFormPropertiesXML(List<FormProperty> propertyList, XMLStreamWriter xtw)
        throws Exception {

    for (FormProperty formProperty : propertyList) {
        xtw.writeStartElement(ACTIVITI_EXTENSIONS_PREFIX, "formProperty", ACTIVITI_EXTENSIONS_NAMESPACE);
        xtw.writeAttribute("id", formProperty.getId());
        if (StringUtils.isNotEmpty(formProperty.getName())) {
            xtw.writeAttribute("name", formProperty.getName());
        }// w  ww  . j a  v a 2  s  .  co  m
        if (StringUtils.isNotEmpty(formProperty.getType())) {
            xtw.writeAttribute("type", formProperty.getType());
        }
        if (StringUtils.isNotEmpty(formProperty.getValue())) {
            xtw.writeAttribute("value", formProperty.getValue());
        }
        if (StringUtils.isNotEmpty(formProperty.getExpression())) {
            xtw.writeAttribute("expression", formProperty.getExpression());
        }
        if (StringUtils.isNotEmpty(formProperty.getVariable())) {
            xtw.writeAttribute("variable", formProperty.getVariable());
        }
        if (StringUtils.isNotEmpty(formProperty.getDefaultExpression())) {
            xtw.writeAttribute("default", formProperty.getDefaultExpression());
        }
        if (StringUtils.isNotEmpty(formProperty.getDatePattern())) {
            xtw.writeAttribute("datePattern", formProperty.getDatePattern());
        }
        if (formProperty.getRequired() != null) {
            xtw.writeAttribute("required", formProperty.getRequired().toString().toLowerCase());
        }
        if (formProperty.getReadable() != null) {
            xtw.writeAttribute("readable", formProperty.getReadable().toString().toLowerCase());
        }
        if (formProperty.getWriteable() != null) {
            xtw.writeAttribute("writable", formProperty.getWriteable().toString().toLowerCase());
        }

        if (formProperty.getFormValues().size() > 0) {
            for (FormValue formValue : formProperty.getFormValues()) {
                xtw.writeStartElement(ACTIVITI_EXTENSIONS_PREFIX, "value", ACTIVITI_EXTENSIONS_NAMESPACE);
                xtw.writeAttribute("id", formValue.getId());
                xtw.writeAttribute("name", formValue.getName());
                xtw.writeEndElement();
            }
        }

        xtw.writeEndElement();
    }
}

From source file:org.activiti.designer.export.bpmn20.export.MailTaskExport.java

private static void writeField(String name, String expression, XMLStreamWriter xtw) throws Exception {
    xtw.writeStartElement(ACTIVITI_EXTENSIONS_PREFIX, "field", ACTIVITI_EXTENSIONS_NAMESPACE);
    xtw.writeAttribute("name", name);
    xtw.writeAttribute("expression", expression);
    xtw.writeEndElement();//from  w  w  w. j a  v  a 2  s  . c  om
}

From source file:org.activiti.designer.export.bpmn20.export.MailTaskExport.java

private static void writeCDataField(String name, String text, XMLStreamWriter xtw) throws Exception {
    xtw.writeStartElement(ACTIVITI_EXTENSIONS_PREFIX, "field", ACTIVITI_EXTENSIONS_NAMESPACE);
    xtw.writeAttribute("name", name);
    xtw.writeStartElement(ACTIVITI_EXTENSIONS_PREFIX, "expression", ACTIVITI_EXTENSIONS_NAMESPACE);
    xtw.writeCData(text);//from w  w  w.  ja  v  a 2 s .  com
    xtw.writeEndElement();
    xtw.writeEndElement();
}

From source file:org.activiti.dmn.converter.util.DmnXMLUtil.java

protected static void writeExtensionElement(DmnExtensionElement extensionElement,
        Map<String, String> namespaceMap, XMLStreamWriter xtw) throws Exception {
    if (StringUtils.isNotEmpty(extensionElement.getName())) {
        Map<String, String> localNamespaceMap = new HashMap<String, String>();
        if (StringUtils.isNotEmpty(extensionElement.getNamespace())) {
            if (StringUtils.isNotEmpty(extensionElement.getNamespacePrefix())) {
                xtw.writeStartElement(extensionElement.getNamespacePrefix(), extensionElement.getName(),
                        extensionElement.getNamespace());

                if (namespaceMap.containsKey(extensionElement.getNamespacePrefix()) == false
                        || namespaceMap.get(extensionElement.getNamespacePrefix())
                                .equals(extensionElement.getNamespace()) == false) {

                    xtw.writeNamespace(extensionElement.getNamespacePrefix(), extensionElement.getNamespace());
                    namespaceMap.put(extensionElement.getNamespacePrefix(), extensionElement.getNamespace());
                    localNamespaceMap.put(extensionElement.getNamespacePrefix(),
                            extensionElement.getNamespace());
                }/*from w  w  w .  j  a va  2 s . c o m*/
            } else {
                xtw.writeStartElement(extensionElement.getNamespace(), extensionElement.getName());
            }
        } else {
            xtw.writeStartElement(extensionElement.getName());
        }

        for (List<DmnExtensionAttribute> attributes : extensionElement.getAttributes().values()) {
            for (DmnExtensionAttribute attribute : attributes) {
                if (StringUtils.isNotEmpty(attribute.getName()) && attribute.getValue() != null) {
                    if (StringUtils.isNotEmpty(attribute.getNamespace())) {
                        if (StringUtils.isNotEmpty(attribute.getNamespacePrefix())) {

                            if (namespaceMap.containsKey(attribute.getNamespacePrefix()) == false
                                    || namespaceMap.get(attribute.getNamespacePrefix())
                                            .equals(attribute.getNamespace()) == false) {

                                xtw.writeNamespace(attribute.getNamespacePrefix(), attribute.getNamespace());
                                namespaceMap.put(attribute.getNamespacePrefix(), attribute.getNamespace());
                            }

                            xtw.writeAttribute(attribute.getNamespacePrefix(), attribute.getNamespace(),
                                    attribute.getName(), attribute.getValue());
                        } else {
                            xtw.writeAttribute(attribute.getNamespace(), attribute.getName(),
                                    attribute.getValue());
                        }
                    } else {
                        xtw.writeAttribute(attribute.getName(), attribute.getValue());
                    }
                }
            }
        }

        if (extensionElement.getElementText() != null) {
            xtw.writeCharacters(extensionElement.getElementText());
        } else {
            for (List<DmnExtensionElement> childElements : extensionElement.getChildElements().values()) {
                for (DmnExtensionElement childElement : childElements) {
                    writeExtensionElement(childElement, namespaceMap, xtw);
                }
            }
        }

        for (String prefix : localNamespaceMap.keySet()) {
            namespaceMap.remove(prefix);
        }

        xtw.writeEndElement();
    }
}

From source file:org.apache.axiom.om.impl.serialize.StreamingOMSerializer.java

/**
 * @param reader/*w ww .j  a v a  2 s  . c o  m*/
 * @param writer
 * @throws XMLStreamException
 */
protected void serializeElement(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException {

    // Note: To serialize the start tag, we must follow the order dictated by the JSR-173 (StAX) specification.
    // Please keep this code in sync with the code in OMSerializerUtil.serializeStartpart

    // The algorithm is:
    // ... generate writeStartElement
    //
    // ... generate setPrefix/setDefaultNamespace for each namespace declaration if the prefix is unassociated.
    // ... generate setPrefix/setDefaultNamespace if the prefix of the element is unassociated
    // ... generate setPrefix/setDefaultNamespace for each unassociated prefix of the attributes.
    //
    // ... generate writeNamespace/writerDefaultNamespace for the new namespace declarations determine during the "set" processing
    // ... generate writeAttribute for each attribute

    ArrayList writePrefixList = null;
    ArrayList writeNSList = null;

    // Get the prefix and namespace of the element.  "" and null are identical.
    String ePrefix = reader.getPrefix();
    ePrefix = (ePrefix != null && ePrefix.length() == 0) ? null : ePrefix;
    String eNamespace = reader.getNamespaceURI();
    eNamespace = (eNamespace != null && eNamespace.length() == 0) ? null : eNamespace;

    // Write the startElement if required
    if (eNamespace != null) {
        if (ePrefix == null) {
            if (!OMSerializerUtil.isAssociated("", eNamespace, writer)) {

                if (writePrefixList == null) {
                    writePrefixList = new ArrayList();
                    writeNSList = new ArrayList();
                }
                writePrefixList.add("");
                writeNSList.add(eNamespace);
            }

            writer.writeStartElement("", reader.getLocalName(), eNamespace);
        } else {

            if (!OMSerializerUtil.isAssociated(ePrefix, eNamespace, writer)) {
                if (writePrefixList == null) {
                    writePrefixList = new ArrayList();
                    writeNSList = new ArrayList();
                }
                writePrefixList.add(ePrefix);
                writeNSList.add(eNamespace);
            }

            writer.writeStartElement(ePrefix, reader.getLocalName(), eNamespace);
        }
    } else {
        writer.writeStartElement(reader.getLocalName());
    }

    // Generate setPrefix for the namespace declarations
    int count = reader.getNamespaceCount();
    for (int i = 0; i < count; i++) {
        String prefix = reader.getNamespacePrefix(i);
        prefix = (prefix != null && prefix.length() == 0) ? null : prefix;
        String namespace = reader.getNamespaceURI(i);
        namespace = (namespace != null && namespace.length() == 0) ? null : namespace;

        String newPrefix = OMSerializerUtil.generateSetPrefix(prefix, namespace, writer, false);
        // If this is a new association, remember it so that it can written out later
        if (newPrefix != null) {
            if (writePrefixList == null) {
                writePrefixList = new ArrayList();
                writeNSList = new ArrayList();
            }
            if (!writePrefixList.contains(newPrefix)) {
                writePrefixList.add(newPrefix);
                writeNSList.add(namespace);
            }
        }
    }

    // Generate setPrefix for the element
    // If the prefix is not associated with a namespace yet, remember it so that we can
    // write out a namespace declaration
    String newPrefix = OMSerializerUtil.generateSetPrefix(ePrefix, eNamespace, writer, false);
    // If this is a new association, remember it so that it can written out later
    if (newPrefix != null) {
        if (writePrefixList == null) {
            writePrefixList = new ArrayList();
            writeNSList = new ArrayList();
        }
        if (!writePrefixList.contains(newPrefix)) {
            writePrefixList.add(newPrefix);
            writeNSList.add(eNamespace);
        }
    }

    // Now Generate setPrefix for each attribute
    count = reader.getAttributeCount();
    for (int i = 0; i < count; i++) {
        String prefix = reader.getAttributePrefix(i);
        prefix = (prefix != null && prefix.length() == 0) ? null : prefix;
        String namespace = reader.getAttributeNamespace(i);
        namespace = (namespace != null && namespace.length() == 0) ? null : namespace;

        // Default prefix referencing is not allowed on an attribute
        if (prefix == null && namespace != null) {
            String writerPrefix = writer.getPrefix(namespace);
            writerPrefix = (writerPrefix != null && writerPrefix.length() == 0) ? null : writerPrefix;
            prefix = (writerPrefix != null) ? writerPrefix : generateUniquePrefix(writer.getNamespaceContext());
        }
        newPrefix = OMSerializerUtil.generateSetPrefix(prefix, namespace, writer, true);
        // If the prefix is not associated with a namespace yet, remember it so that we can
        // write out a namespace declaration
        if (newPrefix != null) {
            if (writePrefixList == null) {
                writePrefixList = new ArrayList();
                writeNSList = new ArrayList();
            }
            if (!writePrefixList.contains(newPrefix)) {
                writePrefixList.add(newPrefix);
                writeNSList.add(namespace);
            }
        }
    }

    // Now Generate setPrefix for each prefix referenced in an xsi:type
    // For example xsi:type="p:dataType"
    // The following code will make sure that setPrefix is called for "p".
    count = reader.getAttributeCount();
    for (int i = 0; i < count; i++) {
        String prefix = reader.getAttributePrefix(i);
        prefix = (prefix != null && prefix.length() == 0) ? null : prefix;
        String namespace = reader.getAttributeNamespace(i);
        namespace = (namespace != null && namespace.length() == 0) ? null : namespace;
        String localName = reader.getAttributeLocalName(i);

        if (XSI_URI.equals(namespace) && XSI_LOCAL_NAME.equals(localName)) {
            String value = reader.getAttributeValue(i);
            if (DEBUG_ENABLED) {
                log.debug("The value of xsi:type is " + value);
            }
            if (value != null) {
                value = value.trim();
                if (value.indexOf(":") > 0) {
                    String refPrefix = value.substring(0, value.indexOf(":"));
                    String refNamespace = reader.getNamespaceURI(refPrefix);
                    if (refNamespace != null && refNamespace.length() > 0) {

                        newPrefix = OMSerializerUtil.generateSetPrefix(refPrefix, refNamespace, writer, true);
                        // If the prefix is not associated with a namespace yet, remember it so that we can
                        // write out a namespace declaration
                        if (newPrefix != null) {
                            if (DEBUG_ENABLED) {
                                log.debug(
                                        "An xmlns:" + newPrefix + "=\"" + refNamespace + "\" will be written");
                            }
                            if (writePrefixList == null) {
                                writePrefixList = new ArrayList();
                                writeNSList = new ArrayList();
                            }
                            if (!writePrefixList.contains(newPrefix)) {
                                writePrefixList.add(newPrefix);
                                writeNSList.add(refNamespace);
                            }
                        }
                    }

                }
            }
        }
    }

    // Now write out the list of namespace declarations in this list that we constructed
    // while doing the "set" processing.
    if (writePrefixList != null) {
        for (int i = 0; i < writePrefixList.size(); i++) {
            String prefix = (String) writePrefixList.get(i);
            String namespace = (String) writeNSList.get(i);
            if (prefix != null) {
                if (namespace == null) {
                    writer.writeNamespace(prefix, "");
                } else {
                    writer.writeNamespace(prefix, namespace);
                }
            } else {
                writer.writeDefaultNamespace(namespace);
            }
        }
    }

    // Now write the attributes
    count = reader.getAttributeCount();
    for (int i = 0; i < count; i++) {
        String prefix = reader.getAttributePrefix(i);
        prefix = (prefix != null && prefix.length() == 0) ? null : prefix;
        String namespace = reader.getAttributeNamespace(i);
        namespace = (namespace != null && namespace.length() == 0) ? null : namespace;

        if (prefix == null && namespace != null) {
            // Default namespaces are not allowed on an attribute reference.
            // Earlier in this code, a unique prefix was added for this case...now obtain and use it
            prefix = writer.getPrefix(namespace);
            //XMLStreamWriter doesn't allow for getPrefix to know whether you're asking for the prefix
            //for an attribute or an element. So if the namespace matches the default namespace getPrefix will return
            //the empty string, as if it were an element, in all cases (even for attributes, and even if 
            //there was a prefix specifically set up for this), which is not the desired behavior.
            //Since the interface is base java, we can't fix it where we need to (by adding an attr boolean to 
            //XMLStreamWriter.getPrefix), so we hack it in here...
            if (prefix == null || "".equals(prefix)) {
                for (int j = 0; j < writePrefixList.size(); j++) {
                    if (namespace.equals((String) writeNSList.get(j))) {
                        prefix = (String) writePrefixList.get(j);
                    }
                }
            }
        } else if (namespace != null && !prefix.equals("xml")) {
            // Use the writer's prefix if it is different, but if the writers 
            // prefix is empty then do not replace because attributes do not
            // default to the default namespace like elements do.
            String writerPrefix = writer.getPrefix(namespace);
            if (!prefix.equals(writerPrefix) && !"".equals(writerPrefix)) {
                prefix = writerPrefix;
            }
        }
        if (namespace != null) {
            // Qualified attribute
            writer.writeAttribute(prefix, namespace, reader.getAttributeLocalName(i),
                    reader.getAttributeValue(i));
        } else {
            // Unqualified attribute
            writer.writeAttribute(reader.getAttributeLocalName(i), reader.getAttributeValue(i));
        }
    }
}

From source file:org.apache.axiom.om.impl.util.OMSerializerUtil.java

/**
 * Method serializeStartpart. Serialize the start tag of an element.
 *
 * @param element/* ww w. j  a v a2 s  .c  om*/
 * @param localName (in some cases, the caller wants to force a different localName)
 * @param writer
 * @throws XMLStreamException
 */
public static void serializeStartpart(OMElement element, String localName, XMLStreamWriter writer)
        throws XMLStreamException {

    // Note: To serialize the start tag, we must follow the order dictated by the JSR-173 (StAX) specification.
    // Please keep this code in sync with the code in StreamingOMSerializer.serializeElement

    // The algorithm is:
    // ... generate writeStartElement
    //
    // ... generate setPrefix/setDefaultNamespace for each namespace declaration if the prefix is unassociated.
    // ... generate setPrefix/setDefaultNamespace if the prefix of the element is unassociated
    // ... generate setPrefix/setDefaultNamespace for each unassociated prefix of the attributes.
    //
    // ... generate writeNamespace/writerDefaultNamespace for the new namespace declarations determine during the "set" processing
    // ... generate writeAttribute for each attribute

    ArrayList writePrefixList = null;
    ArrayList writeNSList = null;

    // Get the namespace and prefix of the element
    OMNamespace eOMNamespace = element.getNamespace();
    String ePrefix = null;
    String eNamespace = null;
    if (eOMNamespace != null) {
        ePrefix = eOMNamespace.getPrefix();
        eNamespace = eOMNamespace.getNamespaceURI();
    }
    ePrefix = (ePrefix != null && ePrefix.length() == 0) ? null : ePrefix;
    eNamespace = (eNamespace != null && eNamespace.length() == 0) ? null : eNamespace;

    if (eNamespace != null) {
        if (ePrefix == null) {
            if (!isAssociated("", eNamespace, writer)) {
                if (writePrefixList == null) {
                    writePrefixList = new ArrayList();
                    writeNSList = new ArrayList();
                }
                if (!writePrefixList.contains("")) {
                    writePrefixList.add("");
                    writeNSList.add(eNamespace);
                }
            }
            writer.writeStartElement("", localName, eNamespace);
        } else {
            /*
             * If XMLStreamWriter.writeStartElement(prefix,localName,namespaceURI) associates
             * the prefix with the namespace .. 
             */
            if (!isAssociated(ePrefix, eNamespace, writer)) {
                if (writePrefixList == null) {
                    writePrefixList = new ArrayList();
                    writeNSList = new ArrayList();
                }
                if (!writePrefixList.contains(ePrefix)) {
                    writePrefixList.add(ePrefix);
                    writeNSList.add(eNamespace);
                }
            }

            writer.writeStartElement(ePrefix, localName, eNamespace);
        }
    } else {
        writer.writeStartElement(localName);
    }

    // Generate setPrefix for the namespace declarations
    Iterator it = element.getAllDeclaredNamespaces();
    while (it != null && it.hasNext()) {
        OMNamespace omNamespace = (OMNamespace) it.next();
        String prefix = null;
        String namespace = null;
        if (omNamespace != null) {
            prefix = omNamespace.getPrefix();
            namespace = omNamespace.getNamespaceURI();
        }
        prefix = (prefix != null && prefix.length() == 0) ? null : prefix;
        namespace = (namespace != null && namespace.length() == 0) ? null : namespace;

        String newPrefix = generateSetPrefix(prefix, namespace, writer, false);
        // If this is a new association, remember it so that it can written out later
        if (newPrefix != null) {
            if (writePrefixList == null) {
                writePrefixList = new ArrayList();
                writeNSList = new ArrayList();
            }
            if (!writePrefixList.contains(newPrefix)) {
                writePrefixList.add(newPrefix);
                writeNSList.add(namespace);
            }
        }
    }

    // Generate setPrefix for the element
    // Get the prefix and namespace of the element.  "" and null are identical.
    String newPrefix = generateSetPrefix(ePrefix, eNamespace, writer, false);
    // If this is a new association, remember it so that it can written out later
    if (newPrefix != null) {
        if (writePrefixList == null) {
            writePrefixList = new ArrayList();
            writeNSList = new ArrayList();
        }
        if (!writePrefixList.contains(newPrefix)) {
            writePrefixList.add(newPrefix);
            writeNSList.add(eNamespace);
        }
    }

    // Now Generate setPrefix for each attribute
    Iterator attrs = element.getAllAttributes();
    while (attrs != null && attrs.hasNext()) {
        OMAttribute attr = (OMAttribute) attrs.next();
        OMNamespace omNamespace = attr.getNamespace();
        String prefix = null;
        String namespace = null;
        if (omNamespace != null) {
            prefix = omNamespace.getPrefix();
            namespace = omNamespace.getNamespaceURI();
        }
        prefix = (prefix != null && prefix.length() == 0) ? null : prefix;
        namespace = (namespace != null && namespace.length() == 0) ? null : namespace;

        // Default prefix referencing is not allowed on an attribute
        if (prefix == null && namespace != null) {
            String writerPrefix = writer.getPrefix(namespace);
            writerPrefix = (writerPrefix != null && writerPrefix.length() == 0) ? null : writerPrefix;
            prefix = (writerPrefix != null) ? writerPrefix : getNextNSPrefix();
        }
        newPrefix = generateSetPrefix(prefix, namespace, writer, true);
        // If the prefix is not associated with a namespace yet, remember it so that we can
        // write out a namespace declaration
        if (newPrefix != null) {
            if (writePrefixList == null) {
                writePrefixList = new ArrayList();
                writeNSList = new ArrayList();
            }
            if (!writePrefixList.contains(newPrefix)) {
                writePrefixList.add(newPrefix);
                writeNSList.add(namespace);
            }
        }
    }

    // Now Generate setPrefix for each prefix referenced in an xsi:type
    // For example xsi:type="p:dataType"
    // The following code will make sure that setPrefix is called for "p".
    attrs = element.getAllAttributes();
    while (attrs != null && attrs.hasNext()) {
        OMAttribute attr = (OMAttribute) attrs.next();
        OMNamespace omNamespace = attr.getNamespace();
        String prefix = null;
        String namespace = null;
        if (omNamespace != null) {
            prefix = omNamespace.getPrefix();
            namespace = omNamespace.getNamespaceURI();
        }
        prefix = (prefix != null && prefix.length() == 0) ? null : prefix;
        namespace = (namespace != null && namespace.length() == 0) ? null : namespace;
        String local = attr.getLocalName();

        if (XSI_URI.equals(namespace) && XSI_LOCAL_NAME.equals(local)) {
            String value = attr.getAttributeValue();
            if (DEBUG_ENABLED) {
                log.debug("The value of xsi:type is " + value);
            }
            if (value != null) {
                value = value.trim();
                if (value.indexOf(":") > 0) {
                    String refPrefix = value.substring(0, value.indexOf(":"));
                    OMNamespace omNS = element.findNamespaceURI(refPrefix);
                    String refNamespace = (omNS == null) ? null : omNS.getNamespaceURI();
                    if (refNamespace != null && refNamespace.length() > 0) {

                        newPrefix = generateSetPrefix(refPrefix, refNamespace, writer, true);
                        // If the prefix is not associated with a namespace yet, remember it so that we can
                        // write out a namespace declaration
                        if (newPrefix != null) {
                            if (DEBUG_ENABLED) {
                                log.debug(
                                        "An xmlns:" + newPrefix + "=\"" + refNamespace + "\" will be written");
                            }
                            if (writePrefixList == null) {
                                writePrefixList = new ArrayList();
                                writeNSList = new ArrayList();
                            }
                            if (!writePrefixList.contains(newPrefix)) {
                                writePrefixList.add(newPrefix);
                                writeNSList.add(refNamespace);
                            }
                        }
                    }
                }
            }
        }
    }

    // Now write out the list of namespace declarations in this list that we constructed
    // while doing the "set" processing.
    if (writePrefixList != null) {
        for (int i = 0; i < writePrefixList.size(); i++) {
            String prefix = (String) writePrefixList.get(i);
            String namespace = (String) writeNSList.get(i);
            if (prefix != null) {
                if (namespace == null) {
                    writer.writeNamespace(prefix, "");
                } else {
                    writer.writeNamespace(prefix, namespace);
                }
            } else {
                writer.writeDefaultNamespace(namespace);
            }
        }
    }

    // Now write the attributes
    attrs = element.getAllAttributes();
    while (attrs != null && attrs.hasNext()) {
        OMAttribute attr = (OMAttribute) attrs.next();
        OMNamespace omNamespace = attr.getNamespace();
        String prefix = null;
        String namespace = null;
        if (omNamespace != null) {
            prefix = omNamespace.getPrefix();
            namespace = omNamespace.getNamespaceURI();
        }
        prefix = (prefix != null && prefix.length() == 0) ? null : prefix;
        namespace = (namespace != null && namespace.length() == 0) ? null : namespace;

        if (prefix == null && namespace != null) {
            // Default namespaces are not allowed on an attribute reference.
            // Earlier in this code, a unique prefix was added for this case...now obtain and use it
            prefix = writer.getPrefix(namespace);
            //XMLStreamWriter doesn't allow for getPrefix to know whether you're asking for the prefix
            //for an attribute or an element. So if the namespace matches the default namespace getPrefix will return
            //the empty string, as if it were an element, in all cases (even for attributes, and even if
            //there was a prefix specifically set up for this), which is not the desired behavior.
            //Since the interface is base java, we can't fix it where we need to (by adding an attr boolean to
            //XMLStreamWriter.getPrefix), so we hack it in here...
            if (prefix == null || "".equals(prefix)) {
                for (int i = 0; i < writePrefixList.size(); i++) {
                    if (namespace.equals((String) writeNSList.get(i))) {
                        prefix = (String) writePrefixList.get(i);
                    }
                }
            }
        } else if (namespace != null) {
            // Use the writer's prefix if it is different, but if the writers
            // prefix is empty then do not replace because attributes do not
            // default to the default namespace like elements do.
            String writerPrefix = writer.getPrefix(namespace);
            if (!prefix.equals(writerPrefix) && writerPrefix != null && !"".equals(writerPrefix)) {
                prefix = writerPrefix;
            }
        }
        if (namespace != null) {
            if (prefix == null && OMConstants.XMLNS_URI.equals(namespace)) {
                prefix = OMConstants.XMLNS_PREFIX;
            }
            // Qualified attribute
            writer.writeAttribute(prefix, namespace, attr.getLocalName(), attr.getAttributeValue());
        } else {
            // Unqualified attribute
            writer.writeAttribute(attr.getLocalName(), attr.getAttributeValue());
        }
    }
}

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

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

    if (prefix == null) {
        prefix = PREFIX;/*from   w w  w  .ja  v a 2s  .c  om*/
        writer.setPrefix(PREFIX, NS);
    }

    writer.writeStartElement(PREFIX, MTOM_SERIALIZATION_CONFIG_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.axis2.policy.model.MTOM11Assertion.java

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

    if (prefix == null) {
        prefix = PREFIX;//w ww  .  j a v  a2  s.  com
        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

/**
 * @return Metadata XML string./* w  w  w  . ja v a2s  .  com*/
 */
private final String generateMetadata() {
    final XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
    assert xmlOutputFactory != null : "Expect XMLOutputFactory implementation.";
    final StringWriter stringWriter = new StringWriter();
    XMLStreamWriter xmlWriter = null;

    try {
        xmlWriter = new XMLFormatter(xmlOutputFactory.createXMLStreamWriter(stringWriter));
        xmlWriter.writeStartDocument();

        xmlWriter.writeStartElement("rdf", "RDF", RDF_URI);
        xmlWriter.setPrefix("rdf", RDF_URI);
        xmlWriter.writeNamespace("rdf", RDF_URI);

        // write rdf:Description
        xmlWriter.writeStartElement(RDF_URI, "Description");
        xmlWriter.setPrefix("dc", DC_URI);
        xmlWriter.setPrefix(VersionInfo.COMPILER_NAMESPACE_PREFIX, VersionInfo.COMPILER_NAMESPACE_URI);
        xmlWriter.writeNamespace("dc", DC_URI);
        xmlWriter.writeNamespace(VersionInfo.COMPILER_NAMESPACE_PREFIX, VersionInfo.COMPILER_NAMESPACE_URI);

        // write dc:format
        xmlWriter.writeStartElement(DC_URI, "format");
        xmlWriter.writeCharacters("application/x-shockwave-flash");
        xmlWriter.writeEndElement();

        if (isFlex()) {
            // write localizedTitles
            writeMap(xmlWriter, DC_URI, "description", localizedDescriptions);

            // write localizedDescription
            writeMap(xmlWriter, DC_URI, "title", localizedTitles);

            // write publisher
            writeCollection(xmlWriter, DC_URI, "publisher", publishers);

            // write creators
            writeCollection(xmlWriter, DC_URI, "creator", creators);

            // write contributor
            writeCollection(xmlWriter, DC_URI, "contributor", contributors);

            // write language
            writeCollection(xmlWriter, DC_URI, "language", langs);

            // write date
            writeDate(xmlWriter);
        }

        // write compiledBy
        writeCompiledBy(xmlWriter);

        // write
        xmlWriter.writeEndElement(); // Description
        xmlWriter.writeEndDocument();
    } catch (XMLStreamException e) {
        return "";
    }

    return stringWriter.toString();
}

From source file:org.apache.olingo.client.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) {
        writer.writeStartElement(Constants.PREFIX_METADATA, Constants.ELEM_ELEMENT, namespaceMetadata);
        value(writer, valueType, kind, item);
        writer.writeEndElement();//from w ww .ja v  a 2s.  co  m
    }
}