XMLElement.cpp 30 KB

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