XMLElement.cpp 30 KB

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