1 #ifndef RAPIDXML_PRINT_HPP_INCLUDED
2 #define RAPIDXML_PRINT_HPP_INCLUDED
12 #ifndef RAPIDXML_NO_STREAMS
36 template<
class OutIt,
class Ch>
37 inline OutIt copy_chars(
const Ch *begin,
const Ch *end, OutIt out)
46 template<
class OutIt,
class Ch>
47 inline OutIt copy_and_expand_chars(
const Ch *begin,
const Ch *end, Ch noexpand, OutIt out)
51 if (*begin == noexpand)
60 *out++ = Ch(
'&'); *out++ = Ch(
'l'); *out++ = Ch(
't'); *out++ = Ch(
';');
63 *out++ = Ch(
'&'); *out++ = Ch(
'g'); *out++ = Ch(
't'); *out++ = Ch(
';');
66 *out++ = Ch(
'&'); *out++ = Ch(
'a'); *out++ = Ch(
'p'); *out++ = Ch(
'o'); *out++ = Ch(
's'); *out++ = Ch(
';');
69 *out++ = Ch(
'&'); *out++ = Ch(
'q'); *out++ = Ch(
'u'); *out++ = Ch(
'o'); *out++ = Ch(
't'); *out++ = Ch(
';');
72 *out++ = Ch(
'&'); *out++ = Ch(
'a'); *out++ = Ch(
'm'); *out++ = Ch(
'p'); *out++ = Ch(
';');
84 template<
class OutIt,
class Ch>
85 inline OutIt fill_chars(OutIt out,
int n, Ch ch)
87 for (
int i = 0; i < n; ++i)
93 template<
class Ch, Ch ch>
94 inline bool find_char(
const Ch *begin,
const Ch *end)
106 template<
class OutIt,
class Ch>
107 inline OutIt print_children(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent);
109 template<
class OutIt,
class Ch>
110 inline OutIt print_attributes(OutIt out,
const xml_node<Ch>* node,
int flags);
112 template<
class OutIt,
class Ch>
113 inline OutIt print_data_node(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent);
115 template<
class OutIt,
class Ch>
116 inline OutIt print_cdata_node(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent);
118 template<
class OutIt,
class Ch>
119 inline OutIt print_element_node(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent);
121 template<
class OutIt,
class Ch>
122 inline OutIt print_declaration_node(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent);
124 template<
class OutIt,
class Ch>
125 inline OutIt print_comment_node(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent);
127 template<
class OutIt,
class Ch>
128 inline OutIt print_doctype_node(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent);
130 template<
class OutIt,
class Ch>
131 inline OutIt print_pi_node(OutIt out,
const xml_node<Ch>* node,
int flags,
int indent);
135 template<
class OutIt,
class Ch>
136 inline OutIt print_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
139 switch (node->
type())
144 out = print_children(out, node, flags, indent);
149 out = print_element_node(out, node, flags, indent);
154 out = print_data_node(out, node, flags, indent);
159 out = print_cdata_node(out, node, flags, indent);
164 out = print_declaration_node(out, node, flags, indent);
169 out = print_comment_node(out, node, flags, indent);
174 out = print_doctype_node(out, node, flags, indent);
179 out = print_pi_node(out, node, flags, indent);
190 *out = Ch(
'\n'), ++out;
197 template<
class OutIt,
class Ch>
198 inline OutIt print_children(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
201 out = print_node(out, child, flags, indent);
206 template<
class OutIt,
class Ch>
207 inline OutIt print_attributes(OutIt out,
const xml_node<Ch> *node,
int flags)
211 if (attribute->name() && attribute->value())
214 *out = Ch(
' '), ++out;
215 out = copy_chars(attribute->name(), attribute->name() + attribute->name_size(), out);
216 *out = Ch(
'='), ++out;
218 if (find_char<Ch, Ch(
'"')>(attribute->value(), attribute->value() + attribute->value_size()))
220 *out = Ch(
'\''), ++out;
221 out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch(
'"'), out);
222 *out = Ch(
'\''), ++out;
226 *out = Ch(
'"'), ++out;
227 out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch(
'\''), out);
228 *out = Ch(
'"'), ++out;
236 template<
class OutIt,
class Ch>
237 inline OutIt print_data_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
241 out = fill_chars(out, indent, Ch(
'\t'));
242 out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);
247 template<
class OutIt,
class Ch>
248 inline OutIt print_cdata_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
252 out = fill_chars(out, indent, Ch(
'\t'));
253 *out = Ch(
'<'); ++out;
254 *out = Ch(
'!'); ++out;
255 *out = Ch(
'['); ++out;
256 *out = Ch(
'C'); ++out;
257 *out = Ch(
'D'); ++out;
258 *out = Ch(
'A'); ++out;
259 *out = Ch(
'T'); ++out;
260 *out = Ch(
'A'); ++out;
261 *out = Ch(
'['); ++out;
262 out = copy_chars(node->value(), node->value() + node->value_size(), out);
263 *out = Ch(
']'); ++out;
264 *out = Ch(
']'); ++out;
265 *out = Ch(
'>'); ++out;
270 template<
class OutIt,
class Ch>
271 inline OutIt print_element_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
277 out = fill_chars(out, indent, Ch(
'\t'));
278 *out = Ch(
'<'), ++out;
279 out = copy_chars(node->name(), node->name() + node->name_size(), out);
280 out = print_attributes(out, node, flags);
283 if (node->value_size() == 0 && !node->
first_node())
286 *out = Ch(
'/'), ++out;
287 *out = Ch(
'>'), ++out;
292 *out = Ch(
'>'), ++out;
299 out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);
304 out = copy_and_expand_chars(child->value(), child->value() + child->value_size(), Ch(0), out);
310 *out = Ch(
'\n'), ++out;
311 out = print_children(out, node, flags, indent + 1);
313 out = fill_chars(out, indent, Ch(
'\t'));
317 *out = Ch(
'<'), ++out;
318 *out = Ch(
'/'), ++out;
319 out = copy_chars(node->name(), node->name() + node->name_size(), out);
320 *out = Ch(
'>'), ++out;
326 template<
class OutIt,
class Ch>
327 inline OutIt print_declaration_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
331 out = fill_chars(out, indent, Ch(
'\t'));
332 *out = Ch(
'<'), ++out;
333 *out = Ch(
'?'), ++out;
334 *out = Ch(
'x'), ++out;
335 *out = Ch(
'm'), ++out;
336 *out = Ch(
'l'), ++out;
339 out = print_attributes(out, node, flags);
342 *out = Ch(
'?'), ++out;
343 *out = Ch(
'>'), ++out;
349 template<
class OutIt,
class Ch>
350 inline OutIt print_comment_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
354 out = fill_chars(out, indent, Ch(
'\t'));
355 *out = Ch(
'<'), ++out;
356 *out = Ch(
'!'), ++out;
357 *out = Ch(
'-'), ++out;
358 *out = Ch(
'-'), ++out;
359 out = copy_chars(node->value(), node->value() + node->value_size(), out);
360 *out = Ch(
'-'), ++out;
361 *out = Ch(
'-'), ++out;
362 *out = Ch(
'>'), ++out;
367 template<
class OutIt,
class Ch>
368 inline OutIt print_doctype_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
372 out = fill_chars(out, indent, Ch(
'\t'));
373 *out = Ch(
'<'), ++out;
374 *out = Ch(
'!'), ++out;
375 *out = Ch(
'D'), ++out;
376 *out = Ch(
'O'), ++out;
377 *out = Ch(
'C'), ++out;
378 *out = Ch(
'T'), ++out;
379 *out = Ch(
'Y'), ++out;
380 *out = Ch(
'P'), ++out;
381 *out = Ch(
'E'), ++out;
382 *out = Ch(
' '), ++out;
383 out = copy_chars(node->value(), node->value() + node->value_size(), out);
384 *out = Ch(
'>'), ++out;
389 template<
class OutIt,
class Ch>
390 inline OutIt print_pi_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
394 out = fill_chars(out, indent, Ch(
'\t'));
395 *out = Ch(
'<'), ++out;
396 *out = Ch(
'?'), ++out;
397 out = copy_chars(node->name(), node->name() + node->name_size(), out);
398 *out = Ch(
' '), ++out;
399 out = copy_chars(node->value(), node->value() + node->value_size(), out);
400 *out = Ch(
'?'), ++out;
401 *out = Ch(
'>'), ++out;
416 template<
class OutIt,
class Ch>
419 return internal::print_node(out, &node, flags, 0);
422 #ifndef RAPIDXML_NO_STREAMS
430 inline std::basic_ostream<Ch> &
print(std::basic_ostream<Ch> &out,
const xml_node<Ch> &node,
int flags = 0)
432 print(std::ostream_iterator<Ch>(out), node, flags);
443 return print(out, node);