1 #ifndef RAPIDXML_PRINT_HPP_INCLUDED
2 #define RAPIDXML_PRINT_HPP_INCLUDED
13 #ifndef RAPIDXML_NO_STREAMS
37 template<
class OutIt,
class Ch>
38 inline OutIt copy_chars(
const Ch* begin,
const Ch* end, OutIt out)
50 template<
class OutIt,
class Ch>
51 inline OutIt copy_and_expand_chars(
const Ch* begin,
const Ch* end, Ch noexpand, OutIt out)
55 if (*begin == noexpand)
115 template<
class OutIt,
class Ch>
116 inline OutIt fill_chars(OutIt out,
int n, Ch ch)
118 for (
int i = 0; i < n; ++i)
127 template<
class Ch, Ch ch>
128 inline bool find_char(
const Ch* begin,
const Ch* end)
144 template<
class OutIt,
class Ch>
145 OutIt print_children(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent);
147 template<
class OutIt,
class Ch>
148 inline OutIt print_element_node(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent);
150 template<
class OutIt,
class Ch>
151 inline OutIt print_attributes(OutIt out,
const xml_node<Ch>* node,
int flags);
153 template<
class OutIt,
class Ch>
154 inline OutIt print_data_node(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent);
156 template<
class OutIt,
class Ch>
157 inline OutIt print_cdata_node(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent);
159 template<
class OutIt,
class Ch>
160 inline OutIt print_declaration_node(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent);
162 template<
class OutIt,
class Ch>
163 inline OutIt print_comment_node(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent);
165 template<
class OutIt,
class Ch>
166 inline OutIt print_doctype_node(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent);
168 template<
class OutIt,
class Ch>
169 inline OutIt print_pi_node(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent);
172 template<
class OutIt,
class Ch>
173 inline OutIt print_node(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent)
176 switch (node->
type())
181 out = print_children(out, node, flags, indent);
186 out = print_element_node(out, node, flags, indent);
191 out = print_data_node(out, node, flags, indent);
196 out = print_cdata_node(out, node, flags, indent);
201 out = print_declaration_node(out, node, flags, indent);
206 out = print_comment_node(out, node, flags, indent);
211 out = print_doctype_node(out, node, flags, indent);
216 out = print_pi_node(out, node, flags, indent);
228 *out = Ch(
'\n'), ++out;
236 template<
class OutIt,
class Ch>
237 inline OutIt print_children(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent)
241 out = print_node(out, child, flags, indent);
248 template<
class OutIt,
class Ch>
249 inline OutIt print_attributes(OutIt out,
const xml_node<Ch>* node,
int flags)
253 if (attribute->name() && attribute->value())
256 *out = Ch(
' '), ++out;
257 out = copy_chars(attribute->name(), attribute->name() + attribute->name_size(), out);
258 *out = Ch(
'='), ++out;
261 if (find_char < Ch, Ch(
'"') > (attribute->value(), attribute->value() + attribute->value_size()))
263 *out = Ch(
'\''), ++out;
264 out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch(
'"'), out);
265 *out = Ch(
'\''), ++out;
269 *out = Ch(
'"'), ++out;
270 out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch(
'\''), out);
271 *out = Ch(
'"'), ++out;
280 template<
class OutIt,
class Ch>
281 inline OutIt print_data_node(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent)
287 out = fill_chars(out, indent, Ch(
'\t'));
290 out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);
295 template<
class OutIt,
class Ch>
296 inline OutIt print_cdata_node(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent)
302 out = fill_chars(out, indent, Ch(
'\t'));
323 out = copy_chars(node->value(), node->value() + node->value_size(), out);
334 template<
class OutIt,
class Ch>
335 inline OutIt print_element_node(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent)
342 out = fill_chars(out, indent, Ch(
'\t'));
345 *out = Ch(
'<'), ++out;
346 out = copy_chars(node->name(), node->name() + node->name_size(), out);
347 out = print_attributes(out, node, flags);
350 if (node->value_size() == 0 && !node->
first_node())
353 *out = Ch(
'/'), ++out;
354 *out = Ch(
'>'), ++out;
359 *out = Ch(
'>'), ++out;
367 out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);
372 out = copy_and_expand_chars(child->value(), child->value() + child->value_size(), Ch(0), out);
379 *out = Ch(
'\n'), ++out;
382 out = print_children(out, node, flags, indent + 1);
386 out = fill_chars(out, indent, Ch(
'\t'));
391 *out = Ch(
'<'), ++out;
392 *out = Ch(
'/'), ++out;
393 out = copy_chars(node->name(), node->name() + node->name_size(), out);
394 *out = Ch(
'>'), ++out;
401 template<
class OutIt,
class Ch>
402 inline OutIt print_declaration_node(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent)
407 out = fill_chars(out, indent, Ch(
'\t'));
410 *out = Ch(
'<'), ++out;
411 *out = Ch(
'?'), ++out;
412 *out = Ch(
'x'), ++out;
413 *out = Ch(
'm'), ++out;
414 *out = Ch(
'l'), ++out;
417 out = print_attributes(out, node, flags);
420 *out = Ch(
'?'), ++out;
421 *out = Ch(
'>'), ++out;
427 template<
class OutIt,
class Ch>
428 inline OutIt print_comment_node(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent)
434 out = fill_chars(out, indent, Ch(
'\t'));
437 *out = Ch(
'<'), ++out;
438 *out = Ch(
'!'), ++out;
439 *out = Ch(
'-'), ++out;
440 *out = Ch(
'-'), ++out;
441 out = copy_chars(node->value(), node->value() + node->value_size(), out);
442 *out = Ch(
'-'), ++out;
443 *out = Ch(
'-'), ++out;
444 *out = Ch(
'>'), ++out;
449 template<
class OutIt,
class Ch>
450 inline OutIt print_doctype_node(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent)
456 out = fill_chars(out, indent, Ch(
'\t'));
459 *out = Ch(
'<'), ++out;
460 *out = Ch(
'!'), ++out;
461 *out = Ch(
'D'), ++out;
462 *out = Ch(
'O'), ++out;
463 *out = Ch(
'C'), ++out;
464 *out = Ch(
'T'), ++out;
465 *out = Ch(
'Y'), ++out;
466 *out = Ch(
'P'), ++out;
467 *out = Ch(
'E'), ++out;
468 *out = Ch(
' '), ++out;
469 out = copy_chars(node->value(), node->value() + node->value_size(), out);
470 *out = Ch(
'>'), ++out;
475 template<
class OutIt,
class Ch>
476 inline OutIt print_pi_node(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent)
482 out = fill_chars(out, indent, Ch(
'\t'));
485 *out = Ch(
'<'), ++out;
486 *out = Ch(
'?'), ++out;
487 out = copy_chars(node->name(), node->name() + node->name_size(), out);
488 *out = Ch(
' '), ++out;
489 out = copy_chars(node->value(), node->value() + node->value_size(), out);
490 *out = Ch(
'?'), ++out;
491 *out = Ch(
'>'), ++out;
506 template<
class OutIt,
class Ch>
509 return internal::print_node(out, &node, flags, 0);
512 #ifndef RAPIDXML_NO_STREAMS
520 inline std::basic_ostream<Ch>&
print(std::basic_ostream<Ch>& out,
const xml_node<Ch>& node,
int flags = 0)
522 print(std::ostream_iterator<Ch>(out), node, flags);
533 return print(out, node);