Example usage for javax.xml.soap SOAPElement addNamespaceDeclaration

List of usage examples for javax.xml.soap SOAPElement addNamespaceDeclaration

Introduction

In this page you can find the example usage for javax.xml.soap SOAPElement addNamespaceDeclaration.

Prototype

public SOAPElement addNamespaceDeclaration(String prefix, String uri) throws SOAPException;

Source Link

Document

Adds a namespace declaration with the specified prefix and URI to this SOAPElement object.

Usage

From source file:org.apache.axis2.jaxws.message.util.impl.SAAJConverterImpl.java

/**
 * update the tag data of the SOAPElement
 *
 * @param NameCreator nc//  ww  w  .  j  a  v  a 2 s . c  o  m
 * @param element     SOAPElement
 * @param reader      XMLStreamReader whose cursor is at START_ELEMENT
 */
protected void updateTagData(NameCreator nc, SOAPElement element, XMLStreamReader reader, boolean newElement)
        throws SOAPException {
    String prefix = reader.getPrefix();
    prefix = (prefix == null) ? "" : prefix;

    // Make sure the prefix is correct
    if (prefix.length() > 0 && !element.getPrefix().equals(prefix)) {
        // Due to a bug in Axiom DOM or in the reader...not sure where yet,
        // there may be a non-null prefix and no namespace
        String ns = reader.getNamespaceURI();
        if (ns != null && ns.length() != 0) {
            element.setPrefix(prefix);
        }

    }

    if (!newElement) {
        // Add the namespace declarations from the reader for the missing namespaces
        int size = reader.getNamespaceCount();
        for (int i = 0; i < size; i++) {
            String pre = reader.getNamespacePrefix(i);
            String ns = reader.getNamespaceURI(i);
            if ((pre != null && pre.length() > 0) && (ns == null || ns.length() == 0)) {
                if (log.isDebugEnabled()) {
                    log.debug("The prefix is (" + pre + ") but there is no namespace.  "
                            + "This erroneous declaration is skipped.");
                }
            } else {
                String existingNS = element.getNamespaceURI(pre);
                if (!ns.equals(existingNS)) {
                    element.removeNamespaceDeclaration(pre); // Is it necessary to remove the existing prefix/ns
                    element.addNamespaceDeclaration(pre, ns);
                }
            }
        }
    } else {
        // Add the namespace declarations from the reader
        int size = reader.getNamespaceCount();
        for (int i = 0; i < size; i++) {
            String newPrefix = reader.getNamespacePrefix(i);
            String newNS = reader.getNamespaceURI(i);

            if ((newPrefix != null && newPrefix.length() > 0) && (newNS == null || newNS.length() == 0)) {
                // Due to a bug in Axiom DOM or the reader, I have
                // seen cases where the prefix is non-null but there is not
                // namespace.  Example: prefix is axis2ns3 and namespace is null.
                // This is an error..log, tolerate and continue
                if (log.isDebugEnabled()) {
                    log.debug("The prefix is (" + newPrefix + ") but there is no namespace.  "
                            + "This erroneous declaration is skipped.");
                }
            } else {
                element.addNamespaceDeclaration(newPrefix, newNS);
            }
        }
    }

    addAttributes(nc, element, reader);

    return;
}

From source file:org.jbpm.bpel.integration.soap.SoapUtil.java

private static String formatQName(QName value, SOAPElement elem) throws SOAPException {
    String namespaceURI = value.getNamespaceURI();
    String localName = value.getLocalPart();

    // easy way out: no namespace
    if (namespaceURI.length() == 0)
        return localName;

    String prefix = SoapUtil.getPrefix(namespaceURI, elem);
    if (prefix == null) {
        String givenPrefix = value.getPrefix();
        prefix = XmlUtil.generatePrefix(givenPrefix.length() > 0 ? givenPrefix : QUALIFIED_VALUE_PREFIX, elem);
        elem.addNamespaceDeclaration(prefix, namespaceURI);
    }//  w  w w . j  av a  2  s  .  c  o m
    return prefix + ':' + localName;
}

From source file:org.jbpm.bpel.integration.soap.SoapUtil.java

public static void ensureNamespaceDeclared(SOAPElement elem, String namespaceURI, String prefix)
        throws SOAPException {
    if (prefix == null || prefix.length() == 0) {
        if (namespaceURI == null || namespaceURI.length() == 0)
            return; // do not declare the empty namespace

        // verify the given URI is the default namespace
        if (!namespaceURI.equals(elem.getNamespaceURI(""))) {
            // the given URI is not the default namespace, declare locally
            elem.addNamespaceDeclaration("", namespaceURI);
        }//from w  w w.j  a  va  2  s.  co  m
    } else {
        if (namespaceURI == null || namespaceURI.length() == 0)
            throw new IllegalArgumentException("namespaceURI cannot be empty unless prefix is empty");

        // verify given prefix is associated to given URI
        if (!namespaceURI.equals(elem.getNamespaceURI(prefix))) {
            // prefix is associated with other/no URI, declare locally
            elem.addNamespaceDeclaration(prefix, namespaceURI);
        }
    }
}

From source file:org.jbpm.bpel.integration.soap.SoapUtil.java

public static void copyNamespaces(SOAPElement target, Element source) throws SOAPException {
    // easy way out: no attributes
    if (!source.hasAttributes())
        return;/* www  .j  a  v a 2 s.c o  m*/
    // traverse attributes to discover namespace declarations
    NamedNodeMap attributes = source.getAttributes();
    for (int i = 0, n = attributes.getLength(); i < n; i++) {
        Node attribute = attributes.item(i);
        // is attribute a namespace declaration?
        if (!BpelConstants.NS_XMLNS.equals(attribute.getNamespaceURI()))
            continue;
        // namespace declaration format xmlns:prefix="namespaceURI" |
        // xmlns="defaultNamespaceURI"
        String namespaceURI = attribute.getNodeValue();
        String prefix = attribute.getLocalName();
        // non-default namespace declaration?
        if (!"xmlns".equals(prefix)) {
            // BPEL-195: prevent addition matching visible declaration at target
            if (namespaceURI.equals(target.getNamespaceURI(prefix)))
                continue;
            target.addNamespaceDeclaration(prefix, namespaceURI);
            if (traceEnabled)
                log.trace("added namespace declaration: " + prefix + "->" + namespaceURI);
        }
        // non-empty default namespace declaration
        else if (namespaceURI.length() > 0) {
            prefix = XmlUtil.generatePrefix(DEFAULT_NAMESPACE_PREFIX, source);
            target.addNamespaceDeclaration(prefix, namespaceURI);
            if (traceEnabled)
                log.trace("reassigned default namespace declaration: " + prefix + "->" + namespaceURI);
        }
    }
}

From source file:org.jbpm.bpel.integration.soap.SoapUtilTest.java

public void testCopyChildElement_domSoap() throws Exception {
    // <soap:Envelope xmlns:soap='${SOAPConstants.URI_NS_SOAP_ENVELOPE}'>
    // <soap:Body xmlns:fish='urn:example:fish'>
    // <meal:lunch xmlns:produce='urn:example:produce'
    // xmlns:meal='urn:example:meal'>
    // <padding />
    // </meal:lunch>
    // </soap:Body>
    // </soap:Envelope>
    SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
    SOAPEnvelope envelope = soapMessage.getSOAPPart().getEnvelope();

    SOAPBody body = envelope.getBody();
    body.addNamespaceDeclaration("fish", "urn:example:fish");

    Name lunchName = envelope.createName("lunch", "meal", "urn:example:meal");
    SOAPElement lunch = body.addBodyElement(lunchName);
    lunch.addNamespaceDeclaration("produce", "urn:example:produce");
    lunch.addNamespaceDeclaration("meal", "urn:example:meal");

    SOAPElement source = SoapUtil.addChildElement(lunch, "padding");
    Element parent = XmlUtil.createElement("urn:example:meal", "lunch");

    // perform the copy
    SoapUtil.copyChildElement(parent, source);
    Element padding = XmlUtil.getElement(parent, "padding");

    // unqualified element
    assertNull(padding.getPrefix());//from   www . jav a 2  s  . c  om

    // reload
    // parent = writeAndRead(parent);
    padding = XmlUtil.getElement(parent, "padding");

    // unqualified element
    assertNull(padding.getPrefix());
}