1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
package tim.prune.function.gpsies;
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* XML handler for dealing with XML returned from gpsies.com
*/
public class GpsiesXmlHandler extends DefaultHandler
{
private String _value = null;
private ArrayList<GpsiesTrack> _trackList = null;
private GpsiesTrack _track = null;
/**
* React to the start of an XML tag
*/
public void startElement(String inUri, String inLocalName, String inTagName,
Attributes inAttributes) throws SAXException
{
if (inTagName.equals("tracks")) {
_trackList = new ArrayList<GpsiesTrack>();
}
else if (inTagName.equals("track")) {
_track = new GpsiesTrack();
}
_value = null;
super.startElement(inUri, inLocalName, inTagName, inAttributes);
}
/**
* React to the end of an XML tag
*/
public void endElement(String inUri, String inLocalName, String inTagName)
throws SAXException
{
if (inTagName.equals("track")) {
_trackList.add(_track);
}
else if (inTagName.equals("title")) {
_track.setTrackName(_value);
}
else if (inTagName.equals("description")) {
_track.setDescription(_value);
}
else if (inTagName.equals("fileId")) {
_track.setWebUrl("http://gpsies.com/map.do?fileId=" + _value);
}
else if (inTagName.equals("trackLengthM")) {
try {
_track.setLength(Double.parseDouble(_value));
}
catch (NumberFormatException nfe) {}
}
else if (inTagName.equals("downloadLink")) {
_track.setDownloadLink(_value);
}
super.endElement(inUri, inLocalName, inTagName);
}
/**
* React to characters received inside tags
*/
public void characters(char[] inCh, int inStart, int inLength)
throws SAXException
{
String value = new String(inCh, inStart, inLength);
_value = (_value==null?value:_value+value);
super.characters(inCh, inStart, inLength);
}
/**
* @return the list of tracks
*/
public ArrayList<GpsiesTrack> getTrackList()
{
return _trackList;
}
}
|