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

How to use wohnen-in-suedtirol.it format

Reading XML in wohnen-in-suedtirol.it format

The class org.openestate.io.wis_it.WisItUtils provides a static createDocument() function to read XML data in wohnen-in-suedtirol.it format from a java.io.File, java.io.InputStream, java.lang.String or org.w3c.dom.Document into a org.openestate.io.wis_it.WisItDocument.

import java.io.File;
import org.openestate.io.wis_it.WisItDocument;
import org.openestate.io.wis_it.WisItUtils;
import org.openestate.io.wis_it.xml.ObjectType;
import org.openestate.io.wis_it.xml.WIS;

public class WisItReadingExample {
    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 WisItDocument
        WisItDocument doc = WisItUtils.createDocument(new File(args[0]));

        // convert WisItDocument into a Java object
        WIS wis = doc.toObject();

        // now we can access the XML content through ordinary Java objects
        if (wis.getOBJEKTE() != null) {
            for (ObjectType obj : wis.getOBJEKTE().getOBJEKT()) {
                // get object nr
                String objectNr = obj.getID();

                // get german object description
                String objectInfoDe = obj.getINFODE();

                // get italian object description
                String objectInfoIt = obj.getINFOIT();

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

See a full example at WisItReadingExample.java.

Accessing XML data in wohnen-in-suedtirol.it format

The class org.openestate.io.wis_it.xml.WIS is equivalent to a <WIS> root element in a wohnen-in-suedtirol.it document. For example the following code creates a wohnen-in-suedtirol.it document programmatically:

import com.thedeanda.lorem.Lorem;
import com.thedeanda.lorem.LoremIpsum;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Calendar;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.openestate.io.wis_it.WisItUtils;
import org.openestate.io.wis_it.xml.AreaType;
import org.openestate.io.wis_it.xml.ConditionType;
import org.openestate.io.wis_it.xml.EnergyStandard;
import org.openestate.io.wis_it.xml.HeatingType;
import org.openestate.io.wis_it.xml.MarketingType;
import org.openestate.io.wis_it.xml.ObjectFactory;
import org.openestate.io.wis_it.xml.ObjectType;
import org.openestate.io.wis_it.xml.PropertyType;
import org.openestate.io.wis_it.xml.WIS;

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

    public static void main(String[] args) {
        // create a WIS object with some example data
        // this object corresponds to the <WIS> element in XML
        WIS wis = FACTORY.createWIS();
        wis.setBENUTZER(FACTORY.createWISBENUTZER());
        wis.setOBJEKTE(FACTORY.createWISOBJEKTE());

        // append some example ads to the transfer
        int objectCount = RandomUtils.nextInt(3, 10);
        for (int i = 0; i < objectCount; i++) {
            wis.getOBJEKTE().getOBJEKT().add(createOBJEKT());
        }
        wis.getOBJEKTE().setANZAHL(BigInteger.valueOf(objectCount));

        // now make something useful with the object
    }

    /**
     * Create an {@link ObjectType} with some example data.
     *
     * @return created example object
     */
    private static ObjectType createOBJEKT() {
        // create an example real estate
        ObjectType obj = FACTORY.createObjectType();
        obj.setABSTELLPLATZ(RandomUtils.nextBoolean());
        obj.setAUFANFRAGE(RandomUtils.nextBoolean());
        obj.setAUFZUG(RandomUtils.nextBoolean());
        obj.setBALKON(RandomUtils.nextBoolean());
        obj.setBAUJAHR(String.valueOf(RandomUtils.nextInt(1900, 2015)));
        obj.setDACHBODEN(RandomUtils.nextBoolean());
        obj.setFLAECHEART(randomValue(AreaType.values()));
        obj.setFOERDERBAR(RandomUtils.nextBoolean());
        obj.setFRAKTION(RANDOMIZER.getWords(1, 5));
        obj.setGARAGE(RandomUtils.nextBoolean());
        obj.setGRUENFLAECHE(RandomUtils.nextBoolean());
        obj.setGUELTIGBIS(Calendar.getInstance());
        obj.setHEIZUNG(randomValue(HeatingType.values()));
        obj.setID(RandomStringUtils.randomAlphanumeric(5));
        obj.setIMMOBILIENART(randomValue(PropertyType.values()));
        obj.setINFODE(RANDOMIZER.getWords(10, 50));
        obj.setINFOIT(RANDOMIZER.getWords(10, 50));
        obj.setKELLER(RandomUtils.nextBoolean());
        obj.setKLIMAHAUS(randomValue(EnergyStandard.values()));
        obj.setKONVENTIONIERT(RandomUtils.nextBoolean());
        obj.setKUBATUR(BigDecimal.valueOf(RandomUtils.nextDouble(100, 1000)));
        obj.setLOESCHEN(RandomUtils.nextBoolean());
        obj.setLOGGIA(RandomUtils.nextBoolean());
        obj.setMIETEKAUF(randomValue(MarketingType.values()));
        obj.setNUTZFLAECHE(BigDecimal.valueOf(RandomUtils.nextDouble(100, 1000)));
        obj.setORT(RANDOMIZER.getCity());
        obj.setPREIS(BigDecimal.valueOf(RandomUtils.nextDouble(300, 3000)));
        obj.setSTOCKWERK(BigInteger.valueOf(RandomUtils.nextInt(1, 5)));
        obj.setSTOCKWERKE(BigInteger.valueOf(RandomUtils.nextInt(1, 10)));
        obj.setTERRASSE(RandomUtils.nextBoolean());
        obj.setUEBERGABEZEITPUNKT(RANDOMIZER.getWords(1, 10));
        obj.setZIMMER(BigInteger.valueOf(RandomUtils.nextInt(1, 5)));
        obj.setZUSTAND(randomValue(ConditionType.values()));

        obj.setBILD1(obj.getID() + "-01.jpg");
        obj.setBILD2(obj.getID() + "-02.jpg");
        obj.setBILD3(obj.getID() + "-03.jpg");
        obj.setBILD4(obj.getID() + "-04.jpg");
        obj.setBILD5(obj.getID() + "-05.jpg");
        obj.setBILD6(obj.getID() + "-06.jpg");
        obj.setBILD7(obj.getID() + "-07.jpg");
        obj.setBILD8(obj.getID() + "-08.jpg");
        obj.setBILD9(obj.getID() + "-09.jpg");
        obj.setBILD10(obj.getID() + "-10.jpg");

        obj.setDOWNLOAD1(obj.getID() + "-01.pdf");
        obj.setDOWNLOAD2(obj.getID() + "-02.pdf");
        obj.setDOWNLOAD3(obj.getID() + "-03.pdf");

        return obj;
    }

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

Writing XML in wohnen-in-suedtirol.it format

After a org.openestate.io.wis_it.xml.WIS object was created, it can be converted into a org.openestate.io.wis_it.WisItDocument with the static newDocument() function.

The class org.openestate.io.wis_it.WisItDocument provides a toXml() function, that finally writes the contents of the WIS 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.wis_it.WisItDocument;
import org.openestate.io.wis_it.xml.WIS;

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

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

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

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

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

See a full example at WisItWritingExample.java.