XMLElement.cpp 28 KB

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