view src/java/de/intevation/mxd/Converter.java @ 307:f9e53dcc7424

Add Read/Write Support for Projections on a per Layer basis
author Andre Heinecke <aheinecke@intevation.de>
date Thu, 06 Sep 2012 18:58:41 +0200
parents 10e0aa283217
children dbfcb0b69a63
line wrap: on
line source
/*
 * Copyright (c) 2011 by Intevation GmbH, Germany <info@intevation.de>
 *
 * This file is part of MXD2map.
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LICENCE.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 *
 * MXD2map has been developed on behalf of the
 * Bundesamt fuer Seeschifffahrt und Hydrographie (BSH) in Hamburg
 * by Intevation GmbH.
 *
 * Authors:
 * Raimund Renkert <raimund.renkert@intevation.de>
 * Bjoern Schilberg <bjoern.schilberg@intevation.de>
 * Stephan Holl <stephan.holl@intevation.de>
 */

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) {
                    logger.warn(
                        "No property file found. " + 
                        "Please provide a valid property file or append " +
                        "parameters for mxd file, map file and template.");
                }
            }
            if(mapfile == null) {
                try {
                    mapfile = readProperty("map");
                }
                catch(Exception e) {
                    logger.warn(
                        "No property file found. " + 
                        "Please provide a valid property file or append " +
                        "parameters for mxd file, map file and template.");
                }
            }
            if(maptemplate == null) {
                try {
                    maptemplate = readProperty("map-template");
                }
                catch(Exception e) {
                    logger.warn(
                        "No property file found. " + 
                        "Please provide a valid property file or append " +
                        "parameters for mxd file, map file and template.");
                }
            }

            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.debug(e.getMessage());
            logger.error(
                "General error." +
                " See logging file for more information.");
        }
    }


    /**
     * Reads a property value from the converter.properties file.
     *
     * @param key The property key.
     *
     * @return The value.
     */
    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);
            }
        }
    }


    /**
     * Print out usage informations.
     */
    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)