jptree.cpp 279 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include <unordered_map>
  14. #include <unordered_set>
  15. #include <string>
  16. #include "platform.h"
  17. #include "jarray.hpp"
  18. #include "jdebug.hpp"
  19. #include "jhash.hpp"
  20. #include "jmutex.hpp"
  21. #include "jexcept.hpp"
  22. #include "jlzw.hpp"
  23. #include "jregexp.hpp"
  24. #include "jstring.hpp"
  25. #include "jutil.hpp"
  26. #include "jmisc.hpp"
  27. #include "yaml.h"
  28. #include <initializer_list>
  29. #define MAKE_LSTRING(name,src,length) \
  30. const char *name = (const char *) alloca((length)+1); \
  31. memcpy((char *) name, (src), (length)); \
  32. *(char *) (name+(length)) = '\0';
  33. #include "jfile.hpp"
  34. #include "jlog.hpp"
  35. #include "jptree.ipp"
  36. #define WARNLEGACYCOMPARE
  37. #define XMLTAG_CONTENT "<>"
  38. #undef UNIMPLEMENTED
  39. #define UNIMPLEMENTED throw MakeIPTException(-1, "UNIMPLEMENTED")
  40. #define CHECK_ATTRIBUTE(X) if (X && isAttribute(X)) throw MakeIPTException(PTreeExcpt_XPath_Unsupported, "Attribute usage invalid here");
  41. #define AMBIGUOUS_PATH(X,P) { StringBuffer buf; buf.append(X": ambiguous xpath \"").append(P).append("\""); throw MakeIPTException(PTreeExcpt_XPath_Ambiguity,"%s",buf.str()); }
  42. #define PTREE_COMPRESS_THRESHOLD (4*1024) // i.e. only use compress if > threshold
  43. #define PTREE_COMPRESS_BOTHER_PECENTAGE (80) // i.e. if it doesn't compress to <80 % of original size don't bother
  44. class NullPTreeIterator final : implements IPropertyTreeIterator
  45. {
  46. public:
  47. virtual ~NullPTreeIterator() {}
  48. virtual void Link() const override {}
  49. virtual bool Release() const override { return true; }
  50. // IPropertyTreeIterator
  51. virtual bool first() override { return false; }
  52. virtual bool next() override { return false; }
  53. virtual bool isValid() override { return false; }
  54. virtual IPropertyTree & query() override { throwUnexpected(); }
  55. } *nullPTreeIterator;
  56. IPropertyTreeIterator *createNullPTreeIterator() { return LINK(nullPTreeIterator); } // initialized in init mod below.
  57. //===================================================================
  58. #ifdef USE_READONLY_ATOMTABLE
  59. RONameTable *AttrStrUnionWithTable::roNameTable = nullptr;
  60. RONameTable *AttrStrUnionWithValueTable::roValueTable = nullptr;
  61. #endif
  62. static AtomRefTable *keyTable = nullptr;
  63. static AtomRefTable *keyTableNC = nullptr;
  64. static CriticalSection hashcrit;
  65. static CAttrValHashTable *attrHT = nullptr;
  66. static AttrValue **freelist = nullptr;
  67. static unsigned freelistmax = 0;
  68. static CLargeMemoryAllocator freeallocator((memsize_t)-1, 0x1000*sizeof(AttrValue), true);
  69. #ifdef USE_READONLY_ATOMTABLE
  70. static const char * roAttributes[] =
  71. {
  72. #include "jptree-attrs.hpp" // potentially auto-generated
  73. nullptr
  74. };
  75. static const char * roAttributeValues[] =
  76. {
  77. #include "jptree-attrvalues.hpp" // potentially auto-generated
  78. nullptr
  79. };
  80. void initializeRoTable()
  81. {
  82. for (const char **attr = roAttributes; *attr; attr++)
  83. {
  84. AttrStrUnionWithTable::roNameTable->find(*attr, true);
  85. }
  86. for (const char **value = roAttributeValues; *value; value++)
  87. {
  88. AttrStrUnionWithValueTable::roValueTable->find(*value, true);
  89. }
  90. // also populate read-only value table by generating some common constants
  91. StringBuffer constStr;
  92. for (unsigned c=0; c<1000; c++) // common unsigned values in attributes
  93. {
  94. constStr.clear().append(c);
  95. AttrStrUnionWithValueTable::roValueTable->find(constStr.str(), true);
  96. }
  97. for (unsigned c=1; c<=400; c++) // outer graphs
  98. {
  99. constStr.clear().append("graph").append(c);
  100. AttrStrUnionWithValueTable::roValueTable->find(constStr.str(), true);
  101. constStr.clear().append("Graph graph ").append(c);
  102. AttrStrUnionWithValueTable::roValueTable->find(constStr.str(), true);
  103. }
  104. for (unsigned c=1; c<=200; c++) // subgraphs
  105. {
  106. constStr.clear().append("sg").append(c);
  107. AttrStrUnionWithValueTable::roValueTable->find(constStr.str(), true);
  108. }
  109. for (unsigned c=1; c<=200; c++) // Edge 0
  110. {
  111. constStr.clear().append(c).append("_0");
  112. AttrStrUnionWithValueTable::roValueTable->find(constStr.str(), true);
  113. }
  114. for (unsigned c=0; c<35; c++)
  115. {
  116. char ch = c<9 ? ('1' + c) : ('A' + (c-9));
  117. constStr.clear().append("~spill::").append(ch); // spills
  118. AttrStrUnionWithValueTable::roValueTable->find(constStr.str(), true);
  119. constStr.clear().append("gl").append(ch); // graph results
  120. AttrStrUnionWithValueTable::roValueTable->find(constStr.str(), true);
  121. constStr.clear().append("mf").append(ch); // meta factories
  122. AttrStrUnionWithValueTable::roValueTable->find(constStr.str(), true);
  123. }
  124. for (unsigned c=1; c<=10; c++) // global auto attributes
  125. {
  126. constStr.clear().append("auto").append(c);
  127. AttrStrUnionWithValueTable::roValueTable->find(constStr.str(), true);
  128. }
  129. #ifdef TRACE_ATOM_SIZE
  130. // If you are wanting an idea of the savings from use of the RO hash table, it may be useful to reset
  131. // the counts here. But it's more correct to actually leave them in place.
  132. //AttrStrAtom::totsize = 0;
  133. //AttrStrAtom::maxsize = 0;
  134. #endif
  135. #ifdef _DEBUG
  136. for (const char **a = roAttributes; *a; a++)
  137. {
  138. // sanity check
  139. unsigned idx = AttrStrUnionWithTable::roNameTable->findIndex(*a, AttrStrC::getHash(*a));
  140. AttrStrC *val = AttrStrUnionWithTable::roNameTable->getIndex(idx);
  141. assert(val && val->eq(*a));
  142. }
  143. for (const char **v = roAttributeValues; *v; v++)
  144. {
  145. // sanity check
  146. unsigned idx = AttrStrUnionWithValueTable::roValueTable->findIndex(*v, AttrStrC::getHash(*v));
  147. AttrStrC *val = AttrStrUnionWithValueTable::roValueTable->getIndex(idx);
  148. assert(val && val->eq(*v));
  149. }
  150. #endif
  151. }
  152. #endif
  153. MODULE_INIT(INIT_PRIORITY_JPTREE)
  154. {
  155. nullPTreeIterator = new NullPTreeIterator;
  156. #ifdef USE_READONLY_ATOMTABLE
  157. AttrStrUnionWithTable::roNameTable = new RONameTable(255);
  158. AttrStrUnionWithValueTable::roValueTable = new RONameTable(4095);
  159. initializeRoTable();
  160. #endif
  161. keyTable = new AtomRefTable;
  162. keyTableNC = new AtomRefTable(true);
  163. attrHT = new CAttrValHashTable;
  164. return true;
  165. }
  166. MODULE_EXIT()
  167. {
  168. delete nullPTreeIterator;
  169. delete attrHT;
  170. keyTable->Release();
  171. keyTableNC->Release();
  172. #ifdef USE_READONLY_ATOMTABLE
  173. delete AttrStrUnionWithTable::roNameTable;
  174. delete AttrStrUnionWithValueTable::roValueTable;
  175. #endif
  176. free(freelist);
  177. freelist = NULL;
  178. }
  179. static int comparePropTrees(IInterface * const *ll, IInterface * const *rr)
  180. {
  181. IPropertyTree *l = (IPropertyTree *) *ll;
  182. IPropertyTree *r = (IPropertyTree *) *rr;
  183. return stricmp(l->queryName(), r->queryName());
  184. };
  185. class CPTArrayIterator : public ArrayIIteratorOf<IArrayOf<IPropertyTree>, IPropertyTree, IPropertyTreeIterator>
  186. {
  187. IArrayOf<IPropertyTree> elems;
  188. public:
  189. CPTArrayIterator(IPropertyTreeIterator &iter, TreeCompareFunc compare) : ArrayIIteratorOf<IArrayOf<IPropertyTree>, IPropertyTree, IPropertyTreeIterator>(elems)
  190. {
  191. ForEach(iter)
  192. elems.append(iter.get());
  193. elems.sort(compare);
  194. }
  195. CPTArrayIterator(IArrayOf<IPropertyTree> & ownedElems, TreeCompareFunc compare) : ArrayIIteratorOf<IArrayOf<IPropertyTree>, IPropertyTree, IPropertyTreeIterator>(elems)
  196. {
  197. elems.swapWith(ownedElems);
  198. elems.sort(compare);
  199. }
  200. };
  201. IPropertyTreeIterator * createSortedIterator(IPropertyTreeIterator & iter)
  202. {
  203. return new CPTArrayIterator(iter, comparePropTrees);
  204. }
  205. IPropertyTreeIterator * createSortedIterator(IPropertyTreeIterator & iter, TreeCompareFunc compare)
  206. {
  207. return new CPTArrayIterator(iter, compare);
  208. }
  209. IPropertyTreeIterator * createSortedIterator(IArrayOf<IPropertyTree> & ownedElems, TreeCompareFunc compare)
  210. {
  211. return new CPTArrayIterator(ownedElems, compare);
  212. }
  213. //////////////////
  214. unsigned ChildMap::getHashFromElement(const void *e) const
  215. {
  216. PTree &elem = (PTree &) (*(IPropertyTree *)e);
  217. return elem.queryHash();
  218. }
  219. unsigned ChildMap::numChildren() const
  220. {
  221. SuperHashIteratorOf<IPropertyTree> iter(*this);
  222. if (!iter.first()) return 0;
  223. unsigned count = 0;
  224. do
  225. {
  226. PTree *element = (PTree *) &iter.query();
  227. if (element->value && element->value->isArray())
  228. count += element->value->elements();
  229. else
  230. ++count;
  231. }
  232. while (iter.next());
  233. return count;
  234. }
  235. IPropertyTreeIterator *ChildMap::getIterator(bool sort)
  236. {
  237. class CPTHashIterator : implements IPropertyTreeIterator, public CInterface
  238. {
  239. SuperHashIteratorOf<IPropertyTree> *hiter;
  240. public:
  241. IMPLEMENT_IINTERFACE;
  242. CPTHashIterator(SuperHashTable &table) { hiter = new SuperHashIteratorOf<IPropertyTree>(table); }
  243. ~CPTHashIterator() { hiter->Release(); }
  244. // IPropertyTreeIterator
  245. virtual bool first() override { return hiter->first(); }
  246. virtual bool next() override { return hiter->next(); }
  247. virtual bool isValid() override { return hiter->isValid(); }
  248. virtual IPropertyTree & query() override { return hiter->query(); }
  249. };
  250. Owned<IPropertyTreeIterator> baseIter = new CPTHashIterator(*this);
  251. if (!sort)
  252. return baseIter.getClear();
  253. return createSortedIterator(*baseIter);
  254. }
  255. ///////////
  256. bool validateXMLTag(const char *name)
  257. {
  258. if (!isValidXPathStartChr(*name)) return false;
  259. ++name;
  260. while (*name != '\0')
  261. {
  262. if (!isValidXPathChr(*name)) return false;
  263. ++name;
  264. }
  265. return true;
  266. }
  267. class jlib_thrown_decl CPTreeException : implements IPTreeException, public CInterface
  268. {
  269. int errCode;
  270. StringBuffer errMsg;
  271. public:
  272. IMPLEMENT_IINTERFACE;
  273. CPTreeException(int _errCode, const char *_errMsg, va_list &args) __attribute__((format(printf,3,0))) : errCode(_errCode)
  274. {
  275. if (_errMsg)
  276. errMsg.valist_appendf(_errMsg, args);
  277. }
  278. StringBuffer &translateCode(StringBuffer &out) const
  279. {
  280. out.append("IPropertyTree: ");
  281. switch (errCode)
  282. {
  283. case PTreeExcpt_XPath_Ambiguity:
  284. return out.append("Ambiguous xpath used");
  285. case PTreeExcpt_XPath_ParseError:
  286. return out.append("xpath parse error");
  287. case PTreeExcpt_XPath_Unsupported:
  288. return out.append("unsupported xpath syntax used");
  289. case PTreeExcpt_InvalidTagName:
  290. return out.append("Invalid tag name");
  291. default:
  292. return out.append("UNKNOWN ERROR CODE: ").append(errCode);
  293. }
  294. }
  295. // IException
  296. int errorCode() const { return errCode; }
  297. StringBuffer &errorMessage(StringBuffer &out) const
  298. {
  299. return translateCode(out).append("\n").append(errMsg.str());
  300. }
  301. MessageAudience errorAudience() const { return MSGAUD_user; }
  302. };
  303. static IPTreeException *MakeIPTException(int code, const char *format, ...) __attribute__((format(printf,2,3)));
  304. static IPTreeException *MakeXPathException(const char *xpath, int code, size_t pos, const char *format, ...) __attribute__((format(printf,4,5)));
  305. IPTreeException *MakeIPTException(int code, const char *format, ...)
  306. {
  307. va_list args;
  308. va_start(args, format);
  309. IPTreeException *e = new CPTreeException(code, format, args);
  310. va_end(args);
  311. return e;
  312. }
  313. IPTreeException *MakeXPathException(const char *xpath, int code, size_t pos, const char *format, ...)
  314. {
  315. va_list args;
  316. va_start(args, format);
  317. StringBuffer s("XPath Exception: ");
  318. s.valist_appendf(format, args);
  319. va_end(args);
  320. #ifdef _DEBUG
  321. PrintStackReport();
  322. #endif
  323. const char *msg = "in xpath = ";
  324. s.append("\n").append(msg).append(xpath);
  325. s.append("\n").appendN((size32_t)(strlen(msg)+pos), ' ').append("^");
  326. return MakeIPTException(code, "%s", s.str());
  327. }
  328. inline static void readID(const char *&xxpath, bool started)
  329. {
  330. const char *xpath = xxpath;
  331. if (isValidXPathStartChr(*xpath) || (started && isValidXPathChr(*xpath)))
  332. {
  333. do
  334. {
  335. xpath++;
  336. } while (isValidXPathChr(*xpath));
  337. xxpath = xpath;
  338. }
  339. }
  340. inline static void readWildId(const char *&xpath, bool &wild)
  341. {
  342. wild = false;
  343. for (;;)
  344. {
  345. readID(xpath, wild);
  346. if ('*' != *xpath)
  347. break;
  348. wild = true;
  349. ++xpath;
  350. }
  351. }
  352. inline const char * readIndex(const char *xpath, StringAttr &index)
  353. {
  354. const char *start = xpath;
  355. do { xpath++; } while (isdigit(*xpath));
  356. index.set(start, (xpath - start));
  357. return xpath;
  358. }
  359. inline static void readWildIdIndex(const char *&xpath, bool &wild, bool &numeric)
  360. {
  361. const char *_xpath = xpath;
  362. readWildId(xpath, wild);
  363. if ('[' == *xpath) // check for local index not iterative qualifier.
  364. {
  365. const char *end = xpath+1;
  366. if (isdigit(*end))
  367. {
  368. StringAttr index;
  369. end = readIndex(end, index);
  370. if (']' != *end)
  371. throw MakeXPathException(_xpath, PTreeExcpt_XPath_ParseError, xpath-_xpath, "Qualifier brace unclosed");
  372. xpath = end+1;
  373. numeric = true;
  374. }
  375. else
  376. numeric = false;
  377. }
  378. else
  379. numeric = false;
  380. }
  381. inline static unsigned getTailIdLength(const char *xxpath, unsigned xxpathlength)
  382. {
  383. const char *xpath = xxpath+xxpathlength;
  384. const char *end = xpath;
  385. while (xpath != xxpath)
  386. {
  387. --xpath;
  388. if (!isValidXPathChr(*xpath)) break;
  389. }
  390. if (!isAttribute(xpath) && xpath != xxpath) ++xpath;
  391. return end-xpath;
  392. }
  393. const char *splitXPathUQ(const char *xpath, StringBuffer &path)
  394. {
  395. size32_t xpathSize = (size32_t) strlen(xpath);
  396. size32_t idSize = getTailIdLength(xpath, xpathSize);
  397. path.append(xpathSize-idSize, xpath);
  398. return xpath + (xpathSize-idSize);
  399. }
  400. const char *splitXPathX(const char *xpath)
  401. {
  402. size32_t xpathSize = (size32_t) strlen(xpath);
  403. size32_t idSize = getTailIdLength(xpath, xpathSize);
  404. return xpath + (xpathSize-idSize);
  405. }
  406. // similar to above, splitXPathUQ doesn't split if qualified
  407. const char *splitXPath(const char *xpath, StringBuffer &headPath)
  408. {
  409. StringBuffer path;
  410. const char *end = xpath+strlen(xpath);
  411. const char *prop = end;
  412. bool quote = false;
  413. bool braced = false;
  414. while (xpath != prop)
  415. {
  416. --prop;
  417. if (*prop == '"')
  418. {
  419. if (quote) quote = false;
  420. else quote = true;
  421. }
  422. else if (*prop == ']' && !quote)
  423. {
  424. assertex(!braced);
  425. braced = true;
  426. }
  427. else if (*prop == '[' && !quote)
  428. {
  429. assertex(braced);
  430. braced = false;
  431. }
  432. else if (*prop == '/' && !quote && !braced)
  433. {
  434. ++prop;
  435. break;
  436. }
  437. }
  438. if (prop == end)
  439. return NULL;
  440. else if (xpath != prop)
  441. {
  442. size32_t ps = prop-xpath-1;
  443. headPath.append(ps, xpath);
  444. }
  445. return prop;
  446. }
  447. const char *queryNextUnquoted(const char *str, char c)
  448. {
  449. bool quote = false;
  450. for (;;)
  451. {
  452. char next = *str;
  453. if (next == '\0')
  454. return NULL;
  455. if ('"' == next)
  456. quote = !quote;
  457. else if (c == next && !quote)
  458. return str;
  459. ++str;
  460. }
  461. }
  462. const char *queryHead(const char *xpath, StringBuffer &head)
  463. {
  464. if (!xpath) return NULL;
  465. const char *start = xpath;
  466. bool quote = false;
  467. bool braced = false;
  468. for (;;)
  469. {
  470. if (*xpath == '\0')
  471. return NULL;
  472. ++xpath;
  473. char next = *xpath;
  474. if ('"' == next)
  475. quote = !quote;
  476. else if (next == ']' && !quote)
  477. {
  478. assertex(braced);
  479. braced = false;
  480. }
  481. else if (next == '[' && !quote)
  482. {
  483. assertex(!braced);
  484. braced = true;
  485. }
  486. else if (next == '/' && !quote && !braced)
  487. {
  488. if ('/' == *start) // so leading '//'
  489. return start;
  490. else if ('/' == *(xpath+1)) // in middle of path
  491. {
  492. head.append(xpath-start, start);
  493. return xpath;
  494. }
  495. break;
  496. }
  497. }
  498. head.append(xpath-start, start);
  499. return xpath+1;
  500. }
  501. ///////////////////
  502. static constexpr unsigned defaultSiblingMapThreshold = 100;
  503. static unsigned siblingMapThreshold = (unsigned)-1; // off until configuration default it on.
  504. void setPTreeMappingThreshold(unsigned threshold)
  505. {
  506. /*
  507. * NB: setPTreeMappingThreshold() will automatically be called via loadConfiguration
  508. * Redefining this limit, will not effect existing maps, and should generally only be called once during startup.
  509. */
  510. if (0 == threshold)
  511. threshold = (unsigned)-1;
  512. siblingMapThreshold = threshold;
  513. }
  514. class CValueMap : public std::unordered_multimap<std::string, const IPropertyTree *>
  515. {
  516. public:
  517. CValueMap(const char *_lhs, IPTArrayValue &array)
  518. {
  519. IPropertyTree **elements = array.getRawArray();
  520. IPropertyTree **last = elements+array.elements();
  521. dbgassertex(elements != last);
  522. while (true)
  523. {
  524. const char *v = (*elements)->queryProp(_lhs);
  525. if (v)
  526. emplace(std::make_pair(std::string(v), *elements));
  527. elements++;
  528. if (last == elements)
  529. break;
  530. }
  531. }
  532. std::pair<CValueMap::iterator, CValueMap::iterator> find(const char *rhs)
  533. {
  534. return equal_range(std::string(rhs));
  535. }
  536. void insertEntry(const char *v, const IPropertyTree *tree)
  537. {
  538. emplace(std::make_pair(std::string(v), tree));
  539. }
  540. bool removeEntry(const char *v, const IPropertyTree *tree)
  541. {
  542. auto range = equal_range(std::string(v));
  543. if (range.first == range.second)
  544. return false;
  545. auto it = range.first;
  546. while (true)
  547. {
  548. if (it->second == tree)
  549. {
  550. it = erase(it);
  551. return true;
  552. }
  553. ++it;
  554. if (it == range.second)
  555. break;
  556. }
  557. throwUnexpected();
  558. }
  559. void replaceEntry(const char *oldV, const char *newV, const IPropertyTree *tree)
  560. {
  561. verifyex(removeEntry(oldV, tree));
  562. if (newV)
  563. insertEntry(newV, tree);
  564. }
  565. };
  566. class CQualifierMap
  567. {
  568. std::unordered_map<std::string, CValueMap *> attrValueMaps;
  569. CriticalSection crit;
  570. public:
  571. CQualifierMap()
  572. {
  573. }
  574. ~CQualifierMap()
  575. {
  576. for (auto &e: attrValueMaps)
  577. delete e.second;
  578. }
  579. CValueMap *addMapping(const char *lhs, IPTArrayValue &array)
  580. {
  581. CValueMap *valueMap = new CValueMap(lhs, array);
  582. attrValueMaps.emplace(std::make_pair(std::string(lhs), valueMap));
  583. return valueMap;
  584. }
  585. CValueMap *addMappingIfNew(const char *lhs, IPTArrayValue &array)
  586. {
  587. CriticalBlock b(crit);
  588. auto it = attrValueMaps.find(lhs);
  589. if (it == attrValueMaps.end())
  590. return addMapping(lhs, array);
  591. else
  592. return it->second;
  593. }
  594. void addMatchingValues(const IPropertyTree *tree)
  595. {
  596. for (auto &e: attrValueMaps)
  597. {
  598. const char *v = tree->queryProp(e.first.c_str());
  599. if (v)
  600. e.second->insertEntry(v, tree);
  601. }
  602. }
  603. void removeMatchingValues(const IPropertyTree *tree)
  604. {
  605. for (auto &e: attrValueMaps)
  606. {
  607. const char *lhsp = e.first.c_str();
  608. const char *oldV = tree->queryProp(lhsp);
  609. if (oldV)
  610. verifyex(e.second->removeEntry(oldV, tree));
  611. }
  612. }
  613. void replaceMatchingValues(const IPropertyTree *oldTree, const IPropertyTree *newTree)
  614. {
  615. for (auto &e: attrValueMaps)
  616. {
  617. const char *lhsp = e.first.c_str();
  618. const char *oldV = oldTree->queryProp(lhsp);
  619. if (oldV)
  620. {
  621. verifyex(e.second->removeEntry(oldV, oldTree));
  622. const char *newV = newTree->queryProp(lhsp);
  623. if (newV)
  624. e.second->insertEntry(newV, newTree);
  625. }
  626. }
  627. }
  628. CValueMap *find(const char *lhs)
  629. {
  630. auto it = attrValueMaps.find(lhs);
  631. if (it == attrValueMaps.end())
  632. return nullptr;
  633. return it->second;
  634. }
  635. void removeEntryIfMapped(const char *lhs, const char *v, const IPropertyTree *tree)
  636. {
  637. auto it = attrValueMaps.find(lhs);
  638. if (it != attrValueMaps.end())
  639. it->second->removeEntry(v, tree);
  640. }
  641. void insertEntryIfMapped(const char *lhs, const char *v, const IPropertyTree *tree)
  642. {
  643. auto it = attrValueMaps.find(lhs);
  644. if (it != attrValueMaps.end())
  645. it->second->insertEntry(v, tree);
  646. }
  647. void replaceEntryIfMapped(const char *lhs, const char *oldv, const char *newv, const IPropertyTree *tree)
  648. {
  649. auto it = attrValueMaps.find(lhs);
  650. if (it != attrValueMaps.end())
  651. it->second->replaceEntry(oldv, newv, tree);
  652. }
  653. };
  654. // parse qualifier, returns true if simple equality expression found
  655. static bool parseEqualityQualifier(const char *&xxpath, unsigned &lhsLen, const char *&rhsBegin, unsigned &rhsLen)
  656. {
  657. const char *xpath = xxpath;
  658. while (*xpath == ' ' || *xpath == '\t') xpath++;
  659. if ('@' != *xpath) // only attributes supported
  660. return false;
  661. const char *start = xpath;
  662. char quote = 0;
  663. const char *lhsEnd, *quoteBegin, *quoteEnd, *rhsEnd;
  664. lhsEnd = quoteBegin = quoteEnd = rhsBegin = rhsEnd = NULL;
  665. bool equalSignFound = false;
  666. for (;;)
  667. {
  668. switch (*xpath)
  669. {
  670. case '"':
  671. case '\'':
  672. if (quote)
  673. {
  674. if (*xpath == quote)
  675. {
  676. quote = 0;
  677. quoteEnd = xpath;
  678. }
  679. }
  680. else
  681. {
  682. if (quoteBegin)
  683. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Quoted left hand side already seen");
  684. quote = *xpath;
  685. quoteBegin = xpath+1;
  686. }
  687. break;
  688. case '[':
  689. if (!quote)
  690. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Unclosed qualifier detected");
  691. break;
  692. case ']':
  693. if (!quote)
  694. {
  695. if (!lhsEnd)
  696. lhsEnd = xpath;
  697. rhsEnd = xpath;
  698. }
  699. break;
  700. case ' ':
  701. case '\t':
  702. if (!lhsEnd)
  703. lhsEnd = xpath;
  704. break;
  705. case '!':
  706. case '>':
  707. case '<':
  708. case '~':
  709. case '/':
  710. if (!quote)
  711. return false;
  712. break;
  713. case '=':
  714. if (!quote)
  715. {
  716. if (equalSignFound)
  717. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Unexpected expression operator xpath");
  718. equalSignFound = true;
  719. if (!lhsEnd)
  720. lhsEnd = xpath;
  721. }
  722. break;
  723. case '?':
  724. case '*':
  725. return false;
  726. case '\0':
  727. rhsEnd = xpath;
  728. break;
  729. }
  730. if (rhsEnd)
  731. break;
  732. xpath++;
  733. if (!rhsBegin && equalSignFound && !isspace(*xpath))
  734. rhsBegin = xpath;
  735. }
  736. if (quote)
  737. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Parse error, unclosed quoted content");
  738. if (!equalSignFound)
  739. return false;
  740. lhsLen = lhsEnd-start;
  741. if (quoteBegin && !quoteEnd)
  742. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Parse error, RHS missing closing quote");
  743. if (rhsBegin && !rhsEnd)
  744. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Parse error, RHS missing closing quote");
  745. if (!quoteBegin && rhsEnd) // only if numeric
  746. return false;
  747. else // quoted
  748. {
  749. rhsBegin = quoteBegin;
  750. rhsLen = quoteEnd - rhsBegin;
  751. }
  752. if (rhsEnd && *xpath == ']')
  753. xpath++;
  754. xxpath = xpath;
  755. return true;
  756. }
  757. class CMapQualifierIterator : public CInterfaceOf<IPropertyTreeIterator>
  758. {
  759. CValueMap::iterator startRange, endRange;
  760. CValueMap::iterator currentIter;
  761. public:
  762. CMapQualifierIterator(CQualifierMap &_map, CValueMap::iterator _startRange, CValueMap::iterator _endRange)
  763. : startRange(_startRange), endRange(_endRange)
  764. {
  765. }
  766. // IPropertyTreeIterator
  767. virtual bool first() override
  768. {
  769. currentIter = startRange;
  770. return currentIter != endRange;
  771. }
  772. virtual bool next() override
  773. {
  774. currentIter++;
  775. return currentIter != endRange;
  776. }
  777. virtual bool isValid() override
  778. {
  779. return currentIter != endRange;
  780. }
  781. virtual IPropertyTree & query() override { return const_cast<IPropertyTree &>(*currentIter->second); }
  782. };
  783. IPropertyTreeIterator *checkMapIterator(const char *&xxpath, IPropertyTree &child)
  784. {
  785. /*
  786. * NB: IPT's are not thread safe. It is up to the caller to ensure multiple writers do not contend.
  787. * ( Dali for example ensures writer threads are exclusive )
  788. *
  789. * That means multiple reader threads could be here concurrently.
  790. * >1 could be constructing the qualifier map for the 1st time.
  791. * For new attr updates (where map already exists), it will block on map::crit,
  792. * so that there is at most 1 thread updating the map. The underlying unordered_multiset
  793. * is thread safe if 1 writer, and multiple readers.
  794. *
  795. * On initial map creation, allow concurrency, but only 1 will succeed to swap in the new active map.
  796. * That could mean a new attr/prop. mapping is lost, until next used.
  797. * NB: once the map is live, updates are write ops. and so, just as with the IPT
  798. * itself, it is expected that something will keep it thread safe (as Dali does)
  799. *
  800. */
  801. // NB: only support simple @<attrname>=<value> qualifiers
  802. if (((unsigned)-1) == siblingMapThreshold) // disabled
  803. return nullptr;
  804. PTree &_child = (PTree &)child;
  805. if (child.isCaseInsensitive()) // NB: could support but not worth it.
  806. return nullptr;
  807. IPTArrayValue *value = _child.queryValue();
  808. if (!value)
  809. return nullptr;
  810. CQualifierMap *map = value->queryMap();
  811. if (!map)
  812. {
  813. if (!value->isArray() || (value->elements() < siblingMapThreshold))
  814. return nullptr;
  815. }
  816. unsigned lhsLen, rhsLen;
  817. const char *rhsStart;
  818. const char *xpath = xxpath;
  819. if (!parseEqualityQualifier(xpath, lhsLen, rhsStart, rhsLen))
  820. return nullptr;
  821. MAKE_LSTRING(lhs, xxpath, lhsLen);
  822. MAKE_LSTRING(rhs, rhsStart, rhsLen);
  823. // NB: there can be a race here where >1 reader is constructing new map
  824. CValueMap *valueMap = nullptr;
  825. if (map)
  826. valueMap = map->addMappingIfNew(lhs, *value);
  827. else
  828. {
  829. OwnedPtr<CQualifierMap> newMap = new CQualifierMap();
  830. valueMap = newMap->addMapping(lhs, *value);
  831. /*
  832. * NB: it's possible another read thread got here 1st, and swapped in a map.
  833. * setMap returns the existing map, and the code below checks to see if it already
  834. * handles the 'lhs' we're adding, if it doesn't it re-adds the qualifier mappings.
  835. */
  836. map = value->setMap(newMap);
  837. if (!map) // successfully swapped newMap in.
  838. map = newMap.getClear(); // NB: setMap owns
  839. else // another thread has swapped in a map whilst I was creating new one
  840. valueMap = map->addMappingIfNew(lhs, *value);
  841. }
  842. xxpath = xpath; // update parsed position
  843. auto range = valueMap->find(rhs);
  844. if (range.first != range.second)
  845. return new CMapQualifierIterator(*map, range.first, range.second);
  846. else
  847. return LINK(nullPTreeIterator);
  848. }
  849. ///////////////////
  850. class SeriesPTIterator : implements IPropertyTreeIterator, public CInterface
  851. {
  852. public:
  853. IMPLEMENT_IINTERFACE;
  854. SeriesPTIterator() : current(NULL), cp(0)
  855. {
  856. }
  857. void addIterator(IPropertyTreeIterator *iter) { iters.append(*iter); }
  858. // IPropertyTreeIterator impl.
  859. virtual bool first() override
  860. {
  861. cp = 0;
  862. iterCount = iters.ordinality();
  863. if (nextIterator())
  864. return true;
  865. else
  866. return false;
  867. }
  868. virtual bool next() override
  869. {
  870. while (currentIter)
  871. {
  872. if (currentIter->next())
  873. {
  874. current = &currentIter->query();
  875. return true;
  876. }
  877. if (nextIterator())
  878. return true;
  879. }
  880. current = NULL;
  881. return false;
  882. }
  883. virtual bool isValid() override { return (NULL != current); }
  884. virtual IPropertyTree & query() override { assertex(current); return *current; }
  885. private:
  886. bool nextIterator()
  887. {
  888. while (cp<iterCount)
  889. {
  890. currentIter = (IPropertyTreeIterator *) &iters.item(cp++);
  891. if (currentIter->first())
  892. {
  893. current = &currentIter->query();
  894. return true;
  895. }
  896. }
  897. current = NULL;
  898. currentIter = NULL;
  899. return false;
  900. }
  901. IArray iters;
  902. IPropertyTreeIterator *currentIter;
  903. IPropertyTree *current;
  904. unsigned cp, iterCount;
  905. };
  906. ///////////////////
  907. CPTValue::CPTValue(size32_t size, const void *data, bool binary, bool raw, bool _compressed)
  908. {
  909. compressed = _compressed;
  910. if (!raw && binary && size > PTREE_COMPRESS_THRESHOLD)
  911. {
  912. unsigned newSize = size * PTREE_COMPRESS_BOTHER_PECENTAGE / 100;
  913. void *newData = NULL;
  914. ICompressor *compressor = NULL;
  915. try
  916. {
  917. newData = malloc(sizeof(size32_t) + newSize);
  918. compressor = createLZWCompressor();
  919. compressor->open(((char *)newData) + sizeof(size32_t), newSize);
  920. if (compressor->write(data, size)==size)
  921. {
  922. compressor->close();
  923. memcpy(newData, &size, sizeof(size32_t));
  924. newSize = sizeof(size32_t) + compressor->buflen();
  925. compressed = true;
  926. set(newSize, newData);
  927. }
  928. free(newData);
  929. compressor->Release();
  930. }
  931. catch (...)
  932. {
  933. if (newData)
  934. free(newData);
  935. if (compressor) compressor->Release();
  936. throw;
  937. }
  938. }
  939. if (raw || !compressed)
  940. set(size, data);
  941. }
  942. static void *uncompress(const void *src, size32_t &sz)
  943. {
  944. IExpander *expander = NULL;
  945. void *uncompressedValue = NULL;
  946. try
  947. {
  948. memcpy(&sz, src, sizeof(size32_t));
  949. assertex(sz);
  950. expander = createLZWExpander();
  951. src = ((const char *)src) + sizeof(size32_t);
  952. uncompressedValue = malloc(sz);
  953. assertex(uncompressedValue);
  954. expander->init(src);
  955. expander->expand(uncompressedValue);
  956. expander->Release();
  957. return uncompressedValue;
  958. }
  959. catch (...)
  960. {
  961. if (expander) expander->Release();
  962. if (uncompressedValue) free(uncompressedValue);
  963. throw;
  964. }
  965. }
  966. const void *CPTValue::queryValue() const
  967. {
  968. if (compressed)
  969. {
  970. size32_t sz;
  971. void *uncompressedValue = uncompress(get(), sz);
  972. ((MemoryAttr *)this)->setOwn(sz, uncompressedValue);
  973. compressed = false;
  974. }
  975. return get();
  976. }
  977. void CPTValue::serialize(MemoryBuffer &tgt)
  978. {
  979. //Retain backward compatibility for the serialization format.
  980. size32_t serialLen = (size32_t)length();
  981. tgt.append(serialLen);
  982. if (serialLen)
  983. {
  984. tgt.append(compressed);
  985. tgt.append(serialLen, get());
  986. }
  987. }
  988. void CPTValue::deserialize(MemoryBuffer &src)
  989. {
  990. size32_t sz;
  991. src.read(sz);
  992. if (sz)
  993. {
  994. src.read(compressed);
  995. set(sz, src.readDirect(sz));
  996. }
  997. else
  998. {
  999. compressed = false;
  1000. clear();
  1001. }
  1002. }
  1003. MemoryBuffer &CPTValue::getValue(MemoryBuffer &tgt, bool binary) const
  1004. {
  1005. if (compressed)
  1006. {
  1007. size32_t sz;
  1008. void *uncompressedValue = uncompress(get(), sz);
  1009. if (!binary) sz -= 1;
  1010. tgt.append(sz, uncompressedValue);
  1011. if (uncompressedValue)
  1012. free(uncompressedValue);
  1013. }
  1014. else
  1015. {
  1016. if (binary)
  1017. tgt.append((size32_t)length(), get());
  1018. else
  1019. tgt.append((size32_t)length()-1, get());
  1020. }
  1021. return tgt;
  1022. }
  1023. StringBuffer &CPTValue::getValue(StringBuffer &tgt, bool binary) const
  1024. {
  1025. if (compressed)
  1026. {
  1027. size32_t sz;
  1028. void *uncompressedValue = NULL;
  1029. try
  1030. {
  1031. uncompressedValue = uncompress(get(), sz);
  1032. if (!binary) sz -= 1;
  1033. tgt.append(sz, (const char *)uncompressedValue);
  1034. free(uncompressedValue);
  1035. }
  1036. catch (IException *)
  1037. {
  1038. if (uncompressedValue) free(uncompressedValue);
  1039. throw;
  1040. }
  1041. }
  1042. else
  1043. {
  1044. if (binary) // this should probably be an assert?
  1045. tgt.append((size32_t)length(), (const char *)get());
  1046. else if (length())
  1047. tgt.append((size32_t)length()-1, (const char *)get());
  1048. }
  1049. return tgt;
  1050. }
  1051. size32_t CPTValue::queryValueSize() const
  1052. {
  1053. if (compressed)
  1054. {
  1055. size32_t sz;
  1056. memcpy(&sz, get(), sizeof(size32_t));
  1057. return sz;
  1058. }
  1059. else
  1060. return (size32_t)length();
  1061. }
  1062. ///////////////////
  1063. CPTArray::~CPTArray()
  1064. {
  1065. if (map.load())
  1066. delete map.load();
  1067. }
  1068. CQualifierMap *CPTArray::setMap(CQualifierMap *_map)
  1069. {
  1070. CQualifierMap *expected = nullptr;
  1071. if (map.compare_exchange_strong(expected, _map))
  1072. return nullptr;
  1073. else
  1074. return expected;
  1075. }
  1076. void CPTArray::addElement(IPropertyTree *tree)
  1077. {
  1078. append(*tree);
  1079. CQualifierMap *map = queryMap();
  1080. if (map)
  1081. {
  1082. if (tree->getAttributeCount())
  1083. map->addMatchingValues(tree);
  1084. }
  1085. }
  1086. void CPTArray::setElement(unsigned idx, IPropertyTree *tree)
  1087. {
  1088. CQualifierMap *map = queryMap();
  1089. if (map)
  1090. {
  1091. // remove any mappings for existing element.
  1092. if (isItem(idx))
  1093. {
  1094. IPropertyTree *existing = &((IPropertyTree &)item(idx));
  1095. map->replaceMatchingValues(existing, tree);
  1096. }
  1097. else
  1098. map->addMatchingValues(tree);
  1099. }
  1100. add(*tree, idx);
  1101. }
  1102. void CPTArray::removeElement(unsigned idx)
  1103. {
  1104. CQualifierMap *map = queryMap();
  1105. if (map)
  1106. {
  1107. IPropertyTree *existing = &((IPropertyTree &)item(idx));
  1108. map->removeMatchingValues(existing);
  1109. }
  1110. remove(idx);
  1111. }
  1112. unsigned CPTArray::find(const IPropertyTree *search) const
  1113. {
  1114. IInterface **start = getArray();
  1115. IInterface **last = start + ordinality();
  1116. IInterface **members = start;
  1117. while (true)
  1118. {
  1119. if (*members == search)
  1120. return members-start;
  1121. members++;
  1122. if (members == last)
  1123. break;
  1124. }
  1125. return NotFound;
  1126. }
  1127. //////////////////
  1128. PTree::PTree(byte _flags, IPTArrayValue *_value, ChildMap *_children)
  1129. {
  1130. flags = _flags;
  1131. children = LINK(_children);
  1132. value = _value;
  1133. }
  1134. PTree::~PTree()
  1135. {
  1136. if (value) delete value;
  1137. ::Release(children);
  1138. }
  1139. IPropertyTree *PTree::queryChild(unsigned index)
  1140. {
  1141. if (!value) return NULL;
  1142. if (!value->isArray()) return this;
  1143. IPropertyTree *v = value->queryElement(index);
  1144. return v;
  1145. }
  1146. aindex_t PTree::findChild(IPropertyTree *child, bool remove)
  1147. {
  1148. if (value && value->isArray())
  1149. {
  1150. unsigned pos = value->find(child);
  1151. if (remove && NotFound != pos)
  1152. value->removeElement(pos);
  1153. return pos;
  1154. }
  1155. else if (checkChildren())
  1156. {
  1157. IPropertyTree *_child = children->query(child->queryName());
  1158. if (_child == child)
  1159. {
  1160. if (remove)
  1161. children->removeExact(_child);
  1162. return 0;
  1163. }
  1164. else if (_child)
  1165. {
  1166. PTree *__child = (PTree *) _child;
  1167. return __child->findChild(child, remove);
  1168. }
  1169. }
  1170. return NotFound;
  1171. }
  1172. ChildMap *PTree::checkChildren() const
  1173. {
  1174. return children;
  1175. }
  1176. void PTree::setLocal(size32_t l, const void *data, bool _binary)
  1177. {
  1178. if (value) delete value;
  1179. if (l)
  1180. value = new CPTValue(l, data, _binary);
  1181. else
  1182. value = NULL;
  1183. if (_binary)
  1184. IptFlagSet(flags, ipt_binary);
  1185. else
  1186. IptFlagClr(flags, ipt_binary);
  1187. }
  1188. void PTree::appendLocal(size32_t l, const void *data, bool binary)
  1189. {
  1190. if (0 == l) return;
  1191. MemoryBuffer mb;
  1192. if (value)
  1193. {
  1194. assertex(!value->isArray());
  1195. assertex(binary == IptFlagTst(flags, ipt_binary));
  1196. value->getValue(mb, binary);
  1197. mb.append(l, data);
  1198. delete value;
  1199. l = mb.length();
  1200. data = mb.toByteArray();
  1201. }
  1202. if (l)
  1203. value = new CPTValue(l, data, binary);
  1204. else
  1205. value = NULL;
  1206. if (binary)
  1207. IptFlagSet(flags, ipt_binary);
  1208. else
  1209. IptFlagClr(flags, ipt_binary);
  1210. }
  1211. // IPropertyTree impl.
  1212. bool PTree::hasProp(const char * xpath) const
  1213. {
  1214. const char *prop = splitXPathX(xpath);
  1215. if (isAttribute(prop)) // JCS - note no wildcards on attributes
  1216. {
  1217. if (prop != xpath)
  1218. {
  1219. MAKE_LSTRING(path, xpath, prop-xpath);
  1220. Owned<IPropertyTreeIterator> iter = getElements(path);
  1221. if (iter->first())
  1222. {
  1223. do
  1224. {
  1225. IPropertyTree &branch = iter->query();
  1226. if (branch.hasProp(prop))
  1227. return true;
  1228. }
  1229. while (iter->next());
  1230. }
  1231. return false;
  1232. }
  1233. else
  1234. return nullptr != findAttribute(xpath);
  1235. }
  1236. else
  1237. {
  1238. IPropertyTreeIterator *iter = getElements(xpath);
  1239. bool res = iter->first();
  1240. iter->Release();
  1241. return res;
  1242. }
  1243. }
  1244. const char *PTree::queryProp(const char *xpath) const
  1245. {
  1246. if (!xpath)
  1247. {
  1248. if (!value) return NULL;
  1249. return (const char *) value->queryValue();
  1250. }
  1251. else if (isAttribute(xpath))
  1252. return getAttributeValue(xpath);
  1253. else
  1254. {
  1255. const char *prop = splitXPathX(xpath);
  1256. if (isAttribute(prop))
  1257. {
  1258. MAKE_LSTRING(path, xpath, prop-xpath);
  1259. IPropertyTree *branch = queryPropTree(path);
  1260. if (!branch) return NULL;
  1261. return branch->queryProp(prop);
  1262. }
  1263. else
  1264. {
  1265. IPropertyTree *branch = queryPropTree(xpath);
  1266. if (!branch) return NULL;
  1267. return branch->queryProp(NULL);
  1268. }
  1269. }
  1270. }
  1271. bool PTree::getProp(const char *xpath, StringBuffer &ret) const
  1272. {
  1273. if (!xpath)
  1274. {
  1275. if (!value) return false;
  1276. value->getValue(ret, IptFlagTst(flags, ipt_binary));
  1277. return true;
  1278. }
  1279. else if (isAttribute(xpath))
  1280. {
  1281. const char *value = getAttributeValue(xpath);
  1282. if (!value) return false;
  1283. ret.append(value);
  1284. return true;
  1285. }
  1286. else
  1287. {
  1288. const char *prop = splitXPathX(xpath);
  1289. if (isAttribute(prop))
  1290. {
  1291. MAKE_LSTRING(path, xpath, prop-xpath)
  1292. IPropertyTree *branch = queryPropTree(path);
  1293. if (!branch) return false;
  1294. return branch->getProp(prop, ret);
  1295. }
  1296. else
  1297. {
  1298. IPropertyTree *branch = queryPropTree(xpath);
  1299. if (!branch) return false;
  1300. return branch->getProp(NULL, ret);
  1301. }
  1302. }
  1303. }
  1304. void PTree::setProp(const char *xpath, const char *val)
  1305. {
  1306. if (!xpath || '\0' == *xpath)
  1307. {
  1308. if (!val)
  1309. {
  1310. if (value) delete value;
  1311. value = NULL;
  1312. }
  1313. else
  1314. {
  1315. size32_t l=(size32_t)strlen(val);
  1316. if (!l)
  1317. {
  1318. if (value) delete value;
  1319. value = NULL;
  1320. }
  1321. else
  1322. setLocal(l+1, val);
  1323. }
  1324. }
  1325. else if (isAttribute(xpath))
  1326. {
  1327. if (!val)
  1328. removeAttribute(xpath);
  1329. else
  1330. setAttribute(xpath, val);
  1331. }
  1332. else
  1333. {
  1334. const char *prop;
  1335. IPropertyTree *branch = splitBranchProp(xpath, prop, true);
  1336. if (isAttribute(prop))
  1337. branch->setProp(prop, val);
  1338. else
  1339. {
  1340. if (val)
  1341. {
  1342. IPropertyTree *propBranch = queryCreateBranch(branch, prop);
  1343. propBranch->setProp(NULL, val);
  1344. }
  1345. else
  1346. branch->removeProp(prop);
  1347. }
  1348. }
  1349. }
  1350. aindex_t PTree::getChildMatchPos(const char *xpath)
  1351. {
  1352. Owned<IPropertyTreeIterator> childIter = getElements(xpath);
  1353. if (!childIter->first())
  1354. return (aindex_t)-1;
  1355. IPropertyTree &childMatch = childIter->query();
  1356. #ifdef _DEBUG
  1357. if (childIter->next())
  1358. AMBIGUOUS_PATH("addPropX", xpath);
  1359. #endif
  1360. if (value)
  1361. if (value->isArray())
  1362. return findChild(&childMatch);
  1363. else
  1364. return 0;
  1365. else
  1366. return 0;
  1367. }
  1368. void PTree::resolveParentChild(const char *xpath, IPropertyTree *&parent, IPropertyTree *&child, StringAttr &path, StringAttr &qualifier)
  1369. {
  1370. parent = child = NULL;
  1371. if (!xpath)
  1372. throw MakeIPTException(-1, "No path to resolve parent from");
  1373. const char *end = xpath+strlen(xpath);
  1374. const char *prop = end;
  1375. while (prop != xpath && *(prop-1) != '/')
  1376. --prop;
  1377. size32_t ps = prop-xpath;
  1378. if (ps)
  1379. {
  1380. path.set(xpath, ps);
  1381. Owned<IPropertyTreeIterator> pathIter = getElements(path);
  1382. if (!pathIter->first())
  1383. throw MakeIPTException(-1, "resolveParentChild: path not found %s", xpath);
  1384. /* If 'path' resolves to iterator of this, then treat as if no leading path
  1385. * i.e. "./x", or "././.x" is equivalent to "x"
  1386. */
  1387. if (this != &pathIter->query())
  1388. {
  1389. IPropertyTree *currentPath = NULL;
  1390. #ifdef _DEBUG
  1391. bool multiplePaths = false;
  1392. #endif
  1393. bool multipleChildMatches = false;
  1394. for (;;)
  1395. {
  1396. // JCSMORE - a bit annoying has to be done again once path has been established
  1397. currentPath = &pathIter->query();
  1398. Owned<IPropertyTreeIterator> childIter = currentPath->getElements(prop);
  1399. if (childIter->first())
  1400. {
  1401. child = &childIter->query();
  1402. #ifdef _DEBUG
  1403. if (parent)
  1404. AMBIGUOUS_PATH("resolveParentChild", xpath);
  1405. #endif
  1406. if (!multipleChildMatches && childIter->next())
  1407. multipleChildMatches = true;
  1408. parent = currentPath;
  1409. }
  1410. if (pathIter->next())
  1411. {
  1412. #ifdef _DEBUG
  1413. multiplePaths = true;
  1414. #endif
  1415. }
  1416. else
  1417. break;
  1418. }
  1419. if (!parent)
  1420. {
  1421. #ifdef _DEBUG
  1422. if (multiplePaths) // i.e. no unique path to child found and multiple parent paths
  1423. AMBIGUOUS_PATH("resolveParentChild", xpath);
  1424. #endif
  1425. parent = currentPath;
  1426. }
  1427. if (multipleChildMatches)
  1428. child = NULL; // single parent, but no single child.
  1429. path.set(prop);
  1430. const char *pstart = prop;
  1431. bool wild;
  1432. readWildId(prop, wild);
  1433. size32_t s = prop-pstart;
  1434. if (wild)
  1435. throw MakeXPathException(pstart, PTreeExcpt_XPath_ParseError, s-1, "Wildcards not permitted on add");
  1436. assertex(s);
  1437. path.set(pstart, s);
  1438. qualifier.set(prop);
  1439. return;
  1440. }
  1441. }
  1442. assertex(prop && *prop);
  1443. parent = this;
  1444. const char *pstart = prop;
  1445. bool wild;
  1446. readWildId(prop, wild);
  1447. assertex(!wild);
  1448. size32_t s = prop-pstart;
  1449. if (*prop && *prop != '[')
  1450. throw MakeXPathException(pstart, PTreeExcpt_XPath_ParseError, s, "Qualifier expected e.g. [..]");
  1451. path.set(pstart, s);
  1452. if (checkChildren())
  1453. child = children->query(path);
  1454. if (child)
  1455. qualifier.set(prop);
  1456. else
  1457. qualifier.clear();
  1458. }
  1459. void PTree::addProp(const char *xpath, const char *val)
  1460. {
  1461. if (!xpath || '\0' == *xpath)
  1462. addLocal((size32_t)strlen(val)+1, val);
  1463. else if (isAttribute(xpath))
  1464. setAttribute(xpath, val);
  1465. else if ('[' == *xpath)
  1466. {
  1467. aindex_t pos = getChildMatchPos(xpath);
  1468. if ((aindex_t) -1 == pos)
  1469. throw MakeIPTException(-1, "addProp: qualifier unmatched %s", xpath);
  1470. addLocal((size32_t)strlen(val)+1, val, false, pos);
  1471. }
  1472. else
  1473. {
  1474. IPropertyTree *parent, *child;
  1475. StringAttr path, qualifier;
  1476. resolveParentChild(xpath, parent, child, path, qualifier);
  1477. if (parent != this)
  1478. parent->addProp(path, val);
  1479. else if (child)
  1480. child->addProp(qualifier, val);
  1481. else
  1482. setProp(path, val);
  1483. }
  1484. }
  1485. void PTree::appendProp(const char *xpath, const char *val)
  1486. {
  1487. if (!xpath || '\0' == *xpath)
  1488. appendLocal((size_t)strlen(val)+1, val, false);
  1489. else if (isAttribute(xpath))
  1490. {
  1491. StringBuffer newVal;
  1492. getProp(xpath, newVal);
  1493. newVal.append(val);
  1494. setAttribute(xpath, newVal.str());
  1495. }
  1496. else if ('[' == *xpath)
  1497. {
  1498. IPropertyTree *qualified = queryPropTree(xpath);
  1499. if (!qualified)
  1500. throw MakeIPTException(-1, "appendProp: qualifier unmatched %s", xpath);
  1501. qualified->appendProp(nullptr, val);
  1502. }
  1503. else
  1504. {
  1505. IPropertyTree *parent, *child;
  1506. StringAttr path, qualifier;
  1507. resolveParentChild(xpath, parent, child, path, qualifier);
  1508. if (parent != this)
  1509. parent->appendProp(path, val);
  1510. else if (child)
  1511. child->appendProp(qualifier, val);
  1512. else
  1513. setProp(path, val);
  1514. }
  1515. }
  1516. bool PTree::getPropBool(const char *xpath, bool dft) const
  1517. {
  1518. const char *val = queryProp(xpath);
  1519. if (val && *val)
  1520. return strToBool(val);
  1521. else
  1522. return dft;
  1523. }
  1524. __int64 PTree::getPropInt64(const char *xpath, __int64 dft) const
  1525. {
  1526. if (!xpath)
  1527. {
  1528. if (!value) return dft;
  1529. else
  1530. {
  1531. const char *v = (const char *)value->queryValue();
  1532. if (!v || !*v) return dft;
  1533. else return _atoi64(v);
  1534. }
  1535. }
  1536. else if (isAttribute(xpath))
  1537. {
  1538. const char *v = getAttributeValue(xpath);
  1539. if (!v || !*v) // intentional return dft if attribute equals ""
  1540. return dft;
  1541. return _atoi64(v);
  1542. }
  1543. else
  1544. {
  1545. const char *prop = splitXPathX(xpath);
  1546. if (isAttribute(prop))
  1547. {
  1548. MAKE_LSTRING(path, xpath, prop-xpath);
  1549. IPropertyTree *branch = queryPropTree(path);
  1550. if (!branch) return dft;
  1551. return branch->getPropInt64(prop, dft);
  1552. }
  1553. else
  1554. {
  1555. IPropertyTree *branch = queryPropTree(xpath);
  1556. if (!branch) return dft;
  1557. return branch->getPropInt64(NULL, dft);
  1558. }
  1559. }
  1560. }
  1561. void PTree::setPropInt64(const char * xpath, __int64 val)
  1562. {
  1563. if (!xpath || '\0' == *xpath)
  1564. {
  1565. char buf[23];
  1566. numtostr(buf, val);
  1567. setLocal((size32_t)strlen(buf)+1, buf);
  1568. }
  1569. else if (isAttribute(xpath))
  1570. {
  1571. char buf[23];
  1572. numtostr(buf, val);
  1573. setAttribute(xpath, buf);
  1574. }
  1575. else
  1576. {
  1577. const char *prop;
  1578. IPropertyTree *branch = splitBranchProp(xpath, prop, true);
  1579. if (isAttribute(prop))
  1580. branch->setPropInt64(prop, val);
  1581. else
  1582. {
  1583. IPropertyTree *propBranch = queryCreateBranch(branch, prop);
  1584. propBranch->setPropInt64(NULL, val);
  1585. }
  1586. }
  1587. }
  1588. void PTree::addPropInt64(const char *xpath, __int64 val)
  1589. {
  1590. if (!xpath || '\0' == *xpath)
  1591. {
  1592. char buf[23];
  1593. numtostr(buf,val);
  1594. addLocal((size32_t)strlen(buf)+1, buf);
  1595. }
  1596. else if (isAttribute(xpath))
  1597. {
  1598. char buf[23];
  1599. numtostr(buf, val);
  1600. setAttribute(xpath, buf);
  1601. }
  1602. else if ('[' == *xpath)
  1603. {
  1604. char buf[23];
  1605. numtostr(buf, val);
  1606. aindex_t pos = getChildMatchPos(xpath);
  1607. if ((aindex_t) -1 == pos)
  1608. throw MakeIPTException(-1, "addPropInt64: qualifier unmatched %s", xpath);
  1609. addLocal((size32_t)strlen(buf)+1, buf, false, pos);
  1610. }
  1611. else
  1612. {
  1613. IPropertyTree *parent, *child;
  1614. StringAttr path, qualifier;
  1615. resolveParentChild(xpath, parent, child, path, qualifier);
  1616. if (parent != this)
  1617. parent->addPropInt64(path, val);
  1618. else if (child)
  1619. child->addPropInt64(qualifier, val);
  1620. else
  1621. setPropInt64(path, val);
  1622. }
  1623. }
  1624. int PTree::getPropInt(const char *xpath, int dft) const
  1625. {
  1626. return (int) getPropInt64(xpath, dft); // underlying type always __int64 (now)
  1627. }
  1628. void PTree::setPropInt(const char *xpath, int val)
  1629. {
  1630. setPropInt64(xpath, val); // underlying type always __int64 (now)
  1631. }
  1632. void PTree::addPropInt(const char *xpath, int val)
  1633. {
  1634. addPropInt64(xpath, val); // underlying type always __int64 (now)
  1635. }
  1636. double PTree::getPropReal(const char *xpath, double dft) const
  1637. {
  1638. const char *val = queryProp(xpath);
  1639. return val?atof(val):dft;
  1640. }
  1641. bool PTree::isCompressed(const char *xpath) const
  1642. {
  1643. if (!xpath)
  1644. return (value && value->isCompressed());
  1645. else if (isAttribute(xpath))
  1646. return false;
  1647. else
  1648. {
  1649. const char *prop = splitXPathX(xpath);
  1650. if (!isAttribute(prop))
  1651. {
  1652. IPropertyTree *branch = queryPropTree(xpath);
  1653. if (branch)
  1654. return branch->isCompressed(nullptr);
  1655. }
  1656. }
  1657. return false;
  1658. }
  1659. bool PTree::isBinary(const char *xpath) const
  1660. {
  1661. if (!xpath)
  1662. return IptFlagTst(flags, ipt_binary);
  1663. else if (isAttribute(xpath)) // still positing that attr cannot be binary for now.
  1664. return false;
  1665. else
  1666. {
  1667. const char *prop = splitXPathX(xpath);
  1668. if (!isAttribute(prop))
  1669. {
  1670. IPropertyTree *branch = queryPropTree(xpath);
  1671. if (branch)
  1672. return branch->isBinary(nullptr);
  1673. }
  1674. }
  1675. return false;
  1676. }
  1677. bool PTree::renameTree(IPropertyTree *child, const char *newName) // really here for hook for SDS (can substationally optimize remote action)
  1678. {
  1679. if (0==strcmp(newName, child->queryName()) && NotFound!=findChild(child)) return false;
  1680. Linked<IPropertyTree> tmp = child;
  1681. if (removeTree(child))
  1682. {
  1683. addPropTree(newName, child);
  1684. tmp.getClear(); // addPropTree has taken ownership.
  1685. return true;
  1686. }
  1687. return false;
  1688. }
  1689. bool PTree::renameProp(const char *xpath, const char *newName)
  1690. {
  1691. if (!xpath || '\0' == *xpath)
  1692. throw MakeIPTException(-1, "renameProp: cannot rename self, renameProp has to rename in context of a parent");
  1693. if (strcmp(xpath,"/")==0) // rename of self allowed assuming no parent
  1694. setName(newName);
  1695. else if ('[' == *xpath)
  1696. UNIMPLEMENTED;
  1697. else if (isAttribute(xpath))
  1698. {
  1699. StringBuffer val;
  1700. if (!getProp(xpath, val))
  1701. return false;
  1702. removeProp(xpath);
  1703. addProp(newName, val.str());
  1704. }
  1705. else
  1706. {
  1707. StringBuffer path;
  1708. const char *prop = splitXPath(xpath, path);
  1709. assertex(prop);
  1710. if (path.length())
  1711. {
  1712. Owned<IPropertyTreeIterator> iter = getElements(path.str());
  1713. if (!iter->first())
  1714. return false;
  1715. IPropertyTree &branch = iter->query();
  1716. #ifdef _DEBUG
  1717. if (iter->next())
  1718. AMBIGUOUS_PATH("renameProp", xpath);
  1719. #endif
  1720. return branch.renameProp(prop, newName);
  1721. }
  1722. else
  1723. {
  1724. IPropertyTree *old = queryPropTree(xpath);
  1725. if (!old)
  1726. return false;
  1727. return renameTree(old, newName);
  1728. }
  1729. }
  1730. return true;
  1731. }
  1732. bool PTree::getPropBin(const char *xpath, MemoryBuffer &ret) const
  1733. {
  1734. CHECK_ATTRIBUTE(xpath);
  1735. if (!xpath)
  1736. {
  1737. if (!value) return true; // exists, but no value
  1738. value->getValue(ret, IptFlagTst(flags, ipt_binary));
  1739. return true;
  1740. }
  1741. else
  1742. {
  1743. const char *prop = splitXPathX(xpath);
  1744. if (isAttribute(prop))
  1745. {
  1746. MAKE_LSTRING(path, xpath, prop-xpath);
  1747. IPropertyTree *branch = queryPropTree(path);
  1748. if (!branch) return false;
  1749. return branch->getPropBin(prop, ret);
  1750. }
  1751. else
  1752. {
  1753. IPropertyTree *branch = queryPropTree(xpath);
  1754. if (!branch) return false;
  1755. return branch->getPropBin(NULL, ret);
  1756. }
  1757. }
  1758. }
  1759. void PTree::setPropBin(const char * xpath, size32_t size, const void *data)
  1760. {
  1761. CHECK_ATTRIBUTE(xpath);
  1762. if (!xpath || '\0' == *xpath)
  1763. setLocal(size, data, true);
  1764. else
  1765. {
  1766. const char *prop;
  1767. IPropertyTree *branch = splitBranchProp(xpath, prop, true);
  1768. if (isAttribute(prop))
  1769. branch->setPropBin(prop, size, data);
  1770. else
  1771. {
  1772. IPropertyTree *propBranch = queryCreateBranch(branch, prop);
  1773. propBranch->setPropBin(NULL, size, data);
  1774. }
  1775. }
  1776. }
  1777. void PTree::addPropBin(const char *xpath, size32_t size, const void *data)
  1778. {
  1779. CHECK_ATTRIBUTE(xpath);
  1780. if (!xpath || '\0' == *xpath)
  1781. addLocal(size, data, true);
  1782. else if ('[' == *xpath)
  1783. {
  1784. aindex_t pos = getChildMatchPos(xpath);
  1785. if ((aindex_t) -1 == pos)
  1786. throw MakeIPTException(-1, "addPropBin: qualifier unmatched %s", xpath);
  1787. addLocal(size, data, true, pos);
  1788. }
  1789. else
  1790. {
  1791. IPropertyTree *parent, *child;
  1792. StringAttr path, qualifier;
  1793. resolveParentChild(xpath, parent, child, path, qualifier);
  1794. if (parent != this)
  1795. parent->addPropBin(path, size, data);
  1796. else if (child)
  1797. child->addPropBin(qualifier, size, data);
  1798. else
  1799. setPropBin(path, size, data);
  1800. }
  1801. }
  1802. void PTree::appendPropBin(const char *xpath, size32_t size, const void *data)
  1803. {
  1804. CHECK_ATTRIBUTE(xpath);
  1805. if (!xpath || '\0' == *xpath)
  1806. appendLocal(size, data, true);
  1807. else if ('[' == *xpath)
  1808. {
  1809. IPropertyTree *qualified = queryPropTree(xpath);
  1810. if (!qualified)
  1811. throw MakeIPTException(-1, "appendPropBin: qualifier unmatched %s", xpath);
  1812. qualified->appendPropBin(nullptr, size, data);
  1813. }
  1814. else
  1815. {
  1816. IPropertyTree *parent, *child;
  1817. StringAttr path, qualifier;
  1818. resolveParentChild(xpath, parent, child, path, qualifier);
  1819. if (parent != this)
  1820. parent->appendPropBin(path, size, data);
  1821. else if (child)
  1822. child->appendPropBin(qualifier, size, data);
  1823. else
  1824. setPropBin(path, size, data);
  1825. }
  1826. }
  1827. IPropertyTree *PTree::getPropTree(const char *xpath) const
  1828. {
  1829. IPropertyTree *tree = queryPropTree(xpath);
  1830. return LINK(tree);
  1831. }
  1832. IPropertyTree *PTree::queryPropTree(const char *xpath) const
  1833. {
  1834. Owned<IPropertyTreeIterator> iter = getElements(xpath);
  1835. IPropertyTree *element = NULL;
  1836. if (iter->first())
  1837. {
  1838. element = &iter->query();
  1839. #ifdef _DEBUG
  1840. //The following call can double the cost of finding a match from an IPropertyTree
  1841. if (iter->next())
  1842. AMBIGUOUS_PATH("getProp",xpath);
  1843. #endif
  1844. }
  1845. return element;
  1846. }
  1847. void PTree::replaceSelf(IPropertyTree *val)
  1848. {
  1849. Owned<IAttributeIterator> aiter = getAttributes();
  1850. StringArray attrs;
  1851. ForEach (*aiter)
  1852. attrs.append(aiter->queryName());
  1853. ForEachItemIn(a, attrs)
  1854. removeProp(attrs.item(a));
  1855. ICopyArrayOf<IPropertyTree> elems;
  1856. Owned<IPropertyTreeIterator> iter = getElements("*");
  1857. ForEach(*iter)
  1858. elems.append(iter->query());
  1859. ForEachItemIn(e, elems)
  1860. removeTree(&elems.item(e));
  1861. aiter.setown(val->getAttributes());
  1862. ForEach(*aiter)
  1863. setProp(aiter->queryName(), aiter->queryValue());
  1864. iter.setown(val->getElements("*"));
  1865. ForEach(*iter)
  1866. {
  1867. IPropertyTree &node = iter->query();
  1868. node.Link();
  1869. addPropTree(node.queryName(), &node);
  1870. }
  1871. val->Release();
  1872. }
  1873. IPropertyTree *PTree::setPropTree(const char *xpath, IPropertyTree *val)
  1874. {
  1875. CHECK_ATTRIBUTE(xpath);
  1876. if (NULL == xpath)
  1877. {
  1878. replaceSelf(val);
  1879. return this;
  1880. }
  1881. else
  1882. {
  1883. StringAttr prop, qualifier;
  1884. IPropertyTree *branch, *child;
  1885. resolveParentChild(xpath, branch, child, prop, qualifier);
  1886. if (branch == this)
  1887. {
  1888. IPropertyTree *_val = ownPTree(val);
  1889. dbgassertex(QUERYINTERFACE(_val, PTree));
  1890. PTree *__val = static_cast<PTree *>(_val);
  1891. __val->setName(prop);
  1892. addingNewElement(*_val, ANE_SET);
  1893. if (!checkChildren()) createChildMap();
  1894. children->set(prop, _val);
  1895. return _val;
  1896. }
  1897. else
  1898. return branch->setPropTree(prop, val);
  1899. }
  1900. }
  1901. bool PTree::isArray(const char *xpath) const
  1902. {
  1903. if (!xpath || !*xpath) //item in an array child of parent? I don't think callers ever access array container directly
  1904. return arrayOwner && arrayOwner->isArray();
  1905. else if (isAttribute(xpath))
  1906. return false;
  1907. else
  1908. {
  1909. StringBuffer path;
  1910. const char *prop = splitXPath(xpath, path);
  1911. assertex(prop);
  1912. if (!isAttribute(prop))
  1913. {
  1914. if (path.length())
  1915. {
  1916. Owned<IPropertyTreeIterator> iter = getElements(path.str());
  1917. if (!iter->first())
  1918. return false;
  1919. IPropertyTree &branch = iter->query();
  1920. #ifdef _DEBUG
  1921. if (iter->next())
  1922. AMBIGUOUS_PATH("isArray", xpath);
  1923. #endif
  1924. return branch.isArray(prop);
  1925. }
  1926. else
  1927. {
  1928. IPropertyTree *child = children->query(xpath);
  1929. if (child)
  1930. {
  1931. PTree *tree = static_cast<PTree *>(child);
  1932. return (tree && tree->value && tree->value->isArray());
  1933. }
  1934. }
  1935. }
  1936. }
  1937. return false;
  1938. }
  1939. void PTree::addPTreeArrayItem(IPropertyTree *existing, const char *xpath, PTree *val, aindex_t pos)
  1940. {
  1941. IPropertyTree *iptval = static_cast<IPropertyTree *>(val);
  1942. PTree *tree = nullptr;
  1943. if (existing)
  1944. {
  1945. dbgassertex(QUERYINTERFACE(existing, PTree));
  1946. tree = static_cast<PTree *>(existing);
  1947. if (tree->value && tree->value->isArray())
  1948. {
  1949. val->setOwner(tree->value);
  1950. if ((aindex_t) -1 == pos)
  1951. tree->value->addElement(iptval);
  1952. else
  1953. tree->value->setElement(pos, iptval);
  1954. return;
  1955. }
  1956. }
  1957. IPTArrayValue *array = new CPTArray();
  1958. IPropertyTree *container = create(xpath, array);
  1959. val->setOwner(array);
  1960. if (existing)
  1961. {
  1962. array->addElement(LINK(existing));
  1963. assertex((aindex_t) -1 == pos || 0 == pos);
  1964. if ((aindex_t) -1 == pos)
  1965. array->addElement(iptval);
  1966. else
  1967. array->setElement(0, iptval);
  1968. tree->setOwner(array);
  1969. children->replace(xpath, container);
  1970. }
  1971. else
  1972. {
  1973. array->addElement(iptval);
  1974. children->set(xpath, container);
  1975. }
  1976. }
  1977. IPropertyTree *PTree::addPropTree(const char *xpath, IPropertyTree *val, bool alwaysUseArray)
  1978. {
  1979. if (!xpath || '\0' == *xpath)
  1980. throw MakeIPTException(PTreeExcpt_InvalidTagName, "Invalid xpath for property tree insertion specified");
  1981. else
  1982. {
  1983. CHECK_ATTRIBUTE(xpath);
  1984. const char *x = xpath;
  1985. for (;;)
  1986. {
  1987. if (!*x++)
  1988. {
  1989. IPropertyTree *_val = ownPTree(val);
  1990. dbgassertex(QUERYINTERFACE(_val, PTree));
  1991. PTree *__val = static_cast<PTree *>(_val);
  1992. /* NB: potentially param xpath is a reference to the existing name.
  1993. * So fetch new name ptr after set.
  1994. */
  1995. __val->setName(xpath);
  1996. xpath = __val->queryName();
  1997. addingNewElement(*_val, -1);
  1998. if (checkChildren())
  1999. {
  2000. IPropertyTree *child = children->query(xpath);
  2001. if (child)
  2002. {
  2003. addPTreeArrayItem(child, xpath, __val);
  2004. return _val;
  2005. }
  2006. }
  2007. else
  2008. createChildMap();
  2009. if (alwaysUseArray)
  2010. addPTreeArrayItem(nullptr, xpath, __val);
  2011. else
  2012. children->set(xpath, _val);
  2013. return _val;
  2014. }
  2015. if ('/' == *x || '[' == *x)
  2016. break;
  2017. }
  2018. IPropertyTree *parent, *child;
  2019. StringAttr path, qualifier;
  2020. resolveParentChild(xpath, parent, child, path, qualifier);
  2021. if (parent != this)
  2022. return parent->addPropTree(path, val);
  2023. else
  2024. {
  2025. aindex_t pos = (aindex_t)-1;
  2026. if (!qualifier.isEmpty())
  2027. {
  2028. pos = ((PTree *)child)->getChildMatchPos(qualifier);
  2029. if ((aindex_t) -1 == pos)
  2030. throw MakeIPTException(-1, "addPropTree: qualifier unmatched %s", xpath);
  2031. }
  2032. IPropertyTree *_val = ownPTree(val);
  2033. dbgassertex(QUERYINTERFACE(_val, PTree));
  2034. PTree *__val = static_cast<PTree *>(_val);
  2035. __val->setName(path);
  2036. addingNewElement(*_val, pos);
  2037. if (child)
  2038. {
  2039. addPTreeArrayItem(child, path, __val, pos);
  2040. }
  2041. else
  2042. {
  2043. if (!checkChildren()) createChildMap();
  2044. if (alwaysUseArray)
  2045. addPTreeArrayItem(nullptr, path, __val);
  2046. else
  2047. children->set(path, _val);
  2048. children->set(path, _val);
  2049. }
  2050. return _val;
  2051. }
  2052. }
  2053. }
  2054. IPropertyTree *PTree::addPropTree(const char *xpath, IPropertyTree *val)
  2055. {
  2056. return addPropTree(xpath, val, false);
  2057. }
  2058. IPropertyTree *PTree::addPropTreeArrayItem(const char *xpath, IPropertyTree *val)
  2059. {
  2060. return addPropTree(xpath, val, true);
  2061. }
  2062. bool PTree::removeTree(IPropertyTree *child)
  2063. {
  2064. if (child == this)
  2065. throw MakeIPTException(-1, "Cannot remove self");
  2066. if (checkChildren())
  2067. {
  2068. IPropertyTree *_child = children->query(child->queryName());
  2069. if (_child)
  2070. {
  2071. if (child == _child)
  2072. return children->removeExact(child);
  2073. else
  2074. {
  2075. IPTArrayValue *value = ((PTree *)_child)->queryValue();
  2076. if (value && value->isArray())
  2077. {
  2078. unsigned pos = value->find(child);
  2079. if (NotFound != pos)
  2080. {
  2081. removingElement(child, pos);
  2082. value->removeElement(pos);
  2083. if (0 == value->elements())
  2084. children->removeExact(_child);
  2085. return true;
  2086. }
  2087. }
  2088. }
  2089. }
  2090. }
  2091. return false;
  2092. }
  2093. bool PTree::removeProp(const char *xpath)
  2094. {
  2095. if (xpath && isAttribute(xpath))
  2096. return removeAttribute(xpath);
  2097. StringBuffer path;
  2098. const char *prop = splitXPath(xpath, path);
  2099. if (!prop)
  2100. throw MakeXPathException(xpath, PTreeExcpt_XPath_ParseError, 0, "Invalid xpath for property deletion");
  2101. if (path.length())
  2102. {
  2103. Owned<IPropertyTreeIterator> iter = getElements(path.str());
  2104. if (!iter)
  2105. return false;
  2106. bool res = false;
  2107. if (iter->first())
  2108. {
  2109. do
  2110. {
  2111. IPropertyTree *branch = &iter->query();
  2112. if (branch) {
  2113. res = branch->removeProp(prop);
  2114. if (res)
  2115. break; // deleted first may be another
  2116. }
  2117. }
  2118. while (iter->next());
  2119. }
  2120. return res;
  2121. }
  2122. else
  2123. {
  2124. if (!queryNextUnquoted(xpath, '[') && !strchr(prop, '*')) // have to work hard to locate qualified prop tree from parent.
  2125. {
  2126. if (!checkChildren()) return false;
  2127. return children->remove(prop); // NB: might be multivalued.
  2128. }
  2129. const char *xxpath = prop;
  2130. readID(xxpath, false);
  2131. const char *idEnd = xxpath;
  2132. if ('[' == *xxpath)
  2133. {
  2134. ++xxpath;
  2135. const char *digitStart = xxpath;
  2136. while (*xxpath && ']' != *xxpath && isdigit(*xxpath)) xxpath++;
  2137. assertex(*xxpath != '\0');
  2138. if (']' == *xxpath) // so it's a digit index!
  2139. {
  2140. StringAttr id(prop, idEnd-prop);
  2141. PTree *child = children?(PTree *)children->query(id):NULL;
  2142. if (child)
  2143. {
  2144. if (child->value && child->value->isArray() && child->value->elements()>1)
  2145. {
  2146. StringAttr digit(digitStart, xxpath-digitStart);
  2147. unsigned i = atoi(digit);
  2148. if (i && i <= child->value->elements())
  2149. {
  2150. removingElement(child->value->queryElement(i-1), i-1);
  2151. child->value->removeElement(i-1);
  2152. return true;
  2153. }
  2154. }
  2155. else
  2156. return children->removeExact(child);
  2157. }
  2158. return false;
  2159. }
  2160. }
  2161. // JCSMORE - This is ridiculous for qualifier have to iterate to find match ok, but then finding where that *was* gees!
  2162. Owned <IPropertyTreeIterator> iter = getElements(prop);
  2163. if (!iter->first())
  2164. return false;
  2165. IPropertyTree *match = &iter->query();
  2166. #if 0 // intentionally removes first encountered
  2167. if (iter->next())
  2168. {
  2169. AMBIGUOUS_PATH("removeProp",xpath);
  2170. }
  2171. #endif
  2172. return removeTree(match);
  2173. }
  2174. return false;
  2175. }
  2176. aindex_t PTree::queryChildIndex(IPropertyTree *child)
  2177. {
  2178. return findChild(child);
  2179. }
  2180. StringBuffer &PTree::getName(StringBuffer &ret) const
  2181. {
  2182. ret.append(queryName());
  2183. return ret;
  2184. }
  2185. typedef CopyReferenceArrayOf<AttrValue> AttrArray;
  2186. IAttributeIterator *PTree::getAttributes(bool sorted) const
  2187. {
  2188. class CAttributeIterator : implements IAttributeIterator, public CInterface
  2189. {
  2190. Linked<const PTree> parent;
  2191. AttrValue *cur = nullptr;
  2192. public:
  2193. IMPLEMENT_IINTERFACE;
  2194. CAttributeIterator(const PTree *_parent) : parent(_parent)
  2195. {
  2196. }
  2197. // IAttributeIterator impl.
  2198. virtual bool first() override
  2199. {
  2200. cur = parent->getNextAttribute(nullptr);
  2201. return cur ? true : false;
  2202. }
  2203. virtual bool next() override
  2204. {
  2205. cur = parent->getNextAttribute(cur);
  2206. return cur ? true : false;
  2207. }
  2208. virtual bool isValid() override { return cur ? true : false; }
  2209. virtual const char *queryName() const override
  2210. {
  2211. return cur->key.get();
  2212. }
  2213. virtual const char *queryValue() const override
  2214. {
  2215. return cur->value.get();
  2216. }
  2217. virtual StringBuffer &getValue(StringBuffer &out) override
  2218. {
  2219. out.append(queryValue());
  2220. return out;
  2221. }
  2222. virtual unsigned count() override { return parent->getAttributeCount(); }
  2223. };
  2224. class CSortedAttributeIterator : implements IAttributeIterator, public CInterface
  2225. {
  2226. typedef ArrayIteratorOf<AttrArray, AttrValue &> AttrIterator;
  2227. AttrArray attrs;
  2228. AttrValue *cur;
  2229. AttrIterator *iter;
  2230. Linked<const PTree> parent;
  2231. public:
  2232. IMPLEMENT_IINTERFACE;
  2233. static int compareAttrs(AttrValue * const *ll, AttrValue * const *rr)
  2234. {
  2235. return stricmp((*ll)->key.get(), (*rr)->key.get());
  2236. };
  2237. CSortedAttributeIterator(const PTree *_parent) : cur(NULL), iter(NULL), parent(_parent)
  2238. {
  2239. AttrValue *cur = parent->getNextAttribute(nullptr);
  2240. if (cur)
  2241. {
  2242. do
  2243. {
  2244. attrs.append(*cur);
  2245. cur = parent->getNextAttribute(cur);
  2246. }
  2247. while (cur);
  2248. attrs.sort(compareAttrs);
  2249. iter = new AttrIterator(attrs);
  2250. }
  2251. }
  2252. ~CSortedAttributeIterator()
  2253. {
  2254. if (iter)
  2255. delete iter;
  2256. }
  2257. // IAttributeIterator impl.
  2258. virtual bool first() override
  2259. {
  2260. if (!iter) return false;
  2261. if (!iter->first()) { cur = NULL; return false; }
  2262. cur = &iter->query();
  2263. return true;
  2264. }
  2265. virtual bool next() override
  2266. {
  2267. if (!iter) return false;
  2268. if (!iter->next()) { cur = NULL; return false; }
  2269. cur = &iter->query();
  2270. return true;
  2271. }
  2272. virtual bool isValid() override { return cur!=NULL; }
  2273. virtual const char *queryName() const override
  2274. {
  2275. assertex(cur);
  2276. return cur->key.get();
  2277. }
  2278. virtual const char *queryValue() const override
  2279. {
  2280. assertex(cur);
  2281. return cur->value.get();
  2282. }
  2283. virtual StringBuffer &getValue(StringBuffer &out) override
  2284. {
  2285. assertex(cur);
  2286. return out.append(queryValue());
  2287. }
  2288. virtual unsigned count() override { return attrs.ordinality(); }
  2289. };
  2290. if (sorted)
  2291. return new CSortedAttributeIterator(this);
  2292. else
  2293. return new CAttributeIterator(this);
  2294. }
  2295. ///////////////////
  2296. class CIndexIterator : implements IPropertyTreeIterator, public CInterface
  2297. {
  2298. Owned<IPropertyTreeIterator> subIter;
  2299. IPropertyTree *celem;
  2300. unsigned index, current;
  2301. public:
  2302. IMPLEMENT_IINTERFACE;
  2303. CIndexIterator(IPropertyTreeIterator *_subIter, unsigned _index) : subIter(_subIter), index(_index)
  2304. {
  2305. }
  2306. // IPropertyTreeIterator
  2307. virtual bool first() override
  2308. {
  2309. if (!index)
  2310. return false;
  2311. if (!subIter->first())
  2312. return false;
  2313. current = 1;
  2314. celem = NULL;
  2315. do
  2316. {
  2317. if (current == index)
  2318. {
  2319. celem = &subIter->query();
  2320. return true;
  2321. }
  2322. if (!subIter->next())
  2323. return false;
  2324. } while (++current <= index);
  2325. return false;
  2326. }
  2327. virtual bool isValid() override
  2328. {
  2329. return celem && (index >= current);
  2330. }
  2331. virtual bool next() override
  2332. {
  2333. celem = NULL;
  2334. return false;
  2335. }
  2336. virtual IPropertyTree & query() override
  2337. {
  2338. return *celem;
  2339. }
  2340. };
  2341. IPropertyTreeIterator *PTree::getElements(const char *xpath, IPTIteratorCodes flags) const
  2342. {
  2343. // NULL iterator for local value (i.e. maybe be single value or array)
  2344. if (NULL == xpath || '\0' == *xpath)
  2345. return new SingleIdIterator(*this);
  2346. Owned<IPropertyTreeIterator> iter;
  2347. const char *_xpath = xpath;
  2348. bool root=true;
  2349. restart:
  2350. switch (*xpath)
  2351. {
  2352. case '.':
  2353. root=false;
  2354. ++xpath;
  2355. if ('\0' == *xpath)
  2356. return new SingleIdIterator(*this);
  2357. else if ('/' != *xpath)
  2358. throw MakeXPathException(xpath-1, PTreeExcpt_XPath_Unsupported, 0, "\"/\" expected");
  2359. goto restart;
  2360. case '/':
  2361. ++xpath;
  2362. if ('/' == *xpath)
  2363. {
  2364. iter.setown(getElements(xpath+1));
  2365. if (checkChildren())
  2366. {
  2367. IPropertyTreeIterator *iter2 = new PTIdMatchIterator(this, "*", isnocase(), flags & iptiter_sort);
  2368. iter2 = new PTStackIterator(iter2, xpath-1);
  2369. SeriesPTIterator *series = new SeriesPTIterator();
  2370. series->addIterator(iter.getClear());
  2371. series->addIterator(iter2);
  2372. return series;
  2373. }
  2374. else
  2375. return iter.getClear();
  2376. }
  2377. else if (root)
  2378. throw MakeXPathException(xpath, PTreeExcpt_XPath_Unsupported, 0, "Root specifier \"/\" specifier is not supported");
  2379. else if ('\0' == *xpath)
  2380. return new SingleIdIterator(*this);
  2381. goto restart;
  2382. case '[':
  2383. {
  2384. ++xpath;
  2385. if (isdigit(*xpath)) {
  2386. StringAttr index;
  2387. xpath = readIndex(xpath, index);
  2388. unsigned i = atoi(index.get());
  2389. if (i)
  2390. {
  2391. if (value && value->isArray())
  2392. {
  2393. IPropertyTree *element = value->queryElement(--i);
  2394. if (element)
  2395. {
  2396. iter.setown(element->getElements(NULL));
  2397. }
  2398. }
  2399. else if (i == 1)
  2400. iter.setown(new SingleIdIterator(*this));
  2401. }
  2402. }
  2403. else
  2404. {
  2405. if (checkPattern(xpath))
  2406. iter.setown(new SingleIdIterator(*this));
  2407. }
  2408. if (']' != *xpath)
  2409. throw MakeXPathException(_xpath, PTreeExcpt_XPath_ParseError, xpath-_xpath, "Qualifier brace unclosed");
  2410. ++xpath;
  2411. break;
  2412. }
  2413. default:
  2414. {
  2415. bool wild;
  2416. const char *start = xpath;
  2417. readWildId(xpath, wild);
  2418. size32_t s = xpath-start;
  2419. if (s)
  2420. {
  2421. MAKE_LSTRING(id, start, s);
  2422. if (checkChildren())
  2423. {
  2424. IPropertyTree *child = NULL;
  2425. if (!wild)
  2426. child = children->query(id);
  2427. if ((wild || child) && '[' == *xpath) // check for local index not iterative qualifier.
  2428. {
  2429. const char *xxpath = xpath+1;
  2430. if (isdigit(*xxpath)) {
  2431. StringAttr idxstr;
  2432. xxpath = readIndex(xxpath, idxstr);
  2433. if (']' != *xxpath)
  2434. throw MakeXPathException(_xpath, PTreeExcpt_XPath_ParseError, xpath-_xpath, "Qualifier brace unclosed");
  2435. ++xxpath;
  2436. unsigned index = atoi(idxstr.get());
  2437. if (index)
  2438. {
  2439. Owned<IPropertyTreeIterator> _iter = getElements(id);
  2440. if (_iter->first())
  2441. {
  2442. do
  2443. {
  2444. if (0 == --index)
  2445. {
  2446. iter.setown(new SingleIdIterator((PTree &)_iter->query()));
  2447. break;
  2448. }
  2449. }
  2450. while (_iter->next());
  2451. }
  2452. }
  2453. xpath = xxpath;
  2454. }
  2455. else
  2456. {
  2457. const char *start = xxpath-1;
  2458. for (;;)
  2459. {
  2460. char quote = 0;
  2461. while (']' != *(++xxpath) || quote)
  2462. {
  2463. switch (*xxpath) {
  2464. case '\"':
  2465. case '\'':
  2466. {
  2467. if (quote)
  2468. {
  2469. if (*xxpath == quote)
  2470. quote = 0;
  2471. }
  2472. else
  2473. quote = *xxpath;
  2474. break;
  2475. }
  2476. case '\0':
  2477. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xxpath-start, "Qualifier brace unclosed");
  2478. }
  2479. }
  2480. ++xxpath;
  2481. if ('[' == *xxpath)
  2482. {
  2483. ++xxpath;
  2484. if (isdigit(*xxpath))
  2485. {
  2486. const char *lhsStart = start+1;
  2487. Owned<IPropertyTreeIterator> siter = checkMapIterator(lhsStart, *child);
  2488. if (!siter)
  2489. {
  2490. if (wild)
  2491. iter.setown(new PTIdMatchIterator(this, id, isnocase(), flags & iptiter_sort));
  2492. else
  2493. iter.setown(child->getElements(NULL));
  2494. StringAttr qualifier(start, (xxpath-1)-start);
  2495. siter.setown(new PTStackIterator(iter.getClear(), qualifier.get()));
  2496. }
  2497. StringAttr index;
  2498. xxpath = readIndex(xxpath, index);
  2499. unsigned i = atoi(index.get());
  2500. iter.setown(new CIndexIterator(siter.getClear(), i));
  2501. ++xxpath;
  2502. break;
  2503. }
  2504. }
  2505. else
  2506. {
  2507. if (!wild)
  2508. {
  2509. const char *lhsStart = start+1;
  2510. Owned<IPropertyTreeIterator> mapIter = checkMapIterator(lhsStart, *child);
  2511. if (mapIter)
  2512. {
  2513. xxpath = lhsStart;
  2514. iter.swap(mapIter);
  2515. break;
  2516. }
  2517. }
  2518. if (wild)
  2519. iter.setown(new PTIdMatchIterator(this, id, isnocase(), flags & iptiter_sort));
  2520. else
  2521. iter.setown(child->getElements(NULL));
  2522. StringAttr qualifier(start, xxpath-start);
  2523. iter.setown(new PTStackIterator(iter.getClear(), qualifier.get()));
  2524. break;
  2525. }
  2526. }
  2527. xpath = xxpath;
  2528. }
  2529. }
  2530. else
  2531. {
  2532. if (wild)
  2533. iter.setown(new PTIdMatchIterator(this, id, isnocase(), flags & iptiter_sort));
  2534. else if (child)
  2535. iter.setown(child->getElements(NULL));
  2536. }
  2537. }
  2538. }
  2539. break;
  2540. }
  2541. }
  2542. if (!iter)
  2543. iter.setown(LINK(nullPTreeIterator));
  2544. if (*xpath == '\0' || (*xpath == '/' && '\0' == *(xpath+1)))
  2545. return iter.getClear();
  2546. else
  2547. return new PTStackIterator(iter.getClear(), xpath);
  2548. }
  2549. void PTree::localizeElements(const char *xpath, bool allTail)
  2550. {
  2551. // null action for local ptree
  2552. }
  2553. unsigned PTree::numChildren() const
  2554. {
  2555. if (!checkChildren()) return 0;
  2556. return children->numChildren();
  2557. }
  2558. unsigned PTree::getCount(const char *xpath) const
  2559. {
  2560. unsigned c=0;
  2561. Owned<IPropertyTreeIterator> iter = getElements(xpath);
  2562. ForEach(*iter)
  2563. ++c;
  2564. return c;
  2565. }
  2566. void getXPathMatchTree(IPropertyTree &parentContext, const char *xpath, IPropertyTree *&matchContainer)
  2567. {
  2568. if (!xpath || !*xpath)
  2569. {
  2570. matchContainer = createPTree(parentContext.queryName());
  2571. return;
  2572. }
  2573. StringBuffer head;
  2574. const char *str = xpath;
  2575. const char *end = str+strlen(xpath);
  2576. bool quote = false;
  2577. bool inQualifier = false;
  2578. bool done = false;
  2579. bool recurse = false;
  2580. while (end != str)
  2581. {
  2582. switch (*str) {
  2583. case '"':
  2584. if (quote) quote = false;
  2585. else quote = true;
  2586. break;
  2587. case '[':
  2588. if (inQualifier)
  2589. {
  2590. if (!quote)
  2591. throw MakeXPathException(xpath, PTreeExcpt_XPath_ParseError, str-xpath, "Unclosed qualifier detected");
  2592. }
  2593. else
  2594. inQualifier = true;
  2595. break;
  2596. case ']':
  2597. if (inQualifier)
  2598. {
  2599. if (!quote)
  2600. inQualifier = false;
  2601. }
  2602. else if (!quote)
  2603. throw MakeXPathException(xpath, PTreeExcpt_XPath_ParseError, str-xpath, "Unopened qualifier detected");
  2604. break;
  2605. case '/':
  2606. if (!quote && !inQualifier)
  2607. {
  2608. if ('/' == *(str+1))
  2609. recurse = true;
  2610. done = true;
  2611. }
  2612. break;
  2613. }
  2614. if (done) break;
  2615. ++str;
  2616. }
  2617. const char *tail;
  2618. if (str==end) // top-level matches
  2619. {
  2620. head.append(xpath);
  2621. if (0 == head.length())
  2622. {
  2623. matchContainer = createPTree(xpath);
  2624. return;
  2625. }
  2626. tail = NULL;
  2627. }
  2628. else
  2629. {
  2630. head.append(str-xpath, xpath);
  2631. if (recurse)
  2632. tail = str+2;
  2633. else
  2634. tail = str+1;
  2635. }
  2636. Owned<IPropertyTreeIterator> parentIter = parentContext.getElements(head.str());
  2637. Owned<IPropertyTree> matchParent;
  2638. ForEach (*parentIter)
  2639. {
  2640. IPropertyTree &parent = parentIter->query();
  2641. if (!matchParent)
  2642. matchParent.setown(createPTree(parentContext.queryName()));
  2643. if (tail && *tail)
  2644. {
  2645. IPropertyTree *childContainer = NULL;
  2646. getXPathMatchTree(parent, tail, childContainer);
  2647. if (childContainer)
  2648. {
  2649. if (!head.length())
  2650. matchParent.setown(childContainer);
  2651. else
  2652. {
  2653. unsigned pos = ((PTree &)parentContext).findChild(&parent);
  2654. matchParent->addPropTree(childContainer->queryName(), childContainer);
  2655. childContainer->setPropInt("@pos", pos+1);
  2656. }
  2657. if (!matchContainer)
  2658. matchContainer = LINK(matchParent);
  2659. }
  2660. if (recurse)
  2661. {
  2662. Owned<IPropertyTreeIterator> iter = parent.getElements("*");
  2663. ForEach (*iter)
  2664. {
  2665. IPropertyTree *childContainer = NULL;
  2666. IPropertyTree &child = iter->query();
  2667. getXPathMatchTree(child, xpath, childContainer);
  2668. if (childContainer)
  2669. {
  2670. unsigned pos = ((PTree &)parent).findChild(&child);
  2671. matchParent->addPropTree(childContainer->queryName(), childContainer);
  2672. childContainer->setPropInt("@pos", pos+1);
  2673. if (!matchContainer)
  2674. matchContainer = LINK(matchParent);
  2675. }
  2676. }
  2677. }
  2678. }
  2679. else
  2680. {
  2681. if (&parent != &parentContext)
  2682. {
  2683. IPropertyTree *childContainer = matchParent->addPropTree(parent.queryName(), createPTree());
  2684. unsigned pos = ((PTree &)parentContext).findChild(&parent);
  2685. childContainer->setPropInt("@pos", pos+1);
  2686. }
  2687. if (!matchContainer)
  2688. matchContainer = LINK(matchParent);
  2689. }
  2690. }
  2691. }
  2692. IPropertyTree *getXPathMatchTree(IPropertyTree &parent, const char *xpath)
  2693. {
  2694. IPropertyTree *matchTree = NULL;
  2695. getXPathMatchTree(parent, xpath, matchTree);
  2696. return matchTree;
  2697. }
  2698. void PTree::serializeAttributes(MemoryBuffer &tgt)
  2699. {
  2700. IAttributeIterator *aIter = getAttributes();
  2701. if (aIter->first())
  2702. {
  2703. do
  2704. {
  2705. tgt.append(aIter->queryName());
  2706. tgt.append(aIter->queryValue());
  2707. }
  2708. while (aIter->next());
  2709. }
  2710. tgt.append(""); // attribute terminator. i.e. blank attr name.
  2711. aIter->Release();
  2712. }
  2713. void PTree::serializeSelf(MemoryBuffer &tgt)
  2714. {
  2715. const char *_name = queryName();
  2716. tgt.append(_name ? _name : "");
  2717. tgt.append(flags);
  2718. serializeAttributes(tgt);
  2719. if (value)
  2720. value->serialize(tgt);
  2721. else
  2722. tgt.append((size32_t)0);
  2723. }
  2724. void PTree::serializeCutOff(MemoryBuffer &tgt, int cutoff, int depth)
  2725. {
  2726. serializeSelf(tgt);
  2727. if (-1 == cutoff || depth<cutoff)
  2728. {
  2729. Owned<IPropertyTreeIterator> iter = getElements("*");
  2730. if (iter->first())
  2731. {
  2732. do
  2733. {
  2734. IPropertyTree *_child = &iter->query();
  2735. PTree *child = QUERYINTERFACE(_child, PTree); assertex(child);
  2736. child->serializeCutOff(tgt, cutoff, depth+1);
  2737. }
  2738. while (iter->next());
  2739. }
  2740. }
  2741. tgt.append(""); // element terminator. i.e. blank child name.
  2742. }
  2743. // serializable impl.
  2744. void PTree::serialize(MemoryBuffer &tgt)
  2745. {
  2746. serializeCutOff(tgt, -1, 0);
  2747. }
  2748. void PTree::deserialize(MemoryBuffer &src)
  2749. {
  2750. deserializeSelf(src);
  2751. StringAttr eName;
  2752. for (;;)
  2753. {
  2754. size32_t pos = src.getPos();
  2755. src.read(eName);
  2756. if (eName.isEmpty())
  2757. break;
  2758. src.reset(pos); // reset to re-read tree name
  2759. IPropertyTree *child = create(src);
  2760. addPropTree(eName, child);
  2761. }
  2762. }
  2763. void PTree::deserializeSelf(MemoryBuffer &src)
  2764. {
  2765. setName(NULL); // needs to be cleared before flags changed
  2766. StringAttr _name;
  2767. src.read(_name);
  2768. src.read(flags);
  2769. if (_name[0]==0)
  2770. setName(NULL);
  2771. else
  2772. setName(_name);
  2773. StringAttr attrName, attrValue;
  2774. for (;;)
  2775. {
  2776. src.read(attrName);
  2777. if (attrName.isEmpty())
  2778. break;
  2779. src.read(attrValue);
  2780. setProp(attrName, attrValue);
  2781. }
  2782. size32_t size;
  2783. unsigned pos = src.getPos();
  2784. src.read(size);
  2785. if (value) delete value;
  2786. if (size)
  2787. {
  2788. src.reset(pos);
  2789. value = new CPTValue(src);
  2790. }
  2791. else value = NULL;
  2792. }
  2793. IPropertyTree *PTree::clone(IPropertyTree &srcTree, bool self, bool sub)
  2794. {
  2795. IPropertyTree *_dstTree = self ? this : create(srcTree.queryName());
  2796. PTree *dstTree = QUERYINTERFACE(_dstTree, PTree);
  2797. dbgassertex(dstTree);
  2798. if (self)
  2799. dstTree->setName(srcTree.queryName());
  2800. clone(srcTree, *dstTree, sub);
  2801. return _dstTree;
  2802. }
  2803. void PTree::clone(IPropertyTree &srcTree, IPropertyTree &dstTree, bool sub)
  2804. {
  2805. PTree *_dstTree = QUERYINTERFACE((&dstTree), PTree); assertex(_dstTree); //JCSMORE
  2806. flags = _dstTree->flags;
  2807. if (srcTree.isBinary(NULL))
  2808. {
  2809. MemoryBuffer mb;
  2810. verifyex(srcTree.getPropBin(NULL, mb));
  2811. dstTree.setPropBin(NULL, mb.length(), mb.toByteArray());
  2812. }
  2813. else if (srcTree.isCompressed(NULL))
  2814. {
  2815. StringBuffer s;
  2816. verifyex(srcTree.getProp(NULL, s));
  2817. dstTree.setProp(NULL, s.str());
  2818. }
  2819. else
  2820. dstTree.setProp(NULL, srcTree.queryProp(NULL));
  2821. IAttributeIterator *attrs = srcTree.getAttributes();
  2822. if (attrs->first())
  2823. {
  2824. do
  2825. {
  2826. dstTree.setProp(attrs->queryName(), attrs->queryValue());
  2827. }
  2828. while (attrs->next());
  2829. }
  2830. attrs->Release();
  2831. if (sub)
  2832. {
  2833. Owned<IPropertyTreeIterator> iter = srcTree.getElements("*");
  2834. if (iter->first())
  2835. {
  2836. do
  2837. {
  2838. IPropertyTree &child = iter->query();
  2839. IPropertyTree *newChild = clone(child, false, sub);
  2840. dstTree.addPropTree(newChild->queryName(), newChild);
  2841. }
  2842. while (iter->next());
  2843. }
  2844. }
  2845. }
  2846. IPropertyTree *PTree::ownPTree(IPropertyTree *tree)
  2847. {
  2848. if (!isEquivalent(tree) || tree->IsShared() || isCaseInsensitive() != tree->isCaseInsensitive())
  2849. {
  2850. IPropertyTree *newTree = clone(*tree);
  2851. tree->Release();
  2852. return newTree;
  2853. }
  2854. else
  2855. return tree;
  2856. }
  2857. IPropertyTree *PTree::queryCreateBranch(IPropertyTree *branch, const char *prop, bool *newBranch)
  2858. {
  2859. IPropertyTree *childBranch = branch->queryPropTree(prop);
  2860. if (!childBranch)
  2861. {
  2862. if (newBranch) *newBranch = true;
  2863. childBranch = create(prop);
  2864. branch->setPropTree(prop, childBranch);
  2865. }
  2866. else if (newBranch) *newBranch = false;
  2867. return childBranch;
  2868. }
  2869. IPropertyTree *PTree::splitBranchProp(const char *xpath, const char *&prop, bool error)
  2870. {
  2871. prop = splitXPathX(xpath);
  2872. MAKE_LSTRING(path, xpath, prop-xpath);
  2873. IPropertyTree *branch = queryPropTree(path);
  2874. if (!branch && error)
  2875. throw MakeIPTException(-1, "path %s not found, when setting prop %s", path, xpath);
  2876. return branch;
  2877. }
  2878. IPropertyTree *_createPropBranch(IPropertyTree *tree, const char *xpath, bool createIntermediates, IPropertyTree *&created, IPropertyTree *&createdParent)
  2879. {
  2880. const char *prop;
  2881. StringBuffer path;
  2882. prop = splitXPathUQ(xpath, path);
  2883. IPropertyTree *branch = tree->queryPropTree(path.str());
  2884. if (!branch)
  2885. {
  2886. if (path.length() == strlen(xpath))
  2887. throw MakeIPTException(-1, "createPropBranch: cannot create path : %s", xpath);
  2888. if (!createIntermediates)
  2889. throw MakeIPTException(-1, "createPropBranch: no path found for : %s", path.str());
  2890. if ('/' == path.charAt(path.length()-1))
  2891. path.remove(path.length()-1, 1);
  2892. branch = _createPropBranch(tree, path.str(), createIntermediates, created, createdParent);
  2893. assertex(branch);
  2894. }
  2895. if (prop && '\0' != *prop && '@' != *prop)
  2896. {
  2897. IPropertyTree *_branch = branch->queryPropTree(prop);
  2898. if (_branch)
  2899. branch = _branch;
  2900. else
  2901. {
  2902. IPropertyTree *p = branch;
  2903. branch = branch->addPropTree(prop, createPTree());
  2904. if (!created) { created = branch; createdParent = p; }
  2905. }
  2906. }
  2907. return branch;
  2908. }
  2909. IPropertyTree *createPropBranch(IPropertyTree *tree, const char *xpath, bool createIntermediates, IPropertyTree **created, IPropertyTree **createdParent)
  2910. {
  2911. IPropertyTree *_created = NULL, *_createdParent = NULL;
  2912. try
  2913. {
  2914. IPropertyTree *ret = _createPropBranch(tree, xpath, createIntermediates, _created, _createdParent);
  2915. if (created) *created = _created;
  2916. if (createdParent) *createdParent = _createdParent;
  2917. return ret;
  2918. }
  2919. catch (...)
  2920. {
  2921. if (_created) (_createdParent)->removeTree(_created);
  2922. throw;
  2923. }
  2924. }
  2925. void PTree::addLocal(size32_t l, const void *data, bool _binary, int pos)
  2926. {
  2927. if (!l) return; // right thing to do on addProp("x", NULL) ?
  2928. IPTArrayValue *newValue = new CPTValue(l, data, _binary);
  2929. Owned<IPropertyTree> tree = create(queryName(), newValue);
  2930. PTree *_tree = QUERYINTERFACE(tree.get(), PTree); assertex(_tree);
  2931. if (_binary)
  2932. IptFlagSet(_tree->flags, ipt_binary);
  2933. else
  2934. IptFlagClr(_tree->flags, ipt_binary);
  2935. addingNewElement(*tree, pos);
  2936. IPTArrayValue *array;
  2937. if (value && value->isArray())
  2938. {
  2939. array = value;
  2940. if (pos != -1 && ((unsigned)pos > array->elements()))
  2941. throw MakeIPTException(-1, "Error trying to insert element at %d of %d", pos, array->elements());
  2942. }
  2943. else
  2944. {
  2945. if (pos > 0)
  2946. throw MakeIPTException(-1, "Error trying to insert element at %d of 0", pos);
  2947. // detach children and attributes of this branch now owned by element of newly created array.
  2948. IPropertyTree *element1 = detach();
  2949. array = new CPTArray();
  2950. addingNewElement(*element1, ANE_APPEND);
  2951. static_cast<PTree *>(element1)->setOwner(array);
  2952. array->addElement(element1);
  2953. value = array;
  2954. }
  2955. _tree->setOwner(array);
  2956. tree->Link();
  2957. if (-1 == pos)
  2958. array->addElement(tree);
  2959. else
  2960. array->setElement(pos, tree);
  2961. }
  2962. enum exprType { t_none, t_equality, t_inequality, t_lteq, t_lt, t_gt, t_gteq } tType;
  2963. inline bool match(bool wild, bool numeric, const char *xpath, exprType t, const char *value, unsigned len, const char *pat, unsigned patLen, bool nocase)
  2964. {
  2965. int m;
  2966. if (numeric)
  2967. {
  2968. __int64 lhsN = atoi64_l(value, len);
  2969. __int64 rhsN = atoi64_l(pat, patLen);
  2970. m = lhsN<rhsN?-1:lhsN>rhsN?1:0;
  2971. }
  2972. else if (wild)
  2973. m = false==WildMatch(value, len, pat, patLen, nocase);
  2974. else
  2975. {
  2976. if (len == patLen)
  2977. m = nocase ? memicmp(value, pat, len) : memcmp(value, pat, len);
  2978. else if (len < patLen)
  2979. m = -1;
  2980. else
  2981. m = 1;
  2982. }
  2983. switch (t)
  2984. {
  2985. case t_inequality:
  2986. return m!=0;
  2987. case t_lt:
  2988. return m<0;
  2989. case t_lteq:
  2990. return m<=0;
  2991. case t_equality:
  2992. return m==0;
  2993. case t_gteq:
  2994. return m>=0;
  2995. case t_gt:
  2996. return m>0;
  2997. }
  2998. throw MakeXPathException(xpath, PTreeExcpt_XPath_ParseError, 0, "Invalid xpath qualifier expression in xpath: %s", xpath);
  2999. }
  3000. bool PTree::checkPattern(const char *&xxpath) const
  3001. {
  3002. // Pattern is an additional filter at the current node level
  3003. // It can be [condition], or it can be empty (we don't support anything else)
  3004. // supported conditions are:
  3005. // tag - must have child called tag
  3006. // @attr - must have attribute called attr
  3007. // tag="value" - must have child called tag with given value
  3008. // @attr="value" - must have attribute called attr with given value
  3009. const char *xpath = xxpath;
  3010. while (*xpath == ' ' || *xpath == '\t') xpath++;
  3011. const char *start = xpath;
  3012. bool wild = false, nocase = isnocase();
  3013. if (*xpath=='@')
  3014. xpath++;
  3015. char quote = 0;
  3016. const char *lhsEnd, *quoteBegin, *quoteEnd, *rhsBegin, *rhsEnd;
  3017. lhsEnd = quoteBegin = quoteEnd = rhsBegin = rhsEnd = NULL;
  3018. exprType tType = t_none;
  3019. bool numeric=false;
  3020. #ifdef WARNLEGACYCOMPARE
  3021. bool legacynumeric=false;
  3022. #endif
  3023. for (;;)
  3024. {
  3025. switch (*xpath) {
  3026. case '"':
  3027. case '\'':
  3028. if (quote)
  3029. {
  3030. if (*xpath == quote)
  3031. {
  3032. quote = 0;
  3033. quoteEnd = xpath;
  3034. }
  3035. }
  3036. else
  3037. {
  3038. if (quoteBegin)
  3039. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Quoted left hand side already seen");
  3040. quote = *xpath;
  3041. quoteBegin = xpath+1;
  3042. }
  3043. break;
  3044. case '[':
  3045. if (!quote)
  3046. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Unclosed qualifier detected");
  3047. break;
  3048. case ']':
  3049. if (!quote)
  3050. {
  3051. if (!lhsEnd)
  3052. lhsEnd = xpath;
  3053. rhsEnd = xpath;
  3054. }
  3055. break;
  3056. case ' ':
  3057. case '\t':
  3058. if (!lhsEnd)
  3059. lhsEnd = xpath;
  3060. break;
  3061. case '!':
  3062. if (!quote)
  3063. {
  3064. if (tType)
  3065. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Unexpected expression operator xpath");
  3066. if ('=' != *(xpath+1))
  3067. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Invalid xpath qualifier expression in xpath");
  3068. if (!lhsEnd)
  3069. lhsEnd = xpath;
  3070. ++xpath;
  3071. tType = t_inequality;
  3072. wild = true; // true by default now, introduced ~ syntax, to denote wild string
  3073. }
  3074. break;
  3075. case '=':
  3076. if (!quote)
  3077. {
  3078. if (wild)
  3079. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Wildcard match '~' makes no sense in this context");
  3080. if (tType)
  3081. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Unexpected expression operator xpath");
  3082. tType = t_equality;
  3083. wild = true; // true by default now, introduced ~ syntax, to denote wild string
  3084. if (!lhsEnd)
  3085. lhsEnd = xpath;
  3086. }
  3087. break;
  3088. case '>':
  3089. if (!quote)
  3090. {
  3091. if (wild)
  3092. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Wildcard match '~' makes no sense in this context");
  3093. if (tType)
  3094. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Unexpected expression operator in xpath");
  3095. if (!lhsEnd)
  3096. lhsEnd = xpath;
  3097. #ifdef WARNLEGACYCOMPARE
  3098. legacynumeric = true;
  3099. #endif
  3100. if ('=' == *(xpath+1))
  3101. {
  3102. ++xpath;
  3103. tType = t_gteq;
  3104. }
  3105. else
  3106. tType = t_gt;
  3107. }
  3108. break;
  3109. case '<':
  3110. if (!quote)
  3111. {
  3112. if (tType)
  3113. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Unexpected expression operator in xpath");
  3114. if (!lhsEnd)
  3115. lhsEnd = xpath;
  3116. #ifdef WARNLEGACYCOMPARE
  3117. legacynumeric = true;
  3118. #endif
  3119. if ('=' == *(xpath+1))
  3120. {
  3121. ++xpath;
  3122. tType = t_lteq;
  3123. }
  3124. else
  3125. tType = t_lt;
  3126. }
  3127. break;
  3128. case '~':
  3129. if (!quote)
  3130. {
  3131. if (!tType)
  3132. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Unexpected wild operator in xpath");
  3133. wild = true;
  3134. }
  3135. break;
  3136. case '?':
  3137. if (!quote)
  3138. {
  3139. if (!tType)
  3140. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Unexpected case-insensitive operator in xpath");
  3141. nocase = true;
  3142. }
  3143. break;
  3144. case '\0':
  3145. rhsEnd = xpath;
  3146. break;
  3147. }
  3148. if (rhsEnd)
  3149. break;
  3150. xpath++;
  3151. if (!rhsBegin && tType && !isspace(*xpath))
  3152. rhsBegin = xpath;
  3153. }
  3154. if (quote)
  3155. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Parse error, unclosed quoted content");
  3156. if (tType)
  3157. {
  3158. if (quoteBegin && !quoteEnd)
  3159. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Parse error, RHS missing closing quote");
  3160. if (rhsBegin && !rhsEnd)
  3161. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Parse error, RHS missing closing quote");
  3162. if (!quoteBegin && rhsEnd) // validate it's a numeric
  3163. {
  3164. const char *c = rhsBegin;
  3165. for (;;)
  3166. {
  3167. if (!isdigit(*c++))
  3168. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Parse error, RHS is an unquoted string");
  3169. if (c==rhsEnd) break;
  3170. }
  3171. }
  3172. }
  3173. MAKE_LSTRING(lhs, start, lhsEnd-start);
  3174. bool ret = false;
  3175. const char *tProp = splitXPathX(lhs);
  3176. MAKE_LSTRING(head, lhs, tProp-lhs);
  3177. Owned<IPropertyTreeIterator> iter = getElements(head);
  3178. ForEach (*iter)
  3179. {
  3180. IPropertyTree &found = iter->query();
  3181. if (t_none == tType)
  3182. {
  3183. if (found.hasProp(tProp))
  3184. {
  3185. ret = true;
  3186. break;
  3187. }
  3188. }
  3189. else
  3190. {
  3191. Owned<IPropertyTreeIterator> _iter2;
  3192. IPropertyTreeIterator *iter2;
  3193. IPropertyTree *matchElem;
  3194. if (isAttribute(tProp))
  3195. {
  3196. matchElem = &found;
  3197. iter2 = NULL;
  3198. }
  3199. else
  3200. {
  3201. _iter2.setown(found.getElements(tProp));
  3202. iter2 = _iter2;
  3203. if (iter2->first())
  3204. matchElem = &iter2->query();
  3205. else
  3206. continue;
  3207. tProp = NULL;
  3208. }
  3209. for (;;)
  3210. {
  3211. if (matchElem->isBinary(tProp))
  3212. UNIMPLEMENTED;
  3213. const char *rhs;
  3214. unsigned rhslength;
  3215. if (quoteEnd)
  3216. {
  3217. rhs = quoteBegin;
  3218. rhslength = quoteEnd-quoteBegin;
  3219. #ifdef WARNLEGACYCOMPARE
  3220. if (legacynumeric)
  3221. {
  3222. if (isdigit(*rhs))
  3223. IWARNLOG("Possible deprecated use of quoted numeric comparison operation: %s", xxpath);
  3224. }
  3225. #endif
  3226. }
  3227. else if (rhsEnd)
  3228. {
  3229. rhs = rhsBegin;
  3230. rhslength = rhsEnd-rhsBegin;
  3231. numeric = true;
  3232. }
  3233. else
  3234. {
  3235. rhs = NULL;
  3236. rhslength = 0;
  3237. }
  3238. if (matchElem->isCompressed(tProp))
  3239. {
  3240. StringBuffer s;
  3241. matchElem->getProp(tProp, s);
  3242. ret = match(wild, numeric, xxpath, tType, s.str(), s.length(), rhs, rhslength, nocase);
  3243. }
  3244. else
  3245. {
  3246. const char *value = matchElem->queryProp(tProp);
  3247. if (value)
  3248. ret = match(wild, numeric, xxpath, tType, value, value?(size32_t)strlen(value):0, rhs, rhslength, nocase);
  3249. else if (tType == t_equality)
  3250. ret = (NULL == rhs || '\0' == *rhs);
  3251. else if (tType == t_inequality)
  3252. ret = (NULL != rhs && '\0' != *rhs);
  3253. }
  3254. if (ret)
  3255. break;
  3256. if (!iter2 || !iter2->next())
  3257. break;
  3258. matchElem = &iter2->query();
  3259. }
  3260. if (ret)
  3261. break;
  3262. }
  3263. }
  3264. xxpath = xpath;
  3265. return ret;
  3266. }
  3267. AttrValue *PTree::findAttribute(const char *key) const
  3268. {
  3269. if (attrs)
  3270. {
  3271. AttrValue *a = attrs+numAttrs;
  3272. if (isnocase())
  3273. {
  3274. while (a-- != attrs)
  3275. {
  3276. if (strieq(a->key.get(), key))
  3277. return a;
  3278. }
  3279. }
  3280. else
  3281. {
  3282. while (a-- != attrs)
  3283. {
  3284. if (streq(a->key.get(), key))
  3285. return a;
  3286. }
  3287. }
  3288. }
  3289. return nullptr;
  3290. }
  3291. const char *PTree::getAttributeValue(const char *key) const
  3292. {
  3293. AttrValue *e = findAttribute(key);
  3294. if (e)
  3295. return e->value.get();
  3296. return nullptr;
  3297. }
  3298. unsigned PTree::getAttributeCount() const
  3299. {
  3300. return numAttrs;
  3301. }
  3302. AttrValue *PTree::getNextAttribute(AttrValue *cur) const
  3303. {
  3304. if (0 == numAttrs)
  3305. return nullptr;
  3306. else if (nullptr == cur)
  3307. return attrs;
  3308. else
  3309. {
  3310. if (cur == (attrs+(numAttrs-1)))
  3311. return nullptr;
  3312. return ++cur;
  3313. }
  3314. }
  3315. //////////////////////
  3316. // LocalPTree
  3317. static RelaxedAtomic<unsigned> numLocalTrees;
  3318. unsigned queryNumLocalTrees()
  3319. {
  3320. return numLocalTrees;
  3321. }
  3322. LocalPTree::LocalPTree(const char *_name, byte _flags, IPTArrayValue *_value, ChildMap *_children) : PTree(_flags|ipt_fast, _value, _children)
  3323. {
  3324. if (_name)
  3325. setName(_name);
  3326. numLocalTrees++;
  3327. }
  3328. LocalPTree::~LocalPTree()
  3329. {
  3330. numLocalTrees--;
  3331. name.destroy();
  3332. if (!attrs)
  3333. return;
  3334. AttrValue *a = attrs+numAttrs;
  3335. while (a--!=attrs)
  3336. {
  3337. a->key.destroy();
  3338. a->value.destroy();
  3339. }
  3340. free(attrs);
  3341. }
  3342. const char *LocalPTree::queryName() const
  3343. {
  3344. return name.get();
  3345. }
  3346. void LocalPTree::setName(const char *_name)
  3347. {
  3348. if (_name==name.get())
  3349. return;
  3350. AttrStr *oname = name.getPtr(); // Don't free until after we copy - they could overlap
  3351. if (!name.set(_name))
  3352. name.setPtr(AttrStr::create(_name));
  3353. if (oname)
  3354. AttrStr::destroy(oname);
  3355. }
  3356. bool LocalPTree::removeAttribute(const char *key)
  3357. {
  3358. AttrValue *del = findAttribute(key);
  3359. if (!del)
  3360. return false;
  3361. if (arrayOwner)
  3362. {
  3363. CQualifierMap *map = arrayOwner->queryMap();
  3364. if (map)
  3365. map->removeEntryIfMapped(key, del->value.get(), this);
  3366. }
  3367. numAttrs--;
  3368. unsigned pos = del-attrs;
  3369. del->key.destroy();
  3370. del->value.destroy();
  3371. memmove(attrs+pos, attrs+pos+1, (numAttrs-pos)*sizeof(AttrValue));
  3372. return true;
  3373. }
  3374. void LocalPTree::setAttribute(const char *key, const char *val)
  3375. {
  3376. if (!key)
  3377. return;
  3378. if (!validateXMLTag(key+1))
  3379. throw MakeIPTException(-1, "Invalid xml attribute: %s", key);
  3380. if (!val)
  3381. val = ""; // cannot have NULL value
  3382. AttrValue *v = findAttribute(key);
  3383. AttrStr *goer = nullptr;
  3384. if (v)
  3385. {
  3386. if (streq(v->value.get(), val))
  3387. return;
  3388. goer = v->value.getPtr();
  3389. }
  3390. else
  3391. {
  3392. attrs = (AttrValue *)realloc(attrs, (numAttrs+1)*sizeof(AttrValue));
  3393. v = new(&attrs[numAttrs++]) AttrValue; // Initialize new AttrValue
  3394. if (!v->key.set(key))
  3395. v->key.setPtr(isnocase() ? AttrStr::createNC(key) : AttrStr::create(key));
  3396. }
  3397. if (arrayOwner)
  3398. {
  3399. CQualifierMap *map = arrayOwner->queryMap();
  3400. if (map)
  3401. {
  3402. if (goer)
  3403. map->replaceEntryIfMapped(key, v->value.get(), val, this);
  3404. else
  3405. map->insertEntryIfMapped(key, val, this);
  3406. }
  3407. }
  3408. if (!v->value.set(val))
  3409. v->value.setPtr(AttrStr::create(val));
  3410. if (goer)
  3411. AttrStr::destroy(goer);
  3412. }
  3413. #ifdef TRACE_STRING_SIZE
  3414. std::atomic<__int64> AttrStr::totsize { 0 };
  3415. std::atomic<__int64> AttrStr::maxsize { 0 };
  3416. #endif
  3417. #ifdef TRACE_ATOM_SIZE
  3418. std::atomic<__int64> AttrStrAtom::totsize { 0 };
  3419. std::atomic<__int64> AttrStrAtom::maxsize { 0 };
  3420. #endif
  3421. ///////////////////
  3422. static RelaxedAtomic<unsigned> numAtomTrees;
  3423. unsigned queryNumAtomTrees()
  3424. {
  3425. return numAtomTrees;
  3426. }
  3427. CAtomPTree::CAtomPTree(const char *_name, byte _flags, IPTArrayValue *_value, ChildMap *_children) : PTree(_flags|ipt_lowmem, _value, _children)
  3428. {
  3429. numAtomTrees++;
  3430. if (_name)
  3431. setName(_name);
  3432. }
  3433. CAtomPTree::~CAtomPTree()
  3434. {
  3435. numAtomTrees--;
  3436. bool nc = isnocase();
  3437. HashKeyElement *name_ptr = name.getPtr();
  3438. if (name_ptr)
  3439. {
  3440. AtomRefTable *kT = nc?keyTableNC:keyTable;
  3441. #ifdef TRACE_ATOM_SIZE
  3442. size_t gosize = sizeof(HashKeyElement)+strlen(name_ptr->get())+1;
  3443. if (kT->releaseKey(name_ptr))
  3444. AttrStrAtom::totsize -= gosize;
  3445. #else
  3446. kT->releaseKey(name_ptr);
  3447. #endif
  3448. }
  3449. if (!attrs)
  3450. return;
  3451. AttrValue *a = attrs+numAttrs;
  3452. {
  3453. CriticalBlock block(hashcrit);
  3454. while (a--!=attrs)
  3455. {
  3456. if (a->key.isPtr())
  3457. attrHT->removekey(a->key.getPtr(), nc);
  3458. if (a->value.isPtr())
  3459. attrHT->removeval(a->value.getPtr());
  3460. }
  3461. freeAttrArray(attrs, numAttrs);
  3462. }
  3463. }
  3464. void CAtomPTree::setName(const char *_name)
  3465. {
  3466. AtomRefTable *kT = isnocase()?keyTableNC:keyTable;
  3467. HashKeyElement *oname = name.getPtr(); // NOTE - don't release yet as could overlap source name
  3468. if (!_name)
  3469. name.setPtr(nullptr);
  3470. else
  3471. {
  3472. if (!validateXMLTag(_name))
  3473. throw MakeIPTException(PTreeExcpt_InvalidTagName, ": %s", _name);
  3474. if (!name.set(_name))
  3475. {
  3476. #ifdef TRACE_ALL_ATOM
  3477. DBGLOG("TRACE_ALL_ATOM: %s", _name);
  3478. #endif
  3479. #ifdef TRACE_ATOM_SIZE
  3480. bool didCreate;
  3481. name.setPtr(kT->queryCreate(_name, didCreate));
  3482. if (didCreate)
  3483. {
  3484. AttrStrAtom::totsize += sizeof(HashKeyElement)+strlen(_name)+1;
  3485. if (AttrStrAtom::totsize > AttrStrAtom::maxsize)
  3486. {
  3487. AttrStrAtom::maxsize.store(AttrStrAtom::totsize);
  3488. DBGLOG("TRACE_ATOM_SIZE: total size now %" I64F "d", AttrStrAtom::maxsize.load());
  3489. }
  3490. }
  3491. #else
  3492. name.setPtr(kT->queryCreate(_name));
  3493. #endif
  3494. }
  3495. }
  3496. if (oname)
  3497. {
  3498. #ifdef TRACE_ATOM_SIZE
  3499. size_t gosize = sizeof(HashKeyElement)+strlen(oname->get())+1;
  3500. if (kT->releaseKey(oname))
  3501. AttrStrAtom::totsize -= gosize;
  3502. #else
  3503. kT->releaseKey(oname);
  3504. #endif
  3505. }
  3506. }
  3507. const char *CAtomPTree::queryName() const
  3508. {
  3509. return name.get();
  3510. }
  3511. unsigned CAtomPTree::queryHash() const
  3512. {
  3513. if (name.isPtr())
  3514. {
  3515. assert(name.getPtr());
  3516. return name.getPtr()->queryHash();
  3517. }
  3518. else
  3519. {
  3520. const char *_name = name.get();
  3521. size32_t nl = strlen(_name);
  3522. return isnocase() ? hashnc((const byte *) _name, nl, 0): hashc((const byte *) _name, nl, 0);
  3523. }
  3524. }
  3525. AttrValue *CAtomPTree::newAttrArray(unsigned n)
  3526. {
  3527. // NB crit must be locked
  3528. if (!n)
  3529. return nullptr;
  3530. if (freelistmax<=n)
  3531. {
  3532. freelist = (AttrValue **)realloc(freelist, sizeof(AttrValue *)*(n+1));
  3533. while (freelistmax<=n)
  3534. freelist[freelistmax++] = nullptr;
  3535. }
  3536. AttrValue *&p = freelist[n];
  3537. AttrValue *ret = p;
  3538. if (ret)
  3539. p = *(AttrValue **)ret;
  3540. else
  3541. ret = (AttrValue *)freeallocator.alloc(sizeof(AttrValue)*n);
  3542. return ret;
  3543. }
  3544. void CAtomPTree::freeAttrArray(AttrValue *a, unsigned n)
  3545. {
  3546. // NB crit must be locked
  3547. if (a)
  3548. {
  3549. AttrValue *&p = freelist[n];
  3550. *(AttrValue **)a = p;
  3551. p = a;
  3552. }
  3553. }
  3554. void CAtomPTree::setAttribute(const char *key, const char *val)
  3555. {
  3556. if (!key)
  3557. return;
  3558. if (!validateXMLTag(key+1))
  3559. throw MakeIPTException(-1, "Invalid xml attribute: %s", key);
  3560. if (!val)
  3561. val = ""; // cannot have NULL value
  3562. AttrValue *v = findAttribute(key);
  3563. if (v)
  3564. {
  3565. if (streq(v->value.get(), val))
  3566. return;
  3567. if (arrayOwner)
  3568. {
  3569. CQualifierMap *map = arrayOwner->queryMap();
  3570. if (map)
  3571. map->replaceEntryIfMapped(key, v->value.get(), val, this);
  3572. }
  3573. AttrStr * goer = v->value.getPtr();
  3574. if (!v->value.set(val))
  3575. {
  3576. CriticalBlock block(hashcrit);
  3577. if (goer)
  3578. attrHT->removeval(goer);
  3579. v->value.setPtr(attrHT->addval(val));
  3580. }
  3581. else if (goer)
  3582. {
  3583. CriticalBlock block(hashcrit);
  3584. attrHT->removeval(goer);
  3585. }
  3586. }
  3587. else
  3588. {
  3589. CriticalBlock block(hashcrit);
  3590. AttrValue *newattrs = newAttrArray(numAttrs+1);
  3591. if (attrs)
  3592. {
  3593. memcpy(newattrs, attrs, numAttrs*sizeof(AttrValue));
  3594. freeAttrArray(attrs, numAttrs);
  3595. }
  3596. if (arrayOwner)
  3597. {
  3598. CQualifierMap *map = arrayOwner->queryMap();
  3599. if (map)
  3600. map->insertEntryIfMapped(key, val, this);
  3601. }
  3602. v = &newattrs[numAttrs];
  3603. if (!v->key.set(key))
  3604. v->key.setPtr(attrHT->addkey(key, isnocase()));
  3605. if (!v->value.set(val))
  3606. v->value.setPtr(attrHT->addval(val));
  3607. numAttrs++;
  3608. attrs = newattrs;
  3609. }
  3610. }
  3611. bool CAtomPTree::removeAttribute(const char *key)
  3612. {
  3613. AttrValue *del = findAttribute(key);
  3614. if (!del)
  3615. return false;
  3616. numAttrs--;
  3617. if (arrayOwner)
  3618. {
  3619. CQualifierMap *map = arrayOwner->queryMap();
  3620. if (map)
  3621. map->removeEntryIfMapped(key, del->value.get(), this);
  3622. }
  3623. CriticalBlock block(hashcrit);
  3624. if (del->key.isPtr())
  3625. attrHT->removekey(del->key.getPtr(), isnocase());
  3626. if (del->value.isPtr())
  3627. attrHT->removeval(del->value.getPtr());
  3628. AttrValue *newattrs = newAttrArray(numAttrs);
  3629. if (newattrs)
  3630. {
  3631. unsigned pos = del-attrs;
  3632. memcpy(newattrs, attrs, pos*sizeof(AttrValue));
  3633. memcpy(newattrs+pos, attrs+pos+1, (numAttrs-pos)*sizeof(AttrValue));
  3634. }
  3635. freeAttrArray(attrs, numAttrs+1);
  3636. attrs = newattrs;
  3637. return true;
  3638. }
  3639. ///////////////////
  3640. bool isEmptyPTree(const IPropertyTree *t)
  3641. {
  3642. if (!t)
  3643. return true;
  3644. if (t->numUniq())
  3645. return false;
  3646. Owned<IAttributeIterator> ai = t->getAttributes();
  3647. if (ai->first())
  3648. return false;
  3649. const char *s = t->queryProp(NULL);
  3650. if (s&&*s)
  3651. return false;
  3652. return true;
  3653. }
  3654. ///////////////////
  3655. PTLocalIteratorBase::PTLocalIteratorBase(const PTree *_tree, const char *_id, bool _nocase, bool _sort) : nocase(_nocase), sort(_sort), id(_id), tree(_tree)
  3656. {
  3657. class CPTArrayIterator : public ArrayIIteratorOf<IArrayOf<IPropertyTree>, IPropertyTree, IPropertyTreeIterator>
  3658. {
  3659. public:
  3660. CPTArrayIterator(IPropertyTreeIterator &src) : ArrayIIteratorOf<IArrayOf<IPropertyTree>, IPropertyTree, IPropertyTreeIterator>(elems)
  3661. {
  3662. ForEach(src)
  3663. elems.append(src.get());
  3664. elems.sort(comparePropTrees);
  3665. }
  3666. IArrayOf<IPropertyTree> elems;
  3667. };
  3668. tree->Link();
  3669. baseIter = tree->checkChildren()->getIterator(sort);
  3670. iter = NULL;
  3671. current = NULL;
  3672. }
  3673. PTLocalIteratorBase::~PTLocalIteratorBase()
  3674. {
  3675. baseIter->Release();
  3676. ::Release(iter);
  3677. tree->Release();
  3678. }
  3679. // IPropertyTreeIterator
  3680. bool PTLocalIteratorBase::first()
  3681. {
  3682. ::Release(iter); iter=NULL;
  3683. if (!baseIter || !baseIter->first()) return false;
  3684. return _next();
  3685. }
  3686. bool PTLocalIteratorBase::_next()
  3687. {
  3688. if (iter && iter->isValid() && iter->next())
  3689. return true;
  3690. for (;;)
  3691. {
  3692. for (;;)
  3693. {
  3694. if (!baseIter->isValid())
  3695. {
  3696. current = NULL;
  3697. return false;
  3698. }
  3699. else if (match())
  3700. break;
  3701. baseIter->next();
  3702. }
  3703. IPropertyTree *element = &baseIter->query();
  3704. baseIter->next();
  3705. if (iter)
  3706. iter->Release();
  3707. iter = element->getElements(NULL);
  3708. if (iter->first())
  3709. {
  3710. current = &iter->query();
  3711. return true;
  3712. }
  3713. }
  3714. }
  3715. bool PTLocalIteratorBase::next()
  3716. {
  3717. return _next();
  3718. }
  3719. bool PTLocalIteratorBase::isValid()
  3720. {
  3721. return (current != NULL);
  3722. }
  3723. /////////////////////////////
  3724. bool PTIdMatchIterator::match()
  3725. {
  3726. IPropertyTree &tree = baseIter->query();
  3727. const char *key = tree.queryName();
  3728. return (0 != WildMatch(key, id, nocase));
  3729. }
  3730. ////////////////////////////
  3731. SingleIdIterator::SingleIdIterator(const PTree &_tree, unsigned pos, unsigned _many) : many(_many), count(0), whichNext(pos-1), start(pos-1), current(NULL), tree(_tree)
  3732. {
  3733. tree.Link();
  3734. }
  3735. SingleIdIterator::~SingleIdIterator()
  3736. {
  3737. tree.Release();
  3738. }
  3739. void SingleIdIterator::setCurrent(unsigned pos)
  3740. {
  3741. current = tree.value->queryElement(pos);
  3742. }
  3743. // IInterface impl.
  3744. bool SingleIdIterator::first()
  3745. {
  3746. whichNext = start;
  3747. if (!tree.value || !tree.value->isArray())
  3748. {
  3749. if (0 == whichNext)
  3750. {
  3751. current = const_cast<PTree*>(&tree);
  3752. count = 1;
  3753. }
  3754. }
  3755. else
  3756. {
  3757. count = tree.value->elements();
  3758. if (whichNext < count)
  3759. setCurrent(whichNext);
  3760. else
  3761. return false;
  3762. }
  3763. ++whichNext;
  3764. return true;
  3765. }
  3766. bool SingleIdIterator::next()
  3767. {
  3768. if ((whichNext>=count) || ((unsigned) -1 != many && whichNext>start+many))
  3769. {
  3770. current = NULL;
  3771. return false;
  3772. }
  3773. setCurrent(whichNext++);
  3774. return true;
  3775. }
  3776. bool SingleIdIterator::isValid()
  3777. {
  3778. return (NULL != current);
  3779. }
  3780. //////////////
  3781. class StackElement
  3782. {
  3783. public:
  3784. void init(IPropertyTreeIterator *_iter, const char *_xpath)
  3785. {
  3786. xpath = (char *)strdup(_xpath);
  3787. iter=LINK(_iter);
  3788. }
  3789. void clear()
  3790. {
  3791. ::Release(iter);
  3792. if (xpath)
  3793. free(xpath);
  3794. }
  3795. IPropertyTreeIterator *get(StringAttr &str)
  3796. {
  3797. str.setown(xpath); return iter; // NB used in place of pop, as element invalid after call
  3798. }
  3799. IPropertyTreeIterator *iter;
  3800. char * xpath;
  3801. };
  3802. ///////////////////
  3803. PTStackIterator::PTStackIterator(IPropertyTreeIterator *_iter, const char *_xpath) : rootIter(_iter), xpath(_xpath)
  3804. {
  3805. iter = NULL;
  3806. xxpath = "";
  3807. current = NULL;
  3808. stacklen = 0;
  3809. stackmax = 4;
  3810. stack = (StackElement *)malloc(sizeof(StackElement)*stackmax);
  3811. }
  3812. PTStackIterator::~PTStackIterator()
  3813. {
  3814. while (stacklen)
  3815. stack[--stacklen].clear();
  3816. ::Release(iter);
  3817. ::Release(rootIter);
  3818. free(stack);
  3819. }
  3820. void PTStackIterator::setIterator(IPropertyTreeIterator *_iter)
  3821. {
  3822. assertex(_iter);
  3823. if (iter)
  3824. iter->Release();
  3825. iter = _iter;
  3826. iter->first();
  3827. }
  3828. // IIterator impl.
  3829. bool PTStackIterator::first()
  3830. {
  3831. while (stacklen)
  3832. stack[--stacklen].clear();
  3833. current = NULL;
  3834. xxpath = xpath;
  3835. rootIter->Link();
  3836. setIterator(rootIter);
  3837. return next();
  3838. }
  3839. bool PTStackIterator::isValid()
  3840. {
  3841. return (current != NULL);
  3842. }
  3843. IPropertyTree &PTStackIterator::query()
  3844. {
  3845. assertex(current);
  3846. return *current;
  3847. }
  3848. bool PTStackIterator::next()
  3849. {
  3850. bool separator = false;
  3851. if (iter)
  3852. {
  3853. IPropertyTree *element = NULL;
  3854. StringBuffer qualifierText;
  3855. for (;;)
  3856. {
  3857. while (!iter->isValid())
  3858. {
  3859. if (iter) iter->Release();
  3860. iter = popFromStack(stackPath); // leaves linked
  3861. if (!iter)
  3862. {
  3863. current = NULL;
  3864. return false;
  3865. }
  3866. xxpath = stackPath;
  3867. element = NULL;
  3868. }
  3869. if (!element)
  3870. {
  3871. element = &iter->query();
  3872. iter->next();
  3873. }
  3874. while (element)
  3875. {
  3876. switch (*xxpath)
  3877. {
  3878. case '\0':
  3879. current = element;
  3880. return true;
  3881. case '.':
  3882. if (separator) throw MakeXPathException(xpath, PTreeExcpt_XPath_ParseError, 0, "Syntax error");
  3883. separator=false;
  3884. ++xxpath;
  3885. if (*xpath && '/' != *xpath)
  3886. throw MakeXPathException(xpath-1, PTreeExcpt_XPath_Unsupported, 0, "\"/\" expected");
  3887. break;
  3888. case '/':
  3889. ++xxpath;
  3890. if ('/' == *xxpath)
  3891. {
  3892. --xxpath;
  3893. if (iter->isValid())
  3894. pushToStack(iter, xxpath);
  3895. setIterator(element->getElements(xxpath));
  3896. xxpath = "";
  3897. element = NULL;
  3898. }
  3899. separator=true;
  3900. break;
  3901. default:
  3902. separator=false;
  3903. if (iter->isValid())
  3904. pushToStack(iter, xxpath);
  3905. bool wild, numeric;
  3906. const char *start = xxpath;
  3907. readWildIdIndex(xxpath, wild, numeric);
  3908. size32_t s = xxpath-start;
  3909. if (s)
  3910. {
  3911. // NB: actually an id not qualifier, just sharing var.
  3912. qualifierText.clear().append(s, start);
  3913. bool mapped = false;
  3914. if (!wild && !numeric)
  3915. {
  3916. ChildMap *children = ((PTree *)element)->checkChildren();
  3917. if (children)
  3918. {
  3919. IPropertyTree *child = children->query(qualifierText);
  3920. if (child)
  3921. {
  3922. if ('[' == *xxpath)
  3923. {
  3924. const char *newXXPath = xxpath+1;
  3925. Owned<IPropertyTreeIterator> mapIter = checkMapIterator(newXXPath, *child);
  3926. if (mapIter)
  3927. {
  3928. setIterator(mapIter.getClear());
  3929. mapped = true;
  3930. xxpath = newXXPath;
  3931. }
  3932. }
  3933. }
  3934. }
  3935. }
  3936. if (!mapped)
  3937. setIterator(element->getElements(qualifierText));
  3938. }
  3939. else // must be qualifier.
  3940. {
  3941. if ('[' != *xxpath)
  3942. throw MakeXPathException(xxpath, PTreeExcpt_XPath_ParseError, 0, "Qualifier expected e.g. [..]");
  3943. const char *start = xxpath;
  3944. char quote = 0;
  3945. while (']' != *(++xxpath) || quote)
  3946. {
  3947. switch (*xxpath) {
  3948. case '\"':
  3949. case '\'':
  3950. {
  3951. if (quote)
  3952. {
  3953. if (*xxpath == quote)
  3954. quote = 0;
  3955. }
  3956. else
  3957. quote = *xxpath;
  3958. break;
  3959. }
  3960. case '\0':
  3961. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xxpath-start, "Qualifier brace unclosed");
  3962. }
  3963. }
  3964. ++xxpath;
  3965. qualifierText.clear().append(xxpath-start, start);
  3966. setIterator(element->getElements(qualifierText.str()));
  3967. }
  3968. element = NULL;
  3969. break;
  3970. }
  3971. }
  3972. }
  3973. }
  3974. return false;
  3975. }
  3976. void PTStackIterator::pushToStack(IPropertyTreeIterator *iter, const char *xpath)
  3977. {
  3978. if (stacklen==stackmax) {
  3979. stackmax *= 2;
  3980. stack = (StackElement *)realloc(stack, sizeof(StackElement)*stackmax);
  3981. }
  3982. stack[stacklen++].init(iter, xpath);
  3983. }
  3984. IPropertyTreeIterator *PTStackIterator::popFromStack(StringAttr &path)
  3985. {
  3986. if (!stacklen)
  3987. return NULL;
  3988. return stack[--stacklen].get(path);
  3989. }
  3990. #define DEFAULT_PTREE_TYPE LocalPTree
  3991. // factory methods
  3992. IPropertyTree *createPTree(MemoryBuffer &src, byte flags)
  3993. {
  3994. IPropertyTree *tree = createPTree(nullptr, flags);
  3995. tree->deserialize(src);
  3996. return tree;
  3997. }
  3998. IPropertyTree *createPTreeFromIPT(const IPropertyTree *srcTree, ipt_flags flags)
  3999. {
  4000. Owned<PTree> tree = (PTree *)createPTree(NULL, flags);
  4001. return tree->clone(*srcTree->queryBranch(NULL));
  4002. }
  4003. void mergePTree(IPropertyTree *target, IPropertyTree *toMerge)
  4004. {
  4005. Owned<IAttributeIterator> aiter = toMerge->getAttributes();
  4006. ForEach (*aiter)
  4007. target->addProp(aiter->queryName(), aiter->queryValue());
  4008. Owned<IPropertyTreeIterator> iter = toMerge->getElements("*");
  4009. ForEach (*iter)
  4010. {
  4011. IPropertyTree &e = iter->query();
  4012. target->addPropTree(e.queryName(), LINK(&e));
  4013. }
  4014. }
  4015. void _synchronizePTree(IPropertyTree *target, const IPropertyTree *source, bool removeTargetsNotInSource)
  4016. {
  4017. Owned<IAttributeIterator> aiter = target->getAttributes();
  4018. StringArray targetAttrs;
  4019. if (removeTargetsNotInSource)
  4020. {
  4021. ForEach (*aiter)
  4022. targetAttrs.append(aiter->queryName());
  4023. }
  4024. aiter.setown(source->getAttributes());
  4025. ForEach (*aiter)
  4026. {
  4027. const char *attr = aiter->queryName();
  4028. if (!target->hasProp(attr))
  4029. target->setProp(attr, aiter->queryValue());
  4030. else
  4031. {
  4032. const char *sValue = aiter->queryValue();
  4033. const char *tValue = target->queryProp(attr);
  4034. if (NULL == sValue)
  4035. {
  4036. if (NULL != tValue)
  4037. target->setProp(attr, sValue);
  4038. }
  4039. else if (NULL == tValue ||0 != strcmp(sValue, tValue))
  4040. target->setProp(attr, sValue);
  4041. if (removeTargetsNotInSource)
  4042. targetAttrs.zap(attr);
  4043. }
  4044. }
  4045. if (removeTargetsNotInSource)
  4046. {
  4047. // remaining
  4048. ForEachItemIn (a, targetAttrs)
  4049. target->removeProp(targetAttrs.item(a));
  4050. }
  4051. bool equal = true;
  4052. MemoryBuffer srcMb;
  4053. const char *src = NULL;
  4054. if (target->isBinary())
  4055. {
  4056. MemoryBuffer tgtMb;
  4057. target->getPropBin(NULL, tgtMb);
  4058. source->getPropBin(NULL, srcMb);
  4059. if (tgtMb.length() != srcMb.length())
  4060. equal = false;
  4061. else if (0 != memcmp(tgtMb.toByteArray(), srcMb.toByteArray(), tgtMb.length()))
  4062. equal = false;
  4063. }
  4064. else
  4065. {
  4066. const char *tgt = target->queryProp(NULL);
  4067. src = source->queryProp(NULL);
  4068. unsigned lTgt = tgt?(size32_t)strlen(tgt):0;
  4069. unsigned lSrc = src?(size32_t)strlen(src):0;
  4070. if (lTgt != lSrc)
  4071. equal = false;
  4072. else if (0 != lTgt && (0 != strcmp(tgt, src)))
  4073. equal = false;
  4074. }
  4075. if (!equal)
  4076. {
  4077. if (target->isBinary())
  4078. target->setPropBin(NULL, srcMb.length(), srcMb.toByteArray());
  4079. else
  4080. target->setProp(NULL, src);
  4081. }
  4082. ICopyArrayOf<IPropertyTree> toProcess;
  4083. Owned<IPropertyTreeIterator> iter = source->getElements("*");
  4084. ForEach (*iter)
  4085. toProcess.append(iter->query());
  4086. iter.setown(target->getElements("*"));
  4087. ICopyArrayOf<IPropertyTree> removeTreeList;
  4088. Owned<IPropertyTreeIterator> srcTypeIter;
  4089. StringAttr firstOfType;
  4090. ForEach (*iter)
  4091. {
  4092. IPropertyTree &e = iter->query();
  4093. const char *name = e.queryName();
  4094. IPropertyTree *sourceCompare;
  4095. if (!source->hasProp(name))
  4096. {
  4097. removeTreeList.append(e);
  4098. firstOfType.clear();
  4099. srcTypeIter.clear();
  4100. }
  4101. else
  4102. {
  4103. if (firstOfType.isEmpty() || 0 != strcmp(firstOfType, e.queryName()))
  4104. {
  4105. if (firstOfType.length() && srcTypeIter)
  4106. {
  4107. // add remaining
  4108. while (srcTypeIter->next())
  4109. {
  4110. sourceCompare = &srcTypeIter->query();
  4111. target->addPropTree(sourceCompare->queryName(), LINK(sourceCompare));
  4112. toProcess.zap(*sourceCompare);
  4113. }
  4114. }
  4115. srcTypeIter.setown(source->getElements(e.queryName()));
  4116. firstOfType.set(e.queryName());
  4117. assertex(srcTypeIter->first());
  4118. sourceCompare = &srcTypeIter->query();
  4119. }
  4120. else // 2nd of type etc..
  4121. sourceCompare = srcTypeIter->next() ? &srcTypeIter->query() : NULL;
  4122. if (sourceCompare)
  4123. {
  4124. toProcess.zap(*sourceCompare);
  4125. _synchronizePTree(&e, sourceCompare, removeTargetsNotInSource);
  4126. }
  4127. else
  4128. removeTreeList.append(e);
  4129. }
  4130. }
  4131. if (removeTargetsNotInSource)
  4132. {
  4133. ForEachItemIn (rt, removeTreeList)
  4134. target->removeTree(&removeTreeList.item(rt));
  4135. }
  4136. // add unprocessed source elements, not reference by name in target
  4137. ForEachItemIn (s, toProcess)
  4138. {
  4139. IPropertyTree &e = toProcess.item(s);
  4140. target->addPropTree(e.queryName(), LINK(&e));
  4141. }
  4142. }
  4143. /* ensure target is equivalent to source whilst retaining elements already present in target.
  4144. * presevers ordering of matching elements.
  4145. * If removeTargetsNotInSource = true (default) elements in the target not present in the source will be removed
  4146. */
  4147. void synchronizePTree(IPropertyTree *target, const IPropertyTree *source, bool removeTargetsNotInSource, bool rootsMustMatch)
  4148. {
  4149. if (rootsMustMatch)
  4150. {
  4151. const char *srcName = source->queryName();
  4152. const char *tgtName = target->queryName();
  4153. if (0 != strcmp(srcName, tgtName))
  4154. throw MakeIPTException(PTreeExcpt_Unsupported, "Cannot synchronize if root nodes mismatch");
  4155. }
  4156. _synchronizePTree(target, source, removeTargetsNotInSource);
  4157. }
  4158. IPropertyTree *ensurePTree(IPropertyTree *root, const char *xpath)
  4159. {
  4160. return createPropBranch(root, xpath, true);
  4161. }
  4162. IPTreeReadException *createPTreeReadException(int code, const char *msg, const char *context, unsigned line, offset_t offset)
  4163. {
  4164. //Do not use jlib_thrown_decl because it causes problems with VS2017 - I think because of beforeDispose() in CInterfaceOf.
  4165. //The type of the object actually thrown is IPTreeReadException which does have a jlib_thrown_decl - so it will still be caught.
  4166. class CPTreeReadException : implements CInterfaceOf<IPTreeReadException>
  4167. {
  4168. int code;
  4169. StringAttr msg;
  4170. StringAttr context;
  4171. unsigned line;
  4172. offset_t offset;
  4173. StringBuffer &getErrorMessage(StringBuffer &out) const
  4174. {
  4175. switch (code)
  4176. {
  4177. case PTreeRead_EOS:
  4178. return out.append("Error - end of stream");
  4179. case PTreeRead_syntax:
  4180. return out.append("Error - syntax error");
  4181. }
  4182. return out;
  4183. }
  4184. public:
  4185. CPTreeReadException(int _code, const char *_msg, const char *_context, unsigned _line, offset_t _offset) : code(_code), msg(_msg), context(_context), line(_line), offset(_offset) { }
  4186. // IException
  4187. int errorCode() const { return code; }
  4188. StringBuffer &errorMessage(StringBuffer &str) const
  4189. {
  4190. getErrorMessage(str);
  4191. if (msg.length())
  4192. str.append(" \"").append(msg).append("\"");
  4193. str.append(" [");
  4194. if (line>1) // don't bother with line 1, there may be no line breaks.
  4195. str.append("line ").append(line).append(", ");
  4196. str.append("file offset ").append(offset).append("]");
  4197. if (context.length())
  4198. str.newline().append(context);
  4199. return str;
  4200. }
  4201. MessageAudience errorAudience() const { return MSGAUD_user; }
  4202. const char *queryDescription() { return msg; }
  4203. unsigned queryLine() { return line; }
  4204. offset_t queryOffset() { return offset; }
  4205. const char *queryContext() { return context.get(); }
  4206. };
  4207. return new CPTreeReadException(code, msg, context, line, offset);
  4208. }
  4209. template <typename T>
  4210. class CommonReaderBase : public CInterface
  4211. {
  4212. Linked<ISimpleReadStream> lstream;
  4213. ISimpleReadStream *stream;
  4214. bool bufOwned, nullTerm;
  4215. byte *buf, *bufPtr;
  4216. size32_t bufSize, bufRemaining;
  4217. protected:
  4218. PTreeReaderOptions readerOptions;
  4219. bool ignoreWhiteSpace, noRoot;
  4220. Linked<IPTreeNotifyEvent> iEvent;
  4221. offset_t curOffset;
  4222. unsigned line;
  4223. char nextChar;
  4224. private:
  4225. void init()
  4226. {
  4227. ignoreWhiteSpace = 0 != ((unsigned)readerOptions & (unsigned)ptr_ignoreWhiteSpace);
  4228. noRoot = 0 != ((unsigned)readerOptions & (unsigned)ptr_noRoot);
  4229. }
  4230. void resetState()
  4231. {
  4232. bufPtr = buf;
  4233. nextChar = 0;
  4234. if (nullTerm || stream)
  4235. bufRemaining = 0;
  4236. curOffset = 0;
  4237. line = 0;
  4238. }
  4239. public:
  4240. CommonReaderBase(ISimpleReadStream &_stream, IPTreeNotifyEvent &_iEvent, PTreeReaderOptions _readerOptions, size32_t _bufSize=0) :
  4241. bufSize(_bufSize), readerOptions(_readerOptions), iEvent(&_iEvent)
  4242. {
  4243. if (!bufSize) bufSize = 0x20000;
  4244. buf = new byte[bufSize];
  4245. bufRemaining = 0;
  4246. curOffset = 0;
  4247. bufOwned = true;
  4248. nullTerm = false;
  4249. lstream.set(&_stream);
  4250. stream = &_stream; // for efficiency
  4251. init();
  4252. resetState();
  4253. }
  4254. CommonReaderBase(const void *_buf, size32_t bufLength, IPTreeNotifyEvent &_iEvent, PTreeReaderOptions _readerOptions) :
  4255. readerOptions(_readerOptions), iEvent(&_iEvent)
  4256. {
  4257. bufSize = 0; // not used for direct reads
  4258. stream = NULL; // not used for direct reads
  4259. bufRemaining = bufLength;
  4260. nullTerm = false;
  4261. buf = (byte *)_buf;
  4262. bufOwned = false;
  4263. init();
  4264. resetState();
  4265. }
  4266. CommonReaderBase(const void *_buf, IPTreeNotifyEvent &_iEvent, PTreeReaderOptions _readerOptions) :
  4267. readerOptions(_readerOptions), iEvent(&_iEvent)
  4268. {
  4269. bufSize = 0; // not used for direct reads
  4270. stream = NULL; // not used for direct reads
  4271. curOffset = 0;
  4272. bufRemaining = 0;
  4273. nullTerm = true;
  4274. buf = (byte *)_buf;
  4275. bufOwned = false;
  4276. init();
  4277. resetState();
  4278. }
  4279. ~CommonReaderBase()
  4280. {
  4281. if (bufOwned)
  4282. delete [] buf;
  4283. }
  4284. protected:
  4285. virtual void reset()
  4286. {
  4287. resetState();
  4288. }
  4289. void rewind(size32_t n)
  4290. {
  4291. assertex(curOffset >= n);
  4292. if (!n) return;
  4293. curOffset -= n;
  4294. size32_t d = (size32_t)(bufPtr-buf);
  4295. if (n > d) n = d;
  4296. if (!nullTerm)
  4297. bufRemaining += n;
  4298. for (;;)
  4299. {
  4300. --bufPtr;
  4301. if (!--n) break;
  4302. if (10 == *bufPtr) --line;
  4303. }
  4304. }
  4305. bool checkBOM()
  4306. {
  4307. bool utf16 = false;
  4308. bool utf8 = false;
  4309. // Note - technically the utf16 LE case could also be utf32 LE (utf32 BE would be 0x00 0x00 0xfe 0xff)
  4310. // But utf32 is so rare that we ignore it for now
  4311. switch ((unsigned char)nextChar)
  4312. {
  4313. case 0xff:
  4314. readNext();
  4315. if (0xfe == (unsigned char)nextChar)
  4316. utf16 = true;
  4317. break;
  4318. case 0xfe:
  4319. readNext();
  4320. if (0xff == (unsigned char)nextChar)
  4321. utf16 = true;
  4322. break;
  4323. case 0xef:
  4324. readNext();
  4325. if (0xbb == (unsigned char)nextChar)
  4326. {
  4327. readNext();
  4328. if (0xbf == (unsigned char)nextChar)
  4329. utf8 = true;
  4330. }
  4331. break;
  4332. default:
  4333. break;
  4334. }
  4335. if (utf8)
  4336. return true;
  4337. else if (utf16)
  4338. error("Unsupported utf16 format detected in BOM header", false);
  4339. return false;
  4340. }
  4341. inline void expecting(const char *str)
  4342. {
  4343. StringBuffer errorMsg("Expecting \"");
  4344. error(errorMsg.append(str).append("\"").str());
  4345. }
  4346. inline void eos()
  4347. {
  4348. error("String terminator hit");
  4349. }
  4350. void match(const char *txt, const char *msg=NULL)
  4351. {
  4352. const char *c = txt;
  4353. for (;;)
  4354. {
  4355. if (*c == '\0') break;
  4356. readNext();
  4357. if (toupper(nextChar) != toupper(*c))
  4358. {
  4359. if (msg)
  4360. error(msg);
  4361. throw c;
  4362. }
  4363. c++;
  4364. }
  4365. }
  4366. void error(const char *msg=NULL, bool giveContext=true, PTreeReadExcptCode code=PTreeRead_syntax) __attribute__((noreturn))
  4367. {
  4368. StringBuffer context;
  4369. if (giveContext)
  4370. {
  4371. size32_t bufPos = (size32_t)(bufPtr-buf);
  4372. unsigned preLen = std::min(40U, bufPos);
  4373. size32_t bR = bufRemaining;
  4374. if (nullTerm)
  4375. {
  4376. byte *tPtr = bufPtr;
  4377. while (bR<40)
  4378. {
  4379. if ('\0' == *tPtr++) break;
  4380. bR++;
  4381. }
  4382. }
  4383. unsigned postLen = std::min(80-preLen, bR);
  4384. const char *bufferContext = (const char *)(bufPtr - preLen);
  4385. context.append(preLen, bufferContext);
  4386. context.append("*ERROR*");
  4387. context.append(postLen, bufferContext+preLen);
  4388. }
  4389. throw createPTreeReadException(code, msg, context.str(), line+1, curOffset);
  4390. }
  4391. inline void readNext()
  4392. {
  4393. if (!readNextToken())
  4394. error("End of stream encountered whilst parsing", true, PTreeRead_EOS);
  4395. curOffset++;
  4396. }
  4397. inline bool checkReadNext()
  4398. {
  4399. if (!readNextToken())
  4400. return false;
  4401. curOffset++;
  4402. return true;
  4403. }
  4404. inline bool checkStartReadNext()
  4405. {
  4406. if (curOffset || nextChar) //not at starting state
  4407. return true;
  4408. return readNextToken();
  4409. }
  4410. inline bool readNextToken();
  4411. inline bool checkSkipWS()
  4412. {
  4413. while (isspace(nextChar)) if (!checkReadNext()) return false;
  4414. return true;
  4415. }
  4416. inline void skipWS()
  4417. {
  4418. while (isspace(nextChar)) readNext();
  4419. }
  4420. };
  4421. class CInstStreamReader { public: }; // only used to ensure different template definitions.
  4422. class CInstBufferReader { public: };
  4423. class CInstStringReader { public: };
  4424. template <> inline bool CommonReaderBase<CInstStreamReader>::readNextToken()
  4425. {
  4426. // do own buffering, to have reasonable error context.
  4427. if (0 == bufRemaining)
  4428. {
  4429. size32_t _bufRemaining = stream->read(bufSize, buf);
  4430. if (!_bufRemaining)
  4431. return false;
  4432. bufRemaining = _bufRemaining;
  4433. bufPtr = buf;
  4434. }
  4435. --bufRemaining;
  4436. nextChar = *bufPtr++;
  4437. if (10 == nextChar)
  4438. line++;
  4439. return true;
  4440. }
  4441. template <> inline bool CommonReaderBase<CInstBufferReader>::readNextToken()
  4442. {
  4443. if (0 == bufRemaining)
  4444. return false;
  4445. --bufRemaining;
  4446. nextChar = *bufPtr++;
  4447. if (10 == nextChar)
  4448. line++;
  4449. return true;
  4450. }
  4451. template <> inline bool CommonReaderBase<CInstStringReader>::readNextToken()
  4452. {
  4453. nextChar = *bufPtr++;
  4454. if ('\0' == nextChar)
  4455. {
  4456. --bufPtr;
  4457. return false;
  4458. }
  4459. if (10 == nextChar)
  4460. line++;
  4461. return true;
  4462. }
  4463. template <typename X>
  4464. class CXMLReaderBase : public CommonReaderBase<X>, implements IEntityHelper
  4465. {
  4466. StringAttrMapping entityTable;
  4467. protected:
  4468. bool ignoreNameSpaces;
  4469. bool hadXMLDecl;
  4470. private:
  4471. void init()
  4472. {
  4473. ignoreNameSpaces = 0 != ((unsigned) readerOptions & (unsigned)ptr_ignoreNameSpaces);
  4474. }
  4475. void resetState()
  4476. {
  4477. hadXMLDecl = false;
  4478. }
  4479. public:
  4480. typedef CommonReaderBase<X> PARENT;
  4481. using PARENT::nextChar;
  4482. using PARENT::readNext;
  4483. using PARENT::expecting;
  4484. using PARENT::match;
  4485. using PARENT::error;
  4486. using PARENT::skipWS;
  4487. using PARENT::rewind;
  4488. using PARENT::readerOptions;
  4489. CXMLReaderBase(ISimpleReadStream &_stream, IPTreeNotifyEvent &_iEvent, PTreeReaderOptions _xmlReaderOptions, size32_t _bufSize=0)
  4490. : CommonReaderBase<X>(_stream, _iEvent, _xmlReaderOptions, _bufSize)
  4491. {
  4492. init();
  4493. resetState();
  4494. }
  4495. CXMLReaderBase(const void *_buf, size32_t bufLength, IPTreeNotifyEvent &_iEvent, PTreeReaderOptions _xmlReaderOptions)
  4496. : CommonReaderBase<X>(_buf, bufLength, _iEvent, _xmlReaderOptions)
  4497. {
  4498. init();
  4499. resetState();
  4500. }
  4501. CXMLReaderBase(const void *_buf, IPTreeNotifyEvent &_iEvent, PTreeReaderOptions _xmlReaderOptions)
  4502. : CommonReaderBase<X>(_buf, _iEvent, _xmlReaderOptions)
  4503. {
  4504. init();
  4505. resetState();
  4506. }
  4507. protected:
  4508. virtual void reset() override
  4509. {
  4510. resetState();
  4511. PARENT::reset();
  4512. }
  4513. void readID(StringBuffer &id)
  4514. {
  4515. if (isValidXPathStartChr(nextChar))
  4516. {
  4517. for (;;)
  4518. {
  4519. id.append(nextChar);
  4520. readNext();
  4521. if (!isValidXPathChr(nextChar)) break;
  4522. }
  4523. }
  4524. }
  4525. void skipString()
  4526. {
  4527. if ('"' == nextChar)
  4528. {
  4529. do { readNext(); } while ('"' != nextChar);
  4530. }
  4531. else if ('\'' == nextChar)
  4532. {
  4533. do { readNext(); } while ('\'' != nextChar);
  4534. }
  4535. else expecting("\" or '");
  4536. }
  4537. bool lookupRefValue(const char *name, StringBuffer &value)
  4538. {
  4539. StringAttr *val = entityTable.getValue(name);
  4540. if (!val) return false;
  4541. value.append(*val);
  4542. return true;
  4543. }
  4544. void storeEntity(const char *name, const char *value)
  4545. {
  4546. entityTable.setValue(name, value);
  4547. }
  4548. void parseEntity()
  4549. {
  4550. try { match("NTITY"); }
  4551. catch (const char *) { error("Bad syntax"); }
  4552. readNext();
  4553. skipWS();
  4554. StringBuffer entityName;
  4555. if ('%' != nextChar)
  4556. {
  4557. readID(entityName);
  4558. skipWS();
  4559. if ('"' == nextChar)
  4560. {
  4561. StringBuffer refValue;
  4562. for (;;)
  4563. {
  4564. readNext();
  4565. if (!nextChar || '"' == nextChar)
  4566. break;
  4567. if ('&' == nextChar)
  4568. {
  4569. readNext();
  4570. StringBuffer ref;
  4571. if ('#' == nextChar)
  4572. {
  4573. ref.append("&#");
  4574. for (;;)
  4575. {
  4576. readNext();
  4577. if (!nextChar)
  4578. expecting(";");
  4579. if (';' == nextChar) break;
  4580. ref.append(nextChar);
  4581. }
  4582. ref.append(";");
  4583. decodeXML(ref, refValue);
  4584. }
  4585. else
  4586. {
  4587. readID(ref);
  4588. if (';' != nextChar)
  4589. expecting(";");
  4590. if (!lookupRefValue(ref, refValue))
  4591. {
  4592. StringBuffer _ref("&");
  4593. _ref.append(ref).append(';');
  4594. decodeXML(ref, refValue); // try inbuilts
  4595. }
  4596. }
  4597. }
  4598. else
  4599. refValue.append(nextChar);
  4600. }
  4601. storeEntity(entityName, refValue);
  4602. }
  4603. }
  4604. do { readNext(); }
  4605. while (nextChar && nextChar != '>');
  4606. }
  4607. void parseIntSubset()
  4608. {
  4609. for (;;)
  4610. {
  4611. readNext();
  4612. skipWS();
  4613. if (']'== nextChar) break;
  4614. if ('<' == nextChar)
  4615. {
  4616. readNext();
  4617. switch (nextChar)
  4618. {
  4619. case '!':
  4620. {
  4621. readNext();
  4622. switch (nextChar)
  4623. {
  4624. case '-':
  4625. parseComment();
  4626. break;
  4627. case 'E':
  4628. parseEntity();
  4629. break;
  4630. default: // ignore anything else
  4631. do { readNext(); }
  4632. while (nextChar && nextChar != '>');
  4633. break;
  4634. }
  4635. break;
  4636. }
  4637. case '?':
  4638. {
  4639. StringBuffer pi;
  4640. parsePI(pi);
  4641. break;
  4642. }
  4643. }
  4644. }
  4645. }
  4646. }
  4647. void parseOther()
  4648. {
  4649. switch (nextChar)
  4650. {
  4651. case '-':
  4652. parseComment2();
  4653. break;
  4654. case 'D':
  4655. {
  4656. try { match("OCTYPE"); }
  4657. catch (const char *) { error("Bad syntax"); }
  4658. readNext();
  4659. skipWS();
  4660. StringBuffer doctypeid;
  4661. readID(doctypeid);
  4662. for (;;)
  4663. {
  4664. skipWS();
  4665. if ('>' == nextChar) break;
  4666. if ('[' == nextChar)
  4667. {
  4668. parseIntSubset();
  4669. if (']' != nextChar)
  4670. expecting("]");
  4671. }
  4672. else if ('S' == nextChar)
  4673. {
  4674. match("YSTEM");
  4675. readNext();
  4676. skipWS();
  4677. skipString();
  4678. }
  4679. else if ('P' == nextChar)
  4680. {
  4681. match("UBLIC");
  4682. readNext();
  4683. skipWS();
  4684. skipString();
  4685. readNext();
  4686. skipWS();
  4687. skipString();
  4688. }
  4689. readNext();
  4690. }
  4691. break;
  4692. }
  4693. default:
  4694. error("Invalid information tag");
  4695. }
  4696. }
  4697. void parsePIOrDecl()
  4698. {
  4699. StringBuffer target;
  4700. parsePI(target);
  4701. if (0 == strcmp("xml", target.str()))
  4702. {
  4703. if (hadXMLDecl)
  4704. error("Only one XML declartion permitted");
  4705. hadXMLDecl = true;
  4706. }
  4707. }
  4708. void parseCData(StringBuffer &text)
  4709. {
  4710. try { match("CDATA["); }
  4711. catch (const char *) { error("Bad CDATA syntax"); }
  4712. for (;;)
  4713. {
  4714. readNext();
  4715. while (']' == nextChar)
  4716. {
  4717. readNext();
  4718. while (']' == nextChar)
  4719. {
  4720. readNext();
  4721. if ('>' == nextChar)
  4722. return;
  4723. else
  4724. text.append(']');
  4725. }
  4726. text.append(']');
  4727. }
  4728. text.append(nextChar);
  4729. }
  4730. }
  4731. void parsePI(StringBuffer &target)
  4732. {
  4733. readNext();
  4734. if (!isValidXPathStartChr(nextChar))
  4735. error("Invalid PI target");
  4736. for (;;)
  4737. {
  4738. target.append(nextChar);
  4739. readNext();
  4740. if (!isValidXPathChr(nextChar))
  4741. break;
  4742. }
  4743. skipWS();
  4744. unsigned closeTag=0;
  4745. for (;;)
  4746. {
  4747. if (!nextChar)
  4748. error("Missing closing PI tag ?>");
  4749. if (1 == closeTag)
  4750. {
  4751. if ('>' == nextChar)
  4752. break;
  4753. closeTag = 0;
  4754. }
  4755. else if ('?' == nextChar)
  4756. closeTag = 1;
  4757. readNext();
  4758. }
  4759. }
  4760. void parseDirective(StringBuffer &res)
  4761. {
  4762. readNext();
  4763. switch (nextChar) {
  4764. case '-':
  4765. parseComment2();
  4766. break;
  4767. case '[':
  4768. parseCData(res);
  4769. break;
  4770. default:
  4771. error("Unrecognised syntax");
  4772. }
  4773. }
  4774. void parseComment()
  4775. {
  4776. readNext();
  4777. if (nextChar != '-') error("Bad comment syntax");
  4778. parseComment2();
  4779. }
  4780. void parseComment2()
  4781. {
  4782. readNext();
  4783. if (nextChar != '-') error("Bad comment syntax");
  4784. readNext();
  4785. unsigned seen = 0;
  4786. while (nextChar)
  4787. {
  4788. if (seen==2)
  4789. {
  4790. if (nextChar=='>')
  4791. return;
  4792. else if (nextChar != '-') // should be syntax error really.
  4793. seen = 0;
  4794. }
  4795. else if (nextChar=='-')
  4796. seen++;
  4797. else
  4798. seen = 0;
  4799. readNext();
  4800. }
  4801. error("Bad comment syntax");
  4802. }
  4803. const char *_decodeXML(unsigned read, const char *startMark, StringBuffer &ret)
  4804. {
  4805. const char *errMark = NULL;
  4806. try { return decodeXML(startMark, ret, &errMark, this); }
  4807. catch (IException *e)
  4808. {
  4809. if (errMark)
  4810. rewind((unsigned)(errMark-startMark));
  4811. StringBuffer errMsg;
  4812. e->errorMessage(errMsg);
  4813. e->Release();
  4814. error(errMsg.str());
  4815. }
  4816. return NULL; // will never get here.
  4817. }
  4818. // IEntityHelper impl.
  4819. virtual bool find(const char *entity, StringBuffer &value) override
  4820. {
  4821. return lookupRefValue(entity, value);
  4822. }
  4823. };
  4824. template <class X>
  4825. class CXMLReader : public CXMLReaderBase<X>, implements IPTreeReader
  4826. {
  4827. bool rootTerminated;
  4828. StringBuffer attrName, attrval;
  4829. StringBuffer tmpStr;
  4830. void init()
  4831. {
  4832. attrName.append('@');
  4833. }
  4834. void resetState()
  4835. {
  4836. rootTerminated = false;
  4837. }
  4838. public:
  4839. typedef CXMLReaderBase<X> PARENT;
  4840. using PARENT::nextChar;
  4841. using PARENT::readNext;
  4842. using PARENT::expecting;
  4843. using PARENT::match;
  4844. using PARENT::error;
  4845. using PARENT::skipWS;
  4846. using PARENT::checkBOM;
  4847. using PARENT::checkReadNext;
  4848. using PARENT::checkSkipWS;
  4849. using PARENT::eos;
  4850. using PARENT::curOffset;
  4851. using PARENT::noRoot;
  4852. using PARENT::ignoreWhiteSpace;
  4853. using PARENT::iEvent;
  4854. using PARENT::parseDirective;
  4855. using PARENT::parseOther;
  4856. using PARENT::parsePI;
  4857. using PARENT::parsePIOrDecl;
  4858. using PARENT::parseComment;
  4859. using PARENT::_decodeXML;
  4860. using PARENT::ignoreNameSpaces;
  4861. using PARENT::hadXMLDecl;
  4862. IMPLEMENT_IINTERFACE;
  4863. CXMLReader(ISimpleReadStream &stream, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions, size32_t bufSize=0)
  4864. : PARENT(stream, iEvent, xmlReaderOptions, bufSize)
  4865. {
  4866. init();
  4867. resetState();
  4868. }
  4869. CXMLReader(const void *buf, size32_t bufLength, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions)
  4870. : PARENT(buf, bufLength, iEvent, xmlReaderOptions)
  4871. {
  4872. init();
  4873. resetState();
  4874. }
  4875. CXMLReader(const void *buf, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions)
  4876. : PARENT(buf, iEvent, xmlReaderOptions)
  4877. {
  4878. init();
  4879. resetState();
  4880. }
  4881. virtual void reset() override
  4882. {
  4883. resetState();
  4884. PARENT::reset();
  4885. }
  4886. // IPTreeReader
  4887. virtual void load() override { loadXML(); }
  4888. virtual offset_t queryOffset() override { return curOffset; }
  4889. void loadXML()
  4890. {
  4891. bool head=true;
  4892. restart:
  4893. if (!checkReadNext()) return;
  4894. if (head)
  4895. {
  4896. head = false;
  4897. if (checkBOM())
  4898. if (!checkReadNext()) return;
  4899. }
  4900. if (!checkSkipWS()) return;
  4901. if ('<' != nextChar)
  4902. expecting("<");
  4903. readNext();
  4904. if ('!' == nextChar)
  4905. {
  4906. readNext();
  4907. parseOther();
  4908. goto restart;
  4909. }
  4910. else if ('?' == nextChar)
  4911. {
  4912. parsePIOrDecl();
  4913. goto restart;
  4914. }
  4915. if (!noRoot && rootTerminated)
  4916. {
  4917. if (ignoreWhiteSpace)
  4918. if (!checkSkipWS()) return;
  4919. error("Trailing xml after close of root tag");
  4920. }
  4921. _loadXML();
  4922. if (noRoot)
  4923. {
  4924. head = true;
  4925. hadXMLDecl = false;
  4926. }
  4927. else
  4928. rootTerminated = true;
  4929. goto restart;
  4930. }
  4931. void _loadXML()
  4932. {
  4933. restart:
  4934. offset_t startOffset = curOffset-2;
  4935. if ('!' == nextChar) // not sure this branch can ever be hit.
  4936. {
  4937. parseComment();
  4938. readNext();
  4939. if ('<' != nextChar)
  4940. expecting("<");
  4941. goto restart;
  4942. }
  4943. StringBuffer tagName;
  4944. if (ignoreWhiteSpace)
  4945. skipWS();
  4946. while (!isspace(nextChar) && nextChar != '>' && nextChar != '/')
  4947. {
  4948. tagName.append(nextChar);
  4949. readNext();
  4950. if ('<' == nextChar)
  4951. error("Unmatched close tag encountered");
  4952. }
  4953. StringBuffer completeTagname(tagName);
  4954. if (ignoreNameSpaces)
  4955. {
  4956. const char *colon;
  4957. if ((colon = strchr(tagName.str(), ':')) != NULL)
  4958. tagName.remove(0, (size32_t)(colon - tagName.str() + 1));
  4959. }
  4960. iEvent->beginNode(tagName.str(), false, startOffset);
  4961. skipWS();
  4962. bool endTag = false;
  4963. bool base64 = false;
  4964. while(nextChar != '>')
  4965. {
  4966. skipWS();
  4967. if (nextChar=='/')
  4968. {
  4969. readNext();
  4970. if (nextChar != '>')
  4971. expecting(">");
  4972. endTag = true;
  4973. break;
  4974. }
  4975. attrName.setLength(1);
  4976. attrval.clear();
  4977. while (nextChar && !isspace(nextChar) && nextChar != '=' && nextChar != '>' && nextChar != '/')
  4978. {
  4979. attrName.append(nextChar);
  4980. readNext();
  4981. }
  4982. skipWS();
  4983. if (nextChar == '=') readNext(); else expecting("=");
  4984. skipWS();
  4985. if (nextChar == '"')
  4986. {
  4987. readNext();
  4988. while (nextChar != '"')
  4989. {
  4990. if (!nextChar)
  4991. eos();
  4992. attrval.append(nextChar);
  4993. readNext();
  4994. }
  4995. }
  4996. else if (nextChar == '\'')
  4997. {
  4998. readNext();
  4999. while (nextChar != '\'')
  5000. {
  5001. attrval.append(nextChar);
  5002. readNext();
  5003. }
  5004. }
  5005. else
  5006. error();
  5007. _decodeXML(0, attrval.str(), tmpStr.clear());
  5008. if (0 == strcmp(attrName.str(), "@xsi:type") &&
  5009. (0 == stricmp(tmpStr.str(),"SOAP-ENC:base64")))
  5010. base64 = true;
  5011. else
  5012. iEvent->newAttribute(attrName.str(), tmpStr.str());
  5013. readNext();
  5014. skipWS();
  5015. }
  5016. iEvent->beginNodeContent(tagName.str());
  5017. StringBuffer tagText;
  5018. bool binary = base64;
  5019. if (!endTag)
  5020. {
  5021. if (nextChar == '>')
  5022. {
  5023. for (;;)
  5024. {
  5025. for (;;)
  5026. {
  5027. readNext();
  5028. if (ignoreWhiteSpace)
  5029. skipWS();
  5030. if ('\0' == nextChar)
  5031. eos();
  5032. StringBuffer mark;
  5033. while (nextChar && nextChar !='<') { mark.append(nextChar); readNext(); }
  5034. size32_t l = mark.length();
  5035. size32_t r = l+1;
  5036. if (l)
  5037. {
  5038. if (ignoreWhiteSpace)
  5039. {
  5040. while (l-- && isspace(mark.charAt(l)));
  5041. mark.setLength(l+1);
  5042. }
  5043. tagText.ensureCapacity(mark.length());
  5044. _decodeXML(r, mark.str(), tagText);
  5045. }
  5046. readNext();
  5047. if ('!' == nextChar)
  5048. parseDirective(tagText);
  5049. else if ('?' == nextChar)
  5050. {
  5051. parsePI(tmpStr.clear());
  5052. #ifdef STRICT_PI
  5053. if (0 == stricmp(tmpStr.str(), "xml"))
  5054. error("Reserved PI target used");
  5055. #endif
  5056. }
  5057. else
  5058. break;
  5059. }
  5060. if (nextChar=='/')
  5061. {
  5062. if (base64)
  5063. {
  5064. JBASE64_Decode(tagText.str(), tmpStr.clear());
  5065. tagText.swapWith(tmpStr);
  5066. }
  5067. else
  5068. {
  5069. if (strlen(tagText.str()) != tagText.length())
  5070. binary = true;
  5071. }
  5072. break; // exit
  5073. }
  5074. else
  5075. _loadXML();
  5076. }
  5077. readNext();
  5078. unsigned i = 0;
  5079. while (!isspace(nextChar) && nextChar != '>')
  5080. {
  5081. if ((i >= completeTagname.length()) ||
  5082. (nextChar != completeTagname.charAt(i++)))
  5083. error("Mismatched opening and closing tags");
  5084. readNext();
  5085. }
  5086. if (i != completeTagname.length())
  5087. error("Mismatched opening and closing tags");
  5088. skipWS();
  5089. if (nextChar != '>')
  5090. expecting(">");
  5091. }
  5092. }
  5093. iEvent->endNode(tagName.str(), tagText.length(), tagText.str(), binary, curOffset);
  5094. }
  5095. };
  5096. template <class X>
  5097. class CPullXMLReader : public CXMLReaderBase<X>, implements IPullPTreeReader
  5098. {
  5099. typedef CXMLReaderBase<X> PARENT;
  5100. using PARENT::nextChar;
  5101. using PARENT::readNext;
  5102. using PARENT::expecting;
  5103. using PARENT::match;
  5104. using PARENT::error;
  5105. using PARENT::skipWS;
  5106. using PARENT::checkBOM;
  5107. using PARENT::checkReadNext;
  5108. using PARENT::checkSkipWS;
  5109. using PARENT::eos;
  5110. using PARENT::curOffset;
  5111. using PARENT::noRoot;
  5112. using PARENT::ignoreWhiteSpace;
  5113. using PARENT::iEvent;
  5114. using PARENT::parseDirective;
  5115. using PARENT::parseOther;
  5116. using PARENT::parsePI;
  5117. using PARENT::parsePIOrDecl;
  5118. using PARENT::parseComment;
  5119. using PARENT::_decodeXML;
  5120. using PARENT::ignoreNameSpaces;
  5121. using PARENT::hadXMLDecl;
  5122. class CStateInfo : public CInterface
  5123. {
  5124. public:
  5125. CStateInfo()
  5126. {
  5127. tag.ensureCapacity(15);
  5128. binary = base64 = false;
  5129. }
  5130. inline void reset()
  5131. {
  5132. binary = base64 = false;
  5133. tag.clear();
  5134. tagText.clear();
  5135. }
  5136. const char *wnsTag;
  5137. StringBuffer tag;
  5138. StringBuffer tagText;
  5139. bool binary, base64;
  5140. };
  5141. CICopyArrayOf<CStateInfo> stack, freeStateInfo;
  5142. CStateInfo *stateInfo;
  5143. enum ParseStates { headerStart, tagStart, tagAttributes, tagContent, tagContent2, tagClose, tagEnd, tagMarker } state;
  5144. bool endOfRoot;
  5145. StringBuffer attrName, attrval, mark, tmpStr;
  5146. void resetState()
  5147. {
  5148. stack.kill();
  5149. state = headerStart;
  5150. stateInfo = NULL;
  5151. endOfRoot = false;
  5152. attrName.append('@');
  5153. }
  5154. public:
  5155. IMPLEMENT_IINTERFACE;
  5156. CPullXMLReader(ISimpleReadStream &stream, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions, size32_t bufSize=0)
  5157. : CXMLReaderBase<X>(stream, iEvent, xmlReaderOptions, bufSize)
  5158. {
  5159. resetState();
  5160. }
  5161. CPullXMLReader(const void *buf, size32_t bufLength, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions)
  5162. : CXMLReaderBase<X>(buf, bufLength, iEvent, xmlReaderOptions)
  5163. {
  5164. resetState();
  5165. }
  5166. CPullXMLReader(const void *buf, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions)
  5167. : CXMLReaderBase<X>(buf, iEvent, xmlReaderOptions)
  5168. {
  5169. resetState();
  5170. }
  5171. ~CPullXMLReader()
  5172. {
  5173. ForEachItemIn(i, stack)
  5174. delete &stack.item(i);
  5175. ForEachItemIn(i2, freeStateInfo)
  5176. delete &freeStateInfo.item(i2);
  5177. }
  5178. // IPullPTreeReader
  5179. virtual void load() override
  5180. {
  5181. while (next()) {}
  5182. }
  5183. virtual void reset() override
  5184. {
  5185. PARENT::reset();
  5186. resetState();
  5187. }
  5188. virtual offset_t queryOffset() override { return curOffset; }
  5189. virtual bool next() override
  5190. {
  5191. switch (state)
  5192. {
  5193. case headerStart:
  5194. {
  5195. if (!checkReadNext()) return false;
  5196. if (checkBOM())
  5197. if (!checkReadNext()) return false;
  5198. for (;;)
  5199. {
  5200. if (!checkSkipWS()) return false;
  5201. if ('<' != nextChar)
  5202. expecting("<");
  5203. readNext();
  5204. if ('!' == nextChar)
  5205. {
  5206. readNext();
  5207. parseOther();
  5208. }
  5209. else if ('?' == nextChar)
  5210. parsePIOrDecl();
  5211. else
  5212. break;
  5213. if (!checkReadNext()) return false;
  5214. }
  5215. state = tagStart;
  5216. break;
  5217. }
  5218. case tagStart:
  5219. {
  5220. offset_t startOffset;
  5221. for (;;)
  5222. {
  5223. if ('!' != nextChar)
  5224. break;
  5225. parseComment();
  5226. readNext();
  5227. if ('<' != nextChar)
  5228. expecting("<");
  5229. }
  5230. startOffset = curOffset-2;
  5231. if (freeStateInfo.ordinality())
  5232. {
  5233. stateInfo = &freeStateInfo.popGet();
  5234. stateInfo->reset();
  5235. }
  5236. else
  5237. stateInfo = new CStateInfo;
  5238. stack.append(*stateInfo);
  5239. if ('/' == nextChar)
  5240. error("Unmatched close tag encountered");
  5241. while (!isspace(nextChar) && nextChar != '>')
  5242. {
  5243. stateInfo->tag.append(nextChar);
  5244. readNext();
  5245. if ('/' == nextChar) break;
  5246. if ('<' == nextChar)
  5247. error("Unmatched close tag encountered");
  5248. }
  5249. stateInfo->wnsTag = stateInfo->tag.str();
  5250. if (ignoreNameSpaces)
  5251. {
  5252. const char *colon;
  5253. if ((colon = strchr(stateInfo->wnsTag, ':')) != NULL)
  5254. stateInfo->wnsTag = colon+1;
  5255. }
  5256. endOfRoot = false;
  5257. try
  5258. {
  5259. iEvent->beginNode(stateInfo->wnsTag, false, startOffset);
  5260. }
  5261. catch (IPTreeException *pe)
  5262. {
  5263. if (PTreeExcpt_InvalidTagName == pe->errorCode())
  5264. {
  5265. pe->Release();
  5266. StringBuffer msg("Expecting valid start tag, but got \"");
  5267. error(msg.append(stateInfo->wnsTag).append("\"").str());
  5268. }
  5269. throw;
  5270. }
  5271. state = tagAttributes;
  5272. break;
  5273. }
  5274. case tagAttributes:
  5275. {
  5276. skipWS();
  5277. if (nextChar == '>')
  5278. state = tagContent;
  5279. else
  5280. {
  5281. skipWS();
  5282. if (nextChar=='/')
  5283. {
  5284. readNext();
  5285. if (nextChar != '>')
  5286. expecting(">");
  5287. // no actual content
  5288. iEvent->beginNodeContent(stateInfo->wnsTag);
  5289. state = tagEnd;
  5290. break;
  5291. }
  5292. attrName.setLength(1);
  5293. attrval.clear();
  5294. while (nextChar && !isspace(nextChar) && nextChar != '=' && nextChar != '>' && nextChar != '/')
  5295. {
  5296. attrName.append(nextChar);
  5297. readNext();
  5298. }
  5299. skipWS();
  5300. if (nextChar == '=') readNext(); else expecting("=");
  5301. skipWS();
  5302. if (nextChar == '"')
  5303. {
  5304. readNext();
  5305. while (nextChar != '"')
  5306. {
  5307. if (!nextChar)
  5308. eos();
  5309. attrval.append(nextChar);
  5310. readNext();
  5311. }
  5312. }
  5313. else if (nextChar == '\'')
  5314. {
  5315. readNext();
  5316. while (nextChar != '\'')
  5317. {
  5318. attrval.append(nextChar);
  5319. readNext();
  5320. }
  5321. }
  5322. else
  5323. error();
  5324. _decodeXML(0, attrval.str(), tmpStr.clear());
  5325. if (0 == strcmp(attrName.str(), "@xsi:type") &&
  5326. (0 == stricmp(tmpStr.str(),"SOAP-ENC:base64")))
  5327. stateInfo->base64 = true;
  5328. else
  5329. iEvent->newAttribute(attrName.str(), tmpStr.str());
  5330. readNext();
  5331. skipWS();
  5332. }
  5333. break;
  5334. }
  5335. case tagContent:
  5336. {
  5337. iEvent->beginNodeContent(stateInfo->wnsTag);
  5338. if ('>' != nextChar)
  5339. state = tagEnd;
  5340. else
  5341. state = tagContent2;
  5342. break;
  5343. }
  5344. case tagContent2:
  5345. {
  5346. try
  5347. {
  5348. for (;;)
  5349. {
  5350. if (endOfRoot)
  5351. {
  5352. if (!checkReadNext()) return false;
  5353. if (!checkSkipWS()) return false;
  5354. }
  5355. else
  5356. {
  5357. readNext();
  5358. if (ignoreWhiteSpace)
  5359. skipWS();
  5360. }
  5361. if ('\0' == nextChar)
  5362. eos();
  5363. mark.clear();
  5364. state = tagMarker;
  5365. while (nextChar && nextChar !='<') { mark.append(nextChar); readNext(); }
  5366. if (!nextChar)
  5367. break;
  5368. size32_t l = mark.length();
  5369. size32_t r = l+1;
  5370. if (l && stateInfo)
  5371. {
  5372. if (ignoreWhiteSpace)
  5373. {
  5374. const char *tb = mark.str();
  5375. const char *t = tb+l-1;
  5376. if (isspace(*t))
  5377. {
  5378. while (t != tb && isspace(*(--t)));
  5379. mark.setLength((size32_t)(t-tb+1));
  5380. }
  5381. }
  5382. stateInfo->tagText.ensureCapacity(mark.length());
  5383. _decodeXML(r, mark.str(), stateInfo->tagText);
  5384. }
  5385. if (endOfRoot && mark.length())
  5386. {
  5387. const char *m = mark.str();
  5388. const char *e = m+mark.length();
  5389. do { if (!isspace(*m++)) error("Trailing content after close of root tag"); }
  5390. while (m!=e);
  5391. }
  5392. readNext();
  5393. if ('!' == nextChar)
  5394. {
  5395. parseDirective(stateInfo->tagText);
  5396. state = tagContent2;
  5397. }
  5398. else if ('?' == nextChar)
  5399. {
  5400. parsePI(tmpStr.clear());
  5401. #ifdef STRICT_PI
  5402. if (0 == stricmp(tmpStr.str(), "xml"))
  5403. error("Reserved PI target used");
  5404. #endif
  5405. state = tagContent2;
  5406. }
  5407. else
  5408. break;
  5409. }
  5410. }
  5411. catch (IPTreeReadException *e)
  5412. {
  5413. if (endOfRoot && PTreeRead_EOS == e->errorCode() && (state != tagContent2 && mark.length())) // only to provide more meaningful error
  5414. {
  5415. const char *m = mark.str();
  5416. const char *es = m+mark.length();
  5417. do
  5418. {
  5419. if (!isspace(*m++))
  5420. {
  5421. e->Release();
  5422. error("Trailing content after close of root tag");
  5423. }
  5424. }
  5425. while (m!=es);
  5426. }
  5427. throw;
  5428. }
  5429. if ('/' == nextChar)
  5430. {
  5431. if (endOfRoot && !noRoot)
  5432. error("Trailing tag close after close of root tag");
  5433. if (stateInfo->base64)
  5434. {
  5435. JBASE64_Decode(stateInfo->tagText.str(), tmpStr.clear());
  5436. stateInfo->tagText.swapWith(tmpStr);
  5437. stateInfo->binary = true;
  5438. // next state tagContent2 still
  5439. }
  5440. else
  5441. {
  5442. if (strlen(stateInfo->tagText.str()) != stateInfo->tagText.length())
  5443. stateInfo->binary = true;
  5444. }
  5445. state = tagClose;
  5446. break; // exit
  5447. }
  5448. else
  5449. {
  5450. if (endOfRoot && !noRoot)
  5451. error("Trailing tag open after close of root tag");
  5452. state = tagStart;
  5453. }
  5454. break;
  5455. }
  5456. case tagClose:
  5457. {
  5458. readNext();
  5459. const char *t = stateInfo->tag.str();
  5460. const char *te = t+stateInfo->tag.length();
  5461. for (;;)
  5462. {
  5463. if (nextChar == '>' || isspace(nextChar))
  5464. {
  5465. if (t != te)
  5466. error("Mismatched opening and closing tags");
  5467. break;
  5468. }
  5469. else if (nextChar != *t++)
  5470. error("Mismatched opening and closing tags");
  5471. readNext();
  5472. }
  5473. skipWS();
  5474. if (nextChar != '>')
  5475. expecting(">");
  5476. state = tagEnd;
  5477. break;
  5478. }
  5479. case tagEnd:
  5480. {
  5481. iEvent->endNode(stateInfo->wnsTag, stateInfo->tagText.length(), stateInfo->tagText.str(), stateInfo->binary, curOffset);
  5482. freeStateInfo.append(*stateInfo);
  5483. stack.pop();
  5484. endOfRoot = 0==stack.ordinality();
  5485. stateInfo = stack.ordinality()?&stack.tos():NULL;
  5486. if (endOfRoot && noRoot)
  5487. {
  5488. state = headerStart;
  5489. hadXMLDecl = false;
  5490. endOfRoot = false;
  5491. }
  5492. else
  5493. state = tagContent2;
  5494. break;
  5495. }
  5496. }
  5497. return true;
  5498. }
  5499. };
  5500. IPTreeReader *createXMLStreamReader(ISimpleReadStream &stream, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions, size32_t bufSize)
  5501. {
  5502. class CXMLStreamReader : public CXMLReader<CInstStreamReader>
  5503. {
  5504. public:
  5505. CXMLStreamReader(ISimpleReadStream &stream, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions, size32_t bufSize=0) : CXMLReader<CInstStreamReader>(stream, iEvent, xmlReaderOptions, bufSize) { }
  5506. };
  5507. return new CXMLStreamReader(stream, iEvent, xmlReaderOptions, bufSize);
  5508. }
  5509. IPTreeReader *createXMLStringReader(const char *xml, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions)
  5510. {
  5511. class CXMLStringReader : public CXMLReader<CInstStringReader>
  5512. {
  5513. public:
  5514. CXMLStringReader(const void *xml, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions) : CXMLReader<CInstStringReader>(xml, iEvent, xmlReaderOptions) { }
  5515. };
  5516. if (NULL == xml)
  5517. throw createPTreeReadException(PTreeRead_syntax, "Null string passed to createXMLStringReader", NULL, 0, 0);
  5518. return new CXMLStringReader(xml, iEvent, xmlReaderOptions);
  5519. }
  5520. IPTreeReader *createXMLBufferReader(const void *buf, size32_t bufLength, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions)
  5521. {
  5522. class CXMLBufferReader : public CXMLReader<CInstBufferReader>
  5523. {
  5524. public:
  5525. CXMLBufferReader(const void *buf, size32_t bufLength, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions) : CXMLReader<CInstBufferReader>(buf, bufLength, iEvent, xmlReaderOptions) { }
  5526. };
  5527. return new CXMLBufferReader(buf, bufLength, iEvent, xmlReaderOptions);
  5528. }
  5529. IPullPTreeReader *createPullXMLStreamReader(ISimpleReadStream &stream, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions, size32_t bufSize)
  5530. {
  5531. class CXMLStreamReader : public CPullXMLReader<CInstStreamReader>
  5532. {
  5533. public:
  5534. CXMLStreamReader(ISimpleReadStream &stream, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions, size32_t bufSize=0) : CPullXMLReader<CInstStreamReader>(stream, iEvent, xmlReaderOptions, bufSize) { }
  5535. };
  5536. return new CXMLStreamReader(stream, iEvent, xmlReaderOptions, bufSize);
  5537. }
  5538. IPullPTreeReader *createPullXMLStringReader(const char *xml, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions)
  5539. {
  5540. class CXMLStringReader : public CPullXMLReader<CInstStringReader>
  5541. {
  5542. public:
  5543. CXMLStringReader(const void *xml, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions) : CPullXMLReader<CInstStringReader>(xml, iEvent, xmlReaderOptions) { }
  5544. };
  5545. return new CXMLStringReader(xml, iEvent, xmlReaderOptions);
  5546. }
  5547. IPullPTreeReader *createPullXMLBufferReader(const void *buf, size32_t bufLength, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions)
  5548. {
  5549. class CXMLBufferReader : public CPullXMLReader<CInstBufferReader>
  5550. {
  5551. public:
  5552. CXMLBufferReader(const void *buf, size32_t bufLength, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions) : CPullXMLReader<CInstBufferReader>(buf, bufLength, iEvent, xmlReaderOptions) { }
  5553. };
  5554. return new CXMLBufferReader(buf, bufLength, iEvent, xmlReaderOptions);
  5555. }
  5556. IPTreeMaker *createPTreeMaker(byte flags, IPropertyTree *root, IPTreeNodeCreator *nodeCreator)
  5557. {
  5558. return new CPTreeMaker(flags, nodeCreator, root);
  5559. }
  5560. IPTreeMaker *createRootLessPTreeMaker(byte flags, IPropertyTree *root, IPTreeNodeCreator *nodeCreator)
  5561. {
  5562. return new CPTreeMaker(flags, nodeCreator, root, true);
  5563. }
  5564. ////////////////////////////
  5565. ///////////////////////////
  5566. static IPTreeMaker *createDefaultPTreeMaker(byte flags, PTreeReaderOptions readFlags)
  5567. {
  5568. bool noRoot = 0 != ((unsigned)readFlags & (unsigned)ptr_noRoot);
  5569. return new CPTreeMaker(flags, NULL, NULL, noRoot);
  5570. }
  5571. IPropertyTree *createPTree(ISimpleReadStream &stream, byte flags, PTreeReaderOptions readFlags, IPTreeMaker *iMaker)
  5572. {
  5573. Owned<IPTreeMaker> _iMaker;
  5574. if (!iMaker)
  5575. {
  5576. iMaker = createDefaultPTreeMaker(flags, readFlags);
  5577. _iMaker.setown(iMaker);
  5578. }
  5579. Owned<IPTreeReader> reader = createXMLStreamReader(stream, *iMaker, readFlags);
  5580. reader->load();
  5581. if (iMaker->queryRoot())
  5582. return LINK(iMaker->queryRoot());
  5583. else
  5584. return iMaker->create(NULL);
  5585. }
  5586. IPropertyTree *createPTree(IFileIO &ifileio, byte flags, PTreeReaderOptions readFlags, IPTreeMaker *iMaker)
  5587. {
  5588. OwnedIFileIOStream stream = createIOStream(&ifileio);
  5589. return createPTree(*stream, flags, readFlags, iMaker);
  5590. }
  5591. IPropertyTree *createPTree(IFile &ifile, byte flags, PTreeReaderOptions readFlags, IPTreeMaker *iMaker)
  5592. {
  5593. OwnedIFileIO ifileio = ifile.open(IFOread);
  5594. if (!ifileio)
  5595. throw MakeStringException(0, "Could not locate filename: %s", ifile.queryFilename());
  5596. return createPTree(*ifileio, flags, readFlags, iMaker);
  5597. }
  5598. IPropertyTree *createPTreeFromXMLFile(const char *filename, byte flags, PTreeReaderOptions readFlags, IPTreeMaker *iMaker)
  5599. {
  5600. OwnedIFile ifile = createIFile(filename);
  5601. return createPTree(*ifile, flags, readFlags, iMaker);
  5602. }
  5603. IPropertyTree *createPTreeFromXMLString(const char *xml, byte flags, PTreeReaderOptions readFlags, IPTreeMaker *iMaker)
  5604. {
  5605. Owned<IPTreeMaker> _iMaker;
  5606. if (!iMaker)
  5607. {
  5608. iMaker = createDefaultPTreeMaker(flags, readFlags);
  5609. _iMaker.setown(iMaker);
  5610. }
  5611. Owned<IPTreeReader> reader = createXMLStringReader(xml, *iMaker, readFlags);
  5612. reader->load();
  5613. return LINK(iMaker->queryRoot());
  5614. }
  5615. IPropertyTree *createPTreeFromXMLString(unsigned len, const char *xml, byte flags, PTreeReaderOptions readFlags, IPTreeMaker *iMaker)
  5616. {
  5617. Owned<IPTreeMaker> _iMaker;
  5618. if (!iMaker)
  5619. {
  5620. iMaker = createDefaultPTreeMaker(flags, readFlags);
  5621. _iMaker.setown(iMaker);
  5622. }
  5623. Owned<IPTreeReader> reader = createXMLBufferReader(xml, len, *iMaker, readFlags);
  5624. reader->load();
  5625. return LINK(iMaker->queryRoot());
  5626. }
  5627. //////////////////////////
  5628. /////////////////////////
  5629. inline bool isHiddenWhenSanitized(const char *val)
  5630. {
  5631. if (!val || !*val)
  5632. return false;
  5633. return !(streq(val, "0") || streq(val, "1") || strieq(val, "true") || strieq(val, "false") || strieq(val, "yes") || strieq(val, "no"));
  5634. }
  5635. inline bool isSanitizedAndHidden(const char *val, byte flags, bool attribute)
  5636. {
  5637. bool sanitize = (attribute) ? ((flags & YAML_SanitizeAttributeValues)!=0) : ((flags & YAML_Sanitize)!=0);
  5638. if (sanitize)
  5639. return isHiddenWhenSanitized(val);
  5640. return false;
  5641. }
  5642. static void _toXML(const IPropertyTree *tree, IIOStream &out, unsigned indent, unsigned flags)
  5643. {
  5644. const char *name = tree->queryName();
  5645. if (!name) name = "__unnamed__";
  5646. bool isBinary = tree->isBinary(NULL);
  5647. bool inlinebody = true;
  5648. if (flags & XML_Embed) writeCharsNToStream(out, ' ', indent);
  5649. writeCharToStream(out, '<');
  5650. writeStringToStream(out, name);
  5651. Owned<IAttributeIterator> it = tree->getAttributes(true);
  5652. if (it->first())
  5653. {
  5654. unsigned attributeindent = indent+2+(size32_t)strlen(name);
  5655. bool first = true;
  5656. do
  5657. {
  5658. const char *key = it->queryName();
  5659. if (!isBinary || stricmp(key, "@xsi:type")!=0)
  5660. {
  5661. if (first)
  5662. {
  5663. if (flags & XML_LineBreak) inlinebody = false;
  5664. first = false;
  5665. writeCharToStream(out, ' ');
  5666. }
  5667. else if ((flags & XML_LineBreakAttributes) && it->count() > 3)
  5668. {
  5669. writeStringToStream(out, "\n");
  5670. writeCharsNToStream(out, ' ', attributeindent);
  5671. }
  5672. else
  5673. writeCharToStream(out, ' ');
  5674. writeStringToStream(out, key+1);
  5675. if (flags & XML_SingleQuoteAttributeValues)
  5676. writeStringToStream(out, "='");
  5677. else
  5678. writeStringToStream(out, "=\"");
  5679. const char *val = it->queryValue();
  5680. if (val)
  5681. {
  5682. if (isSanitizedAndHidden(val, flags, true))
  5683. writeCharsNToStream(out, '*', strlen(val));
  5684. else
  5685. encodeXML(val, out, ENCODE_NEWLINES, (unsigned)-1, true);
  5686. }
  5687. if (flags & XML_SingleQuoteAttributeValues)
  5688. writeCharToStream(out, '\'');
  5689. else
  5690. writeCharToStream(out, '"');
  5691. }
  5692. }
  5693. while (it->next());
  5694. }
  5695. Owned<IPropertyTreeIterator> sub = tree->getElements("*", 0 != (flags & XML_SortTags) ? iptiter_sort : iptiter_null);
  5696. MemoryBuffer thislevelbin;
  5697. StringBuffer _thislevel;
  5698. const char *thislevel = NULL; // to avoid uninitialized warning
  5699. bool empty;
  5700. if (isBinary)
  5701. {
  5702. if (flags & XML_LineBreak) inlinebody = false;
  5703. writeStringToStream(out, " xsi:type=\"SOAP-ENC:base64\"");
  5704. empty = (!tree->getPropBin(NULL, thislevelbin))||(thislevelbin.length()==0);
  5705. }
  5706. else
  5707. {
  5708. if (tree->isCompressed(NULL))
  5709. {
  5710. empty = false; // can't be empty if compressed;
  5711. verifyex(tree->getProp(NULL, _thislevel));
  5712. thislevel = _thislevel.str();
  5713. }
  5714. else
  5715. empty = (NULL == (thislevel = tree->queryProp(NULL)));
  5716. }
  5717. if (sub->first())
  5718. {
  5719. if (flags & XML_LineBreak) inlinebody = false;
  5720. }
  5721. else if (empty && !(flags & XML_Sanitize))
  5722. {
  5723. if (flags & XML_LineBreak)
  5724. writeStringToStream(out, "/>\n");
  5725. else
  5726. writeStringToStream(out, "/>");
  5727. return;
  5728. }
  5729. writeCharToStream(out, '>');
  5730. if (!inlinebody)
  5731. writeStringToStream(out, "\n");
  5732. for(; sub->isValid(); sub->next())
  5733. _toXML(&sub->query(), out, indent+1, flags);
  5734. if (!empty)
  5735. {
  5736. if (!inlinebody)
  5737. writeCharsNToStream(out, ' ', indent+1);
  5738. if (flags & XML_Sanitize)
  5739. {
  5740. // NOTE - we don't output anything for binary.... is that ok?
  5741. if (thislevel)
  5742. {
  5743. if (isHiddenWhenSanitized(thislevel))
  5744. writeCharsNToStream(out, '*', strlen(thislevel));
  5745. else
  5746. writeStringToStream(out, thislevel);
  5747. }
  5748. }
  5749. else if (isBinary)
  5750. {
  5751. if (flags & XML_NoBinaryEncode64)
  5752. {
  5753. if (flags & XML_NoEncode)
  5754. {
  5755. out.write(thislevelbin.length(), thislevelbin.toByteArray());
  5756. }
  5757. else
  5758. {
  5759. const char * buff = static_cast<const char *>(thislevelbin.toByteArray());
  5760. const unsigned len = thislevelbin.length();
  5761. unsigned prefix = 0;
  5762. while ((prefix < len) && isspace(buff[prefix]))
  5763. prefix++;
  5764. encodeXML(buff, out, ENCODE_WHITESPACE, prefix, true);
  5765. if (prefix != len) { // check not all spaces
  5766. unsigned suffix = len;
  5767. while (isspace(buff[suffix-1]))
  5768. suffix--;
  5769. encodeXML(buff+prefix, out, 0, suffix-prefix, true);
  5770. encodeXML(buff+suffix, out, ENCODE_WHITESPACE, len-suffix, true);
  5771. }
  5772. }
  5773. }
  5774. else
  5775. JBASE64_Encode(thislevelbin.toByteArray(), thislevelbin.length(), out, true);
  5776. }
  5777. else
  5778. {
  5779. if (flags & XML_NoEncode)
  5780. {
  5781. writeStringToStream(out, thislevel);
  5782. }
  5783. else
  5784. {
  5785. const char *m = thislevel;
  5786. const char *p = m;
  5787. while (isspace(*p))
  5788. p++;
  5789. encodeXML(m, out, ENCODE_WHITESPACE, p-m, true);
  5790. if (*p) { // check not all spaces
  5791. const char *s = p+strlen(p);
  5792. while (isspace(*(s-1)))
  5793. s--;
  5794. assertex(s>p);
  5795. encodeXML(p, out, 0, s-p, true);
  5796. encodeXML(s, out, ENCODE_WHITESPACE, (unsigned)-1, true);
  5797. }
  5798. }
  5799. if (!inlinebody)
  5800. writeStringToStream(out, "\n");
  5801. }
  5802. }
  5803. if (!inlinebody)
  5804. writeCharsNToStream(out, ' ', indent);
  5805. writeStringToStream(out, "</");
  5806. writeStringToStream(out, name);
  5807. if (flags & XML_LineBreak)
  5808. writeStringToStream(out, ">\n");
  5809. else
  5810. writeCharToStream(out, '>');
  5811. }
  5812. class CStringBufferMarkupIOAdapter : public CInterfaceOf<IIOStream>
  5813. {
  5814. StringBuffer &out;
  5815. public:
  5816. CStringBufferMarkupIOAdapter(StringBuffer &_out) : out(_out) { }
  5817. virtual void flush() override { }
  5818. virtual size32_t read(size32_t len, void * data) override { UNIMPLEMENTED; return 0; }
  5819. virtual size32_t write(size32_t len, const void * data) override { out.append(len, (const char *)data); return len; }
  5820. };
  5821. jlib_decl StringBuffer &toXML(const IPropertyTree *tree, StringBuffer &ret, unsigned indent, unsigned flags)
  5822. {
  5823. CStringBufferMarkupIOAdapter adapter(ret);
  5824. _toXML(tree->queryBranch(NULL), adapter, indent, flags);
  5825. return ret;
  5826. }
  5827. void toXML(const IPropertyTree *tree, IIOStream &out, unsigned indent, unsigned flags)
  5828. {
  5829. _toXML(tree, out, indent, flags);
  5830. }
  5831. void printXML(const IPropertyTree *tree, unsigned indent, unsigned flags)
  5832. {
  5833. StringBuffer xml;
  5834. toXML(tree, xml, indent, flags);
  5835. printf("%s", xml.str());
  5836. }
  5837. void dbglogXML(const IPropertyTree *tree, unsigned indent, unsigned flags)
  5838. {
  5839. StringBuffer xml;
  5840. toXML(tree, xml, indent, flags);
  5841. DBGLOG("%s", xml.str());
  5842. }
  5843. void saveXML(const char *filename, const IPropertyTree *tree, unsigned indent, unsigned flags)
  5844. {
  5845. OwnedIFile ifile = createIFile(filename);
  5846. saveXML(*ifile, tree, indent, flags);
  5847. }
  5848. void saveXML(IFile &ifile, const IPropertyTree *tree, unsigned indent, unsigned flags)
  5849. {
  5850. OwnedIFileIO ifileio = ifile.open(IFOcreate);
  5851. if (!ifileio)
  5852. throw MakeStringException(0, "saveXML: could not find %s to open", ifile.queryFilename());
  5853. saveXML(*ifileio, tree, indent, flags);
  5854. }
  5855. void saveXML(IFileIO &ifileio, const IPropertyTree *tree, unsigned indent, unsigned flags)
  5856. {
  5857. Owned<IIOStream> stream = createIOStream(&ifileio);
  5858. stream.setown(createBufferedIOStream(stream));
  5859. saveXML(*stream, tree, indent, flags);
  5860. }
  5861. void saveXML(IIOStream &stream, const IPropertyTree *tree, unsigned indent, unsigned flags)
  5862. {
  5863. toXML(tree, stream, indent, flags);
  5864. }
  5865. /////////////////////////
  5866. void checkWriteJSONDelimiter(IIOStream &out, bool &delimit)
  5867. {
  5868. if (delimit)
  5869. writeCharToStream(out, ',');
  5870. delimit = false;
  5871. }
  5872. static void writeJSONNameToStream(IIOStream &out, const char *name, unsigned indent, bool &delimit)
  5873. {
  5874. if (!name || !*name)
  5875. return;
  5876. checkWriteJSONDelimiter(out, delimit);
  5877. if (indent)
  5878. {
  5879. writeCharToStream(out, '\n');
  5880. writeCharsNToStream(out, ' ', indent);
  5881. }
  5882. else
  5883. writeCharToStream(out, ' ');
  5884. writeCharToStream(out, '"');
  5885. writeStringToStream(out, name);
  5886. writeStringToStream(out, "\": ");
  5887. delimit = false;
  5888. }
  5889. static void writeJSONValueToStream(IIOStream &out, const char *val, bool &delimit, bool hidden=false)
  5890. {
  5891. checkWriteJSONDelimiter(out, delimit);
  5892. delimit = true;
  5893. if (!val)
  5894. {
  5895. writeStringToStream(out, "null");
  5896. return;
  5897. }
  5898. writeCharToStream(out, '"');
  5899. if (hidden)
  5900. writeCharsNToStream(out, '*', strlen(val));
  5901. else
  5902. {
  5903. StringBuffer s;
  5904. writeStringToStream(out, encodeJSON(s, val));
  5905. }
  5906. writeCharToStream(out, '"');
  5907. }
  5908. static void writeJSONBase64ValueToStream(IIOStream &out, const char *val, size32_t len, bool &delimit, bool hidden)
  5909. {
  5910. checkWriteJSONDelimiter(out, delimit);
  5911. delimit = true;
  5912. if (!val)
  5913. {
  5914. writeStringToStream(out, "null");
  5915. return;
  5916. }
  5917. writeCharToStream(out, '"');
  5918. if (hidden)
  5919. JBASE64_Encode("****", strlen("****"), out, false);
  5920. else
  5921. JBASE64_Encode(val, len, out, false);
  5922. writeCharToStream(out, '"');
  5923. }
  5924. bool isRootArrayObjectHidden(bool root, const char *name, byte flags)
  5925. {
  5926. return ((flags & JSON_HideRootArrayObject) && root && name && streq(name,"__array__"));
  5927. }
  5928. static void _toJSON(const IPropertyTree *tree, IIOStream &out, unsigned indent, byte flags, bool &delimit, bool root=false, bool isArrayItem=false)
  5929. {
  5930. Owned<IAttributeIterator> it = tree->getAttributes(true);
  5931. bool hasAttributes = it->first();
  5932. bool complex = (hasAttributes || tree->hasChildren() || tree->isBinary());
  5933. bool isBinary = tree->isBinary(NULL);
  5934. const char *name = tree->queryName();
  5935. if (!root && !isArrayItem)
  5936. {
  5937. if (!name || !*name)
  5938. name = "__unnamed__";
  5939. writeJSONNameToStream(out, name, (flags & JSON_Format) ? indent : 0, delimit);
  5940. }
  5941. checkWriteJSONDelimiter(out, delimit);
  5942. if (isArrayItem && (flags & JSON_Format))
  5943. {
  5944. writeCharToStream(out, '\n');
  5945. writeCharsNToStream(out, ' ', indent);
  5946. }
  5947. bool hiddenRootArrayObject = isRootArrayObjectHidden(root, name, flags);
  5948. if (!hiddenRootArrayObject)
  5949. {
  5950. if (root || complex)
  5951. {
  5952. writeCharToStream(out, '{');
  5953. delimit = false;
  5954. }
  5955. if (hasAttributes)
  5956. {
  5957. ForEach(*it)
  5958. {
  5959. const char *key = it->queryName();
  5960. if (!isBinary || stricmp(key, "@xsi:type")!=0)
  5961. {
  5962. const char *val = it->queryValue();
  5963. if (val)
  5964. {
  5965. writeJSONNameToStream(out, key, (flags & JSON_Format) ? indent+1 : 0, delimit);
  5966. if (flags & JSON_SanitizeAttributeValues)
  5967. writeJSONValueToStream(out, val, delimit, isHiddenWhenSanitized(val));
  5968. else
  5969. {
  5970. StringBuffer encoded;
  5971. encodeJSON(encoded, val);
  5972. writeJSONValueToStream(out, encoded.str(), delimit);
  5973. }
  5974. }
  5975. }
  5976. }
  5977. }
  5978. }
  5979. MemoryBuffer thislevelbin;
  5980. StringBuffer _thislevel;
  5981. const char *thislevel = NULL; // to avoid uninitialized warning
  5982. bool isNull = true;
  5983. if (!hiddenRootArrayObject)
  5984. {
  5985. if (isBinary)
  5986. {
  5987. isNull = (!tree->getPropBin(NULL, thislevelbin))||(thislevelbin.length()==0);
  5988. }
  5989. else
  5990. {
  5991. if (tree->isCompressed(NULL))
  5992. {
  5993. isNull = false; // can't be empty if compressed;
  5994. verifyex(tree->getProp(NULL, _thislevel));
  5995. thislevel = _thislevel.str();
  5996. }
  5997. else
  5998. isNull = (NULL == (thislevel = tree->queryProp(NULL)));
  5999. }
  6000. if (isNull && !root && !complex)
  6001. {
  6002. writeJSONValueToStream(out, NULL, delimit);
  6003. return;
  6004. }
  6005. }
  6006. Owned<IPropertyTreeIterator> sub = tree->getElements(hiddenRootArrayObject ? "__item__" : "*", 0 != (flags & JSON_SortTags) ? iptiter_sort : iptiter_null);
  6007. //note that detection of repeating elements relies on the fact that ptree elements
  6008. //of the same name will be grouped together
  6009. bool repeatingElement = false;
  6010. sub->first();
  6011. while(sub->isValid())
  6012. {
  6013. Linked<IPropertyTree> element = &sub->query();
  6014. const char *name = element->queryName();
  6015. sub->next();
  6016. if (!repeatingElement)
  6017. {
  6018. if (hiddenRootArrayObject)
  6019. {
  6020. writeCharToStream(out, '[');
  6021. repeatingElement = true;
  6022. delimit = false;
  6023. }
  6024. else if (sub->isValid() && streq(name, sub->query().queryName()))
  6025. {
  6026. if (flags & JSON_Format)
  6027. indent++;
  6028. writeJSONNameToStream(out, name, (flags & JSON_Format) ? indent : 0, delimit);
  6029. writeCharToStream(out, '[');
  6030. repeatingElement = true;
  6031. delimit = false;
  6032. }
  6033. }
  6034. _toJSON(element, out, indent+1, flags, delimit, false, repeatingElement);
  6035. if (repeatingElement && (!sub->isValid() || !streq(name, sub->query().queryName())))
  6036. {
  6037. if (flags & JSON_Format)
  6038. {
  6039. writeCharToStream(out, '\n');
  6040. writeCharsNToStream(out, ' ', indent);
  6041. indent--;
  6042. }
  6043. writeCharToStream(out, ']');
  6044. repeatingElement = false;
  6045. delimit = true;
  6046. }
  6047. }
  6048. if (!hiddenRootArrayObject && !isNull)
  6049. {
  6050. if (complex)
  6051. writeJSONNameToStream(out, isBinary ? "#valuebin" : "#value", (flags & JSON_Format) ? indent+1 : 0, delimit);
  6052. if (isBinary)
  6053. writeJSONBase64ValueToStream(out, thislevelbin.toByteArray(), thislevelbin.length(), delimit, flags & XML_Sanitize);
  6054. else
  6055. {
  6056. writeJSONValueToStream(out, thislevel, delimit, isSanitizedAndHidden(thislevel, flags, false));
  6057. }
  6058. }
  6059. if (!hiddenRootArrayObject)
  6060. {
  6061. if (root || complex)
  6062. {
  6063. if (flags & JSON_Format)
  6064. {
  6065. writeCharToStream(out, '\n');
  6066. writeCharsNToStream(out, ' ', indent);
  6067. }
  6068. writeCharToStream(out, '}');
  6069. delimit = true;
  6070. }
  6071. }
  6072. }
  6073. jlib_decl StringBuffer &toJSON(const IPropertyTree *tree, StringBuffer &ret, unsigned indent, byte flags)
  6074. {
  6075. CStringBufferMarkupIOAdapter adapter(ret);
  6076. bool delimit = false;
  6077. _toJSON(tree->queryBranch(NULL), adapter, indent, flags, delimit, true);
  6078. return ret;
  6079. }
  6080. void toJSON(const IPropertyTree *tree, IIOStream &out, unsigned indent, byte flags)
  6081. {
  6082. bool delimit = false;
  6083. _toJSON(tree, out, indent, flags, delimit, true);
  6084. }
  6085. void printJSON(const IPropertyTree *tree, unsigned indent, byte flags)
  6086. {
  6087. StringBuffer json;
  6088. toJSON(tree, json, indent, flags);
  6089. printf("%s", json.str());
  6090. }
  6091. void dbglogJSON(const IPropertyTree *tree, unsigned indent, unsigned flags)
  6092. {
  6093. StringBuffer json;
  6094. toJSON(tree, json, indent, flags);
  6095. DBGLOG("%s", json.str());
  6096. }
  6097. static inline void skipWS(const char *&xpath)
  6098. {
  6099. while (isspace(*xpath)) xpath++;
  6100. }
  6101. static void _validateXPathSyntax(const char *xpath);
  6102. static void validateQualifier(const char *&xxpath)
  6103. {
  6104. const char *xpath = xxpath;
  6105. const char *start = xpath;
  6106. skipWS(xpath);
  6107. const char *lhsStart = xpath;
  6108. for (;;)
  6109. {
  6110. switch (*xpath) {
  6111. case ']':
  6112. case '!':
  6113. case '=':
  6114. case '\0':
  6115. break;
  6116. default:
  6117. if (!isspace(*xpath))
  6118. {
  6119. xpath++;
  6120. continue;
  6121. }
  6122. }
  6123. break;
  6124. }
  6125. StringAttr lhs(lhsStart, xpath-lhsStart);
  6126. _validateXPathSyntax(lhs);
  6127. skipWS(xpath);
  6128. exprType tType = t_none;
  6129. if ('=' == *xpath)
  6130. {
  6131. ++xpath;
  6132. tType = t_equality;
  6133. }
  6134. else if ('!' == *xpath)
  6135. {
  6136. ++xpath;
  6137. if (*xpath && '=' == *xpath)
  6138. {
  6139. tType = t_inequality;
  6140. ++xpath;
  6141. }
  6142. else
  6143. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, 0, "Invalid qualifier expression");
  6144. }
  6145. if (t_none != tType)
  6146. {
  6147. skipWS(xpath);
  6148. if ('~' == *xpath)
  6149. {
  6150. ++xpath; // Signifies wild (now always true but still accepted...)
  6151. }
  6152. skipWS(xpath);
  6153. char qu = *xpath;
  6154. if (qu != '\'' && qu != '\"')
  6155. throw MakeXPathException(xpath, PTreeExcpt_XPath_ParseError, 0, "Syntax error - no opening \" or \'");
  6156. ++xpath;
  6157. while (*xpath && *xpath != qu)
  6158. xpath++;
  6159. if (!*xpath)
  6160. throw MakeXPathException(xpath, PTreeExcpt_XPath_ParseError, 0, "Syntax error - no closing \" or \'");
  6161. xpath++;
  6162. }
  6163. skipWS(xpath);
  6164. if (']' != *xpath)
  6165. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, 0, "No closing brace to qualifier");
  6166. xxpath = xpath;
  6167. }
  6168. static void _validateXPathSyntax(const char *xpath)
  6169. {
  6170. if (NULL == xpath || '\0' == *xpath)
  6171. return;
  6172. else
  6173. {
  6174. const char *_xpath = xpath;
  6175. restart:
  6176. if (NULL == xpath || '\0' == *xpath)
  6177. return;
  6178. switch (*xpath)
  6179. {
  6180. case '.':
  6181. ++xpath;
  6182. goto restart;
  6183. case '/':
  6184. ++xpath;
  6185. if ('/' == *xpath)
  6186. {
  6187. _validateXPathSyntax(xpath+1);
  6188. return;
  6189. }
  6190. goto restart;
  6191. case '[':
  6192. {
  6193. ++xpath;
  6194. if (isdigit(*xpath))
  6195. {
  6196. StringAttr index;
  6197. xpath = readIndex(xpath, index);
  6198. unsigned i = atoi(index.get());
  6199. if (i)
  6200. {
  6201. }
  6202. else
  6203. {
  6204. // should be syntax error
  6205. }
  6206. if (']' != *xpath)
  6207. throw MakeXPathException(_xpath, PTreeExcpt_XPath_ParseError, xpath-_xpath, "Qualifier brace unclosed");
  6208. }
  6209. else
  6210. validateQualifier(xpath);
  6211. ++xpath;
  6212. break;
  6213. }
  6214. default:
  6215. {
  6216. bool wild;
  6217. const char *start = xpath;
  6218. readWildId(xpath, wild); // validates also
  6219. size32_t s = xpath-start;
  6220. if (s)
  6221. {
  6222. StringAttr id(start, s);
  6223. if ('[' == *xpath) // check for local index not iterative qualifier.
  6224. {
  6225. const char *xxpath = xpath+1;
  6226. if (isdigit(*xxpath))
  6227. {
  6228. StringAttr idxstr;
  6229. xxpath = readIndex(xxpath, idxstr);
  6230. if (']' != *xxpath)
  6231. throw MakeXPathException(_xpath, PTreeExcpt_XPath_ParseError, xpath-_xpath, "Qualifier brace unclosed");
  6232. ++xxpath;
  6233. unsigned index = atoi(idxstr.get());
  6234. if (index)
  6235. {
  6236. }
  6237. xpath = xxpath;
  6238. }
  6239. }
  6240. }
  6241. else if ('@' == *xpath)
  6242. {
  6243. ++xpath;
  6244. const char *start = xpath;
  6245. readID(xpath, false);
  6246. size32_t s = xpath-start;
  6247. if (!s)
  6248. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Missing attribute?");
  6249. StringAttr id(start, s);
  6250. if (!validateXMLTag(id))
  6251. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Invalid xml tag: %s", id.get());
  6252. while (isspace(*xpath)) xpath++;
  6253. if ('\0' != *xpath)
  6254. throw MakeXPathException(start, PTreeExcpt_XPath_ParseError, xpath-start, "Cannot have embedded attribute within path (must be tail component)");
  6255. }
  6256. else
  6257. {
  6258. if ('[' != *xpath)
  6259. throw MakeXPathException(xpath, PTreeExcpt_XPath_ParseError, 0, "Qualifier expected e.g. [..]");
  6260. validateQualifier(xpath);
  6261. }
  6262. break;
  6263. }
  6264. }
  6265. }
  6266. if (*xpath == '\0' || (*xpath == '/' && '\0' == *(xpath+1)))
  6267. return;
  6268. else
  6269. _validateXPathSyntax(xpath);
  6270. }
  6271. bool validateXPathSyntax(const char *xpath, StringBuffer *error)
  6272. {
  6273. try
  6274. {
  6275. if (xpath && '/' == *xpath && *(xpath+1) != '/')
  6276. throw MakeXPathException(xpath, PTreeExcpt_XPath_Unsupported, 0, "Root specifier \"/\" specifier is not supported");
  6277. _validateXPathSyntax(xpath);
  6278. return true;
  6279. }
  6280. catch (IException *e)
  6281. {
  6282. if (error)
  6283. e->errorMessage(*error);
  6284. e->Release();
  6285. return false;
  6286. }
  6287. }
  6288. static bool isContentXPath(const char *xpath, StringBuffer &head)
  6289. {
  6290. unsigned l = xpath?strlen(xpath):0;
  6291. const char *x = xpath+l-2;
  6292. if (l>=2 && 0==strcmp(XMLTAG_CONTENT, x))
  6293. {
  6294. if (x != xpath)
  6295. head.append(x-xpath, xpath);
  6296. return true;
  6297. }
  6298. return false;
  6299. }
  6300. bool validateXMLParseXPath(const char *xpath, StringBuffer *error)
  6301. {
  6302. if (!xpath || !*xpath)
  6303. return true;
  6304. StringBuffer head;
  6305. if (isContentXPath(xpath, head))
  6306. {
  6307. if (head.length())
  6308. {
  6309. if ('/' == *xpath && '/' != *(xpath+1))
  6310. {
  6311. if (error)
  6312. {
  6313. Owned<IException> e = MakeStringException(0, "Invalid extract xml text '<>' usage, xpath cannot from be absolute: %s", xpath);
  6314. e->errorMessage(*error);
  6315. }
  6316. return false;
  6317. }
  6318. return validateXPathSyntax(head.str(), error);
  6319. }
  6320. return true;
  6321. }
  6322. else
  6323. return validateXPathSyntax('/' == *xpath && '/' != *(xpath+1) ? xpath+1 : xpath, error);
  6324. return true;
  6325. }
  6326. bool areMatchingPTrees(const IPropertyTree * left, const IPropertyTree * right)
  6327. {
  6328. if (left == right)
  6329. return true;
  6330. if (!left || !right)
  6331. return false;
  6332. bool isCaseInsensitive = left->isCaseInsensitive();
  6333. const char * lname = left->queryName();
  6334. const char * rname = right->queryName();
  6335. if (!lname || !rname)
  6336. {
  6337. if (lname || rname)
  6338. return false;
  6339. }
  6340. else if ((isCaseInsensitive ? stricmp(lname, rname) : strcmp(lname, rname)) != 0)
  6341. return false;
  6342. Owned<IAttributeIterator> leftAttrIter = left->getAttributes(true);
  6343. Owned<IAttributeIterator> rightAttrIter = right->getAttributes(true);
  6344. rightAttrIter->first();
  6345. ForEach(*leftAttrIter)
  6346. {
  6347. if (!rightAttrIter->isValid()) return false;
  6348. const char * lname = leftAttrIter->queryName();
  6349. const char * rname = rightAttrIter->queryName();
  6350. if ((isCaseInsensitive ? stricmp(lname, rname) : strcmp(lname, rname)) != 0)
  6351. return false;
  6352. if (strcmp(leftAttrIter->queryValue(), rightAttrIter->queryValue()) != 0)
  6353. return false;
  6354. rightAttrIter->next();
  6355. }
  6356. if (rightAttrIter->isValid()) return false;
  6357. Owned<IPropertyTreeIterator> leftElemIter = left->getElements("*", iptiter_sort);
  6358. Owned<IPropertyTreeIterator> rightElemIter = right->getElements("*", iptiter_sort);
  6359. rightElemIter->first();
  6360. ForEach(*leftElemIter)
  6361. {
  6362. if (!rightElemIter->isValid()) return false;
  6363. if (!areMatchingPTrees(&leftElemIter->query(), &rightElemIter->query()))
  6364. return false;
  6365. rightElemIter->next();
  6366. }
  6367. if (rightElemIter->isValid()) return false;
  6368. return true;
  6369. }
  6370. /////////////////////
  6371. static const char * skipWhitespace(const char * text)
  6372. {
  6373. while ((*text==' ') || (*text=='\t'))
  6374. text++;
  6375. return text;
  6376. }
  6377. static const char * skipAsterisk(const char * text)
  6378. {
  6379. if (*text=='*')
  6380. return skipWhitespace(text+1);
  6381. return text;
  6382. }
  6383. static const char * skipToNewline(const char * text)
  6384. {
  6385. while (*text && (*text != '\r') && (*text != '\n'))
  6386. text++;
  6387. return text;
  6388. }
  6389. static const char * skipNewline(const char * text)
  6390. {
  6391. if (*text == '\r')
  6392. text++;
  6393. if (*text == '\n')
  6394. text++;
  6395. return text;
  6396. }
  6397. void extractJavadoc(IPropertyTree * result, const char * text)
  6398. {
  6399. //Skip a leading blank line
  6400. text = skipWhitespace(text);
  6401. text = skipNewline(text);
  6402. //Now process each of the parameters...
  6403. StringBuffer tagname;
  6404. StringBuffer tagtext;
  6405. for (;;)
  6406. {
  6407. text = skipWhitespace(text);
  6408. text = skipAsterisk(text);
  6409. if ((*text == 0) || (*text == '@'))
  6410. {
  6411. if (tagtext.length())
  6412. {
  6413. if (tagname.length())
  6414. result->addProp(tagname.str(), tagtext.str());
  6415. else
  6416. result->setProp("", tagtext.str());
  6417. tagtext.clear();
  6418. }
  6419. if (*text == 0)
  6420. return;
  6421. text++;
  6422. const char * start = text;
  6423. while (isalnum(*text))
  6424. text++;
  6425. if (start != text)
  6426. tagname.clear().append(text-start, start);
  6427. text = skipWhitespace(text);
  6428. }
  6429. const char * start = text;
  6430. text = skipToNewline(text);
  6431. if (start != text)
  6432. {
  6433. if (tagtext.length())
  6434. tagtext.append(" ");
  6435. tagtext.append(text-start, start);
  6436. }
  6437. text = skipNewline(text);
  6438. }
  6439. }
  6440. /////////////////////
  6441. #ifdef _DEBUG
  6442. jlib_decl void validatePTree()
  6443. {
  6444. Owned<IPropertyTree> testTree = createPTreeFromXMLString(
  6445. "<ROOT>" \
  6446. " <E a=\"av1\" b=\"bv1\" c=\"cv1\"></E>" \
  6447. " <E a=\"av1\" b=\"bv1\" c=\"cv2\"></E>" \
  6448. " <E a=\"av2\" b=\"bv1\"></E>" \
  6449. " <SE c=\"cv1\"></SE>" \
  6450. " <E a=\"av1\" b=\"bv2\"></E>" \
  6451. " <E a=\"av2\" b=\"bv2\" c=\"cv3\">ev1</E>" \
  6452. "</ROOT>"
  6453. );
  6454. Owned<IPropertyTreeIterator> iter = testTree->getElements("E[@a=\"av1\"][@b=\"bv2\"]");
  6455. unsigned c = 0;
  6456. ForEach (*iter)
  6457. ++c;
  6458. assertex(1 == c);
  6459. int v = strcmp("bv1", testTree->queryProp("E[@a=\"av1\"][2]/@b"));
  6460. assertex(0 == v);
  6461. v = strcmp("cv2", testTree->queryProp("E[@a=\"av1\"][@b=\"bv1\"][2]/@c"));
  6462. assertex(0 == v);
  6463. v = strcmp("cv2", testTree->queryProp("E[@a=\"av1\"][2]/@c"));
  6464. assertex(0 == v);
  6465. v = strcmp("ev1", testTree->queryProp("E[@a=\"av2\"][@c]"));
  6466. assertex(0 == v);
  6467. }
  6468. jlib_decl void testValidateXPathSyntax()
  6469. {
  6470. verifyex(validateXPathSyntax("@abc"));
  6471. verifyex(validateXPathSyntax("prop"));
  6472. verifyex(validateXPathSyntax("a/b"));
  6473. verifyex(validateXPathSyntax("a/@b"));
  6474. const char *s = "a[@a=\"blah\"]/b";
  6475. verifyex(validateXPathSyntax(s));
  6476. s = "a/b[@b=\"blah\"]";
  6477. verifyex(validateXPathSyntax(s));
  6478. verifyex(validateXPathSyntax(s));
  6479. s = "a/b[b=\"blah\"]";
  6480. verifyex(validateXPathSyntax(s));
  6481. s = "a/b[a/b=\"blah\"]";
  6482. verifyex(validateXPathSyntax(s));
  6483. verifyex(validateXPathSyntax("a[1]/b[2]"));
  6484. s = "a[b]/b[c=\"a\"]/c";
  6485. verifyex(validateXPathSyntax(s));
  6486. verifyex(validateXPathSyntax("//a/b/c"));
  6487. verifyex(!validateXPathSyntax("a[b"));
  6488. verifyex(!validateXPathSyntax("a["));
  6489. verifyex(!validateXPathSyntax("a]"));
  6490. verifyex(!validateXPathSyntax("a[b=blah]"));
  6491. verifyex(!validateXPathSyntax("@a/b"));
  6492. verifyex(!validateXPathSyntax("a[b[c]]"));
  6493. verifyex(validateXMLParseXPath("<>"));
  6494. verifyex(validateXMLParseXPath("a/b/c<>"));
  6495. verifyex(validateXMLParseXPath("a/b/<>"));
  6496. verifyex(validateXMLParseXPath("/a/b"));
  6497. verifyex(!validateXMLParseXPath("a/b[\"]/<>"));
  6498. verifyex(!validateXMLParseXPath("/<>"));
  6499. }
  6500. jlib_decl void testJdocCompare()
  6501. {
  6502. Owned<IPropertyTree> t1 = createPTree();
  6503. Owned<IPropertyTree> t2 = createPTree();
  6504. Owned<IPropertyTree> t3 = createPTree();
  6505. Owned<IPropertyTree> t4 = createPTree();
  6506. Owned<IPropertyTree> t5 = createPTree();
  6507. extractJavadoc(t1, "Defines a record that contains information about a person");
  6508. extractJavadoc(t2, "Allows the name table to be filtered.\n\n@param ages\tThe ages that are allowed to be processed.\n\t\tbadForename Forname to avoid.\n\n@return\tthe filtered dataset.");
  6509. extractJavadoc(t3, "Allows the name table to be filtered.\n\n@param ages\tThe ages that are allowed to be processed.\n\t\tbadForename Forname to avoid.\n\n@return\tthe filtered dataset.");
  6510. extractJavadoc(t4, "Allows the name table to be filtered.\n\n@param ages\tThe ages that are allowed to be processed.\n\t\tbadForename Forname to avoid.\n\n@return\tthe filtered dataset.");
  6511. extractJavadoc(t5, "Allows the name table to be filtered.\n\n@param ages\tThe ages that are allowed to be processed.\n\t\tbadForename Forname to avoid.\n\n@return\tthe filtered dataset.");
  6512. IPropertyTree * t2c = t2->addPropTree("Child1", createPTree());
  6513. extractJavadoc(t2c, "This is some child data\n\n@param ages\tThe ages that are allowed to be processed.");
  6514. IPropertyTree * t3c = t3->addPropTree("Child1", createPTree());
  6515. extractJavadoc(t3c, "This is some child data\n\n@param ages\tThe ages that are allowed to be processed.");
  6516. IPropertyTree * t4c = t4->addPropTree("Child1", createPTree());
  6517. extractJavadoc(t4c, "This is some child data\n\n@param ages\tThe ages that are allowed to be processed, but differs.");
  6518. IPropertyTree * t5c = t5->addPropTree("Child1", createPTree());
  6519. extractJavadoc(t5c, "This is some child data\n\n@param ages\tThe ages that are allowed to be processed.");
  6520. t2->setProp("@childAttr", "1");
  6521. t3->setProp("@childAttr", "1");
  6522. t4->setProp("@childAttr", "1");
  6523. t5->setProp("@childAttr", "2");
  6524. verifyex(areMatchingPTrees(NULL, NULL));
  6525. verifyex(!areMatchingPTrees(NULL, t1));
  6526. verifyex(!areMatchingPTrees(t1, NULL));
  6527. verifyex(areMatchingPTrees(t1, t1));
  6528. verifyex(areMatchingPTrees(t2, t3));
  6529. verifyex(!areMatchingPTrees(t2, t4));
  6530. verifyex(!areMatchingPTrees(t2, t5));
  6531. }
  6532. #endif
  6533. template <class BASE_PTREE>
  6534. class COrderedPTree : public BASE_PTREE
  6535. {
  6536. template <class BASECHILDMAP>
  6537. class jlib_decl COrderedChildMap : public BASECHILDMAP
  6538. {
  6539. typedef COrderedChildMap<BASECHILDMAP> SELF;
  6540. ICopyArrayOf<IPropertyTree> order;
  6541. public:
  6542. IMPLEMENT_SUPERHASHTABLEOF_REF_FIND(IPropertyTree, constcharptr);
  6543. COrderedChildMap<BASECHILDMAP>() : BASECHILDMAP() { }
  6544. ~COrderedChildMap<BASECHILDMAP>() { SELF::kill(); }
  6545. virtual unsigned numChildren() const override { return order.ordinality(); }
  6546. virtual IPropertyTreeIterator *getIterator(bool sort) override
  6547. {
  6548. class CPTArrayIterator : public ArrayIIteratorOf<IArrayOf<IPropertyTree>, IPropertyTree, IPropertyTreeIterator>
  6549. {
  6550. IArrayOf<IPropertyTree> elems;
  6551. public:
  6552. CPTArrayIterator(ICopyArrayOf<IPropertyTree> &order, bool sort) : ArrayIIteratorOf<IArrayOf<IPropertyTree>, IPropertyTree, IPropertyTreeIterator>(elems)
  6553. {
  6554. ForEachItemIn(e, order)
  6555. elems.append(*LINK(&order.item(e)));
  6556. if (sort)
  6557. elems.sort(comparePropTrees);
  6558. }
  6559. };
  6560. return new CPTArrayIterator(order, sort);
  6561. }
  6562. virtual bool set(const char *key, IPropertyTree *tree) override
  6563. {
  6564. IPropertyTree *existing = find(*key);
  6565. if (existing)
  6566. {
  6567. unsigned pos = order.find(*existing);
  6568. BASECHILDMAP::set(key, tree);
  6569. order.replace(*tree, pos);
  6570. }
  6571. else
  6572. {
  6573. BASECHILDMAP::set(key, tree);
  6574. order.append(*tree);
  6575. }
  6576. return true;
  6577. }
  6578. virtual bool replace(const char *key, IPropertyTree *tree) override // provides different semantics, used if element being replaced is not to be treated as deleted.
  6579. {
  6580. return set(key, tree);
  6581. }
  6582. virtual bool remove(const char *key) override
  6583. {
  6584. IPropertyTree *child = BASECHILDMAP::find(*key);
  6585. if (!child)
  6586. return false;
  6587. order.zap(*child);
  6588. return BASECHILDMAP::removeExact(child);
  6589. }
  6590. virtual bool removeExact(IPropertyTree *child) override
  6591. {
  6592. order.zap(*child);
  6593. return BASECHILDMAP::removeExact(child);
  6594. }
  6595. };
  6596. public:
  6597. typedef COrderedPTree<BASE_PTREE> SELF;
  6598. COrderedPTree<BASE_PTREE>(const char *name=NULL, byte flags=ipt_none, IPTArrayValue *value=NULL, ChildMap *children=NULL)
  6599. : BASE_PTREE(name, flags|ipt_ordered, value, children) { }
  6600. virtual bool isEquivalent(IPropertyTree *tree) const override { return (NULL != QUERYINTERFACE(tree, COrderedPTree<BASE_PTREE>)); }
  6601. virtual IPropertyTree *create(const char *name=NULL, IPTArrayValue *value=NULL, ChildMap *children=NULL, bool existing=false) override
  6602. {
  6603. return new COrderedPTree<BASE_PTREE>(name, SELF::flags, value, children);
  6604. }
  6605. virtual IPropertyTree *create(MemoryBuffer &mb) override
  6606. {
  6607. IPropertyTree *tree = new COrderedPTree<BASE_PTREE>();
  6608. tree->deserialize(mb);
  6609. return tree;
  6610. }
  6611. virtual void createChildMap() override
  6612. {
  6613. if (SELF::isnocase())
  6614. SELF::children = new COrderedChildMap<ChildMapNC>();
  6615. else
  6616. SELF::children = new COrderedChildMap<ChildMap>();
  6617. }
  6618. };
  6619. IPropertyTree *createPTree(byte flags)
  6620. {
  6621. return createPTree(NULL, flags);
  6622. }
  6623. IPropertyTree *createPTree(const char *name, byte flags)
  6624. {
  6625. switch (flags & (ipt_ordered|ipt_fast|ipt_lowmem))
  6626. {
  6627. case ipt_ordered|ipt_fast:
  6628. return new COrderedPTree<LocalPTree>(name, flags);
  6629. case ipt_ordered|ipt_lowmem:
  6630. return new COrderedPTree<CAtomPTree>(name, flags);
  6631. case ipt_ordered:
  6632. return new COrderedPTree<DEFAULT_PTREE_TYPE>(name, flags);
  6633. case ipt_fast:
  6634. return new LocalPTree(name, flags);
  6635. case ipt_lowmem:
  6636. return new CAtomPTree(name, flags);
  6637. case 0:
  6638. return new DEFAULT_PTREE_TYPE(name, flags);
  6639. default:
  6640. throwUnexpectedX("Invalid flags - ipt_fast and ipt_lowmem should not be specified together");
  6641. }
  6642. }
  6643. typedef enum _ptElementType
  6644. {
  6645. elementTypeUnknown,
  6646. elementTypeNull,
  6647. elementTypeString,
  6648. elementTypeBool,
  6649. elementTypeInteger,
  6650. elementTypeReal,
  6651. elementTypeObject,
  6652. elementTypeArray
  6653. } ptElementType;
  6654. template <typename X>
  6655. class CJSONReaderBase : public CommonReaderBase<X>
  6656. {
  6657. public:
  6658. typedef CommonReaderBase<X> PARENT;
  6659. using PARENT::reset;
  6660. using PARENT::nextChar;
  6661. using PARENT::readNextToken;
  6662. using PARENT::checkReadNext;
  6663. using PARENT::checkStartReadNext;
  6664. using PARENT::readNext;
  6665. using PARENT::expecting;
  6666. using PARENT::match;
  6667. using PARENT::error;
  6668. using PARENT::skipWS;
  6669. using PARENT::rewind;
  6670. using PARENT::ignoreWhiteSpace;
  6671. CJSONReaderBase(ISimpleReadStream &_stream, IPTreeNotifyEvent &_iEvent, PTreeReaderOptions _readerOptions, size32_t _bufSize=0) :
  6672. CommonReaderBase<X>(_stream, _iEvent, _readerOptions, _bufSize)
  6673. {
  6674. }
  6675. CJSONReaderBase(const void *_buf, size32_t bufLength, IPTreeNotifyEvent &_iEvent, PTreeReaderOptions _readerOptions) :
  6676. CommonReaderBase<X>(_buf, bufLength, _iEvent, _readerOptions)
  6677. {
  6678. }
  6679. CJSONReaderBase(const void *_buf, IPTreeNotifyEvent &_iEvent, PTreeReaderOptions _readerOptions) :
  6680. CommonReaderBase<X>(_buf, _iEvent, _readerOptions)
  6681. {
  6682. }
  6683. ~CJSONReaderBase()
  6684. {
  6685. }
  6686. protected:
  6687. inline StringBuffer &appendChar(StringBuffer &id, char c)
  6688. {
  6689. int charlen = validJSONUtf8ChrLen(c);
  6690. if (!charlen)
  6691. error("invalid JSON character", true);
  6692. id.append(nextChar);
  6693. while (--charlen)
  6694. {
  6695. readNext();
  6696. id.append(nextChar);
  6697. }
  6698. return id;
  6699. }
  6700. void readString(StringBuffer &value)
  6701. {
  6702. readNext();
  6703. StringBuffer s;
  6704. bool decode=false;
  6705. while ('\"'!=nextChar)
  6706. {
  6707. if (nextChar=='\\')
  6708. decode=true;
  6709. appendChar(s, nextChar);
  6710. readNext();
  6711. }
  6712. size32_t r = s.length();
  6713. if (ignoreWhiteSpace)
  6714. s.trimRight();
  6715. if (decode)
  6716. _decodeJSON(r, s.str(), value, s.length()+1);
  6717. else
  6718. value.swapWith(s);
  6719. }
  6720. void readName(StringBuffer &name)
  6721. {
  6722. if ('\"'!=nextChar)
  6723. expecting("\"");
  6724. readString(name);
  6725. if (!name.length())
  6726. error("empty JSON id");
  6727. readNext();
  6728. skipWS();
  6729. if (':'!=nextChar)
  6730. expecting(":");
  6731. readNext();
  6732. }
  6733. ptElementType readValue(StringBuffer &value)
  6734. {
  6735. ptElementType type = elementTypeUnknown;
  6736. switch (nextChar)
  6737. {
  6738. case '\"':
  6739. {
  6740. readString(value);
  6741. type = elementTypeString;
  6742. break;
  6743. }
  6744. case 't':
  6745. match("rue", "Bad value");
  6746. value.append("true");
  6747. type = elementTypeBool;
  6748. break;
  6749. case 'f':
  6750. match("alse", "Bad value");
  6751. value.append("false");
  6752. type = elementTypeBool;
  6753. break;
  6754. case 'n':
  6755. match("ull", "Bad value");
  6756. type = elementTypeNull;
  6757. break;
  6758. case '-':
  6759. value.append(nextChar);
  6760. readNext();
  6761. //fall through
  6762. default:
  6763. if (!isdigit(nextChar))
  6764. error("Bad value");
  6765. type = elementTypeInteger;
  6766. bool exponent = false;
  6767. while (isdigit(nextChar) || '.'==nextChar || 'e'==nextChar || 'E'==nextChar)
  6768. {
  6769. if ('e'==nextChar || 'E'==nextChar)
  6770. {
  6771. if (exponent)
  6772. error("Bad value");
  6773. exponent=true;
  6774. value.append(nextChar);
  6775. readNext();
  6776. if ('-'==nextChar)
  6777. type=elementTypeReal;
  6778. else if (!isdigit(nextChar) && '+'!=nextChar)
  6779. error("Bad value");
  6780. }
  6781. if ('.'==nextChar)
  6782. {
  6783. if (exponent || type==elementTypeReal) //already found decimal
  6784. error("Bad value");
  6785. type = elementTypeReal;
  6786. }
  6787. value.append(nextChar);
  6788. readNext();
  6789. }
  6790. rewind(1);
  6791. break;
  6792. }
  6793. return type;
  6794. }
  6795. const char *_decodeJSON(unsigned read, const char *startMark, StringBuffer &ret, unsigned len)
  6796. {
  6797. const char *errMark = NULL;
  6798. try { return decodeJSON(startMark, ret, len, &errMark); }
  6799. catch (IException *e)
  6800. {
  6801. if (errMark)
  6802. {
  6803. if (read>(unsigned)(errMark-startMark))
  6804. rewind((unsigned)(read - (errMark-startMark)));
  6805. else
  6806. rewind((unsigned)(errMark-startMark));
  6807. }
  6808. StringBuffer errMsg;
  6809. e->errorMessage(errMsg);
  6810. e->Release();
  6811. error(errMsg.str());
  6812. }
  6813. return NULL; // will never get here.
  6814. }
  6815. };
  6816. template <class X>
  6817. class CJSONReader : public CJSONReaderBase<X>, implements IPTreeReader
  6818. {
  6819. typedef CJSONReaderBase<X> PARENT;
  6820. using PARENT::checkBOM;
  6821. using PARENT::rewind;
  6822. using PARENT::readNext;
  6823. using PARENT::readValue;
  6824. using PARENT::readName;
  6825. using PARENT::checkReadNext;
  6826. using PARENT::checkSkipWS;
  6827. using PARENT::checkStartReadNext;
  6828. using PARENT::expecting;
  6829. using PARENT::error;
  6830. using PARENT::eos;
  6831. using PARENT::_decodeJSON;
  6832. using PARENT::skipWS;
  6833. using PARENT::nextChar;
  6834. using PARENT::curOffset;
  6835. using PARENT::noRoot;
  6836. using PARENT::ignoreWhiteSpace;
  6837. using PARENT::iEvent;
  6838. // StringBuffer tmpStr;
  6839. public:
  6840. IMPLEMENT_IINTERFACE;
  6841. CJSONReader(ISimpleReadStream &stream, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions, size32_t bufSize=0)
  6842. : PARENT(stream, iEvent, readerOptions, bufSize)
  6843. {
  6844. }
  6845. CJSONReader(const void *buf, size32_t bufLength, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions)
  6846. : PARENT(buf, bufLength, iEvent, readerOptions)
  6847. {
  6848. }
  6849. CJSONReader(const void *buf, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions)
  6850. : PARENT(buf, iEvent, readerOptions)
  6851. {
  6852. }
  6853. void readValueNotify(const char *name, bool skipAttributes, StringBuffer *retValue, bool *isValueBinary)
  6854. {
  6855. offset_t startOffset = curOffset;
  6856. StringBuffer value;
  6857. if (readValue(value)!=elementTypeNull)
  6858. {
  6859. if ('@'==*name)
  6860. {
  6861. if (!skipAttributes)
  6862. iEvent->newAttribute(name, value.str());
  6863. return;
  6864. }
  6865. else if ('#'==*name)
  6866. {
  6867. dbgassertex(retValue && isValueBinary);
  6868. *isValueBinary = false;
  6869. if (0 == strncmp(name+1, "value", 5)) // this is a special IPT JSON prop name, representing a 'complex' value
  6870. {
  6871. if ('\0' == *(name+6)) // #value
  6872. {
  6873. retValue->swapWith(value);
  6874. return;
  6875. }
  6876. else if (streq(name+6, "bin")) // #valuebin
  6877. {
  6878. *isValueBinary = true;
  6879. JBASE64_Decode(value.str(), *retValue);
  6880. return;
  6881. }
  6882. }
  6883. }
  6884. }
  6885. iEvent->beginNode(name, false, startOffset);
  6886. iEvent->beginNodeContent(name);
  6887. iEvent->endNode(name, value.length(), value.str(), false, curOffset);
  6888. }
  6889. void readArray(const char *name)
  6890. {
  6891. if ('@'==*name)
  6892. name++;
  6893. readNext();
  6894. skipWS();
  6895. while (']' != nextChar)
  6896. {
  6897. switch (nextChar)
  6898. {
  6899. case '[':
  6900. iEvent->beginNode(name, false, curOffset);
  6901. iEvent->beginNodeContent(name);
  6902. readArray(name);
  6903. iEvent->endNode(name, 0, "", false, curOffset);
  6904. break;
  6905. case '{':
  6906. readObject(name);
  6907. break;
  6908. default:
  6909. readValueNotify(name, true, nullptr, nullptr);
  6910. break;
  6911. }
  6912. readNext();
  6913. skipWS();
  6914. if (','==nextChar)
  6915. readNext();
  6916. else if (']'!=nextChar)
  6917. error("expected ',' or ']'");
  6918. skipWS();
  6919. }
  6920. }
  6921. void readChild(const char *name, bool skipAttributes, StringBuffer *value, bool *isValueBinary)
  6922. {
  6923. skipWS();
  6924. switch (nextChar)
  6925. {
  6926. case '}':
  6927. {
  6928. VStringBuffer msg("named item with no value defined %s [%d]", name, (int) curOffset);
  6929. error(msg.str());
  6930. }
  6931. break;
  6932. case '{':
  6933. readObject(name);
  6934. break;
  6935. case '[':
  6936. readArray(name);
  6937. break;
  6938. default:
  6939. readValueNotify(name, skipAttributes, value, isValueBinary);
  6940. break;
  6941. }
  6942. }
  6943. void readObject(const char *name)
  6944. {
  6945. if ('@'==*name)
  6946. name++;
  6947. iEvent->beginNode(name, false, curOffset);
  6948. readNext();
  6949. skipWS();
  6950. bool attributesFinalized=false;
  6951. StringBuffer childValue; // for #value
  6952. bool isChildValueBinary = false; // for #value
  6953. while ('}' != nextChar)
  6954. {
  6955. StringBuffer tagName;
  6956. readName(tagName);
  6957. //internal convention so we can convert to and from xml
  6958. //values at top of object with names starting with '@' become ptree attributes
  6959. if (*tagName.str()!='@')
  6960. attributesFinalized=true;
  6961. readChild(tagName.str(), attributesFinalized, &childValue.clear(), &isChildValueBinary);
  6962. readNext();
  6963. skipWS();
  6964. if (','==nextChar)
  6965. readNext();
  6966. else if ('}'!=nextChar)
  6967. error("expected ',' or '}'");
  6968. skipWS();
  6969. }
  6970. iEvent->endNode(name, childValue.length(), childValue.str(), isChildValueBinary, curOffset);
  6971. }
  6972. void loadJSON()
  6973. {
  6974. if (!checkStartReadNext())
  6975. return;
  6976. if (checkBOM() && !checkReadNext())
  6977. return;
  6978. if (!checkSkipWS())
  6979. return;
  6980. if (noRoot)
  6981. {
  6982. StringBuffer tagName;
  6983. for (;;)
  6984. {
  6985. switch (nextChar)
  6986. {
  6987. case '\"': //treat named objects like we're in a noroot object
  6988. readName(tagName.clear());
  6989. readChild(tagName.str(), true, nullptr, nullptr);
  6990. break;
  6991. case '{': //treat unnamed objects like we're in a noroot array
  6992. readObject("__object__");
  6993. break;
  6994. case '[': //treat unnamed arrays like we're in a noroot array
  6995. iEvent->beginNode("__array__", false, curOffset);
  6996. readArray("__item__");
  6997. iEvent->endNode("__array__", 0, "", false, curOffset);
  6998. break;
  6999. default:
  7000. expecting("{[ or \"");
  7001. }
  7002. if (!checkReadNext() || !checkSkipWS())
  7003. break;
  7004. switch (nextChar)
  7005. {
  7006. case '{': //support file formats with whitespace (usually \n) seperated objects at the root
  7007. case '[':
  7008. break;
  7009. case ',':
  7010. readNext();
  7011. skipWS();
  7012. break;
  7013. default:
  7014. expecting(",");
  7015. break;
  7016. }
  7017. }
  7018. }
  7019. else
  7020. {
  7021. if ('{' == nextChar)
  7022. readObject("__object__");
  7023. else if ('[' == nextChar)
  7024. {
  7025. iEvent->beginNode("__array__", false, curOffset);
  7026. readArray("__item__");
  7027. iEvent->endNode("__array__", 0, "", false, curOffset);
  7028. }
  7029. else
  7030. error("expected '{' or '['");
  7031. if (checkReadNext() && checkSkipWS())
  7032. error("trailing content after JSON closed");
  7033. }
  7034. }
  7035. // IPTreeReader
  7036. virtual void load() { loadJSON(); }
  7037. virtual offset_t queryOffset() { return curOffset; }
  7038. };
  7039. template <class X>
  7040. class CPullJSONReader : public CJSONReaderBase<X>, implements IPullPTreeReader
  7041. {
  7042. typedef CJSONReaderBase<X> PARENT;
  7043. using PARENT::checkBOM;
  7044. using PARENT::rewind;
  7045. using PARENT::readNext;
  7046. using PARENT::readValue;
  7047. using PARENT::readName;
  7048. using PARENT::checkReadNext;
  7049. using PARENT::checkSkipWS;
  7050. using PARENT::checkStartReadNext;
  7051. using PARENT::expecting;
  7052. using PARENT::error;
  7053. using PARENT::eos;
  7054. using PARENT::_decodeJSON;
  7055. using PARENT::skipWS;
  7056. using PARENT::nextChar;
  7057. using PARENT::curOffset;
  7058. using PARENT::noRoot;
  7059. using PARENT::ignoreWhiteSpace;
  7060. using PARENT::iEvent;
  7061. class CStateInfo : public CInterface
  7062. {
  7063. public:
  7064. CStateInfo()
  7065. {
  7066. tag.ensureCapacity(15);
  7067. type = elementTypeUnknown;
  7068. childCount = 0;
  7069. wnsTag = NULL;
  7070. }
  7071. inline void reset()
  7072. {
  7073. wnsTag = NULL;
  7074. tag.clear();
  7075. tagText.clear();
  7076. type = elementTypeUnknown;
  7077. childCount = 0;
  7078. }
  7079. StringBuffer tag;
  7080. StringBuffer tagText;
  7081. ptElementType type;
  7082. const char *wnsTag;
  7083. unsigned childCount;
  7084. };
  7085. CICopyArrayOf<CStateInfo> stack, freeStateInfo;
  7086. CStateInfo *stateInfo;
  7087. enum ParseStates { headerStart, nameStart, valueStart, itemStart, objAttributes, itemContent, itemEnd } state;
  7088. bool endOfRoot;
  7089. bool preReadItemName;
  7090. bool more;
  7091. StringBuffer tag, value;
  7092. void init()
  7093. {
  7094. state = headerStart;
  7095. stateInfo = NULL;
  7096. endOfRoot = false;
  7097. preReadItemName = false;
  7098. more = true;
  7099. }
  7100. virtual void resetState()
  7101. {
  7102. stack.kill();
  7103. more = true;
  7104. }
  7105. public:
  7106. IMPLEMENT_IINTERFACE;
  7107. CPullJSONReader(ISimpleReadStream &stream, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions, size32_t bufSize=0)
  7108. : CJSONReaderBase<X>(stream, iEvent, readerOptions, bufSize)
  7109. {
  7110. init();
  7111. }
  7112. CPullJSONReader(const void *buf, size32_t bufLength, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions)
  7113. : CJSONReaderBase<X>(buf, bufLength, iEvent, readerOptions)
  7114. {
  7115. init();
  7116. }
  7117. CPullJSONReader(const void *buf, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions)
  7118. : CJSONReaderBase<X>(buf, iEvent, readerOptions)
  7119. {
  7120. init();
  7121. }
  7122. ~CPullJSONReader()
  7123. {
  7124. ForEachItemIn(i, stack)
  7125. delete &stack.item(i);
  7126. ForEachItemIn(i2, freeStateInfo)
  7127. delete &freeStateInfo.item(i2);
  7128. }
  7129. inline void checkDelimiter(const char *msg=",")
  7130. {
  7131. if (stateInfo && stateInfo->childCount > 0)
  7132. {
  7133. if (','!=nextChar)
  7134. expecting(msg);
  7135. readNext();
  7136. skipWS();
  7137. }
  7138. }
  7139. inline ptElementType getParentType()
  7140. {
  7141. if (stack.ordinality()<2)
  7142. return stateInfo->type;
  7143. return ((CStateInfo *)&stack.tos(1))->type;
  7144. }
  7145. void beginNode(const char *name, offset_t offset, ptElementType jsonType, bool notify=true)
  7146. {
  7147. if (stateInfo)
  7148. stateInfo->childCount++;
  7149. if (freeStateInfo.ordinality())
  7150. {
  7151. stateInfo = &freeStateInfo.popGet();
  7152. stateInfo->reset();
  7153. }
  7154. else
  7155. stateInfo = new CStateInfo;
  7156. stack.append(*stateInfo);
  7157. stateInfo->type=jsonType;
  7158. if (name)
  7159. stateInfo->tag.set(name);
  7160. else
  7161. stateInfo->tag.swapWith(tag);
  7162. stateInfo->wnsTag = stateInfo->tag.str();
  7163. if (!notify)
  7164. return;
  7165. try
  7166. {
  7167. iEvent->beginNode(stateInfo->wnsTag, false, offset);
  7168. }
  7169. catch (IPTreeException *pe)
  7170. {
  7171. if (PTreeExcpt_InvalidTagName == pe->errorCode())
  7172. {
  7173. pe->Release();
  7174. StringBuffer msg("Expecting valid start tag, but got \"");
  7175. error(msg.append(name).append("\"").str());
  7176. }
  7177. throw;
  7178. }
  7179. }
  7180. inline const char *arrayItemName(const char *defaultName)
  7181. {
  7182. if (stack.ordinality()>1)
  7183. return stateInfo->wnsTag;
  7184. return defaultName;
  7185. }
  7186. bool arrayItem(offset_t offset)
  7187. {
  7188. skipWS();
  7189. switch (nextChar)
  7190. {
  7191. case ']':
  7192. state=itemContent;
  7193. if (stack.ordinality()>1)
  7194. readNext();
  7195. if (!endNode(curOffset, getParentType()==elementTypeArray))
  7196. return false;
  7197. break;
  7198. case '{':
  7199. state=objAttributes;
  7200. readNext();
  7201. beginNode(arrayItemName("__object__"), offset, elementTypeObject);
  7202. break;
  7203. case '[':
  7204. state=valueStart;
  7205. readNext();
  7206. beginNode(arrayItemName("__array__"), offset, elementTypeArray, true);
  7207. break;
  7208. default:
  7209. state=valueStart;
  7210. ptElementType type = readValue(value.clear());
  7211. readNext();
  7212. beginNode(arrayItemName("__item__"), offset, type, true);
  7213. stateInfo->tagText.swapWith(value);
  7214. break;
  7215. }
  7216. return true;
  7217. }
  7218. void namedItem()
  7219. {
  7220. if (!preReadItemName)
  7221. readName(tag.clear());
  7222. else
  7223. preReadItemName = false;
  7224. skipWS();
  7225. switch (nextChar)
  7226. {
  7227. case '}':
  7228. error("unexpected object close marker");
  7229. case ']':
  7230. error("unexpected array close marker");
  7231. case '{':
  7232. state=objAttributes;
  7233. readNext();
  7234. beginNode(NULL, curOffset, elementTypeObject);
  7235. break;
  7236. case '[':
  7237. readNext();
  7238. beginNode(NULL, curOffset, elementTypeArray, false); //false because items present events, not the array
  7239. arrayItem(curOffset); //so process the first item so every next() has event
  7240. break;
  7241. default:
  7242. state=valueStart;
  7243. ptElementType type = readValue(value.clear());
  7244. readNext();
  7245. beginNode(NULL, curOffset, type);
  7246. stateInfo->tagText.swapWith(value);
  7247. break;
  7248. }
  7249. }
  7250. void rootItem()
  7251. {
  7252. if ('\"'==nextChar)
  7253. namedItem();
  7254. else if ('{'==nextChar || '['==nextChar)
  7255. arrayItem(curOffset);
  7256. else
  7257. expecting("[{ or \"");
  7258. }
  7259. bool rootNext()
  7260. {
  7261. if (!noRoot)
  7262. return false;
  7263. if (!checkReadNext() || !checkSkipWS())
  7264. return true;
  7265. switch (nextChar)
  7266. {
  7267. case '{': //support files where root level objects are separated by whitespace (usually \n)
  7268. case '[':
  7269. case ',':
  7270. break;
  7271. default:
  7272. expecting(",");
  7273. }
  7274. return true;
  7275. }
  7276. void newNamedAttribute()
  7277. {
  7278. skipWS();
  7279. readValue(value.clear());
  7280. readNext();
  7281. stateInfo->childCount++;
  7282. iEvent->newAttribute(tag.str(), value.str());
  7283. }
  7284. bool endNode(offset_t offset, bool notify=true)
  7285. {
  7286. if (stack.ordinality()<2)
  7287. {
  7288. state = headerStart;
  7289. more = rootNext();
  7290. }
  7291. if (notify)
  7292. {
  7293. if (stateInfo->type==elementTypeNull)
  7294. iEvent->endNode(stateInfo->wnsTag, 0, "", false, offset);
  7295. else
  7296. iEvent->endNode(stateInfo->wnsTag, stateInfo->tagText.length(), stateInfo->tagText.str(), false, offset);
  7297. }
  7298. freeStateInfo.append(*stateInfo);
  7299. stack.pop();
  7300. stateInfo = (stack.ordinality()) ? &stack.tos() : NULL;
  7301. return true;
  7302. }
  7303. // IPullPTreeReader
  7304. virtual void load()
  7305. {
  7306. while (next()) {}
  7307. }
  7308. virtual void reset()
  7309. {
  7310. PARENT::reset();
  7311. resetState();
  7312. }
  7313. virtual offset_t queryOffset() { return curOffset; }
  7314. virtual bool next()
  7315. {
  7316. if (!more)
  7317. return false;
  7318. checkStartReadNext();
  7319. checkSkipWS();
  7320. switch (state)
  7321. {
  7322. case headerStart:
  7323. {
  7324. if (nextChar!='{' && nextChar!='[') //already positioned at start
  7325. {
  7326. if (!checkReadNext())
  7327. return false;
  7328. if (checkBOM())
  7329. if (!checkReadNext())
  7330. return false;
  7331. if (!checkSkipWS())
  7332. return false;
  7333. }
  7334. if (noRoot)
  7335. rootItem();
  7336. else
  7337. {
  7338. switch (nextChar)
  7339. {
  7340. case '{':
  7341. state=objAttributes;
  7342. readNext();
  7343. beginNode("__object__", curOffset, elementTypeObject);
  7344. break;
  7345. case '[':
  7346. state=valueStart;
  7347. readNext();
  7348. beginNode("__array__", curOffset, elementTypeArray);
  7349. break;
  7350. default:
  7351. expecting("{ or [");
  7352. break;
  7353. }
  7354. }
  7355. break;
  7356. }
  7357. case nameStart:
  7358. namedItem();
  7359. break;
  7360. case objAttributes:
  7361. {
  7362. if ('}'==nextChar)
  7363. {
  7364. state=itemEnd;
  7365. iEvent->beginNodeContent(stateInfo->wnsTag);
  7366. break;
  7367. }
  7368. checkDelimiter(", or }");
  7369. if (nextChar != '\"')
  7370. expecting("\"");
  7371. readName(tag.clear());
  7372. if (tag.charAt(0)=='@')
  7373. newNamedAttribute();
  7374. else
  7375. {
  7376. preReadItemName = true;
  7377. state=itemContent;
  7378. stateInfo->childCount=0;
  7379. iEvent->beginNodeContent(stateInfo->wnsTag);
  7380. }
  7381. break;
  7382. }
  7383. case valueStart:
  7384. state=itemContent;
  7385. iEvent->beginNodeContent(stateInfo->wnsTag);
  7386. break;
  7387. case itemContent:
  7388. {
  7389. switch (stateInfo->type)
  7390. {
  7391. case elementTypeBool:
  7392. case elementTypeString:
  7393. case elementTypeInteger:
  7394. case elementTypeReal:
  7395. case elementTypeNull:
  7396. return endNode(curOffset);
  7397. break;
  7398. case elementTypeArray:
  7399. if (']'!=nextChar)
  7400. checkDelimiter(", or ]");
  7401. return arrayItem(curOffset);
  7402. case elementTypeObject:
  7403. if ('}'!=nextChar)
  7404. {
  7405. checkDelimiter(", or }");
  7406. namedItem();
  7407. }
  7408. else
  7409. {
  7410. if (stack.ordinality()>1)
  7411. readNext();
  7412. return endNode(curOffset);
  7413. }
  7414. break;
  7415. }
  7416. break;
  7417. }
  7418. case itemEnd:
  7419. {
  7420. if (!stack.length())
  7421. {
  7422. if (!noRoot || !rootNext())
  7423. return false;
  7424. readNext();
  7425. skipWS();
  7426. rootItem();
  7427. }
  7428. else
  7429. {
  7430. readNext();
  7431. state = itemContent;
  7432. return endNode(curOffset);
  7433. }
  7434. break;
  7435. }
  7436. }
  7437. return true;
  7438. }
  7439. };
  7440. IPTreeReader *createJSONStreamReader(ISimpleReadStream &stream, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions, size32_t bufSize)
  7441. {
  7442. class CJSONStreamReader : public CJSONReader<CInstStreamReader>
  7443. {
  7444. public:
  7445. CJSONStreamReader(ISimpleReadStream &stream, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions, size32_t bufSize=0) : CJSONReader<CInstStreamReader>(stream, iEvent, readerOptions, bufSize) { }
  7446. };
  7447. return new CJSONStreamReader(stream, iEvent, readerOptions, bufSize);
  7448. }
  7449. IPTreeReader *createJSONStringReader(const char *json, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions)
  7450. {
  7451. class CJSONStringReader : public CJSONReader<CInstStringReader>
  7452. {
  7453. public:
  7454. CJSONStringReader(const void *json, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions) : CJSONReader<CInstStringReader>(json, iEvent, readerOptions) { }
  7455. };
  7456. if (NULL == json)
  7457. throw createPTreeReadException(PTreeRead_syntax, "Null string passed to createJSONStringReader", NULL, 0, 0);
  7458. return new CJSONStringReader(json, iEvent, readerOptions);
  7459. }
  7460. IPTreeReader *createJSONBufferReader(const void *buf, size32_t bufLength, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions)
  7461. {
  7462. class CJSONBufferReader : public CJSONReader<CInstBufferReader>
  7463. {
  7464. public:
  7465. CJSONBufferReader(const void *buf, size32_t bufLength, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions) : CJSONReader<CInstBufferReader>(buf, bufLength, iEvent, readerOptions) { }
  7466. };
  7467. return new CJSONBufferReader(buf, bufLength, iEvent, readerOptions);
  7468. }
  7469. IPullPTreeReader *createPullJSONStreamReader(ISimpleReadStream &stream, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions, size32_t bufSize)
  7470. {
  7471. class CJSONStreamReader : public CPullJSONReader<CInstStreamReader>
  7472. {
  7473. public:
  7474. CJSONStreamReader(ISimpleReadStream &stream, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions, size32_t bufSize=0) : CPullJSONReader<CInstStreamReader>(stream, iEvent, readerOptions, bufSize) { }
  7475. };
  7476. return new CJSONStreamReader(stream, iEvent, readerOptions, bufSize);
  7477. }
  7478. IPullPTreeReader *createPullJSONStringReader(const char *json, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions)
  7479. {
  7480. class CJSONStringReader : public CPullJSONReader<CInstStringReader>
  7481. {
  7482. public:
  7483. CJSONStringReader(const void *json, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions) : CPullJSONReader<CInstStringReader>(json, iEvent, readerOptions) { }
  7484. };
  7485. return new CJSONStringReader(json, iEvent, readerOptions);
  7486. }
  7487. IPullPTreeReader *createPullJSONBufferReader(const void *buf, size32_t bufLength, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions)
  7488. {
  7489. class CJSONBufferReader : public CPullJSONReader<CInstBufferReader>
  7490. {
  7491. public:
  7492. CJSONBufferReader(const void *buf, size32_t bufLength, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions) : CPullJSONReader<CInstBufferReader>(buf, bufLength, iEvent, readerOptions) { }
  7493. };
  7494. return new CJSONBufferReader(buf, bufLength, iEvent, readerOptions);
  7495. }
  7496. IPropertyTree *createPTreeFromJSONString(const char *json, byte flags, PTreeReaderOptions readFlags, IPTreeMaker *iMaker)
  7497. {
  7498. Owned<IPTreeMaker> _iMaker;
  7499. if (!iMaker)
  7500. {
  7501. iMaker = createDefaultPTreeMaker(flags, readFlags);
  7502. _iMaker.setown(iMaker);
  7503. }
  7504. Owned<IPTreeReader> reader = createJSONStringReader(json, *iMaker, readFlags);
  7505. reader->load();
  7506. return LINK(iMaker->queryRoot());
  7507. }
  7508. IPropertyTree *createPTreeFromJSONString(unsigned len, const char *json, byte flags, PTreeReaderOptions readFlags, IPTreeMaker *iMaker)
  7509. {
  7510. Owned<IPTreeMaker> _iMaker;
  7511. if (!iMaker)
  7512. {
  7513. iMaker = createDefaultPTreeMaker(flags, readFlags);
  7514. _iMaker.setown(iMaker);
  7515. }
  7516. Owned<IPTreeReader> reader = createJSONBufferReader(json, len, *iMaker, readFlags);
  7517. reader->load();
  7518. return LINK(iMaker->queryRoot());
  7519. }
  7520. static const char * nextHttpParameterTag(StringBuffer &tag, const char *path)
  7521. {
  7522. while (*path=='.')
  7523. path++;
  7524. const char *finger = strchr(path, '.');
  7525. if (finger)
  7526. {
  7527. tag.clear().append(finger - path, path);
  7528. finger++;
  7529. }
  7530. else
  7531. tag.set(path);
  7532. return finger;
  7533. }
  7534. static void ensureHttpParameter(IPropertyTree *pt, StringBuffer &tag, const char *path, const char *value, const char *fullpath)
  7535. {
  7536. if (!tag.length())
  7537. return;
  7538. unsigned idx = 1;
  7539. if (path && isdigit(*path))
  7540. {
  7541. StringBuffer pos;
  7542. path = nextHttpParameterTag(pos, path);
  7543. idx = (unsigned) atoi(pos.str())+1;
  7544. }
  7545. if ('@'==*tag)
  7546. {
  7547. if (path && *path)
  7548. throw MakeStringException(-1, "'@' not allowed in parent node of parameter path: %s", fullpath);
  7549. pt->setProp(tag, value);
  7550. return;
  7551. }
  7552. if (tag.charAt(tag.length()-1)=='$')
  7553. {
  7554. if (path && *path)
  7555. throw MakeStringException(-1, "'$' not allowed in parent node of parameter path: %s", fullpath);
  7556. tag.setLength(tag.length()-1);
  7557. StringArray values;
  7558. values.appendList(value, "\r");
  7559. ForEachItemIn(pos, values)
  7560. {
  7561. const char *itemValue = values.item(pos);
  7562. while (*itemValue=='\n')
  7563. itemValue++;
  7564. pt->addProp(tag, itemValue);
  7565. }
  7566. return;
  7567. }
  7568. unsigned count = pt->getCount(tag);
  7569. while (count++ < idx)
  7570. pt->addPropTree(tag, createPTree(tag));
  7571. StringBuffer xpath(tag);
  7572. xpath.append('[').append(idx).append(']');
  7573. pt = pt->queryPropTree(xpath);
  7574. if (!path || !*path)
  7575. {
  7576. pt->setProp(NULL, value);
  7577. return;
  7578. }
  7579. StringBuffer nextTag;
  7580. path = nextHttpParameterTag(nextTag, path);
  7581. ensureHttpParameter(pt, nextTag, path, value, fullpath);
  7582. }
  7583. static void ensureHttpParameter(IPropertyTree *pt, const char *path, const char *value)
  7584. {
  7585. const char *fullpath = path;
  7586. StringBuffer tag;
  7587. path = nextHttpParameterTag(tag, path);
  7588. ensureHttpParameter(pt, tag, path, value, fullpath);
  7589. }
  7590. bool checkParseUrlPathNodeValue(const char *s, StringBuffer &name, StringAttr &value)
  7591. {
  7592. s = skipWhitespace(s);
  7593. const char *pn = strchr(s, '(');
  7594. if (pn) //strict format param('value') so we can extend later
  7595. {
  7596. const char *vp = pn + 1;
  7597. if (*vp!='\'')
  7598. return false;
  7599. const char *end =strchr(++vp, '\'');
  7600. if (!end || *(end+1)!=')')
  7601. return false;
  7602. if (!validateXMLTag(name.append(pn-s, s).trim()))
  7603. return false;
  7604. value.set(vp, end-vp);
  7605. }
  7606. else
  7607. {
  7608. if (!validateXMLTag(name.append(s).trim()))
  7609. return false;
  7610. }
  7611. return true;
  7612. }
  7613. IPropertyTree *createPTreeFromHttpPath(const char *nameWithAttrs, IPropertyTree *content, bool nestedRoot, ipt_flags flags)
  7614. {
  7615. StringArray nameAttrList;
  7616. nameAttrList.appendList(nameWithAttrs, "/");
  7617. if (!nameAttrList.ordinality())
  7618. return NULL;
  7619. Owned<IPropertyTree> pt = createPTree(nameAttrList.item(0), flags);
  7620. for (aindex_t pos=1; nameAttrList.isItem(pos); pos++)
  7621. {
  7622. StringBuffer name;
  7623. StringAttr value;
  7624. if (!checkParseUrlPathNodeValue(nameAttrList.item(pos), name, value))
  7625. throw MakeStringException(-1, "Invalid URL parameter format %s", nameAttrList.item(pos));
  7626. StringBuffer xpath("@");
  7627. xpath.append(name.str());
  7628. if (!value.get())
  7629. pt->setPropBool(xpath, true);
  7630. else
  7631. pt->setProp(xpath, value);
  7632. }
  7633. IPropertyTree *parent = pt;
  7634. const char *input = pt->queryProp("@input");
  7635. if (input)
  7636. {
  7637. StringArray inputNodes;
  7638. inputNodes.appendList(input, ".");
  7639. ForEachItemIn(in, inputNodes)
  7640. {
  7641. const char *tag = inputNodes.item(in);
  7642. if (!validateXMLTag(tag))
  7643. throw MakeStringException(-1, "Invalid REST query input specifier %s", input);
  7644. parent = parent->addPropTree(tag, createPTree(tag, flags));
  7645. }
  7646. }
  7647. if (streq("__array__", content->queryName()))
  7648. {
  7649. Owned<IAttributeIterator> aiter = content->getAttributes();
  7650. ForEach (*aiter)
  7651. parent->addProp(aiter->queryName(), aiter->queryValue());
  7652. Owned<IPropertyTreeIterator> iter = content->getElements("__item__");
  7653. ForEach (*iter)
  7654. {
  7655. IPropertyTree &e = iter->query();
  7656. e.renameProp("/", "Row");
  7657. parent->addPropTree("Row", LINK(&e));
  7658. }
  7659. }
  7660. else
  7661. mergePTree(parent, content);
  7662. if (nestedRoot)
  7663. {
  7664. Owned<IPropertyTree> root = createPTree(flags);
  7665. root->setPropTree(nameAttrList.item(0), pt.getClear());
  7666. return root.getClear();
  7667. }
  7668. return pt.getClear();
  7669. }
  7670. //URL node nameWithAttrs is of the form: "TagName/attr1('abc')/attr2/attr3('xyz')"
  7671. IPropertyTree *createPTreeFromHttpParameters(const char *nameWithAttrs, IProperties *parameters, bool skipLeadingDotParameters, bool nestedRoot, ipt_flags flags)
  7672. {
  7673. Owned<IPropertyTree> content = createPTree("content", flags);
  7674. Owned<IPropertyIterator> iter = parameters->getIterator();
  7675. ForEach(*iter)
  7676. {
  7677. StringBuffer key(iter->getPropKey());
  7678. if (!key.length() || key.charAt(key.length()-1)=='!')
  7679. continue;
  7680. if (skipLeadingDotParameters && key.charAt(0)=='.')
  7681. continue;
  7682. const char *value = parameters->queryProp(key);
  7683. if (!value || !*value)
  7684. continue;
  7685. ensureHttpParameter(content, key, value);
  7686. }
  7687. return createPTreeFromHttpPath(nameWithAttrs, content.getClear(), nestedRoot, flags);
  7688. }
  7689. IPropertyTree *createPTreeFromJSONFile(const char *filename, byte flags, PTreeReaderOptions readFlags, IPTreeMaker *iMaker)
  7690. {
  7691. Owned<IFile> in = createIFile(filename);
  7692. if (!in->exists())
  7693. return nullptr;
  7694. StringBuffer contents;
  7695. try
  7696. {
  7697. contents.loadFile(in);
  7698. }
  7699. catch (IException * e)
  7700. {
  7701. EXCLOG(e);
  7702. e->Release();
  7703. return nullptr;
  7704. }
  7705. return createPTreeFromJSONString(contents.length(), contents.str(), flags, readFlags, iMaker);
  7706. }
  7707. //---------------------------------------------------------------------------------------------------------------------
  7708. static constexpr const char * currentVersion = "1.0";
  7709. //---------------------------------------------------------------------------------------------------------------------
  7710. /*
  7711. * Use source to overwrite any changes in target
  7712. * Attributes are replaced
  7713. * Singleton elements are replaced.
  7714. * Entire arrays of scalar elements are replaced.
  7715. * Entire arrays of elements with no name attribute are replaced.
  7716. * Elements with a name attribute are matched by name. If there is a match it is merged. If there is no match it is added.
  7717. */
  7718. static bool checkInSequence(IPropertyTree & child, StringAttr &seqname, bool &first, bool &endprior)
  7719. {
  7720. first = false;
  7721. endprior = false;
  7722. if (seqname.length() && streq(seqname, child.queryName()))
  7723. return true;
  7724. endprior = !seqname.isEmpty();
  7725. if (child.isArray(nullptr))
  7726. {
  7727. first=true;
  7728. seqname.set(child.queryName());
  7729. return true;
  7730. }
  7731. seqname.clear();
  7732. return false;
  7733. }
  7734. inline bool isScalarItem(IPropertyTree &child)
  7735. {
  7736. if (child.hasChildren())
  7737. return false;
  7738. return child.getAttributeCount()==0;
  7739. }
  7740. static IPropertyTree *ensureMergeConfigTarget(IPropertyTree &target, const char *tag, const char *nameAttribute, const char *name, bool sequence)
  7741. {
  7742. StringBuffer tempPath;
  7743. const char * path = (sequence) ? nullptr : tag;
  7744. if (name && nameAttribute && *nameAttribute)
  7745. {
  7746. tempPath.append(tag).append("[").append(nameAttribute).append("=\'").append(name).append("']");
  7747. path = tempPath;
  7748. }
  7749. IPropertyTree * match = (path) ? target.queryPropTree(path) : nullptr;
  7750. if (!match)
  7751. {
  7752. if (sequence)
  7753. match = target.addPropTreeArrayItem(tag, createPTree(tag));
  7754. else
  7755. match = target.addPropTree(tag);
  7756. }
  7757. return match;
  7758. }
  7759. void mergeConfiguration(IPropertyTree & target, const IPropertyTree & source, const char *altNameAttribute, bool overwriteAttr)
  7760. {
  7761. Owned<IAttributeIterator> aiter = source.getAttributes();
  7762. ForEach(*aiter)
  7763. {
  7764. if (overwriteAttr || !target.hasProp(aiter->queryName()))
  7765. target.addProp(aiter->queryName(), aiter->queryValue());
  7766. }
  7767. StringAttr seqname;
  7768. Owned<IPropertyTreeIterator> iter = source.getElements("*");
  7769. ForEach(*iter)
  7770. {
  7771. IPropertyTree & child = iter->query();
  7772. const char * tag = child.queryName();
  7773. const char * name = child.queryProp("@name");
  7774. bool altname = false;
  7775. //Legacy support for old component configuration files that have repeated elements with no name tag but another unique id
  7776. if (!name && altNameAttribute && *altNameAttribute)
  7777. {
  7778. name = child.queryProp(altNameAttribute);
  7779. altname = name!=nullptr;
  7780. }
  7781. bool first = false;
  7782. bool endprior = false;
  7783. bool sequence = checkInSequence(child, seqname, first, endprior);
  7784. if (first && (!name || isScalarItem(child))) //arrays of unamed objects or scalars are replaced
  7785. target.removeProp(tag);
  7786. IPropertyTree * match = ensureMergeConfigTarget(target, tag, altname ? altNameAttribute : "@name", name, sequence);
  7787. mergeConfiguration(*match, child, altNameAttribute, overwriteAttr);
  7788. }
  7789. const char * sourceValue = source.queryProp("");
  7790. target.setProp("", sourceValue);
  7791. }
  7792. /*
  7793. * Load a json/yaml configuration file.
  7794. * If there is an extends tag in the root of the file then this file is applied as a delta to the base file
  7795. * the configuration is the contents of the tag within the file that matches the component tag.
  7796. */
  7797. static IPropertyTree * loadConfiguration(const char * filename, const char * componentTag, bool required, const char *altNameAttribute)
  7798. {
  7799. if (!checkFileExists(filename))
  7800. throw makeStringExceptionV(99, "Configuration file %s not found", filename);
  7801. const char * ext = pathExtension(filename);
  7802. Owned<IPropertyTree> configTree;
  7803. if (!ext || strieq(ext, ".yaml"))
  7804. {
  7805. try
  7806. {
  7807. configTree.setown(createPTreeFromYAMLFile(filename, 0, ptr_ignoreWhiteSpace, nullptr));
  7808. }
  7809. catch (IException *E)
  7810. {
  7811. StringBuffer msg;
  7812. E->errorMessage(msg);
  7813. ::Release(E);
  7814. throw makeStringExceptionV(99, "Error loading configuration file %s (invalid yaml): %s", filename, msg.str());
  7815. }
  7816. }
  7817. else
  7818. throw makeStringExceptionV(99, "Unrecognised file extension %s", ext);
  7819. if (!configTree)
  7820. throw makeStringExceptionV(99, "Error loading configuration file %s", filename);
  7821. IPropertyTree * config = configTree->queryPropTree(componentTag);
  7822. if (!config)
  7823. {
  7824. if (required)
  7825. throw makeStringExceptionV(99, "Section %s is missing from file %s", componentTag, filename);
  7826. return nullptr;
  7827. }
  7828. const char * base = configTree->queryProp("@extends");
  7829. if (!base)
  7830. return LINK(config);
  7831. StringBuffer baseFilename;
  7832. splitFilename(filename, &baseFilename, &baseFilename, nullptr, nullptr, false);
  7833. addNonEmptyPathSepChar(baseFilename);
  7834. baseFilename.append(base);
  7835. Owned<IPropertyTree> baseTree = loadConfiguration(baseFilename, componentTag, required, altNameAttribute);
  7836. mergeConfiguration(*baseTree, *config, altNameAttribute);
  7837. return LINK(baseTree);
  7838. }
  7839. static constexpr const char * envPrefix = "HPCC_CONFIG_";
  7840. static void applyEnvironmentConfig(IPropertyTree & target, const char * cptPrefix, const char * value)
  7841. {
  7842. const char * name = value;
  7843. if (!startsWith(name, envPrefix))
  7844. return;
  7845. name += strlen(envPrefix);
  7846. if (cptPrefix)
  7847. {
  7848. if (!startsWith(name, cptPrefix))
  7849. return;
  7850. name += strlen(cptPrefix);
  7851. if (*name++ != '_')
  7852. return;
  7853. }
  7854. StringBuffer propName;
  7855. if (startsWith(name, "PROP_"))
  7856. {
  7857. propName.append("@");
  7858. name += 5;
  7859. }
  7860. const char * eq = strchr(value, '=');
  7861. if (eq)
  7862. {
  7863. propName.append(eq - name, name);
  7864. target.setProp(propName, eq + 1);
  7865. }
  7866. else
  7867. {
  7868. propName.append(name);
  7869. target.setProp(propName, nullptr);
  7870. }
  7871. }
  7872. IPropertyTree * createPTreeFromYAML(const char * yaml)
  7873. {
  7874. if (*yaml == '{')
  7875. return createPTreeFromJSONString(yaml, 0, ptr_ignoreWhiteSpace, nullptr);
  7876. return createPTreeFromYAMLString(yaml, 0, ptr_ignoreWhiteSpace, nullptr);
  7877. }
  7878. static const char * extractOption(const char * option, const char * cur)
  7879. {
  7880. if (startsWith(cur, option))
  7881. {
  7882. cur += strlen(option);
  7883. if (*cur == '=')
  7884. return cur + 1;
  7885. if (*cur)
  7886. return nullptr;
  7887. return "1";
  7888. }
  7889. return nullptr;
  7890. }
  7891. static void applyCommandLineOption(IPropertyTree * config, const char * option, const char * value)
  7892. {
  7893. //Ignore -- with no following option.
  7894. if (isEmptyString(option))
  7895. return;
  7896. const char *tail;
  7897. while ((tail = strchr(option, '.')) != nullptr)
  7898. {
  7899. StringAttr elemName(option, tail-option);
  7900. if (!config->hasProp(elemName))
  7901. config = config->addPropTree(elemName);
  7902. else
  7903. {
  7904. config = config->queryPropTree(elemName);
  7905. if (!config)
  7906. throw makeStringExceptionV(99, "Cannot overriding scalar configuration element %s with structure", elemName.get());
  7907. }
  7908. option = tail+1;
  7909. }
  7910. if (!validateXMLTag(option))
  7911. throw makeStringExceptionV(99, "Invalid option name '%s'", option);
  7912. StringBuffer path;
  7913. path.append('@').append(option);
  7914. config->setProp(path, value);
  7915. }
  7916. static void applyCommandLineOption(IPropertyTree * config, const char * option, std::initializer_list<const char *> ignoreOptions)
  7917. {
  7918. const char * eq = strchr(option, '=');
  7919. StringBuffer name;
  7920. const char *val = nullptr;
  7921. if (eq)
  7922. {
  7923. name.append(eq - option, option);
  7924. option = name;
  7925. val = eq + 1;
  7926. }
  7927. else
  7928. {
  7929. //MORE: Support --x- and --x+?
  7930. val = "1";
  7931. }
  7932. if (stdContains(ignoreOptions, option))
  7933. return;
  7934. applyCommandLineOption(config, option, val);
  7935. }
  7936. static Owned<IPropertyTree> componentConfiguration;
  7937. static Owned<IPropertyTree> globalConfiguration;
  7938. MODULE_INIT(INIT_PRIORITY_STANDARD)
  7939. {
  7940. return true;
  7941. }
  7942. MODULE_EXIT()
  7943. {
  7944. componentConfiguration.clear();
  7945. globalConfiguration.clear();
  7946. }
  7947. IPropertyTree & queryComponentConfig()
  7948. {
  7949. if (!componentConfiguration)
  7950. throw makeStringException(99, "Configuration file has not yet been processed");
  7951. return *componentConfiguration;
  7952. }
  7953. IPropertyTree & queryGlobalConfig()
  7954. {
  7955. if (!globalConfiguration)
  7956. throw makeStringException(99, "Configuration file has not yet been processed");
  7957. return *globalConfiguration;
  7958. }
  7959. jlib_decl IPropertyTree * loadArgsIntoConfiguration(IPropertyTree *config, const char * * argv, std::initializer_list<const char *> ignoreOptions)
  7960. {
  7961. for (const char * * pArg = argv; *pArg; pArg++)
  7962. {
  7963. const char * cur = *pArg;
  7964. if (startsWith(cur, "--"))
  7965. applyCommandLineOption(config, cur + 2, ignoreOptions);
  7966. }
  7967. return config;
  7968. }
  7969. #ifdef _DEBUG
  7970. static void holdLoop()
  7971. {
  7972. DBGLOG("Component paused for debugging purposes, attach and set held=false to release");
  7973. bool held = true;
  7974. while (held)
  7975. Sleep(5);
  7976. }
  7977. #endif
  7978. jlib_decl IPropertyTree * loadConfiguration(IPropertyTree *componentDefault, const char * * argv, const char * componentTag, const char * envPrefix, const char * legacyFilename, IPropertyTree * (mapper)(IPropertyTree *), const char *altNameAttribute)
  7979. {
  7980. if (componentConfiguration)
  7981. throw makeStringExceptionV(99, "Configuration for component %s has already been initialised", componentTag);
  7982. Linked<IPropertyTree> config(componentDefault);
  7983. const char * optConfig = nullptr;
  7984. bool outputConfig = false;
  7985. #ifdef _DEBUG
  7986. bool held = false;
  7987. #endif
  7988. for (const char * * pArg = argv; *pArg; pArg++)
  7989. {
  7990. const char * cur = *pArg;
  7991. const char * matchConfig = extractOption("--config", cur);
  7992. if (matchConfig)
  7993. optConfig = matchConfig;
  7994. else if (strsame(cur, "--help"))
  7995. {
  7996. #if 0
  7997. //Better not to include this until it has been implemented, since it breaks eclcc
  7998. //MORE: displayHelp(config);
  7999. printf("%s <options>\n", argv[0]);
  8000. exit(0);
  8001. #endif
  8002. }
  8003. else if (strsame(cur, "--init"))
  8004. {
  8005. StringBuffer yamlText;
  8006. toYAML(componentDefault, yamlText, 0, YAML_SortTags);
  8007. printf("%s\n", yamlText.str());
  8008. exit(0);
  8009. }
  8010. else if (strsame(cur, "--outputconfig"))
  8011. {
  8012. outputConfig = true;
  8013. }
  8014. else
  8015. {
  8016. matchConfig = extractOption("--componentTag", cur);
  8017. if (matchConfig)
  8018. componentTag = matchConfig;
  8019. #ifdef _DEBUG
  8020. else
  8021. {
  8022. const char *matchHold = extractOption("--hold", cur);
  8023. if (matchHold)
  8024. {
  8025. if (strToBool(matchHold))
  8026. {
  8027. held = true;
  8028. holdLoop();
  8029. }
  8030. }
  8031. }
  8032. #endif
  8033. }
  8034. }
  8035. Owned<IPropertyTree> delta;
  8036. if (optConfig)
  8037. {
  8038. if (streq(optConfig, "1"))
  8039. throw makeStringExceptionV(99, "Name of configuration file omitted (use --config=<filename>)");
  8040. //--config= with no filename can be used to ignore the legacy configuration file
  8041. if (!isEmptyString(optConfig))
  8042. {
  8043. StringBuffer fullpath;
  8044. if (!isAbsolutePath(optConfig))
  8045. {
  8046. appendCurrentDirectory(fullpath, false);
  8047. addNonEmptyPathSepChar(fullpath);
  8048. }
  8049. fullpath.append(optConfig);
  8050. delta.setown(loadConfiguration(fullpath, componentTag, true, altNameAttribute));
  8051. globalConfiguration.setown(loadConfiguration(fullpath, "global", false, altNameAttribute));
  8052. }
  8053. }
  8054. else
  8055. {
  8056. if (legacyFilename && checkFileExists(legacyFilename))
  8057. delta.setown(createPTreeFromXMLFile(legacyFilename, ipt_caseInsensitive));
  8058. if (delta && mapper)
  8059. delta.setown(mapper(delta));
  8060. }
  8061. if (delta)
  8062. mergeConfiguration(*config, *delta, altNameAttribute);
  8063. const char * * environment = const_cast<const char * *>(getSystemEnv());
  8064. for (const char * * cur = environment; *cur; cur++)
  8065. {
  8066. applyEnvironmentConfig(*config, envPrefix, *cur);
  8067. }
  8068. if (outputConfig)
  8069. {
  8070. loadArgsIntoConfiguration(config, argv, { "config", "outputconfig" });
  8071. Owned<IPropertyTree> recreated = createPTree();
  8072. recreated->setProp("@version", currentVersion);
  8073. recreated->addPropTree(componentTag, LINK(config));
  8074. if (globalConfiguration)
  8075. recreated->addPropTree("global", globalConfiguration.getLink());
  8076. StringBuffer yamlText;
  8077. toYAML(recreated, yamlText, 0, YAML_SortTags);
  8078. printf("%s\n", yamlText.str());
  8079. exit(0);
  8080. }
  8081. else
  8082. loadArgsIntoConfiguration(config, argv);
  8083. //For legacy (and other weird cases) ensure there is a global section
  8084. if (!globalConfiguration)
  8085. globalConfiguration.setown(createPTree("global"));
  8086. #ifdef _DEBUG
  8087. // NB: don't re-hold, if CLI --hold already held.
  8088. if (!held && config->getPropBool("@hold"))
  8089. holdLoop();
  8090. #endif
  8091. unsigned ptreeMappingThreshold = globalConfiguration->getPropInt("@ptreeMappingThreshold", defaultSiblingMapThreshold);
  8092. setPTreeMappingThreshold(ptreeMappingThreshold);
  8093. componentConfiguration.set(config);
  8094. return config.getClear();
  8095. }
  8096. jlib_decl IPropertyTree * loadConfiguration(const char * defaultYaml, const char * * argv, const char * componentTag, const char * envPrefix, const char * legacyFilename, IPropertyTree * (mapper)(IPropertyTree *), const char *altNameAttribute)
  8097. {
  8098. if (componentConfiguration)
  8099. throw makeStringExceptionV(99, "Configuration for component %s has already been initialised", componentTag);
  8100. Owned<IPropertyTree> componentDefault;
  8101. if (defaultYaml)
  8102. {
  8103. Owned<IPropertyTree> defaultConfig = createPTreeFromYAML(defaultYaml);
  8104. componentDefault.set(defaultConfig->queryPropTree(componentTag));
  8105. if (!componentDefault)
  8106. throw makeStringExceptionV(99, "Default configuration does not contain the tag %s", componentTag);
  8107. }
  8108. else
  8109. componentDefault.setown(createPTree(componentTag));
  8110. return loadConfiguration(componentDefault, argv, componentTag, envPrefix, legacyFilename, mapper, altNameAttribute);
  8111. }
  8112. class CYAMLBufferReader : public CInterfaceOf<IPTreeReader>
  8113. {
  8114. protected:
  8115. Linked<IPTreeNotifyEvent> iEvent;
  8116. yaml_parser_t parser;
  8117. PTreeReaderOptions readerOptions = ptr_none;
  8118. bool noRoot = false;
  8119. public:
  8120. CYAMLBufferReader(const void *buf, size32_t bufLength, IPTreeNotifyEvent &_iEvent, PTreeReaderOptions _readerOptions) :
  8121. iEvent(&_iEvent), readerOptions(_readerOptions)
  8122. {
  8123. if (!yaml_parser_initialize(&parser))
  8124. throw makeStringException(99, "Filed to initialize libyaml parser");
  8125. yaml_parser_set_input_string(&parser, (const unsigned char *)buf, bufLength);
  8126. noRoot = 0 != ((unsigned)readerOptions & (unsigned)ptr_noRoot);
  8127. }
  8128. ~CYAMLBufferReader()
  8129. {
  8130. yaml_parser_delete(&parser);
  8131. }
  8132. yaml_event_type_t nextEvent(yaml_event_t &event, yaml_event_type_t final=YAML_NO_EVENT, yaml_event_type_t expected=YAML_NO_EVENT, const char *error="")
  8133. {
  8134. if (!yaml_parser_parse(&parser, &event))
  8135. throw makeStringExceptionV(99, "libyaml parser error %s", parser.problem);
  8136. if (event.type!=final && expected!=YAML_NO_EVENT && event.type!=expected)
  8137. throw makeStringExceptionV(99, "libyaml parser %s", error);
  8138. return event.type;
  8139. }
  8140. virtual void loadSequence(const char *tagname)
  8141. {
  8142. if (!tagname || !*tagname) //if unmapped (unnamed) sequences are possible have to decide how to name them in the ptree, later
  8143. throw makeStringException(99, "libyaml parser expected sequence name");
  8144. yaml_event_t event;
  8145. yaml_event_type_t eventType = YAML_NO_EVENT;
  8146. while (eventType!=YAML_SEQUENCE_END_EVENT)
  8147. {
  8148. eventType = nextEvent(event);
  8149. switch (eventType)
  8150. {
  8151. case YAML_MAPPING_START_EVENT: //child map
  8152. loadMap(tagname, true);
  8153. break;
  8154. case YAML_SEQUENCE_START_EVENT:
  8155. //todo
  8156. break;
  8157. case YAML_SCALAR_EVENT:
  8158. iEvent->beginNode(tagname, true, parser.offset);
  8159. iEvent->endNode(tagname, event.data.scalar.length, (const void *)event.data.scalar.value, false, parser.offset);
  8160. break;
  8161. case YAML_ALIAS_EVENT: //reference to an anchor, ignore for now
  8162. iEvent->beginNode(tagname, true, parser.offset);
  8163. iEvent->endNode(tagname, 0, nullptr, false, parser.offset);
  8164. break;
  8165. case YAML_SEQUENCE_END_EVENT: //done
  8166. break;
  8167. case YAML_NO_EVENT:
  8168. case YAML_MAPPING_END_EVENT:
  8169. case YAML_STREAM_START_EVENT:
  8170. case YAML_STREAM_END_EVENT:
  8171. case YAML_DOCUMENT_START_EVENT:
  8172. case YAML_DOCUMENT_END_EVENT:
  8173. default:
  8174. //shouldn't be here
  8175. break;
  8176. }
  8177. yaml_event_delete(&event);
  8178. }
  8179. }
  8180. virtual void loadMap(const char *tagname, bool sequence)
  8181. {
  8182. bool binaryContent = false;
  8183. StringBuffer content;
  8184. if (tagname && *tagname)
  8185. iEvent->beginNode(tagname, sequence, parser.offset);
  8186. yaml_event_t event;
  8187. yaml_event_type_t eventType = YAML_NO_EVENT;
  8188. while (eventType!=YAML_MAPPING_END_EVENT)
  8189. {
  8190. eventType = nextEvent(event, YAML_MAPPING_END_EVENT, YAML_SCALAR_EVENT, "expected map to start with scalar name");
  8191. if (eventType==YAML_MAPPING_END_EVENT)
  8192. {
  8193. yaml_event_delete(&event);
  8194. continue;
  8195. }
  8196. StringBuffer attname('@');
  8197. attname.append(event.data.scalar.length, (const char *)event.data.scalar.value);
  8198. const char *elname = attname.str()+1;
  8199. yaml_event_delete(&event);
  8200. eventType = nextEvent(event);
  8201. switch (eventType)
  8202. {
  8203. case YAML_MAPPING_START_EVENT: //child map
  8204. loadMap(elname, false);
  8205. break;
  8206. case YAML_SEQUENCE_START_EVENT:
  8207. loadSequence(elname);
  8208. break;
  8209. case YAML_SCALAR_EVENT:
  8210. {
  8211. //!el or !element will be our local tag (custom schema type) for an element
  8212. //ptree toYAML should set this for element scalars, and parent text content
  8213. const char *tag = (const char *)event.data.scalar.tag;
  8214. if (tag && (streq(tag, "!binary") || streq(tag, "!!binary")))
  8215. {
  8216. if (streq(elname, "^")) //text content of parent node
  8217. {
  8218. binaryContent = true;
  8219. JBASE64_Decode((const char *) event.data.scalar.value, content.clear());
  8220. }
  8221. else
  8222. {
  8223. StringBuffer decoded;
  8224. JBASE64_Decode((const char *) event.data.scalar.value, decoded);
  8225. iEvent->beginNode(elname, false, parser.offset);
  8226. iEvent->endNode(elname, decoded.length(), (const void *) decoded.str(), true, parser.offset);
  8227. }
  8228. }
  8229. else if (streq(elname, "^")) //text content of parent node
  8230. {
  8231. content.set((const char *) event.data.scalar.value);
  8232. }
  8233. else if (tag && (streq(tag, "!el") || streq(tag, "!element")))
  8234. {
  8235. iEvent->beginNode(elname, false, parser.offset);
  8236. iEvent->endNode(elname, event.data.scalar.length, (const void *) event.data.scalar.value, false, parser.offset);
  8237. }
  8238. else //by default all named scalars are ptree attributes
  8239. {
  8240. iEvent->newAttribute(attname, (const char *)event.data.scalar.value);
  8241. }
  8242. break;
  8243. }
  8244. case YAML_ALIAS_EVENT: //reference to an anchor, ignore for now
  8245. iEvent->beginNode(elname, false, parser.offset);
  8246. iEvent->endNode(elname, 0, nullptr, false, parser.offset);
  8247. break;
  8248. case YAML_MAPPING_END_EVENT: //done
  8249. break;
  8250. case YAML_NO_EVENT:
  8251. case YAML_SEQUENCE_END_EVENT:
  8252. case YAML_STREAM_START_EVENT:
  8253. case YAML_STREAM_END_EVENT:
  8254. case YAML_DOCUMENT_START_EVENT:
  8255. case YAML_DOCUMENT_END_EVENT:
  8256. default:
  8257. //shouldn't be here
  8258. break;
  8259. }
  8260. yaml_event_delete(&event);
  8261. }
  8262. if (tagname && *tagname)
  8263. iEvent->endNode(tagname, content.length(), content, binaryContent, parser.offset);
  8264. }
  8265. virtual void load() override
  8266. {
  8267. yaml_event_t event;
  8268. yaml_event_type_t eventType = YAML_NO_EVENT;
  8269. bool doc = false;
  8270. bool content = false;
  8271. while (eventType!=YAML_STREAM_END_EVENT)
  8272. {
  8273. eventType = nextEvent(event);
  8274. switch (eventType)
  8275. {
  8276. case YAML_MAPPING_START_EVENT:
  8277. //root content, the start of all mappings, should be only one at the root
  8278. if (content)
  8279. throw makeStringException(99, "YAML: Currently only support one content section (map) per stream");
  8280. loadMap(noRoot ? nullptr : "__object__", false); //root map
  8281. content=true;
  8282. break;
  8283. case YAML_SEQUENCE_START_EVENT:
  8284. //root content, sequence (array), should be only one at the root and can't mix with mappings
  8285. if (content)
  8286. throw makeStringException(99, "YAML: Currently only support one content section (sequence) per stream");
  8287. if (!noRoot)
  8288. iEvent->beginNode("__array__", false, 0);
  8289. loadSequence("__item__");
  8290. if (!noRoot)
  8291. iEvent->endNode("__array__", 0, nullptr, false, parser.offset);
  8292. content=true;
  8293. break;
  8294. case YAML_STREAM_START_EVENT:
  8295. case YAML_STREAM_END_EVENT:
  8296. //don't think we need to do anything... unless we start saving hints
  8297. break;
  8298. case YAML_DOCUMENT_START_EVENT:
  8299. //should only support one? multiple documents would imply an extra level of nesting (future flag?)
  8300. if (doc)
  8301. throw makeStringException(99, "YAML: Currently only support one document per stream");
  8302. doc=true;
  8303. break;
  8304. case YAML_DOCUMENT_END_EVENT:
  8305. break;
  8306. case YAML_NO_EVENT:
  8307. case YAML_ALIAS_EVENT: //root alias?
  8308. case YAML_MAPPING_END_EVENT:
  8309. case YAML_SCALAR_EVENT: //root unmapped (unnamed) scalars?
  8310. case YAML_SEQUENCE_END_EVENT:
  8311. //shouldn't be here
  8312. break;
  8313. default:
  8314. break;
  8315. }
  8316. yaml_event_delete(&event);
  8317. }
  8318. }
  8319. virtual offset_t queryOffset() override
  8320. {
  8321. return parser.offset;
  8322. }
  8323. };
  8324. IPTreeReader *createYAMLBufferReader(const void *buf, size32_t bufLength, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions)
  8325. {
  8326. return new CYAMLBufferReader(buf, bufLength, iEvent, readerOptions);
  8327. }
  8328. IPropertyTree *createPTreeFromYAMLString(unsigned len, const char *yaml, byte flags, PTreeReaderOptions readFlags, IPTreeMaker *iMaker)
  8329. {
  8330. Owned<IPTreeMaker> _iMaker;
  8331. if (!iMaker)
  8332. {
  8333. iMaker = createDefaultPTreeMaker(flags, readFlags);
  8334. _iMaker.setown(iMaker);
  8335. }
  8336. Owned<IPTreeReader> reader = createYAMLBufferReader(yaml, len, *iMaker, readFlags);
  8337. reader->load();
  8338. return LINK(iMaker->queryRoot());
  8339. }
  8340. IPropertyTree *createPTreeFromYAMLString(const char *yaml, byte flags, PTreeReaderOptions readFlags, IPTreeMaker *iMaker)
  8341. {
  8342. return createPTreeFromYAMLString(strlen(yaml), yaml, flags, readFlags, iMaker);
  8343. }
  8344. IPropertyTree *createPTreeFromYAMLFile(const char *filename, byte flags, PTreeReaderOptions readFlags, IPTreeMaker *iMaker)
  8345. {
  8346. Owned<IFile> in = createIFile(filename);
  8347. if (!in->exists())
  8348. return nullptr;
  8349. StringBuffer contents;
  8350. try
  8351. {
  8352. contents.loadFile(in);
  8353. }
  8354. catch (IException * e)
  8355. {
  8356. EXCLOG(e);
  8357. e->Release();
  8358. return nullptr;
  8359. }
  8360. return createPTreeFromYAMLString(contents.length(), contents, flags, readFlags, iMaker);
  8361. }
  8362. static int yaml_write_iiostream(void *data, unsigned char *buffer, size_t size)
  8363. {
  8364. IIOStream *out = (IIOStream *) data;
  8365. out->write(size, (void *)buffer);
  8366. out->flush();
  8367. return 1;
  8368. }
  8369. class YAMLEmitter
  8370. {
  8371. yaml_emitter_t emitter;
  8372. yaml_event_t event;
  8373. IIOStream &out;
  8374. public:
  8375. YAMLEmitter(IIOStream &ios, int indent) : out(ios)
  8376. {
  8377. if (!yaml_emitter_initialize(&emitter))
  8378. throw MakeStringException(0, "YAMLEmitter: failed to initialize");
  8379. yaml_emitter_set_output(&emitter, yaml_write_iiostream, &out);
  8380. yaml_emitter_set_canonical(&emitter, false);
  8381. yaml_emitter_set_unicode(&emitter, true);
  8382. yaml_emitter_set_indent(&emitter, indent);
  8383. beginStream();
  8384. beginDocument();
  8385. }
  8386. ~YAMLEmitter()
  8387. {
  8388. endDocument();
  8389. endStream();
  8390. yaml_emitter_delete(&emitter);
  8391. }
  8392. yaml_char_t *getTag(bool binary, bool element)
  8393. {
  8394. if (binary)
  8395. return (yaml_char_t *) "!binary";
  8396. if (element)
  8397. return (yaml_char_t *) "!el";
  8398. return nullptr;
  8399. }
  8400. void emit()
  8401. {
  8402. yaml_emitter_emit(&emitter, &event);
  8403. }
  8404. void checkInit(int success, const char *descr)
  8405. {
  8406. if (success==0)
  8407. throw MakeStringException(0, "YAMLEmitter: %s failed", descr);
  8408. }
  8409. void writeValue(const char *value, bool element, bool hidden, bool binary)
  8410. {
  8411. yaml_scalar_style_t style = binary ? YAML_LITERAL_SCALAR_STYLE : YAML_ANY_SCALAR_STYLE;
  8412. const yaml_char_t *tag = getTag(binary, element);
  8413. bool implicit = tag==nullptr;
  8414. StringBuffer s;
  8415. if (!value)
  8416. value = "null";
  8417. else if (hidden)
  8418. value = (binary) ? "KioqKg==" : s.appendN(strlen(value), '*').str(); //KioqKg== is base64 of ****
  8419. checkInit(yaml_scalar_event_initialize(&event, nullptr, tag, (yaml_char_t *) value, -1, implicit, implicit, style), "yaml_scalar_event_initialize");
  8420. emit();
  8421. }
  8422. void writeName(const char *name)
  8423. {
  8424. dbgassertex(name!=nullptr);
  8425. return writeValue(name, false, false,false);
  8426. }
  8427. void writeNamedValue(const char *name, const char *value, bool element, bool hidden)
  8428. {
  8429. writeName(name);
  8430. writeValue(value, element, hidden, false);
  8431. }
  8432. void writeAttribute(const char *name, const char *value, bool hidden)
  8433. {
  8434. writeNamedValue(name, value, false, hidden);
  8435. }
  8436. void beginMap()
  8437. {
  8438. checkInit(yaml_mapping_start_event_initialize(&event, nullptr, nullptr, 0, YAML_BLOCK_MAPPING_STYLE), "yaml_mapping_start_event_initialize");
  8439. emit();
  8440. }
  8441. void endMap()
  8442. {
  8443. checkInit(yaml_mapping_end_event_initialize(&event), "yaml_mapping_end_event_initialize");
  8444. emit();
  8445. }
  8446. void beginSequence(const char *name)
  8447. {
  8448. if (name)
  8449. writeName(name);
  8450. checkInit(yaml_sequence_start_event_initialize(&event, nullptr, nullptr, 0, YAML_ANY_SEQUENCE_STYLE), "yaml_sequence_start_event_initialize");
  8451. emit();
  8452. }
  8453. void endSequence()
  8454. {
  8455. checkInit(yaml_sequence_end_event_initialize(&event), "yaml_sequence_end_event_initialize");
  8456. emit();
  8457. }
  8458. void beginDocument()
  8459. {
  8460. checkInit(yaml_document_start_event_initialize(&event, nullptr, nullptr, nullptr, true), "yaml_document_start_event_initialize");
  8461. emit();
  8462. }
  8463. void endDocument()
  8464. {
  8465. checkInit(yaml_document_end_event_initialize(&event, true), "yaml_document_end_event_initialize");
  8466. emit();
  8467. }
  8468. void beginStream()
  8469. {
  8470. checkInit(yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING), "yaml_stream_start_event_initialize");
  8471. emit();
  8472. }
  8473. void endStream()
  8474. {
  8475. checkInit(yaml_stream_end_event_initialize(&event), "yaml_stream_end_event_initialize");
  8476. emit();
  8477. }
  8478. };
  8479. static void _toYAML(const IPropertyTree *tree, YAMLEmitter &yaml, byte flags, bool root=false, bool isArrayItem=false)
  8480. {
  8481. const char *name = tree->queryName();
  8482. if (!root && !isArrayItem)
  8483. {
  8484. if (!name || !*name)
  8485. name = "__unnamed__";
  8486. yaml.writeName(name);
  8487. }
  8488. Owned<IAttributeIterator> it = tree->getAttributes(true);
  8489. bool hasAttributes = it->first();
  8490. bool complex = (hasAttributes || tree->hasChildren());
  8491. bool hiddenRootArrayObject = isRootArrayObjectHidden(root, name, flags);
  8492. if (!hiddenRootArrayObject)
  8493. {
  8494. if (complex)
  8495. yaml.beginMap();
  8496. if (hasAttributes)
  8497. {
  8498. ForEach(*it)
  8499. {
  8500. const char *key = it->queryName()+1;
  8501. const char *val = it->queryValue();
  8502. yaml.writeAttribute(key, val, isSanitizedAndHidden(val, flags, true));
  8503. }
  8504. }
  8505. }
  8506. StringBuffer _content;
  8507. const char *content = nullptr; // to avoid uninitialized warning
  8508. bool isBinary = tree->isBinary(NULL);
  8509. bool isNull = true;
  8510. if (!hiddenRootArrayObject)
  8511. {
  8512. if (isBinary)
  8513. {
  8514. MemoryBuffer thislevelbin;
  8515. isNull = (!tree->getPropBin(NULL, thislevelbin))||(thislevelbin.length()==0);
  8516. if (!isNull)
  8517. JBASE64_Encode(thislevelbin.toByteArray(), thislevelbin.length(), _content, true);
  8518. content = _content.str();
  8519. }
  8520. else if (tree->isCompressed(NULL))
  8521. {
  8522. isNull = false; // can't be empty if compressed;
  8523. verifyex(tree->getProp(NULL, _content));
  8524. content = _content.str();
  8525. }
  8526. else
  8527. isNull = (NULL == (content = tree->queryProp(NULL)));
  8528. if (isNull && !root && !complex)
  8529. {
  8530. yaml.writeValue("null", false, false, false);
  8531. return;
  8532. }
  8533. }
  8534. Owned<IPropertyTreeIterator> sub = tree->getElements(hiddenRootArrayObject ? "__item__" : "*", 0 != (flags & YAML_SortTags) ? iptiter_sort : iptiter_null);
  8535. //note that detection of repeating elements relies on the fact that ptree elements
  8536. //of the same name will be grouped together
  8537. StringAttr seqname;
  8538. bool sequence = false;
  8539. ForEach(*sub)
  8540. {
  8541. IPropertyTree &element = sub->query();
  8542. bool first = false;
  8543. bool endprior = false;
  8544. sequence = checkInSequence(element, seqname, first, endprior);
  8545. if (endprior)
  8546. yaml.endSequence();
  8547. if (first)
  8548. yaml.beginSequence(hiddenRootArrayObject ? nullptr : element.queryName());
  8549. _toYAML(&element, yaml, flags, false, sequence);
  8550. }
  8551. if (sequence)
  8552. yaml.endSequence();
  8553. if (!isNull)
  8554. {
  8555. if (complex)
  8556. yaml.writeName("^");
  8557. //repeating/array/sequence items are implicitly elements, no need for tag
  8558. yaml.writeValue(content, isArrayItem ? false : true, isSanitizedAndHidden(content, flags, false), isBinary);
  8559. }
  8560. if (!hiddenRootArrayObject && complex)
  8561. yaml.endMap();
  8562. }
  8563. static void _toYAML(const IPropertyTree *tree, IIOStream &out, unsigned indent, byte flags, bool root=false, bool isArrayItem=false)
  8564. {
  8565. YAMLEmitter yaml(out, indent);
  8566. _toYAML(tree, yaml, flags, true, false);
  8567. }
  8568. jlib_decl StringBuffer &toYAML(const IPropertyTree *tree, StringBuffer &ret, unsigned indent, byte flags)
  8569. {
  8570. CStringBufferMarkupIOAdapter adapter(ret);
  8571. _toYAML(tree->queryBranch(NULL), adapter, indent, flags, true);
  8572. return ret;
  8573. }
  8574. void toYAML(const IPropertyTree *tree, IIOStream &out, unsigned indent, byte flags)
  8575. {
  8576. _toYAML(tree, out, indent, flags, true);
  8577. }
  8578. void printYAML(const IPropertyTree *tree, unsigned indent, unsigned flags)
  8579. {
  8580. StringBuffer yaml;
  8581. toYAML(tree, yaml, indent, flags);
  8582. printf("%s", yaml.str());
  8583. }
  8584. void dbglogYAML(const IPropertyTree *tree, unsigned indent, unsigned flags)
  8585. {
  8586. StringBuffer yaml;
  8587. toYAML(tree, yaml, indent, flags);
  8588. DBGLOG("%s", yaml.str());
  8589. }
  8590. void saveYAML(const char *filename, const IPropertyTree *tree, unsigned indent, unsigned flags)
  8591. {
  8592. OwnedIFile ifile = createIFile(filename);
  8593. saveYAML(*ifile, tree, indent, flags);
  8594. }
  8595. void saveYAML(IFile &ifile, const IPropertyTree *tree, unsigned indent, unsigned flags)
  8596. {
  8597. OwnedIFileIO ifileio = ifile.open(IFOcreate);
  8598. if (!ifileio)
  8599. throw MakeStringException(0, "saveXML: could not find %s to open", ifile.queryFilename());
  8600. saveYAML(*ifileio, tree, indent, flags);
  8601. }
  8602. void saveYAML(IFileIO &ifileio, const IPropertyTree *tree, unsigned indent, unsigned flags)
  8603. {
  8604. Owned<IIOStream> stream = createIOStream(&ifileio);
  8605. stream.setown(createBufferedIOStream(stream));
  8606. saveYAML(*stream, tree, indent, flags);
  8607. }
  8608. void saveYAML(IIOStream &stream, const IPropertyTree *tree, unsigned indent, unsigned flags)
  8609. {
  8610. toYAML(tree, stream, indent, flags);
  8611. }
  8612. jlib_decl IPropertyTree * queryCostsConfiguration()
  8613. {
  8614. return queryComponentConfig().queryPropTree("costs");
  8615. }