Audacity 3.2.0
XMLFileReader.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 XMLFileReader.cpp
6
7 Dominic Mazzoni
8
9*******************************************************************//*******************************************************************/
15
16#include "XMLFileReader.h"
17
18#include <wx/defs.h>
19#include <wx/ffile.h>
20#include <wx/intl.h>
21#include <wx/log.h>
22
23#include <string.h>
24
25#include "expat.h"
26
28{
29 mParser = XML_ParserCreate(NULL);
30 XML_SetUserData(mParser, (void *)this);
31 XML_SetElementHandler(mParser, startElement, endElement);
32 XML_SetCharacterDataHandler(mParser, charHandler);
33 mBaseHandler = NULL;
34 mHandler.reserve(128);
35}
36
38{
39 XML_ParserFree(mParser);
40}
41
43 const FilePath &fname)
44{
45 wxFFile theXMLFile(fname, wxT("rb"));
46 if (!theXMLFile.IsOpened()) {
47 mErrorStr = XO("Could not open file: \"%s\"").Format( fname );
48 return false;
49 }
50
51 mBaseHandler = baseHandler;
52
53 const size_t bufferSize = 16384;
54 char buffer[16384];
55 int done = 0;
56 do {
57 size_t len = fread(buffer, 1, bufferSize, theXMLFile.fp());
58 done = (len < bufferSize);
59 if (!XML_Parse(mParser, buffer, len, done)) {
60
61 // Embedded error string from expat doesn't translate (yet)
62 // We could make a table of XOs if we wanted so that it could
63 // If we do, uncomment the second constructor argument so it's not
64 // a verbatim string
66 XML_ErrorString(XML_GetErrorCode(mParser)) // , {}
67 );
68
69 mErrorStr = XO("Error: %s at line %lu").Format(
71 (long unsigned int)XML_GetCurrentLineNumber(mParser)
72 );
73
74 theXMLFile.Close();
75 return false;
76
77// If we did want to handle every single parse error, these are they....
78/*
79 XML_L("out of memory"),
80 XML_L("syntax error"),
81 XML_L("no element found"),
82 XML_L("not well-formed (invalid token)"),
83 XML_L("unclosed token"),
84 XML_L("partial character"),
85 XML_L("mismatched tag"),
86 XML_L("duplicate attribute"),
87 XML_L("junk after document element"),
88 XML_L("illegal parameter entity reference"),
89 XML_L("undefined entity"),
90 XML_L("recursive entity reference"),
91 XML_L("asynchronous entity"),
92 XML_L("reference to invalid character number"),
93 XML_L("reference to binary entity"),
94 XML_L("reference to external entity in attribute"),
95 XML_L("XML or text declaration not at start of entity"),
96 XML_L("unknown encoding"),
97 XML_L("encoding specified in XML declaration is incorrect"),
98 XML_L("unclosed CDATA section"),
99 XML_L("error in processing external entity reference"),
100 XML_L("document is not standalone"),
101 XML_L("unexpected parser state - please send a bug report"),
102 XML_L("entity declared in parameter entity"),
103 XML_L("requested feature requires XML_DTD support in Expat"),
104 XML_L("cannot change setting once parsing has begun"),
105 XML_L("unbound prefix"),
106 XML_L("must not undeclare prefix"),
107 XML_L("incomplete markup in parameter entity"),
108 XML_L("XML declaration not well-formed"),
109 XML_L("text declaration not well-formed"),
110 XML_L("illegal character(s) in public id"),
111 XML_L("parser suspended"),
112 XML_L("parser not suspended"),
113 XML_L("parsing aborted"),
114 XML_L("parsing finished"),
115 XML_L("cannot suspend in external parameter entity"),
116 XML_L("reserved prefix (xml) must not be undeclared or bound to another namespace name"),
117 XML_L("reserved prefix (xmlns) must not be declared or undeclared"),
118 XML_L("prefix must not be bound to one of the reserved namespace names")
119*/
120 }
121 } while (!done);
122
123 theXMLFile.Close();
124
125 // Even though there were no parse errors, we only succeed if
126 // the first-level handler actually got called, and didn't
127 // return false.
128 if (mBaseHandler)
129 return true;
130 else {
131 mErrorStr = XO("Could not load file: \"%s\"").Format( fname );
132 return false;
133 }
134}
135
137 const wxString &xmldata)
138{
139 auto utf8 = xmldata.ToUTF8();
140 const char *buffer = utf8.data();
141 int len = utf8.length();
142
143 mBaseHandler = baseHandler;
144
145 if (!ParseBuffer(baseHandler, utf8.data(), utf8.length(), true))
146 return false;
147
148 // Even though there were no parse errors, we only succeed if
149 // the first-level handler actually got called, and didn't
150 // return false.
151 if (!mBaseHandler)
152 {
153 mErrorStr = XO("Could not parse XML");
154 return false;
155 }
156
157 return true;
158}
159
161 XMLTagHandler* baseHandler, const MemoryStream& xmldata)
162{
163 mBaseHandler = baseHandler;
164
165 for (auto chunk : xmldata)
166 {
167 if (!ParseBuffer(baseHandler, static_cast<const char*>(chunk.first), chunk.second, false))
168 return false;
169 }
170
171 if (!ParseBuffer(baseHandler, nullptr, 0, true))
172 return false;
173
174 if (!mBaseHandler)
175 {
176 mErrorStr = XO("Could not parse XML");
177 return false;
178 }
179
180 return true;
181}
182
184{
185 return mErrorStr;
186}
187
189{
190 return mLibraryErrorStr;
191}
192
193// static
194void XMLFileReader::startElement(void *userData, const char *name,
195 const char **atts)
196{
197 XMLFileReader *This = (XMLFileReader *)userData;
198 Handlers &handlers = This->mHandler;
199
200 if (handlers.empty()) {
201 handlers.push_back(This->mBaseHandler);
202 }
203 else {
204 if (XMLTagHandler *const handler = handlers.back())
205 handlers.push_back(handler->ReadXMLChild(name));
206 else
207 handlers.push_back(NULL);
208 }
209
210 if (XMLTagHandler *& handler = handlers.back()) {
211 This->mCurrentTagAttributes.clear();
212
213 while(*atts)
214 {
215 const char* name = *atts++;
216 const char* value = *atts++;
217
218 This->mCurrentTagAttributes.emplace_back(
219 std::string_view(name), XMLAttributeValueView(std::string_view(value)));
220 }
221
222 if (!handler->HandleXMLTag(name, This->mCurrentTagAttributes)) {
223 handler = nullptr;
224 if (handlers.size() == 1)
225 This->mBaseHandler = nullptr;
226 }
227 }
228}
229
230// static
231void XMLFileReader::endElement(void *userData, const char *name)
232{
233 XMLFileReader *This = (XMLFileReader *)userData;
234 Handlers &handlers = This->mHandler;
235
236 if (XMLTagHandler *const handler = handlers.back())
237 handler->ReadXMLEndTag(name);
238
239 handlers.pop_back();
240}
241
242// static
243void XMLFileReader::charHandler(void *userData, const char *s, int len)
244{
245 XMLFileReader *This = (XMLFileReader *)userData;
246 Handlers &handlers = This->mHandler;
247
248 if (XMLTagHandler *const handler = handlers.back())
249 handler->ReadXMLContent(s, len);
250}
251
253 XMLTagHandler* baseHandler, const char* buffer, size_t len, bool isFinal)
254{
255 if (!XML_Parse(mParser, buffer, len, isFinal))
256 {
257
258 // Embedded error string from expat doesn't translate (yet)
259 // We could make a table of XOs if we wanted so that it could
260 // If we do, uncomment the second constructor argument so it's not
261 // a verbatim string
263 Verbatim(XML_ErrorString(XML_GetErrorCode(mParser)) // , {}
264 );
265
266 mErrorStr = XO("Error: %s at line %lu")
267 .Format(
269 (long unsigned int)XML_GetCurrentLineNumber(mParser));
270
271 wxLogMessage(
272 wxT("ParseString error: %s\n===begin===%s\n===end==="),
273 mErrorStr.Debug(), buffer);
274
275 return false;
276 }
277
278 return true;
279}
const TranslatableString name
Definition: Distortion.cpp:82
#define XO(s)
Definition: Internat.h:31
wxString FilePath
Definition: Project.h:20
TranslatableString Verbatim(wxString str)
Require calls to the one-argument constructor to go through this distinct global function name.
A low overhead memory stream with O(1) append, low heap fragmentation and a linear memory view.
Definition: MemoryStream.h:31
Holds a msgid for the translation catalog; may also bind format arguments.
wxString Debug() const
Format as an English string for debugging logs and developers' eyes, not for end users.
A view into an attribute value. The class does not take the ownership of the data.
Reads a file and passes the results through an XMLTagHandler.
Definition: XMLFileReader.h:19
bool ParseString(XMLTagHandler *baseHandler, const wxString &xmldata)
const TranslatableString & GetErrorStr() const
static void startElement(void *userData, const char *name, const char **atts)
bool ParseMemoryStream(XMLTagHandler *baseHandler, const MemoryStream &xmldata)
bool Parse(XMLTagHandler *baseHandler, const FilePath &fname)
const TranslatableString & GetLibraryErrorStr() const
static void endElement(void *userData, const char *name)
std::vector< XMLTagHandler * > Handlers
Definition: XMLFileReader.h:49
Handlers mHandler
Definition: XMLFileReader.h:50
TranslatableString mErrorStr
Definition: XMLFileReader.h:51
XML_Parser mParser
Definition: XMLFileReader.h:47
AttributesList mCurrentTagAttributes
Definition: XMLFileReader.h:55
XMLTagHandler * mBaseHandler
Definition: XMLFileReader.h:48
static void charHandler(void *userData, const char *s, int len)
bool ParseBuffer(XMLTagHandler *baseHandler, const char *buffer, size_t len, bool isFinal)
TranslatableString mLibraryErrorStr
Definition: XMLFileReader.h:52
This class is an interface which should be implemented by classes which wish to be able to load and s...
Definition: XMLTagHandler.h:42