XMLElement.cpp 31 KB

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