XMLElement.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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. BufferToString(dataStr, data, size);
  230. return SetAttribute(name, dataStr);
  231. }
  232. bool XMLElement::SetBuffer(const String& name, const PODVector<unsigned char>& value)
  233. {
  234. if (!value.Size())
  235. return SetAttribute(name, String::EMPTY);
  236. else
  237. return SetBuffer(name, &value[0], value.Size());
  238. }
  239. bool XMLElement::SetColor(const String& name, const Color& value)
  240. {
  241. return SetAttribute(name, value.ToString());
  242. }
  243. bool XMLElement::SetFloat(const String& name, float value)
  244. {
  245. return SetAttribute(name, String(value));
  246. }
  247. bool XMLElement::SetUInt(const String& name, unsigned value)
  248. {
  249. return SetAttribute(name, String(value));
  250. }
  251. bool XMLElement::SetInt(const String& name, int value)
  252. {
  253. return SetAttribute(name, String(value));
  254. }
  255. bool XMLElement::SetIntRect(const String& name, const IntRect& value)
  256. {
  257. return SetAttribute(name, value.ToString());
  258. }
  259. bool XMLElement::SetIntVector2(const String& name, const IntVector2& value)
  260. {
  261. return SetAttribute(name, value.ToString());
  262. }
  263. bool XMLElement::SetRect(const String& name, const Rect& value)
  264. {
  265. return SetAttribute(name, value.ToString());
  266. }
  267. bool XMLElement::SetQuaternion(const String& name, const Quaternion& value)
  268. {
  269. return SetAttribute(name, value.ToString());
  270. }
  271. bool XMLElement::SetString(const String& name, const String& value)
  272. {
  273. return SetAttribute(name, value);
  274. }
  275. bool XMLElement::SetVariant(const Variant& value)
  276. {
  277. if (!SetAttribute("type", value.GetTypeName()))
  278. return false;
  279. return SetVariantValue(value);
  280. }
  281. bool XMLElement::SetVariantValue(const Variant& value)
  282. {
  283. switch (value.GetType())
  284. {
  285. case VAR_RESOURCEREF:
  286. return SetResourceRef(value.GetResourceRef());
  287. case VAR_RESOURCEREFLIST:
  288. return SetResourceRefList(value.GetResourceRefList());
  289. case VAR_VARIANTVECTOR:
  290. return SetVariantVector(value.GetVariantVector());
  291. case VAR_VARIANTMAP:
  292. return SetVariantMap(value.GetVariantMap());
  293. default:
  294. return SetAttribute("value", value.ToString().CString());
  295. }
  296. }
  297. bool XMLElement::SetResourceRef(const ResourceRef& value)
  298. {
  299. if (!file_ || (!node_ && !xpathNode_))
  300. return false;
  301. // Need the context & resource cache to query for reverse hash mappings
  302. Context* context = file_->GetContext();
  303. ResourceCache* cache = file_->GetSubsystem<ResourceCache>();
  304. return SetAttribute("value", String(context->GetTypeName(value.type_)) + ";" + cache->GetResourceName(value.id_));
  305. }
  306. bool XMLElement::SetResourceRefList(const ResourceRefList& value)
  307. {
  308. if (!file_ || (!node_ && !xpathNode_))
  309. return false;
  310. // Need the context & resource cache to query for reverse hash mappings
  311. Context* context = file_->GetContext();
  312. ResourceCache* cache = file_->GetSubsystem<ResourceCache>();
  313. String str(context->GetTypeName(value.type_));
  314. for (unsigned i = 0; i < value.ids_.Size(); ++i)
  315. {
  316. str += ";";
  317. str += cache->GetResourceName(value.ids_[i]);
  318. }
  319. return SetAttribute("value", str.CString());
  320. }
  321. bool XMLElement::SetVariantVector(const VariantVector& value)
  322. {
  323. // Must remove all existing variant child elements (if they exist) to not cause confusion
  324. if (!RemoveChildren("variant"))
  325. return false;
  326. for (VariantVector::ConstIterator i = value.Begin(); i != value.End(); ++i)
  327. {
  328. XMLElement variantElem = CreateChild("variant");
  329. if (!variantElem)
  330. return false;
  331. variantElem.SetVariant(*i);
  332. }
  333. return true;
  334. }
  335. bool XMLElement::SetVariantMap(const VariantMap& value)
  336. {
  337. if (!RemoveChildren("variant"))
  338. return false;
  339. for (VariantMap::ConstIterator i = value.Begin(); i != value.End(); ++i)
  340. {
  341. XMLElement variantElem = CreateChild("variant");
  342. if (!variantElem)
  343. return false;
  344. variantElem.SetInt("hash", i->first_.Value());
  345. variantElem.SetVariant(i->second_);
  346. }
  347. return true;
  348. }
  349. bool XMLElement::SetVector2(const String& name, const Vector2& value)
  350. {
  351. return SetAttribute(name, value.ToString());
  352. }
  353. bool XMLElement::SetVector3(const String& name, const Vector3& value)
  354. {
  355. return SetAttribute(name, value.ToString());
  356. }
  357. bool XMLElement::SetVector4(const String& name, const Vector4& value)
  358. {
  359. return SetAttribute(name, value.ToString());
  360. }
  361. bool XMLElement::IsNull() const
  362. {
  363. return !NotNull();
  364. }
  365. bool XMLElement::NotNull() const
  366. {
  367. return node_ || (xpathNode_ && !xpathNode_->operator !());
  368. }
  369. XMLElement::operator bool () const
  370. {
  371. return NotNull();
  372. }
  373. String XMLElement::GetName() const
  374. {
  375. if ((!file_ || !node_) && !xpathNode_)
  376. return String();
  377. // If xpath_node contains just attribute, return its name instead
  378. if (xpathNode_ && xpathNode_->attribute())
  379. return String(xpathNode_->attribute().name());
  380. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  381. return String(node.name());
  382. }
  383. bool XMLElement::HasChild(const String& name) const
  384. {
  385. return HasChild(name.CString());
  386. }
  387. bool XMLElement::HasChild(const char* name) const
  388. {
  389. if (!file_ || (!node_ && !xpathNode_))
  390. return false;
  391. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  392. return !node.child(name).empty();
  393. }
  394. XMLElement XMLElement::GetChild(const String& name) const
  395. {
  396. return GetChild(name.CString());
  397. }
  398. XMLElement XMLElement::GetChild(const char* name) const
  399. {
  400. if (!file_ || (!node_ && !xpathNode_))
  401. return XMLElement();
  402. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  403. if (!String::CStringLength(name))
  404. return XMLElement(file_, node.first_child().internal_object());
  405. else
  406. return XMLElement(file_, node.child(name).internal_object());
  407. }
  408. XMLElement XMLElement::GetNext(const String& name) const
  409. {
  410. return GetNext(name.CString());
  411. }
  412. XMLElement XMLElement::GetNext(const char* name) const
  413. {
  414. if (!file_ || (!node_ && !xpathNode_))
  415. return XMLElement();
  416. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  417. if (!String::CStringLength(name))
  418. return XMLElement(file_, node.next_sibling().internal_object());
  419. else
  420. return XMLElement(file_, node.next_sibling(name).internal_object());
  421. }
  422. XMLElement XMLElement::GetParent() const
  423. {
  424. if (!file_ || (!node_ && !xpathNode_))
  425. return XMLElement();
  426. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  427. return XMLElement(file_, node.parent().internal_object());
  428. }
  429. unsigned XMLElement::GetNumAttributes() const
  430. {
  431. if (!file_ || (!node_ && !xpathNode_))
  432. return 0;
  433. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  434. unsigned ret = 0;
  435. pugi::xml_attribute attr = node.first_attribute();
  436. while (!attr.empty())
  437. {
  438. ++ret;
  439. attr = attr.next_attribute();
  440. }
  441. return ret;
  442. }
  443. bool XMLElement::HasAttribute(const String& name) const
  444. {
  445. return HasAttribute(name.CString());
  446. }
  447. bool XMLElement::HasAttribute(const char* name) const
  448. {
  449. if (!file_ || (!node_ && !xpathNode_))
  450. return false;
  451. // If xpath_node contains just attribute, check against it
  452. if (xpathNode_ && xpathNode_->attribute())
  453. return String(xpathNode_->attribute().name()) == name;
  454. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  455. return !node.attribute(name).empty();
  456. }
  457. String XMLElement::GetAttribute(const String& name) const
  458. {
  459. return String(GetAttribute(name.CString()));
  460. }
  461. String XMLElement::GetAttribute(const char* name) const
  462. {
  463. if (!file_ || (!node_ && !xpathNode_))
  464. return String::EMPTY;
  465. // If xpath_node contains just attribute, return it regardless of the specified name
  466. if (xpathNode_ && xpathNode_->attribute())
  467. return xpathNode_->attribute().value();
  468. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  469. return node.attribute(name).value();
  470. }
  471. String XMLElement::GetAttributeLower(const String& name) const
  472. {
  473. return GetAttribute(name).ToLower();
  474. }
  475. String XMLElement::GetAttributeLower(const char* name) const
  476. {
  477. return String(GetAttribute(name)).ToLower();
  478. }
  479. String XMLElement::GetAttributeUpper(const String& name) const
  480. {
  481. return GetAttribute(name).ToUpper();
  482. }
  483. String XMLElement::GetAttributeUpper(const char* name) const
  484. {
  485. return String(GetAttribute(name)).ToUpper();
  486. }
  487. Vector<String> XMLElement::GetAttributeNames() const
  488. {
  489. if (!file_ || (!node_ && !xpathNode_))
  490. return Vector<String>();
  491. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  492. Vector<String> ret;
  493. pugi::xml_attribute attr = node.first_attribute();
  494. while (!attr.empty())
  495. {
  496. ret.Push(String(attr.name()));
  497. attr = attr.next_attribute();
  498. }
  499. return ret;
  500. }
  501. bool XMLElement::GetBool(const String& name) const
  502. {
  503. return ToBool(GetAttribute(name));
  504. }
  505. BoundingBox XMLElement::GetBoundingBox() const
  506. {
  507. BoundingBox ret;
  508. ret.min_ = GetVector3("min");
  509. ret.max_ = GetVector3("max");
  510. ret.defined_ = true;
  511. return ret;
  512. }
  513. PODVector<unsigned char> XMLElement::GetBuffer(const String& name) const
  514. {
  515. PODVector<unsigned char> ret;
  516. StringToBuffer(ret, GetAttribute(name));
  517. return ret;
  518. }
  519. bool XMLElement::GetBuffer(const String& name, void* dest, unsigned size) const
  520. {
  521. PODVector<unsigned char> ret;
  522. Vector<String> bytes = GetAttribute(name).Split(' ');
  523. unsigned char* destBytes = (unsigned char*)dest;
  524. if (size < bytes.Size())
  525. return false;
  526. for (unsigned i = 0; i < bytes.Size(); ++i)
  527. destBytes[i] = ToInt(bytes[i]);
  528. return true;
  529. }
  530. Color XMLElement::GetColor(const String& name) const
  531. {
  532. return ToColor(GetAttribute(name));
  533. }
  534. float XMLElement::GetFloat(const String& name) const
  535. {
  536. return ToFloat(GetAttribute(name));
  537. }
  538. unsigned XMLElement::GetUInt(const String& name) const
  539. {
  540. return ToUInt(GetAttribute(name));
  541. }
  542. int XMLElement::GetInt(const String& name) const
  543. {
  544. return ToInt(GetAttribute(name));
  545. }
  546. IntRect XMLElement::GetIntRect(const String& name) const
  547. {
  548. return ToIntRect(GetAttribute(name));
  549. }
  550. IntVector2 XMLElement::GetIntVector2(const String& name) const
  551. {
  552. return ToIntVector2(GetAttribute(name));
  553. }
  554. Quaternion XMLElement::GetQuaternion(const String& name) const
  555. {
  556. return ToQuaternion(GetAttribute(name));
  557. }
  558. Rect XMLElement::GetRect(const String& name) const
  559. {
  560. return ToRect(GetAttribute(name));
  561. }
  562. Variant XMLElement::GetVariant() const
  563. {
  564. VariantType type = Variant::GetTypeFromName(GetAttribute("type"));
  565. return GetVariantValue(type);
  566. }
  567. Variant XMLElement::GetVariantValue(VariantType type) const
  568. {
  569. Variant ret;
  570. if (type == VAR_RESOURCEREF)
  571. ret = GetResourceRef();
  572. else if (type == VAR_RESOURCEREFLIST)
  573. ret = GetResourceRefList();
  574. else if (type == VAR_VARIANTVECTOR)
  575. ret = GetVariantVector();
  576. else if (type == VAR_VARIANTMAP)
  577. ret = GetVariantMap();
  578. else
  579. ret.FromString(type, GetAttribute("value"));
  580. return ret;
  581. }
  582. ResourceRef XMLElement::GetResourceRef() const
  583. {
  584. ResourceRef ret;
  585. Vector<String> values = GetAttribute("value").Split(';');
  586. if (values.Size() == 2)
  587. {
  588. ret.type_ = values[0];
  589. ret.id_ = values[1];
  590. // Whenever we encounter a resource name read from a ResourceRef XML element, store the reverse mapping to
  591. // ResourceCache if possible. We will probably use the hash to request a resource shortly afterward
  592. if (file_)
  593. file_->GetSubsystem<ResourceCache>()->StoreNameHash(values[1]);
  594. }
  595. return ret;
  596. }
  597. ResourceRefList XMLElement::GetResourceRefList() const
  598. {
  599. ResourceRefList ret;
  600. Vector<String> values = GetAttribute("value").Split(';');
  601. if (values.Size() >= 1)
  602. {
  603. // Whenever we encounter resource names read from a ResourceRefList XML element, store the reverse mapping to
  604. // ResourceCache if possible. We will probably use the hashes to request resources shortly afterward
  605. ResourceCache* cache = file_ ? file_->GetSubsystem<ResourceCache>() : 0;
  606. ret.type_ = values[0];
  607. ret.ids_.Resize(values.Size() - 1);
  608. for (unsigned i = 1; i < values.Size(); ++i)
  609. {
  610. ret.ids_[i - 1] = StringHash(values[i]);
  611. if (cache)
  612. cache->StoreNameHash(values[i]);
  613. }
  614. }
  615. return ret;
  616. }
  617. VariantVector XMLElement::GetVariantVector() const
  618. {
  619. VariantVector ret;
  620. XMLElement variantElem = GetChild("variant");
  621. while (variantElem)
  622. {
  623. ret.Push(variantElem.GetVariant());
  624. variantElem = variantElem.GetNext("variant");
  625. }
  626. return ret;
  627. }
  628. VariantMap XMLElement::GetVariantMap() const
  629. {
  630. VariantMap ret;
  631. XMLElement variantElem = GetChild("variant");
  632. while (variantElem)
  633. {
  634. ShortStringHash key(variantElem.GetInt("hash"));
  635. ret[key] = variantElem.GetVariant();
  636. variantElem = variantElem.GetNext("variant");
  637. }
  638. return ret;
  639. }
  640. Vector2 XMLElement::GetVector2(const String& name) const
  641. {
  642. return ToVector2(GetAttribute(name));
  643. }
  644. Vector3 XMLElement::GetVector3(const String& name) const
  645. {
  646. return ToVector3(GetAttribute(name));
  647. }
  648. Vector4 XMLElement::GetVector4(const String& name) const
  649. {
  650. return ToVector4(GetAttribute(name));
  651. }
  652. Vector4 XMLElement::GetVector(const String& name) const
  653. {
  654. return ToVector4(GetAttribute(name), true);
  655. }
  656. XMLFile* XMLElement::GetFile() const
  657. {
  658. return file_;
  659. }
  660. XMLElement XMLElement::NextResult() const
  661. {
  662. if (!xpathResultSet_ || !xpathNode_)
  663. return XMLElement();
  664. return xpathResultSet_->operator [](++xpathResultIndex_);
  665. }
  666. XPathResultSet::XPathResultSet() :
  667. resultSet_(0)
  668. {
  669. }
  670. XPathResultSet::XPathResultSet(XMLFile* file, pugi::xpath_node_set* resultSet) :
  671. file_(file),
  672. resultSet_(resultSet ? new pugi::xpath_node_set(resultSet->begin(), resultSet->end()) : 0)
  673. {
  674. // Sort the node set in forward document order
  675. if (resultSet_)
  676. resultSet_->sort();
  677. }
  678. XPathResultSet::XPathResultSet(const XPathResultSet& rhs) :
  679. file_(rhs.file_),
  680. resultSet_(rhs.resultSet_ ? new pugi::xpath_node_set(rhs.resultSet_->begin(), rhs.resultSet_->end()) : 0)
  681. {
  682. }
  683. XPathResultSet::~XPathResultSet()
  684. {
  685. delete resultSet_;
  686. resultSet_ = 0;
  687. }
  688. XPathResultSet& XPathResultSet::operator = (const XPathResultSet& rhs)
  689. {
  690. file_ = rhs.file_;
  691. resultSet_ = rhs.resultSet_ ? new pugi::xpath_node_set(rhs.resultSet_->begin(), rhs.resultSet_->end()) : 0;
  692. return *this;
  693. }
  694. XMLElement XPathResultSet::operator[](unsigned index) const
  695. {
  696. if (!resultSet_)
  697. LOGERROR(ToString("Could not return result at index: %u. Most probably this is caused by the XPathResultSet not being stored in a lhs variable.", index));
  698. return resultSet_ && index < Size() ? XMLElement(file_, this, &resultSet_->operator [](index), index) : XMLElement();
  699. }
  700. XMLElement XPathResultSet::FirstResult()
  701. {
  702. return operator [](0);
  703. }
  704. unsigned XPathResultSet::Size() const
  705. {
  706. return resultSet_ ? resultSet_->size() : 0;
  707. }
  708. bool XPathResultSet::Empty() const
  709. {
  710. return resultSet_ ? resultSet_->empty() : true;
  711. }
  712. XPathQuery::XPathQuery() :
  713. query_(0),
  714. variables_(0)
  715. {
  716. }
  717. XPathQuery::XPathQuery(const String& queryString, const String& variableString) :
  718. query_(0),
  719. variables_(0)
  720. {
  721. SetQuery(queryString, variableString);
  722. }
  723. XPathQuery::~XPathQuery()
  724. {
  725. delete variables_;
  726. variables_ = 0;
  727. delete query_;
  728. query_ = 0;
  729. }
  730. void XPathQuery::Bind()
  731. {
  732. // Delete previous query object and create a new one binding it with variable set
  733. delete query_;
  734. query_ = new pugi::xpath_query(queryString_.CString(), variables_);
  735. }
  736. bool XPathQuery::SetVariable(const String& name, bool value)
  737. {
  738. if (!variables_)
  739. variables_ = new pugi::xpath_variable_set();
  740. return variables_->set(name.CString(), value);
  741. }
  742. bool XPathQuery::SetVariable(const String& name, float value)
  743. {
  744. if (!variables_)
  745. variables_ = new pugi::xpath_variable_set();
  746. return variables_->set(name.CString(), value);
  747. }
  748. bool XPathQuery::SetVariable(const String& name, const String& value)
  749. {
  750. return SetVariable(name.CString(), value.CString());
  751. }
  752. bool XPathQuery::SetVariable(const char* name, const char* value)
  753. {
  754. if (!variables_)
  755. variables_ = new pugi::xpath_variable_set();
  756. return variables_->set(name, value);
  757. }
  758. bool XPathQuery::SetVariable(const String& name, const XPathResultSet& value)
  759. {
  760. if (!variables_)
  761. variables_ = new pugi::xpath_variable_set();
  762. return variables_->set(name.CString(), value.GetXPathNodeSet());
  763. }
  764. bool XPathQuery::SetQuery(const String& queryString, const String& variableString, bool bind)
  765. {
  766. if (!variableString.Empty())
  767. {
  768. Clear();
  769. variables_ = new pugi::xpath_variable_set();
  770. // Parse the variable string having format "name1:type1,name2:type2,..." where type is one of "Bool", "Float", "String", "ResultSet"
  771. Vector<String> vars = variableString.Split(',');
  772. for (Vector<String>::ConstIterator i = vars.Begin(); i != vars.End(); ++i)
  773. {
  774. Vector<String> tokens = i->Trimmed().Split(':');
  775. if (tokens.Size() != 2)
  776. continue;
  777. pugi::xpath_value_type type;
  778. if (tokens[1] == "Bool")
  779. type = pugi::xpath_type_boolean;
  780. else if (tokens[1] == "Float")
  781. type = pugi::xpath_type_number;
  782. else if (tokens[1] == "String")
  783. type = pugi::xpath_type_string;
  784. else if (tokens[1] == "ResultSet")
  785. type = pugi::xpath_type_node_set;
  786. else
  787. return false;
  788. if (!variables_->add(tokens[0].CString(), type))
  789. return false;
  790. }
  791. }
  792. queryString_ = queryString;
  793. if (bind)
  794. Bind();
  795. return true;
  796. }
  797. void XPathQuery::Clear()
  798. {
  799. queryString_.Clear();
  800. delete variables_;
  801. variables_ = 0;
  802. delete query_;
  803. query_ = 0;
  804. }
  805. bool XPathQuery::EvaluateToBool(XMLElement element) const
  806. {
  807. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  808. return false;
  809. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node(): pugi::xml_node(element.GetNode());
  810. return query_->evaluate_boolean(node);
  811. }
  812. float XPathQuery::EvaluateToFloat(XMLElement element) const
  813. {
  814. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  815. return 0.0f;
  816. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node(): pugi::xml_node(element.GetNode());
  817. return query_->evaluate_number(node);
  818. }
  819. String XPathQuery::EvaluateToString(XMLElement element) const
  820. {
  821. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  822. return String::EMPTY;
  823. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node(): pugi::xml_node(element.GetNode());
  824. String result;
  825. result.Reserve(query_->evaluate_string(0, 0, node)); // First call get the size
  826. query_->evaluate_string(const_cast<pugi::char_t*>(result.CString()), result.Capacity(), node); // Second call get the actual string
  827. return result;
  828. }
  829. XPathResultSet XPathQuery::Evaluate(XMLElement element) const
  830. {
  831. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  832. return XPathResultSet();
  833. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node(): pugi::xml_node(element.GetNode());
  834. pugi::xpath_node_set result = query_->evaluate_node_set(node);
  835. return XPathResultSet(element.GetFile(), &result);
  836. }
  837. }