rapidxml_print.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. #ifndef RAPIDXML_PRINT_HPP_INCLUDED
  2. #define RAPIDXML_PRINT_HPP_INCLUDED
  3. // Copyright (C) 2006, 2009 Marcin Kalicinski
  4. // Version 1.13
  5. // Revision $DateTime: 2009/05/13 01:46:17 $
  6. //! \file rapidxml_print.hpp This file contains rapidxml printer implementation
  7. #include "rapidxml.hpp"
  8. // Only include streams if not disabled
  9. #ifndef RAPIDXML_NO_STREAMS
  10. #include <ostream>
  11. #include <iterator>
  12. #endif
  13. namespace rapidxml
  14. {
  15. ///////////////////////////////////////////////////////////////////////
  16. // Printing flags
  17. const int print_no_indenting = 0x1; //!< Printer flag instructing the printer to suppress indenting of XML. See print() function.
  18. ///////////////////////////////////////////////////////////////////////
  19. // Internal
  20. //! \cond internal
  21. namespace internal
  22. {
  23. ///////////////////////////////////////////////////////////////////////////
  24. // Internal character operations
  25. // Copy characters from given range to given output iterator
  26. template<class OutIt, class Ch>
  27. inline OutIt copy_chars(const Ch *begin, const Ch *end, OutIt out)
  28. {
  29. while (begin != end)
  30. *out++ = *begin++;
  31. return out;
  32. }
  33. // Copy characters from given range to given output iterator and expand
  34. // characters into references (&lt; &gt; &apos; &quot; &amp;)
  35. template<class OutIt, class Ch>
  36. inline OutIt copy_and_expand_chars(const Ch *begin, const Ch *end, Ch noexpand, OutIt out)
  37. {
  38. while (begin != end)
  39. {
  40. if (*begin == noexpand)
  41. {
  42. *out++ = *begin; // No expansion, copy character
  43. }
  44. else
  45. {
  46. switch (*begin)
  47. {
  48. case Ch('<'):
  49. *out++ = Ch('&'); *out++ = Ch('l'); *out++ = Ch('t'); *out++ = Ch(';');
  50. break;
  51. case Ch('>'):
  52. *out++ = Ch('&'); *out++ = Ch('g'); *out++ = Ch('t'); *out++ = Ch(';');
  53. break;
  54. case Ch('\''):
  55. *out++ = Ch('&'); *out++ = Ch('a'); *out++ = Ch('p'); *out++ = Ch('o'); *out++ = Ch('s'); *out++ = Ch(';');
  56. break;
  57. case Ch('"'):
  58. *out++ = Ch('&'); *out++ = Ch('q'); *out++ = Ch('u'); *out++ = Ch('o'); *out++ = Ch('t'); *out++ = Ch(';');
  59. break;
  60. case Ch('&'):
  61. *out++ = Ch('&'); *out++ = Ch('a'); *out++ = Ch('m'); *out++ = Ch('p'); *out++ = Ch(';');
  62. break;
  63. default:
  64. *out++ = *begin; // No expansion, copy character
  65. }
  66. }
  67. ++begin; // Step to next character
  68. }
  69. return out;
  70. }
  71. // Fill given output iterator with repetitions of the same character
  72. template<class OutIt, class Ch>
  73. inline OutIt fill_chars(OutIt out, int n, Ch ch)
  74. {
  75. for (int i = 0; i < n; ++i)
  76. *out++ = ch;
  77. return out;
  78. }
  79. // Find character
  80. template<class Ch, Ch ch>
  81. inline bool find_char(const Ch *begin, const Ch *end)
  82. {
  83. while (begin != end)
  84. if (*begin++ == ch)
  85. return true;
  86. return false;
  87. }
  88. ///////////////////////////////////////////////////////////////////////////
  89. // Internal printing operations
  90. template<class OutIt, class Ch>
  91. OutIt print_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
  92. // Print children of the node
  93. template<class OutIt, class Ch>
  94. inline OutIt print_children(OutIt out, const xml_node<Ch> *node, int flags, int indent)
  95. {
  96. for (xml_node<Ch> *child = node->first_node(); child; child = child->next_sibling())
  97. out = print_node(out, child, flags, indent);
  98. return out;
  99. }
  100. // Print attributes of the node
  101. template<class OutIt, class Ch>
  102. inline OutIt print_attributes(OutIt out, const xml_node<Ch> *node, int flags)
  103. {
  104. for (xml_attribute<Ch> *attribute = node->first_attribute(); attribute; attribute = attribute->next_attribute())
  105. {
  106. if (attribute->name() && attribute->value())
  107. {
  108. // Print attribute name
  109. *out = Ch(' '), ++out;
  110. out = copy_chars(attribute->name(), attribute->name() + attribute->name_size(), out);
  111. *out = Ch('='), ++out;
  112. // Print attribute value using appropriate quote type
  113. if (find_char<Ch, Ch('"')>(attribute->value(), attribute->value() + attribute->value_size()))
  114. {
  115. *out = Ch('\''), ++out;
  116. out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch('"'), out);
  117. *out = Ch('\''), ++out;
  118. }
  119. else
  120. {
  121. *out = Ch('"'), ++out;
  122. out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch('\''), out);
  123. *out = Ch('"'), ++out;
  124. }
  125. }
  126. }
  127. return out;
  128. }
  129. // Print data node
  130. template<class OutIt, class Ch>
  131. inline OutIt print_data_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
  132. {
  133. assert(node->type() == node_data);
  134. if (!(flags & print_no_indenting))
  135. out = fill_chars(out, indent, Ch('\t'));
  136. out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);
  137. return out;
  138. }
  139. // Print data node
  140. template<class OutIt, class Ch>
  141. inline OutIt print_cdata_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
  142. {
  143. assert(node->type() == node_cdata);
  144. if (!(flags & print_no_indenting))
  145. out = fill_chars(out, indent, Ch('\t'));
  146. *out = Ch('<'); ++out;
  147. *out = Ch('!'); ++out;
  148. *out = Ch('['); ++out;
  149. *out = Ch('C'); ++out;
  150. *out = Ch('D'); ++out;
  151. *out = Ch('A'); ++out;
  152. *out = Ch('T'); ++out;
  153. *out = Ch('A'); ++out;
  154. *out = Ch('['); ++out;
  155. out = copy_chars(node->value(), node->value() + node->value_size(), out);
  156. *out = Ch(']'); ++out;
  157. *out = Ch(']'); ++out;
  158. *out = Ch('>'); ++out;
  159. return out;
  160. }
  161. // Print element node
  162. template<class OutIt, class Ch>
  163. inline OutIt print_element_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
  164. {
  165. assert(node->type() == node_element);
  166. // Print element name and attributes, if any
  167. if (!(flags & print_no_indenting))
  168. out = fill_chars(out, indent, Ch('\t'));
  169. *out = Ch('<'), ++out;
  170. out = copy_chars(node->name(), node->name() + node->name_size(), out);
  171. out = print_attributes(out, node, flags);
  172. // If node is childless
  173. if (node->value_size() == 0 && !node->first_node())
  174. {
  175. // Print childless node tag ending
  176. *out = Ch('/'), ++out;
  177. *out = Ch('>'), ++out;
  178. }
  179. else
  180. {
  181. // Print normal node tag ending
  182. *out = Ch('>'), ++out;
  183. // Test if node contains a single data node only (and no other nodes)
  184. xml_node<Ch> *child = node->first_node();
  185. if (!child)
  186. {
  187. // If node has no children, only print its value without indenting
  188. out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);
  189. }
  190. else if (child->next_sibling() == 0 && child->type() == node_data)
  191. {
  192. // If node has a sole data child, only print its value without indenting
  193. out = copy_and_expand_chars(child->value(), child->value() + child->value_size(), Ch(0), out);
  194. }
  195. else
  196. {
  197. // Print all children with full indenting
  198. if (!(flags & print_no_indenting))
  199. *out = Ch('\n'), ++out;
  200. out = print_children(out, node, flags, indent + 1);
  201. if (!(flags & print_no_indenting))
  202. out = fill_chars(out, indent, Ch('\t'));
  203. }
  204. // Print node end
  205. *out = Ch('<'), ++out;
  206. *out = Ch('/'), ++out;
  207. out = copy_chars(node->name(), node->name() + node->name_size(), out);
  208. *out = Ch('>'), ++out;
  209. }
  210. return out;
  211. }
  212. // Print declaration node
  213. template<class OutIt, class Ch>
  214. inline OutIt print_declaration_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
  215. {
  216. // Print declaration start
  217. if (!(flags & print_no_indenting))
  218. out = fill_chars(out, indent, Ch('\t'));
  219. *out = Ch('<'), ++out;
  220. *out = Ch('?'), ++out;
  221. *out = Ch('x'), ++out;
  222. *out = Ch('m'), ++out;
  223. *out = Ch('l'), ++out;
  224. // Print attributes
  225. out = print_attributes(out, node, flags);
  226. // Print declaration end
  227. *out = Ch('?'), ++out;
  228. *out = Ch('>'), ++out;
  229. return out;
  230. }
  231. // Print comment node
  232. template<class OutIt, class Ch>
  233. inline OutIt print_comment_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
  234. {
  235. assert(node->type() == node_comment);
  236. if (!(flags & print_no_indenting))
  237. out = fill_chars(out, indent, Ch('\t'));
  238. *out = Ch('<'), ++out;
  239. *out = Ch('!'), ++out;
  240. *out = Ch('-'), ++out;
  241. *out = Ch('-'), ++out;
  242. out = copy_chars(node->value(), node->value() + node->value_size(), out);
  243. *out = Ch('-'), ++out;
  244. *out = Ch('-'), ++out;
  245. *out = Ch('>'), ++out;
  246. return out;
  247. }
  248. // Print doctype node
  249. template<class OutIt, class Ch>
  250. inline OutIt print_doctype_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
  251. {
  252. assert(node->type() == node_doctype);
  253. if (!(flags & print_no_indenting))
  254. out = fill_chars(out, indent, Ch('\t'));
  255. *out = Ch('<'), ++out;
  256. *out = Ch('!'), ++out;
  257. *out = Ch('D'), ++out;
  258. *out = Ch('O'), ++out;
  259. *out = Ch('C'), ++out;
  260. *out = Ch('T'), ++out;
  261. *out = Ch('Y'), ++out;
  262. *out = Ch('P'), ++out;
  263. *out = Ch('E'), ++out;
  264. *out = Ch(' '), ++out;
  265. out = copy_chars(node->value(), node->value() + node->value_size(), out);
  266. *out = Ch('>'), ++out;
  267. return out;
  268. }
  269. // Print pi node
  270. template<class OutIt, class Ch>
  271. inline OutIt print_pi_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
  272. {
  273. assert(node->type() == node_pi);
  274. if (!(flags & print_no_indenting))
  275. out = fill_chars(out, indent, Ch('\t'));
  276. *out = Ch('<'), ++out;
  277. *out = Ch('?'), ++out;
  278. out = copy_chars(node->name(), node->name() + node->name_size(), out);
  279. *out = Ch(' '), ++out;
  280. out = copy_chars(node->value(), node->value() + node->value_size(), out);
  281. *out = Ch('?'), ++out;
  282. *out = Ch('>'), ++out;
  283. return out;
  284. }
  285. // Print node
  286. template<class OutIt, class Ch>
  287. inline OutIt print_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
  288. {
  289. // Print proper node type
  290. switch (node->type())
  291. {
  292. // Document
  293. case node_document:
  294. out = print_children(out, node, flags, indent);
  295. break;
  296. // Element
  297. case node_element:
  298. out = print_element_node(out, node, flags, indent);
  299. break;
  300. // Data
  301. case node_data:
  302. out = print_data_node(out, node, flags, indent);
  303. break;
  304. // CDATA
  305. case node_cdata:
  306. out = print_cdata_node(out, node, flags, indent);
  307. break;
  308. // Declaration
  309. case node_declaration:
  310. out = print_declaration_node(out, node, flags, indent);
  311. break;
  312. // Comment
  313. case node_comment:
  314. out = print_comment_node(out, node, flags, indent);
  315. break;
  316. // Doctype
  317. case node_doctype:
  318. out = print_doctype_node(out, node, flags, indent);
  319. break;
  320. // Pi
  321. case node_pi:
  322. out = print_pi_node(out, node, flags, indent);
  323. break;
  324. // Unknown
  325. default:
  326. assert(0);
  327. break;
  328. }
  329. // If indenting not disabled, add line break after node
  330. if (!(flags & print_no_indenting))
  331. *out = Ch('\n'), ++out;
  332. // Return modified iterator
  333. return out;
  334. }
  335. }
  336. //! \endcond
  337. ///////////////////////////////////////////////////////////////////////////
  338. // Printing
  339. //! Prints XML to given output iterator.
  340. //! \param out Output iterator to print to.
  341. //! \param node Node to be printed. Pass xml_document to print entire document.
  342. //! \param flags Flags controlling how XML is printed.
  343. //! \return Output iterator pointing to position immediately after last character of printed text.
  344. template<class OutIt, class Ch>
  345. inline OutIt print(OutIt out, const xml_node<Ch> &node, int flags = 0)
  346. {
  347. return internal::print_node(out, &node, flags, 0);
  348. }
  349. #ifndef RAPIDXML_NO_STREAMS
  350. //! Prints XML to given output stream.
  351. //! \param out Output stream to print to.
  352. //! \param node Node to be printed. Pass xml_document to print entire document.
  353. //! \param flags Flags controlling how XML is printed.
  354. //! \return Output stream.
  355. template<class Ch>
  356. inline std::basic_ostream<Ch> &print(std::basic_ostream<Ch> &out, const xml_node<Ch> &node, int flags = 0)
  357. {
  358. print(std::ostream_iterator<Ch>(out), node, flags);
  359. return out;
  360. }
  361. //! Prints formatted XML to given output stream. Uses default printing flags. Use print() function to customize printing process.
  362. //! \param out Output stream to print to.
  363. //! \param node Node to be printed.
  364. //! \return Output stream.
  365. template<class Ch>
  366. inline std::basic_ostream<Ch> &operator <<(std::basic_ostream<Ch> &out, const xml_node<Ch> &node)
  367. {
  368. return print(out, node);
  369. }
  370. #endif
  371. }
  372. #endif