view src/java/de/intevation/mxd/Converter.java @ 81:18814ae6d92d

Merged with Stephans commits.
author Raimund Renkert <rrenkert@intevation.de>
date Fri, 27 May 2011 13:25:58 +0200
parents f0c02ff120d6
children e19c5eb43099
line wrap: on
line source
package de.intevation.mxd;

import java.io.IOException;
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();
            reader.init();
            reader.setFilename(mxdfile);
            reader.read();

            IWriter writer = new MapScriptWriter(maptemplate, mapfile);
            writer.write(reader.getMapDocument());
            reader.shutdown();
        }
        catch(IOException e) {
            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);
            }
        }
    }

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