XMLElement.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 "Log.h"
  26. #include "StringUtils.h"
  27. #include "XMLElement.h"
  28. #include <tinyxml.h>
  29. XMLElement::XMLElement() :
  30. mElement(0)
  31. {
  32. }
  33. XMLElement::XMLElement(TiXmlElement* element) :
  34. mElement(element)
  35. {
  36. }
  37. XMLElement::XMLElement(const XMLElement& rhs) :
  38. mElement(rhs.mElement)
  39. {
  40. }
  41. XMLElement::~XMLElement()
  42. {
  43. }
  44. XMLElement XMLElement::createChildElement(const std::string& name)
  45. {
  46. if (!mElement)
  47. {
  48. LOGERROR("Null XML element, can not create child element " + name);
  49. return XMLElement();
  50. }
  51. TiXmlElement newElement(name.c_str());
  52. mElement->InsertEndChild(newElement);
  53. return XMLElement(static_cast<TiXmlElement*>(mElement->LastChild()));
  54. }
  55. bool XMLElement::removeChildElement(const std::string& name, bool last)
  56. {
  57. if (!mElement)
  58. return false;
  59. TiXmlNode* element;
  60. if (name.empty())
  61. {
  62. if (last)
  63. element = mElement->LastChild();
  64. else
  65. element = mElement->FirstChild();
  66. }
  67. else
  68. {
  69. if (last)
  70. element = mElement->LastChild(name.c_str());
  71. else
  72. element = mElement->FirstChild(name.c_str());
  73. }
  74. if (element)
  75. {
  76. mElement->RemoveChild(element);
  77. return true;
  78. }
  79. return false;
  80. }
  81. bool XMLElement::setAttribute(const std::string& name, const std::string& value)
  82. {
  83. if (!mElement)
  84. {
  85. LOGERROR("Null XML element, can not set attribute " + name);
  86. return false;
  87. }
  88. mElement->SetAttribute(name.c_str(), value.c_str());
  89. return true;
  90. }
  91. bool XMLElement::setBool(const std::string& name, bool value)
  92. {
  93. return setAttribute(name, toString(value));
  94. }
  95. bool XMLElement::setBoundingBox(const BoundingBox& value)
  96. {
  97. if (!setVector3("min", value.mMin))
  98. return false;
  99. return setVector3("max", value.mMax);
  100. }
  101. bool XMLElement::setBuffer(const std::string& name, const void* data, unsigned size)
  102. {
  103. std::string dataStr;
  104. const unsigned char* bytes = (const unsigned char*)data;
  105. for (unsigned i = 0; i < size; ++i)
  106. dataStr += toString(bytes[i]) + " ";
  107. return setAttribute(name, dataStr);
  108. }
  109. bool XMLElement::setBuffer(const std::string& name, const std::vector<unsigned char>& value)
  110. {
  111. if (!value.size())
  112. return setAttribute(name, std::string());
  113. else
  114. return setBuffer(name, &value[0], value.size());
  115. }
  116. bool XMLElement::setColor(const std::string& name, const Color& value)
  117. {
  118. return setAttribute(name, toString(value));
  119. }
  120. bool XMLElement::setFloat(const std::string& name, float value)
  121. {
  122. return setAttribute(name, toString(value));
  123. }
  124. bool XMLElement::setInt(const std::string& name, int value)
  125. {
  126. return setAttribute(name, toString(value));
  127. }
  128. bool XMLElement::setIntRect(const std::string& name, const IntRect& value)
  129. {
  130. return setAttribute(name, toString(value));
  131. }
  132. bool XMLElement::setIntVector2(const std::string& name, const IntVector2& value)
  133. {
  134. return setAttribute(name, toString(value));
  135. }
  136. bool XMLElement::setRect(const std::string& name, const Rect& value)
  137. {
  138. return setAttribute(name, toString(value));
  139. }
  140. bool XMLElement::setQuaternion(const std::string& name, const Quaternion& value)
  141. {
  142. return XMLElement::setAttribute(name, toString(value));
  143. }
  144. bool XMLElement::setString(const std::string& name, const std::string& value)
  145. {
  146. return setAttribute(name, value);
  147. }
  148. bool XMLElement::setVariant(const Variant& value)
  149. {
  150. if (!setAttribute("type", value.getTypeName()))
  151. return false;
  152. return setAttribute("value", value.toString());
  153. }
  154. bool XMLElement::setVariantMap(const VariantMap& value)
  155. {
  156. for (VariantMap::const_iterator i = value.begin(); i != value.end(); ++i)
  157. {
  158. XMLElement variantElem = createChildElement("variant");
  159. if (!variantElem)
  160. return false;
  161. std::string name = shortHashToString(i->first);
  162. if (name.empty())
  163. variantElem.setInt("hash", i->first.mData);
  164. else
  165. variantElem.setString("name", name);
  166. variantElem.setVariant(i->second);
  167. }
  168. return true;
  169. }
  170. bool XMLElement::setVector2(const std::string& name, const Vector2& value)
  171. {
  172. return setAttribute(name, toString(value));
  173. }
  174. bool XMLElement::setVector3(const std::string& name, const Vector3& value)
  175. {
  176. return setAttribute(name, toString(value));
  177. }
  178. bool XMLElement::setVector4(const std::string& name, const Vector4& value)
  179. {
  180. return setAttribute(name, toString(value));
  181. }
  182. std::string XMLElement::getName() const
  183. {
  184. if (!mElement)
  185. return std::string();
  186. return std::string(mElement->Value());
  187. }
  188. std::string XMLElement::getText() const
  189. {
  190. if (!mElement)
  191. return std::string();
  192. TiXmlNode* node = mElement->FirstChild();
  193. if (node)
  194. return std::string(node->Value());
  195. else
  196. return std::string();
  197. }
  198. bool XMLElement::hasChildElement(const std::string& name) const
  199. {
  200. if (!mElement)
  201. return false;
  202. if (mElement->FirstChildElement(name.c_str()) != 0)
  203. return true;
  204. else
  205. return false;
  206. }
  207. XMLElement XMLElement::getChildElement(const std::string& name) const
  208. {
  209. if (!mElement)
  210. return XMLElement();
  211. else
  212. {
  213. if (name.empty())
  214. return XMLElement(mElement->FirstChildElement());
  215. else
  216. return XMLElement(mElement->FirstChildElement(name.c_str()));
  217. }
  218. }
  219. XMLElement XMLElement::getNextElement(const std::string& name) const
  220. {
  221. if (!mElement)
  222. return XMLElement();
  223. if (name.empty())
  224. return XMLElement(mElement->NextSiblingElement());
  225. else
  226. return XMLElement(mElement->NextSiblingElement(name.c_str()));
  227. }
  228. XMLElement XMLElement::getParentElement() const
  229. {
  230. if (!mElement)
  231. return XMLElement();
  232. return XMLElement(dynamic_cast<TiXmlElement*>(mElement->Parent()));
  233. }
  234. bool XMLElement::hasAttribute(const std::string& name) const
  235. {
  236. if (!mElement)
  237. return false;
  238. if (mElement->Attribute(name.c_str()) != 0)
  239. return true;
  240. else
  241. return false;
  242. }
  243. std::string XMLElement::getAttribute(const std::string& name) const
  244. {
  245. if (!mElement)
  246. return std::string();
  247. else
  248. {
  249. const char* data = mElement->Attribute(name.c_str());
  250. if (!data)
  251. return std::string();
  252. else
  253. return std::string(data);
  254. }
  255. }
  256. bool XMLElement::getBool(const std::string& name) const
  257. {
  258. return toBool(getAttribute(name));
  259. }
  260. BoundingBox XMLElement::getBoundingBox() const
  261. {
  262. BoundingBox ret;
  263. ret.mMin = getVector3("min");
  264. ret.mMax = getVector3("max");
  265. ret.mDefined = true;
  266. return ret;
  267. }
  268. std::vector<unsigned char> XMLElement::getBuffer(const std::string& name) const
  269. {
  270. std::vector<unsigned char> ret;
  271. std::vector<std::string> bytes = split(getAttribute(name), ' ');
  272. ret.resize(bytes.size());
  273. for (unsigned i = 0; i < bytes.size(); ++i)
  274. ret[i] = toInt(bytes[i]);
  275. return ret;
  276. }
  277. void XMLElement::getBuffer(const std::string& name, void* dest, unsigned size) const
  278. {
  279. std::vector<unsigned char> ret;
  280. std::vector<std::string> bytes = split(getAttribute(name), ' ');
  281. unsigned char* destBytes = (unsigned char*)dest;
  282. if (size < bytes.size())
  283. SAFE_EXCEPTION("Destination buffer not big enough");
  284. for (unsigned i = 0; i < bytes.size(); ++i)
  285. destBytes[i] = toInt(bytes[i]);
  286. }
  287. Color XMLElement::getColor(const std::string& name) const
  288. {
  289. return toColor(getAttribute(name));
  290. }
  291. float XMLElement::getFloat(const std::string& name) const
  292. {
  293. return toFloat(getAttribute(name));
  294. }
  295. int XMLElement::getInt(const std::string& name) const
  296. {
  297. return toInt(getAttribute(name));
  298. }
  299. IntRect XMLElement::getIntRect(const std::string& name) const
  300. {
  301. return toIntRect(getAttribute(name));
  302. }
  303. IntVector2 XMLElement::getIntVector2(const std::string& name) const
  304. {
  305. return toIntVector2(getAttribute(name));
  306. }
  307. Quaternion XMLElement::getQuaternion(const std::string& name) const
  308. {
  309. return toQuaternion(getAttribute(name));
  310. }
  311. Rect XMLElement::getRect(const std::string& name) const
  312. {
  313. return toRect(getAttribute(name));
  314. }
  315. std::string XMLElement::getString(const std::string& name) const
  316. {
  317. return getAttribute(name);
  318. }
  319. std::string XMLElement::getStringLower(const std::string& name) const
  320. {
  321. return toLower(getAttribute(name));
  322. }
  323. std::string XMLElement::getStringUpper(const std::string& name) const
  324. {
  325. return toUpper(getAttribute(name));
  326. }
  327. Variant XMLElement::getVariant() const
  328. {
  329. Variant ret;
  330. std::string type = getAttribute("type");
  331. std::string value = getAttribute("value");
  332. ret.fromString(type, value);
  333. return ret;
  334. }
  335. VariantMap XMLElement::getVariantMap() const
  336. {
  337. VariantMap ret;
  338. XMLElement variantElem = getChildElement("variant");
  339. while (variantElem)
  340. {
  341. ShortStringHash key;
  342. if (variantElem.hasAttribute("hash"))
  343. key.mData = variantElem.getInt("hash");
  344. else
  345. key = ShortStringHash(variantElem.getString("name"));
  346. ret[key] = variantElem.getVariant();
  347. variantElem = variantElem.getNextElement("variant");
  348. }
  349. return ret;
  350. }
  351. Vector2 XMLElement::getVector2(const std::string& name) const
  352. {
  353. return toVector2(getAttribute(name));
  354. }
  355. Vector3 XMLElement::getVector3(const std::string& name) const
  356. {
  357. return toVector3(getAttribute(name));
  358. }
  359. Vector4 XMLElement::getVector4(const std::string& name) const
  360. {
  361. return toVector4(getAttribute(name));
  362. }
  363. Vector4 XMLElement::getVector(const std::string& name) const
  364. {
  365. return toVector4(getAttribute(name), true);
  366. }