XmlWriter.h
1 /*
2  * Copyright (C) 2005-2013 Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef Pt_Xml_XmlWriter_h
30 #define Pt_Xml_XmlWriter_h
31 
32 #include <Pt/Xml/Api.h>
33 #include <Pt/String.h>
34 #include <Pt/TextStream.h>
35 #include <cstddef>
36 
37 namespace Pt {
38 
39 namespace Xml {
40 
42 PT_XML_API void xmlEncode(std::basic_ostream<Pt::Char>& os, const Pt::Char* str, std::size_t n);
43 
45 PT_XML_API void xmlEncode(std::basic_ostream<Pt::Char>& os, const Pt::Char* str);
46 
48 inline void xmlEncode(std::basic_ostream<Pt::Char>& os, const Pt::String& str);
49 
52 class PT_XML_API XmlWriter
53 {
54  public:
57  XmlWriter();
58 
61  XmlWriter(std::basic_ostream<Char>& os);
62 
65  ~XmlWriter();
66 
69  bool isFormatting() const;
70 
73  void setFormatting(bool value);
74 
77  const Pt::String& indent() const;
78 
81  void setIndent(const Pt::String& indent);
82 
85  Pt::Char quote() const;
86 
89  void setQuote(Pt::Char ch);
90 
96  void reset();
97 
103  void reset(std::basic_ostream<Char>& os);
104 
107  std::basic_ostream<Char>* output();
108 
111  std::size_t depth() const;
112 
115  void setDefaultNamespace(const Pt::String& ns);
116 
119  void setNamespacePrefix(const Pt::String& prefix, const Pt::String& ns);
120 
123  void writeStartDocument(const Pt::Char* version, std::size_t versionSize,
124  const Pt::Char* encoding, std::size_t encodingSize, bool standalone = false);
125 
128  void writeStartDocument(const Pt::String& version, const Pt::String& encoding, bool standalone = false);
129 
132  void writeEndDocument();
133 
136  void writeDocType(const Pt::Char* dtd, std::size_t n);
137 
140  void writeDocType(const Pt::String& dtd);
141 
144  void writeStartElement(const Pt::Char* localName, std::size_t localNameSize);
145 
148  void writeStartElement(const Pt::String& localName);
149 
152  void writeStartElement(const Char* ns, std::size_t nsSize,
153  const Char* localName, std::size_t localNameSize);
154 
157  void writeStartElement(const Pt::String& ns, const Pt::String& localName);
158 
161  void writeAttribute(const Char* localName, std::size_t localNameSize,
162  const Char* value, std::size_t valueSize);
163 
166  void writeAttribute(const Pt::String& localName, const Pt::String& value);
167 
170  void writeAttribute(const Char* ns, std::size_t nsSize,
171  const Char* localName, std::size_t localNameSize,
172  const Char* value, std::size_t valueSize);
173 
176  void writeAttribute(const Pt::String& ns, const Pt::String& localName, const Pt::String& value);
177 
180  void writeEmptyElement(const Pt::Char* localName, std::size_t localNameSize);
181 
184  void writeEmptyElement(const Pt::String& localName);
185 
188  void writeEmptyElement(const Char* ns, std::size_t nsSize,
189  const Char* localName, std::size_t localNameSize);
190 
193  void writeEmptyElement(const Pt::String& ns, const Pt::String& localName);
194 
197  void writeEndElement();
198 
201  void writeCharacters(const Pt::Char* text, std::size_t n);
202 
205  void writeCharacters(const Pt::String& text);
206 
209  void writeEntityReference(const Pt::Char* name, std::size_t n);
210 
213  void writeEntityReference(const Pt::String& name);
214 
217  void writeCData(const Pt::Char* text, std::size_t n);
218 
221  void writeCData(const Pt::String& text);
222 
225  void writeComment(const Pt::Char* text, std::size_t n);
226 
229  void writeComment(const Pt::String& text);
230 
233  void writeProcessingInstruction(const Pt::Char* target, std::size_t targetSize,
234  const Pt::Char* data, std::size_t dataSize);
235 
238  void writeProcessingInstruction(const Pt::String& text, const Pt::String& data);
239 
242  void writeStartTag(const Pt::Char* name);
243 
246  void writeEndTag(const Pt::Char* name);
247 
248  private:
249  class XmlWriterImpl* _impl;
250 };
251 
252 
253 inline void xmlEncode(std::basic_ostream<Pt::Char>& os, const Pt::String& str)
254 {
255  xmlEncode(os, str.c_str(), str.size());
256 }
257 
258 
259 inline void XmlWriter::writeStartDocument(const Pt::String& version, const Pt::String& encoding, bool standalone)
260 {
261  this->writeStartDocument(version.c_str(), version.size(),
262  encoding.c_str(), encoding.size(), standalone);
263 }
264 
265 
266 inline void XmlWriter::writeDocType(const Pt::String& dtd)
267 {
268  this->writeDocType(dtd.c_str(), dtd.size());
269 }
270 
271 
272 inline void XmlWriter::writeStartElement(const Pt::String& localName)
273 {
274  this->writeStartElement(localName.c_str(), localName.size());
275 }
276 
277 
278 inline void XmlWriter::writeStartElement(const Pt::String& ns, const Pt::String& localName)
279 {
280  this->writeStartElement(ns.c_str(), ns.size(),
281  localName.c_str(), localName.size());
282 }
283 
284 
285 inline void XmlWriter::writeAttribute(const Pt::String& localName, const Pt::String& value)
286 {
287  this->writeAttribute( localName.c_str(), localName.size(),
288  value.c_str(), value.size() );
289 }
290 
291 
292 inline void XmlWriter::writeAttribute(const Pt::String& ns, const Pt::String& localName, const Pt::String& value)
293 {
294  this->writeAttribute( ns.c_str(), ns.size(),
295  localName.c_str(), localName.size(),
296  value.c_str(), value.size() );
297 }
298 
299 
300 inline void XmlWriter::writeEmptyElement(const Pt::String& localName)
301 {
302  this->writeEmptyElement(localName.c_str(), localName.size());
303 }
304 
305 
306 inline void XmlWriter::writeEmptyElement(const Pt::String& ns, const Pt::String& localName)
307 {
308  this->writeEmptyElement(ns.c_str(), ns.size(),
309  localName.c_str(), localName.size());
310 }
311 
312 
313 inline void XmlWriter::writeCharacters(const Pt::String& text)
314 {
315  this->writeCharacters(text.c_str(), text.size());
316 }
317 
318 
320 {
321  this->writeEntityReference(name.c_str(), name.size());
322 }
323 
324 
325 inline void XmlWriter::writeCData(const Pt::String& text)
326 {
327  this->writeCData(text.c_str(), text.size());
328 }
329 
330 
331 inline void XmlWriter::writeComment(const Pt::String& text)
332 {
333  this->writeComment(text.c_str(), text.size());
334 }
335 
336 
337 inline void XmlWriter::writeProcessingInstruction(const Pt::String& target, const Pt::String& data)
338 {
339  this->writeProcessingInstruction( target.c_str(), target.size(),
340  data.c_str(), data.size() );
341 }
342 
343 } // namespace Xml
344 
345 } // namespace Pt
346 
347 #endif // Pt_Xml_XmlWriter_h
void writeAttribute(const Char *localName, std::size_t localNameSize, const Char *value, std::size_t valueSize)
Writes an XML attribute.
void writeCData(const Pt::Char *text, std::size_t n)
Writes text as a CDATA section.
size_type size() const
Returns the length of the string.
Definition: String.h:239
void writeEmptyElement(const Pt::Char *localName, std::size_t localNameSize)
Writes an empty XML element.
Writes XML to a text stream.
Definition: XmlWriter.h:52
const Pt::Char * c_str() const
Returns a null terminated C string.
Definition: String.h:264
void writeStartElement(const Pt::Char *localName, std::size_t localNameSize)
Writes a start element.
Unicode character type.
Definition: String.h:66
Unicode capable basic_string.
Definition: String.h:42
void writeCharacters(const Pt::Char *text, std::size_t n)
Writes text as element content.
void writeEntityReference(const Pt::Char *name, std::size_t n)
Writes an entitiy reference.
void writeStartDocument(const Pt::Char *version, std::size_t versionSize, const Pt::Char *encoding, std::size_t encodingSize, bool standalone=false)
Writes an XML declaration.
void writeProcessingInstruction(const Pt::Char *target, std::size_t targetSize, const Pt::Char *data, std::size_t dataSize)
Writes a processing instruction.
void writeDocType(const Pt::Char *dtd, std::size_t n)
Writes a DOCTYPE declaration.
void writeComment(const Pt::Char *text, std::size_t n)
Writes a comment.