Audacity  2.2.2
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>

Public Member Functions

 XMLFileReader ()
 
 ~XMLFileReader ()
 
bool Parse (XMLTagHandler *baseHandler, const wxString &fname)
 
wxString GetErrorStr ()
 

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
 
wxString mErrorStr
 

Detailed Description

Reads a file and passes the results through an XMLTagHandler.

Definition at line 18 of file XMLFileReader.h.

Member Typedef Documentation

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

Definition at line 40 of file XMLFileReader.h.

Constructor & Destructor Documentation

XMLFileReader::XMLFileReader ( )

Definition at line 25 of file XMLFileReader.cpp.

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

26 {
27  mParser = XML_ParserCreate(NULL);
28  XML_SetUserData(mParser, (void *)this);
29  XML_SetElementHandler(mParser, startElement, endElement);
30  XML_SetCharacterDataHandler(mParser, charHandler);
31  mBaseHandler = NULL;
32  mErrorStr = wxT("");
33  mHandler.reserve(128);
34 }
XMLTagHandler * mBaseHandler
Definition: XMLFileReader.h:39
Handlers mHandler
Definition: XMLFileReader.h:41
XML_Parser mParser
Definition: XMLFileReader.h:38
wxString mErrorStr
Definition: XMLFileReader.h:42
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)
XMLFileReader::~XMLFileReader ( )

Definition at line 36 of file XMLFileReader.cpp.

References mParser.

37 {
38  XML_ParserFree(mParser);
39 }
XML_Parser mParser
Definition: XMLFileReader.h:38

Member Function Documentation

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

Definition at line 124 of file XMLFileReader.cpp.

References mHandler.

Referenced by XMLFileReader().

125 {
126  XMLFileReader *This = (XMLFileReader *)userData;
127  Handlers &handlers = This->mHandler;
128 
129  if (XMLTagHandler *const handler = handlers.back())
130  handler->ReadXMLContent(s, len);
131 }
Reads a file and passes the results through an XMLTagHandler.
Definition: XMLFileReader.h:18
Handlers mHandler
Definition: XMLFileReader.h:41
This class is an interface which should be implemented by classes which wish to be able to load and s...
Definition: XMLTagHandler.h:70
std::vector< XMLTagHandler * > Handlers
Definition: XMLFileReader.h:40
void XMLFileReader::endElement ( void *  userData,
const char *  name 
)
static

Definition at line 112 of file XMLFileReader.cpp.

References mHandler.

Referenced by XMLFileReader().

113 {
114  XMLFileReader *This = (XMLFileReader *)userData;
115  Handlers &handlers = This->mHandler;
116 
117  if (XMLTagHandler *const handler = handlers.back())
118  handler->ReadXMLEndTag(name);
119 
120  handlers.pop_back();
121 }
Reads a file and passes the results through an XMLTagHandler.
Definition: XMLFileReader.h:18
Handlers mHandler
Definition: XMLFileReader.h:41
This class is an interface which should be implemented by classes which wish to be able to load and s...
Definition: XMLTagHandler.h:70
std::vector< XMLTagHandler * > Handlers
Definition: XMLFileReader.h:40
const wxChar * name
Definition: Distortion.cpp:94
wxString XMLFileReader::GetErrorStr ( )

Definition at line 80 of file XMLFileReader.cpp.

References mErrorStr.

Referenced by EffectEqualization::LoadCurves(), KeyConfigPrefs::OnImport(), TagsEditor::OnLoad(), and AudacityProject::OpenFile().

81 {
82  return mErrorStr;
83 }
wxString mErrorStr
Definition: XMLFileReader.h:42
bool XMLFileReader::Parse ( XMLTagHandler baseHandler,
const wxString &  fname 
)

Definition at line 41 of file XMLFileReader.cpp.

References _(), mBaseHandler, mErrorStr, and mParser.

Referenced by EffectEqualization::LoadCurves(), KeyConfigPrefs::OnImport(), TagsEditor::OnLoad(), and AudacityProject::OpenFile().

43 {
44  wxFFile theXMLFile(fname, wxT("rb"));
45  if (!theXMLFile.IsOpened()) {
46  mErrorStr.Printf(_("Could not open file: \"%s\""), 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  mErrorStr.Printf(_("Error: %hs at line %lu"),
60  XML_ErrorString(XML_GetErrorCode(mParser)),
61  (long unsigned int)XML_GetCurrentLineNumber(mParser));
62  theXMLFile.Close();
63  return false;
64  }
65  } while (!done);
66 
67  theXMLFile.Close();
68 
69  // Even though there were no parse errors, we only succeed if
70  // the first-level handler actually got called, and didn't
71  // return false.
72  if (mBaseHandler)
73  return true;
74  else {
75  mErrorStr.Printf(_("Could not load file: \"%s\""), fname);
76  return false;
77  }
78 }
XMLTagHandler * mBaseHandler
Definition: XMLFileReader.h:39
XML_Parser mParser
Definition: XMLFileReader.h:38
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom")).Raw()), OnMoveTrack)#define SET_TRACK_NAME_PLUGIN_SYMBOLclass SetTrackNameCommand:public AudacityCommand
wxString mErrorStr
Definition: XMLFileReader.h:42
void XMLFileReader::startElement ( void *  userData,
const char *  name,
const char **  atts 
)
static

Definition at line 86 of file XMLFileReader.cpp.

References mBaseHandler, and mHandler.

Referenced by XMLFileReader().

88 {
89  XMLFileReader *This = (XMLFileReader *)userData;
90  Handlers &handlers = This->mHandler;
91 
92  if (handlers.empty()) {
93  handlers.push_back(This->mBaseHandler);
94  }
95  else {
96  if (XMLTagHandler *const handler = handlers.back())
97  handlers.push_back(handler->ReadXMLChild(name));
98  else
99  handlers.push_back(NULL);
100  }
101 
102  if (XMLTagHandler *& handler = handlers.back()) {
103  if (!handler->ReadXMLTag(name, atts)) {
104  handler = nullptr;
105  if (handlers.size() == 1)
106  This->mBaseHandler = nullptr;
107  }
108  }
109 }
XMLTagHandler * mBaseHandler
Definition: XMLFileReader.h:39
Reads a file and passes the results through an XMLTagHandler.
Definition: XMLFileReader.h:18
Handlers mHandler
Definition: XMLFileReader.h:41
This class is an interface which should be implemented by classes which wish to be able to load and s...
Definition: XMLTagHandler.h:70
std::vector< XMLTagHandler * > Handlers
Definition: XMLFileReader.h:40
const wxChar * name
Definition: Distortion.cpp:94

Member Data Documentation

XMLTagHandler* XMLFileReader::mBaseHandler
private

Definition at line 39 of file XMLFileReader.h.

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

wxString XMLFileReader::mErrorStr
private

Definition at line 42 of file XMLFileReader.h.

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

Handlers XMLFileReader::mHandler
private

Definition at line 41 of file XMLFileReader.h.

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

XML_Parser XMLFileReader::mParser
private

Definition at line 38 of file XMLFileReader.h.

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


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