XMLElement.cpp 33 KB

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