view src/java/de/intevation/mxd/Converter.java @ 121:9f74f4d36822

Set default values and improved logging and exception handling for map reader.
author Raimund Renkert <rrenkert@intevation.de>
date Thu, 16 Jun 2011 18:36:48 +0200
parents 39957898c694
children 707f13cfba74
line wrap: on
line source
package de.intevation.mxd;

import java.io.File;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import java.net.MalformedURLException;

import de.intevation.mxd.reader.IReader;
import de.intevation.mxd.reader.MXDReader;
import de.intevation.mxd.writer.IWriter;
import de.intevation.mxd.writer.MapScriptWriter;
/**
 * The entry point of the MXD converter tool.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class Converter {

    /**
     * The logging is done via Log4j. To configure the logging
     * a file 'log4j.properties' is search in the configuration
     * directory.
     */
    public static final String LOG4J_PROPERTIES = "log4j.properties";

    /**
     *
     * The path of the configuration directory.
     */
    public static final String CONFIG_PATH = System.getProperty("config.dir",
                                                                "conf");


    /**
     * The logger used in this class.
     */
    private static Logger logger;

    static {
        configureLogging();
        logger = Logger.getLogger(Converter.class);
    }


    /**
     * Entrypoint for the application.
     */
    public static void main(String[] args) {
        try{
            String mxdfile = "";
            String mapfile = "";
            String maptemplate = "";
            int ndx = 0;
            if (args.length > 0) {
                if (args[0].equals("-mxd") && args.length >= 2) {
                    mxdfile = args[1];
                    ndx = 2;
                }
            }
            if (args.length >= ndx + 2) {
                if (args[ndx].equals("-map")) {
                    mapfile = args[ndx+1];
                    ndx += 2;
                }
            }
            if (args.length >= ndx + 2) {
                if (args[ndx].equals("-template")) {
                    maptemplate = args[ndx+1];
                    ndx += 2;
                }
            }

            if(mxdfile.equals("")) {
                try {
                    mxdfile = readProperty("mxd");
                }
                catch(Exception e) {
                    e.printStackTrace ();
                }
            }
            if(mapfile.equals("")) {
                try {
                    mapfile = readProperty("map");
                }
                catch(Exception e) {
                    e.printStackTrace ();
                }
            }
            if(maptemplate.equals("")) {
                try {
                    maptemplate = readProperty("map-template");
                }
                catch(Exception e) {
                    e.printStackTrace ();
                }
            }

            IReader reader = new MXDReader();
            IWriter writer = new MapScriptWriter(maptemplate, mapfile);

            reader.init();
            reader.setFilename(mxdfile);
            reader.read();

            writer.write(reader.getMapDocument());
            reader.shutdown();
        }
        catch(Exception e) {
            logger.error(e.getMessage());
        }
    }

    private static String readProperty (String key)
    throws Exception {
        Properties properties = new Properties();
        BufferedInputStream stream =
            new BufferedInputStream(
                new FileInputStream("converter.properties")
            );
        properties.load(stream);
        stream.close();
        return properties.getProperty(key);
    }
    /**
     * Trys to load the Log4j configuration
     * from ${config.dir}/log4j.properties.
     */
    public static final void configureLogging() {
        File configDir = new File(CONFIG_PATH);
        File propFile  = new File(configDir, LOG4J_PROPERTIES);
        if (propFile.isFile() && propFile.canRead()) {
            try {
                PropertyConfigurator.configure(propFile.toURI().toURL());
            }
            catch (MalformedURLException mue) {
                mue.printStackTrace(System.err);
            }
        }
    }

}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)