package net.sourceforge.gpstools;
/* gpsdings
* Copyright (C) 2006 Moritz Ringler
* $Id: GpxOVL.java 441 2010-12-13 20:04:20Z ringler $
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import net.sourceforge.gpstools.ovl.OVLVisitor;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.MissingOptionException;
public class GpxOVL extends GPSDings {
private Transformer ovlMaker;
private Ovl2gpx o2g;
public static enum Mode {
AUTODETECT, GPXINPUT, OVLINPUT;
}
private Mode mode = Mode.AUTODETECT;
private File outputFile;
public GpxOVL() {
// no extra initialization.
}
public void convertFiles(File[] input) throws IOException,
TransformerConfigurationException, TransformerException,
OVLVisitor.OVLVisitorException, java.text.ParseException {
InputStream in = null;
OutputStream out = null;
for (File f : input) {
in = new BufferedInputStream(new FileInputStream(f));
Mode fmode = mode;
if (fmode == Mode.AUTODETECT) {
fmode = (f.getName().endsWith(".gpx")) ? Mode.GPXINPUT
: Mode.OVLINPUT;
}
File todir = (outputFile == null) ? f.getAbsoluteFile()
.getParentFile() : outputFile;
File fout;
try {
switch (fmode) {
case GPXINPUT:
fout = new File(todir, f.getName().replaceFirst("\\.gpx$",
"")
+ ".ovl");
out = new BufferedOutputStream(new FileOutputStream(fout));
gpx2ovl(in, out);
out.flush();
break;
case OVLINPUT:
fout = new File(todir, f.getName().replaceFirst("\\.ovl$",
"")
+ ".gpx");
out = new BufferedOutputStream(new FileOutputStream(fout));
ovl2gpx(in, out);
out.flush();
break;
default:
}
} finally {
try {
in.close();
} catch (IOException ignore) {
// ignore
}
if (out != null) {
try {
out.close();
} catch (IOException ignore) {
// ignore
}
}
}
}
}
public synchronized void gpx2ovl(InputStream in, OutputStream out)
throws IOException, TransformerConfigurationException,
TransformerException {
if (ovlMaker == null) {
TransformerFactory tf = TransformerFactory.newInstance();
InputStream style = null;
try {
style = GpxOVL.class
.getResourceAsStream("/net/sourceforge/gpstools/xslt/gpx2ovl.xsl");
Source styleSrc = new StreamSource(style);
styleSrc.setSystemId(GpxOVL.class.getResource(
"/net/sourceforge/gpstools/xslt/gpx2ovl.xsl")
.toString());
ovlMaker = tf.newTransformer(styleSrc);
} finally {
if (style != null) {
style.close();
}
}
}
Source src = new StreamSource(in);
// src.setSystemId(input.toURL().toString());
Result res = new StreamResult(out);
ovlMaker.transform(src, res);
}
public synchronized void ovl2gpx(InputStream in, OutputStream out)
throws IOException, OVLVisitor.OVLVisitorException,
java.text.ParseException {
if (o2g == null) {
o2g = new Ovl2gpx();
}
o2g.transform(in, out);
}
public void setMode(Mode m) {
mode = m;
}
public void setOutputDirectory(File dir) {
outputFile = dir;
}
public static void main(String[] argv) {
(new GpxOvlCommandLine(argv)).execute();
}
private static class GpxOvlCommandLine extends AbstractCommandLine<GpxOVL> {
public static final String OPT_GPX2OVL = "gpx2ovl";
public static final String OPT_OVL2GPX = "ovl2gpx";
public static final String OPT_TO_DIR = "todir";
@Override
public void printHelp(PrintStream out) {
try {
super.printHelp(out);
cat("gpxovl.txt");
out.println();
out.println("System information:");
out.println("Java version: "
+ System.getProperty("java.version"));
TransformerFactory tf = TransformerFactory.newInstance();
out.println("XSLT implementation: "
+ tf.getClass().getName()
.replaceFirst("\\.TransformerFactoryImpl$", ""));
} catch (Exception ex) {
throw new Error(ex);
}
}
public GpxOvlCommandLine(String[] argv) {
super(new GpxOVL(), argv);
}
@Override
public void execute() {
try {
// Locale.setDefault(Locale.US);
processArguments();
String[] inp = getInput();
if (inp.length == 1 && "-".equals(inp[0])) {
switch (app.mode) {
case GPXINPUT:
app.gpx2ovl(System.in, System.out);
return;
case OVLINPUT:
app.ovl2gpx(System.in, System.out);
return;
default:
throw new MissingOptionException(
"You must specify the conversion direction when"
+ " processing standard input.");
}
}
File[] input = getInputFiles();
if (input == null) {
System.exit(1);
}
app.convertFiles(input);
} catch (Exception ex) {
ex.printStackTrace();
System.err.println(ex.getClass().getName() + ": "
+ ex.getMessage());
System.exit(1);
}
}
@Override
protected Options createOptions() {
Options options = super.createOptions();
OptionGroup mode = new OptionGroup();
mode.addOption(makeOption(OPT_GPX2OVL, Boolean.class, 0, null));
mode.addOption(makeOption(OPT_OVL2GPX, Boolean.class, 0, null));
options.addOptionGroup(mode);
options.addOption(makeOption(OPT_TO_DIR, File.class, 1, "directory"));
return options;
}
@Override
protected CommandLine processOptions(Options options)
throws org.apache.commons.cli.ParseException, IOException,
java.text.ParseException {
CommandLine cl = super.processOptions(options);
char c;
/* mode options */
opt = OPT_GPX2OVL;
c = opt.charAt(0);
if (cl.hasOption(c)) {
app.setMode(Mode.GPXINPUT);
}
opt = OPT_OVL2GPX;
c = opt.charAt(0);
if (cl.hasOption(c)) {
app.setMode(Mode.OVLINPUT);
}
/* todir option */
opt = OPT_TO_DIR;
c = opt.charAt(0);
if (cl.hasOption(c)) {
File outputdir = new File(cl.getOptionValue(c));
if (!outputdir.isDirectory() || outputdir.mkdirs()) {
throw new FileNotFoundException(
"Cannot neither access nor create directory "
+ outputdir.getAbsolutePath());
}
app.setOutputDirectory(outputdir);
}
return cl;
}
}
}