view src/java/de/intevation/mxd/Converter.java @ 181:0bde090506f9

Added comments.
author Raimund Renkert <rrenkert@intevation.de>
date Mon, 11 Jul 2011 14:28:38 +0200
parents f4eb506499f5
children df4e0946ef02
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 jargs.gnu.CmdLineParser;

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:raimund.renkert@intevation.de">Raimund Renkert</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) {
        CmdLineParser parser = new CmdLineParser();
        CmdLineParser.Option mxd = parser.addStringOption('m', "mxd");
        CmdLineParser.Option map = parser.addStringOption('a', "map");
        CmdLineParser.Option temp = parser.addStringOption('t', "template");

        try{
            parser.parse(args);
        }
        catch(CmdLineParser.OptionException oe) {
            logger.warn("Invalid options.");
            printUsage();
            System.exit(1);
        }

        try{
            String mxdfile = "";
            String mapfile = "";
            String maptemplate = "";

            mxdfile = (String)parser.getOptionValue(mxd);
            mapfile = (String)parser.getOptionValue(map);
            maptemplate = (String)parser.getOptionValue(temp);

            if(mxdfile == null) {
                try {
                    mxdfile = readProperty("mxd");
                }
                catch(Exception e) {
                    e.printStackTrace ();
                }
            }
            if(mapfile == null) {
                try {
                    mapfile = readProperty("map");
                }
                catch(Exception e) {
                    e.printStackTrace ();
                }
            }
            if(maptemplate == null) {
                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());
            e.printStackTrace();
        }
    }

    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);
            }
        }
    }


    private static void printUsage() {
        System.out.println("Available parameters:[{-m,--mxd} path/to/mxd]\n" +
                           "\t\t[{-a,--map} path/to/mapfile]\n" +
                           "\t\t[{-t,--template} path/to/template]");
    }
}
// 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)