-
Notifications
You must be signed in to change notification settings - Fork 16
Usage Trovit
The class org.openestate.io.trovit.TrovitUtils
provides a static
createDocument()
function to read XML data in trovit.com format from a
java.io.File
, java.io.InputStream
, java.lang.String
or
org.w3c.dom.Document
into a org.openestate.io.trovit.TrovitDocument
.
import java.io.File;
import org.apache.commons.lang3.StringUtils;
import org.openestate.io.trovit.TrovitDocument;
import org.openestate.io.trovit.TrovitUtils;
import org.openestate.io.trovit.xml.Ad;
import org.openestate.io.trovit.xml.Trovit;
public static void main( String[] args )
{
if (args.length<1)
{
System.err.println( "No file was specified!" );
System.exit( 1 );
}
// read file into a TrovitDocument
TrovitDocument doc = TrovitUtils.createDocument( new File( args[0] ) );
// convert TrovitDocument into a Java object
Trovit trovit = doc.toObject();
// now we can access the XML content through ordinary Java objects
for (Ad ad : trovit.getAd())
{
// get object nr
String objectNr = StringUtils.trimToNull( ad.getId() );
if (objectNr==null) objectNr = "???";
// get object title
String objectTitle = StringUtils.trimToNull( ad.getTitle() );
if (objectTitle==null) objectTitle = "???";
// print object informations to console
LOGGER.info( "> found object '" + objectNr + "' "
+ "with title '" + objectTitle + "'" );
}
}
See a full example at TrovitReadingExample.java
.
The class org.openestate.io.trovit.xml.Trovit
is equivalent to a <trovit>
root element in a trovit.com document. For example the following code creates
a trovit.com document programmatically:
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.trovit.TrovitDocument;
import org.openestate.io.trovit.TrovitUtils;
import org.openestate.io.trovit.xml.Ad;
import org.openestate.io.trovit.xml.Picture;
import org.openestate.io.trovit.xml.Trovit;
import org.openestate.io.trovit.xml.TypeNew;
import org.openestate.io.trovit.xml.types.Action;
import org.openestate.io.trovit.xml.types.IntBool;
import org.openestate.io.trovit.xml.types.PriceInterval;
import org.openestate.io.trovit.xml.types.Unit;
public static void main( String[] args )
{
// create a Trovit object with some example data
// this object corresponds to the <trovit> element in XML
Trovit trovit = TrovitUtils.getFactory().createTrovit();
// append some example ads to the transfer
trovit.getAd().add( createAd() );
trovit.getAd().add( createAd() );
trovit.getAd().add( createAd() );
// now make something useful with the object
}
protected static Ad createAd()
{
// create an example real estate
Ad ad = TrovitUtils.getFactory().createAd();
ad.setAddress( "object address" );
ad.setAgency( "name of the agency" );
ad.setBathrooms( RandomUtils.nextDouble( 0, 5 ) );
ad.setCity( "name of the city" );
ad.setCityArea( "name of the district" );
ad.setCondition( "some notes about the condition" );
ad.setContent( "some more descriptions" );
ad.setDate( Calendar.getInstance() );
ad.setExpirationDate( Calendar.getInstance() );
ad.setFloorNumber( "number of floors" );
ad.setForeclosure( "notes about foreclosure" );
ad.setId( RandomStringUtils.randomAlphanumeric( 5 ) );
ad.setIsFurnished( new IntBool( RandomUtils.nextInt( 0, 2 )==1 ) );
ad.setIsNew( TypeNew.NEW );
ad.setLatitude( RandomUtils.nextDouble( 0, 90 ) );
ad.setLongitude( RandomUtils.nextDouble( 0, 90 ) );
ad.setMlsDatabase( "notes about mls database" );
ad.setOrientation( "notes about orientation" );
ad.setParking( new IntBool( RandomUtils.nextInt( 0, 2 )==1 ) );
ad.setPlotArea( RandomUtils.nextInt( 100, 5000 ) );
ad.setPostcode( "postcode" );
ad.setPropertyType( "notes about the property type" );
ad.setRegion( "notes about the region" );
ad.setRooms( RandomUtils.nextDouble( 1, 10 ) );
ad.setTime( Calendar.getInstance() );
ad.setTitle( "title of the object" );
ad.setType( Action.FOR_RENT );
ad.setUrl( "http://mywebsite.org/" );
ad.setVirtualTour( "notes about virtual tour" );
ad.setYear( RandomUtils.nextInt( 1990, 2010 ) );
ad.setFloorArea( TrovitUtils.getFactory().createFloorArea() );
ad.getFloorArea().setUnit( Unit.METERS );
ad.getFloorArea().setValue( RandomUtils.nextInt( 10, 10000 ) );
ad.setPictures( TrovitUtils.getFactory().createPictures() );
ad.getPictures().getPicture().add( createPicture() );
ad.getPictures().getPicture().add( createPicture() );
ad.getPictures().getPicture().add( createPicture() );
ad.setPrice( TrovitUtils.getFactory().createPrice() );
ad.getPrice().setPeriod( PriceInterval.MONTHLY );
ad.getPrice().setValue( RandomUtils.nextDouble( 100, 2000 ) );
return ad;
}
protected static Picture createPicture()
{
Picture pic = TrovitUtils.getFactory().createPicture();
pic.setPictureTitle( "some descriptive title" );
pic.setPictureUrl( "http://mywebsite.org/image" + RandomStringUtils.randomNumeric( 5 ) + ".jpg" );
return pic;
}
See a full example at TrovitWritingExample.java
.
After a org.openestate.io.trovit.xml.Trovit
object was created, it can be
converted into a org.openestate.io.trovit.TrovitDocument
with the static
newDocument()
function.
The class org.openestate.io.trovit.TrovitDocument
provides a toXml()
function, that finally writes the contents of the Trovit
object as XML into a
java.io.File
, java.io.OutputStream
or java.io.Writer
.
protected static void write( Trovit trovit, File file )
{
try
{
TrovitDocument doc = TrovitDocument.newDocument( trovit );
doc.toXml( file );
}
catch (Exception ex)
{
LOGGER.error( "Can't write document into a file!" );
LOGGER.error( "> " + ex.getLocalizedMessage(), ex );
System.exit( 1 );
}
}
See a full example at TrovitWritingExample.java
.