Skip to content

Commit

Permalink
Added jsonbale interface, filter non-buses
Browse files Browse the repository at this point in the history
  • Loading branch information
tinypony committed Jan 16, 2015
1 parent 3da3c2b commit aa6f579
Show file tree
Hide file tree
Showing 9 changed files with 309 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
Expand Down
9 changes: 6 additions & 3 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -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
23 changes: 22 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,26 @@
<artifactId>jcommander</artifactId>
<version>1.30</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
</dependencies>
</project>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
82 changes: 73 additions & 9 deletions src/main/java/edu/aalto/emn/Bus.java
Original file line number Diff line number Diff line change
@@ -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<String, BusStop> stops; // time - stop
private int route;
public class Bus implements Jsonable {

private String serviceID;
private String serviceNbr;
private ArrayList<Stop> stops; //stop
private String route;
private String companyId;
private String trnsmode;

public Bus() {
this.stops = new HashMap<String, BusStop>();
this.stops = new ArrayList<Stop>();
}

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<Stop> 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;
}
}
3 changes: 2 additions & 1 deletion src/main/java/edu/aalto/emn/BusStop.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
144 changes: 96 additions & 48 deletions src/main/java/edu/aalto/emn/DBHandler.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,60 +12,107 @@
import org.xml.sax.helpers.DefaultHandler;

public class DBHandler extends DefaultHandler {
private Map<String, BusStop> stops;
private List<Bus> buses;
private int route;
private Map<String, BusStop> stops;
private ArrayList<Bus> buses;

private Stack<String> elementStack = new Stack<String>();
private Stack<Object> objectStack = new Stack<Object>();
private List<String> 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<Bus>();
}
private Stack<String> elementStack = new Stack<String>();
private Stack<Object> objectStack = new Stack<Object>();


public List<Bus> 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<String, BusStop>();
this.buses = new ArrayList<Bus>();
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
public List<Bus> getBuses() {
return this.buses;
}

this.elementStack.push(qName);
public Map<String, BusStop> 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());
}
}
8 changes: 8 additions & 0 deletions src/main/java/edu/aalto/emn/Jsonable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package edu.aalto.emn;

import org.json.JSONObject;

public interface Jsonable {

public JSONObject toJson();
}
Loading

0 comments on commit aa6f579

Please sign in to comment.