Audacity  3.0.3
Public Member Functions | Static Public Member Functions | Private Types | 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)
 
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 Attributes

XML_Parser mParser
 
XMLTagHandlermBaseHandler
 
Handlers mHandler
 
TranslatableString mErrorStr
 
TranslatableString mLibraryErrorStr
 

Detailed Description

Reads a file and passes the results through an XMLTagHandler.

Definition at line 20 of file XMLFileReader.h.

Member Typedef Documentation

◆ Handlers

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

Definition at line 45 of file XMLFileReader.h.

Constructor & Destructor Documentation

◆ XMLFileReader()

XMLFileReader::XMLFileReader ( )

Definition at line 26 of file XMLFileReader.cpp.

27 {
28  mParser = XML_ParserCreate(NULL);
29  XML_SetUserData(mParser, (void *)this);
30  XML_SetElementHandler(mParser, startElement, endElement);
31  XML_SetCharacterDataHandler(mParser, charHandler);
32  mBaseHandler = NULL;
33  mHandler.reserve(128);
34 }

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

Here is the call graph for this function:

◆ ~XMLFileReader()

XMLFileReader::~XMLFileReader ( )

Definition at line 36 of file XMLFileReader.cpp.

37 {
38  XML_ParserFree(mParser);
39 }

References mParser.

Member Function Documentation

◆ charHandler()

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

Definition at line 226 of file XMLFileReader.cpp.

227 {
228  XMLFileReader *This = (XMLFileReader *)userData;
229  Handlers &handlers = This->mHandler;
230 
231  if (XMLTagHandler *const handler = handlers.back())
232  handler->ReadXMLContent(s, len);
233 }

References 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 214 of file XMLFileReader.cpp.

215 {
216  XMLFileReader *This = (XMLFileReader *)userData;
217  Handlers &handlers = This->mHandler;
218 
219  if (XMLTagHandler *const handler = handlers.back())
220  handler->ReadXMLEndTag(name);
221 
222  handlers.pop_back();
223 }

References mHandler, and name.

Referenced by XMLFileReader().

Here is the caller graph for this function:

◆ GetErrorStr()

const TranslatableString & XMLFileReader::GetErrorStr ( ) const

Definition at line 177 of file XMLFileReader.cpp.

178 {
179  return mErrorStr;
180 }

References mErrorStr.

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

Here is the caller graph for this function:

◆ GetLibraryErrorStr()

const TranslatableString & XMLFileReader::GetLibraryErrorStr ( ) const

Definition at line 182 of file XMLFileReader.cpp.

183 {
184  return mLibraryErrorStr;
185 }

References mLibraryErrorStr.

◆ Parse()

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

Definition at line 41 of file XMLFileReader.cpp.

43 {
44  wxFFile theXMLFile(fname, wxT("rb"));
45  if (!theXMLFile.IsOpened()) {
46  mErrorStr = XO("Could not open file: \"%s\"").Format( fname );
47  return false;
48  }
49 
50  mBaseHandler = baseHandler;
51 
52  const size_t bufferSize = 16384;
53  char buffer[16384];
54  int done = 0;
55  do {
56  size_t len = fread(buffer, 1, bufferSize, theXMLFile.fp());
57  done = (len < bufferSize);
58  if (!XML_Parse(mParser, buffer, len, done)) {
59 
60  // Embedded error string from expat doesn't translate (yet)
61  // We could make a table of XOs if we wanted so that it could
62  // If we do, uncomment the second constructor argument so it's not
63  // a verbatim string
65  XML_ErrorString(XML_GetErrorCode(mParser)) // , {}
66  );
67 
68  mErrorStr = XO("Error: %s at line %lu").Format(
70  (long unsigned int)XML_GetCurrentLineNumber(mParser)
71  );
72 
73  theXMLFile.Close();
74  return false;
75 
76 // If we did want to handle every single parse error, these are they....
77 /*
78  XML_L("out of memory"),
79  XML_L("syntax error"),
80  XML_L("no element found"),
81  XML_L("not well-formed (invalid token)"),
82  XML_L("unclosed token"),
83  XML_L("partial character"),
84  XML_L("mismatched tag"),
85  XML_L("duplicate attribute"),
86  XML_L("junk after document element"),
87  XML_L("illegal parameter entity reference"),
88  XML_L("undefined entity"),
89  XML_L("recursive entity reference"),
90  XML_L("asynchronous entity"),
91  XML_L("reference to invalid character number"),
92  XML_L("reference to binary entity"),
93  XML_L("reference to external entity in attribute"),
94  XML_L("XML or text declaration not at start of entity"),
95  XML_L("unknown encoding"),
96  XML_L("encoding specified in XML declaration is incorrect"),
97  XML_L("unclosed CDATA section"),
98  XML_L("error in processing external entity reference"),
99  XML_L("document is not standalone"),
100  XML_L("unexpected parser state - please send a bug report"),
101  XML_L("entity declared in parameter entity"),
102  XML_L("requested feature requires XML_DTD support in Expat"),
103  XML_L("cannot change setting once parsing has begun"),
104  XML_L("unbound prefix"),
105  XML_L("must not undeclare prefix"),
106  XML_L("incomplete markup in parameter entity"),
107  XML_L("XML declaration not well-formed"),
108  XML_L("text declaration not well-formed"),
109  XML_L("illegal character(s) in public id"),
110  XML_L("parser suspended"),
111  XML_L("parser not suspended"),
112  XML_L("parsing aborted"),
113  XML_L("parsing finished"),
114  XML_L("cannot suspend in external parameter entity"),
115  XML_L("reserved prefix (xml) must not be undeclared or bound to another namespace name"),
116  XML_L("reserved prefix (xmlns) must not be declared or undeclared"),
117  XML_L("prefix must not be bound to one of the reserved namespace names")
118 */
119  }
120  } while (!done);
121 
122  theXMLFile.Close();
123 
124  // Even though there were no parse errors, we only succeed if
125  // the first-level handler actually got called, and didn't
126  // return false.
127  if (mBaseHandler)
128  return true;
129  else {
130  mErrorStr = XO("Could not load file: \"%s\"").Format( fname );
131  return false;
132  }
133 }

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

Referenced by FFmpegPresets::FFmpegPresets(), AUPImportFileHandle::Import(), FFmpegPresets::ImportPresets(), EffectEqualization::LoadCurves(), VSTEffect::LoadXML(), KeyConfigPrefs::OnImport(), TagsEditorDialog::OnLoad(), and EffectEqualization::UpdateDefaultCurves().

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

◆ ParseString()

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

Definition at line 135 of file XMLFileReader.cpp.

137 {
138  auto utf8 = xmldata.ToUTF8();
139  const char *buffer = utf8.data();
140  int len = utf8.length();
141 
142  mBaseHandler = baseHandler;
143 
144  if (!XML_Parse(mParser, buffer, len, true))
145  {
146 
147  // Embedded error string from expat doesn't translate (yet)
148  // We could make a table of XOs if we wanted so that it could
149  // If we do, uncomment the second constructor argument so it's not
150  // a verbatim string
152  XML_ErrorString(XML_GetErrorCode(mParser)) // , {}
153  );
154 
155  mErrorStr = XO("Error: %s at line %lu").Format(
157  (long unsigned int)XML_GetCurrentLineNumber(mParser)
158  );
159 
160  wxLogMessage(wxT("ParseString error: %s\n===begin===%s\n===end==="), mErrorStr.Debug(), buffer);
161 
162  return false;
163  }
164 
165  // Even though there were no parse errors, we only succeed if
166  // the first-level handler actually got called, and didn't
167  // return false.
168  if (!mBaseHandler)
169  {
170  mErrorStr = XO("Could not parse XML");
171  return false;
172  }
173 
174  return true;
175 }

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

Referenced by ProjectFileIO::LoadProject(), 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 188 of file XMLFileReader.cpp.

190 {
191  XMLFileReader *This = (XMLFileReader *)userData;
192  Handlers &handlers = This->mHandler;
193 
194  if (handlers.empty()) {
195  handlers.push_back(This->mBaseHandler);
196  }
197  else {
198  if (XMLTagHandler *const handler = handlers.back())
199  handlers.push_back(handler->ReadXMLChild(name));
200  else
201  handlers.push_back(NULL);
202  }
203 
204  if (XMLTagHandler *& handler = handlers.back()) {
205  if (!handler->ReadXMLTag(name, atts)) {
206  handler = nullptr;
207  if (handlers.size() == 1)
208  This->mBaseHandler = nullptr;
209  }
210  }
211 }

References mBaseHandler, 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 44 of file XMLFileReader.h.

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

◆ mErrorStr

TranslatableString XMLFileReader::mErrorStr
private

Definition at line 47 of file XMLFileReader.h.

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

◆ mHandler

Handlers XMLFileReader::mHandler
private

Definition at line 46 of file XMLFileReader.h.

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

◆ mLibraryErrorStr

TranslatableString XMLFileReader::mLibraryErrorStr
private

Definition at line 48 of file XMLFileReader.h.

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

◆ mParser

XML_Parser XMLFileReader::mParser
private

Definition at line 43 of file XMLFileReader.h.

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


The documentation for this class was generated from the following files:
XO
#define XO(s)
Definition: Internat.h:31
TranslatableString::Debug
wxString Debug() const
Format as an English string for debugging logs and developers' eyes, not for end users.
Definition: TranslatableString.h:82
XMLFileReader::mHandler
Handlers mHandler
Definition: XMLFileReader.h:46
XMLFileReader::charHandler
static void charHandler(void *userData, const char *s, int len)
Definition: XMLFileReader.cpp:226
XMLFileReader::Handlers
std::vector< XMLTagHandler * > Handlers
Definition: XMLFileReader.h:45
XMLFileReader::mErrorStr
TranslatableString mErrorStr
Definition: XMLFileReader.h:47
name
const TranslatableString name
Definition: Distortion.cpp:98
XMLFileReader::mBaseHandler
XMLTagHandler * mBaseHandler
Definition: XMLFileReader.h:44
XMLTagHandler
This class is an interface which should be implemented by classes which wish to be able to load and s...
Definition: XMLTagHandler.h:80
XMLFileReader::endElement
static void endElement(void *userData, const char *name)
Definition: XMLFileReader.cpp:214
XMLFileReader
Reads a file and passes the results through an XMLTagHandler.
Definition: XMLFileReader.h:20
Verbatim
TranslatableString Verbatim(wxString str)
Require calls to the one-argument constructor to go through this distinct global function name.
Definition: TranslatableString.h:321
XMLFileReader::mParser
XML_Parser mParser
Definition: XMLFileReader.h:43
XMLFileReader::startElement
static void startElement(void *userData, const char *name, const char **atts)
Definition: XMLFileReader.cpp:188
XMLFileReader::mLibraryErrorStr
TranslatableString mLibraryErrorStr
Definition: XMLFileReader.h:48