XMLElement.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. bool XMLElement::hasAttribute(const std::string& name) const
  229. {
  230. if (!mElement)
  231. return false;
  232. if (mElement->Attribute(name.c_str()) != 0)
  233. return true;
  234. else
  235. return false;
  236. }
  237. std::string XMLElement::getAttribute(const std::string& name) const
  238. {
  239. if (!mElement)
  240. return std::string();
  241. else
  242. {
  243. const char* data = mElement->Attribute(name.c_str());
  244. if (!data)
  245. return std::string();
  246. else
  247. return std::string(data);
  248. }
  249. }
  250. bool XMLElement::getBool(const std::string& name) const
  251. {
  252. return toBool(getAttribute(name));
  253. }
  254. BoundingBox XMLElement::getBoundingBox() const
  255. {
  256. BoundingBox ret;
  257. ret.mMin = getVector3("min");
  258. ret.mMax = getVector3("max");
  259. ret.mDefined = true;
  260. return ret;
  261. }
  262. std::vector<unsigned char> XMLElement::getBuffer(const std::string& name) const
  263. {
  264. std::vector<unsigned char> ret;
  265. std::vector<std::string> bytes = split(getAttribute(name), ' ');
  266. ret.resize(bytes.size());
  267. for (unsigned i = 0; i < bytes.size(); ++i)
  268. ret[i] = toInt(bytes[i]);
  269. return ret;
  270. }
  271. void XMLElement::getBuffer(const std::string& name, void* dest, unsigned size) const
  272. {
  273. std::vector<unsigned char> ret;
  274. std::vector<std::string> bytes = split(getAttribute(name), ' ');
  275. unsigned char* destBytes = (unsigned char*)dest;
  276. if (size < bytes.size())
  277. SAFE_EXCEPTION("Destination buffer not big enough");
  278. for (unsigned i = 0; i < bytes.size(); ++i)
  279. destBytes[i] = toInt(bytes[i]);
  280. }
  281. Color XMLElement::getColor(const std::string& name) const
  282. {
  283. return toColor(getAttribute(name));
  284. }
  285. float XMLElement::getFloat(const std::string& name) const
  286. {
  287. return toFloat(getAttribute(name));
  288. }
  289. int XMLElement::getInt(const std::string& name) const
  290. {
  291. return toInt(getAttribute(name));
  292. }
  293. IntRect XMLElement::getIntRect(const std::string& name) const
  294. {
  295. return toIntRect(getAttribute(name));
  296. }
  297. IntVector2 XMLElement::getIntVector2(const std::string& name) const
  298. {
  299. return toIntVector2(getAttribute(name));
  300. }
  301. Quaternion XMLElement::getQuaternion(const std::string& name) const
  302. {
  303. return toQuaternion(getAttribute(name));
  304. }
  305. Rect XMLElement::getRect(const std::string& name) const
  306. {
  307. return toRect(getAttribute(name));
  308. }
  309. std::string XMLElement::getString(const std::string& name) const
  310. {
  311. return getAttribute(name);
  312. }
  313. std::string XMLElement::getStringLower(const std::string& name) const
  314. {
  315. return toLower(getAttribute(name));
  316. }
  317. std::string XMLElement::getStringUpper(const std::string& name) const
  318. {
  319. return toUpper(getAttribute(name));
  320. }
  321. Variant XMLElement::getVariant() const
  322. {
  323. Variant ret;
  324. std::string type = getAttribute("type");
  325. std::string value = getAttribute("value");
  326. ret.fromString(type, value);
  327. return ret;
  328. }
  329. VariantMap XMLElement::getVariantMap() const
  330. {
  331. VariantMap ret;
  332. XMLElement variantElem = getChildElement("variant");
  333. while (variantElem)
  334. {
  335. ShortStringHash key;
  336. if (variantElem.hasAttribute("hash"))
  337. key.mData = variantElem.getInt("hash");
  338. else
  339. key = ShortStringHash(variantElem.getString("name"));
  340. ret[key] = variantElem.getVariant();
  341. variantElem = variantElem.getNextElement("variant");
  342. }
  343. return ret;
  344. }
  345. Vector2 XMLElement::getVector2(const std::string& name) const
  346. {
  347. return toVector2(getAttribute(name));
  348. }
  349. Vector3 XMLElement::getVector3(const std::string& name) const
  350. {
  351. return toVector3(getAttribute(name));
  352. }
  353. Vector4 XMLElement::getVector4(const std::string& name) const
  354. {
  355. return toVector4(getAttribute(name));
  356. }
  357. Vector4 XMLElement::getVector(const std::string& name) const
  358. {
  359. return toVector4(getAttribute(name), true);
  360. }