XMLElement.cpp 32 KB

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