llxmlnode.cpp 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143
  1. /**
  2. * @file llxmlnode.cpp
  3. * @author Tom Yedwab
  4. * @brief LLXMLNode implementation
  5. *
  6. * $LicenseInfo:firstyear=2005&license=viewergpl$
  7. *
  8. * Copyright (c) 2005-2009, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #include "linden_common.h"
  34. #include <iostream>
  35. #include "llxmlnode.h"
  36. #include "llbase64.h"
  37. #include "llcolor3.h"
  38. #include "llcolor4.h"
  39. #include "llcolor4u.h"
  40. #include "llvector3.h"
  41. #include "llvector3d.h"
  42. #include "llvector4.h"
  43. #include "llquaternion.h"
  44. #include "llsd.h"
  45. //static
  46. bool LLXMLNode::sStripEscapedStrings = true;
  47. bool LLXMLNode::sStripWhitespaceValues = false;
  48. LLXMLNode::LLXMLNode()
  49. : mParser(NULL),
  50. mIsAttribute(false),
  51. mVersionMajor(0),
  52. mVersionMinor(0),
  53. mLength(0),
  54. mPrecision(64),
  55. mType(TYPE_CONTAINER),
  56. mEncoding(ENCODING_DEFAULT),
  57. mLineNumber(-1),
  58. mParent(NULL),
  59. mChildren(NULL),
  60. mAttributes(),
  61. mPrev(NULL),
  62. mNext(NULL),
  63. mName(NULL),
  64. mDefault(NULL)
  65. {
  66. }
  67. LLXMLNode::LLXMLNode(const char* name, bool is_attribute)
  68. : mParser(NULL),
  69. mIsAttribute(is_attribute),
  70. mVersionMajor(0),
  71. mVersionMinor(0),
  72. mLength(0),
  73. mPrecision(64),
  74. mType(TYPE_CONTAINER),
  75. mEncoding(ENCODING_DEFAULT),
  76. mLineNumber(-1),
  77. mParent(NULL),
  78. mChildren(NULL),
  79. mAttributes(),
  80. mPrev(NULL),
  81. mNext(NULL),
  82. mDefault(NULL)
  83. {
  84. mName = gStringTable.addStringEntry(name);
  85. }
  86. LLXMLNode::LLXMLNode(LLStringTableEntry* name, bool is_attribute)
  87. : mParser(NULL),
  88. mIsAttribute(is_attribute),
  89. mVersionMajor(0),
  90. mVersionMinor(0),
  91. mLength(0),
  92. mPrecision(64),
  93. mType(TYPE_CONTAINER),
  94. mEncoding(ENCODING_DEFAULT),
  95. mLineNumber(-1),
  96. mParent(NULL),
  97. mChildren(NULL),
  98. mAttributes(),
  99. mPrev(NULL),
  100. mNext(NULL),
  101. mName(name),
  102. mDefault(NULL)
  103. {
  104. }
  105. // copy constructor (except for the children)
  106. LLXMLNode::LLXMLNode(const LLXMLNode& rhs)
  107. : mID(rhs.mID),
  108. mIsAttribute(rhs.mIsAttribute),
  109. mVersionMajor(rhs.mVersionMajor),
  110. mVersionMinor(rhs.mVersionMinor),
  111. mLength(rhs.mLength),
  112. mPrecision(rhs.mPrecision),
  113. mType(rhs.mType),
  114. mEncoding(rhs.mEncoding),
  115. mLineNumber(0),
  116. mParser(NULL),
  117. mParent(NULL),
  118. mChildren(NULL),
  119. mAttributes(),
  120. mPrev(NULL),
  121. mNext(NULL),
  122. mName(rhs.mName),
  123. mValue(rhs.mValue),
  124. mDefault(rhs.mDefault)
  125. {
  126. }
  127. // returns a new copy of this node and all its children
  128. LLXMLNodePtr LLXMLNode::deepCopy()
  129. {
  130. LLXMLNodePtr newnode = LLXMLNodePtr(new LLXMLNode(*this));
  131. if (mChildren.notNull())
  132. {
  133. for (LLXMLChildList::iterator it = mChildren->map.begin(),
  134. end = mChildren->map.end();
  135. it != end; ++it)
  136. {
  137. LLXMLNodePtr temp_ptr_for_gcc(it->second->deepCopy());
  138. newnode->addChild(temp_ptr_for_gcc);
  139. }
  140. }
  141. for (LLXMLAttribList::iterator it = mAttributes.begin(),
  142. end = mAttributes.end();
  143. it != end; ++it)
  144. {
  145. LLXMLNodePtr temp_ptr_for_gcc(it->second->deepCopy());
  146. newnode->addChild(temp_ptr_for_gcc);
  147. }
  148. return newnode;
  149. }
  150. // virtual
  151. LLXMLNode::~LLXMLNode()
  152. {
  153. // Strictly speaking none of this should be required except
  154. // 'delete mChildren'...
  155. // Sadly, that is only true if we had not had reference-counted smart
  156. // pointers linked in three different directions. This entire class is a
  157. // frightening, hard-to-maintain mess.
  158. if (mChildren.notNull())
  159. {
  160. for (LLXMLChildList::iterator it = mChildren->map.begin(),
  161. end = mChildren->map.end();
  162. it != end; ++it)
  163. {
  164. LLXMLNodePtr child = it->second;
  165. if (child.notNull())
  166. {
  167. child->mParent = NULL;
  168. child->mNext = NULL;
  169. child->mPrev = NULL;
  170. }
  171. }
  172. mChildren->map.clear();
  173. mChildren->head = NULL;
  174. mChildren->tail = NULL;
  175. mChildren = NULL;
  176. }
  177. for (LLXMLAttribList::iterator it = mAttributes.begin(),
  178. end = mAttributes.end();
  179. it != end; ++it)
  180. {
  181. LLXMLNodePtr attr = it->second;
  182. if (attr.notNull())
  183. {
  184. attr->mParent = NULL;
  185. attr->mNext = NULL;
  186. attr->mPrev = NULL;
  187. }
  188. }
  189. llassert(mParent == NULL);
  190. mDefault = NULL;
  191. }
  192. // protected
  193. bool LLXMLNode::removeChild(LLXMLNode* target_child)
  194. {
  195. if (!target_child)
  196. {
  197. return false;
  198. }
  199. if (target_child->mIsAttribute)
  200. {
  201. LLXMLAttribList::iterator it = mAttributes.find(target_child->mName);
  202. if (it != mAttributes.end())
  203. {
  204. target_child->mParent = NULL;
  205. mAttributes.erase(it);
  206. return true;
  207. }
  208. }
  209. else if (mChildren.notNull())
  210. {
  211. LLXMLChildList::iterator it = mChildren->map.find(target_child->mName);
  212. while (it != mChildren->map.end())
  213. {
  214. if (target_child == it->second)
  215. {
  216. if (target_child == mChildren->head)
  217. {
  218. mChildren->head = target_child->mNext;
  219. }
  220. if (target_child == mChildren->tail)
  221. {
  222. mChildren->tail = target_child->mPrev;
  223. }
  224. LLXMLNodePtr prev = target_child->mPrev;
  225. LLXMLNodePtr next = target_child->mNext;
  226. if (prev.notNull()) prev->mNext = next;
  227. if (next.notNull()) next->mPrev = prev;
  228. target_child->mPrev = NULL;
  229. target_child->mNext = NULL;
  230. target_child->mParent = NULL;
  231. mChildren->map.erase(it);
  232. if (mChildren->map.empty())
  233. {
  234. mChildren = NULL;
  235. }
  236. return true;
  237. }
  238. else if (it->first != target_child->mName)
  239. {
  240. break;
  241. }
  242. else
  243. {
  244. ++it;
  245. }
  246. }
  247. }
  248. return false;
  249. }
  250. void LLXMLNode::addChild(LLXMLNodePtr new_child, LLXMLNodePtr after_child)
  251. {
  252. if (new_child.isNull())
  253. {
  254. llassert(false);
  255. return;
  256. }
  257. if (new_child->mParent)
  258. {
  259. if (new_child->mParent == this)
  260. {
  261. return;
  262. }
  263. new_child->mParent->removeChild(new_child);
  264. }
  265. new_child->mParent = this;
  266. if (new_child->mIsAttribute)
  267. {
  268. mAttributes.emplace(new_child->mName, new_child);
  269. }
  270. else
  271. {
  272. if (mChildren.isNull())
  273. {
  274. mChildren = new LLXMLChildren();
  275. mChildren->head = new_child;
  276. mChildren->tail = new_child;
  277. }
  278. mChildren->map.emplace(new_child->mName, new_child);
  279. // if after_child is specified, it damn well better be in the list of
  280. // children for this node. I am not going to assert that, because it
  281. // would be expensive, but don't specify that parameter if you did not
  282. // get the value for it from the list of children of this node!
  283. if (after_child.isNull())
  284. {
  285. if (mChildren->tail != new_child)
  286. {
  287. mChildren->tail->mNext = new_child;
  288. new_child->mPrev = mChildren->tail;
  289. mChildren->tail = new_child;
  290. }
  291. }
  292. // If after_child == parent, then put new_child at beginning
  293. else if (after_child == this)
  294. {
  295. // Add to front of list
  296. new_child->mNext = mChildren->head;
  297. if (mChildren->head)
  298. {
  299. mChildren->head->mPrev = new_child;
  300. mChildren->head = new_child;
  301. }
  302. else // no children
  303. {
  304. mChildren->head = new_child;
  305. mChildren->tail = new_child;
  306. }
  307. }
  308. else
  309. {
  310. if (after_child->mNext.notNull())
  311. {
  312. // If after_child was not the last item, fix up some pointers
  313. after_child->mNext->mPrev = new_child;
  314. new_child->mNext = after_child->mNext;
  315. }
  316. new_child->mPrev = after_child;
  317. after_child->mNext = new_child;
  318. if (mChildren->tail == after_child)
  319. {
  320. mChildren->tail = new_child;
  321. }
  322. }
  323. }
  324. new_child->updateDefault();
  325. }
  326. // virtual
  327. LLXMLNodePtr LLXMLNode::createChild(const char* name, bool is_attribute)
  328. {
  329. return createChild(gStringTable.addStringEntry(name), is_attribute);
  330. }
  331. // virtual
  332. LLXMLNodePtr LLXMLNode::createChild(LLStringTableEntry* name,
  333. bool is_attribute)
  334. {
  335. LLXMLNode* ret = new LLXMLNode(name, is_attribute);
  336. ret->mID.clear();
  337. addChild(ret);
  338. return ret;
  339. }
  340. bool LLXMLNode::deleteChild(LLXMLNode* child)
  341. {
  342. return removeChild(child);
  343. }
  344. void LLXMLNode::setParent(LLXMLNodePtr new_parent)
  345. {
  346. if (new_parent.notNull())
  347. {
  348. new_parent->addChild(this);
  349. }
  350. else if (mParent)
  351. {
  352. LLXMLNodePtr old_parent = mParent;
  353. mParent = NULL;
  354. old_parent->removeChild(this);
  355. }
  356. }
  357. void LLXMLNode::updateDefault()
  358. {
  359. if (mParent && !mParent->mDefault.isNull())
  360. {
  361. mDefault = NULL;
  362. // Find default value in parent's default tree
  363. if (!mParent->mDefault.isNull())
  364. {
  365. findDefault(mParent->mDefault);
  366. }
  367. }
  368. if (mChildren.notNull())
  369. {
  370. for (LLXMLChildList::const_iterator it = mChildren->map.begin(),
  371. end = mChildren->map.end();
  372. it != end; ++it)
  373. {
  374. LLXMLNodePtr child = it->second;
  375. if (child.notNull())
  376. {
  377. child->updateDefault();
  378. }
  379. }
  380. }
  381. }
  382. void XMLCALL StartXMLNode(void* user_data, const XML_Char* name,
  383. const XML_Char** atts)
  384. {
  385. if (!user_data)
  386. {
  387. llwarns << "Parent (user_data) is NULL; aborting." << llendl;
  388. return;
  389. }
  390. // Create a new node
  391. LLXMLNode* new_node_ptr = new LLXMLNode(name, false);
  392. LLXMLNodePtr new_node = new_node_ptr;
  393. new_node->mID.clear();
  394. LLXMLNodePtr ptr_new_node = new_node;
  395. // Set the parent-child relationship with the current active node
  396. LLXMLNode* parent = (LLXMLNode*)user_data;
  397. new_node_ptr->mParser = parent->mParser;
  398. new_node_ptr->setLineNumber(XML_GetCurrentLineNumber(*new_node_ptr->mParser));
  399. // Set the current active node to the new node
  400. XML_Parser* parser = parent->mParser;
  401. XML_SetUserData(*parser, (void*)new_node_ptr);
  402. // Parse attributes
  403. U32 pos = 0;
  404. std::string attr_name, attr_value;
  405. while (atts[pos])
  406. {
  407. attr_name = atts[pos];
  408. attr_value = atts[pos + 1];
  409. // Special cases
  410. if ('i' == attr_name[0] && "id" == attr_name)
  411. {
  412. new_node->mID = attr_value;
  413. }
  414. else if ('v' == attr_name[0] && "version" == attr_name)
  415. {
  416. U32 version_major = 0;
  417. U32 version_minor = 0;
  418. if (sscanf(attr_value.c_str(), "%d.%d", &version_major,
  419. &version_minor) > 0)
  420. {
  421. new_node->mVersionMajor = version_major;
  422. new_node->mVersionMinor = version_minor;
  423. }
  424. }
  425. else if (('s' == attr_name[0] && "size" == attr_name) ||
  426. ('l' == attr_name[0] && "length" == attr_name))
  427. {
  428. U32 length;
  429. if (sscanf(attr_value.c_str(), "%d", &length) > 0)
  430. {
  431. new_node->mLength = length;
  432. }
  433. }
  434. else if ('p' == attr_name[0] && "precision" == attr_name)
  435. {
  436. U32 precision;
  437. if (sscanf(attr_value.c_str(), "%d", &precision) > 0)
  438. {
  439. new_node->mPrecision = precision;
  440. }
  441. }
  442. else if ('t' == attr_name[0] && "type" == attr_name)
  443. {
  444. if ("boolean" == attr_value)
  445. {
  446. new_node->mType = LLXMLNode::TYPE_BOOLEAN;
  447. }
  448. else if ("integer" == attr_value)
  449. {
  450. new_node->mType = LLXMLNode::TYPE_INTEGER;
  451. }
  452. else if ("float" == attr_value)
  453. {
  454. new_node->mType = LLXMLNode::TYPE_FLOAT;
  455. }
  456. else if ("string" == attr_value)
  457. {
  458. new_node->mType = LLXMLNode::TYPE_STRING;
  459. }
  460. else if ("uuid" == attr_value)
  461. {
  462. new_node->mType = LLXMLNode::TYPE_UUID;
  463. }
  464. else if ("noderef" == attr_value)
  465. {
  466. new_node->mType = LLXMLNode::TYPE_NODEREF;
  467. }
  468. }
  469. else if ('e' == attr_name[0] && "encoding" == attr_name)
  470. {
  471. if ("decimal" == attr_value)
  472. {
  473. new_node->mEncoding = LLXMLNode::ENCODING_DECIMAL;
  474. }
  475. else if ("hex" == attr_value)
  476. {
  477. new_node->mEncoding = LLXMLNode::ENCODING_HEX;
  478. }
  479. /*else if (attr_value == "base32")
  480. {
  481. new_node->mEncoding = LLXMLNode::ENCODING_BASE32;
  482. }*/
  483. }
  484. // only one attribute child per description
  485. LLXMLNodePtr attr_node;
  486. if (!new_node->getAttribute(attr_name.c_str(), attr_node, false))
  487. {
  488. attr_node = new LLXMLNode(attr_name.c_str(), true);
  489. attr_node->setLineNumber(XML_GetCurrentLineNumber(*new_node_ptr->mParser));
  490. }
  491. attr_node->setValue(attr_value);
  492. new_node->addChild(attr_node);
  493. pos += 2;
  494. }
  495. if (parent)
  496. {
  497. parent->addChild(new_node);
  498. }
  499. }
  500. void XMLCALL EndXMLNode(void* user_data, const XML_Char* name)
  501. {
  502. if (!user_data)
  503. {
  504. llwarns << "Node (user_data) is NULL; aborting." << llendl;
  505. return;
  506. }
  507. // [FUGLY] Set the current active node to the current node's parent
  508. LLXMLNode* node = (LLXMLNode*)user_data;
  509. XML_Parser* parser = node->mParser;
  510. XML_SetUserData(*parser, (void*)node->mParent);
  511. // SJB: total hack:
  512. if (LLXMLNode::sStripWhitespaceValues)
  513. {
  514. std::string value = node->getValue();
  515. bool is_empty = true;
  516. for (size_t s = 0; s < value.length(); ++s)
  517. {
  518. char c = value[s];
  519. if (c != ' ' && c != '\t' && c != '\n')
  520. {
  521. is_empty = false;
  522. break;
  523. }
  524. }
  525. if (is_empty)
  526. {
  527. value.clear();
  528. node->setValue(value);
  529. }
  530. }
  531. }
  532. void XMLCALL XMLData(void* user_data, const XML_Char* s, int len)
  533. {
  534. if (!user_data)
  535. {
  536. llwarns << "Node (user_data) is NULL; aborting." << llendl;
  537. return;
  538. }
  539. LLXMLNode* current_node = (LLXMLNode*)user_data;
  540. std::string value = current_node->getValue();
  541. if (LLXMLNode::sStripEscapedStrings)
  542. {
  543. if (s[0] == '\"' && s[len - 1] == '\"')
  544. {
  545. // Special-case: Escaped string.
  546. std::string unescaped_string;
  547. for (S32 pos = 1; pos < len - 1; ++pos)
  548. {
  549. if (s[pos] == '\\' && s[pos + 1] == '\\')
  550. {
  551. unescaped_string.append("\\");
  552. ++pos;
  553. }
  554. else if (s[pos] == '\\' && s[pos + 1] == '\"')
  555. {
  556. unescaped_string.append("\"");
  557. ++pos;
  558. }
  559. else
  560. {
  561. unescaped_string.append(&s[pos], 1);
  562. }
  563. }
  564. value.append(unescaped_string);
  565. current_node->setValue(value);
  566. return;
  567. }
  568. }
  569. value.append(std::string(s, len));
  570. current_node->setValue(value);
  571. }
  572. //static
  573. bool LLXMLNode::updateNode(LLXMLNodePtr& node, LLXMLNodePtr& update_node)
  574. {
  575. if (node.isNull() || update_node.isNull())
  576. {
  577. llwarns << "Invalid node. Skipping." << llendl;
  578. return false;
  579. }
  580. // Update the node value
  581. node->mValue = update_node->mValue;
  582. // Update all attribute values
  583. for (LLXMLAttribList::const_iterator it = update_node->mAttributes.begin(),
  584. end = update_node->mAttributes.end();
  585. it != end; ++it)
  586. {
  587. const LLStringTableEntry* attrib_name_entry = it->first;
  588. LLXMLNodePtr update_attrib_node = it->second;
  589. LLXMLNodePtr attrib_node;
  590. node->getAttribute(attrib_name_entry, attrib_node, 0);
  591. if (attrib_node)
  592. {
  593. attrib_node->mValue = update_attrib_node->mValue;
  594. }
  595. }
  596. // Update all of node's children with updateNodes children that match name
  597. LLXMLNodePtr child = node->getFirstChild();
  598. LLXMLNodePtr last_child = child;
  599. std::string node_name, update_name;
  600. for (LLXMLNodePtr update_child = update_node->getFirstChild();
  601. update_child.notNull(); update_child = update_child->getNextSibling())
  602. {
  603. while (child.notNull())
  604. {
  605. update_child->getAttributeString("name", update_name);
  606. child->getAttributeString("name", node_name);
  607. // if it is a combobox there's no name, but there is a value
  608. if (update_name.empty())
  609. {
  610. update_child->getAttributeString("value", update_name);
  611. child->getAttributeString("value", node_name);
  612. }
  613. if (!node_name.empty() && update_name == node_name)
  614. {
  615. updateNode(child, update_child);
  616. last_child = child;
  617. child = child->getNextSibling();
  618. if (child.isNull())
  619. {
  620. child = node->getFirstChild();
  621. }
  622. break;
  623. }
  624. child = child->getNextSibling();
  625. if (child.isNull())
  626. {
  627. child = node->getFirstChild();
  628. }
  629. if (child == last_child)
  630. {
  631. break;
  632. }
  633. }
  634. }
  635. return true;
  636. }
  637. //static
  638. LLXMLNodePtr LLXMLNode::replaceNode(LLXMLNodePtr node,
  639. LLXMLNodePtr update_node)
  640. {
  641. if (node.isNull() || update_node.isNull())
  642. {
  643. llwarns << "Node invalid" << llendl;
  644. return node;
  645. }
  646. LLXMLNodePtr cloned_node = update_node->deepCopy();
  647. node->mParent->addChild(cloned_node, node); // add after node
  648. LLXMLNodePtr parent = node->mParent;
  649. parent->removeChild(node);
  650. parent->updateDefault();
  651. return cloned_node;
  652. }
  653. //static
  654. bool LLXMLNode::parseFile(const std::string& filename, LLXMLNodePtr& node,
  655. LLXMLNode* defaults_tree)
  656. {
  657. // Read file
  658. LL_DEBUGS("XMLNode") << "Parsing XML file: " << filename << LL_ENDL;
  659. std::string xml = LLFile::getContents(filename);
  660. bool read_success = !xml.empty();
  661. if (read_success &&
  662. parseBuffer(xml.data(), xml.size(), node, defaults_tree))
  663. {
  664. return true;
  665. }
  666. // It does happen we ask for an optional file that may not exist (e.g.
  667. // textures.xml for a custom skin): do not warn for those. HB
  668. if (read_success)
  669. {
  670. llwarns << "Failed to parse XML file: " << filename << llendl;
  671. }
  672. else // Still let it be known, as a debug message. HB
  673. {
  674. LL_DEBUGS("XMLNode") << "Could not read XML file: " << filename
  675. << LL_ENDL;
  676. }
  677. node = NULL;
  678. return false;
  679. }
  680. //static
  681. bool LLXMLNode::parseBuffer(const char* buffer, U64 length, LLXMLNodePtr& node,
  682. LLXMLNode* defaults)
  683. {
  684. // Init
  685. XML_Parser my_parser = XML_ParserCreate(NULL);
  686. XML_SetElementHandler(my_parser, StartXMLNode, EndXMLNode);
  687. XML_SetCharacterDataHandler(my_parser, XMLData);
  688. LL_DEBUGS("XMLNode") << "Parser created" << LL_ENDL;
  689. // Create a root node
  690. LLXMLNode* file_node_ptr = new LLXMLNode("XML", false);
  691. LLXMLNodePtr file_node = file_node_ptr;
  692. file_node->mParser = &my_parser;
  693. XML_SetUserData(my_parser, file_node_ptr);
  694. LL_DEBUGS("XMLNode") << "Root node created" << LL_ENDL;
  695. // Do the parsing
  696. bool success = XML_Parse(my_parser, buffer, length, true) == XML_STATUS_OK;
  697. if (!success)
  698. {
  699. llwarns << "Error parsing XML. Error code: "
  700. << XML_ErrorString(XML_GetErrorCode(my_parser))
  701. << " at line " << XML_GetCurrentLineNumber(my_parser)
  702. << ", column " << XML_GetCurrentColumnNumber(my_parser)
  703. << llendl;
  704. }
  705. // Deinit
  706. XML_ParserFree(my_parser);
  707. if (!success)
  708. {
  709. return false;
  710. }
  711. if (!file_node->mChildren || file_node->mChildren->map.size() != 1)
  712. {
  713. llwarns << "Parse failure - wrong number of top-level nodes."
  714. << llendl;
  715. node = NULL;
  716. return false;
  717. }
  718. LLXMLNode* return_node = file_node->mChildren->map.begin()->second;
  719. if (!return_node)
  720. {
  721. llwarns << "Parse failure - Could not allocate a new node !" << llendl;
  722. return false;
  723. }
  724. return_node->setDefault(defaults);
  725. return_node->updateDefault();
  726. node = return_node;
  727. LL_DEBUGS("XMLNode") << "Parse success" << LL_ENDL;
  728. return true;
  729. }
  730. //static
  731. bool LLXMLNode::parseStream(std::istream& str, LLXMLNodePtr& node,
  732. LLXMLNode* defaults)
  733. {
  734. // Init
  735. XML_Parser my_parser = XML_ParserCreate(NULL);
  736. XML_SetElementHandler(my_parser, StartXMLNode, EndXMLNode);
  737. XML_SetCharacterDataHandler(my_parser, XMLData);
  738. // Create a root node
  739. LLXMLNode* file_node_ptr = new LLXMLNode("XML", false);
  740. LLXMLNodePtr file_node = file_node_ptr;
  741. file_node->mParser = &my_parser;
  742. XML_SetUserData(my_parser, (void*)file_node_ptr);
  743. constexpr int BUFSIZE = 1024;
  744. U8* buffer = new U8[BUFSIZE];
  745. while (str.good())
  746. {
  747. str.read((char*)buffer, BUFSIZE);
  748. int count = (int)str.gcount();
  749. if (XML_Parse(my_parser, (const char*)buffer, count,
  750. !str.good()) != XML_STATUS_OK)
  751. {
  752. llwarns << "Error parsing XML. Error code: "
  753. << XML_ErrorString(XML_GetErrorCode(my_parser))
  754. << " at line " << XML_GetCurrentLineNumber(my_parser)
  755. << llendl;
  756. break;
  757. }
  758. }
  759. delete[] buffer;
  760. // Deinit
  761. XML_ParserFree(my_parser);
  762. if (!file_node->mChildren || file_node->mChildren->map.size() != 1)
  763. {
  764. llwarns << "Parse failure - wrong number of top-level nodes."
  765. << llendl;
  766. node = NULL;
  767. return false;
  768. }
  769. LLXMLNode* return_node = file_node->mChildren->map.begin()->second;
  770. if (!return_node)
  771. {
  772. llwarns << "Parse failure - Could not allocate a new node !" << llendl;
  773. node = NULL;
  774. return false;
  775. }
  776. return_node->setDefault(defaults);
  777. return_node->updateDefault();
  778. node = return_node;
  779. return true;
  780. }
  781. bool LLXMLNode::isFullyDefault()
  782. {
  783. if (mDefault.isNull())
  784. {
  785. return false;
  786. }
  787. bool has_default_value = mValue == mDefault->mValue;
  788. bool has_default_attribute = mIsAttribute == mDefault->mIsAttribute;
  789. bool has_default_type = mIsAttribute || mType == mDefault->mType;
  790. bool has_default_encoding = mIsAttribute || mEncoding == mDefault->mEncoding;
  791. bool has_default_precision = mIsAttribute || mPrecision == mDefault->mPrecision;
  792. bool has_default_length = mIsAttribute || mLength == mDefault->mLength;
  793. if (has_default_value && has_default_type && has_default_encoding &&
  794. has_default_precision && has_default_length && has_default_attribute)
  795. {
  796. if (mChildren.notNull())
  797. {
  798. for (LLXMLChildList::const_iterator it = mChildren->map.begin(),
  799. end = mChildren->map.end();
  800. it != end; ++it)
  801. {
  802. LLXMLNodePtr child = it->second;
  803. if (child.notNull() && !child->isFullyDefault())
  804. {
  805. return false;
  806. }
  807. }
  808. }
  809. return true;
  810. }
  811. return false;
  812. }
  813. //static
  814. bool LLXMLNode::getLayeredXMLNode(LLXMLNodePtr& root,
  815. const std::vector<std::string>& paths)
  816. {
  817. if (paths.empty()) return false;
  818. std::vector<std::string>::const_iterator it = paths.begin();
  819. const std::string& filename = *it;
  820. if (filename.empty())
  821. {
  822. return false;
  823. }
  824. if (!LLXMLNode::parseFile(filename, root, NULL))
  825. {
  826. llwarns << "Problem reading UI description file: " << filename
  827. << llendl;
  828. return false;
  829. }
  830. LLXMLNodePtr update_root;
  831. std::string node_name, update_name;
  832. std::vector<std::string>::const_iterator end = paths.end();
  833. while (++it != end)
  834. {
  835. const std::string& layer_filename = *it;
  836. if (layer_filename.empty() || layer_filename == filename)
  837. {
  838. // no localized version of this file, that's ok, keep looking
  839. continue;
  840. }
  841. if (!LLXMLNode::parseFile(layer_filename, update_root, NULL))
  842. {
  843. llwarns << "Problem reading localized UI description file: "
  844. << layer_filename << llendl;
  845. return false;
  846. }
  847. update_root->getAttributeString("name", update_name);
  848. root->getAttributeString("name", node_name);
  849. if (update_name == node_name)
  850. {
  851. LLXMLNode::updateNode(root, update_root);
  852. }
  853. }
  854. return true;
  855. }
  856. //static
  857. void LLXMLNode::writeHeaderToFile(LLFILE* out_file)
  858. {
  859. fprintf(out_file,
  860. "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n");
  861. }
  862. void LLXMLNode::writeToFile(LLFILE* out_file,
  863. const std::string& indent,
  864. bool use_type_decorations)
  865. {
  866. if (isFullyDefault())
  867. {
  868. // Don't write out nodes that are an exact match to defaults
  869. return;
  870. }
  871. std::ostringstream ostream;
  872. writeToOstream(ostream, indent, use_type_decorations);
  873. std::string outstring = ostream.str();
  874. size_t written = fwrite(outstring.c_str(), 1, outstring.length(),
  875. out_file);
  876. if (written != outstring.length())
  877. {
  878. llwarns << "Short write" << llendl;
  879. }
  880. }
  881. void LLXMLNode::writeToOstream(std::ostream& output_stream,
  882. const std::string& indent,
  883. bool use_type_decorations)
  884. {
  885. if (isFullyDefault())
  886. {
  887. // Don't write out nodes that are an exact match to defaults
  888. LL_DEBUGS("XMLNode") << "Node "
  889. << std::string(mName ? mName->mString : "no name")
  890. << " is full default, not writing." << LL_ENDL;
  891. return;
  892. }
  893. if (!mName)
  894. {
  895. llwarns << "No name for node. Skipping." << llendl;
  896. return;
  897. }
  898. bool has_default = mDefault.notNull();
  899. bool has_default_type = has_default && mType == mDefault->mType;
  900. bool has_default_encoding = has_default && mEncoding == mDefault->mEncoding;
  901. bool has_default_precision = has_default && mPrecision == mDefault->mPrecision;
  902. bool has_default_length = has_default && mLength == mDefault->mLength;
  903. // stream the name
  904. output_stream << indent << "<" << mName->mString << "\n";
  905. if (use_type_decorations)
  906. {
  907. LL_DEBUGS("XMLNode") << "Writing decorations for node: "
  908. << mName->mString << LL_ENDL;
  909. // ID
  910. if (!mID.empty())
  911. {
  912. LL_DEBUGS("XMLNode") << " - Id: " << mID << LL_ENDL;
  913. output_stream << indent << " id=\"" << mID << "\"\n";
  914. }
  915. // Type
  916. if (!has_default_type)
  917. {
  918. switch (mType)
  919. {
  920. case TYPE_BOOLEAN:
  921. LL_DEBUGS("XMLNode") << " - Type: boolean" << LL_ENDL;
  922. output_stream << indent << " type=\"boolean\"\n";
  923. break;
  924. case TYPE_INTEGER:
  925. LL_DEBUGS("XMLNode") << " - Type: integer" << LL_ENDL;
  926. output_stream << indent << " type=\"integer\"\n";
  927. break;
  928. case TYPE_FLOAT:
  929. LL_DEBUGS("XMLNode") << " - Type: float" << LL_ENDL;
  930. output_stream << indent << " type=\"float\"\n";
  931. break;
  932. case TYPE_STRING:
  933. LL_DEBUGS("XMLNode") << " - Type: string" << LL_ENDL;
  934. output_stream << indent << " type=\"string\"\n";
  935. break;
  936. case TYPE_UUID:
  937. LL_DEBUGS("XMLNode") << " - Type: UUID" << LL_ENDL;
  938. output_stream << indent << " type=\"uuid\"\n";
  939. break;
  940. case TYPE_NODEREF:
  941. LL_DEBUGS("XMLNode") << " - Type: node ref" << LL_ENDL;
  942. output_stream << indent << " type=\"noderef\"\n";
  943. break;
  944. default:
  945. break;
  946. }
  947. }
  948. // Encoding
  949. if (!has_default_encoding)
  950. {
  951. switch (mEncoding)
  952. {
  953. case ENCODING_DECIMAL:
  954. LL_DEBUGS("XMLNode") << " - Encoding: decimal" << LL_ENDL;
  955. output_stream << indent << " encoding=\"decimal\"\n";
  956. break;
  957. case ENCODING_HEX:
  958. LL_DEBUGS("XMLNode") << " - Encoding: hexadecimal"
  959. << LL_ENDL;
  960. output_stream << indent << " encoding=\"hex\"\n";
  961. break;
  962. #if 0
  963. case ENCODING_BASE32:
  964. output_stream << indent << " encoding=\"base32\"\n";
  965. break;
  966. #endif
  967. default:
  968. break;
  969. }
  970. }
  971. // Precision
  972. if (!has_default_precision &&
  973. (mType == TYPE_INTEGER || mType == TYPE_FLOAT))
  974. {
  975. LL_DEBUGS("XMLNode") << " - Precision: " << mPrecision << LL_ENDL;
  976. output_stream << indent << " precision=\"" << mPrecision << "\"\n";
  977. }
  978. // Version
  979. if (mVersionMajor > 0 || mVersionMinor > 0)
  980. {
  981. LL_DEBUGS("XMLNode") << " - Version: " << mVersionMajor << "."
  982. << mVersionMinor << LL_ENDL;
  983. output_stream << indent << " version=\"" << mVersionMajor << "."
  984. << mVersionMinor << "\"\n";
  985. }
  986. // Array length
  987. if (!has_default_length && mLength > 0)
  988. {
  989. LL_DEBUGS("XMLNode") << " - Length: " << mLength << LL_ENDL;
  990. output_stream << indent << " length=\"" << mLength << "\"\n";
  991. }
  992. }
  993. {
  994. // Write out attributes
  995. LL_DEBUGS("XMLNode") << "Writing attributes for node: "
  996. << mName->mString << LL_ENDL;
  997. std::string attr_str;
  998. for (LLXMLAttribList::const_iterator it = mAttributes.begin(),
  999. end = mAttributes.end();
  1000. it != end; ++it)
  1001. {
  1002. LLXMLNodePtr child = it->second;
  1003. if (child.isNull() || !child->mName) continue;
  1004. LL_DEBUGS("XMLNode") << "Child: " << child->mName->mString
  1005. << LL_ENDL;
  1006. if (child->mDefault.isNull() ||
  1007. child->mDefault->mValue != child->mValue)
  1008. {
  1009. std::string attr = child->mName->mString;
  1010. if (use_type_decorations &&
  1011. (attr == "id" || attr == "type" || attr == "encoding" ||
  1012. attr == "precision" || attr == "version" ||
  1013. attr == "length"))
  1014. {
  1015. continue; // skip built-in attributes
  1016. }
  1017. attr_str = llformat(" %s=\"%s\"", attr.c_str(),
  1018. escapeXML(child->mValue).c_str());
  1019. LL_DEBUGS("XMLNode") << " - attribute: " << attr_str
  1020. << LL_ENDL;
  1021. output_stream << indent << attr_str << "\n";
  1022. }
  1023. }
  1024. }
  1025. // Erase last \n before attaching final > or />
  1026. output_stream.seekp(-1, std::ios::cur);
  1027. if (mChildren.isNull() && mValue.empty())
  1028. {
  1029. output_stream << " />\n";
  1030. return;
  1031. }
  1032. else
  1033. {
  1034. output_stream << ">\n";
  1035. if (mChildren.notNull())
  1036. {
  1037. // stream non-attributes
  1038. std::string next_indent = indent + " ";
  1039. for (LLXMLNode* child = getFirstChild(); child;
  1040. child = child->getNextSibling())
  1041. {
  1042. child->writeToOstream(output_stream, next_indent,
  1043. use_type_decorations);
  1044. }
  1045. }
  1046. if (!mValue.empty())
  1047. {
  1048. std::string contents = getTextContents();
  1049. output_stream << indent << " " << escapeXML(contents) << "\n";
  1050. LL_DEBUGS("XMLNode") << "Value: " << escapeXML(contents)
  1051. << LL_ENDL;
  1052. }
  1053. output_stream << indent << "</" << mName->mString << ">\n";
  1054. }
  1055. LL_DEBUGS("XMLNode") << "Finished writing data for node: "
  1056. << mName->mString << LL_ENDL;
  1057. }
  1058. void LLXMLNode::findName(const std::string& name, LLXMLNodeList& results)
  1059. {
  1060. LLStringTableEntry* name_entry = gStringTable.checkStringEntry(name);
  1061. if (name_entry == mName)
  1062. {
  1063. results.emplace(mName->mString, this);
  1064. }
  1065. else if (mChildren.notNull())
  1066. {
  1067. for (LLXMLChildList::const_iterator it = mChildren->map.begin(),
  1068. end = mChildren->map.end();
  1069. it != end; ++it)
  1070. {
  1071. LLXMLNodePtr child = it->second;
  1072. if (child.notNull())
  1073. {
  1074. child->findName(name_entry, results);
  1075. }
  1076. }
  1077. }
  1078. }
  1079. void LLXMLNode::findName(LLStringTableEntry* name, LLXMLNodeList& results)
  1080. {
  1081. if (name == mName)
  1082. {
  1083. results.emplace(mName->mString, this);
  1084. }
  1085. else if (mChildren.notNull())
  1086. {
  1087. for (LLXMLChildList::const_iterator it = mChildren->map.begin(),
  1088. end = mChildren->map.end();
  1089. it != end; ++it)
  1090. {
  1091. LLXMLNodePtr child = it->second;
  1092. if (child.notNull())
  1093. {
  1094. child->findName(name, results);
  1095. }
  1096. }
  1097. }
  1098. }
  1099. void LLXMLNode::findID(const std::string& id, LLXMLNodeList& results)
  1100. {
  1101. if (id == mID)
  1102. {
  1103. results.emplace(mName->mString, this);
  1104. }
  1105. else if (mChildren.notNull())
  1106. {
  1107. for (LLXMLChildList::const_iterator it = mChildren->map.begin(),
  1108. end = mChildren->map.end();
  1109. it != end; ++it)
  1110. {
  1111. LLXMLNodePtr child = it->second;
  1112. if (child.notNull())
  1113. {
  1114. child->findID(id, results);
  1115. }
  1116. }
  1117. }
  1118. }
  1119. void LLXMLNode::scrubToTree(LLXMLNode* tree)
  1120. {
  1121. if (tree && tree->mChildren.notNull() && mChildren.notNull())
  1122. {
  1123. std::vector<LLXMLNodePtr> to_delete_list;
  1124. LLXMLChildList::iterator it = mChildren->map.begin();
  1125. while (it != mChildren->map.end())
  1126. {
  1127. LLXMLNodePtr child = it->second;
  1128. if (child.isNull()) continue; // Paranoia
  1129. LLXMLNodePtr child_tree = NULL;
  1130. // Look for this child in the default's children
  1131. bool found = false;
  1132. LLXMLChildList::iterator it2 = tree->mChildren->map.begin();
  1133. while (it2 != tree->mChildren->map.end())
  1134. {
  1135. if (child->mName == it2->second->mName)
  1136. {
  1137. child_tree = it2->second;
  1138. found = true;
  1139. }
  1140. ++it2;
  1141. }
  1142. if (!found)
  1143. {
  1144. to_delete_list.push_back(child);
  1145. }
  1146. else
  1147. {
  1148. child->scrubToTree(child_tree);
  1149. }
  1150. ++it;
  1151. }
  1152. for (std::vector<LLXMLNodePtr>::iterator it3 = to_delete_list.begin(),
  1153. end3 = to_delete_list.end();
  1154. it3 != end3; ++it3)
  1155. {
  1156. (*it3)->setParent(NULL);
  1157. }
  1158. }
  1159. }
  1160. bool LLXMLNode::getChild(const char* name,
  1161. LLXMLNodePtr& node,
  1162. bool use_default_if_missing)
  1163. {
  1164. return getChild(gStringTable.checkStringEntry(name), node,
  1165. use_default_if_missing);
  1166. }
  1167. bool LLXMLNode::getChild(const LLStringTableEntry* name,
  1168. LLXMLNodePtr& node,
  1169. bool use_default_if_missing)
  1170. {
  1171. if (mChildren.notNull())
  1172. {
  1173. LLXMLChildList::const_iterator it = mChildren->map.find(name);
  1174. if (it != mChildren->map.end())
  1175. {
  1176. node = it->second;
  1177. return true;
  1178. }
  1179. }
  1180. if (use_default_if_missing && !mDefault.isNull())
  1181. {
  1182. return mDefault->getChild(name, node, false);
  1183. }
  1184. node = NULL;
  1185. return false;
  1186. }
  1187. void LLXMLNode::getChildren(const char* name,
  1188. LLXMLNodeList& children,
  1189. bool use_default_if_missing) const
  1190. {
  1191. getChildren(gStringTable.checkStringEntry(name), children,
  1192. use_default_if_missing);
  1193. }
  1194. void LLXMLNode::getChildren(const LLStringTableEntry* name,
  1195. LLXMLNodeList& children,
  1196. bool use_default_if_missing) const
  1197. {
  1198. if (mChildren.notNull())
  1199. {
  1200. LLXMLChildList::const_iterator end = mChildren->map.end();
  1201. LLXMLChildList::const_iterator it = mChildren->map.find(name);
  1202. if (it != end)
  1203. {
  1204. while (it != end)
  1205. {
  1206. LLXMLNodePtr child = (it++)->second;
  1207. if (child.isNull() || !child->mName) continue;
  1208. if (name != child->mName)
  1209. {
  1210. break;
  1211. }
  1212. children.emplace(child->mName->mString, child);
  1213. }
  1214. }
  1215. }
  1216. if (children.empty() && use_default_if_missing && !mDefault.isNull())
  1217. {
  1218. mDefault->getChildren(name, children, false);
  1219. }
  1220. }
  1221. // Recursively walks the tree and returns all children at all nesting levels
  1222. // matching the name
  1223. void LLXMLNode::getDescendants(const LLStringTableEntry* name,
  1224. LLXMLNodeList& children) const
  1225. {
  1226. if (mChildren.notNull())
  1227. {
  1228. for (LLXMLChildList::const_iterator it = mChildren->map.begin(),
  1229. end = mChildren->map.end();
  1230. it != end; ++it)
  1231. {
  1232. LLXMLNodePtr child = it->second;
  1233. if (child.isNull() || !child->mName) continue;
  1234. if (name == child->mName)
  1235. {
  1236. children.emplace(child->mName->mString, child);
  1237. }
  1238. // and check each child as well
  1239. child->getDescendants(name, children);
  1240. }
  1241. }
  1242. }
  1243. bool LLXMLNode::getAttribute(const char* name,
  1244. LLXMLNodePtr& node,
  1245. bool use_default_if_missing)
  1246. {
  1247. return getAttribute(gStringTable.checkStringEntry(name), node,
  1248. use_default_if_missing);
  1249. }
  1250. bool LLXMLNode::getAttribute(const LLStringTableEntry* name,
  1251. LLXMLNodePtr& node,
  1252. bool use_default_if_missing)
  1253. {
  1254. LLXMLAttribList::const_iterator it = mAttributes.find(name);
  1255. if (it != mAttributes.end())
  1256. {
  1257. node = it->second;
  1258. return true;
  1259. }
  1260. if (use_default_if_missing && !mDefault.isNull())
  1261. {
  1262. return mDefault->getAttribute(name, node, false);
  1263. }
  1264. return false;
  1265. }
  1266. bool LLXMLNode::setAttributeString(const char* attr, const std::string& value)
  1267. {
  1268. LLStringTableEntry* name = gStringTable.checkStringEntry(attr);
  1269. LLXMLAttribList::const_iterator it = mAttributes.find(name);
  1270. if (it != mAttributes.end())
  1271. {
  1272. LLXMLNodePtr node = it->second;
  1273. if (node.notNull())
  1274. {
  1275. node->setValue(value);
  1276. return true;
  1277. }
  1278. }
  1279. return false;
  1280. }
  1281. bool LLXMLNode::hasAttribute(const char* name)
  1282. {
  1283. LLXMLNodePtr node;
  1284. return getAttribute(name, node);
  1285. }
  1286. bool LLXMLNode::getAttributeBool(const char* name, bool& value)
  1287. {
  1288. LLXMLNodePtr node;
  1289. return getAttribute(name, node) && node->getBoolValue(1, &value);
  1290. }
  1291. bool LLXMLNode::getAttributeU8(const char* name, U8& value)
  1292. {
  1293. LLXMLNodePtr node;
  1294. return (getAttribute(name, node) && node->getByteValue(1, &value));
  1295. }
  1296. bool LLXMLNode::getAttributeS8(const char* name, S8& value)
  1297. {
  1298. LLXMLNodePtr node;
  1299. S32 val;
  1300. if (!(getAttribute(name, node) && node->getIntValue(1, &val)))
  1301. {
  1302. return false;
  1303. }
  1304. value = val;
  1305. return true;
  1306. }
  1307. bool LLXMLNode::getAttributeU16(const char* name, U16& value)
  1308. {
  1309. LLXMLNodePtr node;
  1310. U32 val;
  1311. if (!(getAttribute(name, node) && node->getUnsignedValue(1, &val)))
  1312. {
  1313. return false;
  1314. }
  1315. value = val;
  1316. return true;
  1317. }
  1318. bool LLXMLNode::getAttributeS16(const char* name, S16& value)
  1319. {
  1320. LLXMLNodePtr node;
  1321. S32 val;
  1322. if (!(getAttribute(name, node) && node->getIntValue(1, &val)))
  1323. {
  1324. return false;
  1325. }
  1326. value = val;
  1327. return true;
  1328. }
  1329. bool LLXMLNode::getAttributeU32(const char* name, U32& value)
  1330. {
  1331. LLXMLNodePtr node;
  1332. return (getAttribute(name, node) && node->getUnsignedValue(1, &value));
  1333. }
  1334. bool LLXMLNode::getAttributeS32(const char* name, S32& value)
  1335. {
  1336. LLXMLNodePtr node;
  1337. return (getAttribute(name, node) && node->getIntValue(1, &value));
  1338. }
  1339. bool LLXMLNode::getAttributeF32(const char* name, F32& value)
  1340. {
  1341. LLXMLNodePtr node;
  1342. return (getAttribute(name, node) && node->getFloatValue(1, &value));
  1343. }
  1344. bool LLXMLNode::getAttributeF64(const char* name, F64& value)
  1345. {
  1346. LLXMLNodePtr node;
  1347. return (getAttribute(name, node) && node->getDoubleValue(1, &value));
  1348. }
  1349. bool LLXMLNode::getAttributeColor(const char* name, LLColor4& value)
  1350. {
  1351. LLXMLNodePtr node;
  1352. return (getAttribute(name, node) && node->getFloatValue(4, value.mV));
  1353. }
  1354. bool LLXMLNode::getAttributeColor4(const char* name, LLColor4& value)
  1355. {
  1356. LLXMLNodePtr node;
  1357. return (getAttribute(name, node) && node->getFloatValue(4, value.mV));
  1358. }
  1359. bool LLXMLNode::getAttributeColor4U(const char* name, LLColor4U& value)
  1360. {
  1361. LLXMLNodePtr node;
  1362. return (getAttribute(name, node) && node->getByteValue(4, value.mV));
  1363. }
  1364. bool LLXMLNode::getAttributeVector3(const char* name, LLVector3& value)
  1365. {
  1366. LLXMLNodePtr node;
  1367. return (getAttribute(name, node) && node->getFloatValue(3, value.mV));
  1368. }
  1369. bool LLXMLNode::getAttributeVector3d(const char* name, LLVector3d& value)
  1370. {
  1371. LLXMLNodePtr node;
  1372. return (getAttribute(name, node) && node->getDoubleValue(3, value.mdV));
  1373. }
  1374. bool LLXMLNode::getAttributeQuat(const char* name, LLQuaternion& value)
  1375. {
  1376. LLXMLNodePtr node;
  1377. return (getAttribute(name, node) && node->getFloatValue(4, value.mQ));
  1378. }
  1379. bool LLXMLNode::getAttributeUUID(const char* name, LLUUID& value)
  1380. {
  1381. LLXMLNodePtr node;
  1382. return (getAttribute(name, node) && node->getUUIDValue(1, &value));
  1383. }
  1384. bool LLXMLNode::getAttributeString(const char* name, std::string& value)
  1385. {
  1386. LLXMLNodePtr node;
  1387. if (!getAttribute(name, node))
  1388. {
  1389. return false;
  1390. }
  1391. value = node->getValue();
  1392. return true;
  1393. }
  1394. LLXMLNodePtr LLXMLNode::getRoot()
  1395. {
  1396. if (mParent)
  1397. {
  1398. return mParent->getRoot();
  1399. }
  1400. return this;
  1401. }
  1402. //static
  1403. const char* LLXMLNode::skipWhitespace(const char* str)
  1404. {
  1405. if (!str) return NULL;
  1406. // skip whitespace characters
  1407. while (str[0] == ' ' || str[0] == '\t' || str[0] == '\n')
  1408. {
  1409. ++str;
  1410. }
  1411. return str;
  1412. }
  1413. //static
  1414. const char* LLXMLNode::skipNonWhitespace(const char* str)
  1415. {
  1416. if (!str) return NULL;
  1417. // skip non-whitespace characters
  1418. while (str[0] != ' ' && str[0] != '\t' && str[0] != '\n' && str[0] != 0)
  1419. {
  1420. ++str;
  1421. }
  1422. return str;
  1423. }
  1424. //static
  1425. const char* LLXMLNode::parseInteger(const char* str, U64* dest,
  1426. bool* is_negative, U32 precision,
  1427. Encoding encoding)
  1428. {
  1429. *dest = 0;
  1430. *is_negative = false;
  1431. str = skipWhitespace(str);
  1432. if (str[0] == 0) return NULL;
  1433. if (encoding == ENCODING_DECIMAL || encoding == ENCODING_DEFAULT)
  1434. {
  1435. if (str[0] == '+')
  1436. {
  1437. ++str;
  1438. }
  1439. if (str[0] == '-')
  1440. {
  1441. *is_negative = true;
  1442. ++str;
  1443. }
  1444. str = skipWhitespace(str);
  1445. U64 ret = 0;
  1446. while (str[0] >= '0' && str[0] <= '9')
  1447. {
  1448. ret *= 10;
  1449. ret += str[0] - '0';
  1450. ++str;
  1451. }
  1452. if (str[0] == '.')
  1453. {
  1454. // If there is a fractional part, skip it
  1455. str = skipNonWhitespace(str);
  1456. }
  1457. *dest = ret;
  1458. return str;
  1459. }
  1460. if (encoding == ENCODING_HEX)
  1461. {
  1462. U64 ret = 0;
  1463. str = skipWhitespace(str);
  1464. for (U32 pos = 0; pos < precision / 4; ++pos)
  1465. {
  1466. ret <<= 4;
  1467. str = skipWhitespace(str);
  1468. if (str[0] >= '0' && str[0] <= '9')
  1469. {
  1470. ret += str[0] - '0';
  1471. }
  1472. else if (str[0] >= 'a' && str[0] <= 'f')
  1473. {
  1474. ret += str[0] - 'a' + 10;
  1475. }
  1476. else if (str[0] >= 'A' && str[0] <= 'F')
  1477. {
  1478. ret += str[0] - 'A' + 10;
  1479. }
  1480. else
  1481. {
  1482. return NULL;
  1483. }
  1484. ++str;
  1485. }
  1486. *dest = ret;
  1487. return str;
  1488. }
  1489. return NULL;
  1490. }
  1491. // 25 elements - decimal expansions of 1/(2^n), multiplied by 10 each iteration
  1492. static const U64 float_coeff_table[] =
  1493. { 5, 25, 125, 625, 3125,
  1494. 15625, 78125, 390625, 1953125, 9765625,
  1495. 48828125, 244140625, 1220703125, 6103515625LL, 30517578125LL,
  1496. 152587890625LL, 762939453125LL, 3814697265625LL, 19073486328125LL, 95367431640625LL,
  1497. 476837158203125LL, 2384185791015625LL, 11920928955078125LL, 59604644775390625LL, 298023223876953125LL
  1498. };
  1499. // 36 elements - decimal expansions of 1/(2^n) after the last 28, truncated, no
  1500. // multiply each iteration
  1501. static const U64 float_coeff_table_2[] =
  1502. { 149011611938476562LL, 74505805969238281LL,
  1503. 37252902984619140LL, 18626451492309570LL, 9313225746154785LL, 4656612873077392LL,
  1504. 2328306436538696LL, 1164153218269348LL, 582076609134674LL, 291038304567337LL,
  1505. 145519152283668LL, 72759576141834LL, 36379788070917LL, 18189894035458LL,
  1506. 9094947017729LL, 4547473508864LL, 2273736754432LL, 1136868377216LL,
  1507. 568434188608LL, 284217094304LL, 142108547152LL, 71054273576LL,
  1508. 35527136788LL, 17763568394LL, 8881784197LL, 4440892098LL,
  1509. 2220446049LL, 1110223024LL, 555111512LL, 277555756LL,
  1510. 138777878, 69388939, 34694469, 17347234,
  1511. 8673617, 4336808, 2168404, 1084202,
  1512. 542101, 271050, 135525, 67762,
  1513. };
  1514. //static
  1515. const char* LLXMLNode::parseFloat(const char* str, F64* dest, U32 precision,
  1516. Encoding encoding)
  1517. {
  1518. str = skipWhitespace(str);
  1519. if (!str[0]) return NULL;
  1520. if (encoding == ENCODING_DECIMAL || encoding == ENCODING_DEFAULT)
  1521. {
  1522. str = skipWhitespace(str);
  1523. if (strncmp(str, "inf", 3) == 0)
  1524. {
  1525. *(U64*)dest = 0x7FF0000000000000ll;
  1526. return str + 3;
  1527. }
  1528. if (strncmp(str, "-inf", 4) == 0)
  1529. {
  1530. *(U64*)dest = 0xFFF0000000000000ll;
  1531. return str + 4;
  1532. }
  1533. if (strncmp(str, "1.#INF", 6) == 0)
  1534. {
  1535. *(U64*)dest = 0x7FF0000000000000ll;
  1536. return str + 6;
  1537. }
  1538. if (strncmp(str, "-1.#INF", 7) == 0)
  1539. {
  1540. *(U64*)dest = 0xFFF0000000000000ll;
  1541. return str + 7;
  1542. }
  1543. F64 negative = 1.0;
  1544. if (str[0] == '+')
  1545. {
  1546. ++str;
  1547. }
  1548. if (str[0] == '-')
  1549. {
  1550. negative = -1.0;
  1551. ++str;
  1552. }
  1553. const char* base_str = str;
  1554. str = skipWhitespace(str);
  1555. // Parse the integer part of the expression
  1556. U64 int_part = 0;
  1557. while (str[0] >= '0' && str[0] <= '9')
  1558. {
  1559. int_part *= 10;
  1560. int_part += U64(str[0] - '0');
  1561. ++str;
  1562. }
  1563. U64 f_part = 0;
  1564. if (str[0] == '.')
  1565. {
  1566. ++str;
  1567. U64 remainder = 0;
  1568. U32 pos = 0;
  1569. // Parse the decimal part of the expression
  1570. while (str[0] >= '0' && str[0] <= '9' && pos < 25)
  1571. {
  1572. remainder = remainder * 10 + U64(str[0] - '0');
  1573. f_part <<= 1;
  1574. // Check the n'th bit
  1575. if (remainder >= float_coeff_table[pos])
  1576. {
  1577. remainder -= float_coeff_table[pos];
  1578. f_part |= 1;
  1579. }
  1580. ++pos;
  1581. ++str;
  1582. }
  1583. if (pos == 25)
  1584. {
  1585. // Drop any excessive digits
  1586. while (str[0] >= '0' && str[0] <= '9')
  1587. {
  1588. ++str;
  1589. }
  1590. }
  1591. else
  1592. {
  1593. while (pos < 25)
  1594. {
  1595. remainder *= 10;
  1596. f_part <<= 1;
  1597. // Check the n'th bit
  1598. if (remainder >= float_coeff_table[pos])
  1599. {
  1600. remainder -= float_coeff_table[pos];
  1601. f_part |= 1;
  1602. }
  1603. ++pos;
  1604. }
  1605. }
  1606. pos = 0;
  1607. while (pos < 36)
  1608. {
  1609. f_part <<= 1;
  1610. if (remainder >= float_coeff_table_2[pos])
  1611. {
  1612. remainder -= float_coeff_table_2[pos];
  1613. f_part |= 1;
  1614. }
  1615. ++pos;
  1616. }
  1617. }
  1618. F64 ret = F64(int_part) + (F64(f_part) / F64(1LL << 61));
  1619. F64 exponent = 1.0;
  1620. if (str[0] == 'e')
  1621. {
  1622. // Scientific notation
  1623. U64 exp;
  1624. bool is_negative;
  1625. str = parseInteger(++str, &exp, &is_negative, 64,
  1626. ENCODING_DECIMAL);
  1627. if (!str)
  1628. {
  1629. exp = 1;
  1630. }
  1631. F64 exp_d = is_negative ? -F64(exp) : F64(exp);
  1632. exponent = pow(10.0, exp_d);
  1633. }
  1634. if (str == base_str)
  1635. {
  1636. // no digits parsed
  1637. return NULL;
  1638. }
  1639. else
  1640. {
  1641. *dest = ret * negative * exponent;
  1642. return str;
  1643. }
  1644. }
  1645. if (encoding == ENCODING_HEX)
  1646. {
  1647. U64 bytes_dest;
  1648. bool is_negative;
  1649. str = parseInteger(str, (U64*)&bytes_dest, &is_negative, precision,
  1650. ENCODING_HEX);
  1651. // Upcast to F64
  1652. switch (precision)
  1653. {
  1654. case 32:
  1655. {
  1656. U32 short_dest = (U32)bytes_dest;
  1657. F32 ret_val = *(F32*)&short_dest;
  1658. *dest = ret_val;
  1659. break;
  1660. }
  1661. case 64:
  1662. *dest = *(F64*)&bytes_dest;
  1663. break;
  1664. default:
  1665. return NULL;
  1666. }
  1667. return str;
  1668. }
  1669. return NULL;
  1670. }
  1671. U32 LLXMLNode::getBoolValue(U32 expected_length, bool* array)
  1672. {
  1673. if (!array)
  1674. {
  1675. llwarns << "NULL array pointer passed !" << llendl;
  1676. llassert(false);
  1677. return 0;
  1678. }
  1679. // Check type - accept booleans or strings
  1680. if (mType != TYPE_BOOLEAN && mType != TYPE_STRING && mType != TYPE_UNKNOWN)
  1681. {
  1682. return 0;
  1683. }
  1684. std::string* str_array = new std::string[expected_length];
  1685. U32 length = getStringValue(expected_length, str_array);
  1686. U32 ret_length = 0;
  1687. for (U32 i = 0; i < length; ++i)
  1688. {
  1689. LLStringUtil::toLower(str_array[i]);
  1690. if (str_array[i] == "false")
  1691. {
  1692. array[ret_length++] = false;
  1693. }
  1694. else if (str_array[i] == "true")
  1695. {
  1696. array[ret_length++] = true;
  1697. }
  1698. }
  1699. delete[] str_array;
  1700. if (ret_length != expected_length)
  1701. {
  1702. LL_DEBUGS("XMLNode") << "Failure to get bool for node named '"
  1703. << mName->mString << "'. Expected "
  1704. << expected_length << " but only found "
  1705. << ret_length << LL_ENDL;
  1706. }
  1707. return ret_length;
  1708. }
  1709. U32 LLXMLNode::getByteValue(U32 expected_length, U8* array, Encoding encoding)
  1710. {
  1711. if (!array)
  1712. {
  1713. llwarns << "NULL array pointer passed !" << llendl;
  1714. llassert(false);
  1715. return 0;
  1716. }
  1717. // Check type - accept bytes or integers (below 256 only)
  1718. if (mType != TYPE_INTEGER
  1719. && mType != TYPE_UNKNOWN)
  1720. {
  1721. return 0;
  1722. }
  1723. if (mLength > 0 && mLength != expected_length)
  1724. {
  1725. llwarns << "asked for " << expected_length
  1726. << " elements, while node has " << mLength << llendl;
  1727. return 0;
  1728. }
  1729. if (encoding == ENCODING_DEFAULT)
  1730. {
  1731. encoding = mEncoding;
  1732. }
  1733. const char* value_string = mValue.c_str();
  1734. U32 i;
  1735. for (i = 0; i < expected_length; ++i)
  1736. {
  1737. U64 value;
  1738. bool is_negative;
  1739. value_string = parseInteger(value_string, &value, &is_negative, 8,
  1740. encoding);
  1741. if (value_string == NULL)
  1742. {
  1743. break;
  1744. }
  1745. if (value > 255 || is_negative)
  1746. {
  1747. llwarns << "value outside of valid range." << llendl;
  1748. break;
  1749. }
  1750. array[i] = U8(value);
  1751. }
  1752. if (i != expected_length)
  1753. {
  1754. LL_DEBUGS("XMLNode") << "failed for node named '" << mName->mString
  1755. << "'. Expected " << expected_length
  1756. << " but only found " << i << LL_ENDL;
  1757. }
  1758. return i;
  1759. }
  1760. U32 LLXMLNode::getIntValue(U32 expected_length, S32* array, Encoding encoding)
  1761. {
  1762. if (!array)
  1763. {
  1764. llwarns << "NULL array pointer passed !" << llendl;
  1765. llassert(false);
  1766. return 0;
  1767. }
  1768. // Check type - accept bytes or integers
  1769. if (mType != TYPE_INTEGER && mType != TYPE_UNKNOWN)
  1770. {
  1771. return 0;
  1772. }
  1773. if (mLength > 0 && mLength != expected_length)
  1774. {
  1775. llwarns << "asked for " << expected_length
  1776. << " elements, while node has " << mLength << llendl;
  1777. return 0;
  1778. }
  1779. if (encoding == ENCODING_DEFAULT)
  1780. {
  1781. encoding = mEncoding;
  1782. }
  1783. const char* value_string = mValue.c_str();
  1784. U32 i = 0;
  1785. for (i = 0; i < expected_length; ++i)
  1786. {
  1787. U64 value;
  1788. bool is_negative;
  1789. value_string = parseInteger(value_string, &value, &is_negative, 32,
  1790. encoding);
  1791. if (value_string == NULL)
  1792. {
  1793. break;
  1794. }
  1795. if (value > 0x7fffffff)
  1796. {
  1797. llwarns << "value outside of valid range." << llendl;
  1798. break;
  1799. }
  1800. array[i] = is_negative ? -S32(value) : S32(value);
  1801. }
  1802. if (i != expected_length)
  1803. {
  1804. LL_DEBUGS("XMLNode") << "failed for node named '" << mName->mString
  1805. << "'. Expected " << expected_length
  1806. << " but only found " << i << LL_ENDL;
  1807. }
  1808. return i;
  1809. }
  1810. U32 LLXMLNode::getUnsignedValue(U32 expected_length, U32* array,
  1811. Encoding encoding)
  1812. {
  1813. if (!array)
  1814. {
  1815. llwarns << "NULL array pointer passed !" << llendl;
  1816. llassert(false);
  1817. return 0;
  1818. }
  1819. // Check type - accept bytes or integers
  1820. if (mType != TYPE_INTEGER && mType != TYPE_UNKNOWN)
  1821. {
  1822. return 0;
  1823. }
  1824. if (mLength > 0 && mLength != expected_length)
  1825. {
  1826. llwarns << "asked for " << expected_length
  1827. << " elements, while node has " << mLength << llendl;
  1828. return 0;
  1829. }
  1830. if (encoding == ENCODING_DEFAULT)
  1831. {
  1832. encoding = mEncoding;
  1833. }
  1834. const char* value_string = mValue.c_str();
  1835. U32 i = 0;
  1836. // Int type
  1837. for (i = 0; i < expected_length; ++i)
  1838. {
  1839. U64 value;
  1840. bool is_negative;
  1841. value_string = parseInteger(value_string, &value, &is_negative, 32,
  1842. encoding);
  1843. if (value_string == NULL)
  1844. {
  1845. break;
  1846. }
  1847. if (is_negative || value > 0xffffffff)
  1848. {
  1849. llwarns << "value outside of valid range." << llendl;
  1850. break;
  1851. }
  1852. array[i] = U32(value);
  1853. }
  1854. if (i != expected_length)
  1855. {
  1856. LL_DEBUGS("XMLNode") << "failed for node named '" << mName->mString
  1857. << "'. Expected " << expected_length
  1858. << " but only found " << i << LL_ENDL;
  1859. }
  1860. return i;
  1861. }
  1862. U32 LLXMLNode::getLongValue(U32 expected_length, U64* array, Encoding encoding)
  1863. {
  1864. if (!array)
  1865. {
  1866. llwarns << "NULL array pointer passed !" << llendl;
  1867. llassert(false);
  1868. return 0;
  1869. }
  1870. // Check type - accept bytes or integers
  1871. if (mType != TYPE_INTEGER && mType != TYPE_UNKNOWN)
  1872. {
  1873. return 0;
  1874. }
  1875. if (mLength > 0 && mLength != expected_length)
  1876. {
  1877. llwarns << "asked for " << expected_length
  1878. << " elements, while node has " << mLength << llendl;
  1879. return 0;
  1880. }
  1881. if (encoding == ENCODING_DEFAULT)
  1882. {
  1883. encoding = mEncoding;
  1884. }
  1885. const char* value_string = mValue.c_str();
  1886. U32 i = 0;
  1887. // Int type
  1888. for (i = 0; i < expected_length; ++i)
  1889. {
  1890. U64 value;
  1891. bool is_negative;
  1892. value_string = parseInteger(value_string, &value, &is_negative, 64,
  1893. encoding);
  1894. if (value_string == NULL)
  1895. {
  1896. break;
  1897. }
  1898. if (is_negative)
  1899. {
  1900. llwarns << "value outside of valid range." << llendl;
  1901. break;
  1902. }
  1903. array[i] = value;
  1904. }
  1905. if (i != expected_length)
  1906. {
  1907. LL_DEBUGS("XMLNode") << "failed for node named '" << mName->mString
  1908. << "'. Expected " << expected_length
  1909. << " but only found " << i << LL_ENDL;
  1910. }
  1911. return i;
  1912. }
  1913. U32 LLXMLNode::getFloatValue(U32 expected_length, F32* array,
  1914. Encoding encoding)
  1915. {
  1916. if (!array)
  1917. {
  1918. llwarns << "NULL array pointer passed !" << llendl;
  1919. llassert(false);
  1920. return 0;
  1921. }
  1922. // Check type - accept only floats or doubles
  1923. if (mType != TYPE_FLOAT && mType != TYPE_UNKNOWN)
  1924. {
  1925. return 0;
  1926. }
  1927. if (mLength > 0 && mLength != expected_length)
  1928. {
  1929. llwarns << "asked for " << expected_length
  1930. << " elements, while node has " << mLength << llendl;
  1931. return 0;
  1932. }
  1933. if (encoding == ENCODING_DEFAULT)
  1934. {
  1935. encoding = mEncoding;
  1936. }
  1937. const char* value_string = mValue.c_str();
  1938. U32 i;
  1939. for (i = 0; i < expected_length; ++i)
  1940. {
  1941. F64 value;
  1942. value_string = parseFloat(value_string, &value, 32, encoding);
  1943. if (value_string == NULL)
  1944. {
  1945. break;
  1946. }
  1947. array[i] = F32(value);
  1948. }
  1949. if (i != expected_length)
  1950. {
  1951. LL_DEBUGS("XMLNode") << "failed for node named '" << mName->mString
  1952. << "'. Expected " << expected_length
  1953. << " but only found " << i << LL_ENDL;
  1954. }
  1955. return i;
  1956. }
  1957. U32 LLXMLNode::getDoubleValue(U32 expected_length, F64* array,
  1958. Encoding encoding)
  1959. {
  1960. if (!array)
  1961. {
  1962. llwarns << "NULL array pointer passed !" << llendl;
  1963. llassert(false);
  1964. return 0;
  1965. }
  1966. // Check type - accept only floats or doubles
  1967. if (mType != TYPE_FLOAT && mType != TYPE_UNKNOWN)
  1968. {
  1969. return 0;
  1970. }
  1971. if (mLength > 0 && mLength != expected_length)
  1972. {
  1973. llwarns << "asked for " << expected_length
  1974. << " elements, while node has " << mLength << llendl;
  1975. return 0;
  1976. }
  1977. if (encoding == ENCODING_DEFAULT)
  1978. {
  1979. encoding = mEncoding;
  1980. }
  1981. const char* value_string = mValue.c_str();
  1982. U32 i;
  1983. for (i = 0; i < expected_length; ++i)
  1984. {
  1985. F64 value;
  1986. value_string = parseFloat(value_string, &value, 64, encoding);
  1987. if (value_string == NULL)
  1988. {
  1989. break;
  1990. }
  1991. array[i] = value;
  1992. }
  1993. if (i != expected_length)
  1994. {
  1995. LL_DEBUGS("XMLNode") << "failed for node named '" << mName->mString
  1996. << "'. Expected " << expected_length
  1997. << " but only found " << i << LL_ENDL;
  1998. }
  1999. return i;
  2000. }
  2001. // Can always return any value as a string
  2002. U32 LLXMLNode::getStringValue(U32 expected_length, std::string* array)
  2003. {
  2004. if (!array)
  2005. {
  2006. llwarns << "NULL array pointer passed !" << llendl;
  2007. llassert(false);
  2008. return 0;
  2009. }
  2010. if (mLength > 0 && mLength != expected_length)
  2011. {
  2012. llwarns << "asked for " << expected_length
  2013. << " elements, while node has " << mLength << llendl;
  2014. return 0;
  2015. }
  2016. U32 num_returned_strings = 0;
  2017. // Array of strings is whitespace-separated
  2018. const std::string sep(" \n\t");
  2019. size_t n = 0;
  2020. size_t m = 0;
  2021. while (true)
  2022. {
  2023. if (num_returned_strings >= expected_length)
  2024. {
  2025. break;
  2026. }
  2027. n = mValue.find_first_not_of(sep, m);
  2028. m = mValue.find_first_of(sep, n);
  2029. if (m == std::string::npos)
  2030. {
  2031. break;
  2032. }
  2033. array[num_returned_strings++] = mValue.substr(n,m-n);
  2034. }
  2035. if (n != std::string::npos && num_returned_strings < expected_length)
  2036. {
  2037. array[num_returned_strings++] = mValue.substr(n);
  2038. }
  2039. if (num_returned_strings != expected_length)
  2040. {
  2041. LL_DEBUGS("XMLNode") << "failed for node named '" << mName->mString
  2042. << "'. Expected " << expected_length
  2043. << " but only found " << num_returned_strings
  2044. << LL_ENDL;
  2045. }
  2046. return num_returned_strings;
  2047. }
  2048. U32 LLXMLNode::getUUIDValue(U32 expected_length, LLUUID* array)
  2049. {
  2050. if (!array)
  2051. {
  2052. llwarns << "NULL array pointer passed !" << llendl;
  2053. llassert(false);
  2054. return 0;
  2055. }
  2056. // Check type
  2057. if (mType != TYPE_UUID && mType != TYPE_UNKNOWN)
  2058. {
  2059. return 0;
  2060. }
  2061. const char* value_string = mValue.c_str();
  2062. U32 i;
  2063. for (i = 0; i < expected_length; ++i)
  2064. {
  2065. LLUUID uuid_value;
  2066. value_string = skipWhitespace(value_string);
  2067. if (strlen(value_string) < UUID_STR_LENGTH - 1)
  2068. {
  2069. break;
  2070. }
  2071. char uuid_string[UUID_STR_LENGTH];
  2072. memcpy(uuid_string, value_string, UUID_STR_LENGTH - 1);
  2073. uuid_string[(UUID_STR_LENGTH - 1)] = 0;
  2074. if (!LLUUID::parseUUID(std::string(uuid_string), &uuid_value))
  2075. {
  2076. break;
  2077. }
  2078. value_string = &value_string[(UUID_STR_LENGTH - 1)];
  2079. array[i] = uuid_value;
  2080. }
  2081. if (i != expected_length)
  2082. {
  2083. LL_DEBUGS("XMLNode") << "failed for node named '" << mName->mString
  2084. << "'. Expected " << expected_length
  2085. << " but only found " << i << LL_ENDL;
  2086. }
  2087. return i;
  2088. }
  2089. U32 LLXMLNode::getNodeRefValue(U32 expected_length, LLXMLNode** array)
  2090. {
  2091. if (!array)
  2092. {
  2093. llwarns << "NULL array pointer passed !" << llendl;
  2094. llassert(false);
  2095. return 0;
  2096. }
  2097. // Check type
  2098. if (mType != TYPE_NODEREF && mType != TYPE_UNKNOWN)
  2099. {
  2100. return 0;
  2101. }
  2102. std::string *string_array = new std::string[expected_length];
  2103. U32 num_strings = getStringValue(expected_length, string_array);
  2104. U32 num_returned_refs = 0;
  2105. LLXMLNodePtr root = getRoot();
  2106. for (U32 strnum = 0; strnum < num_strings; ++strnum)
  2107. {
  2108. LLXMLNodeList node_list;
  2109. root->findID(string_array[strnum], node_list);
  2110. if (node_list.empty())
  2111. {
  2112. llwarns << "XML: Could not find node ID: " << string_array[strnum]
  2113. << llendl;
  2114. }
  2115. else if (node_list.size() > 1)
  2116. {
  2117. llwarns << "XML: Node ID not unique: " << string_array[strnum]
  2118. << llendl;
  2119. }
  2120. else
  2121. {
  2122. LLXMLNodeList::const_iterator list_itr = node_list.begin();
  2123. if (list_itr != node_list.end())
  2124. {
  2125. LLXMLNode* child = (*list_itr).second;
  2126. array[num_returned_refs++] = child;
  2127. }
  2128. }
  2129. }
  2130. delete[] string_array;
  2131. return num_returned_refs;
  2132. }
  2133. void LLXMLNode::setBoolValue(U32 length, const bool* array)
  2134. {
  2135. if (!length || !array) return;
  2136. std::string new_value;
  2137. for (U32 pos = 0; pos < length; ++pos)
  2138. {
  2139. if (pos > 0)
  2140. {
  2141. new_value = llformat("%s %s", new_value.c_str(),
  2142. array[pos] ? "true" : "false");
  2143. }
  2144. else
  2145. {
  2146. new_value = array[pos] ? "true" : "false";
  2147. }
  2148. }
  2149. mValue = new_value;
  2150. mEncoding = ENCODING_DEFAULT;
  2151. mLength = length;
  2152. mType = TYPE_BOOLEAN;
  2153. }
  2154. void LLXMLNode::setByteValue(U32 length, const U8* const array,
  2155. Encoding encoding)
  2156. {
  2157. if (!length || !array) return;
  2158. std::string new_value;
  2159. if (encoding == ENCODING_DEFAULT || encoding == ENCODING_DECIMAL)
  2160. {
  2161. for (U32 pos = 0; pos < length; ++pos)
  2162. {
  2163. if (pos > 0)
  2164. {
  2165. new_value.append(llformat(" %u", array[pos]));
  2166. }
  2167. else
  2168. {
  2169. new_value = llformat("%u", array[pos]);
  2170. }
  2171. }
  2172. }
  2173. if (encoding == ENCODING_HEX)
  2174. {
  2175. for (U32 pos = 0; pos < length; ++pos)
  2176. {
  2177. if (pos > 0 && pos % 16 == 0)
  2178. {
  2179. new_value.append(llformat(" %02X", array[pos]));
  2180. }
  2181. else
  2182. {
  2183. new_value.append(llformat("%02X", array[pos]));
  2184. }
  2185. }
  2186. }
  2187. // *TODO: handle Base32
  2188. mValue = new_value;
  2189. mEncoding = encoding;
  2190. mLength = length;
  2191. mType = TYPE_INTEGER;
  2192. mPrecision = 8;
  2193. }
  2194. void LLXMLNode::setIntValue(U32 length, const S32* array, Encoding encoding)
  2195. {
  2196. if (!length) return;
  2197. std::string new_value;
  2198. if (encoding == ENCODING_DEFAULT || encoding == ENCODING_DECIMAL)
  2199. {
  2200. for (U32 pos = 0; pos < length; ++pos)
  2201. {
  2202. if (pos > 0)
  2203. {
  2204. new_value.append(llformat(" %d", array[pos]));
  2205. }
  2206. else
  2207. {
  2208. new_value = llformat("%d", array[pos]);
  2209. }
  2210. }
  2211. mValue = new_value;
  2212. }
  2213. else if (encoding == ENCODING_HEX)
  2214. {
  2215. for (U32 pos = 0; pos < length; ++pos)
  2216. {
  2217. if (pos > 0 && pos % 16 == 0)
  2218. {
  2219. new_value.append(llformat(" %08X", ((U32*)array)[pos]));
  2220. }
  2221. else
  2222. {
  2223. new_value.append(llformat("%08X", ((U32*)array)[pos]));
  2224. }
  2225. }
  2226. mValue = new_value;
  2227. }
  2228. else
  2229. {
  2230. mValue = new_value;
  2231. }
  2232. // *TODO: handle Base32
  2233. mEncoding = encoding;
  2234. mLength = length;
  2235. mType = TYPE_INTEGER;
  2236. mPrecision = 32;
  2237. }
  2238. void LLXMLNode::setUnsignedValue(U32 length, const U32* array, Encoding encoding)
  2239. {
  2240. if (!length) return;
  2241. std::string new_value;
  2242. if (encoding == ENCODING_DEFAULT || encoding == ENCODING_DECIMAL)
  2243. {
  2244. for (U32 pos = 0; pos < length; ++pos)
  2245. {
  2246. if (pos > 0)
  2247. {
  2248. new_value.append(llformat(" %u", array[pos]));
  2249. }
  2250. else
  2251. {
  2252. new_value = llformat("%u", array[pos]);
  2253. }
  2254. }
  2255. }
  2256. if (encoding == ENCODING_HEX)
  2257. {
  2258. for (U32 pos = 0; pos < length; ++pos)
  2259. {
  2260. if (pos > 0 && pos % 16 == 0)
  2261. {
  2262. new_value.append(llformat(" %08X", array[pos]));
  2263. }
  2264. else
  2265. {
  2266. new_value.append(llformat("%08X", array[pos]));
  2267. }
  2268. }
  2269. mValue = new_value;
  2270. }
  2271. // *TODO: handle Base32
  2272. mValue = new_value;
  2273. mEncoding = encoding;
  2274. mLength = length;
  2275. mType = TYPE_INTEGER;
  2276. mPrecision = 32;
  2277. }
  2278. #if LL_WINDOWS
  2279. #define PU64 "I64u"
  2280. #else
  2281. #define PU64 "llu"
  2282. #endif
  2283. void LLXMLNode::setLongValue(U32 length, const U64* array, Encoding encoding)
  2284. {
  2285. if (!length) return;
  2286. std::string new_value;
  2287. if (encoding == ENCODING_DEFAULT || encoding == ENCODING_DECIMAL)
  2288. {
  2289. for (U32 pos = 0; pos < length; ++pos)
  2290. {
  2291. if (pos > 0)
  2292. {
  2293. new_value.append(llformat(" %" PU64, array[pos]));
  2294. }
  2295. else
  2296. {
  2297. new_value = llformat("%" PU64, array[pos]);
  2298. }
  2299. }
  2300. mValue = new_value;
  2301. }
  2302. if (encoding == ENCODING_HEX)
  2303. {
  2304. for (U32 pos = 0; pos < length; ++pos)
  2305. {
  2306. U32 upper_32 = U32(array[pos]>>32);
  2307. U32 lower_32 = U32(array[pos]&0xffffffff);
  2308. if (pos > 0 && pos % 8 == 0)
  2309. {
  2310. new_value.append(llformat(" %08X%08X", upper_32, lower_32));
  2311. }
  2312. else
  2313. {
  2314. new_value.append(llformat("%08X%08X", upper_32, lower_32));
  2315. }
  2316. }
  2317. mValue = new_value;
  2318. }
  2319. else
  2320. {
  2321. mValue = new_value;
  2322. }
  2323. // *TODO: handle Base32
  2324. mEncoding = encoding;
  2325. mLength = length;
  2326. mType = TYPE_INTEGER;
  2327. mPrecision = 64;
  2328. }
  2329. void LLXMLNode::setFloatValue(U32 length, const F32* array, Encoding encoding,
  2330. U32 precision)
  2331. {
  2332. if (!length) return;
  2333. std::string new_value;
  2334. if (encoding == ENCODING_DEFAULT || encoding == ENCODING_DECIMAL)
  2335. {
  2336. std::string format_string;
  2337. if (precision > 0)
  2338. {
  2339. if (precision > 25)
  2340. {
  2341. precision = 25;
  2342. }
  2343. format_string = llformat("%%.%dg", precision);
  2344. }
  2345. else
  2346. {
  2347. format_string = llformat("%%g");
  2348. }
  2349. for (U32 pos = 0; pos < length; ++pos)
  2350. {
  2351. if (pos > 0)
  2352. {
  2353. new_value.append(" ");
  2354. new_value.append(llformat(format_string.c_str(), array[pos]));
  2355. }
  2356. else
  2357. {
  2358. new_value.assign(llformat(format_string.c_str(), array[pos]));
  2359. }
  2360. }
  2361. mValue = new_value;
  2362. }
  2363. else if (encoding == ENCODING_HEX)
  2364. {
  2365. U32* byte_array = (U32*)array;
  2366. setUnsignedValue(length, byte_array, ENCODING_HEX);
  2367. }
  2368. else
  2369. {
  2370. mValue = new_value;
  2371. }
  2372. mEncoding = encoding;
  2373. mLength = length;
  2374. mType = TYPE_FLOAT;
  2375. mPrecision = 32;
  2376. }
  2377. void LLXMLNode::setDoubleValue(U32 length, const F64* array, Encoding encoding,
  2378. U32 precision)
  2379. {
  2380. if (!length) return;
  2381. std::string new_value;
  2382. if (encoding == ENCODING_DEFAULT || encoding == ENCODING_DECIMAL)
  2383. {
  2384. std::string format_string;
  2385. if (precision > 0)
  2386. {
  2387. if (precision > 25)
  2388. {
  2389. precision = 25;
  2390. }
  2391. format_string = llformat("%%.%dg", precision);
  2392. }
  2393. else
  2394. {
  2395. format_string = llformat("%%g");
  2396. }
  2397. for (U32 pos = 0; pos < length; ++pos)
  2398. {
  2399. if (pos > 0)
  2400. {
  2401. new_value.append(" ");
  2402. new_value.append(llformat(format_string.c_str(), array[pos]));
  2403. }
  2404. else
  2405. {
  2406. new_value.assign(llformat(format_string.c_str(), array[pos]));
  2407. }
  2408. }
  2409. mValue = new_value;
  2410. }
  2411. if (encoding == ENCODING_HEX)
  2412. {
  2413. U64* byte_array = (U64*)array;
  2414. setLongValue(length, byte_array, ENCODING_HEX);
  2415. }
  2416. else
  2417. {
  2418. mValue = new_value;
  2419. }
  2420. // *TODO: handle Base32
  2421. mEncoding = encoding;
  2422. mLength = length;
  2423. mType = TYPE_FLOAT;
  2424. mPrecision = 64;
  2425. }
  2426. //static
  2427. std::string LLXMLNode::escapeXML(const std::string& xml)
  2428. {
  2429. std::string out;
  2430. for (size_t i = 0; i < xml.size(); ++i)
  2431. {
  2432. char c = xml[i];
  2433. switch (c)
  2434. {
  2435. case '"': out.append("&quot;"); break;
  2436. case '\'': out.append("&apos;"); break;
  2437. case '&': out.append("&amp;"); break;
  2438. case '<': out.append("&lt;"); break;
  2439. case '>': out.append("&gt;"); break;
  2440. default: out.push_back(c); break;
  2441. }
  2442. }
  2443. return out;
  2444. }
  2445. void LLXMLNode::setStringValue(U32 length, const std::string* strings)
  2446. {
  2447. if (!length) return;
  2448. std::string new_value;
  2449. for (U32 pos = 0; pos < length; ++pos)
  2450. {
  2451. // *NOTE: Do not escape strings here: do it on output
  2452. new_value.append(strings[pos]);
  2453. if (pos < length - 1)
  2454. {
  2455. new_value.append(" ");
  2456. }
  2457. }
  2458. mValue = new_value;
  2459. mEncoding = ENCODING_DEFAULT;
  2460. mLength = length;
  2461. mType = TYPE_STRING;
  2462. }
  2463. void LLXMLNode::setUUIDValue(U32 length, const LLUUID* array)
  2464. {
  2465. if (!length) return;
  2466. std::string new_value;
  2467. for (U32 pos = 0; pos < length; ++pos)
  2468. {
  2469. new_value.append(array[pos].asString());
  2470. if (pos < length - 1)
  2471. {
  2472. new_value.append(" ");
  2473. }
  2474. }
  2475. mValue = new_value;
  2476. mEncoding = ENCODING_DEFAULT;
  2477. mLength = length;
  2478. mType = TYPE_UUID;
  2479. }
  2480. void LLXMLNode::setNodeRefValue(U32 length, const LLXMLNode** array)
  2481. {
  2482. if (!length) return;
  2483. std::string new_value;
  2484. for (U32 pos = 0; pos < length; ++pos)
  2485. {
  2486. if (array[pos]->mID != "")
  2487. {
  2488. new_value.append(array[pos]->mID);
  2489. }
  2490. else
  2491. {
  2492. new_value.append("(null)");
  2493. }
  2494. if (pos < length - 1)
  2495. {
  2496. new_value.append(" ");
  2497. }
  2498. }
  2499. mValue = new_value;
  2500. mEncoding = ENCODING_DEFAULT;
  2501. mLength = length;
  2502. mType = TYPE_NODEREF;
  2503. }
  2504. void LLXMLNode::setValue(const std::string& value)
  2505. {
  2506. if (mType == TYPE_CONTAINER)
  2507. {
  2508. mType = TYPE_UNKNOWN;
  2509. }
  2510. mValue = value;
  2511. }
  2512. void LLXMLNode::findDefault(LLXMLNode* defaults_list)
  2513. {
  2514. if (defaults_list)
  2515. {
  2516. LLXMLNodeList children;
  2517. defaults_list->getChildren(mName->mString, children);
  2518. for (LLXMLNodeList::const_iterator it = children.begin(),
  2519. end = children.end();
  2520. it != end; ++it)
  2521. {
  2522. LLXMLNode* child = it->second;
  2523. if (child && child->mVersionMajor == mVersionMajor &&
  2524. child->mVersionMinor == mVersionMinor)
  2525. {
  2526. mDefault = child;
  2527. return;
  2528. }
  2529. }
  2530. }
  2531. mDefault = NULL;
  2532. }
  2533. bool LLXMLNode::deleteChildren(const std::string& name)
  2534. {
  2535. U32 removed_count = 0;
  2536. LLXMLNodeList node_list;
  2537. findName(name, node_list);
  2538. if (!node_list.empty())
  2539. {
  2540. // *TODO: use multimap::find()
  2541. // *TODO: need to watch out for invalid iterators
  2542. for (LLXMLNodeList::const_iterator it = node_list.begin(),
  2543. end = node_list.end();
  2544. it != end; ++it)
  2545. {
  2546. LLXMLNode* child = it->second;
  2547. if (deleteChild(child))
  2548. {
  2549. ++removed_count;
  2550. }
  2551. }
  2552. }
  2553. return removed_count > 0;
  2554. }
  2555. bool LLXMLNode::deleteChildren(LLStringTableEntry* name)
  2556. {
  2557. U32 removed_count = 0;
  2558. LLXMLNodeList node_list;
  2559. findName(name, node_list);
  2560. if (!node_list.empty())
  2561. {
  2562. // *TODO: use multimap::find()
  2563. // *TODO: need to watch out for invalid iterators
  2564. LLXMLNodeList::iterator it;
  2565. for (LLXMLNodeList::iterator it = node_list.begin(),
  2566. end = node_list.end();
  2567. it != end; ++it)
  2568. {
  2569. LLXMLNode* child = it->second;
  2570. if (deleteChild(child))
  2571. {
  2572. ++removed_count;
  2573. }
  2574. }
  2575. }
  2576. return removed_count > 0;
  2577. }
  2578. void LLXMLNode::setAttributes(LLXMLNode::ValueType type, U32 precision,
  2579. LLXMLNode::Encoding encoding, U32 length)
  2580. {
  2581. mType = type;
  2582. mEncoding = encoding;
  2583. mPrecision = precision;
  2584. mLength = length;
  2585. }
  2586. void LLXMLNode::setName(const std::string& name)
  2587. {
  2588. setName(gStringTable.addStringEntry(name));
  2589. }
  2590. void LLXMLNode::setName(LLStringTableEntry* name)
  2591. {
  2592. LLXMLNode* old_parent = mParent;
  2593. if (mParent)
  2594. {
  2595. // We need to remove and re-add to the parent so that the multimap key
  2596. // agrees with this node's name
  2597. mParent->removeChild(this);
  2598. }
  2599. mName = name;
  2600. if (old_parent)
  2601. {
  2602. old_parent->addChild(this);
  2603. }
  2604. }
  2605. LLXMLNodePtr LLXMLNode::getFirstChild() const
  2606. {
  2607. if (mChildren.isNull()) return NULL;
  2608. LLXMLNodePtr ret = mChildren->head;
  2609. return ret;
  2610. }
  2611. LLXMLNodePtr LLXMLNode::getNextSibling() const
  2612. {
  2613. LLXMLNodePtr ret = mNext;
  2614. return ret;
  2615. }
  2616. std::string LLXMLNode::getSanitizedValue() const
  2617. {
  2618. if (mIsAttribute)
  2619. {
  2620. return getValue();
  2621. }
  2622. return getTextContents();
  2623. }
  2624. std::string LLXMLNode::getTextContents() const
  2625. {
  2626. std::string msg;
  2627. std::string contents = mValue;
  2628. size_t n = contents.find_first_not_of(" \t\n");
  2629. if (n != std::string::npos && contents[n] == '\"')
  2630. {
  2631. // Case 1: node has quoted text
  2632. S32 num_lines = 0;
  2633. while (true)
  2634. {
  2635. // mContents[n] == '"'
  2636. size_t t = ++n;
  2637. size_t m = 0;
  2638. // fix-up escaped characters
  2639. while (true)
  2640. {
  2641. m = contents.find_first_of("\\\"", t); // find first \ or "
  2642. if (m == std::string::npos || contents[m] == '\"')
  2643. {
  2644. break;
  2645. }
  2646. contents.erase(m, 1);
  2647. t = m + 1;
  2648. }
  2649. if (m == std::string::npos)
  2650. {
  2651. break;
  2652. }
  2653. // mContents[m] == '"'
  2654. ++num_lines;
  2655. msg += contents.substr(n, m - n) + "\n";
  2656. n = contents.find_first_of("\"", m + 1);
  2657. if (n == std::string::npos)
  2658. {
  2659. if (num_lines == 1)
  2660. {
  2661. msg.erase(msg.size() - 1); // remove "\n" if only one line
  2662. }
  2663. break;
  2664. }
  2665. }
  2666. }
  2667. else
  2668. {
  2669. // Case 2: node has embedded text (beginning and trailing white space
  2670. // trimmed)
  2671. size_t start = mValue.find_first_not_of(" \t\n");
  2672. if (start != mValue.npos)
  2673. {
  2674. size_t end = mValue.find_last_not_of(" \t\n");
  2675. if (end != mValue.npos)
  2676. {
  2677. msg = mValue.substr(start, end + 1 - start);
  2678. }
  2679. else
  2680. {
  2681. msg = mValue.substr(start);
  2682. }
  2683. }
  2684. // Convert any internal CR to LF
  2685. msg = utf8str_removeCRLF(msg);
  2686. }
  2687. return msg;
  2688. }
  2689. bool LLXMLNode::parseXmlRpcArrayValue(LLSD& target)
  2690. {
  2691. LLXMLNode* datap = getFirstChild().get();
  2692. if (!datap)
  2693. {
  2694. llwarns << "No inner XML element" << llendl;
  2695. return false;
  2696. }
  2697. if (!datap->hasName("data"))
  2698. {
  2699. llwarns << "No inner XML element (<data> expected, got: "
  2700. << datap->mName->mString << ")" << llendl;
  2701. return false;
  2702. }
  2703. if (datap->getNextSibling().get())
  2704. {
  2705. llwarns << "Multiple inner XML elements (single <data> expected)"
  2706. << llendl;
  2707. return false;
  2708. }
  2709. for (LLXMLNode* itemp = datap->getFirstChild().get(); itemp;
  2710. itemp = itemp->getNextSibling().get())
  2711. {
  2712. LLSD value;
  2713. if (!itemp->fromXMLRPCValue(value))
  2714. {
  2715. return false;
  2716. }
  2717. target.append(value);
  2718. }
  2719. return true;
  2720. }
  2721. bool LLXMLNode::parseXmlRpcStructValue(LLSD& target)
  2722. {
  2723. std::string name;
  2724. LLSD value;
  2725. for (LLXMLNode* itemp = getFirstChild().get(); itemp;
  2726. itemp = itemp->getNextSibling().get())
  2727. {
  2728. if (!itemp->hasName("member"))
  2729. {
  2730. llwarns << "Invalid inner XML element (<member> expected, got: <"
  2731. << itemp->mName->mString << ">" << llendl;
  2732. return false;
  2733. }
  2734. name.clear();
  2735. value.clear();
  2736. for (LLXMLNode* chilp = itemp->getFirstChild().get(); chilp;
  2737. chilp = chilp->getNextSibling().get())
  2738. {
  2739. if (chilp->hasName("name"))
  2740. {
  2741. name = LLStringFn::xml_decode(chilp->getTextContents());
  2742. }
  2743. else if (!chilp->fromXMLRPCValue(value))
  2744. {
  2745. return false;
  2746. }
  2747. }
  2748. if (name.empty())
  2749. {
  2750. llwarns << "Empty struct member name" << llendl;
  2751. return false;
  2752. }
  2753. target.insert(name, value);
  2754. }
  2755. return true;
  2756. }
  2757. bool LLXMLNode::fromXMLRPCValue(LLSD& target)
  2758. {
  2759. target.clear();
  2760. if (!hasName("value"))
  2761. {
  2762. llwarns << "Invalid XML element (<value> expected), got: <"
  2763. << mName->mString << ">" << llendl;
  2764. return false;
  2765. }
  2766. LLXMLNode* childp = getFirstChild().get();
  2767. if (!childp)
  2768. {
  2769. llwarns << "No inner XML element (value type expected)" << llendl;
  2770. // Value with no type qualifier is treated as string
  2771. target.assign(LLStringFn::xml_decode(getTextContents()));
  2772. return true;
  2773. }
  2774. if (childp->getNextSibling())
  2775. {
  2776. llwarns << "Multiple inner XML elements (single expected)" << llendl;
  2777. return false;
  2778. }
  2779. if (childp->hasName("string"))
  2780. {
  2781. target.assign(LLStringFn::xml_decode(childp->getTextContents()));
  2782. return true;
  2783. }
  2784. if (childp->hasName("int") || childp->hasName("i4"))
  2785. {
  2786. target.assign(std::stoi(childp->getTextContents()));
  2787. return true;
  2788. }
  2789. if (childp->hasName("double"))
  2790. {
  2791. target.assign(std::stod(childp->getTextContents()));
  2792. return true;
  2793. }
  2794. if (childp->hasName("boolean"))
  2795. {
  2796. target.assign(std::stoi(childp->getTextContents()) != 0);
  2797. return true;
  2798. }
  2799. if (childp->hasName("dateTime.iso8601"))
  2800. {
  2801. target.assign(LLSD::Date(childp->getTextContents()));
  2802. return true;
  2803. }
  2804. if (childp->hasName("base64"))
  2805. {
  2806. std::string decoded =
  2807. LLBase64::decode(childp->getTextContents().c_str());
  2808. size_t size = decoded.size();
  2809. LLSD::Binary binary(size);
  2810. if (size)
  2811. {
  2812. memcpy((void*)binary.data(), (void*)decoded.data(), size);
  2813. }
  2814. target.assign(binary);
  2815. return true;
  2816. }
  2817. if (childp->hasName("array"))
  2818. {
  2819. if (!childp->parseXmlRpcArrayValue(target))
  2820. {
  2821. target.clear();
  2822. return false;
  2823. }
  2824. return true;
  2825. }
  2826. if (childp->hasName("struct"))
  2827. {
  2828. if (!childp->parseXmlRpcStructValue(target))
  2829. {
  2830. target.clear();
  2831. return false;
  2832. }
  2833. return true;
  2834. }
  2835. llwarns << "Unknown inner XML element (known value type expected)"
  2836. << llendl;
  2837. // Value with unknown type qualifier is treated as string
  2838. target.assign(LLStringFn::xml_decode(childp->getTextContents()));
  2839. return true;
  2840. }