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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/***************************************************************************
* Copyright (C) 2004 by Albert Astals Cid *
* tsdgeos@terra.es *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include <klocale.h>
#include "division.h"
#include "map.h"
#include "mapparser.h"
/* unitReader */
mapReader::mapReader() : TQXmlSimpleReader()
{
}
bool mapReader::parseMap(const TQString &path)
{
TQString aux;
p_map = new KGmap();
p_map -> setFile(path);
aux = path.left(path.tqfindRev('/') + 1); // aux = path but without the file name
mapParser handler(p_map, aux);
TQFile xmlFile(path);
if (xmlFile.exists())
{
TQXmlInputSource source(TQT_TQIODEVICE(&xmlFile));
setContentHandler(&handler);
if (parse(source)) return true;
p_error = handler.errorString();
}
else
{
p_error = i18n("%1 does not exist.").tqarg(path);
}
return false;
}
TQString mapReader::getError()
{
return p_error;
}
KGmap *mapReader::getMap()
{
return p_map;
}
/* mapParser */
mapParser::mapParser(KGmap *m, const TQString &path) : TQXmlDefaultHandler(), p_map(m), p_path(path)
{
}
bool mapParser::startDocument()
{
p_mapNameSet = false;
p_mapFileSet = false;
return true;
}
bool mapParser::startElement(const TQString&, const TQString &name, const TQString&, const TQXmlAttributes&)
{
TQString prev;
bool b = true;
prev = getPreviousTag();
p_previousTags += ":" + name;
if (prev.isEmpty())
{
b = name == "map";
if (!b) p_error = i18n("The map description file should begin with the %1 tag").tqarg("map");
}
else if (prev == "map")
{
if (name != "mapFile" && name != "name" && name != "division")
{
b = false;
p_error = i18n("%1 is not a valid tag inside tag %2. Valid tags are %3, %4 and %5").tqarg(name).tqarg(prev).tqarg("mapFile").tqarg("name").tqarg("division");
}
else if ((name == "mapFile" && p_mapFileSet) || (name == "name" && p_mapNameSet))
{
b = false;
p_error = i18n("%1 tag has already been set").tqarg(name);
}
p_colorSet = false;
if (name == "division")
{
p_divisionNameSet = false;
p_divisionIgnoreSet = false;
p_flagFileSet = false;
p_capitalSet = false;
p_division = new division();
}
}
else if (prev == "mapFile" || prev == "name" || prev == "red" || prev == "green" || prev == "blue" ||
prev == "ignore")
{
b = false;
p_error = i18n("There can not be a tag inside %1 tag").tqarg(prev);
}
else if (prev == "division")
{
if (name != "color" && name != "name" && name != "ignore" && name != "flag" && name != "capital")
{
b = false;
p_error = i18n("%1 is not a valid tag inside tag %2. Valid tags are %3, %4, %5, %6 and %7").tqarg(name).tqarg(prev).tqarg("color").tqarg("name").tqarg("ignore").tqarg("capital").tqarg("flag");
}
else if ((name == "name" && p_divisionNameSet) || (name == "color" && p_colorSet) ||
(name == "ignore" && p_divisionIgnoreSet) || (name == "flag" && p_flagFileSet) ||
(name == "capital" && p_capitalSet))
{
b = false;
p_error = i18n("%1 tag has already been set").tqarg(name);
}
p_red = -1;
p_green = -1;
p_blue = -1;
}
else if (prev == "color")
{
b = (name == "red" && p_red == -1) || (name == "green" && p_green == -1) ||
(name == "blue" && p_blue == -1);
}
else b = false;
return b;
}
bool mapParser::endElement(const TQString &, const TQString &, const TQString &)
{
TQString aux;
bool b;
aux = getPreviousTag();
b = true;
if (p_previousTags == ":map:name")
{
p_map -> setName(p_contents);
p_mapNameSet = true;
}
else if (p_previousTags == ":map:mapFile")
{
b = p_map -> setMapFile(p_path + p_contents);
p_mapFileSet = true;
if (!b) p_error = i18n("File %1 does not exist").tqarg(p_path + p_contents);
}
else if (aux == "division")
{
p_division -> setRGB(p_red, p_green , p_blue);
b = p_divisionNameSet;
if (!b) p_error = i18n("There is a division without name");
b = b && p_map -> addDivision(p_division);
if (!b) p_error = i18n("There is already either a division called %1 or a division with the same colors as %2").tqarg(p_division -> getName()).tqarg(p_division -> getName());
b = b && (p_capitalSet || !p_division -> canAsk(false));
if (!b) p_error = i18n("Division %1 has no capital").tqarg(p_division -> getName());
}
else if (p_previousTags == ":map:division:name")
{
p_divisionNameSet = true;
p_division -> setName(p_contents);
}
else if (p_previousTags == ":map:division:capital")
{
p_capitalSet = true;
p_division -> setCapital(p_contents);
}
else if (aux == "color")
{
if (p_red == -1)
{
b = false;
p_error = i18n("Tag %1 has not the %2 tag.").tqarg("<color>").tqarg("<red>");
}
else if (p_green == -1)
{
b = false;
p_error = i18n("Tag %1 has not the %2 tag.").tqarg("<color>").tqarg("<green>");
}
else if (p_blue == -1)
{
b = false;
p_error = i18n("Tag %1 has not the %2 tag.").tqarg("<color>").tqarg("<blue>");
}
else p_colorSet = true;
}
else if (aux == "red")
{
p_red = p_contents.toInt();
}
else if (aux == "green")
{
p_green = p_contents.toInt();
}
else if (aux == "blue")
{
p_blue = p_contents.toInt();
}
else if (aux == "ignore")
{
p_divisionIgnoreSet = true;
if (p_contents.lower() == "yes")
{
p_division -> setIgnore(true, false);
}
else if (p_contents.lower() == "no")
{
p_division -> setIgnore(false, false);
}
else if (p_contents.lower() == "allowclickmode")
{
p_division -> setIgnore(true, true);
}
else
{
b = false;
p_error = i18n("Invalid value in tag %1").tqarg("<ignore>");
}
}
else if (aux == "flag")
{
b = p_division -> setFlagFile(p_path + "flags/" + p_contents);
p_flagFileSet = true;
if (!b) p_error = i18n("Could not find flag file %1").tqarg(p_path + "flags/" + p_contents);
}
else if (aux == "map")
{
}
else b = false;
removeLastTag();
p_contents = TQString();
return b;
}
bool mapParser::characters(const TQString &ch)
{
TQString aux;
if (ch.simplifyWhiteSpace().length() == 0) return true;
aux = getPreviousTag();
if (aux == "mapFile" || aux == "name" || aux == "red" || aux == "green" || aux == "blue" || aux == "ignore" || aux == "flag" || aux == "capital")
{
p_contents += ch;
return true;
}
p_error = i18n("There are characters outside tags.");
return false;
}
bool mapParser::endDocument()
{
TQString aux;
if (p_mapNameSet && p_mapFileSet)
{
return true;
}
else if (!p_mapNameSet) aux = "name";
else if (!p_mapFileSet) aux = "mapFile";
p_error = i18n("Tag %1 is missing.").tqarg(aux);
return false;
}
TQString mapParser::errorString()
{
if (!p_error.isEmpty()) return p_error;
return i18n("The XML document is malformed.");
}
TQString mapParser::getPreviousTag() const
{
return p_previousTags.right(p_previousTags.length() - p_previousTags.tqfindRev(':') - 1);
}
void mapParser::removeLastTag()
{
p_previousTags = p_previousTags.left(p_previousTags.tqfindRev(':'));
}
|