XMLElement.cpp 29 KB

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