XMLElement.cpp 17 KB

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