/*
 * Copyright 2012 ToureNPlaner
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package de.tourenplaner.utils;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Map;

/**
 * This utility class is used to convert the result of a /algsp request to gpx
 * and prints it on stdout
 *
 * @author Christoph Haag, Sascha Meusel, Niklas Schnelle, Peter Vollmer
 */
public class SPtoGPX {

	public static void main(String[] args) {
		if (args.length != 1) {
			System.err
					.println("Please supply a .json file as first parameter or - to read from stdin");
			return;
		}
		InputStream input;
		try {
			input = args[0].equals("-") ? System.in : new FileInputStream(
					args[0]);
		} catch (FileNotFoundException e) {
			System.err.println("The file: " + args[0] + " could not be found");
			return;
		}
		ObjectMapper mapper = new ObjectMapper();

		try {
			Map<String, Object> requestJSON = mapper.readValue(input,
					new TypeReference<Map<String, Object>>() {
					});
			@SuppressWarnings("unchecked")
			ArrayList<Map<String, Object>> points = (ArrayList<Map<String, Object>>) requestJSON
					.get("way");

			System.out
					.println("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>");
			System.out
					.println("<gpx xmlns=\"http://www.topografix.com/GPX/1/1\" xmlns:gpxx=\"http://www.garmin.com/xmlschemas/GpxExtensions/v3\" xmlns:gpxtpx=\"http://www.garmin.com/xmlschemas/TrackPointExtension/v1\" creator=\"Oregon 400t\" version=\"1.1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd\">");
			System.out.println("  <trk>\n"
					+ "    <name>Example GPX Document</name>");
			System.out.println("<trkseg>");
			for (Map<String, Object> point : points) {
				System.out.println("<trkpt lat=\""
						+ ((Integer) point.get("lt")).doubleValue()
						/ 10000000.0 + "\" lon=\""
						+ ((Integer) point.get("ln")).doubleValue()
						/ 10000000.0 + "\"></trkpt>");
			}
			System.out.println("</trkseg>\n</trk>\n</gpx>");

		} catch (IOException e) {
			System.err.println("An IO Error ocurred: " + e.getMessage());
		}
	}
}

@@ -898,16 +898,39 @@
 ayList;%0A
+import java.util.List;%0A
 import j
@@ -943,16 +943,16 @@
 il.Map;%0A
-
 %0A/**%0A * 
@@ -1786,82 +1786,56 @@
 d%22)%0A
-%09%09%09ArrayList%3CMap%3CString, Object%3E%3E points = (ArrayList%3CMap%3CString, 
+            List%3CObject%3E subways = (List%3C
 Object%3E
-%3E
 ) re
@@ -1843,22 +1843,16 @@
 uestJSON
-%0A%09%09%09%09%09
 .get(%22wa
@@ -1855,16 +1855,53 @@
 (%22way%22);
+%0A%09%09%09List%3CMap%3CString, Object%3E%3E points;
 %0A%0A%09%09%09Sys
@@ -2708,46 +2708,169 @@
 %22);%0A
+%0A
 %09%09%09
-System.out.println(%22%3Ctrkseg%3E%22);%0A%09%09%09
+for (Object subway : subways) %7B%0A                System.out.println(%22%3Ctrkseg%3E%22);%0A                points = (List%3CMap%3CString, Object%3E%3E) subway;%0A                
 for 
@@ -2908,20 +2908,36 @@
 ints) %7B%0A
-%09%09%09%09
+                    
 System.o
@@ -2959,30 +2959,52 @@
 kpt lat=%5C%22%22%0A
-%09%09%09%09%09%09
+                            
 + ((Integer)
@@ -3039,47 +3039,91 @@
 e()%0A
-%09%09%09%09%09%09/ 10000000.0 + %22%5C%22 lon=%5C%22%22%0A%09%09%09%09%09%09
+                            / 10000000.0 + %22%5C%22 lon=%5C%22%22%0A                            
 + ((
@@ -3166,14 +3166,36 @@
 e()%0A
-%09%09%09%09%09%09
+                            
 / 10
@@ -3225,47 +3225,114 @@
 %22);%0A
-%09%09%09%7D%0A%09%09%09System.out.println(%22%3C/trkseg%3E%5Cn
+                %7D%0A                System.out.println(%22%3C/trkseg%3E%5Cn%22);%0A            %7D%0A%09%09%09System.out.println(%22
 %3C/tr

fix for able to convert the new responses