OpenImmo_1_2_3 extends XmlConverter<OpenImmoDocument, OpenImmoVersion> { protected void downgradeFlaechenChildElements(Document doc) throws JaxenException { List nodes = XmlUtils.newXPath( "/io:openimmo/io:anbieter/io:immobilie/io:flaechen", doc).selectNodes(doc); for (Object item : nodes) { Element parentNode = (Element) item; boolean passedAnzahlBalkone = false; boolean passedAnzahlTerrassen = false; double sum = 0; List childNodes = XmlUtils.newXPath("io:anzahl_balkone", doc) .selectNodes(parentNode); for (Object childItem : childNodes) { Node node = (Node) childItem; if (!passedAnzahlBalkone) { passedAnzahlBalkone = true; String value = StringUtils.trimToNull(node.getTextContent()); try { sum += (value != null) ? Double.parseDouble(value) : 0; } catch (NumberFormatException ex) { LOGGER.warn("Can't parse <anzahl_balkone>" + value + "</anzahl_balkone> into a numeric value!"); LOGGER.warn("> " + ex.getLocalizedMessage(), ex); } } parentNode.removeChild(node); } childNodes = XmlUtils.newXPath("io:anzahl_terrassen", doc) .selectNodes(parentNode); for (Object childItem : childNodes) { Node node = (Node) childItem; if (!passedAnzahlTerrassen) { passedAnzahlTerrassen = true; String value = StringUtils.trimToNull(node.getTextContent()); try { sum += (value != null) ? Double.parseDouble(value) : 0; } catch (NumberFormatException ex) { LOGGER.warn("Can't parse <anzahl_terrassen>" + value + "</anzahl_terrassen> into a numeric value!"); LOGGER.warn("> " + ex.getLocalizedMessage(), ex); } } parentNode.removeChild(node); } if (sum > 0) { Element node = (Element) XmlUtils.newXPath("io:anzahl_balkon_terrassen", doc) .selectSingleNode(parentNode); if (node == null) { node = doc.createElementNS(StringUtils.EMPTY, "anzahl_balkon_terrassen"); node.setTextContent(String.valueOf(sum)); parentNode.appendChild(node); } else if (StringUtils.isBlank(node.getTextContent())) { node.setTextContent(String.valueOf(sum)); } } } }  @Override OpenImmoVersion getVersion(); @Override void downgradeToPreviousVersion(OpenImmoDocument doc); @Override void upgradeFromPreviousVersion(OpenImmoDocument doc);  }
@Test public void testDowngradeFlaechenChildElements() { try { Document doc = buildTransferDocumentForDowngrade(); int count; count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:flaechen/io:anzahl_balkone | " + "/io:openimmo/io:anbieter/io:immobilie/io:flaechen/io:anzahl_terrassen", doc); Assert.assertEquals( "6 convertable <anzahl_balkone> and <anzahl_terrassen> elements available before conversion.", 6, count); new OpenImmo_1_2_3().downgradeFlaechenChildElements(doc); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:flaechen/io:anzahl_balkone | " + "/io:openimmo/io:anbieter/io:immobilie/io:flaechen/io:anzahl_terrassen", doc); Assert.assertEquals( "0 convertable <anzahl_balkone> and <anzahl_terrassen> elements available after conversion.", 0, count); } catch (Exception ex) { LOGGER.error("Test of OpenImmo_1_2_3.downgradeFlaechenChildElements failed!"); LOGGER.error("> " + ex.getLocalizedMessage(), ex); Assert.fail("Test of OpenImmo_1_2_3.downgradeFlaechenChildElements failed!"); } }