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

How to use kyero.com format

Reading XML in kyero.com format

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.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 class KyeroReadingExample {
    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 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();

            // get english object description
            String objectInfoEn = (obj.getDesc() != null) ?
                    obj.getDesc().getEn() :
                    null;

            // get german object description
            String objectInfoDe = (obj.getDesc() != null) ?
                    obj.getDesc().getDe() :
                    null;

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

See a full example at KyeroReadingExample.java.

Accessing XML data in kyero.com format

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 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.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 class KyeroWritingExample {
    private final static ObjectFactory FACTORY = KyeroUtils.getFactory();
    private final static Lorem RANDOMIZER = new LoremIpsum();

    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 = FACTORY.createRoot();
        root.setKyero(createKyero());
        root.setAgent(createAgent());

        int propertyCount = RandomUtils.nextInt(3, 10);
        for (int i = 0; i < propertyCount; i++) {
            root.getProperty().add(createProperty());
        }

        // now make something useful with the object
    }

    /**
     * Create an {@link Agent} object with some example data.
     *
     * @return created example object
     */
    private static Agent createAgent() {
        Agent agent = FACTORY.createRootAgent();
        agent.setAddr1(RANDOMIZER.getWords(1, 5));
        agent.setAddr2(RANDOMIZER.getWords(1, 5));
        agent.setCountry(RANDOMIZER.getCountry());
        agent.setEmail(RANDOMIZER.getEmail());
        agent.setFax(RANDOMIZER.getPhone());
        agent.setId(BigInteger.valueOf(RandomUtils.nextLong(1, 10000)));
        agent.setMob(RANDOMIZER.getPhone());
        agent.setName(RANDOMIZER.getName());
        agent.setPostcode(RANDOMIZER.getZipCode());
        agent.setRegion(RANDOMIZER.getStateFull());
        agent.setTel(RANDOMIZER.getPhone());
        agent.setTown(RANDOMIZER.getCity());
        return agent;
    }

    /**
     * Create a {@link KyeroType} object with some example data.
     *
     * @return created example object
     */
    private static KyeroType createKyero() {
        KyeroType kyero = FACTORY.createKyeroType();
        kyero.setFeedGenerated(Calendar.getInstance());
        kyero.setFeedVersion(KyeroUtils.VERSION.toXmlVersion());
        return kyero;
    }

    /**
     * Create a {@link PropertyType} object with some example data.
     *
     * @return created example object
     */
    private static PropertyType createProperty() {
        // create an example real estate
        PropertyType obj = FACTORY.createPropertyType();
        obj.setBaths(BigInteger.valueOf(RandomUtils.nextLong(0, 5)));
        obj.setBeds(BigInteger.valueOf(RandomUtils.nextLong(0, 5)));
        obj.setCountry(RANDOMIZER.getCountry());
        obj.setCurrency(randomValue(CurrencyType.values()));
        obj.setDate(Calendar.getInstance());
        obj.setId(RandomStringUtils.randomAlphanumeric(5));
        obj.setLeasehold(RandomUtils.nextBoolean());
        obj.setLocationDetail(RANDOMIZER.getWords(2, 10));
        obj.setNewBuild(RandomUtils.nextBoolean());
        obj.setNotes(RANDOMIZER.getWords(10, 50));
        obj.setPartOwnership(RandomUtils.nextBoolean());
        obj.setPool(RandomUtils.nextBoolean());
        obj.setPrice(RandomUtils.nextLong(10000, 9999999));
        obj.setPriceFreq(randomValue(PriceFreqType.values()));
        obj.setProvince(RANDOMIZER.getStateFull());
        obj.setRef(RandomStringUtils.randomAlphanumeric(5));
        obj.setTown(RANDOMIZER.getCity());
        obj.setType(RANDOMIZER.getWords(1));

        obj.setDesc(FACTORY.createLangType());
        obj.getDesc().setCa(RANDOMIZER.getWords(10, 50));
        obj.getDesc().setDa(RANDOMIZER.getWords(10, 50));
        obj.getDesc().setDe(RANDOMIZER.getWords(10, 50));
        obj.getDesc().setEn(RANDOMIZER.getWords(10, 50));
        obj.getDesc().setEs(RANDOMIZER.getWords(10, 50));
        obj.getDesc().setFi(RANDOMIZER.getWords(10, 50));
        obj.getDesc().setFr(RANDOMIZER.getWords(10, 50));
        obj.getDesc().setIt(RANDOMIZER.getWords(10, 50));
        obj.getDesc().setNl(RANDOMIZER.getWords(10, 50));
        obj.getDesc().setNo(RANDOMIZER.getWords(10, 50));
        obj.getDesc().setPt(RANDOMIZER.getWords(10, 50));
        obj.getDesc().setRu(RANDOMIZER.getWords(10, 50));
        obj.getDesc().setSv(RANDOMIZER.getWords(10, 50));

        obj.setEnergyRating(FACTORY.createEnergyRatingType());
        obj.getEnergyRating().setConsumption(randomValue(EnergyRatingMarkType.values()));
        obj.getEnergyRating().setEmissions(randomValue(EnergyRatingMarkType.values()));

        obj.setFeatures(FACTORY.createFeaturesType());
        int featureCount = RandomUtils.nextInt(1, 10);
        for (int i = 0; i < featureCount; i++) {
            obj.getFeatures().getFeature().add(RANDOMIZER.getWords(1, 5));
        }

        obj.setImages(FACTORY.createImagesType());
        int imageCount = RandomUtils.nextInt(1, 10);
        for (int i = 0; i < imageCount; i++) {
            obj.getImages().getImage().add(createPropertyImage(i));
        }

        obj.setLocation(FACTORY.createGpsLocationType());
        obj.getLocation().setLatitude(BigDecimal.valueOf(RandomUtils.nextDouble(0, 90)));
        obj.getLocation().setLongitude(BigDecimal.valueOf(RandomUtils.nextDouble(0, 90)));

        obj.setSurfaceArea(FACTORY.createSurfaceType());
        obj.getSurfaceArea().setBuilt(BigInteger.valueOf(RandomUtils.nextLong(50, 250)));
        obj.getSurfaceArea().setPlot(BigInteger.valueOf(RandomUtils.nextLong(100, 1500)));

        obj.setUrl(FACTORY.createUrlType());
        try {
            obj.getUrl().setCa(new URI("https://www.example.com/catalan/" + obj.getId() + ".html"));
            obj.getUrl().setDa(new URI("https://www.example.com/danish/" + obj.getId() + ".html"));
            obj.getUrl().setDe(new URI("https://www.example.com/german/" + obj.getId() + ".html"));
            obj.getUrl().setEn(new URI("https://www.example.com/english/" + obj.getId() + ".html"));
            obj.getUrl().setEs(new URI("https://www.example.com/spanish/" + obj.getId() + ".html"));
            obj.getUrl().setFi(new URI("https://www.example.com/finnish/" + obj.getId() + ".html"));
            obj.getUrl().setFr(new URI("https://www.example.com/french/" + obj.getId() + ".html"));
            obj.getUrl().setIt(new URI("https://www.example.com/italian/" + obj.getId() + ".html"));
            obj.getUrl().setNl(new URI("https://www.example.com/dutch/" + obj.getId() + ".html"));
            obj.getUrl().setNo(new URI("https://www.example.com/norwegian/" + obj.getId() + ".html"));
            obj.getUrl().setPt(new URI("https://www.example.com/portuguese/" + obj.getId() + ".html"));
            obj.getUrl().setRu(new URI("https://www.example.com/russian/" + obj.getId() + ".html"));
            obj.getUrl().setSv(new URI("https://www.example.com/swedish/" + obj.getId() + ".html"));
        } catch (Exception ex) {
        }

        return obj;
    }

    /**
     * Create an {@link Image} object with some example data.
     *
     * @param pos index position within the property images
     * @return created example object
     */
    private static Image createPropertyImage(int pos) {
        // create an example image
        Image img = FACTORY.createImagesTypeImage();
        img.setId(pos);
        try {
            img.setUrl(new URI("https://www.example.com/image-" + pos + ".jpg"));
        } catch (Exception ex) {
        }
        return img;
    }

    /**
     * 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 KyeroWritingExample.java.

Writing XML in kyero.com format

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 java.io.OutputStream;
import java.io.Writer;
import org.openestate.io.kyero.KyeroDocument;
import org.openestate.io.kyero.xml.Root;

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

    /**
     * Convert a {@link Root} to XML and write it into a {@link File}.
     *
     * @param root 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(Root root, File file) throws Exception {
        KyeroDocument
                .newDocument(root)
                .toXml(file, PRETTY_PRINT);
    }

    /**
     * Convert a {@link Root} to XML and write it into an {@link OutputStream}.
     *
     * @param root   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(Root root, OutputStream output) throws Exception {
        KyeroDocument
                .newDocument(root)
                .toXml(output, PRETTY_PRINT);
    }

    /**
     * Convert a {@link Root} to XML and write it into a {@link Writer}.
     *
     * @param root   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(Root root, Writer output) throws Exception {
        KyeroDocument
                .newDocument(root)
                .toXml(output, PRETTY_PRINT);
    }

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

See a full example at KyeroWritingExample.java.

Migrating between different kyero.com versions

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;

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

    /**
     * Downgrade a {@link Root} element to an earlier version
     * and write the resulting XML into a {@link File}.
     *
     * @param root    the document to write
     * @param file    the file, where the document is written to
     * @param version the XML version to write
     * @throws Exception if the document can't be written
     */
    private static void write(Root root, File file, KyeroVersion version) throws Exception {
        KyeroDocument doc = KyeroDocument.newDocument(root);
        doc.downgrade(version);
        doc.toXml(file, PRETTY_PRINT);
    }
}