-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSAXHandler.java
149 lines (127 loc) · 4.1 KB
/
SAXHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
SAXHandler.java
Author: Shreyas Jayanna
Version 1
Date: 05/15/2015
*/
// Import statements
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.ArrayList;
import java.util.List;
/**
* Class SAXHandler. This class defines a handler to read XML file
*/
public class SAXHandler extends DefaultHandler {
MyNode myNode;
List<MyNode> myNodeList;
static MyEdge myEdge;
List<MyEdge> myEdgeList;
RoadGraph roadGraph;
Connections connections;
String from;
String to;
String content;
/**
* Constructor
* @param roadGraph Road graph object
* @param connections Connections object
*/
public SAXHandler(RoadGraph roadGraph, Connections connections) {
myNode = null;
myNodeList = new ArrayList<MyNode>();
myEdge = null;
myEdgeList = new ArrayList<MyEdge>();
this.roadGraph = roadGraph;
this.connections = connections;
this.content = null;
}
/**
* This method reads the start element in the XML tag
* @param uri URI
* @param localName Local name
* @param qName Name of the element
* @param attributes attributes
* @throws SAXException
*/
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if(qName.equals("node")) {
myNode = new MyNode(attributes.getValue("id"));
} else if(qName.equals("edge")) {
if(attributes.getValue("from") != null) {
myEdge = new MyEdge(attributes.getValue("id"),
this.roadGraph.getNodes().get(attributes.getValue("from")),
this.roadGraph.getNodes().get(attributes.getValue("to")));
} else {
myEdge = null;
}
}
if(qName.equals("lane")) {
if(myEdge != null) {
double len = Double.parseDouble(attributes.getValue("length"));
myEdge.setLength(len);
double speed = Double.parseDouble(attributes.getValue("speed"));
myEdge.setSpeedLimit(speed);
double time = len/speed;
myEdge.setTime(time);
}
}
if(qName.equals("connection")) {
this.from = new String(attributes.getValue("from"));
this.to = new String(attributes.getValue("to"));
//System.out.println("Connection from " + this.from + " to " + this.to);
}
}
/**
* This method reads the end element of the XML tag
* @param uri URI
* @param localName Local name
* @param qName Name of the element
* @throws SAXException
*/
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if(qName.equals("node")) {
this.myNodeList.add(myNode);
} else if(qName.equals("edge")) {
if(myEdge != null)
this.myEdgeList.add(myEdge);
} else if(qName.equals("connection")) {
this.connections.addConnection(this.from, this.to);
}
}
/**
* This method extracts the characters and sets it into a variable.
* @param ch Character array
* @param start start position
* @param length Length
* @throws SAXException
*/
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
content = String.copyValueOf(ch, start, length).trim();
}
/**
* This method returns the node list
* @return List of nodes
*/
public List<MyNode> getMyNodeList() {
return this.myNodeList;
}
/**
* This method returns the edge list
* @return List of edges
*/
public List<MyEdge> getMyEdgeList() {
return this.myEdgeList;
}
/**
* This method returns the connections
* @return map of connections
*/
public Connections getConnections() {
return this.connections;
}
}