-
Notifications
You must be signed in to change notification settings - Fork 16
Usage Kyero
The class org.openestate.io.kyero.KyeroUtils
provides a
static createDocument()
function to read XML data in kyero.com format
from a java.io.File
, java.io.InputStream
, java.lang.String
or
org.w3c.dom.Document
into a org.openestate.io.kyero.KyeroDocument
.
import java.io.File;
import org.apache.commons.lang3.StringUtils;
import org.openestate.io.kyero.KyeroDocument;
import org.openestate.io.kyero.KyeroUtils;
import org.openestate.io.kyero.xml.PropertyType;
import org.openestate.io.kyero.xml.Root;
public static void main( String[] args )
{
if (args.length<1)
{
System.err.println( "No file was specified!" );
System.exit( 1 );
}
// read file into a KyeroDocument
KyeroDocument doc = KyeroUtils.createDocument( new File( args[0] ) );
// convert KyeroDocument into a Java object
Root root = doc.toObject();
// now we can access the XML content through ordinary Java objects
for (PropertyType obj : root.getProperty())
{
// get object nr
String objectNr = (obj.getId()!=null)?
obj.getId(): "???";
// get object description
String objectInfo = null;
if (obj.getDesc()!=null)
{
objectInfo = StringUtils.trimToNull( obj.getDesc().getEn() );
if (objectInfo==null)
objectInfo = StringUtils.trimToNull( obj.getDesc().getDe() );
if (objectInfo==null)
objectInfo = StringUtils.trimToNull( obj.getDesc().getEs() );
}
// print object informations to console
System.out.println( "> found object '" + objectNr + "' "
+ "with title '" + objectInfo + "'" );
}
}
See a full example at KyeroReadingExample.java
.
The class org.openestate.io.kyero.xml.Root
is equivalent to a <root>
root
element in a kyero.com document. For example the following code creates a
kyero.com document programmatically:
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Calendar;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.openestate.io.kyero.KyeroDocument;
import org.openestate.io.kyero.KyeroUtils;
import org.openestate.io.kyero.xml.CurrencyType;
import org.openestate.io.kyero.xml.EnergyRatingMarkType;
import org.openestate.io.kyero.xml.ImagesType.Image;
import org.openestate.io.kyero.xml.KyeroType;
import org.openestate.io.kyero.xml.ObjectFactory;
import org.openestate.io.kyero.xml.PriceFreqType;
import org.openestate.io.kyero.xml.PropertyType;
import org.openestate.io.kyero.xml.Root;
import org.openestate.io.kyero.xml.Root.Agent;
public static void main( String[] args )
{
// create a Root object with some example data
// this object corresponds to the <root> element in XML
Root root = KyeroUtils.getFactory().createRoot();
root.setKyero( createKyero() );
root.setAgent( createAgent() );
root.getProperty().add( createProperty() );
root.getProperty().add( createProperty() );
root.getProperty().add( createProperty() );
// now make something useful with the object
}
protected static Agent createAgent()
{
Agent agent = KyeroUtils.getFactory().createRootAgent();
agent.setAddr1( "first address line" );
agent.setAddr2( "second address line" );
agent.setCountry( "Germany" );
agent.setEmail( "test@test.org" );
agent.setFax( "030/123456" );
agent.setId( RandomUtils.nextLong( 1, 10000 ) );
agent.setMob( "030/123457" );
agent.setName( "name of the company" );
agent.setPostcode( "12345" );
agent.setRegion( "Berlin" );
agent.setTel( "030/123458" );
agent.setTown( "Berlin" );
return agent;
}
protected static KyeroType createKyero()
{
KyeroType kyero = KyeroUtils.getFactory().createKyeroType();
kyero.setFeedGenerated( Calendar.getInstance() );
kyero.setFeedVersion( KyeroUtils.VERSION.toXmlVersion() );
return kyero;
}
protected static PropertyType createProperty()
{
final String id = RandomStringUtils.randomAlphanumeric( 5 );
int imageCount = 0;
// create an example real estate
PropertyType obj = KyeroUtils.getFactory().createPropertyType();
obj.setBaths( RandomUtils.nextLong( 0, 5 ) );
obj.setBeds( RandomUtils.nextLong( 0, 5 ) );
obj.setCurrency( CurrencyType.EUR );
obj.setDate( Calendar.getInstance() );
obj.setId( id );
obj.setLeasehold( RandomUtils.nextInt( 0, 2 )==1 );
obj.setLocationDetail( "some details about the location" );
obj.setNewBuild( RandomUtils.nextInt( 0, 2 )==1 );
obj.setNotes( "some notes about the property" );
obj.setPartOwnership( RandomUtils.nextInt( 0, 2 )==1 );
obj.setPool( RandomUtils.nextInt( 0, 2 )==1 );
obj.setPrice( RandomUtils.nextLong( 10000, 9999999 ) );
obj.setPriceFreq( PriceFreqType.SALE );
obj.setProvince( "Berlin" );
obj.setRef( RandomStringUtils.randomAlphanumeric( 5 ) );
obj.setTown( "Berlin" );
obj.setType( "house" );
obj.setDesc( KyeroUtils.getFactory().createLangType() );
obj.getDesc().setAr( "Arabic property description" );
obj.getDesc().setBg( "Bulgarian property description" );
obj.getDesc().setCa( "Catalan property description" );
obj.getDesc().setCs( "Czech property description" );
obj.getDesc().setDa( "Danish property description" );
obj.getDesc().setDe( "German property description" );
obj.getDesc().setEl( "Greek property description" );
obj.getDesc().setEn( "English property description" );
obj.getDesc().setEs( "Spanish property description" );
obj.getDesc().setEt( "Estonian property description" );
obj.getDesc().setFa( "Farsi property description" );
obj.getDesc().setFi( "Finnish property description" );
obj.getDesc().setFr( "French property description" );
obj.getDesc().setHe( "Hebrew property description" );
obj.getDesc().setHi( "Hindi property description" );
obj.getDesc().setHu( "Hungarian property description" );
obj.getDesc().setId( "Indonesian property description" );
obj.getDesc().setIt( "Italian property description" );
obj.getDesc().setJa( "Japanese property description" );
obj.getDesc().setKo( "Korean property description" );
obj.getDesc().setLt( "Lithuanian property description" );
obj.getDesc().setLv( "Latvian property description" );
obj.getDesc().setNl( "Dutch property description" );
obj.getDesc().setNo( "Norwegian property description" );
obj.getDesc().setPl( "Polish property description" );
obj.getDesc().setPt( "Portuguese property description" );
obj.getDesc().setRo( "Romanian property description" );
obj.getDesc().setRu( "Russian property description" );
obj.getDesc().setSk( "Slovak property description" );
obj.getDesc().setSl( "Slovenian property description" );
obj.getDesc().setSv( "Swedish property description" );
obj.getDesc().setTh( "Thai property description" );
obj.getDesc().setTr( "Turkish property description" );
obj.getDesc().setUk( "Ukranian property description" );
obj.getDesc().setVi( "Vietnamese property description" );
obj.getDesc().setZh( "Simplified Chinese property description" );
obj.setEnergyRating( KyeroUtils.getFactory().createEnergyRatingType() );
obj.getEnergyRating().setConsumption( EnergyRatingMarkType.C );
obj.getEnergyRating().setEmissions( EnergyRatingMarkType.E );
obj.setFeatures( KyeroUtils.getFactory().createFeaturesType() );
obj.getFeatures().getFeature().add( "name of a feature" );
obj.getFeatures().getFeature().add( "name of another feature" );
obj.setImages( KyeroUtils.getFactory().createImagesType() );
obj.getImages().getImage().add( createPropertyImage( id, ++imageCount ) );
obj.getImages().getImage().add( createPropertyImage( id, ++imageCount ) );
obj.getImages().getImage().add( createPropertyImage( id, ++imageCount ) );
obj.setLocation( KyeroUtils.getFactory().createGpsLocationType() );
obj.getLocation().setLatitude( RandomUtils.nextDouble( 0, 90 ) );
obj.getLocation().setLongitude( RandomUtils.nextDouble( 0, 90 ) );
obj.setSurfaceArea( KyeroUtils.getFactory().createSurfaceType() );
obj.getSurfaceArea().setBuilt( RandomUtils.nextLong( 50, 250 ) );
obj.getSurfaceArea().setPlot( RandomUtils.nextLong( 100, 1500 ) );
obj.setUrl( KyeroUtils.getFactory().createUrlType() );
try
{
obj.getUrl().setAr( new URL( "http://arabic.website.com/property/"+id+".htm" ) );
obj.getUrl().setBg( new URL( "http://bulgarian.website.com/property/"+id+".htm" ) );
obj.getUrl().setCa( new URL( "http://catalan.website.com/property/"+id+".htm" ) );
obj.getUrl().setCs( new URL( "http://czech.website.com/property/"+id+".htm" ) );
obj.getUrl().setDa( new URL( "http://danish.website.com/property/"+id+".htm" ) );
obj.getUrl().setDe( new URL( "http://german.website.com/property/"+id+".htm" ) );
obj.getUrl().setEl( new URL( "http://greek.website.com/property/"+id+".htm" ) );
obj.getUrl().setEn( new URL( "http://english.website.com/property/"+id+".htm" ) );
obj.getUrl().setEs( new URL( "http://spanish.website.com/property/"+id+".htm" ) );
obj.getUrl().setEt( new URL( "http://estonian.website.com/property/"+id+".htm" ) );
obj.getUrl().setFa( new URL( "http://farsi.website.com/property/"+id+".htm" ) );
obj.getUrl().setFi( new URL( "http://finnish.website.com/property/"+id+".htm" ) );
obj.getUrl().setFr( new URL( "http://french.website.com/property/"+id+".htm" ) );
obj.getUrl().setHe( new URL( "http://hebrew.website.com/property/"+id+".htm" ) );
obj.getUrl().setHi( new URL( "http://hindi.website.com/property/"+id+".htm" ) );
obj.getUrl().setHu( new URL( "http://hungarian.website.com/property/"+id+".htm" ) );
obj.getUrl().setId( new URL( "http://indonesian.website.com/property/"+id+".htm" ) );
obj.getUrl().setIt( new URL( "http://italian.website.com/property/"+id+".htm" ) );
obj.getUrl().setJa( new URL( "http://japanese.website.com/property/"+id+".htm" ) );
obj.getUrl().setKo( new URL( "http://korean.website.com/property/"+id+".htm" ) );
obj.getUrl().setLt( new URL( "http://lithunain.website.com/property/"+id+".htm" ) );
obj.getUrl().setLv( new URL( "http://latvian.website.com/property/"+id+".htm" ) );
obj.getUrl().setNl( new URL( "http://dutch.website.com/property/"+id+".htm" ) );
obj.getUrl().setNo( new URL( "http://norwegian.website.com/property/"+id+".htm" ) );
obj.getUrl().setPl( new URL( "http://polish.website.com/property/"+id+".htm" ) );
obj.getUrl().setPt( new URL( "http://portuguese.website.com/property/"+id+".htm" ) );
obj.getUrl().setRo( new URL( "http://romanian.website.com/property/"+id+".htm" ) );
obj.getUrl().setRu( new URL( "http://russian.website.com/property/"+id+".htm" ) );
obj.getUrl().setSk( new URL( "http://slovak.website.com/property/"+id+".htm" ) );
obj.getUrl().setSl( new URL( "http://slovenian.website.com/property/"+id+".htm" ) );
obj.getUrl().setSv( new URL( "http://swedish.website.com/property/"+id+".htm" ) );
obj.getUrl().setTh( new URL( "http://thai.website.com/property/"+id+".htm" ) );
obj.getUrl().setTr( new URL( "http://turkish.website.com/property/"+id+".htm" ) );
obj.getUrl().setUk( new URL( "http://ukranian.website.com/property/"+id+".htm" ) );
obj.getUrl().setVi( new URL( "http://vietnamese.website.com/property/"+id+".htm" ) );
obj.getUrl().setZh( new URL( "http://chinese_simplified.website.com/property/"+id+".htm" ) );
}
catch (MalformedURLException ex)
{}
return obj;
}
protected static Image createPropertyImage( String id, int pos )
{
// create an example image
Image img = KyeroUtils.getFactory().createImagesTypeImage();
img.setId( pos );
try
{
img.setUrl( new URL( "http://website.com/property/" + id + "/image_" + pos + ".jpg" ) );
}
catch (MalformedURLException ex)
{}
img.setTitle( KyeroUtils.getFactory().createLangType() );
img.getTitle().setAr( "Arabic image title" );
img.getTitle().setBg( "Bulgarian image title" );
img.getTitle().setCa( "Catalan image title" );
img.getTitle().setCs( "Czech image title" );
img.getTitle().setDa( "Danish image title" );
img.getTitle().setDe( "German image title" );
img.getTitle().setEl( "Greek image title" );
img.getTitle().setEn( "English image title" );
img.getTitle().setEs( "Spanish image title" );
img.getTitle().setEt( "Estonian image title" );
img.getTitle().setFa( "Farsi image title" );
img.getTitle().setFi( "Finnish image title" );
img.getTitle().setFr( "French image title" );
img.getTitle().setHe( "Hebrew image title" );
img.getTitle().setHi( "Hindi image title" );
img.getTitle().setHu( "Hungarian image title" );
img.getTitle().setId( "Indonesian image title" );
img.getTitle().setIt( "Italian image title" );
img.getTitle().setJa( "Japanese image title" );
img.getTitle().setKo( "Korean image title" );
img.getTitle().setLt( "Lithuanian image title" );
img.getTitle().setLv( "Latvian image title" );
img.getTitle().setNl( "Dutch image title" );
img.getTitle().setNo( "Norwegian image title" );
img.getTitle().setPl( "Polish image title" );
img.getTitle().setPt( "Portuguese image title" );
img.getTitle().setRo( "Romanian image title" );
img.getTitle().setRu( "Russian image title" );
img.getTitle().setSk( "Slovak image title" );
img.getTitle().setSl( "Slovenian image title" );
img.getTitle().setSv( "Swedish image title" );
img.getTitle().setTh( "Thai image title" );
img.getTitle().setTr( "Turkish image title" );
img.getTitle().setUk( "Ukranian image title" );
img.getTitle().setVi( "Vietnamese image title" );
img.getTitle().setZh( "Simplified Chinese image title" );
return img;
}
See a full example at KyeroWritingExample.java
.
After a org.openestate.io.kyero.xml.Root
object was created, it can be
converted into a org.openestate.io.kyero.KyeroDocument
with the static
newDocument()
function.
The class org.openestate.io.kyero.KyeroDocument
provides a toXml()
function,
that finally writes the contents of the Root
object as XML into a
java.io.File
, java.io.OutputStream
or java.io.Writer
.
import java.io.File;
import org.openestate.io.kyero.KyeroDocument;
import org.openestate.io.kyero.xml.Root;
protected static void write( Root root, File file )
{
try
{
KyeroDocument doc = KyeroDocument.newDocument( root );
doc.toXml( file );
}
catch (Exception ex)
{
System.err.println( "Can't write document into a file!" );
System.err.println( "> " + ex.getLocalizedMessage(), ex );
System.exit( 1 );
}
}
See a full example at KyeroWritingExample.java
.
OpenEstate-IO-Kyero supports version 3 and 2.1 of the kyero.com format. The
Java classes in the org.openestate.io.kyero.xml
package are bound to version 3
of the kyero.com format.
If a document in kyero.com format is imported in any desired version, a call to
the toObject()
function will automatically update the document to the
latest version 3 before the Java object is created.
In order to write a kyero.com document with an earlier version then 3, you can
call the downgrade()
function on the document before calling the toXml()
function:
import java.io.File;
import org.openestate.io.kyero.KyeroDocument;
import org.openestate.io.kyero.KyeroVersion;
import org.openestate.io.kyero.xml.Root;
protected static void write( Root root, File file, KyeroVersion version )
{
try
{
KyeroDocument doc = KyeroDocument.newDocument( root );
doc.downgrade( version );
doc.toXml( file );
}
catch (Exception ex)
{
System.err.println( "Can't write document into a file!" );
System.err.println( "> " + ex.getLocalizedMessage(), ex );
System.exit( 1 );
}
}