Example usage for org.w3c.dom Node getLocalName

List of usage examples for org.w3c.dom Node getLocalName

Introduction

In this page you can find the example usage for org.w3c.dom Node getLocalName.

Prototype

public String getLocalName();

Source Link

Document

Returns the local part of the qualified name of this node.

Usage

From source file:org.apache.axis2.jaxws.client.dispatch.BaseDispatch.java

/**
 * Given a JAXWS Message Context which contains an outbound service-requester Message for a Dispatch client,
 * determine the QName of the first body element contained in that message.
 * /*from w ww . j  a va  2 s  .co m*/
 * @param requestMessageCtx requestMessageCtx JAXWS Message Context containing the outbound Dispatch message
 * @return the QName of the first body element contained in the outbound Dispatch message, or null if it 
 * can not be determined.
 */
QName getBodyElementQNameFromDispatchMessage(MessageContext requestMessageCtx) {
    QName bodyElementQName = null;
    Message dispatchMessage = requestMessageCtx.getMessage();
    SOAPMessage soapMessage = dispatchMessage.getAsSOAPMessage();
    try {
        SOAPBody soapBody = soapMessage.getSOAPBody();
        Node firstElement = soapBody.getFirstChild();
        // A Doc/Lit/Bare message may not have a firsElement.  The soap:Body element may be empty if there 
        // are no arguments to the operation.
        if (firstElement != null) {
            String ns = firstElement.getNamespaceURI();
            String lp = firstElement.getLocalName();
            // A Doc/Lit/Bare message may not have a localPart on the element.  That can happen if the first element
            // is the argument value and there is no wrapper element surrounding it.
            if (lp != null) {
                bodyElementQName = new QName(ns, lp);
            }
        }
    } catch (SOAPException e) {
        if (log.isDebugEnabled()) {
            log.debug("Unabled to get the first body element from the outbound dispatch message", e);
        }
    }
    return bodyElementQName;
}

From source file:org.apache.axis2.jaxws.wsdl.impl.SchemaReaderImpl.java

private boolean isElementName(String name, Node domNode) {
    if (domNode == null) {
        return false;
    }//w  ww  .j a  va2  s  . com
    if (domNode.getNodeType() == Node.ELEMENT_NODE) {
        String localName = domNode.getLocalName();
        return localName != null && localName.equals(name);
    }
    return false;
}

From source file:org.apache.camel.component.xmlsecurity.api.XAdESSignatureProperties.java

protected void replacePrefix(Element el, Input input) {
    replacePrefixForNode(el, input);/* w  w  w  .  j ava2s . c o m*/
    NamedNodeMap nnm = el.getAttributes();
    List<Attr> xmlnsToBeRemoved = new ArrayList<Attr>(2);
    int length = nnm.getLength();
    for (int i = 0; i < length; i++) {
        Node attr = nnm.item(i);
        replacePrefixForNode(attr, input);
        if (attr.getNodeType() == Node.ATTRIBUTE_NODE) {
            if ("xmlns".equals(attr.getLocalName()) || "xmlns".equals(attr.getPrefix())) {
                if (XMLSignature.XMLNS.equals(attr.getTextContent())
                        || findNamespace(input.getMessage()).equals(attr.getTextContent())) {
                    xmlnsToBeRemoved.add((Attr) attr);
                }
            }
        }
    }
    // remove xml namespace declaration for XML signature and XAdES namespace
    for (Attr toBeRemoved : xmlnsToBeRemoved) {
        el.removeAttributeNode(toBeRemoved);
    }

}

From source file:org.apache.cocoon.bean.helpers.BeanConfigurator.java

private static String getNodeValue(Node node) throws IllegalArgumentException {
    StringBuffer s = new StringBuffer();
    NodeList children = node.getChildNodes();
    boolean found = false;

    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE) {
            s.append(child.getNodeValue());
            found = true;//from   w ww  . ja v  a2s  . c  o  m
        } else {
            throw new IllegalArgumentException("Unexpected node:" + child.getLocalName());
        }
    }
    if (!found) {
        throw new IllegalArgumentException("Expected value for " + node.getLocalName() + " node");
    }
    return s.toString();
}

From source file:org.apache.cocoon.forms.util.DomHelper.java

/**
 * Returns all Element children of an Element that belong to the given
 * namespace and have the given local name.
 *///from   w w  w  .  j  av a2 s . com
public static Element[] getChildElements(Element element, String namespace, String localName) {
    ArrayList elements = new ArrayList();
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node instanceof Element && namespace.equals(node.getNamespaceURI())
                && localName.equals(node.getLocalName())) {
            elements.add(node);
        }
    }
    return (Element[]) elements.toArray(new Element[elements.size()]);
}

From source file:org.apache.cocoon.forms.util.DomHelper.java

/**
 * Returns the first child element with the given namespace and localName,
 * or null if there is no such element and required flag is unset or
 * throws an Exception if the "required" flag is set.
 *//*from w w w  . j  av  a 2  s  . c  o  m*/
public static Element getChildElement(Element element, String namespace, String localName, boolean required)
        throws Exception {
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node instanceof Element && namespace.equals(node.getNamespaceURI())
                && localName.equals(node.getLocalName())) {
            return (Element) node;
        }
    }
    if (required) {
        throw new Exception("Missing element \"" + localName + "\" as child of element \""
                + element.getTagName() + "\" at " + DomHelper.getLocation(element));
    } else {
        return null;
    }
}

From source file:org.apache.cocoon.woody.util.DomHelper.java

/**
 * Returns all Element children of an Element that belong to the given
 * namespace and have the given local name.
 *//*from  ww  w  . j  a v  a 2  s.c  om*/
public static Element[] getChildElements(Element element, String namespace, String localName) {
    ArrayList elements = new ArrayList();
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node instanceof Element && namespace.equals(node.getNamespaceURI())
                && localName.equals(node.getLocalName())) {
            elements.add(node);
        }
    }
    return (Element[]) elements.toArray(new Element[0]);
}

From source file:org.apache.cocoon.xml.dom.DOMUtil.java

/**
 * Use a path to select the first occurence of a node. The namespace
 * of a node is ignored!//from   w  w  w. j  a v a  2 s.c  o m
 * @param contextNode The node starting the search.
 * @param path        The path to search the node. The
 *                    contextNode is searched for a child named path[0],
 *                    this node is searched for a child named path[1]...
 * @param create      If a child with the corresponding name is not found
 *                    and create is set, this node will be created.
*/
public static Node getFirstNodeFromPath(Node contextNode, final String[] path, final boolean create) {
    if (contextNode == null || path == null || path.length == 0)
        return contextNode;
    // first test if the node exists
    Node item = getFirstNodeFromPath(contextNode, path, 0);
    if (item == null && create == true) {
        int i = 0;
        NodeList childs;
        boolean found;
        int m, l;
        while (contextNode != null && i < path.length) {
            childs = contextNode.getChildNodes();
            found = false;
            if (childs != null) {
                m = 0;
                l = childs.getLength();
                while (found == false && m < l) {
                    item = childs.item(m);
                    if (item.getNodeType() == Node.ELEMENT_NODE
                            && item.getLocalName().equals(path[i]) == true) {
                        found = true;
                        contextNode = item;
                    }
                    m++;
                }
            }
            if (found == false) {
                Element e = contextNode.getOwnerDocument().createElementNS(null, path[i]);
                contextNode.appendChild(e);
                contextNode = e;
            }
            i++;
        }
        item = contextNode;
    }
    return item;
}

From source file:org.apache.cocoon.xml.dom.DOMUtil.java

/**
 * Private helper method for getFirstNodeFromPath()
 *//*from ww w  .j  a v  a2  s .  c o  m*/
private static Node getFirstNodeFromPath(final Node contextNode, final String[] path, final int startIndex) {
    int i = 0;
    NodeList childs;
    boolean found;
    int l;
    Node item = null;

    childs = contextNode.getChildNodes();
    found = false;
    if (childs != null) {
        i = 0;
        l = childs.getLength();
        while (found == false && i < l) {
            item = childs.item(i);
            if (item.getNodeType() == Node.ELEMENT_NODE && path[startIndex]
                    .equals(item.getLocalName() != null ? item.getLocalName() : item.getNodeName()) == true) {
                if (startIndex == path.length - 1) {
                    found = true;
                } else {
                    item = getFirstNodeFromPath(item, path, startIndex + 1);
                    if (item != null)
                        found = true;
                }
            }
            if (found == false) {
                i++;
            }
        }
        if (found == false) {
            item = null;
        }
    }
    return item;
}

From source file:org.apache.cocoon.xml.dom.DOMUtil.java

/**
 * Helper method for getNodeListFromPath()
 *//*from   w w  w .ja v  a 2s  . c o m*/
private static void getNodesFromPath(final NodeListImpl result, final Node contextNode, final String[] path,
        final int startIndex) {
    final NodeList childs = contextNode.getChildNodes();
    int m, l;
    Node item;
    if (startIndex == (path.length - 1)) {
        if (childs != null) {
            m = 0;
            l = childs.getLength();
            while (m < l) {
                item = childs.item(m);
                if (item.getNodeType() == Node.ELEMENT_NODE) {
                    // Work around: org.apache.xerces.dom.ElementImpl doesn't handle getLocalName() correct
                    if (path[startIndex].equals(
                            item.getLocalName() != null ? item.getLocalName() : item.getNodeName()) == true) {
                        result.addNode(item);
                    }
                }
                m++;
            }
        }
    } else {
        if (childs != null) {
            m = 0;
            l = childs.getLength();
            while (m < l) {
                item = childs.item(m);
                if (item.getNodeType() == Node.ELEMENT_NODE) {
                    // Work around: org.apache.xerces.dom.ElementImpl doesn't handle getLocalName() correct
                    if (path[startIndex].equals(
                            item.getLocalName() != null ? item.getLocalName() : item.getNodeName()) == true) {
                        getNodesFromPath(result, item, path, startIndex + 1);
                    }
                }
                m++;
            }
        }
    }
}