Statistics - initial version
[scorecard] / src / xml-parser.cpp
1 #include <QtGui>
2
3 #include <QDebug>
4
5 #include "xml-parser.h"
6 #include "data.h"
7
8 //
9 // Score
10 //
11 ScoreHandler::ScoreHandler(QList<Score *> &scoreList) : scoreList(scoreList)
12 {
13   score = 0;
14   hole = 0;
15 }
16
17 bool ScoreHandler::startDocument()
18 {
19   inScore = false;
20   return true;
21 }
22
23 bool ScoreHandler::endDocument()
24 {
25   return true;
26 }
27
28 bool ScoreHandler::startElement(const QString & /* namespaceURI */,
29                                 const QString & /* localName */,
30                                 const QString &name,
31                                 const QXmlAttributes &attrs)
32 {
33   if (inScore && name == "hole") {
34     hole = new Hole(attrs);
35   }
36   else if (name == "score") {
37     score = new Score(attrs);
38     inScore = true;
39   }
40   return true;
41 }
42
43 bool ScoreHandler::endElement(const QString & /* namespaceURI */,
44                               const QString & /* localName */,
45                               const QString &name)
46 {
47   if (name == "score") {
48     inScore = false;
49     scoreList << score;
50   } else if (name == "hole")
51     score->addHole(hole);
52
53   return true;
54 }
55
56 bool ScoreHandler::fatalError(const QXmlParseException &exception)
57 {
58   qWarning() << "Fatal error on line" << exception.lineNumber()
59              << ", column" << exception.columnNumber() << ":"
60              << exception.message();
61
62   return false;
63 }
64
65 QString ScoreHandler::errorString() const
66 {
67   return errorStr;
68 }
69
70 //
71 // Club
72 //
73 ClubHandler::ClubHandler(QList<Club *> &clubList) : clubList(clubList)
74 {
75   club = 0;
76   hole = 0;
77 }
78
79 bool ClubHandler::startDocument()
80 {
81   inClub = false;
82   inCourse = false;
83   return true;
84 }
85
86 bool ClubHandler::endDocument()
87 {
88   return true;
89 }
90
91 bool ClubHandler::startElement(const QString & /* namespaceURI */,
92                                const QString & /* localName */,
93                                const QString &name,
94                                const QXmlAttributes &attrs)
95 {
96   if (inCourse && name == "hole") {
97     hole = new Hole(attrs);
98   }
99   else if (name == "course") {
100     course = new Course(attrs);
101     inCourse = true;
102   }  
103   else if (name == "club") {
104     club = new Club(attrs);
105     inClub = true;
106   }
107   return true;
108 }
109
110 bool ClubHandler::endElement(const QString & /* namespaceURI */,
111                              const QString & /* localName */,
112                              const QString &name)
113 {
114   if (name == "club") {
115     inClub = false;
116     clubList << club;
117   } 
118   else if (name == "course") {
119     inCourse = false;
120     club->addCourse(course);
121   }
122   else if (name == "hole")
123     course->addHole(hole);
124
125   return true;
126 }
127
128 bool ClubHandler::fatalError(const QXmlParseException &exception)
129 {
130   qWarning() << "Fatal error on line" << exception.lineNumber()
131              << ", column" << exception.columnNumber() << ":"
132              << exception.message();
133
134   return false;
135 }
136
137 QString ClubHandler::errorString() const
138 {
139   return errorStr;
140 }