OpenImmo_1_2_1 extends XmlConverter<OpenImmoDocument, OpenImmoVersion> { protected void upgradeEnergiepassElements(Document doc) throws JaxenException { List nodes = XmlUtils.newXPath( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass", doc).selectNodes(doc); for (Object item : nodes) { Element parentNode = (Element) item; String energiebedarfValue = null; String skalaValue = null; Element artNode = (Element) XmlUtils.newXPath("io:art", doc) .selectSingleNode(parentNode); String artValue = (artNode != null) ? StringUtils.trimToNull(artNode.getTextContent()) : null; List childNodes = XmlUtils.newXPath("io:heizwert", doc) .selectNodes(parentNode); for (Object childItem : childNodes) { Node childNode = (Node) childItem; childNode.getParentNode().removeChild(childNode); } childNodes = XmlUtils.newXPath("io:energiebedarf", doc) .selectNodes(parentNode); for (Object childItem : childNodes) { Node childNode = (Node) childItem; if (energiebedarfValue == null) energiebedarfValue = StringUtils.trimToNull(childNode.getTextContent()); childNode.getParentNode().removeChild(childNode); } childNodes = XmlUtils.newXPath("io:skala", doc) .selectNodes(parentNode); for (Object childItem : childNodes) { Element childNode = (Element) childItem; if (skalaValue == null && "ZAHL".equalsIgnoreCase(childNode.getAttribute("type"))) skalaValue = StringUtils.trimToNull(childNode.getTextContent()); childNode.getParentNode().removeChild(childNode); } if (artNode != null && "VERBRAUCH".equalsIgnoreCase(artValue)) { artNode.setTextContent("VERBRAUCH"); String value = skalaValue; if (value != null) { Element newNode = doc.createElementNS(StringUtils.EMPTY, "energieverbrauchkennwert"); newNode.setTextContent(value); parentNode.appendChild(newNode); } } else if (artNode != null && "BEDARF".equalsIgnoreCase(artValue)) { artNode.setTextContent("BEDARF"); String value = (energiebedarfValue != null) ? energiebedarfValue : skalaValue; if (value != null) { Element newNode = doc.createElementNS(StringUtils.EMPTY, "endenergiebedarf"); newNode.setTextContent(value); parentNode.appendChild(newNode); } } } }  @Override OpenImmoVersion getVersion(); @Override void downgradeToPreviousVersion(OpenImmoDocument doc); @Override @SuppressWarnings("Duplicates") void upgradeFromPreviousVersion(OpenImmoDocument doc);  }
@Test public void testUpgradeEnergiepassElements() { try { Document doc = buildTransferDocumentForUpgrade(); int count; count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:skala", doc); Assert.assertEquals( "2 convertable <energiepass> elements available before conversion.", 2, count); new OpenImmo_1_2_1().upgradeEnergiepassElements(doc); new OpenImmo_1_2_1().upgradeXmlNamespace(doc); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:skala", doc); Assert.assertEquals( "0 convertable <energiepass> elements available after conversion.", 0, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:energieverbrauchkennwert | " + "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:endenergiebedarf", doc); Assert.assertEquals( "2 converted <energiepass> elements available after conversion.", 2, count); } catch (Exception ex) { LOGGER.error("Test of OpenImmo_1_2_1.upgradeEnergiepassElements failed!"); LOGGER.error("> " + ex.getLocalizedMessage(), ex); Assert.fail("Test of OpenImmo_1_2_1.upgradeEnergiepassElements failed!"); } }