XMLElement.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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. xpathResultSet_(0),
  35. xpathNode_(0),
  36. xpathResultIndex_(0)
  37. {
  38. }
  39. XMLElement::XMLElement(XMLFile* file, pugi::xml_node_struct* node) :
  40. file_(file),
  41. node_(node),
  42. xpathResultSet_(0),
  43. xpathNode_(0),
  44. xpathResultIndex_(0)
  45. {
  46. }
  47. XMLElement::XMLElement(const XPathResultSet* resultSet, const pugi::xpath_node* xpathNode, unsigned xpathResultIndex) :
  48. node_(0),
  49. xpathResultSet_(resultSet),
  50. xpathNode_(resultSet ? xpathNode : new pugi::xpath_node(xpathNode->attribute(), xpathNode->node())),
  51. xpathResultIndex_(xpathResultIndex)
  52. {
  53. }
  54. XMLElement::XMLElement(const XMLElement& rhs) :
  55. file_(rhs.file_),
  56. node_(rhs.node_),
  57. xpathResultSet_(rhs.xpathResultSet_),
  58. xpathNode_(rhs.xpathResultSet_ ? rhs.xpathNode_ : (rhs.xpathNode_ ? new pugi::xpath_node(rhs.xpathNode_->attribute(), rhs.xpathNode_->node()) : 0)),
  59. xpathResultIndex_(rhs.xpathResultIndex_)
  60. {
  61. }
  62. XMLElement::~XMLElement()
  63. {
  64. // XMLElement class takes the ownership of a single xpath_node object, so destruct it now
  65. if (!xpathResultSet_ && xpathNode_)
  66. {
  67. delete xpathNode_;
  68. xpathNode_ = 0;
  69. }
  70. }
  71. XMLElement& XMLElement::operator = (const XMLElement& rhs)
  72. {
  73. file_ = rhs.file_;
  74. node_ = rhs.node_;
  75. xpathResultSet_ = rhs.xpathResultSet_;
  76. xpathNode_ = rhs.xpathResultSet_ ? rhs.xpathNode_ : (rhs.xpathNode_ ? new pugi::xpath_node(rhs.xpathNode_->attribute(), rhs.xpathNode_->node()) : 0);
  77. xpathResultIndex_ = rhs.xpathResultIndex_;
  78. return *this;
  79. }
  80. XMLElement XMLElement::CreateChild(const String& name)
  81. {
  82. return CreateChild(name.CString());
  83. }
  84. XMLElement XMLElement::CreateChild(const char* name)
  85. {
  86. if ((!file_ || !node_) && !xpathNode_)
  87. return XMLElement();
  88. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node(): pugi::xml_node(node_);
  89. pugi::xml_node child = const_cast<pugi::xml_node&>(node).append_child(name);
  90. return XMLElement(file_, child.internal_object());
  91. }
  92. bool XMLElement::RemoveChild(const XMLElement& element)
  93. {
  94. if ((!element.node_ && !element.xpathNode_) || (!node_ && !xpathNode_))
  95. return false;
  96. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node(): pugi::xml_node(node_);
  97. const pugi::xml_node& child = element.xpathNode_ ? element.xpathNode_->node(): pugi::xml_node(element.node_);
  98. return const_cast<pugi::xml_node&>(node).remove_child(child);
  99. }
  100. bool XMLElement::RemoveChild(const String& name)
  101. {
  102. return RemoveChild(name.CString());
  103. }
  104. bool XMLElement::RemoveChild(const char* name)
  105. {
  106. if ((!file_ || !node_) && !xpathNode_)
  107. return false;
  108. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node(): pugi::xml_node(node_);
  109. return const_cast<pugi::xml_node&>(node).remove_child(name);
  110. }
  111. bool XMLElement::RemoveChildren(const String& name)
  112. {
  113. return RemoveChildren(name.CString());
  114. }
  115. bool XMLElement::RemoveChildren(const char* name)
  116. {
  117. if ((!file_ || !node_) && !xpathNode_)
  118. return false;
  119. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node(): pugi::xml_node(node_);
  120. if (!String::CStringLength(name))
  121. {
  122. for (;;)
  123. {
  124. pugi::xml_node child = node.last_child();
  125. if (child.empty())
  126. break;
  127. const_cast<pugi::xml_node&>(node).remove_child(child);
  128. }
  129. }
  130. else
  131. {
  132. for (;;)
  133. {
  134. pugi::xml_node child = node.child(name);
  135. if (child.empty())
  136. break;
  137. const_cast<pugi::xml_node&>(node).remove_child(child);
  138. }
  139. }
  140. return true;
  141. }
  142. XMLElement XMLElement::SelectSingle(const String& query, pugi::xpath_variable_set* variables)
  143. {
  144. if ((!file_ || !node_) && !xpathNode_)
  145. return XMLElement();
  146. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node(): pugi::xml_node(node_);
  147. pugi::xpath_node result = node.select_single_node(query.CString(), variables);
  148. return XMLElement(0, &result, 0);
  149. }
  150. XPathResultSet XMLElement::Select(const String& query, pugi::xpath_variable_set* variables)
  151. {
  152. if ((!file_ || !node_) && !xpathNode_)
  153. return XPathResultSet();
  154. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node(): pugi::xml_node(node_);
  155. pugi::xpath_node_set result = node.select_nodes(query.CString(), variables);
  156. return XPathResultSet(&result);
  157. }
  158. bool XMLElement::SetAttribute(const String& name, const String& value)
  159. {
  160. return SetAttribute(name.CString(), value.CString());
  161. }
  162. bool XMLElement::SetAttribute(const char* name, const char* value)
  163. {
  164. if ((!file_ || !node_) && !xpathNode_)
  165. return false;
  166. // If xpath_node contains just attribute, set its value regardless of the specified name
  167. if (xpathNode_ && xpathNode_->attribute())
  168. return xpathNode_->attribute().set_value(value);
  169. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node(): pugi::xml_node(node_);
  170. pugi::xml_attribute attr = node.attribute(name);
  171. if (attr.empty())
  172. attr = const_cast<pugi::xml_node&>(node).append_attribute(name);
  173. return attr.set_value(value);
  174. }
  175. bool XMLElement::SetAttribute(const String& value)
  176. {
  177. return SetAttribute(value.CString());
  178. }
  179. bool XMLElement::SetAttribute(const char* value)
  180. {
  181. // If xpath_node contains just attribute, set its value
  182. return xpathNode_ && xpathNode_->attribute() && xpathNode_->attribute().set_value(value);
  183. }
  184. bool XMLElement::SetBool(const String& name, bool value)
  185. {
  186. return SetAttribute(name, String(value));
  187. }
  188. bool XMLElement::SetBoundingBox(const BoundingBox& value)
  189. {
  190. if (!SetVector3("min", value.min_))
  191. return false;
  192. return SetVector3("max", value.max_);
  193. }
  194. bool XMLElement::SetBuffer(const String& name, const void* data, unsigned size)
  195. {
  196. String dataStr;
  197. const unsigned char* bytes = (const unsigned char*)data;
  198. for (unsigned i = 0; i < size; ++i)
  199. dataStr += String((unsigned)bytes[i]) + " ";
  200. return SetAttribute(name, dataStr);
  201. }
  202. bool XMLElement::SetBuffer(const String& name, const PODVector<unsigned char>& value)
  203. {
  204. if (!value.Size())
  205. return SetAttribute(name, String::EMPTY);
  206. else
  207. return SetBuffer(name, &value[0], value.Size());
  208. }
  209. bool XMLElement::SetColor(const String& name, const Color& value)
  210. {
  211. return SetAttribute(name, value.ToString());
  212. }
  213. bool XMLElement::SetFloat(const String& name, float value)
  214. {
  215. return SetAttribute(name, String(value));
  216. }
  217. bool XMLElement::SetUInt(const String& name, unsigned value)
  218. {
  219. return SetAttribute(name, String(value));
  220. }
  221. bool XMLElement::SetInt(const String& name, int value)
  222. {
  223. return SetAttribute(name, String(value));
  224. }
  225. bool XMLElement::SetIntRect(const String& name, const IntRect& value)
  226. {
  227. return SetAttribute(name, value.ToString());
  228. }
  229. bool XMLElement::SetIntVector2(const String& name, const IntVector2& value)
  230. {
  231. return SetAttribute(name, value.ToString());
  232. }
  233. bool XMLElement::SetRect(const String& name, const Rect& value)
  234. {
  235. return SetAttribute(name, value.ToString());
  236. }
  237. bool XMLElement::SetQuaternion(const String& name, const Quaternion& value)
  238. {
  239. return SetAttribute(name, value.ToString());
  240. }
  241. bool XMLElement::SetString(const String& name, const String& value)
  242. {
  243. return SetAttribute(name, value);
  244. }
  245. bool XMLElement::SetVariant(const Variant& value)
  246. {
  247. if (!SetAttribute("type", value.GetTypeName()))
  248. return false;
  249. return SetVariantValue(value);
  250. }
  251. bool XMLElement::SetVariantValue(const Variant& value)
  252. {
  253. switch (value.GetType())
  254. {
  255. case VAR_RESOURCEREF:
  256. return SetResourceRef(value.GetResourceRef());
  257. case VAR_RESOURCEREFLIST:
  258. return SetResourceRefList(value.GetResourceRefList());
  259. case VAR_VARIANTVECTOR:
  260. return SetVariantVector(value.GetVariantVector());
  261. case VAR_VARIANTMAP:
  262. return SetVariantMap(value.GetVariantMap());
  263. default:
  264. return SetAttribute("value", value.ToString().CString());
  265. }
  266. }
  267. bool XMLElement::SetResourceRef(const ResourceRef& value)
  268. {
  269. if ((!file_ || !node_) && !xpathNode_)
  270. return false;
  271. // Need the context & resource cache to query for reverse hash mappings
  272. Context* context = file_->GetContext();
  273. ResourceCache* cache = file_->GetSubsystem<ResourceCache>();
  274. return SetAttribute("value", String(context->GetTypeName(value.type_)) + ";" + cache->GetResourceName(value.id_));
  275. }
  276. bool XMLElement::SetResourceRefList(const ResourceRefList& value)
  277. {
  278. if ((!file_ || !node_) && !xpathNode_)
  279. return false;
  280. // Need the context & resource cache to query for reverse hash mappings
  281. Context* context = file_->GetContext();
  282. ResourceCache* cache = file_->GetSubsystem<ResourceCache>();
  283. String str(context->GetTypeName(value.type_));
  284. for (unsigned i = 0; i < value.ids_.Size(); ++i)
  285. {
  286. str += ";";
  287. str += cache->GetResourceName(value.ids_[i]);
  288. }
  289. return SetAttribute("value", str.CString());
  290. }
  291. bool XMLElement::SetVariantVector(const VariantVector& value)
  292. {
  293. // Must remove all existing variant child elements (if they exist) to not cause confusion
  294. if (!RemoveChildren("variant"))
  295. return false;
  296. for (VariantVector::ConstIterator i = value.Begin(); i != value.End(); ++i)
  297. {
  298. XMLElement variantElem = CreateChild("variant");
  299. if (!variantElem)
  300. return false;
  301. variantElem.SetVariant(*i);
  302. }
  303. return true;
  304. }
  305. bool XMLElement::SetVariantMap(const VariantMap& value)
  306. {
  307. if (!RemoveChildren("variant"))
  308. return false;
  309. for (VariantMap::ConstIterator i = value.Begin(); i != value.End(); ++i)
  310. {
  311. XMLElement variantElem = CreateChild("variant");
  312. if (!variantElem)
  313. return false;
  314. variantElem.SetInt("hash", i->first_.Value());
  315. variantElem.SetVariant(i->second_);
  316. }
  317. return true;
  318. }
  319. bool XMLElement::SetVector2(const String& name, const Vector2& value)
  320. {
  321. return SetAttribute(name, value.ToString());
  322. }
  323. bool XMLElement::SetVector3(const String& name, const Vector3& value)
  324. {
  325. return SetAttribute(name, value.ToString());
  326. }
  327. bool XMLElement::SetVector4(const String& name, const Vector4& value)
  328. {
  329. return SetAttribute(name, value.ToString());
  330. }
  331. String XMLElement::GetName() const
  332. {
  333. if ((!file_ || !node_) && !xpathNode_)
  334. return String();
  335. // If xpath_node contains just attribute, return its name instead
  336. if (xpathNode_ && xpathNode_->attribute())
  337. return String(xpathNode_->attribute().name());
  338. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  339. return String(node.name());
  340. }
  341. bool XMLElement::HasChild(const String& name) const
  342. {
  343. return HasChild(name.CString());
  344. }
  345. bool XMLElement::HasChild(const char* name) const
  346. {
  347. if ((!file_ || !node_) && !xpathNode_)
  348. return false;
  349. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  350. return !node.child(name).empty();
  351. }
  352. XMLElement XMLElement::GetChild(const String& name) const
  353. {
  354. return GetChild(name.CString());
  355. }
  356. XMLElement XMLElement::GetChild(const char* name) const
  357. {
  358. if ((!file_ || !node_) && !xpathNode_)
  359. return XMLElement();
  360. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  361. if (!String::CStringLength(name))
  362. return XMLElement(file_, node.first_child().internal_object());
  363. else
  364. return XMLElement(file_, node.child(name).internal_object());
  365. }
  366. XMLElement XMLElement::GetNext(const String& name) const
  367. {
  368. return GetNext(name.CString());
  369. }
  370. XMLElement XMLElement::GetNext(const char* name) const
  371. {
  372. if ((!file_ || !node_) && !xpathNode_)
  373. return XMLElement();
  374. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  375. if (!String::CStringLength(name))
  376. return XMLElement(file_, node.next_sibling().internal_object());
  377. else
  378. return XMLElement(file_, node.next_sibling(name).internal_object());
  379. }
  380. XMLElement XMLElement::GetParent() const
  381. {
  382. if ((!file_ || !node_) && !xpathNode_)
  383. return XMLElement();
  384. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  385. return XMLElement(file_, node.parent().internal_object());
  386. }
  387. unsigned XMLElement::GetNumAttributes() const
  388. {
  389. if ((!file_ || !node_) && !xpathNode_)
  390. return 0;
  391. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  392. unsigned ret = 0;
  393. pugi::xml_attribute attr = node.first_attribute();
  394. while (!attr.empty())
  395. {
  396. ++ret;
  397. attr = attr.next_attribute();
  398. }
  399. return ret;
  400. }
  401. bool XMLElement::HasAttribute(const String& name) const
  402. {
  403. return HasAttribute(name.CString());
  404. }
  405. bool XMLElement::HasAttribute(const char* name) const
  406. {
  407. if ((!file_ || !node_) && !xpathNode_)
  408. return false;
  409. // If xpath_node contains just attribute, check against it
  410. if (xpathNode_ && xpathNode_->attribute())
  411. return String(xpathNode_->attribute().name()) == name;
  412. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  413. return !node.attribute(name).empty();
  414. }
  415. String XMLElement::GetAttribute(const String& name) const
  416. {
  417. return String(GetAttribute(name.CString()));
  418. }
  419. const char* XMLElement::GetAttribute(const char* name) const
  420. {
  421. if ((!file_ || !node_) && !xpathNode_)
  422. return 0;
  423. // If xpath_node contains just attribute, return it regardless of the specified name
  424. if (xpathNode_ && xpathNode_->attribute())
  425. return xpathNode_->attribute().value();
  426. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  427. return node.attribute(name).value();
  428. }
  429. String XMLElement::GetAttributeLower(const String& name) const
  430. {
  431. return GetAttribute(name).ToLower();
  432. }
  433. String XMLElement::GetAttributeLower(const char* name) const
  434. {
  435. return String(GetAttribute(name)).ToLower();
  436. }
  437. String XMLElement::GetAttributeUpper(const String& name) const
  438. {
  439. return GetAttribute(name).ToUpper();
  440. }
  441. String XMLElement::GetAttributeUpper(const char* name) const
  442. {
  443. return String(GetAttribute(name)).ToUpper();
  444. }
  445. Vector<String> XMLElement::GetAttributeNames() const
  446. {
  447. if ((!file_ || !node_) && !xpathNode_)
  448. return Vector<String>();
  449. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  450. Vector<String> ret;
  451. pugi::xml_attribute attr = node.first_attribute();
  452. while (!attr.empty())
  453. {
  454. ret.Push(String(attr.name()));
  455. attr = attr.next_attribute();
  456. }
  457. return ret;
  458. }
  459. bool XMLElement::GetBool(const String& name) const
  460. {
  461. return ToBool(GetAttribute(name));
  462. }
  463. BoundingBox XMLElement::GetBoundingBox() const
  464. {
  465. BoundingBox ret;
  466. ret.min_ = GetVector3("min");
  467. ret.max_ = GetVector3("max");
  468. ret.defined_ = true;
  469. return ret;
  470. }
  471. PODVector<unsigned char> XMLElement::GetBuffer(const String& name) const
  472. {
  473. PODVector<unsigned char> ret;
  474. Vector<String> bytes = GetAttribute(name).Split(' ');
  475. ret.Resize(bytes.Size());
  476. for (unsigned i = 0; i < bytes.Size(); ++i)
  477. ret[i] = ToInt(bytes[i]);
  478. return ret;
  479. }
  480. bool XMLElement::GetBuffer(const String& name, void* dest, unsigned size) const
  481. {
  482. PODVector<unsigned char> ret;
  483. Vector<String> bytes = GetAttribute(name).Split(' ');
  484. unsigned char* destBytes = (unsigned char*)dest;
  485. if (size < bytes.Size())
  486. return false;
  487. for (unsigned i = 0; i < bytes.Size(); ++i)
  488. destBytes[i] = ToInt(bytes[i]);
  489. return true;
  490. }
  491. Color XMLElement::GetColor(const String& name) const
  492. {
  493. return ToColor(GetAttribute(name));
  494. }
  495. float XMLElement::GetFloat(const String& name) const
  496. {
  497. return ToFloat(GetAttribute(name));
  498. }
  499. unsigned XMLElement::GetUInt(const String& name) const
  500. {
  501. return ToUInt(GetAttribute(name));
  502. }
  503. int XMLElement::GetInt(const String& name) const
  504. {
  505. return ToInt(GetAttribute(name));
  506. }
  507. IntRect XMLElement::GetIntRect(const String& name) const
  508. {
  509. return ToIntRect(GetAttribute(name));
  510. }
  511. IntVector2 XMLElement::GetIntVector2(const String& name) const
  512. {
  513. return ToIntVector2(GetAttribute(name));
  514. }
  515. Quaternion XMLElement::GetQuaternion(const String& name) const
  516. {
  517. return ToQuaternion(GetAttribute(name));
  518. }
  519. Rect XMLElement::GetRect(const String& name) const
  520. {
  521. return ToRect(GetAttribute(name));
  522. }
  523. Variant XMLElement::GetVariant() const
  524. {
  525. VariantType type = Variant::GetTypeFromName(GetAttribute("type"));
  526. return GetVariantValue(type);
  527. }
  528. Variant XMLElement::GetVariantValue(VariantType type) const
  529. {
  530. Variant ret;
  531. if (type == VAR_RESOURCEREF)
  532. ret = GetResourceRef();
  533. else if (type == VAR_RESOURCEREFLIST)
  534. ret = GetResourceRefList();
  535. else if (type == VAR_VARIANTVECTOR)
  536. ret = GetVariantVector();
  537. else if (type == VAR_VARIANTMAP)
  538. ret = GetVariantMap();
  539. else
  540. ret.FromString(type, GetAttribute("value"));
  541. return ret;
  542. }
  543. ResourceRef XMLElement::GetResourceRef() const
  544. {
  545. ResourceRef ret;
  546. Vector<String> values = String::Split(GetAttribute("value"), ';');
  547. if (values.Size() == 2)
  548. {
  549. ret.type_ = values[0];
  550. ret.id_ = values[1];
  551. // Whenever we encounter a resource name read from a ResourceRef XML element, store the reverse mapping to
  552. // ResourceCache if possible. We will probably use the hash to request a resource shortly afterward
  553. if (file_)
  554. file_->GetSubsystem<ResourceCache>()->StoreNameHash(values[1]);
  555. }
  556. return ret;
  557. }
  558. ResourceRefList XMLElement::GetResourceRefList() const
  559. {
  560. ResourceRefList ret;
  561. Vector<String> values = String::Split(GetAttribute("value"), ';');
  562. if (values.Size() >= 1)
  563. {
  564. // Whenever we encounter resource names read from a ResourceRefList XML element, store the reverse mapping to
  565. // ResourceCache if possible. We will probably use the hashes to request resources shortly afterward
  566. ResourceCache* cache = file_ ? file_->GetSubsystem<ResourceCache>() : 0;
  567. ret.type_ = values[0];
  568. ret.ids_.Resize(values.Size() - 1);
  569. for (unsigned i = 1; i < values.Size(); ++i)
  570. {
  571. ret.ids_[i - 1] = StringHash(values[i]);
  572. if (cache)
  573. cache->StoreNameHash(values[i]);
  574. }
  575. }
  576. return ret;
  577. }
  578. VariantVector XMLElement::GetVariantVector() const
  579. {
  580. VariantVector ret;
  581. XMLElement variantElem = GetChild("variant");
  582. while (variantElem)
  583. {
  584. ret.Push(variantElem.GetVariant());
  585. variantElem = variantElem.GetNext("variant");
  586. }
  587. return ret;
  588. }
  589. VariantMap XMLElement::GetVariantMap() const
  590. {
  591. VariantMap ret;
  592. XMLElement variantElem = GetChild("variant");
  593. while (variantElem)
  594. {
  595. ShortStringHash key(variantElem.GetInt("hash"));
  596. ret[key] = variantElem.GetVariant();
  597. variantElem = variantElem.GetNext("variant");
  598. }
  599. return ret;
  600. }
  601. Vector2 XMLElement::GetVector2(const String& name) const
  602. {
  603. return ToVector2(GetAttribute(name));
  604. }
  605. Vector3 XMLElement::GetVector3(const String& name) const
  606. {
  607. return ToVector3(GetAttribute(name));
  608. }
  609. Vector4 XMLElement::GetVector4(const String& name) const
  610. {
  611. return ToVector4(GetAttribute(name));
  612. }
  613. Vector4 XMLElement::GetVector(const String& name) const
  614. {
  615. return ToVector4(GetAttribute(name), true);
  616. }
  617. XMLFile* XMLElement::GetFile() const
  618. {
  619. return file_;
  620. }
  621. XMLElement XMLElement::NextResult() const
  622. {
  623. if (!xpathResultSet_ || !xpathNode_)
  624. return XMLElement();
  625. return xpathResultSet_->operator [](++xpathResultIndex_);
  626. }
  627. XPathResultSet::XPathResultSet() :
  628. resultSet_(0)
  629. {
  630. }
  631. XPathResultSet::XPathResultSet(pugi::xpath_node_set* resultSet) :
  632. resultSet_(new pugi::xpath_node_set(resultSet->begin(), resultSet->end()))
  633. {
  634. // Sort the node set in forward document order
  635. resultSet_->sort();
  636. }
  637. XPathResultSet::XPathResultSet(const XPathResultSet& rhs) :
  638. resultSet_(new pugi::xpath_node_set(rhs.resultSet_->begin(), rhs.resultSet_->end()))
  639. {
  640. }
  641. XPathResultSet::~XPathResultSet()
  642. {
  643. delete resultSet_;
  644. resultSet_ = 0;
  645. }
  646. XPathResultSet& XPathResultSet::operator = (const XPathResultSet& rhs)
  647. {
  648. resultSet_ = new pugi::xpath_node_set(rhs.resultSet_->begin(), rhs.resultSet_->end());
  649. return *this;
  650. }
  651. XMLElement XPathResultSet::operator[](unsigned index) const
  652. {
  653. return index >= Size() ? XMLElement() : XMLElement(this, &resultSet_->operator [](index), index);
  654. }
  655. XMLElement XPathResultSet::FirstResult()
  656. {
  657. return Empty() ? XMLElement() : XMLElement(this, resultSet_->begin(), 0);
  658. }
  659. unsigned XPathResultSet::Size() const
  660. {
  661. return resultSet_ ? resultSet_->size() : 0;
  662. }
  663. bool XPathResultSet::Empty() const
  664. {
  665. return resultSet_ ? resultSet_->empty() : true;
  666. }
  667. XPathQuery::XPathQuery() :
  668. query_(0),
  669. variables_(0)
  670. {
  671. }
  672. XPathQuery::XPathQuery(const String& queryString) :
  673. queryString_(queryString),
  674. variables_(0)
  675. {
  676. query_ = new pugi::xpath_query(queryString_.CString(), variables_);
  677. }
  678. XPathQuery::~XPathQuery()
  679. {
  680. delete variables_;
  681. variables_ = 0;
  682. delete query_;
  683. query_ = 0;
  684. }
  685. void XPathQuery::Bind()
  686. {
  687. // Delete previous query object and create a new one binding it with variable set
  688. delete query_;
  689. query_ = new pugi::xpath_query(queryString_.CString(), variables_);
  690. }
  691. bool XPathQuery::SetVariable(const String& name, bool value)
  692. {
  693. if (!variables_)
  694. variables_ = new pugi::xpath_variable_set();
  695. return variables_->set(name.CString(), value);
  696. }
  697. bool XPathQuery::SetVariable(const String& name, float value)
  698. {
  699. if (!variables_)
  700. variables_ = new pugi::xpath_variable_set();
  701. return variables_->set(name.CString(), value);
  702. }
  703. bool XPathQuery::SetVariable(const String& name, const String& value)
  704. {
  705. if (!variables_)
  706. variables_ = new pugi::xpath_variable_set();
  707. return variables_->set(name.CString(), value.CString());
  708. }
  709. bool XPathQuery::SetVariable(const String& name, const XPathResultSet& value)
  710. {
  711. if (!variables_)
  712. variables_ = new pugi::xpath_variable_set();
  713. return variables_->set(name.CString(), value.GetXPathNodeSet());
  714. }
  715. void XPathQuery::RemoveVariables()
  716. {
  717. delete variables_;
  718. variables_ = 0;
  719. }
  720. void XPathQuery::SetQuery(const String& queryString, bool bind)
  721. {
  722. queryString_ = queryString;
  723. if (bind)
  724. Bind();
  725. }
  726. bool XPathQuery::EvaluateToBool(XMLElement element) const
  727. {
  728. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  729. return false;
  730. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node(): pugi::xml_node(element.GetNode());
  731. return query_->evaluate_boolean(node);
  732. }
  733. float XPathQuery::EvaluateToFloat(XMLElement element) const
  734. {
  735. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  736. return 0.0f;
  737. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node(): pugi::xml_node(element.GetNode());
  738. return query_->evaluate_number(node);
  739. }
  740. String XPathQuery::EvaluateToString(XMLElement element) const
  741. {
  742. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  743. return String::EMPTY;
  744. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node(): pugi::xml_node(element.GetNode());
  745. String result;
  746. result.Reserve(query_->evaluate_string(0, 0, node)); // First call get the size
  747. query_->evaluate_string(const_cast<pugi::char_t*>(result.CString()), result.Capacity(), node); // Second call get the actual string
  748. return result;
  749. }
  750. XPathResultSet XPathQuery::Evaluate(XMLElement element) const
  751. {
  752. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  753. return XPathResultSet();
  754. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node(): pugi::xml_node(element.GetNode());
  755. pugi::xpath_node_set result = query_->evaluate_node_set(node);
  756. return XPathResultSet(&result);
  757. }
  758. }