Example usage for javax.xml.soap SOAPElement removeNamespaceDeclaration

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

Introduction

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

Prototype

public boolean removeNamespaceDeclaration(String prefix);

Source Link

Document

Removes the namespace declaration corresponding to the given prefix.

Usage

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

@Validated
@Test// www  .ja  va2  s  .  c  o  m
public void testRemoveNamespaceDeclaration() throws SOAPException {
    SOAPElement element = saajUtil.createSOAPElement(null, "test", null);
    element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:p", "urn:ns");
    assertTrue(element.removeNamespaceDeclaration("p"));
    assertEquals(0, element.getAttributes().getLength());
}

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

/**
 * update the tag data of the SOAPElement
 *
 * @param NameCreator nc//  w  w  w .  ja  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

public static void removeNamespaces(SOAPElement elem) {
    Iterator prefixIt = elem.getNamespacePrefixes();
    while (prefixIt.hasNext()) {
        String prefix = (String) prefixIt.next();
        elem.removeNamespaceDeclaration(prefix);
    }/*from  w  w  w. ja  v  a2 s . c om*/
}

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

public void testCopyVisibleNamespaces_soapDom_targetMatch() throws Exception {
    String xml = "<part xmlns:produce='urn:example:produce'>"
            + " <lunch produce:lettuce='0.1lb' fish:fillet='0.25lb' "
            + "  xmlns:fish='urn:example:fish' xmlns='urn:example:meal'/>" + "</part>";
    Element source = XmlUtil.getElement(XmlUtil.parseText(xml), "urn:example:meal", "lunch");

    String targetXml = "<soap:Envelope xmlns:soap='" + SOAPConstants.URI_NS_SOAP_ENVELOPE + "'>"
            + " <soap:Body>"
            + "  <other:Operation xmlns:produce='urn:example:produce' xmlns:meal='urn:example:meal'"
            + "   xmlns:other='urn:example:other'>" + "   <lunch />" + "  </other:Operation>" + " </soap:Body>"
            + "</soap:Envelope>";
    SOAPMessage soapMessage = parseSoap(targetXml);
    SOAPElement operation = SoapUtil.getElement(soapMessage.getSOAPBody(), "urn:example:other", "Operation");
    SOAPElement target = SoapUtil.getElement(operation, "lunch");

    // in the WS4EE stack, target contains the *visible* namespace after parsing
    target.removeNamespaceDeclaration("produce");
    target.removeNamespaceDeclaration("meal");

    // perform the copy
    SoapUtil.copyVisibleNamespaces(target, source);
    List prefixes = IteratorUtils.toList(target.getNamespacePrefixes());

    // prefixed declaration
    assertTrue(prefixes.contains("fish"));
    assertEquals("urn:example:fish", target.getNamespaceURI("fish"));
    // parent prefixed declaration
    assertFalse(prefixes.contains("produce"));
    assertEquals("urn:example:produce", target.getNamespaceURI("produce"));
    // default declaration (reassigned)
    assertFalse(prefixes.contains("meal"));
    assertEquals("urn:example:meal", target.getNamespaceURI("meal"));
}