Example usage for javax.xml.stream XMLStreamWriter getPrefix

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

Introduction

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

Prototype

public String getPrefix(String uri) throws XMLStreamException;

Source Link

Document

Gets the prefix the uri is bound to

Usage

From source file:Main.java

public static void writeNamespaceIfNotBound(XMLStreamWriter xmlStream, String prefix, String nsUri)
        throws XMLStreamException {
    if (!prefix.equals(xmlStream.getPrefix(nsUri))) {
        xmlStream.writeNamespace(prefix, nsUri);
    }/*from  www .  ja v  a2s .  com*/
}

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

/**
 * @param reader//w  w  w . j  ava  2 s .co 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.serialize.StreamingOMSerializer.java

/**
 * @param reader//ww w . j  av a2s  .c o  m
 * @param writer
 * @throws XMLStreamException
 */
protected void serializeAttributes(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException {
    int count = reader.getAttributeCount();
    String prefix = null;
    String namespaceName = null;
    String writerPrefix = null;
    for (int i = 0; i < count; i++) {
        prefix = reader.getAttributePrefix(i);
        namespaceName = reader.getAttributeNamespace(i);
        /*
           Some parser implementations return null for the unqualified namespace.
           But getPrefix(null) will throw an exception (according to the XMLStreamWriter
           javadoc. We guard against this by using "" for the unqualified namespace. 
        */
        namespaceName = (namespaceName == null) ? "" : namespaceName;

        // Using getNamespaceContext should be avoided when not necessary.
        // Some parser implementations construct a new NamespaceContext each time it is invoked.
        // writerPrefix = writer.getNamespaceContext().getPrefix(namespaceName);
        writerPrefix = writer.getPrefix(namespaceName);

        if (!"".equals(namespaceName)) {
            //prefix has already being declared but this particular attrib has a
            //no prefix attached. So use the prefix provided by the writer
            if (writerPrefix != null && (prefix == null || prefix.equals(""))) {
                writer.writeAttribute(writerPrefix, namespaceName, reader.getAttributeLocalName(i),
                        reader.getAttributeValue(i));

                //writer prefix is available but different from the current
                //prefix of the attrib. We should be decalring the new prefix
                //as a namespace declaration
            } else if (prefix != null && !"".equals(prefix) && !prefix.equals(writerPrefix)) {
                writer.writeNamespace(prefix, namespaceName);
                writer.writeAttribute(prefix, namespaceName, reader.getAttributeLocalName(i),
                        reader.getAttributeValue(i));

                //prefix is null (or empty), but the namespace name is valid! it has not
                //being written previously also. So we need to generate a prefix
                //here
            } else {
                prefix = generateUniquePrefix(writer.getNamespaceContext());
                writer.writeNamespace(prefix, namespaceName);
                writer.writeAttribute(prefix, namespaceName, reader.getAttributeLocalName(i),
                        reader.getAttributeValue(i));
            }
        } else {
            //empty namespace is equal to no namespace!
            writer.writeAttribute(reader.getAttributeLocalName(i), reader.getAttributeValue(i));
        }

    }
}

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

/**
 * Method serializeNamespace./*  w ww.j  a  v a2 s  .c  o  m*/
 *
 * @param prefix
 * @param URI
 * @param writer
 * @throws XMLStreamException
 */
private void serializeNamespace(String prefix, String URI, XMLStreamWriter writer) throws XMLStreamException {
    String prefix1 = writer.getPrefix(URI);
    if (prefix1 == null) {
        writer.writeNamespace(prefix, URI);
        writer.setPrefix(prefix, URI);
    }
}

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

/**
 * Method serializeNamespace./*from  w  w w . j a  va  2 s  .  c o m*/
 *
 * @param namespace
 * @param writer
 * @throws XMLStreamException
 * @deprecated Use serializeStartpart instead
 */
public static void serializeNamespace(OMNamespace namespace, XMLStreamWriter writer) throws XMLStreamException {
    if (namespace == null) {
        return;
    }
    String uri = namespace.getNamespaceURI();
    String prefix = namespace.getPrefix();

    if (uri != null && !"".equals(uri)) {
        String prefixFromWriter = writer.getPrefix(uri);

        // Handling Default Namespaces First
        // Case 1 :
        //        here we are trying define a default namespace. But has this been defined in the current context.
        //        yes, there can be a default namespace, but it may have a different URI. If its a different URI
        //        then explicitly define the default namespace here.
        // Case 2 :
        //        The passed in namespace is a default ns, but there is a non-default ns declared
        //        in the current scope.
        if (("".equals(prefix) && "".equals(prefixFromWriter)
                && !uri.equals(writer.getNamespaceContext().getNamespaceURI("")))
                || (prefix != null && "".equals(prefix)
                        && (prefixFromWriter == null || !prefix.equals(prefixFromWriter)))) {
            // this has not been declared earlier
            writer.writeDefaultNamespace(uri);
            writer.setDefaultNamespace(uri);
        } else {
            prefix = prefix == null ? getNextNSPrefix(writer) : prefix;
            if (prefix != null && !prefix.equals(prefixFromWriter)
                    && !checkForPrefixInTheCurrentContext(writer, uri, prefix)) {
                writer.writeNamespace(prefix, uri);
                writer.setPrefix(prefix, uri);
            }
        }
    } else {
        // now the nsURI passed is "" or null. Meaning we gonna work with defaultNS.
        // check whether there is a defaultNS already declared. If yes, is it the same as this ?
        String currentDefaultNSURI = writer.getNamespaceContext().getNamespaceURI("");
        if ((currentDefaultNSURI != null && !currentDefaultNSURI.equals(uri))
                || uri != null && !uri.equals(currentDefaultNSURI)) {
            // this has not been declared earlier
            writer.writeDefaultNamespace(uri);
            writer.setDefaultNamespace(uri);
        }
    }
}

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

/**
 * Method serializeStartpart. Serialize the start tag of an element.
 *
 * @param element/*from   w ww  .j a  v a  2  s. co m*/
 * @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.axiom.om.impl.util.OMSerializerUtil.java

/**
 * @param prefix // w  w w .  ja  v a2 s  .  com
 * @param namespace
 * @param writer
 * @return true if the prefix is associated with the namespace in the current context
 */
public static boolean isAssociated(String prefix, String namespace, XMLStreamWriter writer)
        throws XMLStreamException {

    // The "xml" prefix is always (implicitly) associated. Returning true here makes sure that
    // we never write a declaration for the xml namespace. See WSCOMMONS-281 for a discussion
    // of this issue.
    if ("xml".equals(prefix)) {
        return true;
    }

    // NOTE: Calling getNamespaceContext() on many XMLStreamWriter implementations is expensive.
    // Please use other writer methods first.

    // For consistency, convert null arguments.
    // This helps get around the parser implementation differences.
    // In addition, the getPrefix/getNamespace methods cannot be called with null parameters.
    prefix = (prefix == null) ? "" : prefix;
    namespace = (namespace == null) ? "" : namespace;

    if (namespace.length() > 0) {
        // QUALIFIED NAMESPACE
        // Get the namespace associated with the prefix
        String writerPrefix = writer.getPrefix(namespace);
        if (prefix.equals(writerPrefix)) {
            return true;
        }

        // It is possible that the namespace is associated with multiple prefixes,
        // So try getting the namespace as a second step.
        if (writerPrefix != null) {
            NamespaceContext nsContext = writer.getNamespaceContext();
            if (nsContext != null) {
                String writerNS = nsContext.getNamespaceURI(prefix);
                return namespace.equals(writerNS);
            }
        }
        return false;
    } else {
        // UNQUALIFIED NAMESPACE

        // Cannot associate a prefix with an unqualifed name.
        // However sometimes axiom creates a fake prefix name if xmns="" is not in effect.
        // So return true
        if (prefix.length() > 0) {
            return true;
        }

        // Get the namespace associated with the prefix.
        // It is illegal to call getPrefix with null, but the specification is not
        // clear on what happens if called with "".  So the following code is 
        // protected
        try {
            String writerPrefix = writer.getPrefix("");
            if (writerPrefix != null && writerPrefix.length() == 0) {
                return true;
            }
        } catch (Throwable t) {
            if (DEBUG_ENABLED) {
                log.debug("Caught exception from getPrefix(\"\"). Processing continues: " + t);
            }
        }

        // Fallback to using the namespace context
        NamespaceContext nsContext = writer.getNamespaceContext();
        if (nsContext != null) {
            String writerNS = nsContext.getNamespaceURI("");
            if (writerNS != null && writerNS.length() > 0) {
                return false;
            }
        }
        return true;
    }
}

From source file:org.apache.axis2.databinding.utils.ConverterUtil.java

public static void serializeAnyType(Object value, XMLStreamWriter xmlStreamWriter) throws XMLStreamException {
    if (value instanceof String) {
        serializeAnyType("string", value.toString(), xmlStreamWriter);
    } else if (value instanceof Integer) {
        serializeAnyType("int", value.toString(), xmlStreamWriter);
    } else if (value instanceof Boolean) {
        serializeAnyType("boolean", value.toString(), xmlStreamWriter);
    } else if (value instanceof URI) {
        serializeAnyType("anyURI", value.toString(), xmlStreamWriter);
    } else if (value instanceof Byte) {
        serializeAnyType("byte", value.toString(), xmlStreamWriter);
    } else if (value instanceof Date) {
        serializeAnyType("date", convertToString((Date) value), xmlStreamWriter);
    } else if (value instanceof Calendar) {
        serializeAnyType("dateTime", convertToString((Calendar) value), xmlStreamWriter);
    } else if (value instanceof Time) {
        serializeAnyType("time", convertToString((Time) value), xmlStreamWriter);
    } else if (value instanceof Float) {
        serializeAnyType("float", value.toString(), xmlStreamWriter);
    } else if (value instanceof Long) {
        serializeAnyType("long", value.toString(), xmlStreamWriter);
    } else if (value instanceof Double) {
        serializeAnyType("double", value.toString(), xmlStreamWriter);
    } else if (value instanceof Short) {
        serializeAnyType("short", value.toString(), xmlStreamWriter);
    } else if (value instanceof BigDecimal) {
        serializeAnyType("decimal", ((BigDecimal) value).toPlainString(), xmlStreamWriter);
    } else if (value instanceof DataHandler) {
        addTypeAttribute(xmlStreamWriter, "base64Binary");
        try {//  www .  j a  v  a  2s. com
            XMLStreamWriterUtils.writeDataHandler(xmlStreamWriter, (DataHandler) value, null, true);
        } catch (IOException ex) {
            throw new XMLStreamException("Unable to read data handler", ex);
        }
    } else if (value instanceof QName) {
        QName qNameValue = (QName) value;
        String prefix = xmlStreamWriter.getPrefix(qNameValue.getNamespaceURI());
        if (prefix == null) {
            prefix = BeanUtil.getUniquePrefix();
            xmlStreamWriter.writeNamespace(prefix, qNameValue.getNamespaceURI());
            xmlStreamWriter.setPrefix(prefix, qNameValue.getNamespaceURI());
        }
        String attributeValue = qNameValue.getLocalPart();
        if (!prefix.equals("")) {
            attributeValue = prefix + ":" + attributeValue;
        }
        serializeAnyType("QName", attributeValue, xmlStreamWriter);
    } else if (value instanceof UnsignedByte) {
        serializeAnyType("unsignedByte", convertToString((UnsignedByte) value), xmlStreamWriter);
    } else if (value instanceof UnsignedLong) {
        serializeAnyType("unsignedLong", convertToString((UnsignedLong) value), xmlStreamWriter);
    } else if (value instanceof UnsignedShort) {
        serializeAnyType("unsignedShort", convertToString((UnsignedShort) value), xmlStreamWriter);
    } else if (value instanceof UnsignedInt) {
        serializeAnyType("unsignedInt", convertToString((UnsignedInt) value), xmlStreamWriter);
    } else if (value instanceof PositiveInteger) {
        serializeAnyType("positiveInteger", convertToString((PositiveInteger) value), xmlStreamWriter);
    } else if (value instanceof NegativeInteger) {
        serializeAnyType("negativeInteger", convertToString((NegativeInteger) value), xmlStreamWriter);
    } else if (value instanceof NonNegativeInteger) {
        serializeAnyType("nonNegativeInteger", convertToString((NonNegativeInteger) value), xmlStreamWriter);
    } else if (value instanceof NonPositiveInteger) {
        serializeAnyType("nonPositiveInteger", convertToString((NonPositiveInteger) value), xmlStreamWriter);
    } else {
        throw new XMLStreamException("Unknow type can not serialize");
    }
}

From source file:org.apache.axis2.databinding.utils.ConverterUtil.java

private static void addTypeAttribute(XMLStreamWriter xmlStreamWriter, String type) throws XMLStreamException {
    String prefix = xmlStreamWriter.getPrefix(Constants.XSI_NAMESPACE);
    if (prefix == null) {
        prefix = BeanUtil.getUniquePrefix();
        xmlStreamWriter.writeNamespace(prefix, Constants.XSI_NAMESPACE);
        xmlStreamWriter.setPrefix(prefix, Constants.XSI_NAMESPACE);
    }//from  ww  w .j ava 2  s . c o  m

    prefix = xmlStreamWriter.getPrefix(Constants.XSD_NAMESPACE);
    if (prefix == null) {
        prefix = BeanUtil.getUniquePrefix();
        xmlStreamWriter.writeNamespace(prefix, Constants.XSD_NAMESPACE);
        xmlStreamWriter.setPrefix(prefix, Constants.XSD_NAMESPACE);
    }

    String attributeValue = null;
    if (prefix.equals("")) {
        attributeValue = type;
    } else {
        attributeValue = prefix + ":" + type;
    }

    xmlStreamWriter.writeAttribute(Constants.XSI_NAMESPACE, "type", attributeValue);
}

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 . j av  a 2s .co m*/
        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();

}