XMLElement.cpp 30 KB

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