package org.r3pek.pharmacies; import java.util.ArrayList; import org.xml.sax.Attributes; import org.xml.sax.helpers.DefaultHandler; public class XMLParserHandler extends DefaultHandler { private boolean inName = false; private boolean inAddress = false; private boolean inZipCode = false; private boolean inMunicipality = false; private boolean inLatitude = false; private boolean inLongitude = false; private boolean inPhone = false; private boolean inDistance = false; private boolean inIsAtService = false; private boolean inParish = false; private boolean error = false; private boolean inErrorMessage; private String errorMessage; private Pharmacy currentPharmacy; private ArrayList result; public XMLParserHandler() { result = new ArrayList(); } private void checkTag(String tag, boolean newValue) { if ((tag.equals("GetPharmaciesByCoordinatesResponse") || tag.equals("GetPharmaciesAtServiceByCoordinatesResponse")) && newValue) { if (error) error = false; result.clear(); } else if (tag.equals("Pharmacy")) { if (newValue) currentPharmacy = new Pharmacy(); else result.add(currentPharmacy); } else if (tag.equals("Name")) inName = newValue; else if (tag.equals("Street")) inAddress = newValue; else if (tag.equals("ZipCode")) inZipCode = newValue; else if (tag.equals("Municipality")) inMunicipality = newValue; else if (tag.equals("Latitude")) inLatitude = newValue; else if (tag.equals("Longitude")) inLongitude = newValue; else if (tag.equals("Phone")) inPhone = newValue; else if (tag.equals("Distance")) inDistance = newValue; else if (tag.equals("IsAtService")) inIsAtService = newValue; else if (tag.equals("Parish")) inParish = newValue; else if (tag.equals("Fault")) error = true; else if (tag.equals("FaultString")) inErrorMessage = newValue; } public void startElement(String uri, String name, String qName, Attributes atts) { checkTag(name.trim(), true); } public void endElement(String uri, String name, String qName) { checkTag(name.trim(), false); } public void characters(char ch[], int start, int length) { String chars = (new String(ch).substring(start, start + length)); if (inName) currentPharmacy.setName(currentPharmacy.getName() + chars); else if (inAddress) currentPharmacy.setAddress(currentPharmacy.getAddress() + chars); else if (inZipCode) currentPharmacy.setAddress(currentPharmacy.getAddress() + "\n" + chars); else if (inMunicipality) currentPharmacy.setAddress(currentPharmacy.getAddress() + " " + chars); else if (inLatitude) currentPharmacy.setLatitude(currentPharmacy.getLatitude() + chars); else if (inLongitude) currentPharmacy.setLongitude(currentPharmacy.getLongitude() + chars); else if (inPhone) currentPharmacy.setPhoneNumber(currentPharmacy.getPhoneNumber() + chars); else if (inDistance) currentPharmacy.setDistance(currentPharmacy.getDistance() + chars); else if (inIsAtService) currentPharmacy.setAtService(Boolean.parseBoolean(chars)); else if (inParish) currentPharmacy.setParish(currentPharmacy.getParish() + chars); else if (inErrorMessage) errorMessage += chars; } public ArrayList getPharmacies() { if (error) return null; return result; } public String getErrorMessage() { return errorMessage; } }