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
37namespace Pt {
38
39namespace Xml {
40
42PT_XML_API void xmlEncode(std::basic_ostream<Pt::Char>& os, const Pt::Char* str, std::size_t n);
43
45PT_XML_API void xmlEncode(std::basic_ostream<Pt::Char>& os, const Pt::Char* str);
46
48inline void xmlEncode(std::basic_ostream<Pt::Char>& os, const Pt::String& str);
49
52class PT_XML_API XmlWriter
53{
54 public:
58
61 XmlWriter(std::basic_ostream<Char>& os);
62
66
69 bool isFormatting() const;
70
73 void setFormatting(bool value);
74
77 const Pt::String& indent() const;
78
82
85 Pt::Char quote() const;
86
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
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
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
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
253inline void xmlEncode(std::basic_ostream<Pt::Char>& os, const Pt::String& str)
254{
255 xmlEncode(os, str.c_str(), str.size());
256}
257
258
259inline 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
266inline void XmlWriter::writeDocType(const Pt::String& dtd)
267{
268 this->writeDocType(dtd.c_str(), dtd.size());
269}
270
271
272inline void XmlWriter::writeStartElement(const Pt::String& localName)
273{
274 this->writeStartElement(localName.c_str(), localName.size());
275}
276
277
278inline 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
285inline 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
292inline 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
300inline void XmlWriter::writeEmptyElement(const Pt::String& localName)
301{
302 this->writeEmptyElement(localName.c_str(), localName.size());
303}
304
305
306inline 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
314{
315 this->writeCharacters(text.c_str(), text.size());
316}
317
318
320{
321 this->writeEntityReference(name.c_str(), name.size());
322}
323
324
325inline void XmlWriter::writeCData(const Pt::String& text)
326{
327 this->writeCData(text.c_str(), text.size());
328}
329
330
331inline void XmlWriter::writeComment(const Pt::String& text)
332{
333 this->writeComment(text.c_str(), text.size());
334}
335
336
337inline 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
Unicode capable basic_string.
Definition Api-String.h:67
size_type size() const
Returns the length of the string.
Definition Api-String.h:263
const Pt::Char * c_str() const
Returns a null terminated C string.
Definition Api-String.h:288
void writeEmptyElement(const Char *ns, std::size_t nsSize, const Char *localName, std::size_t localNameSize)
Writes an empty XML element.
Pt::Char quote() const
Returns the quotation character.
void writeEndDocument()
Finishes the document and closes all open elements.
void writeComment(const Pt::Char *text, std::size_t n)
Writes a comment.
void setNamespacePrefix(const Pt::String &prefix, const Pt::String &ns)
Sets the namespace for the child elemnts.
XmlWriter()
Constructor.
void writeCharacters(const Pt::Char *text, std::size_t n)
Writes text as element content.
void writeEndTag(const Pt::Char *name)
Writes an end tag.
void writeEndElement()
Closes the last XML element.
void writeEntityReference(const Pt::Char *name, std::size_t n)
Writes an entitiy reference.
void writeAttribute(const Char *ns, std::size_t nsSize, const Char *localName, std::size_t localNameSize, const Char *value, std::size_t valueSize)
Writes an XML attribute.
void writeAttribute(const Char *localName, std::size_t localNameSize, const Char *value, std::size_t valueSize)
Writes an XML attribute.
std::basic_ostream< Char > * output()
Returns the output stream or a nullptr if none was set.
void setIndent(const Pt::String &indent)
Sets the indentation string.
void writeStartElement(const Char *ns, std::size_t nsSize, const Char *localName, std::size_t localNameSize)
Writes a start element.
void writeStartElement(const Pt::Char *localName, std::size_t localNameSize)
Writes a start element.
XmlWriter(std::basic_ostream< Char > &os)
Constructs with output stream.
void setDefaultNamespace(const Pt::String &ns)
Sets the default namespace for the child elemnts.
void writeProcessingInstruction(const Pt::Char *target, std::size_t targetSize, const Pt::Char *data, std::size_t dataSize)
Writes a processing instruction.
std::size_t depth() const
Returns the current element depth.
void writeEmptyElement(const Pt::Char *localName, std::size_t localNameSize)
Writes an empty XML element.
void reset()
Clears the writer state and output.
void writeCData(const Pt::Char *text, std::size_t n)
Writes text as a CDATA section.
~XmlWriter()
Destructor.
void reset(std::basic_ostream< Char > &os)
Clears the writer state and output.
const Pt::String & indent() const
Returns the indentation string.
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 setQuote(Pt::Char ch)
Sets the quotation character.
void setFormatting(bool value)
Indicates wether indentation should be written.
bool isFormatting() const
Returns true if indentation should be written.
void writeDocType(const Pt::Char *dtd, std::size_t n)
Writes a DOCTYPE declaration.
void writeStartTag(const Pt::Char *name)
Writes a start tag.
Read and write XML ducuments.
Core module.
Definition Allocator.h:33
Unicode character type.
Definition String.h:67