Java tutorial
/* * Copyright 2015-2016 OpenEstate.org. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.openestate.io.openimmo.converters; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.jaxen.JaxenException; import org.openestate.io.core.XmlConverter; import org.openestate.io.core.XmlUtils; import org.openestate.io.openimmo.OpenImmoVersion; import org.openestate.io.openimmo.OpenImmoDocument; import org.openestate.io.openimmo.OpenImmoTransferDocument; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; /** * Converter for version 1.2.6. * * @since 1.0 * @author Andreas Rudolph */ public class OpenImmo_1_2_6 extends XmlConverter<OpenImmoDocument, OpenImmoVersion> { private final static Logger LOGGER = LoggerFactory.getLogger(OpenImmo_1_2_6.class); @Override public OpenImmoVersion getVersion() { return OpenImmoVersion.V1_2_6; } /** * Downgrade an OpenImmo document from version 1.2.6 to 1.2.5. * * @param doc OpenImmo document in version 1.2.6 */ @Override public void downgradeToPreviousVersion(OpenImmoDocument doc) { doc.setDocumentVersion(OpenImmoVersion.V1_2_5); if (doc instanceof OpenImmoTransferDocument) { try { this.removePreiseChildElements(doc.getDocument()); } catch (Exception ex) { LOGGER.error("Can't remove unsupported children of <preise> elements!"); LOGGER.error("> " + ex.getLocalizedMessage(), ex); } try { this.downgradeKaufpreisElements(doc.getDocument()); } catch (Exception ex) { LOGGER.error("Can't downgrade <kaufpreis> elements!"); LOGGER.error("> " + ex.getLocalizedMessage(), ex); } try { this.removeZwangsversteigerungElements(doc.getDocument()); } catch (Exception ex) { LOGGER.error("Can't remove unsupported <zwangsversteigerung> elements!"); LOGGER.error("> " + ex.getLocalizedMessage(), ex); } try { this.removeFlaechenChildElements(doc.getDocument()); } catch (Exception ex) { LOGGER.error("Can't remove unsupported children of <flaechen> elements!"); LOGGER.error("> " + ex.getLocalizedMessage(), ex); } try { this.removeBauzoneElements(doc.getDocument()); } catch (Exception ex) { LOGGER.error("Can't remove unsupported <bauzone> elements!"); LOGGER.error("> " + ex.getLocalizedMessage(), ex); } try { this.downgradeBodenElements(doc.getDocument()); } catch (Exception ex) { LOGGER.error("Can't downgrade <boden> elements!"); LOGGER.error("> " + ex.getLocalizedMessage(), ex); } try { this.downgradeEnergietypElements(doc.getDocument()); } catch (Exception ex) { LOGGER.error("Can't downgrade <energietyp> elements!"); LOGGER.error("> " + ex.getLocalizedMessage(), ex); } try { this.downgradeAusblickElements(doc.getDocument()); } catch (Exception ex) { LOGGER.error("Can't downgrade <ausblick> elements!"); LOGGER.error("> " + ex.getLocalizedMessage(), ex); } try { this.downgradeBueroPraxenElements(doc.getDocument()); } catch (Exception ex) { LOGGER.error("Can't downgrade <buero_praxen> elements!"); LOGGER.error("> " + ex.getLocalizedMessage(), ex); } } } /** * Upgrade an OpenImmo document from version 1.2.5 to 1.2.6. * * @param doc OpenImmo document in version 1.2.5 */ @Override public void upgradeFromPreviousVersion(OpenImmoDocument doc) { doc.setDocumentVersion(OpenImmoVersion.V1_2_6); } /** * Downgrade <ausblick> elements to OpenImmo 1.2.5. * <p> * The option "MEER" for the "blick" attribute of * <ausblick> elements is not available in version 1.2.5. * <p> * Any occurence of the "MEER" value is replaced by the "SEE" value. * * @param doc OpenImmo document in version 1.2.6 * @throws JaxenException */ protected void downgradeAusblickElements(Document doc) throws JaxenException { List nodes = XmlUtils .newXPath("/io:openimmo/io:anbieter/io:immobilie/io:infrastruktur/io:ausblick[@blick]", doc) .selectNodes(doc); for (Object item : nodes) { Element node = (Element) item; String value = StringUtils.trimToNull(node.getAttribute("blick")); if ("MEER".equalsIgnoreCase(value)) node.setAttribute("blick", "SEE"); } } /** * Downgrade <boden> elements to OpenImmo 1.2.5. * <p> * The attribute "GRANIT" of <boden> elements are not available in * OpenImmo 1.2.5. * * @param doc OpenImmo document in version 1.2.6 * @throws JaxenException */ protected void downgradeBodenElements(Document doc) throws JaxenException { List nodes = XmlUtils .newXPath("/io:openimmo/io:anbieter/io:immobilie/io:ausstattung/io:boden[@GRANIT]", doc) .selectNodes(doc); for (Object item : nodes) { Element node = (Element) item; node.removeAttribute("GRANIT"); } } /** * Downgrade <buero_praxen> elements to OpenImmo 1.2.5. * <p> * The options "COWORKING", "SHARED_OFFICE" for the "buero_typ" attribute of * <buero_praxen> elements are not available in version 1.2.5. * <p> * Any occurence of these values is replaced by the general "BUEROFLAECHE" * value. * * @param doc OpenImmo document in version 1.2.6 * @throws JaxenException */ protected void downgradeBueroPraxenElements(Document doc) throws JaxenException { List nodes = XmlUtils.newXPath( "/io:openimmo/io:anbieter/io:immobilie/io:objektkategorie/io:objektart/io:buero_praxen[@buero_typ]", doc).selectNodes(doc); for (Object item : nodes) { Element node = (Element) item; String value = StringUtils.trimToNull(node.getAttribute("buero_typ")); if ("COWORKING".equalsIgnoreCase(value)) node.setAttribute("buero_typ", "BUEROFLAECHE"); else if ("SHARED_OFFICE".equalsIgnoreCase(value)) node.setAttribute("buero_typ", "BUEROFLAECHE"); } } /** * Downgrade <kaufpreis> elements to OpenImmo 1.2.5. * <p> * The attribute "auf_anfrage" of <kaufpreis> elements are not available * in OpenImmo 1.2.5. * * @param doc OpenImmo document in version 1.2.6 * @throws JaxenException */ protected void downgradeKaufpreisElements(Document doc) throws JaxenException { List nodes = XmlUtils .newXPath("/io:openimmo/io:anbieter/io:immobilie/io:preise/io:kaufpreis[@auf_anfrage]", doc) .selectNodes(doc); for (Object item : nodes) { Element node = (Element) item; node.removeAttribute("auf_anfrage"); } } /** * Remove <bauzone> elements. * <p> * OpenImmo 1.2.5 does not support <bauzone> elements. * * @param doc OpenImmo document in version 1.2.6 * @throws JaxenException */ protected void removeBauzoneElements(Document doc) throws JaxenException { List nodes = XmlUtils.newXPath("/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:bauzone", doc) .selectNodes(doc); for (Object item : nodes) { Element node = (Element) item; Element parentNode = (Element) node.getParentNode(); parentNode.removeChild(node); } } /** * Downgrade <energietyp> elements to OpenImmo 1.2.5. * <p> * The attributes "MINERGIEBAUWEISE", "MINERGIE_ZERTIFIZIERT" of * <energietyp> elements are not available in OpenImmo 1.2.5. * * @param doc OpenImmo document in version 1.2.6 * @throws JaxenException */ protected void downgradeEnergietypElements(Document doc) throws JaxenException { List nodes = XmlUtils.newXPath( "/io:openimmo/io:anbieter/io:immobilie/io:ausstattung/io:energietyp[@MINERGIEBAUWEISE or @MINERGIE_ZERTIFIZIERT]", doc).selectNodes(doc); for (Object item : nodes) { Element node = (Element) item; node.removeAttribute("MINERGIEBAUWEISE"); node.removeAttribute("MINERGIE_ZERTIFIZIERT"); } } /** * Remove unsupported children from all <flaechen> elements. * <p> * OpenImmo 1.2.5 does not support the following children for * <flaechen> elements: <kubatur>, <ausnuetzungsziffer>, * <flaechevon>, <flaechebis> * <p> * These elements are removed by this function. * * @param doc OpenImmo document in version 1.2.6 * @throws JaxenException */ protected void removeFlaechenChildElements(Document doc) throws JaxenException { List nodes = XmlUtils.newXPath("/io:openimmo/io:anbieter/io:immobilie/io:flaechen/io:kubatur | " + "/io:openimmo/io:anbieter/io:immobilie/io:flaechen/io:ausnuetzungsziffer | " + "/io:openimmo/io:anbieter/io:immobilie/io:flaechen/io:flaechevon | " + "/io:openimmo/io:anbieter/io:immobilie/io:flaechen/io:flaechebis", doc).selectNodes(doc); for (Object item : nodes) { Element node = (Element) item; Element parentNode = (Element) node.getParentNode(); parentNode.removeChild(node); } } /** * Remove unsupported children from all <preise> elements. * <p> * OpenImmo 1.2.5 does not support the following children for * <preise> elements: <kaufpreisnetto>, <kaufpreisbrutto>, * <hauptmietzinsnetto>, <pauschalmiete>, * <betriebskostennetto>, <evbnetto>, <gesamtmietenetto>, * <gesamtmietebrutto>, <gesamtbelastungnetto>, * <gesamtbelastungbrutto>, <gesamtkostenprom2von>, * <heizkostennetto>, <monatlichekostennetto>, * <monatlichekostenbrutto>, <nebenkostenprom2von>, * <ruecklagenetto>, <sonstigekostennetto>, * <sonstigemietenetto>, <summemietenetto>, * <nettomieteprom2von>, <provisionnetto>, * <provisionbrutto>, <richtpreisprom2> * <p> * These elements are removed by this function. * * @param doc OpenImmo document in version 1.2.6 * @throws JaxenException */ protected void removePreiseChildElements(Document doc) throws JaxenException { List nodes = XmlUtils .newXPath("/io:openimmo/io:anbieter/io:immobilie/io:preise/io:kaufpreisnetto | " + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:kaufpreisbrutto | " + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:hauptmietzinsnetto | " + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:pauschalmiete | " + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:betriebskostennetto | " + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:evbnetto | " + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:gesamtmietenetto | " + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:gesamtmietebrutto | " + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:gesamtbelastungnetto | " + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:gesamtbelastungbrutto | " + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:gesamtkostenprom2von | " + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:heizkostennetto | " + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:monatlichekostennetto | " + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:monatlichekostenbrutto | " + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:nebenkostenprom2von | " + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:ruecklagenetto | " + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:sonstigekostennetto | " + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:sonstigemietenetto | " + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:summemietenetto | " + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:nettomieteprom2von | " + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:provisionnetto | " + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:provisionbrutto | " + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:richtpreisprom2", doc) .selectNodes(doc); for (Object item : nodes) { Element node = (Element) item; Element parentNode = (Element) node.getParentNode(); parentNode.removeChild(node); } } /** * Remove <zwangsversteigerung> elements. * <p> * OpenImmo 1.2.5 does not support <zwangsversteigerung> elements. * * @param doc OpenImmo document in version 1.2.6 * @throws JaxenException */ protected void removeZwangsversteigerungElements(Document doc) throws JaxenException { List nodes = XmlUtils .newXPath("/io:openimmo/io:anbieter/io:immobilie/io:versteigerung/io:zwangsversteigerung", doc) .selectNodes(doc); for (Object item : nodes) { Element node = (Element) item; Element parentNode = (Element) node.getParentNode(); parentNode.removeChild(node); } } }