XMLElement.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Exception.h"
  25. #include "StringUtils.h"
  26. #include "XMLElement.h"
  27. #include <tinyxml.h>
  28. XMLElement::XMLElement() :
  29. mElement(0)
  30. {
  31. }
  32. XMLElement::XMLElement(TiXmlElement* element) :
  33. mElement(element)
  34. {
  35. }
  36. XMLElement::XMLElement(const XMLElement& rhs) :
  37. mElement(rhs.mElement)
  38. {
  39. }
  40. XMLElement::~XMLElement()
  41. {
  42. }
  43. XMLElement XMLElement::createChildElement(const std::string& name)
  44. {
  45. if (!mElement)
  46. SAFE_EXCEPTION_RET("Null XML element, can not create child element " + name, XMLElement());
  47. TiXmlElement newElement(name.c_str());
  48. mElement->InsertEndChild(newElement);
  49. return XMLElement(static_cast<TiXmlElement*>(mElement->LastChild()));
  50. }
  51. void XMLElement::removeChildElement(const std::string& name, bool last)
  52. {
  53. if (!mElement)
  54. return;
  55. TiXmlNode* element;
  56. if (name.empty())
  57. {
  58. if (last)
  59. element = mElement->LastChild();
  60. else
  61. element = mElement->FirstChild();
  62. }
  63. else
  64. {
  65. if (last)
  66. element = mElement->LastChild(name.c_str());
  67. else
  68. element = mElement->FirstChild(name.c_str());
  69. }
  70. mElement->RemoveChild(element);
  71. }
  72. void XMLElement::setAttribute(const std::string& name, const std::string& value)
  73. {
  74. if (!mElement)
  75. SAFE_EXCEPTION("Null XML element, can not set attribute " + name);
  76. mElement->SetAttribute(name.c_str(), value.c_str());
  77. }
  78. void XMLElement::setBool(const std::string& name, bool value)
  79. {
  80. setAttribute(name, toString(value));
  81. }
  82. void XMLElement::setBoundingBox(const BoundingBox& value)
  83. {
  84. setVector3("min", value.mMin);
  85. setVector3("max", value.mMax);
  86. }
  87. void XMLElement::setBuffer(const std::string& name, const void* data, unsigned size)
  88. {
  89. std::string dataStr;
  90. const unsigned char* bytes = (const unsigned char*)data;
  91. for (unsigned i = 0; i < size; ++i)
  92. dataStr += toString(bytes[i]) + " ";
  93. setAttribute(name, dataStr);
  94. }
  95. void XMLElement::setBuffer(const std::string& name, const std::vector<unsigned char>& value)
  96. {
  97. if (!value.size())
  98. setAttribute(name, std::string());
  99. else
  100. setBuffer(name, &value[0], value.size());
  101. }
  102. void XMLElement::setColor(const std::string& name, const Color& value)
  103. {
  104. setAttribute(name, toString(value));
  105. }
  106. void XMLElement::setFloat(const std::string& name, float value)
  107. {
  108. setAttribute(name, toString(value));
  109. }
  110. void XMLElement::setInt(const std::string& name, int value)
  111. {
  112. setAttribute(name, toString(value));
  113. }
  114. void XMLElement::setIntRect(const std::string& name, const IntRect& value)
  115. {
  116. setAttribute(name, toString(value));
  117. }
  118. void XMLElement::setIntVector2(const std::string& name, const IntVector2& value)
  119. {
  120. setAttribute(name, toString(value));
  121. }
  122. void XMLElement::setRect(const std::string& name, const Rect& value)
  123. {
  124. setAttribute(name, toString(value));
  125. }
  126. void XMLElement::setQuaternion(const std::string& name, const Quaternion& value)
  127. {
  128. XMLElement::setAttribute(name, toString(value));
  129. }
  130. void XMLElement::setString(const std::string& name, const std::string& value)
  131. {
  132. setAttribute(name, value);
  133. }
  134. void XMLElement::setVariant(const Variant& value)
  135. {
  136. setAttribute("type", value.getTypeName());
  137. setAttribute("value", value.toString());
  138. }
  139. void XMLElement::setVariantMap(const VariantMap& value)
  140. {
  141. for (VariantMap::const_iterator i = value.begin(); i != value.end(); ++i)
  142. {
  143. XMLElement variantElem = createChildElement("variant");
  144. std::string name = shortHashToString(i->first);
  145. if (name.empty())
  146. variantElem.setInt("hash", i->first.mData);
  147. else
  148. variantElem.setString("name", name);
  149. variantElem.setVariant(i->second);
  150. }
  151. }
  152. void XMLElement::setVector2(const std::string& name, const Vector2& value)
  153. {
  154. setAttribute(name, toString(value));
  155. }
  156. void XMLElement::setVector3(const std::string& name, const Vector3& value)
  157. {
  158. setAttribute(name, toString(value));
  159. }
  160. void XMLElement::setVector4(const std::string& name, const Vector4& value)
  161. {
  162. setAttribute(name, toString(value));
  163. }
  164. std::string XMLElement::getName() const
  165. {
  166. if (!mElement)
  167. return std::string();
  168. return std::string(mElement->Value());
  169. }
  170. std::string XMLElement::getText() const
  171. {
  172. if (!mElement)
  173. return std::string();
  174. TiXmlNode* node = mElement->FirstChild();
  175. if (node)
  176. return std::string(node->Value());
  177. else
  178. return std::string();
  179. }
  180. bool XMLElement::hasChildElement(const std::string& name) const
  181. {
  182. if (!mElement)
  183. return false;
  184. if (mElement->FirstChildElement(name.c_str()) != 0)
  185. return true;
  186. else
  187. return false;
  188. }
  189. XMLElement XMLElement::getChildElement(const std::string& name, bool throwIfMissing) const
  190. {
  191. if (throwIfMissing)
  192. {
  193. if (!mElement)
  194. SAFE_EXCEPTION_RET("Null XML element, can not get child element " + name, XMLElement());
  195. TiXmlElement* child;
  196. if (name.empty())
  197. child = mElement->FirstChildElement();
  198. else
  199. child = mElement->FirstChildElement(name.c_str());
  200. if (!child)
  201. SAFE_EXCEPTION_RET("XML element " + std::string(mElement->Value()) + " has no child element " + name, XMLElement());
  202. return XMLElement(child);
  203. }
  204. else
  205. {
  206. if (!mElement)
  207. return XMLElement();
  208. else
  209. {
  210. if (name.empty())
  211. return XMLElement(mElement->FirstChildElement());
  212. else
  213. return XMLElement(mElement->FirstChildElement(name.c_str()));
  214. }
  215. }
  216. }
  217. XMLElement XMLElement::getNextElement(const std::string& name) const
  218. {
  219. if (!mElement)
  220. return XMLElement();
  221. if (name.empty())
  222. return XMLElement(mElement->NextSiblingElement());
  223. else
  224. return XMLElement(mElement->NextSiblingElement(name.c_str()));
  225. }
  226. bool XMLElement::hasAttribute(const std::string& name) const
  227. {
  228. if (!mElement)
  229. return false;
  230. if (mElement->Attribute(name.c_str()) != 0)
  231. return true;
  232. else
  233. return false;
  234. }
  235. std::string XMLElement::getAttribute(const std::string& name, bool throwIfMissing) const
  236. {
  237. if (throwIfMissing)
  238. {
  239. if (!mElement)
  240. SAFE_EXCEPTION_RET("Null XML element, can not get attribute " + name, std::string());
  241. const char* data = mElement->Attribute(name.c_str());
  242. if (!data)
  243. SAFE_EXCEPTION_RET("XML element " + std::string(mElement->Value()) + " has no attribute " + name, std::string())
  244. else
  245. return std::string(data);
  246. }
  247. else
  248. {
  249. if (!mElement)
  250. return std::string();
  251. else
  252. {
  253. const char* data = mElement->Attribute(name.c_str());
  254. if (!data)
  255. return std::string();
  256. else
  257. return std::string(data);
  258. }
  259. }
  260. }
  261. bool XMLElement::getBool(const std::string& name, bool throwIfMissing) const
  262. {
  263. return toBool(getAttribute(name, throwIfMissing));
  264. }
  265. BoundingBox XMLElement::getBoundingBox(bool throwIfMissing) const
  266. {
  267. BoundingBox ret;
  268. ret.mMin = getVector3("min", throwIfMissing);
  269. ret.mMax = getVector3("max", throwIfMissing);
  270. ret.mDefined = true;
  271. return ret;
  272. }
  273. std::vector<unsigned char> XMLElement::getBuffer(const std::string& name, bool throwIfMissing) const
  274. {
  275. std::vector<unsigned char> ret;
  276. std::vector<std::string> bytes = split(getAttribute(name, throwIfMissing), ' ');
  277. ret.resize(bytes.size());
  278. for (unsigned i = 0; i < bytes.size(); ++i)
  279. ret[i] = toInt(bytes[i]);
  280. return ret;
  281. }
  282. void XMLElement::getBuffer(const std::string& name, void* dest, unsigned size, bool throwIfMissing) const
  283. {
  284. std::vector<unsigned char> ret;
  285. std::vector<std::string> bytes = split(getAttribute(name, throwIfMissing), ' ');
  286. unsigned char* destBytes = (unsigned char*)dest;
  287. if (size < bytes.size())
  288. SAFE_EXCEPTION("Destination buffer not big enough");
  289. for (unsigned i = 0; i < bytes.size(); ++i)
  290. destBytes[i] = toInt(bytes[i]);
  291. }
  292. Color XMLElement::getColor(const std::string& name, bool throwIfMissing) const
  293. {
  294. return toColor(getAttribute(name, throwIfMissing));
  295. }
  296. float XMLElement::getFloat(const std::string& name, bool throwIfMissing) const
  297. {
  298. return toFloat(getAttribute(name, throwIfMissing));
  299. }
  300. int XMLElement::getInt(const std::string& name, bool throwIfMissing) const
  301. {
  302. return toInt(getAttribute(name, throwIfMissing));
  303. }
  304. IntRect XMLElement::getIntRect(const std::string& name, bool throwIfMissing) const
  305. {
  306. return toIntRect(getAttribute(name, throwIfMissing));
  307. }
  308. IntVector2 XMLElement::getIntVector2(const std::string& name, bool throwIfMissing) const
  309. {
  310. return toIntVector2(getAttribute(name, throwIfMissing));
  311. }
  312. Quaternion XMLElement::getQuaternion(const std::string& name, bool throwIfMissing) const
  313. {
  314. return toQuaternion(getAttribute(name, throwIfMissing));
  315. }
  316. Rect XMLElement::getRect(const std::string& name, bool throwIfMissing) const
  317. {
  318. return toRect(getAttribute(name, throwIfMissing));
  319. }
  320. std::string XMLElement::getString(const std::string& name, bool throwIfMissing) const
  321. {
  322. return getAttribute(name, throwIfMissing);
  323. }
  324. std::string XMLElement::getStringLower(const std::string& name, bool throwIfMissing) const
  325. {
  326. return toLower(getAttribute(name, throwIfMissing));
  327. }
  328. std::string XMLElement::getStringUpper(const std::string& name, bool throwIfMissing) const
  329. {
  330. return toUpper(getAttribute(name, throwIfMissing));
  331. }
  332. Variant XMLElement::getVariant(bool throwIfMissing) const
  333. {
  334. Variant ret;
  335. std::string type = getAttribute("type", throwIfMissing);
  336. std::string value = getAttribute("value", throwIfMissing);
  337. ret.fromString(type, value);
  338. return ret;
  339. }
  340. VariantMap XMLElement::getVariantMap(bool throwIfMissing) const
  341. {
  342. VariantMap ret;
  343. XMLElement variantElem = getChildElement("variant", false);
  344. while (variantElem.notNull())
  345. {
  346. ShortStringHash key;
  347. if (variantElem.hasAttribute("hash"))
  348. key.mData = variantElem.getInt("hash");
  349. else
  350. key = ShortStringHash(variantElem.getString("name"));
  351. ret[key] =variantElem.getVariant(throwIfMissing);
  352. variantElem = variantElem.getNextElement("variant");
  353. }
  354. return ret;
  355. }
  356. Vector2 XMLElement::getVector2(const std::string& name, bool throwIfMissing) const
  357. {
  358. return toVector2(getAttribute(name, throwIfMissing));
  359. }
  360. Vector3 XMLElement::getVector3(const std::string& name, bool throwIfMissing) const
  361. {
  362. return toVector3(getAttribute(name, throwIfMissing));
  363. }
  364. Vector4 XMLElement::getVector4(const std::string& name, bool throwIfMissing) const
  365. {
  366. return toVector4(getAttribute(name, throwIfMissing));
  367. }
  368. Vector4 XMLElement::getVector(const std::string& name, bool throwIfMissing) const
  369. {
  370. return toVector4(getAttribute(name, throwIfMissing), true);
  371. }