List of usage examples for org.w3c.dom Element getParentNode
public Node getParentNode();
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 <new_build> elements to Kyero 2.1. * <p>/* w ww . java 2s . c om*/ * The <new_build> elements are not available in version 2.1. Instead * the value "new_build" is used in the <price_freq> element. * <p> * Any <new_build> elements are removed. If its value is set to "1", * then <price_freq>sale</price_freq> is convertet to * <price_freq>new_build</price_freq>, * * @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 <custom> elements. * <p>//from ww w . j a va2 s. co m * Kyero 3 does not support <custom> elements in <property> and * <agent>. * <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 <energy_rating> elements. * <p>//from www . j a v a 2s . c o m * Kyero 2.1 does not support <energy_rating> 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 <location> elements. * <p>/*from w w w . j av a 2 s . c o m*/ * Kyero 2.1 does not support <location> 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 <notes> elements. * <p>/* www. jav a 2 s.c o m*/ * Kyero 2.1 does not support <notes> 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 <currency> elements to Kyero 3. * <p>/*from w w w.j av a 2 s. co m*/ * The <currency> only supports the values "EUR", "GBP", "USD" in * version 3. * <p> * Any <currency> 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 <new_build> elements for Kyero 3. * <p>/* ww w . j a v a2 s.co m*/ * The <new_build> elements are not available in version 2.1. Instead * the value "new_build" is used in the <price_freq> element. * <p> * Any occurences of <price_freq>new_build</price_freq> is * replaced by <price_freq>sale</price_freq> and * <new_build>1</new_build> 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 <url> elements to Kyero 3. * <p>/* w w w.ja v a 2 s. co m*/ * The <url> 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 <url> elements is removed and copied into the * <en> 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 <anbieter> elements. * <p>//from www .j a v a2s . c o m * OpenImmo 1.1 does not support the following children for * <anbieter> elements: <lizenzkennung>, <impressum_strukt> * <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); } }