1 #ifndef RAPIDXML_PRINT_HPP_INCLUDED
2 #define RAPIDXML_PRINT_HPP_INCLUDED
4 // Copyright (C) 2006, 2009 Marcin Kalicinski
6 // Revision $DateTime: 2009/05/13 01:46:17 $
7 //! \file rapidxml_print.hpp This file contains rapidxml printer implementation
9 #include "rapidxml.hpp"
11 // Only include streams if not disabled
12 #ifndef RAPIDXML_NO_STREAMS
20 ///////////////////////////////////////////////////////////////////////
23 const int print_no_indenting =
24 0x1; //!< Printer flag instructing the printer to suppress indenting of XML. See print() function.
26 ///////////////////////////////////////////////////////////////////////
33 ///////////////////////////////////////////////////////////////////////////
34 // Internal character operations
36 // Copy characters from given range to given output iterator
37 template<class OutIt, class Ch>
38 inline OutIt copy_chars(const Ch *begin, const Ch *end, OutIt out)
45 // Copy characters from given range to given output iterator and expand
46 // characters into references (< > ' " &)
47 template<class OutIt, class Ch>
48 inline OutIt copy_and_expand_chars(const Ch *begin, const Ch *end, Ch noexpand, OutIt out)
52 if (*begin == noexpand)
54 *out++ = *begin; // No expansion, copy character
61 *out++ = Ch('&'); *out++ = Ch('l'); *out++ = Ch('t'); *out++ = Ch(';');
64 *out++ = Ch('&'); *out++ = Ch('g'); *out++ = Ch('t'); *out++ = Ch(';');
67 *out++ = Ch('&'); *out++ = Ch('a'); *out++ = Ch('p'); *out++ = Ch('o'); *out++ = Ch('s');
71 *out++ = Ch('&'); *out++ = Ch('q'); *out++ = Ch('u'); *out++ = Ch('o'); *out++ = Ch('t');
75 *out++ = Ch('&'); *out++ = Ch('a'); *out++ = Ch('m'); *out++ = Ch('p'); *out++ = Ch(';');
78 *out++ = *begin; // No expansion, copy character
81 ++begin; // Step to next character
86 // Fill given output iterator with repetitions of the same character
87 template<class OutIt, class Ch>
88 inline OutIt fill_chars(OutIt out, int n, Ch ch)
90 for (int i = 0; i < n; ++i)
96 template<class Ch, Ch ch>
97 inline bool find_char(const Ch *begin, const Ch *end)
105 template<class OutIt, class Ch>
106 inline OutIt print_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
108 // Print children of the node
109 template<class OutIt, class Ch>
110 inline OutIt print_children(OutIt out, const xml_node<Ch> *node, int flags, int indent)
112 for (xml_node<Ch> *child = node->first_node(); child; child = child->next_sibling())
113 out = print_node(out, child, flags, indent);
117 // Print attributes of the node
118 template<class OutIt, class Ch>
119 inline OutIt print_attributes(OutIt out, const xml_node<Ch> *node, int flags)
121 for (xml_attribute<Ch> *attribute = node->first_attribute(); attribute;
122 attribute = attribute->next_attribute())
124 if (attribute->name() && attribute->value())
126 // Print attribute name
127 *out = Ch(' '), ++out;
128 out = copy_chars(attribute->name(), attribute->name() + attribute->name_size(), out);
129 *out = Ch('='), ++out;
130 // Print attribute value using appropriate quote type
131 if (find_char<Ch, Ch('"')>(attribute->value(), attribute->value() + attribute->value_size()))
133 *out = Ch('\''), ++out;
134 out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(),
136 *out = Ch('\''), ++out;
140 *out = Ch('"'), ++out;
141 out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(),
143 *out = Ch('"'), ++out;
151 template<class OutIt, class Ch>
152 inline OutIt print_data_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
154 assert(node->type() == node_data);
155 if (!(flags & print_no_indenting))
156 out = fill_chars(out, indent, Ch('\t'));
157 out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);
162 template<class OutIt, class Ch>
163 inline OutIt print_cdata_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
165 assert(node->type() == node_cdata);
166 if (!(flags & print_no_indenting))
167 out = fill_chars(out, indent, Ch('\t'));
168 *out = Ch('<'); ++out;
169 *out = Ch('!'); ++out;
170 *out = Ch('['); ++out;
171 *out = Ch('C'); ++out;
172 *out = Ch('D'); ++out;
173 *out = Ch('A'); ++out;
174 *out = Ch('T'); ++out;
175 *out = Ch('A'); ++out;
176 *out = Ch('['); ++out;
177 out = copy_chars(node->value(), node->value() + node->value_size(), out);
178 *out = Ch(']'); ++out;
179 *out = Ch(']'); ++out;
180 *out = Ch('>'); ++out;
184 // Print element node
185 template<class OutIt, class Ch>
186 inline OutIt print_element_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
188 assert(node->type() == node_element);
190 // Print element name and attributes, if any
191 if (!(flags & print_no_indenting))
192 out = fill_chars(out, indent, Ch('\t'));
193 *out = Ch('<'), ++out;
194 out = copy_chars(node->name(), node->name() + node->name_size(), out);
195 out = print_attributes(out, node, flags);
197 // If node is childless
198 if (node->value_size() == 0 && !node->first_node())
200 // Print childless node tag ending
201 *out = Ch('/'), ++out;
202 *out = Ch('>'), ++out;
206 // Print normal node tag ending
207 *out = Ch('>'), ++out;
209 // Test if node contains a single data node only (and no other nodes)
210 xml_node<Ch> *child = node->first_node();
213 // If node has no children, only print its value without indenting
214 out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);
216 else if (child->next_sibling() == 0 && child->type() == node_data)
218 // If node has a sole data child, only print its value without indenting
219 out = copy_and_expand_chars(child->value(), child->value() + child->value_size(), Ch(0), out);
223 // Print all children with full indenting
224 if (!(flags & print_no_indenting))
225 *out = Ch('\n'), ++out;
226 out = print_children(out, node, flags, indent + 1);
227 if (!(flags & print_no_indenting))
228 out = fill_chars(out, indent, Ch('\t'));
232 *out = Ch('<'), ++out;
233 *out = Ch('/'), ++out;
234 out = copy_chars(node->name(), node->name() + node->name_size(), out);
235 *out = Ch('>'), ++out;
240 // Print declaration node
241 template<class OutIt, class Ch>
242 inline OutIt print_declaration_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
244 // Print declaration start
245 if (!(flags & print_no_indenting))
246 out = fill_chars(out, indent, Ch('\t'));
247 *out = Ch('<'), ++out;
248 *out = Ch('?'), ++out;
249 *out = Ch('x'), ++out;
250 *out = Ch('m'), ++out;
251 *out = Ch('l'), ++out;
254 out = print_attributes(out, node, flags);
256 // Print declaration end
257 *out = Ch('?'), ++out;
258 *out = Ch('>'), ++out;
263 // Print comment node
264 template<class OutIt, class Ch>
265 inline OutIt print_comment_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
267 assert(node->type() == node_comment);
268 if (!(flags & print_no_indenting))
269 out = fill_chars(out, indent, Ch('\t'));
270 *out = Ch('<'), ++out;
271 *out = Ch('!'), ++out;
272 *out = Ch('-'), ++out;
273 *out = Ch('-'), ++out;
274 out = copy_chars(node->value(), node->value() + node->value_size(), out);
275 *out = Ch('-'), ++out;
276 *out = Ch('-'), ++out;
277 *out = Ch('>'), ++out;
281 // Print doctype node
282 template<class OutIt, class Ch>
283 inline OutIt print_doctype_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
285 assert(node->type() == node_doctype);
286 if (!(flags & print_no_indenting))
287 out = fill_chars(out, indent, Ch('\t'));
288 *out = Ch('<'), ++out;
289 *out = Ch('!'), ++out;
290 *out = Ch('D'), ++out;
291 *out = Ch('O'), ++out;
292 *out = Ch('C'), ++out;
293 *out = Ch('T'), ++out;
294 *out = Ch('Y'), ++out;
295 *out = Ch('P'), ++out;
296 *out = Ch('E'), ++out;
297 *out = Ch(' '), ++out;
298 out = copy_chars(node->value(), node->value() + node->value_size(), out);
299 *out = Ch('>'), ++out;
304 template<class OutIt, class Ch>
305 inline OutIt print_pi_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
307 assert(node->type() == node_pi);
308 if (!(flags & print_no_indenting))
309 out = fill_chars(out, indent, Ch('\t'));
310 *out = Ch('<'), ++out;
311 *out = Ch('?'), ++out;
312 out = copy_chars(node->name(), node->name() + node->name_size(), out);
313 *out = Ch(' '), ++out;
314 out = copy_chars(node->value(), node->value() + node->value_size(), out);
315 *out = Ch('?'), ++out;
316 *out = Ch('>'), ++out;
319 ///////////////////////////////////////////////////////////////////////////
320 // Internal printing operations
323 template<class OutIt, class Ch>
324 inline OutIt print_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
326 // Print proper node type
327 switch (node->type())
332 out = print_children(out, node, flags, indent);
337 out = print_element_node(out, node, flags, indent);
342 out = print_data_node(out, node, flags, indent);
347 out = print_cdata_node(out, node, flags, indent);
351 case node_declaration:
352 out = print_declaration_node(out, node, flags, indent);
357 out = print_comment_node(out, node, flags, indent);
362 out = print_doctype_node(out, node, flags, indent);
367 out = print_pi_node(out, node, flags, indent);
376 // If indenting not disabled, add line break after node
377 if (!(flags & print_no_indenting))
378 *out = Ch('\n'), ++out;
380 // Return modified iterator
386 ///////////////////////////////////////////////////////////////////////////
389 //! Prints XML to given output iterator.
390 //! \param out Output iterator to print to.
391 //! \param node Node to be printed. Pass xml_document to print entire document.
392 //! \param flags Flags controlling how XML is printed.
393 //! \return Output iterator pointing to position immediately after last character of printed text.
394 template<class OutIt, class Ch>
395 inline OutIt print(OutIt out, const xml_node<Ch> &node, int flags = 0)
397 return internal::print_node(out, &node, flags, 0);
400 #ifndef RAPIDXML_NO_STREAMS
402 //! Prints XML to given output stream.
403 //! \param out Output stream to print to.
404 //! \param node Node to be printed. Pass xml_document to print entire document.
405 //! \param flags Flags controlling how XML is printed.
406 //! \return Output stream.
408 inline std::basic_ostream<Ch> &print(std::basic_ostream<Ch> &out, const xml_node<Ch> &node,
411 print(std::ostream_iterator<Ch>(out), node, flags);
415 //! Prints formatted XML to given output stream. Uses default printing flags. Use print() function to customize printing process.
416 //! \param out Output stream to print to.
417 //! \param node Node to be printed.
418 //! \return Output stream.
420 inline std::basic_ostream<Ch> &operator <<(std::basic_ostream<Ch> &out, const xml_node<Ch> &node)
422 return print(out, node);