Audacity  3.0.3
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/ffile.h> // to inherit
15 
16 #include "../FileException.h"
17 
18 #include "Identifier.h"
19 
23 class AUDACITY_DLL_API XMLWriter /* not final */ {
24 
25  public:
26 
27  XMLWriter();
28  virtual ~XMLWriter();
29 
30  virtual void StartTag(const wxString &name);
31  virtual void EndTag(const wxString &name);
32 
33  // nonvirtual pass-through
34  void WriteAttr(const wxString &name, const Identifier &value)
35  // using GET once here, permitting Identifiers in XML,
36  // so no need for it at each WriteAttr call
37  { WriteAttr( name, value.GET() ); }
38 
39  virtual void WriteAttr(const wxString &name, const wxString &value);
40  virtual void WriteAttr(const wxString &name, const wxChar *value);
41 
42  virtual void WriteAttr(const wxString &name, int value);
43  virtual void WriteAttr(const wxString &name, bool value);
44  virtual void WriteAttr(const wxString &name, long value);
45  virtual void WriteAttr(const wxString &name, long long value);
46  virtual void WriteAttr(const wxString &name, size_t value);
47  virtual void WriteAttr(const wxString &name, float value, int digits = -1);
48  virtual void WriteAttr(const wxString &name, double value, int digits = -1);
49 
50  virtual void WriteData(const wxString &value);
51 
52  virtual void WriteSubTree(const wxString &value);
53 
54  virtual void Write(const wxString &data) = 0;
55 
56  // Escape a string, replacing certain characters with their
57  // XML encoding, i.e. '<' becomes '&lt;'
58  wxString XMLEsc(const wxString & s);
59 
60  protected:
61 
62  bool mInTag;
63  int mDepth;
64  wxArrayString mTagstack;
65  std::vector<int> mHasKids;
66 
67 };
68 
72 
81 class AUDACITY_DLL_API XMLFileWriter final : private wxFFile, public XMLWriter {
82 
83  public:
84 
88  const FilePath &outputPath, const TranslatableString &caption,
89  bool keepBackup = false );
90 
91  virtual ~XMLFileWriter();
92 
97  void Commit();
98 
100  void PreCommit();
101 
104  void PostCommit();
105 
107  void Write(const wxString &data) override;
108 
109  FilePath GetBackupName() const { return mBackupName; }
110 
111  private:
112 
114  const wxFileName &fileName, const TranslatableString &caption)
115  {
116  throw FileException{ FileException::Cause::Write, fileName, caption };
117  }
118 
121  void CloseWithoutEndingTags(); // for auto-save files
122 
126  const bool mKeepBackup;
127 
128  wxFFile mBackupFile;
129 
130  bool mCommitted{ false };
131 };
132 
136 class XMLStringWriter final : public wxString, public XMLWriter {
137 
138  public:
139 
140  XMLStringWriter(size_t initialSize = 0);
141  virtual ~XMLStringWriter();
142 
143  void Write(const wxString &data) override;
144 
145  private:
146 
147 };
148 
149 #endif
XMLWriter
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:23
XMLFileWriter::ThrowException
void ThrowException(const wxFileName &fileName, const TranslatableString &caption)
Definition: XMLWriter.h:113
FilePath
wxString FilePath
Definition: Identifier.h:227
TranslatableString
Holds a msgid for the translation catalog; may also bind format arguments.
Definition: TranslatableString.h:32
XMLFileWriter::mBackupFile
wxFFile mBackupFile
Definition: XMLWriter.h:128
FileException::Cause::Write
@ Write
most important to detect when storage space is exhausted
XMLStringWriter::~XMLStringWriter
virtual ~XMLStringWriter()
Definition: XMLWriter.cpp:424
XMLWriter::mInTag
bool mInTag
Definition: XMLWriter.h:62
XMLFileWriter::mCaption
const TranslatableString mCaption
Definition: XMLWriter.h:124
FileException
Thrown for failure of file or database operations in deeply nested places.
Definition: FileException.h:19
XMLStringWriter
Wrapper to output XML data to strings.
Definition: XMLWriter.h:136
XMLWriter::mHasKids
std::vector< int > mHasKids
Definition: XMLWriter.h:65
XMLFileWriter::mBackupName
FilePath mBackupName
Definition: XMLWriter.h:125
Identifier
An explicitly nonlocalized string, not meant for the user to see.
Definition: Identifier.h:22
XMLWriter::mTagstack
wxArrayString mTagstack
Definition: XMLWriter.h:64
XMLWriter::mDepth
int mDepth
Definition: XMLWriter.h:63
XMLStringWriter::Write
void Write(const wxString &data) override
Definition: XMLWriter.cpp:428
name
const TranslatableString name
Definition: Distortion.cpp:98
Identifier.h
Identifier::GET
const wxString & GET() const
Explicit conversion to wxString, meant to be ugly-looking and demanding of a comment why it's correct...
Definition: Identifier.h:66
XMLFileWriter::GetBackupName
FilePath GetBackupName() const
Definition: XMLWriter.h:109
XMLWriter::Write
virtual void Write(const wxString &data)=0
XMLFileWriter
Wrapper to output XML data to files.
Definition: XMLWriter.h:81
XMLFileWriter::mOutputPath
const FilePath mOutputPath
Definition: XMLWriter.h:123
XMLWriter::WriteAttr
void WriteAttr(const wxString &name, const Identifier &value)
Definition: XMLWriter.h:34
XMLStringWriter::XMLStringWriter
XMLStringWriter(size_t initialSize=0)
Definition: XMLWriter.cpp:416
XMLFileWriter::mKeepBackup
const bool mKeepBackup
Definition: XMLWriter.h:126