Example usage for javax.xml.soap SOAPElement getNamespaceURI

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

Introduction

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

Prototype

public String getNamespaceURI(String prefix);

Source Link

Document

Returns the URI of the namespace that has the given prefix.

Usage

From source file:com.inbravo.scribe.rest.service.crm.ms.auth.SOAPExecutor.java

/**
 * Namespace context resolver/* ww  w.  j a  va 2 s.c o m*/
 * 
 * @param context namespace context
 * @param element SOAP message element
 */
public static final void addNamespaces(final SimpleNamespaceContext context, final SOAPElement element) {
    final Iterator<?> namespaces = element.getNamespacePrefixes();
    while (namespaces.hasNext()) {
        final String prefix = (String) namespaces.next();
        final String uri = element.getNamespaceURI(prefix);
        context.addNamespace(prefix, uri);
    }
}

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

@Validated
@Test/*w  w  w  .j  ava2  s.co m*/
public void testGetNamespaceURIUnbound() {
    SOAPElement element = saajUtil.createSOAPElement(null, "test", null);
    assertNull(element.getNamespaceURI("p"));
}

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

/**
 * Test that {@link SOAPElement#getNamespaceURI(String)} only takes into account explicit
 * namespace declarations. This is in contrast to
 * {@link org.w3c.dom.Node#lookupNamespaceURI(String)}, which also takes into account prefixes
 * used on elements, but for which no explicit namespace declaration exists.
 *///from  w  w w . j a v  a  2s  .  c om
@Validated
@Test
public void testGetNamespaceURIStrictLookup() {
    SOAPElement element = saajUtil.createSOAPElement("urn:ns", "test", "p");
    assertNull(element.getNamespaceURI("p"));
}

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

@Validated
@Test/*w  w  w.j  a  v  a2s . c  o m*/
public void testGetNamespaceURIForPrefixDeclaredOnParent() throws Exception {
    SOAPElement parent = saajUtil.createSOAPElement("urn:ns", "test", "p");
    parent.addNamespaceDeclaration("p", "urn:ns");
    SOAPElement child = parent.addChildElement(new QName("child"));
    assertEquals("urn:ns", child.getNamespaceURI("p"));
}

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

@Validated
@Test//from www . ja  v  a 2s  .co  m
public void testGetNamespaceURIDefault() throws Exception {
    SOAPElement element = saajUtil.createSOAPElement("urn:ns", "test", null);
    element.addNamespaceDeclaration("", "urn:ns");
    assertEquals("urn:ns", element.getNamespaceURI(""));
}

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

/**
 * update the tag data of the SOAPElement
 *
 * @param NameCreator nc/*from   ww  w .  j  a  va 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 void copyNamespaces(Element target, SOAPElement source, Iterator prefixIt) {
    // namespace declarations appear as attributes in the target element
    while (prefixIt.hasNext()) {
        String prefix = (String) prefixIt.next();
        String namespaceURI = source.getNamespaceURI(prefix);
        // BPEL-195: prevent addition matching visible declaration at target
        if (prefix.equals(XmlUtil.getPrefix(namespaceURI, target)))
            continue;
        XmlUtil.addNamespaceDeclaration(target, namespaceURI, prefix);
        if (traceEnabled)
            log.trace("added namespace declaration: " + prefix + "->" + namespaceURI);
    }//ww  w .  ja v  a  2 s  . c om
}

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);
        }/* w w w.  j  a v a2s .c  om*/
    } 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;//from w ww  . j av a  2 s.  com
    // 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.SoapUtil.java

public static String getPrefix(String namespaceURI, SOAPElement contextElem) {
    Iterator prefixIt = contextElem.getVisibleNamespacePrefixes();
    while (prefixIt.hasNext()) {
        String prefix = (String) prefixIt.next();
        if (namespaceURI.equals(contextElem.getNamespaceURI(prefix)))
            return prefix;
    }//  w w  w . j a  v  a  2s  .co m
    return null;
}