Audacity 3.2.0
Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
XMLFileReader Class Referencefinal

Reads a file and passes the results through an XMLTagHandler. More...

#include <XMLFileReader.h>

Collaboration diagram for XMLFileReader:
[legend]

Public Member Functions

 XMLFileReader ()
 
 ~XMLFileReader ()
 
bool Parse (XMLTagHandler *baseHandler, const FilePath &fname)
 
bool ParseString (XMLTagHandler *baseHandler, const wxString &xmldata)
 
bool ParseMemoryStream (XMLTagHandler *baseHandler, const MemoryStream &xmldata)
 
const TranslatableStringGetErrorStr () const
 
const TranslatableStringGetLibraryErrorStr () const
 

Static Public Member Functions

static void startElement (void *userData, const char *name, const char **atts)
 
static void endElement (void *userData, const char *name)
 
static void charHandler (void *userData, const char *s, int len)
 

Private Types

using Handlers = std::vector< XMLTagHandler * >
 

Private Member Functions

bool ParseBuffer (XMLTagHandler *baseHandler, const char *buffer, size_t len, bool isFinal)
 

Private Attributes

XML_Parser mParser
 
XMLTagHandlermBaseHandler
 
Handlers mHandler
 
TranslatableString mErrorStr
 
TranslatableString mLibraryErrorStr
 
AttributesList mCurrentTagAttributes
 

Detailed Description

Reads a file and passes the results through an XMLTagHandler.

Definition at line 19 of file XMLFileReader.h.

Member Typedef Documentation

◆ Handlers

using XMLFileReader::Handlers = std::vector<XMLTagHandler*>
private

Definition at line 49 of file XMLFileReader.h.

Constructor & Destructor Documentation

◆ XMLFileReader()

XMLFileReader::XMLFileReader ( )

Definition at line 27 of file XMLFileReader.cpp.

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}
static void startElement(void *userData, const char *name, const char **atts)
static void endElement(void *userData, const char *name)
Handlers mHandler
Definition: XMLFileReader.h:50
XML_Parser mParser
Definition: XMLFileReader.h:47
XMLTagHandler * mBaseHandler
Definition: XMLFileReader.h:48
static void charHandler(void *userData, const char *s, int len)

References charHandler(), endElement(), mBaseHandler, mHandler, mParser, and startElement().

Here is the call graph for this function:

◆ ~XMLFileReader()

XMLFileReader::~XMLFileReader ( )

Definition at line 37 of file XMLFileReader.cpp.

38{
39 XML_ParserFree(mParser);
40}

References mParser.

Member Function Documentation

◆ charHandler()

void XMLFileReader::charHandler ( void *  userData,
const char *  s,
int  len 
)
static

Definition at line 243 of file XMLFileReader.cpp.

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}
Reads a file and passes the results through an XMLTagHandler.
Definition: XMLFileReader.h:19
std::vector< XMLTagHandler * > Handlers
Definition: XMLFileReader.h:49
This class is an interface which should be implemented by classes which wish to be able to load and s...
Definition: XMLTagHandler.h:42

References cloud::audiocom::anonymous_namespace{AuthorizationHandler.cpp}::handler, and mHandler.

Referenced by XMLFileReader().

Here is the caller graph for this function:

◆ endElement()

void XMLFileReader::endElement ( void *  userData,
const char *  name 
)
static

Definition at line 231 of file XMLFileReader.cpp.

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}
const TranslatableString name
Definition: Distortion.cpp:82

References cloud::audiocom::anonymous_namespace{AuthorizationHandler.cpp}::handler, mHandler, and name.

Referenced by XMLFileReader().

Here is the caller graph for this function:

◆ GetErrorStr()

const TranslatableString & XMLFileReader::GetErrorStr ( ) const

Definition at line 183 of file XMLFileReader.cpp.

184{
185 return mErrorStr;
186}
TranslatableString mErrorStr
Definition: XMLFileReader.h:51

References mErrorStr.

Referenced by AUPImportFileHandle::Import(), EffectEqualization::LoadCurves(), VSTEffectWrapper::LoadXML(), KeyConfigPrefs::OnImport(), and TagsEditorDialog::OnLoad().

Here is the caller graph for this function:

◆ GetLibraryErrorStr()

const TranslatableString & XMLFileReader::GetLibraryErrorStr ( ) const

Definition at line 188 of file XMLFileReader.cpp.

189{
190 return mLibraryErrorStr;
191}
TranslatableString mLibraryErrorStr
Definition: XMLFileReader.h:52

References mLibraryErrorStr.

◆ Parse()

bool XMLFileReader::Parse ( XMLTagHandler baseHandler,
const FilePath fname 
)

Definition at line 42 of file XMLFileReader.cpp.

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}
wxT("CloseDown"))
#define XO(s)
Definition: Internat.h:31
TranslatableString Verbatim(wxString str)
Require calls to the one-argument constructor to go through this distinct global function name.

References mBaseHandler, mErrorStr, mLibraryErrorStr, mParser, Verbatim(), wxT(), and XO.

Referenced by FFmpegPresets::FFmpegPresets(), AUPImportFileHandle::Import(), FFmpegPresets::ImportPresets(), EffectEqualization::LoadCurves(), anonymous_namespace{PluginMenus.cpp}::LoadEffectsMenuGroups(), VSTEffectWrapper::LoadXML(), KeyConfigPrefs::OnImport(), TagsEditorDialog::OnLoad(), and EffectEqualization::UpdateDefaultCurves().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseBuffer()

bool XMLFileReader::ParseBuffer ( XMLTagHandler baseHandler,
const char *  buffer,
size_t  len,
bool  isFinal 
)
private

Definition at line 252 of file XMLFileReader.cpp.

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}
wxString Debug() const
Format as an English string for debugging logs and developers' eyes, not for end users.

References TranslatableString::Debug(), mErrorStr, mLibraryErrorStr, mParser, Verbatim(), wxT(), and XO.

Referenced by ParseMemoryStream(), and ParseString().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseMemoryStream()

bool XMLFileReader::ParseMemoryStream ( XMLTagHandler baseHandler,
const MemoryStream xmldata 
)

Definition at line 160 of file XMLFileReader.cpp.

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}
bool ParseBuffer(XMLTagHandler *baseHandler, const char *buffer, size_t len, bool isFinal)

References mBaseHandler, mErrorStr, ParseBuffer(), and XO.

Here is the call graph for this function:

◆ ParseString()

bool XMLFileReader::ParseString ( XMLTagHandler baseHandler,
const wxString &  xmldata 
)

Definition at line 136 of file XMLFileReader.cpp.

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}

References mBaseHandler, mErrorStr, ParseBuffer(), and XO.

Referenced by AsyncPluginValidator::Impl::OnDataAvailable(), and UpdateDataParser::Parse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ startElement()

void XMLFileReader::startElement ( void *  userData,
const char *  name,
const char **  atts 
)
static

Definition at line 194 of file XMLFileReader.cpp.

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}
A view into an attribute value. The class does not take the ownership of the data.
AttributesList mCurrentTagAttributes
Definition: XMLFileReader.h:55

References cloud::audiocom::anonymous_namespace{AuthorizationHandler.cpp}::handler, mBaseHandler, mCurrentTagAttributes, mHandler, and name.

Referenced by XMLFileReader().

Here is the caller graph for this function:

Member Data Documentation

◆ mBaseHandler

XMLTagHandler* XMLFileReader::mBaseHandler
private

Definition at line 48 of file XMLFileReader.h.

Referenced by Parse(), ParseMemoryStream(), ParseString(), startElement(), and XMLFileReader().

◆ mCurrentTagAttributes

AttributesList XMLFileReader::mCurrentTagAttributes
private

Definition at line 55 of file XMLFileReader.h.

Referenced by startElement().

◆ mErrorStr

TranslatableString XMLFileReader::mErrorStr
private

Definition at line 51 of file XMLFileReader.h.

Referenced by GetErrorStr(), Parse(), ParseBuffer(), ParseMemoryStream(), and ParseString().

◆ mHandler

Handlers XMLFileReader::mHandler
private

Definition at line 50 of file XMLFileReader.h.

Referenced by charHandler(), endElement(), startElement(), and XMLFileReader().

◆ mLibraryErrorStr

TranslatableString XMLFileReader::mLibraryErrorStr
private

Definition at line 52 of file XMLFileReader.h.

Referenced by GetLibraryErrorStr(), Parse(), and ParseBuffer().

◆ mParser

XML_Parser XMLFileReader::mParser
private

Definition at line 47 of file XMLFileReader.h.

Referenced by Parse(), ParseBuffer(), XMLFileReader(), and ~XMLFileReader().


The documentation for this class was generated from the following files: