package groep3.location; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.ParserConfigurationException; import java.io.*; import java.net.URL; import java.net.MalformedURLException; import java.sql.SQLException; import groep3.connection.db.Database; import groep3.location.Location; public class GeoXMLHandler extends DefaultHandler { private final static int LONGITUDE = 1; private final static int LATITUDE = 2; private final static int DEFAULT = 3; // laatst gelezen opening tag private int huidigElement = DEFAULT; private float lng; private float lat; private InputStream inputstream; private URL url; // default ctr public GeoXMLHandler() { } // ctr public GeoXMLHandler(String u) throws Exception { try { url = new URL(u); inputstream = url.openStream(); } catch(MalformedURLException mue) { throw new Exception(mue); } catch(IOException ioe) { throw new Exception(ioe); } } public void set(String u) throws Exception { try { url = new URL(u); inputstream = url.openStream(); String s = inputstream.toString(); } catch(MalformedURLException mue) { throw new Exception(mue); } catch(IOException ioe) { throw new Exception(ioe); } } public float getLongitude() { return lng; } public float getLatitude() { return lat; } // aangeroepen bij opening tag public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase("geo:long")) huidigElement = LONGITUDE; else if (qName.equalsIgnoreCase("geo:lat")) huidigElement = LATITUDE; else huidigElement = DEFAULT; } public void endElement(String namespaceURI, String localName, String qName) { huidigElement = DEFAULT; } // aangeroepen bij lezen van tekstinhoud public void characters(char[] ch, int start, int length) { if (huidigElement == LONGITUDE) lng = Float.parseFloat(new String(ch, start, length)); else if (huidigElement == LATITUDE) lat = Float.parseFloat(new String(ch, start, length)); } // haal de xml file af en parse de gegevens public float[] parseGeoCoding(String url) throws Exception { float[] longlat = new float[2]; try { set(url); SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setValidating(false); SAXParser sp = spf.newSAXParser(); GeoXMLHandler handler = new GeoXMLHandler(); sp.parse(inputstream, handler); longlat[0] = handler.getLongitude(); longlat[1] = handler.getLatitude(); } catch(SAXException se) { throw new Exception(se); } catch(ParserConfigurationException pce) { throw new Exception(pce); } catch(IOException ioe) { throw new Exception(ioe); } /*longlat[0] = 11.1f; longlat[1] = 22.2f;*/ return longlat; } public boolean updateCurrentLocation(float[] longlat) throws Exception { try { Database db = new Database(true); String update = "UPDATE tblUsers SET currentLocation=? WHERE id=?"; Location loc = new Location(longlat[0], longlat[1]); int currentLocation = loc.storeDB(); String[] args = {"" + currentLocation, "" + (int) longlat[2]}; db.updateP(update, args); db.close(); } catch(SQLException ex) { throw new Exception(ex); } return true; } public static void main(String[] args) throws Exception { GeoXMLHandler gxh = new GeoXMLHandler(); float[] array = gxh.parseGeoCoding("http://worldkit.org/geocoder/rest/?city=Lummen,BEL"); System.out.println(array[0] + " " + array[1]); } }