Example usage for org.w3c.dom Element getNamespaceURI

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

Introduction

In this page you can find the example usage for org.w3c.dom Element getNamespaceURI.

Prototype

public String getNamespaceURI();

Source Link

Document

The namespace URI of this node, or null if it is unspecified (see ).

Usage

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

/**
 * Get the first child element matching the local name and namespace
 * /*from  www.  j av a2  s. 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 = 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.ftpserver.config.spring.SpringUtil.java

/**
 * Get the text context of first child element matching the local name and
 * namespace/* w w  w.  j  a  v a2 s  . com*/
 * 
 * @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 = 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:org.apache.smscserver.config.spring.SpringUtil.java

/**
 * Get the first child element matching the local name and namespace
 * //w w w .  ja  v a2  s  .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 ww .j  a  v  a 2  s.  co 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 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

/**
 * Get the child elements having the supplied localname and namespace.
 * Can be used instead of XPath.//from   ww w .  j  a v a 2  s. c  o  m
 * @param nodeList List of DOM nodes on which to perform the search.
 * @param localname Localname of the element required.  Supports "*" wildcards.
 * @param namespaceURI Namespace URI of the required element, or null
 * if a namespace comparison is not to be performed.
 * @return A list of W3C DOM {@link Element}s.  An empty list if no such
 * child elements exist on the parent element.
 */
public static List<Element> getElements(NodeList nodeList, String localname, String namespaceURI) {
    int count = nodeList.getLength();
    List<Element> elements = new ArrayList<>();
    for (int i = 0; i < count; i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            if (localname.equals("*") || getName(element).equals(localname)) {
                // The local name matches the element we're after...
                if (namespaceURI == null || namespaceURI.equals(element.getNamespaceURI())) {
                    elements.add(element);
                }
            }
        }
    }
    return elements;
}

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. 
 * // w w w.ja  v a  2 s  .  c o 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  . j  ava2s . c o 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:Main.java

/**
 * Append XPath [expr] like /a/b[1] or /a/b[@name='x']
 * @param sb/*w w  w. j  a  v  a 2 s. c  o  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:edu.internet2.middleware.shibboleth.common.config.SpringConfigurationUtils.java

/**
 * Creates a {@link BeanDefinition} from a custom element.
 * /*from   www . j  a  v  a  2  s.c  om*/
 * @param element configuration element
 * @param parserContext currently parser context
 * 
 * @return the bean definition
 */
private static BeanDefinition createBeanDefinition(Element element, ParserContext parserContext) {
    BeanDefinitionParserDelegate delegate = parserContext.getDelegate();
    String namespaceUri = element.getNamespaceURI();

    if (XMLHelper.hasXSIType(element)) {
        namespaceUri = XMLHelper.getXSIType(element).getNamespaceURI();
    }

    NamespaceHandler handler = delegate.getReaderContext().getNamespaceHandlerResolver().resolve(namespaceUri);
    if (handler == null) {
        log.error("Unable to locate NamespaceHandler for namespace [" + namespaceUri + "]");
        return null;
    }
    return handler.parse(element, new ParserContext(delegate.getReaderContext(), delegate));
}

From source file:Main.java

public static Element getElementByTagNameNS(Node element, String namespace, String name) {
    NodeList elements = element.getChildNodes();
    CharSequence colon = ":";
    if (elements != null) {
        for (int i = 0; i < elements.getLength(); i++) {
            if (elements.item(i).getNodeType() == Node.ELEMENT_NODE && (elements.item(i).getAttributes()
                    .getNamedItemNS("http://www.w3.org/2001/XMLSchema-instance", "nil") == null
                    || !"true".equals(elements.item(i).getAttributes()
                            .getNamedItemNS("http://www.w3.org/2001/XMLSchema-instance", "nil")))) {
                Element currentElement = (Element) elements.item(i);
                String nodeName = currentElement.getNodeName();
                String nodeNameOnly = nodeName;
                if (nodeName.contains(colon)) {
                    String[] nodeNameSplit = nodeName.split(":");
                    nodeNameOnly = nodeNameSplit[1];
                }/*  ww  w  . ja  v a  2 s  . c o m*/

                if ((currentElement.getNamespaceURI() == null
                        || currentElement.getNamespaceURI().equals(namespace)) && nodeNameOnly.equals(name)) {
                    return currentElement;
                }
            }
        }
    }

    return null;
}