serializer 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // file : xml/serializer -*- C++ -*-
  2. // copyright : Copyright (c) 2013-2014 Code Synthesis Tools CC
  3. // license : MIT; see accompanying LICENSE file
  4. #ifndef XML_SERIALIZER
  5. #define XML_SERIALIZER
  6. #include <xml/details/pre.hxx>
  7. #include <string>
  8. #include <ostream>
  9. #include <cstddef> // std::size_t
  10. #include <xml/details/genx/genx.h>
  11. #include <xml/forward>
  12. #include <xml/qname>
  13. #include <xml/exception>
  14. #include <xml/details/export.hxx>
  15. namespace xml
  16. {
  17. class serializer;
  18. struct LIBSTUDXML_EXPORT serialization: exception
  19. {
  20. virtual
  21. ~serialization () throw ();
  22. serialization (const std::string& name,
  23. const std::string& description);
  24. serialization (const serializer&, const std::string& description);
  25. const std::string&
  26. name () const {return name_;}
  27. const std::string&
  28. description () const {return description_;}
  29. virtual const char*
  30. what () const throw ();
  31. private:
  32. void
  33. init ();
  34. private:
  35. std::string name_;
  36. std::string description_;
  37. std::string what_;
  38. };
  39. class LIBSTUDXML_EXPORT serializer
  40. {
  41. public:
  42. typedef xml::qname qname_type;
  43. // Serialize to std::ostream. Output name is used in diagnostics to
  44. // identify the document being serialized. The indentation argument
  45. // specifies the number of indentation spaces that should be used for
  46. // pretty-printing. If 0 is passed, no pretty-printing is performed.
  47. //
  48. // If stream exceptions are enabled then std::ios_base::failure
  49. // exception is used to report io errors (badbit and failbit).
  50. // Otherwise, those are reported as the serialization exception.
  51. //
  52. serializer (std::ostream&,
  53. const std::string& output_name,
  54. unsigned short indentation = 2);
  55. const std::string&
  56. output_name () const {return oname_;}
  57. ~serializer ();
  58. private:
  59. serializer (const serializer&);
  60. serializer& operator= (const serializer&);
  61. // Serialization functions.
  62. //
  63. public:
  64. // Elements.
  65. //
  66. void
  67. start_element (const qname_type& qname);
  68. void
  69. start_element (const std::string& name);
  70. void
  71. start_element (const std::string& ns, const std::string& name);
  72. void
  73. end_element ();
  74. // Helpers for serializing elements with simple content. The first two
  75. // functions assume that start_element() has already been called. The
  76. // other two serialize the complete element, from start to end.
  77. //
  78. void
  79. element (const std::string& value);
  80. template <typename T>
  81. void
  82. element (const T& value);
  83. void
  84. element (const std::string& name, const std::string& value);
  85. template <typename T>
  86. void
  87. element (const std::string& name, const T& value);
  88. void
  89. element (const qname_type& qname, const std::string& value);
  90. template <typename T>
  91. void
  92. element (const qname_type& qname, const T& value);
  93. void
  94. element (const std::string& namespace_,
  95. const std::string& name,
  96. const std::string& value);
  97. template <typename T>
  98. void
  99. element (const std::string& namespace_,
  100. const std::string& name,
  101. const T& value);
  102. // Attributes.
  103. //
  104. void
  105. start_attribute (const qname_type& qname);
  106. void
  107. start_attribute (const std::string& name);
  108. void
  109. start_attribute (const std::string& ns, const std::string& name);
  110. void
  111. end_attribute ();
  112. void
  113. attribute (const qname_type& qname, const std::string& value);
  114. template <typename T>
  115. void
  116. attribute (const qname_type& qname, const T& value);
  117. void
  118. attribute (const std::string& name, const std::string& value);
  119. template <typename T>
  120. void
  121. attribute (const std::string& name, const T& value);
  122. void
  123. attribute (const std::string& ns,
  124. const std::string& name,
  125. const std::string& value);
  126. template <typename T>
  127. void
  128. attribute (const std::string& ns,
  129. const std::string& name,
  130. const T& value);
  131. // Characters.
  132. //
  133. void
  134. characters (const std::string& value);
  135. template <typename T>
  136. void
  137. characters (const T& value);
  138. // Namespaces declaration. If prefix is empty, then the default
  139. // namespace is declared. If both prefix and namespace are empty,
  140. // then the default namespace declaration is cleared (xmlns="").
  141. //
  142. void
  143. namespace_decl (const std::string& ns, const std::string& prefix);
  144. // XML declaration. If encoding or standalone are not specified,
  145. // then these attributes are omitted from the output.
  146. //
  147. void
  148. xml_decl (const std::string& version = "1.0",
  149. const std::string& encoding = "UTF-8",
  150. const std::string& standalone = "");
  151. // Utility functions.
  152. //
  153. public:
  154. // Return true if there is a mapping. In this case, prefix contains
  155. // the mapped prefix.
  156. //
  157. bool
  158. lookup_namespace_prefix (const std::string& ns, std::string& prefix);
  159. private:
  160. void
  161. handle_error (genxStatus);
  162. private:
  163. std::ostream& os_;
  164. std::ostream::iostate os_state_; // Original exception state.
  165. const std::string oname_;
  166. genxWriter s_;
  167. genxSender sender_;
  168. std::size_t depth_;
  169. };
  170. }
  171. #include <xml/serializer.ixx>
  172. #include <xml/details/post.hxx>
  173. #endif // XML_SERIALIZER