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:de.codesourcery.spring.contextrewrite.XMLRewrite.java

private static Rule wrap(ReplaceRule r) {
    final String newValue;

    if (!ContextRewritingBootStrapper.NULL_STRING.equals(r.replacement())) {
        if (r.replacementClassName() != ReplaceRule.NULL_CLASS) {
            throw new RuntimeException("Either replacement or replacementClassName needs to be set");
        }//ww  w  . j  ava  2s  .c o m
        newValue = r.replacement();
    } else if (r.replacementClassName() != ReplaceRule.NULL_CLASS) {
        newValue = r.replacementClassName().getName();
    } else {
        throw new RuntimeException(
                "You need to provide EITHER 'replacement' OR 'replacementClassName' attributes");
    }
    return new Rule(r.xpath(), r.id()) {
        public void apply(Document document, Node matchedNode) throws Exception {
            switch (matchedNode.getNodeType()) {
            case Node.ATTRIBUTE_NODE:
                matchedNode.setNodeValue(newValue);
                break;
            case Node.ELEMENT_NODE:
                final Document replDocument = parseXMLFragment(newValue);
                final Node firstChild = replDocument.getFirstChild();
                final Node adoptedNode = document.importNode(firstChild, true);
                matchedNode.getParentNode().replaceChild(adoptedNode, matchedNode);
                break;
            default:
                throw new RuntimeException();
            }
        }

        @Override
        public String toString() {
            return "REPLACE: " + r.xpath() + " with '" + newValue;
        }
    };
}

From source file:Main.java

/**
 * Sarches for ressources that have to be downloaded and creates a list with all download links.
 *
 * @param node to check for download links
 * @return list with all found download links
 *//*from  w  ww . ja  v a 2s.  c o m*/
private static ArrayList<String> findUrls(Node node) {

    int type = node.getNodeType();
    ArrayList<String> result = new ArrayList<>();
    String temp = null;
    NamedNodeMap atts = node.getAttributes();
    Log.i(TAG, "parsing for ressources.  node: " + node.getNodeName() + " value: " + node.getNodeValue()
            + " atts length: " + atts.getLength() + "type: " + type);
    switch (type) {
    //Element
    case Node.ELEMENT_NODE:
        //TODO: This method is stupid. It just looks for
        // attributes named ressourcepath. What if we have to download several ressources?

        for (int j = 0; j < atts.getLength(); j++) {
            Log.i(TAG, "atts: " + atts.item(j).getNodeName() + " value: " + atts.item(j).getNodeValue());
            temp = atts.item(j).getNodeValue();

            if (temp != null) {
                result.add(temp);
                Log.i(TAG, "added path: " + temp);
            }

            Log.i(TAG, "parent node:" + node.getParentNode().getNodeName());
            Element parent = (Element) node.getParentNode();
            parent.setAttribute("TESTITEST", "dllink");

        }
        // get the pages, means the children
        for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
            ArrayList<String> childres = findUrls(child);
            if (childres.size() > 0) {
                result.addAll(childres);
            }
        }
        break;
    }
    return result;
}

From source file:DomUtils.java

public static int getDepth(Element element) {
    Node parent = element.getParentNode();
    int depth = 0;

    while (parent != null && parent.getNodeType() == Node.ELEMENT_NODE) {
        depth++;// ww  w. jav  a2 s .c  om
        parent = parent.getParentNode();
    }

    return depth;
}

From source file:com.cognifide.aet.job.common.datafilters.removenodes.RemoveNodesDataModifier.java

private void removeNodes(Document document) throws ProcessingException {
    try {//ww w. java 2 s . c  o  m
        NodeList nl = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            node.getParentNode().removeChild(node);
        }
    } catch (XPathExpressionException e) {
        throw new ProcessingException(e.getMessage(), e);
    }
}

From source file:ItemSearcher.java

public short acceptNode(Node n) {
    if (n.getNodeType() == Node.TEXT_NODE) {
        Node parent = n.getParentNode();
        if ((parent.getNodeName().equalsIgnoreCase("b")) || (parent.getNodeName().equalsIgnoreCase("i"))) {
            return FILTER_ACCEPT;
        }//from  w w  w  .  java2  s . c  om
    }
    // If we got here, not interested
    return FILTER_SKIP;
}

From source file:com.concursive.connect.web.modules.wiki.utils.HTMLToWikiUtils.java

private static boolean hasParentNodeType(Node n, String tagToMatch) {
    Node parent = n.getParentNode();
    while (parent != null) {
        if (parent.getNodeType() == Node.ELEMENT_NODE) {
            Element element = ((Element) parent);
            String tag = element.getTagName();
            if (tagToMatch.equals(tag)) {
                return true;
            }// w ww. j a v  a  2  s.c o  m
        }
        parent = parent.getParentNode();
    }
    return false;
}

From source file:com.apricot.eating.xml.XMLParser.java

public void removeNode(Node node) {
    node.getParentNode().removeChild(node);
}

From source file:net.sf.jasperreports.engine.util.xml.JaxenNsAwareXPathExecuter.java

private Map<String, String> extractXmlNamespaces(Node contextNode) throws JRException {
    Map<String, String> namespaces = new HashMap<String, String>();
    List<Node> nlist;
    String namespaceXPathString = "//namespace::node()";

    try {/*  w  w  w. j av  a  2s  .c o m*/
        XPath xpath = new DOMXPath(namespaceXPathString);
        nlist = xpath.selectNodes(contextNode);

        for (int i = 0; i < nlist.size(); i++) {
            Node node = nlist.get(i);
            if (node.getParentNode() != null && node.getParentNode().getPrefix() != null) {
                if (!namespaces.containsKey(node.getParentNode().getPrefix())) {
                    namespaces.put(node.getParentNode().getPrefix(), node.getParentNode().getNamespaceURI());
                }
            }
        }

    } catch (JaxenException e) {
        throw new JRException(EXCEPTION_MESSAGE_KEY_XPATH_SELECTION_FAILURE,
                new Object[] { namespaceXPathString }, e);
    }

    return namespaces;
}

From source file:Main.java

public static void spreadNamespaces(Node node, String tns, boolean overwrite) {
    Document doc = node instanceof Document ? (Document) node : node.getOwnerDocument();
    boolean isParent = false;
    while (node != null) {
        Node next = null;//from ww  w  .ja  v a2 s  .  c o m
        if (!isParent && node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNamespaceURI() == null) {
                node = doc.renameNode(node, tns, node.getNodeName());
            } else {
                if (overwrite) {
                    tns = node.getNamespaceURI();
                }
            }
            NamedNodeMap nodeMap = node.getAttributes();
            int nodeMapLengthl = nodeMap.getLength();
            for (int i = 0; i < nodeMapLengthl; i++) {
                Node attr = nodeMap.item(i);
                if (attr.getNamespaceURI() == null) {
                    doc.renameNode(attr, tns, attr.getNodeName());
                }
            }
        }
        isParent = (isParent || (next = node.getFirstChild()) == null)
                && (next = node.getNextSibling()) == null;
        node = isParent ? node.getParentNode() : next;
        if (isParent && node != null) {
            if (overwrite) {
                tns = node.getNamespaceURI();
            }
        }
    }
}

From source file:com.amalto.core.history.accessor.UnaryFieldAccessor.java

@Override
public void delete() {
    while (exist()) {
        Element element = getElement();
        element.getParentNode().removeChild(element);
    }//from   w  w w .  ja v  a 2 s  .  c om

    // if the parent is exist and have no the child, will remove.
    if (parent.exist() && isEmptyChildForXSIType()) {
        Node parentNode = parent.getNode();
        parentNode.getParentNode().removeChild(parentNode);
    }
}