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_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
110 switch (node->
type())
115 out = print_children(out, node, flags, indent);
120 out = print_element_node(out, node, flags, indent);
125 out = print_data_node(out, node, flags, indent);
130 out = print_cdata_node(out, node, flags, indent);
135 out = print_declaration_node(out, node, flags, indent);
140 out = print_comment_node(out, node, flags, indent);
145 out = print_doctype_node(out, node, flags, indent);
150 out = print_pi_node(out, node, flags, indent);
161 *out = Ch(
'\n'), ++out;
168 template<
class OutIt,
class Ch>
169 inline OutIt print_children(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
172 out = print_node(out, child, flags, indent);
177 template<
class OutIt,
class Ch>
178 inline OutIt print_attributes(OutIt out,
const xml_node<Ch> *node,
int flags)
182 if (attribute->name() && attribute->value())
185 *out = Ch(
' '), ++out;
186 out = copy_chars(attribute->name(), attribute->name() + attribute->name_size(), out);
187 *out = Ch(
'='), ++out;
189 if (find_char<Ch, Ch(
'"')>(attribute->value(), attribute->value() + attribute->value_size()))
191 *out = Ch(
'\''), ++out;
192 out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch(
'"'), out);
193 *out = Ch(
'\''), ++out;
197 *out = Ch(
'"'), ++out;
198 out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch(
'\''), out);
199 *out = Ch(
'"'), ++out;
207 template<
class OutIt,
class Ch>
208 inline OutIt print_data_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
212 out = fill_chars(out, indent, Ch(
'\t'));
213 out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);
218 template<
class OutIt,
class Ch>
219 inline OutIt print_cdata_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
223 out = fill_chars(out, indent, Ch(
'\t'));
224 *out = Ch(
'<'); ++out;
225 *out = Ch(
'!'); ++out;
226 *out = Ch(
'['); ++out;
227 *out = Ch(
'C'); ++out;
228 *out = Ch(
'D'); ++out;
229 *out = Ch(
'A'); ++out;
230 *out = Ch(
'T'); ++out;
231 *out = Ch(
'A'); ++out;
232 *out = Ch(
'['); ++out;
233 out = copy_chars(node->value(), node->value() + node->value_size(), out);
234 *out = Ch(
']'); ++out;
235 *out = Ch(
']'); ++out;
236 *out = Ch(
'>'); ++out;
241 template<
class OutIt,
class Ch>
242 inline OutIt print_element_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
248 out = fill_chars(out, indent, Ch(
'\t'));
249 *out = Ch(
'<'), ++out;
250 out = copy_chars(node->name(), node->name() + node->name_size(), out);
251 out = print_attributes(out, node, flags);
254 if (node->value_size() == 0 && !node->
first_node())
257 *out = Ch(
'/'), ++out;
258 *out = Ch(
'>'), ++out;
263 *out = Ch(
'>'), ++out;
270 out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);
275 out = copy_and_expand_chars(child->value(), child->value() + child->value_size(), Ch(0), out);
281 *out = Ch(
'\n'), ++out;
282 out = print_children(out, node, flags, indent + 1);
284 out = fill_chars(out, indent, Ch(
'\t'));
288 *out = Ch(
'<'), ++out;
289 *out = Ch(
'/'), ++out;
290 out = copy_chars(node->name(), node->name() + node->name_size(), out);
291 *out = Ch(
'>'), ++out;
297 template<
class OutIt,
class Ch>
298 inline OutIt print_declaration_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
302 out = fill_chars(out, indent, Ch(
'\t'));
303 *out = Ch(
'<'), ++out;
304 *out = Ch(
'?'), ++out;
305 *out = Ch(
'x'), ++out;
306 *out = Ch(
'm'), ++out;
307 *out = Ch(
'l'), ++out;
310 out = print_attributes(out, node, flags);
313 *out = Ch(
'?'), ++out;
314 *out = Ch(
'>'), ++out;
320 template<
class OutIt,
class Ch>
321 inline OutIt print_comment_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
325 out = fill_chars(out, indent, Ch(
'\t'));
326 *out = Ch(
'<'), ++out;
327 *out = Ch(
'!'), ++out;
328 *out = Ch(
'-'), ++out;
329 *out = Ch(
'-'), ++out;
330 out = copy_chars(node->value(), node->value() + node->value_size(), out);
331 *out = Ch(
'-'), ++out;
332 *out = Ch(
'-'), ++out;
333 *out = Ch(
'>'), ++out;
338 template<
class OutIt,
class Ch>
339 inline OutIt print_doctype_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
343 out = fill_chars(out, indent, Ch(
'\t'));
344 *out = Ch(
'<'), ++out;
345 *out = Ch(
'!'), ++out;
346 *out = Ch(
'D'), ++out;
347 *out = Ch(
'O'), ++out;
348 *out = Ch(
'C'), ++out;
349 *out = Ch(
'T'), ++out;
350 *out = Ch(
'Y'), ++out;
351 *out = Ch(
'P'), ++out;
352 *out = Ch(
'E'), ++out;
353 *out = Ch(
' '), ++out;
354 out = copy_chars(node->value(), node->value() + node->value_size(), out);
355 *out = Ch(
'>'), ++out;
360 template<
class OutIt,
class Ch>
361 inline OutIt print_pi_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
365 out = fill_chars(out, indent, Ch(
'\t'));
366 *out = Ch(
'<'), ++out;
367 *out = Ch(
'?'), ++out;
368 out = copy_chars(node->name(), node->name() + node->name_size(), out);
369 *out = Ch(
' '), ++out;
370 out = copy_chars(node->value(), node->value() + node->value_size(), out);
371 *out = Ch(
'?'), ++out;
372 *out = Ch(
'>'), ++out;
387 template<
class OutIt,
class Ch>
390 return internal::print_node(out, &node, flags, 0);
393 #ifndef RAPIDXML_NO_STREAMS
401 inline std::basic_ostream<Ch> &
print(std::basic_ostream<Ch> &out,
const xml_node<Ch> &node,
int flags = 0)
403 print(std::ostream_iterator<Ch>(out), node, flags);
414 return print(out, node);