XMLElement.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. //
  2. // Copyright (c) 2008-2017 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/pugixml.hpp>
  27. #include "../DebugNew.h"
  28. namespace Urho3D
  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. XMLElement XMLElement::GetOrCreateChild(const String& name)
  94. {
  95. XMLElement child = GetChild(name);
  96. if (child.NotNull())
  97. return child;
  98. else
  99. return CreateChild(name);
  100. }
  101. XMLElement XMLElement::GetOrCreateChild(const char* name)
  102. {
  103. XMLElement child = GetChild(name);
  104. if (child.NotNull())
  105. return child;
  106. else
  107. return CreateChild(name);
  108. }
  109. bool XMLElement::RemoveChild(const XMLElement& element)
  110. {
  111. if (!element.file_ || (!element.node_ && !element.xpathNode_) || !file_ || (!node_ && !xpathNode_))
  112. return false;
  113. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  114. const pugi::xml_node& child = element.xpathNode_ ? element.xpathNode_->node() : pugi::xml_node(element.node_);
  115. return const_cast<pugi::xml_node&>(node).remove_child(child);
  116. }
  117. bool XMLElement::RemoveChild(const String& name)
  118. {
  119. return RemoveChild(name.CString());
  120. }
  121. bool XMLElement::RemoveChild(const char* name)
  122. {
  123. if (!file_ || (!node_ && !xpathNode_))
  124. return false;
  125. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  126. return const_cast<pugi::xml_node&>(node).remove_child(name);
  127. }
  128. bool XMLElement::RemoveChildren(const String& name)
  129. {
  130. return RemoveChildren(name.CString());
  131. }
  132. bool XMLElement::RemoveChildren(const char* name)
  133. {
  134. if ((!file_ || !node_) && !xpathNode_)
  135. return false;
  136. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  137. if (!String::CStringLength(name))
  138. {
  139. for (;;)
  140. {
  141. pugi::xml_node child = node.last_child();
  142. if (child.empty())
  143. break;
  144. const_cast<pugi::xml_node&>(node).remove_child(child);
  145. }
  146. }
  147. else
  148. {
  149. for (;;)
  150. {
  151. pugi::xml_node child = node.child(name);
  152. if (child.empty())
  153. break;
  154. const_cast<pugi::xml_node&>(node).remove_child(child);
  155. }
  156. }
  157. return true;
  158. }
  159. bool XMLElement::RemoveAttribute(const String& name)
  160. {
  161. return RemoveAttribute(name.CString());
  162. }
  163. bool XMLElement::RemoveAttribute(const char* name)
  164. {
  165. if (!file_ || (!node_ && !xpathNode_))
  166. return false;
  167. // If xpath_node contains just attribute, remove it regardless of the specified name
  168. if (xpathNode_ && xpathNode_->attribute())
  169. return xpathNode_->parent().remove_attribute(
  170. xpathNode_->attribute()); // In attribute context, xpath_node's parent is the parent node of the attribute itself
  171. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  172. return const_cast<pugi::xml_node&>(node).remove_attribute(node.attribute(name));
  173. }
  174. XMLElement XMLElement::SelectSingle(const String& query, pugi::xpath_variable_set* variables) const
  175. {
  176. if (!file_ || (!node_ && !xpathNode_))
  177. return XMLElement();
  178. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  179. pugi::xpath_node result = node.select_single_node(query.CString(), variables);
  180. return XMLElement(file_, 0, &result, 0);
  181. }
  182. XMLElement XMLElement::SelectSinglePrepared(const XPathQuery& query) const
  183. {
  184. if (!file_ || (!node_ && !xpathNode_ && !query.GetXPathQuery()))
  185. return XMLElement();
  186. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  187. pugi::xpath_node result = node.select_single_node(*query.GetXPathQuery());
  188. return XMLElement(file_, 0, &result, 0);
  189. }
  190. XPathResultSet XMLElement::Select(const String& query, pugi::xpath_variable_set* variables) const
  191. {
  192. if (!file_ || (!node_ && !xpathNode_))
  193. return XPathResultSet();
  194. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  195. pugi::xpath_node_set result = node.select_nodes(query.CString(), variables);
  196. return XPathResultSet(file_, &result);
  197. }
  198. XPathResultSet XMLElement::SelectPrepared(const XPathQuery& query) const
  199. {
  200. if (!file_ || (!node_ && !xpathNode_ && query.GetXPathQuery()))
  201. return XPathResultSet();
  202. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  203. pugi::xpath_node_set result = node.select_nodes(*query.GetXPathQuery());
  204. return XPathResultSet(file_, &result);
  205. }
  206. bool XMLElement::SetValue(const String& value)
  207. {
  208. return SetValue(value.CString());
  209. }
  210. bool XMLElement::SetValue(const char* value)
  211. {
  212. if (!file_ || (!node_ && !xpathNode_))
  213. return false;
  214. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  215. // Search for existing value first
  216. for (pugi::xml_node child = node.first_child(); child; child = child.next_sibling())
  217. {
  218. if (child.type() == pugi::node_pcdata)
  219. return const_cast<pugi::xml_node&>(child).set_value(value);
  220. }
  221. // If no previous value found, append new
  222. return const_cast<pugi::xml_node&>(node).append_child(pugi::node_pcdata).set_value(value);
  223. }
  224. bool XMLElement::SetAttribute(const String& name, const String& value)
  225. {
  226. return SetAttribute(name.CString(), value.CString());
  227. }
  228. bool XMLElement::SetAttribute(const char* name, const char* value)
  229. {
  230. if (!file_ || (!node_ && !xpathNode_))
  231. return false;
  232. // If xpath_node contains just attribute, set its value regardless of the specified name
  233. if (xpathNode_ && xpathNode_->attribute())
  234. return xpathNode_->attribute().set_value(value);
  235. const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
  236. pugi::xml_attribute attr = node.attribute(name);
  237. if (attr.empty())
  238. attr = const_cast<pugi::xml_node&>(node).append_attribute(name);
  239. return attr.set_value(value);
  240. }
  241. bool XMLElement::SetAttribute(const String& value)
  242. {
  243. return SetAttribute(value.CString());
  244. }
  245. bool XMLElement::SetAttribute(const char* value)
  246. {
  247. // If xpath_node contains just attribute, set its value
  248. return xpathNode_ && xpathNode_->attribute() && xpathNode_->attribute().set_value(value);
  249. }
  250. bool XMLElement::SetBool(const String& name, bool value)
  251. {
  252. return SetAttribute(name, String(value));
  253. }
  254. bool XMLElement::SetBoundingBox(const BoundingBox& value)
  255. {
  256. if (!SetVector3("min", value.min_))
  257. return false;
  258. return SetVector3("max", value.max_);
  259. }
  260. bool XMLElement::SetBuffer(const String& name, const void* data, unsigned size)
  261. {
  262. String dataStr;
  263. BufferToString(dataStr, data, size);
  264. return SetAttribute(name, dataStr);
  265. }
  266. bool XMLElement::SetBuffer(const String& name, const PODVector<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::SetUInt(const String& name, unsigned value)
  286. {
  287. return SetAttribute(name, String(value));
  288. }
  289. bool XMLElement::SetInt(const String& name, int value)
  290. {
  291. return SetAttribute(name, String(value));
  292. }
  293. bool XMLElement::SetUInt64(const String& name, unsigned long long value)
  294. {
  295. return SetAttribute(name, String(value));
  296. }
  297. bool XMLElement::SetInt64(const String& name, long long 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 (unsigned i = 0; i < value.names_.Size(); ++i)
  365. {
  366. str += ";";
  367. str += value.names_[i];
  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.SetUInt("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. unsigned 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. unsigned 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 0;
  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. PODVector<unsigned char> XMLElement::GetBuffer(const String& name) const
  608. {
  609. PODVector<unsigned char> ret;
  610. StringToBuffer(ret, GetAttribute(name));
  611. return ret;
  612. }
  613. bool XMLElement::GetBuffer(const String& name, void* dest, unsigned size) const
  614. {
  615. Vector<String> bytes = GetAttribute(name).Split(' ');
  616. if (size < bytes.Size())
  617. return false;
  618. unsigned char* destBytes = (unsigned char*)dest;
  619. for (unsigned i = 0; i < bytes.Size(); ++i)
  620. destBytes[i] = (unsigned char)ToInt(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. unsigned XMLElement::GetUInt(const String& name) const
  636. {
  637. return ToUInt(GetAttribute(name));
  638. }
  639. int XMLElement::GetInt(const String& name) const
  640. {
  641. return ToInt(GetAttribute(name));
  642. }
  643. unsigned long long XMLElement::GetUInt64(const String& name) const
  644. {
  645. return ToUInt64(GetAttribute(name));
  646. }
  647. long long XMLElement::GetInt64(const String& name) const
  648. {
  649. return ToInt64(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 (unsigned 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.GetUInt("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_(0)
  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()) : 0)
  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()) : 0)
  811. {
  812. }
  813. XPathResultSet::~XPathResultSet()
  814. {
  815. delete resultSet_;
  816. resultSet_ = 0;
  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()) : 0;
  822. return *this;
  823. }
  824. XMLElement XPathResultSet::operator [](unsigned index) const
  825. {
  826. if (!resultSet_)
  827. URHO3D_LOGERRORF(
  828. "Could not return result at index: %u. Most probably this is caused by the XPathResultSet not being stored in a lhs variable.",
  829. index);
  830. return resultSet_ && index < Size() ? XMLElement(file_, this, &resultSet_->operator [](index), index) : XMLElement();
  831. }
  832. XMLElement XPathResultSet::FirstResult()
  833. {
  834. return operator [](0);
  835. }
  836. unsigned XPathResultSet::Size() const
  837. {
  838. return resultSet_ ? (unsigned)resultSet_->size() : 0;
  839. }
  840. bool XPathResultSet::Empty() const
  841. {
  842. return resultSet_ ? resultSet_->empty() : true;
  843. }
  844. XPathQuery::XPathQuery()
  845. {
  846. }
  847. XPathQuery::XPathQuery(const String& queryString, const String& variableString)
  848. {
  849. SetQuery(queryString, variableString);
  850. }
  851. XPathQuery::~XPathQuery()
  852. {
  853. }
  854. void XPathQuery::Bind()
  855. {
  856. // Delete previous query object and create a new one binding it with variable set
  857. query_ = new pugi::xpath_query(queryString_.CString(), variables_.Get());
  858. }
  859. bool XPathQuery::SetVariable(const String& name, bool value)
  860. {
  861. if (!variables_)
  862. variables_ = new pugi::xpath_variable_set();
  863. return variables_->set(name.CString(), value);
  864. }
  865. bool XPathQuery::SetVariable(const String& name, float value)
  866. {
  867. if (!variables_)
  868. variables_ = new pugi::xpath_variable_set();
  869. return variables_->set(name.CString(), value);
  870. }
  871. bool XPathQuery::SetVariable(const String& name, const String& value)
  872. {
  873. return SetVariable(name.CString(), value.CString());
  874. }
  875. bool XPathQuery::SetVariable(const char* name, const char* value)
  876. {
  877. if (!variables_)
  878. variables_ = new pugi::xpath_variable_set();
  879. return variables_->set(name, value);
  880. }
  881. bool XPathQuery::SetVariable(const String& name, const XPathResultSet& value)
  882. {
  883. if (!variables_)
  884. variables_ = new pugi::xpath_variable_set();
  885. pugi::xpath_node_set* nodeSet = value.GetXPathNodeSet();
  886. if (!nodeSet)
  887. return false;
  888. return variables_->set(name.CString(), *nodeSet);
  889. }
  890. bool XPathQuery::SetQuery(const String& queryString, const String& variableString, bool bind)
  891. {
  892. if (!variableString.Empty())
  893. {
  894. Clear();
  895. variables_ = new pugi::xpath_variable_set();
  896. // Parse the variable string having format "name1:type1,name2:type2,..." where type is one of "Bool", "Float", "String", "ResultSet"
  897. Vector<String> vars = variableString.Split(',');
  898. for (Vector<String>::ConstIterator i = vars.Begin(); i != vars.End(); ++i)
  899. {
  900. Vector<String> tokens = i->Trimmed().Split(':');
  901. if (tokens.Size() != 2)
  902. continue;
  903. pugi::xpath_value_type type;
  904. if (tokens[1] == "Bool")
  905. type = pugi::xpath_type_boolean;
  906. else if (tokens[1] == "Float")
  907. type = pugi::xpath_type_number;
  908. else if (tokens[1] == "String")
  909. type = pugi::xpath_type_string;
  910. else if (tokens[1] == "ResultSet")
  911. type = pugi::xpath_type_node_set;
  912. else
  913. return false;
  914. if (!variables_->add(tokens[0].CString(), type))
  915. return false;
  916. }
  917. }
  918. queryString_ = queryString;
  919. if (bind)
  920. Bind();
  921. return true;
  922. }
  923. void XPathQuery::Clear()
  924. {
  925. queryString_.Clear();
  926. variables_.Reset();
  927. query_.Reset();
  928. }
  929. bool XPathQuery::EvaluateToBool(XMLElement element) const
  930. {
  931. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  932. return false;
  933. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node() : pugi::xml_node(element.GetNode());
  934. return query_->evaluate_boolean(node);
  935. }
  936. float XPathQuery::EvaluateToFloat(XMLElement element) const
  937. {
  938. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  939. return 0.0f;
  940. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node() : pugi::xml_node(element.GetNode());
  941. return (float)query_->evaluate_number(node);
  942. }
  943. String XPathQuery::EvaluateToString(XMLElement element) const
  944. {
  945. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  946. return String::EMPTY;
  947. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node() : pugi::xml_node(element.GetNode());
  948. String result;
  949. // First call get the size
  950. result.Reserve((unsigned)query_->evaluate_string(0, 0, node));
  951. // Second call get the actual string
  952. query_->evaluate_string(const_cast<pugi::char_t*>(result.CString()), result.Capacity(), node);
  953. return result;
  954. }
  955. XPathResultSet XPathQuery::Evaluate(XMLElement element) const
  956. {
  957. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  958. return XPathResultSet();
  959. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node() : pugi::xml_node(element.GetNode());
  960. pugi::xpath_node_set result = query_->evaluate_node_set(node);
  961. return XPathResultSet(element.GetFile(), &result);
  962. }
  963. }