XMLElement.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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 "Context.h"
  25. #include "ResourceCache.h"
  26. #include "StringUtils.h"
  27. #include "XMLFile.h"
  28. #include <tinyxml.h>
  29. XMLElement::XMLElement() :
  30. element_(0)
  31. {
  32. }
  33. XMLElement::XMLElement(XMLFile* file, TiXmlElement* element) :
  34. file_(file),
  35. element_(element)
  36. {
  37. }
  38. XMLElement::XMLElement(const XMLElement& rhs) :
  39. file_(rhs.file_),
  40. element_(rhs.file_ ? rhs.element_ : 0)
  41. {
  42. }
  43. XMLElement::~XMLElement()
  44. {
  45. }
  46. XMLElement XMLElement::CreateChildElement(const std::string& name)
  47. {
  48. if ((!file_) || (!element_))
  49. return XMLElement();
  50. TiXmlElement newElement(name.c_str());
  51. element_->InsertEndChild(newElement);
  52. return XMLElement(file_, static_cast<TiXmlElement*>(element_->LastChild()));
  53. }
  54. bool XMLElement::RemoveChildElement(const std::string& name, bool last)
  55. {
  56. if ((!file_) || (!element_))
  57. return false;
  58. TiXmlNode* element;
  59. if (name.empty())
  60. {
  61. if (last)
  62. element = element_->LastChild();
  63. else
  64. element = element_->FirstChild();
  65. }
  66. else
  67. {
  68. if (last)
  69. element = element_->LastChild(name.c_str());
  70. else
  71. element = element_->FirstChild(name.c_str());
  72. }
  73. if (element)
  74. {
  75. element_->RemoveChild(element);
  76. return true;
  77. }
  78. return false;
  79. }
  80. bool XMLElement::RemoveChildElements(const std::string& name)
  81. {
  82. if ((!file_) || (!element_))
  83. return false;
  84. TiXmlNode* element;
  85. if (name.empty())
  86. {
  87. while (element = element_->LastChild())
  88. element_->RemoveChild(element);
  89. }
  90. else
  91. {
  92. while (element = element_->LastChild(name.c_str()))
  93. element_->RemoveChild(element);
  94. }
  95. return true;
  96. }
  97. bool XMLElement::SetAttribute(const std::string& name, const std::string& value)
  98. {
  99. if ((!file_) || (!element_))
  100. return false;
  101. element_->SetAttribute(name.c_str(), value.c_str());
  102. return true;
  103. }
  104. bool XMLElement::SetBool(const std::string& name, bool value)
  105. {
  106. return SetAttribute(name, ToString(value));
  107. }
  108. bool XMLElement::SetBoundingBox(const BoundingBox& value)
  109. {
  110. if (!SetVector3("min", value.min_))
  111. return false;
  112. return SetVector3("max", value.max_);
  113. }
  114. bool XMLElement::SetBuffer(const std::string& name, const void* data, unsigned size)
  115. {
  116. std::string dataStr;
  117. const unsigned char* bytes = (const unsigned char*)data;
  118. for (unsigned i = 0; i < size; ++i)
  119. dataStr += ToString(bytes[i]) + " ";
  120. return SetAttribute(name, dataStr);
  121. }
  122. bool XMLElement::SetBuffer(const std::string& name, const std::vector<unsigned char>& value)
  123. {
  124. if (!value.size())
  125. return SetAttribute(name, std::string());
  126. else
  127. return SetBuffer(name, &value[0], value.size());
  128. }
  129. bool XMLElement::SetColor(const std::string& name, const Color& value)
  130. {
  131. return SetAttribute(name, ToString(value));
  132. }
  133. bool XMLElement::SetFloat(const std::string& name, float value)
  134. {
  135. return SetAttribute(name, ToString(value));
  136. }
  137. bool XMLElement::SetInt(const std::string& name, int value)
  138. {
  139. return SetAttribute(name, ToString(value));
  140. }
  141. bool XMLElement::SetIntRect(const std::string& name, const IntRect& value)
  142. {
  143. return SetAttribute(name, ToString(value));
  144. }
  145. bool XMLElement::SetIntVector2(const std::string& name, const IntVector2& value)
  146. {
  147. return SetAttribute(name, ToString(value));
  148. }
  149. bool XMLElement::SetRect(const std::string& name, const Rect& value)
  150. {
  151. return SetAttribute(name, ToString(value));
  152. }
  153. bool XMLElement::SetQuaternion(const std::string& name, const Quaternion& value)
  154. {
  155. return XMLElement::SetAttribute(name, ToString(value));
  156. }
  157. bool XMLElement::SetString(const std::string& name, const std::string& value)
  158. {
  159. return SetAttribute(name, value);
  160. }
  161. bool XMLElement::SetVariant(const Variant& value)
  162. {
  163. if (!SetAttribute("type", value.GetTypeName()))
  164. return false;
  165. switch (value.GetType())
  166. {
  167. case VAR_RESOURCEREF:
  168. return SetResourceRef(value.GetResourceRef());
  169. case VAR_RESOURCEREFLIST:
  170. return SetResourceRefList(value.GetResourceRefList());
  171. case VAR_VARIANTVECTOR:
  172. return SetVariantVector(value.GetVariantVector());
  173. case VAR_VARIANTMAP:
  174. return SetVariantMap(value.GetVariantMap());
  175. default:
  176. return SetAttribute("value", value.ToString());
  177. }
  178. }
  179. bool XMLElement::SetResourceRef(const ResourceRef& value)
  180. {
  181. if ((!file_) || (!element_))
  182. return false;
  183. // Need the context & resource cache to query for reverse hash mappings
  184. Context* context = file_->GetContext();
  185. ResourceCache* cache = file_->GetSubsystem<ResourceCache>();
  186. SetAttribute("value", std::string(context->GetTypeName(value.type_)) + ";" + cache->GetResourceName(value.id_));
  187. return true;
  188. }
  189. bool XMLElement::SetResourceRefList(const ResourceRefList& value)
  190. {
  191. if ((!file_) || (!element_))
  192. return false;
  193. // Need the context & resource cache to query for reverse hash mappings
  194. Context* context = file_->GetContext();
  195. ResourceCache* cache = file_->GetSubsystem<ResourceCache>();
  196. std::string str(context->GetTypeName(value.type_));
  197. for (unsigned i = 0; i < value.ids_.size(); ++i)
  198. {
  199. str += ";";
  200. str += cache->GetResourceName(value.ids_[i]);
  201. }
  202. SetAttribute("value", str);
  203. return true;
  204. }
  205. bool XMLElement::SetVariantVector(const VariantVector& value)
  206. {
  207. // Must remove all existing variant child elements (if they exist) to not cause confusion
  208. if (!RemoveChildElements("variant"))
  209. return false;
  210. for (VariantVector::const_iterator i = value.begin(); i != value.end(); ++i)
  211. {
  212. XMLElement variantElem = CreateChildElement("variant");
  213. if (!variantElem)
  214. return false;
  215. variantElem.SetVariant(*i);
  216. }
  217. return true;
  218. }
  219. bool XMLElement::SetVariantMap(const VariantMap& value)
  220. {
  221. if (!RemoveChildElements("variant"))
  222. return false;
  223. for (VariantMap::const_iterator i = value.begin(); i != value.end(); ++i)
  224. {
  225. XMLElement variantElem = CreateChildElement("variant");
  226. if (!variantElem)
  227. return false;
  228. variantElem.SetInt("hash", i->first.GetValue());
  229. variantElem.SetVariant(i->second);
  230. }
  231. return true;
  232. }
  233. bool XMLElement::SetVector2(const std::string& name, const Vector2& value)
  234. {
  235. return SetAttribute(name, ToString(value));
  236. }
  237. bool XMLElement::SetVector3(const std::string& name, const Vector3& value)
  238. {
  239. return SetAttribute(name, ToString(value));
  240. }
  241. bool XMLElement::SetVector4(const std::string& name, const Vector4& value)
  242. {
  243. return SetAttribute(name, ToString(value));
  244. }
  245. std::string XMLElement::GetName() const
  246. {
  247. if ((!file_) || (!element_))
  248. return std::string();
  249. return std::string(element_->Value());
  250. }
  251. std::string XMLElement::GetText() const
  252. {
  253. if ((!file_) || (!element_))
  254. return std::string();
  255. TiXmlNode* node = element_->FirstChild();
  256. if (node)
  257. return std::string(node->Value());
  258. else
  259. return std::string();
  260. }
  261. bool XMLElement::HasChildElement(const std::string& name) const
  262. {
  263. if ((!file_) || (!element_))
  264. return false;
  265. if (element_->FirstChildElement(name.c_str()) != 0)
  266. return true;
  267. else
  268. return false;
  269. }
  270. XMLElement XMLElement::GetChildElement(const std::string& name) const
  271. {
  272. if ((!file_) || (!element_))
  273. return XMLElement();
  274. else
  275. {
  276. if (name.empty())
  277. return XMLElement(file_, element_->FirstChildElement());
  278. else
  279. return XMLElement(file_, element_->FirstChildElement(name.c_str()));
  280. }
  281. }
  282. XMLElement XMLElement::GetNextElement(const std::string& name) const
  283. {
  284. if ((!file_) || (!element_))
  285. return XMLElement();
  286. if (name.empty())
  287. return XMLElement(file_, element_->NextSiblingElement());
  288. else
  289. return XMLElement(file_, element_->NextSiblingElement(name.c_str()));
  290. }
  291. XMLElement XMLElement::GetParentElement() const
  292. {
  293. if ((!file_) || (!element_))
  294. return XMLElement();
  295. return XMLElement(file_, dynamic_cast<TiXmlElement*>(element_->Parent()));
  296. }
  297. unsigned XMLElement::GetNumAttributes() const
  298. {
  299. unsigned ret = 0;
  300. if ((file_) && (element_))
  301. {
  302. const TiXmlAttribute* attribute = element_->FirstAttribute();
  303. while (attribute)
  304. {
  305. ++ret;
  306. attribute = attribute->Next();
  307. }
  308. }
  309. return ret;
  310. }
  311. bool XMLElement::HasAttribute(const std::string& name) const
  312. {
  313. if ((!file_) || (!element_))
  314. return false;
  315. if (element_->Attribute(name.c_str()) != 0)
  316. return true;
  317. else
  318. return false;
  319. }
  320. std::string XMLElement::GetAttribute(const std::string& name) const
  321. {
  322. if ((!file_) || (!element_))
  323. return std::string();
  324. else
  325. {
  326. const char* data = element_->Attribute(name.c_str());
  327. if (!data)
  328. return std::string();
  329. else
  330. return std::string(data);
  331. }
  332. }
  333. std::vector<std::string> XMLElement::GetAttributeNames() const
  334. {
  335. std::vector<std::string> ret;
  336. if ((file_) && (element_))
  337. {
  338. const TiXmlAttribute* attribute = element_->FirstAttribute();
  339. while (attribute)
  340. {
  341. ret.push_back(std::string(attribute->Name()));
  342. attribute = attribute->Next();
  343. }
  344. }
  345. return ret;
  346. }
  347. bool XMLElement::GetBool(const std::string& name) const
  348. {
  349. return ToBool(GetAttribute(name));
  350. }
  351. BoundingBox XMLElement::GetBoundingBox() const
  352. {
  353. BoundingBox ret;
  354. ret.min_ = GetVector3("min");
  355. ret.max_ = GetVector3("max");
  356. ret.defined_ = true;
  357. return ret;
  358. }
  359. std::vector<unsigned char> XMLElement::GetBuffer(const std::string& name) const
  360. {
  361. std::vector<unsigned char> ret;
  362. std::vector<std::string> bytes = Split(GetAttribute(name), ' ');
  363. ret.resize(bytes.size());
  364. for (unsigned i = 0; i < bytes.size(); ++i)
  365. ret[i] = ToInt(bytes[i]);
  366. return ret;
  367. }
  368. bool XMLElement::GetBuffer(const std::string& name, void* dest, unsigned size) const
  369. {
  370. std::vector<unsigned char> ret;
  371. std::vector<std::string> bytes = Split(GetAttribute(name), ' ');
  372. unsigned char* destBytes = (unsigned char*)dest;
  373. if (size < bytes.size())
  374. return false;
  375. for (unsigned i = 0; i < bytes.size(); ++i)
  376. destBytes[i] = ToInt(bytes[i]);
  377. return true;
  378. }
  379. Color XMLElement::GetColor(const std::string& name) const
  380. {
  381. return ToColor(GetAttribute(name));
  382. }
  383. float XMLElement::GetFloat(const std::string& name) const
  384. {
  385. return ToFloat(GetAttribute(name));
  386. }
  387. int XMLElement::GetInt(const std::string& name) const
  388. {
  389. return ToInt(GetAttribute(name));
  390. }
  391. IntRect XMLElement::GetIntRect(const std::string& name) const
  392. {
  393. return ToIntRect(GetAttribute(name));
  394. }
  395. IntVector2 XMLElement::GetIntVector2(const std::string& name) const
  396. {
  397. return ToIntVector2(GetAttribute(name));
  398. }
  399. Quaternion XMLElement::GetQuaternion(const std::string& name) const
  400. {
  401. return ToQuaternion(GetAttribute(name));
  402. }
  403. Rect XMLElement::GetRect(const std::string& name) const
  404. {
  405. return ToRect(GetAttribute(name));
  406. }
  407. std::string XMLElement::GetString(const std::string& name) const
  408. {
  409. return GetAttribute(name);
  410. }
  411. std::string XMLElement::GetStringLower(const std::string& name) const
  412. {
  413. return ToLower(GetAttribute(name));
  414. }
  415. std::string XMLElement::GetStringUpper(const std::string& name) const
  416. {
  417. return ToUpper(GetAttribute(name));
  418. }
  419. Variant XMLElement::GetVariant() const
  420. {
  421. Variant ret;
  422. std::string type = ToLower(GetAttribute("type"));
  423. if (type == "resourceref")
  424. ret = GetResourceRef();
  425. else if (type == "resourcereflist")
  426. ret = GetResourceRefList();
  427. else if (type == "variantvector")
  428. ret = GetVariantVector();
  429. else if (type == "variantmap")
  430. ret = GetVariantMap();
  431. else
  432. ret.FromString(type, GetAttribute("value"));
  433. return ret;
  434. }
  435. ResourceRef XMLElement::GetResourceRef() const
  436. {
  437. ResourceRef ret;
  438. std::vector<std::string> values = Split(GetAttribute("value"), ';');
  439. if (values.size() == 2)
  440. {
  441. ret.type_ = ShortStringHash(values[0]);
  442. ret.id_ = StringHash(values[1]);
  443. }
  444. return ret;
  445. }
  446. ResourceRefList XMLElement::GetResourceRefList() const
  447. {
  448. ResourceRefList ret;
  449. std::vector<std::string> values = Split(GetAttribute("value"), ';');
  450. if (values.size() >= 1)
  451. {
  452. ret.type_ = ShortStringHash(values[0]);
  453. ret.ids_.resize(values.size() - 1);
  454. for (unsigned i = 1; i < values.size(); ++i)
  455. ret.ids_[i - 1] = StringHash(values[i]);
  456. }
  457. return ret;
  458. }
  459. VariantVector XMLElement::GetVariantVector() const
  460. {
  461. VariantVector ret;
  462. XMLElement variantElem = GetChildElement("variant");
  463. while (variantElem)
  464. {
  465. ret.push_back(variantElem.GetVariant());
  466. variantElem = variantElem.GetNextElement("variant");
  467. }
  468. return ret;
  469. }
  470. VariantMap XMLElement::GetVariantMap() const
  471. {
  472. VariantMap ret;
  473. XMLElement variantElem = GetChildElement("variant");
  474. while (variantElem)
  475. {
  476. ShortStringHash key(variantElem.GetInt("hash"));
  477. ret[key] = variantElem.GetVariant();
  478. variantElem = variantElem.GetNextElement("variant");
  479. }
  480. return ret;
  481. }
  482. Vector2 XMLElement::GetVector2(const std::string& name) const
  483. {
  484. return ToVector2(GetAttribute(name));
  485. }
  486. Vector3 XMLElement::GetVector3(const std::string& name) const
  487. {
  488. return ToVector3(GetAttribute(name));
  489. }
  490. Vector4 XMLElement::GetVector4(const std::string& name) const
  491. {
  492. return ToVector4(GetAttribute(name));
  493. }
  494. Vector4 XMLElement::GetVector(const std::string& name) const
  495. {
  496. return ToVector4(GetAttribute(name), true);
  497. }
  498. XMLFile* XMLElement::GetFile() const
  499. {
  500. return file_;
  501. }