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:org.sakaiproject.tool.assessment.qti.helper.AuthoringXml.java

/**
 * Based on method in XmlStringBuffer/*from  w  w  w.j  a  va 2 s .c om*/
 * @author rpembry
 * @author casong changed XmlStringBuffer to be org.w3c.dom compliance,
 * @author Ed Smiley esmiley@stanford.edu changed method signatures used Document
 * @param document Document
 * @param xpath
 * @param element
 * @return modified Document
 */
public Document update(Document document, String xpath, Element element) {
    if (log.isDebugEnabled()) {
        log.debug("update(String " + xpath + ", Element " + element + ")");
    }

    List itemResults = this.selectNodes(document, xpath);
    Iterator iterator = itemResults.iterator();
    while (iterator.hasNext()) {
        Element node = (Element) iterator.next();
        Element replacement = (Element) node.getOwnerDocument().importNode(element, true);
        node.getParentNode().replaceChild(replacement, node);
    }

    if (itemResults.size() == 0) {
        String parentPath = xpath.substring(0, xpath.lastIndexOf("/"));
        addElement(document, parentPath, element);
    }

    return document;
}

From source file:org.sakaiproject.tool.assessment.qti.util.XmlStringBuffer.java

/**
 * update element, xpath//  w w  w .j a  v  a2  s  .  c  om
 *
 * @param xpath
 * @param element
 */
public final void update(String xpath, Element element) {
    if (log.isDebugEnabled()) {
        log.debug("update(String " + xpath + ", Element " + element + ")");
    }

    List itemResults = this.selectNodes(xpath);
    Iterator iterator = itemResults.iterator();
    while (iterator.hasNext()) {
        Element node = (Element) iterator.next();
        Element replacement = (Element) node.getOwnerDocument().importNode(element, true);
        node.getParentNode().replaceChild(replacement, node);
    }

    if (itemResults.size() == 0) {
        String parentPath = xpath.substring(0, xpath.lastIndexOf("/"));
        addElement(parentPath, element);
    }
}

From source file:org.smartfrog.avalanche.client.sf.apps.gt4.javawscore.utils.EditXML.java

public void removeNode(Element ele) {
    Node parent = ele.getParentNode();
    parent.removeChild(ele);
}

From source file:org.sonar.plugins.csharp.stylecop.profiles.utils.StyleCopRuleParser.java

/**
 * @param reader/*w  w  w  .j  av  a 2 s  . c o m*/
 * @return
 */
public static List<StyleCopRule> parse(Reader reader) {
    InputSource source = new InputSource(reader);
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    List<StyleCopRule> result = new ArrayList<StyleCopRule>();

    try {
        XPathExpression expression = xpath.compile("//Rule");
        NodeList nodes = (NodeList) expression.evaluate(source, XPathConstants.NODESET);
        int count = nodes.getLength();
        for (int idxRule = 0; idxRule < count; idxRule++) {
            Element ruleElement = (Element) nodes.item(idxRule);
            Element analyzerElement = (Element) ruleElement.getParentNode().getParentNode();
            String ruleName = ruleElement.getAttribute("Name");
            String priority = ruleElement.getAttribute("SonarPriority");

            StyleCopRule rule = new StyleCopRule();
            NodeList elements = ruleElement.getElementsByTagName("BooleanProperty");
            boolean active = true;
            int countBoolean = elements.getLength();
            for (int idxElement = 0; idxElement < countBoolean; idxElement++) {
                Element booleanElement = (Element) elements.item(idxElement);
                String booleanName = booleanElement.getAttribute("Name");
                if ("Enabled".equals(booleanName)) {
                    String activeStr = booleanElement.getTextContent();
                    active = !activeStr.toLowerCase().contains("false");
                }
            }
            String analyzerId = analyzerElement.getAttribute("AnalyzerId");
            String category = StringUtils.removeEnd(StringUtils.substringAfterLast(analyzerId, "."), "Rules");
            rule.setAnalyzerId(analyzerId);
            rule.setName(ruleName);
            rule.setPriority(priority);
            rule.setEnabled(active);
            rule.setCategory(category);
            result.add(rule);
        }
    } catch (XPathExpressionException e) {
        LOG.debug("Xpath error un stylecop report", e);
    }

    return result;
}

From source file:org.sonatype.nexus.yum.internal.MetadataProcessor.java

/**
 * Remove references to sqlite from repomd.xml
 *
 * @param repository containing repomd.xml
 * @return true if repomd.xml was changed
 *///from   w ww  .j a  v  a  2 s  .co m
private static boolean removeSqliteFromRepoMD(final Repository repository, final Document repoMDDoc)
        throws Exception {
    boolean changed = false;
    List<Element> elementsToRemove = Lists.newArrayList();
    NodeList dataNodes = repoMDDoc.getElementsByTagName("data");
    for (int i = 0; i < dataNodes.getLength(); i++) {
        Element data = (Element) dataNodes.item(i);
        if (data.getAttribute("type").endsWith("_db")) {
            elementsToRemove.add(data);
            changed = true;
        }
    }
    if (changed) {
        log.debug("Removing sqllite from {}:repomd.xml", repository.getId());
        for (Element element : elementsToRemove) {
            element.getParentNode().removeChild(element);
        }
    }

    return changed;
}

From source file:org.springframework.data.gemfire.config.AbstractRegionParser.java

protected boolean isSubRegion(final Element element) {
    String localName = element.getParentNode().getLocalName();
    return (localName != null && localName.endsWith("region"));
}

From source file:org.springframework.data.gemfire.config.xml.AbstractRegionParser.java

protected boolean isSubRegion(Element element) {
    String localName = element.getParentNode().getLocalName();
    return (localName != null && localName.endsWith("region"));
}

From source file:org.unitedinternet.cosmo.dav.property.StandardDavProperty.java

/**
 * <p>/*from w ww. java  2s .  c  o m*/
 * Returns an instance of <code>StandardDavProperty</code> representing
 * the given element. The element itself is provided as the property value.
 * If either the element or its parent element has the attribute
 * <code>xml:lang</code>, that attribute's value is provided as the
 * property's language. The resulting property is not "protected" (i.e.
 * it will not appear in "allprop" <code>PROPFIND</code> responses).
 * </p>
 */
public static StandardDavProperty createFromXml(Element e) {
    DavPropertyName name = DavPropertyName.createFromXml(e);
    String lang = DomUtil.getAttribute(e, XML_LANG, NAMESPACE_XML);
    if (lang == null && e.getParentNode() != null && e.getParentNode().getNodeType() == Node.ELEMENT_NODE) {
        lang = DomUtil.getAttribute((Element) e.getParentNode(), XML_LANG, NAMESPACE_XML);
    }
    return new StandardDavProperty(name, e, lang);
}

From source file:org.wso2.carbon.pc.core.ProcessStore.java

public String deleteSubprocess(String deleteSubprocess) {
    try {/*from   w w  w.  jav a 2 s. co m*/
        RegistryService registryService = ProcessCenterServerHolder.getInstance().getRegistryService();

        if (registryService != null) {
            UserRegistry reg = registryService.getGovernanceSystemRegistry();

            JSONObject processInfo = new JSONObject(deleteSubprocess);
            String processName = processInfo.getString("processName");
            String processVersion = processInfo.getString("processVersion");
            JSONObject subprocess = processInfo.getJSONObject("deleteSubprocess");

            String processAssetPath = "processes/" + processName + "/" + processVersion;
            Resource resource = reg.get(processAssetPath);
            String processContent = new String((byte[]) resource.getContent());
            Document doc = stringToXML(processContent);

            if (subprocess != null) {
                NodeList subprocessElements = ((Element) doc.getFirstChild())
                        .getElementsByTagName("subprocess");
                for (int i = 0; i < subprocessElements.getLength(); i++) {
                    Element subprocessElement = (Element) subprocessElements.item(i);
                    String subprocessName = subprocessElement.getElementsByTagName("name").item(0)
                            .getTextContent();
                    String subprocessPath = subprocessElement.getElementsByTagName("path").item(0)
                            .getTextContent();
                    String subprocessId = subprocessElement.getElementsByTagName("id").item(0).getTextContent();

                    if (subprocessName.equals(subprocess.getString("name"))
                            && subprocessPath.equals(subprocess.getString("path"))
                            && subprocessId.equals(subprocess.getString("id"))) {
                        subprocessElement.getParentNode().removeChild(subprocessElement);
                        break;
                    }
                }
                String newProcessContent = xmlToString(doc);
                resource.setContent(newProcessContent);
                reg.put(processAssetPath, resource);
            }
        }
    } catch (Exception e) {
        log.error(e);
    }
    return "OK";
}

From source file:org.wso2.carbon.pc.core.ProcessStore.java

public String deleteSuccessor(String deleteSuccessor) {
    try {// w w w .  j a v a  2  s.  com
        RegistryService registryService = ProcessCenterServerHolder.getInstance().getRegistryService();

        if (registryService != null) {
            UserRegistry reg = registryService.getGovernanceSystemRegistry();

            JSONObject processInfo = new JSONObject(deleteSuccessor);
            String processName = processInfo.getString("processName");
            String processVersion = processInfo.getString("processVersion");
            JSONObject successor = processInfo.getJSONObject("deleteSuccessor");

            String processAssetPath = "processes/" + processName + "/" + processVersion;
            Resource resource = reg.get(processAssetPath);
            String processContent = new String((byte[]) resource.getContent());
            Document doc = stringToXML(processContent);

            if (successor != null) {
                NodeList successorElements = ((Element) doc.getFirstChild()).getElementsByTagName("successor");
                for (int i = 0; i < successorElements.getLength(); i++) {
                    Element successorElement = (Element) successorElements.item(i);
                    String successorName = successorElement.getElementsByTagName("name").item(0)
                            .getTextContent();
                    String successorPath = successorElement.getElementsByTagName("path").item(0)
                            .getTextContent();
                    String successorId = successorElement.getElementsByTagName("id").item(0).getTextContent();

                    if (successorName.equals(successor.getString("name"))
                            && successorPath.equals(successor.getString("path"))
                            && successorId.equals(successor.getString("id"))) {
                        successorElement.getParentNode().removeChild(successorElement);
                        break;
                    }
                }
                String newProcessContent = xmlToString(doc);
                resource.setContent(newProcessContent);
                reg.put(processAssetPath, resource);
            }
        }
    } catch (Exception e) {
        log.error(e);
    }
    return "OK";
}