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:Main.java

/**
 * Returns the XPath to retrieve targetElement from rootElement. rootElement may be null, in this case the XPath starts with and includes
 * the farthest non-null ancestor of targetElement. If rootElement == targetElement, an empty string
 * is returned. /*from   w w w.j  a va 2 s  .  c  o m*/
 * @param includeElementIndex Indicates if the element indices in the form elementName[n] should
 * be included in the XPath. 
 * @param namespacesMap Maps namespace ids to namespace URIs.
 */
public static String getXPath(Element rootElement, Element targetElement, boolean includeElementIndex,
        Map<String, String> namespacesMap) {
    Stack<Element> elementPath = new Stack<Element>();

    // since we need the mapping the other way round, we invert the map
    Map<String, String> namespaceUriToIdMap = new HashMap<String, String>();
    for (Entry<String, String> entry : namespacesMap.entrySet()) {
        namespaceUriToIdMap.put(entry.getValue(), entry.getKey());
    }

    // recursively find all ancestors of targetElement (up to, not including, rootElement) 
    {
        Element currentElement = targetElement;
        while (currentElement != null && currentElement != rootElement) {
            elementPath.push(currentElement);
            Node parent = currentElement.getParentNode();
            if (parent instanceof Element) {
                currentElement = (Element) currentElement.getParentNode();
            } else {
                currentElement = null;
            }
        }
    }

    // construct XPath
    StringBuilder builder = new StringBuilder();
    while (!elementPath.isEmpty()) {
        Element currentElement = elementPath.pop();
        if (builder.length() > 0) {
            // don't include "/" at the beginning
            builder.append("/");
        }

        if (namespacesMap != null) {
            String namespace = currentElement.getNamespaceURI();
            if (namespace != null) {
                namespace = namespaceUriToIdMap.get(namespace);
                builder.append(namespace);
                builder.append(":");
            }
        }
        builder.append(currentElement.getLocalName());
        if (includeElementIndex) {
            int index = getElementIndex(currentElement);
            builder.append("[");
            builder.append(index);
            builder.append("]");
        }
    }
    return builder.toString();
}

From source file:org.carewebframework.shell.BaseXmlParser.java

/**
 * Returns the children under the specified tag. Compensates for namespace usage.
 * // w w  w. java2s  .c o m
 * @param tagName Name of tag whose children are sought.
 * @param element Element to search for tag.
 * @return Node list containing children of tag.
 */
protected NodeList getTagChildren(String tagName, Element element) {
    return element.getNamespaceURI() == null ? element.getElementsByTagName(tagName)
            : element.getElementsByTagNameNS(element.getNamespaceURI(), tagName);
}

From source file:org.springmodules.validation.bean.conf.namespace.ValangConditionParserDefinitionParser.java

protected boolean isFunctionDefinition(Element element) {
    return VALIDATION_BEANS_NAMESPACE.equals(element.getNamespaceURI())
            && FUNCTION_ELEMENT.equals(element.getLocalName());
}

From source file:org.springmodules.validation.bean.conf.namespace.ValangConditionParserDefinitionParser.java

protected boolean isDateParserDefinition(Element element) {
    return VALIDATION_BEANS_NAMESPACE.equals(element.getNamespaceURI())
            && DATE_PARSER_ELEMENT.equals(element.getLocalName());
}

From source file:DomUtils.java

/**
 * Get the parent element of the supplied element having the
 * specified tag name./* w w  w  .  ja  va  2 s  .co m*/
 * @param child Child element. 
 * @param parentLocalName Parent element local name.
 * @param namespaceURI Namespace URI of the required parent element,
 * or null if a non-namespaced get is to be performed.
 * @return The first parent element of "child" having the tagname "parentName",
 * or null if no such parent element exists.
 */
public static Element getParentElement(Element child, String parentLocalName, String namespaceURI) {

    Node parentNode = child.getParentNode();

    while (parentNode != null && parentNode.getNodeType() == Node.ELEMENT_NODE) {
        Element parentElement = (Element) parentNode;

        if (getName(parentElement).equalsIgnoreCase(parentLocalName)) {
            if (namespaceURI == null) {
                return parentElement;
            } else if (parentElement.getNamespaceURI().equals(namespaceURI)) {
                return parentElement;
            }
        }
        parentNode = parentNode.getParentNode();
    }

    return null;
}

From source file:com.sitewhere.configuration.ConfigurationMigrationSupport.java

/**
 * Perform the migration work.//  w w w  . ja v a 2 s  . c  o m
 * 
 * @param document
 * @throws SiteWhereException
 */
protected static void migrateTenantConfiguration(Document document) throws SiteWhereException {
    Element beans = document.getDocumentElement();
    Element config = DomUtils.getChildElementByTagName(beans, "tenant-configuration");
    if (config == null) {
        throw new SiteWhereException("Tenant configuration element not found.");
    }
    Element dcomm = DomUtils.getChildElementByTagName(config, "device-communication");
    if (dcomm == null) {
        throw new SiteWhereException("Device communication element missing.");
    }
    Element asset = DomUtils.getChildElementByTagName(config, "asset-management");
    if (asset == null) {
        throw new SiteWhereException("Asset management element missing.");
    }
    Element eproc = DomUtils.getChildElementByTagName(config, "event-processing");
    if (eproc == null) {
        LOGGER.info("Event processing element missing. Migrating to new configuration format.");
        eproc = document.createElementNS(config.getNamespaceURI(), "event-processing");
        eproc.setPrefix(config.getPrefix());
        config.insertBefore(eproc, asset);

        moveEventProcessingElements(config, dcomm, eproc, document);
    }
    document.normalizeDocument();
}

From source file:DomUtils.java

/**
 * Get the child elements having the supplied localname and namespace.
 * <p/>//from   w  ww.j  av a  2s  .c om
 * Can be used instead of XPath.
 * @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 getElements(NodeList nodeList, String localname, String namespaceURI) {

    int count = nodeList.getLength();
    Vector elements = new Vector();

    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:de.betterform.xml.xforms.CustomElementFactory.java

/**
 * Given an element, generate a key used to find objects in ELEMENTS.
 * //w  w w . j a  va2s  .  c om
 * @param element
 *            Element whose configuration is to be looked for.
 * 
 * @return The key corresponding to the element definition.
 */
public String getKeyForElement(Element element) {
    return element.getNamespaceURI() + ":" + element.getLocalName();
}

From source file:fr.mael.microrss.util.XMLUtil.java

public boolean isAtom(String URL) throws Exception {
    DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
    f.setNamespaceAware(true);//  www  .  j a  va2 s  . com
    DocumentBuilder builder = f.newDocumentBuilder();
    HttpGet get = new HttpGet(URL);
    HttpResponse response = client.execute(get);
    try {
        Document doc = builder.parse(response.getEntity().getContent());
        Element e = doc.getDocumentElement();
        return e.getLocalName().equals("feed") && e.getNamespaceURI().equals("http://www.w3.org/2005/Atom");
    } catch (Exception e) {
        EntityUtils.consume(response.getEntity());
        throw new Exception(e);
    }
}

From source file:bpelg.packaging.ode.util.BgSchemaCollection.java

protected void handleImports(BgSchema schema) throws Exception {
    NodeList children = schema.getRoot().getChildNodes();
    List<Element> imports = new ArrayList<>();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child instanceof Element) {
            Element ce = (Element) child;
            if ("http://www.w3.org/2001/XMLSchema".equals(ce.getNamespaceURI())
                    && "import".equals(ce.getLocalName())) {
                imports.add(ce);/*from w  ww  .  j  ava 2 s .  c  o  m*/
            }
        }
    }
    for (Element ce : imports) {
        String namespace = ce.getAttribute("namespace");
        if (schemas.get(namespace) == null) {
            String location = ce.getAttribute("schemaLocation");
            if (location != null && !"".equals(location)) {
                read(location, schema.getSourceUri());
            }
        }
        schema.addImport(namespace);
        schema.getRoot().removeChild(ce);
    }
}