package android.codesprint.opentmb;
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.text.format.Time;
public class StopHandler extends DefaultHandler {
private ArrayList<Stop> mStops;
private Stop mCurrentStop;
private Shelter mCurrentShelter;
private ArrayList<Time> mCurrentHours;
private StringBuilder mBuilder;
public ArrayList<Stop> getStops() {
return mStops;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
mBuilder.append(ch, start, length);
}
@Override
public void endElement(String uri, String localName, String name)
throws SAXException {
super.endElement(uri, localName, name);
String nodeValue = mBuilder.toString().trim();
if (mCurrentStop != null){
//Stop attributes
if(localName.equalsIgnoreCase(SaxStopParser.LINE)){
mCurrentStop.setLine(nodeValue);
}
else if(localName.equalsIgnoreCase(SaxStopParser.ADDRESS)) {
mCurrentStop.setAddress(nodeValue);
}
else if(localName.equalsIgnoreCase(SaxStopParser.ROUTE)) {
mCurrentStop.setRoute(nodeValue);
}
else if(localName.equalsIgnoreCase(SaxStopParser.ORIGIN)) {
mCurrentStop.setOrigin(mCurrentShelter);
mCurrentShelter = null;
}
else if(localName.equalsIgnoreCase(SaxStopParser.DESTINATION)) {
mCurrentStop.setDestination(mCurrentShelter);
mCurrentShelter = null;
}
else if(localName.equalsIgnoreCase(SaxStopParser.STOP)) {
mStops.add(mCurrentStop);
mCurrentStop = null;
}
//Shelter attributes
if(mCurrentShelter != null) {
if(localName.equalsIgnoreCase(SaxStopParser.LATITUDE)) {
mCurrentShelter.setLatitude(Double.parseDouble(nodeValue));
}
else if(localName.equalsIgnoreCase(SaxStopParser.LONGITUDE)) {
mCurrentShelter.setLongitude(Double.parseDouble(nodeValue));
}
//Hour attributes
if(mCurrentHours != null) {
if(localName.equalsIgnoreCase(SaxStopParser.HOUR)) {
Time time = new Time();
long millis = Long.parseLong(nodeValue);
time.set(millis);
mCurrentHours.add(time);
}
else if(localName.equalsIgnoreCase(SaxStopParser.HOURS)) {
mCurrentShelter.setHours(mCurrentHours);
mCurrentHours = null;
}
}
}
mBuilder.setLength(0);
}
}
@Override
public void startDocument() throws SAXException {
super.startDocument();
mStops = new ArrayList<Stop>();
mBuilder = new StringBuilder();
}
@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, name, attributes);
if (localName.equalsIgnoreCase(SaxStopParser.STOP)){
mCurrentStop = new Stop();
}
else if(localName.equalsIgnoreCase(SaxStopParser.ORIGIN) ||
localName.equalsIgnoreCase(SaxStopParser.DESTINATION)) {
mCurrentShelter = new Shelter();
}
else if(localName.equalsIgnoreCase(SaxStopParser.HOURS)) {
mCurrentHours = new ArrayList<Time>();
}
}
}
|