rapidxml_print.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #ifndef CEREAL_RAPIDXML_PRINT_HPP_INCLUDED
  2. #define CEREAL_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. #include "rapidxml.hpp"
  7. // Only include streams if not disabled
  8. #ifndef CEREAL_RAPIDXML_NO_STREAMS
  9. #include <ostream>
  10. #include <iterator>
  11. #endif
  12. namespace cereal {
  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. // Print node
  91. template<class OutIt, class Ch>
  92. inline OutIt print_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
  93. // Print children of the node
  94. template<class OutIt, class Ch>
  95. inline OutIt print_children(OutIt out, const xml_node<Ch> *node, int flags, int indent)
  96. {
  97. for (xml_node<Ch> *child = node->first_node(); child; child = child->next_sibling())
  98. out = print_node(out, child, flags, indent);
  99. return out;
  100. }
  101. // Print attributes of the node
  102. template<class OutIt, class Ch>
  103. inline OutIt print_attributes(OutIt out, const xml_node<Ch> *node, int /*flags*/)
  104. {
  105. for (xml_attribute<Ch> *attribute = node->first_attribute(); attribute; attribute = attribute->next_attribute())
  106. {
  107. if (attribute->name() && attribute->value())
  108. {
  109. // Print attribute name
  110. *out = Ch(' '), ++out;
  111. out = copy_chars(attribute->name(), attribute->name() + attribute->name_size(), out);
  112. *out = Ch('='), ++out;
  113. // Print attribute value using appropriate quote type
  114. if (find_char<Ch, Ch('"')>(attribute->value(), attribute->value() + attribute->value_size()))
  115. {
  116. *out = Ch('\''), ++out;
  117. out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch('"'), out);
  118. *out = Ch('\''), ++out;
  119. }
  120. else
  121. {
  122. *out = Ch('"'), ++out;
  123. out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch('\''), out);
  124. *out = Ch('"'), ++out;
  125. }
  126. }
  127. }
  128. return out;
  129. }
  130. // Print data node
  131. template<class OutIt, class Ch>
  132. inline OutIt print_data_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
  133. {
  134. assert(node->type() == node_data);
  135. if (!(flags & print_no_indenting))
  136. out = fill_chars(out, indent, Ch('\t'));
  137. out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);
  138. return out;
  139. }
  140. // Print data node
  141. template<class OutIt, class Ch>
  142. inline OutIt print_cdata_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
  143. {
  144. assert(node->type() == node_cdata);
  145. if (!(flags & print_no_indenting))
  146. out = fill_chars(out, indent, Ch('\t'));
  147. *out = Ch('<'); ++out;
  148. *out = Ch('!'); ++out;
  149. *out = Ch('['); ++out;
  150. *out = Ch('C'); ++out;
  151. *out = Ch('D'); ++out;
  152. *out = Ch('A'); ++out;
  153. *out = Ch('T'); ++out;
  154. *out = Ch('A'); ++out;
  155. *out = Ch('['); ++out;
  156. out = copy_chars(node->value(), node->value() + node->value_size(), out);
  157. *out = Ch(']'); ++out;
  158. *out = Ch(']'); ++out;
  159. *out = Ch('>'); ++out;
  160. return out;
  161. }
  162. // Print element node
  163. template<class OutIt, class Ch>
  164. inline OutIt print_element_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
  165. {
  166. assert(node->type() == node_element);
  167. // Print element name and attributes, if any
  168. if (!(flags & print_no_indenting))
  169. out = fill_chars(out, indent, Ch('\t'));
  170. *out = Ch('<'), ++out;
  171. out = copy_chars(node->name(), node->name() + node->name_size(), out);
  172. out = print_attributes(out, node, flags);
  173. // If node is childless
  174. if (node->value_size() == 0 && !node->first_node())
  175. {
  176. // Print childless node tag ending
  177. *out = Ch('/'), ++out;
  178. *out = Ch('>'), ++out;
  179. }
  180. else
  181. {
  182. // Print normal node tag ending
  183. *out = Ch('>'), ++out;
  184. // Test if node contains a single data node only (and no other nodes)
  185. xml_node<Ch> *child = node->first_node();
  186. if (!child)
  187. {
  188. // If node has no children, only print its value without indenting
  189. out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);
  190. }
  191. else if (child->next_sibling() == 0 && child->type() == node_data)
  192. {
  193. // If node has a sole data child, only print its value without indenting
  194. out = copy_and_expand_chars(child->value(), child->value() + child->value_size(), Ch(0), out);
  195. }
  196. else
  197. {
  198. // Print all children with full indenting
  199. if (!(flags & print_no_indenting))
  200. *out = Ch('\n'), ++out;
  201. out = print_children(out, node, flags, indent + 1);
  202. if (!(flags & print_no_indenting))
  203. out = fill_chars(out, indent, Ch('\t'));
  204. }
  205. // Print node end
  206. *out = Ch('<'), ++out;
  207. *out = Ch('/'), ++out;
  208. out = copy_chars(node->name(), node->name() + node->name_size(), out);
  209. *out = Ch('>'), ++out;
  210. }
  211. return out;
  212. }
  213. // Print declaration node
  214. template<class OutIt, class Ch>
  215. inline OutIt print_declaration_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
  216. {
  217. // Print declaration start
  218. if (!(flags & print_no_indenting))
  219. out = fill_chars(out, indent, Ch('\t'));
  220. *out = Ch('<'), ++out;
  221. *out = Ch('?'), ++out;
  222. *out = Ch('x'), ++out;
  223. *out = Ch('m'), ++out;
  224. *out = Ch('l'), ++out;
  225. // Print attributes
  226. out = print_attributes(out, node, flags);
  227. // Print declaration end
  228. *out = Ch('?'), ++out;
  229. *out = Ch('>'), ++out;
  230. return out;
  231. }
  232. // Print comment node
  233. template<class OutIt, class Ch>
  234. inline OutIt print_comment_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
  235. {
  236. assert(node->type() == node_comment);
  237. if (!(flags & print_no_indenting))
  238. out = fill_chars(out, indent, Ch('\t'));
  239. *out = Ch('<'), ++out;
  240. *out = Ch('!'), ++out;
  241. *out = Ch('-'), ++out;
  242. *out = Ch('-'), ++out;
  243. out = copy_chars(node->value(), node->value() + node->value_size(), out);
  244. *out = Ch('-'), ++out;
  245. *out = Ch('-'), ++out;
  246. *out = Ch('>'), ++out;
  247. return out;
  248. }
  249. // Print doctype node
  250. template<class OutIt, class Ch>
  251. inline OutIt print_doctype_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
  252. {
  253. assert(node->type() == node_doctype);
  254. if (!(flags & print_no_indenting))
  255. out = fill_chars(out, indent, Ch('\t'));
  256. *out = Ch('<'), ++out;
  257. *out = Ch('!'), ++out;
  258. *out = Ch('D'), ++out;
  259. *out = Ch('O'), ++out;
  260. *out = Ch('C'), ++out;
  261. *out = Ch('T'), ++out;
  262. *out = Ch('Y'), ++out;
  263. *out = Ch('P'), ++out;
  264. *out = Ch('E'), ++out;
  265. *out = Ch(' '), ++out;
  266. out = copy_chars(node->value(), node->value() + node->value_size(), out);
  267. *out = Ch('>'), ++out;
  268. return out;
  269. }
  270. // Print pi node
  271. template<class OutIt, class Ch>
  272. inline OutIt print_pi_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
  273. {
  274. assert(node->type() == node_pi);
  275. if (!(flags & print_no_indenting))
  276. out = fill_chars(out, indent, Ch('\t'));
  277. *out = Ch('<'), ++out;
  278. *out = Ch('?'), ++out;
  279. out = copy_chars(node->name(), node->name() + node->name_size(), out);
  280. *out = Ch(' '), ++out;
  281. out = copy_chars(node->value(), node->value() + node->value_size(), out);
  282. *out = Ch('?'), ++out;
  283. *out = Ch('>'), ++out;
  284. return out;
  285. }
  286. // Print node
  287. template<class OutIt, class Ch>
  288. inline OutIt print_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
  289. {
  290. // Print proper node type
  291. switch (node->type())
  292. {
  293. // Document
  294. case node_document:
  295. out = print_children(out, node, flags, indent);
  296. break;
  297. // Element
  298. case node_element:
  299. out = print_element_node(out, node, flags, indent);
  300. break;
  301. // Data
  302. case node_data:
  303. out = print_data_node(out, node, flags, indent);
  304. break;
  305. // CDATA
  306. case node_cdata:
  307. out = print_cdata_node(out, node, flags, indent);
  308. break;
  309. // Declaration
  310. case node_declaration:
  311. out = print_declaration_node(out, node, flags, indent);
  312. break;
  313. // Comment
  314. case node_comment:
  315. out = print_comment_node(out, node, flags, indent);
  316. break;
  317. // Doctype
  318. case node_doctype:
  319. out = print_doctype_node(out, node, flags, indent);
  320. break;
  321. // Pi
  322. case node_pi:
  323. out = print_pi_node(out, node, flags, indent);
  324. break;
  325. #ifndef __GNUC__
  326. // Unknown
  327. default:
  328. assert(0);
  329. break;
  330. #endif
  331. }
  332. // If indenting not disabled, add line break after node
  333. if (!(flags & print_no_indenting))
  334. *out = Ch('\n'), ++out;
  335. // Return modified iterator
  336. return out;
  337. }
  338. }
  339. //! \endcond
  340. ///////////////////////////////////////////////////////////////////////////
  341. // Printing
  342. //! Prints XML to given output iterator.
  343. //! \param out Output iterator to print to.
  344. //! \param node Node to be printed. Pass xml_document to print entire document.
  345. //! \param flags Flags controlling how XML is printed.
  346. //! \return Output iterator pointing to position immediately after last character of printed text.
  347. template<class OutIt, class Ch>
  348. inline OutIt print(OutIt out, const xml_node<Ch> &node, int flags = 0)
  349. {
  350. return internal::print_node(out, &node, flags, 0);
  351. }
  352. #ifndef CEREAL_RAPIDXML_NO_STREAMS
  353. //! Prints XML to given output stream.
  354. //! \param out Output stream to print to.
  355. //! \param node Node to be printed. Pass xml_document to print entire document.
  356. //! \param flags Flags controlling how XML is printed.
  357. //! \return Output stream.
  358. template<class Ch>
  359. inline std::basic_ostream<Ch> &print(std::basic_ostream<Ch> &out, const xml_node<Ch> &node, int flags = 0)
  360. {
  361. print(std::ostream_iterator<Ch>(out), node, flags);
  362. return out;
  363. }
  364. //! Prints formatted XML to given output stream. Uses default printing flags. Use print() function to customize printing process.
  365. //! \param out Output stream to print to.
  366. //! \param node Node to be printed.
  367. //! \return Output stream.
  368. template<class Ch>
  369. inline std::basic_ostream<Ch> &operator <<(std::basic_ostream<Ch> &out, const xml_node<Ch> &node)
  370. {
  371. return print(out, node);
  372. }
  373. #endif
  374. }
  375. } // namespace cereal
  376. #endif