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.ofbiz.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));
    }/*w  ww. java  2 s . c o m*/
    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://ofbiz.apache.org/dtds/ofbiz-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();
}

From source file:org.openestate.io.kyero.converters.Kyero_3.java

/**
 * Downgrade &lt;new_build&gt; elements to Kyero 2.1.
 * <p>/*  w ww  .  java 2s . c  om*/
 * The &lt;new_build&gt; elements are not available in version 2.1. Instead
 * the value "new_build" is used in the &lt;price_freq&gt; element.
 * <p>
 * Any &lt;new_build&gt; elements are removed. If its value is set to "1",
 * then &lt;price_freq&gt;sale&lt;/price_freq&gt; is convertet to
 * &lt;price_freq&gt;new_build&lt;/price_freq&gt;,
 *
 * @param doc Kyero document in version 3
 * @throws JaxenException
 */
protected void downgradeNewBuildElements(Document doc) throws JaxenException {
    List nodes = XmlUtils.newXPath("/io:root/io:property/io:new_build", doc).selectNodes(doc);
    for (Object item : nodes) {
        Element node = (Element) item;
        Element parentNode = (Element) node.getParentNode();
        String value = StringUtils.trimToNull(node.getTextContent());
        if ("1".equals(value)) {
            Element priceFreqNode = (Element) XmlUtils.newXPath("io:price_freq", doc)
                    .selectSingleNode(parentNode);
            if (priceFreqNode == null) {
                priceFreqNode = doc.createElementNS(KyeroUtils.NAMESPACE, "price_freq");
                priceFreqNode.setTextContent("new_build");
                parentNode.appendChild(priceFreqNode);
            } else if ("sale".equalsIgnoreCase(priceFreqNode.getTextContent())) {
                priceFreqNode.setTextContent("new_build");
            }
        }
        parentNode.removeChild(node);
    }
}

From source file:org.openestate.io.kyero.converters.Kyero_3.java

/**
 * Remove &lt;custom&gt; elements.
 * <p>//from ww w .  j a  va2 s. co  m
 * Kyero 3 does not support &lt;custom&gt; elements in &lt;property&gt; and
 * &lt;agent&gt;.
 * <p>
 * Any occurence of these elements is removed.
 *
 * @param doc OpenImmo document in version 2.1
 * @throws JaxenException
 */
protected void removeCustomElements(Document doc) throws JaxenException {
    List nodes = XmlUtils.newXPath("/io:root/io:property/io:custom  | " + "/io:root/io:agent/io:custom", doc)
            .selectNodes(doc);
    for (Object item : nodes) {
        Element node = (Element) item;
        Element parentNode = (Element) node.getParentNode();
        parentNode.removeChild(node);
    }
}

From source file:org.openestate.io.kyero.converters.Kyero_3.java

/**
 * Remove &lt;energy_rating&gt; elements.
 * <p>//from www .  j a  v a 2s .  c  o  m
 * Kyero 2.1 does not support &lt;energy_rating&gt; elements.
 *
 * @param doc OpenImmo document in version 3
 * @throws JaxenException
 */
protected void removeEnergyRatingElements(Document doc) throws JaxenException {
    List nodes = XmlUtils.newXPath("/io:root/io:property/io:energy_rating", doc).selectNodes(doc);
    for (Object item : nodes) {
        Element node = (Element) item;
        Element parentNode = (Element) node.getParentNode();
        parentNode.removeChild(node);
    }
}

From source file:org.openestate.io.kyero.converters.Kyero_3.java

/**
 * Remove &lt;location&gt; elements.
 * <p>/*from w w w  .  j av a  2  s  .  c  o m*/
 * Kyero 2.1 does not support &lt;location&gt; elements.
 *
 * @param doc OpenImmo document in version 3
 * @throws JaxenException
 */
protected void removeLocationElements(Document doc) throws JaxenException {
    List nodes = XmlUtils.newXPath("/io:root/io:property/io:location", doc).selectNodes(doc);
    for (Object item : nodes) {
        Element node = (Element) item;
        Element parentNode = (Element) node.getParentNode();
        parentNode.removeChild(node);
    }
}

From source file:org.openestate.io.kyero.converters.Kyero_3.java

/**
 * Remove &lt;notes&gt; elements.
 * <p>/*  www.  jav  a  2 s.c  o  m*/
 * Kyero 2.1 does not support &lt;notes&gt; elements.
 *
 * @param doc OpenImmo document in version 3
 * @throws JaxenException
 */
protected void removeNotesElements(Document doc) throws JaxenException {
    List nodes = XmlUtils.newXPath("/io:root/io:property/io:notes", doc).selectNodes(doc);
    for (Object item : nodes) {
        Element node = (Element) item;
        Element parentNode = (Element) node.getParentNode();
        parentNode.removeChild(node);
    }
}

From source file:org.openestate.io.kyero.converters.Kyero_3.java

/**
 * Upgrade &lt;currency&gt; elements to Kyero 3.
 * <p>/*from  w  w  w.j av  a  2 s.  co m*/
 * The &lt;currency&gt; only supports the values "EUR", "GBP", "USD" in
 * version 3.
 * <p>
 * Any &lt;currency&gt; with an unsupported value is removed from the
 * document.
 *
 * @param doc Kyero document in version 2.1
 * @throws JaxenException
 */
protected void upgradeCurrencyElements(Document doc) throws JaxenException {
    List nodes = XmlUtils.newXPath("/io:root/io:property/io:currency", doc).selectNodes(doc);
    for (Object item : nodes) {
        Element node = (Element) item;
        Element parentNode = (Element) node.getParentNode();
        String value = StringUtils.trimToNull(node.getTextContent());
        if ("EUR".equalsIgnoreCase(value))
            node.setTextContent("EUR");
        else if ("GBP".equalsIgnoreCase(value))
            node.setTextContent("GBP");
        else if ("USD".equalsIgnoreCase(value))
            node.setTextContent("USD");
        else
            parentNode.removeChild(node);
    }
}

From source file:org.openestate.io.kyero.converters.Kyero_3.java

/**
 * Upgrade &lt;new_build&gt; elements for Kyero 3.
 * <p>/*  ww  w . j a v a2 s.co  m*/
 * The &lt;new_build&gt; elements are not available in version 2.1. Instead
 * the value "new_build" is used in the &lt;price_freq&gt; element.
 * <p>
 * Any occurences of &lt;price_freq&gt;new_build&lt;/price_freq&gt; is
 * replaced by &lt;price_freq&gt;sale&lt;/price_freq&gt; and
 * &lt;new_build&gt;1&lt;/new_build&gt; is added to the property.
 *
 * @param doc Kyero document in version 2.1
 * @throws JaxenException
 */
protected void upgradeNewBuildElements(Document doc) throws JaxenException {
    List nodes = XmlUtils.newXPath("/io:root/io:property/io:price_freq", doc).selectNodes(doc);
    for (Object item : nodes) {
        Element node = (Element) item;
        Element parentNode = (Element) node.getParentNode();
        if (!"new_build".equalsIgnoreCase(node.getTextContent()))
            continue;
        node.setTextContent("sale");

        Element newBuildNode = doc.createElementNS(KyeroUtils.NAMESPACE, "new_build");
        newBuildNode.setTextContent("1");
        parentNode.appendChild(newBuildNode);
    }
}

From source file:org.openestate.io.kyero.converters.Kyero_3.java

/**
 * Upgrade &lt;url&gt; elements to Kyero 3.
 * <p>/*  w w w.ja v  a  2  s.  co m*/
 * The &lt;url&gt; elements only support a simple text value in version
 * 2.1. Version 3 allows different URL's for different languages.
 * <p>
 * The simple value of &lt;url&gt; elements is removed and copied into the
 * &lt;en&gt; child element.
 *
 * @param doc Kyero document in version 2.1
 * @throws JaxenException
 */
protected void upgradeUrlElements(Document doc) throws JaxenException {
    List nodes = XmlUtils.newXPath("/io:root/io:property/io:url", doc).selectNodes(doc);
    for (Object item : nodes) {
        Element node = (Element) item;
        String value = StringUtils.trimToNull(node.getTextContent());
        node.setTextContent(null);
        if (value == null) {
            Element parentNode = (Element) node.getParentNode();
            parentNode.removeChild(node);
        } else {
            Element childNode = doc.createElementNS(KyeroUtils.NAMESPACE, "en");
            childNode.setTextContent(value);
            node.appendChild(childNode);
        }
    }
}

From source file:org.openestate.io.openimmo.converters.OpenImmo_1_2_0.java

/**
 * Remove unsupported children from all &lt;anbieter&gt; elements.
 * <p>//from  www  .j  a v  a2s .  c o  m
 * OpenImmo 1.1 does not support the following children for
 * &lt;anbieter&gt; elements: &lt;lizenzkennung&gt;, &lt;impressum_strukt&gt;
 * <p>
 * These elements are removed by this function.
 *
 * @param doc OpenImmo document in version 1.2.0
 * @throws JaxenException
 */
protected void removeAnbieterChildElements(Document doc) throws JaxenException {
    List nodes = XmlUtils.newXPath(
            "/io:openimmo/io:anbieter/io:lizenzkennung | " + "/io:openimmo/io:anbieter/io:impressum_strukt",
            doc).selectNodes(doc);
    for (Object item : nodes) {
        Element node = (Element) item;
        Element parentNode = (Element) node.getParentNode();
        parentNode.removeChild(node);
    }
}