org.openestate.is24.restapi.examples.RandomRealEstateExample.java Source code

Java tutorial

Introduction

Here is the source code for org.openestate.is24.restapi.examples.RandomRealEstateExample.java

Source

/*
 * Copyright 2014-2016 OpenEstate.org.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openestate.is24.restapi.examples;

import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import javax.xml.bind.JAXBException;
import org.apache.commons.io.IOUtils;
import org.openestate.is24.restapi.utils.RandomRealEstateFactory;
import org.openestate.is24.restapi.utils.XmlUtils;
import org.openestate.is24.restapi.xml.realestates.RealEstate;

/**
 * This example illustrates the creation of random real estate data.
 *
 * @author Andreas Rudolph <andy@openindex.de>
 */
public class RandomRealEstateExample {
    /**
     * Main function.
     *
     * @param args
     * command line arguments
     */
    public static void main(String[] args) {
        try {
            final RandomRealEstateFactory factory = new RandomRealEstateFactory();
            final Charset charset = Charset.forName("UTF-8");
            for (RandomRealEstateFactory.Type type : RandomRealEstateFactory.Type.values()) {
                System.out.println("----------------------------------------");
                System.out.println("example for " + type + ":");
                RealEstate realEstate = factory.createRandomObject(type);
                ByteArrayOutputStream output = null;
                try {
                    output = new ByteArrayOutputStream();
                    XmlUtils.writeXml(realEstate, charset.name(), true, output);
                    ByteBuffer buffer = ByteBuffer.wrap(output.toByteArray());
                    String xml = charset.decode(buffer).toString();
                    System.out.println(xml.trim());
                } finally {
                    IOUtils.closeQuietly(output);
                }
            }
        } catch (JAXBException ex) {
            System.err.println("XML error!");
            ex.printStackTrace(System.err);
        }
    }
}