-
Notifications
You must be signed in to change notification settings - Fork 16
Usage DaftIE
The class org.openestate.io.daft_ie.DaftIeUtils
provides a static createDocument()
function to read XML data in daft.ie format from a java.io.File
, java.io.InputStream
, java.lang.String
or org.w3c.dom.Document
into a org.openestate.io.daft_ie.DaftIeDocument
.
import java.io.File;
import org.openestate.io.daft_ie.DaftIeDocument;
import org.openestate.io.daft_ie.DaftIeUtils;
import org.openestate.io.daft_ie.xml.Daft;
import org.openestate.io.daft_ie.xml.OverseasRentalAdType;
import org.openestate.io.daft_ie.xml.OverseasSaleAdType;
public class DaftIeReadingExample {
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 DaftIeDocument
DaftIeDocument doc = DaftIeUtils.createDocument(new File(args[0]));
// convert DaftIeDocument into a Java object
Daft daft = doc.toObject();
// now we can access the XML content through ordinary Java objects
// process overseas rental
if (daft.getOverseasRental() != null) {
for (OverseasRentalAdType ad : daft.getOverseasRental().getOverseasRentalAd()) {
// get object nr
String objectNr = ad.getExternalId();
// get object description
String objectInfo = ad.getDescription();
// print object information to console
System.out.println("found object "
+ "'" + objectNr + "' for rent: " + objectInfo);
}
}
// process overseas sales
if (daft.getOverseasSales() != null) {
for (OverseasSaleAdType ad : daft.getOverseasSales().getOverseasSaleAd()) {
// get object nr
String objectNr = ad.getExternalId();
// get object description
String objectInfo = ad.getDescription();
// print object information to console
System.out.println("found object "
+ "'" + objectNr + "' for sale: " + objectInfo);
}
}
}
}
See a full example at DaftIeReadingExample.java
.
The class org.openestate.io.daft_ie.xml.Daft
is equivalent to a <daft>
root element in a daft.ie document. For example the following code creates a daft.ie 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.daft_ie.DaftIeUtils;
import org.openestate.io.daft_ie.xml.CommercialType;
import org.openestate.io.daft_ie.xml.Daft;
import org.openestate.io.daft_ie.xml.HouseType;
import org.openestate.io.daft_ie.xml.ObjectFactory;
import org.openestate.io.daft_ie.xml.OverseasRentalAdType;
import org.openestate.io.daft_ie.xml.OverseasSaleAdType;
import org.openestate.io.daft_ie.xml.PropertyType;
public class DaftIeWritingExample {
private final static ObjectFactory FACTORY = DaftIeUtils.getFactory();
private final static Lorem RANDOMIZER = new LoremIpsum();
public static void main(String[] args) throws Exception {
// create a Daft object with some example data
// this object corresponds to the <daft> root element in XML
Daft daft = FACTORY.createDaft();
// append some example objects for rent to the Daft object
daft.setOverseasRental(FACTORY.createDaftOverseasRental());
int rentalCount = RandomUtils.nextInt(5, 10);
for (int i = 0; i < rentalCount; i++) {
daft.getOverseasRental().getOverseasRentalAd().add(createAdForRent());
}
// append some example objects for sale to the Daft object
daft.setOverseasSales(FACTORY.createDaftOverseasSales());
int saleCount = RandomUtils.nextInt(5, 10);
for (int i = 0; i < saleCount; i++) {
daft.getOverseasSales().getOverseasSaleAd().add(createAdForSale());
}
// now make something useful with the object
}
/**
* Create an {@link OverseasRentalAdType} with some example data.
*
* @return created example object
*/
private static OverseasRentalAdType createAdForRent() {
// create an example real estate for rent
OverseasRentalAdType ad = FACTORY.createOverseasRentalAdType();
ad.setAddress(RANDOMIZER.getWords(1, 3));
ad.setAgentId(RandomStringUtils.randomAlphanumeric(1, 5));
ad.setArea(RANDOMIZER.getCity());
ad.setAvailableFrom(Calendar.getInstance());
ad.setBathroomNumber(BigInteger.valueOf(RandomUtils.nextInt(1, 5)));
ad.setBedroomNumber(BigInteger.valueOf(RandomUtils.nextInt(1, 5)));
ad.setCableTelevision(RandomUtils.nextBoolean());
ad.setCcEmail(RANDOMIZER.getEmail());
ad.setCommercialType(randomValue(CommercialType.values()));
ad.setContactName(RANDOMIZER.getName());
ad.setCountry(randomValue(new String[]{"DE", "UK", "ES"}));
ad.setDescription(RANDOMIZER.getWords(10, 50));
ad.setDishwasher(RandomUtils.nextBoolean());
ad.setDoubleBeds(BigInteger.valueOf(RandomUtils.nextInt(1, 5)));
ad.setDryer(RandomUtils.nextBoolean());
ad.setExternalId(RandomStringUtils.randomAlphanumeric(1, 4));
ad.setFurnished(randomValue(OverseasRentalAdType.Furnished.values()));
ad.setHouseType(randomValue(HouseType.values()));
ad.setLease(BigInteger.valueOf(RandomUtils.nextInt(100, 1000)));
ad.setMainEmail(RANDOMIZER.getEmail());
ad.setMicrowave(RandomUtils.nextBoolean());
ad.setNumberPeople(BigInteger.valueOf(RandomUtils.nextInt(1, 5)));
ad.setPhone1(RANDOMIZER.getPhone());
ad.setPhone2(RANDOMIZER.getPhone());
ad.setPhoneInfo(RANDOMIZER.getWords(2, 10));
ad.setPropertyType(randomValue(PropertyType.values()));
ad.setRegion(RANDOMIZER.getCity());
ad.setRent(BigInteger.valueOf(RandomUtils.nextInt(100, 1000)));
ad.setRentCollectionPeriod(OverseasRentalAdType.RentPeriod.MONTHLY);
ad.setSingleBeds(BigInteger.valueOf(RandomUtils.nextInt(1, 5)));
ad.setTwinBeds(BigInteger.valueOf(RandomUtils.nextInt(1, 5)));
ad.setWashingMachine(RandomUtils.nextBoolean());
// add some features
ad.setFeatures(FACTORY.createFeaturesType());
int featureCount = RandomUtils.nextInt(3, 10);
for (int i = 0; i < featureCount; i++) {
ad.getFeatures().getFeature().add(RANDOMIZER.getWords(1, 4));
}
// add some photos
ad.setPhotos(FACTORY.createPhotosType());
int photoCount = RandomUtils.nextInt(5, 10);
for (int i = 0; i < photoCount; i++) {
try {
ad.getPhotos().getPhoto().add(new URI("https://www.example.com/image-" + i + ".jpg"));
} catch (Exception ex) {
}
}
return ad;
}
/**
* Create an {@link OverseasRentalAdType} with some example data.
*
* @return created example object
*/
private static OverseasSaleAdType createAdForSale() {
// create an example real estate for sale
OverseasSaleAdType ad = FACTORY.createOverseasSaleAdType();
ad.setAcres(BigDecimal.valueOf(RandomUtils.nextDouble(10, 1000)));
ad.setAddress(RANDOMIZER.getWords(1, 3));
ad.setAgentId(RandomStringUtils.randomAlphanumeric(1, 5));
ad.setArea(RANDOMIZER.getCity());
ad.setBathroomNumber(BigInteger.valueOf(RandomUtils.nextInt(1, 5)));
ad.setBedroomNumber(BigInteger.valueOf(RandomUtils.nextInt(1, 5)));
ad.setCcEmail(RANDOMIZER.getEmail());
ad.setCo2Rating(RANDOMIZER.getWords(1, 3));
ad.setCommercialType(CommercialType.SHOP);
ad.setContactName(RANDOMIZER.getName());
ad.setCountry(randomValue(new String[]{"DE", "UK", "ES"}));
ad.setDescription(RANDOMIZER.getWords(10, 50));
ad.setDirections(RANDOMIZER.getWords(3, 10));
ad.setEnergyRating(RANDOMIZER.getWords(3, 10));
ad.setExternalId(RandomStringUtils.randomAlphanumeric(1, 4));
ad.setHouseType(randomValue(HouseType.values()));
ad.setIsNewDevelopment(RandomUtils.nextBoolean());
ad.setMainEmail(RANDOMIZER.getEmail());
ad.setNewDevelopmentAvailability(RANDOMIZER.getWords(3, 10));
ad.setPhone1(RANDOMIZER.getPhone());
ad.setPhone2(RANDOMIZER.getPhone());
ad.setPhoneInfo(RANDOMIZER.getWords(2, 10));
ad.setPrice(BigInteger.valueOf(RandomUtils.nextInt(100, 1000000)));
ad.setPriceType(randomValue(OverseasSaleAdType.PriceType.values()));
ad.setPropertyStatus(randomValue(OverseasSaleAdType.PropertyStatus.values()));
ad.setPropertyType(randomValue(PropertyType.values()));
ad.setRegion(RANDOMIZER.getCity());
ad.setSquareMetres(BigDecimal.valueOf(RandomUtils.nextDouble(10, 1000)));
ad.setUnitsAvailable(BigInteger.valueOf(RandomUtils.nextInt(1, 50)));
ad.setViewingDetails(RANDOMIZER.getWords(3, 10));
// add some features
ad.setFeatures(FACTORY.createFeaturesType());
int featureCount = RandomUtils.nextInt(3, 10);
for (int i = 0; i < featureCount; i++) {
ad.getFeatures().getFeature().add(RANDOMIZER.getWords(1, 4));
}
// add some pdf documents
ad.setPdfs(FACTORY.createPdfsType());
int pdfCount = RandomUtils.nextInt(1, 3);
for (int i = 0; i < pdfCount; i++) {
try {
ad.getPdfs().getPdf().add(new URI("https://www.example.com/document-" + i + ".pdf"));
} catch (Exception ex) {
}
}
// add some photos
ad.setPhotos(FACTORY.createPhotosType());
int photoCount = RandomUtils.nextInt(5, 10);
for (int i = 0; i < photoCount; i++) {
try {
ad.getPhotos().getPhoto().add(new URI("https://www.example.com/image-" + i + ".jpg"));
} catch (Exception ex) {
}
}
return ad;
}
/**
* 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 DaftIeWritingExample.java
.
After a org.openestate.io.daft_ie.xml.Daft
object was created, it can be converted into a org.openestate.io.daft_ie.DaftIeDocument
with the static newDocument()
function.
The class org.openestate.io.daft_ie.DaftIeDocument
provides a toXml()
function, that finally writes the contents of the Daft
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.daft_ie.DaftIeDocument;
import org.openestate.io.daft_ie.xml.Daft;
public class DaftIeWritingExample {
private final static boolean PRETTY_PRINT = true;
/**
* Convert a {@link Daft} to XML and write it into a {@link File}.
*
* @param daft 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(Daft daft, File file) throws Exception {
DaftIeDocument
.newDocument(daft)
.toXml(file, PRETTY_PRINT);
}
/**
* Convert a {@link Daft} to XML and write it into an {@link OutputStream}.
*
* @param daft 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(Daft daft, OutputStream output) throws Exception {
DaftIeDocument
.newDocument(daft)
.toXml(output, PRETTY_PRINT);
}
/**
* Convert a {@link Daft} to XML and write it into a {@link Writer}.
*
* @param daft 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(Daft daft, Writer output) throws Exception {
DaftIeDocument
.newDocument(daft)
.toXml(output, PRETTY_PRINT);
}
/**
* Convert a {@link Daft} to XML and print the results to the console.
*
* @param daft Java object representing the XML root element
* @throws Exception if the document can't be written
*/
private static void writeToConsole(Daft daft) throws Exception {
System.out.println(
DaftIeDocument
.newDocument(daft)
.toXmlString(PRETTY_PRINT)
);
}
}
See a full example at DaftIeWritingExample.java
.