01.
import
java.util.ArrayList;
02.
import
org.xml.sax.Attributes;
03.
import
org.xml.sax.ContentHandler;
04.
import
org.xml.sax.Locator;
05.
import
org.xml.sax.SAXException;
06.
07.
public
class
ScoreboardXmlParser
implements
ContentHandler {
08.
private
long
lastUpdated;
09.
private
int
totalPlayers;
10.
private
int
totalSubmissionsFromPlayer;
11.
private
ArrayList<Score> highScores;
12.
private
ArrayList<Score> playerScores;
13.
private
ArrayList<Score> m_currentList;
14.
public
Scoreboard scoreboard;
15.
16.
@Override
17.
public
void
startDocument()
throws
SAXException {
18.
highScores =
new
ArrayList<Score>();
19.
playerScores =
new
ArrayList<Score>();
20.
lastUpdated =
0
;
21.
totalPlayers =
0
;
22.
totalSubmissionsFromPlayer =
0
;
23.
}
24.
25.
@Override
26.
public
void
endDocument()
throws
SAXException {
27.
scoreboard =
new
Scoreboard();
28.
scoreboard.lastUpdated = lastUpdated;
29.
scoreboard.totalPlayers = totalPlayers;
30.
scoreboard.totalSubmissionsFromPlayer = totalSubmissionsFromPlayer;
31.
scoreboard.highScores =
new
ArrayList<Score>(highScores);
32.
scoreboard.playerScores =
new
ArrayList<Score>(playerScores);
33.
}
34.
35.
@Override
36.
public
void
startElement(String uri, String localName, String name, Attributes atts)
throws
SAXException {
37.
if
(localName.equals(
"scoreboard"
)) {
38.
lastUpdated = Long.parseLong(atts.getValue(
"lastUpdated"
));
39.
}
40.
else
if
(localName.equals(
"highscores"
)) {
41.
totalPlayers = Integer.parseInt(atts.getValue(
"players"
));
42.
m_currentList = highScores;
43.
}
44.
else
if
(localName.equals(
"scores"
)) {
45.
totalSubmissionsFromPlayer = Integer.parseInt(atts.getValue(
"submitted"
));
46.
m_currentList = playerScores;
47.
}
48.
49.
if
(localName.equals(
"score"
)) {
50.
m_currentList.add(
new
Score(atts));
51.
}
52.
}
53.
54.
@Override
55.
public
void
endElement(String uri, String localName, String name)
throws
SAXException {
56.
if
(localName.equals(
"highscores"
) || (localName.equals(
"scores"
))) {
57.
m_currentList =
null
;
58.
}
59.
}
60.
}