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 template<
class OutIt,
class Ch>
111 inline OutIt print_children(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
114 out = print_node(out, child, flags, indent);
119 template<
class OutIt,
class Ch>
120 inline OutIt print_attributes(OutIt out,
const xml_node<Ch> *node,
int flags)
124 if (attribute->name() && attribute->value())
127 *out = Ch(
' '), ++out;
128 out = copy_chars(attribute->name(), attribute->name() + attribute->name_size(), out);
129 *out = Ch(
'='), ++out;
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(), Ch(
'"'), out);
135 *out = Ch(
'\''), ++out;
139 *out = Ch(
'"'), ++out;
140 out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch(
'\''), out);
141 *out = Ch(
'"'), ++out;
149 template<
class OutIt,
class Ch>
150 inline OutIt print_data_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
154 out = fill_chars(out, indent, Ch(
'\t'));
155 out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);
160 template<
class OutIt,
class Ch>
161 inline OutIt print_cdata_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
165 out = fill_chars(out, indent, Ch(
'\t'));
166 *out = Ch(
'<'); ++out;
167 *out = Ch(
'!'); ++out;
168 *out = Ch(
'['); ++out;
169 *out = Ch(
'C'); ++out;
170 *out = Ch(
'D'); ++out;
171 *out = Ch(
'A'); ++out;
172 *out = Ch(
'T'); ++out;
173 *out = Ch(
'A'); ++out;
174 *out = Ch(
'['); ++out;
175 out = copy_chars(node->value(), node->value() + node->value_size(), out);
176 *out = Ch(
']'); ++out;
177 *out = Ch(
']'); ++out;
178 *out = Ch(
'>'); ++out;
183 template<
class OutIt,
class Ch>
184 inline OutIt print_element_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
190 out = fill_chars(out, indent, Ch(
'\t'));
191 *out = Ch(
'<'), ++out;
192 out = copy_chars(node->name(), node->name() + node->name_size(), out);
193 out = print_attributes(out, node, flags);
196 if (node->value_size() == 0 && !node->
first_node())
199 *out = Ch(
'/'), ++out;
200 *out = Ch(
'>'), ++out;
205 *out = Ch(
'>'), ++out;
212 out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);
217 out = copy_and_expand_chars(child->value(), child->value() + child->value_size(), Ch(0), out);
223 *out = Ch(
'\n'), ++out;
224 out = print_children(out, node, flags, indent + 1);
226 out = fill_chars(out, indent, Ch(
'\t'));
230 *out = Ch(
'<'), ++out;
231 *out = Ch(
'/'), ++out;
232 out = copy_chars(node->name(), node->name() + node->name_size(), out);
233 *out = Ch(
'>'), ++out;
239 template<
class OutIt,
class Ch>
240 inline OutIt print_declaration_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
244 out = fill_chars(out, indent, Ch(
'\t'));
245 *out = Ch(
'<'), ++out;
246 *out = Ch(
'?'), ++out;
247 *out = Ch(
'x'), ++out;
248 *out = Ch(
'm'), ++out;
249 *out = Ch(
'l'), ++out;
252 out = print_attributes(out, node, flags);
255 *out = Ch(
'?'), ++out;
256 *out = Ch(
'>'), ++out;
262 template<
class OutIt,
class Ch>
263 inline OutIt print_comment_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
267 out = fill_chars(out, indent, Ch(
'\t'));
268 *out = Ch(
'<'), ++out;
269 *out = Ch(
'!'), ++out;
270 *out = Ch(
'-'), ++out;
271 *out = Ch(
'-'), ++out;
272 out = copy_chars(node->value(), node->value() + node->value_size(), out);
273 *out = Ch(
'-'), ++out;
274 *out = Ch(
'-'), ++out;
275 *out = Ch(
'>'), ++out;
280 template<
class OutIt,
class Ch>
281 inline OutIt print_doctype_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
285 out = fill_chars(out, indent, Ch(
'\t'));
286 *out = Ch(
'<'), ++out;
287 *out = Ch(
'!'), ++out;
288 *out = Ch(
'D'), ++out;
289 *out = Ch(
'O'), ++out;
290 *out = Ch(
'C'), ++out;
291 *out = Ch(
'T'), ++out;
292 *out = Ch(
'Y'), ++out;
293 *out = Ch(
'P'), ++out;
294 *out = Ch(
'E'), ++out;
295 *out = Ch(
' '), ++out;
296 out = copy_chars(node->value(), node->value() + node->value_size(), out);
297 *out = Ch(
'>'), ++out;
302 template<
class OutIt,
class Ch>
303 inline OutIt print_pi_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
307 out = fill_chars(out, indent, Ch(
'\t'));
308 *out = Ch(
'<'), ++out;
309 *out = Ch(
'?'), ++out;
310 out = copy_chars(node->name(), node->name() + node->name_size(), out);
311 *out = Ch(
' '), ++out;
312 out = copy_chars(node->value(), node->value() + node->value_size(), out);
313 *out = Ch(
'?'), ++out;
314 *out = Ch(
'>'), ++out;
319 template<
class OutIt,
class Ch>
320 inline OutIt print_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
323 switch (node->
type())
328 out = print_children(out, node, flags, indent);
333 out = print_element_node(out, node, flags, indent);
338 out = print_data_node(out, node, flags, indent);
343 out = print_cdata_node(out, node, flags, indent);
348 out = print_declaration_node(out, node, flags, indent);
353 out = print_comment_node(out, node, flags, indent);
358 out = print_doctype_node(out, node, flags, indent);
363 out = print_pi_node(out, node, flags, indent);
374 *out = Ch(
'\n'), ++out;
391 template<
class OutIt,
class Ch>
394 return internal::print_node(out, &node, flags, 0);
397 #ifndef RAPIDXML_NO_STREAMS
405 inline std::basic_ostream<Ch> &
print(std::basic_ostream<Ch> &out,
const xml_node<Ch> &node,
int flags = 0)
407 print(std::ostream_iterator<Ch>(out), node, flags);
418 return print(out, node);