Skip to content
Andreas Rudolph edited this page Aug 16, 2021 · 19 revisions

How to use trovit.com format

Reading XML in trovit.com format

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.openestate.io.trovit.TrovitDocument;
import org.openestate.io.trovit.TrovitUtils;
import org.openestate.io.trovit.xml.AdType;
import org.openestate.io.trovit.xml.Trovit;

public class TrovitReadingExample {
    public static void main(String[] args) throws Exception {
        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 (AdType ad : trovit.getAd()) {
            // get object nr
            String objectNr = ad.getId();

            // get object title
            String objectTitle = ad.getTitle();

            // print object information to console
            System.out.println("found object " +
                    "'" + objectNr + "': " + objectTitle);
        }
    }
}

See a full example at TrovitReadingExample.java.

Accessing XML data in trovit.com format

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 com.thedeanda.lorem.Lorem;
import com.thedeanda.lorem.LoremIpsum;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.util.Calendar;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.openestate.io.trovit.TrovitUtils;
import org.openestate.io.trovit.xml.AdType;
import org.openestate.io.trovit.xml.ObjectFactory;
import org.openestate.io.trovit.xml.PictureType;
import org.openestate.io.trovit.xml.Trovit;
import org.openestate.io.trovit.xml.types.AreaUnitValue;
import org.openestate.io.trovit.xml.types.ForeclosureTypeValue;
import org.openestate.io.trovit.xml.types.OrientationValue;
import org.openestate.io.trovit.xml.types.PricePeriodValue;
import org.openestate.io.trovit.xml.types.TypeValue;

public class TrovitWritingExample {
    private final static ObjectFactory FACTORY = TrovitUtils.getFactory();
    private final static Lorem RANDOMIZER = new LoremIpsum();

    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
        int adCount = RandomUtils.nextInt(3, 10);
        for (int i = 0; i < adCount; i++) {
            trovit.getAd().add(createAd());
        }

        // now make something useful with the object
    }

    /**
     * Create an {@link AdType} with some example data.
     *
     * @return created example object
     */
    private static AdType createAd() {
        // create an example real estate
        AdType ad = FACTORY.createAdType();
        ad.setAddress(RANDOMIZER.getWords(1, 4));
        ad.setAgency(RANDOMIZER.getName());
        ad.setBathrooms(BigDecimal.valueOf(RandomUtils.nextDouble(0, 5)));
        ad.setByOwner(RandomUtils.nextBoolean());
        ad.setCity(RANDOMIZER.getCity());
        ad.setCityArea(RANDOMIZER.getWords(1, 3));
        ad.setCondition(RANDOMIZER.getWords(3, 10));
        ad.setContactEmail(RANDOMIZER.getEmail());
        ad.setContactName(RANDOMIZER.getName());
        ad.setContactTelephone(RANDOMIZER.getPhone());
        ad.setContent(RANDOMIZER.getWords(10, 50));
        ad.setCountry(randomValue(new String[]{"DE", "AT", "CH", "ES"}));
        ad.setDate(Calendar.getInstance());
        ad.setEcoScore(randomValue(new String[]{"A", "B", "C", "D"}));
        ad.setExpirationDate(Calendar.getInstance());
        ad.setFloorNumber(RandomStringUtils.randomNumeric(1));
        ad.setForeclosure(RandomUtils.nextBoolean());
        ad.setForeclosureType(randomValue(ForeclosureTypeValue.values()));
        ad.setId(RandomStringUtils.randomAlphanumeric(5));
        ad.setIsFurnished(RandomUtils.nextBoolean());
        ad.setIsNew(RandomUtils.nextBoolean());
        ad.setIsRentToOwn(RandomUtils.nextBoolean());
        ad.setLatitude(BigDecimal.valueOf(RandomUtils.nextDouble(0, 180) - 90));
        ad.setLongitude(BigDecimal.valueOf(RandomUtils.nextDouble(0, 360) - 180));
        ad.setMlsDatabase(RANDOMIZER.getWords(2, 5));
        ad.setNeighborhood(RANDOMIZER.getWords(10, 30));
        ad.setOrientation(randomValue(OrientationValue.values()));
        ad.setParking(RandomUtils.nextBoolean());
        ad.setPostcode(RANDOMIZER.getZipCode());
        ad.setPropertyType(RANDOMIZER.getWords(1, 5));
        ad.setRegion(RANDOMIZER.getStateFull());
        ad.setRooms(BigDecimal.valueOf(RandomUtils.nextDouble(1, 10)));
        ad.setTitle(RANDOMIZER.getWords(2, 6));
        ad.setType(randomValue(TypeValue.values()));
        ad.setYear(BigInteger.valueOf(RandomUtils.nextInt(1700, 2017)));

        ad.setFloorArea(FACTORY.createFloorAreaType());
        ad.getFloorArea().setUnit(randomValue(AreaUnitValue.values()));
        ad.getFloorArea().setValue(BigInteger.valueOf(RandomUtils.nextInt(10, 10000)));

        ad.setPictures(FACTORY.createAdTypePictures());
        int pictureCount = RandomUtils.nextInt(3, 10);
        for (int i = 0; i < pictureCount; i++) {
            ad.getPictures().getPicture().add(createPicture(i));
        }

        ad.setPlotArea(FACTORY.createPlotAreaType());
        ad.getPlotArea().setUnit(randomValue(AreaUnitValue.values()));
        ad.getPlotArea().setValue(BigInteger.valueOf(RandomUtils.nextInt(10, 10000)));

        ad.setPrice(FACTORY.createPriceType());
        ad.getPrice().setPeriod(randomValue(PricePeriodValue.values()));
        ad.getPrice().setValue(BigDecimal.valueOf(RandomUtils.nextDouble(100, 2000)));

        try {
            ad.setUrl(new URI("https://www.example.com/" + ad.getId()));
            ad.setMobileUrl(new URI("https://mobile.example.com/" + ad.getId()));
            ad.setVirtualTour(new URI("https://tour.example.com/" + ad.getId()));
        } catch (Exception ex) {
        }

        return ad;
    }

    /**
     * Create a {@link PictureType} with some example data.
     *
     * @param pos image position
     * @return created example object
     */
    private static PictureType createPicture(int pos) {
        try {
            PictureType pic = FACTORY.createPictureType();
            pic.setPictureTitle(RANDOMIZER.getWords(2, 5));
            pic.setPictureUrl(new URI("https://www.example.com/image" + pos + ".jpg"));
            pic.setFeatured(pos == 0);
            return pic;
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * Get a random value from an array.
     *
     * @param values array containing values to select from
     * @param <T>    type of contained values
     * @return randomly selected value
     */
    private static <T> T randomValue(T[] values) {
        return (values != null && values.length > 0) ?
                values[RandomUtils.nextInt(0, values.length)] :
                null;
    }
}

See a full example at TrovitWritingExample.java.

Writing XML in trovit.com format

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.

import java.io.File;
import java.io.OutputStream;
import java.io.Writer;
import org.openestate.io.trovit.TrovitDocument;
import org.openestate.io.trovit.xml.Trovit;

public class TrovitWritingExample {
    private final static boolean PRETTY_PRINT = true;

    /**
     * Convert a {@link Trovit} to XML and write it into a {@link File}.
     *
     * @param trovit Java object representing the XML root element
     * @param file   the file, where the document is written to
     * @throws Exception if the document can't be written
     */
    private static void write(Trovit trovit, File file) throws Exception {
        TrovitDocument
                .newDocument(trovit)
                .toXml(file, PRETTY_PRINT);
    }

    /**
     * Convert a {@link Trovit} to XML and write it into an {@link OutputStream}.
     *
     * @param trovit Java object representing the XML root element
     * @param output the stream, where the document is written to
     * @throws Exception if the document can't be written
     */
    private static void write(Trovit trovit, OutputStream output) throws Exception {
        TrovitDocument
                .newDocument(trovit)
                .toXml(output, PRETTY_PRINT);
    }

    /**
     * Convert a {@link Trovit} to XML and write it into a {@link Writer}.
     *
     * @param trovit Java object representing the XML root element
     * @param output the writer, where the document is written to
     * @throws Exception if the document can't be written
     */
    private static void write(Trovit trovit, Writer output) throws Exception {
        TrovitDocument
                .newDocument(trovit)
                .toXml(output, PRETTY_PRINT);
    }

    /**
     * Convert a {@link Trovit} to XML and print the results to the console.
     *
     * @param trovit Java object representing the XML root element
     * @throws Exception if the document can't be written
     */
    private static void writeToConsole(Trovit trovit) throws Exception {
        System.out.println(
            TrovitDocument
                .newDocument(trovit)
                .toXmlString(PRETTY_PRINT)
        );
    }
}

See a full example at TrovitWritingExample.java.