
package uk.co.wingpath.gpx;

import java.io.*;
import java.util.*;
import uk.co.wingpath.xml.*;
import uk.co.wingpath.util.*;

// GPX documents contain a metadata header, followed by waypoints, routes,
// and tracks. You can add your own elements to the extensions section of
// the GPX document.

public class Gpx
    implements Xml.Savable, Xml.Loader
{
    private String version;
    private String creator;
    private MetaData metadata;
    private List<Waypoint> waypoints;
    private List<Route> routes;
    private List<Track> tracks;

    public Gpx ()
    {
        version = null;
        creator = null;
        metadata = new MetaData ();
        waypoints = new ArrayList<Waypoint> ();
        routes = new ArrayList<Route> ();
        tracks = new ArrayList<Track> ();
    }

    public List<Track> getTracks ()
    {
        return tracks;
    }

    public List<Waypoint> getWaypoints ()
    {
        return waypoints;
    }

    public List<Route> getRoutes ()
    {
        return routes;
    }

	public void add (Waypoint w)
    {
        waypoints.add (w);
    }

	public void add (Route r)
    {
        routes.add (r);
    }

	public void add (Track t)
    {
        tracks.add (t);
    }

    public Xml.Loader startChild (String tag)
    {
        if (tag.equalsIgnoreCase ("metadata"))
        {
            return metadata;
        }
        else if (tag.equalsIgnoreCase ("wpt"))
        {
            Waypoint w = new Waypoint ();
            waypoints.add (w);
            return w;
        }
        else if (tag.equalsIgnoreCase ("rte"))
        {
            Route r = new Route ();
            routes.add (r);
            return r;
        }
        else if (tag.equalsIgnoreCase ("trk"))
        {
            Track t = new Track ();
            tracks.add (t);
            return t;
        }
        else
        {
            return metadata.startChild (tag);
        }
    }

    public void attribute (String attr, String value)
    {
        if (attr.equals ("creator"))
            creator = value;
        else if (attr.equals ("version"))
            version = value;
    }

    public void end (String value)
        throws ValueException
    {
        if (!value.equals (""))
            throw new ValueException ("Value not allowed");
    }

    public void cleanup ()
    {
    }

    public void save (Xml.Saver saver)
        throws IOException
    {
        saver.saveAttribute ("version", "1.1");
        saver.saveAttribute ("creator", "garx 1.0");
        saver.saveAttribute ("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        saver.saveAttribute ("xmlns", "http://www.topografix.com/GPX/1/1");
        saver.saveAttribute ("xsi:schemaLocation", "http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd");

        saver.saveValue ("metadata", metadata);

        for (Waypoint w : waypoints)
            saver.saveValue ("wpt", w);

        for (Route r : routes)
            saver.saveValue ("rte", r);

        for (Track t : tracks)
            saver.saveValue ("trk", t);
    }
}


