XMLElement.cpp 29 KB

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