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)
104 template<
class OutIt,
class Ch>
105 inline OutIt print_children(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent);
106 template<
class OutIt,
class Ch>
107 inline OutIt print_data_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent);
108 template<
class OutIt,
class Ch>
109 inline OutIt print_element_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent);
110 template<
class OutIt,
class Ch>
111 inline OutIt print_cdata_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent);
112 template<
class OutIt,
class Ch>
113 inline OutIt print_declaration_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent);
114 template<
class OutIt,
class Ch>
115 inline OutIt print_comment_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent);
116 template<
class OutIt,
class Ch>
117 inline OutIt print_doctype_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent);
118 template<
class OutIt,
class Ch>
119 inline OutIt print_pi_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent);
123 template<
class OutIt,
class Ch>
124 inline OutIt print_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
127 switch (node->
type())
132 out = print_children(out, node, flags, indent);
137 out = print_element_node(out, node, flags, indent);
142 out = print_data_node(out, node, flags, indent);
147 out = print_cdata_node(out, node, flags, indent);
152 out = print_declaration_node(out, node, flags, indent);
157 out = print_comment_node(out, node, flags, indent);
162 out = print_doctype_node(out, node, flags, indent);
167 out = print_pi_node(out, node, flags, indent);
178 *out = Ch(
'\n'), ++out;
185 template<
class OutIt,
class Ch>
186 inline OutIt print_children(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
189 out = print_node(out, child, flags, indent);
194 template<
class OutIt,
class Ch>
195 inline OutIt print_attributes(OutIt out,
const xml_node<Ch> *node,
int flags)
199 if (attribute->name() && attribute->value())
202 *out = Ch(
' '), ++out;
203 out = copy_chars(attribute->name(), attribute->name() + attribute->name_size(), out);
204 *out = Ch(
'='), ++out;
206 if (find_char<Ch, Ch(
'"')>(attribute->value(), attribute->value() + attribute->value_size()))
208 *out = Ch(
'\''), ++out;
209 out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch(
'"'), out);
210 *out = Ch(
'\''), ++out;
214 *out = Ch(
'"'), ++out;
215 out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch(
'\''), out);
216 *out = Ch(
'"'), ++out;
224 template<
class OutIt,
class Ch>
225 inline OutIt print_data_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
229 out = fill_chars(out, indent, Ch(
'\t'));
230 out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);
235 template<
class OutIt,
class Ch>
236 inline OutIt print_cdata_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
240 out = fill_chars(out, indent, Ch(
'\t'));
241 *out = Ch(
'<'); ++out;
242 *out = Ch(
'!'); ++out;
243 *out = Ch(
'['); ++out;
244 *out = Ch(
'C'); ++out;
245 *out = Ch(
'D'); ++out;
246 *out = Ch(
'A'); ++out;
247 *out = Ch(
'T'); ++out;
248 *out = Ch(
'A'); ++out;
249 *out = Ch(
'['); ++out;
250 out = copy_chars(node->value(), node->value() + node->value_size(), out);
251 *out = Ch(
']'); ++out;
252 *out = Ch(
']'); ++out;
253 *out = Ch(
'>'); ++out;
258 template<
class OutIt,
class Ch>
259 inline OutIt print_element_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
265 out = fill_chars(out, indent, Ch(
'\t'));
266 *out = Ch(
'<'), ++out;
267 out = copy_chars(node->name(), node->name() + node->name_size(), out);
268 out = print_attributes(out, node, flags);
271 if (node->value_size() == 0 && !node->
first_node())
274 *out = Ch(
'/'), ++out;
275 *out = Ch(
'>'), ++out;
280 *out = Ch(
'>'), ++out;
287 out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);
292 out = copy_and_expand_chars(child->value(), child->value() + child->value_size(), Ch(0), out);
298 *out = Ch(
'\n'), ++out;
299 out = print_children(out, node, flags, indent + 1);
301 out = fill_chars(out, indent, Ch(
'\t'));
305 *out = Ch(
'<'), ++out;
306 *out = Ch(
'/'), ++out;
307 out = copy_chars(node->name(), node->name() + node->name_size(), out);
308 *out = Ch(
'>'), ++out;
314 template<
class OutIt,
class Ch>
315 inline OutIt print_declaration_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
319 out = fill_chars(out, indent, Ch(
'\t'));
320 *out = Ch(
'<'), ++out;
321 *out = Ch(
'?'), ++out;
322 *out = Ch(
'x'), ++out;
323 *out = Ch(
'm'), ++out;
324 *out = Ch(
'l'), ++out;
327 out = print_attributes(out, node, flags);
330 *out = Ch(
'?'), ++out;
331 *out = Ch(
'>'), ++out;
337 template<
class OutIt,
class Ch>
338 inline OutIt print_comment_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
342 out = fill_chars(out, indent, Ch(
'\t'));
343 *out = Ch(
'<'), ++out;
344 *out = Ch(
'!'), ++out;
345 *out = Ch(
'-'), ++out;
346 *out = Ch(
'-'), ++out;
347 out = copy_chars(node->value(), node->value() + node->value_size(), out);
348 *out = Ch(
'-'), ++out;
349 *out = Ch(
'-'), ++out;
350 *out = Ch(
'>'), ++out;
355 template<
class OutIt,
class Ch>
356 inline OutIt print_doctype_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
360 out = fill_chars(out, indent, Ch(
'\t'));
361 *out = Ch(
'<'), ++out;
362 *out = Ch(
'!'), ++out;
363 *out = Ch(
'D'), ++out;
364 *out = Ch(
'O'), ++out;
365 *out = Ch(
'C'), ++out;
366 *out = Ch(
'T'), ++out;
367 *out = Ch(
'Y'), ++out;
368 *out = Ch(
'P'), ++out;
369 *out = Ch(
'E'), ++out;
370 *out = Ch(
' '), ++out;
371 out = copy_chars(node->value(), node->value() + node->value_size(), out);
372 *out = Ch(
'>'), ++out;
377 template<
class OutIt,
class Ch>
378 inline OutIt print_pi_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
382 out = fill_chars(out, indent, Ch(
'\t'));
383 *out = Ch(
'<'), ++out;
384 *out = Ch(
'?'), ++out;
385 out = copy_chars(node->name(), node->name() + node->name_size(), out);
386 *out = Ch(
' '), ++out;
387 out = copy_chars(node->value(), node->value() + node->value_size(), out);
388 *out = Ch(
'?'), ++out;
389 *out = Ch(
'>'), ++out;
404 template<
class OutIt,
class Ch>
407 return internal::print_node(out, &node, flags, 0);
410 #ifndef RAPIDXML_NO_STREAMS
418 inline std::basic_ostream<Ch> &
print(std::basic_ostream<Ch> &out,
const xml_node<Ch> &node,
int flags = 0)
420 print(std::ostream_iterator<Ch>(out), node, flags);
431 return print(out, node);