rrenkert@243: /* rrenkert@243: * Copyright (c) 2011 by Intevation GmbH, Germany rrenkert@243: * rrenkert@243: * This file is part of MXD2map. rrenkert@243: * rrenkert@243: * This program is free software under the LGPL (>=v2.1) rrenkert@243: * Read the file LICENCE.txt coming with the software for details rrenkert@243: * or visit http://www.gnu.org/licenses/ if it does not exist. rrenkert@243: * rrenkert@243: * MXD2map has been developed on behalf of the rrenkert@243: * Bundesamt fuer Seeschifffahrt und Hydrographie (BSH) in Hamburg rrenkert@243: * by Intevation GmbH. rrenkert@243: * rrenkert@243: * Authors: rrenkert@243: * Raimund Renkert rrenkert@243: * Bjoern Schilberg rrenkert@243: * Stephan Holl rrenkert@243: */ rrenkert@243: rrenkert@33: package de.intevation.mxd; ingo@24: rrenkert@25: import java.io.File; rrenkert@55: import java.io.FileInputStream; rrenkert@55: import java.io.BufferedInputStream; rrenkert@55: import java.util.Properties; rrenkert@25: rrenkert@25: import org.apache.log4j.Logger; rrenkert@25: import org.apache.log4j.PropertyConfigurator; rrenkert@25: raimund@174: import jargs.gnu.CmdLineParser; raimund@174: rrenkert@25: import java.net.MalformedURLException; ingo@24: rrenkert@33: import de.intevation.mxd.reader.IReader; rrenkert@33: import de.intevation.mxd.reader.MXDReader; rrenkert@41: import de.intevation.mxd.writer.IWriter; rrenkert@41: import de.intevation.mxd.writer.MapScriptWriter; ingo@24: /** ingo@24: * The entry point of the MXD converter tool. ingo@24: * rrenkert@180: * @author Raimund Renkert ingo@24: */ ingo@24: public class Converter { ingo@24: rrenkert@25: /** rrenkert@25: * The logging is done via Log4j. To configure the logging rrenkert@25: * a file 'log4j.properties' is search in the configuration rrenkert@25: * directory. rrenkert@25: */ rrenkert@25: public static final String LOG4J_PROPERTIES = "log4j.properties"; rrenkert@25: rrenkert@25: /** rrenkert@25: * rrenkert@25: * The path of the configuration directory. rrenkert@25: */ rrenkert@25: public static final String CONFIG_PATH = System.getProperty("config.dir", rrenkert@25: "conf"); rrenkert@25: rrenkert@25: rrenkert@25: /** rrenkert@25: * The logger used in this class. rrenkert@25: */ rrenkert@25: private static Logger logger; rrenkert@25: rrenkert@25: static { rrenkert@25: configureLogging(); rrenkert@25: logger = Logger.getLogger(Converter.class); rrenkert@25: } rrenkert@25: rrenkert@25: rrenkert@25: /** rrenkert@55: * Entrypoint for the application. rrenkert@55: */ rrenkert@55: public static void main(String[] args) { raimund@174: CmdLineParser parser = new CmdLineParser(); raimund@174: CmdLineParser.Option mxd = parser.addStringOption('m', "mxd"); raimund@174: CmdLineParser.Option map = parser.addStringOption('a', "map"); raimund@174: CmdLineParser.Option temp = parser.addStringOption('t', "template"); raimund@174: raimund@174: try{ raimund@174: parser.parse(args); raimund@174: } raimund@174: catch(CmdLineParser.OptionException oe) { raimund@174: logger.warn("Invalid options."); raimund@174: printUsage(); raimund@174: System.exit(1); raimund@174: } raimund@174: rrenkert@55: try{ rrenkert@55: String mxdfile = ""; rrenkert@55: String mapfile = ""; rrenkert@55: String maptemplate = ""; rrenkert@55: raimund@174: mxdfile = (String)parser.getOptionValue(mxd); raimund@174: mapfile = (String)parser.getOptionValue(map); raimund@174: maptemplate = (String)parser.getOptionValue(temp); raimund@174: raimund@174: if(mxdfile == null) { rrenkert@55: try { rrenkert@55: mxdfile = readProperty("mxd"); rrenkert@55: } rrenkert@55: catch(Exception e) { raimund@255: logger.warn( raimund@255: "No property file found. " + raimund@255: "Please provide a valid property file or append " + raimund@255: "parameters for mxd file, map file and template."); rrenkert@55: } rrenkert@55: } raimund@174: if(mapfile == null) { rrenkert@55: try { rrenkert@55: mapfile = readProperty("map"); rrenkert@55: } rrenkert@55: catch(Exception e) { raimund@255: logger.warn( raimund@255: "No property file found. " + raimund@255: "Please provide a valid property file or append " + raimund@255: "parameters for mxd file, map file and template."); rrenkert@55: } rrenkert@55: } raimund@174: if(maptemplate == null) { rrenkert@55: try { rrenkert@55: maptemplate = readProperty("map-template"); rrenkert@55: } rrenkert@55: catch(Exception e) { raimund@255: logger.warn( raimund@255: "No property file found. " + raimund@255: "Please provide a valid property file or append " + raimund@255: "parameters for mxd file, map file and template."); rrenkert@55: } rrenkert@55: } rrenkert@55: rrenkert@55: IReader reader = new MXDReader(); rrenkert@86: IWriter writer = new MapScriptWriter(maptemplate, mapfile); rrenkert@86: rrenkert@55: reader.init(); rrenkert@55: reader.setFilename(mxdfile); rrenkert@55: reader.read(); rrenkert@55: rrenkert@55: writer.write(reader.getMapDocument()); aheinecke@340: logger.debug("write done"); rrenkert@55: reader.shutdown(); rrenkert@55: } rrenkert@86: catch(Exception e) { raimund@255: logger.debug(e.getMessage()); aheinecke@340: e.printStackTrace(); raimund@255: logger.error( raimund@255: "General error." + raimund@255: " See logging file for more information."); rrenkert@55: } rrenkert@55: } rrenkert@55: raimund@260: raimund@260: /** raimund@260: * Reads a property value from the converter.properties file. raimund@260: * raimund@260: * @param key The property key. raimund@260: * raimund@260: * @return The value. raimund@260: */ rrenkert@55: private static String readProperty (String key) rrenkert@55: throws Exception { rrenkert@55: Properties properties = new Properties(); rrenkert@55: BufferedInputStream stream = rrenkert@55: new BufferedInputStream( rrenkert@55: new FileInputStream("converter.properties") rrenkert@55: ); rrenkert@55: properties.load(stream); rrenkert@55: stream.close(); rrenkert@55: return properties.getProperty(key); rrenkert@55: } raimund@260: raimund@260: rrenkert@55: /** rrenkert@25: * Trys to load the Log4j configuration rrenkert@25: * from ${config.dir}/log4j.properties. rrenkert@25: */ rrenkert@25: public static final void configureLogging() { rrenkert@25: File configDir = new File(CONFIG_PATH); rrenkert@25: File propFile = new File(configDir, LOG4J_PROPERTIES); rrenkert@25: if (propFile.isFile() && propFile.canRead()) { rrenkert@25: try { rrenkert@25: PropertyConfigurator.configure(propFile.toURI().toURL()); rrenkert@25: } rrenkert@25: catch (MalformedURLException mue) { rrenkert@25: mue.printStackTrace(System.err); rrenkert@25: } rrenkert@25: } rrenkert@25: } rrenkert@25: rrenkert@180: raimund@260: /** raimund@260: * Print out usage informations. raimund@260: */ raimund@174: private static void printUsage() { raimund@174: System.out.println("Available parameters:[{-m,--mxd} path/to/mxd]\n" + raimund@174: "\t\t[{-a,--map} path/to/mapfile]\n" + raimund@174: "\t\t[{-t,--template} path/to/template]"); raimund@174: } ingo@24: } ingo@24: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :