Skip to content

Commit

Permalink
Initil
Browse files Browse the repository at this point in the history
  • Loading branch information
tinypony committed Jan 14, 2015
1 parent 7c54ab0 commit 3da3c2b
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<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">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/target
23 changes: 23 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>snippets</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
5 changes: 5 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.5
4 changes: 4 additions & 0 deletions .settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.aalto.emn</groupId>
<artifactId>snippets</artifactId>
<version>1.0-SNAPSHOT</version>
<description>Project for analyzing bus schedule data</description>
<dependencies>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.30</version>
</dependency>
</dependencies>
</project>
22 changes: 22 additions & 0 deletions src/main/java/edu/aalto/emn/Bus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package edu.aalto.emn;

import java.util.HashMap;
import java.util.Map;

public class Bus {

private Map<String, BusStop> stops; // time - stop
private int route;

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

public void setRoute(int route) {
this.route = route;
}

public void addStop(String time, BusStop stop) {
this.stops.put(time, stop);
}
}
80 changes: 80 additions & 0 deletions src/main/java/edu/aalto/emn/BusStop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package edu.aalto.emn;

import org.xml.sax.Attributes;


public class BusStop {
private String id;
private String name;
private String x;
private String y;

public BusStop() {

}

public BusStop(Attributes atts) {
String name = atts.getValue("Name");
String id = atts.getValue("StationId");
String x = atts.getValue("X");
String y = atts.getValue("Y");

if(name != null) {
this.setName(name);
}

if(id !=null ) {
this.setId(id);
}

if(x !=null) {
this.setX(x);
}

if(y !=null) {
this.setY(y);
}
}

public BusStop(String id, String name, String x, String y) {
this.setId(id);
this.setName(name);
this.setX(x);
this.setY(y);
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getX() {
return x;
}

public void setX(String x) {
this.x = x;
}

public String getY() {
return y;
}

public void setY(String y) {
this.y = y;
}



}
70 changes: 70 additions & 0 deletions src/main/java/edu/aalto/emn/DBHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package edu.aalto.emn;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

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

private Stack<String> elementStack = new Stack<String>();
private Stack<Object> objectStack = new Stack<Object>();

public DBHandler(int route) {
this.route = route;
this.stops = new HashMap();
this.buses = new ArrayList<Bus>();
}

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));
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

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();
this.objectStack.push(bus);
}
}

public void endElement(String uri, String localName, String qName) throws SAXException {

this.elementStack.pop();
String qNameLow = qName.toLowerCase();

if ("station".equals(qNameLow) || "service".equals(qNameLow)) {
Object object = this.objectStack.pop();
}
}
}
40 changes: 40 additions & 0 deletions src/main/java/edu/aalto/emn/SnippetMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package edu.aalto.emn;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import com.beust.jcommander.Parameter;

public class SnippetMain {

@Parameter(names = { "-route" }, description = "Route number")
private static Integer route = 23;

@Parameter(names = "-xml", description = "Path to XML database dump", required=true)
private static String path;

public static void main(String[] args) {
File xmlFile = new File(args[0]);

SAXParserFactory factory = SAXParserFactory.newInstance();
try {
InputStream xmlInput = new FileInputStream(args[0]);

SAXParser saxParser = factory.newSAXParser();
DBHandler handler = new DBHandler(route);
saxParser.parse(xmlInput, handler);

for(Bus bus : handler.getBuses()){
// System.out.println(bus);
}
System.out.println("Done");
} catch (Throwable err) {
err.printStackTrace ();
}
}

}

0 comments on commit 3da3c2b

Please sign in to comment.