Example usage for org.w3c.dom Element getLocalName

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

Introduction

In this page you can find the example usage for org.w3c.dom Element 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.smscserver.config.spring.SpringUtil.java

/**
 * Get the first child element matching the local name and namespace
 * //ww w  .  j av a  2s  .  c  o m
 * @param parent
 *            The element for which to locate the child
 * @param ns
 *            The namespace to match, or null for any namespace
 * @param localName
 *            The local name to match, or null for any local name
 * @return The first child matching the criteria
 */
public static Element getChildElement(final Element parent, final String ns, final String localName) {
    List<Element> elements = SpringUtil.getChildElements(parent);

    for (Element element : elements) {
        if (((ns == null) || (ns.equals(element.getNamespaceURI())
                && ((localName == null) || localName.equals(element.getLocalName()))))) {
            return element;
        }
    }

    return null;
}

From source file:org.apache.smscserver.config.spring.SpringUtil.java

/**
 * Get the text context of first child element matching the local name and namespace
 * //from  w  w  w . ja v a 2 s  .c  om
 * @param parent
 *            The element for which to locate the child
 * @param ns
 *            The namespace to match, or null for any namespace
 * @param localName
 *            The local name to match, or null for any local name
 * @return The text content of the first child matching the criteria or null if element not found
 */
public static String getChildElementText(final Element parent, final String ns, final String localName) {
    List<Element> elements = SpringUtil.getChildElements(parent);

    for (Element element : elements) {
        if (((ns == null) || (ns.equals(element.getNamespaceURI())
                && ((localName == null) || localName.equals(element.getLocalName()))))) {
            return DomUtils.getTextValue(element);
        }
    }

    return null;
}

From source file:Main.java

/**
 * Append XPath [expr] like /a/b[1] or /a/b[@name='x']
 * @param sb// ww w.j  av  a 2s . co  m
 * @param n
 */
private static void appendElementQualifier(StringBuffer sb, Element n) {
    if (n.getParentNode() == n.getOwnerDocument()) {
        return;
    }

    if (n.getAttributes() != null && n.getAttributes().getNamedItem("name") != null) {
        sb.append("[@name='").append(n.getAttributes().getNamedItem("name").getNodeValue()).append("']");
    } else if (getChildrenCount((Element) n.getParentNode(), n.getNamespaceURI(), n.getLocalName()) != 1) {
        sb.append("[").append(getPosition(n)).append("]");
    }
}

From source file:Main.java

/**
 * Returns the index of element in the list of all elements with the same name in its parent node.
 * If element's parent node is null, this function returns 0.
 *//*  ww w . j  a  v  a2  s .c  om*/
public static int getElementIndex(Element element) {
    int index = 1;
    Node sibling = element;
    while ((sibling = sibling.getPreviousSibling()) != null) {
        if (sibling instanceof Element) {
            Element siblingElement = (Element) sibling;

            // check if element names and element namespaces match 
            if (element.getLocalName().equals(siblingElement.getLocalName())
                    && (element.getNamespaceURI() == null ? siblingElement.getNamespaceURI() == null
                            : element.getNamespaceURI().equals(siblingElement.getNamespaceURI()))) {
                ++index;
            }
        }
    }
    return index;
}

From source file:Main.java

/**
 * Looks up a child with the given (local) name.
 * <p>/*from w  w  w . j a  v  a 2  s  .co  m*/
 * An array of several names may be passed, in which case they will be traversed in a simple
 * XPath-like fashion.
 */

public static Element getChildNamed(Element element, String... names) {

    if (element == null) {
        return null;
    }

    Element child = null;
    NodeList children = element.getChildNodes();

    outer: for (String name : names) {
        for (int loop = 0, length = children.getLength(); loop < length; loop++) {
            Node node = children.item(loop);

            if (!(node instanceof Element)) {
                continue;
            }

            child = (Element) node;

            if (name.equals(child.getLocalName())) {
                children = child.getChildNodes();
                continue outer;
            }
        }

        // No match found

        return null;
    }

    return child;
}

From source file:Main.java

/**
 * Performs the actual recursive dumping of a DOM tree to a given
 * <CODE>PrintStream</CODE>. Note that dump is intended to be a detailed
 * debugging aid rather than pretty to look at. 
 * /*from  w  w w  .  ja v  a2  s.  co m*/
 * @param    out            The <CODE>PrintStream</CODE> to write to.
 * @param    node         The <CODE>Node</CODE> under consideration.
 * @param    indent         The level of indentation.
 * @see      #dump(Node)
 * @see      #dump(PrintStream, Node)
 * @since   TFP 1.0
 */
private static void doDump(PrintStream out, final Node node, int indent) {
    if (node != null) {
        for (int index = 0; index < indent; ++index)
            out.write(' ');

        switch (node.getNodeType()) {
        case Node.DOCUMENT_NODE: {
            Document document = (Document) node;

            out.println("DOCUMENT:");

            doDump(out, document.getDoctype(), indent + 1);
            doDump(out, document.getDocumentElement(), indent + 1);
            break;
        }

        case Node.DOCUMENT_TYPE_NODE: {
            DocumentType type = (DocumentType) node;

            out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId="
                    + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]");
            break;
        }

        case Node.ELEMENT_NODE: {
            Element element = (Element) node;

            out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name="
                    + format(element.getLocalName()) + "]");

            NamedNodeMap attrs = element.getAttributes();

            for (int index = 0; index < attrs.getLength(); ++index)
                doDump(out, attrs.item(index), indent + 1);

            for (Node child = element.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }
        case Node.ATTRIBUTE_NODE: {
            Attr attr = (Attr) node;

            out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix="
                    + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value="
                    + format(attr.getNodeValue()) + "]");
            break;
        }

        case Node.TEXT_NODE: {
            Text text = (Text) node;

            out.println("TEXT: [" + format(text.getNodeValue()) + "]");

            for (Node child = text.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }

        case Node.CDATA_SECTION_NODE: {
            CDATASection data = (CDATASection) node;

            out.println("CDATA: [" + format(data.getNodeValue()) + "]");
            break;
        }

        case Node.COMMENT_NODE: {
            Comment comm = (Comment) node;

            out.println("COMMENT: [" + format(comm.getNodeValue()) + "]");
            break;
        }

        default:
            out.println("UNKNOWN: [type=" + node.getNodeType() + "]");
            break;
        }
    }
}

From source file:Main.java

/**
 * Performs the actual recursive dumping of a DOM tree to a given
 * <CODE>PrintWriter</CODE>. Note that dump is intended to be a detailed
 * debugging aid rather than pretty to look at. 
 * //from   w w  w. ja  v a 2 s . co m
 * @param    out            The <CODE>PrintWriter</CODE> to write to.
 * @param    node         The <CODE>Node</CODE> under consideration.
 * @param    indent         The level of indentation.
 * @see      #dump(PrintWriter, Node)
 * @since   TFP 1.0
 */
private static void doDump(PrintWriter out, final Node node, int indent) {
    if (node != null) {
        for (int index = 0; index < indent; ++index)
            out.write(' ');

        switch (node.getNodeType()) {
        case Node.DOCUMENT_NODE: {
            Document document = (Document) node;

            out.println("DOCUMENT:");

            doDump(out, document.getDoctype(), indent + 1);
            doDump(out, document.getDocumentElement(), indent + 1);
            break;
        }

        case Node.DOCUMENT_TYPE_NODE: {
            DocumentType type = (DocumentType) node;

            out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId="
                    + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]");
            break;
        }

        case Node.ELEMENT_NODE: {
            Element element = (Element) node;

            out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name="
                    + format(element.getLocalName()) + "]");

            NamedNodeMap attrs = element.getAttributes();

            for (int index = 0; index < attrs.getLength(); ++index)
                doDump(out, attrs.item(index), indent + 1);

            for (Node child = element.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }
        case Node.ATTRIBUTE_NODE: {
            Attr attr = (Attr) node;

            out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix="
                    + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value="
                    + format(attr.getNodeValue()) + "]");
            break;
        }

        case Node.TEXT_NODE: {
            Text text = (Text) node;

            out.println("TEXT: [" + format(text.getNodeValue()) + "]");

            for (Node child = text.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }

        case Node.CDATA_SECTION_NODE: {
            CDATASection data = (CDATASection) node;

            out.println("CDATA: [" + format(data.getNodeValue()) + "]");
            break;
        }

        case Node.COMMENT_NODE: {
            Comment comm = (Comment) node;

            out.println("COMMENT: [" + format(comm.getNodeValue()) + "]");
            break;
        }

        default:
            out.println("UNKNOWN: [type=" + node.getNodeType() + "]");
            break;
        }
    }
}

From source file:gov.nist.healthcare.ttt.parsing.Parsing.java

public static String getWsseHeaderFromMTOM(String mtom) throws IOException, MessagingException {

    SOAPWithAttachment swa = Parsing.parseMtom(mtom);
    String wsseHeader = null;/*from  ww  w.j  a v a2  s .c om*/
    Envelope env = (Envelope) JAXB.unmarshal(new StringReader(swa.getSoap()), Envelope.class);
    List<Object> headers = env.getHeader().getAny();
    if (headers == null) {
        return null;
    }
    Iterator it = headers.iterator();
    while (it.hasNext()) {
        Element header = (Element) it.next();
        if (header.getLocalName().equals(ELEMENT_NAME_WSSE_SECURITY)
                && header.getNamespaceURI().equals(NAMESPACE_WSSE)) {
            wsseHeader = MiscUtil.xmlToString(header);
            break;
        }
    }
    return wsseHeader;
}

From source file:gov.nist.healthcare.ttt.parsing.Parsing.java

private static boolean isSoapValidDirectAddressBlock(String soap) {
    Envelope env = (Envelope) JAXB.unmarshal(new StringReader(soap), Envelope.class);
    List<Object> headers = env.getHeader().getAny();
    if (headers == null) {
        return false;
    }//from  w ww . j  a  v  a 2 s  . c o m
    Iterator it = headers.iterator();
    boolean foundDirectAddressBlock = false;
    while (it.hasNext()) {
        Element header = (Element) it.next();
        if (header.getLocalName().equals(ELEMENT_NAME_DIRECT_ADDRESS_BLOCK)
                && header.getNamespaceURI().equals(NAMESPACE_DIRECT)) {
            foundDirectAddressBlock = true;
            NodeList directFrom = header.getElementsByTagNameNS(NAMESPACE_DIRECT, ELEMENT_NAME_DIRECT_FROM);
            NodeList directTo = header.getElementsByTagNameNS(NAMESPACE_DIRECT, ELEMENT_NAME_DIRECT_TO);
            if (directFrom.getLength() > 0 && directTo.getLength() > 0) {
                return true;
            } else {
                return false;
            }
        }
    }
    return false;

}

From source file:gov.nist.healthcare.ttt.parsing.Parsing.java

public static DirectAddressing getDirectAddressing(String mtom) throws MessagingException, IOException {

    SOAPWithAttachment swa = Parsing.parseMtom(mtom);
    DirectAddressing directAddressing = new DirectAddressing();
    Envelope env = (Envelope) JAXB.unmarshal(new StringReader(swa.getSoap()), Envelope.class);
    List<Object> headers = env.getHeader().getAny();
    if (headers == null) {
        return directAddressing;
    }//from w  w w  .j a v a2  s  . co m
    Iterator it = headers.iterator();
    boolean foundDirectAddressBlock = false;
    while (it.hasNext()) {
        Element header = (Element) it.next();
        if (header.getLocalName().equals(ELEMENT_NAME_DIRECT_ADDRESS_BLOCK)
                && header.getNamespaceURI().equals(NAMESPACE_DIRECT)) {
            foundDirectAddressBlock = true;
            NodeList directFrom = header.getElementsByTagNameNS(NAMESPACE_DIRECT, ELEMENT_NAME_DIRECT_FROM);
            NodeList directTo = header.getElementsByTagNameNS(NAMESPACE_DIRECT, ELEMENT_NAME_DIRECT_TO);

            directAddressing.setDirectFrom(directFrom.item(0).getFirstChild().getNodeValue());
            directAddressing.setDirectTo(directTo.item(0).getFirstChild().getNodeValue());
        } else if (header.getLocalName().equals(ELEMENT_NAME_WSA_MESSAGEID)
                && header.getNamespaceURI().equals(NAMESPACE_WSA)) {
            directAddressing.setMessageID(header.getFirstChild().getNodeValue());
        }
    }
    return directAddressing;
}