XMLElement.cpp 16 KB

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