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:eu.europa.ec.markt.dss.applet.view.validationpolicy.EditView.java

String getXPath(Node node) {
    Node parent = node.getParentNode();
    if (parent == null || parent instanceof Document) {
        return node.getNodeName();
    }//from   www.j  a v  a2  s.  c  o  m
    return getXPath(parent) + "/" + node.getNodeName();
}

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

/**
 * clean whitespace around tags//  ww w  .  j  a va2 s  .  com
 * 
 * @param doc
 * @throws IllegalArgumentException
 */
private void cleanWhiteSpace(org.w3c.dom.Document doc) throws IllegalArgumentException {
    try {
        NodeList nodeList = (NodeList) _xPath.evaluate("//text()[normalize-space()='']", doc,
                XPathConstants.NODESET);
        for (int i = 0; i < nodeList.getLength(); ++i) {
            Node node = nodeList.item(i);
            node.getParentNode().removeChild(node);
        }
    } catch (XPathExpressionException ex) {
        LOGGER.error(ex, ex);
        throw new IllegalArgumentException("Xpath evaluation failed.");
    }
}

From source file:eu.europa.esig.dss.applet.view.validationpolicy.EditView.java

String getXPath(Node node) {
    Node parent = node.getParentNode();
    if ((parent == null) || (parent instanceof Document)) {
        return node.getNodeName();
    }/* ww  w  . j a v  a 2  s. c  om*/
    return getXPath(parent) + "/" + node.getNodeName();
}

From source file:br.gov.lexml.parser.documentoarticulado.LexMLParserFromText.java

private boolean isAlteracao(Node item) {
    if (item == null)
        return false;
    if (item.getNodeName().equals("Alteracao"))
        return true;
    return isAlteracao(item.getParentNode());
}

From source file:com.evolveum.midpoint.util.DOMUtil.java

public static Node getNextSiblingElement(Node node) {
    if (node == null || node.getParentNode() == null) {
        return null;
    }//  www.j a v  a2  s . c o  m
    Node parent = node.getParentNode();
    NodeList nodes = parent.getChildNodes();
    if (nodes == null) {
        return null;
    }
    boolean found = false;
    for (int i = 0; i < nodes.getLength(); i++) {
        Node child = nodes.item(i);
        if (child.equals(node)) {
            found = true;
            continue;
        }
        if (found && child.getNodeType() == Node.ELEMENT_NODE) {
            return child;
        }
    }

    return null;
}

From source file:io.cloudslang.dependency.impl.services.DependencyServiceImpl.java

private void removeByXpathExpression(String pomFilePath, String expression) throws SAXException, IOException,
        ParserConfigurationException, XPathExpressionException, TransformerException {
    File xmlFile = new File(pomFilePath);
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlFile);
    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList nl = (NodeList) xpath.compile(expression).evaluate(doc, XPathConstants.NODESET);

    if (nl != null && nl.getLength() > 0) {
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            node.getParentNode().removeChild(node);
        }/* w w w  .j  av a  2s.  com*/

        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        // need to convert to file and then to path to override a problem with spaces
        Result output = new StreamResult(new File(pomFilePath).getPath());
        Source input = new DOMSource(doc);
        transformer.transform(input, output);
    }
}

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

public void delete() {
    if (exist()) {
        Node node = getCollectionItemNode();
        if (node == null) {
            return; // Node has already been deleted.
        }/*from  w w w.  j  a v  a 2s .c  o  m*/
        node.getParentNode().removeChild(node);
    }

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

From source file:org.alfresco.web.config.ConfigRuntime.java

/**
 * @param parent/*from   www . j  a v  a2  s. c o  m*/
 * @param child
 */
protected void appendChild(Node parent, Node child) {
    parent.appendChild(child);
    Node previousNode = child.getPreviousSibling();
    if (previousNode != null && previousNode instanceof org.w3c.dom.Text) {
        previousNode.getParentNode().removeChild(previousNode);
    }
}

From source file:org.alfresco.web.config.ConfigRuntime.java

/**
 * @param parent//from   w w w.ja v  a2  s.c om
 * @param child
 */
protected void removeChild(Node child) {
    Node previousNode = child.getPreviousSibling();
    if (previousNode != null && previousNode instanceof org.w3c.dom.Text) {
        previousNode.getParentNode().removeChild(previousNode);
    }
    child.getParentNode().removeChild(child);
}

From source file:io.fabric8.forge.ipaas.repository.NexusConnectionRepository.java

protected void indexNexus() throws Exception {
    // must have q parameter so use connector to find all connectors
    String query = nexusUrl + "?q=connector";
    URL url = new URL(query);

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//  w  w w.ja v a 2  s.c  om
    factory.setIgnoringElementContentWhitespace(true);
    factory.setIgnoringComments(true);

    DocumentBuilder documentBuilder = factory.newDocumentBuilder();

    InputStream is = url.openStream();
    Document dom = documentBuilder.parse(is);

    XPathFactory xpFactory = XPathFactory.newInstance();
    XPath exp = xpFactory.newXPath();
    NodeList list = (NodeList) exp.evaluate("//classifier[text() = '" + CLASSIFIER + "']", dom,
            XPathConstants.NODESET);

    Set<NexusArtifactDto> newArtifacts = new LinkedHashSet<>();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        Node parent = node.getParentNode();

        String g = getNodeText(parent.getChildNodes(), "groupId");
        String a = getNodeText(parent.getChildNodes(), "artifactId");
        String v = getNodeText(parent.getChildNodes(), "version");
        String l = getNodeText(parent.getChildNodes(), "artifactLink");

        if (g != null & a != null & v != null & l != null) {
            NexusArtifactDto dto = new NexusArtifactDto();
            dto.setGroupId(g);
            dto.setArtifactId(a);
            dto.setVersion(v);
            dto.setArtifactLink(l);

            System.out.println("Found connector: " + dto.getGroupId() + ":" + dto.getArtifactId() + ":"
                    + dto.getVersion());

            // is it a new artifact
            boolean newArtifact = true;
            for (NexusArtifactDto existing : indexedArtifacts) {
                if (existing.getGroupId().equals(dto.getGroupId())
                        && existing.getArtifactId().equals(dto.getArtifactId())
                        && existing.getVersion().equals(dto.getVersion())) {
                    newArtifact = false;
                    break;
                }
            }
            if (newArtifact) {
                newArtifacts.add(dto);
            }
        }
    }

    // now download the new artifact JARs and look inside to find more details
    for (NexusArtifactDto dto : newArtifacts) {
        try {
            // download using url classloader reader
            URL jarUrl = new URL(dto.getArtifactLink());
            String json = loadCamelConnectorJSonSchema(jarUrl);

            ObjectMapper mapper = new ObjectMapper();
            ConnectionCatalogDto cat = mapper.readerFor(ConnectionCatalogDto.class).readValue(json);

            indexedArtifacts.add(dto);
            connectors.putIfAbsent(dto, cat);
            System.out.println("Added connector: " + dto.getGroupId() + ":" + dto.getArtifactId() + ":"
                    + dto.getVersion());
        } catch (Exception e) {
            System.err.println("Error downloading connector JAR " + dto.getArtifactLink()
                    + ". This exception is ignored. " + e.getMessage());
        }
    }

    IOHelpers.close(is);
}