Example usage for org.w3c.dom Node getParentNode

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

Introduction

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

Prototype

public Node getParentNode();

Source Link

Document

The parent of this node.

Usage

From source file:tut.pori.javadocer.Javadocer.java

/**
 * //from w w  w . j  a  v  a  2 s.  c om
 * @param doc
 * @return node located under ELEMENT_EXAMPLE, or null if not available
 * @throws IOException 
 * @throws SAXException 
 * @throws IllegalStateException 
 */
private Node getExampleContent(org.w3c.dom.Document doc)
        throws IllegalStateException, SAXException, IOException {
    NodeList nodes = doc.getElementsByTagName(ELEMENT_EXAMPLE);
    if (nodes.getLength() < 1) {
        LOGGER.debug("No element " + ELEMENT_EXAMPLE);
        return null;
    }

    NodeList childNodes = null;
    Node root = doc.getDocumentElement();
    for (int i = 0, count = nodes.getLength(); i < count; ++i) {
        Node node = nodes.item(i);
        if (node.getParentNode().equals(root)) { // do not go deeper than one level below root
            childNodes = node.getChildNodes();
            break;
        }
    }
    if (childNodes == null || childNodes.getLength() < 1) {
        LOGGER.debug("No valid element " + ELEMENT_EXAMPLE);
        return null;
    }

    Node node = null;
    for (int i = 0, count = childNodes.getLength(); i < count; ++i) {
        Node child = childNodes.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            node = child;
            break;
        }
    }
    return node;
}

From source file:com.enonic.esl.xml.XMLTool.java

public static Node moveNode(Node node, Node fromParent, Node toParent) {
    node.getParentNode().removeChild(node);
    return toParent.appendChild(node);
}

From source file:com.enonic.esl.xml.XMLTool.java

private static Node moveNode(Node node, Node toParent) {
    Node fromParent = node.getParentNode();
    fromParent.removeChild(node);//from w  w  w  .  j a v  a 2  s .  c o  m
    return toParent.appendChild(node);
}

From source file:bridge.toolkit.commands.S1000DConverter.java

/**
 * Receive the node, the DOM object and the new name of the node
 * /*  w  w w. j av  a2 s .  co m*/
 * @param nodo
 * @param doc
 * @param newname
 */
public static Node changeNodename(Node nodo, org.w3c.dom.Document doc, String newname) {
    // Create an element with the new name
    Node element2 = doc.createElement(newname);

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

    // Move all the children
    while (nodo.hasChildNodes()) {
        element2.appendChild(nodo.getFirstChild());
    }

    // Replace the old node with the new node
    nodo.getParentNode().replaceChild(element2, nodo);
    return element2;

}

From source file:de.betterform.xml.dom.DOMUtil.java

/**
 * __UNDOCUMENTED__/*from   w ww.j a  va 2s.  c o m*/
 *
 * @param newChild __UNDOCUMENTED__
 * @param refChild __UNDOCUMENTED__
 * @throws DOMException __UNDOCUMENTED__
 */
public static void insertAfter(Node newChild, Node refChild) throws DOMException {
    if (refChild == null) {
        throw new DOMException(DOMException.NOT_FOUND_ERR, "refChild == null");
    }

    Node nextSibling = refChild.getNextSibling();

    if (nextSibling == null) {
        refChild.getParentNode().appendChild(newChild);
    } else {
        refChild.getParentNode().insertBefore(newChild, nextSibling);
    }
}

From source file:model.connection.amazon.AmazonItemLookupService.java

private String getReferenceByName(String name) {
    String reference = null;/* w w w.  ja  v a2  s.  c  o m*/
    String url = this.getSearchByName(name);
    if (url != null) {
        Document doc = null;
        try {
            doc = super.fetchDocFromUrl(url);
        } catch (FileNotFoundException | TooFastConnectionException e) {
            logger.error("Issue connecting to Amazon by searching by name", e);
        }
        if (doc != null) {
            NodeList nodes = super.getNodes(doc, "/ItemSearchResponse/Items/Item/ItemAttributes/Title");
            if (nodes != null) {
                List<String> productNames = new ArrayList<>();
                for (int i = 0; i < nodes.getLength(); i++) {
                    String title = "";
                    title = nodes.item(i).getTextContent();
                    productNames.add(title);
                }

                int selectedIndex = super.selectName(productNames, name);
                if (selectedIndex >= 0) {
                    Node selectedNode = nodes.item(selectedIndex);
                    if (selectedNode != null && selectedNode.getParentNode() != null
                            && selectedNode.getParentNode().getParentNode() != null) {
                        NodeList dataNodes = selectedNode.getParentNode().getParentNode().getChildNodes();
                        if (dataNodes != null) {
                            for (int i = 0; i < dataNodes.getLength(); i++) {
                                if ("ASIN".equals(dataNodes.item(i).getNodeName())) {
                                    reference = dataNodes.item(i).getTextContent();
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return reference;

}

From source file:hudson.plugins.plot.XMLSeries.java

/***
 * This is a fallback strategy for nodesets that include non numeric content
 * enabling users to create lists by selecting them such that names and
 * values share a common parent. If a node has attributes and is empty that
 * node will be re-enqueued as a parent to its attributes.
 * /*from ww w. ja  va 2  s .  c  om*/
 * @param buildNumber
 *            the build number
 * 
 * @returns a list of PlotPoints where the label is the last non numeric
 *          text content and the value is the last numeric text content for
 *          each set of nodes under a given parent.
 ***/
private List<PlotPoint> coalesceTextnodesAsLabelsStrategy(NodeList nodeList, int buildNumber) {
    Map<Node, List<Node>> parentNodeMap = new HashMap<Node, List<Node>>();

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (!parentNodeMap.containsKey(node.getParentNode())) {
            parentNodeMap.put(node.getParentNode(), new ArrayList<Node>());
        }
        parentNodeMap.get(node.getParentNode()).add(node);
    }

    List<PlotPoint> retval = new ArrayList<PlotPoint>();
    Queue<Node> parents = new ArrayDeque<Node>(parentNodeMap.keySet());
    while (!parents.isEmpty()) {
        Node parent = parents.poll();
        Double value = null;
        String label = null;

        for (Node child : parentNodeMap.get(parent)) {
            if (null == child.getTextContent() || child.getTextContent().trim().isEmpty()) {
                NamedNodeMap attrmap = child.getAttributes();
                List<Node> attrs = new ArrayList<Node>();
                for (int i = 0; i < attrmap.getLength(); i++) {
                    attrs.add(attrmap.item(i));
                }
                parentNodeMap.put(child, attrs);
                parents.add(child);
            } else if (new Scanner(child.getTextContent().trim()).hasNextDouble()) {
                value = new Scanner(child.getTextContent().trim()).nextDouble();
            } else {
                label = child.getTextContent().trim();
            }
        }
        if ((label != null) && (value != null)) {
            addValueToList(retval, new String(label), String.valueOf(value), buildNumber);
        }
    }
    return retval;
}

From source file:DomUtils.java

/**
 * Get the parent element of the supplied element having the
 * specified tag name./*from w w w. ja  v  a  2s  .  c  o  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.amalto.core.history.accessor.UnaryFieldAccessor.java

private Element internalCreate() {
    parent.create();//from   ww w .  j a  va 2s .  c o m
    Document domDocument = document.asDOM();
    Element element = getElement();
    if (element == null) {
        Element newElement = domDocument.createElementNS(domDocument.getNamespaceURI(), fieldName);
        Node parentNode = parent.getNode();
        Node lastAccessedNode = document.getLastAccessedNode();
        if (parentNode == lastAccessedNode) {
            parentNode.insertBefore(newElement, parentNode.getFirstChild());
        } else if (lastAccessedNode != null && lastAccessedNode.getParentNode() == parentNode) {
            parentNode.insertBefore(newElement, lastAccessedNode.getNextSibling());
        } else {
            parentNode.appendChild(newElement);
        }
        element = newElement;
        document.setLastAccessedNode(element);
    }
    return element;
}

From source file:com.wavemaker.tools.project.LocalDeploymentManager.java

private void updateTomcatDeployConfig(Map<String, Object> properties) {
    LocalFolder projectDir = (LocalFolder) properties.get(PROJECT_DIR_PROPERTY);
    String deployName = (String) properties.get(DEPLOY_NAME_PROPERTY);
    LocalFile tomcatConfigXml = (LocalFile) projectDir.getFile(deployName + ".xml");
    if (tomcatConfigXml.exists()) {
        tomcatConfigXml.delete();//from  ww  w.  j av  a2s  . com
    }
    tomcatConfigXml.createIfMissing();
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = factory.newDocumentBuilder();
        Document doc = docBuilder.newDocument();

        NodeList nodeList = doc.getElementsByTagName("Context");

        if (nodeList != null && nodeList.getLength() > 0) {
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                if (node.getParentNode() != null) {
                    node.getParentNode().removeChild(node);
                }
            }
        }

        Element context = doc.createElement("Context");
        context.setAttribute("antiJARLocking", "true'");
        context.setAttribute("antiResourceLocking", "false");
        context.setAttribute("privileged", "true");
        String docBase = ((LocalFolder) properties.get(BUILD_WEBAPPROOT_PROPERTY)).getLocalFile()
                .getAbsolutePath();
        context.setAttribute("docBase", docBase);

        doc.appendChild(context);

        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer tFormer = tFactory.newTransformer();

        Source source = new DOMSource(doc);
        Result dest = new StreamResult(tomcatConfigXml.getLocalFile());
        tFormer.transform(source, dest);

    } catch (Exception e) {
        throw new WMRuntimeException(e);
    }
}