XMLElement.cpp 29 KB

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