diff --git a/.classpath b/.classpath index 9fc2de7..534b5e5 100644 --- a/.classpath +++ b/.classpath @@ -22,7 +22,7 @@ - + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index abec6ca..69c31cd 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,5 +1,8 @@ eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 -org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.source=1.5 +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/pom.xml b/pom.xml index c4ab8e6..dc5a93f 100644 --- a/pom.xml +++ b/pom.xml @@ -10,5 +10,26 @@ jcommander 1.30 + + org.json + json + 20140107 + - \ No newline at end of file + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.2 + + 1.6 + 1.6 + + + + + + diff --git a/src/main/java/edu/aalto/emn/Bus.java b/src/main/java/edu/aalto/emn/Bus.java index 3959202..05ee61f 100644 --- a/src/main/java/edu/aalto/emn/Bus.java +++ b/src/main/java/edu/aalto/emn/Bus.java @@ -1,22 +1,86 @@ package edu.aalto.emn; -import java.util.HashMap; -import java.util.Map; +import java.util.ArrayList; -public class Bus { +import org.json.JSONArray; +import org.json.JSONObject; - private Map stops; // time - stop - private int route; +public class Bus implements Jsonable { + + private String serviceID; + private String serviceNbr; + private ArrayList stops; //stop + private String route; + private String companyId; + private String trnsmode; public Bus() { - this.stops = new HashMap(); + this.stops = new ArrayList(); } - public void setRoute(int route) { + public void setRoute(String route) { this.route = route; } + + public String getRoute() { + return this.route; + } - public void addStop(String time, BusStop stop) { - this.stops.put(time, stop); + public void addStop(Stop stop) { + this.stops.add(stop); + } + + public ArrayList getStops() { + return this.stops; } + + public String getServiceID() { + return serviceID; + } + + public void setServiceID(String serviceID) { + this.serviceID = serviceID; + } + + public String getServiceNbr() { + return serviceNbr; + } + + public void setServiceNbr(String serviceNbr) { + this.serviceNbr = serviceNbr; + } + + public void setCompany(String companyId) { + this.companyId = companyId; + } + + public String getCompany() { + return this.companyId; + } + + @Override + public JSONObject toJson() { + JSONObject jBus = new JSONObject(); + jBus.put("serviceId", this.getServiceID()); + jBus.put("companyId", this.getCompany()); + jBus.put("serviceNbr", this.getServiceNbr()); + jBus.put("route", this.getRoute()); + JSONArray jstops = new JSONArray(); + + for(Stop stop : this.getStops()) { + jstops.put(stop.toJson()); + } + + jBus.put("stops", jstops); + + return jBus; + } + + public String getTrnsmode() { + return trnsmode; + } + + public void setTrnsmode(String trnsmode) { + this.trnsmode = trnsmode; + } } diff --git a/src/main/java/edu/aalto/emn/BusStop.java b/src/main/java/edu/aalto/emn/BusStop.java index 054bbe0..03c5a25 100644 --- a/src/main/java/edu/aalto/emn/BusStop.java +++ b/src/main/java/edu/aalto/emn/BusStop.java @@ -1,9 +1,10 @@ package edu.aalto.emn; +import org.json.JSONObject; import org.xml.sax.Attributes; -public class BusStop { +public class BusStop{ private String id; private String name; private String x; diff --git a/src/main/java/edu/aalto/emn/DBHandler.java b/src/main/java/edu/aalto/emn/DBHandler.java index eb57b07..163b4be 100644 --- a/src/main/java/edu/aalto/emn/DBHandler.java +++ b/src/main/java/edu/aalto/emn/DBHandler.java @@ -1,6 +1,7 @@ package edu.aalto.emn; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -11,60 +12,107 @@ import org.xml.sax.helpers.DefaultHandler; public class DBHandler extends DefaultHandler { - private Map stops; - private List buses; - private int route; + private Map stops; + private ArrayList buses; - private Stack elementStack = new Stack(); - private Stack objectStack = new Stack(); + private List allowedModes = Arrays.asList("1", "3", "4", "5", "25"); + private String route, company; - public DBHandler(int route) { - this.route = route; - this.stops = new HashMap(); - this.buses = new ArrayList(); - } + private Stack elementStack = new Stack(); + private Stack objectStack = new Stack(); + - public List getBuses() { - return this.buses; - } - - public boolean isReal(Attributes atts) { - String type = atts.getValue("type"); - String isVirtual = atts.getValue("isVirtual"); - - if(type == null && isVirtual == null) { - return true; - } - - return (type != null && "0".equals(type)) || (isVirtual != null && "false".equals(isVirtual)); - } + public DBHandler(String route, String company) { + this.route = route; + this.company = company; + this.stops = new HashMap(); + this.buses = new ArrayList(); + } - @Override - public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + public List getBuses() { + return this.buses; + } - this.elementStack.push(qName); + public Map getStops() { + return this.stops; + } - if ("station".equals(qName.toLowerCase())) { - if(isReal(attributes)) { - BusStop stop = new BusStop(attributes); - this.objectStack.push(stop); - this.stops.put(stop.getId(), stop); - } else { - this.elementStack.pop(); - } - } else if ("service".equals(qName.toLowerCase())) { - Bus bus = new Bus(); - this.objectStack.push(bus); - } - } + public boolean isReal(Attributes atts) { + String type = atts.getValue("type"); + String isVirtual = atts.getValue("isVirtual"); - public void endElement(String uri, String localName, String qName) throws SAXException { + if (type == null && isVirtual == null) { + return true; + } - this.elementStack.pop(); - String qNameLow = qName.toLowerCase(); + return (type != null && "0".equals(type)) + || (isVirtual != null && "false".equals(isVirtual)); + } - if ("station".equals(qNameLow) || "service".equals(qNameLow)) { - Object object = this.objectStack.pop(); - } - } -} + @Override + public void startElement(String uri, String localName, String qName, + Attributes attributes) throws SAXException { + String qNameLow = qName.toLowerCase(); + this.elementStack.push(qName); + + if ("station".equals(qName.toLowerCase())) { + if (isReal(attributes)) { + BusStop stop = new BusStop(attributes); + this.objectStack.push(stop); + this.stops.put(stop.getId(), stop); + } else { + this.elementStack.pop(); + } + } else if ("service".equals(qName.toLowerCase())) { + Bus bus = new Bus(); + bus.setServiceID(attributes.getValue("ServiceId")); + this.objectStack.push(bus); + + } else if ("servicenbr".equals(qNameLow)) { + String routeNbr = attributes.getValue("Variant"); + String serviceNbr = attributes.getValue("ServiceNbr"); + String companyId = attributes.getValue("CompanyId"); + + + if (routeNbr != null) { + Bus bus = (Bus) this.objectStack.peek(); + bus.setCompany(companyId); + bus.setRoute(routeNbr); + bus.setServiceNbr(serviceNbr); + } + + } else if ("stop".equals(qNameLow)) { + Bus bus = (Bus) this.objectStack.peek(); + + try { + Stop stop = new Stop(attributes, this.getStops()); + bus.addStop(stop); + } catch(Exception e) { + System.out.println("Error parsing stop"); + } + } else if ("servicetrnsmode".equals(qNameLow)) { + Bus bus = (Bus) this.objectStack.peek(); + bus.setTrnsmode(attributes.getValue("TrnsmodeId")); + } + } + + public void endElement(String uri, String localName, String qName) + throws SAXException { + + this.elementStack.pop(); + String qNameLow = qName.toLowerCase(); + + if ("service".equals(qNameLow)) { + Bus bus = (Bus) this.objectStack.pop(); + + if(filter(bus)) { + this.buses.add(bus); + } + } + } + + + private boolean filter(Bus bus) { + return allowedModes.contains(bus.getTrnsmode()); //bus.getRoute().equals(this.route) && this.company.equals(bus.getCompany()); + } +} \ No newline at end of file diff --git a/src/main/java/edu/aalto/emn/Jsonable.java b/src/main/java/edu/aalto/emn/Jsonable.java new file mode 100644 index 0000000..194fed5 --- /dev/null +++ b/src/main/java/edu/aalto/emn/Jsonable.java @@ -0,0 +1,8 @@ +package edu.aalto.emn; + +import org.json.JSONObject; + +public interface Jsonable { + + public JSONObject toJson(); +} diff --git a/src/main/java/edu/aalto/emn/SnippetMain.java b/src/main/java/edu/aalto/emn/SnippetMain.java index db81b80..c2ae885 100644 --- a/src/main/java/edu/aalto/emn/SnippetMain.java +++ b/src/main/java/edu/aalto/emn/SnippetMain.java @@ -3,35 +3,66 @@ import java.io.File; import java.io.FileInputStream; import java.io.InputStream; +import java.io.PrintWriter; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.beust.jcommander.JCommander; import com.beust.jcommander.Parameter; public class SnippetMain { @Parameter(names = { "-route" }, description = "Route number") - private static Integer route = 23; + private String route = "23"; + + private String companyId = "15690"; @Parameter(names = "-xml", description = "Path to XML database dump", required=true) - private static String path; + private String path; + + @Parameter(names = "-out", required = true) + private String out; public static void main(String[] args) { - File xmlFile = new File(args[0]); + SnippetMain snippet = new SnippetMain(); + new JCommander(snippet, args); SAXParserFactory factory = SAXParserFactory.newInstance(); + try { - InputStream xmlInput = new FileInputStream(args[0]); + InputStream xmlInput = new FileInputStream(snippet.path); SAXParser saxParser = factory.newSAXParser(); - DBHandler handler = new DBHandler(route); + DBHandler handler = new DBHandler(snippet.route, snippet.companyId); saxParser.parse(xmlInput, handler); + + JSONObject db = new JSONObject(); + JSONArray busesAr = new JSONArray(); + - for(Bus bus : handler.getBuses()){ - // System.out.println(bus); + System.out.println("Got " + handler.getStops().keySet().size() + " stops"); + System.out.println("Got " + handler.getBuses().size() + " buses"); + + for(Bus bus : handler.getBuses()) { + busesAr.put(bus.toJson()); } - System.out.println("Done"); + + db.put("buses", busesAr); + + File outfile = new File(snippet.out); + outfile.createNewFile(); + PrintWriter writer = new PrintWriter(outfile); + writer.print(""); + writer.close(); + + writer = new PrintWriter(outfile); + writer.print(db.toString()); + writer.close(); + System.out.println("Done!"); } catch (Throwable err) { err.printStackTrace (); } diff --git a/src/main/java/edu/aalto/emn/Stop.java b/src/main/java/edu/aalto/emn/Stop.java new file mode 100644 index 0000000..ba92434 --- /dev/null +++ b/src/main/java/edu/aalto/emn/Stop.java @@ -0,0 +1,62 @@ +package edu.aalto.emn; + +import java.util.Map; + +import org.json.JSONObject; +import org.xml.sax.Attributes; + +public class Stop implements Jsonable { + private BusStop stop; + private int order; + private String arrival; + + @Override + public JSONObject toJson() { + JSONObject jstop = new JSONObject(); + jstop.put("id", this.getStop().getId()); + jstop.put("time", this.getArrival()); + jstop.put("name", this.getStop().getName()); + jstop.put("posX", this.getStop().getX()); + jstop.put("posY", this.getStop().getY()); + return jstop; + } + + public Stop(Attributes atts, Map stops) throws IllegalArgumentException { + this.stop = stops.get(atts.getValue("StationId")); + + if(this.stop == null) { + throw new IllegalArgumentException("Cannot find a specified stop"); + } + + this.order = Integer.parseInt(atts.getValue("Ix")); + this.arrival = atts.getValue("Arrival"); + + if(this.arrival == null) { + throw new IllegalArgumentException("Arrival was not specified"); + } + } + + public BusStop getStop() { + return stop; + } + + public void setStop(BusStop stop) { + this.stop = stop; + } + + public int getOrder() { + return order; + } + + public void setOrder(int order) { + this.order = order; + } + + public String getArrival() { + return arrival; + } + + public void setArrival(String arrival) { + this.arrival = arrival; + } +}