11 #ifndef __TEXTWOLF_XML_PRINTER_HPP__
12 #define __TEXTWOLF_XML_PRINTER_HPP__
30 template <
class IOCharset,
class AppCharset,
class BufferType>
38 void printToBuffer(
const char* src, std::size_t srcsize, BufferType& buf)
const
44 while ((ch = ts.
chr()) != 0)
46 m_output.print( ch, buf);
57 void printEsc(
char ch, BufferType& buf,
unsigned int nof_echr,
const char* echr,
const char** estr)
const
59 const char* cc = (
const char*)memchr( echr, ch, nof_echr);
63 const char* tt = estr[ cc-echr];
64 while (tt[ii]) m_output.print( tt[ii++], buf);
68 m_output.print( ch, buf);
79 void printToBufferSubstChr(
const char* src, std::size_t srcsize, BufferType& buf,
unsigned int nof_echr,
const char* echr,
const char** estr)
const
85 while ((ch = ts.
chr()) != 0)
89 printEsc( (
char)ch, buf, nof_echr, echr, estr);
93 m_output.print( ch, buf);
105 enum {nof_echr = 12};
106 static const char* estr[nof_echr] = {
"<",
">",
"'",
""",
"&",
"�",
"",
"	",
" ",
" "};
107 static const char echr[nof_echr+1] =
"<>'\"&\0\b\t\n\r";
108 m_output.print(
'"', buf);
110 m_output.print(
'"', buf);
120 static const char* estr[nof_echr] = {
"<",
">",
"&",
"�",
""};
121 static const char echr[nof_echr+1] =
"<>&\0\b";
138 :m_state(subDocument?
Content:
Init),m_lasterror(0){}
143 explicit XMLPrinter(
const IOCharset& output_,
bool subDocument=
false)
144 :m_state(subDocument?
Content:
Init),m_output(output_),m_lasterror(0){}
148 :m_state(o.m_state),m_tagstack(o.m_tagstack),m_output(o.m_output),m_lasterror(o.m_lasterror)
165 bool printHeader(
const char* encoding,
const char* standalone, BufferType& buf)
169 m_lasterror =
"printing xml header not at the beginning of the document";
172 std::string enc = encoding?encoding:
"UTF-8";
173 printToBuffer(
"<?xml version=\"1.0\" encoding=\"", 30, buf);
195 bool printDoctype(
const char* rootid,
const char* publicid,
const char* systemid, BufferType& buf)
203 m_lasterror =
"defined DOCTYPE with PUBLIC id but no SYSTEM id";
241 m_lasterror =
"printed xml without root element";
255 bool printOpenTag(
const char* src, std::size_t srcsize, BufferType& buf)
261 m_tagstack.
push( src, srcsize);
289 bool printValue(
const char* src, std::size_t srcsize, BufferType& buf)
310 std::size_t cltagsize;
312 if (!m_tagstack.
top( cltag, cltagsize) || !cltagsize)
334 if (m_tagstack.
empty())
368 const char* m_lasterror;
bool printDoctype(const char *rootid, const char *publicid, const char *systemid, BufferType &buf)
Prints an XML <!DOCTYPE ...> declaration.
Definition: xmlprinter.hpp:195
XMLPrinter(const XMLPrinter &o)
Copy constructor.
Definition: xmlprinter.hpp:147
State
Internal state.
Definition: xmlprinter.hpp:342
Definition: xmlprinter.hpp:347
Character encoding dependent XML printer.
Definition: xmlprinter.hpp:31
bool printAttribute(const char *src, std::size_t srcsize, BufferType &buf)
Print the start of an attribute name.
Definition: xmlprinter.hpp:271
bool top(const void *&element, std::size_t &elementsize)
Get the topmost tag.
Definition: xmltagstack.hpp:73
void printToBuffer(char ch, BufferType &buf) const
Prints a character to an STL back insertion sequence buffer in the IO character set encoding...
Definition: xmlprinter.hpp:128
stack of tag names
Definition: xmltagstack.hpp:23
uint32_t UChar
Unicode character type.
Definition: char.hpp:37
void printToBuffer(const char *src, std::size_t srcsize, BufferType &buf) const
Prints a character string to an STL back insertion sequence buffer in the IO character set encoding...
Definition: xmlprinter.hpp:38
const char * lasterror() const
Get the last error occurred.
Definition: xmlprinter.hpp:359
void printToBufferAttributeValue(const char *src, std::size_t srcsize, BufferType &buf) const
print attribute value string
Definition: xmlprinter.hpp:103
void clear()
Definition: xmltagstack.hpp:97
bool empty() const
Find out if the stack is empty.
Definition: xmltagstack.hpp:92
State state() const
Get the current internal state.
Definition: xmlprinter.hpp:352
bool printOpenTag(const char *src, std::size_t srcsize, BufferType &buf)
Print the start of an open tag.
Definition: xmlprinter.hpp:255
void pop()
Pop (remove) the topmost tag.
Definition: xmltagstack.hpp:82
XMLPrinter(const IOCharset &output_, bool subDocument=false)
Constructor.
Definition: xmlprinter.hpp:143
void printToBufferSubstChr(const char *src, std::size_t srcsize, BufferType &buf, unsigned int nof_echr, const char *echr, const char **estr) const
print a value with some characters replaced by a string
Definition: xmlprinter.hpp:79
void printEsc(char ch, BufferType &buf, unsigned int nof_echr, const char *echr, const char **estr) const
print a character substitute or the character itself
Definition: xmlprinter.hpp:57
UChar chr()
Get the unicode representation of the current character.
Definition: textscanner.hpp:161
Reader for scanning the input character by character.
Definition: textscanner.hpp:68
Input iterator on a constant string returning null characters after EOF as required by textwolf scann...
Definition: cstringiterator.hpp:23
void push(const char *pp, std::size_t nn)
Push a tag on top.
Definition: xmltagstack.hpp:50
XML parser iterator interface for processing the XML elements one by one.
textwolf iterator on strings
bool printCloseTag(BufferType &buf)
Print the close of the current tag open.
Definition: xmlprinter.hpp:307
void printToBufferContent(const char *src, std::size_t srcsize, BufferType &buf) const
print content value string
Definition: xmlprinter.hpp:117
bool printValue(const char *src, std::size_t srcsize, BufferType &buf)
Print a content or attribute value depending on context.
Definition: xmlprinter.hpp:289
bool printHeader(const char *encoding, const char *standalone, BufferType &buf)
Prints an XML header (version "1.0")
Definition: xmlprinter.hpp:165
Definition: xmlprinter.hpp:346
XMLPrinter(bool subDocument=false)
Default constructor.
Definition: xmlprinter.hpp:137
Definition: xmlprinter.hpp:344
void reset(bool subDocument=false)
Reset the state.
Definition: xmlprinter.hpp:153
Implementation of iterator for character-wise parsing of input.
bool exitTagContext(BufferType &buf)
Close the current tag attribute context opened.
Definition: xmlprinter.hpp:235
Definition: xmlprinter.hpp:345
Character set encodings already implemented in textwolf.