forked from tenacityteam/tenacity-legacy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXMLFileReader.cpp
233 lines (193 loc) · 6.72 KB
/
XMLFileReader.cpp
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
/**********************************************************************
Audacity: A Digital Audio Editor
XMLFileReader.cpp
Dominic Mazzoni
*******************************************************************//**
\class XMLFileReader
\brief Reads a file and passes the results through an XMLTagHandler.
*//*******************************************************************/
#include "XMLFileReader.h"
#include <wx/defs.h>
#include <wx/ffile.h>
#include <wx/intl.h>
#include <string.h>
#include "expat.h"
XMLFileReader::XMLFileReader()
{
mParser = XML_ParserCreate(NULL);
XML_SetUserData(mParser, (void *)this);
XML_SetElementHandler(mParser, startElement, endElement);
XML_SetCharacterDataHandler(mParser, charHandler);
mBaseHandler = NULL;
mHandler.reserve(128);
}
XMLFileReader::~XMLFileReader()
{
XML_ParserFree(mParser);
}
bool XMLFileReader::Parse(XMLTagHandler *baseHandler,
const FilePath &fname)
{
wxFFile theXMLFile(fname, wxT("rb"));
if (!theXMLFile.IsOpened()) {
mErrorStr = XO("Could not open file: \"%s\"").Format( fname );
return false;
}
mBaseHandler = baseHandler;
const size_t bufferSize = 16384;
char buffer[16384];
int done = 0;
do {
size_t len = fread(buffer, 1, bufferSize, theXMLFile.fp());
done = (len < bufferSize);
if (!XML_Parse(mParser, buffer, len, done)) {
// Embedded error string from expat doesn't translate (yet)
// We could make a table of XOs if we wanted so that it could
// If we do, uncomment the second constructor argument so it's not
// a verbatim string
mLibraryErrorStr = Verbatim(
XML_ErrorString(XML_GetErrorCode(mParser)) // , {}
);
mErrorStr = XO("Error: %s at line %lu").Format(
mLibraryErrorStr,
(long unsigned int)XML_GetCurrentLineNumber(mParser)
);
theXMLFile.Close();
return false;
// If we did want to handle every single parse error, these are they....
/*
XML_L("out of memory"),
XML_L("syntax error"),
XML_L("no element found"),
XML_L("not well-formed (invalid token)"),
XML_L("unclosed token"),
XML_L("partial character"),
XML_L("mismatched tag"),
XML_L("duplicate attribute"),
XML_L("junk after document element"),
XML_L("illegal parameter entity reference"),
XML_L("undefined entity"),
XML_L("recursive entity reference"),
XML_L("asynchronous entity"),
XML_L("reference to invalid character number"),
XML_L("reference to binary entity"),
XML_L("reference to external entity in attribute"),
XML_L("XML or text declaration not at start of entity"),
XML_L("unknown encoding"),
XML_L("encoding specified in XML declaration is incorrect"),
XML_L("unclosed CDATA section"),
XML_L("error in processing external entity reference"),
XML_L("document is not standalone"),
XML_L("unexpected parser state - please send a bug report"),
XML_L("entity declared in parameter entity"),
XML_L("requested feature requires XML_DTD support in Expat"),
XML_L("cannot change setting once parsing has begun"),
XML_L("unbound prefix"),
XML_L("must not undeclare prefix"),
XML_L("incomplete markup in parameter entity"),
XML_L("XML declaration not well-formed"),
XML_L("text declaration not well-formed"),
XML_L("illegal character(s) in public id"),
XML_L("parser suspended"),
XML_L("parser not suspended"),
XML_L("parsing aborted"),
XML_L("parsing finished"),
XML_L("cannot suspend in external parameter entity"),
XML_L("reserved prefix (xml) must not be undeclared or bound to another namespace name"),
XML_L("reserved prefix (xmlns) must not be declared or undeclared"),
XML_L("prefix must not be bound to one of the reserved namespace names")
*/
}
} while (!done);
theXMLFile.Close();
// Even though there were no parse errors, we only succeed if
// the first-level handler actually got called, and didn't
// return false.
if (mBaseHandler)
return true;
else {
mErrorStr = XO("Could not load file: \"%s\"").Format( fname );
return false;
}
}
bool XMLFileReader::ParseString(XMLTagHandler *baseHandler,
const wxString &xmldata)
{
auto utf8 = xmldata.ToUTF8();
const char *buffer = utf8.data();
int len = utf8.length();
mBaseHandler = baseHandler;
if (!XML_Parse(mParser, buffer, len, true))
{
// Embedded error string from expat doesn't translate (yet)
// We could make a table of XOs if we wanted so that it could
// If we do, uncomment the second constructor argument so it's not
// a verbatim string
mLibraryErrorStr = Verbatim(
XML_ErrorString(XML_GetErrorCode(mParser)) // , {}
);
mErrorStr = XO("Error: %s at line %lu").Format(
mLibraryErrorStr,
(long unsigned int)XML_GetCurrentLineNumber(mParser)
);
wxLogMessage(wxT("ParseString error: %s\n===begin===%s\n===end==="), mErrorStr.Debug(), buffer);
return false;
}
// Even though there were no parse errors, we only succeed if
// the first-level handler actually got called, and didn't
// return false.
if (!mBaseHandler)
{
mErrorStr = XO("Could not parse XML");
return false;
}
return true;
}
const TranslatableString &XMLFileReader::GetErrorStr() const
{
return mErrorStr;
}
const TranslatableString &XMLFileReader::GetLibraryErrorStr() const
{
return mLibraryErrorStr;
}
// static
void XMLFileReader::startElement(void *userData, const char *name,
const char **atts)
{
XMLFileReader *This = (XMLFileReader *)userData;
Handlers &handlers = This->mHandler;
if (handlers.empty()) {
handlers.push_back(This->mBaseHandler);
}
else {
if (XMLTagHandler *const handler = handlers.back())
handlers.push_back(handler->ReadXMLChild(name));
else
handlers.push_back(NULL);
}
if (XMLTagHandler *& handler = handlers.back()) {
if (!handler->ReadXMLTag(name, atts)) {
handler = nullptr;
if (handlers.size() == 1)
This->mBaseHandler = nullptr;
}
}
}
// static
void XMLFileReader::endElement(void *userData, const char *name)
{
XMLFileReader *This = (XMLFileReader *)userData;
Handlers &handlers = This->mHandler;
if (XMLTagHandler *const handler = handlers.back())
handler->ReadXMLEndTag(name);
handlers.pop_back();
}
// static
void XMLFileReader::charHandler(void *userData, const char *s, int len)
{
XMLFileReader *This = (XMLFileReader *)userData;
Handlers &handlers = This->mHandler;
if (XMLTagHandler *const handler = handlers.back())
handler->ReadXMLContent(s, len);
}