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:be.fedict.eid.tsl.TrustServiceList.java

public void save() throws IOException {
    if (null == this.tslFile) {
        throw new IllegalStateException("no TSL file set");
    }/*  w w w  .  j av a2 s  .  c  o  m*/
    LOG.debug("save to: " + this.tslFile.getAbsolutePath());
    if (null == this.tslDocument) {
        try {
            marshall();
        } catch (Exception e) {
            throw new IOException("marshall error: " + e.getMessage(), e);
        }
        /*
         * Only remove existing XML signature from new (or changed) DOM
         * documents.
         */
        Node signatureNode = getSignatureNode();
        if (null != signatureNode) {
            signatureNode.getParentNode().removeChild(signatureNode);
        }
    }
    try {
        toFile(this.tslFile);
    } catch (Exception e) {
        throw new IOException("DOM transformation error: " + e.getMessage(), e);
    }
    clearChanged();
}

From source file:com.gargoylesoftware.htmlunit.html.DomNode.java

/**
 * {@inheritDoc}//from w  w  w .j  a v  a2  s. c o m
 */
public Node removeChild(final Node child) {
    if (child.getParentNode() != this) {
        throw new DOMException(DOMException.NOT_FOUND_ERR, "Node is not a child of this node.");
    }
    ((DomNode) child).remove();
    return child;
}

From source file:com.gargoylesoftware.htmlunit.html.DomNode.java

/**
 * {@inheritDoc}//w w  w . ja  v  a2 s. c  o  m
 */
public Node replaceChild(final Node newChild, final Node oldChild) {
    if (oldChild.getParentNode() != this) {
        throw new DOMException(DOMException.NOT_FOUND_ERR, "Node is not a child of this node.");
    }
    ((DomNode) oldChild).replace((DomNode) newChild);
    return oldChild;
}

From source file:DOMProcessor.java

/** Renames the given element with the given new name.
  * @param existingElement Element to rename.
  * @param newName New name to give element.
  *///from  w  ww  .j  a va2  s .co m
public void renameElement(Node existingElement, String newName) {
    // Create an element with the new name
    Node newElement = dom.createElement(newName);

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

    // Move all the children
    while (existingElement.hasChildNodes()) {
        newElement.appendChild(existingElement.getFirstChild());
    }

    // Replace the old node with the new node
    existingElement.getParentNode().replaceChild(newElement, existingElement);
}

From source file:Main.java

public static void copyInto(Node src, Node dest) throws DOMException {

    Document factory = dest.getOwnerDocument();

    //Node start = src;
    Node parent = null;
    Node place = src;//ww  w.  j a v a 2 s .co m

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr) attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException(
                    "can't copy node type, " + type + " (" + place.getNodeName() + ')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place = place.getFirstChild();
            dest = node;
        } else if (parent == null) {
            place = null;
        } else {
            // advance
            place = place.getNextSibling();
            while (place == null && parent != null) {
                place = parent.getNextSibling();
                parent = parent.getParentNode();
                dest = dest.getParentNode();
            }
        }

    }

}

From source file:com.globalsight.everest.tda.TdaHelper.java

private void domNodeVisitor(Node p_node, ArrayList matchList, long tmProfileThreshold) {
    HashMap TDAResults = new HashMap();
    LeverageTDAResult tdaResult = null;//from   www .  j  a v  a 2s  .  c o m

    if (matchList.size() > 0) {
        tdaResult = (LeverageTDAResult) matchList.get(matchList.size() - 1);
    }

    while (true) {
        if (p_node == null) {
            return;
        }

        switch (p_node.getNodeType()) {
        case Node.DOCUMENT_NODE:
            domNodeVisitor(p_node.getFirstChild(), matchList, tmProfileThreshold);
            return;
        case Node.ELEMENT_NODE:
            String nodeName = p_node.getNodeName().toLowerCase();

            if (nodeName.equals("alt-trans")) {
                String tuid = "-1";

                NamedNodeMap parentAttrs = p_node.getParentNode().getAttributes();

                for (int i = 0; i < parentAttrs.getLength(); ++i) {
                    Node att = parentAttrs.item(i);
                    String attname = att.getNodeName();
                    String value = att.getNodeValue();

                    if (attname.equals("id")) {
                        tuid = value;
                    }
                }

                NamedNodeMap attrs = p_node.getAttributes();
                boolean fromTDA = false;
                String percentValue = "";

                for (int i = 0; i < attrs.getLength(); ++i) {
                    Node att = attrs.item(i);
                    String attname = att.getNodeName();
                    String value = att.getNodeValue();

                    if (attname.equals("tda:provider")) {
                        fromTDA = true;
                    }

                    if (attname.equals("match-quality")) {
                        percentValue = value;
                    }

                }

                if (fromTDA) {
                    if (PecentToInt(percentValue) > tmProfileThreshold
                            || PecentToInt(percentValue) == tmProfileThreshold) {
                        tdaResult = new LeverageTDAResult();
                        tdaResult.setTuid(Long.parseLong(tuid));
                        tdaResult.setMatchPercent(percentValue);
                        matchList.add(tdaResult);
                    } else {
                        p_node = p_node.getNextSibling();
                        break;
                    }
                }
            }

            domNodeVisitor(p_node.getFirstChild(), matchList, tmProfileThreshold);
            p_node = p_node.getNextSibling();
            break;
        case Node.TEXT_NODE:
            nodeName = p_node.getNodeName().toLowerCase();

            if (p_node.getParentNode() != null && p_node.getParentNode().getParentNode() != null) {
                String parentNodeName = p_node.getParentNode().getNodeName().toLowerCase();
                String grandNodeName = p_node.getParentNode().getParentNode().getNodeName().toLowerCase();

                if (grandNodeName.equals("alt-trans") && parentNodeName.equals("target")) {
                    if (tdaResult != null) {
                        tdaResult.setResultText(p_node.getNodeValue());
                    }
                } else if (grandNodeName.equals("alt-trans") && parentNodeName.equals("source")) {
                    if (tdaResult != null) {
                        tdaResult.setSourceText(p_node.getNodeValue());
                    }
                }
            }

            p_node = p_node.getNextSibling();
            break;
        }
    }
}

From source file:jef.tools.XMLUtils.java

/**
 * ?? /*  ww w  . j av a2s. co m*/
 * 
 * <pre>
 *    &lt;parent&gt;
 *       &lt;a&gt;text-a&lt;/a&gt;
 *       &lt;c&gt;text-c&lt;/c&gt;
 *    &lt;/parent&gt;
 * </pre>
 * 
 * {@code addElementBefore(c,"b","text-b")}c?c 
 * 
 * <pre>
 *     &lt;parent&gt;
 *       &lt;a&gt;text-a&lt;/a&gt;
 *       &lt;b&gt;text-b&lt;/b&gt;
 *       &lt;c&gt;text-c&lt;/c&gt;
 *    &lt;/parent&gt;
 * </pre>
 * 
 * @param node
 *            DOM
 * @param tagName
 *            ??
 * @param nodeText
 *            
 * @return Element
 */
public static Element addElementBefore(Node node, String tagName, String... nodeText) {
    Node pNode = node.getParentNode();
    List<Node> movingNodes = new ArrayList<Node>();
    for (Node n : toArray(pNode.getChildNodes())) {
        if (n == node) {
            movingNodes.add(n);
        } else if (movingNodes.size() > 0) {
            movingNodes.add(n);
        }
    }
    Element e = addElement(pNode, tagName, nodeText);
    for (Node n : movingNodes) {
        pNode.appendChild(n);
    }
    return e;
}

From source file:jef.tools.XMLUtils.java

/**
 * ??/* www.  j  a  va  2  s.  c om*/
 * 
 * @param node
 *            DOM
 * @param tagName
 *            ??
 * @param nodeText
 *            
 * @return Element
 */
public static Element addElementAfter(Node node, String tagName, String... nodeText) {
    Node pNode = node.getParentNode();
    List<Node> movingNodes = new ArrayList<Node>();
    boolean flag = false;
    for (Node n : toArray(pNode.getChildNodes())) {
        if (flag) {
            movingNodes.add(n);
        } else if (n == node) {
            flag = true;
        }
    }
    Element e = addElement(pNode, tagName, nodeText);
    for (Node n : movingNodes) {
        pNode.appendChild(n);
    }
    return e;
}

From source file:jef.tools.XMLUtils.java

/**
 * ???/* w  w w .j a  va 2  s . c o m*/
 * 
 * @param node
 *            
 * @param tagName
 *            ??
 * @param nodeText
 *            
 * @return Element
 */
public static Element replaceElement(Node node, String tagName, String... nodeText) {
    Node pNode = node.getParentNode();
    Assert.notNull(pNode);
    Document doc = null;
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        doc = (Document) node;
    } else {
        doc = node.getOwnerDocument();
    }
    Element e = doc.createElement(tagName);
    if (nodeText.length == 1) {
        setText(e, nodeText[0]);
    } else if (nodeText.length > 1) {
        setText(e, StringUtils.join(nodeText, '\n'));
    }
    pNode.replaceChild(e, node);
    return e;
}

From source file:com.gargoylesoftware.htmlunit.html.DomNode.java

/**
 * Gets the ancestors of the node./*w  w  w .  j  ava2 s .  co  m*/
 * @param includeSelf should this node be returned too
 * @return a list of the ancestors with the root at the first position
 */
protected List<Node> getAncestors(final boolean includeSelf) {
    final List<Node> list = new ArrayList<Node>();
    if (includeSelf) {
        list.add(this);
    }
    Node node = getParentNode();
    while (node != null) {
        list.add(0, node);
        node = node.getParentNode();
    }
    return list;
}