-
Notifications
You must be signed in to change notification settings - Fork 14
Usage CasaIT
The class org.openestate.io.casa_it.CasaItUtils
provides a static createDocument()
function to read XML data in casa.it format from a java.io.File
, java.io.InputStream
, java.lang.String
or org.w3c.dom.Document
into a org.openestate.io.casa_it.CasaItDocument
.
import java.io.File;
import org.openestate.io.casa_it.CasaItDocument;
import org.openestate.io.casa_it.CasaItUtils;
import org.openestate.io.casa_it.xml.Container;
public class CasaItReadingExample {
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 CasaItDocument
CasaItDocument doc = CasaItUtils.createDocument(new File(args[0]));
// convert CasaItDocument into a Java object
Container container = doc.toObject();
// now we can access the XML content through ordinary Java objects
if (container.getRealestateitems() != null) {
for (Container.Realestateitems.Realestate obj : container.getRealestateitems().getRealestate()) {
// get object nr
String objectNr = obj.getReference();
// get object title
String objectTitle = (obj.getDescription() != null) ?
obj.getDescription().getValue() :
null;
// print object information to console
System.out.println("found object " +
"'" + objectNr + "': " + objectTitle);
}
}
}
}
See a full example at CasaItReadingExample.java
.
The class org.openestate.io.casa_it.xml.Container
is equivalent to a <container>
root element in a casa.it document. For example the following code creates a casa.it document programmatically:
import com.thedeanda.lorem.Lorem;
import com.thedeanda.lorem.LoremIpsum;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.openestate.io.casa_it.CasaItUtils;
import org.openestate.io.casa_it.xml.Container;
import org.openestate.io.casa_it.xml.Container.Realestateitems.Realestate;
import org.openestate.io.casa_it.xml.Container.Realestateitems.Realestate.Images.Advertismentimage;
import org.openestate.io.casa_it.xml.ObjectFactory;
public class CasaItWritingExample {
private final static ObjectFactory FACTORY = CasaItUtils.getFactory();
private final static Lorem RANDOMIZER = new LoremIpsum();
public static void main(String[] args) throws Exception {
// create a Container object with some example data
// this object corresponds to the <container> root element in XML
Container container = FACTORY.createContainer();
container.setRealestateitems(FACTORY.createContainerRealestateitems());
// append some example objects to the Container object
int propertyCount = RandomUtils.nextInt(5, 10);
for (int i = 0; i < propertyCount; i++) {
container.getRealestateitems().getRealestate().add(createRealestate());
}
// now make something useful with the object
}
/**
* Create a {@link Realestate} with some example data.
*
* @return created example object
*/
private static Realestate createRealestate() {
// create an example real estate
Realestate obj = FACTORY.createContainerRealestateitemsRealestate();
obj.setAction(BigInteger.ONE);
obj.setAgencycode(RandomUtils.nextInt(0, 100));
obj.setBathrooms(BigInteger.valueOf(RandomUtils.nextLong(1, 5)));
obj.setCondition(BigInteger.ONE);
obj.setContracttype(BigInteger.ONE);
obj.setFloor(BigInteger.valueOf(RandomUtils.nextLong(1, 5)));
obj.setHasbalcony(RandomUtils.nextBoolean());
obj.setHasterrace(RandomUtils.nextBoolean());
obj.setHeatingtype(BigInteger.ONE);
obj.setHousetypology(BigInteger.ONE);
obj.setOccupationstate(BigInteger.ONE);
obj.setRealestatetype(BigInteger.ONE);
obj.setReference(RandomStringUtils.randomAlphanumeric(1, 5));
obj.setReferenceID(RandomUtils.nextInt(1, 1000));
obj.setRooms(BigInteger.valueOf(RandomUtils.nextLong(1, 10)));
obj.setSize(BigInteger.valueOf(RandomUtils.nextLong(50, 5000)));
obj.setAddress(FACTORY.createContainerRealestateitemsRealestateAddress());
obj.getAddress().setCity(RANDOMIZER.getCity());
obj.getAddress().setNumber(RandomStringUtils.randomNumeric(1, 3));
obj.getAddress().setStreet(RANDOMIZER.getWords(2, 3));
obj.getAddress().setZip(RANDOMIZER.getZipCode());
obj.getAddress().setZone(RANDOMIZER.getCity());
obj.setBox(FACTORY.createContainerRealestateitemsRealestateBox());
obj.getBox().setSize(BigInteger.valueOf(RandomUtils.nextLong(50, 1000)));
obj.getBox().setType(BigInteger.ONE);
obj.setBuilding(FACTORY.createContainerRealestateitemsRealestateBuilding());
obj.getBuilding().setAge(BigInteger.valueOf(RandomUtils.nextLong(5, 50)));
obj.getBuilding().setExpenses(BigDecimal.valueOf(RandomUtils.nextDouble(1000, 1000000)));
obj.getBuilding().setHaslift(RandomUtils.nextBoolean());
obj.getBuilding().setTotalfloors(BigInteger.valueOf(RandomUtils.nextLong(1, 5)));
obj.getBuilding().setUnits(BigInteger.ONE);
obj.setConfiguration(FACTORY.createContainerRealestateitemsRealestateConfiguration());
obj.getConfiguration().setIsaddressvisibleonsite(RandomUtils.nextBoolean());
obj.getConfiguration().setIsmapvisible(RandomUtils.nextBoolean());
obj.getConfiguration().setIsrealestatevisibleonmap(RandomUtils.nextBoolean());
obj.setDescription(FACTORY.createContainerRealestateitemsRealestateDescription());
obj.getDescription().setValue(RANDOMIZER.getWords(3, 15));
obj.setGarden(FACTORY.createContainerRealestateitemsRealestateGarden());
obj.getGarden().setSize(BigInteger.valueOf(RandomUtils.nextLong(10, 100)));
obj.getGarden().setType(BigInteger.ONE);
obj.setGooglemapcoordinate(FACTORY.createContainerRealestateitemsRealestateGooglemapcoordinate());
obj.getGooglemapcoordinate().setLatitude(BigDecimal.valueOf(RandomUtils.nextDouble(0, 90)));
obj.getGooglemapcoordinate().setLatitudemapcenter(BigDecimal.valueOf(RandomUtils.nextDouble(0, 90)));
obj.getGooglemapcoordinate().setLongitude(BigDecimal.valueOf(RandomUtils.nextDouble(0, 90)));
obj.getGooglemapcoordinate().setLongitudemapcenter(BigDecimal.valueOf(RandomUtils.nextDouble(0, 90)));
obj.getGooglemapcoordinate().setMapzoom(10);
obj.setImages(FACTORY.createContainerRealestateitemsRealestateImages());
int imageCount = RandomUtils.nextInt(1, 10);
for (int i = 0; i < imageCount; i++) {
obj.getImages().getAdvertismentimage().add(createAdvertismentimage());
}
obj.setPrice(FACTORY.createContainerRealestateitemsRealestatePrice());
obj.getPrice().setMax(BigDecimal.valueOf(RandomUtils.nextDouble(1000, 1000000)));
obj.getPrice().setMin(BigDecimal.valueOf(RandomUtils.nextDouble(1000, 1000000)));
obj.getPrice().setValue(BigDecimal.valueOf(RandomUtils.nextDouble(1000, 1000000)));
return obj;
}
/**
* Create an {@link Advertismentimage} with some example data.
*
* @return created example object
*/
private static Advertismentimage createAdvertismentimage() {
Advertismentimage img = FACTORY.createContainerRealestateitemsRealestateImagesAdvertismentimage();
img.setImagetype("image/jpeg");
img.setPath("image-" + RandomStringUtils.randomNumeric(3) + ".jpg");
return img;
}
}
See a full example at CasaItWritingExample.java
.
After a org.openestate.io.casa_it.xml.Container
object was created, it can be converted into a org.openestate.io.casa_it.CasaItDocument
with the static newDocument()
function.
The class org.openestate.io.casa_it.CasaItDocument
provides a toXml()
function, that finally writes the contents of the Container
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.casa_it.CasaItDocument;
import org.openestate.io.casa_it.xml.Container;
public class CasaItWritingExample {
private final static boolean PRETTY_PRINT = true;
/**
* Convert a {@link Container} to XML and write it into a {@link File}.
*
* @param container 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(Container container, File file) throws Exception {
CasaItDocument
.newDocument(container)
.toXml(file, PRETTY_PRINT);
}
/**
* Convert a {@link Container} to XML and write it into an {@link OutputStream}.
*
* @param container 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(Container container, OutputStream output) throws Exception {
CasaItDocument
.newDocument(container)
.toXml(output, PRETTY_PRINT);
}
/**
* Convert a {@link Container} to XML and write it into a {@link Writer}.
*
* @param container 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(Container container, Writer output) throws Exception {
CasaItDocument
.newDocument(container)
.toXml(output, PRETTY_PRINT);
}
/**
* Convert a {@link Container} to XML and print the results to the console.
*
* @param container Java object representing the XML root element
* @throws Exception if the document can't be written
*/
private static void writeToConsole(Container container) throws Exception {
System.out.println(
CasaItDocument
.newDocument(container)
.toXmlString(PRETTY_PRINT)
);
}
}
See a full example at CasaItWritingExample.java
.