XMLElement.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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::CreateChild(const String& name)
  47. {
  48. if (!file_ || !element_)
  49. return XMLElement();
  50. TiXmlElement newElement(name.CString());
  51. element_->InsertEndChild(newElement);
  52. return XMLElement(file_, static_cast<TiXmlElement*>(element_->LastChild()));
  53. }
  54. bool XMLElement::RemoveChild(const 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.CString());
  70. else
  71. element = element_->FirstChild(name.CString());
  72. }
  73. if (element)
  74. {
  75. element_->RemoveChild(element);
  76. return true;
  77. }
  78. return false;
  79. }
  80. bool XMLElement::RemoveChildren(const 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.CString()))
  93. element_->RemoveChild(element);
  94. }
  95. return true;
  96. }
  97. bool XMLElement::SetAttribute(const String& name, const String& value)
  98. {
  99. if (!file_ || !element_)
  100. return false;
  101. element_->SetAttribute(name.CString(), value.CString());
  102. return true;
  103. }
  104. bool XMLElement::SetBool(const String& name, bool value)
  105. {
  106. return SetAttribute(name, String(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 String& name, const void* data, unsigned size)
  115. {
  116. String dataStr;
  117. const unsigned char* bytes = (const unsigned char*)data;
  118. for (unsigned i = 0; i < size; ++i)
  119. dataStr += String((unsigned)bytes[i]) + " ";
  120. return SetAttribute(name, dataStr);
  121. }
  122. bool XMLElement::SetBuffer(const String& name, const PODVector<unsigned char>& value)
  123. {
  124. if (!value.Size())
  125. return SetAttribute(name, String());
  126. else
  127. return SetBuffer(name, &value[0], value.Size());
  128. }
  129. bool XMLElement::SetColor(const String& name, const Color& value)
  130. {
  131. return SetAttribute(name, value.ToString());
  132. }
  133. bool XMLElement::SetFloat(const String& name, float value)
  134. {
  135. return SetAttribute(name, String(value));
  136. }
  137. bool XMLElement::SetInt(const String& name, int value)
  138. {
  139. return SetAttribute(name, String(value));
  140. }
  141. bool XMLElement::SetIntRect(const String& name, const IntRect& value)
  142. {
  143. return SetAttribute(name, value.ToString());
  144. }
  145. bool XMLElement::SetIntVector2(const String& name, const IntVector2& value)
  146. {
  147. return SetAttribute(name, value.ToString());
  148. }
  149. bool XMLElement::SetRect(const String& name, const Rect& value)
  150. {
  151. return SetAttribute(name, value.ToString());
  152. }
  153. bool XMLElement::SetQuaternion(const String& name, const Quaternion& value)
  154. {
  155. return XMLElement::SetAttribute(name, value.ToString());
  156. }
  157. bool XMLElement::SetString(const String& name, const 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", 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. 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 (!RemoveChildren("variant"))
  209. return false;
  210. for (VariantVector::ConstIterator i = value.Begin(); i != value.End(); ++i)
  211. {
  212. XMLElement variantElem = CreateChild("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 (!RemoveChildren("variant"))
  222. return false;
  223. for (VariantMap::ConstIterator i = value.Begin(); i != value.End(); ++i)
  224. {
  225. XMLElement variantElem = CreateChild("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 String& name, const Vector2& value)
  234. {
  235. return SetAttribute(name, value.ToString());
  236. }
  237. bool XMLElement::SetVector3(const String& name, const Vector3& value)
  238. {
  239. return SetAttribute(name, value.ToString());
  240. }
  241. bool XMLElement::SetVector4(const String& name, const Vector4& value)
  242. {
  243. return SetAttribute(name, value.ToString());
  244. }
  245. String XMLElement::GetName() const
  246. {
  247. if (!file_ || !element_)
  248. return String();
  249. return String(element_->Value());
  250. }
  251. String XMLElement::GetText() const
  252. {
  253. if (!file_ || !element_)
  254. return String();
  255. TiXmlNode* node = element_->FirstChild();
  256. if (node)
  257. return String(node->Value());
  258. else
  259. return String();
  260. }
  261. bool XMLElement::HasChild(const String& name) const
  262. {
  263. if (!file_ || !element_)
  264. return false;
  265. if (element_->FirstChildElement(name.CString()) != 0)
  266. return true;
  267. else
  268. return false;
  269. }
  270. XMLElement XMLElement::GetChild(const 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.CString()));
  280. }
  281. }
  282. XMLElement XMLElement::GetNext(const 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.CString()));
  290. }
  291. XMLElement XMLElement::GetParent() 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 String& name) const
  312. {
  313. if (!file_ || !element_)
  314. return false;
  315. if (element_->Attribute(name.CString()) != 0)
  316. return true;
  317. else
  318. return false;
  319. }
  320. String XMLElement::GetAttribute(const String& name) const
  321. {
  322. if (!file_ || !element_)
  323. return String();
  324. else
  325. {
  326. const char* data = element_->Attribute(name.CString());
  327. if (!data)
  328. return String();
  329. else
  330. return String(data);
  331. }
  332. }
  333. Vector<String> XMLElement::GetAttributeNames() const
  334. {
  335. Vector<String> ret;
  336. if (file_ && element_)
  337. {
  338. const TiXmlAttribute* attribute = element_->FirstAttribute();
  339. while (attribute)
  340. {
  341. ret.Push(String(attribute->Name()));
  342. attribute = attribute->Next();
  343. }
  344. }
  345. return ret;
  346. }
  347. bool XMLElement::GetBool(const 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. PODVector<unsigned char> XMLElement::GetBuffer(const String& name) const
  360. {
  361. PODVector<unsigned char> ret;
  362. Vector<String> bytes = GetAttribute(name).Split(' ');
  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 String& name, void* dest, unsigned size) const
  369. {
  370. PODVector<unsigned char> ret;
  371. Vector<String> bytes = GetAttribute(name).Split(' ');
  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 String& name) const
  380. {
  381. return ToColor(GetAttribute(name));
  382. }
  383. float XMLElement::GetFloat(const String& name) const
  384. {
  385. return ToFloat(GetAttribute(name));
  386. }
  387. int XMLElement::GetInt(const String& name) const
  388. {
  389. return ToInt(GetAttribute(name));
  390. }
  391. IntRect XMLElement::GetIntRect(const String& name) const
  392. {
  393. return ToIntRect(GetAttribute(name));
  394. }
  395. IntVector2 XMLElement::GetIntVector2(const String& name) const
  396. {
  397. return ToIntVector2(GetAttribute(name));
  398. }
  399. Quaternion XMLElement::GetQuaternion(const String& name) const
  400. {
  401. return ToQuaternion(GetAttribute(name));
  402. }
  403. Rect XMLElement::GetRect(const String& name) const
  404. {
  405. return ToRect(GetAttribute(name));
  406. }
  407. String XMLElement::GetString(const String& name) const
  408. {
  409. return GetAttribute(name);
  410. }
  411. String XMLElement::GetStringLower(const String& name) const
  412. {
  413. return GetAttribute(name).ToLower();
  414. }
  415. String XMLElement::GetStringUpper(const String& name) const
  416. {
  417. return GetAttribute(name).ToUpper();
  418. }
  419. Variant XMLElement::GetVariant() const
  420. {
  421. Variant ret;
  422. String type = GetAttribute("type").ToLower();
  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. Vector<String> values = GetAttribute("value").Split(';');
  439. if (values.Size() == 2)
  440. {
  441. ret.type_ = ShortStringHash(values[0]);
  442. ret.id_ = StringHash(values[1]);
  443. // Whenever we encounter a resource name read from a ResourceRef XML element, store the reverse mapping to
  444. // ResourceCache if possible. We will probably use the hash to request a resource shortly afterward
  445. if (file_)
  446. file_->GetSubsystem<ResourceCache>()->StoreNameHash(values[1]);
  447. }
  448. return ret;
  449. }
  450. ResourceRefList XMLElement::GetResourceRefList() const
  451. {
  452. ResourceRefList ret;
  453. Vector<String> values = GetAttribute("value").Split(';');
  454. if (values.Size() >= 1)
  455. {
  456. // Whenever we encounter resource names read from a ResourceRefList XML element, store the reverse mapping to
  457. // ResourceCache if possible. We will probably use the hashes to request resources shortly afterward
  458. ResourceCache* cache = file_ ? file_->GetSubsystem<ResourceCache>() : 0;
  459. ret.type_ = ShortStringHash(values[0]);
  460. ret.ids_.Resize(values.Size() - 1);
  461. for (unsigned i = 1; i < values.Size(); ++i)
  462. {
  463. ret.ids_[i - 1] = StringHash(values[i]);
  464. if (cache)
  465. cache->StoreNameHash(values[i]);
  466. }
  467. }
  468. return ret;
  469. }
  470. VariantVector XMLElement::GetVariantVector() const
  471. {
  472. VariantVector ret;
  473. XMLElement variantElem = GetChild("variant");
  474. while (variantElem)
  475. {
  476. ret.Push(variantElem.GetVariant());
  477. variantElem = variantElem.GetNext("variant");
  478. }
  479. return ret;
  480. }
  481. VariantMap XMLElement::GetVariantMap() const
  482. {
  483. VariantMap ret;
  484. XMLElement variantElem = GetChild("variant");
  485. while (variantElem)
  486. {
  487. ShortStringHash key(variantElem.GetInt("hash"));
  488. ret[key] = variantElem.GetVariant();
  489. variantElem = variantElem.GetNext("variant");
  490. }
  491. return ret;
  492. }
  493. Vector2 XMLElement::GetVector2(const String& name) const
  494. {
  495. return ToVector2(GetAttribute(name));
  496. }
  497. Vector3 XMLElement::GetVector3(const String& name) const
  498. {
  499. return ToVector3(GetAttribute(name));
  500. }
  501. Vector4 XMLElement::GetVector4(const String& name) const
  502. {
  503. return ToVector4(GetAttribute(name));
  504. }
  505. Vector4 XMLElement::GetVector(const String& name) const
  506. {
  507. return ToVector4(GetAttribute(name), true);
  508. }
  509. XMLFile* XMLElement::GetFile() const
  510. {
  511. return file_;
  512. }