XMLElement.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "ResourceCache.h"
  25. #include "StringUtils.h"
  26. #include "XMLFile.h"
  27. #include <pugixml.hpp>
  28. #include "DebugNew.h"
  29. namespace Urho3D
  30. {
  31. const XMLElement XMLElement::EMPTY;
  32. XMLElement::XMLElement() :
  33. node_(0)
  34. {
  35. }
  36. XMLElement::XMLElement(XMLFile* file, pugi::xml_node_struct* node) :
  37. file_(file),
  38. node_(node)
  39. {
  40. }
  41. XMLElement::XMLElement(const XMLElement& rhs) :
  42. file_(rhs.file_),
  43. node_(rhs.file_ ? rhs.node_ : 0)
  44. {
  45. }
  46. XMLElement::~XMLElement()
  47. {
  48. }
  49. XMLElement XMLElement::CreateChild(const String& name)
  50. {
  51. return CreateChild(name.CString());
  52. }
  53. XMLElement XMLElement::CreateChild(const char* name)
  54. {
  55. if (!file_ || !node_)
  56. return XMLElement();
  57. pugi::xml_node node(node_);
  58. pugi::xml_node child = node.append_child(name);
  59. return XMLElement(file_, child.internal_object());
  60. }
  61. bool XMLElement::RemoveChild(const XMLElement& element)
  62. {
  63. if (!file_ || !node_ || element.node_)
  64. return false;
  65. pugi::xml_node node(node_);
  66. pugi::xml_node child(element.node_);
  67. return node.remove_child(child);
  68. }
  69. bool XMLElement::RemoveChild(const String& name)
  70. {
  71. return RemoveChild(name.CString());
  72. }
  73. bool XMLElement::RemoveChild(const char* name)
  74. {
  75. if (!file_ || !node_)
  76. return false;
  77. pugi::xml_node node(node_);
  78. return node.remove_child(name);
  79. }
  80. bool XMLElement::RemoveChildren(const String& name)
  81. {
  82. return RemoveChildren(name.CString());
  83. }
  84. bool XMLElement::RemoveChildren(const char* name)
  85. {
  86. if (!file_ || !node_)
  87. return false;
  88. pugi::xml_node node(node_);
  89. if (!String::CStringLength(name))
  90. {
  91. for (;;)
  92. {
  93. pugi::xml_node child = node.last_child();
  94. if (child.empty())
  95. break;
  96. node.remove_child(child);
  97. }
  98. }
  99. else
  100. {
  101. for (;;)
  102. {
  103. pugi::xml_node child = node.child(name);
  104. if (child.empty())
  105. break;
  106. node.remove_child(child);
  107. }
  108. }
  109. return true;
  110. }
  111. bool XMLElement::SetAttribute(const String& name, const String& value)
  112. {
  113. return SetAttribute(name.CString(), value.CString());
  114. }
  115. bool XMLElement::SetAttribute(const char* name, const char* value)
  116. {
  117. if (!file_ || !node_)
  118. return false;
  119. pugi::xml_node node(node_);
  120. pugi::xml_attribute attr = node.attribute(name);
  121. if (attr.empty())
  122. attr = node.append_attribute(name);
  123. attr.set_value(value);
  124. return true;
  125. }
  126. bool XMLElement::SetBool(const String& name, bool value)
  127. {
  128. return SetAttribute(name, String(value));
  129. }
  130. bool XMLElement::SetBoundingBox(const BoundingBox& value)
  131. {
  132. if (!SetVector3("min", value.min_))
  133. return false;
  134. return SetVector3("max", value.max_);
  135. }
  136. bool XMLElement::SetBuffer(const String& name, const void* data, unsigned size)
  137. {
  138. String dataStr;
  139. const unsigned char* bytes = (const unsigned char*)data;
  140. for (unsigned i = 0; i < size; ++i)
  141. dataStr += String((unsigned)bytes[i]) + " ";
  142. return SetAttribute(name, dataStr);
  143. }
  144. bool XMLElement::SetBuffer(const String& name, const PODVector<unsigned char>& value)
  145. {
  146. if (!value.Size())
  147. return SetAttribute(name, String::EMPTY);
  148. else
  149. return SetBuffer(name, &value[0], value.Size());
  150. }
  151. bool XMLElement::SetColor(const String& name, const Color& value)
  152. {
  153. return SetAttribute(name, value.ToString());
  154. }
  155. bool XMLElement::SetFloat(const String& name, float value)
  156. {
  157. return SetAttribute(name, String(value));
  158. }
  159. bool XMLElement::SetInt(const String& name, int value)
  160. {
  161. return SetAttribute(name, String(value));
  162. }
  163. bool XMLElement::SetIntRect(const String& name, const IntRect& value)
  164. {
  165. return SetAttribute(name, value.ToString());
  166. }
  167. bool XMLElement::SetIntVector2(const String& name, const IntVector2& value)
  168. {
  169. return SetAttribute(name, value.ToString());
  170. }
  171. bool XMLElement::SetRect(const String& name, const Rect& value)
  172. {
  173. return SetAttribute(name, value.ToString());
  174. }
  175. bool XMLElement::SetQuaternion(const String& name, const Quaternion& value)
  176. {
  177. return XMLElement::SetAttribute(name, value.ToString());
  178. }
  179. bool XMLElement::SetString(const String& name, const String& value)
  180. {
  181. return SetAttribute(name, value);
  182. }
  183. bool XMLElement::SetVariant(const Variant& value)
  184. {
  185. if (!SetAttribute("type", value.GetTypeName()))
  186. return false;
  187. return SetVariantValue(value);
  188. }
  189. bool XMLElement::SetVariantValue(const Variant& value)
  190. {
  191. switch (value.GetType())
  192. {
  193. case VAR_RESOURCEREF:
  194. return SetResourceRef(value.GetResourceRef());
  195. case VAR_RESOURCEREFLIST:
  196. return SetResourceRefList(value.GetResourceRefList());
  197. case VAR_VARIANTVECTOR:
  198. return SetVariantVector(value.GetVariantVector());
  199. case VAR_VARIANTMAP:
  200. return SetVariantMap(value.GetVariantMap());
  201. default:
  202. return SetAttribute("value", value.ToString().CString());
  203. }
  204. }
  205. bool XMLElement::SetResourceRef(const ResourceRef& value)
  206. {
  207. if (!file_ || !node_)
  208. return false;
  209. // Need the context & resource cache to query for reverse hash mappings
  210. Context* context = file_->GetContext();
  211. ResourceCache* cache = file_->GetSubsystem<ResourceCache>();
  212. SetAttribute("value", String(context->GetTypeName(value.type_)) + ";" + cache->GetResourceName(value.id_));
  213. return true;
  214. }
  215. bool XMLElement::SetResourceRefList(const ResourceRefList& value)
  216. {
  217. if (!file_ || !node_)
  218. return false;
  219. // Need the context & resource cache to query for reverse hash mappings
  220. Context* context = file_->GetContext();
  221. ResourceCache* cache = file_->GetSubsystem<ResourceCache>();
  222. String str(context->GetTypeName(value.type_));
  223. for (unsigned i = 0; i < value.ids_.Size(); ++i)
  224. {
  225. str += ";";
  226. str += cache->GetResourceName(value.ids_[i]);
  227. }
  228. SetAttribute("value", str.CString());
  229. return true;
  230. }
  231. bool XMLElement::SetVariantVector(const VariantVector& value)
  232. {
  233. // Must remove all existing variant child elements (if they exist) to not cause confusion
  234. if (!RemoveChildren("variant"))
  235. return false;
  236. for (VariantVector::ConstIterator i = value.Begin(); i != value.End(); ++i)
  237. {
  238. XMLElement variantElem = CreateChild("variant");
  239. if (!variantElem)
  240. return false;
  241. variantElem.SetVariant(*i);
  242. }
  243. return true;
  244. }
  245. bool XMLElement::SetVariantMap(const VariantMap& value)
  246. {
  247. if (!RemoveChildren("variant"))
  248. return false;
  249. for (VariantMap::ConstIterator i = value.Begin(); i != value.End(); ++i)
  250. {
  251. XMLElement variantElem = CreateChild("variant");
  252. if (!variantElem)
  253. return false;
  254. variantElem.SetInt("hash", i->first_.Value());
  255. variantElem.SetVariant(i->second_);
  256. }
  257. return true;
  258. }
  259. bool XMLElement::SetVector2(const String& name, const Vector2& value)
  260. {
  261. return SetAttribute(name, value.ToString());
  262. }
  263. bool XMLElement::SetVector3(const String& name, const Vector3& value)
  264. {
  265. return SetAttribute(name, value.ToString());
  266. }
  267. bool XMLElement::SetVector4(const String& name, const Vector4& value)
  268. {
  269. return SetAttribute(name, value.ToString());
  270. }
  271. String XMLElement::GetName() const
  272. {
  273. if (!file_ || !node_)
  274. return String();
  275. pugi::xml_node node(node_);
  276. return String(node.name());
  277. }
  278. bool XMLElement::HasChild(const String& name) const
  279. {
  280. return HasChild(name.CString());
  281. }
  282. bool XMLElement::HasChild(const char* name) const
  283. {
  284. if (!file_ || !node_)
  285. return false;
  286. pugi::xml_node node(node_);
  287. return !node.child(name).empty();
  288. }
  289. XMLElement XMLElement::GetChild(const String& name) const
  290. {
  291. return GetChild(name.CString());
  292. }
  293. XMLElement XMLElement::GetChild(const char* name) const
  294. {
  295. if (!file_ || !node_)
  296. return XMLElement();
  297. pugi::xml_node node(node_);
  298. if (!String::CStringLength(name))
  299. return XMLElement(file_, node.first_child().internal_object());
  300. else
  301. return XMLElement(file_, node.child(name).internal_object());
  302. }
  303. XMLElement XMLElement::GetNext(const String& name) const
  304. {
  305. return GetNext(name.CString());
  306. }
  307. XMLElement XMLElement::GetNext(const char* name) const
  308. {
  309. if (!file_ || !node_)
  310. return XMLElement();
  311. pugi::xml_node node(node_);
  312. if (!String::CStringLength(name))
  313. return XMLElement(file_, node.next_sibling().internal_object());
  314. else
  315. return XMLElement(file_, node.next_sibling(name).internal_object());
  316. }
  317. XMLElement XMLElement::GetParent() const
  318. {
  319. if (!file_ || !node_)
  320. return XMLElement();
  321. pugi::xml_node node(node_);
  322. return XMLElement(file_, node.parent().internal_object());
  323. }
  324. unsigned XMLElement::GetNumAttributes() const
  325. {
  326. if (!file_ || !node_)
  327. return 0;
  328. pugi::xml_node node(node_);
  329. unsigned ret = 0;
  330. pugi::xml_attribute attr = node.first_attribute();
  331. while (!attr.empty())
  332. {
  333. ++ret;
  334. attr = attr.next_attribute();
  335. }
  336. return ret;
  337. }
  338. bool XMLElement::HasAttribute(const String& name) const
  339. {
  340. return HasAttribute(name.CString());
  341. }
  342. bool XMLElement::HasAttribute(const char* name) const
  343. {
  344. if (!file_ || !node_)
  345. return false;
  346. pugi::xml_node node(node_);
  347. return !node.attribute(name).empty();
  348. }
  349. String XMLElement::GetAttribute(const String& name) const
  350. {
  351. if (!file_ || !node_)
  352. return String();
  353. pugi::xml_node node(node_);
  354. return String(node.attribute(name.CString()).value());
  355. }
  356. const char* XMLElement::GetAttribute(const char* name) const
  357. {
  358. if (!file_ || !node_)
  359. return 0;
  360. pugi::xml_node node(node_);
  361. return node.attribute(name).value();
  362. }
  363. String XMLElement::GetAttributeLower(const String& name) const
  364. {
  365. return GetAttribute(name).ToLower();
  366. }
  367. String XMLElement::GetAttributeLower(const char* name) const
  368. {
  369. return String(GetAttribute(name)).ToLower();
  370. }
  371. String XMLElement::GetAttributeUpper(const String& name) const
  372. {
  373. return GetAttribute(name).ToUpper();
  374. }
  375. String XMLElement::GetAttributeUpper(const char* name) const
  376. {
  377. return String(GetAttribute(name)).ToUpper();
  378. }
  379. Vector<String> XMLElement::GetAttributeNames() const
  380. {
  381. if (!file_ || !node_)
  382. return Vector<String>();
  383. pugi::xml_node node(node_);
  384. Vector<String> ret;
  385. pugi::xml_attribute attr = node.first_attribute();
  386. while (!attr.empty())
  387. {
  388. ret.Push(String(attr.name()));
  389. attr = attr.next_attribute();
  390. }
  391. return ret;
  392. }
  393. bool XMLElement::GetBool(const String& name) const
  394. {
  395. return ToBool(GetAttribute(name));
  396. }
  397. BoundingBox XMLElement::GetBoundingBox() const
  398. {
  399. BoundingBox ret;
  400. ret.min_ = GetVector3("min");
  401. ret.max_ = GetVector3("max");
  402. ret.defined_ = true;
  403. return ret;
  404. }
  405. PODVector<unsigned char> XMLElement::GetBuffer(const String& name) const
  406. {
  407. PODVector<unsigned char> ret;
  408. Vector<String> bytes = GetAttribute(name).Split(' ');
  409. ret.Resize(bytes.Size());
  410. for (unsigned i = 0; i < bytes.Size(); ++i)
  411. ret[i] = ToInt(bytes[i]);
  412. return ret;
  413. }
  414. bool XMLElement::GetBuffer(const String& name, void* dest, unsigned size) const
  415. {
  416. PODVector<unsigned char> ret;
  417. Vector<String> bytes = GetAttribute(name).Split(' ');
  418. unsigned char* destBytes = (unsigned char*)dest;
  419. if (size < bytes.Size())
  420. return false;
  421. for (unsigned i = 0; i < bytes.Size(); ++i)
  422. destBytes[i] = ToInt(bytes[i]);
  423. return true;
  424. }
  425. Color XMLElement::GetColor(const String& name) const
  426. {
  427. return ToColor(GetAttribute(name));
  428. }
  429. float XMLElement::GetFloat(const String& name) const
  430. {
  431. return ToFloat(GetAttribute(name));
  432. }
  433. int XMLElement::GetInt(const String& name) const
  434. {
  435. return ToInt(GetAttribute(name));
  436. }
  437. IntRect XMLElement::GetIntRect(const String& name) const
  438. {
  439. return ToIntRect(GetAttribute(name));
  440. }
  441. IntVector2 XMLElement::GetIntVector2(const String& name) const
  442. {
  443. return ToIntVector2(GetAttribute(name));
  444. }
  445. Quaternion XMLElement::GetQuaternion(const String& name) const
  446. {
  447. return ToQuaternion(GetAttribute(name));
  448. }
  449. Rect XMLElement::GetRect(const String& name) const
  450. {
  451. return ToRect(GetAttribute(name));
  452. }
  453. Variant XMLElement::GetVariant() const
  454. {
  455. VariantType type = Variant::GetTypeFromName(GetAttribute("type"));
  456. return GetVariantValue(type);
  457. }
  458. Variant XMLElement::GetVariantValue(VariantType type) const
  459. {
  460. Variant ret;
  461. if (type == VAR_RESOURCEREF)
  462. ret = GetResourceRef();
  463. else if (type == VAR_RESOURCEREFLIST)
  464. ret = GetResourceRefList();
  465. else if (type == VAR_VARIANTVECTOR)
  466. ret = GetVariantVector();
  467. else if (type == VAR_VARIANTMAP)
  468. ret = GetVariantMap();
  469. else
  470. ret.FromString(type, GetAttribute("value"));
  471. return ret;
  472. }
  473. ResourceRef XMLElement::GetResourceRef() const
  474. {
  475. ResourceRef ret;
  476. Vector<String> values = String::Split(GetAttribute("value"), ';');
  477. if (values.Size() == 2)
  478. {
  479. ret.type_ = ShortStringHash(values[0]);
  480. ret.id_ = StringHash(values[1]);
  481. // Whenever we encounter a resource name read from a ResourceRef XML element, store the reverse mapping to
  482. // ResourceCache if possible. We will probably use the hash to request a resource shortly afterward
  483. if (file_)
  484. file_->GetSubsystem<ResourceCache>()->StoreNameHash(values[1]);
  485. }
  486. return ret;
  487. }
  488. ResourceRefList XMLElement::GetResourceRefList() const
  489. {
  490. ResourceRefList ret;
  491. Vector<String> values = String::Split(GetAttribute("value"), ';');
  492. if (values.Size() >= 1)
  493. {
  494. // Whenever we encounter resource names read from a ResourceRefList XML element, store the reverse mapping to
  495. // ResourceCache if possible. We will probably use the hashes to request resources shortly afterward
  496. ResourceCache* cache = file_ ? file_->GetSubsystem<ResourceCache>() : 0;
  497. ret.type_ = ShortStringHash(values[0]);
  498. ret.ids_.Resize(values.Size() - 1);
  499. for (unsigned i = 1; i < values.Size(); ++i)
  500. {
  501. ret.ids_[i - 1] = StringHash(values[i]);
  502. if (cache)
  503. cache->StoreNameHash(values[i]);
  504. }
  505. }
  506. return ret;
  507. }
  508. VariantVector XMLElement::GetVariantVector() const
  509. {
  510. VariantVector ret;
  511. XMLElement variantElem = GetChild("variant");
  512. while (variantElem)
  513. {
  514. ret.Push(variantElem.GetVariant());
  515. variantElem = variantElem.GetNext("variant");
  516. }
  517. return ret;
  518. }
  519. VariantMap XMLElement::GetVariantMap() const
  520. {
  521. VariantMap ret;
  522. XMLElement variantElem = GetChild("variant");
  523. while (variantElem)
  524. {
  525. ShortStringHash key(variantElem.GetInt("hash"));
  526. ret[key] = variantElem.GetVariant();
  527. variantElem = variantElem.GetNext("variant");
  528. }
  529. return ret;
  530. }
  531. Vector2 XMLElement::GetVector2(const String& name) const
  532. {
  533. return ToVector2(GetAttribute(name));
  534. }
  535. Vector3 XMLElement::GetVector3(const String& name) const
  536. {
  537. return ToVector3(GetAttribute(name));
  538. }
  539. Vector4 XMLElement::GetVector4(const String& name) const
  540. {
  541. return ToVector4(GetAttribute(name));
  542. }
  543. Vector4 XMLElement::GetVector(const String& name) const
  544. {
  545. return ToVector4(GetAttribute(name), true);
  546. }
  547. XMLFile* XMLElement::GetFile() const
  548. {
  549. return file_;
  550. }
  551. }