XMLElement.cpp 31 KB

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