package gpx.parser.local;
import java.util.Vector;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import gpx.parser.local.ParsedData;
public class ContentHandler extends DefaultHandler
{
// Fields
private boolean in_gpx = false;
private boolean in_extensions = false;
//private boolean in_distance = false;
//private boolean in_time = false;
private boolean in_start = false;
private boolean in_end = false;
//private boolean in_rtept = false;
//private boolean in_desc = false;
//private boolean in_offset = false;
//private boolean in_distance_text = false;
//private boolean in_direction = false;
//private boolean in_azimuth = false;
//private boolean in_turn = false;
//private boolean in_turn_angle = false;
//private boolean in_rte = false;
private boolean in_wpt = false;
private ParsedData myParsedData = new ParsedData();
// Getter Method
public ParsedData getParsedData()
{
return this.myParsedData;
}
public void startDocument() throws SAXException
{
this.myParsedData = new ParsedData();
}
public void endDocument() throws SAXException
{
// do nothing
}
// Takes care of the opening tags
/** Gets be called on opening tags like:
* <tag>
* Can provide attribute(s), when xml was like:
* <tag attribute="attributeValue">*/
public void startTags(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException
{
if(localName.equals("gpx"))
{
this.in_gpx = true;
if(localName.equals("extensions"))
{
this.in_extensions = true;
/*else if(localName.equals("distance"))
{
this.in_distance = true;
}
else if(localName.equals("time"))
{
this.in_time = true;
}*/
if(localName.equals("start"))
{
this.in_start = true;
}
else if(localName.equals("end"))
{
this.in_end = true;
}
}
/*else if(localName.equals("rtept"))
{
this.in_rtept = true;
}
else if(localName.equals("desc"))
{
this.in_desc = true;
}
else if(localName.equals("offset"))
{
this.in_offset = true;
}
else if(localName.equals("distance_text"))
{
this.in_distance_text = true;
}
else if(localName.equals("direction"))
{
this.in_direction = true;
}
else if(localName.equals("azimuth"))
{
this.in_azimuth = true;
}
else if(localName.equals("turn"))
{
this.in_turn = true;
}
else if(localName.equals("turn_angle"))
{
this.in_turn_angle = true;
}*/
else if(localName.equals("wpt"))
{
String attributeValue = atts.getValue("lat");
double lat = Double.parseDouble(attributeValue);
myParsedData.setWpt(lat);
String attributeValue2 = atts.getValue("lon");
double lon = Double.parseDouble(attributeValue2);
myParsedData.setWpt(lon);
}
}
}
/** Gets be called on closing tags like:
* </tag> */
public void endTags(String namespaceURI, String localName, String qName)
throws SAXException
{
if(localName.equals("gpx"))
{
this.in_gpx = false;
if(localName.equals("extensions"))
{
this.in_extensions = false;
/*else if(localName.equals("distance"))
{
this.in_distance = false;
}
else if(localName.equals("time"))
{
this.in_time = false;
}*/
if(localName.equals("start"))
{
this.in_start = false;
}
else if(localName.equals("end"))
{
this.in_end = false;
}
}
/*else if(localName.equals("rtept"))
{
this.in_rtept = false;
}
else if(localName.equals("desc"))
{
this.in_desc = false;
}
else if(localName.equals("offset"))
{
this.in_offset = false;
}
else if(localName.equals("distance_text"))
{
this.in_distance_text = false;
}
else if(localName.equals("direction"))
{
this.in_direction = false;
}
else if(localName.equals("azimuth"))
{
this.in_azimuth = false;
}
else if(localName.equals("turn"))
{
this.in_turn = false;
}
else if(localName.equals("turn_angle"))
{
this.in_turn_angle = false;
}*/
else if(localName.equals("wpt"))
{
// Do nothing...
}
}
}
/** Gets be called on the following structure:
* <tag>characters</tag> */
public void characters(char ch[], int start, int length)
{
if(this.in_start)
{
//myParsedData.setStart(new String(ch, start, length));
myParsedData.setStart(new StringBuffer().append(ch).toString());
}
if(this.in_end)
{
myParsedData.setEnd(new StringBuffer().append(ch).toString());
}
}
}