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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
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 boolean _inTracks = false;
private boolean _inTrack = false;
private boolean _inTrackName = false;
private boolean _inDescription = false;
private boolean _inFileId = false;
private boolean _inTrackLength = false;
private boolean _inLink = false;
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")) {
_inTracks = true;
_trackList = new ArrayList<GpsiesTrack>();
}
else if (_inTracks && inTagName.equals("track")) {
_inTrack = true;
_track = new GpsiesTrack();
}
else if (_inTrack && inTagName.equals("title")) {_inTrackName = true;}
else if (_inTrack && inTagName.equals("description")) {_inDescription = true;}
else if (_inTrack && inTagName.equals("fileId")) {_inFileId = true;}
else if (_inTrack && inTagName.equals("trackLengthM")) {_inTrackLength = true;}
else if (_inTrack && inTagName.equals("downloadLink")) {_inLink = true;}
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("tracks")) {_inTracks = false;}
else if (_inTrack && inTagName.equals("track")) {
_trackList.add(_track);
_inTrack = false;
}
else if (_inTrackName && inTagName.equals("title")) {
_track.setTrackName(_value);
_inTrackName = false;
}
else if (_inDescription && inTagName.equals("description")) {
_track.setDescription(_value);
_inDescription = false;
}
else if (_inFileId && inTagName.equals("fileId")) {
_track.setFileId(_value);
_inFileId = false;
}
else if (_inTrackLength && inTagName.equals("trackLengthM")) {
try {
_track.setLength(Double.parseDouble(_value));
}
catch (NumberFormatException nfe) {}
_inTrackLength = false;
}
else if (_inLink && inTagName.equals("downloadLink")) {
_track.setDownloadLink(_value);
_inLink = false;
}
super.endElement(inUri, inLocalName, inTagName);
}
/**
* React to characters received inside tags
*/
public void characters(char[] inCh, int inStart, int inLength)
throws SAXException
{
_value = new String(inCh, inStart, inLength);
// System.out.println("Value: '" + value + "'");
// TODO: Note, this doesn't cope well with split characters for really long descriptions etc
super.characters(inCh, inStart, inLength);
}
/**
* @return the list of tracks
*/
public ArrayList<GpsiesTrack> getTrackList()
{
return _trackList;
}
}
|