Example usage for org.w3c.dom Element getParentNode

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

Introduction

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

Prototype

public Node getParentNode();

Source Link

Document

The parent of this node.

Usage

From source file:cz.incad.kramerius.rest.api.k5.client.search.SearchResource.java

public static void filterFieldsInDOM(Element docE) {
    // filter/* w ww  . j a va  2  s  . co m*/
    String[] filters = KConfiguration.getInstance().getAPISolrFilter();
    for (final String name : filters) {
        Element found = findSolrElement(docE, name);
        if (found != null) {
            Node parentNode = found.getParentNode();
            Node removed = parentNode.removeChild(found);
        }
    }
}

From source file:org.dozer.eclipse.plugin.editorpage.utils.DozerUiUtils.java

public static TableViewerSelectionListener createDeleteSelectionListener(IDOMModel model) {
    TableViewerSelectionListener listener = new TableViewerSelectionListener(model) {

        @Override//from  w  w  w .j  a  v a 2s. c om
        protected void invoceSelected(Object selected) {
            Element selection = (Element) selected;
            Element parentNode = (Element) selection.getParentNode();

            IObservableList values = (IObservableList) getTableViewer().getInput();
            values.remove(selection);

            //if last node had been deleted, delete parent
            if (DOMUtils.getElements(parentNode).length == 0) {
                Element parentParentNode = (Element) parentNode.getParentNode();

                parentNode.getParentNode().removeChild(parentNode);

                if (DOMUtils.getElements(parentParentNode).length == 0) {
                    parentParentNode.getParentNode().removeChild(parentParentNode);
                }
            }
        }

    };

    return listener;
}

From source file:Main.java

/**
 * Rename an element in a DOM document. It happens to involves a node
 * replication.//ww w. j a va 2  s  . c o m
 * 
 * @param document
 *            The document containing the element (some way to verify
 *            that?).
 */
public static Element renameElement(Document document, Element element, String newName, String namespace) {
    if (namespace == null) {
        throw new IllegalArgumentException("No namespace provided for element " + element);
    }
    Element newElement = document.createElementNS(namespace, newName);
    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0, iEnd = attributes.getLength(); i < iEnd; i++) {
        Attr attr2 = (Attr) document.importNode(attributes.item(i), true);
        newElement.getAttributes().setNamedItem(attr2);
    }
    while (element.hasChildNodes()) {
        newElement.appendChild(element.getFirstChild());
    }
    element.getParentNode().replaceChild(newElement, element);
    return newElement;
}

From source file:Main.java

/**
 * Changes the tag name of an element.//from   w w w.ja  v  a2s. co  m
 * 
 * @param elem Element which name should be changed
 * @param newName new tag name of the element
 * @return true if the name was changed successfully or false otherwise
 */
static public boolean changeTagName(Element elem, String newName) {
    if (elem == null)
        return false; // not Found!
    Document doc = elem.getOwnerDocument();
    Element newElem = doc.createElement(newName);

    // Copy the attributes to the new element
    NamedNodeMap attrs = elem.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr2 = (Attr) doc.importNode(attrs.item(i), true);
        newElem.getAttributes().setNamedItem(attr2);
    }

    // Copy all Child Elements
    for (Node node = elem.getFirstChild(); node != null; node = node.getNextSibling())
        newElem.appendChild(node.cloneNode(true));
    // insert
    Node parent = elem.getParentNode();
    parent.replaceChild(newElem, elem);
    return true;
}

From source file:Main.java

/**
 * Rename an element, replacing it in its document.
 * @param element the element to rename.
 * @param name the new element name.//from  w w  w. j  a  v a2 s  . co  m
 * @return the renamed element.
 */
public static Element renameElement(Element element, String name) {
    if (element.getNodeName().equals(name))
        return element;
    Element el = element.getOwnerDocument().createElement(name);

    //Copy the attributes
    NamedNodeMap attributes = element.getAttributes();
    int nAttrs = attributes.getLength();
    for (int i = 0; i < nAttrs; i++) {
        Node attr = attributes.item(i);
        el.setAttribute(attr.getNodeName(), attr.getNodeValue());
    }

    //Copy the children
    Node node = element.getFirstChild();
    while (node != null) {
        Node clone = node.cloneNode(true);
        el.appendChild(clone);
        node = node.getNextSibling();
    }

    //Replace the element
    element.getParentNode().replaceChild(el, element);
    return el;
}

From source file:Main.java

/**
 * Gather all the namespaces defined on a node
 *
 * @return/*from  ww w  .  jav a2  s. co  m*/
 */
public static Iterable<Entry<String, String>> getNamespaces(Element element) {
    TreeMap<String, String> map = new TreeMap<String, String>();
    do {
        NamedNodeMap attributes = element.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Attr attr = (Attr) attributes.item(i);
            final String name = attr.getLocalName();

            if (attr.getPrefix() != null) {
                if ("xmlns".equals(attr.getPrefix()))
                    if (!map.containsKey(name))
                        map.put(name, attr.getValue());
            } else if ("xmlns".equals(name)) {
                if (!map.containsKey(""))
                    map.put("", attr.getValue());
            }
        }
        if (element.getParentNode() == null || element.getParentNode().getNodeType() != Node.ELEMENT_NODE)
            break;
        element = (Element) element.getParentNode();
    } while (true);
    return map.entrySet();
}

From source file:XMLUtils.java

/**
 * Retrieve the namespace for a given prefix. 
 * Does a lookup into the parent hierarchy.
 * @param el/*from   w w  w.  j ava2 s  .co m*/
 * @param prefix
 * @return
 */
public static String getNamespace(Element el, String prefix) {
    Element parent = el;
    while (parent != null) {
        String ns = parent.getAttribute("xmlns:" + prefix);
        if (ns != null && ns.length() > 0) {
            return ns;
        }
        // get the parent
        Node n = parent.getParentNode();
        if (n instanceof Element) {
            parent = (Element) n;
        } else {
            parent = null;
        }
    }
    // nothing found
    return null;
}

From source file:Utils.java

/**
 * Starting from a node, find the namespace declaration for a prefix. for a matching namespace
 * declaration./*  w ww .ja v a  2s . c om*/
 * 
 * @param node search up from here to search for namespace definitions
 * @param searchPrefix the prefix we are searching for
 * @return the namespace if found.
 */
public static String getNamespace(Node node, String searchPrefix) {

    Element el;
    while (!(node instanceof Element)) {
        node = node.getParentNode();
    }
    el = (Element) node;

    NamedNodeMap atts = el.getAttributes();
    for (int i = 0; i < atts.getLength(); i++) {
        Node currentAttribute = atts.item(i);
        String currentLocalName = currentAttribute.getLocalName();
        String currentPrefix = currentAttribute.getPrefix();
        if (searchPrefix.equals(currentLocalName) && XMLNAMESPACE.equals(currentPrefix)) {
            return currentAttribute.getNodeValue();
        } else if (isEmpty(searchPrefix) && XMLNAMESPACE.equals(currentLocalName) && isEmpty(currentPrefix)) {
            return currentAttribute.getNodeValue();
        }
    }

    Node parent = el.getParentNode();
    if (parent instanceof Element) {
        return getNamespace((Element) parent, searchPrefix);
    }

    return null;
}

From source file:Main.java

/**
 * @return A String[] of length two, [prefix, URI].
 *///from  w ww .j a  v  a2 s  .  c  o  m
public static String[] getNamespaceDeclaration(Element ele, String prefixHint) {
    String[] ns = new String[2]; // prefix, URI
    NamedNodeMap attribs = ele.getAttributes();

    for (int i = 0; i < attribs.getLength(); i++) {
        Attr attr = (Attr) attribs.item(i);
        if (attr.getName().startsWith("xmlns")) {
            if ((prefixHint != null && attr.getName().endsWith(prefixHint)) || attr.getName().equals("xmlns")) {
                ns[0] = attr.getLocalName(); // prefix
                ns[1] = attr.getTextContent(); // URI

                // catch default namespace change
                if (ns[0] == "xmlns") {
                    ns[0] = UUID.randomUUID().toString();
                }
            }
        }
    }

    if (ns[1] == null) {
        return getNamespaceDeclaration((Element) ele.getParentNode(), prefixHint);
    } else {
        return ns;
    }
}

From source file:cross.applicationContext.ReflectionApplicationContextGenerator.java

/**
 * Creates a stub application context xml file for the given classes. The
 * default bean scope is 'prototype'.//w w  w  .j  ava 2s  .  co  m
 *
 * @param outputFile the output file for the generated xml file
 * @param springVersion the spring version to use for validation
 * @param defaultScope the default scope
 * @param classes the classes to generate stubs for
 */
public static void createContextXml(File outputFile, String springVersion, String defaultScope,
        String... classes) {
    //generate the application context XML
    ReflectionApplicationContextGenerator generator = new ReflectionApplicationContextGenerator(springVersion,
            defaultScope);
    for (String className : classes) {
        try {
            Collection<?> serviceImplementations = Lookup.getDefault().lookupAll(Class.forName(className));
            if (serviceImplementations.isEmpty()) {
                //standard bean, add it
                try {
                    generator.addBean(className);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(ReflectionApplicationContextGenerator.class.getName()).log(Level.SEVERE,
                            null, ex);
                }
            } else {//service provider implementation
                for (Object obj : serviceImplementations) {
                    try {
                        generator.addBean(obj.getClass().getCanonicalName());
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(ReflectionApplicationContextGenerator.class.getName())
                                .log(Level.SEVERE, null, ex);
                    }
                }
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ReflectionApplicationContextGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    Document document = generator.getDocument();
    Element element = document.getDocumentElement();
    Comment comment = document.createComment(
            "In order to use this template in a runnable pipeline, please include it from another application context xml file.");
    element.getParentNode().insertBefore(comment, element);
    writeToFile(document, outputFile);
}