XmlData.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* ----------------------------------------------------------------- */
  2. /* The HMM-Based Singing Voice Synthesis System "Sinsy" */
  3. /* developed by Sinsy Working Group */
  4. /* http://sinsy.sourceforge.net/ */
  5. /* ----------------------------------------------------------------- */
  6. /* */
  7. /* Copyright (c) 2009-2015 Nagoya Institute of Technology */
  8. /* Department of Computer Science */
  9. /* */
  10. /* All rights reserved. */
  11. /* */
  12. /* Redistribution and use in source and binary forms, with or */
  13. /* without modification, are permitted provided that the following */
  14. /* conditions are met: */
  15. /* */
  16. /* - Redistributions of source code must retain the above copyright */
  17. /* notice, this list of conditions and the following disclaimer. */
  18. /* - Redistributions in binary form must reproduce the above */
  19. /* copyright notice, this list of conditions and the following */
  20. /* disclaimer in the documentation and/or other materials provided */
  21. /* with the distribution. */
  22. /* - Neither the name of the Sinsy working group nor the names of */
  23. /* its contributors may be used to endorse or promote products */
  24. /* derived from this software without specific prior written */
  25. /* permission. */
  26. /* */
  27. /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND */
  28. /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  29. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  30. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  31. /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS */
  32. /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, */
  33. /* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
  34. /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
  35. /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
  36. /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, */
  37. /* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY */
  38. /* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  39. /* POSSIBILITY OF SUCH DAMAGE. */
  40. /* ----------------------------------------------------------------- */
  41. #include <algorithm>
  42. #include "XmlData.h"
  43. #include "Deleter.h"
  44. #include "util_log.h"
  45. #include "util_string.h"
  46. namespace sinsy
  47. {
  48. namespace
  49. {
  50. class StringOutputter
  51. {
  52. public:
  53. //! constructor
  54. StringOutputter(std::ostream& s) : stream(s) {}
  55. //! copy constructor
  56. StringOutputter(const StringOutputter& obj) : stream(obj.stream) {}
  57. //! destructor
  58. virtual ~StringOutputter() {}
  59. //! ...
  60. void operator()(const IStringWritable* obj) {
  61. stream << *obj;
  62. }
  63. private:
  64. //! assignment operator (donot use)
  65. StringOutputter& operator=(const StringOutputter&);
  66. //! stream
  67. std::ostream& stream;
  68. };
  69. class AttributeOutputter
  70. {
  71. public:
  72. //! constructor
  73. AttributeOutputter(std::ostream& s) : stream(s) {}
  74. //! copy constructor
  75. AttributeOutputter(const AttributeOutputter& obj) : stream(obj.stream) {}
  76. //! destructor
  77. virtual ~AttributeOutputter() {}
  78. //! ...
  79. void operator()(const std::pair<std::string, std::string>& obj) {
  80. stream << " " << obj.first << "=\"" << obj.second << "\"";
  81. }
  82. private:
  83. //! assignment operator (donot use)
  84. AttributeOutputter& operator=(const AttributeOutputter&);
  85. //! stream
  86. std::ostream& stream;
  87. };
  88. }; // namespace
  89. /*!
  90. constructor
  91. @param t tag
  92. @param d data
  93. */
  94. XmlData::XmlData(const std::string& t, const std::string& d) : tag(t), data(d)
  95. {
  96. toLower(tag);
  97. }
  98. /*!
  99. copy constructor
  100. */
  101. XmlData::XmlData(const XmlData& obj) : tag(obj.tag), data(obj.data), attributes(obj.attributes)
  102. {
  103. try {
  104. // copy children
  105. children.reserve(obj.children.size());
  106. const Children::const_iterator itrEnd(obj.childEnd());
  107. for (Children::const_iterator itr(obj.childBegin()); itrEnd != itr; ++itr) {
  108. this->addChild(new XmlData(**itr));
  109. }
  110. } catch (const std::exception&) {
  111. // delete children
  112. std::for_each(children.begin(), children.end(), Deleter<XmlData>());
  113. throw;
  114. }
  115. }
  116. /*!
  117. destructor
  118. */
  119. XmlData::~XmlData()
  120. {
  121. std::for_each(children.begin(), children.end(), Deleter<XmlData>());
  122. }
  123. /*!
  124. get tag
  125. */
  126. const std::string& XmlData::getTag() const
  127. {
  128. return tag;
  129. }
  130. /*!
  131. get data
  132. */
  133. const std::string& XmlData::getData() const
  134. {
  135. return data;
  136. }
  137. /*!
  138. set data
  139. */
  140. void XmlData::setData(const std::string& str)
  141. {
  142. data = str;
  143. }
  144. /*!
  145. add child
  146. */
  147. XmlData::Children::iterator XmlData::addChild(XmlData* child)
  148. {
  149. if (NULL == child) {
  150. std::runtime_error("XmlData::addChild() NULL pointer");
  151. }
  152. children.push_back(child);
  153. return children.end() - 1;
  154. }
  155. //! erase child
  156. XmlData::Children::iterator XmlData::eraseChild(const Children::iterator& itr)
  157. {
  158. delete *itr;
  159. return children.erase(itr);
  160. }
  161. /*!
  162. add attribute
  163. */
  164. bool XmlData::addAttribute(const std::string& k, const std::string& v)
  165. {
  166. std::string key(k);
  167. std::string value(v);
  168. toLower(key);
  169. toLower(value);
  170. if (!attributes.insert(std::make_pair(key, value)).second) {
  171. ERR_MSG("Failed to insert attribute (" << key << ", " << value << ")");
  172. return false;
  173. }
  174. return true;
  175. }
  176. /*!
  177. get attribute
  178. */
  179. const std::string& XmlData::getAttribute(const std::string& k) const
  180. {
  181. Attributes::const_iterator itr = attributes.find(k);
  182. if (attributes.end() == itr) {
  183. return NULL_STR;
  184. }
  185. return itr->second;
  186. }
  187. /*!
  188. to string stream
  189. */
  190. std::ostream& XmlData::toStringStream(std::ostream& stream) const
  191. {
  192. stream << "<" << tag;
  193. std::for_each(attributes.begin(), attributes.end(), AttributeOutputter(stream));
  194. if (data.empty() && children.empty()) {
  195. stream << " />\n";
  196. return stream;
  197. }
  198. stream << ">" << data;
  199. if (!children.empty()) {
  200. stream << "\n";
  201. std::for_each(children.begin(), children.end(), StringOutputter(stream));
  202. }
  203. stream << "</" << tag << ">\n";
  204. return stream;
  205. }
  206. /*!
  207. get begin const iterator of children
  208. */
  209. XmlData::Children::const_iterator XmlData::childBegin() const
  210. {
  211. return children.begin();
  212. }
  213. /*!
  214. get end const iterator of children
  215. */
  216. XmlData::Children::const_iterator XmlData::childEnd() const
  217. {
  218. return children.end();
  219. }
  220. /*!
  221. get begin iterator of children
  222. */
  223. XmlData::Children::iterator XmlData::childBegin()
  224. {
  225. return children.begin();
  226. }
  227. /*!
  228. get end iterator of children
  229. */
  230. XmlData::Children::iterator XmlData::childEnd()
  231. {
  232. return children.end();
  233. }
  234. }; // namespace sinsy