Skip to content

Commit

Permalink
support namespace prefixes when reading XML formats
Browse files Browse the repository at this point in the history
The XML parsers were slightly improved in order to read XML elements with namespace prefix properly.
  • Loading branch information
pinhead84 committed Sep 1, 2015
1 parent 570e350 commit 1ac16e7
Show file tree
Hide file tree
Showing 18 changed files with 588 additions and 21 deletions.
Expand Up @@ -60,7 +60,7 @@ public CasaItDocument( Document document )
public static boolean isReadable( Document doc )
{
Element root = XmlUtils.getRootElement( doc );
return "container".equals( root.getTagName() );
return "container".equals( root.getLocalName() );
}

/**
Expand Down
Expand Up @@ -111,7 +111,7 @@ public DaftIeVersion getLatestVersion()
public static boolean isReadable( Document doc )
{
Element root = XmlUtils.getRootElement( doc );
return "daft".equals( root.getTagName() );
return "daft".equals( root.getLocalName() );
}

/**
Expand Down
Expand Up @@ -60,7 +60,7 @@ public Is24XmlDocument( Document document )
public static boolean isReadable( Document doc )
{
Element root = XmlUtils.getRootElement( doc );
return "IS24ImmobilienTransfer".equals( root.getTagName() );
return "IS24ImmobilienTransfer".equals( root.getLocalName() );
}

/**
Expand Down
@@ -0,0 +1,96 @@
/*
* Copyright 2015 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.is24_xml;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.openestate.io.core.XmlUtils;
import org.openestate.io.is24_xml.xml.ImmobilienTransferTyp;
import org.openestate.io.is24_xml.xml.ImmobilienTransferTyp.Anbieter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;

/**
*
* @author Andreas Rudolph
*/
@RunWith( JUnit4.class )
public class Is24XmlDocumentNamespaceTest
{
private final static Logger LOGGER = LoggerFactory.getLogger( Is24XmlDocumentNamespaceTest.class );

private static Document buildExampleDocument() throws Exception
{
return XmlUtils.newDocument(Is24XmlDocumentNamespaceTest.class.getResourceAsStream( "/is24-namespace.xml" ) );
}

@Test
public void testToObject()
{
try
{
Is24XmlDocument doc = new Is24XmlDocument( buildExampleDocument() );

ImmobilienTransferTyp obj = doc.toObject();
Assert.assertNotNull(
"Created object for transfer document.", obj );
}
catch (Exception ex)
{
LOGGER.error( "Test of Is24XmlDocument.toObject failed!" );
LOGGER.error( "> " + ex.getLocalizedMessage(), ex );
Assert.fail( "Test of Is24XmlDocument.toObject failed!" );
}
}

@Test
@Ignore
public void testToXml()
{
//ObjectType obj = Is24XmlUtils.getFactory().createObjectType();
//obj.setINFODE( "an example property" );
//obj.setORT( "Berlin" );

Anbieter agent = Is24XmlUtils.getFactory().createImmobilienTransferTypAnbieter();
agent.setScoutKundenID( "123456" );
//agent.getImmobilien().add( e );

ImmobilienTransferTyp transfer = Is24XmlUtils.getFactory().createImmobilienTransferTyp();
transfer.setAnbieter( agent );
transfer.setEmailBeiFehler( "test@test.org" );
transfer.setErstellerSoftware( "OpenEstate.org" );
transfer.setErstellerSoftwareVersion( "1.0" );

try
{
Is24XmlDocument doc = Is24XmlDocument.newDocument( transfer );

String xml = doc.toXmlString( true );
LOGGER.info( "XML: " + xml );
}
catch (Exception ex)
{
LOGGER.error( "Test of Is24XmlDocument.toXml failed!" );
LOGGER.error( "> " + ex.getLocalizedMessage(), ex );
Assert.fail( "Test of Is24XmlDocument.toXml failed!" );
}
}
}
73 changes: 73 additions & 0 deletions IS24-XML/src/test/resources/is24-namespace.xml
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<is24:IS24ImmobilienTransfer ErstellerSoftwareVersion="1.0-SNAPSHOT" ErstellerSoftware="OpenEstate-ImmoTool" EmailBeiFehler="test@test.de" xmlns:is24="http://www.immobilienscout24.de/immobilientransfer">
<is24:Anbieter ScoutKundenID="123">
<is24:WohnungKauf Ausstattungsqualitaet="Gehoben" Parkplatz="keineAngabe" AlsFerienwohnungGeeignet="true" Heizungsart="keineAngabe" Objektzustand="keineAngabe" JahrLetzteModernisierung="1985" Baujahr="1950" Zimmer="2" Wohnflaeche="123" WohnungKategorie="Sonstige" Waehrung="EUR" Provisionshinweis="provision-de" Provisionspflichtig="false" Ueberschrift="wohnung-kauf" Adressdruck="false" Importmodus="aktualisieren" StatusHP="aktiv" StatusIS24="aktiv" StatusVBM="aktiv" AnbieterObjektID="1">
<is24:Adresse Laenderkennzeichen="DEU" Ort="Berlin" Postleitzahl="12345" Hausnummer="123" Strasse="Karl-Marx-Straße"/>
<is24:Kontaktperson EMail="test@test.de" Telefon="030/123456" Nachname="Simpson" Vorname="Homer" Laenderkennzeichen="DEU"/>
<is24:Objektbeschreibung>details-de</is24:Objektbeschreibung>
<is24:Lage>lage-de</is24:Lage>
<is24:Ausstattung>ausstattung-de</is24:Ausstattung>
<is24:SonstigeAngaben>weiteres-de

ENERGIEAUSWEIS

- Energieeffizienzklasse: B
- erstellt am: 02.05.2014
- gültig bis: 31.03.2018
- Art des Energieausweises: nach Bedarf
- Endenergiebedarf insgesamt: ca. 123 kWh/(m²a)
- Endenergiebedarf für Strom: ca. 234 kWh/(m²a)
- Endenergiebedarf für Heizung: ca. 345 kWh/(m²a)
- Baujahr: 1950</is24:SonstigeAngaben>
<is24:MultimediaAnhang Dateityp="PNG" Titel="agent" Dateiname="f4f5bc373b8c8e4aab3f459cf7324258d6d9dcfb.png" AnhangArt="grundrissBild"/>
<is24:MultimediaAnhang Dateityp="PNG" Titel="3d" Dateiname="7f53789631c6e4b87ce65afd66ee7751e6ff9975.png" AnhangArt="bild"/>
<is24:MultimediaAnhang Dateityp="PNG" Titel="access" Dateiname="d99912f602973c320c291c2655e775f492bda10f.png" AnhangArt="bild"/>
<is24:MultimediaAnhang Dateityp="PNG" Titel="acroread" Dateiname="3df316f7de5038d89805c370be89c17208ffa3db.png" AnhangArt="bild"/>
<is24:MultimediaAnhang Dateityp="PNG" Titel="advancedsettings" Dateiname="1f8c6118d8ae9c90cb827326e63bb4715bd69844.png" AnhangArt="bild"/>
<is24:MultimediaAnhang Dateityp="PNG" Titel="agt aisles" Dateiname="a6b5186a5e4864891af87e77397f8614717cac34.png" AnhangArt="grundrissBild"/>
<is24:MultimediaAnhang Dateityp="PNG" Titel="agt announcements" Dateiname="4248ad94c9c66c7b05ebfae325e6c1498d756b33.png" AnhangArt="grundrissBild"/>
<is24:MultimediaAnhang Dateityp="PNG" Titel="asd" Dateiname="55193ffb5097ad2ce5c8cf543bc6be17bd690943.png" AnhangArt="bild"/>
<is24:Kaufpreise Kaufpreis="12345"/>
<is24:BefeuerungsArt>
<is24:KeineAngabe xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</is24:BefeuerungsArt>
<is24:Energieausweis Energieverbrauchskennwert="123" Energieausweistyp="Endenergiebedarf"/>
</is24:WohnungKauf>
<is24:WohnungMiete FreiAb="17.06.2013" Parkplatz="keineAngabe" Haustiere="keineAngabe" Heizungsart="keineAngabe" Objektzustand="keineAngabe" Zimmer="3" Wohnflaeche="234" WohnungKategorie="Sonstige" Waehrung="EUR" Provisionspflichtig="true" Provision="keine Provision" Ueberschrift="wohnung-miete" Adressdruck="true" Importmodus="aktualisieren" StatusHP="aktiv" StatusIS24="aktiv" StatusVBM="aktiv" AnbieterObjektID="2">
<is24:Adresse Laenderkennzeichen="DEU" Ort="Berlin" Postleitzahl="12345" Hausnummer="124" Strasse="Friedrichstraße"/>
<is24:Kontaktperson EMail="hans@dampf.de" Telefon="030/123456" Nachname="Dampf" Vorname="Hans" Laenderkennzeichen="DEU"/>
<is24:Objektbeschreibung>Er hörte leise Schritte hinter sich. Das bedeutete nichts Gutes. Wer würde ihm schon folgen, spät in der Nacht und dazu noch in dieser engen Gasse mitten im übel beleumundeten Hafenviertel? Gerade jetzt, wo er das Ding seines Lebens gedreht hatte und mit der Beute verschwinden wollte! Hatte einer seiner zahllosen Kollegen dieselbe Idee gehabt, ihn beobachtet und abgewartet, um ihn nun um die Früchte seiner Arbeit zu erleichtern?

Oder gehörten die Schritte hinter ihm zu einem der unzähligen Gesetzeshüter dieser Stadt, und die stählerne Acht um seine Handgelenke würde gleich zuschnappen? Er konnte die Aufforderung stehen zu bleiben schon hören. Gehetzt sah er sich um. Plötzlich erblickte er den schmalen Durchgang. Blitzartig drehte er sich nach rechts und verschwand zwischen den beiden Gebäuden. Beinahe wäre er dabei über den umgestürzten Mülleimer gefallen, der mitten im Weg lag. Er versuchte, sich in der Dunkelheit seinen Weg zu ertasten und erstarrte: Anscheinend gab es keinen anderen Ausweg aus diesem kleinen Hof als den Durchgang, durch den er gekommen war. Die Schritte wurden lauter und lauter, er sah eine dunkle Gestalt um die Ecke biegen. Fieberhaft irrten seine Augen durch die nächtliche Dunkelheit und suchten einen Ausweg.</is24:Objektbeschreibung>
<is24:SonstigeAngaben>ENERGIEAUSWEIS

- Energieeffizienzklasse: B
- erstellt am: 05.05.2014
- gültig bis: 20.05.2020
- Art des Energieausweises: nach Bedarf
- Endenergiebedarf insgesamt: ca. 123 kWh/(m²a)
- Endenergiebedarf für Strom: ca. 234 kWh/(m²a)
- Endenergiebedarf für Heizung: ca. 345 kWh/(m²a)</is24:SonstigeAngaben>
<is24:MultimediaAnhang Dateityp="JPG" Titel="titel 2" Dateiname="bc11b82fea15f8384a82e2556792be704ea2f206.jpg" AnhangArt="bild"/>
<is24:MultimediaAnhang Dateityp="JPG" Titel="foto3" Dateiname="30d9485c2539b32942a9bf4e0558222301867aaf.jpg" AnhangArt="bild"/>
<is24:MultimediaAnhang Dateityp="JPG" Titel="foto4" Dateiname="8298db6c57930c45f7c0558ede51daabf08d7d5a.jpg" AnhangArt="bild"/>
<is24:MultimediaAnhang Dateityp="JPG" Titel="lageplan 1" Dateiname="ca04b415c029b9d57b2426f7f03a7a62f2270c63.jpg" AnhangArt="bild"/>
<is24:MultimediaAnhang Dateityp="JPG" Titel="lageplan 2" Dateiname="9897ddd6a2416bf4f9d7e5521f660b669e59ae68.jpg" AnhangArt="bild"/>
<is24:Mietpreise Kaltmiete="123"/>
<is24:BefeuerungsArt>
<is24:KeineAngabe xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</is24:BefeuerungsArt>
<is24:Energieausweis Energieverbrauchskennwert="123" Energieausweistyp="Endenergiebedarf"/>
</is24:WohnungMiete>
<is24:GarageKauf Objektzustand="keineAngabe" ObjektKategorie2="keineAngabe" Waehrung="EUR" Provisionspflichtig="false" Ueberschrift="Stellplatz / Kauf" Adressdruck="false" Importmodus="loeschen" StatusHP="inaktiv" StatusIS24="inaktiv" StatusVBM="inaktiv" AnbieterObjektID="3">
<is24:Adresse Laenderkennzeichen="DEU" Ort="Berlin" Postleitzahl="13125"/>
<is24:Kontaktperson EMail="test@test.de" Telefon="030/123456"/>
<is24:Kaufpreise Kaufpreis="99999"/>
</is24:GarageKauf>
<is24:GarageKauf Objektzustand="keineAngabe" ObjektKategorie2="keineAngabe" Waehrung="EUR" Provisionspflichtig="false" Ueberschrift="Stellplatz / Kauf" Adressdruck="false" Importmodus="loeschen" StatusHP="inaktiv" StatusIS24="inaktiv" StatusVBM="inaktiv" AnbieterObjektID="4">
<is24:Adresse Laenderkennzeichen="DEU" Ort="Berlin" Postleitzahl="13125"/>
<is24:Kontaktperson EMail="test@test.de" Telefon="030/123456"/>
<is24:Kaufpreise Kaufpreis="99999"/>
</is24:GarageKauf>
</is24:Anbieter>
</is24:IS24ImmobilienTransfer>
Expand Up @@ -110,7 +110,7 @@ public ImmoXmlVersion getLatestVersion()
public static boolean isReadable( Document doc )
{
Element root = XmlUtils.getRootElement( doc );
return "immoxml".equals( root.getTagName() );
return "immoxml".equals( root.getLocalName() );
}

/**
Expand Down
@@ -0,0 +1,112 @@
/*
* Copyright 2015 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.immoxml;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.openestate.io.core.XmlUtils;
import org.openestate.io.immoxml.xml.Immoxml;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;

/**
*
* @author Andreas Rudolph
*/
@RunWith( JUnit4.class )
public class ImmoXmlDocumentNamespaceTest
{
private final static Logger LOGGER = LoggerFactory.getLogger( ImmoXmlDocumentNamespaceTest.class );

private static Document buildExampleDocument( String version ) throws Exception
{
return XmlUtils.newDocument("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
+ "<imo:immoxml xmlns:imo=\"" + ImmoXmlUtils.NAMESPACE + "\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.openimmo.de openimmo.xsd\">\n"
+ " <imo:uebertragung version=\"" + ImmoXmlUtils.VERSION.toReadableVersion() + "\" "
+ " sendersoftware=\"OpenEstate.org\" "
+ " techn_email=\"test@openestate.org\" "
+ " umfang=\"VOLL\" art=\"OFFLINE\"/>\n"
+ " <imo:anbieter>\n"
+ " </imo:anbieter>\n"
+ "</imo:immoxml>" );
}

@Test
public void testDowngrade()
{
try
{
String version = ImmoXmlUtils.VERSION.toReadableVersion();
ImmoXmlDocument doc = new ImmoXmlDocument( buildExampleDocument( version ) );

doc.downgrade( ImmoXmlVersion.V3_0 );
Assert.assertEquals("downgrade from "+version+" to 3.0", ImmoXmlVersion.V3_0, doc.getDocumentVersion() );
}
catch (Exception ex)
{
LOGGER.error( "Test of ImmoXmlDocument.downgrade failed!" );
LOGGER.error( "> " + ex.getLocalizedMessage(), ex );
Assert.fail( "Test of ImmoXmlDocument.downgrade failed!" );
}
}

@Test
public void testToObject()
{
try
{
String version = ImmoXmlUtils.VERSION.toReadableVersion();

ImmoXmlDocument doc = new ImmoXmlDocument( buildExampleDocument( version ) );
Assert.assertEquals(version, ImmoXmlUtils.VERSION, doc.getDocumentVersion() );

Immoxml obj = doc.toObject();
Assert.assertNotNull(
"Created object for transfer document.", obj );
Assert.assertEquals(
"Matching version between document and object.", doc.getDocumentVersion().toReadableVersion(), obj.getUebertragung().getVersion() );
}
catch (Exception ex)
{
LOGGER.error( "Test of ImmoXmlDocument.toObject failed!" );
LOGGER.error( "> " + ex.getLocalizedMessage(), ex );
Assert.fail( "Test of ImmoXmlDocument.toObject failed!" );
}
}

@Test
public void testUpgrade()
{
try
{
String version = ImmoXmlUtils.VERSION.toReadableVersion();
ImmoXmlDocument doc = new ImmoXmlDocument( buildExampleDocument( "1.1" ) );

doc.upgrade( ImmoXmlUtils.VERSION );
Assert.assertEquals("upgrade from 1.1 to "+version, ImmoXmlUtils.VERSION, doc.getDocumentVersion() );
}
catch (Exception ex)
{
LOGGER.error( "Test of ImmoXmlDocument.upgrade failed!" );
LOGGER.error( "> " + ex.getLocalizedMessage(), ex );
Assert.fail( "Test of ImmoXmlDocument.upgrade failed!" );
}
}
}
Expand Up @@ -113,7 +113,7 @@ public ImmobiliareItVersion getLatestVersion()
public static boolean isReadable( Document doc )
{
Element root = XmlUtils.getRootElement( doc );
return "feed".equals( root.getTagName() );
return "feed".equals( root.getLocalName() );
}

/**
Expand Down
Expand Up @@ -20,10 +20,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Currency;
import java.util.Date;
import java.util.Locale;
import javax.xml.bind.DatatypeConverter;
import javax.xml.bind.JAXBContext;
Expand All @@ -32,7 +30,6 @@
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.openestate.io.core.LocaleUtils;
import org.openestate.io.core.XmlUtils;
import org.openestate.io.core.XmlValidationHandler;
Expand Down

0 comments on commit 1ac16e7

Please sign in to comment.