XMLElement.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. return SetVariantValue(value);
  166. }
  167. bool XMLElement::SetVariantValue(const Variant& value)
  168. {
  169. switch (value.GetType())
  170. {
  171. case VAR_RESOURCEREF:
  172. return SetResourceRef(value.GetResourceRef());
  173. case VAR_RESOURCEREFLIST:
  174. return SetResourceRefList(value.GetResourceRefList());
  175. case VAR_VARIANTVECTOR:
  176. return SetVariantVector(value.GetVariantVector());
  177. case VAR_VARIANTMAP:
  178. return SetVariantMap(value.GetVariantMap());
  179. default:
  180. return SetAttribute("value", value.ToString());
  181. }
  182. }
  183. bool XMLElement::SetResourceRef(const ResourceRef& value)
  184. {
  185. if (!file_ || !element_)
  186. return false;
  187. // Need the context & resource cache to query for reverse hash mappings
  188. Context* context = file_->GetContext();
  189. ResourceCache* cache = file_->GetSubsystem<ResourceCache>();
  190. SetAttribute("value", String(context->GetTypeName(value.type_)) + ";" + cache->GetResourceName(value.id_));
  191. return true;
  192. }
  193. bool XMLElement::SetResourceRefList(const ResourceRefList& value)
  194. {
  195. if (!file_ || !element_)
  196. return false;
  197. // Need the context & resource cache to query for reverse hash mappings
  198. Context* context = file_->GetContext();
  199. ResourceCache* cache = file_->GetSubsystem<ResourceCache>();
  200. String str(context->GetTypeName(value.type_));
  201. for (unsigned i = 0; i < value.ids_.Size(); ++i)
  202. {
  203. str += ";";
  204. str += cache->GetResourceName(value.ids_[i]);
  205. }
  206. SetAttribute("value", str);
  207. return true;
  208. }
  209. bool XMLElement::SetVariantVector(const VariantVector& value)
  210. {
  211. // Must remove all existing variant child elements (if they exist) to not cause confusion
  212. if (!RemoveChildren("variant"))
  213. return false;
  214. for (VariantVector::ConstIterator i = value.Begin(); i != value.End(); ++i)
  215. {
  216. XMLElement variantElem = CreateChild("variant");
  217. if (!variantElem)
  218. return false;
  219. variantElem.SetVariant(*i);
  220. }
  221. return true;
  222. }
  223. bool XMLElement::SetVariantMap(const VariantMap& value)
  224. {
  225. if (!RemoveChildren("variant"))
  226. return false;
  227. for (VariantMap::ConstIterator i = value.Begin(); i != value.End(); ++i)
  228. {
  229. XMLElement variantElem = CreateChild("variant");
  230. if (!variantElem)
  231. return false;
  232. variantElem.SetInt("hash", i->first_.GetValue());
  233. variantElem.SetVariant(i->second_);
  234. }
  235. return true;
  236. }
  237. bool XMLElement::SetVector2(const String& name, const Vector2& value)
  238. {
  239. return SetAttribute(name, value.ToString());
  240. }
  241. bool XMLElement::SetVector3(const String& name, const Vector3& value)
  242. {
  243. return SetAttribute(name, value.ToString());
  244. }
  245. bool XMLElement::SetVector4(const String& name, const Vector4& value)
  246. {
  247. return SetAttribute(name, value.ToString());
  248. }
  249. String XMLElement::GetName() const
  250. {
  251. if (!file_ || !element_)
  252. return String();
  253. return String(element_->Value());
  254. }
  255. String XMLElement::GetText() const
  256. {
  257. if (!file_ || !element_)
  258. return String();
  259. TiXmlNode* node = element_->FirstChild();
  260. if (node)
  261. return String(node->Value());
  262. else
  263. return String();
  264. }
  265. bool XMLElement::HasChild(const String& name) const
  266. {
  267. if (!file_ || !element_)
  268. return false;
  269. if (element_->FirstChildElement(name.CString()) != 0)
  270. return true;
  271. else
  272. return false;
  273. }
  274. XMLElement XMLElement::GetChild(const String& name) const
  275. {
  276. if (!file_ || !element_)
  277. return XMLElement();
  278. else
  279. {
  280. if (name.Empty())
  281. return XMLElement(file_, element_->FirstChildElement());
  282. else
  283. return XMLElement(file_, element_->FirstChildElement(name.CString()));
  284. }
  285. }
  286. XMLElement XMLElement::GetNext(const String& name) const
  287. {
  288. if (!file_ || !element_)
  289. return XMLElement();
  290. if (name.Empty())
  291. return XMLElement(file_, element_->NextSiblingElement());
  292. else
  293. return XMLElement(file_, element_->NextSiblingElement(name.CString()));
  294. }
  295. XMLElement XMLElement::GetParent() const
  296. {
  297. if (!file_ || !element_)
  298. return XMLElement();
  299. return XMLElement(file_, dynamic_cast<TiXmlElement*>(element_->Parent()));
  300. }
  301. unsigned XMLElement::GetNumAttributes() const
  302. {
  303. unsigned ret = 0;
  304. if (file_ && element_)
  305. {
  306. const TiXmlAttribute* attribute = element_->FirstAttribute();
  307. while (attribute)
  308. {
  309. ++ret;
  310. attribute = attribute->Next();
  311. }
  312. }
  313. return ret;
  314. }
  315. bool XMLElement::HasAttribute(const String& name) const
  316. {
  317. if (!file_ || !element_)
  318. return false;
  319. if (element_->Attribute(name.CString()) != 0)
  320. return true;
  321. else
  322. return false;
  323. }
  324. String XMLElement::GetAttribute(const String& name) const
  325. {
  326. if (!file_ || !element_)
  327. return String();
  328. else
  329. {
  330. const char* data = element_->Attribute(name.CString());
  331. if (!data)
  332. return String();
  333. else
  334. return String(data);
  335. }
  336. }
  337. Vector<String> XMLElement::GetAttributeNames() const
  338. {
  339. Vector<String> ret;
  340. if (file_ && element_)
  341. {
  342. const TiXmlAttribute* attribute = element_->FirstAttribute();
  343. while (attribute)
  344. {
  345. ret.Push(String(attribute->Name()));
  346. attribute = attribute->Next();
  347. }
  348. }
  349. return ret;
  350. }
  351. bool XMLElement::GetBool(const String& name) const
  352. {
  353. return ToBool(GetAttribute(name));
  354. }
  355. BoundingBox XMLElement::GetBoundingBox() const
  356. {
  357. BoundingBox ret;
  358. ret.min_ = GetVector3("min");
  359. ret.max_ = GetVector3("max");
  360. ret.defined_ = true;
  361. return ret;
  362. }
  363. PODVector<unsigned char> XMLElement::GetBuffer(const String& name) const
  364. {
  365. PODVector<unsigned char> ret;
  366. Vector<String> bytes = GetAttribute(name).Split(' ');
  367. ret.Resize(bytes.Size());
  368. for (unsigned i = 0; i < bytes.Size(); ++i)
  369. ret[i] = ToInt(bytes[i]);
  370. return ret;
  371. }
  372. bool XMLElement::GetBuffer(const String& name, void* dest, unsigned size) const
  373. {
  374. PODVector<unsigned char> ret;
  375. Vector<String> bytes = GetAttribute(name).Split(' ');
  376. unsigned char* destBytes = (unsigned char*)dest;
  377. if (size < bytes.Size())
  378. return false;
  379. for (unsigned i = 0; i < bytes.Size(); ++i)
  380. destBytes[i] = ToInt(bytes[i]);
  381. return true;
  382. }
  383. Color XMLElement::GetColor(const String& name) const
  384. {
  385. return ToColor(GetAttribute(name));
  386. }
  387. float XMLElement::GetFloat(const String& name) const
  388. {
  389. return ToFloat(GetAttribute(name));
  390. }
  391. int XMLElement::GetInt(const String& name) const
  392. {
  393. return ToInt(GetAttribute(name));
  394. }
  395. IntRect XMLElement::GetIntRect(const String& name) const
  396. {
  397. return ToIntRect(GetAttribute(name));
  398. }
  399. IntVector2 XMLElement::GetIntVector2(const String& name) const
  400. {
  401. return ToIntVector2(GetAttribute(name));
  402. }
  403. Quaternion XMLElement::GetQuaternion(const String& name) const
  404. {
  405. return ToQuaternion(GetAttribute(name));
  406. }
  407. Rect XMLElement::GetRect(const String& name) const
  408. {
  409. return ToRect(GetAttribute(name));
  410. }
  411. String XMLElement::GetString(const String& name) const
  412. {
  413. return GetAttribute(name);
  414. }
  415. String XMLElement::GetStringLower(const String& name) const
  416. {
  417. return GetAttribute(name).ToLower();
  418. }
  419. String XMLElement::GetStringUpper(const String& name) const
  420. {
  421. return GetAttribute(name).ToUpper();
  422. }
  423. Variant XMLElement::GetVariant() const
  424. {
  425. VariantType type = Variant::GetTypeFromName(GetAttribute("type"));
  426. return GetVariantValue(type);
  427. }
  428. Variant XMLElement::GetVariantValue(VariantType type) const
  429. {
  430. Variant ret;
  431. if (type == VAR_RESOURCEREF)
  432. ret = GetResourceRef();
  433. else if (type == VAR_RESOURCEREFLIST)
  434. ret = GetResourceRefList();
  435. else if (type == VAR_VARIANTVECTOR)
  436. ret = GetVariantVector();
  437. else if (type == VAR_VARIANTMAP)
  438. ret = GetVariantMap();
  439. else
  440. ret.FromString(type, GetAttribute("value"));
  441. return ret;
  442. }
  443. ResourceRef XMLElement::GetResourceRef() const
  444. {
  445. ResourceRef ret;
  446. Vector<String> values = GetAttribute("value").Split(';');
  447. if (values.Size() == 2)
  448. {
  449. ret.type_ = ShortStringHash(values[0]);
  450. ret.id_ = StringHash(values[1]);
  451. // Whenever we encounter a resource name read from a ResourceRef XML element, store the reverse mapping to
  452. // ResourceCache if possible. We will probably use the hash to request a resource shortly afterward
  453. if (file_)
  454. file_->GetSubsystem<ResourceCache>()->StoreNameHash(values[1]);
  455. }
  456. return ret;
  457. }
  458. ResourceRefList XMLElement::GetResourceRefList() const
  459. {
  460. ResourceRefList ret;
  461. Vector<String> values = GetAttribute("value").Split(';');
  462. if (values.Size() >= 1)
  463. {
  464. // Whenever we encounter resource names read from a ResourceRefList XML element, store the reverse mapping to
  465. // ResourceCache if possible. We will probably use the hashes to request resources shortly afterward
  466. ResourceCache* cache = file_ ? file_->GetSubsystem<ResourceCache>() : 0;
  467. ret.type_ = ShortStringHash(values[0]);
  468. ret.ids_.Resize(values.Size() - 1);
  469. for (unsigned i = 1; i < values.Size(); ++i)
  470. {
  471. ret.ids_[i - 1] = StringHash(values[i]);
  472. if (cache)
  473. cache->StoreNameHash(values[i]);
  474. }
  475. }
  476. return ret;
  477. }
  478. VariantVector XMLElement::GetVariantVector() const
  479. {
  480. VariantVector ret;
  481. XMLElement variantElem = GetChild("variant");
  482. while (variantElem)
  483. {
  484. ret.Push(variantElem.GetVariant());
  485. variantElem = variantElem.GetNext("variant");
  486. }
  487. return ret;
  488. }
  489. VariantMap XMLElement::GetVariantMap() const
  490. {
  491. VariantMap ret;
  492. XMLElement variantElem = GetChild("variant");
  493. while (variantElem)
  494. {
  495. ShortStringHash key(variantElem.GetInt("hash"));
  496. ret[key] = variantElem.GetVariant();
  497. variantElem = variantElem.GetNext("variant");
  498. }
  499. return ret;
  500. }
  501. Vector2 XMLElement::GetVector2(const String& name) const
  502. {
  503. return ToVector2(GetAttribute(name));
  504. }
  505. Vector3 XMLElement::GetVector3(const String& name) const
  506. {
  507. return ToVector3(GetAttribute(name));
  508. }
  509. Vector4 XMLElement::GetVector4(const String& name) const
  510. {
  511. return ToVector4(GetAttribute(name));
  512. }
  513. Vector4 XMLElement::GetVector(const String& name) const
  514. {
  515. return ToVector4(GetAttribute(name), true);
  516. }
  517. XMLFile* XMLElement::GetFile() const
  518. {
  519. return file_;
  520. }