XMLElement.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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. {
  721. // ATOMIC BEGIN
  722. const String name = variantElem.GetAttribute("name");
  723. // register the name in the variant map, as significant for reverse lookup from hash
  724. StringHash::RegisterSignificantString(name);
  725. ret[StringHash(name)] = variantElem.GetVariant();
  726. // ATOMIC END
  727. }
  728. else if (variantElem.HasAttribute("hash"))
  729. ret[StringHash(variantElem.GetUInt("hash"))] = variantElem.GetVariant();
  730. variantElem = variantElem.GetNext("variant");
  731. }
  732. return ret;
  733. }
  734. Vector2 XMLElement::GetVector2(const String& name) const
  735. {
  736. return ToVector2(GetAttribute(name));
  737. }
  738. Vector3 XMLElement::GetVector3(const String& name) const
  739. {
  740. return ToVector3(GetAttribute(name));
  741. }
  742. Vector4 XMLElement::GetVector4(const String& name) const
  743. {
  744. return ToVector4(GetAttribute(name));
  745. }
  746. Vector4 XMLElement::GetVector(const String& name) const
  747. {
  748. return ToVector4(GetAttribute(name), true);
  749. }
  750. Variant XMLElement::GetVectorVariant(const String& name) const
  751. {
  752. return ToVectorVariant(GetAttribute(name));
  753. }
  754. Matrix3 XMLElement::GetMatrix3(const String& name) const
  755. {
  756. return ToMatrix3(GetAttribute(name));
  757. }
  758. Matrix3x4 XMLElement::GetMatrix3x4(const String& name) const
  759. {
  760. return ToMatrix3x4(GetAttribute(name));
  761. }
  762. Matrix4 XMLElement::GetMatrix4(const String& name) const
  763. {
  764. return ToMatrix4(GetAttribute(name));
  765. }
  766. XMLFile* XMLElement::GetFile() const
  767. {
  768. return file_;
  769. }
  770. XMLElement XMLElement::NextResult() const
  771. {
  772. if (!xpathResultSet_ || !xpathNode_)
  773. return XMLElement();
  774. return xpathResultSet_->operator [](++xpathResultIndex_);
  775. }
  776. XPathResultSet::XPathResultSet() :
  777. resultSet_(0)
  778. {
  779. }
  780. XPathResultSet::XPathResultSet(XMLFile* file, pugi::xpath_node_set* resultSet) :
  781. file_(file),
  782. resultSet_(resultSet ? new pugi::xpath_node_set(resultSet->begin(), resultSet->end()) : 0)
  783. {
  784. // Sort the node set in forward document order
  785. if (resultSet_)
  786. resultSet_->sort();
  787. }
  788. XPathResultSet::XPathResultSet(const XPathResultSet& rhs) :
  789. file_(rhs.file_),
  790. resultSet_(rhs.resultSet_ ? new pugi::xpath_node_set(rhs.resultSet_->begin(), rhs.resultSet_->end()) : 0)
  791. {
  792. }
  793. XPathResultSet::~XPathResultSet()
  794. {
  795. delete resultSet_;
  796. resultSet_ = 0;
  797. }
  798. XPathResultSet& XPathResultSet::operator =(const XPathResultSet& rhs)
  799. {
  800. file_ = rhs.file_;
  801. resultSet_ = rhs.resultSet_ ? new pugi::xpath_node_set(rhs.resultSet_->begin(), rhs.resultSet_->end()) : 0;
  802. return *this;
  803. }
  804. XMLElement XPathResultSet::operator [](unsigned index) const
  805. {
  806. if (!resultSet_)
  807. ATOMIC_LOGERRORF(
  808. "Could not return result at index: %u. Most probably this is caused by the XPathResultSet not being stored in a lhs variable.",
  809. index);
  810. return resultSet_ && index < Size() ? XMLElement(file_, this, &resultSet_->operator [](index), index) : XMLElement();
  811. }
  812. XMLElement XPathResultSet::FirstResult()
  813. {
  814. return operator [](0);
  815. }
  816. unsigned XPathResultSet::Size() const
  817. {
  818. return resultSet_ ? (unsigned)resultSet_->size() : 0;
  819. }
  820. bool XPathResultSet::Empty() const
  821. {
  822. return resultSet_ ? resultSet_->empty() : true;
  823. }
  824. XPathQuery::XPathQuery() :
  825. query_(0),
  826. variables_(0)
  827. {
  828. }
  829. XPathQuery::XPathQuery(const String& queryString, const String& variableString) :
  830. query_(0),
  831. variables_(0)
  832. {
  833. SetQuery(queryString, variableString);
  834. }
  835. XPathQuery::~XPathQuery()
  836. {
  837. delete variables_;
  838. variables_ = 0;
  839. delete query_;
  840. query_ = 0;
  841. }
  842. void XPathQuery::Bind()
  843. {
  844. // Delete previous query object and create a new one binding it with variable set
  845. delete query_;
  846. query_ = new pugi::xpath_query(queryString_.CString(), variables_);
  847. }
  848. bool XPathQuery::SetVariable(const String& name, bool value)
  849. {
  850. if (!variables_)
  851. variables_ = new pugi::xpath_variable_set();
  852. return variables_->set(name.CString(), value);
  853. }
  854. bool XPathQuery::SetVariable(const String& name, float value)
  855. {
  856. if (!variables_)
  857. variables_ = new pugi::xpath_variable_set();
  858. return variables_->set(name.CString(), value);
  859. }
  860. bool XPathQuery::SetVariable(const String& name, const String& value)
  861. {
  862. return SetVariable(name.CString(), value.CString());
  863. }
  864. bool XPathQuery::SetVariable(const char* name, const char* value)
  865. {
  866. if (!variables_)
  867. variables_ = new pugi::xpath_variable_set();
  868. return variables_->set(name, value);
  869. }
  870. bool XPathQuery::SetVariable(const String& name, const XPathResultSet& value)
  871. {
  872. if (!variables_)
  873. variables_ = new pugi::xpath_variable_set();
  874. pugi::xpath_node_set* nodeSet = value.GetXPathNodeSet();
  875. if (!nodeSet)
  876. return false;
  877. return variables_->set(name.CString(), *nodeSet);
  878. }
  879. bool XPathQuery::SetQuery(const String& queryString, const String& variableString, bool bind)
  880. {
  881. if (!variableString.Empty())
  882. {
  883. Clear();
  884. variables_ = new pugi::xpath_variable_set();
  885. // Parse the variable string having format "name1:type1,name2:type2,..." where type is one of "Bool", "Float", "String", "ResultSet"
  886. Vector<String> vars = variableString.Split(',');
  887. for (Vector<String>::ConstIterator i = vars.Begin(); i != vars.End(); ++i)
  888. {
  889. Vector<String> tokens = i->Trimmed().Split(':');
  890. if (tokens.Size() != 2)
  891. continue;
  892. pugi::xpath_value_type type;
  893. if (tokens[1] == "Bool")
  894. type = pugi::xpath_type_boolean;
  895. else if (tokens[1] == "Float")
  896. type = pugi::xpath_type_number;
  897. else if (tokens[1] == "String")
  898. type = pugi::xpath_type_string;
  899. else if (tokens[1] == "ResultSet")
  900. type = pugi::xpath_type_node_set;
  901. else
  902. return false;
  903. if (!variables_->add(tokens[0].CString(), type))
  904. return false;
  905. }
  906. }
  907. queryString_ = queryString;
  908. if (bind)
  909. Bind();
  910. return true;
  911. }
  912. void XPathQuery::Clear()
  913. {
  914. queryString_.Clear();
  915. delete variables_;
  916. variables_ = 0;
  917. delete query_;
  918. query_ = 0;
  919. }
  920. bool XPathQuery::EvaluateToBool(XMLElement element) const
  921. {
  922. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  923. return false;
  924. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node() : pugi::xml_node(element.GetNode());
  925. return query_->evaluate_boolean(node);
  926. }
  927. float XPathQuery::EvaluateToFloat(XMLElement element) const
  928. {
  929. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  930. return 0.0f;
  931. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node() : pugi::xml_node(element.GetNode());
  932. return (float)query_->evaluate_number(node);
  933. }
  934. String XPathQuery::EvaluateToString(XMLElement element) const
  935. {
  936. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  937. return String::EMPTY;
  938. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node() : pugi::xml_node(element.GetNode());
  939. String result;
  940. // First call get the size
  941. result.Reserve((unsigned)query_->evaluate_string(0, 0, node));
  942. // Second call get the actual string
  943. query_->evaluate_string(const_cast<pugi::char_t*>(result.CString()), result.Capacity(), node);
  944. return result;
  945. }
  946. XPathResultSet XPathQuery::Evaluate(XMLElement element) const
  947. {
  948. if (!query_ || ((!element.GetFile() || !element.GetNode()) && !element.GetXPathNode()))
  949. return XPathResultSet();
  950. const pugi::xml_node& node = element.GetXPathNode() ? element.GetXPathNode()->node() : pugi::xml_node(element.GetNode());
  951. pugi::xpath_node_set result = query_->evaluate_node_set(node);
  952. return XPathResultSet(element.GetFile(), &result);
  953. }
  954. }