
package uk.co.wingpath.gpx;

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

// Information about the GPX file, author, and copyright
// restrictions goes in the metadata section. Providing rich, meaningful
// information about your GPX files allows others to search for and use
// your GPS data. 

public class MetaData
    implements Xml.Savable, Xml.Loader
{
    private String name;        // The name of the GPX file.
    private String desc;        // Description of the contents of the file.
    private Person author;      // Person or organization who created the file.
    private Copyright copyright; // Copyright and license information
                                // governing use of the file.
    private List<Link> link;    // URLs associated with the location
                                // described in the file.
    private String time;        // The creation date of the file.
	private boolean hasTime;
    private String keywords;    // Keywords associated with the file.
                                // Search engines or databases can use this
                                // information to classify the data.
    private Bounds bounds;      // Minimum and maximum coordinates which
                                // describe the extent of the coordinates
                                // in the file.

	public MetaData ()
    {
        name = null;
        desc = null;
        author = null;
        copyright = null;
        link = new ArrayList<Link> ();
        hasTime = false;
        keywords = null;
        bounds = null;
    }

    public Bounds getBounds ()
    {
        return bounds;
    }

    public Xml.Loader startChild (String tag)
    {
        if (tag.equalsIgnoreCase ("author"))
        {
            if (author == null)
                author = new Person ();
            return author;
        }
        else if (tag.equalsIgnoreCase ("copyright"))
        {
            if (copyright == null)
                copyright = new Copyright ();
            return copyright;
        }
        else if (tag.equalsIgnoreCase ("link"))
        {
            Link l = new Link ();
            link.add (l);
            return l;
        }
        else if (tag.equalsIgnoreCase ("bounds"))
        {
            bounds = new Bounds ();
            return bounds;
        }
        else if (tag.equalsIgnoreCase ("email"))
        {
            if (author == null)
                author = new Person ();
            return author.startChild (tag);
        }
        if (tag.equalsIgnoreCase ("name"))
        {
            return new Xml.StringLoader (new Xml.Receiver<String> ()
            {
                public void receive (String value)
                {
                    name = value;
                }
            });
        }
        if (tag.equalsIgnoreCase ("desc"))
        {
            return new Xml.StringLoader (new Xml.Receiver<String> ()
            {
                public void receive (String value)
                {
                    desc = value;
                }
            });
        }
        if (tag.equalsIgnoreCase ("time"))
        {
            return new Xml.StringLoader (new Xml.Receiver<String> ()
            {
                public void receive (String value)
                {
                    time = value;
                    hasTime = true;
                }
            });
        }
        if (tag.equalsIgnoreCase ("keywords"))
        {
            return new Xml.StringLoader (new Xml.Receiver<String> ()
            {
                public void receive (String value)
                {
                    keywords = value;
                }
            });
        }
        return null;
    }

    public void attribute (String attr, String value)
    {
    }

    public void end (String value)
    {
    }

    public void cleanup ()
    {
    }

    public void save (Xml.Saver saver)
        throws IOException
    {
        if (name != null)
            saver.saveValue ("name", name);
        if (desc != null)
            saver.saveValue ("desc", desc);
        if (author != null)
            saver.saveValue ("author", author);
        if (copyright != null)
            saver.saveValue ("copyright", copyright);

        for (Link l : link)
            saver.saveValue ("link", l);

        if (hasTime)
            saver.saveValue ("time", time);
        if (keywords != null)
            saver.saveValue ("keywords", keywords);
        if (bounds != null)
            saver.saveValue ("bounds", bounds);
    }
}

