Audacity  2.2.2
Public Member Functions | Protected Attributes | List of all members
XMLWriter Class Referenceabstract

Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating XML in UTF8 encoding. More...

#include <XMLWriter.h>

Inheritance diagram for XMLWriter:
AutoSaveFile XMLFileWriter XMLStringWriter

Public Member Functions

 XMLWriter ()
 
virtual ~XMLWriter ()
 
virtual void StartTag (const wxString &name)
 
virtual void EndTag (const wxString &name)
 
virtual void WriteAttr (const wxString &name, const wxString &value)
 
virtual void WriteAttr (const wxString &name, const wxChar *value)
 
virtual void WriteAttr (const wxString &name, int value)
 
virtual void WriteAttr (const wxString &name, bool value)
 
virtual void WriteAttr (const wxString &name, long value)
 
virtual void WriteAttr (const wxString &name, long long value)
 
virtual void WriteAttr (const wxString &name, size_t value)
 
virtual void WriteAttr (const wxString &name, float value, int digits=-1)
 
virtual void WriteAttr (const wxString &name, double value, int digits=-1)
 
virtual void WriteData (const wxString &value)
 
virtual void WriteSubTree (const wxString &value)
 
virtual void Write (const wxString &data)=0
 
wxString XMLEsc (const wxString &s)
 

Protected Attributes

bool mInTag
 
int mDepth
 
wxArrayString mTagstack
 
std::vector< int > mHasKids
 

Detailed Description

Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating XML in UTF8 encoding.

XMLWriter

Definition at line 22 of file XMLWriter.h.

Constructor & Destructor Documentation

XMLWriter::XMLWriter ( )

XMLWriter base class

Definition at line 67 of file XMLWriter.cpp.

References mDepth, mHasKids, and mInTag.

68 {
69  mDepth = 0;
70  mInTag = false;
71  mHasKids.push_back(false);
72 }
int mDepth
Definition: XMLWriter.h:56
bool mInTag
Definition: XMLWriter.h:55
std::vector< int > mHasKids
Definition: XMLWriter.h:58
XMLWriter::~XMLWriter ( )
virtual

Definition at line 74 of file XMLWriter.cpp.

75 {
76 }

Member Function Documentation

void XMLWriter::EndTag ( const wxString &  name)
virtual

Reimplemented in AutoSaveFile.

Definition at line 101 of file XMLWriter.cpp.

References mDepth, mHasKids, mInTag, mTagstack, and Write().

Referenced by ConvertLegacyTrack(), XMLFileWriter::PreCommit(), LegacyAliasBlockFile::SaveXML(), PCMAliasBlockFile::SaveXML(), SilentBlockFile::SaveXML(), LegacyBlockFile::SaveXML(), SimpleBlockFile::SaveXML(), ODPCMAliasBlockFile::SaveXML(), ODDecodeBlockFile::SaveXML(), and AudacityProject::WriteXML().

103 {
104  int i;
105 
106  if (mTagstack.GetCount() > 0) {
107  if (mTagstack[0] == name) {
108  if (mHasKids[1]) { // There will always be at least 2 at this point
109  if (mInTag) {
110  Write(wxT("/>\n"));
111  }
112  else {
113  for (i = 0; i < mDepth - 1; i++) {
114  Write(wxT("\t"));
115  }
116  Write(wxString::Format(wxT("</%s>\n"), name));
117  }
118  }
119  else {
120  Write(wxT(">\n"));
121  }
122  mTagstack.RemoveAt(0);
123  mHasKids.erase(mHasKids.begin());
124  }
125  }
126 
127  mDepth--;
128  mInTag = false;
129 }
wxArrayString mTagstack
Definition: XMLWriter.h:57
virtual void Write(const wxString &data)=0
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
void XMLWriter::StartTag ( const wxString &  name)
virtual

Reimplemented in AutoSaveFile.

Definition at line 78 of file XMLWriter.cpp.

References mDepth, mHasKids, mInTag, mTagstack, and Write().

Referenced by ConvertLegacyTrack(), LegacyAliasBlockFile::SaveXML(), PCMAliasBlockFile::SaveXML(), SilentBlockFile::SaveXML(), LegacyBlockFile::SaveXML(), SimpleBlockFile::SaveXML(), ODPCMAliasBlockFile::SaveXML(), ODDecodeBlockFile::SaveXML(), and AudacityProject::WriteXML().

80 {
81  int i;
82 
83  if (mInTag) {
84  Write(wxT(">\n"));
85  mInTag = false;
86  }
87 
88  for (i = 0; i < mDepth; i++) {
89  Write(wxT("\t"));
90  }
91 
92  Write(wxString::Format(wxT("<%s"), name));
93 
94  mTagstack.Insert(name, 0);
95  mHasKids[0] = true;
96  mHasKids.insert(mHasKids.begin(), false);
97  mDepth++;
98  mInTag = true;
99 }
wxArrayString mTagstack
Definition: XMLWriter.h:57
virtual void Write(const wxString &data)=0
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
virtual void XMLWriter::Write ( const wxString &  data)
pure virtual
void XMLWriter::WriteAttr ( const wxString &  name,
const wxString &  value 
)
virtual

Reimplemented in AutoSaveFile.

Definition at line 131 of file XMLWriter.cpp.

References Write(), and XMLEsc().

Referenced by ConvertLegacyTrack(), LegacyAliasBlockFile::SaveXML(), PCMAliasBlockFile::SaveXML(), SilentBlockFile::SaveXML(), LegacyBlockFile::SaveXML(), SimpleBlockFile::SaveXML(), ODPCMAliasBlockFile::SaveXML(), ODDecodeBlockFile::SaveXML(), WriteAttr(), AudacityProject::WriteXML(), SelectedRegion::WriteXMLAttributes(), and PlayableTrack::WriteXMLAttributes().

133 {
134  Write(wxString::Format(wxT(" %s=\"%s\""),
135  name,
136  XMLEsc(value)));
137 }
virtual void Write(const wxString &data)=0
wxString XMLEsc(const wxString &s)
Definition: XMLWriter.cpp:226
const wxChar * name
Definition: Distortion.cpp:94
void XMLWriter::WriteAttr ( const wxString &  name,
const wxChar *  value 
)
virtual

Reimplemented in AutoSaveFile.

Definition at line 139 of file XMLWriter.cpp.

References WriteAttr().

141 {
142  WriteAttr(name, wxString(value));
143 }
virtual void WriteAttr(const wxString &name, const wxString &value)
Definition: XMLWriter.cpp:131
const wxChar * name
Definition: Distortion.cpp:94
void XMLWriter::WriteAttr ( const wxString &  name,
int  value 
)
virtual

Reimplemented in AutoSaveFile.

Definition at line 145 of file XMLWriter.cpp.

References Write().

147 {
148  Write(wxString::Format(wxT(" %s=\"%d\""),
149  name,
150  value));
151 }
virtual void Write(const wxString &data)=0
const wxChar * name
Definition: Distortion.cpp:94
void XMLWriter::WriteAttr ( const wxString &  name,
bool  value 
)
virtual

Reimplemented in AutoSaveFile.

Definition at line 153 of file XMLWriter.cpp.

References Write().

155 {
156  Write(wxString::Format(wxT(" %s=\"%d\""),
157  name,
158  value));
159 }
virtual void Write(const wxString &data)=0
const wxChar * name
Definition: Distortion.cpp:94
void XMLWriter::WriteAttr ( const wxString &  name,
long  value 
)
virtual

Reimplemented in AutoSaveFile.

Definition at line 161 of file XMLWriter.cpp.

References Write().

163 {
164  Write(wxString::Format(wxT(" %s=\"%ld\""),
165  name,
166  value));
167 }
virtual void Write(const wxString &data)=0
const wxChar * name
Definition: Distortion.cpp:94
void XMLWriter::WriteAttr ( const wxString &  name,
long long  value 
)
virtual

Reimplemented in AutoSaveFile.

Definition at line 169 of file XMLWriter.cpp.

References Write().

171 {
172  Write(wxString::Format(wxT(" %s=\"%lld\""),
173  name,
174  value));
175 }
virtual void Write(const wxString &data)=0
const wxChar * name
Definition: Distortion.cpp:94
void XMLWriter::WriteAttr ( const wxString &  name,
size_t  value 
)
virtual

Reimplemented in AutoSaveFile.

Definition at line 177 of file XMLWriter.cpp.

References Write().

179 {
180  Write(wxString::Format(wxT(" %s=\"%lld\""),
181  name,
182  (long long) value));
183 }
virtual void Write(const wxString &data)=0
const wxChar * name
Definition: Distortion.cpp:94
void XMLWriter::WriteAttr ( const wxString &  name,
float  value,
int  digits = -1 
)
virtual

Reimplemented in AutoSaveFile.

Definition at line 185 of file XMLWriter.cpp.

References Internat::ToString(), and Write().

187 {
188  Write(wxString::Format(wxT(" %s=\"%s\""),
189  name,
190  Internat::ToString(value, digits)));
191 }
virtual void Write(const wxString &data)=0
static wxString ToString(double numberToConvert, int digitsAfterDecimalPoint=-1)
Convert a number to a string, always uses the dot as decimal separator.
Definition: Internat.cpp:138
const wxChar * name
Definition: Distortion.cpp:94
void XMLWriter::WriteAttr ( const wxString &  name,
double  value,
int  digits = -1 
)
virtual

Reimplemented in AutoSaveFile.

Definition at line 193 of file XMLWriter.cpp.

References Internat::ToString(), and Write().

195 {
196  Write(wxString::Format(wxT(" %s=\"%s\""),
197  name,
198  Internat::ToString(value, digits)));
199 }
virtual void Write(const wxString &data)=0
static wxString ToString(double numberToConvert, int digitsAfterDecimalPoint=-1)
Convert a number to a string, always uses the dot as decimal separator.
Definition: Internat.cpp:138
const wxChar * name
Definition: Distortion.cpp:94
void XMLWriter::WriteData ( const wxString &  value)
virtual

Reimplemented in AutoSaveFile.

Definition at line 201 of file XMLWriter.cpp.

References mDepth, Write(), and XMLEsc().

203 {
204  int i;
205 
206  for (i = 0; i < mDepth; i++) {
207  Write(wxT("\t"));
208  }
209 
210  Write(XMLEsc(value));
211 }
virtual void Write(const wxString &data)=0
int mDepth
Definition: XMLWriter.h:56
wxString XMLEsc(const wxString &s)
Definition: XMLWriter.cpp:226
void XMLWriter::WriteSubTree ( const wxString &  value)
virtual

Definition at line 213 of file XMLWriter.cpp.

References mHasKids, mInTag, and Write().

215 {
216  if (mInTag) {
217  Write(wxT(">\n"));
218  mInTag = false;
219  mHasKids[0] = true;
220  }
221 
222  Write(value);
223 }
virtual void Write(const wxString &data)=0
bool mInTag
Definition: XMLWriter.h:55
std::vector< int > mHasKids
Definition: XMLWriter.h:58
wxString XMLWriter::XMLEsc ( const wxString &  s)

Definition at line 226 of file XMLWriter.cpp.

References charXMLCompatiblity, MAX_HIGH_SURROGATE, MAX_LOW_SURROGATE, MIN_HIGH_SURROGATE, MIN_LOW_SURROGATE, NONCHARACTER_FFFE, and NONCHARACTER_FFFF.

Referenced by WriteAttr(), and WriteData().

227 {
228  wxString result;
229  int len = s.Length();
230 
231  for(int i=0; i<len; i++) {
232  wxUChar c = s.GetChar(i);
233 
234  switch (c) {
235  case wxT('\''):
236  result += wxT("&apos;");
237  break;
238 
239  case wxT('"'):
240  result += wxT("&quot;");
241  break;
242 
243  case wxT('&'):
244  result += wxT("&amp;");
245  break;
246 
247  case wxT('<'):
248  result += wxT("&lt;");
249  break;
250 
251  case wxT('>'):
252  result += wxT("&gt;");
253  break;
254 
255  default:
256  if (sizeof(c) == 2 && c >= MIN_HIGH_SURROGATE && c <= MAX_HIGH_SURROGATE && i < len - 1) {
257  // If wxUChar is 2 bytes, then supplementary characters (those greater than U+FFFF) are represented
258  // with a high surrogate (U+D800..U+DBFF) followed by a low surrogate (U+DC00..U+DFFF).
259  // Handle those here.
260  wxUChar c2 = s.GetChar(++i);
261  if (c2 >= MIN_LOW_SURROGATE && c2 <= MAX_LOW_SURROGATE) {
262  // Surrogate pair found; simply add it to the output string.
263  result += c;
264  result += c2;
265  }
266  else {
267  // That high surrogate isn't paired, so ignore it.
268  i--;
269  }
270  }
271  else if (!wxIsprint(c)) {
272  //ignore several characters such ase eot (0x04) and stx (0x02) because it makes expat parser bail
273  //see xmltok.c in expat checkCharRefNumber() to see how expat bails on these chars.
274  //also see wxWidgets-2.8.12/src/expat/lib/asciitab.h to see which characters are nonxml compatible
275  //post decode (we can still encode '&' and '<' with this table, but it prevents us from encoding eot)
276  //everything is compatible past ascii 0x20 except for surrogates and the noncharacters U+FFFE and U+FFFF,
277  //so we don't check the compatibility table higher than this.
278  if((c> 0x1F || charXMLCompatiblity[c]!=0) &&
279  (c < MIN_HIGH_SURROGATE || c > MAX_LOW_SURROGATE) &&
281  result += wxString::Format(wxT("&#x%04x;"), c);
282  }
283  else {
284  result += c;
285  }
286  break;
287  }
288  }
289 
290  return result;
291 }
#define MIN_LOW_SURROGATE
Definition: XMLWriter.cpp:56
#define NONCHARACTER_FFFF
Definition: XMLWriter.cpp:61
#define MIN_HIGH_SURROGATE
Definition: XMLWriter.cpp:54
#define NONCHARACTER_FFFE
Definition: XMLWriter.cpp:60
static int charXMLCompatiblity[]
Definition: XMLWriter.cpp:40
#define MAX_HIGH_SURROGATE
Definition: XMLWriter.cpp:55
#define MAX_LOW_SURROGATE
Definition: XMLWriter.cpp:57

Member Data Documentation

int XMLWriter::mDepth
protected

Definition at line 56 of file XMLWriter.h.

Referenced by EndTag(), StartTag(), WriteData(), and XMLWriter().

std::vector<int> XMLWriter::mHasKids
protected

Definition at line 58 of file XMLWriter.h.

Referenced by EndTag(), StartTag(), WriteSubTree(), and XMLWriter().

bool XMLWriter::mInTag
protected

Definition at line 55 of file XMLWriter.h.

Referenced by EndTag(), StartTag(), WriteSubTree(), and XMLWriter().

wxArrayString XMLWriter::mTagstack
protected

Definition at line 57 of file XMLWriter.h.

Referenced by EndTag(), XMLFileWriter::PreCommit(), and StartTag().


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