XMLElement.cpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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::SetVectorVariant(const String& name, const Variant& value)
  362. {
  363. VariantType type = value.GetType();
  364. if (type == VAR_FLOAT || type == VAR_VECTOR2 || type == VAR_VECTOR3 || type == VAR_VECTOR4)
  365. return SetAttribute(name, value.ToString());
  366. else
  367. return false;
  368. }
  369. bool XMLElement::IsNull() const
  370. {
  371. return !NotNull();
  372. }
  373. bool XMLElement::NotNull() const
  374. {
  375. return node_ || (xpathNode_ && !xpathNode_->operator !());
  376. }
  377. XMLElement::operator bool () const
  378. {
  379. return NotNull();
  380. }
  381. String XMLElement::GetName() const
  382. {
  383. if ((!file_ || !node_) && !xpathNode_)
  384. return String();
  385. // If xpath_node contains just attribute, return its name instead
  386. if (xpathNode_ && xpathNode_->attribute())
  387. return String(xpathNode_->attribute().name());
  388. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  389. return String(node.name());
  390. }
  391. bool XMLElement::HasChild(const String& name) const
  392. {
  393. return HasChild(name.CString());
  394. }
  395. bool XMLElement::HasChild(const char* name) const
  396. {
  397. if (!file_ || (!node_ && !xpathNode_))
  398. return false;
  399. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  400. return !node.child(name).empty();
  401. }
  402. XMLElement XMLElement::GetChild(const String& name) const
  403. {
  404. return GetChild(name.CString());
  405. }
  406. XMLElement XMLElement::GetChild(const char* name) const
  407. {
  408. if (!file_ || (!node_ && !xpathNode_))
  409. return XMLElement();
  410. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  411. if (!String::CStringLength(name))
  412. return XMLElement(file_, node.first_child().internal_object());
  413. else
  414. return XMLElement(file_, node.child(name).internal_object());
  415. }
  416. XMLElement XMLElement::GetNext(const String& name) const
  417. {
  418. return GetNext(name.CString());
  419. }
  420. XMLElement XMLElement::GetNext(const char* name) const
  421. {
  422. if (!file_ || (!node_ && !xpathNode_))
  423. return XMLElement();
  424. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  425. if (!String::CStringLength(name))
  426. return XMLElement(file_, node.next_sibling().internal_object());
  427. else
  428. return XMLElement(file_, node.next_sibling(name).internal_object());
  429. }
  430. XMLElement XMLElement::GetParent() const
  431. {
  432. if (!file_ || (!node_ && !xpathNode_))
  433. return XMLElement();
  434. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  435. return XMLElement(file_, node.parent().internal_object());
  436. }
  437. unsigned XMLElement::GetNumAttributes() const
  438. {
  439. if (!file_ || (!node_ && !xpathNode_))
  440. return 0;
  441. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  442. unsigned ret = 0;
  443. pugi::xml_attribute attr = node.first_attribute();
  444. while (!attr.empty())
  445. {
  446. ++ret;
  447. attr = attr.next_attribute();
  448. }
  449. return ret;
  450. }
  451. bool XMLElement::HasAttribute(const String& name) const
  452. {
  453. return HasAttribute(name.CString());
  454. }
  455. bool XMLElement::HasAttribute(const char* name) const
  456. {
  457. if (!file_ || (!node_ && !xpathNode_))
  458. return false;
  459. // If xpath_node contains just attribute, check against it
  460. if (xpathNode_ && xpathNode_->attribute())
  461. return String(xpathNode_->attribute().name()) == name;
  462. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  463. return !node.attribute(name).empty();
  464. }
  465. String XMLElement::GetAttribute(const String& name) const
  466. {
  467. return String(GetAttributeCString(name.CString()));
  468. }
  469. String XMLElement::GetAttribute(const char* name) const
  470. {
  471. return String(GetAttributeCString(name));
  472. }
  473. const char* XMLElement::GetAttributeCString(const char* name) const
  474. {
  475. if (!file_ || (!node_ && !xpathNode_))
  476. return 0;
  477. // If xpath_node contains just attribute, return it regardless of the specified name
  478. if (xpathNode_ && xpathNode_->attribute())
  479. return xpathNode_->attribute().value();
  480. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  481. return node.attribute(name).value();
  482. }
  483. String XMLElement::GetAttributeLower(const String& name) const
  484. {
  485. return GetAttribute(name).ToLower();
  486. }
  487. String XMLElement::GetAttributeLower(const char* name) const
  488. {
  489. return String(GetAttribute(name)).ToLower();
  490. }
  491. String XMLElement::GetAttributeUpper(const String& name) const
  492. {
  493. return GetAttribute(name).ToUpper();
  494. }
  495. String XMLElement::GetAttributeUpper(const char* name) const
  496. {
  497. return String(GetAttribute(name)).ToUpper();
  498. }
  499. Vector<String> XMLElement::GetAttributeNames() const
  500. {
  501. if (!file_ || (!node_ && !xpathNode_))
  502. return Vector<String>();
  503. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  504. Vector<String> ret;
  505. pugi::xml_attribute attr = node.first_attribute();
  506. while (!attr.empty())
  507. {
  508. ret.Push(String(attr.name()));
  509. attr = attr.next_attribute();
  510. }
  511. return ret;
  512. }
  513. bool XMLElement::GetBool(const String& name) const
  514. {
  515. return ToBool(GetAttribute(name));
  516. }
  517. BoundingBox XMLElement::GetBoundingBox() const
  518. {
  519. BoundingBox ret;
  520. ret.min_ = GetVector3("min");
  521. ret.max_ = GetVector3("max");
  522. ret.defined_ = true;
  523. return ret;
  524. }
  525. PODVector<unsigned char> XMLElement::GetBuffer(const String& name) const
  526. {
  527. PODVector<unsigned char> ret;
  528. StringToBuffer(ret, GetAttribute(name));
  529. return ret;
  530. }
  531. bool XMLElement::GetBuffer(const String& name, void* dest, unsigned size) const
  532. {
  533. PODVector<unsigned char> ret;
  534. Vector<String> bytes = GetAttribute(name).Split(' ');
  535. unsigned char* destBytes = (unsigned char*)dest;
  536. if (size < bytes.Size())
  537. return false;
  538. for (unsigned i = 0; i < bytes.Size(); ++i)
  539. destBytes[i] = ToInt(bytes[i]);
  540. return true;
  541. }
  542. Color XMLElement::GetColor(const String& name) const
  543. {
  544. return ToColor(GetAttribute(name));
  545. }
  546. float XMLElement::GetFloat(const String& name) const
  547. {
  548. return ToFloat(GetAttribute(name));
  549. }
  550. unsigned XMLElement::GetUInt(const String& name) const
  551. {
  552. return ToUInt(GetAttribute(name));
  553. }
  554. int XMLElement::GetInt(const String& name) const
  555. {
  556. return ToInt(GetAttribute(name));
  557. }
  558. IntRect XMLElement::GetIntRect(const String& name) const
  559. {
  560. return ToIntRect(GetAttribute(name));
  561. }
  562. IntVector2 XMLElement::GetIntVector2(const String& name) const
  563. {
  564. return ToIntVector2(GetAttribute(name));
  565. }
  566. Quaternion XMLElement::GetQuaternion(const String& name) const
  567. {
  568. return ToQuaternion(GetAttribute(name));
  569. }
  570. Rect XMLElement::GetRect(const String& name) const
  571. {
  572. return ToRect(GetAttribute(name));
  573. }
  574. Variant XMLElement::GetVariant() const
  575. {
  576. VariantType type = Variant::GetTypeFromName(GetAttribute("type"));
  577. return GetVariantValue(type);
  578. }
  579. Variant XMLElement::GetVariantValue(VariantType type) const
  580. {
  581. Variant ret;
  582. if (type == VAR_RESOURCEREF)
  583. ret = GetResourceRef();
  584. else if (type == VAR_RESOURCEREFLIST)
  585. ret = GetResourceRefList();
  586. else if (type == VAR_VARIANTVECTOR)
  587. ret = GetVariantVector();
  588. else if (type == VAR_VARIANTMAP)
  589. ret = GetVariantMap();
  590. else
  591. ret.FromString(type, GetAttributeCString("value"));
  592. return ret;
  593. }
  594. ResourceRef XMLElement::GetResourceRef() const
  595. {
  596. ResourceRef ret;
  597. Vector<String> values = GetAttribute("value").Split(';');
  598. if (values.Size() == 2)
  599. {
  600. ret.type_ = values[0];
  601. ret.id_ = values[1];
  602. // Whenever we encounter a resource name read from a ResourceRef XML element, store the reverse mapping to
  603. // ResourceCache if possible. We will probably use the hash to request a resource shortly afterward
  604. if (file_)
  605. file_->GetSubsystem<ResourceCache>()->StoreNameHash(values[1]);
  606. }
  607. return ret;
  608. }
  609. ResourceRefList XMLElement::GetResourceRefList() const
  610. {
  611. ResourceRefList ret;
  612. Vector<String> values = GetAttribute("value").Split(';');
  613. if (values.Size() >= 1)
  614. {
  615. // Whenever we encounter resource names read from a ResourceRefList XML element, store the reverse mapping to
  616. // ResourceCache if possible. We will probably use the hashes to request resources shortly afterward
  617. ResourceCache* cache = file_ ? file_->GetSubsystem<ResourceCache>() : 0;
  618. ret.type_ = values[0];
  619. ret.ids_.Resize(values.Size() - 1);
  620. for (unsigned i = 1; i < values.Size(); ++i)
  621. {
  622. ret.ids_[i - 1] = StringHash(values[i]);
  623. if (cache)
  624. cache->StoreNameHash(values[i]);
  625. }
  626. }
  627. return ret;
  628. }
  629. VariantVector XMLElement::GetVariantVector() const
  630. {
  631. VariantVector ret;
  632. XMLElement variantElem = GetChild("variant");
  633. while (variantElem)
  634. {
  635. ret.Push(variantElem.GetVariant());
  636. variantElem = variantElem.GetNext("variant");
  637. }
  638. return ret;
  639. }
  640. VariantMap XMLElement::GetVariantMap() const
  641. {
  642. VariantMap ret;
  643. XMLElement variantElem = GetChild("variant");
  644. while (variantElem)
  645. {
  646. ShortStringHash key(variantElem.GetInt("hash"));
  647. ret[key] = variantElem.GetVariant();
  648. variantElem = variantElem.GetNext("variant");
  649. }
  650. return ret;
  651. }
  652. Vector2 XMLElement::GetVector2(const String& name) const
  653. {
  654. return ToVector2(GetAttribute(name));
  655. }
  656. Vector3 XMLElement::GetVector3(const String& name) const
  657. {
  658. return ToVector3(GetAttribute(name));
  659. }
  660. Vector4 XMLElement::GetVector4(const String& name) const
  661. {
  662. return ToVector4(GetAttribute(name));
  663. }
  664. Vector4 XMLElement::GetVector(const String& name) const
  665. {
  666. return ToVector4(GetAttribute(name), true);
  667. }
  668. Variant XMLElement::GetVectorVariant(const String& name) const
  669. {
  670. return ToVectorVariant(GetAttribute(name));
  671. }
  672. XMLFile* XMLElement::GetFile() const
  673. {
  674. return file_;
  675. }
  676. XMLElement XMLElement::NextResult() const
  677. {
  678. if (!xpathResultSet_ || !xpathNode_)
  679. return XMLElement();
  680. return xpathResultSet_->operator [](++xpathResultIndex_);
  681. }
  682. XPathResultSet::XPathResultSet() :
  683. resultSet_(0)
  684. {
  685. }
  686. XPathResultSet::XPathResultSet(XMLFile* file, pugi::xpath_node_set* resultSet) :
  687. file_(file),
  688. resultSet_(resultSet ? new pugi::xpath_node_set(resultSet->begin(), resultSet->end()) : 0)
  689. {
  690. // Sort the node set in forward document order
  691. if (resultSet_)
  692. resultSet_->sort();
  693. }
  694. XPathResultSet::XPathResultSet(const XPathResultSet& rhs) :
  695. file_(rhs.file_),
  696. resultSet_(rhs.resultSet_ ? new pugi::xpath_node_set(rhs.resultSet_->begin(), rhs.resultSet_->end()) : 0)
  697. {
  698. }
  699. XPathResultSet::~XPathResultSet()
  700. {
  701. delete resultSet_;
  702. resultSet_ = 0;
  703. }
  704. XPathResultSet& XPathResultSet::operator = (const XPathResultSet& rhs)
  705. {
  706. file_ = rhs.file_;
  707. resultSet_ = rhs.resultSet_ ? new pugi::xpath_node_set(rhs.resultSet_->begin(), rhs.resultSet_->end()) : 0;
  708. return *this;
  709. }
  710. XMLElement XPathResultSet::operator[](unsigned index) const
  711. {
  712. if (!resultSet_)
  713. 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));
  714. return resultSet_ && index < Size() ? XMLElement(file_, this, &resultSet_->operator [](index), index) : XMLElement();
  715. }
  716. XMLElement XPathResultSet::FirstResult()
  717. {
  718. return operator [](0);
  719. }
  720. unsigned XPathResultSet::Size() const
  721. {
  722. return resultSet_ ? resultSet_->size() : 0;
  723. }
  724. bool XPathResultSet::Empty() const
  725. {
  726. return resultSet_ ? resultSet_->empty() : true;
  727. }
  728. XPathQuery::XPathQuery() :
  729. query_(0),
  730. variables_(0)
  731. {
  732. }
  733. XPathQuery::XPathQuery(const String& queryString, const String& variableString) :
  734. query_(0),
  735. variables_(0)
  736. {
  737. SetQuery(queryString, variableString);
  738. }
  739. XPathQuery::~XPathQuery()
  740. {
  741. delete variables_;
  742. variables_ = 0;
  743. delete query_;
  744. query_ = 0;
  745. }
  746. void XPathQuery::Bind()
  747. {
  748. // Delete previous query object and create a new one binding it with variable set
  749. delete query_;
  750. query_ = new pugi::xpath_query(queryString_.CString(), variables_);
  751. }
  752. bool XPathQuery::SetVariable(const String& name, bool value)
  753. {
  754. if (!variables_)
  755. variables_ = new pugi::xpath_variable_set();
  756. return variables_->set(name.CString(), value);
  757. }
  758. bool XPathQuery::SetVariable(const String& name, float value)
  759. {
  760. if (!variables_)
  761. variables_ = new pugi::xpath_variable_set();
  762. return variables_->set(name.CString(), value);
  763. }
  764. bool XPathQuery::SetVariable(const String& name, const String& value)
  765. {
  766. return SetVariable(name.CString(), value.CString());
  767. }
  768. bool XPathQuery::SetVariable(const char* name, const char* value)
  769. {
  770. if (!variables_)
  771. variables_ = new pugi::xpath_variable_set();
  772. return variables_->set(name, value);
  773. }
  774. bool XPathQuery::SetVariable(const String& name, const XPathResultSet& value)
  775. {
  776. if (!variables_)
  777. variables_ = new pugi::xpath_variable_set();
  778. pugi::xpath_node_set* nodeSet = value.GetXPathNodeSet();
  779. if (!nodeSet)
  780. return false;
  781. return variables_->set(name.CString(), *nodeSet);
  782. }
  783. bool XPathQuery::SetQuery(const String& queryString, const String& variableString, bool bind)
  784. {
  785. if (!variableString.Empty())
  786. {
  787. Clear();
  788. variables_ = new pugi::xpath_variable_set();
  789. // Parse the variable string having format "name1:type1,name2:type2,..." where type is one of "Bool", "Float", "String", "ResultSet"
  790. Vector<String> vars = variableString.Split(',');
  791. for (Vector<String>::ConstIterator i = vars.Begin(); i != vars.End(); ++i)
  792. {
  793. Vector<String> tokens = i->Trimmed().Split(':');
  794. if (tokens.Size() != 2)
  795. continue;
  796. pugi::xpath_value_type type;
  797. if (tokens[1] == "Bool")
  798. type = pugi::xpath_type_boolean;
  799. else if (tokens[1] == "Float")
  800. type = pugi::xpath_type_number;
  801. else if (tokens[1] == "String")
  802. type = pugi::xpath_type_string;
  803. else if (tokens[1] == "ResultSet")
  804. type = pugi::xpath_type_node_set;
  805. else
  806. return false;
  807. if (!variables_->add(tokens[0].CString(), type))
  808. return false;
  809. }
  810. }
  811. queryString_ = queryString;
  812. if (bind)
  813. Bind();
  814. return true;
  815. }
  816. void XPathQuery::Clear()
  817. {
  818. queryString_.Clear();
  819. delete variables_;
  820. variables_ = 0;
  821. delete query_;
  822. query_ = 0;
  823. }
  824. bool XPathQuery::EvaluateToBool(XMLElement element) const
  825. {
  826. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  827. return false;
  828. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node(): pugi::xml_node(element.GetNode());
  829. return query_->evaluate_boolean(node);
  830. }
  831. float XPathQuery::EvaluateToFloat(XMLElement element) const
  832. {
  833. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  834. return 0.0f;
  835. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node(): pugi::xml_node(element.GetNode());
  836. return (float)query_->evaluate_number(node);
  837. }
  838. String XPathQuery::EvaluateToString(XMLElement element) const
  839. {
  840. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  841. return String::EMPTY;
  842. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node(): pugi::xml_node(element.GetNode());
  843. String result;
  844. result.Reserve(query_->evaluate_string(0, 0, node)); // First call get the size
  845. query_->evaluate_string(const_cast<pugi::char_t*>(result.CString()), result.Capacity(), node); // Second call get the actual string
  846. return result;
  847. }
  848. XPathResultSet XPathQuery::Evaluate(XMLElement element) const
  849. {
  850. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  851. return XPathResultSet();
  852. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node(): pugi::xml_node(element.GetNode());
  853. pugi::xpath_node_set result = query_->evaluate_node_set(node);
  854. return XPathResultSet(element.GetFile(), &result);
  855. }
  856. }