XMLElement.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. //
  2. // Copyright (c) 2008-2015 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 "../Core/Context.h"
  24. #include "../IO/Log.h"
  25. #include "../Resource/XMLFile.h"
  26. #include <PugiXml/src/pugixml.hpp>
  27. #include "../DebugNew.h"
  28. namespace Atomic
  29. {
  30. const XMLElement XMLElement::EMPTY;
  31. XMLElement::XMLElement() :
  32. node_(0),
  33. xpathResultSet_(0),
  34. xpathNode_(0),
  35. xpathResultIndex_(0)
  36. {
  37. }
  38. XMLElement::XMLElement(XMLFile* file, pugi::xml_node_struct* node) :
  39. file_(file),
  40. node_(node),
  41. xpathResultSet_(0),
  42. xpathNode_(0),
  43. xpathResultIndex_(0)
  44. {
  45. }
  46. XMLElement::XMLElement(XMLFile* file, const XPathResultSet* resultSet, const pugi::xpath_node* xpathNode,
  47. 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(
  154. 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::SetValue(const String& value)
  191. {
  192. return SetValue(value.CString());
  193. }
  194. bool XMLElement::SetValue(const char* value)
  195. {
  196. if (!file_ || (!node_ && !xpathNode_))
  197. return false;
  198. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  199. // Search for existing value first
  200. for (pugi::xml_node child = node.first_child(); child; child = child.next_sibling())
  201. {
  202. if (child.type() == pugi::node_pcdata)
  203. return const_cast<pugi::xml_node&>(child).set_value(value);
  204. }
  205. // If no previous value found, append new
  206. return const_cast<pugi::xml_node&>(node).append_child(pugi::node_pcdata).set_value(value);
  207. }
  208. bool XMLElement::SetAttribute(const String& name, const String& value)
  209. {
  210. return SetAttribute(name.CString(), value.CString());
  211. }
  212. bool XMLElement::SetAttribute(const char* name, const char* value)
  213. {
  214. if (!file_ || (!node_ && !xpathNode_))
  215. return false;
  216. // If xpath_node contains just attribute, set its value regardless of the specified name
  217. if (xpathNode_ && xpathNode_->attribute())
  218. return xpathNode_->attribute().set_value(value);
  219. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  220. pugi::xml_attribute attr = node.attribute(name);
  221. if (attr.empty())
  222. attr = const_cast<pugi::xml_node&>(node).append_attribute(name);
  223. return attr.set_value(value);
  224. }
  225. bool XMLElement::SetAttribute(const String& value)
  226. {
  227. return SetAttribute(value.CString());
  228. }
  229. bool XMLElement::SetAttribute(const char* value)
  230. {
  231. // If xpath_node contains just attribute, set its value
  232. return xpathNode_ && xpathNode_->attribute() && xpathNode_->attribute().set_value(value);
  233. }
  234. bool XMLElement::SetBool(const String& name, bool value)
  235. {
  236. return SetAttribute(name, String(value));
  237. }
  238. bool XMLElement::SetBoundingBox(const BoundingBox& value)
  239. {
  240. if (!SetVector3("min", value.min_))
  241. return false;
  242. return SetVector3("max", value.max_);
  243. }
  244. bool XMLElement::SetBuffer(const String& name, const void* data, unsigned size)
  245. {
  246. String dataStr;
  247. BufferToString(dataStr, data, size);
  248. return SetAttribute(name, dataStr);
  249. }
  250. bool XMLElement::SetBuffer(const String& name, const PODVector<unsigned char>& value)
  251. {
  252. if (!value.Size())
  253. return SetAttribute(name, String::EMPTY);
  254. else
  255. return SetBuffer(name, &value[0], value.Size());
  256. }
  257. bool XMLElement::SetColor(const String& name, const Color& value)
  258. {
  259. return SetAttribute(name, value.ToString());
  260. }
  261. bool XMLElement::SetFloat(const String& name, float value)
  262. {
  263. return SetAttribute(name, String(value));
  264. }
  265. bool XMLElement::SetDouble(const String& name, double value)
  266. {
  267. return SetAttribute(name, String(value));
  268. }
  269. bool XMLElement::SetUInt(const String& name, unsigned value)
  270. {
  271. return SetAttribute(name, String(value));
  272. }
  273. bool XMLElement::SetInt(const String& name, int value)
  274. {
  275. return SetAttribute(name, String(value));
  276. }
  277. bool XMLElement::SetIntRect(const String& name, const IntRect& value)
  278. {
  279. return SetAttribute(name, value.ToString());
  280. }
  281. bool XMLElement::SetIntVector2(const String& name, const IntVector2& value)
  282. {
  283. return SetAttribute(name, value.ToString());
  284. }
  285. bool XMLElement::SetRect(const String& name, const Rect& value)
  286. {
  287. return SetAttribute(name, value.ToString());
  288. }
  289. bool XMLElement::SetQuaternion(const String& name, const Quaternion& value)
  290. {
  291. return SetAttribute(name, value.ToString());
  292. }
  293. bool XMLElement::SetString(const String& name, const String& value)
  294. {
  295. return SetAttribute(name, value);
  296. }
  297. bool XMLElement::SetVariant(const Variant& value)
  298. {
  299. if (!SetAttribute("type", value.GetTypeName()))
  300. return false;
  301. return SetVariantValue(value);
  302. }
  303. bool XMLElement::SetVariantValue(const Variant& value)
  304. {
  305. switch (value.GetType())
  306. {
  307. case VAR_RESOURCEREF:
  308. return SetResourceRef(value.GetResourceRef());
  309. case VAR_RESOURCEREFLIST:
  310. return SetResourceRefList(value.GetResourceRefList());
  311. case VAR_VARIANTVECTOR:
  312. return SetVariantVector(value.GetVariantVector());
  313. case VAR_STRINGVECTOR:
  314. return SetStringVector(value.GetStringVector());
  315. case VAR_VARIANTMAP:
  316. return SetVariantMap(value.GetVariantMap());
  317. default:
  318. return SetAttribute("value", value.ToString().CString());
  319. }
  320. }
  321. bool XMLElement::SetResourceRef(const ResourceRef& value)
  322. {
  323. if (!file_ || (!node_ && !xpathNode_))
  324. return false;
  325. // Need the context to query for the type
  326. Context* context = file_->GetContext();
  327. return SetAttribute("value", String(context->GetTypeName(value.type_)) + ";" + value.name_);
  328. }
  329. bool XMLElement::SetResourceRefList(const ResourceRefList& value)
  330. {
  331. if (!file_ || (!node_ && !xpathNode_))
  332. return false;
  333. // Need the context to query for the type
  334. Context* context = file_->GetContext();
  335. String str(context->GetTypeName(value.type_));
  336. for (unsigned i = 0; i < value.names_.Size(); ++i)
  337. {
  338. str += ";";
  339. str += value.names_[i];
  340. }
  341. return SetAttribute("value", str.CString());
  342. }
  343. bool XMLElement::SetVariantVector(const VariantVector& value)
  344. {
  345. // Must remove all existing variant child elements (if they exist) to not cause confusion
  346. if (!RemoveChildren("variant"))
  347. return false;
  348. for (VariantVector::ConstIterator i = value.Begin(); i != value.End(); ++i)
  349. {
  350. XMLElement variantElem = CreateChild("variant");
  351. if (!variantElem)
  352. return false;
  353. variantElem.SetVariant(*i);
  354. }
  355. return true;
  356. }
  357. bool XMLElement::SetStringVector(const StringVector& value)
  358. {
  359. if (!RemoveChildren("string"))
  360. return false;
  361. for (StringVector::ConstIterator i = value.Begin(); i != value.End(); ++i)
  362. {
  363. XMLElement stringElem = CreateChild("string");
  364. if (!stringElem)
  365. return false;
  366. stringElem.SetAttribute("value", *i);
  367. }
  368. return true;
  369. }
  370. bool XMLElement::SetVariantMap(const VariantMap& value)
  371. {
  372. if (!RemoveChildren("variant"))
  373. return false;
  374. for (VariantMap::ConstIterator i = value.Begin(); i != value.End(); ++i)
  375. {
  376. XMLElement variantElem = CreateChild("variant");
  377. if (!variantElem)
  378. return false;
  379. variantElem.SetUInt("hash", i->first_.Value());
  380. variantElem.SetVariant(i->second_);
  381. }
  382. return true;
  383. }
  384. bool XMLElement::SetVector2(const String& name, const Vector2& value)
  385. {
  386. return SetAttribute(name, value.ToString());
  387. }
  388. bool XMLElement::SetVector3(const String& name, const Vector3& value)
  389. {
  390. return SetAttribute(name, value.ToString());
  391. }
  392. bool XMLElement::SetVector4(const String& name, const Vector4& value)
  393. {
  394. return SetAttribute(name, value.ToString());
  395. }
  396. bool XMLElement::SetVectorVariant(const String& name, const Variant& value)
  397. {
  398. VariantType type = value.GetType();
  399. if (type == VAR_FLOAT || type == VAR_VECTOR2 || type == VAR_VECTOR3 || type == VAR_VECTOR4 || type == VAR_MATRIX3 ||
  400. type == VAR_MATRIX3X4 || type == VAR_MATRIX4)
  401. return SetAttribute(name, value.ToString());
  402. else
  403. return false;
  404. }
  405. bool XMLElement::SetMatrix3(const String& name, const Matrix3& value)
  406. {
  407. return SetAttribute(name, value.ToString());
  408. }
  409. bool XMLElement::SetMatrix3x4(const String& name, const Matrix3x4& value)
  410. {
  411. return SetAttribute(name, value.ToString());
  412. }
  413. bool XMLElement::SetMatrix4(const String& name, const Matrix4& value)
  414. {
  415. return SetAttribute(name, value.ToString());
  416. }
  417. bool XMLElement::IsNull() const
  418. {
  419. return !NotNull();
  420. }
  421. bool XMLElement::NotNull() const
  422. {
  423. return node_ || (xpathNode_ && !xpathNode_->operator !());
  424. }
  425. XMLElement::operator bool() const
  426. {
  427. return NotNull();
  428. }
  429. String XMLElement::GetName() const
  430. {
  431. if ((!file_ || !node_) && !xpathNode_)
  432. return String();
  433. // If xpath_node contains just attribute, return its name instead
  434. if (xpathNode_ && xpathNode_->attribute())
  435. return String(xpathNode_->attribute().name());
  436. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  437. return String(node.name());
  438. }
  439. bool XMLElement::HasChild(const String& name) const
  440. {
  441. return HasChild(name.CString());
  442. }
  443. bool XMLElement::HasChild(const char* name) const
  444. {
  445. if (!file_ || (!node_ && !xpathNode_))
  446. return false;
  447. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  448. return !node.child(name).empty();
  449. }
  450. XMLElement XMLElement::GetChild(const String& name) const
  451. {
  452. return GetChild(name.CString());
  453. }
  454. XMLElement XMLElement::GetChild(const char* name) const
  455. {
  456. if (!file_ || (!node_ && !xpathNode_))
  457. return XMLElement();
  458. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  459. if (!String::CStringLength(name))
  460. return XMLElement(file_, node.first_child().internal_object());
  461. else
  462. return XMLElement(file_, node.child(name).internal_object());
  463. }
  464. XMLElement XMLElement::GetNext(const String& name) const
  465. {
  466. return GetNext(name.CString());
  467. }
  468. XMLElement XMLElement::GetNext(const char* name) const
  469. {
  470. if (!file_ || (!node_ && !xpathNode_))
  471. return XMLElement();
  472. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  473. if (!String::CStringLength(name))
  474. return XMLElement(file_, node.next_sibling().internal_object());
  475. else
  476. return XMLElement(file_, node.next_sibling(name).internal_object());
  477. }
  478. XMLElement XMLElement::GetParent() const
  479. {
  480. if (!file_ || (!node_ && !xpathNode_))
  481. return XMLElement();
  482. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  483. return XMLElement(file_, node.parent().internal_object());
  484. }
  485. unsigned XMLElement::GetNumAttributes() const
  486. {
  487. if (!file_ || (!node_ && !xpathNode_))
  488. return 0;
  489. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  490. unsigned ret = 0;
  491. pugi::xml_attribute attr = node.first_attribute();
  492. while (!attr.empty())
  493. {
  494. ++ret;
  495. attr = attr.next_attribute();
  496. }
  497. return ret;
  498. }
  499. bool XMLElement::HasAttribute(const String& name) const
  500. {
  501. return HasAttribute(name.CString());
  502. }
  503. bool XMLElement::HasAttribute(const char* name) const
  504. {
  505. if (!file_ || (!node_ && !xpathNode_))
  506. return false;
  507. // If xpath_node contains just attribute, check against it
  508. if (xpathNode_ && xpathNode_->attribute())
  509. return String(xpathNode_->attribute().name()) == name;
  510. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  511. return !node.attribute(name).empty();
  512. }
  513. String XMLElement::GetValue() const
  514. {
  515. if (!file_ || (!node_ && !xpathNode_))
  516. return String::EMPTY;
  517. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  518. return String(node.child_value());
  519. }
  520. String XMLElement::GetAttribute(const String& name) const
  521. {
  522. return String(GetAttributeCString(name.CString()));
  523. }
  524. String XMLElement::GetAttribute(const char* name) const
  525. {
  526. return String(GetAttributeCString(name));
  527. }
  528. const char* XMLElement::GetAttributeCString(const char* name) const
  529. {
  530. if (!file_ || (!node_ && !xpathNode_))
  531. return 0;
  532. // If xpath_node contains just attribute, return it regardless of the specified name
  533. if (xpathNode_ && xpathNode_->attribute())
  534. return xpathNode_->attribute().value();
  535. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  536. return node.attribute(name).value();
  537. }
  538. String XMLElement::GetAttributeLower(const String& name) const
  539. {
  540. return GetAttribute(name).ToLower();
  541. }
  542. String XMLElement::GetAttributeLower(const char* name) const
  543. {
  544. return String(GetAttribute(name)).ToLower();
  545. }
  546. String XMLElement::GetAttributeUpper(const String& name) const
  547. {
  548. return GetAttribute(name).ToUpper();
  549. }
  550. String XMLElement::GetAttributeUpper(const char* name) const
  551. {
  552. return String(GetAttribute(name)).ToUpper();
  553. }
  554. Vector<String> XMLElement::GetAttributeNames() const
  555. {
  556. if (!file_ || (!node_ && !xpathNode_))
  557. return Vector<String>();
  558. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  559. Vector<String> ret;
  560. pugi::xml_attribute attr = node.first_attribute();
  561. while (!attr.empty())
  562. {
  563. ret.Push(String(attr.name()));
  564. attr = attr.next_attribute();
  565. }
  566. return ret;
  567. }
  568. bool XMLElement::GetBool(const String& name) const
  569. {
  570. return ToBool(GetAttribute(name));
  571. }
  572. BoundingBox XMLElement::GetBoundingBox() const
  573. {
  574. BoundingBox ret;
  575. ret.min_ = GetVector3("min");
  576. ret.max_ = GetVector3("max");
  577. ret.defined_ = true;
  578. return ret;
  579. }
  580. PODVector<unsigned char> XMLElement::GetBuffer(const String& name) const
  581. {
  582. PODVector<unsigned char> ret;
  583. StringToBuffer(ret, GetAttribute(name));
  584. return ret;
  585. }
  586. bool XMLElement::GetBuffer(const String& name, void* dest, unsigned size) const
  587. {
  588. Vector<String> bytes = GetAttribute(name).Split(' ');
  589. if (size < bytes.Size())
  590. return false;
  591. unsigned char* destBytes = (unsigned char*)dest;
  592. for (unsigned i = 0; i < bytes.Size(); ++i)
  593. destBytes[i] = (unsigned char)ToInt(bytes[i]);
  594. return true;
  595. }
  596. Color XMLElement::GetColor(const String& name) const
  597. {
  598. return ToColor(GetAttribute(name));
  599. }
  600. float XMLElement::GetFloat(const String& name) const
  601. {
  602. return ToFloat(GetAttribute(name));
  603. }
  604. double XMLElement::GetDouble(const String& name) const
  605. {
  606. return ToDouble(GetAttribute(name));
  607. }
  608. unsigned XMLElement::GetUInt(const String& name) const
  609. {
  610. return ToUInt(GetAttribute(name));
  611. }
  612. int XMLElement::GetInt(const String& name) const
  613. {
  614. return ToInt(GetAttribute(name));
  615. }
  616. IntRect XMLElement::GetIntRect(const String& name) const
  617. {
  618. return ToIntRect(GetAttribute(name));
  619. }
  620. IntVector2 XMLElement::GetIntVector2(const String& name) const
  621. {
  622. return ToIntVector2(GetAttribute(name));
  623. }
  624. Quaternion XMLElement::GetQuaternion(const String& name) const
  625. {
  626. return ToQuaternion(GetAttribute(name));
  627. }
  628. Rect XMLElement::GetRect(const String& name) const
  629. {
  630. return ToRect(GetAttribute(name));
  631. }
  632. Variant XMLElement::GetVariant() const
  633. {
  634. VariantType type = Variant::GetTypeFromName(GetAttribute("type"));
  635. return GetVariantValue(type);
  636. }
  637. Variant XMLElement::GetVariantValue(VariantType type) const
  638. {
  639. Variant ret;
  640. if (type == VAR_RESOURCEREF)
  641. ret = GetResourceRef();
  642. else if (type == VAR_RESOURCEREFLIST)
  643. ret = GetResourceRefList();
  644. else if (type == VAR_VARIANTVECTOR)
  645. ret = GetVariantVector();
  646. else if (type == VAR_STRINGVECTOR)
  647. ret = GetStringVector();
  648. else if (type == VAR_VARIANTMAP)
  649. ret = GetVariantMap();
  650. else
  651. ret.FromString(type, GetAttributeCString("value"));
  652. return ret;
  653. }
  654. ResourceRef XMLElement::GetResourceRef() const
  655. {
  656. ResourceRef ret;
  657. Vector<String> values = GetAttribute("value").Split(';');
  658. if (values.Size() == 2)
  659. {
  660. ret.type_ = values[0];
  661. ret.name_ = values[1];
  662. }
  663. return ret;
  664. }
  665. ResourceRefList XMLElement::GetResourceRefList() const
  666. {
  667. ResourceRefList ret;
  668. Vector<String> values = GetAttribute("value").Split(';');
  669. if (values.Size() >= 1)
  670. {
  671. ret.type_ = values[0];
  672. ret.names_.Resize(values.Size() - 1);
  673. for (unsigned i = 1; i < values.Size(); ++i)
  674. ret.names_[i - 1] = values[i];
  675. }
  676. return ret;
  677. }
  678. VariantVector XMLElement::GetVariantVector() const
  679. {
  680. VariantVector ret;
  681. XMLElement variantElem = GetChild("variant");
  682. while (variantElem)
  683. {
  684. ret.Push(variantElem.GetVariant());
  685. variantElem = variantElem.GetNext("variant");
  686. }
  687. return ret;
  688. }
  689. StringVector XMLElement::GetStringVector() const
  690. {
  691. StringVector ret;
  692. XMLElement stringElem = GetChild("string");
  693. while (stringElem)
  694. {
  695. ret.Push(stringElem.GetAttributeCString("value"));
  696. stringElem = stringElem.GetNext("string");
  697. }
  698. return ret;
  699. }
  700. VariantMap XMLElement::GetVariantMap() const
  701. {
  702. VariantMap ret;
  703. XMLElement variantElem = GetChild("variant");
  704. while (variantElem)
  705. {
  706. StringHash key(variantElem.GetUInt("hash"));
  707. ret[key] = variantElem.GetVariant();
  708. variantElem = variantElem.GetNext("variant");
  709. }
  710. return ret;
  711. }
  712. Vector2 XMLElement::GetVector2(const String& name) const
  713. {
  714. return ToVector2(GetAttribute(name));
  715. }
  716. Vector3 XMLElement::GetVector3(const String& name) const
  717. {
  718. return ToVector3(GetAttribute(name));
  719. }
  720. Vector4 XMLElement::GetVector4(const String& name) const
  721. {
  722. return ToVector4(GetAttribute(name));
  723. }
  724. Vector4 XMLElement::GetVector(const String& name) const
  725. {
  726. return ToVector4(GetAttribute(name), true);
  727. }
  728. Variant XMLElement::GetVectorVariant(const String& name) const
  729. {
  730. return ToVectorVariant(GetAttribute(name));
  731. }
  732. Matrix3 XMLElement::GetMatrix3(const String& name) const
  733. {
  734. return ToMatrix3(GetAttribute(name));
  735. }
  736. Matrix3x4 XMLElement::GetMatrix3x4(const String& name) const
  737. {
  738. return ToMatrix3x4(GetAttribute(name));
  739. }
  740. Matrix4 XMLElement::GetMatrix4(const String& name) const
  741. {
  742. return ToMatrix4(GetAttribute(name));
  743. }
  744. XMLFile* XMLElement::GetFile() const
  745. {
  746. return file_;
  747. }
  748. XMLElement XMLElement::NextResult() const
  749. {
  750. if (!xpathResultSet_ || !xpathNode_)
  751. return XMLElement();
  752. return xpathResultSet_->operator [](++xpathResultIndex_);
  753. }
  754. XPathResultSet::XPathResultSet() :
  755. resultSet_(0)
  756. {
  757. }
  758. XPathResultSet::XPathResultSet(XMLFile* file, pugi::xpath_node_set* resultSet) :
  759. file_(file),
  760. resultSet_(resultSet ? new pugi::xpath_node_set(resultSet->begin(), resultSet->end()) : 0)
  761. {
  762. // Sort the node set in forward document order
  763. if (resultSet_)
  764. resultSet_->sort();
  765. }
  766. XPathResultSet::XPathResultSet(const XPathResultSet& rhs) :
  767. file_(rhs.file_),
  768. resultSet_(rhs.resultSet_ ? new pugi::xpath_node_set(rhs.resultSet_->begin(), rhs.resultSet_->end()) : 0)
  769. {
  770. }
  771. XPathResultSet::~XPathResultSet()
  772. {
  773. delete resultSet_;
  774. resultSet_ = 0;
  775. }
  776. XPathResultSet& XPathResultSet::operator =(const XPathResultSet& rhs)
  777. {
  778. file_ = rhs.file_;
  779. resultSet_ = rhs.resultSet_ ? new pugi::xpath_node_set(rhs.resultSet_->begin(), rhs.resultSet_->end()) : 0;
  780. return *this;
  781. }
  782. XMLElement XPathResultSet::operator [](unsigned index) const
  783. {
  784. if (!resultSet_)
  785. LOGERRORF(
  786. "Could not return result at index: %u. Most probably this is caused by the XPathResultSet not being stored in a lhs variable.",
  787. index);
  788. return resultSet_ && index < Size() ? XMLElement(file_, this, &resultSet_->operator [](index), index) : XMLElement();
  789. }
  790. XMLElement XPathResultSet::FirstResult()
  791. {
  792. return operator [](0);
  793. }
  794. unsigned XPathResultSet::Size() const
  795. {
  796. return resultSet_ ? (unsigned)resultSet_->size() : 0;
  797. }
  798. bool XPathResultSet::Empty() const
  799. {
  800. return resultSet_ ? resultSet_->empty() : true;
  801. }
  802. XPathQuery::XPathQuery() :
  803. query_(0),
  804. variables_(0)
  805. {
  806. }
  807. XPathQuery::XPathQuery(const String& queryString, const String& variableString) :
  808. query_(0),
  809. variables_(0)
  810. {
  811. SetQuery(queryString, variableString);
  812. }
  813. XPathQuery::~XPathQuery()
  814. {
  815. delete variables_;
  816. variables_ = 0;
  817. delete query_;
  818. query_ = 0;
  819. }
  820. void XPathQuery::Bind()
  821. {
  822. // Delete previous query object and create a new one binding it with variable set
  823. delete query_;
  824. query_ = new pugi::xpath_query(queryString_.CString(), variables_);
  825. }
  826. bool XPathQuery::SetVariable(const String& name, bool value)
  827. {
  828. if (!variables_)
  829. variables_ = new pugi::xpath_variable_set();
  830. return variables_->set(name.CString(), value);
  831. }
  832. bool XPathQuery::SetVariable(const String& name, float value)
  833. {
  834. if (!variables_)
  835. variables_ = new pugi::xpath_variable_set();
  836. return variables_->set(name.CString(), value);
  837. }
  838. bool XPathQuery::SetVariable(const String& name, const String& value)
  839. {
  840. return SetVariable(name.CString(), value.CString());
  841. }
  842. bool XPathQuery::SetVariable(const char* name, const char* value)
  843. {
  844. if (!variables_)
  845. variables_ = new pugi::xpath_variable_set();
  846. return variables_->set(name, value);
  847. }
  848. bool XPathQuery::SetVariable(const String& name, const XPathResultSet& value)
  849. {
  850. if (!variables_)
  851. variables_ = new pugi::xpath_variable_set();
  852. pugi::xpath_node_set* nodeSet = value.GetXPathNodeSet();
  853. if (!nodeSet)
  854. return false;
  855. return variables_->set(name.CString(), *nodeSet);
  856. }
  857. bool XPathQuery::SetQuery(const String& queryString, const String& variableString, bool bind)
  858. {
  859. if (!variableString.Empty())
  860. {
  861. Clear();
  862. variables_ = new pugi::xpath_variable_set();
  863. // Parse the variable string having format "name1:type1,name2:type2,..." where type is one of "Bool", "Float", "String", "ResultSet"
  864. Vector<String> vars = variableString.Split(',');
  865. for (Vector<String>::ConstIterator i = vars.Begin(); i != vars.End(); ++i)
  866. {
  867. Vector<String> tokens = i->Trimmed().Split(':');
  868. if (tokens.Size() != 2)
  869. continue;
  870. pugi::xpath_value_type type;
  871. if (tokens[1] == "Bool")
  872. type = pugi::xpath_type_boolean;
  873. else if (tokens[1] == "Float")
  874. type = pugi::xpath_type_number;
  875. else if (tokens[1] == "String")
  876. type = pugi::xpath_type_string;
  877. else if (tokens[1] == "ResultSet")
  878. type = pugi::xpath_type_node_set;
  879. else
  880. return false;
  881. if (!variables_->add(tokens[0].CString(), type))
  882. return false;
  883. }
  884. }
  885. queryString_ = queryString;
  886. if (bind)
  887. Bind();
  888. return true;
  889. }
  890. void XPathQuery::Clear()
  891. {
  892. queryString_.Clear();
  893. delete variables_;
  894. variables_ = 0;
  895. delete query_;
  896. query_ = 0;
  897. }
  898. bool XPathQuery::EvaluateToBool(XMLElement element) const
  899. {
  900. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  901. return false;
  902. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node() : pugi::xml_node(element.GetNode());
  903. return query_->evaluate_boolean(node);
  904. }
  905. float XPathQuery::EvaluateToFloat(XMLElement element) const
  906. {
  907. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  908. return 0.0f;
  909. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node() : pugi::xml_node(element.GetNode());
  910. return (float)query_->evaluate_number(node);
  911. }
  912. String XPathQuery::EvaluateToString(XMLElement element) const
  913. {
  914. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  915. return String::EMPTY;
  916. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node() : pugi::xml_node(element.GetNode());
  917. String result;
  918. // First call get the size
  919. result.Reserve((unsigned)query_->evaluate_string(0, 0, node));
  920. // Second call get the actual string
  921. query_->evaluate_string(const_cast<pugi::char_t*>(result.CString()), result.Capacity(), node);
  922. return result;
  923. }
  924. XPathResultSet XPathQuery::Evaluate(XMLElement element) const
  925. {
  926. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  927. return XPathResultSet();
  928. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node() : pugi::xml_node(element.GetNode());
  929. pugi::xpath_node_set result = query_->evaluate_node_set(node);
  930. return XPathResultSet(element.GetFile(), &result);
  931. }
  932. }