XMLElement.cpp 29 KB

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