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.kuali.test.utils.Utils.java

/**
 *
 * @param curnode/* w w w .j av  a 2s. c  om*/
 * @param nodeName
 * @return
 */
public static Element findFirstParentNode(Element curnode, String nodeName) {
    Element retval = null;

    if (curnode != null) {
        Element parent = (Element) curnode.getParentNode();

        while (parent != null) {
            if (parent.getNodeName().equals(nodeName)) {
                retval = parent;
                break;
            }
            parent = (Element) parent.getParentNode();
        }
    }

    return retval;
}

From source file:org.kuali.test.utils.Utils.java

/**
 *
 * @param curnode//from   w  w w  .j av  a2  s.  c o  m
 * @param nodeName
 * @param attributeName
 * @param attributeValue
 * @return
 */
public static Element findFirstParentNode(Element curnode, String nodeName, String attributeName,
        String attributeValue) {
    Element retval = null;
    Element parent = (Element) curnode.getParentNode();

    while (parent != null) {
        if (parent.getNodeName().equals(nodeName)) {
            if (attributeValue.equalsIgnoreCase(parent.getAttribute(attributeName))) {
                retval = parent;
                break;
            }
        }
        parent = (Element) parent.getParentNode();
    }

    return retval;
}

From source file:org.kuali.test.utils.Utils.java

/**
 *
 * @param element//ww w. j  a va 2s.  c o  m
 * @return
 */
public static List<Element> getSiblingElements(Element element) {
    List<Element> retval = new ArrayList<Element>();

    if (isElement(element.getParentNode())) {
        retval = getChildElements((Element) element.getParentNode());

        if (retval != null) {
            Iterator<Element> it = retval.iterator();

            while (it.hasNext()) {
                if (it.next() == element) {
                    it.remove();
                    break;
                }
            }
        }
    }

    return retval;
}

From source file:org.limy.eclipse.qalab.outline.jdepend.JDependSvgParser.java

public String getQualifiedName(Element el) {
    String qualifiedName = null;//from w  w w. j ava  2s .  c om
    NodeList childNodes = el.getParentNode().getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        if ("title".equals(child.getNodeName())) {
            qualifiedName = child.getFirstChild().getNodeValue();
            break;
        }
    }
    return qualifiedName;
}

From source file:org.ms123.common.importing.XmlImporter.java

private String getElementPath(Element e) {
    StringBuilder sb = new StringBuilder();
    sb.append(e.getNodeName());/*from w  ww  . j a v a  2s  . c  o  m*/
    Node n = e.getParentNode();
    while (n != null) {
        String s = n.getNodeName();
        if ("#document".equals(s)) {
            break;
        }
        sb.insert(0, s + "/");
        n = n.getParentNode();
    }
    String ret = sb.toString();
    int slash = ret.indexOf("/");
    if (slash != -1) {
        return ret.substring(slash + 1);
    }
    return ret;
}

From source file:org.mule.config.spring.MuleHierarchicalBeanDefinitionParserDelegate.java

protected String generateChildBeanName(Element e) {
    String id = SpringXMLUtils.getNameOrId(e);
    if (StringUtils.isBlank(id)) {
        String parentId = SpringXMLUtils.getNameOrId((Element) e.getParentNode());
        return "." + parentId + ":" + e.getLocalName();
    } else {/*from  ww w. j a va  2s  .  co m*/
        return id;
    }
}

From source file:org.mule.config.spring.parsers.AbstractMuleBeanDefinitionParser.java

/**
 * @param element The element to test/*  w  w  w. j  ava2 s .c o  m*/
 * @return true if the element's parent is <mule> or similar
 */
protected boolean isTopLevel(Element element) {
    return element.getParentNode().getLocalName().equals(ROOT_ELEMENT);
}

From source file:org.mule.module.extension.internal.config.OperationBeanDefinitionParser.java

private String generateChildBeanName(Element element) {
    String id = SpringXMLUtils.getNameOrId(element);
    if (StringUtils.isBlank(id)) {
        String parentId = SpringXMLUtils.getNameOrId(((Element) element.getParentNode()));
        return String.format(".%s:%s", parentId, element.getLocalName());
    } else {//  ww w  . j  a v a 2  s.  c om
        return id;
    }
}

From source file:org.mule.security.oauth.config.AbstractDevkitBasedDefinitionParser.java

protected String generateChildBeanName(Element element) {
    String id = SpringXMLUtils.getNameOrId(element);
    if (StringUtils.isBlank(id)) {
        String parentId = SpringXMLUtils.getNameOrId(((Element) element.getParentNode()));
        return ((("." + parentId) + ":") + element.getLocalName());
    } else {/*from w  w  w .  ja  v  a  2 s . co  m*/
        return id;
    }
}

From source file:org.noerp.webtools.labelmanager.SaveLabelsToXmlFile.java

public static Map<String, Object> saveLabelsToXmlFile(DispatchContext dctx,
        Map<String, ? extends Object> context) {
    Locale locale = (Locale) context.get("locale");
    String fileName = (String) context.get("fileName");
    if (UtilValidate.isEmpty(fileName)) {
        Debug.logError("labelFileName cannot be empty", module);
        return ServiceUtil.returnFailure(UtilProperties.getMessage(resource,
                "saveLabelsToXmlFile.exceptionDuringSaveLabelsToXmlFile", locale));
    }/*from   w ww. j a  v  a  2s  .  c  om*/
    String key = (String) context.get("key");
    String keyComment = (String) context.get("keyComment");
    String update_label = (String) context.get("update_label");
    String confirm = (String) context.get("confirm");
    String removeLabel = (String) context.get("removeLabel");
    List<String> localeNames = UtilGenerics.cast(context.get("localeNames"));
    List<String> localeValues = UtilGenerics.cast(context.get("localeValues"));
    List<String> localeComments = UtilGenerics.cast(context.get("localeComments"));
    String apacheLicenseText = null;
    try {
        apacheLicenseText = FileUtil.readString("UTF-8",
                FileUtil.getFile("component://webtools/config/APACHE2_HEADER_FOR_XML"));
    } catch (IOException e) {
        Debug.logWarning(e, "Unable to read Apache License text file", module);
    }
    try {
        LabelManagerFactory factory = LabelManagerFactory.getInstance();
        LabelFile labelFile = factory.getLabelFile(fileName);
        if (labelFile == null) {
            Debug.logError("Invalid file name: " + fileName, module);
            return ServiceUtil.returnFailure(UtilProperties.getMessage(resource,
                    "saveLabelsToXmlFile.exceptionDuringSaveLabelsToXmlFile", locale));
        }
        synchronized (SaveLabelsToXmlFile.class) {
            factory.findMatchingLabels(null, fileName, null, null);
            Map<String, LabelInfo> labels = factory.getLabels();
            Set<String> labelsList = factory.getLabelsList();
            Set<String> localesFound = factory.getLocalesFound();
            for (String localeName : localeNames) {
                localesFound.add(localeName);
            }
            // Remove a Label
            if (UtilValidate.isNotEmpty(removeLabel)) {
                labels.remove(key + LabelManagerFactory.keySeparator + fileName);
            } else if (UtilValidate.isNotEmpty(confirm)) {
                LabelInfo label = labels.get(key + LabelManagerFactory.keySeparator + fileName);
                // Update a Label
                if (update_label.equalsIgnoreCase("Y")) {
                    if (UtilValidate.isNotEmpty(label)) {
                        factory.updateLabelValue(localeNames, localeValues, localeComments, label, key,
                                keyComment, fileName);
                    }
                    // Insert a new Label
                } else {
                    if (UtilValidate.isNotEmpty(label)) {
                        return ServiceUtil.returnError(
                                UtilProperties.getMessage(resource, "WebtoolsLabelManagerNewLabelExisting",
                                        UtilMisc.toMap("key", key, "fileName", fileName), locale));
                    } else {
                        if (UtilValidate.isEmpty(key)) {
                            return ServiceUtil.returnError(UtilProperties.getMessage(resource,
                                    "WebtoolsLabelManagerNewLabelEmptyKey", locale));
                        } else {
                            int notEmptyLabels = factory.updateLabelValue(localeNames, localeValues,
                                    localeComments, null, key, keyComment, fileName);
                            if (notEmptyLabels == 0) {
                                return ServiceUtil.returnError(UtilProperties.getMessage(resource,
                                        "WebtoolsLabelManagerNewLabelEmpty", locale));
                            }
                        }
                    }
                }
            }
            Document resourceDocument = UtilXml.makeEmptyXmlDocument("resource");
            Element resourceElem = resourceDocument.getDocumentElement();
            resourceElem.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            resourceElem.setAttribute("xsi:noNamespaceSchemaLocation",
                    "http://noerp.apache.org/dtds/noerp-properties.xsd");
            for (String labelKey : labelsList) {
                LabelInfo labelInfo = labels.get(labelKey);
                if (!(labelInfo.getFileName().equalsIgnoreCase(fileName))) {
                    continue;
                }
                Element propertyElem = UtilXml.addChildElement(resourceElem, "property", resourceDocument);
                propertyElem.setAttribute("key", StringEscapeUtils.unescapeHtml(labelInfo.getLabelKey()));
                if (UtilValidate.isNotEmpty(labelInfo.getLabelKeyComment())) {
                    Comment labelKeyComment = resourceDocument
                            .createComment(StringEscapeUtils.unescapeHtml(labelInfo.getLabelKeyComment()));
                    Node parent = propertyElem.getParentNode();
                    parent.insertBefore(labelKeyComment, propertyElem);
                }
                for (String localeFound : localesFound) {
                    LabelValue labelValue = labelInfo.getLabelValue(localeFound);
                    String valueString = null;
                    if (labelValue != null) {
                        valueString = labelValue.getLabelValue();
                    }
                    if (UtilValidate.isNotEmpty(valueString)) {
                        valueString = StringEscapeUtils.unescapeHtml(valueString);
                        Element valueElem = UtilXml.addChildElementValue(propertyElem, "value", valueString,
                                resourceDocument);
                        valueElem.setAttribute("xml:lang", localeFound);
                        if (UtilValidate.isNotEmpty(labelValue.getLabelComment())) {
                            Comment labelComment = resourceDocument.createComment(
                                    StringEscapeUtils.unescapeHtml(labelValue.getLabelComment()));
                            Node parent = valueElem.getParentNode();
                            parent.insertBefore(labelComment, valueElem);
                        }
                    }
                }
                FileOutputStream fos = new FileOutputStream(labelFile.file);
                try {
                    if (apacheLicenseText != null) {
                        fos.write(apacheLicenseText.getBytes());
                    }
                    UtilXml.writeXmlDocument(resourceElem, fos, "UTF-8", !(apacheLicenseText == null), true, 4);
                } finally {
                    fos.close();
                    // clear cache to see immediately the new labels and
                    // translations in OFBiz
                    UtilCache.clearCache("properties.UtilPropertiesBundleCache");
                }
            }
        }
    } catch (Exception e) {
        Debug.logError(e, "Exception during save labels to xml file:", module);
        return ServiceUtil.returnFailure(UtilProperties.getMessage(resource,
                "saveLabelsToXmlFile.exceptionDuringSaveLabelsToXmlFile", locale));
    }
    return ServiceUtil.returnSuccess();
}