# HG changeset patch # User Raimund Renkert # Date 1305562887 -7200 # Node ID f0c02ff120d69bad757d48e404a0a39e53d17d3f # Parent 691864097eb1d8798da4bcc45ca6cc65338e165e Read filenames from properties file or commandline arguments. diff -r 691864097eb1 -r f0c02ff120d6 ChangeLog --- a/ChangeLog Wed May 11 10:10:18 2011 +0200 +++ b/ChangeLog Mon May 16 18:21:27 2011 +0200 @@ -1,3 +1,18 @@ +2011-05-16 Raimund Renkert + + * build.xml: Set the classpath for the executabel jar file. The path to + the external libraries (lib/) is relative to the executable. + + * src/java/de/intevation/mxd/Converter.java: + Use commandline arguments to set the filenames for the MXD-, MAP- and + the template file. If no arguments are used, read the filenames from + the converter.properties file. + + * src/java/de/intevation/mxd/writer/MapScriptWriter.java: + Use the template file for mapfile creation. + + * converter.properties.sample: New. Example for the properties file. + 2011-05-11 Stephan Holl * src/java/de/intevation/mxd/writer/MapScriptWriter.java: Added diff -r 691864097eb1 -r f0c02ff120d6 build.xml --- a/build.xml Wed May 11 10:10:18 2011 +0200 +++ b/build.xml Mon May 16 18:21:27 2011 +0200 @@ -56,8 +56,9 @@ - - + + + diff -r 691864097eb1 -r f0c02ff120d6 converter.properties.sample --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/converter.properties.sample Mon May 16 18:21:27 2011 +0200 @@ -0,0 +1,4 @@ +# Java properties file for the MXD Converter Tool. +mxd = \\full\\path\\to\\mxd-file +map = \\full\\path\\to\\map-file +map-template = \\full\\path\\to\\mapfile-template diff -r 691864097eb1 -r f0c02ff120d6 src/java/de/intevation/mxd/Converter.java --- a/src/java/de/intevation/mxd/Converter.java Wed May 11 10:10:18 2011 +0200 +++ b/src/java/de/intevation/mxd/Converter.java Mon May 16 18:21:27 2011 +0200 @@ -2,6 +2,9 @@ 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; @@ -46,6 +49,84 @@ /** + * 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. */ @@ -62,31 +143,5 @@ } } - /** - * Entrypoint for the application. - */ - public static void main(String[] args) { - try{ - - IReader reader = new MXDReader(); - IWriter writer = new MapScriptWriter(); - reader.init(); - - String path = System.getProperty("mxd.file"); - if (path.equals("${MXDFILE}")) { - System.out.println("No valid MXD file. Use ant parameter" + - " \"-DMXDFILE=path/to/file.mxd\"."); - System.exit(-1); - } - reader.setFilename(path); - - reader.read(); - writer.write(reader.getMapDocument()); - reader.shutdown(); - } - catch(IOException e) { - e.printStackTrace(); - } - } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 691864097eb1 -r f0c02ff120d6 src/java/de/intevation/mxd/writer/MapScriptWriter.java --- a/src/java/de/intevation/mxd/writer/MapScriptWriter.java Wed May 11 10:10:18 2011 +0200 +++ b/src/java/de/intevation/mxd/writer/MapScriptWriter.java Mon May 16 18:21:27 2011 +0200 @@ -44,18 +44,16 @@ */ private Document root; private mapObj map; - + private String mapFilename; - /** - * TODO: Workarround to import the Mapfile-Template, it should be done - * via a property file, currently hardcoded. - */ public MapScriptWriter() { - map = new mapObj("z:/mxd-testbed_sh/mapserver/mapfile/mxd.map"); + map = new mapObj(""); + mapFilename = ""; } - public MapScriptWriter(String path) { - map = new mapObj(path); + public MapScriptWriter(String templ, String filename) { + map = new mapObj(templ); + mapFilename = filename; } /** @@ -73,17 +71,13 @@ root, "/mxd/file", XPathConstants.NODE); - String filename = fileNode.getAttribute("name"); - if(filename.endsWith(".mxd")) { - filename = filename.replace(".mxd", ".map"); - } //Write the map attributes. writeMap(); //Write the layers. writeLayer(); //Save the map. mapObj cloneMap = map.cloneMap(); - cloneMap.save(filename); + cloneMap.save(mapFilename); return true; } @@ -242,14 +236,16 @@ styleObj style = new styleObj(co); style.setAngle( Double.parseDouble(symbolElement.getAttribute("angle"))); - String c = symbolElement.getAttribute("color"); - Color col = Color.decode(c); - colorObj color = new colorObj( - col.getRed(), - col.getGreen(), - col.getBlue(), - -4); - style.setColor(color); + if(symbolElement.hasAttribute("color")) { + String c = symbolElement.getAttribute("color"); + Color col = Color.decode(c); + colorObj color = new colorObj( + col.getRed(), + col.getGreen(), + col.getBlue(), + -4); + style.setColor(color); + } style.setSize(Double.parseDouble( symbolElement.getAttribute("size"))); if(symbolElement.hasAttribute("outline_color")) {