Audacity  2.2.2
XMLWriter.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  XMLWriter.h
6 
7  Leland Lucius
8 
9 **********************************************************************/
10 #ifndef __AUDACITY_XML_XML_FILE_WRITER__
11 #define __AUDACITY_XML_XML_FILE_WRITER__
12 
13 #include <vector>
14 #include <wx/arrstr.h>
15 #include <wx/ffile.h>
16 
17 #include "../FileException.h"
18 
22 class AUDACITY_DLL_API XMLWriter /* not final */ {
23 
24  public:
25 
26  XMLWriter();
27  virtual ~XMLWriter();
28 
29  virtual void StartTag(const wxString &name);
30  virtual void EndTag(const wxString &name);
31 
32  virtual void WriteAttr(const wxString &name, const wxString &value);
33  virtual void WriteAttr(const wxString &name, const wxChar *value);
34 
35  virtual void WriteAttr(const wxString &name, int value);
36  virtual void WriteAttr(const wxString &name, bool value);
37  virtual void WriteAttr(const wxString &name, long value);
38  virtual void WriteAttr(const wxString &name, long long value);
39  virtual void WriteAttr(const wxString &name, size_t value);
40  virtual void WriteAttr(const wxString &name, float value, int digits = -1);
41  virtual void WriteAttr(const wxString &name, double value, int digits = -1);
42 
43  virtual void WriteData(const wxString &value);
44 
45  virtual void WriteSubTree(const wxString &value);
46 
47  virtual void Write(const wxString &data) = 0;
48 
49  // Escape a string, replacing certain characters with their
50  // XML encoding, i.e. '<' becomes '&lt;'
51  wxString XMLEsc(const wxString & s);
52 
53  protected:
54 
55  bool mInTag;
56  int mDepth;
57  wxArrayString mTagstack;
58  std::vector<int> mHasKids;
59 
60 };
61 
65 
74 class AUDACITY_DLL_API XMLFileWriter final : private wxFFile, public XMLWriter {
75 
76  public:
77 
81  ( const wxString &outputPath, const wxString &caption,
82  bool keepBackup = false );
83 
84  virtual ~XMLFileWriter();
85 
90  void Commit();
91 
93  void PreCommit();
94 
97  void PostCommit();
98 
100  void Write(const wxString &data) override;
101 
102  wxString GetBackupName() const { return mBackupName; }
103 
104  private:
105 
107  const wxFileName &fileName, const wxString &caption)
108  {
109  throw FileException{ FileException::Cause::Write, fileName, caption };
110  }
111 
114  void CloseWithoutEndingTags(); // for auto-save files
115 
116  const wxString mOutputPath;
117  const wxString mCaption;
118  wxString mBackupName;
119  const bool mKeepBackup;
120 
121  wxFFile mBackupFile;
122 
123  bool mCommitted{ false };
124 };
125 
129 class XMLStringWriter final : public wxString, public XMLWriter {
130 
131  public:
132 
133  XMLStringWriter(size_t initialSize = 0);
134  virtual ~XMLStringWriter();
135 
136  void Write(const wxString &data) override;
137 
138  wxString Get();
139 
140  private:
141 
142 };
143 
144 #endif
XMLStringWriter(size_t initialSize=0)
Definition: XMLWriter.cpp:417
const wxString mOutputPath
Definition: XMLWriter.h:116
wxArrayString mTagstack
Definition: XMLWriter.h:57
Wrapper to output XML data to strings.
Definition: XMLWriter.h:129
const bool mKeepBackup
Definition: XMLWriter.h:119
const wxString mCaption
Definition: XMLWriter.h:117
wxFFile mBackupFile
Definition: XMLWriter.h:121
virtual ~XMLStringWriter()
Definition: XMLWriter.cpp:425
wxString GetBackupName() const
Definition: XMLWriter.h:102
wxString mBackupName
Definition: XMLWriter.h:118
virtual void Write(const wxString &data)=0
void Write(const wxString &data) override
Definition: XMLWriter.cpp:429
wxString Get()
Wrapper to output XML data to files.
Definition: XMLWriter.h:74
int mDepth
Definition: XMLWriter.h:56
bool mInTag
Definition: XMLWriter.h:55
const wxChar * name
Definition: Distortion.cpp:94
std::vector< int > mHasKids
Definition: XMLWriter.h:58
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:22
void ThrowException(const wxFileName &fileName, const wxString &caption)
Definition: XMLWriter.h:106