tree.c 260 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219
  1. /*
  2. * tree.c : implementation of access function for an XML tree.
  3. *
  4. * References:
  5. * XHTML 1.0 W3C REC: http://www.w3.org/TR/2002/REC-xhtml1-20020801/
  6. *
  7. * See Copyright for the status of this software.
  8. *
  9. * daniel@veillard.com
  10. *
  11. */
  12. /* To avoid EBCDIC trouble when parsing on zOS */
  13. #if defined(__MVS__)
  14. #pragma convert("ISO8859-1")
  15. #endif
  16. #define IN_LIBXML
  17. #include "libxml.h"
  18. #include <string.h> /* for memset() only ! */
  19. #include <stddef.h>
  20. #include <limits.h>
  21. #include <ctype.h>
  22. #include <stdlib.h>
  23. #ifdef LIBXML_ZLIB_ENABLED
  24. #include <zlib.h>
  25. #endif
  26. #include <libxml/xmlmemory.h>
  27. #include <libxml/tree.h>
  28. #include <libxml/parser.h>
  29. #include <libxml/uri.h>
  30. #include <libxml/entities.h>
  31. #include <libxml/valid.h>
  32. #include <libxml/xmlerror.h>
  33. #include <libxml/parserInternals.h>
  34. #include <libxml/globals.h>
  35. #ifdef LIBXML_HTML_ENABLED
  36. #include <libxml/HTMLtree.h>
  37. #endif
  38. #ifdef LIBXML_DEBUG_ENABLED
  39. #include <libxml/debugXML.h>
  40. #endif
  41. #include "buf.h"
  42. #include "save.h"
  43. int __xmlRegisterCallbacks = 0;
  44. /************************************************************************
  45. * *
  46. * Forward declarations *
  47. * *
  48. ************************************************************************/
  49. static xmlNsPtr
  50. xmlNewReconciledNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns);
  51. static xmlChar* xmlGetPropNodeValueInternal(const xmlAttr *prop);
  52. /************************************************************************
  53. * *
  54. * Tree memory error handler *
  55. * *
  56. ************************************************************************/
  57. /**
  58. * xmlTreeErrMemory:
  59. * @extra: extra information
  60. *
  61. * Handle an out of memory condition
  62. */
  63. static void
  64. xmlTreeErrMemory(const char *extra)
  65. {
  66. __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra);
  67. }
  68. /**
  69. * xmlTreeErr:
  70. * @code: the error number
  71. * @extra: extra information
  72. *
  73. * Handle an out of memory condition
  74. */
  75. static void
  76. xmlTreeErr(int code, xmlNodePtr node, const char *extra)
  77. {
  78. const char *msg = NULL;
  79. switch(code) {
  80. case XML_TREE_INVALID_HEX:
  81. msg = "invalid hexadecimal character value\n";
  82. break;
  83. case XML_TREE_INVALID_DEC:
  84. msg = "invalid decimal character value\n";
  85. break;
  86. case XML_TREE_UNTERMINATED_ENTITY:
  87. msg = "unterminated entity reference %15s\n";
  88. break;
  89. case XML_TREE_NOT_UTF8:
  90. msg = "string is not in UTF-8\n";
  91. break;
  92. default:
  93. msg = "unexpected error number\n";
  94. }
  95. __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra);
  96. }
  97. /************************************************************************
  98. * *
  99. * A few static variables and macros *
  100. * *
  101. ************************************************************************/
  102. /* #undef xmlStringText */
  103. const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
  104. /* #undef xmlStringTextNoenc */
  105. const xmlChar xmlStringTextNoenc[] =
  106. { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 };
  107. /* #undef xmlStringComment */
  108. const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
  109. static int xmlCompressMode = 0;
  110. static int xmlCheckDTD = 1;
  111. #define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \
  112. xmlNodePtr ulccur = (n)->children; \
  113. if (ulccur == NULL) { \
  114. (n)->last = NULL; \
  115. } else { \
  116. while (ulccur->next != NULL) { \
  117. ulccur->parent = (n); \
  118. ulccur = ulccur->next; \
  119. } \
  120. ulccur->parent = (n); \
  121. (n)->last = ulccur; \
  122. }}
  123. #define IS_STR_XML(str) ((str != NULL) && (str[0] == 'x') && \
  124. (str[1] == 'm') && (str[2] == 'l') && (str[3] == 0))
  125. /* #define DEBUG_BUFFER */
  126. /* #define DEBUG_TREE */
  127. /************************************************************************
  128. * *
  129. * Functions to move to entities.c once the *
  130. * API freeze is smoothen and they can be made public. *
  131. * *
  132. ************************************************************************/
  133. #include <libxml/hash.h>
  134. #ifdef LIBXML_TREE_ENABLED
  135. /**
  136. * xmlGetEntityFromDtd:
  137. * @dtd: A pointer to the DTD to search
  138. * @name: The entity name
  139. *
  140. * Do an entity lookup in the DTD entity hash table and
  141. * return the corresponding entity, if found.
  142. *
  143. * Returns A pointer to the entity structure or NULL if not found.
  144. */
  145. static xmlEntityPtr
  146. xmlGetEntityFromDtd(const xmlDtd *dtd, const xmlChar *name) {
  147. xmlEntitiesTablePtr table;
  148. if((dtd != NULL) && (dtd->entities != NULL)) {
  149. table = (xmlEntitiesTablePtr) dtd->entities;
  150. return((xmlEntityPtr) xmlHashLookup(table, name));
  151. /* return(xmlGetEntityFromTable(table, name)); */
  152. }
  153. return(NULL);
  154. }
  155. /**
  156. * xmlGetParameterEntityFromDtd:
  157. * @dtd: A pointer to the DTD to search
  158. * @name: The entity name
  159. *
  160. * Do an entity lookup in the DTD parameter entity hash table and
  161. * return the corresponding entity, if found.
  162. *
  163. * Returns A pointer to the entity structure or NULL if not found.
  164. */
  165. static xmlEntityPtr
  166. xmlGetParameterEntityFromDtd(const xmlDtd *dtd, const xmlChar *name) {
  167. xmlEntitiesTablePtr table;
  168. if ((dtd != NULL) && (dtd->pentities != NULL)) {
  169. table = (xmlEntitiesTablePtr) dtd->pentities;
  170. return((xmlEntityPtr) xmlHashLookup(table, name));
  171. /* return(xmlGetEntityFromTable(table, name)); */
  172. }
  173. return(NULL);
  174. }
  175. #endif /* LIBXML_TREE_ENABLED */
  176. /************************************************************************
  177. * *
  178. * QName handling helper *
  179. * *
  180. ************************************************************************/
  181. /**
  182. * xmlBuildQName:
  183. * @ncname: the Name
  184. * @prefix: the prefix
  185. * @memory: preallocated memory
  186. * @len: preallocated memory length
  187. *
  188. * Builds the QName @prefix:@ncname in @memory if there is enough space
  189. * and prefix is not NULL nor empty, otherwise allocate a new string.
  190. * If prefix is NULL or empty it returns ncname.
  191. *
  192. * Returns the new string which must be freed by the caller if different from
  193. * @memory and @ncname or NULL in case of error
  194. */
  195. xmlChar *
  196. xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix,
  197. xmlChar *memory, int len) {
  198. int lenn, lenp;
  199. xmlChar *ret;
  200. if (ncname == NULL) return(NULL);
  201. if (prefix == NULL) return((xmlChar *) ncname);
  202. lenn = strlen((char *) ncname);
  203. lenp = strlen((char *) prefix);
  204. if ((memory == NULL) || (len < lenn + lenp + 2)) {
  205. ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
  206. if (ret == NULL) {
  207. xmlTreeErrMemory("building QName");
  208. return(NULL);
  209. }
  210. } else {
  211. ret = memory;
  212. }
  213. memcpy(&ret[0], prefix, lenp);
  214. ret[lenp] = ':';
  215. memcpy(&ret[lenp + 1], ncname, lenn);
  216. ret[lenn + lenp + 1] = 0;
  217. return(ret);
  218. }
  219. /**
  220. * xmlSplitQName2:
  221. * @name: the full QName
  222. * @prefix: a xmlChar **
  223. *
  224. * parse an XML qualified name string
  225. *
  226. * [NS 5] QName ::= (Prefix ':')? LocalPart
  227. *
  228. * [NS 6] Prefix ::= NCName
  229. *
  230. * [NS 7] LocalPart ::= NCName
  231. *
  232. * Returns NULL if the name doesn't have a prefix. Otherwise, returns the
  233. * local part, and prefix is updated to get the Prefix. Both the return value
  234. * and the prefix must be freed by the caller.
  235. */
  236. xmlChar *
  237. xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
  238. int len = 0;
  239. xmlChar *ret = NULL;
  240. if (prefix == NULL) return(NULL);
  241. *prefix = NULL;
  242. if (name == NULL) return(NULL);
  243. #ifndef XML_XML_NAMESPACE
  244. /* xml: prefix is not really a namespace */
  245. if ((name[0] == 'x') && (name[1] == 'm') &&
  246. (name[2] == 'l') && (name[3] == ':'))
  247. return(NULL);
  248. #endif
  249. /* nasty but valid */
  250. if (name[0] == ':')
  251. return(NULL);
  252. /*
  253. * we are not trying to validate but just to cut, and yes it will
  254. * work even if this is as set of UTF-8 encoded chars
  255. */
  256. while ((name[len] != 0) && (name[len] != ':'))
  257. len++;
  258. if (name[len] == 0)
  259. return(NULL);
  260. *prefix = xmlStrndup(name, len);
  261. if (*prefix == NULL) {
  262. xmlTreeErrMemory("QName split");
  263. return(NULL);
  264. }
  265. ret = xmlStrdup(&name[len + 1]);
  266. if (ret == NULL) {
  267. xmlTreeErrMemory("QName split");
  268. if (*prefix != NULL) {
  269. xmlFree(*prefix);
  270. *prefix = NULL;
  271. }
  272. return(NULL);
  273. }
  274. return(ret);
  275. }
  276. /**
  277. * xmlSplitQName3:
  278. * @name: the full QName
  279. * @len: an int *
  280. *
  281. * parse an XML qualified name string,i
  282. *
  283. * returns NULL if it is not a Qualified Name, otherwise, update len
  284. * with the length in byte of the prefix and return a pointer
  285. * to the start of the name without the prefix
  286. */
  287. const xmlChar *
  288. xmlSplitQName3(const xmlChar *name, int *len) {
  289. int l = 0;
  290. if (name == NULL) return(NULL);
  291. if (len == NULL) return(NULL);
  292. /* nasty but valid */
  293. if (name[0] == ':')
  294. return(NULL);
  295. /*
  296. * we are not trying to validate but just to cut, and yes it will
  297. * work even if this is as set of UTF-8 encoded chars
  298. */
  299. while ((name[l] != 0) && (name[l] != ':'))
  300. l++;
  301. if (name[l] == 0)
  302. return(NULL);
  303. *len = l;
  304. return(&name[l+1]);
  305. }
  306. /************************************************************************
  307. * *
  308. * Check Name, NCName and QName strings *
  309. * *
  310. ************************************************************************/
  311. #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
  312. #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_DEBUG_ENABLED) || defined (LIBXML_HTML_ENABLED) || defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) || defined(LIBXML_WRITER_ENABLED) || defined(LIBXML_LEGACY_ENABLED)
  313. /**
  314. * xmlValidateNCName:
  315. * @value: the value to check
  316. * @space: allow spaces in front and end of the string
  317. *
  318. * Check that a value conforms to the lexical space of NCName
  319. *
  320. * Returns 0 if this validates, a positive error code number otherwise
  321. * and -1 in case of internal or API error.
  322. */
  323. int
  324. xmlValidateNCName(const xmlChar *value, int space) {
  325. const xmlChar *cur = value;
  326. int c,l;
  327. if (value == NULL)
  328. return(-1);
  329. /*
  330. * First quick algorithm for ASCII range
  331. */
  332. if (space)
  333. while (IS_BLANK_CH(*cur)) cur++;
  334. if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
  335. (*cur == '_'))
  336. cur++;
  337. else
  338. goto try_complex;
  339. while (((*cur >= 'a') && (*cur <= 'z')) ||
  340. ((*cur >= 'A') && (*cur <= 'Z')) ||
  341. ((*cur >= '0') && (*cur <= '9')) ||
  342. (*cur == '_') || (*cur == '-') || (*cur == '.'))
  343. cur++;
  344. if (space)
  345. while (IS_BLANK_CH(*cur)) cur++;
  346. if (*cur == 0)
  347. return(0);
  348. try_complex:
  349. /*
  350. * Second check for chars outside the ASCII range
  351. */
  352. cur = value;
  353. c = CUR_SCHAR(cur, l);
  354. if (space) {
  355. while (IS_BLANK(c)) {
  356. cur += l;
  357. c = CUR_SCHAR(cur, l);
  358. }
  359. }
  360. if ((!IS_LETTER(c)) && (c != '_'))
  361. return(1);
  362. cur += l;
  363. c = CUR_SCHAR(cur, l);
  364. while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
  365. (c == '-') || (c == '_') || IS_COMBINING(c) ||
  366. IS_EXTENDER(c)) {
  367. cur += l;
  368. c = CUR_SCHAR(cur, l);
  369. }
  370. if (space) {
  371. while (IS_BLANK(c)) {
  372. cur += l;
  373. c = CUR_SCHAR(cur, l);
  374. }
  375. }
  376. if (c != 0)
  377. return(1);
  378. return(0);
  379. }
  380. #endif
  381. #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
  382. /**
  383. * xmlValidateQName:
  384. * @value: the value to check
  385. * @space: allow spaces in front and end of the string
  386. *
  387. * Check that a value conforms to the lexical space of QName
  388. *
  389. * Returns 0 if this validates, a positive error code number otherwise
  390. * and -1 in case of internal or API error.
  391. */
  392. int
  393. xmlValidateQName(const xmlChar *value, int space) {
  394. const xmlChar *cur = value;
  395. int c,l;
  396. if (value == NULL)
  397. return(-1);
  398. /*
  399. * First quick algorithm for ASCII range
  400. */
  401. if (space)
  402. while (IS_BLANK_CH(*cur)) cur++;
  403. if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
  404. (*cur == '_'))
  405. cur++;
  406. else
  407. goto try_complex;
  408. while (((*cur >= 'a') && (*cur <= 'z')) ||
  409. ((*cur >= 'A') && (*cur <= 'Z')) ||
  410. ((*cur >= '0') && (*cur <= '9')) ||
  411. (*cur == '_') || (*cur == '-') || (*cur == '.'))
  412. cur++;
  413. if (*cur == ':') {
  414. cur++;
  415. if (((*cur >= 'a') && (*cur <= 'z')) ||
  416. ((*cur >= 'A') && (*cur <= 'Z')) ||
  417. (*cur == '_'))
  418. cur++;
  419. else
  420. goto try_complex;
  421. while (((*cur >= 'a') && (*cur <= 'z')) ||
  422. ((*cur >= 'A') && (*cur <= 'Z')) ||
  423. ((*cur >= '0') && (*cur <= '9')) ||
  424. (*cur == '_') || (*cur == '-') || (*cur == '.'))
  425. cur++;
  426. }
  427. if (space)
  428. while (IS_BLANK_CH(*cur)) cur++;
  429. if (*cur == 0)
  430. return(0);
  431. try_complex:
  432. /*
  433. * Second check for chars outside the ASCII range
  434. */
  435. cur = value;
  436. c = CUR_SCHAR(cur, l);
  437. if (space) {
  438. while (IS_BLANK(c)) {
  439. cur += l;
  440. c = CUR_SCHAR(cur, l);
  441. }
  442. }
  443. if ((!IS_LETTER(c)) && (c != '_'))
  444. return(1);
  445. cur += l;
  446. c = CUR_SCHAR(cur, l);
  447. while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
  448. (c == '-') || (c == '_') || IS_COMBINING(c) ||
  449. IS_EXTENDER(c)) {
  450. cur += l;
  451. c = CUR_SCHAR(cur, l);
  452. }
  453. if (c == ':') {
  454. cur += l;
  455. c = CUR_SCHAR(cur, l);
  456. if ((!IS_LETTER(c)) && (c != '_'))
  457. return(1);
  458. cur += l;
  459. c = CUR_SCHAR(cur, l);
  460. while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
  461. (c == '-') || (c == '_') || IS_COMBINING(c) ||
  462. IS_EXTENDER(c)) {
  463. cur += l;
  464. c = CUR_SCHAR(cur, l);
  465. }
  466. }
  467. if (space) {
  468. while (IS_BLANK(c)) {
  469. cur += l;
  470. c = CUR_SCHAR(cur, l);
  471. }
  472. }
  473. if (c != 0)
  474. return(1);
  475. return(0);
  476. }
  477. /**
  478. * xmlValidateName:
  479. * @value: the value to check
  480. * @space: allow spaces in front and end of the string
  481. *
  482. * Check that a value conforms to the lexical space of Name
  483. *
  484. * Returns 0 if this validates, a positive error code number otherwise
  485. * and -1 in case of internal or API error.
  486. */
  487. int
  488. xmlValidateName(const xmlChar *value, int space) {
  489. const xmlChar *cur = value;
  490. int c,l;
  491. if (value == NULL)
  492. return(-1);
  493. /*
  494. * First quick algorithm for ASCII range
  495. */
  496. if (space)
  497. while (IS_BLANK_CH(*cur)) cur++;
  498. if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
  499. (*cur == '_') || (*cur == ':'))
  500. cur++;
  501. else
  502. goto try_complex;
  503. while (((*cur >= 'a') && (*cur <= 'z')) ||
  504. ((*cur >= 'A') && (*cur <= 'Z')) ||
  505. ((*cur >= '0') && (*cur <= '9')) ||
  506. (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
  507. cur++;
  508. if (space)
  509. while (IS_BLANK_CH(*cur)) cur++;
  510. if (*cur == 0)
  511. return(0);
  512. try_complex:
  513. /*
  514. * Second check for chars outside the ASCII range
  515. */
  516. cur = value;
  517. c = CUR_SCHAR(cur, l);
  518. if (space) {
  519. while (IS_BLANK(c)) {
  520. cur += l;
  521. c = CUR_SCHAR(cur, l);
  522. }
  523. }
  524. if ((!IS_LETTER(c)) && (c != '_') && (c != ':'))
  525. return(1);
  526. cur += l;
  527. c = CUR_SCHAR(cur, l);
  528. while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
  529. (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
  530. cur += l;
  531. c = CUR_SCHAR(cur, l);
  532. }
  533. if (space) {
  534. while (IS_BLANK(c)) {
  535. cur += l;
  536. c = CUR_SCHAR(cur, l);
  537. }
  538. }
  539. if (c != 0)
  540. return(1);
  541. return(0);
  542. }
  543. /**
  544. * xmlValidateNMToken:
  545. * @value: the value to check
  546. * @space: allow spaces in front and end of the string
  547. *
  548. * Check that a value conforms to the lexical space of NMToken
  549. *
  550. * Returns 0 if this validates, a positive error code number otherwise
  551. * and -1 in case of internal or API error.
  552. */
  553. int
  554. xmlValidateNMToken(const xmlChar *value, int space) {
  555. const xmlChar *cur = value;
  556. int c,l;
  557. if (value == NULL)
  558. return(-1);
  559. /*
  560. * First quick algorithm for ASCII range
  561. */
  562. if (space)
  563. while (IS_BLANK_CH(*cur)) cur++;
  564. if (((*cur >= 'a') && (*cur <= 'z')) ||
  565. ((*cur >= 'A') && (*cur <= 'Z')) ||
  566. ((*cur >= '0') && (*cur <= '9')) ||
  567. (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
  568. cur++;
  569. else
  570. goto try_complex;
  571. while (((*cur >= 'a') && (*cur <= 'z')) ||
  572. ((*cur >= 'A') && (*cur <= 'Z')) ||
  573. ((*cur >= '0') && (*cur <= '9')) ||
  574. (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
  575. cur++;
  576. if (space)
  577. while (IS_BLANK_CH(*cur)) cur++;
  578. if (*cur == 0)
  579. return(0);
  580. try_complex:
  581. /*
  582. * Second check for chars outside the ASCII range
  583. */
  584. cur = value;
  585. c = CUR_SCHAR(cur, l);
  586. if (space) {
  587. while (IS_BLANK(c)) {
  588. cur += l;
  589. c = CUR_SCHAR(cur, l);
  590. }
  591. }
  592. if (!(IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
  593. (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)))
  594. return(1);
  595. cur += l;
  596. c = CUR_SCHAR(cur, l);
  597. while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
  598. (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
  599. cur += l;
  600. c = CUR_SCHAR(cur, l);
  601. }
  602. if (space) {
  603. while (IS_BLANK(c)) {
  604. cur += l;
  605. c = CUR_SCHAR(cur, l);
  606. }
  607. }
  608. if (c != 0)
  609. return(1);
  610. return(0);
  611. }
  612. #endif /* LIBXML_TREE_ENABLED */
  613. /************************************************************************
  614. * *
  615. * Allocation and deallocation of basic structures *
  616. * *
  617. ************************************************************************/
  618. /**
  619. * xmlSetBufferAllocationScheme:
  620. * @scheme: allocation method to use
  621. *
  622. * Set the buffer allocation method. Types are
  623. * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
  624. * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
  625. * improves performance
  626. */
  627. void
  628. xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) {
  629. if ((scheme == XML_BUFFER_ALLOC_EXACT) ||
  630. (scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
  631. (scheme == XML_BUFFER_ALLOC_HYBRID))
  632. xmlBufferAllocScheme = scheme;
  633. }
  634. /**
  635. * xmlGetBufferAllocationScheme:
  636. *
  637. * Types are
  638. * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
  639. * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
  640. * improves performance
  641. * XML_BUFFER_ALLOC_HYBRID - use exact sizes on small strings to keep memory usage tight
  642. * in normal usage, and doubleit on large strings to avoid
  643. * pathological performance.
  644. *
  645. * Returns the current allocation scheme
  646. */
  647. xmlBufferAllocationScheme
  648. xmlGetBufferAllocationScheme(void) {
  649. return(xmlBufferAllocScheme);
  650. }
  651. /**
  652. * xmlNewNs:
  653. * @node: the element carrying the namespace
  654. * @href: the URI associated
  655. * @prefix: the prefix for the namespace
  656. *
  657. * Creation of a new Namespace. This function will refuse to create
  658. * a namespace with a similar prefix than an existing one present on this
  659. * node.
  660. * Note that for a default namespace, @prefix should be NULL.
  661. *
  662. * We use href==NULL in the case of an element creation where the namespace
  663. * was not defined.
  664. *
  665. * Returns a new namespace pointer or NULL
  666. */
  667. xmlNsPtr
  668. xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) {
  669. xmlNsPtr cur;
  670. if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
  671. return(NULL);
  672. if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
  673. /* xml namespace is predefined, no need to add it */
  674. if (xmlStrEqual(href, XML_XML_NAMESPACE))
  675. return(NULL);
  676. /*
  677. * Problem, this is an attempt to bind xml prefix to a wrong
  678. * namespace, which breaks
  679. * Namespace constraint: Reserved Prefixes and Namespace Names
  680. * from XML namespace. But documents authors may not care in
  681. * their context so let's proceed.
  682. */
  683. }
  684. /*
  685. * Allocate a new Namespace and fill the fields.
  686. */
  687. cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
  688. if (cur == NULL) {
  689. xmlTreeErrMemory("building namespace");
  690. return(NULL);
  691. }
  692. memset(cur, 0, sizeof(xmlNs));
  693. cur->type = XML_LOCAL_NAMESPACE;
  694. if (href != NULL)
  695. cur->href = xmlStrdup(href);
  696. if (prefix != NULL)
  697. cur->prefix = xmlStrdup(prefix);
  698. /*
  699. * Add it at the end to preserve parsing order ...
  700. * and checks for existing use of the prefix
  701. */
  702. if (node != NULL) {
  703. if (node->nsDef == NULL) {
  704. node->nsDef = cur;
  705. } else {
  706. xmlNsPtr prev = node->nsDef;
  707. if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
  708. (xmlStrEqual(prev->prefix, cur->prefix))) {
  709. xmlFreeNs(cur);
  710. return(NULL);
  711. }
  712. while (prev->next != NULL) {
  713. prev = prev->next;
  714. if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
  715. (xmlStrEqual(prev->prefix, cur->prefix))) {
  716. xmlFreeNs(cur);
  717. return(NULL);
  718. }
  719. }
  720. prev->next = cur;
  721. }
  722. }
  723. return(cur);
  724. }
  725. /**
  726. * xmlSetNs:
  727. * @node: a node in the document
  728. * @ns: a namespace pointer
  729. *
  730. * Associate a namespace to a node, a posteriori.
  731. */
  732. void
  733. xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
  734. if (node == NULL) {
  735. #ifdef DEBUG_TREE
  736. xmlGenericError(xmlGenericErrorContext,
  737. "xmlSetNs: node == NULL\n");
  738. #endif
  739. return;
  740. }
  741. if ((node->type == XML_ELEMENT_NODE) ||
  742. (node->type == XML_ATTRIBUTE_NODE))
  743. node->ns = ns;
  744. }
  745. /**
  746. * xmlFreeNs:
  747. * @cur: the namespace pointer
  748. *
  749. * Free up the structures associated to a namespace
  750. */
  751. void
  752. xmlFreeNs(xmlNsPtr cur) {
  753. if (cur == NULL) {
  754. #ifdef DEBUG_TREE
  755. xmlGenericError(xmlGenericErrorContext,
  756. "xmlFreeNs : ns == NULL\n");
  757. #endif
  758. return;
  759. }
  760. if (cur->href != NULL) xmlFree((char *) cur->href);
  761. if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
  762. xmlFree(cur);
  763. }
  764. /**
  765. * xmlFreeNsList:
  766. * @cur: the first namespace pointer
  767. *
  768. * Free up all the structures associated to the chained namespaces.
  769. */
  770. void
  771. xmlFreeNsList(xmlNsPtr cur) {
  772. xmlNsPtr next;
  773. if (cur == NULL) {
  774. #ifdef DEBUG_TREE
  775. xmlGenericError(xmlGenericErrorContext,
  776. "xmlFreeNsList : ns == NULL\n");
  777. #endif
  778. return;
  779. }
  780. while (cur != NULL) {
  781. next = cur->next;
  782. xmlFreeNs(cur);
  783. cur = next;
  784. }
  785. }
  786. /**
  787. * xmlNewDtd:
  788. * @doc: the document pointer
  789. * @name: the DTD name
  790. * @ExternalID: the external ID
  791. * @SystemID: the system ID
  792. *
  793. * Creation of a new DTD for the external subset. To create an
  794. * internal subset, use xmlCreateIntSubset().
  795. *
  796. * Returns a pointer to the new DTD structure
  797. */
  798. xmlDtdPtr
  799. xmlNewDtd(xmlDocPtr doc, const xmlChar *name,
  800. const xmlChar *ExternalID, const xmlChar *SystemID) {
  801. xmlDtdPtr cur;
  802. if ((doc != NULL) && (doc->extSubset != NULL)) {
  803. #ifdef DEBUG_TREE
  804. xmlGenericError(xmlGenericErrorContext,
  805. "xmlNewDtd(%s): document %s already have a DTD %s\n",
  806. /* !!! */ (char *) name, doc->name,
  807. /* !!! */ (char *)doc->extSubset->name);
  808. #endif
  809. return(NULL);
  810. }
  811. /*
  812. * Allocate a new DTD and fill the fields.
  813. */
  814. cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
  815. if (cur == NULL) {
  816. xmlTreeErrMemory("building DTD");
  817. return(NULL);
  818. }
  819. memset(cur, 0 , sizeof(xmlDtd));
  820. cur->type = XML_DTD_NODE;
  821. if (name != NULL)
  822. cur->name = xmlStrdup(name);
  823. if (ExternalID != NULL)
  824. cur->ExternalID = xmlStrdup(ExternalID);
  825. if (SystemID != NULL)
  826. cur->SystemID = xmlStrdup(SystemID);
  827. if (doc != NULL)
  828. doc->extSubset = cur;
  829. cur->doc = doc;
  830. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  831. xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
  832. return(cur);
  833. }
  834. /**
  835. * xmlGetIntSubset:
  836. * @doc: the document pointer
  837. *
  838. * Get the internal subset of a document
  839. * Returns a pointer to the DTD structure or NULL if not found
  840. */
  841. xmlDtdPtr
  842. xmlGetIntSubset(const xmlDoc *doc) {
  843. xmlNodePtr cur;
  844. if (doc == NULL)
  845. return(NULL);
  846. cur = doc->children;
  847. while (cur != NULL) {
  848. if (cur->type == XML_DTD_NODE)
  849. return((xmlDtdPtr) cur);
  850. cur = cur->next;
  851. }
  852. return((xmlDtdPtr) doc->intSubset);
  853. }
  854. /**
  855. * xmlCreateIntSubset:
  856. * @doc: the document pointer
  857. * @name: the DTD name
  858. * @ExternalID: the external (PUBLIC) ID
  859. * @SystemID: the system ID
  860. *
  861. * Create the internal subset of a document
  862. * Returns a pointer to the new DTD structure
  863. */
  864. xmlDtdPtr
  865. xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
  866. const xmlChar *ExternalID, const xmlChar *SystemID) {
  867. xmlDtdPtr cur;
  868. if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) {
  869. #ifdef DEBUG_TREE
  870. xmlGenericError(xmlGenericErrorContext,
  871. "xmlCreateIntSubset(): document %s already have an internal subset\n",
  872. doc->name);
  873. #endif
  874. return(NULL);
  875. }
  876. /*
  877. * Allocate a new DTD and fill the fields.
  878. */
  879. cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
  880. if (cur == NULL) {
  881. xmlTreeErrMemory("building internal subset");
  882. return(NULL);
  883. }
  884. memset(cur, 0, sizeof(xmlDtd));
  885. cur->type = XML_DTD_NODE;
  886. if (name != NULL) {
  887. cur->name = xmlStrdup(name);
  888. if (cur->name == NULL) {
  889. xmlTreeErrMemory("building internal subset");
  890. xmlFree(cur);
  891. return(NULL);
  892. }
  893. }
  894. if (ExternalID != NULL) {
  895. cur->ExternalID = xmlStrdup(ExternalID);
  896. if (cur->ExternalID == NULL) {
  897. xmlTreeErrMemory("building internal subset");
  898. if (cur->name != NULL)
  899. xmlFree((char *)cur->name);
  900. xmlFree(cur);
  901. return(NULL);
  902. }
  903. }
  904. if (SystemID != NULL) {
  905. cur->SystemID = xmlStrdup(SystemID);
  906. if (cur->SystemID == NULL) {
  907. xmlTreeErrMemory("building internal subset");
  908. if (cur->name != NULL)
  909. xmlFree((char *)cur->name);
  910. if (cur->ExternalID != NULL)
  911. xmlFree((char *)cur->ExternalID);
  912. xmlFree(cur);
  913. return(NULL);
  914. }
  915. }
  916. if (doc != NULL) {
  917. doc->intSubset = cur;
  918. cur->parent = doc;
  919. cur->doc = doc;
  920. if (doc->children == NULL) {
  921. doc->children = (xmlNodePtr) cur;
  922. doc->last = (xmlNodePtr) cur;
  923. } else {
  924. if (doc->type == XML_HTML_DOCUMENT_NODE) {
  925. xmlNodePtr prev;
  926. prev = doc->children;
  927. prev->prev = (xmlNodePtr) cur;
  928. cur->next = prev;
  929. doc->children = (xmlNodePtr) cur;
  930. } else {
  931. xmlNodePtr next;
  932. next = doc->children;
  933. while ((next != NULL) && (next->type != XML_ELEMENT_NODE))
  934. next = next->next;
  935. if (next == NULL) {
  936. cur->prev = doc->last;
  937. cur->prev->next = (xmlNodePtr) cur;
  938. cur->next = NULL;
  939. doc->last = (xmlNodePtr) cur;
  940. } else {
  941. cur->next = next;
  942. cur->prev = next->prev;
  943. if (cur->prev == NULL)
  944. doc->children = (xmlNodePtr) cur;
  945. else
  946. cur->prev->next = (xmlNodePtr) cur;
  947. next->prev = (xmlNodePtr) cur;
  948. }
  949. }
  950. }
  951. }
  952. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  953. xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
  954. return(cur);
  955. }
  956. /**
  957. * DICT_FREE:
  958. * @str: a string
  959. *
  960. * Free a string if it is not owned by the "dict" dictionary in the
  961. * current scope
  962. */
  963. #define DICT_FREE(str) \
  964. if ((str) && ((!dict) || \
  965. (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
  966. xmlFree((char *)(str));
  967. /**
  968. * DICT_COPY:
  969. * @str: a string
  970. *
  971. * Copy a string using a "dict" dictionary in the current scope,
  972. * if available.
  973. */
  974. #define DICT_COPY(str, cpy) \
  975. if (str) { \
  976. if (dict) { \
  977. if (xmlDictOwns(dict, (const xmlChar *)(str))) \
  978. cpy = (xmlChar *) (str); \
  979. else \
  980. cpy = (xmlChar *) xmlDictLookup((dict), (const xmlChar *)(str), -1); \
  981. } else \
  982. cpy = xmlStrdup((const xmlChar *)(str)); }
  983. /**
  984. * DICT_CONST_COPY:
  985. * @str: a string
  986. *
  987. * Copy a string using a "dict" dictionary in the current scope,
  988. * if available.
  989. */
  990. #define DICT_CONST_COPY(str, cpy) \
  991. if (str) { \
  992. if (dict) { \
  993. if (xmlDictOwns(dict, (const xmlChar *)(str))) \
  994. cpy = (const xmlChar *) (str); \
  995. else \
  996. cpy = xmlDictLookup((dict), (const xmlChar *)(str), -1); \
  997. } else \
  998. cpy = (const xmlChar *) xmlStrdup((const xmlChar *)(str)); }
  999. /**
  1000. * xmlFreeDtd:
  1001. * @cur: the DTD structure to free up
  1002. *
  1003. * Free a DTD structure.
  1004. */
  1005. void
  1006. xmlFreeDtd(xmlDtdPtr cur) {
  1007. xmlDictPtr dict = NULL;
  1008. if (cur == NULL) {
  1009. return;
  1010. }
  1011. if (cur->doc != NULL) dict = cur->doc->dict;
  1012. if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
  1013. xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
  1014. if (cur->children != NULL) {
  1015. xmlNodePtr next, c = cur->children;
  1016. /*
  1017. * Cleanup all nodes which are not part of the specific lists
  1018. * of notations, elements, attributes and entities.
  1019. */
  1020. while (c != NULL) {
  1021. next = c->next;
  1022. if ((c->type != XML_NOTATION_NODE) &&
  1023. (c->type != XML_ELEMENT_DECL) &&
  1024. (c->type != XML_ATTRIBUTE_DECL) &&
  1025. (c->type != XML_ENTITY_DECL)) {
  1026. xmlUnlinkNode(c);
  1027. xmlFreeNode(c);
  1028. }
  1029. c = next;
  1030. }
  1031. }
  1032. DICT_FREE(cur->name)
  1033. DICT_FREE(cur->SystemID)
  1034. DICT_FREE(cur->ExternalID)
  1035. /* TODO !!! */
  1036. if (cur->notations != NULL)
  1037. xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
  1038. if (cur->elements != NULL)
  1039. xmlFreeElementTable((xmlElementTablePtr) cur->elements);
  1040. if (cur->attributes != NULL)
  1041. xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
  1042. if (cur->entities != NULL)
  1043. xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
  1044. if (cur->pentities != NULL)
  1045. xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
  1046. xmlFree(cur);
  1047. }
  1048. /**
  1049. * xmlNewDoc:
  1050. * @version: xmlChar string giving the version of XML "1.0"
  1051. *
  1052. * Creates a new XML document
  1053. *
  1054. * Returns a new document
  1055. */
  1056. xmlDocPtr
  1057. xmlNewDoc(const xmlChar *version) {
  1058. xmlDocPtr cur;
  1059. if (version == NULL)
  1060. version = (const xmlChar *) "1.0";
  1061. /*
  1062. * Allocate a new document and fill the fields.
  1063. */
  1064. cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
  1065. if (cur == NULL) {
  1066. xmlTreeErrMemory("building doc");
  1067. return(NULL);
  1068. }
  1069. memset(cur, 0, sizeof(xmlDoc));
  1070. cur->type = XML_DOCUMENT_NODE;
  1071. cur->version = xmlStrdup(version);
  1072. if (cur->version == NULL) {
  1073. xmlTreeErrMemory("building doc");
  1074. xmlFree(cur);
  1075. return(NULL);
  1076. }
  1077. cur->standalone = -1;
  1078. cur->compression = -1; /* not initialized */
  1079. cur->doc = cur;
  1080. cur->parseFlags = 0;
  1081. cur->properties = XML_DOC_USERBUILT;
  1082. /*
  1083. * The in memory encoding is always UTF8
  1084. * This field will never change and would
  1085. * be obsolete if not for binary compatibility.
  1086. */
  1087. cur->charset = XML_CHAR_ENCODING_UTF8;
  1088. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  1089. xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
  1090. return(cur);
  1091. }
  1092. /**
  1093. * xmlFreeDoc:
  1094. * @cur: pointer to the document
  1095. *
  1096. * Free up all the structures used by a document, tree included.
  1097. */
  1098. void
  1099. xmlFreeDoc(xmlDocPtr cur) {
  1100. xmlDtdPtr extSubset, intSubset;
  1101. xmlDictPtr dict = NULL;
  1102. if (cur == NULL) {
  1103. #ifdef DEBUG_TREE
  1104. xmlGenericError(xmlGenericErrorContext,
  1105. "xmlFreeDoc : document == NULL\n");
  1106. #endif
  1107. return;
  1108. }
  1109. #ifdef LIBXML_DEBUG_RUNTIME
  1110. #ifdef LIBXML_DEBUG_ENABLED
  1111. xmlDebugCheckDocument(stderr, cur);
  1112. #endif
  1113. #endif
  1114. if (cur != NULL) dict = cur->dict;
  1115. if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
  1116. xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
  1117. /*
  1118. * Do this before freeing the children list to avoid ID lookups
  1119. */
  1120. if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
  1121. cur->ids = NULL;
  1122. if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
  1123. cur->refs = NULL;
  1124. extSubset = cur->extSubset;
  1125. intSubset = cur->intSubset;
  1126. if (intSubset == extSubset)
  1127. extSubset = NULL;
  1128. if (extSubset != NULL) {
  1129. xmlUnlinkNode((xmlNodePtr) cur->extSubset);
  1130. cur->extSubset = NULL;
  1131. xmlFreeDtd(extSubset);
  1132. }
  1133. if (intSubset != NULL) {
  1134. xmlUnlinkNode((xmlNodePtr) cur->intSubset);
  1135. cur->intSubset = NULL;
  1136. xmlFreeDtd(intSubset);
  1137. }
  1138. if (cur->children != NULL) xmlFreeNodeList(cur->children);
  1139. if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
  1140. DICT_FREE(cur->version)
  1141. DICT_FREE(cur->name)
  1142. DICT_FREE(cur->encoding)
  1143. DICT_FREE(cur->URL)
  1144. xmlFree(cur);
  1145. if (dict) xmlDictFree(dict);
  1146. }
  1147. /**
  1148. * xmlStringLenGetNodeList:
  1149. * @doc: the document
  1150. * @value: the value of the text
  1151. * @len: the length of the string value
  1152. *
  1153. * Parse the value string and build the node list associated. Should
  1154. * produce a flat tree with only TEXTs and ENTITY_REFs.
  1155. * Returns a pointer to the first child
  1156. */
  1157. xmlNodePtr
  1158. xmlStringLenGetNodeList(const xmlDoc *doc, const xmlChar *value, int len) {
  1159. xmlNodePtr ret = NULL, last = NULL;
  1160. xmlNodePtr node;
  1161. xmlChar *val;
  1162. const xmlChar *cur, *end;
  1163. const xmlChar *q;
  1164. xmlEntityPtr ent;
  1165. xmlBufPtr buf;
  1166. if (value == NULL) return(NULL);
  1167. cur = value;
  1168. end = cur + len;
  1169. buf = xmlBufCreateSize(0);
  1170. if (buf == NULL) return(NULL);
  1171. xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_DOUBLEIT);
  1172. q = cur;
  1173. while ((cur < end) && (*cur != 0)) {
  1174. if (cur[0] == '&') {
  1175. int charval = 0;
  1176. xmlChar tmp;
  1177. /*
  1178. * Save the current text.
  1179. */
  1180. if (cur != q) {
  1181. if (xmlBufAdd(buf, q, cur - q))
  1182. goto out;
  1183. }
  1184. q = cur;
  1185. if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) {
  1186. cur += 3;
  1187. if (cur < end)
  1188. tmp = *cur;
  1189. else
  1190. tmp = 0;
  1191. while (tmp != ';') { /* Non input consuming loop */
  1192. /*
  1193. * If you find an integer overflow here when fuzzing,
  1194. * the bug is probably elsewhere. This function should
  1195. * only receive entities that were already validated by
  1196. * the parser, typically by xmlParseAttValueComplex
  1197. * calling xmlStringDecodeEntities.
  1198. *
  1199. * So it's better *not* to check for overflow to
  1200. * potentially discover new bugs.
  1201. */
  1202. if ((tmp >= '0') && (tmp <= '9'))
  1203. charval = charval * 16 + (tmp - '0');
  1204. else if ((tmp >= 'a') && (tmp <= 'f'))
  1205. charval = charval * 16 + (tmp - 'a') + 10;
  1206. else if ((tmp >= 'A') && (tmp <= 'F'))
  1207. charval = charval * 16 + (tmp - 'A') + 10;
  1208. else {
  1209. xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
  1210. NULL);
  1211. charval = 0;
  1212. break;
  1213. }
  1214. cur++;
  1215. if (cur < end)
  1216. tmp = *cur;
  1217. else
  1218. tmp = 0;
  1219. }
  1220. if (tmp == ';')
  1221. cur++;
  1222. q = cur;
  1223. } else if ((cur + 1 < end) && (cur[1] == '#')) {
  1224. cur += 2;
  1225. if (cur < end)
  1226. tmp = *cur;
  1227. else
  1228. tmp = 0;
  1229. while (tmp != ';') { /* Non input consuming loops */
  1230. /* Don't check for integer overflow, see above. */
  1231. if ((tmp >= '0') && (tmp <= '9'))
  1232. charval = charval * 10 + (tmp - '0');
  1233. else {
  1234. xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
  1235. NULL);
  1236. charval = 0;
  1237. break;
  1238. }
  1239. cur++;
  1240. if (cur < end)
  1241. tmp = *cur;
  1242. else
  1243. tmp = 0;
  1244. }
  1245. if (tmp == ';')
  1246. cur++;
  1247. q = cur;
  1248. } else {
  1249. /*
  1250. * Read the entity string
  1251. */
  1252. cur++;
  1253. q = cur;
  1254. while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++;
  1255. if ((cur >= end) || (*cur == 0)) {
  1256. xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc,
  1257. (const char *) q);
  1258. goto out;
  1259. }
  1260. if (cur != q) {
  1261. /*
  1262. * Predefined entities don't generate nodes
  1263. */
  1264. val = xmlStrndup(q, cur - q);
  1265. ent = xmlGetDocEntity(doc, val);
  1266. if ((ent != NULL) &&
  1267. (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
  1268. if (xmlBufCat(buf, ent->content))
  1269. goto out;
  1270. } else {
  1271. /*
  1272. * Flush buffer so far
  1273. */
  1274. if (!xmlBufIsEmpty(buf)) {
  1275. node = xmlNewDocText(doc, NULL);
  1276. if (node == NULL) {
  1277. if (val != NULL) xmlFree(val);
  1278. goto out;
  1279. }
  1280. node->content = xmlBufDetach(buf);
  1281. if (last == NULL) {
  1282. last = ret = node;
  1283. } else {
  1284. last = xmlAddNextSibling(last, node);
  1285. }
  1286. }
  1287. /*
  1288. * Create a new REFERENCE_REF node
  1289. */
  1290. node = xmlNewReference(doc, val);
  1291. if (node == NULL) {
  1292. if (val != NULL) xmlFree(val);
  1293. goto out;
  1294. }
  1295. else if ((ent != NULL) && (ent->children == NULL)) {
  1296. xmlNodePtr temp;
  1297. /* Set to non-NULL value to avoid recursion. */
  1298. ent->children = (xmlNodePtr) -1;
  1299. ent->children = xmlStringGetNodeList(doc,
  1300. (const xmlChar*)node->content);
  1301. ent->owner = 1;
  1302. temp = ent->children;
  1303. while (temp) {
  1304. temp->parent = (xmlNodePtr)ent;
  1305. ent->last = temp;
  1306. temp = temp->next;
  1307. }
  1308. }
  1309. if (last == NULL) {
  1310. last = ret = node;
  1311. } else {
  1312. last = xmlAddNextSibling(last, node);
  1313. }
  1314. }
  1315. xmlFree(val);
  1316. }
  1317. cur++;
  1318. q = cur;
  1319. }
  1320. if (charval != 0) {
  1321. xmlChar buffer[10];
  1322. int l;
  1323. l = xmlCopyCharMultiByte(buffer, charval);
  1324. buffer[l] = 0;
  1325. if (xmlBufCat(buf, buffer))
  1326. goto out;
  1327. charval = 0;
  1328. }
  1329. } else
  1330. cur++;
  1331. }
  1332. if (cur != q) {
  1333. /*
  1334. * Handle the last piece of text.
  1335. */
  1336. if (xmlBufAdd(buf, q, cur - q))
  1337. goto out;
  1338. }
  1339. if (!xmlBufIsEmpty(buf)) {
  1340. node = xmlNewDocText(doc, NULL);
  1341. if (node == NULL) goto out;
  1342. node->content = xmlBufDetach(buf);
  1343. if (last == NULL) {
  1344. ret = node;
  1345. } else {
  1346. xmlAddNextSibling(last, node);
  1347. }
  1348. } else if (ret == NULL) {
  1349. ret = xmlNewDocText(doc, BAD_CAST "");
  1350. }
  1351. out:
  1352. xmlBufFree(buf);
  1353. return(ret);
  1354. }
  1355. /**
  1356. * xmlStringGetNodeList:
  1357. * @doc: the document
  1358. * @value: the value of the attribute
  1359. *
  1360. * Parse the value string and build the node list associated. Should
  1361. * produce a flat tree with only TEXTs and ENTITY_REFs.
  1362. * Returns a pointer to the first child
  1363. */
  1364. xmlNodePtr
  1365. xmlStringGetNodeList(const xmlDoc *doc, const xmlChar *value) {
  1366. xmlNodePtr ret = NULL, last = NULL;
  1367. xmlNodePtr node;
  1368. xmlChar *val;
  1369. const xmlChar *cur = value;
  1370. const xmlChar *q;
  1371. xmlEntityPtr ent;
  1372. xmlBufPtr buf;
  1373. if (value == NULL) return(NULL);
  1374. buf = xmlBufCreateSize(0);
  1375. if (buf == NULL) return(NULL);
  1376. xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_DOUBLEIT);
  1377. q = cur;
  1378. while (*cur != 0) {
  1379. if (cur[0] == '&') {
  1380. int charval = 0;
  1381. xmlChar tmp;
  1382. /*
  1383. * Save the current text.
  1384. */
  1385. if (cur != q) {
  1386. if (xmlBufAdd(buf, q, cur - q))
  1387. goto out;
  1388. }
  1389. q = cur;
  1390. if ((cur[1] == '#') && (cur[2] == 'x')) {
  1391. cur += 3;
  1392. tmp = *cur;
  1393. while (tmp != ';') { /* Non input consuming loop */
  1394. /* Don't check for integer overflow, see above. */
  1395. if ((tmp >= '0') && (tmp <= '9'))
  1396. charval = charval * 16 + (tmp - '0');
  1397. else if ((tmp >= 'a') && (tmp <= 'f'))
  1398. charval = charval * 16 + (tmp - 'a') + 10;
  1399. else if ((tmp >= 'A') && (tmp <= 'F'))
  1400. charval = charval * 16 + (tmp - 'A') + 10;
  1401. else {
  1402. xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
  1403. NULL);
  1404. charval = 0;
  1405. break;
  1406. }
  1407. cur++;
  1408. tmp = *cur;
  1409. }
  1410. if (tmp == ';')
  1411. cur++;
  1412. q = cur;
  1413. } else if (cur[1] == '#') {
  1414. cur += 2;
  1415. tmp = *cur;
  1416. while (tmp != ';') { /* Non input consuming loops */
  1417. /* Don't check for integer overflow, see above. */
  1418. if ((tmp >= '0') && (tmp <= '9'))
  1419. charval = charval * 10 + (tmp - '0');
  1420. else {
  1421. xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
  1422. NULL);
  1423. charval = 0;
  1424. break;
  1425. }
  1426. cur++;
  1427. tmp = *cur;
  1428. }
  1429. if (tmp == ';')
  1430. cur++;
  1431. q = cur;
  1432. } else {
  1433. /*
  1434. * Read the entity string
  1435. */
  1436. cur++;
  1437. q = cur;
  1438. while ((*cur != 0) && (*cur != ';')) cur++;
  1439. if (*cur == 0) {
  1440. xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY,
  1441. (xmlNodePtr) doc, (const char *) q);
  1442. goto out;
  1443. }
  1444. if (cur != q) {
  1445. /*
  1446. * Predefined entities don't generate nodes
  1447. */
  1448. val = xmlStrndup(q, cur - q);
  1449. ent = xmlGetDocEntity(doc, val);
  1450. if ((ent != NULL) &&
  1451. (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
  1452. if (xmlBufCat(buf, ent->content))
  1453. goto out;
  1454. } else {
  1455. /*
  1456. * Flush buffer so far
  1457. */
  1458. if (!xmlBufIsEmpty(buf)) {
  1459. node = xmlNewDocText(doc, NULL);
  1460. if (node == NULL) {
  1461. if (val != NULL) xmlFree(val);
  1462. goto out;
  1463. }
  1464. node->content = xmlBufDetach(buf);
  1465. if (last == NULL) {
  1466. last = ret = node;
  1467. } else {
  1468. last = xmlAddNextSibling(last, node);
  1469. }
  1470. }
  1471. /*
  1472. * Create a new REFERENCE_REF node
  1473. */
  1474. node = xmlNewReference(doc, val);
  1475. if (node == NULL) {
  1476. if (val != NULL) xmlFree(val);
  1477. goto out;
  1478. }
  1479. else if ((ent != NULL) && (ent->children == NULL)) {
  1480. xmlNodePtr temp;
  1481. /* Set to non-NULL value to avoid recursion. */
  1482. ent->children = (xmlNodePtr) -1;
  1483. ent->children = xmlStringGetNodeList(doc,
  1484. (const xmlChar*)node->content);
  1485. ent->owner = 1;
  1486. temp = ent->children;
  1487. while (temp) {
  1488. temp->parent = (xmlNodePtr)ent;
  1489. ent->last = temp;
  1490. temp = temp->next;
  1491. }
  1492. }
  1493. if (last == NULL) {
  1494. last = ret = node;
  1495. } else {
  1496. last = xmlAddNextSibling(last, node);
  1497. }
  1498. }
  1499. xmlFree(val);
  1500. }
  1501. cur++;
  1502. q = cur;
  1503. }
  1504. if (charval != 0) {
  1505. xmlChar buffer[10];
  1506. int len;
  1507. len = xmlCopyCharMultiByte(buffer, charval);
  1508. buffer[len] = 0;
  1509. if (xmlBufCat(buf, buffer))
  1510. goto out;
  1511. charval = 0;
  1512. }
  1513. } else
  1514. cur++;
  1515. }
  1516. if ((cur != q) || (ret == NULL)) {
  1517. /*
  1518. * Handle the last piece of text.
  1519. */
  1520. xmlBufAdd(buf, q, cur - q);
  1521. }
  1522. if (!xmlBufIsEmpty(buf)) {
  1523. node = xmlNewDocText(doc, NULL);
  1524. if (node == NULL) {
  1525. xmlBufFree(buf);
  1526. return(NULL);
  1527. }
  1528. node->content = xmlBufDetach(buf);
  1529. if (last == NULL) {
  1530. ret = node;
  1531. } else {
  1532. xmlAddNextSibling(last, node);
  1533. }
  1534. }
  1535. out:
  1536. xmlBufFree(buf);
  1537. return(ret);
  1538. }
  1539. /**
  1540. * xmlNodeListGetString:
  1541. * @doc: the document
  1542. * @list: a Node list
  1543. * @inLine: should we replace entity contents or show their external form
  1544. *
  1545. * Build the string equivalent to the text contained in the Node list
  1546. * made of TEXTs and ENTITY_REFs
  1547. *
  1548. * Returns a pointer to the string copy, the caller must free it with xmlFree().
  1549. */
  1550. xmlChar *
  1551. xmlNodeListGetString(xmlDocPtr doc, const xmlNode *list, int inLine)
  1552. {
  1553. const xmlNode *node = list;
  1554. xmlChar *ret = NULL;
  1555. xmlEntityPtr ent;
  1556. int attr;
  1557. if (list == NULL)
  1558. return (NULL);
  1559. if ((list->parent != NULL) && (list->parent->type == XML_ATTRIBUTE_NODE))
  1560. attr = 1;
  1561. else
  1562. attr = 0;
  1563. while (node != NULL) {
  1564. if ((node->type == XML_TEXT_NODE) ||
  1565. (node->type == XML_CDATA_SECTION_NODE)) {
  1566. if (inLine) {
  1567. ret = xmlStrcat(ret, node->content);
  1568. } else {
  1569. xmlChar *buffer;
  1570. if (attr)
  1571. buffer = xmlEncodeAttributeEntities(doc, node->content);
  1572. else
  1573. buffer = xmlEncodeEntitiesReentrant(doc, node->content);
  1574. if (buffer != NULL) {
  1575. ret = xmlStrcat(ret, buffer);
  1576. xmlFree(buffer);
  1577. }
  1578. }
  1579. } else if (node->type == XML_ENTITY_REF_NODE) {
  1580. if (inLine) {
  1581. ent = xmlGetDocEntity(doc, node->name);
  1582. if (ent != NULL) {
  1583. xmlChar *buffer;
  1584. /* an entity content can be any "well balanced chunk",
  1585. * i.e. the result of the content [43] production:
  1586. * http://www.w3.org/TR/REC-xml#NT-content.
  1587. * So it can contain text, CDATA section or nested
  1588. * entity reference nodes (among others).
  1589. * -> we recursive call xmlNodeListGetString()
  1590. * which handles these types */
  1591. buffer = xmlNodeListGetString(doc, ent->children, 1);
  1592. if (buffer != NULL) {
  1593. ret = xmlStrcat(ret, buffer);
  1594. xmlFree(buffer);
  1595. }
  1596. } else {
  1597. ret = xmlStrcat(ret, node->content);
  1598. }
  1599. } else {
  1600. xmlChar buf[2];
  1601. buf[0] = '&';
  1602. buf[1] = 0;
  1603. ret = xmlStrncat(ret, buf, 1);
  1604. ret = xmlStrcat(ret, node->name);
  1605. buf[0] = ';';
  1606. buf[1] = 0;
  1607. ret = xmlStrncat(ret, buf, 1);
  1608. }
  1609. }
  1610. #if 0
  1611. else {
  1612. xmlGenericError(xmlGenericErrorContext,
  1613. "xmlGetNodeListString : invalid node type %d\n",
  1614. node->type);
  1615. }
  1616. #endif
  1617. node = node->next;
  1618. }
  1619. return (ret);
  1620. }
  1621. #ifdef LIBXML_TREE_ENABLED
  1622. /**
  1623. * xmlNodeListGetRawString:
  1624. * @doc: the document
  1625. * @list: a Node list
  1626. * @inLine: should we replace entity contents or show their external form
  1627. *
  1628. * Builds the string equivalent to the text contained in the Node list
  1629. * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
  1630. * this function doesn't do any character encoding handling.
  1631. *
  1632. * Returns a pointer to the string copy, the caller must free it with xmlFree().
  1633. */
  1634. xmlChar *
  1635. xmlNodeListGetRawString(const xmlDoc *doc, const xmlNode *list, int inLine)
  1636. {
  1637. const xmlNode *node = list;
  1638. xmlChar *ret = NULL;
  1639. xmlEntityPtr ent;
  1640. if (list == NULL)
  1641. return (NULL);
  1642. while (node != NULL) {
  1643. if ((node->type == XML_TEXT_NODE) ||
  1644. (node->type == XML_CDATA_SECTION_NODE)) {
  1645. if (inLine) {
  1646. ret = xmlStrcat(ret, node->content);
  1647. } else {
  1648. xmlChar *buffer;
  1649. buffer = xmlEncodeSpecialChars(doc, node->content);
  1650. if (buffer != NULL) {
  1651. ret = xmlStrcat(ret, buffer);
  1652. xmlFree(buffer);
  1653. }
  1654. }
  1655. } else if (node->type == XML_ENTITY_REF_NODE) {
  1656. if (inLine) {
  1657. ent = xmlGetDocEntity(doc, node->name);
  1658. if (ent != NULL) {
  1659. xmlChar *buffer;
  1660. /* an entity content can be any "well balanced chunk",
  1661. * i.e. the result of the content [43] production:
  1662. * http://www.w3.org/TR/REC-xml#NT-content.
  1663. * So it can contain text, CDATA section or nested
  1664. * entity reference nodes (among others).
  1665. * -> we recursive call xmlNodeListGetRawString()
  1666. * which handles these types */
  1667. buffer =
  1668. xmlNodeListGetRawString(doc, ent->children, 1);
  1669. if (buffer != NULL) {
  1670. ret = xmlStrcat(ret, buffer);
  1671. xmlFree(buffer);
  1672. }
  1673. } else {
  1674. ret = xmlStrcat(ret, node->content);
  1675. }
  1676. } else {
  1677. xmlChar buf[2];
  1678. buf[0] = '&';
  1679. buf[1] = 0;
  1680. ret = xmlStrncat(ret, buf, 1);
  1681. ret = xmlStrcat(ret, node->name);
  1682. buf[0] = ';';
  1683. buf[1] = 0;
  1684. ret = xmlStrncat(ret, buf, 1);
  1685. }
  1686. }
  1687. #if 0
  1688. else {
  1689. xmlGenericError(xmlGenericErrorContext,
  1690. "xmlGetNodeListString : invalid node type %d\n",
  1691. node->type);
  1692. }
  1693. #endif
  1694. node = node->next;
  1695. }
  1696. return (ret);
  1697. }
  1698. #endif /* LIBXML_TREE_ENABLED */
  1699. static xmlAttrPtr
  1700. xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns,
  1701. const xmlChar * name, const xmlChar * value,
  1702. int eatname)
  1703. {
  1704. xmlAttrPtr cur;
  1705. xmlDocPtr doc = NULL;
  1706. if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) {
  1707. if ((eatname == 1) &&
  1708. ((node->doc == NULL) ||
  1709. (!(xmlDictOwns(node->doc->dict, name)))))
  1710. xmlFree((xmlChar *) name);
  1711. return (NULL);
  1712. }
  1713. /*
  1714. * Allocate a new property and fill the fields.
  1715. */
  1716. cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
  1717. if (cur == NULL) {
  1718. if ((eatname == 1) &&
  1719. ((node == NULL) || (node->doc == NULL) ||
  1720. (!(xmlDictOwns(node->doc->dict, name)))))
  1721. xmlFree((xmlChar *) name);
  1722. xmlTreeErrMemory("building attribute");
  1723. return (NULL);
  1724. }
  1725. memset(cur, 0, sizeof(xmlAttr));
  1726. cur->type = XML_ATTRIBUTE_NODE;
  1727. cur->parent = node;
  1728. if (node != NULL) {
  1729. doc = node->doc;
  1730. cur->doc = doc;
  1731. }
  1732. cur->ns = ns;
  1733. if (eatname == 0) {
  1734. if ((doc != NULL) && (doc->dict != NULL))
  1735. cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
  1736. else
  1737. cur->name = xmlStrdup(name);
  1738. } else
  1739. cur->name = name;
  1740. if (value != NULL) {
  1741. xmlNodePtr tmp;
  1742. cur->children = xmlNewDocText(doc, value);
  1743. cur->last = NULL;
  1744. tmp = cur->children;
  1745. while (tmp != NULL) {
  1746. tmp->parent = (xmlNodePtr) cur;
  1747. if (tmp->next == NULL)
  1748. cur->last = tmp;
  1749. tmp = tmp->next;
  1750. }
  1751. }
  1752. /*
  1753. * Add it at the end to preserve parsing order ...
  1754. */
  1755. if (node != NULL) {
  1756. if (node->properties == NULL) {
  1757. node->properties = cur;
  1758. } else {
  1759. xmlAttrPtr prev = node->properties;
  1760. while (prev->next != NULL)
  1761. prev = prev->next;
  1762. prev->next = cur;
  1763. cur->prev = prev;
  1764. }
  1765. }
  1766. if ((value != NULL) && (node != NULL) &&
  1767. (xmlIsID(node->doc, node, cur) == 1))
  1768. xmlAddID(NULL, node->doc, value, cur);
  1769. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  1770. xmlRegisterNodeDefaultValue((xmlNodePtr) cur);
  1771. return (cur);
  1772. }
  1773. #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
  1774. defined(LIBXML_SCHEMAS_ENABLED)
  1775. /**
  1776. * xmlNewProp:
  1777. * @node: the holding node
  1778. * @name: the name of the attribute
  1779. * @value: the value of the attribute
  1780. *
  1781. * Create a new property carried by a node.
  1782. * Returns a pointer to the attribute
  1783. */
  1784. xmlAttrPtr
  1785. xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
  1786. if (name == NULL) {
  1787. #ifdef DEBUG_TREE
  1788. xmlGenericError(xmlGenericErrorContext,
  1789. "xmlNewProp : name == NULL\n");
  1790. #endif
  1791. return(NULL);
  1792. }
  1793. return xmlNewPropInternal(node, NULL, name, value, 0);
  1794. }
  1795. #endif /* LIBXML_TREE_ENABLED */
  1796. /**
  1797. * xmlNewNsProp:
  1798. * @node: the holding node
  1799. * @ns: the namespace
  1800. * @name: the name of the attribute
  1801. * @value: the value of the attribute
  1802. *
  1803. * Create a new property tagged with a namespace and carried by a node.
  1804. * Returns a pointer to the attribute
  1805. */
  1806. xmlAttrPtr
  1807. xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
  1808. const xmlChar *value) {
  1809. if (name == NULL) {
  1810. #ifdef DEBUG_TREE
  1811. xmlGenericError(xmlGenericErrorContext,
  1812. "xmlNewNsProp : name == NULL\n");
  1813. #endif
  1814. return(NULL);
  1815. }
  1816. return xmlNewPropInternal(node, ns, name, value, 0);
  1817. }
  1818. /**
  1819. * xmlNewNsPropEatName:
  1820. * @node: the holding node
  1821. * @ns: the namespace
  1822. * @name: the name of the attribute
  1823. * @value: the value of the attribute
  1824. *
  1825. * Create a new property tagged with a namespace and carried by a node.
  1826. * Returns a pointer to the attribute
  1827. */
  1828. xmlAttrPtr
  1829. xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
  1830. const xmlChar *value) {
  1831. if (name == NULL) {
  1832. #ifdef DEBUG_TREE
  1833. xmlGenericError(xmlGenericErrorContext,
  1834. "xmlNewNsPropEatName : name == NULL\n");
  1835. #endif
  1836. return(NULL);
  1837. }
  1838. return xmlNewPropInternal(node, ns, name, value, 1);
  1839. }
  1840. /**
  1841. * xmlNewDocProp:
  1842. * @doc: the document
  1843. * @name: the name of the attribute
  1844. * @value: the value of the attribute
  1845. *
  1846. * Create a new property carried by a document.
  1847. * NOTE: @value is supposed to be a piece of XML CDATA, so it allows entity
  1848. * references, but XML special chars need to be escaped first by using
  1849. * xmlEncodeEntitiesReentrant(). Use xmlNewProp() if you don't need
  1850. * entities support.
  1851. *
  1852. * Returns a pointer to the attribute
  1853. */
  1854. xmlAttrPtr
  1855. xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
  1856. xmlAttrPtr cur;
  1857. if (name == NULL) {
  1858. #ifdef DEBUG_TREE
  1859. xmlGenericError(xmlGenericErrorContext,
  1860. "xmlNewDocProp : name == NULL\n");
  1861. #endif
  1862. return(NULL);
  1863. }
  1864. /*
  1865. * Allocate a new property and fill the fields.
  1866. */
  1867. cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
  1868. if (cur == NULL) {
  1869. xmlTreeErrMemory("building attribute");
  1870. return(NULL);
  1871. }
  1872. memset(cur, 0, sizeof(xmlAttr));
  1873. cur->type = XML_ATTRIBUTE_NODE;
  1874. if ((doc != NULL) && (doc->dict != NULL))
  1875. cur->name = xmlDictLookup(doc->dict, name, -1);
  1876. else
  1877. cur->name = xmlStrdup(name);
  1878. cur->doc = doc;
  1879. if (value != NULL) {
  1880. xmlNodePtr tmp;
  1881. cur->children = xmlStringGetNodeList(doc, value);
  1882. cur->last = NULL;
  1883. tmp = cur->children;
  1884. while (tmp != NULL) {
  1885. tmp->parent = (xmlNodePtr) cur;
  1886. if (tmp->next == NULL)
  1887. cur->last = tmp;
  1888. tmp = tmp->next;
  1889. }
  1890. }
  1891. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  1892. xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
  1893. return(cur);
  1894. }
  1895. /**
  1896. * xmlFreePropList:
  1897. * @cur: the first property in the list
  1898. *
  1899. * Free a property and all its siblings, all the children are freed too.
  1900. */
  1901. void
  1902. xmlFreePropList(xmlAttrPtr cur) {
  1903. xmlAttrPtr next;
  1904. if (cur == NULL) return;
  1905. while (cur != NULL) {
  1906. next = cur->next;
  1907. xmlFreeProp(cur);
  1908. cur = next;
  1909. }
  1910. }
  1911. /**
  1912. * xmlFreeProp:
  1913. * @cur: an attribute
  1914. *
  1915. * Free one attribute, all the content is freed too
  1916. */
  1917. void
  1918. xmlFreeProp(xmlAttrPtr cur) {
  1919. xmlDictPtr dict = NULL;
  1920. if (cur == NULL) return;
  1921. if (cur->doc != NULL) dict = cur->doc->dict;
  1922. if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
  1923. xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
  1924. /* Check for ID removal -> leading to invalid references ! */
  1925. if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) {
  1926. xmlRemoveID(cur->doc, cur);
  1927. }
  1928. if (cur->children != NULL) xmlFreeNodeList(cur->children);
  1929. DICT_FREE(cur->name)
  1930. xmlFree(cur);
  1931. }
  1932. /**
  1933. * xmlRemoveProp:
  1934. * @cur: an attribute
  1935. *
  1936. * Unlink and free one attribute, all the content is freed too
  1937. * Note this doesn't work for namespace definition attributes
  1938. *
  1939. * Returns 0 if success and -1 in case of error.
  1940. */
  1941. int
  1942. xmlRemoveProp(xmlAttrPtr cur) {
  1943. xmlAttrPtr tmp;
  1944. if (cur == NULL) {
  1945. #ifdef DEBUG_TREE
  1946. xmlGenericError(xmlGenericErrorContext,
  1947. "xmlRemoveProp : cur == NULL\n");
  1948. #endif
  1949. return(-1);
  1950. }
  1951. if (cur->parent == NULL) {
  1952. #ifdef DEBUG_TREE
  1953. xmlGenericError(xmlGenericErrorContext,
  1954. "xmlRemoveProp : cur->parent == NULL\n");
  1955. #endif
  1956. return(-1);
  1957. }
  1958. tmp = cur->parent->properties;
  1959. if (tmp == cur) {
  1960. cur->parent->properties = cur->next;
  1961. if (cur->next != NULL)
  1962. cur->next->prev = NULL;
  1963. xmlFreeProp(cur);
  1964. return(0);
  1965. }
  1966. while (tmp != NULL) {
  1967. if (tmp->next == cur) {
  1968. tmp->next = cur->next;
  1969. if (tmp->next != NULL)
  1970. tmp->next->prev = tmp;
  1971. xmlFreeProp(cur);
  1972. return(0);
  1973. }
  1974. tmp = tmp->next;
  1975. }
  1976. #ifdef DEBUG_TREE
  1977. xmlGenericError(xmlGenericErrorContext,
  1978. "xmlRemoveProp : attribute not owned by its node\n");
  1979. #endif
  1980. return(-1);
  1981. }
  1982. /**
  1983. * xmlNewDocPI:
  1984. * @doc: the target document
  1985. * @name: the processing instruction name
  1986. * @content: the PI content
  1987. *
  1988. * Creation of a processing instruction element.
  1989. * Returns a pointer to the new node object.
  1990. */
  1991. xmlNodePtr
  1992. xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) {
  1993. xmlNodePtr cur;
  1994. if (name == NULL) {
  1995. #ifdef DEBUG_TREE
  1996. xmlGenericError(xmlGenericErrorContext,
  1997. "xmlNewPI : name == NULL\n");
  1998. #endif
  1999. return(NULL);
  2000. }
  2001. /*
  2002. * Allocate a new node and fill the fields.
  2003. */
  2004. cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
  2005. if (cur == NULL) {
  2006. xmlTreeErrMemory("building PI");
  2007. return(NULL);
  2008. }
  2009. memset(cur, 0, sizeof(xmlNode));
  2010. cur->type = XML_PI_NODE;
  2011. if ((doc != NULL) && (doc->dict != NULL))
  2012. cur->name = xmlDictLookup(doc->dict, name, -1);
  2013. else
  2014. cur->name = xmlStrdup(name);
  2015. if (content != NULL) {
  2016. cur->content = xmlStrdup(content);
  2017. }
  2018. cur->doc = doc;
  2019. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  2020. xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
  2021. return(cur);
  2022. }
  2023. /**
  2024. * xmlNewPI:
  2025. * @name: the processing instruction name
  2026. * @content: the PI content
  2027. *
  2028. * Creation of a processing instruction element.
  2029. * Use xmlDocNewPI preferably to get string interning
  2030. *
  2031. * Returns a pointer to the new node object.
  2032. */
  2033. xmlNodePtr
  2034. xmlNewPI(const xmlChar *name, const xmlChar *content) {
  2035. return(xmlNewDocPI(NULL, name, content));
  2036. }
  2037. /**
  2038. * xmlNewNode:
  2039. * @ns: namespace if any
  2040. * @name: the node name
  2041. *
  2042. * Creation of a new node element. @ns is optional (NULL).
  2043. *
  2044. * Returns a pointer to the new node object. Uses xmlStrdup() to make
  2045. * copy of @name.
  2046. */
  2047. xmlNodePtr
  2048. xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
  2049. xmlNodePtr cur;
  2050. if (name == NULL) {
  2051. #ifdef DEBUG_TREE
  2052. xmlGenericError(xmlGenericErrorContext,
  2053. "xmlNewNode : name == NULL\n");
  2054. #endif
  2055. return(NULL);
  2056. }
  2057. /*
  2058. * Allocate a new node and fill the fields.
  2059. */
  2060. cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
  2061. if (cur == NULL) {
  2062. xmlTreeErrMemory("building node");
  2063. return(NULL);
  2064. }
  2065. memset(cur, 0, sizeof(xmlNode));
  2066. cur->type = XML_ELEMENT_NODE;
  2067. cur->name = xmlStrdup(name);
  2068. cur->ns = ns;
  2069. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  2070. xmlRegisterNodeDefaultValue(cur);
  2071. return(cur);
  2072. }
  2073. /**
  2074. * xmlNewNodeEatName:
  2075. * @ns: namespace if any
  2076. * @name: the node name
  2077. *
  2078. * Creation of a new node element. @ns is optional (NULL).
  2079. *
  2080. * Returns a pointer to the new node object, with pointer @name as
  2081. * new node's name. Use xmlNewNode() if a copy of @name string is
  2082. * is needed as new node's name.
  2083. */
  2084. xmlNodePtr
  2085. xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) {
  2086. xmlNodePtr cur;
  2087. if (name == NULL) {
  2088. #ifdef DEBUG_TREE
  2089. xmlGenericError(xmlGenericErrorContext,
  2090. "xmlNewNode : name == NULL\n");
  2091. #endif
  2092. return(NULL);
  2093. }
  2094. /*
  2095. * Allocate a new node and fill the fields.
  2096. */
  2097. cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
  2098. if (cur == NULL) {
  2099. xmlTreeErrMemory("building node");
  2100. /* we can't check here that name comes from the doc dictionary */
  2101. return(NULL);
  2102. }
  2103. memset(cur, 0, sizeof(xmlNode));
  2104. cur->type = XML_ELEMENT_NODE;
  2105. cur->name = name;
  2106. cur->ns = ns;
  2107. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  2108. xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
  2109. return(cur);
  2110. }
  2111. /**
  2112. * xmlNewDocNode:
  2113. * @doc: the document
  2114. * @ns: namespace if any
  2115. * @name: the node name
  2116. * @content: the XML text content if any
  2117. *
  2118. * Creation of a new node element within a document. @ns and @content
  2119. * are optional (NULL).
  2120. * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
  2121. * references, but XML special chars need to be escaped first by using
  2122. * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
  2123. * need entities support.
  2124. *
  2125. * Returns a pointer to the new node object.
  2126. */
  2127. xmlNodePtr
  2128. xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
  2129. const xmlChar *name, const xmlChar *content) {
  2130. xmlNodePtr cur;
  2131. if ((doc != NULL) && (doc->dict != NULL))
  2132. cur = xmlNewNodeEatName(ns, (xmlChar *)
  2133. xmlDictLookup(doc->dict, name, -1));
  2134. else
  2135. cur = xmlNewNode(ns, name);
  2136. if (cur != NULL) {
  2137. cur->doc = doc;
  2138. if (content != NULL) {
  2139. cur->children = xmlStringGetNodeList(doc, content);
  2140. UPDATE_LAST_CHILD_AND_PARENT(cur)
  2141. }
  2142. }
  2143. return(cur);
  2144. }
  2145. /**
  2146. * xmlNewDocNodeEatName:
  2147. * @doc: the document
  2148. * @ns: namespace if any
  2149. * @name: the node name
  2150. * @content: the XML text content if any
  2151. *
  2152. * Creation of a new node element within a document. @ns and @content
  2153. * are optional (NULL).
  2154. * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
  2155. * references, but XML special chars need to be escaped first by using
  2156. * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
  2157. * need entities support.
  2158. *
  2159. * Returns a pointer to the new node object.
  2160. */
  2161. xmlNodePtr
  2162. xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns,
  2163. xmlChar *name, const xmlChar *content) {
  2164. xmlNodePtr cur;
  2165. cur = xmlNewNodeEatName(ns, name);
  2166. if (cur != NULL) {
  2167. cur->doc = doc;
  2168. if (content != NULL) {
  2169. cur->children = xmlStringGetNodeList(doc, content);
  2170. UPDATE_LAST_CHILD_AND_PARENT(cur)
  2171. }
  2172. } else {
  2173. /* if name don't come from the doc dictionary free it here */
  2174. if ((name != NULL) && (doc != NULL) &&
  2175. (!(xmlDictOwns(doc->dict, name))))
  2176. xmlFree(name);
  2177. }
  2178. return(cur);
  2179. }
  2180. #ifdef LIBXML_TREE_ENABLED
  2181. /**
  2182. * xmlNewDocRawNode:
  2183. * @doc: the document
  2184. * @ns: namespace if any
  2185. * @name: the node name
  2186. * @content: the text content if any
  2187. *
  2188. * Creation of a new node element within a document. @ns and @content
  2189. * are optional (NULL).
  2190. *
  2191. * Returns a pointer to the new node object.
  2192. */
  2193. xmlNodePtr
  2194. xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
  2195. const xmlChar *name, const xmlChar *content) {
  2196. xmlNodePtr cur;
  2197. cur = xmlNewDocNode(doc, ns, name, NULL);
  2198. if (cur != NULL) {
  2199. cur->doc = doc;
  2200. if (content != NULL) {
  2201. cur->children = xmlNewDocText(doc, content);
  2202. UPDATE_LAST_CHILD_AND_PARENT(cur)
  2203. }
  2204. }
  2205. return(cur);
  2206. }
  2207. /**
  2208. * xmlNewDocFragment:
  2209. * @doc: the document owning the fragment
  2210. *
  2211. * Creation of a new Fragment node.
  2212. * Returns a pointer to the new node object.
  2213. */
  2214. xmlNodePtr
  2215. xmlNewDocFragment(xmlDocPtr doc) {
  2216. xmlNodePtr cur;
  2217. /*
  2218. * Allocate a new DocumentFragment node and fill the fields.
  2219. */
  2220. cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
  2221. if (cur == NULL) {
  2222. xmlTreeErrMemory("building fragment");
  2223. return(NULL);
  2224. }
  2225. memset(cur, 0, sizeof(xmlNode));
  2226. cur->type = XML_DOCUMENT_FRAG_NODE;
  2227. cur->doc = doc;
  2228. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  2229. xmlRegisterNodeDefaultValue(cur);
  2230. return(cur);
  2231. }
  2232. #endif /* LIBXML_TREE_ENABLED */
  2233. /**
  2234. * xmlNewText:
  2235. * @content: the text content
  2236. *
  2237. * Creation of a new text node.
  2238. * Returns a pointer to the new node object.
  2239. */
  2240. xmlNodePtr
  2241. xmlNewText(const xmlChar *content) {
  2242. xmlNodePtr cur;
  2243. /*
  2244. * Allocate a new node and fill the fields.
  2245. */
  2246. cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
  2247. if (cur == NULL) {
  2248. xmlTreeErrMemory("building text");
  2249. return(NULL);
  2250. }
  2251. memset(cur, 0, sizeof(xmlNode));
  2252. cur->type = XML_TEXT_NODE;
  2253. cur->name = xmlStringText;
  2254. if (content != NULL) {
  2255. cur->content = xmlStrdup(content);
  2256. }
  2257. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  2258. xmlRegisterNodeDefaultValue(cur);
  2259. return(cur);
  2260. }
  2261. #ifdef LIBXML_TREE_ENABLED
  2262. /**
  2263. * xmlNewTextChild:
  2264. * @parent: the parent node
  2265. * @ns: a namespace if any
  2266. * @name: the name of the child
  2267. * @content: the text content of the child if any.
  2268. *
  2269. * Creation of a new child element, added at the end of @parent children list.
  2270. * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
  2271. * created element inherits the namespace of @parent. If @content is non NULL,
  2272. * a child TEXT node will be created containing the string @content.
  2273. * NOTE: Use xmlNewChild() if @content will contain entities that need to be
  2274. * preserved. Use this function, xmlNewTextChild(), if you need to ensure that
  2275. * reserved XML chars that might appear in @content, such as the ampersand,
  2276. * greater-than or less-than signs, are automatically replaced by their XML
  2277. * escaped entity representations.
  2278. *
  2279. * Returns a pointer to the new node object.
  2280. */
  2281. xmlNodePtr
  2282. xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
  2283. const xmlChar *name, const xmlChar *content) {
  2284. xmlNodePtr cur, prev;
  2285. if (parent == NULL) {
  2286. #ifdef DEBUG_TREE
  2287. xmlGenericError(xmlGenericErrorContext,
  2288. "xmlNewTextChild : parent == NULL\n");
  2289. #endif
  2290. return(NULL);
  2291. }
  2292. if (name == NULL) {
  2293. #ifdef DEBUG_TREE
  2294. xmlGenericError(xmlGenericErrorContext,
  2295. "xmlNewTextChild : name == NULL\n");
  2296. #endif
  2297. return(NULL);
  2298. }
  2299. /*
  2300. * Allocate a new node
  2301. */
  2302. if (parent->type == XML_ELEMENT_NODE) {
  2303. if (ns == NULL)
  2304. cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
  2305. else
  2306. cur = xmlNewDocRawNode(parent->doc, ns, name, content);
  2307. } else if ((parent->type == XML_DOCUMENT_NODE) ||
  2308. (parent->type == XML_HTML_DOCUMENT_NODE)) {
  2309. if (ns == NULL)
  2310. cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content);
  2311. else
  2312. cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content);
  2313. } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
  2314. cur = xmlNewDocRawNode( parent->doc, ns, name, content);
  2315. } else {
  2316. return(NULL);
  2317. }
  2318. if (cur == NULL) return(NULL);
  2319. /*
  2320. * add the new element at the end of the children list.
  2321. */
  2322. cur->type = XML_ELEMENT_NODE;
  2323. cur->parent = parent;
  2324. cur->doc = parent->doc;
  2325. if (parent->children == NULL) {
  2326. parent->children = cur;
  2327. parent->last = cur;
  2328. } else {
  2329. prev = parent->last;
  2330. prev->next = cur;
  2331. cur->prev = prev;
  2332. parent->last = cur;
  2333. }
  2334. return(cur);
  2335. }
  2336. #endif /* LIBXML_TREE_ENABLED */
  2337. /**
  2338. * xmlNewCharRef:
  2339. * @doc: the document
  2340. * @name: the char ref string, starting with # or "&# ... ;"
  2341. *
  2342. * Creation of a new character reference node.
  2343. * Returns a pointer to the new node object.
  2344. */
  2345. xmlNodePtr
  2346. xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
  2347. xmlNodePtr cur;
  2348. if (name == NULL)
  2349. return(NULL);
  2350. /*
  2351. * Allocate a new node and fill the fields.
  2352. */
  2353. cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
  2354. if (cur == NULL) {
  2355. xmlTreeErrMemory("building character reference");
  2356. return(NULL);
  2357. }
  2358. memset(cur, 0, sizeof(xmlNode));
  2359. cur->type = XML_ENTITY_REF_NODE;
  2360. cur->doc = doc;
  2361. if (name[0] == '&') {
  2362. int len;
  2363. name++;
  2364. len = xmlStrlen(name);
  2365. if (name[len - 1] == ';')
  2366. cur->name = xmlStrndup(name, len - 1);
  2367. else
  2368. cur->name = xmlStrndup(name, len);
  2369. } else
  2370. cur->name = xmlStrdup(name);
  2371. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  2372. xmlRegisterNodeDefaultValue(cur);
  2373. return(cur);
  2374. }
  2375. /**
  2376. * xmlNewReference:
  2377. * @doc: the document
  2378. * @name: the reference name, or the reference string with & and ;
  2379. *
  2380. * Creation of a new reference node.
  2381. * Returns a pointer to the new node object.
  2382. */
  2383. xmlNodePtr
  2384. xmlNewReference(const xmlDoc *doc, const xmlChar *name) {
  2385. xmlNodePtr cur;
  2386. xmlEntityPtr ent;
  2387. if (name == NULL)
  2388. return(NULL);
  2389. /*
  2390. * Allocate a new node and fill the fields.
  2391. */
  2392. cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
  2393. if (cur == NULL) {
  2394. xmlTreeErrMemory("building reference");
  2395. return(NULL);
  2396. }
  2397. memset(cur, 0, sizeof(xmlNode));
  2398. cur->type = XML_ENTITY_REF_NODE;
  2399. cur->doc = (xmlDoc *)doc;
  2400. if (name[0] == '&') {
  2401. int len;
  2402. name++;
  2403. len = xmlStrlen(name);
  2404. if (name[len - 1] == ';')
  2405. cur->name = xmlStrndup(name, len - 1);
  2406. else
  2407. cur->name = xmlStrndup(name, len);
  2408. } else
  2409. cur->name = xmlStrdup(name);
  2410. ent = xmlGetDocEntity(doc, cur->name);
  2411. if (ent != NULL) {
  2412. cur->content = ent->content;
  2413. /*
  2414. * The parent pointer in entity is a DTD pointer and thus is NOT
  2415. * updated. Not sure if this is 100% correct.
  2416. * -George
  2417. */
  2418. cur->children = (xmlNodePtr) ent;
  2419. cur->last = (xmlNodePtr) ent;
  2420. }
  2421. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  2422. xmlRegisterNodeDefaultValue(cur);
  2423. return(cur);
  2424. }
  2425. /**
  2426. * xmlNewDocText:
  2427. * @doc: the document
  2428. * @content: the text content
  2429. *
  2430. * Creation of a new text node within a document.
  2431. * Returns a pointer to the new node object.
  2432. */
  2433. xmlNodePtr
  2434. xmlNewDocText(const xmlDoc *doc, const xmlChar *content) {
  2435. xmlNodePtr cur;
  2436. cur = xmlNewText(content);
  2437. if (cur != NULL) cur->doc = (xmlDoc *)doc;
  2438. return(cur);
  2439. }
  2440. /**
  2441. * xmlNewTextLen:
  2442. * @content: the text content
  2443. * @len: the text len.
  2444. *
  2445. * Creation of a new text node with an extra parameter for the content's length
  2446. * Returns a pointer to the new node object.
  2447. */
  2448. xmlNodePtr
  2449. xmlNewTextLen(const xmlChar *content, int len) {
  2450. xmlNodePtr cur;
  2451. /*
  2452. * Allocate a new node and fill the fields.
  2453. */
  2454. cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
  2455. if (cur == NULL) {
  2456. xmlTreeErrMemory("building text");
  2457. return(NULL);
  2458. }
  2459. memset(cur, 0, sizeof(xmlNode));
  2460. cur->type = XML_TEXT_NODE;
  2461. cur->name = xmlStringText;
  2462. if (content != NULL) {
  2463. cur->content = xmlStrndup(content, len);
  2464. }
  2465. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  2466. xmlRegisterNodeDefaultValue(cur);
  2467. return(cur);
  2468. }
  2469. /**
  2470. * xmlNewDocTextLen:
  2471. * @doc: the document
  2472. * @content: the text content
  2473. * @len: the text len.
  2474. *
  2475. * Creation of a new text node with an extra content length parameter. The
  2476. * text node pertain to a given document.
  2477. * Returns a pointer to the new node object.
  2478. */
  2479. xmlNodePtr
  2480. xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
  2481. xmlNodePtr cur;
  2482. cur = xmlNewTextLen(content, len);
  2483. if (cur != NULL) cur->doc = doc;
  2484. return(cur);
  2485. }
  2486. /**
  2487. * xmlNewComment:
  2488. * @content: the comment content
  2489. *
  2490. * Creation of a new node containing a comment.
  2491. * Returns a pointer to the new node object.
  2492. */
  2493. xmlNodePtr
  2494. xmlNewComment(const xmlChar *content) {
  2495. xmlNodePtr cur;
  2496. /*
  2497. * Allocate a new node and fill the fields.
  2498. */
  2499. cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
  2500. if (cur == NULL) {
  2501. xmlTreeErrMemory("building comment");
  2502. return(NULL);
  2503. }
  2504. memset(cur, 0, sizeof(xmlNode));
  2505. cur->type = XML_COMMENT_NODE;
  2506. cur->name = xmlStringComment;
  2507. if (content != NULL) {
  2508. cur->content = xmlStrdup(content);
  2509. }
  2510. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  2511. xmlRegisterNodeDefaultValue(cur);
  2512. return(cur);
  2513. }
  2514. /**
  2515. * xmlNewCDataBlock:
  2516. * @doc: the document
  2517. * @content: the CDATA block content content
  2518. * @len: the length of the block
  2519. *
  2520. * Creation of a new node containing a CDATA block.
  2521. * Returns a pointer to the new node object.
  2522. */
  2523. xmlNodePtr
  2524. xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
  2525. xmlNodePtr cur;
  2526. /*
  2527. * Allocate a new node and fill the fields.
  2528. */
  2529. cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
  2530. if (cur == NULL) {
  2531. xmlTreeErrMemory("building CDATA");
  2532. return(NULL);
  2533. }
  2534. memset(cur, 0, sizeof(xmlNode));
  2535. cur->type = XML_CDATA_SECTION_NODE;
  2536. cur->doc = doc;
  2537. if (content != NULL) {
  2538. cur->content = xmlStrndup(content, len);
  2539. }
  2540. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  2541. xmlRegisterNodeDefaultValue(cur);
  2542. return(cur);
  2543. }
  2544. /**
  2545. * xmlNewDocComment:
  2546. * @doc: the document
  2547. * @content: the comment content
  2548. *
  2549. * Creation of a new node containing a comment within a document.
  2550. * Returns a pointer to the new node object.
  2551. */
  2552. xmlNodePtr
  2553. xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
  2554. xmlNodePtr cur;
  2555. cur = xmlNewComment(content);
  2556. if (cur != NULL) cur->doc = doc;
  2557. return(cur);
  2558. }
  2559. static const xmlChar *_copyStringForNewDictIfNeeded(xmlDictPtr oldDict, xmlDictPtr newDict, const xmlChar *oldValue) {
  2560. const xmlChar *newValue = oldValue;
  2561. if (oldValue) {
  2562. int oldDictOwnsOldValue = oldDict && (xmlDictOwns(oldDict, oldValue) == 1);
  2563. if (oldDictOwnsOldValue) {
  2564. if (newDict)
  2565. newValue = xmlDictLookup(newDict, oldValue, -1);
  2566. else
  2567. newValue = xmlStrdup(oldValue);
  2568. }
  2569. }
  2570. return newValue;
  2571. }
  2572. /**
  2573. * xmlSetTreeDoc:
  2574. * @tree: the top element
  2575. * @doc: the document
  2576. *
  2577. * update all nodes under the tree to point to the right document
  2578. */
  2579. void
  2580. xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
  2581. xmlAttrPtr prop;
  2582. if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL))
  2583. return;
  2584. if (tree->doc != doc) {
  2585. xmlDictPtr oldTreeDict = tree->doc ? tree->doc->dict : NULL;
  2586. xmlDictPtr newDict = doc ? doc->dict : NULL;
  2587. if(tree->type == XML_ELEMENT_NODE) {
  2588. prop = tree->properties;
  2589. while (prop != NULL) {
  2590. if (prop->atype == XML_ATTRIBUTE_ID) {
  2591. xmlRemoveID(tree->doc, prop);
  2592. }
  2593. if (prop->doc != doc) {
  2594. xmlDictPtr oldPropDict = prop->doc ? prop->doc->dict : NULL;
  2595. prop->name = _copyStringForNewDictIfNeeded(oldPropDict, newDict, prop->name);
  2596. prop->doc = doc;
  2597. }
  2598. xmlSetListDoc(prop->children, doc);
  2599. /*
  2600. * TODO: ID attributes should be also added to the new
  2601. * document, but this breaks things like xmlReplaceNode.
  2602. * The underlying problem is that xmlRemoveID is only called
  2603. * if a node is destroyed, not if it's unlinked.
  2604. */
  2605. #if 0
  2606. if (xmlIsID(doc, tree, prop)) {
  2607. xmlChar *idVal = xmlNodeListGetString(doc, prop->children,
  2608. 1);
  2609. xmlAddID(NULL, doc, idVal, prop);
  2610. }
  2611. #endif
  2612. prop = prop->next;
  2613. }
  2614. }
  2615. if (tree->type == XML_ENTITY_REF_NODE) {
  2616. /*
  2617. * Clear 'children' which points to the entity declaration
  2618. * from the original document.
  2619. */
  2620. tree->children = NULL;
  2621. } else if (tree->children != NULL) {
  2622. xmlSetListDoc(tree->children, doc);
  2623. }
  2624. tree->name = _copyStringForNewDictIfNeeded(oldTreeDict, newDict, tree->name);
  2625. tree->content = (xmlChar *)_copyStringForNewDictIfNeeded(oldTreeDict, NULL, tree->content);
  2626. /* FIXME: tree->ns should be updated as in xmlStaticCopyNode(). */
  2627. tree->doc = doc;
  2628. }
  2629. }
  2630. /**
  2631. * xmlSetListDoc:
  2632. * @list: the first element
  2633. * @doc: the document
  2634. *
  2635. * update all nodes in the list to point to the right document
  2636. */
  2637. void
  2638. xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
  2639. xmlNodePtr cur;
  2640. if ((list == NULL) || (list->type == XML_NAMESPACE_DECL))
  2641. return;
  2642. cur = list;
  2643. while (cur != NULL) {
  2644. if (cur->doc != doc)
  2645. xmlSetTreeDoc(cur, doc);
  2646. cur = cur->next;
  2647. }
  2648. }
  2649. #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
  2650. /**
  2651. * xmlNewChild:
  2652. * @parent: the parent node
  2653. * @ns: a namespace if any
  2654. * @name: the name of the child
  2655. * @content: the XML content of the child if any.
  2656. *
  2657. * Creation of a new child element, added at the end of @parent children list.
  2658. * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
  2659. * created element inherits the namespace of @parent. If @content is non NULL,
  2660. * a child list containing the TEXTs and ENTITY_REFs node will be created.
  2661. * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
  2662. * references. XML special chars must be escaped first by using
  2663. * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used.
  2664. *
  2665. * Returns a pointer to the new node object.
  2666. */
  2667. xmlNodePtr
  2668. xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
  2669. const xmlChar *name, const xmlChar *content) {
  2670. xmlNodePtr cur, prev;
  2671. if (parent == NULL) {
  2672. #ifdef DEBUG_TREE
  2673. xmlGenericError(xmlGenericErrorContext,
  2674. "xmlNewChild : parent == NULL\n");
  2675. #endif
  2676. return(NULL);
  2677. }
  2678. if (name == NULL) {
  2679. #ifdef DEBUG_TREE
  2680. xmlGenericError(xmlGenericErrorContext,
  2681. "xmlNewChild : name == NULL\n");
  2682. #endif
  2683. return(NULL);
  2684. }
  2685. /*
  2686. * Allocate a new node
  2687. */
  2688. if (parent->type == XML_ELEMENT_NODE) {
  2689. if (ns == NULL)
  2690. cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
  2691. else
  2692. cur = xmlNewDocNode(parent->doc, ns, name, content);
  2693. } else if ((parent->type == XML_DOCUMENT_NODE) ||
  2694. (parent->type == XML_HTML_DOCUMENT_NODE)) {
  2695. if (ns == NULL)
  2696. cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content);
  2697. else
  2698. cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content);
  2699. } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
  2700. cur = xmlNewDocNode( parent->doc, ns, name, content);
  2701. } else {
  2702. return(NULL);
  2703. }
  2704. if (cur == NULL) return(NULL);
  2705. /*
  2706. * add the new element at the end of the children list.
  2707. */
  2708. cur->type = XML_ELEMENT_NODE;
  2709. cur->parent = parent;
  2710. cur->doc = parent->doc;
  2711. if (parent->children == NULL) {
  2712. parent->children = cur;
  2713. parent->last = cur;
  2714. } else {
  2715. prev = parent->last;
  2716. prev->next = cur;
  2717. cur->prev = prev;
  2718. parent->last = cur;
  2719. }
  2720. return(cur);
  2721. }
  2722. #endif /* LIBXML_TREE_ENABLED */
  2723. /**
  2724. * xmlAddPropSibling:
  2725. * @prev: the attribute to which @prop is added after
  2726. * @cur: the base attribute passed to calling function
  2727. * @prop: the new attribute
  2728. *
  2729. * Add a new attribute after @prev using @cur as base attribute.
  2730. * When inserting before @cur, @prev is passed as @cur->prev.
  2731. * When inserting after @cur, @prev is passed as @cur.
  2732. * If an existing attribute is found it is destroyed prior to adding @prop.
  2733. *
  2734. * Returns the attribute being inserted or NULL in case of error.
  2735. */
  2736. static xmlNodePtr
  2737. xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) {
  2738. xmlAttrPtr attr;
  2739. if ((cur == NULL) || (cur->type != XML_ATTRIBUTE_NODE) ||
  2740. (prop == NULL) || (prop->type != XML_ATTRIBUTE_NODE) ||
  2741. ((prev != NULL) && (prev->type != XML_ATTRIBUTE_NODE)))
  2742. return(NULL);
  2743. /* check if an attribute with the same name exists */
  2744. if (prop->ns == NULL)
  2745. attr = xmlHasNsProp(cur->parent, prop->name, NULL);
  2746. else
  2747. attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href);
  2748. if (prop->doc != cur->doc) {
  2749. xmlSetTreeDoc(prop, cur->doc);
  2750. }
  2751. prop->parent = cur->parent;
  2752. prop->prev = prev;
  2753. if (prev != NULL) {
  2754. prop->next = prev->next;
  2755. prev->next = prop;
  2756. if (prop->next)
  2757. prop->next->prev = prop;
  2758. } else {
  2759. prop->next = cur;
  2760. cur->prev = prop;
  2761. }
  2762. if (prop->prev == NULL && prop->parent != NULL)
  2763. prop->parent->properties = (xmlAttrPtr) prop;
  2764. if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) {
  2765. /* different instance, destroy it (attributes must be unique) */
  2766. xmlRemoveProp((xmlAttrPtr) attr);
  2767. }
  2768. return prop;
  2769. }
  2770. /**
  2771. * xmlAddNextSibling:
  2772. * @cur: the child node
  2773. * @elem: the new node
  2774. *
  2775. * Add a new node @elem as the next sibling of @cur
  2776. * If the new node was already inserted in a document it is
  2777. * first unlinked from its existing context.
  2778. * As a result of text merging @elem may be freed.
  2779. * If the new node is ATTRIBUTE, it is added into properties instead of children.
  2780. * If there is an attribute with equal name, it is first destroyed.
  2781. *
  2782. * Returns the new node or NULL in case of error.
  2783. */
  2784. xmlNodePtr
  2785. xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
  2786. if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
  2787. #ifdef DEBUG_TREE
  2788. xmlGenericError(xmlGenericErrorContext,
  2789. "xmlAddNextSibling : cur == NULL\n");
  2790. #endif
  2791. return(NULL);
  2792. }
  2793. if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
  2794. #ifdef DEBUG_TREE
  2795. xmlGenericError(xmlGenericErrorContext,
  2796. "xmlAddNextSibling : elem == NULL\n");
  2797. #endif
  2798. return(NULL);
  2799. }
  2800. if (cur == elem) {
  2801. #ifdef DEBUG_TREE
  2802. xmlGenericError(xmlGenericErrorContext,
  2803. "xmlAddNextSibling : cur == elem\n");
  2804. #endif
  2805. return(NULL);
  2806. }
  2807. xmlUnlinkNode(elem);
  2808. if (elem->type == XML_TEXT_NODE) {
  2809. if (cur->type == XML_TEXT_NODE) {
  2810. xmlNodeAddContent(cur, elem->content);
  2811. xmlFreeNode(elem);
  2812. return(cur);
  2813. }
  2814. if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
  2815. (cur->name == cur->next->name)) {
  2816. xmlChar *tmp;
  2817. tmp = xmlStrdup(elem->content);
  2818. tmp = xmlStrcat(tmp, cur->next->content);
  2819. xmlNodeSetContent(cur->next, tmp);
  2820. xmlFree(tmp);
  2821. xmlFreeNode(elem);
  2822. return(cur->next);
  2823. }
  2824. } else if (elem->type == XML_ATTRIBUTE_NODE) {
  2825. return xmlAddPropSibling(cur, cur, elem);
  2826. }
  2827. if (elem->doc != cur->doc) {
  2828. xmlSetTreeDoc(elem, cur->doc);
  2829. }
  2830. elem->parent = cur->parent;
  2831. elem->prev = cur;
  2832. elem->next = cur->next;
  2833. cur->next = elem;
  2834. if (elem->next != NULL)
  2835. elem->next->prev = elem;
  2836. if ((elem->parent != NULL) && (elem->parent->last == cur))
  2837. elem->parent->last = elem;
  2838. return(elem);
  2839. }
  2840. #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
  2841. defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
  2842. /**
  2843. * xmlAddPrevSibling:
  2844. * @cur: the child node
  2845. * @elem: the new node
  2846. *
  2847. * Add a new node @elem as the previous sibling of @cur
  2848. * merging adjacent TEXT nodes (@elem may be freed)
  2849. * If the new node was already inserted in a document it is
  2850. * first unlinked from its existing context.
  2851. * If the new node is ATTRIBUTE, it is added into properties instead of children.
  2852. * If there is an attribute with equal name, it is first destroyed.
  2853. *
  2854. * Returns the new node or NULL in case of error.
  2855. */
  2856. xmlNodePtr
  2857. xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
  2858. if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
  2859. #ifdef DEBUG_TREE
  2860. xmlGenericError(xmlGenericErrorContext,
  2861. "xmlAddPrevSibling : cur == NULL\n");
  2862. #endif
  2863. return(NULL);
  2864. }
  2865. if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
  2866. #ifdef DEBUG_TREE
  2867. xmlGenericError(xmlGenericErrorContext,
  2868. "xmlAddPrevSibling : elem == NULL\n");
  2869. #endif
  2870. return(NULL);
  2871. }
  2872. if (cur == elem) {
  2873. #ifdef DEBUG_TREE
  2874. xmlGenericError(xmlGenericErrorContext,
  2875. "xmlAddPrevSibling : cur == elem\n");
  2876. #endif
  2877. return(NULL);
  2878. }
  2879. xmlUnlinkNode(elem);
  2880. if (elem->type == XML_TEXT_NODE) {
  2881. if (cur->type == XML_TEXT_NODE) {
  2882. xmlChar *tmp;
  2883. tmp = xmlStrdup(elem->content);
  2884. tmp = xmlStrcat(tmp, cur->content);
  2885. xmlNodeSetContent(cur, tmp);
  2886. xmlFree(tmp);
  2887. xmlFreeNode(elem);
  2888. return(cur);
  2889. }
  2890. if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
  2891. (cur->name == cur->prev->name)) {
  2892. xmlNodeAddContent(cur->prev, elem->content);
  2893. xmlFreeNode(elem);
  2894. return(cur->prev);
  2895. }
  2896. } else if (elem->type == XML_ATTRIBUTE_NODE) {
  2897. return xmlAddPropSibling(cur->prev, cur, elem);
  2898. }
  2899. if (elem->doc != cur->doc) {
  2900. xmlSetTreeDoc(elem, cur->doc);
  2901. }
  2902. elem->parent = cur->parent;
  2903. elem->next = cur;
  2904. elem->prev = cur->prev;
  2905. cur->prev = elem;
  2906. if (elem->prev != NULL)
  2907. elem->prev->next = elem;
  2908. if ((elem->parent != NULL) && (elem->parent->children == cur)) {
  2909. elem->parent->children = elem;
  2910. }
  2911. return(elem);
  2912. }
  2913. #endif /* LIBXML_TREE_ENABLED */
  2914. /**
  2915. * xmlAddSibling:
  2916. * @cur: the child node
  2917. * @elem: the new node
  2918. *
  2919. * Add a new element @elem to the list of siblings of @cur
  2920. * merging adjacent TEXT nodes (@elem may be freed)
  2921. * If the new element was already inserted in a document it is
  2922. * first unlinked from its existing context.
  2923. *
  2924. * Returns the new element or NULL in case of error.
  2925. */
  2926. xmlNodePtr
  2927. xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
  2928. xmlNodePtr parent;
  2929. if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
  2930. #ifdef DEBUG_TREE
  2931. xmlGenericError(xmlGenericErrorContext,
  2932. "xmlAddSibling : cur == NULL\n");
  2933. #endif
  2934. return(NULL);
  2935. }
  2936. if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
  2937. #ifdef DEBUG_TREE
  2938. xmlGenericError(xmlGenericErrorContext,
  2939. "xmlAddSibling : elem == NULL\n");
  2940. #endif
  2941. return(NULL);
  2942. }
  2943. if (cur == elem) {
  2944. #ifdef DEBUG_TREE
  2945. xmlGenericError(xmlGenericErrorContext,
  2946. "xmlAddSibling : cur == elem\n");
  2947. #endif
  2948. return(NULL);
  2949. }
  2950. /*
  2951. * Constant time is we can rely on the ->parent->last to find
  2952. * the last sibling.
  2953. */
  2954. if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) &&
  2955. (cur->parent->children != NULL) &&
  2956. (cur->parent->last != NULL) &&
  2957. (cur->parent->last->next == NULL)) {
  2958. cur = cur->parent->last;
  2959. } else {
  2960. while (cur->next != NULL) cur = cur->next;
  2961. }
  2962. xmlUnlinkNode(elem);
  2963. if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) &&
  2964. (cur->name == elem->name)) {
  2965. xmlNodeAddContent(cur, elem->content);
  2966. xmlFreeNode(elem);
  2967. return(cur);
  2968. } else if (elem->type == XML_ATTRIBUTE_NODE) {
  2969. return xmlAddPropSibling(cur, cur, elem);
  2970. }
  2971. if (elem->doc != cur->doc) {
  2972. xmlSetTreeDoc(elem, cur->doc);
  2973. }
  2974. parent = cur->parent;
  2975. elem->prev = cur;
  2976. elem->next = NULL;
  2977. elem->parent = parent;
  2978. cur->next = elem;
  2979. if (parent != NULL)
  2980. parent->last = elem;
  2981. return(elem);
  2982. }
  2983. /**
  2984. * xmlAddChildList:
  2985. * @parent: the parent node
  2986. * @cur: the first node in the list
  2987. *
  2988. * Add a list of node at the end of the child list of the parent
  2989. * merging adjacent TEXT nodes (@cur may be freed)
  2990. *
  2991. * Returns the last child or NULL in case of error.
  2992. */
  2993. xmlNodePtr
  2994. xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
  2995. xmlNodePtr prev;
  2996. if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
  2997. #ifdef DEBUG_TREE
  2998. xmlGenericError(xmlGenericErrorContext,
  2999. "xmlAddChildList : parent == NULL\n");
  3000. #endif
  3001. return(NULL);
  3002. }
  3003. if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
  3004. #ifdef DEBUG_TREE
  3005. xmlGenericError(xmlGenericErrorContext,
  3006. "xmlAddChildList : child == NULL\n");
  3007. #endif
  3008. return(NULL);
  3009. }
  3010. if ((cur->doc != NULL) && (parent->doc != NULL) &&
  3011. (cur->doc != parent->doc)) {
  3012. #ifdef DEBUG_TREE
  3013. xmlGenericError(xmlGenericErrorContext,
  3014. "Elements moved to a different document\n");
  3015. #endif
  3016. }
  3017. /*
  3018. * add the first element at the end of the children list.
  3019. */
  3020. if (parent->children == NULL) {
  3021. parent->children = cur;
  3022. } else {
  3023. /*
  3024. * If cur and parent->last both are TEXT nodes, then merge them.
  3025. */
  3026. if ((cur->type == XML_TEXT_NODE) &&
  3027. (parent->last->type == XML_TEXT_NODE) &&
  3028. (cur->name == parent->last->name)) {
  3029. xmlNodeAddContent(parent->last, cur->content);
  3030. /*
  3031. * if it's the only child, nothing more to be done.
  3032. */
  3033. if (cur->next == NULL) {
  3034. xmlFreeNode(cur);
  3035. return(parent->last);
  3036. }
  3037. prev = cur;
  3038. cur = cur->next;
  3039. xmlFreeNode(prev);
  3040. }
  3041. prev = parent->last;
  3042. prev->next = cur;
  3043. cur->prev = prev;
  3044. }
  3045. while (cur->next != NULL) {
  3046. cur->parent = parent;
  3047. if (cur->doc != parent->doc) {
  3048. xmlSetTreeDoc(cur, parent->doc);
  3049. }
  3050. cur = cur->next;
  3051. }
  3052. cur->parent = parent;
  3053. /* the parent may not be linked to a doc ! */
  3054. if (cur->doc != parent->doc) {
  3055. xmlSetTreeDoc(cur, parent->doc);
  3056. }
  3057. parent->last = cur;
  3058. return(cur);
  3059. }
  3060. /**
  3061. * xmlAddChild:
  3062. * @parent: the parent node
  3063. * @cur: the child node
  3064. *
  3065. * Add a new node to @parent, at the end of the child (or property) list
  3066. * merging adjacent TEXT nodes (in which case @cur is freed)
  3067. * If the new node is ATTRIBUTE, it is added into properties instead of children.
  3068. * If there is an attribute with equal name, it is first destroyed.
  3069. *
  3070. * Returns the child or NULL in case of error.
  3071. */
  3072. xmlNodePtr
  3073. xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
  3074. xmlNodePtr prev;
  3075. if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
  3076. #ifdef DEBUG_TREE
  3077. xmlGenericError(xmlGenericErrorContext,
  3078. "xmlAddChild : parent == NULL\n");
  3079. #endif
  3080. return(NULL);
  3081. }
  3082. if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
  3083. #ifdef DEBUG_TREE
  3084. xmlGenericError(xmlGenericErrorContext,
  3085. "xmlAddChild : child == NULL\n");
  3086. #endif
  3087. return(NULL);
  3088. }
  3089. if (parent == cur) {
  3090. #ifdef DEBUG_TREE
  3091. xmlGenericError(xmlGenericErrorContext,
  3092. "xmlAddChild : parent == cur\n");
  3093. #endif
  3094. return(NULL);
  3095. }
  3096. /*
  3097. * If cur is a TEXT node, merge its content with adjacent TEXT nodes
  3098. * cur is then freed.
  3099. */
  3100. if (cur->type == XML_TEXT_NODE) {
  3101. if ((parent->type == XML_TEXT_NODE) &&
  3102. (parent->content != NULL) &&
  3103. (parent->name == cur->name)) {
  3104. xmlNodeAddContent(parent, cur->content);
  3105. xmlFreeNode(cur);
  3106. return(parent);
  3107. }
  3108. if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
  3109. (parent->last->name == cur->name) &&
  3110. (parent->last != cur)) {
  3111. xmlNodeAddContent(parent->last, cur->content);
  3112. xmlFreeNode(cur);
  3113. return(parent->last);
  3114. }
  3115. }
  3116. /*
  3117. * add the new element at the end of the children list.
  3118. */
  3119. prev = cur->parent;
  3120. cur->parent = parent;
  3121. if (cur->doc != parent->doc) {
  3122. xmlSetTreeDoc(cur, parent->doc);
  3123. }
  3124. /* this check prevents a loop on tree-traversions if a developer
  3125. * tries to add a node to its parent multiple times
  3126. */
  3127. if (prev == parent)
  3128. return(cur);
  3129. /*
  3130. * Coalescing
  3131. */
  3132. if ((parent->type == XML_TEXT_NODE) &&
  3133. (parent->content != NULL) &&
  3134. (parent != cur)) {
  3135. xmlNodeAddContent(parent, cur->content);
  3136. xmlFreeNode(cur);
  3137. return(parent);
  3138. }
  3139. if (cur->type == XML_ATTRIBUTE_NODE) {
  3140. if (parent->type != XML_ELEMENT_NODE)
  3141. return(NULL);
  3142. if (parent->properties != NULL) {
  3143. /* check if an attribute with the same name exists */
  3144. xmlAttrPtr lastattr;
  3145. if (cur->ns == NULL)
  3146. lastattr = xmlHasNsProp(parent, cur->name, NULL);
  3147. else
  3148. lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
  3149. if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) {
  3150. /* different instance, destroy it (attributes must be unique) */
  3151. xmlUnlinkNode((xmlNodePtr) lastattr);
  3152. xmlFreeProp(lastattr);
  3153. }
  3154. if (lastattr == (xmlAttrPtr) cur)
  3155. return(cur);
  3156. }
  3157. if (parent->properties == NULL) {
  3158. parent->properties = (xmlAttrPtr) cur;
  3159. } else {
  3160. /* find the end */
  3161. xmlAttrPtr lastattr = parent->properties;
  3162. while (lastattr->next != NULL) {
  3163. lastattr = lastattr->next;
  3164. }
  3165. lastattr->next = (xmlAttrPtr) cur;
  3166. ((xmlAttrPtr) cur)->prev = lastattr;
  3167. }
  3168. } else {
  3169. if (parent->children == NULL) {
  3170. parent->children = cur;
  3171. parent->last = cur;
  3172. } else {
  3173. prev = parent->last;
  3174. prev->next = cur;
  3175. cur->prev = prev;
  3176. parent->last = cur;
  3177. }
  3178. }
  3179. return(cur);
  3180. }
  3181. /**
  3182. * xmlGetLastChild:
  3183. * @parent: the parent node
  3184. *
  3185. * Search the last child of a node.
  3186. * Returns the last child or NULL if none.
  3187. */
  3188. xmlNodePtr
  3189. xmlGetLastChild(const xmlNode *parent) {
  3190. if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
  3191. #ifdef DEBUG_TREE
  3192. xmlGenericError(xmlGenericErrorContext,
  3193. "xmlGetLastChild : parent == NULL\n");
  3194. #endif
  3195. return(NULL);
  3196. }
  3197. return(parent->last);
  3198. }
  3199. #ifdef LIBXML_TREE_ENABLED
  3200. /*
  3201. * 5 interfaces from DOM ElementTraversal
  3202. */
  3203. /**
  3204. * xmlChildElementCount:
  3205. * @parent: the parent node
  3206. *
  3207. * Finds the current number of child nodes of that element which are
  3208. * element nodes.
  3209. * Note the handling of entities references is different than in
  3210. * the W3C DOM element traversal spec since we don't have back reference
  3211. * from entities content to entities references.
  3212. *
  3213. * Returns the count of element child or 0 if not available
  3214. */
  3215. unsigned long
  3216. xmlChildElementCount(xmlNodePtr parent) {
  3217. unsigned long ret = 0;
  3218. xmlNodePtr cur = NULL;
  3219. if (parent == NULL)
  3220. return(0);
  3221. switch (parent->type) {
  3222. case XML_ELEMENT_NODE:
  3223. case XML_ENTITY_NODE:
  3224. case XML_DOCUMENT_NODE:
  3225. case XML_DOCUMENT_FRAG_NODE:
  3226. case XML_HTML_DOCUMENT_NODE:
  3227. cur = parent->children;
  3228. break;
  3229. default:
  3230. return(0);
  3231. }
  3232. while (cur != NULL) {
  3233. if (cur->type == XML_ELEMENT_NODE)
  3234. ret++;
  3235. cur = cur->next;
  3236. }
  3237. return(ret);
  3238. }
  3239. /**
  3240. * xmlFirstElementChild:
  3241. * @parent: the parent node
  3242. *
  3243. * Finds the first child node of that element which is a Element node
  3244. * Note the handling of entities references is different than in
  3245. * the W3C DOM element traversal spec since we don't have back reference
  3246. * from entities content to entities references.
  3247. *
  3248. * Returns the first element child or NULL if not available
  3249. */
  3250. xmlNodePtr
  3251. xmlFirstElementChild(xmlNodePtr parent) {
  3252. xmlNodePtr cur = NULL;
  3253. if (parent == NULL)
  3254. return(NULL);
  3255. switch (parent->type) {
  3256. case XML_ELEMENT_NODE:
  3257. case XML_ENTITY_NODE:
  3258. case XML_DOCUMENT_NODE:
  3259. case XML_DOCUMENT_FRAG_NODE:
  3260. case XML_HTML_DOCUMENT_NODE:
  3261. cur = parent->children;
  3262. break;
  3263. default:
  3264. return(NULL);
  3265. }
  3266. while (cur != NULL) {
  3267. if (cur->type == XML_ELEMENT_NODE)
  3268. return(cur);
  3269. cur = cur->next;
  3270. }
  3271. return(NULL);
  3272. }
  3273. /**
  3274. * xmlLastElementChild:
  3275. * @parent: the parent node
  3276. *
  3277. * Finds the last child node of that element which is a Element node
  3278. * Note the handling of entities references is different than in
  3279. * the W3C DOM element traversal spec since we don't have back reference
  3280. * from entities content to entities references.
  3281. *
  3282. * Returns the last element child or NULL if not available
  3283. */
  3284. xmlNodePtr
  3285. xmlLastElementChild(xmlNodePtr parent) {
  3286. xmlNodePtr cur = NULL;
  3287. if (parent == NULL)
  3288. return(NULL);
  3289. switch (parent->type) {
  3290. case XML_ELEMENT_NODE:
  3291. case XML_ENTITY_NODE:
  3292. case XML_DOCUMENT_NODE:
  3293. case XML_DOCUMENT_FRAG_NODE:
  3294. case XML_HTML_DOCUMENT_NODE:
  3295. cur = parent->last;
  3296. break;
  3297. default:
  3298. return(NULL);
  3299. }
  3300. while (cur != NULL) {
  3301. if (cur->type == XML_ELEMENT_NODE)
  3302. return(cur);
  3303. cur = cur->prev;
  3304. }
  3305. return(NULL);
  3306. }
  3307. /**
  3308. * xmlPreviousElementSibling:
  3309. * @node: the current node
  3310. *
  3311. * Finds the first closest previous sibling of the node which is an
  3312. * element node.
  3313. * Note the handling of entities references is different than in
  3314. * the W3C DOM element traversal spec since we don't have back reference
  3315. * from entities content to entities references.
  3316. *
  3317. * Returns the previous element sibling or NULL if not available
  3318. */
  3319. xmlNodePtr
  3320. xmlPreviousElementSibling(xmlNodePtr node) {
  3321. if (node == NULL)
  3322. return(NULL);
  3323. switch (node->type) {
  3324. case XML_ELEMENT_NODE:
  3325. case XML_TEXT_NODE:
  3326. case XML_CDATA_SECTION_NODE:
  3327. case XML_ENTITY_REF_NODE:
  3328. case XML_ENTITY_NODE:
  3329. case XML_PI_NODE:
  3330. case XML_COMMENT_NODE:
  3331. case XML_XINCLUDE_START:
  3332. case XML_XINCLUDE_END:
  3333. node = node->prev;
  3334. break;
  3335. default:
  3336. return(NULL);
  3337. }
  3338. while (node != NULL) {
  3339. if (node->type == XML_ELEMENT_NODE)
  3340. return(node);
  3341. node = node->prev;
  3342. }
  3343. return(NULL);
  3344. }
  3345. /**
  3346. * xmlNextElementSibling:
  3347. * @node: the current node
  3348. *
  3349. * Finds the first closest next sibling of the node which is an
  3350. * element node.
  3351. * Note the handling of entities references is different than in
  3352. * the W3C DOM element traversal spec since we don't have back reference
  3353. * from entities content to entities references.
  3354. *
  3355. * Returns the next element sibling or NULL if not available
  3356. */
  3357. xmlNodePtr
  3358. xmlNextElementSibling(xmlNodePtr node) {
  3359. if (node == NULL)
  3360. return(NULL);
  3361. switch (node->type) {
  3362. case XML_ELEMENT_NODE:
  3363. case XML_TEXT_NODE:
  3364. case XML_CDATA_SECTION_NODE:
  3365. case XML_ENTITY_REF_NODE:
  3366. case XML_ENTITY_NODE:
  3367. case XML_PI_NODE:
  3368. case XML_COMMENT_NODE:
  3369. case XML_DTD_NODE:
  3370. case XML_XINCLUDE_START:
  3371. case XML_XINCLUDE_END:
  3372. node = node->next;
  3373. break;
  3374. default:
  3375. return(NULL);
  3376. }
  3377. while (node != NULL) {
  3378. if (node->type == XML_ELEMENT_NODE)
  3379. return(node);
  3380. node = node->next;
  3381. }
  3382. return(NULL);
  3383. }
  3384. #endif /* LIBXML_TREE_ENABLED */
  3385. /**
  3386. * xmlFreeNodeList:
  3387. * @cur: the first node in the list
  3388. *
  3389. * Free a node and all its siblings, this is a recursive behaviour, all
  3390. * the children are freed too.
  3391. */
  3392. void
  3393. xmlFreeNodeList(xmlNodePtr cur) {
  3394. xmlNodePtr next;
  3395. xmlNodePtr parent;
  3396. xmlDictPtr dict = NULL;
  3397. size_t depth = 0;
  3398. if (cur == NULL) return;
  3399. if (cur->type == XML_NAMESPACE_DECL) {
  3400. xmlFreeNsList((xmlNsPtr) cur);
  3401. return;
  3402. }
  3403. if (cur->doc != NULL) dict = cur->doc->dict;
  3404. while (1) {
  3405. while ((cur->children != NULL) &&
  3406. (cur->type != XML_DOCUMENT_NODE) &&
  3407. (cur->type != XML_HTML_DOCUMENT_NODE) &&
  3408. (cur->type != XML_DTD_NODE) &&
  3409. (cur->type != XML_ENTITY_REF_NODE)) {
  3410. cur = cur->children;
  3411. depth += 1;
  3412. }
  3413. next = cur->next;
  3414. parent = cur->parent;
  3415. if ((cur->type == XML_DOCUMENT_NODE) ||
  3416. (cur->type == XML_HTML_DOCUMENT_NODE)) {
  3417. xmlFreeDoc((xmlDocPtr) cur);
  3418. } else if (cur->type != XML_DTD_NODE) {
  3419. if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
  3420. xmlDeregisterNodeDefaultValue(cur);
  3421. if (((cur->type == XML_ELEMENT_NODE) ||
  3422. (cur->type == XML_XINCLUDE_START) ||
  3423. (cur->type == XML_XINCLUDE_END)) &&
  3424. (cur->properties != NULL))
  3425. xmlFreePropList(cur->properties);
  3426. if ((cur->type != XML_ELEMENT_NODE) &&
  3427. (cur->type != XML_XINCLUDE_START) &&
  3428. (cur->type != XML_XINCLUDE_END) &&
  3429. (cur->type != XML_ENTITY_REF_NODE) &&
  3430. (cur->content != (xmlChar *) &(cur->properties))) {
  3431. DICT_FREE(cur->content)
  3432. }
  3433. if (((cur->type == XML_ELEMENT_NODE) ||
  3434. (cur->type == XML_XINCLUDE_START) ||
  3435. (cur->type == XML_XINCLUDE_END)) &&
  3436. (cur->nsDef != NULL))
  3437. xmlFreeNsList(cur->nsDef);
  3438. /*
  3439. * When a node is a text node or a comment, it uses a global static
  3440. * variable for the name of the node.
  3441. * Otherwise the node name might come from the document's
  3442. * dictionary
  3443. */
  3444. if ((cur->name != NULL) &&
  3445. (cur->type != XML_TEXT_NODE) &&
  3446. (cur->type != XML_COMMENT_NODE))
  3447. DICT_FREE(cur->name)
  3448. xmlFree(cur);
  3449. }
  3450. if (next != NULL) {
  3451. cur = next;
  3452. } else {
  3453. if ((depth == 0) || (parent == NULL))
  3454. break;
  3455. depth -= 1;
  3456. cur = parent;
  3457. cur->children = NULL;
  3458. }
  3459. }
  3460. }
  3461. /**
  3462. * xmlFreeNode:
  3463. * @cur: the node
  3464. *
  3465. * Free a node, this is a recursive behaviour, all the children are freed too.
  3466. * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
  3467. */
  3468. void
  3469. xmlFreeNode(xmlNodePtr cur) {
  3470. xmlDictPtr dict = NULL;
  3471. if (cur == NULL) return;
  3472. /* use xmlFreeDtd for DTD nodes */
  3473. if (cur->type == XML_DTD_NODE) {
  3474. xmlFreeDtd((xmlDtdPtr) cur);
  3475. return;
  3476. }
  3477. if (cur->type == XML_NAMESPACE_DECL) {
  3478. xmlFreeNs((xmlNsPtr) cur);
  3479. return;
  3480. }
  3481. if (cur->type == XML_ATTRIBUTE_NODE) {
  3482. xmlFreeProp((xmlAttrPtr) cur);
  3483. return;
  3484. }
  3485. if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
  3486. xmlDeregisterNodeDefaultValue(cur);
  3487. if (cur->doc != NULL) dict = cur->doc->dict;
  3488. if (cur->type == XML_ENTITY_DECL) {
  3489. xmlEntityPtr ent = (xmlEntityPtr) cur;
  3490. DICT_FREE(ent->SystemID);
  3491. DICT_FREE(ent->ExternalID);
  3492. }
  3493. if ((cur->children != NULL) &&
  3494. (cur->type != XML_ENTITY_REF_NODE))
  3495. xmlFreeNodeList(cur->children);
  3496. if ((cur->type == XML_ELEMENT_NODE) ||
  3497. (cur->type == XML_XINCLUDE_START) ||
  3498. (cur->type == XML_XINCLUDE_END)) {
  3499. if (cur->properties != NULL)
  3500. xmlFreePropList(cur->properties);
  3501. if (cur->nsDef != NULL)
  3502. xmlFreeNsList(cur->nsDef);
  3503. } else if ((cur->content != NULL) &&
  3504. (cur->type != XML_ENTITY_REF_NODE) &&
  3505. (cur->content != (xmlChar *) &(cur->properties))) {
  3506. DICT_FREE(cur->content)
  3507. }
  3508. /*
  3509. * When a node is a text node or a comment, it uses a global static
  3510. * variable for the name of the node.
  3511. * Otherwise the node name might come from the document's dictionary
  3512. */
  3513. if ((cur->name != NULL) &&
  3514. (cur->type != XML_TEXT_NODE) &&
  3515. (cur->type != XML_COMMENT_NODE))
  3516. DICT_FREE(cur->name)
  3517. xmlFree(cur);
  3518. }
  3519. /**
  3520. * xmlUnlinkNode:
  3521. * @cur: the node
  3522. *
  3523. * Unlink a node from it's current context, the node is not freed
  3524. * If one need to free the node, use xmlFreeNode() routine after the
  3525. * unlink to discard it.
  3526. * Note that namespace nodes can't be unlinked as they do not have
  3527. * pointer to their parent.
  3528. */
  3529. void
  3530. xmlUnlinkNode(xmlNodePtr cur) {
  3531. if (cur == NULL) {
  3532. #ifdef DEBUG_TREE
  3533. xmlGenericError(xmlGenericErrorContext,
  3534. "xmlUnlinkNode : node == NULL\n");
  3535. #endif
  3536. return;
  3537. }
  3538. if (cur->type == XML_NAMESPACE_DECL)
  3539. return;
  3540. if (cur->type == XML_DTD_NODE) {
  3541. xmlDocPtr doc;
  3542. doc = cur->doc;
  3543. if (doc != NULL) {
  3544. if (doc->intSubset == (xmlDtdPtr) cur)
  3545. doc->intSubset = NULL;
  3546. if (doc->extSubset == (xmlDtdPtr) cur)
  3547. doc->extSubset = NULL;
  3548. }
  3549. }
  3550. if (cur->type == XML_ENTITY_DECL) {
  3551. xmlDocPtr doc;
  3552. doc = cur->doc;
  3553. if (doc != NULL) {
  3554. if (doc->intSubset != NULL) {
  3555. if (xmlHashLookup(doc->intSubset->entities, cur->name) == cur)
  3556. xmlHashRemoveEntry(doc->intSubset->entities, cur->name,
  3557. NULL);
  3558. if (xmlHashLookup(doc->intSubset->pentities, cur->name) == cur)
  3559. xmlHashRemoveEntry(doc->intSubset->pentities, cur->name,
  3560. NULL);
  3561. }
  3562. if (doc->extSubset != NULL) {
  3563. if (xmlHashLookup(doc->extSubset->entities, cur->name) == cur)
  3564. xmlHashRemoveEntry(doc->extSubset->entities, cur->name,
  3565. NULL);
  3566. if (xmlHashLookup(doc->extSubset->pentities, cur->name) == cur)
  3567. xmlHashRemoveEntry(doc->extSubset->pentities, cur->name,
  3568. NULL);
  3569. }
  3570. }
  3571. }
  3572. if (cur->parent != NULL) {
  3573. xmlNodePtr parent;
  3574. parent = cur->parent;
  3575. if (cur->type == XML_ATTRIBUTE_NODE) {
  3576. if (parent->properties == (xmlAttrPtr) cur)
  3577. parent->properties = ((xmlAttrPtr) cur)->next;
  3578. } else {
  3579. if (parent->children == cur)
  3580. parent->children = cur->next;
  3581. if (parent->last == cur)
  3582. parent->last = cur->prev;
  3583. }
  3584. cur->parent = NULL;
  3585. }
  3586. if (cur->next != NULL)
  3587. cur->next->prev = cur->prev;
  3588. if (cur->prev != NULL)
  3589. cur->prev->next = cur->next;
  3590. cur->next = cur->prev = NULL;
  3591. }
  3592. #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
  3593. /**
  3594. * xmlReplaceNode:
  3595. * @old: the old node
  3596. * @cur: the node
  3597. *
  3598. * Unlink the old node from its current context, prune the new one
  3599. * at the same place. If @cur was already inserted in a document it is
  3600. * first unlinked from its existing context.
  3601. *
  3602. * Returns the @old node
  3603. */
  3604. xmlNodePtr
  3605. xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
  3606. if (old == cur) return(NULL);
  3607. if ((old == NULL) || (old->type == XML_NAMESPACE_DECL) ||
  3608. (old->parent == NULL)) {
  3609. #ifdef DEBUG_TREE
  3610. xmlGenericError(xmlGenericErrorContext,
  3611. "xmlReplaceNode : old == NULL or without parent\n");
  3612. #endif
  3613. return(NULL);
  3614. }
  3615. if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
  3616. xmlUnlinkNode(old);
  3617. return(old);
  3618. }
  3619. if (cur == old) {
  3620. return(old);
  3621. }
  3622. if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
  3623. #ifdef DEBUG_TREE
  3624. xmlGenericError(xmlGenericErrorContext,
  3625. "xmlReplaceNode : Trying to replace attribute node with other node type\n");
  3626. #endif
  3627. return(old);
  3628. }
  3629. if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
  3630. #ifdef DEBUG_TREE
  3631. xmlGenericError(xmlGenericErrorContext,
  3632. "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
  3633. #endif
  3634. return(old);
  3635. }
  3636. xmlUnlinkNode(cur);
  3637. xmlSetTreeDoc(cur, old->doc);
  3638. cur->parent = old->parent;
  3639. cur->next = old->next;
  3640. if (cur->next != NULL)
  3641. cur->next->prev = cur;
  3642. cur->prev = old->prev;
  3643. if (cur->prev != NULL)
  3644. cur->prev->next = cur;
  3645. if (cur->parent != NULL) {
  3646. if (cur->type == XML_ATTRIBUTE_NODE) {
  3647. if (cur->parent->properties == (xmlAttrPtr)old)
  3648. cur->parent->properties = ((xmlAttrPtr) cur);
  3649. } else {
  3650. if (cur->parent->children == old)
  3651. cur->parent->children = cur;
  3652. if (cur->parent->last == old)
  3653. cur->parent->last = cur;
  3654. }
  3655. }
  3656. old->next = old->prev = NULL;
  3657. old->parent = NULL;
  3658. return(old);
  3659. }
  3660. #endif /* LIBXML_TREE_ENABLED */
  3661. /************************************************************************
  3662. * *
  3663. * Copy operations *
  3664. * *
  3665. ************************************************************************/
  3666. /**
  3667. * xmlCopyNamespace:
  3668. * @cur: the namespace
  3669. *
  3670. * Do a copy of the namespace.
  3671. *
  3672. * Returns: a new #xmlNsPtr, or NULL in case of error.
  3673. */
  3674. xmlNsPtr
  3675. xmlCopyNamespace(xmlNsPtr cur) {
  3676. xmlNsPtr ret;
  3677. if (cur == NULL) return(NULL);
  3678. switch (cur->type) {
  3679. case XML_LOCAL_NAMESPACE:
  3680. ret = xmlNewNs(NULL, cur->href, cur->prefix);
  3681. break;
  3682. default:
  3683. #ifdef DEBUG_TREE
  3684. xmlGenericError(xmlGenericErrorContext,
  3685. "xmlCopyNamespace: invalid type %d\n", cur->type);
  3686. #endif
  3687. return(NULL);
  3688. }
  3689. return(ret);
  3690. }
  3691. /**
  3692. * xmlCopyNamespaceList:
  3693. * @cur: the first namespace
  3694. *
  3695. * Do a copy of an namespace list.
  3696. *
  3697. * Returns: a new #xmlNsPtr, or NULL in case of error.
  3698. */
  3699. xmlNsPtr
  3700. xmlCopyNamespaceList(xmlNsPtr cur) {
  3701. xmlNsPtr ret = NULL;
  3702. xmlNsPtr p = NULL,q;
  3703. while (cur != NULL) {
  3704. q = xmlCopyNamespace(cur);
  3705. if (p == NULL) {
  3706. ret = p = q;
  3707. } else {
  3708. p->next = q;
  3709. p = q;
  3710. }
  3711. cur = cur->next;
  3712. }
  3713. return(ret);
  3714. }
  3715. static xmlNodePtr
  3716. xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
  3717. static xmlAttrPtr
  3718. xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) {
  3719. xmlAttrPtr ret;
  3720. if (cur == NULL) return(NULL);
  3721. if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
  3722. return(NULL);
  3723. if (target != NULL)
  3724. ret = xmlNewDocProp(target->doc, cur->name, NULL);
  3725. else if (doc != NULL)
  3726. ret = xmlNewDocProp(doc, cur->name, NULL);
  3727. else if (cur->parent != NULL)
  3728. ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
  3729. else if (cur->children != NULL)
  3730. ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
  3731. else
  3732. ret = xmlNewDocProp(NULL, cur->name, NULL);
  3733. if (ret == NULL) return(NULL);
  3734. ret->parent = target;
  3735. if ((cur->ns != NULL) && (target != NULL)) {
  3736. xmlNsPtr ns;
  3737. ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
  3738. if (ns == NULL) {
  3739. /*
  3740. * Humm, we are copying an element whose namespace is defined
  3741. * out of the new tree scope. Search it in the original tree
  3742. * and add it at the top of the new tree
  3743. */
  3744. ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix);
  3745. if (ns != NULL) {
  3746. xmlNodePtr root = target;
  3747. xmlNodePtr pred = NULL;
  3748. while (root->parent != NULL) {
  3749. pred = root;
  3750. root = root->parent;
  3751. }
  3752. if (root == (xmlNodePtr) target->doc) {
  3753. /* correct possibly cycling above the document elt */
  3754. root = pred;
  3755. }
  3756. ret->ns = xmlNewNs(root, ns->href, ns->prefix);
  3757. }
  3758. } else {
  3759. /*
  3760. * we have to find something appropriate here since
  3761. * we can't be sure, that the namespace we found is identified
  3762. * by the prefix
  3763. */
  3764. if (xmlStrEqual(ns->href, cur->ns->href)) {
  3765. /* this is the nice case */
  3766. ret->ns = ns;
  3767. } else {
  3768. /*
  3769. * we are in trouble: we need a new reconciled namespace.
  3770. * This is expensive
  3771. */
  3772. ret->ns = xmlNewReconciledNs(target->doc, target, cur->ns);
  3773. }
  3774. }
  3775. } else
  3776. ret->ns = NULL;
  3777. if (cur->children != NULL) {
  3778. xmlNodePtr tmp;
  3779. ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
  3780. ret->last = NULL;
  3781. tmp = ret->children;
  3782. while (tmp != NULL) {
  3783. /* tmp->parent = (xmlNodePtr)ret; */
  3784. if (tmp->next == NULL)
  3785. ret->last = tmp;
  3786. tmp = tmp->next;
  3787. }
  3788. }
  3789. /*
  3790. * Try to handle IDs
  3791. */
  3792. if ((target!= NULL) && (cur!= NULL) &&
  3793. (target->doc != NULL) && (cur->doc != NULL) &&
  3794. (cur->doc->ids != NULL) && (cur->parent != NULL)) {
  3795. if (xmlIsID(cur->doc, cur->parent, cur)) {
  3796. xmlChar *id;
  3797. id = xmlNodeListGetString(cur->doc, cur->children, 1);
  3798. if (id != NULL) {
  3799. xmlAddID(NULL, target->doc, id, ret);
  3800. xmlFree(id);
  3801. }
  3802. }
  3803. }
  3804. return(ret);
  3805. }
  3806. /**
  3807. * xmlCopyProp:
  3808. * @target: the element where the attribute will be grafted
  3809. * @cur: the attribute
  3810. *
  3811. * Do a copy of the attribute.
  3812. *
  3813. * Returns: a new #xmlAttrPtr, or NULL in case of error.
  3814. */
  3815. xmlAttrPtr
  3816. xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
  3817. return xmlCopyPropInternal(NULL, target, cur);
  3818. }
  3819. /**
  3820. * xmlCopyPropList:
  3821. * @target: the element where the attributes will be grafted
  3822. * @cur: the first attribute
  3823. *
  3824. * Do a copy of an attribute list.
  3825. *
  3826. * Returns: a new #xmlAttrPtr, or NULL in case of error.
  3827. */
  3828. xmlAttrPtr
  3829. xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
  3830. xmlAttrPtr ret = NULL;
  3831. xmlAttrPtr p = NULL,q;
  3832. if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
  3833. return(NULL);
  3834. while (cur != NULL) {
  3835. q = xmlCopyProp(target, cur);
  3836. if (q == NULL)
  3837. return(NULL);
  3838. if (p == NULL) {
  3839. ret = p = q;
  3840. } else {
  3841. p->next = q;
  3842. q->prev = p;
  3843. p = q;
  3844. }
  3845. cur = cur->next;
  3846. }
  3847. return(ret);
  3848. }
  3849. /*
  3850. * NOTE about the CopyNode operations !
  3851. *
  3852. * They are split into external and internal parts for one
  3853. * tricky reason: namespaces. Doing a direct copy of a node
  3854. * say RPM:Copyright without changing the namespace pointer to
  3855. * something else can produce stale links. One way to do it is
  3856. * to keep a reference counter but this doesn't work as soon
  3857. * as one moves the element or the subtree out of the scope of
  3858. * the existing namespace. The actual solution seems to be to add
  3859. * a copy of the namespace at the top of the copied tree if
  3860. * not available in the subtree.
  3861. * Hence two functions, the public front-end call the inner ones
  3862. * The argument "recursive" normally indicates a recursive copy
  3863. * of the node with values 0 (no) and 1 (yes). For XInclude,
  3864. * however, we allow a value of 2 to indicate copy properties and
  3865. * namespace info, but don't recurse on children.
  3866. */
  3867. static xmlNodePtr
  3868. xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
  3869. int extended) {
  3870. xmlNodePtr ret;
  3871. if (node == NULL) return(NULL);
  3872. switch (node->type) {
  3873. case XML_TEXT_NODE:
  3874. case XML_CDATA_SECTION_NODE:
  3875. case XML_ELEMENT_NODE:
  3876. case XML_DOCUMENT_FRAG_NODE:
  3877. case XML_ENTITY_REF_NODE:
  3878. case XML_ENTITY_NODE:
  3879. case XML_PI_NODE:
  3880. case XML_COMMENT_NODE:
  3881. case XML_XINCLUDE_START:
  3882. case XML_XINCLUDE_END:
  3883. break;
  3884. case XML_ATTRIBUTE_NODE:
  3885. return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node));
  3886. case XML_NAMESPACE_DECL:
  3887. return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
  3888. case XML_DOCUMENT_NODE:
  3889. case XML_HTML_DOCUMENT_NODE:
  3890. #ifdef LIBXML_TREE_ENABLED
  3891. return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended));
  3892. #endif /* LIBXML_TREE_ENABLED */
  3893. case XML_DOCUMENT_TYPE_NODE:
  3894. case XML_NOTATION_NODE:
  3895. case XML_DTD_NODE:
  3896. case XML_ELEMENT_DECL:
  3897. case XML_ATTRIBUTE_DECL:
  3898. case XML_ENTITY_DECL:
  3899. return(NULL);
  3900. }
  3901. /*
  3902. * Allocate a new node and fill the fields.
  3903. */
  3904. ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
  3905. if (ret == NULL) {
  3906. xmlTreeErrMemory("copying node");
  3907. return(NULL);
  3908. }
  3909. memset(ret, 0, sizeof(xmlNode));
  3910. ret->type = node->type;
  3911. ret->doc = doc;
  3912. ret->parent = parent;
  3913. if (node->name == xmlStringText)
  3914. ret->name = xmlStringText;
  3915. else if (node->name == xmlStringTextNoenc)
  3916. ret->name = xmlStringTextNoenc;
  3917. else if (node->name == xmlStringComment)
  3918. ret->name = xmlStringComment;
  3919. else if (node->name != NULL) {
  3920. if ((doc != NULL) && (doc->dict != NULL))
  3921. ret->name = xmlDictLookup(doc->dict, node->name, -1);
  3922. else
  3923. ret->name = xmlStrdup(node->name);
  3924. }
  3925. if ((node->type != XML_ELEMENT_NODE) &&
  3926. (node->content != NULL) &&
  3927. (node->type != XML_ENTITY_REF_NODE) &&
  3928. (node->type != XML_XINCLUDE_END) &&
  3929. (node->type != XML_XINCLUDE_START)) {
  3930. ret->content = xmlStrdup(node->content);
  3931. }else{
  3932. if (node->type == XML_ELEMENT_NODE)
  3933. ret->line = node->line;
  3934. }
  3935. if (parent != NULL) {
  3936. xmlNodePtr tmp;
  3937. /*
  3938. * this is a tricky part for the node register thing:
  3939. * in case ret does get coalesced in xmlAddChild
  3940. * the deregister-node callback is called; so we register ret now already
  3941. */
  3942. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  3943. xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
  3944. /*
  3945. * Note that since ret->parent is already set, xmlAddChild will
  3946. * return early and not actually insert the node. It will only
  3947. * coalesce text nodes and unnecessarily call xmlSetTreeDoc.
  3948. * Assuming that the subtree to be copied always has its text
  3949. * nodes coalesced, the somewhat confusing call to xmlAddChild
  3950. * could be removed.
  3951. */
  3952. tmp = xmlAddChild(parent, ret);
  3953. /* node could have coalesced */
  3954. if (tmp != ret)
  3955. return(tmp);
  3956. }
  3957. if (!extended)
  3958. goto out;
  3959. if (((node->type == XML_ELEMENT_NODE) ||
  3960. (node->type == XML_XINCLUDE_START)) && (node->nsDef != NULL))
  3961. ret->nsDef = xmlCopyNamespaceList(node->nsDef);
  3962. if (node->ns != NULL) {
  3963. xmlNsPtr ns;
  3964. ns = xmlSearchNs(doc, ret, node->ns->prefix);
  3965. if (ns == NULL) {
  3966. /*
  3967. * Humm, we are copying an element whose namespace is defined
  3968. * out of the new tree scope. Search it in the original tree
  3969. * and add it at the top of the new tree
  3970. */
  3971. ns = xmlSearchNs(node->doc, node, node->ns->prefix);
  3972. if (ns != NULL) {
  3973. xmlNodePtr root = ret;
  3974. while (root->parent != NULL) root = root->parent;
  3975. ret->ns = xmlNewNs(root, ns->href, ns->prefix);
  3976. } else {
  3977. ret->ns = xmlNewReconciledNs(doc, ret, node->ns);
  3978. }
  3979. } else {
  3980. /*
  3981. * reference the existing namespace definition in our own tree.
  3982. */
  3983. ret->ns = ns;
  3984. }
  3985. }
  3986. if (((node->type == XML_ELEMENT_NODE) ||
  3987. (node->type == XML_XINCLUDE_START)) && (node->properties != NULL))
  3988. ret->properties = xmlCopyPropList(ret, node->properties);
  3989. if (node->type == XML_ENTITY_REF_NODE) {
  3990. if ((doc == NULL) || (node->doc != doc)) {
  3991. /*
  3992. * The copied node will go into a separate document, so
  3993. * to avoid dangling references to the ENTITY_DECL node
  3994. * we cannot keep the reference. Try to find it in the
  3995. * target document.
  3996. */
  3997. ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
  3998. } else {
  3999. ret->children = node->children;
  4000. }
  4001. ret->last = ret->children;
  4002. } else if ((node->children != NULL) && (extended != 2)) {
  4003. xmlNodePtr cur, insert;
  4004. cur = node->children;
  4005. insert = ret;
  4006. while (cur != NULL) {
  4007. xmlNodePtr copy = xmlStaticCopyNode(cur, doc, insert, 2);
  4008. if (copy == NULL) {
  4009. xmlFreeNode(ret);
  4010. return(NULL);
  4011. }
  4012. /* Check for coalesced text nodes */
  4013. if (insert->last != copy) {
  4014. if (insert->last == NULL) {
  4015. insert->children = copy;
  4016. } else {
  4017. copy->prev = insert->last;
  4018. insert->last->next = copy;
  4019. }
  4020. insert->last = copy;
  4021. }
  4022. if ((cur->type != XML_ENTITY_REF_NODE) &&
  4023. (cur->children != NULL)) {
  4024. cur = cur->children;
  4025. insert = copy;
  4026. continue;
  4027. }
  4028. while (1) {
  4029. if (cur->next != NULL) {
  4030. cur = cur->next;
  4031. break;
  4032. }
  4033. cur = cur->parent;
  4034. insert = insert->parent;
  4035. if (cur == node) {
  4036. cur = NULL;
  4037. break;
  4038. }
  4039. }
  4040. }
  4041. }
  4042. out:
  4043. /* if parent != NULL we already registered the node above */
  4044. if ((parent == NULL) &&
  4045. ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)))
  4046. xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
  4047. return(ret);
  4048. }
  4049. static xmlNodePtr
  4050. xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
  4051. xmlNodePtr ret = NULL;
  4052. xmlNodePtr p = NULL,q;
  4053. while (node != NULL) {
  4054. #ifdef LIBXML_TREE_ENABLED
  4055. if (node->type == XML_DTD_NODE ) {
  4056. if (doc == NULL) {
  4057. node = node->next;
  4058. continue;
  4059. }
  4060. if (doc->intSubset == NULL) {
  4061. q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
  4062. if (q == NULL) return(NULL);
  4063. q->doc = doc;
  4064. q->parent = parent;
  4065. doc->intSubset = (xmlDtdPtr) q;
  4066. xmlAddChild(parent, q);
  4067. } else {
  4068. q = (xmlNodePtr) doc->intSubset;
  4069. xmlAddChild(parent, q);
  4070. }
  4071. } else
  4072. #endif /* LIBXML_TREE_ENABLED */
  4073. q = xmlStaticCopyNode(node, doc, parent, 1);
  4074. if (q == NULL) return(NULL);
  4075. if (ret == NULL) {
  4076. q->prev = NULL;
  4077. ret = p = q;
  4078. } else if (p != q) {
  4079. /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
  4080. p->next = q;
  4081. q->prev = p;
  4082. p = q;
  4083. }
  4084. node = node->next;
  4085. }
  4086. return(ret);
  4087. }
  4088. /**
  4089. * xmlCopyNode:
  4090. * @node: the node
  4091. * @extended: if 1 do a recursive copy (properties, namespaces and children
  4092. * when applicable)
  4093. * if 2 copy properties and namespaces (when applicable)
  4094. *
  4095. * Do a copy of the node.
  4096. *
  4097. * Returns: a new #xmlNodePtr, or NULL in case of error.
  4098. */
  4099. xmlNodePtr
  4100. xmlCopyNode(xmlNodePtr node, int extended) {
  4101. xmlNodePtr ret;
  4102. ret = xmlStaticCopyNode(node, NULL, NULL, extended);
  4103. return(ret);
  4104. }
  4105. /**
  4106. * xmlDocCopyNode:
  4107. * @node: the node
  4108. * @doc: the document
  4109. * @extended: if 1 do a recursive copy (properties, namespaces and children
  4110. * when applicable)
  4111. * if 2 copy properties and namespaces (when applicable)
  4112. *
  4113. * Do a copy of the node to a given document.
  4114. *
  4115. * Returns: a new #xmlNodePtr, or NULL in case of error.
  4116. */
  4117. xmlNodePtr
  4118. xmlDocCopyNode(xmlNodePtr node, xmlDocPtr doc, int extended) {
  4119. xmlNodePtr ret;
  4120. ret = xmlStaticCopyNode(node, doc, NULL, extended);
  4121. return(ret);
  4122. }
  4123. /**
  4124. * xmlDocCopyNodeList:
  4125. * @doc: the target document
  4126. * @node: the first node in the list.
  4127. *
  4128. * Do a recursive copy of the node list.
  4129. *
  4130. * Returns: a new #xmlNodePtr, or NULL in case of error.
  4131. */
  4132. xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, xmlNodePtr node) {
  4133. xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL);
  4134. return(ret);
  4135. }
  4136. /**
  4137. * xmlCopyNodeList:
  4138. * @node: the first node in the list.
  4139. *
  4140. * Do a recursive copy of the node list.
  4141. * Use xmlDocCopyNodeList() if possible to ensure string interning.
  4142. *
  4143. * Returns: a new #xmlNodePtr, or NULL in case of error.
  4144. */
  4145. xmlNodePtr xmlCopyNodeList(xmlNodePtr node) {
  4146. xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
  4147. return(ret);
  4148. }
  4149. #if defined(LIBXML_TREE_ENABLED)
  4150. /**
  4151. * xmlCopyDtd:
  4152. * @dtd: the dtd
  4153. *
  4154. * Do a copy of the dtd.
  4155. *
  4156. * Returns: a new #xmlDtdPtr, or NULL in case of error.
  4157. */
  4158. xmlDtdPtr
  4159. xmlCopyDtd(xmlDtdPtr dtd) {
  4160. xmlDtdPtr ret;
  4161. xmlNodePtr cur, p = NULL, q;
  4162. if (dtd == NULL) return(NULL);
  4163. ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
  4164. if (ret == NULL) return(NULL);
  4165. if (dtd->entities != NULL)
  4166. ret->entities = (void *) xmlCopyEntitiesTable(
  4167. (xmlEntitiesTablePtr) dtd->entities);
  4168. if (dtd->notations != NULL)
  4169. ret->notations = (void *) xmlCopyNotationTable(
  4170. (xmlNotationTablePtr) dtd->notations);
  4171. if (dtd->elements != NULL)
  4172. ret->elements = (void *) xmlCopyElementTable(
  4173. (xmlElementTablePtr) dtd->elements);
  4174. if (dtd->attributes != NULL)
  4175. ret->attributes = (void *) xmlCopyAttributeTable(
  4176. (xmlAttributeTablePtr) dtd->attributes);
  4177. if (dtd->pentities != NULL)
  4178. ret->pentities = (void *) xmlCopyEntitiesTable(
  4179. (xmlEntitiesTablePtr) dtd->pentities);
  4180. cur = dtd->children;
  4181. while (cur != NULL) {
  4182. q = NULL;
  4183. if (cur->type == XML_ENTITY_DECL) {
  4184. xmlEntityPtr tmp = (xmlEntityPtr) cur;
  4185. switch (tmp->etype) {
  4186. case XML_INTERNAL_GENERAL_ENTITY:
  4187. case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
  4188. case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
  4189. q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
  4190. break;
  4191. case XML_INTERNAL_PARAMETER_ENTITY:
  4192. case XML_EXTERNAL_PARAMETER_ENTITY:
  4193. q = (xmlNodePtr)
  4194. xmlGetParameterEntityFromDtd(ret, tmp->name);
  4195. break;
  4196. case XML_INTERNAL_PREDEFINED_ENTITY:
  4197. break;
  4198. }
  4199. } else if (cur->type == XML_ELEMENT_DECL) {
  4200. xmlElementPtr tmp = (xmlElementPtr) cur;
  4201. q = (xmlNodePtr)
  4202. xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
  4203. } else if (cur->type == XML_ATTRIBUTE_DECL) {
  4204. xmlAttributePtr tmp = (xmlAttributePtr) cur;
  4205. q = (xmlNodePtr)
  4206. xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
  4207. } else if (cur->type == XML_COMMENT_NODE) {
  4208. q = xmlCopyNode(cur, 0);
  4209. }
  4210. if (q == NULL) {
  4211. cur = cur->next;
  4212. continue;
  4213. }
  4214. if (p == NULL)
  4215. ret->children = q;
  4216. else
  4217. p->next = q;
  4218. q->prev = p;
  4219. q->parent = (xmlNodePtr) ret;
  4220. q->next = NULL;
  4221. ret->last = q;
  4222. p = q;
  4223. cur = cur->next;
  4224. }
  4225. return(ret);
  4226. }
  4227. #endif
  4228. #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
  4229. /**
  4230. * xmlCopyDoc:
  4231. * @doc: the document
  4232. * @recursive: if not zero do a recursive copy.
  4233. *
  4234. * Do a copy of the document info. If recursive, the content tree will
  4235. * be copied too as well as DTD, namespaces and entities.
  4236. *
  4237. * Returns: a new #xmlDocPtr, or NULL in case of error.
  4238. */
  4239. xmlDocPtr
  4240. xmlCopyDoc(xmlDocPtr doc, int recursive) {
  4241. xmlDocPtr ret;
  4242. if (doc == NULL) return(NULL);
  4243. ret = xmlNewDoc(doc->version);
  4244. if (ret == NULL) return(NULL);
  4245. ret->type = doc->type;
  4246. if (doc->name != NULL)
  4247. ret->name = xmlMemStrdup(doc->name);
  4248. if (doc->encoding != NULL)
  4249. ret->encoding = xmlStrdup(doc->encoding);
  4250. if (doc->URL != NULL)
  4251. ret->URL = xmlStrdup(doc->URL);
  4252. ret->charset = doc->charset;
  4253. ret->compression = doc->compression;
  4254. ret->standalone = doc->standalone;
  4255. if (!recursive) return(ret);
  4256. ret->last = NULL;
  4257. ret->children = NULL;
  4258. #ifdef LIBXML_TREE_ENABLED
  4259. if (doc->intSubset != NULL) {
  4260. ret->intSubset = xmlCopyDtd(doc->intSubset);
  4261. if (ret->intSubset == NULL) {
  4262. xmlFreeDoc(ret);
  4263. return(NULL);
  4264. }
  4265. xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
  4266. ret->intSubset->parent = ret;
  4267. }
  4268. #endif
  4269. if (doc->oldNs != NULL)
  4270. ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
  4271. if (doc->children != NULL) {
  4272. xmlNodePtr tmp;
  4273. ret->children = xmlStaticCopyNodeList(doc->children, ret,
  4274. (xmlNodePtr)ret);
  4275. ret->last = NULL;
  4276. tmp = ret->children;
  4277. while (tmp != NULL) {
  4278. if (tmp->next == NULL)
  4279. ret->last = tmp;
  4280. tmp = tmp->next;
  4281. }
  4282. }
  4283. return(ret);
  4284. }
  4285. #endif /* LIBXML_TREE_ENABLED */
  4286. /************************************************************************
  4287. * *
  4288. * Content access functions *
  4289. * *
  4290. ************************************************************************/
  4291. /**
  4292. * xmlGetLineNoInternal:
  4293. * @node: valid node
  4294. * @depth: used to limit any risk of recursion
  4295. *
  4296. * Get line number of @node.
  4297. * Try to override the limitation of lines being store in 16 bits ints
  4298. *
  4299. * Returns the line number if successful, -1 otherwise
  4300. */
  4301. static long
  4302. xmlGetLineNoInternal(const xmlNode *node, int depth)
  4303. {
  4304. long result = -1;
  4305. if (depth >= 5)
  4306. return(-1);
  4307. if (!node)
  4308. return result;
  4309. if ((node->type == XML_ELEMENT_NODE) ||
  4310. (node->type == XML_TEXT_NODE) ||
  4311. (node->type == XML_COMMENT_NODE) ||
  4312. (node->type == XML_PI_NODE)) {
  4313. if (node->line == 65535) {
  4314. if ((node->type == XML_TEXT_NODE) && (node->psvi != NULL))
  4315. result = (long) (ptrdiff_t) node->psvi;
  4316. else if ((node->type == XML_ELEMENT_NODE) &&
  4317. (node->children != NULL))
  4318. result = xmlGetLineNoInternal(node->children, depth + 1);
  4319. else if (node->next != NULL)
  4320. result = xmlGetLineNoInternal(node->next, depth + 1);
  4321. else if (node->prev != NULL)
  4322. result = xmlGetLineNoInternal(node->prev, depth + 1);
  4323. }
  4324. if ((result == -1) || (result == 65535))
  4325. result = (long) node->line;
  4326. } else if ((node->prev != NULL) &&
  4327. ((node->prev->type == XML_ELEMENT_NODE) ||
  4328. (node->prev->type == XML_TEXT_NODE) ||
  4329. (node->prev->type == XML_COMMENT_NODE) ||
  4330. (node->prev->type == XML_PI_NODE)))
  4331. result = xmlGetLineNoInternal(node->prev, depth + 1);
  4332. else if ((node->parent != NULL) &&
  4333. (node->parent->type == XML_ELEMENT_NODE))
  4334. result = xmlGetLineNoInternal(node->parent, depth + 1);
  4335. return result;
  4336. }
  4337. /**
  4338. * xmlGetLineNo:
  4339. * @node: valid node
  4340. *
  4341. * Get line number of @node.
  4342. * Try to override the limitation of lines being store in 16 bits ints
  4343. * if XML_PARSE_BIG_LINES parser option was used
  4344. *
  4345. * Returns the line number if successful, -1 otherwise
  4346. */
  4347. long
  4348. xmlGetLineNo(const xmlNode *node)
  4349. {
  4350. return(xmlGetLineNoInternal(node, 0));
  4351. }
  4352. #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
  4353. /**
  4354. * xmlGetNodePath:
  4355. * @node: a node
  4356. *
  4357. * Build a structure based Path for the given node
  4358. *
  4359. * Returns the new path or NULL in case of error. The caller must free
  4360. * the returned string
  4361. */
  4362. xmlChar *
  4363. xmlGetNodePath(const xmlNode *node)
  4364. {
  4365. const xmlNode *cur, *tmp, *next;
  4366. xmlChar *buffer = NULL, *temp;
  4367. size_t buf_len;
  4368. xmlChar *buf;
  4369. const char *sep;
  4370. const char *name;
  4371. char nametemp[100];
  4372. int occur = 0, generic;
  4373. if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
  4374. return (NULL);
  4375. buf_len = 500;
  4376. buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
  4377. if (buffer == NULL) {
  4378. xmlTreeErrMemory("getting node path");
  4379. return (NULL);
  4380. }
  4381. buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
  4382. if (buf == NULL) {
  4383. xmlTreeErrMemory("getting node path");
  4384. xmlFree(buffer);
  4385. return (NULL);
  4386. }
  4387. buffer[0] = 0;
  4388. cur = node;
  4389. do {
  4390. name = "";
  4391. sep = "?";
  4392. occur = 0;
  4393. if ((cur->type == XML_DOCUMENT_NODE) ||
  4394. (cur->type == XML_HTML_DOCUMENT_NODE)) {
  4395. if (buffer[0] == '/')
  4396. break;
  4397. sep = "/";
  4398. next = NULL;
  4399. } else if (cur->type == XML_ELEMENT_NODE) {
  4400. generic = 0;
  4401. sep = "/";
  4402. name = (const char *) cur->name;
  4403. if (cur->ns) {
  4404. if (cur->ns->prefix != NULL) {
  4405. snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
  4406. (char *)cur->ns->prefix, (char *)cur->name);
  4407. nametemp[sizeof(nametemp) - 1] = 0;
  4408. name = nametemp;
  4409. } else {
  4410. /*
  4411. * We cannot express named elements in the default
  4412. * namespace, so use "*".
  4413. */
  4414. generic = 1;
  4415. name = "*";
  4416. }
  4417. }
  4418. next = cur->parent;
  4419. /*
  4420. * Thumbler index computation
  4421. * TODO: the occurrence test seems bogus for namespaced names
  4422. */
  4423. tmp = cur->prev;
  4424. while (tmp != NULL) {
  4425. if ((tmp->type == XML_ELEMENT_NODE) &&
  4426. (generic ||
  4427. (xmlStrEqual(cur->name, tmp->name) &&
  4428. ((tmp->ns == cur->ns) ||
  4429. ((tmp->ns != NULL) && (cur->ns != NULL) &&
  4430. (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
  4431. occur++;
  4432. tmp = tmp->prev;
  4433. }
  4434. if (occur == 0) {
  4435. tmp = cur->next;
  4436. while (tmp != NULL && occur == 0) {
  4437. if ((tmp->type == XML_ELEMENT_NODE) &&
  4438. (generic ||
  4439. (xmlStrEqual(cur->name, tmp->name) &&
  4440. ((tmp->ns == cur->ns) ||
  4441. ((tmp->ns != NULL) && (cur->ns != NULL) &&
  4442. (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
  4443. occur++;
  4444. tmp = tmp->next;
  4445. }
  4446. if (occur != 0)
  4447. occur = 1;
  4448. } else
  4449. occur++;
  4450. } else if (cur->type == XML_COMMENT_NODE) {
  4451. sep = "/";
  4452. name = "comment()";
  4453. next = cur->parent;
  4454. /*
  4455. * Thumbler index computation
  4456. */
  4457. tmp = cur->prev;
  4458. while (tmp != NULL) {
  4459. if (tmp->type == XML_COMMENT_NODE)
  4460. occur++;
  4461. tmp = tmp->prev;
  4462. }
  4463. if (occur == 0) {
  4464. tmp = cur->next;
  4465. while (tmp != NULL && occur == 0) {
  4466. if (tmp->type == XML_COMMENT_NODE)
  4467. occur++;
  4468. tmp = tmp->next;
  4469. }
  4470. if (occur != 0)
  4471. occur = 1;
  4472. } else
  4473. occur++;
  4474. } else if ((cur->type == XML_TEXT_NODE) ||
  4475. (cur->type == XML_CDATA_SECTION_NODE)) {
  4476. sep = "/";
  4477. name = "text()";
  4478. next = cur->parent;
  4479. /*
  4480. * Thumbler index computation
  4481. */
  4482. tmp = cur->prev;
  4483. while (tmp != NULL) {
  4484. if ((tmp->type == XML_TEXT_NODE) ||
  4485. (tmp->type == XML_CDATA_SECTION_NODE))
  4486. occur++;
  4487. tmp = tmp->prev;
  4488. }
  4489. /*
  4490. * Evaluate if this is the only text- or CDATA-section-node;
  4491. * if yes, then we'll get "text()", otherwise "text()[1]".
  4492. */
  4493. if (occur == 0) {
  4494. tmp = cur->next;
  4495. while (tmp != NULL) {
  4496. if ((tmp->type == XML_TEXT_NODE) ||
  4497. (tmp->type == XML_CDATA_SECTION_NODE))
  4498. {
  4499. occur = 1;
  4500. break;
  4501. }
  4502. tmp = tmp->next;
  4503. }
  4504. } else
  4505. occur++;
  4506. } else if (cur->type == XML_PI_NODE) {
  4507. sep = "/";
  4508. snprintf(nametemp, sizeof(nametemp) - 1,
  4509. "processing-instruction('%s')", (char *)cur->name);
  4510. nametemp[sizeof(nametemp) - 1] = 0;
  4511. name = nametemp;
  4512. next = cur->parent;
  4513. /*
  4514. * Thumbler index computation
  4515. */
  4516. tmp = cur->prev;
  4517. while (tmp != NULL) {
  4518. if ((tmp->type == XML_PI_NODE) &&
  4519. (xmlStrEqual(cur->name, tmp->name)))
  4520. occur++;
  4521. tmp = tmp->prev;
  4522. }
  4523. if (occur == 0) {
  4524. tmp = cur->next;
  4525. while (tmp != NULL && occur == 0) {
  4526. if ((tmp->type == XML_PI_NODE) &&
  4527. (xmlStrEqual(cur->name, tmp->name)))
  4528. occur++;
  4529. tmp = tmp->next;
  4530. }
  4531. if (occur != 0)
  4532. occur = 1;
  4533. } else
  4534. occur++;
  4535. } else if (cur->type == XML_ATTRIBUTE_NODE) {
  4536. sep = "/@";
  4537. name = (const char *) (((xmlAttrPtr) cur)->name);
  4538. if (cur->ns) {
  4539. if (cur->ns->prefix != NULL)
  4540. snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
  4541. (char *)cur->ns->prefix, (char *)cur->name);
  4542. else
  4543. snprintf(nametemp, sizeof(nametemp) - 1, "%s",
  4544. (char *)cur->name);
  4545. nametemp[sizeof(nametemp) - 1] = 0;
  4546. name = nametemp;
  4547. }
  4548. next = ((xmlAttrPtr) cur)->parent;
  4549. } else {
  4550. xmlFree(buf);
  4551. xmlFree(buffer);
  4552. return (NULL);
  4553. }
  4554. /*
  4555. * Make sure there is enough room
  4556. */
  4557. if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
  4558. buf_len =
  4559. 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
  4560. temp = (xmlChar *) xmlRealloc(buffer, buf_len);
  4561. if (temp == NULL) {
  4562. xmlTreeErrMemory("getting node path");
  4563. xmlFree(buf);
  4564. xmlFree(buffer);
  4565. return (NULL);
  4566. }
  4567. buffer = temp;
  4568. temp = (xmlChar *) xmlRealloc(buf, buf_len);
  4569. if (temp == NULL) {
  4570. xmlTreeErrMemory("getting node path");
  4571. xmlFree(buf);
  4572. xmlFree(buffer);
  4573. return (NULL);
  4574. }
  4575. buf = temp;
  4576. }
  4577. if (occur == 0)
  4578. snprintf((char *) buf, buf_len, "%s%s%s",
  4579. sep, name, (char *) buffer);
  4580. else
  4581. snprintf((char *) buf, buf_len, "%s%s[%d]%s",
  4582. sep, name, occur, (char *) buffer);
  4583. snprintf((char *) buffer, buf_len, "%s", (char *)buf);
  4584. cur = next;
  4585. } while (cur != NULL);
  4586. xmlFree(buf);
  4587. return (buffer);
  4588. }
  4589. #endif /* LIBXML_TREE_ENABLED */
  4590. /**
  4591. * xmlDocGetRootElement:
  4592. * @doc: the document
  4593. *
  4594. * Get the root element of the document (doc->children is a list
  4595. * containing possibly comments, PIs, etc ...).
  4596. *
  4597. * Returns the #xmlNodePtr for the root or NULL
  4598. */
  4599. xmlNodePtr
  4600. xmlDocGetRootElement(const xmlDoc *doc) {
  4601. xmlNodePtr ret;
  4602. if (doc == NULL) return(NULL);
  4603. ret = doc->children;
  4604. while (ret != NULL) {
  4605. if (ret->type == XML_ELEMENT_NODE)
  4606. return(ret);
  4607. ret = ret->next;
  4608. }
  4609. return(ret);
  4610. }
  4611. #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
  4612. /**
  4613. * xmlDocSetRootElement:
  4614. * @doc: the document
  4615. * @root: the new document root element, if root is NULL no action is taken,
  4616. * to remove a node from a document use xmlUnlinkNode(root) instead.
  4617. *
  4618. * Set the root element of the document (doc->children is a list
  4619. * containing possibly comments, PIs, etc ...).
  4620. *
  4621. * Returns the old root element if any was found, NULL if root was NULL
  4622. */
  4623. xmlNodePtr
  4624. xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
  4625. xmlNodePtr old = NULL;
  4626. if (doc == NULL) return(NULL);
  4627. if ((root == NULL) || (root->type == XML_NAMESPACE_DECL))
  4628. return(NULL);
  4629. xmlUnlinkNode(root);
  4630. xmlSetTreeDoc(root, doc);
  4631. root->parent = (xmlNodePtr) doc;
  4632. old = doc->children;
  4633. while (old != NULL) {
  4634. if (old->type == XML_ELEMENT_NODE)
  4635. break;
  4636. old = old->next;
  4637. }
  4638. if (old == NULL) {
  4639. if (doc->children == NULL) {
  4640. doc->children = root;
  4641. doc->last = root;
  4642. } else {
  4643. xmlAddSibling(doc->children, root);
  4644. }
  4645. } else {
  4646. xmlReplaceNode(old, root);
  4647. }
  4648. return(old);
  4649. }
  4650. #endif
  4651. #if defined(LIBXML_TREE_ENABLED)
  4652. /**
  4653. * xmlNodeSetLang:
  4654. * @cur: the node being changed
  4655. * @lang: the language description
  4656. *
  4657. * Set the language of a node, i.e. the values of the xml:lang
  4658. * attribute.
  4659. */
  4660. void
  4661. xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
  4662. xmlNsPtr ns;
  4663. if (cur == NULL) return;
  4664. switch(cur->type) {
  4665. case XML_TEXT_NODE:
  4666. case XML_CDATA_SECTION_NODE:
  4667. case XML_COMMENT_NODE:
  4668. case XML_DOCUMENT_NODE:
  4669. case XML_DOCUMENT_TYPE_NODE:
  4670. case XML_DOCUMENT_FRAG_NODE:
  4671. case XML_NOTATION_NODE:
  4672. case XML_HTML_DOCUMENT_NODE:
  4673. case XML_DTD_NODE:
  4674. case XML_ELEMENT_DECL:
  4675. case XML_ATTRIBUTE_DECL:
  4676. case XML_ENTITY_DECL:
  4677. case XML_PI_NODE:
  4678. case XML_ENTITY_REF_NODE:
  4679. case XML_ENTITY_NODE:
  4680. case XML_NAMESPACE_DECL:
  4681. case XML_XINCLUDE_START:
  4682. case XML_XINCLUDE_END:
  4683. return;
  4684. case XML_ELEMENT_NODE:
  4685. case XML_ATTRIBUTE_NODE:
  4686. break;
  4687. }
  4688. ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
  4689. if (ns == NULL)
  4690. return;
  4691. xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
  4692. }
  4693. #endif /* LIBXML_TREE_ENABLED */
  4694. /**
  4695. * xmlNodeGetLang:
  4696. * @cur: the node being checked
  4697. *
  4698. * Searches the language of a node, i.e. the values of the xml:lang
  4699. * attribute or the one carried by the nearest ancestor.
  4700. *
  4701. * Returns a pointer to the lang value, or NULL if not found
  4702. * It's up to the caller to free the memory with xmlFree().
  4703. */
  4704. xmlChar *
  4705. xmlNodeGetLang(const xmlNode *cur) {
  4706. xmlChar *lang;
  4707. if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
  4708. return(NULL);
  4709. while (cur != NULL) {
  4710. lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
  4711. if (lang != NULL)
  4712. return(lang);
  4713. cur = cur->parent;
  4714. }
  4715. return(NULL);
  4716. }
  4717. #ifdef LIBXML_TREE_ENABLED
  4718. /**
  4719. * xmlNodeSetSpacePreserve:
  4720. * @cur: the node being changed
  4721. * @val: the xml:space value ("0": default, 1: "preserve")
  4722. *
  4723. * Set (or reset) the space preserving behaviour of a node, i.e. the
  4724. * value of the xml:space attribute.
  4725. */
  4726. void
  4727. xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
  4728. xmlNsPtr ns;
  4729. if (cur == NULL) return;
  4730. switch(cur->type) {
  4731. case XML_TEXT_NODE:
  4732. case XML_CDATA_SECTION_NODE:
  4733. case XML_COMMENT_NODE:
  4734. case XML_DOCUMENT_NODE:
  4735. case XML_DOCUMENT_TYPE_NODE:
  4736. case XML_DOCUMENT_FRAG_NODE:
  4737. case XML_NOTATION_NODE:
  4738. case XML_HTML_DOCUMENT_NODE:
  4739. case XML_DTD_NODE:
  4740. case XML_ELEMENT_DECL:
  4741. case XML_ATTRIBUTE_DECL:
  4742. case XML_ENTITY_DECL:
  4743. case XML_PI_NODE:
  4744. case XML_ENTITY_REF_NODE:
  4745. case XML_ENTITY_NODE:
  4746. case XML_NAMESPACE_DECL:
  4747. case XML_XINCLUDE_START:
  4748. case XML_XINCLUDE_END:
  4749. return;
  4750. case XML_ELEMENT_NODE:
  4751. case XML_ATTRIBUTE_NODE:
  4752. break;
  4753. }
  4754. ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
  4755. if (ns == NULL)
  4756. return;
  4757. switch (val) {
  4758. case 0:
  4759. xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
  4760. break;
  4761. case 1:
  4762. xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
  4763. break;
  4764. }
  4765. }
  4766. #endif /* LIBXML_TREE_ENABLED */
  4767. /**
  4768. * xmlNodeGetSpacePreserve:
  4769. * @cur: the node being checked
  4770. *
  4771. * Searches the space preserving behaviour of a node, i.e. the values
  4772. * of the xml:space attribute or the one carried by the nearest
  4773. * ancestor.
  4774. *
  4775. * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
  4776. */
  4777. int
  4778. xmlNodeGetSpacePreserve(const xmlNode *cur) {
  4779. xmlChar *space;
  4780. if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
  4781. return(-1);
  4782. while (cur != NULL) {
  4783. space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
  4784. if (space != NULL) {
  4785. if (xmlStrEqual(space, BAD_CAST "preserve")) {
  4786. xmlFree(space);
  4787. return(1);
  4788. }
  4789. if (xmlStrEqual(space, BAD_CAST "default")) {
  4790. xmlFree(space);
  4791. return(0);
  4792. }
  4793. xmlFree(space);
  4794. }
  4795. cur = cur->parent;
  4796. }
  4797. return(-1);
  4798. }
  4799. #ifdef LIBXML_TREE_ENABLED
  4800. /**
  4801. * xmlNodeSetName:
  4802. * @cur: the node being changed
  4803. * @name: the new tag name
  4804. *
  4805. * Set (or reset) the name of a node.
  4806. */
  4807. void
  4808. xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
  4809. xmlDocPtr doc;
  4810. xmlDictPtr dict;
  4811. const xmlChar *freeme = NULL;
  4812. if (cur == NULL) return;
  4813. if (name == NULL) return;
  4814. switch(cur->type) {
  4815. case XML_TEXT_NODE:
  4816. case XML_CDATA_SECTION_NODE:
  4817. case XML_COMMENT_NODE:
  4818. case XML_DOCUMENT_TYPE_NODE:
  4819. case XML_DOCUMENT_FRAG_NODE:
  4820. case XML_NOTATION_NODE:
  4821. case XML_HTML_DOCUMENT_NODE:
  4822. case XML_NAMESPACE_DECL:
  4823. case XML_XINCLUDE_START:
  4824. case XML_XINCLUDE_END:
  4825. return;
  4826. case XML_ELEMENT_NODE:
  4827. case XML_ATTRIBUTE_NODE:
  4828. case XML_PI_NODE:
  4829. case XML_ENTITY_REF_NODE:
  4830. case XML_ENTITY_NODE:
  4831. case XML_DTD_NODE:
  4832. case XML_DOCUMENT_NODE:
  4833. case XML_ELEMENT_DECL:
  4834. case XML_ATTRIBUTE_DECL:
  4835. case XML_ENTITY_DECL:
  4836. break;
  4837. }
  4838. doc = cur->doc;
  4839. if (doc != NULL)
  4840. dict = doc->dict;
  4841. else
  4842. dict = NULL;
  4843. if (dict != NULL) {
  4844. if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
  4845. freeme = cur->name;
  4846. cur->name = xmlDictLookup(dict, name, -1);
  4847. } else {
  4848. if (cur->name != NULL)
  4849. freeme = cur->name;
  4850. cur->name = xmlStrdup(name);
  4851. }
  4852. if (freeme)
  4853. xmlFree((xmlChar *) freeme);
  4854. }
  4855. #endif
  4856. #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
  4857. /**
  4858. * xmlNodeSetBase:
  4859. * @cur: the node being changed
  4860. * @uri: the new base URI
  4861. *
  4862. * Set (or reset) the base URI of a node, i.e. the value of the
  4863. * xml:base attribute.
  4864. */
  4865. void
  4866. xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
  4867. xmlNsPtr ns;
  4868. xmlChar* fixed;
  4869. if (cur == NULL) return;
  4870. switch(cur->type) {
  4871. case XML_TEXT_NODE:
  4872. case XML_CDATA_SECTION_NODE:
  4873. case XML_COMMENT_NODE:
  4874. case XML_DOCUMENT_TYPE_NODE:
  4875. case XML_DOCUMENT_FRAG_NODE:
  4876. case XML_NOTATION_NODE:
  4877. case XML_DTD_NODE:
  4878. case XML_ELEMENT_DECL:
  4879. case XML_ATTRIBUTE_DECL:
  4880. case XML_ENTITY_DECL:
  4881. case XML_PI_NODE:
  4882. case XML_ENTITY_REF_NODE:
  4883. case XML_ENTITY_NODE:
  4884. case XML_NAMESPACE_DECL:
  4885. case XML_XINCLUDE_START:
  4886. case XML_XINCLUDE_END:
  4887. return;
  4888. case XML_ELEMENT_NODE:
  4889. case XML_ATTRIBUTE_NODE:
  4890. break;
  4891. case XML_DOCUMENT_NODE:
  4892. case XML_HTML_DOCUMENT_NODE: {
  4893. xmlDocPtr doc = (xmlDocPtr) cur;
  4894. if (doc->URL != NULL)
  4895. xmlFree((xmlChar *) doc->URL);
  4896. if (uri == NULL)
  4897. doc->URL = NULL;
  4898. else
  4899. doc->URL = xmlPathToURI(uri);
  4900. return;
  4901. }
  4902. }
  4903. ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
  4904. if (ns == NULL)
  4905. return;
  4906. fixed = xmlPathToURI(uri);
  4907. if (fixed != NULL) {
  4908. xmlSetNsProp(cur, ns, BAD_CAST "base", fixed);
  4909. xmlFree(fixed);
  4910. } else {
  4911. xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
  4912. }
  4913. }
  4914. #endif /* LIBXML_TREE_ENABLED */
  4915. /**
  4916. * xmlNodeGetBase:
  4917. * @doc: the document the node pertains to
  4918. * @cur: the node being checked
  4919. *
  4920. * Searches for the BASE URL. The code should work on both XML
  4921. * and HTML document even if base mechanisms are completely different.
  4922. * It returns the base as defined in RFC 2396 sections
  4923. * 5.1.1. Base URI within Document Content
  4924. * and
  4925. * 5.1.2. Base URI from the Encapsulating Entity
  4926. * However it does not return the document base (5.1.3), use
  4927. * doc->URL in this case
  4928. *
  4929. * Returns a pointer to the base URL, or NULL if not found
  4930. * It's up to the caller to free the memory with xmlFree().
  4931. */
  4932. xmlChar *
  4933. xmlNodeGetBase(const xmlDoc *doc, const xmlNode *cur) {
  4934. xmlChar *oldbase = NULL;
  4935. xmlChar *base, *newbase;
  4936. if ((cur == NULL) && (doc == NULL))
  4937. return(NULL);
  4938. if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
  4939. return(NULL);
  4940. if (doc == NULL) doc = cur->doc;
  4941. if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
  4942. cur = doc->children;
  4943. while ((cur != NULL) && (cur->name != NULL)) {
  4944. if (cur->type != XML_ELEMENT_NODE) {
  4945. cur = cur->next;
  4946. continue;
  4947. }
  4948. if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
  4949. cur = cur->children;
  4950. continue;
  4951. }
  4952. if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
  4953. cur = cur->children;
  4954. continue;
  4955. }
  4956. if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
  4957. return(xmlGetProp(cur, BAD_CAST "href"));
  4958. }
  4959. cur = cur->next;
  4960. }
  4961. return(NULL);
  4962. }
  4963. while (cur != NULL) {
  4964. if (cur->type == XML_ENTITY_DECL) {
  4965. xmlEntityPtr ent = (xmlEntityPtr) cur;
  4966. return(xmlStrdup(ent->URI));
  4967. }
  4968. if (cur->type == XML_ELEMENT_NODE) {
  4969. base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
  4970. if (base != NULL) {
  4971. if (oldbase != NULL) {
  4972. newbase = xmlBuildURI(oldbase, base);
  4973. if (newbase != NULL) {
  4974. xmlFree(oldbase);
  4975. xmlFree(base);
  4976. oldbase = newbase;
  4977. } else {
  4978. xmlFree(oldbase);
  4979. xmlFree(base);
  4980. return(NULL);
  4981. }
  4982. } else {
  4983. oldbase = base;
  4984. }
  4985. if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
  4986. (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
  4987. (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
  4988. return(oldbase);
  4989. }
  4990. }
  4991. cur = cur->parent;
  4992. }
  4993. if ((doc != NULL) && (doc->URL != NULL)) {
  4994. if (oldbase == NULL)
  4995. return(xmlStrdup(doc->URL));
  4996. newbase = xmlBuildURI(oldbase, doc->URL);
  4997. xmlFree(oldbase);
  4998. return(newbase);
  4999. }
  5000. return(oldbase);
  5001. }
  5002. /**
  5003. * xmlNodeBufGetContent:
  5004. * @buffer: a buffer
  5005. * @cur: the node being read
  5006. *
  5007. * Read the value of a node @cur, this can be either the text carried
  5008. * directly by this node if it's a TEXT node or the aggregate string
  5009. * of the values carried by this node child's (TEXT and ENTITY_REF).
  5010. * Entity references are substituted.
  5011. * Fills up the buffer @buffer with this value
  5012. *
  5013. * Returns 0 in case of success and -1 in case of error.
  5014. */
  5015. int
  5016. xmlNodeBufGetContent(xmlBufferPtr buffer, const xmlNode *cur)
  5017. {
  5018. xmlBufPtr buf;
  5019. int ret;
  5020. if ((cur == NULL) || (buffer == NULL)) return(-1);
  5021. buf = xmlBufFromBuffer(buffer);
  5022. ret = xmlBufGetNodeContent(buf, cur);
  5023. buffer = xmlBufBackToBuffer(buf);
  5024. if ((ret < 0) || (buffer == NULL))
  5025. return(-1);
  5026. return(0);
  5027. }
  5028. /**
  5029. * xmlBufGetNodeContent:
  5030. * @buf: a buffer xmlBufPtr
  5031. * @cur: the node being read
  5032. *
  5033. * Read the value of a node @cur, this can be either the text carried
  5034. * directly by this node if it's a TEXT node or the aggregate string
  5035. * of the values carried by this node child's (TEXT and ENTITY_REF).
  5036. * Entity references are substituted.
  5037. * Fills up the buffer @buf with this value
  5038. *
  5039. * Returns 0 in case of success and -1 in case of error.
  5040. */
  5041. int
  5042. xmlBufGetNodeContent(xmlBufPtr buf, const xmlNode *cur)
  5043. {
  5044. if ((cur == NULL) || (buf == NULL)) return(-1);
  5045. switch (cur->type) {
  5046. case XML_CDATA_SECTION_NODE:
  5047. case XML_TEXT_NODE:
  5048. xmlBufCat(buf, cur->content);
  5049. break;
  5050. case XML_DOCUMENT_FRAG_NODE:
  5051. case XML_ELEMENT_NODE:{
  5052. const xmlNode *tmp = cur;
  5053. while (tmp != NULL) {
  5054. switch (tmp->type) {
  5055. case XML_CDATA_SECTION_NODE:
  5056. case XML_TEXT_NODE:
  5057. if (tmp->content != NULL)
  5058. xmlBufCat(buf, tmp->content);
  5059. break;
  5060. case XML_ENTITY_REF_NODE:
  5061. xmlBufGetNodeContent(buf, tmp);
  5062. break;
  5063. default:
  5064. break;
  5065. }
  5066. /*
  5067. * Skip to next node
  5068. */
  5069. if (tmp->children != NULL) {
  5070. if (tmp->children->type != XML_ENTITY_DECL) {
  5071. tmp = tmp->children;
  5072. continue;
  5073. }
  5074. }
  5075. if (tmp == cur)
  5076. break;
  5077. if (tmp->next != NULL) {
  5078. tmp = tmp->next;
  5079. continue;
  5080. }
  5081. do {
  5082. tmp = tmp->parent;
  5083. if (tmp == NULL)
  5084. break;
  5085. if (tmp == cur) {
  5086. tmp = NULL;
  5087. break;
  5088. }
  5089. if (tmp->next != NULL) {
  5090. tmp = tmp->next;
  5091. break;
  5092. }
  5093. } while (tmp != NULL);
  5094. }
  5095. break;
  5096. }
  5097. case XML_ATTRIBUTE_NODE:{
  5098. xmlAttrPtr attr = (xmlAttrPtr) cur;
  5099. xmlNodePtr tmp = attr->children;
  5100. while (tmp != NULL) {
  5101. if (tmp->type == XML_TEXT_NODE)
  5102. xmlBufCat(buf, tmp->content);
  5103. else
  5104. xmlBufGetNodeContent(buf, tmp);
  5105. tmp = tmp->next;
  5106. }
  5107. break;
  5108. }
  5109. case XML_COMMENT_NODE:
  5110. case XML_PI_NODE:
  5111. xmlBufCat(buf, cur->content);
  5112. break;
  5113. case XML_ENTITY_REF_NODE:{
  5114. xmlEntityPtr ent;
  5115. xmlNodePtr tmp;
  5116. /* lookup entity declaration */
  5117. ent = xmlGetDocEntity(cur->doc, cur->name);
  5118. if (ent == NULL)
  5119. return(-1);
  5120. /* an entity content can be any "well balanced chunk",
  5121. * i.e. the result of the content [43] production:
  5122. * http://www.w3.org/TR/REC-xml#NT-content
  5123. * -> we iterate through child nodes and recursive call
  5124. * xmlNodeGetContent() which handles all possible node types */
  5125. tmp = ent->children;
  5126. while (tmp) {
  5127. xmlBufGetNodeContent(buf, tmp);
  5128. tmp = tmp->next;
  5129. }
  5130. break;
  5131. }
  5132. case XML_ENTITY_NODE:
  5133. case XML_DOCUMENT_TYPE_NODE:
  5134. case XML_NOTATION_NODE:
  5135. case XML_DTD_NODE:
  5136. case XML_XINCLUDE_START:
  5137. case XML_XINCLUDE_END:
  5138. break;
  5139. case XML_DOCUMENT_NODE:
  5140. case XML_HTML_DOCUMENT_NODE:
  5141. cur = cur->children;
  5142. while (cur!= NULL) {
  5143. if ((cur->type == XML_ELEMENT_NODE) ||
  5144. (cur->type == XML_TEXT_NODE) ||
  5145. (cur->type == XML_CDATA_SECTION_NODE)) {
  5146. xmlBufGetNodeContent(buf, cur);
  5147. }
  5148. cur = cur->next;
  5149. }
  5150. break;
  5151. case XML_NAMESPACE_DECL:
  5152. xmlBufCat(buf, ((xmlNsPtr) cur)->href);
  5153. break;
  5154. case XML_ELEMENT_DECL:
  5155. case XML_ATTRIBUTE_DECL:
  5156. case XML_ENTITY_DECL:
  5157. break;
  5158. }
  5159. return(0);
  5160. }
  5161. /**
  5162. * xmlNodeGetContent:
  5163. * @cur: the node being read
  5164. *
  5165. * Read the value of a node, this can be either the text carried
  5166. * directly by this node if it's a TEXT node or the aggregate string
  5167. * of the values carried by this node child's (TEXT and ENTITY_REF).
  5168. * Entity references are substituted.
  5169. * Returns a new #xmlChar * or NULL if no content is available.
  5170. * It's up to the caller to free the memory with xmlFree().
  5171. */
  5172. xmlChar *
  5173. xmlNodeGetContent(const xmlNode *cur)
  5174. {
  5175. if (cur == NULL)
  5176. return (NULL);
  5177. switch (cur->type) {
  5178. case XML_DOCUMENT_FRAG_NODE:
  5179. case XML_ELEMENT_NODE:{
  5180. xmlBufPtr buf;
  5181. xmlChar *ret;
  5182. buf = xmlBufCreateSize(64);
  5183. if (buf == NULL)
  5184. return (NULL);
  5185. xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_DOUBLEIT);
  5186. xmlBufGetNodeContent(buf, cur);
  5187. ret = xmlBufDetach(buf);
  5188. xmlBufFree(buf);
  5189. return (ret);
  5190. }
  5191. case XML_ATTRIBUTE_NODE:
  5192. return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur));
  5193. case XML_COMMENT_NODE:
  5194. case XML_PI_NODE:
  5195. if (cur->content != NULL)
  5196. return (xmlStrdup(cur->content));
  5197. return (NULL);
  5198. case XML_ENTITY_REF_NODE:{
  5199. xmlEntityPtr ent;
  5200. xmlBufPtr buf;
  5201. xmlChar *ret;
  5202. /* lookup entity declaration */
  5203. ent = xmlGetDocEntity(cur->doc, cur->name);
  5204. if (ent == NULL)
  5205. return (NULL);
  5206. buf = xmlBufCreate();
  5207. if (buf == NULL)
  5208. return (NULL);
  5209. xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_DOUBLEIT);
  5210. xmlBufGetNodeContent(buf, cur);
  5211. ret = xmlBufDetach(buf);
  5212. xmlBufFree(buf);
  5213. return (ret);
  5214. }
  5215. case XML_ENTITY_NODE:
  5216. case XML_DOCUMENT_TYPE_NODE:
  5217. case XML_NOTATION_NODE:
  5218. case XML_DTD_NODE:
  5219. case XML_XINCLUDE_START:
  5220. case XML_XINCLUDE_END:
  5221. return (NULL);
  5222. case XML_DOCUMENT_NODE:
  5223. case XML_HTML_DOCUMENT_NODE: {
  5224. xmlBufPtr buf;
  5225. xmlChar *ret;
  5226. buf = xmlBufCreate();
  5227. if (buf == NULL)
  5228. return (NULL);
  5229. xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_DOUBLEIT);
  5230. xmlBufGetNodeContent(buf, (xmlNodePtr) cur);
  5231. ret = xmlBufDetach(buf);
  5232. xmlBufFree(buf);
  5233. return (ret);
  5234. }
  5235. case XML_NAMESPACE_DECL: {
  5236. xmlChar *tmp;
  5237. tmp = xmlStrdup(((xmlNsPtr) cur)->href);
  5238. return (tmp);
  5239. }
  5240. case XML_ELEMENT_DECL:
  5241. /* TODO !!! */
  5242. return (NULL);
  5243. case XML_ATTRIBUTE_DECL:
  5244. /* TODO !!! */
  5245. return (NULL);
  5246. case XML_ENTITY_DECL:
  5247. /* TODO !!! */
  5248. return (NULL);
  5249. case XML_CDATA_SECTION_NODE:
  5250. case XML_TEXT_NODE:
  5251. if (cur->content != NULL)
  5252. return (xmlStrdup(cur->content));
  5253. return (NULL);
  5254. }
  5255. return (NULL);
  5256. }
  5257. /**
  5258. * xmlNodeSetContent:
  5259. * @cur: the node being modified
  5260. * @content: the new value of the content
  5261. *
  5262. * Replace the content of a node.
  5263. * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
  5264. * references, but XML special chars need to be escaped first by using
  5265. * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
  5266. */
  5267. void
  5268. xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
  5269. if (cur == NULL) {
  5270. #ifdef DEBUG_TREE
  5271. xmlGenericError(xmlGenericErrorContext,
  5272. "xmlNodeSetContent : node == NULL\n");
  5273. #endif
  5274. return;
  5275. }
  5276. switch (cur->type) {
  5277. case XML_DOCUMENT_FRAG_NODE:
  5278. case XML_ELEMENT_NODE:
  5279. case XML_ATTRIBUTE_NODE:
  5280. if (cur->children != NULL) xmlFreeNodeList(cur->children);
  5281. cur->children = xmlStringGetNodeList(cur->doc, content);
  5282. UPDATE_LAST_CHILD_AND_PARENT(cur)
  5283. break;
  5284. case XML_TEXT_NODE:
  5285. case XML_CDATA_SECTION_NODE:
  5286. case XML_ENTITY_REF_NODE:
  5287. case XML_ENTITY_NODE:
  5288. case XML_PI_NODE:
  5289. case XML_COMMENT_NODE:
  5290. if ((cur->content != NULL) &&
  5291. (cur->content != (xmlChar *) &(cur->properties))) {
  5292. if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
  5293. (xmlDictOwns(cur->doc->dict, cur->content))))
  5294. xmlFree(cur->content);
  5295. }
  5296. if (cur->children != NULL) xmlFreeNodeList(cur->children);
  5297. cur->last = cur->children = NULL;
  5298. if (content != NULL) {
  5299. cur->content = xmlStrdup(content);
  5300. } else
  5301. cur->content = NULL;
  5302. cur->properties = NULL;
  5303. break;
  5304. case XML_DOCUMENT_NODE:
  5305. case XML_HTML_DOCUMENT_NODE:
  5306. case XML_DOCUMENT_TYPE_NODE:
  5307. case XML_XINCLUDE_START:
  5308. case XML_XINCLUDE_END:
  5309. break;
  5310. case XML_NOTATION_NODE:
  5311. break;
  5312. case XML_DTD_NODE:
  5313. break;
  5314. case XML_NAMESPACE_DECL:
  5315. break;
  5316. case XML_ELEMENT_DECL:
  5317. /* TODO !!! */
  5318. break;
  5319. case XML_ATTRIBUTE_DECL:
  5320. /* TODO !!! */
  5321. break;
  5322. case XML_ENTITY_DECL:
  5323. /* TODO !!! */
  5324. break;
  5325. }
  5326. }
  5327. #ifdef LIBXML_TREE_ENABLED
  5328. /**
  5329. * xmlNodeSetContentLen:
  5330. * @cur: the node being modified
  5331. * @content: the new value of the content
  5332. * @len: the size of @content
  5333. *
  5334. * Replace the content of a node.
  5335. * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
  5336. * references, but XML special chars need to be escaped first by using
  5337. * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
  5338. */
  5339. void
  5340. xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
  5341. if (cur == NULL) {
  5342. #ifdef DEBUG_TREE
  5343. xmlGenericError(xmlGenericErrorContext,
  5344. "xmlNodeSetContentLen : node == NULL\n");
  5345. #endif
  5346. return;
  5347. }
  5348. switch (cur->type) {
  5349. case XML_DOCUMENT_FRAG_NODE:
  5350. case XML_ELEMENT_NODE:
  5351. case XML_ATTRIBUTE_NODE:
  5352. if (cur->children != NULL) xmlFreeNodeList(cur->children);
  5353. cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
  5354. UPDATE_LAST_CHILD_AND_PARENT(cur)
  5355. break;
  5356. case XML_TEXT_NODE:
  5357. case XML_CDATA_SECTION_NODE:
  5358. case XML_ENTITY_REF_NODE:
  5359. case XML_ENTITY_NODE:
  5360. case XML_PI_NODE:
  5361. case XML_COMMENT_NODE:
  5362. case XML_NOTATION_NODE:
  5363. if ((cur->content != NULL) &&
  5364. (cur->content != (xmlChar *) &(cur->properties))) {
  5365. if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
  5366. (xmlDictOwns(cur->doc->dict, cur->content))))
  5367. xmlFree(cur->content);
  5368. }
  5369. if (cur->children != NULL) xmlFreeNodeList(cur->children);
  5370. cur->children = cur->last = NULL;
  5371. if (content != NULL) {
  5372. cur->content = xmlStrndup(content, len);
  5373. } else
  5374. cur->content = NULL;
  5375. cur->properties = NULL;
  5376. break;
  5377. case XML_DOCUMENT_NODE:
  5378. case XML_DTD_NODE:
  5379. case XML_HTML_DOCUMENT_NODE:
  5380. case XML_DOCUMENT_TYPE_NODE:
  5381. case XML_NAMESPACE_DECL:
  5382. case XML_XINCLUDE_START:
  5383. case XML_XINCLUDE_END:
  5384. break;
  5385. case XML_ELEMENT_DECL:
  5386. /* TODO !!! */
  5387. break;
  5388. case XML_ATTRIBUTE_DECL:
  5389. /* TODO !!! */
  5390. break;
  5391. case XML_ENTITY_DECL:
  5392. /* TODO !!! */
  5393. break;
  5394. }
  5395. }
  5396. #endif /* LIBXML_TREE_ENABLED */
  5397. /**
  5398. * xmlNodeAddContentLen:
  5399. * @cur: the node being modified
  5400. * @content: extra content
  5401. * @len: the size of @content
  5402. *
  5403. * Append the extra substring to the node content.
  5404. * NOTE: In contrast to xmlNodeSetContentLen(), @content is supposed to be
  5405. * raw text, so unescaped XML special chars are allowed, entity
  5406. * references are not supported.
  5407. */
  5408. void
  5409. xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
  5410. if (cur == NULL) {
  5411. #ifdef DEBUG_TREE
  5412. xmlGenericError(xmlGenericErrorContext,
  5413. "xmlNodeAddContentLen : node == NULL\n");
  5414. #endif
  5415. return;
  5416. }
  5417. if (len <= 0) return;
  5418. switch (cur->type) {
  5419. case XML_DOCUMENT_FRAG_NODE:
  5420. case XML_ELEMENT_NODE: {
  5421. xmlNodePtr last, newNode, tmp;
  5422. last = cur->last;
  5423. newNode = xmlNewDocTextLen(cur->doc, content, len);
  5424. if (newNode != NULL) {
  5425. tmp = xmlAddChild(cur, newNode);
  5426. if (tmp != newNode)
  5427. return;
  5428. if ((last != NULL) && (last->next == newNode)) {
  5429. xmlTextMerge(last, newNode);
  5430. }
  5431. }
  5432. break;
  5433. }
  5434. case XML_ATTRIBUTE_NODE:
  5435. break;
  5436. case XML_TEXT_NODE:
  5437. case XML_CDATA_SECTION_NODE:
  5438. case XML_ENTITY_REF_NODE:
  5439. case XML_ENTITY_NODE:
  5440. case XML_PI_NODE:
  5441. case XML_COMMENT_NODE:
  5442. case XML_NOTATION_NODE:
  5443. if (content != NULL) {
  5444. if ((cur->content == (xmlChar *) &(cur->properties)) ||
  5445. ((cur->doc != NULL) && (cur->doc->dict != NULL) &&
  5446. xmlDictOwns(cur->doc->dict, cur->content))) {
  5447. cur->content = xmlStrncatNew(cur->content, content, len);
  5448. cur->properties = NULL;
  5449. } else {
  5450. cur->content = xmlStrncat(cur->content, content, len);
  5451. }
  5452. }
  5453. break;
  5454. case XML_DOCUMENT_NODE:
  5455. case XML_DTD_NODE:
  5456. case XML_HTML_DOCUMENT_NODE:
  5457. case XML_DOCUMENT_TYPE_NODE:
  5458. case XML_NAMESPACE_DECL:
  5459. case XML_XINCLUDE_START:
  5460. case XML_XINCLUDE_END:
  5461. break;
  5462. case XML_ELEMENT_DECL:
  5463. case XML_ATTRIBUTE_DECL:
  5464. case XML_ENTITY_DECL:
  5465. break;
  5466. }
  5467. }
  5468. /**
  5469. * xmlNodeAddContent:
  5470. * @cur: the node being modified
  5471. * @content: extra content
  5472. *
  5473. * Append the extra substring to the node content.
  5474. * NOTE: In contrast to xmlNodeSetContent(), @content is supposed to be
  5475. * raw text, so unescaped XML special chars are allowed, entity
  5476. * references are not supported.
  5477. */
  5478. void
  5479. xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
  5480. int len;
  5481. if (cur == NULL) {
  5482. #ifdef DEBUG_TREE
  5483. xmlGenericError(xmlGenericErrorContext,
  5484. "xmlNodeAddContent : node == NULL\n");
  5485. #endif
  5486. return;
  5487. }
  5488. if (content == NULL) return;
  5489. len = xmlStrlen(content);
  5490. xmlNodeAddContentLen(cur, content, len);
  5491. }
  5492. /**
  5493. * xmlTextMerge:
  5494. * @first: the first text node
  5495. * @second: the second text node being merged
  5496. *
  5497. * Merge two text nodes into one
  5498. * Returns the first text node augmented
  5499. */
  5500. xmlNodePtr
  5501. xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
  5502. if (first == NULL) return(second);
  5503. if (second == NULL) return(first);
  5504. if (first->type != XML_TEXT_NODE) return(first);
  5505. if (second->type != XML_TEXT_NODE) return(first);
  5506. if (second->name != first->name)
  5507. return(first);
  5508. xmlNodeAddContent(first, second->content);
  5509. xmlUnlinkNode(second);
  5510. xmlFreeNode(second);
  5511. return(first);
  5512. }
  5513. #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
  5514. /**
  5515. * xmlGetNsList:
  5516. * @doc: the document
  5517. * @node: the current node
  5518. *
  5519. * Search all the namespace applying to a given element.
  5520. * Returns an NULL terminated array of all the #xmlNsPtr found
  5521. * that need to be freed by the caller or NULL if no
  5522. * namespace if defined
  5523. */
  5524. xmlNsPtr *
  5525. xmlGetNsList(const xmlDoc *doc ATTRIBUTE_UNUSED, const xmlNode *node)
  5526. {
  5527. xmlNsPtr cur;
  5528. xmlNsPtr *ret = NULL;
  5529. int nbns = 0;
  5530. int maxns = 10;
  5531. int i;
  5532. if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
  5533. return(NULL);
  5534. while (node != NULL) {
  5535. if (node->type == XML_ELEMENT_NODE) {
  5536. cur = node->nsDef;
  5537. while (cur != NULL) {
  5538. if (ret == NULL) {
  5539. ret =
  5540. (xmlNsPtr *) xmlMalloc((maxns + 1) *
  5541. sizeof(xmlNsPtr));
  5542. if (ret == NULL) {
  5543. xmlTreeErrMemory("getting namespace list");
  5544. return (NULL);
  5545. }
  5546. ret[nbns] = NULL;
  5547. }
  5548. for (i = 0; i < nbns; i++) {
  5549. if ((cur->prefix == ret[i]->prefix) ||
  5550. (xmlStrEqual(cur->prefix, ret[i]->prefix)))
  5551. break;
  5552. }
  5553. if (i >= nbns) {
  5554. if (nbns >= maxns) {
  5555. maxns *= 2;
  5556. ret = (xmlNsPtr *) xmlRealloc(ret,
  5557. (maxns +
  5558. 1) *
  5559. sizeof(xmlNsPtr));
  5560. if (ret == NULL) {
  5561. xmlTreeErrMemory("getting namespace list");
  5562. return (NULL);
  5563. }
  5564. }
  5565. ret[nbns++] = cur;
  5566. ret[nbns] = NULL;
  5567. }
  5568. cur = cur->next;
  5569. }
  5570. }
  5571. node = node->parent;
  5572. }
  5573. return (ret);
  5574. }
  5575. #endif /* LIBXML_TREE_ENABLED */
  5576. /*
  5577. * xmlTreeEnsureXMLDecl:
  5578. * @doc: the doc
  5579. *
  5580. * Ensures that there is an XML namespace declaration on the doc.
  5581. *
  5582. * Returns the XML ns-struct or NULL on API and internal errors.
  5583. */
  5584. static xmlNsPtr
  5585. xmlTreeEnsureXMLDecl(xmlDocPtr doc)
  5586. {
  5587. if (doc == NULL)
  5588. return (NULL);
  5589. if (doc->oldNs != NULL)
  5590. return (doc->oldNs);
  5591. {
  5592. xmlNsPtr ns;
  5593. ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
  5594. if (ns == NULL) {
  5595. xmlTreeErrMemory(
  5596. "allocating the XML namespace");
  5597. return (NULL);
  5598. }
  5599. memset(ns, 0, sizeof(xmlNs));
  5600. ns->type = XML_LOCAL_NAMESPACE;
  5601. ns->href = xmlStrdup(XML_XML_NAMESPACE);
  5602. ns->prefix = xmlStrdup((const xmlChar *)"xml");
  5603. doc->oldNs = ns;
  5604. return (ns);
  5605. }
  5606. }
  5607. /**
  5608. * xmlSearchNs:
  5609. * @doc: the document
  5610. * @node: the current node
  5611. * @nameSpace: the namespace prefix
  5612. *
  5613. * Search a Ns registered under a given name space for a document.
  5614. * recurse on the parents until it finds the defined namespace
  5615. * or return NULL otherwise.
  5616. * @nameSpace can be NULL, this is a search for the default namespace.
  5617. * We don't allow to cross entities boundaries. If you don't declare
  5618. * the namespace within those you will be in troubles !!! A warning
  5619. * is generated to cover this case.
  5620. *
  5621. * Returns the namespace pointer or NULL.
  5622. */
  5623. xmlNsPtr
  5624. xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
  5625. xmlNsPtr cur;
  5626. const xmlNode *orig = node;
  5627. if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) return(NULL);
  5628. if ((nameSpace != NULL) &&
  5629. (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
  5630. if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
  5631. /*
  5632. * The XML-1.0 namespace is normally held on the root
  5633. * element. In this case exceptionally create it on the
  5634. * node element.
  5635. */
  5636. cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
  5637. if (cur == NULL) {
  5638. xmlTreeErrMemory("searching namespace");
  5639. return(NULL);
  5640. }
  5641. memset(cur, 0, sizeof(xmlNs));
  5642. cur->type = XML_LOCAL_NAMESPACE;
  5643. cur->href = xmlStrdup(XML_XML_NAMESPACE);
  5644. cur->prefix = xmlStrdup((const xmlChar *)"xml");
  5645. cur->next = node->nsDef;
  5646. node->nsDef = cur;
  5647. return(cur);
  5648. }
  5649. if (doc == NULL) {
  5650. doc = node->doc;
  5651. if (doc == NULL)
  5652. return(NULL);
  5653. }
  5654. /*
  5655. * Return the XML namespace declaration held by the doc.
  5656. */
  5657. if (doc->oldNs == NULL)
  5658. return(xmlTreeEnsureXMLDecl(doc));
  5659. else
  5660. return(doc->oldNs);
  5661. }
  5662. while (node != NULL) {
  5663. if ((node->type == XML_ENTITY_REF_NODE) ||
  5664. (node->type == XML_ENTITY_NODE) ||
  5665. (node->type == XML_ENTITY_DECL))
  5666. return(NULL);
  5667. if (node->type == XML_ELEMENT_NODE) {
  5668. cur = node->nsDef;
  5669. while (cur != NULL) {
  5670. if ((cur->prefix == NULL) && (nameSpace == NULL) &&
  5671. (cur->href != NULL))
  5672. return(cur);
  5673. if ((cur->prefix != NULL) && (nameSpace != NULL) &&
  5674. (cur->href != NULL) &&
  5675. (xmlStrEqual(cur->prefix, nameSpace)))
  5676. return(cur);
  5677. cur = cur->next;
  5678. }
  5679. if (orig != node) {
  5680. cur = node->ns;
  5681. if (cur != NULL) {
  5682. if ((cur->prefix == NULL) && (nameSpace == NULL) &&
  5683. (cur->href != NULL))
  5684. return(cur);
  5685. if ((cur->prefix != NULL) && (nameSpace != NULL) &&
  5686. (cur->href != NULL) &&
  5687. (xmlStrEqual(cur->prefix, nameSpace)))
  5688. return(cur);
  5689. }
  5690. }
  5691. }
  5692. node = node->parent;
  5693. }
  5694. return(NULL);
  5695. }
  5696. /**
  5697. * xmlNsInScope:
  5698. * @doc: the document
  5699. * @node: the current node
  5700. * @ancestor: the ancestor carrying the namespace
  5701. * @prefix: the namespace prefix
  5702. *
  5703. * Verify that the given namespace held on @ancestor is still in scope
  5704. * on node.
  5705. *
  5706. * Returns 1 if true, 0 if false and -1 in case of error.
  5707. */
  5708. static int
  5709. xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
  5710. xmlNodePtr ancestor, const xmlChar * prefix)
  5711. {
  5712. xmlNsPtr tst;
  5713. while ((node != NULL) && (node != ancestor)) {
  5714. if ((node->type == XML_ENTITY_REF_NODE) ||
  5715. (node->type == XML_ENTITY_NODE) ||
  5716. (node->type == XML_ENTITY_DECL))
  5717. return (-1);
  5718. if (node->type == XML_ELEMENT_NODE) {
  5719. tst = node->nsDef;
  5720. while (tst != NULL) {
  5721. if ((tst->prefix == NULL)
  5722. && (prefix == NULL))
  5723. return (0);
  5724. if ((tst->prefix != NULL)
  5725. && (prefix != NULL)
  5726. && (xmlStrEqual(tst->prefix, prefix)))
  5727. return (0);
  5728. tst = tst->next;
  5729. }
  5730. }
  5731. node = node->parent;
  5732. }
  5733. if (node != ancestor)
  5734. return (-1);
  5735. return (1);
  5736. }
  5737. /**
  5738. * xmlSearchNsByHref:
  5739. * @doc: the document
  5740. * @node: the current node
  5741. * @href: the namespace value
  5742. *
  5743. * Search a Ns aliasing a given URI. Recurse on the parents until it finds
  5744. * the defined namespace or return NULL otherwise.
  5745. * Returns the namespace pointer or NULL.
  5746. */
  5747. xmlNsPtr
  5748. xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
  5749. {
  5750. xmlNsPtr cur;
  5751. xmlNodePtr orig = node;
  5752. int is_attr;
  5753. if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || (href == NULL))
  5754. return (NULL);
  5755. if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
  5756. /*
  5757. * Only the document can hold the XML spec namespace.
  5758. */
  5759. if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
  5760. /*
  5761. * The XML-1.0 namespace is normally held on the root
  5762. * element. In this case exceptionally create it on the
  5763. * node element.
  5764. */
  5765. cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
  5766. if (cur == NULL) {
  5767. xmlTreeErrMemory("searching namespace");
  5768. return (NULL);
  5769. }
  5770. memset(cur, 0, sizeof(xmlNs));
  5771. cur->type = XML_LOCAL_NAMESPACE;
  5772. cur->href = xmlStrdup(XML_XML_NAMESPACE);
  5773. cur->prefix = xmlStrdup((const xmlChar *) "xml");
  5774. cur->next = node->nsDef;
  5775. node->nsDef = cur;
  5776. return (cur);
  5777. }
  5778. if (doc == NULL) {
  5779. doc = node->doc;
  5780. if (doc == NULL)
  5781. return(NULL);
  5782. }
  5783. /*
  5784. * Return the XML namespace declaration held by the doc.
  5785. */
  5786. if (doc->oldNs == NULL)
  5787. return(xmlTreeEnsureXMLDecl(doc));
  5788. else
  5789. return(doc->oldNs);
  5790. }
  5791. is_attr = (node->type == XML_ATTRIBUTE_NODE);
  5792. while (node != NULL) {
  5793. if ((node->type == XML_ENTITY_REF_NODE) ||
  5794. (node->type == XML_ENTITY_NODE) ||
  5795. (node->type == XML_ENTITY_DECL))
  5796. return (NULL);
  5797. if (node->type == XML_ELEMENT_NODE) {
  5798. cur = node->nsDef;
  5799. while (cur != NULL) {
  5800. if ((cur->href != NULL) && (href != NULL) &&
  5801. (xmlStrEqual(cur->href, href))) {
  5802. if (((!is_attr) || (cur->prefix != NULL)) &&
  5803. (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
  5804. return (cur);
  5805. }
  5806. cur = cur->next;
  5807. }
  5808. if (orig != node) {
  5809. cur = node->ns;
  5810. if (cur != NULL) {
  5811. if ((cur->href != NULL) && (href != NULL) &&
  5812. (xmlStrEqual(cur->href, href))) {
  5813. if (((!is_attr) || (cur->prefix != NULL)) &&
  5814. (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
  5815. return (cur);
  5816. }
  5817. }
  5818. }
  5819. }
  5820. node = node->parent;
  5821. }
  5822. return (NULL);
  5823. }
  5824. /**
  5825. * xmlNewReconciledNs:
  5826. * @doc: the document
  5827. * @tree: a node expected to hold the new namespace
  5828. * @ns: the original namespace
  5829. *
  5830. * This function tries to locate a namespace definition in a tree
  5831. * ancestors, or create a new namespace definition node similar to
  5832. * @ns trying to reuse the same prefix. However if the given prefix is
  5833. * null (default namespace) or reused within the subtree defined by
  5834. * @tree or on one of its ancestors then a new prefix is generated.
  5835. * Returns the (new) namespace definition or NULL in case of error
  5836. */
  5837. static xmlNsPtr
  5838. xmlNewReconciledNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
  5839. xmlNsPtr def;
  5840. xmlChar prefix[50];
  5841. int counter = 1;
  5842. if ((tree == NULL) || (tree->type != XML_ELEMENT_NODE)) {
  5843. #ifdef DEBUG_TREE
  5844. xmlGenericError(xmlGenericErrorContext,
  5845. "xmlNewReconciledNs : tree == NULL\n");
  5846. #endif
  5847. return(NULL);
  5848. }
  5849. if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
  5850. #ifdef DEBUG_TREE
  5851. xmlGenericError(xmlGenericErrorContext,
  5852. "xmlNewReconciledNs : ns == NULL\n");
  5853. #endif
  5854. return(NULL);
  5855. }
  5856. /*
  5857. * Search an existing namespace definition inherited.
  5858. */
  5859. def = xmlSearchNsByHref(doc, tree, ns->href);
  5860. if (def != NULL)
  5861. return(def);
  5862. /*
  5863. * Find a close prefix which is not already in use.
  5864. * Let's strip namespace prefixes longer than 20 chars !
  5865. */
  5866. if (ns->prefix == NULL)
  5867. snprintf((char *) prefix, sizeof(prefix), "default");
  5868. else
  5869. snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
  5870. def = xmlSearchNs(doc, tree, prefix);
  5871. while (def != NULL) {
  5872. if (counter > 1000) return(NULL);
  5873. if (ns->prefix == NULL)
  5874. snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
  5875. else
  5876. snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
  5877. (char *)ns->prefix, counter++);
  5878. def = xmlSearchNs(doc, tree, prefix);
  5879. }
  5880. /*
  5881. * OK, now we are ready to create a new one.
  5882. */
  5883. def = xmlNewNs(tree, ns->href, prefix);
  5884. return(def);
  5885. }
  5886. #ifdef LIBXML_TREE_ENABLED
  5887. /**
  5888. * xmlReconciliateNs:
  5889. * @doc: the document
  5890. * @tree: a node defining the subtree to reconciliate
  5891. *
  5892. * This function checks that all the namespaces declared within the given
  5893. * tree are properly declared. This is needed for example after Copy or Cut
  5894. * and then paste operations. The subtree may still hold pointers to
  5895. * namespace declarations outside the subtree or invalid/masked. As much
  5896. * as possible the function try to reuse the existing namespaces found in
  5897. * the new environment. If not possible the new namespaces are redeclared
  5898. * on @tree at the top of the given subtree.
  5899. * Returns the number of namespace declarations created or -1 in case of error.
  5900. */
  5901. int
  5902. xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
  5903. xmlNsPtr *oldNs = NULL;
  5904. xmlNsPtr *newNs = NULL;
  5905. int sizeCache = 0;
  5906. int nbCache = 0;
  5907. xmlNsPtr n;
  5908. xmlNodePtr node = tree;
  5909. xmlAttrPtr attr;
  5910. int ret = 0, i;
  5911. if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1);
  5912. if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1);
  5913. if (node->doc != doc) return(-1);
  5914. while (node != NULL) {
  5915. /*
  5916. * Reconciliate the node namespace
  5917. */
  5918. if (node->ns != NULL) {
  5919. /*
  5920. * initialize the cache if needed
  5921. */
  5922. if (sizeCache == 0) {
  5923. sizeCache = 10;
  5924. oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
  5925. sizeof(xmlNsPtr));
  5926. if (oldNs == NULL) {
  5927. xmlTreeErrMemory("fixing namespaces");
  5928. return(-1);
  5929. }
  5930. newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
  5931. sizeof(xmlNsPtr));
  5932. if (newNs == NULL) {
  5933. xmlTreeErrMemory("fixing namespaces");
  5934. xmlFree(oldNs);
  5935. return(-1);
  5936. }
  5937. }
  5938. for (i = 0;i < nbCache;i++) {
  5939. if (oldNs[i] == node->ns) {
  5940. node->ns = newNs[i];
  5941. break;
  5942. }
  5943. }
  5944. if (i == nbCache) {
  5945. /*
  5946. * OK we need to recreate a new namespace definition
  5947. */
  5948. n = xmlNewReconciledNs(doc, tree, node->ns);
  5949. if (n != NULL) { /* :-( what if else ??? */
  5950. /*
  5951. * check if we need to grow the cache buffers.
  5952. */
  5953. if (sizeCache <= nbCache) {
  5954. sizeCache *= 2;
  5955. oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
  5956. sizeof(xmlNsPtr));
  5957. if (oldNs == NULL) {
  5958. xmlTreeErrMemory("fixing namespaces");
  5959. xmlFree(newNs);
  5960. return(-1);
  5961. }
  5962. newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
  5963. sizeof(xmlNsPtr));
  5964. if (newNs == NULL) {
  5965. xmlTreeErrMemory("fixing namespaces");
  5966. xmlFree(oldNs);
  5967. return(-1);
  5968. }
  5969. }
  5970. newNs[nbCache] = n;
  5971. oldNs[nbCache++] = node->ns;
  5972. node->ns = n;
  5973. }
  5974. }
  5975. }
  5976. /*
  5977. * now check for namespace held by attributes on the node.
  5978. */
  5979. if (node->type == XML_ELEMENT_NODE) {
  5980. attr = node->properties;
  5981. while (attr != NULL) {
  5982. if (attr->ns != NULL) {
  5983. /*
  5984. * initialize the cache if needed
  5985. */
  5986. if (sizeCache == 0) {
  5987. sizeCache = 10;
  5988. oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
  5989. sizeof(xmlNsPtr));
  5990. if (oldNs == NULL) {
  5991. xmlTreeErrMemory("fixing namespaces");
  5992. return(-1);
  5993. }
  5994. newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
  5995. sizeof(xmlNsPtr));
  5996. if (newNs == NULL) {
  5997. xmlTreeErrMemory("fixing namespaces");
  5998. xmlFree(oldNs);
  5999. return(-1);
  6000. }
  6001. }
  6002. for (i = 0;i < nbCache;i++) {
  6003. if (oldNs[i] == attr->ns) {
  6004. attr->ns = newNs[i];
  6005. break;
  6006. }
  6007. }
  6008. if (i == nbCache) {
  6009. /*
  6010. * OK we need to recreate a new namespace definition
  6011. */
  6012. n = xmlNewReconciledNs(doc, tree, attr->ns);
  6013. if (n != NULL) { /* :-( what if else ??? */
  6014. /*
  6015. * check if we need to grow the cache buffers.
  6016. */
  6017. if (sizeCache <= nbCache) {
  6018. sizeCache *= 2;
  6019. oldNs = (xmlNsPtr *) xmlRealloc(oldNs,
  6020. sizeCache * sizeof(xmlNsPtr));
  6021. if (oldNs == NULL) {
  6022. xmlTreeErrMemory("fixing namespaces");
  6023. xmlFree(newNs);
  6024. return(-1);
  6025. }
  6026. newNs = (xmlNsPtr *) xmlRealloc(newNs,
  6027. sizeCache * sizeof(xmlNsPtr));
  6028. if (newNs == NULL) {
  6029. xmlTreeErrMemory("fixing namespaces");
  6030. xmlFree(oldNs);
  6031. return(-1);
  6032. }
  6033. }
  6034. newNs[nbCache] = n;
  6035. oldNs[nbCache++] = attr->ns;
  6036. attr->ns = n;
  6037. }
  6038. }
  6039. }
  6040. attr = attr->next;
  6041. }
  6042. }
  6043. /*
  6044. * Browse the full subtree, deep first
  6045. */
  6046. if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
  6047. /* deep first */
  6048. node = node->children;
  6049. } else if ((node != tree) && (node->next != NULL)) {
  6050. /* then siblings */
  6051. node = node->next;
  6052. } else if (node != tree) {
  6053. /* go up to parents->next if needed */
  6054. while (node != tree) {
  6055. if (node->parent != NULL)
  6056. node = node->parent;
  6057. if ((node != tree) && (node->next != NULL)) {
  6058. node = node->next;
  6059. break;
  6060. }
  6061. if (node->parent == NULL) {
  6062. node = NULL;
  6063. break;
  6064. }
  6065. }
  6066. /* exit condition */
  6067. if (node == tree)
  6068. node = NULL;
  6069. } else
  6070. break;
  6071. }
  6072. if (oldNs != NULL)
  6073. xmlFree(oldNs);
  6074. if (newNs != NULL)
  6075. xmlFree(newNs);
  6076. return(ret);
  6077. }
  6078. #endif /* LIBXML_TREE_ENABLED */
  6079. static xmlAttrPtr
  6080. xmlGetPropNodeInternal(const xmlNode *node, const xmlChar *name,
  6081. const xmlChar *nsName, int useDTD)
  6082. {
  6083. xmlAttrPtr prop;
  6084. /* Avoid unused variable warning if features are disabled. */
  6085. (void) useDTD;
  6086. if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
  6087. return(NULL);
  6088. if (node->properties != NULL) {
  6089. prop = node->properties;
  6090. if (nsName == NULL) {
  6091. /*
  6092. * We want the attr to be in no namespace.
  6093. */
  6094. do {
  6095. if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) {
  6096. return(prop);
  6097. }
  6098. prop = prop->next;
  6099. } while (prop != NULL);
  6100. } else {
  6101. /*
  6102. * We want the attr to be in the specified namespace.
  6103. */
  6104. do {
  6105. if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) &&
  6106. ((prop->ns->href == nsName) ||
  6107. xmlStrEqual(prop->ns->href, nsName)))
  6108. {
  6109. return(prop);
  6110. }
  6111. prop = prop->next;
  6112. } while (prop != NULL);
  6113. }
  6114. }
  6115. #ifdef LIBXML_TREE_ENABLED
  6116. if (! useDTD)
  6117. return(NULL);
  6118. /*
  6119. * Check if there is a default/fixed attribute declaration in
  6120. * the internal or external subset.
  6121. */
  6122. if ((node->doc != NULL) && (node->doc->intSubset != NULL)) {
  6123. xmlDocPtr doc = node->doc;
  6124. xmlAttributePtr attrDecl = NULL;
  6125. xmlChar *elemQName, *tmpstr = NULL;
  6126. /*
  6127. * We need the QName of the element for the DTD-lookup.
  6128. */
  6129. if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
  6130. tmpstr = xmlStrdup(node->ns->prefix);
  6131. tmpstr = xmlStrcat(tmpstr, BAD_CAST ":");
  6132. tmpstr = xmlStrcat(tmpstr, node->name);
  6133. if (tmpstr == NULL)
  6134. return(NULL);
  6135. elemQName = tmpstr;
  6136. } else
  6137. elemQName = (xmlChar *) node->name;
  6138. if (nsName == NULL) {
  6139. /*
  6140. * The common and nice case: Attr in no namespace.
  6141. */
  6142. attrDecl = xmlGetDtdQAttrDesc(doc->intSubset,
  6143. elemQName, name, NULL);
  6144. if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
  6145. attrDecl = xmlGetDtdQAttrDesc(doc->extSubset,
  6146. elemQName, name, NULL);
  6147. }
  6148. } else if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
  6149. /*
  6150. * The XML namespace must be bound to prefix 'xml'.
  6151. */
  6152. attrDecl = xmlGetDtdQAttrDesc(doc->intSubset,
  6153. elemQName, name, BAD_CAST "xml");
  6154. if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
  6155. attrDecl = xmlGetDtdQAttrDesc(doc->extSubset,
  6156. elemQName, name, BAD_CAST "xml");
  6157. }
  6158. } else {
  6159. xmlNsPtr *nsList, *cur;
  6160. /*
  6161. * The ugly case: Search using the prefixes of in-scope
  6162. * ns-decls corresponding to @nsName.
  6163. */
  6164. nsList = xmlGetNsList(node->doc, node);
  6165. if (nsList == NULL) {
  6166. if (tmpstr != NULL)
  6167. xmlFree(tmpstr);
  6168. return(NULL);
  6169. }
  6170. cur = nsList;
  6171. while (*cur != NULL) {
  6172. if (xmlStrEqual((*cur)->href, nsName)) {
  6173. attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName,
  6174. name, (*cur)->prefix);
  6175. if (attrDecl)
  6176. break;
  6177. if (doc->extSubset != NULL) {
  6178. attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName,
  6179. name, (*cur)->prefix);
  6180. if (attrDecl)
  6181. break;
  6182. }
  6183. }
  6184. cur++;
  6185. }
  6186. xmlFree(nsList);
  6187. }
  6188. if (tmpstr != NULL)
  6189. xmlFree(tmpstr);
  6190. /*
  6191. * Only default/fixed attrs are relevant.
  6192. */
  6193. if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
  6194. return((xmlAttrPtr) attrDecl);
  6195. }
  6196. #endif /* LIBXML_TREE_ENABLED */
  6197. return(NULL);
  6198. }
  6199. static xmlChar*
  6200. xmlGetPropNodeValueInternal(const xmlAttr *prop)
  6201. {
  6202. if (prop == NULL)
  6203. return(NULL);
  6204. if (prop->type == XML_ATTRIBUTE_NODE) {
  6205. /*
  6206. * Note that we return at least the empty string.
  6207. * TODO: Do we really always want that?
  6208. */
  6209. if (prop->children != NULL) {
  6210. if ((prop->children->next == NULL) &&
  6211. ((prop->children->type == XML_TEXT_NODE) ||
  6212. (prop->children->type == XML_CDATA_SECTION_NODE)))
  6213. {
  6214. /*
  6215. * Optimization for the common case: only 1 text node.
  6216. */
  6217. return(xmlStrdup(prop->children->content));
  6218. } else {
  6219. xmlChar *ret;
  6220. ret = xmlNodeListGetString(prop->doc, prop->children, 1);
  6221. if (ret != NULL)
  6222. return(ret);
  6223. }
  6224. }
  6225. return(xmlStrdup((xmlChar *)""));
  6226. } else if (prop->type == XML_ATTRIBUTE_DECL) {
  6227. return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue));
  6228. }
  6229. return(NULL);
  6230. }
  6231. /**
  6232. * xmlHasProp:
  6233. * @node: the node
  6234. * @name: the attribute name
  6235. *
  6236. * Search an attribute associated to a node
  6237. * This function also looks in DTD attribute declaration for #FIXED or
  6238. * default declaration values unless DTD use has been turned off.
  6239. *
  6240. * Returns the attribute or the attribute declaration or NULL if
  6241. * neither was found.
  6242. */
  6243. xmlAttrPtr
  6244. xmlHasProp(const xmlNode *node, const xmlChar *name) {
  6245. xmlAttrPtr prop;
  6246. xmlDocPtr doc;
  6247. if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
  6248. return(NULL);
  6249. /*
  6250. * Check on the properties attached to the node
  6251. */
  6252. prop = node->properties;
  6253. while (prop != NULL) {
  6254. if (xmlStrEqual(prop->name, name)) {
  6255. return(prop);
  6256. }
  6257. prop = prop->next;
  6258. }
  6259. if (!xmlCheckDTD) return(NULL);
  6260. /*
  6261. * Check if there is a default declaration in the internal
  6262. * or external subsets
  6263. */
  6264. doc = node->doc;
  6265. if (doc != NULL) {
  6266. xmlAttributePtr attrDecl;
  6267. if (doc->intSubset != NULL) {
  6268. attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
  6269. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  6270. attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
  6271. if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
  6272. /* return attribute declaration only if a default value is given
  6273. (that includes #FIXED declarations) */
  6274. return((xmlAttrPtr) attrDecl);
  6275. }
  6276. }
  6277. return(NULL);
  6278. }
  6279. /**
  6280. * xmlHasNsProp:
  6281. * @node: the node
  6282. * @name: the attribute name
  6283. * @nameSpace: the URI of the namespace
  6284. *
  6285. * Search for an attribute associated to a node
  6286. * This attribute has to be anchored in the namespace specified.
  6287. * This does the entity substitution.
  6288. * This function looks in DTD attribute declaration for #FIXED or
  6289. * default declaration values unless DTD use has been turned off.
  6290. * Note that a namespace of NULL indicates to use the default namespace.
  6291. *
  6292. * Returns the attribute or the attribute declaration or NULL
  6293. * if neither was found.
  6294. */
  6295. xmlAttrPtr
  6296. xmlHasNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) {
  6297. return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD));
  6298. }
  6299. /**
  6300. * xmlGetProp:
  6301. * @node: the node
  6302. * @name: the attribute name
  6303. *
  6304. * Search and get the value of an attribute associated to a node
  6305. * This does the entity substitution.
  6306. * This function looks in DTD attribute declaration for #FIXED or
  6307. * default declaration values unless DTD use has been turned off.
  6308. * NOTE: this function acts independently of namespaces associated
  6309. * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
  6310. * for namespace aware processing.
  6311. *
  6312. * Returns the attribute value or NULL if not found.
  6313. * It's up to the caller to free the memory with xmlFree().
  6314. */
  6315. xmlChar *
  6316. xmlGetProp(const xmlNode *node, const xmlChar *name) {
  6317. xmlAttrPtr prop;
  6318. prop = xmlHasProp(node, name);
  6319. if (prop == NULL)
  6320. return(NULL);
  6321. return(xmlGetPropNodeValueInternal(prop));
  6322. }
  6323. /**
  6324. * xmlGetNoNsProp:
  6325. * @node: the node
  6326. * @name: the attribute name
  6327. *
  6328. * Search and get the value of an attribute associated to a node
  6329. * This does the entity substitution.
  6330. * This function looks in DTD attribute declaration for #FIXED or
  6331. * default declaration values unless DTD use has been turned off.
  6332. * This function is similar to xmlGetProp except it will accept only
  6333. * an attribute in no namespace.
  6334. *
  6335. * Returns the attribute value or NULL if not found.
  6336. * It's up to the caller to free the memory with xmlFree().
  6337. */
  6338. xmlChar *
  6339. xmlGetNoNsProp(const xmlNode *node, const xmlChar *name) {
  6340. xmlAttrPtr prop;
  6341. prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD);
  6342. if (prop == NULL)
  6343. return(NULL);
  6344. return(xmlGetPropNodeValueInternal(prop));
  6345. }
  6346. /**
  6347. * xmlGetNsProp:
  6348. * @node: the node
  6349. * @name: the attribute name
  6350. * @nameSpace: the URI of the namespace
  6351. *
  6352. * Search and get the value of an attribute associated to a node
  6353. * This attribute has to be anchored in the namespace specified.
  6354. * This does the entity substitution.
  6355. * This function looks in DTD attribute declaration for #FIXED or
  6356. * default declaration values unless DTD use has been turned off.
  6357. *
  6358. * Returns the attribute value or NULL if not found.
  6359. * It's up to the caller to free the memory with xmlFree().
  6360. */
  6361. xmlChar *
  6362. xmlGetNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) {
  6363. xmlAttrPtr prop;
  6364. prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD);
  6365. if (prop == NULL)
  6366. return(NULL);
  6367. return(xmlGetPropNodeValueInternal(prop));
  6368. }
  6369. #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
  6370. /**
  6371. * xmlUnsetProp:
  6372. * @node: the node
  6373. * @name: the attribute name
  6374. *
  6375. * Remove an attribute carried by a node.
  6376. * This handles only attributes in no namespace.
  6377. * Returns 0 if successful, -1 if not found
  6378. */
  6379. int
  6380. xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
  6381. xmlAttrPtr prop;
  6382. prop = xmlGetPropNodeInternal(node, name, NULL, 0);
  6383. if (prop == NULL)
  6384. return(-1);
  6385. xmlUnlinkNode((xmlNodePtr) prop);
  6386. xmlFreeProp(prop);
  6387. return(0);
  6388. }
  6389. /**
  6390. * xmlUnsetNsProp:
  6391. * @node: the node
  6392. * @ns: the namespace definition
  6393. * @name: the attribute name
  6394. *
  6395. * Remove an attribute carried by a node.
  6396. * Returns 0 if successful, -1 if not found
  6397. */
  6398. int
  6399. xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
  6400. xmlAttrPtr prop;
  6401. prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
  6402. if (prop == NULL)
  6403. return(-1);
  6404. xmlUnlinkNode((xmlNodePtr) prop);
  6405. xmlFreeProp(prop);
  6406. return(0);
  6407. }
  6408. #endif
  6409. #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_HTML_ENABLED)
  6410. /**
  6411. * xmlSetProp:
  6412. * @node: the node
  6413. * @name: the attribute name (a QName)
  6414. * @value: the attribute value
  6415. *
  6416. * Set (or reset) an attribute carried by a node.
  6417. * If @name has a prefix, then the corresponding
  6418. * namespace-binding will be used, if in scope; it is an
  6419. * error it there's no such ns-binding for the prefix in
  6420. * scope.
  6421. * Returns the attribute pointer.
  6422. *
  6423. */
  6424. xmlAttrPtr
  6425. xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
  6426. int len;
  6427. const xmlChar *nqname;
  6428. if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
  6429. return(NULL);
  6430. /*
  6431. * handle QNames
  6432. */
  6433. nqname = xmlSplitQName3(name, &len);
  6434. if (nqname != NULL) {
  6435. xmlNsPtr ns;
  6436. xmlChar *prefix = xmlStrndup(name, len);
  6437. ns = xmlSearchNs(node->doc, node, prefix);
  6438. if (prefix != NULL)
  6439. xmlFree(prefix);
  6440. if (ns != NULL)
  6441. return(xmlSetNsProp(node, ns, nqname, value));
  6442. }
  6443. return(xmlSetNsProp(node, NULL, name, value));
  6444. }
  6445. /**
  6446. * xmlSetNsProp:
  6447. * @node: the node
  6448. * @ns: the namespace definition
  6449. * @name: the attribute name
  6450. * @value: the attribute value
  6451. *
  6452. * Set (or reset) an attribute carried by a node.
  6453. * The ns structure must be in scope, this is not checked
  6454. *
  6455. * Returns the attribute pointer.
  6456. */
  6457. xmlAttrPtr
  6458. xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
  6459. const xmlChar *value)
  6460. {
  6461. xmlAttrPtr prop;
  6462. if (ns && (ns->href == NULL))
  6463. return(NULL);
  6464. prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
  6465. if (prop != NULL) {
  6466. /*
  6467. * Modify the attribute's value.
  6468. */
  6469. if (prop->atype == XML_ATTRIBUTE_ID) {
  6470. xmlRemoveID(node->doc, prop);
  6471. prop->atype = XML_ATTRIBUTE_ID;
  6472. }
  6473. if (prop->children != NULL)
  6474. xmlFreeNodeList(prop->children);
  6475. prop->children = NULL;
  6476. prop->last = NULL;
  6477. prop->ns = ns;
  6478. if (value != NULL) {
  6479. xmlNodePtr tmp;
  6480. prop->children = xmlNewDocText(node->doc, value);
  6481. prop->last = NULL;
  6482. tmp = prop->children;
  6483. while (tmp != NULL) {
  6484. tmp->parent = (xmlNodePtr) prop;
  6485. if (tmp->next == NULL)
  6486. prop->last = tmp;
  6487. tmp = tmp->next;
  6488. }
  6489. }
  6490. if (prop->atype == XML_ATTRIBUTE_ID)
  6491. xmlAddID(NULL, node->doc, value, prop);
  6492. return(prop);
  6493. }
  6494. /*
  6495. * No equal attr found; create a new one.
  6496. */
  6497. return(xmlNewPropInternal(node, ns, name, value, 0));
  6498. }
  6499. #endif /* LIBXML_TREE_ENABLED */
  6500. /**
  6501. * xmlNodeIsText:
  6502. * @node: the node
  6503. *
  6504. * Is this node a Text node ?
  6505. * Returns 1 yes, 0 no
  6506. */
  6507. int
  6508. xmlNodeIsText(const xmlNode *node) {
  6509. if (node == NULL) return(0);
  6510. if (node->type == XML_TEXT_NODE) return(1);
  6511. return(0);
  6512. }
  6513. /**
  6514. * xmlIsBlankNode:
  6515. * @node: the node
  6516. *
  6517. * Checks whether this node is an empty or whitespace only
  6518. * (and possibly ignorable) text-node.
  6519. *
  6520. * Returns 1 yes, 0 no
  6521. */
  6522. int
  6523. xmlIsBlankNode(const xmlNode *node) {
  6524. const xmlChar *cur;
  6525. if (node == NULL) return(0);
  6526. if ((node->type != XML_TEXT_NODE) &&
  6527. (node->type != XML_CDATA_SECTION_NODE))
  6528. return(0);
  6529. if (node->content == NULL) return(1);
  6530. cur = node->content;
  6531. while (*cur != 0) {
  6532. if (!IS_BLANK_CH(*cur)) return(0);
  6533. cur++;
  6534. }
  6535. return(1);
  6536. }
  6537. /**
  6538. * xmlTextConcat:
  6539. * @node: the node
  6540. * @content: the content
  6541. * @len: @content length
  6542. *
  6543. * Concat the given string at the end of the existing node content
  6544. *
  6545. * Returns -1 in case of error, 0 otherwise
  6546. */
  6547. int
  6548. xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
  6549. if (node == NULL) return(-1);
  6550. if ((node->type != XML_TEXT_NODE) &&
  6551. (node->type != XML_CDATA_SECTION_NODE) &&
  6552. (node->type != XML_COMMENT_NODE) &&
  6553. (node->type != XML_PI_NODE)) {
  6554. #ifdef DEBUG_TREE
  6555. xmlGenericError(xmlGenericErrorContext,
  6556. "xmlTextConcat: node is not text nor CDATA\n");
  6557. #endif
  6558. return(-1);
  6559. }
  6560. /* need to check if content is currently in the dictionary */
  6561. if ((node->content == (xmlChar *) &(node->properties)) ||
  6562. ((node->doc != NULL) && (node->doc->dict != NULL) &&
  6563. xmlDictOwns(node->doc->dict, node->content))) {
  6564. node->content = xmlStrncatNew(node->content, content, len);
  6565. } else {
  6566. node->content = xmlStrncat(node->content, content, len);
  6567. }
  6568. node->properties = NULL;
  6569. if (node->content == NULL)
  6570. return(-1);
  6571. return(0);
  6572. }
  6573. /************************************************************************
  6574. * *
  6575. * Output : to a FILE or in memory *
  6576. * *
  6577. ************************************************************************/
  6578. /**
  6579. * xmlBufferCreate:
  6580. *
  6581. * routine to create an XML buffer.
  6582. * returns the new structure.
  6583. */
  6584. xmlBufferPtr
  6585. xmlBufferCreate(void) {
  6586. xmlBufferPtr ret;
  6587. ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
  6588. if (ret == NULL) {
  6589. xmlTreeErrMemory("creating buffer");
  6590. return(NULL);
  6591. }
  6592. ret->use = 0;
  6593. ret->size = xmlDefaultBufferSize;
  6594. ret->alloc = xmlBufferAllocScheme;
  6595. ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
  6596. if (ret->content == NULL) {
  6597. xmlTreeErrMemory("creating buffer");
  6598. xmlFree(ret);
  6599. return(NULL);
  6600. }
  6601. ret->content[0] = 0;
  6602. ret->contentIO = NULL;
  6603. return(ret);
  6604. }
  6605. /**
  6606. * xmlBufferCreateSize:
  6607. * @size: initial size of buffer
  6608. *
  6609. * routine to create an XML buffer.
  6610. * returns the new structure.
  6611. */
  6612. xmlBufferPtr
  6613. xmlBufferCreateSize(size_t size) {
  6614. xmlBufferPtr ret;
  6615. if (size >= UINT_MAX)
  6616. return(NULL);
  6617. ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
  6618. if (ret == NULL) {
  6619. xmlTreeErrMemory("creating buffer");
  6620. return(NULL);
  6621. }
  6622. ret->use = 0;
  6623. ret->alloc = xmlBufferAllocScheme;
  6624. ret->size = (size ? size + 1 : 0); /* +1 for ending null */
  6625. if (ret->size){
  6626. ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
  6627. if (ret->content == NULL) {
  6628. xmlTreeErrMemory("creating buffer");
  6629. xmlFree(ret);
  6630. return(NULL);
  6631. }
  6632. ret->content[0] = 0;
  6633. } else
  6634. ret->content = NULL;
  6635. ret->contentIO = NULL;
  6636. return(ret);
  6637. }
  6638. /**
  6639. * xmlBufferDetach:
  6640. * @buf: the buffer
  6641. *
  6642. * Remove the string contained in a buffer and gie it back to the
  6643. * caller. The buffer is reset to an empty content.
  6644. * This doesn't work with immutable buffers as they can't be reset.
  6645. *
  6646. * Returns the previous string contained by the buffer.
  6647. */
  6648. xmlChar *
  6649. xmlBufferDetach(xmlBufferPtr buf) {
  6650. xmlChar *ret;
  6651. if (buf == NULL)
  6652. return(NULL);
  6653. if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
  6654. return(NULL);
  6655. ret = buf->content;
  6656. buf->content = NULL;
  6657. buf->size = 0;
  6658. buf->use = 0;
  6659. return ret;
  6660. }
  6661. /**
  6662. * xmlBufferCreateStatic:
  6663. * @mem: the memory area
  6664. * @size: the size in byte
  6665. *
  6666. * routine to create an XML buffer from an immutable memory area.
  6667. * The area won't be modified nor copied, and is expected to be
  6668. * present until the end of the buffer lifetime.
  6669. *
  6670. * returns the new structure.
  6671. */
  6672. xmlBufferPtr
  6673. xmlBufferCreateStatic(void *mem, size_t size) {
  6674. xmlBufferPtr ret;
  6675. if ((mem == NULL) || (size == 0))
  6676. return(NULL);
  6677. if (size > UINT_MAX)
  6678. return(NULL);
  6679. ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
  6680. if (ret == NULL) {
  6681. xmlTreeErrMemory("creating buffer");
  6682. return(NULL);
  6683. }
  6684. ret->use = size;
  6685. ret->size = size;
  6686. ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
  6687. ret->content = (xmlChar *) mem;
  6688. return(ret);
  6689. }
  6690. /**
  6691. * xmlBufferSetAllocationScheme:
  6692. * @buf: the buffer to tune
  6693. * @scheme: allocation scheme to use
  6694. *
  6695. * Sets the allocation scheme for this buffer
  6696. */
  6697. void
  6698. xmlBufferSetAllocationScheme(xmlBufferPtr buf,
  6699. xmlBufferAllocationScheme scheme) {
  6700. if (buf == NULL) {
  6701. #ifdef DEBUG_BUFFER
  6702. xmlGenericError(xmlGenericErrorContext,
  6703. "xmlBufferSetAllocationScheme: buf == NULL\n");
  6704. #endif
  6705. return;
  6706. }
  6707. if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
  6708. (buf->alloc == XML_BUFFER_ALLOC_IO)) return;
  6709. if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
  6710. (scheme == XML_BUFFER_ALLOC_EXACT) ||
  6711. (scheme == XML_BUFFER_ALLOC_HYBRID) ||
  6712. (scheme == XML_BUFFER_ALLOC_IMMUTABLE))
  6713. buf->alloc = scheme;
  6714. }
  6715. /**
  6716. * xmlBufferFree:
  6717. * @buf: the buffer to free
  6718. *
  6719. * Frees an XML buffer. It frees both the content and the structure which
  6720. * encapsulate it.
  6721. */
  6722. void
  6723. xmlBufferFree(xmlBufferPtr buf) {
  6724. if (buf == NULL) {
  6725. #ifdef DEBUG_BUFFER
  6726. xmlGenericError(xmlGenericErrorContext,
  6727. "xmlBufferFree: buf == NULL\n");
  6728. #endif
  6729. return;
  6730. }
  6731. if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
  6732. (buf->contentIO != NULL)) {
  6733. xmlFree(buf->contentIO);
  6734. } else if ((buf->content != NULL) &&
  6735. (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
  6736. xmlFree(buf->content);
  6737. }
  6738. xmlFree(buf);
  6739. }
  6740. /**
  6741. * xmlBufferEmpty:
  6742. * @buf: the buffer
  6743. *
  6744. * empty a buffer.
  6745. */
  6746. void
  6747. xmlBufferEmpty(xmlBufferPtr buf) {
  6748. if (buf == NULL) return;
  6749. if (buf->content == NULL) return;
  6750. buf->use = 0;
  6751. if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
  6752. buf->content = BAD_CAST "";
  6753. } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
  6754. (buf->contentIO != NULL)) {
  6755. size_t start_buf = buf->content - buf->contentIO;
  6756. buf->size += start_buf;
  6757. buf->content = buf->contentIO;
  6758. buf->content[0] = 0;
  6759. } else {
  6760. buf->content[0] = 0;
  6761. }
  6762. }
  6763. /**
  6764. * xmlBufferShrink:
  6765. * @buf: the buffer to dump
  6766. * @len: the number of xmlChar to remove
  6767. *
  6768. * Remove the beginning of an XML buffer.
  6769. *
  6770. * Returns the number of #xmlChar removed, or -1 in case of failure.
  6771. */
  6772. int
  6773. xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
  6774. if (buf == NULL) return(-1);
  6775. if (len == 0) return(0);
  6776. if (len > buf->use) return(-1);
  6777. buf->use -= len;
  6778. if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
  6779. ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) {
  6780. /*
  6781. * we just move the content pointer, but also make sure
  6782. * the perceived buffer size has shrunk accordingly
  6783. */
  6784. buf->content += len;
  6785. buf->size -= len;
  6786. /*
  6787. * sometimes though it maybe be better to really shrink
  6788. * on IO buffers
  6789. */
  6790. if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
  6791. size_t start_buf = buf->content - buf->contentIO;
  6792. if (start_buf >= buf->size) {
  6793. memmove(buf->contentIO, &buf->content[0], buf->use);
  6794. buf->content = buf->contentIO;
  6795. buf->content[buf->use] = 0;
  6796. buf->size += start_buf;
  6797. }
  6798. }
  6799. } else {
  6800. memmove(buf->content, &buf->content[len], buf->use);
  6801. buf->content[buf->use] = 0;
  6802. }
  6803. return(len);
  6804. }
  6805. /**
  6806. * xmlBufferGrow:
  6807. * @buf: the buffer
  6808. * @len: the minimum free size to allocate
  6809. *
  6810. * Grow the available space of an XML buffer.
  6811. *
  6812. * Returns the new available space or -1 in case of error
  6813. */
  6814. int
  6815. xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
  6816. unsigned int size;
  6817. xmlChar *newbuf;
  6818. if (buf == NULL) return(-1);
  6819. if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
  6820. if (len < buf->size - buf->use)
  6821. return(0);
  6822. if (len >= UINT_MAX - buf->use) {
  6823. xmlTreeErrMemory("growing buffer past UINT_MAX");
  6824. return(-1);
  6825. }
  6826. if (buf->size > (size_t) len) {
  6827. size = buf->size > UINT_MAX / 2 ? UINT_MAX : buf->size * 2;
  6828. } else {
  6829. size = buf->use + len;
  6830. size = size > UINT_MAX - 100 ? UINT_MAX : size + 100;
  6831. }
  6832. if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
  6833. size_t start_buf = buf->content - buf->contentIO;
  6834. newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size);
  6835. if (newbuf == NULL) {
  6836. xmlTreeErrMemory("growing buffer");
  6837. return(-1);
  6838. }
  6839. buf->contentIO = newbuf;
  6840. buf->content = newbuf + start_buf;
  6841. } else {
  6842. newbuf = (xmlChar *) xmlRealloc(buf->content, size);
  6843. if (newbuf == NULL) {
  6844. xmlTreeErrMemory("growing buffer");
  6845. return(-1);
  6846. }
  6847. buf->content = newbuf;
  6848. }
  6849. buf->size = size;
  6850. return(buf->size - buf->use - 1);
  6851. }
  6852. /**
  6853. * xmlBufferDump:
  6854. * @file: the file output
  6855. * @buf: the buffer to dump
  6856. *
  6857. * Dumps an XML buffer to a FILE *.
  6858. * Returns the number of #xmlChar written
  6859. */
  6860. int
  6861. xmlBufferDump(FILE *file, xmlBufferPtr buf) {
  6862. size_t ret;
  6863. if (buf == NULL) {
  6864. #ifdef DEBUG_BUFFER
  6865. xmlGenericError(xmlGenericErrorContext,
  6866. "xmlBufferDump: buf == NULL\n");
  6867. #endif
  6868. return(0);
  6869. }
  6870. if (buf->content == NULL) {
  6871. #ifdef DEBUG_BUFFER
  6872. xmlGenericError(xmlGenericErrorContext,
  6873. "xmlBufferDump: buf->content == NULL\n");
  6874. #endif
  6875. return(0);
  6876. }
  6877. if (file == NULL)
  6878. file = stdout;
  6879. ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
  6880. return(ret > INT_MAX ? INT_MAX : (int)ret);
  6881. }
  6882. /**
  6883. * xmlBufferContent:
  6884. * @buf: the buffer
  6885. *
  6886. * Function to extract the content of a buffer
  6887. *
  6888. * Returns the internal content
  6889. */
  6890. const xmlChar *
  6891. xmlBufferContent(const xmlBuffer *buf)
  6892. {
  6893. if(!buf)
  6894. return NULL;
  6895. return buf->content;
  6896. }
  6897. /**
  6898. * xmlBufferLength:
  6899. * @buf: the buffer
  6900. *
  6901. * Function to get the length of a buffer
  6902. *
  6903. * Returns the length of data in the internal content
  6904. */
  6905. int
  6906. xmlBufferLength(const xmlBuffer *buf)
  6907. {
  6908. if(!buf)
  6909. return 0;
  6910. return buf->use;
  6911. }
  6912. /**
  6913. * xmlBufferResize:
  6914. * @buf: the buffer to resize
  6915. * @size: the desired size
  6916. *
  6917. * Resize a buffer to accommodate minimum size of @size.
  6918. *
  6919. * Returns 0 in case of problems, 1 otherwise
  6920. */
  6921. int
  6922. xmlBufferResize(xmlBufferPtr buf, unsigned int size)
  6923. {
  6924. unsigned int newSize;
  6925. xmlChar* rebuf = NULL;
  6926. size_t start_buf;
  6927. if (buf == NULL)
  6928. return(0);
  6929. if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
  6930. /* Don't resize if we don't have to */
  6931. if (size < buf->size)
  6932. return 1;
  6933. if (size > UINT_MAX - 10) {
  6934. xmlTreeErrMemory("growing buffer past UINT_MAX");
  6935. return 0;
  6936. }
  6937. /* figure out new size */
  6938. switch (buf->alloc){
  6939. case XML_BUFFER_ALLOC_IO:
  6940. case XML_BUFFER_ALLOC_DOUBLEIT:
  6941. /*take care of empty case*/
  6942. if (buf->size == 0)
  6943. newSize = (size > UINT_MAX - 10 ? UINT_MAX : size + 10);
  6944. else
  6945. newSize = buf->size;
  6946. while (size > newSize) {
  6947. if (newSize > UINT_MAX / 2) {
  6948. xmlTreeErrMemory("growing buffer");
  6949. return 0;
  6950. }
  6951. newSize *= 2;
  6952. }
  6953. break;
  6954. case XML_BUFFER_ALLOC_EXACT:
  6955. newSize = (size > UINT_MAX - 10 ? UINT_MAX : size + 10);
  6956. break;
  6957. case XML_BUFFER_ALLOC_HYBRID:
  6958. if (buf->use < BASE_BUFFER_SIZE)
  6959. newSize = size;
  6960. else {
  6961. newSize = buf->size;
  6962. while (size > newSize) {
  6963. if (newSize > UINT_MAX / 2) {
  6964. xmlTreeErrMemory("growing buffer");
  6965. return 0;
  6966. }
  6967. newSize *= 2;
  6968. }
  6969. }
  6970. break;
  6971. default:
  6972. newSize = (size > UINT_MAX - 10 ? UINT_MAX : size + 10);
  6973. break;
  6974. }
  6975. if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
  6976. start_buf = buf->content - buf->contentIO;
  6977. if (start_buf > newSize) {
  6978. /* move data back to start */
  6979. memmove(buf->contentIO, buf->content, buf->use);
  6980. buf->content = buf->contentIO;
  6981. buf->content[buf->use] = 0;
  6982. buf->size += start_buf;
  6983. } else {
  6984. rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize);
  6985. if (rebuf == NULL) {
  6986. xmlTreeErrMemory("growing buffer");
  6987. return 0;
  6988. }
  6989. buf->contentIO = rebuf;
  6990. buf->content = rebuf + start_buf;
  6991. }
  6992. } else {
  6993. if (buf->content == NULL) {
  6994. rebuf = (xmlChar *) xmlMallocAtomic(newSize);
  6995. buf->use = 0;
  6996. rebuf[buf->use] = 0;
  6997. } else if (buf->size - buf->use < 100) {
  6998. rebuf = (xmlChar *) xmlRealloc(buf->content, newSize);
  6999. } else {
  7000. /*
  7001. * if we are reallocating a buffer far from being full, it's
  7002. * better to make a new allocation and copy only the used range
  7003. * and free the old one.
  7004. */
  7005. rebuf = (xmlChar *) xmlMallocAtomic(newSize);
  7006. if (rebuf != NULL) {
  7007. memcpy(rebuf, buf->content, buf->use);
  7008. xmlFree(buf->content);
  7009. rebuf[buf->use] = 0;
  7010. }
  7011. }
  7012. if (rebuf == NULL) {
  7013. xmlTreeErrMemory("growing buffer");
  7014. return 0;
  7015. }
  7016. buf->content = rebuf;
  7017. }
  7018. buf->size = newSize;
  7019. return 1;
  7020. }
  7021. /**
  7022. * xmlBufferAdd:
  7023. * @buf: the buffer to dump
  7024. * @str: the #xmlChar string
  7025. * @len: the number of #xmlChar to add
  7026. *
  7027. * Add a string range to an XML buffer. if len == -1, the length of
  7028. * str is recomputed.
  7029. *
  7030. * Returns 0 successful, a positive error code number otherwise
  7031. * and -1 in case of internal or API error.
  7032. */
  7033. int
  7034. xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
  7035. unsigned int needSize;
  7036. if ((str == NULL) || (buf == NULL)) {
  7037. return -1;
  7038. }
  7039. if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
  7040. if (len < -1) {
  7041. #ifdef DEBUG_BUFFER
  7042. xmlGenericError(xmlGenericErrorContext,
  7043. "xmlBufferAdd: len < 0\n");
  7044. #endif
  7045. return -1;
  7046. }
  7047. if (len == 0) return 0;
  7048. if (len < 0)
  7049. len = xmlStrlen(str);
  7050. if (len < 0) return -1;
  7051. if (len == 0) return 0;
  7052. /* Note that both buf->size and buf->use can be zero here. */
  7053. if ((unsigned) len >= buf->size - buf->use) {
  7054. if ((unsigned) len >= UINT_MAX - buf->use) {
  7055. xmlTreeErrMemory("growing buffer past UINT_MAX");
  7056. return XML_ERR_NO_MEMORY;
  7057. }
  7058. needSize = buf->use + len + 1;
  7059. if (!xmlBufferResize(buf, needSize)){
  7060. xmlTreeErrMemory("growing buffer");
  7061. return XML_ERR_NO_MEMORY;
  7062. }
  7063. }
  7064. memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
  7065. buf->use += len;
  7066. buf->content[buf->use] = 0;
  7067. return 0;
  7068. }
  7069. /**
  7070. * xmlBufferAddHead:
  7071. * @buf: the buffer
  7072. * @str: the #xmlChar string
  7073. * @len: the number of #xmlChar to add
  7074. *
  7075. * Add a string range to the beginning of an XML buffer.
  7076. * if len == -1, the length of @str is recomputed.
  7077. *
  7078. * Returns 0 successful, a positive error code number otherwise
  7079. * and -1 in case of internal or API error.
  7080. */
  7081. int
  7082. xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
  7083. unsigned int needSize;
  7084. if (buf == NULL)
  7085. return(-1);
  7086. if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
  7087. if (str == NULL) {
  7088. #ifdef DEBUG_BUFFER
  7089. xmlGenericError(xmlGenericErrorContext,
  7090. "xmlBufferAddHead: str == NULL\n");
  7091. #endif
  7092. return -1;
  7093. }
  7094. if (len < -1) {
  7095. #ifdef DEBUG_BUFFER
  7096. xmlGenericError(xmlGenericErrorContext,
  7097. "xmlBufferAddHead: len < 0\n");
  7098. #endif
  7099. return -1;
  7100. }
  7101. if (len == 0) return 0;
  7102. if (len < 0)
  7103. len = xmlStrlen(str);
  7104. if (len <= 0) return -1;
  7105. if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
  7106. size_t start_buf = buf->content - buf->contentIO;
  7107. if (start_buf > (unsigned int) len) {
  7108. /*
  7109. * We can add it in the space previously shrunk
  7110. */
  7111. buf->content -= len;
  7112. memmove(&buf->content[0], str, len);
  7113. buf->use += len;
  7114. buf->size += len;
  7115. buf->content[buf->use] = 0;
  7116. return(0);
  7117. }
  7118. }
  7119. /* Note that both buf->size and buf->use can be zero here. */
  7120. if ((unsigned) len >= buf->size - buf->use) {
  7121. if ((unsigned) len >= UINT_MAX - buf->use) {
  7122. xmlTreeErrMemory("growing buffer past UINT_MAX");
  7123. return(-1);
  7124. }
  7125. needSize = buf->use + len + 1;
  7126. if (!xmlBufferResize(buf, needSize)){
  7127. xmlTreeErrMemory("growing buffer");
  7128. return XML_ERR_NO_MEMORY;
  7129. }
  7130. }
  7131. memmove(&buf->content[len], &buf->content[0], buf->use);
  7132. memmove(&buf->content[0], str, len);
  7133. buf->use += len;
  7134. buf->content[buf->use] = 0;
  7135. return 0;
  7136. }
  7137. /**
  7138. * xmlBufferCat:
  7139. * @buf: the buffer to add to
  7140. * @str: the #xmlChar string
  7141. *
  7142. * Append a zero terminated string to an XML buffer.
  7143. *
  7144. * Returns 0 successful, a positive error code number otherwise
  7145. * and -1 in case of internal or API error.
  7146. */
  7147. int
  7148. xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
  7149. if (buf == NULL)
  7150. return(-1);
  7151. if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
  7152. if (str == NULL) return -1;
  7153. return xmlBufferAdd(buf, str, -1);
  7154. }
  7155. /**
  7156. * xmlBufferCCat:
  7157. * @buf: the buffer to dump
  7158. * @str: the C char string
  7159. *
  7160. * Append a zero terminated C string to an XML buffer.
  7161. *
  7162. * Returns 0 successful, a positive error code number otherwise
  7163. * and -1 in case of internal or API error.
  7164. */
  7165. int
  7166. xmlBufferCCat(xmlBufferPtr buf, const char *str) {
  7167. return xmlBufferCat(buf, (const xmlChar *) str);
  7168. }
  7169. /**
  7170. * xmlBufferWriteCHAR:
  7171. * @buf: the XML buffer
  7172. * @string: the string to add
  7173. *
  7174. * routine which manages and grows an output buffer. This one adds
  7175. * xmlChars at the end of the buffer.
  7176. */
  7177. void
  7178. xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
  7179. if (buf == NULL)
  7180. return;
  7181. if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
  7182. xmlBufferCat(buf, string);
  7183. }
  7184. /**
  7185. * xmlBufferWriteChar:
  7186. * @buf: the XML buffer output
  7187. * @string: the string to add
  7188. *
  7189. * routine which manage and grows an output buffer. This one add
  7190. * C chars at the end of the array.
  7191. */
  7192. void
  7193. xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
  7194. if (buf == NULL)
  7195. return;
  7196. if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
  7197. xmlBufferCCat(buf, string);
  7198. }
  7199. /**
  7200. * xmlBufferWriteQuotedString:
  7201. * @buf: the XML buffer output
  7202. * @string: the string to add
  7203. *
  7204. * routine which manage and grows an output buffer. This one writes
  7205. * a quoted or double quoted #xmlChar string, checking first if it holds
  7206. * quote or double-quotes internally
  7207. */
  7208. void
  7209. xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
  7210. const xmlChar *cur, *base;
  7211. if (buf == NULL)
  7212. return;
  7213. if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
  7214. if (xmlStrchr(string, '\"')) {
  7215. if (xmlStrchr(string, '\'')) {
  7216. #ifdef DEBUG_BUFFER
  7217. xmlGenericError(xmlGenericErrorContext,
  7218. "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
  7219. #endif
  7220. xmlBufferCCat(buf, "\"");
  7221. base = cur = string;
  7222. while(*cur != 0){
  7223. if(*cur == '"'){
  7224. if (base != cur)
  7225. xmlBufferAdd(buf, base, cur - base);
  7226. xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
  7227. cur++;
  7228. base = cur;
  7229. }
  7230. else {
  7231. cur++;
  7232. }
  7233. }
  7234. if (base != cur)
  7235. xmlBufferAdd(buf, base, cur - base);
  7236. xmlBufferCCat(buf, "\"");
  7237. }
  7238. else{
  7239. xmlBufferCCat(buf, "\'");
  7240. xmlBufferCat(buf, string);
  7241. xmlBufferCCat(buf, "\'");
  7242. }
  7243. } else {
  7244. xmlBufferCCat(buf, "\"");
  7245. xmlBufferCat(buf, string);
  7246. xmlBufferCCat(buf, "\"");
  7247. }
  7248. }
  7249. /**
  7250. * xmlGetDocCompressMode:
  7251. * @doc: the document
  7252. *
  7253. * get the compression ratio for a document, ZLIB based
  7254. * Returns 0 (uncompressed) to 9 (max compression)
  7255. */
  7256. int
  7257. xmlGetDocCompressMode (const xmlDoc *doc) {
  7258. if (doc == NULL) return(-1);
  7259. return(doc->compression);
  7260. }
  7261. /**
  7262. * xmlSetDocCompressMode:
  7263. * @doc: the document
  7264. * @mode: the compression ratio
  7265. *
  7266. * set the compression ratio for a document, ZLIB based
  7267. * Correct values: 0 (uncompressed) to 9 (max compression)
  7268. */
  7269. void
  7270. xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
  7271. if (doc == NULL) return;
  7272. if (mode < 0) doc->compression = 0;
  7273. else if (mode > 9) doc->compression = 9;
  7274. else doc->compression = mode;
  7275. }
  7276. /**
  7277. * xmlGetCompressMode:
  7278. *
  7279. * get the default compression mode used, ZLIB based.
  7280. * Returns 0 (uncompressed) to 9 (max compression)
  7281. */
  7282. int
  7283. xmlGetCompressMode(void)
  7284. {
  7285. return (xmlCompressMode);
  7286. }
  7287. /**
  7288. * xmlSetCompressMode:
  7289. * @mode: the compression ratio
  7290. *
  7291. * set the default compression mode used, ZLIB based
  7292. * Correct values: 0 (uncompressed) to 9 (max compression)
  7293. */
  7294. void
  7295. xmlSetCompressMode(int mode) {
  7296. if (mode < 0) xmlCompressMode = 0;
  7297. else if (mode > 9) xmlCompressMode = 9;
  7298. else xmlCompressMode = mode;
  7299. }
  7300. #define XML_TREE_NSMAP_PARENT -1
  7301. #define XML_TREE_NSMAP_XML -2
  7302. #define XML_TREE_NSMAP_DOC -3
  7303. #define XML_TREE_NSMAP_CUSTOM -4
  7304. typedef struct xmlNsMapItem *xmlNsMapItemPtr;
  7305. struct xmlNsMapItem {
  7306. xmlNsMapItemPtr next;
  7307. xmlNsMapItemPtr prev;
  7308. xmlNsPtr oldNs; /* old ns decl reference */
  7309. xmlNsPtr newNs; /* new ns decl reference */
  7310. int shadowDepth; /* Shadowed at this depth */
  7311. /*
  7312. * depth:
  7313. * >= 0 == @node's ns-decls
  7314. * -1 == @parent's ns-decls
  7315. * -2 == the doc->oldNs XML ns-decl
  7316. * -3 == the doc->oldNs storage ns-decls
  7317. * -4 == ns-decls provided via custom ns-handling
  7318. */
  7319. int depth;
  7320. };
  7321. typedef struct xmlNsMap *xmlNsMapPtr;
  7322. struct xmlNsMap {
  7323. xmlNsMapItemPtr first;
  7324. xmlNsMapItemPtr last;
  7325. xmlNsMapItemPtr pool;
  7326. };
  7327. #define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL))
  7328. #define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next)
  7329. #define XML_NSMAP_POP(m, i) \
  7330. i = (m)->last; \
  7331. (m)->last = (i)->prev; \
  7332. if ((m)->last == NULL) \
  7333. (m)->first = NULL; \
  7334. else \
  7335. (m)->last->next = NULL; \
  7336. (i)->next = (m)->pool; \
  7337. (m)->pool = i;
  7338. /*
  7339. * xmlDOMWrapNsMapFree:
  7340. * @map: the ns-map
  7341. *
  7342. * Frees the ns-map
  7343. */
  7344. static void
  7345. xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap)
  7346. {
  7347. xmlNsMapItemPtr cur, tmp;
  7348. if (nsmap == NULL)
  7349. return;
  7350. cur = nsmap->pool;
  7351. while (cur != NULL) {
  7352. tmp = cur;
  7353. cur = cur->next;
  7354. xmlFree(tmp);
  7355. }
  7356. cur = nsmap->first;
  7357. while (cur != NULL) {
  7358. tmp = cur;
  7359. cur = cur->next;
  7360. xmlFree(tmp);
  7361. }
  7362. xmlFree(nsmap);
  7363. }
  7364. /*
  7365. * xmlDOMWrapNsMapAddItem:
  7366. * @map: the ns-map
  7367. * @oldNs: the old ns-struct
  7368. * @newNs: the new ns-struct
  7369. * @depth: depth and ns-kind information
  7370. *
  7371. * Adds an ns-mapping item.
  7372. */
  7373. static xmlNsMapItemPtr
  7374. xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position,
  7375. xmlNsPtr oldNs, xmlNsPtr newNs, int depth)
  7376. {
  7377. xmlNsMapItemPtr ret;
  7378. xmlNsMapPtr map;
  7379. if (nsmap == NULL)
  7380. return(NULL);
  7381. if ((position != -1) && (position != 0))
  7382. return(NULL);
  7383. map = *nsmap;
  7384. if (map == NULL) {
  7385. /*
  7386. * Create the ns-map.
  7387. */
  7388. map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap));
  7389. if (map == NULL) {
  7390. xmlTreeErrMemory("allocating namespace map");
  7391. return (NULL);
  7392. }
  7393. memset(map, 0, sizeof(struct xmlNsMap));
  7394. *nsmap = map;
  7395. }
  7396. if (map->pool != NULL) {
  7397. /*
  7398. * Reuse an item from the pool.
  7399. */
  7400. ret = map->pool;
  7401. map->pool = ret->next;
  7402. memset(ret, 0, sizeof(struct xmlNsMapItem));
  7403. } else {
  7404. /*
  7405. * Create a new item.
  7406. */
  7407. ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem));
  7408. if (ret == NULL) {
  7409. xmlTreeErrMemory("allocating namespace map item");
  7410. return (NULL);
  7411. }
  7412. memset(ret, 0, sizeof(struct xmlNsMapItem));
  7413. }
  7414. if (map->first == NULL) {
  7415. /*
  7416. * First ever.
  7417. */
  7418. map->first = ret;
  7419. map->last = ret;
  7420. } else if (position == -1) {
  7421. /*
  7422. * Append.
  7423. */
  7424. ret->prev = map->last;
  7425. map->last->next = ret;
  7426. map->last = ret;
  7427. } else if (position == 0) {
  7428. /*
  7429. * Set on first position.
  7430. */
  7431. map->first->prev = ret;
  7432. ret->next = map->first;
  7433. map->first = ret;
  7434. }
  7435. ret->oldNs = oldNs;
  7436. ret->newNs = newNs;
  7437. ret->shadowDepth = -1;
  7438. ret->depth = depth;
  7439. return (ret);
  7440. }
  7441. /*
  7442. * xmlDOMWrapStoreNs:
  7443. * @doc: the doc
  7444. * @nsName: the namespace name
  7445. * @prefix: the prefix
  7446. *
  7447. * Creates or reuses an xmlNs struct on doc->oldNs with
  7448. * the given prefix and namespace name.
  7449. *
  7450. * Returns the acquired ns struct or NULL in case of an API
  7451. * or internal error.
  7452. */
  7453. static xmlNsPtr
  7454. xmlDOMWrapStoreNs(xmlDocPtr doc,
  7455. const xmlChar *nsName,
  7456. const xmlChar *prefix)
  7457. {
  7458. xmlNsPtr ns;
  7459. if (doc == NULL)
  7460. return (NULL);
  7461. ns = xmlTreeEnsureXMLDecl(doc);
  7462. if (ns == NULL)
  7463. return (NULL);
  7464. if (ns->next != NULL) {
  7465. /* Reuse. */
  7466. ns = ns->next;
  7467. while (ns != NULL) {
  7468. if (((ns->prefix == prefix) ||
  7469. xmlStrEqual(ns->prefix, prefix)) &&
  7470. xmlStrEqual(ns->href, nsName)) {
  7471. return (ns);
  7472. }
  7473. if (ns->next == NULL)
  7474. break;
  7475. ns = ns->next;
  7476. }
  7477. }
  7478. /* Create. */
  7479. if (ns != NULL) {
  7480. ns->next = xmlNewNs(NULL, nsName, prefix);
  7481. return (ns->next);
  7482. }
  7483. return(NULL);
  7484. }
  7485. /*
  7486. * xmlDOMWrapNewCtxt:
  7487. *
  7488. * Allocates and initializes a new DOM-wrapper context.
  7489. *
  7490. * Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal error.
  7491. */
  7492. xmlDOMWrapCtxtPtr
  7493. xmlDOMWrapNewCtxt(void)
  7494. {
  7495. xmlDOMWrapCtxtPtr ret;
  7496. ret = xmlMalloc(sizeof(xmlDOMWrapCtxt));
  7497. if (ret == NULL) {
  7498. xmlTreeErrMemory("allocating DOM-wrapper context");
  7499. return (NULL);
  7500. }
  7501. memset(ret, 0, sizeof(xmlDOMWrapCtxt));
  7502. return (ret);
  7503. }
  7504. /*
  7505. * xmlDOMWrapFreeCtxt:
  7506. * @ctxt: the DOM-wrapper context
  7507. *
  7508. * Frees the DOM-wrapper context.
  7509. */
  7510. void
  7511. xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt)
  7512. {
  7513. if (ctxt == NULL)
  7514. return;
  7515. if (ctxt->namespaceMap != NULL)
  7516. xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap);
  7517. /*
  7518. * TODO: Store the namespace map in the context.
  7519. */
  7520. xmlFree(ctxt);
  7521. }
  7522. /*
  7523. * xmlTreeLookupNsListByPrefix:
  7524. * @nsList: a list of ns-structs
  7525. * @prefix: the searched prefix
  7526. *
  7527. * Searches for a ns-decl with the given prefix in @nsList.
  7528. *
  7529. * Returns the ns-decl if found, NULL if not found and on
  7530. * API errors.
  7531. */
  7532. static xmlNsPtr
  7533. xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix)
  7534. {
  7535. if (nsList == NULL)
  7536. return (NULL);
  7537. {
  7538. xmlNsPtr ns;
  7539. ns = nsList;
  7540. do {
  7541. if ((prefix == ns->prefix) ||
  7542. xmlStrEqual(prefix, ns->prefix)) {
  7543. return (ns);
  7544. }
  7545. ns = ns->next;
  7546. } while (ns != NULL);
  7547. }
  7548. return (NULL);
  7549. }
  7550. /*
  7551. *
  7552. * xmlDOMWrapNSNormGatherInScopeNs:
  7553. * @map: the namespace map
  7554. * @node: the node to start with
  7555. *
  7556. * Puts in-scope namespaces into the ns-map.
  7557. *
  7558. * Returns 0 on success, -1 on API or internal errors.
  7559. */
  7560. static int
  7561. xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map,
  7562. xmlNodePtr node)
  7563. {
  7564. xmlNodePtr cur;
  7565. xmlNsPtr ns;
  7566. xmlNsMapItemPtr mi;
  7567. int shadowed;
  7568. if ((map == NULL) || (*map != NULL))
  7569. return (-1);
  7570. if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
  7571. return (-1);
  7572. /*
  7573. * Get in-scope ns-decls of @parent.
  7574. */
  7575. cur = node;
  7576. while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) {
  7577. if (cur->type == XML_ELEMENT_NODE) {
  7578. if (cur->nsDef != NULL) {
  7579. ns = cur->nsDef;
  7580. do {
  7581. shadowed = 0;
  7582. if (XML_NSMAP_NOTEMPTY(*map)) {
  7583. /*
  7584. * Skip shadowed prefixes.
  7585. */
  7586. XML_NSMAP_FOREACH(*map, mi) {
  7587. if ((ns->prefix == mi->newNs->prefix) ||
  7588. xmlStrEqual(ns->prefix, mi->newNs->prefix)) {
  7589. shadowed = 1;
  7590. break;
  7591. }
  7592. }
  7593. }
  7594. /*
  7595. * Insert mapping.
  7596. */
  7597. mi = xmlDOMWrapNsMapAddItem(map, 0, NULL,
  7598. ns, XML_TREE_NSMAP_PARENT);
  7599. if (mi == NULL)
  7600. return (-1);
  7601. if (shadowed)
  7602. mi->shadowDepth = 0;
  7603. ns = ns->next;
  7604. } while (ns != NULL);
  7605. }
  7606. }
  7607. cur = cur->parent;
  7608. }
  7609. return (0);
  7610. }
  7611. /*
  7612. * XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict;
  7613. * otherwise copy it, when it was in the source-dict.
  7614. */
  7615. #define XML_TREE_ADOPT_STR(str) \
  7616. if (adoptStr && (str != NULL)) { \
  7617. if (destDoc->dict) { \
  7618. const xmlChar *old = str; \
  7619. str = xmlDictLookup(destDoc->dict, str, -1); \
  7620. if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \
  7621. (!xmlDictOwns(sourceDoc->dict, old))) \
  7622. xmlFree((char *)old); \
  7623. } else if ((sourceDoc) && (sourceDoc->dict) && \
  7624. xmlDictOwns(sourceDoc->dict, str)) { \
  7625. str = BAD_CAST xmlStrdup(str); \
  7626. } \
  7627. }
  7628. /*
  7629. * XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then
  7630. * put it in dest-dict or copy it.
  7631. */
  7632. #define XML_TREE_ADOPT_STR_2(str) \
  7633. if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \
  7634. (sourceDoc->dict != NULL) && \
  7635. xmlDictOwns(sourceDoc->dict, cur->content)) { \
  7636. if (destDoc->dict) \
  7637. cur->content = (xmlChar *) \
  7638. xmlDictLookup(destDoc->dict, cur->content, -1); \
  7639. else \
  7640. cur->content = xmlStrdup(BAD_CAST cur->content); \
  7641. }
  7642. /*
  7643. * xmlDOMWrapNSNormAddNsMapItem2:
  7644. *
  7645. * For internal use. Adds a ns-decl mapping.
  7646. *
  7647. * Returns 0 on success, -1 on internal errors.
  7648. */
  7649. static int
  7650. xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number,
  7651. xmlNsPtr oldNs, xmlNsPtr newNs)
  7652. {
  7653. if (*list == NULL) {
  7654. *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr));
  7655. if (*list == NULL) {
  7656. xmlTreeErrMemory("alloc ns map item");
  7657. return(-1);
  7658. }
  7659. *size = 3;
  7660. *number = 0;
  7661. } else if ((*number) >= (*size)) {
  7662. *size *= 2;
  7663. *list = (xmlNsPtr *) xmlRealloc(*list,
  7664. (*size) * 2 * sizeof(xmlNsPtr));
  7665. if (*list == NULL) {
  7666. xmlTreeErrMemory("realloc ns map item");
  7667. return(-1);
  7668. }
  7669. }
  7670. (*list)[2 * (*number)] = oldNs;
  7671. (*list)[2 * (*number) +1] = newNs;
  7672. (*number)++;
  7673. return (0);
  7674. }
  7675. /*
  7676. * xmlDOMWrapRemoveNode:
  7677. * @ctxt: a DOM wrapper context
  7678. * @doc: the doc
  7679. * @node: the node to be removed.
  7680. * @options: set of options, unused at the moment
  7681. *
  7682. * Unlinks the given node from its owner.
  7683. * This will substitute ns-references to node->nsDef for
  7684. * ns-references to doc->oldNs, thus ensuring the removed
  7685. * branch to be autark wrt ns-references.
  7686. *
  7687. * NOTE: This function was not intensively tested.
  7688. *
  7689. * Returns 0 on success, 1 if the node is not supported,
  7690. * -1 on API and internal errors.
  7691. */
  7692. int
  7693. xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc,
  7694. xmlNodePtr node, int options ATTRIBUTE_UNUSED)
  7695. {
  7696. xmlNsPtr *list = NULL;
  7697. int sizeList, nbList, i, j;
  7698. xmlNsPtr ns;
  7699. if ((node == NULL) || (doc == NULL) || (node->doc != doc))
  7700. return (-1);
  7701. /* TODO: 0 or -1 ? */
  7702. if (node->parent == NULL)
  7703. return (0);
  7704. switch (node->type) {
  7705. case XML_TEXT_NODE:
  7706. case XML_CDATA_SECTION_NODE:
  7707. case XML_ENTITY_REF_NODE:
  7708. case XML_PI_NODE:
  7709. case XML_COMMENT_NODE:
  7710. xmlUnlinkNode(node);
  7711. return (0);
  7712. case XML_ELEMENT_NODE:
  7713. case XML_ATTRIBUTE_NODE:
  7714. break;
  7715. default:
  7716. return (1);
  7717. }
  7718. xmlUnlinkNode(node);
  7719. /*
  7720. * Save out-of-scope ns-references in doc->oldNs.
  7721. */
  7722. do {
  7723. switch (node->type) {
  7724. case XML_ELEMENT_NODE:
  7725. if ((ctxt == NULL) && (node->nsDef != NULL)) {
  7726. ns = node->nsDef;
  7727. do {
  7728. if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
  7729. &nbList, ns, ns) == -1)
  7730. goto internal_error;
  7731. ns = ns->next;
  7732. } while (ns != NULL);
  7733. }
  7734. /* Falls through. */
  7735. case XML_ATTRIBUTE_NODE:
  7736. if (node->ns != NULL) {
  7737. /*
  7738. * Find a mapping.
  7739. */
  7740. if (list != NULL) {
  7741. for (i = 0, j = 0; i < nbList; i++, j += 2) {
  7742. if (node->ns == list[j]) {
  7743. node->ns = list[++j];
  7744. goto next_node;
  7745. }
  7746. }
  7747. }
  7748. ns = NULL;
  7749. if (ctxt != NULL) {
  7750. /*
  7751. * User defined.
  7752. */
  7753. } else {
  7754. /*
  7755. * Add to doc's oldNs.
  7756. */
  7757. ns = xmlDOMWrapStoreNs(doc, node->ns->href,
  7758. node->ns->prefix);
  7759. if (ns == NULL)
  7760. goto internal_error;
  7761. }
  7762. if (ns != NULL) {
  7763. /*
  7764. * Add mapping.
  7765. */
  7766. if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
  7767. &nbList, node->ns, ns) == -1)
  7768. goto internal_error;
  7769. }
  7770. node->ns = ns;
  7771. }
  7772. if ((node->type == XML_ELEMENT_NODE) &&
  7773. (node->properties != NULL)) {
  7774. node = (xmlNodePtr) node->properties;
  7775. continue;
  7776. }
  7777. break;
  7778. default:
  7779. goto next_sibling;
  7780. }
  7781. next_node:
  7782. if ((node->type == XML_ELEMENT_NODE) &&
  7783. (node->children != NULL)) {
  7784. node = node->children;
  7785. continue;
  7786. }
  7787. next_sibling:
  7788. if (node == NULL)
  7789. break;
  7790. if (node->next != NULL)
  7791. node = node->next;
  7792. else {
  7793. node = node->parent;
  7794. goto next_sibling;
  7795. }
  7796. } while (node != NULL);
  7797. if (list != NULL)
  7798. xmlFree(list);
  7799. return (0);
  7800. internal_error:
  7801. if (list != NULL)
  7802. xmlFree(list);
  7803. return (-1);
  7804. }
  7805. /*
  7806. * xmlSearchNsByNamespaceStrict:
  7807. * @doc: the document
  7808. * @node: the start node
  7809. * @nsName: the searched namespace name
  7810. * @retNs: the resulting ns-decl
  7811. * @prefixed: if the found ns-decl must have a prefix (for attributes)
  7812. *
  7813. * Dynamically searches for a ns-declaration which matches
  7814. * the given @nsName in the ancestor-or-self axis of @node.
  7815. *
  7816. * Returns 1 if a ns-decl was found, 0 if not and -1 on API
  7817. * and internal errors.
  7818. */
  7819. static int
  7820. xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node,
  7821. const xmlChar* nsName,
  7822. xmlNsPtr *retNs, int prefixed)
  7823. {
  7824. xmlNodePtr cur, prev = NULL, out = NULL;
  7825. xmlNsPtr ns, prevns;
  7826. if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
  7827. return (-1);
  7828. if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
  7829. return(-1);
  7830. *retNs = NULL;
  7831. if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
  7832. *retNs = xmlTreeEnsureXMLDecl(doc);
  7833. if (*retNs == NULL)
  7834. return (-1);
  7835. return (1);
  7836. }
  7837. cur = node;
  7838. do {
  7839. if (cur->type == XML_ELEMENT_NODE) {
  7840. if (cur->nsDef != NULL) {
  7841. for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
  7842. if (prefixed && (ns->prefix == NULL))
  7843. continue;
  7844. if (prev != NULL) {
  7845. /*
  7846. * Check the last level of ns-decls for a
  7847. * shadowing prefix.
  7848. */
  7849. prevns = prev->nsDef;
  7850. do {
  7851. if ((prevns->prefix == ns->prefix) ||
  7852. ((prevns->prefix != NULL) &&
  7853. (ns->prefix != NULL) &&
  7854. xmlStrEqual(prevns->prefix, ns->prefix))) {
  7855. /*
  7856. * Shadowed.
  7857. */
  7858. break;
  7859. }
  7860. prevns = prevns->next;
  7861. } while (prevns != NULL);
  7862. if (prevns != NULL)
  7863. continue;
  7864. }
  7865. /*
  7866. * Ns-name comparison.
  7867. */
  7868. if ((nsName == ns->href) ||
  7869. xmlStrEqual(nsName, ns->href)) {
  7870. /*
  7871. * At this point the prefix can only be shadowed,
  7872. * if we are the the (at least) 3rd level of
  7873. * ns-decls.
  7874. */
  7875. if (out) {
  7876. int ret;
  7877. ret = xmlNsInScope(doc, node, prev, ns->prefix);
  7878. if (ret < 0)
  7879. return (-1);
  7880. /*
  7881. * TODO: Should we try to find a matching ns-name
  7882. * only once? This here keeps on searching.
  7883. * I think we should try further since, there might
  7884. * be an other matching ns-decl with an unshadowed
  7885. * prefix.
  7886. */
  7887. if (! ret)
  7888. continue;
  7889. }
  7890. *retNs = ns;
  7891. return (1);
  7892. }
  7893. }
  7894. out = prev;
  7895. prev = cur;
  7896. }
  7897. } else if ((cur->type == XML_ENTITY_NODE) ||
  7898. (cur->type == XML_ENTITY_DECL))
  7899. return (0);
  7900. cur = cur->parent;
  7901. } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
  7902. return (0);
  7903. }
  7904. /*
  7905. * xmlSearchNsByPrefixStrict:
  7906. * @doc: the document
  7907. * @node: the start node
  7908. * @prefix: the searched namespace prefix
  7909. * @retNs: the resulting ns-decl
  7910. *
  7911. * Dynamically searches for a ns-declaration which matches
  7912. * the given @nsName in the ancestor-or-self axis of @node.
  7913. *
  7914. * Returns 1 if a ns-decl was found, 0 if not and -1 on API
  7915. * and internal errors.
  7916. */
  7917. static int
  7918. xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node,
  7919. const xmlChar* prefix,
  7920. xmlNsPtr *retNs)
  7921. {
  7922. xmlNodePtr cur;
  7923. xmlNsPtr ns;
  7924. if ((doc == NULL) || (node == NULL) || (node->type == XML_NAMESPACE_DECL))
  7925. return(-1);
  7926. if (retNs)
  7927. *retNs = NULL;
  7928. if (IS_STR_XML(prefix)) {
  7929. if (retNs) {
  7930. *retNs = xmlTreeEnsureXMLDecl(doc);
  7931. if (*retNs == NULL)
  7932. return (-1);
  7933. }
  7934. return (1);
  7935. }
  7936. cur = node;
  7937. do {
  7938. if (cur->type == XML_ELEMENT_NODE) {
  7939. if (cur->nsDef != NULL) {
  7940. ns = cur->nsDef;
  7941. do {
  7942. if ((prefix == ns->prefix) ||
  7943. xmlStrEqual(prefix, ns->prefix))
  7944. {
  7945. /*
  7946. * Disabled namespaces, e.g. xmlns:abc="".
  7947. */
  7948. if (ns->href == NULL)
  7949. return(0);
  7950. if (retNs)
  7951. *retNs = ns;
  7952. return (1);
  7953. }
  7954. ns = ns->next;
  7955. } while (ns != NULL);
  7956. }
  7957. } else if ((cur->type == XML_ENTITY_NODE) ||
  7958. (cur->type == XML_ENTITY_DECL))
  7959. return (0);
  7960. cur = cur->parent;
  7961. } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
  7962. return (0);
  7963. }
  7964. /*
  7965. * xmlDOMWrapNSNormDeclareNsForced:
  7966. * @doc: the doc
  7967. * @elem: the element-node to declare on
  7968. * @nsName: the namespace-name of the ns-decl
  7969. * @prefix: the preferred prefix of the ns-decl
  7970. * @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls
  7971. *
  7972. * Declares a new namespace on @elem. It tries to use the
  7973. * given @prefix; if a ns-decl with the given prefix is already existent
  7974. * on @elem, it will generate an other prefix.
  7975. *
  7976. * Returns 1 if a ns-decl was found, 0 if not and -1 on API
  7977. * and internal errors.
  7978. */
  7979. static xmlNsPtr
  7980. xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc,
  7981. xmlNodePtr elem,
  7982. const xmlChar *nsName,
  7983. const xmlChar *prefix,
  7984. int checkShadow)
  7985. {
  7986. xmlNsPtr ret;
  7987. char buf[50];
  7988. const xmlChar *pref;
  7989. int counter = 0;
  7990. if ((doc == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
  7991. return(NULL);
  7992. /*
  7993. * Create a ns-decl on @anchor.
  7994. */
  7995. pref = prefix;
  7996. while (1) {
  7997. /*
  7998. * Lookup whether the prefix is unused in elem's ns-decls.
  7999. */
  8000. if ((elem->nsDef != NULL) &&
  8001. (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL))
  8002. goto ns_next_prefix;
  8003. if (checkShadow && elem->parent &&
  8004. ((xmlNodePtr) elem->parent->doc != elem->parent)) {
  8005. /*
  8006. * Does it shadow ancestor ns-decls?
  8007. */
  8008. if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1)
  8009. goto ns_next_prefix;
  8010. }
  8011. ret = xmlNewNs(NULL, nsName, pref);
  8012. if (ret == NULL)
  8013. return (NULL);
  8014. if (elem->nsDef == NULL)
  8015. elem->nsDef = ret;
  8016. else {
  8017. xmlNsPtr ns2 = elem->nsDef;
  8018. while (ns2->next != NULL)
  8019. ns2 = ns2->next;
  8020. ns2->next = ret;
  8021. }
  8022. return (ret);
  8023. ns_next_prefix:
  8024. counter++;
  8025. if (counter > 1000)
  8026. return (NULL);
  8027. if (prefix == NULL) {
  8028. snprintf((char *) buf, sizeof(buf),
  8029. "ns_%d", counter);
  8030. } else
  8031. snprintf((char *) buf, sizeof(buf),
  8032. "%.30s_%d", (char *)prefix, counter);
  8033. pref = BAD_CAST buf;
  8034. }
  8035. }
  8036. /*
  8037. * xmlDOMWrapNSNormAcquireNormalizedNs:
  8038. * @doc: the doc
  8039. * @elem: the element-node to declare namespaces on
  8040. * @ns: the ns-struct to use for the search
  8041. * @retNs: the found/created ns-struct
  8042. * @nsMap: the ns-map
  8043. * @depth: the current tree depth
  8044. * @ancestorsOnly: search in ancestor ns-decls only
  8045. * @prefixed: if the searched ns-decl must have a prefix (for attributes)
  8046. *
  8047. * Searches for a matching ns-name in the ns-decls of @nsMap, if not
  8048. * found it will either declare it on @elem, or store it in doc->oldNs.
  8049. * If a new ns-decl needs to be declared on @elem, it tries to use the
  8050. * @ns->prefix for it, if this prefix is already in use on @elem, it will
  8051. * change the prefix or the new ns-decl.
  8052. *
  8053. * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
  8054. */
  8055. static int
  8056. xmlDOMWrapNSNormAcquireNormalizedNs(xmlDocPtr doc,
  8057. xmlNodePtr elem,
  8058. xmlNsPtr ns,
  8059. xmlNsPtr *retNs,
  8060. xmlNsMapPtr *nsMap,
  8061. int depth,
  8062. int ancestorsOnly,
  8063. int prefixed)
  8064. {
  8065. xmlNsMapItemPtr mi;
  8066. if ((doc == NULL) || (ns == NULL) || (retNs == NULL) ||
  8067. (nsMap == NULL))
  8068. return (-1);
  8069. *retNs = NULL;
  8070. /*
  8071. * Handle XML namespace.
  8072. */
  8073. if (IS_STR_XML(ns->prefix)) {
  8074. /*
  8075. * Insert XML namespace mapping.
  8076. */
  8077. *retNs = xmlTreeEnsureXMLDecl(doc);
  8078. if (*retNs == NULL)
  8079. return (-1);
  8080. return (0);
  8081. }
  8082. /*
  8083. * If the search should be done in ancestors only and no
  8084. * @elem (the first ancestor) was specified, then skip the search.
  8085. */
  8086. if ((XML_NSMAP_NOTEMPTY(*nsMap)) &&
  8087. (! (ancestorsOnly && (elem == NULL))))
  8088. {
  8089. /*
  8090. * Try to find an equal ns-name in in-scope ns-decls.
  8091. */
  8092. XML_NSMAP_FOREACH(*nsMap, mi) {
  8093. if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
  8094. /*
  8095. * ancestorsOnly: This should be turned on to gain speed,
  8096. * if one knows that the branch itself was already
  8097. * ns-wellformed and no stale references existed.
  8098. * I.e. it searches in the ancestor axis only.
  8099. */
  8100. ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) &&
  8101. /* Skip shadowed prefixes. */
  8102. (mi->shadowDepth == -1) &&
  8103. /* Skip xmlns="" or xmlns:foo="". */
  8104. ((mi->newNs->href != NULL) &&
  8105. (mi->newNs->href[0] != 0)) &&
  8106. /* Ensure a prefix if wanted. */
  8107. ((! prefixed) || (mi->newNs->prefix != NULL)) &&
  8108. /* Equal ns name */
  8109. ((mi->newNs->href == ns->href) ||
  8110. xmlStrEqual(mi->newNs->href, ns->href))) {
  8111. /* Set the mapping. */
  8112. mi->oldNs = ns;
  8113. *retNs = mi->newNs;
  8114. return (0);
  8115. }
  8116. }
  8117. }
  8118. /*
  8119. * No luck, the namespace is out of scope or shadowed.
  8120. */
  8121. if (elem == NULL) {
  8122. xmlNsPtr tmpns;
  8123. /*
  8124. * Store ns-decls in "oldNs" of the document-node.
  8125. */
  8126. tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix);
  8127. if (tmpns == NULL)
  8128. return (-1);
  8129. /*
  8130. * Insert mapping.
  8131. */
  8132. if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns,
  8133. tmpns, XML_TREE_NSMAP_DOC) == NULL) {
  8134. xmlFreeNs(tmpns);
  8135. return (-1);
  8136. }
  8137. *retNs = tmpns;
  8138. } else {
  8139. xmlNsPtr tmpns;
  8140. tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href,
  8141. ns->prefix, 0);
  8142. if (tmpns == NULL)
  8143. return (-1);
  8144. if (*nsMap != NULL) {
  8145. /*
  8146. * Does it shadow ancestor ns-decls?
  8147. */
  8148. XML_NSMAP_FOREACH(*nsMap, mi) {
  8149. if ((mi->depth < depth) &&
  8150. (mi->shadowDepth == -1) &&
  8151. ((ns->prefix == mi->newNs->prefix) ||
  8152. xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
  8153. /*
  8154. * Shadows.
  8155. */
  8156. mi->shadowDepth = depth;
  8157. break;
  8158. }
  8159. }
  8160. }
  8161. if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) {
  8162. xmlFreeNs(tmpns);
  8163. return (-1);
  8164. }
  8165. *retNs = tmpns;
  8166. }
  8167. return (0);
  8168. }
  8169. typedef enum {
  8170. XML_DOM_RECONNS_REMOVEREDUND = 1<<0
  8171. } xmlDOMReconcileNSOptions;
  8172. /*
  8173. * xmlDOMWrapReconcileNamespaces:
  8174. * @ctxt: DOM wrapper context, unused at the moment
  8175. * @elem: the element-node
  8176. * @options: option flags
  8177. *
  8178. * Ensures that ns-references point to ns-decls hold on element-nodes.
  8179. * Ensures that the tree is namespace wellformed by creating additional
  8180. * ns-decls where needed. Note that, since prefixes of already existent
  8181. * ns-decls can be shadowed by this process, it could break QNames in
  8182. * attribute values or element content.
  8183. *
  8184. * NOTE: This function was not intensively tested.
  8185. *
  8186. * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
  8187. */
  8188. int
  8189. xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED,
  8190. xmlNodePtr elem,
  8191. int options)
  8192. {
  8193. int depth = -1, adoptns = 0, parnsdone = 0;
  8194. xmlNsPtr ns, prevns;
  8195. xmlDocPtr doc;
  8196. xmlNodePtr cur, curElem = NULL;
  8197. xmlNsMapPtr nsMap = NULL;
  8198. xmlNsMapItemPtr /* topmi = NULL, */ mi;
  8199. /* @ancestorsOnly should be set by an option flag. */
  8200. int ancestorsOnly = 0;
  8201. int optRemoveRedundantNS =
  8202. ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0;
  8203. xmlNsPtr *listRedund = NULL;
  8204. int sizeRedund = 0, nbRedund = 0, ret, i, j;
  8205. if ((elem == NULL) || (elem->doc == NULL) ||
  8206. (elem->type != XML_ELEMENT_NODE))
  8207. return (-1);
  8208. doc = elem->doc;
  8209. cur = elem;
  8210. do {
  8211. switch (cur->type) {
  8212. case XML_ELEMENT_NODE:
  8213. adoptns = 1;
  8214. curElem = cur;
  8215. depth++;
  8216. /*
  8217. * Namespace declarations.
  8218. */
  8219. if (cur->nsDef != NULL) {
  8220. prevns = NULL;
  8221. ns = cur->nsDef;
  8222. while (ns != NULL) {
  8223. if (! parnsdone) {
  8224. if ((elem->parent) &&
  8225. ((xmlNodePtr) elem->parent->doc != elem->parent)) {
  8226. /*
  8227. * Gather ancestor in-scope ns-decls.
  8228. */
  8229. if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
  8230. elem->parent) == -1)
  8231. goto internal_error;
  8232. }
  8233. parnsdone = 1;
  8234. }
  8235. /*
  8236. * Lookup the ns ancestor-axis for equal ns-decls in scope.
  8237. */
  8238. if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) {
  8239. XML_NSMAP_FOREACH(nsMap, mi) {
  8240. if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
  8241. (mi->shadowDepth == -1) &&
  8242. ((ns->prefix == mi->newNs->prefix) ||
  8243. xmlStrEqual(ns->prefix, mi->newNs->prefix)) &&
  8244. ((ns->href == mi->newNs->href) ||
  8245. xmlStrEqual(ns->href, mi->newNs->href)))
  8246. {
  8247. /*
  8248. * A redundant ns-decl was found.
  8249. * Add it to the list of redundant ns-decls.
  8250. */
  8251. if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund,
  8252. &sizeRedund, &nbRedund, ns, mi->newNs) == -1)
  8253. goto internal_error;
  8254. /*
  8255. * Remove the ns-decl from the element-node.
  8256. */
  8257. if (prevns)
  8258. prevns->next = ns->next;
  8259. else
  8260. cur->nsDef = ns->next;
  8261. goto next_ns_decl;
  8262. }
  8263. }
  8264. }
  8265. /*
  8266. * Skip ns-references handling if the referenced
  8267. * ns-decl is declared on the same element.
  8268. */
  8269. if ((cur->ns != NULL) && adoptns && (cur->ns == ns))
  8270. adoptns = 0;
  8271. /*
  8272. * Does it shadow any ns-decl?
  8273. */
  8274. if (XML_NSMAP_NOTEMPTY(nsMap)) {
  8275. XML_NSMAP_FOREACH(nsMap, mi) {
  8276. if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
  8277. (mi->shadowDepth == -1) &&
  8278. ((ns->prefix == mi->newNs->prefix) ||
  8279. xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
  8280. mi->shadowDepth = depth;
  8281. }
  8282. }
  8283. }
  8284. /*
  8285. * Push mapping.
  8286. */
  8287. if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns,
  8288. depth) == NULL)
  8289. goto internal_error;
  8290. prevns = ns;
  8291. next_ns_decl:
  8292. ns = ns->next;
  8293. }
  8294. }
  8295. if (! adoptns)
  8296. goto ns_end;
  8297. /* Falls through. */
  8298. case XML_ATTRIBUTE_NODE:
  8299. /* No ns, no fun. */
  8300. if (cur->ns == NULL)
  8301. goto ns_end;
  8302. if (! parnsdone) {
  8303. if ((elem->parent) &&
  8304. ((xmlNodePtr) elem->parent->doc != elem->parent)) {
  8305. if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
  8306. elem->parent) == -1)
  8307. goto internal_error;
  8308. }
  8309. parnsdone = 1;
  8310. }
  8311. /*
  8312. * Adjust the reference if this was a redundant ns-decl.
  8313. */
  8314. if (listRedund) {
  8315. for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
  8316. if (cur->ns == listRedund[j]) {
  8317. cur->ns = listRedund[++j];
  8318. break;
  8319. }
  8320. }
  8321. }
  8322. /*
  8323. * Adopt ns-references.
  8324. */
  8325. if (XML_NSMAP_NOTEMPTY(nsMap)) {
  8326. /*
  8327. * Search for a mapping.
  8328. */
  8329. XML_NSMAP_FOREACH(nsMap, mi) {
  8330. if ((mi->shadowDepth == -1) &&
  8331. (cur->ns == mi->oldNs)) {
  8332. cur->ns = mi->newNs;
  8333. goto ns_end;
  8334. }
  8335. }
  8336. }
  8337. /*
  8338. * Acquire a normalized ns-decl and add it to the map.
  8339. */
  8340. if (xmlDOMWrapNSNormAcquireNormalizedNs(doc, curElem,
  8341. cur->ns, &ns,
  8342. &nsMap, depth,
  8343. ancestorsOnly,
  8344. (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
  8345. goto internal_error;
  8346. cur->ns = ns;
  8347. ns_end:
  8348. if ((cur->type == XML_ELEMENT_NODE) &&
  8349. (cur->properties != NULL)) {
  8350. /*
  8351. * Process attributes.
  8352. */
  8353. cur = (xmlNodePtr) cur->properties;
  8354. continue;
  8355. }
  8356. break;
  8357. default:
  8358. goto next_sibling;
  8359. }
  8360. into_content:
  8361. if ((cur->type == XML_ELEMENT_NODE) &&
  8362. (cur->children != NULL)) {
  8363. /*
  8364. * Process content of element-nodes only.
  8365. */
  8366. cur = cur->children;
  8367. continue;
  8368. }
  8369. next_sibling:
  8370. if (cur == elem)
  8371. break;
  8372. if (cur->type == XML_ELEMENT_NODE) {
  8373. if (XML_NSMAP_NOTEMPTY(nsMap)) {
  8374. /*
  8375. * Pop mappings.
  8376. */
  8377. while ((nsMap->last != NULL) &&
  8378. (nsMap->last->depth >= depth))
  8379. {
  8380. XML_NSMAP_POP(nsMap, mi)
  8381. }
  8382. /*
  8383. * Unshadow.
  8384. */
  8385. XML_NSMAP_FOREACH(nsMap, mi) {
  8386. if (mi->shadowDepth >= depth)
  8387. mi->shadowDepth = -1;
  8388. }
  8389. }
  8390. depth--;
  8391. }
  8392. if (cur->next != NULL)
  8393. cur = cur->next;
  8394. else {
  8395. if (cur->type == XML_ATTRIBUTE_NODE) {
  8396. cur = cur->parent;
  8397. goto into_content;
  8398. }
  8399. cur = cur->parent;
  8400. goto next_sibling;
  8401. }
  8402. } while (cur != NULL);
  8403. ret = 0;
  8404. goto exit;
  8405. internal_error:
  8406. ret = -1;
  8407. exit:
  8408. if (listRedund) {
  8409. for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
  8410. xmlFreeNs(listRedund[j]);
  8411. }
  8412. xmlFree(listRedund);
  8413. }
  8414. if (nsMap != NULL)
  8415. xmlDOMWrapNsMapFree(nsMap);
  8416. return (ret);
  8417. }
  8418. /*
  8419. * xmlDOMWrapAdoptBranch:
  8420. * @ctxt: the optional context for custom processing
  8421. * @sourceDoc: the optional sourceDoc
  8422. * @node: the element-node to start with
  8423. * @destDoc: the destination doc for adoption
  8424. * @destParent: the optional new parent of @node in @destDoc
  8425. * @options: option flags
  8426. *
  8427. * Ensures that ns-references point to @destDoc: either to
  8428. * elements->nsDef entries if @destParent is given, or to
  8429. * @destDoc->oldNs otherwise.
  8430. * If @destParent is given, it ensures that the tree is namespace
  8431. * wellformed by creating additional ns-decls where needed.
  8432. * Note that, since prefixes of already existent ns-decls can be
  8433. * shadowed by this process, it could break QNames in attribute
  8434. * values or element content.
  8435. *
  8436. * NOTE: This function was not intensively tested.
  8437. *
  8438. * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
  8439. */
  8440. static int
  8441. xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt,
  8442. xmlDocPtr sourceDoc,
  8443. xmlNodePtr node,
  8444. xmlDocPtr destDoc,
  8445. xmlNodePtr destParent,
  8446. int options ATTRIBUTE_UNUSED)
  8447. {
  8448. int ret = 0;
  8449. xmlNodePtr cur, curElem = NULL;
  8450. xmlNsMapPtr nsMap = NULL;
  8451. xmlNsMapItemPtr mi;
  8452. xmlNsPtr ns = NULL;
  8453. int depth = -1, adoptStr = 1;
  8454. /* gather @parent's ns-decls. */
  8455. int parnsdone;
  8456. /* @ancestorsOnly should be set per option. */
  8457. int ancestorsOnly = 0;
  8458. /*
  8459. * Optimize string adoption for equal or none dicts.
  8460. */
  8461. if ((sourceDoc != NULL) &&
  8462. (sourceDoc->dict == destDoc->dict))
  8463. adoptStr = 0;
  8464. else
  8465. adoptStr = 1;
  8466. /*
  8467. * Get the ns-map from the context if available.
  8468. */
  8469. if (ctxt)
  8470. nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
  8471. /*
  8472. * Disable search for ns-decls in the parent-axis of the
  8473. * destination element, if:
  8474. * 1) there's no destination parent
  8475. * 2) custom ns-reference handling is used
  8476. */
  8477. if ((destParent == NULL) ||
  8478. (ctxt && ctxt->getNsForNodeFunc))
  8479. {
  8480. parnsdone = 1;
  8481. } else
  8482. parnsdone = 0;
  8483. cur = node;
  8484. if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
  8485. goto internal_error;
  8486. while (cur != NULL) {
  8487. /*
  8488. * Paranoid source-doc sanity check.
  8489. */
  8490. if (cur->doc != sourceDoc) {
  8491. /*
  8492. * We'll assume XIncluded nodes if the doc differs.
  8493. * TODO: Do we need to reconciliate XIncluded nodes?
  8494. * This here skips XIncluded nodes and tries to handle
  8495. * broken sequences.
  8496. */
  8497. if (cur->next == NULL)
  8498. goto leave_node;
  8499. do {
  8500. cur = cur->next;
  8501. if ((cur->type == XML_XINCLUDE_END) ||
  8502. (cur->doc == node->doc))
  8503. break;
  8504. } while (cur->next != NULL);
  8505. if (cur->doc != node->doc)
  8506. goto leave_node;
  8507. }
  8508. cur->doc = destDoc;
  8509. switch (cur->type) {
  8510. case XML_XINCLUDE_START:
  8511. case XML_XINCLUDE_END:
  8512. /*
  8513. * TODO
  8514. */
  8515. return (-1);
  8516. case XML_ELEMENT_NODE:
  8517. curElem = cur;
  8518. depth++;
  8519. /*
  8520. * Namespace declarations.
  8521. * - ns->href and ns->prefix are never in the dict, so
  8522. * we need not move the values over to the destination dict.
  8523. * - Note that for custom handling of ns-references,
  8524. * the ns-decls need not be stored in the ns-map,
  8525. * since they won't be referenced by node->ns.
  8526. */
  8527. if ((cur->nsDef) &&
  8528. ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL)))
  8529. {
  8530. if (! parnsdone) {
  8531. /*
  8532. * Gather @parent's in-scope ns-decls.
  8533. */
  8534. if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
  8535. destParent) == -1)
  8536. goto internal_error;
  8537. parnsdone = 1;
  8538. }
  8539. for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
  8540. /*
  8541. * NOTE: ns->prefix and ns->href are never in the dict.
  8542. * XML_TREE_ADOPT_STR(ns->prefix)
  8543. * XML_TREE_ADOPT_STR(ns->href)
  8544. */
  8545. /*
  8546. * Does it shadow any ns-decl?
  8547. */
  8548. if (XML_NSMAP_NOTEMPTY(nsMap)) {
  8549. XML_NSMAP_FOREACH(nsMap, mi) {
  8550. if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
  8551. (mi->shadowDepth == -1) &&
  8552. ((ns->prefix == mi->newNs->prefix) ||
  8553. xmlStrEqual(ns->prefix,
  8554. mi->newNs->prefix))) {
  8555. mi->shadowDepth = depth;
  8556. }
  8557. }
  8558. }
  8559. /*
  8560. * Push mapping.
  8561. */
  8562. if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
  8563. ns, ns, depth) == NULL)
  8564. goto internal_error;
  8565. }
  8566. }
  8567. /* Falls through. */
  8568. case XML_ATTRIBUTE_NODE:
  8569. /* No namespace, no fun. */
  8570. if (cur->ns == NULL)
  8571. goto ns_end;
  8572. if (! parnsdone) {
  8573. if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
  8574. destParent) == -1)
  8575. goto internal_error;
  8576. parnsdone = 1;
  8577. }
  8578. /*
  8579. * Adopt ns-references.
  8580. */
  8581. if (XML_NSMAP_NOTEMPTY(nsMap)) {
  8582. /*
  8583. * Search for a mapping.
  8584. */
  8585. XML_NSMAP_FOREACH(nsMap, mi) {
  8586. if ((mi->shadowDepth == -1) &&
  8587. (cur->ns == mi->oldNs)) {
  8588. cur->ns = mi->newNs;
  8589. goto ns_end;
  8590. }
  8591. }
  8592. }
  8593. /*
  8594. * No matching namespace in scope. We need a new one.
  8595. */
  8596. if ((ctxt) && (ctxt->getNsForNodeFunc)) {
  8597. /*
  8598. * User-defined behaviour.
  8599. */
  8600. ns = ctxt->getNsForNodeFunc(ctxt, cur,
  8601. cur->ns->href, cur->ns->prefix);
  8602. /*
  8603. * Insert mapping if ns is available; it's the users fault
  8604. * if not.
  8605. */
  8606. if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
  8607. cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
  8608. goto internal_error;
  8609. cur->ns = ns;
  8610. } else {
  8611. /*
  8612. * Acquire a normalized ns-decl and add it to the map.
  8613. */
  8614. if (xmlDOMWrapNSNormAcquireNormalizedNs(destDoc,
  8615. /* ns-decls on curElem or on destDoc->oldNs */
  8616. destParent ? curElem : NULL,
  8617. cur->ns, &ns,
  8618. &nsMap, depth,
  8619. ancestorsOnly,
  8620. /* ns-decls must be prefixed for attributes. */
  8621. (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
  8622. goto internal_error;
  8623. cur->ns = ns;
  8624. }
  8625. ns_end:
  8626. /*
  8627. * Further node properties.
  8628. * TODO: Is this all?
  8629. */
  8630. XML_TREE_ADOPT_STR(cur->name)
  8631. if (cur->type == XML_ELEMENT_NODE) {
  8632. cur->psvi = NULL;
  8633. cur->line = 0;
  8634. cur->extra = 0;
  8635. /*
  8636. * Walk attributes.
  8637. */
  8638. if (cur->properties != NULL) {
  8639. /*
  8640. * Process first attribute node.
  8641. */
  8642. cur = (xmlNodePtr) cur->properties;
  8643. continue;
  8644. }
  8645. } else {
  8646. /*
  8647. * Attributes.
  8648. */
  8649. if ((sourceDoc != NULL) &&
  8650. (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID))
  8651. {
  8652. xmlRemoveID(sourceDoc, (xmlAttrPtr) cur);
  8653. }
  8654. ((xmlAttrPtr) cur)->atype = 0;
  8655. ((xmlAttrPtr) cur)->psvi = NULL;
  8656. }
  8657. break;
  8658. case XML_TEXT_NODE:
  8659. case XML_CDATA_SECTION_NODE:
  8660. /*
  8661. * This puts the content in the dest dict, only if
  8662. * it was previously in the source dict.
  8663. */
  8664. XML_TREE_ADOPT_STR_2(cur->content)
  8665. goto leave_node;
  8666. case XML_ENTITY_REF_NODE:
  8667. /*
  8668. * Remove reference to the entity-node.
  8669. */
  8670. cur->content = NULL;
  8671. cur->children = NULL;
  8672. cur->last = NULL;
  8673. if ((destDoc->intSubset) || (destDoc->extSubset)) {
  8674. xmlEntityPtr ent;
  8675. /*
  8676. * Assign new entity-node if available.
  8677. */
  8678. ent = xmlGetDocEntity(destDoc, cur->name);
  8679. if (ent != NULL) {
  8680. cur->content = ent->content;
  8681. cur->children = (xmlNodePtr) ent;
  8682. cur->last = (xmlNodePtr) ent;
  8683. }
  8684. }
  8685. goto leave_node;
  8686. case XML_PI_NODE:
  8687. XML_TREE_ADOPT_STR(cur->name)
  8688. XML_TREE_ADOPT_STR_2(cur->content)
  8689. break;
  8690. case XML_COMMENT_NODE:
  8691. break;
  8692. default:
  8693. goto internal_error;
  8694. }
  8695. /*
  8696. * Walk the tree.
  8697. */
  8698. if (cur->children != NULL) {
  8699. cur = cur->children;
  8700. continue;
  8701. }
  8702. leave_node:
  8703. if (cur == node)
  8704. break;
  8705. if ((cur->type == XML_ELEMENT_NODE) ||
  8706. (cur->type == XML_XINCLUDE_START) ||
  8707. (cur->type == XML_XINCLUDE_END))
  8708. {
  8709. /*
  8710. * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
  8711. */
  8712. if (XML_NSMAP_NOTEMPTY(nsMap)) {
  8713. /*
  8714. * Pop mappings.
  8715. */
  8716. while ((nsMap->last != NULL) &&
  8717. (nsMap->last->depth >= depth))
  8718. {
  8719. XML_NSMAP_POP(nsMap, mi)
  8720. }
  8721. /*
  8722. * Unshadow.
  8723. */
  8724. XML_NSMAP_FOREACH(nsMap, mi) {
  8725. if (mi->shadowDepth >= depth)
  8726. mi->shadowDepth = -1;
  8727. }
  8728. }
  8729. depth--;
  8730. }
  8731. if (cur->next != NULL)
  8732. cur = cur->next;
  8733. else if ((cur->type == XML_ATTRIBUTE_NODE) &&
  8734. (cur->parent->children != NULL))
  8735. {
  8736. cur = cur->parent->children;
  8737. } else {
  8738. cur = cur->parent;
  8739. goto leave_node;
  8740. }
  8741. }
  8742. goto exit;
  8743. internal_error:
  8744. ret = -1;
  8745. exit:
  8746. /*
  8747. * Cleanup.
  8748. */
  8749. if (nsMap != NULL) {
  8750. if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
  8751. /*
  8752. * Just cleanup the map but don't free.
  8753. */
  8754. if (nsMap->first) {
  8755. if (nsMap->pool)
  8756. nsMap->last->next = nsMap->pool;
  8757. nsMap->pool = nsMap->first;
  8758. nsMap->first = NULL;
  8759. }
  8760. } else
  8761. xmlDOMWrapNsMapFree(nsMap);
  8762. }
  8763. return(ret);
  8764. }
  8765. /*
  8766. * xmlDOMWrapCloneNode:
  8767. * @ctxt: the optional context for custom processing
  8768. * @sourceDoc: the optional sourceDoc
  8769. * @node: the node to start with
  8770. * @resNode: the clone of the given @node
  8771. * @destDoc: the destination doc
  8772. * @destParent: the optional new parent of @node in @destDoc
  8773. * @deep: descend into child if set
  8774. * @options: option flags
  8775. *
  8776. * References of out-of scope ns-decls are remapped to point to @destDoc:
  8777. * 1) If @destParent is given, then nsDef entries on element-nodes are used
  8778. * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used.
  8779. * This is the case when you don't know already where the cloned branch
  8780. * will be added to.
  8781. *
  8782. * If @destParent is given, it ensures that the tree is namespace
  8783. * wellformed by creating additional ns-decls where needed.
  8784. * Note that, since prefixes of already existent ns-decls can be
  8785. * shadowed by this process, it could break QNames in attribute
  8786. * values or element content.
  8787. * TODO:
  8788. * 1) What to do with XInclude? Currently this returns an error for XInclude.
  8789. *
  8790. * Returns 0 if the operation succeeded,
  8791. * 1 if a node of unsupported (or not yet supported) type was given,
  8792. * -1 on API/internal errors.
  8793. */
  8794. int
  8795. xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt,
  8796. xmlDocPtr sourceDoc,
  8797. xmlNodePtr node,
  8798. xmlNodePtr *resNode,
  8799. xmlDocPtr destDoc,
  8800. xmlNodePtr destParent,
  8801. int deep,
  8802. int options ATTRIBUTE_UNUSED)
  8803. {
  8804. int ret = 0;
  8805. xmlNodePtr cur, curElem = NULL;
  8806. xmlNsMapPtr nsMap = NULL;
  8807. xmlNsMapItemPtr mi;
  8808. xmlNsPtr ns;
  8809. int depth = -1;
  8810. /* int adoptStr = 1; */
  8811. /* gather @parent's ns-decls. */
  8812. int parnsdone = 0;
  8813. /*
  8814. * @ancestorsOnly:
  8815. * TODO: @ancestorsOnly should be set per option.
  8816. *
  8817. */
  8818. int ancestorsOnly = 0;
  8819. xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL;
  8820. xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL;
  8821. xmlDictPtr dict; /* The destination dict */
  8822. if ((node == NULL) || (resNode == NULL) || (destDoc == NULL))
  8823. return(-1);
  8824. /*
  8825. * TODO: Initially we support only element-nodes.
  8826. */
  8827. if (node->type != XML_ELEMENT_NODE)
  8828. return(1);
  8829. /*
  8830. * Check node->doc sanity.
  8831. */
  8832. if ((node->doc != NULL) && (sourceDoc != NULL) &&
  8833. (node->doc != sourceDoc)) {
  8834. /*
  8835. * Might be an XIncluded node.
  8836. */
  8837. return (-1);
  8838. }
  8839. if (sourceDoc == NULL)
  8840. sourceDoc = node->doc;
  8841. if (sourceDoc == NULL)
  8842. return (-1);
  8843. dict = destDoc->dict;
  8844. /*
  8845. * Reuse the namespace map of the context.
  8846. */
  8847. if (ctxt)
  8848. nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
  8849. *resNode = NULL;
  8850. cur = node;
  8851. if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
  8852. return(-1);
  8853. while (cur != NULL) {
  8854. if (cur->doc != sourceDoc) {
  8855. /*
  8856. * We'll assume XIncluded nodes if the doc differs.
  8857. * TODO: Do we need to reconciliate XIncluded nodes?
  8858. * TODO: This here returns -1 in this case.
  8859. */
  8860. goto internal_error;
  8861. }
  8862. /*
  8863. * Create a new node.
  8864. */
  8865. switch (cur->type) {
  8866. case XML_XINCLUDE_START:
  8867. case XML_XINCLUDE_END:
  8868. /*
  8869. * TODO: What to do with XInclude?
  8870. */
  8871. goto internal_error;
  8872. break;
  8873. case XML_ELEMENT_NODE:
  8874. case XML_TEXT_NODE:
  8875. case XML_CDATA_SECTION_NODE:
  8876. case XML_COMMENT_NODE:
  8877. case XML_PI_NODE:
  8878. case XML_DOCUMENT_FRAG_NODE:
  8879. case XML_ENTITY_REF_NODE:
  8880. case XML_ENTITY_NODE:
  8881. /*
  8882. * Nodes of xmlNode structure.
  8883. */
  8884. clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
  8885. if (clone == NULL) {
  8886. xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node");
  8887. goto internal_error;
  8888. }
  8889. memset(clone, 0, sizeof(xmlNode));
  8890. /*
  8891. * Set hierarchical links.
  8892. */
  8893. if (resultClone != NULL) {
  8894. clone->parent = parentClone;
  8895. if (prevClone) {
  8896. prevClone->next = clone;
  8897. clone->prev = prevClone;
  8898. } else
  8899. parentClone->children = clone;
  8900. } else
  8901. resultClone = clone;
  8902. break;
  8903. case XML_ATTRIBUTE_NODE:
  8904. /*
  8905. * Attributes (xmlAttr).
  8906. */
  8907. clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr));
  8908. if (clone == NULL) {
  8909. xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node");
  8910. goto internal_error;
  8911. }
  8912. memset(clone, 0, sizeof(xmlAttr));
  8913. /*
  8914. * Set hierarchical links.
  8915. * TODO: Change this to add to the end of attributes.
  8916. */
  8917. if (resultClone != NULL) {
  8918. clone->parent = parentClone;
  8919. if (prevClone) {
  8920. prevClone->next = clone;
  8921. clone->prev = prevClone;
  8922. } else
  8923. parentClone->properties = (xmlAttrPtr) clone;
  8924. } else
  8925. resultClone = clone;
  8926. break;
  8927. default:
  8928. /*
  8929. * TODO QUESTION: Any other nodes expected?
  8930. */
  8931. goto internal_error;
  8932. }
  8933. clone->type = cur->type;
  8934. clone->doc = destDoc;
  8935. /*
  8936. * Clone the name of the node if any.
  8937. */
  8938. if (cur->name == xmlStringText)
  8939. clone->name = xmlStringText;
  8940. else if (cur->name == xmlStringTextNoenc)
  8941. /*
  8942. * NOTE: Although xmlStringTextNoenc is never assigned to a node
  8943. * in tree.c, it might be set in Libxslt via
  8944. * "xsl:disable-output-escaping".
  8945. */
  8946. clone->name = xmlStringTextNoenc;
  8947. else if (cur->name == xmlStringComment)
  8948. clone->name = xmlStringComment;
  8949. else if (cur->name != NULL) {
  8950. DICT_CONST_COPY(cur->name, clone->name);
  8951. }
  8952. switch (cur->type) {
  8953. case XML_XINCLUDE_START:
  8954. case XML_XINCLUDE_END:
  8955. /*
  8956. * TODO
  8957. */
  8958. return (-1);
  8959. case XML_ELEMENT_NODE:
  8960. curElem = cur;
  8961. depth++;
  8962. /*
  8963. * Namespace declarations.
  8964. */
  8965. if (cur->nsDef != NULL) {
  8966. if (! parnsdone) {
  8967. if (destParent && (ctxt == NULL)) {
  8968. /*
  8969. * Gather @parent's in-scope ns-decls.
  8970. */
  8971. if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
  8972. destParent) == -1)
  8973. goto internal_error;
  8974. }
  8975. parnsdone = 1;
  8976. }
  8977. /*
  8978. * Clone namespace declarations.
  8979. */
  8980. cloneNsDefSlot = &(clone->nsDef);
  8981. for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
  8982. /*
  8983. * Create a new xmlNs.
  8984. */
  8985. cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
  8986. if (cloneNs == NULL) {
  8987. xmlTreeErrMemory("xmlDOMWrapCloneNode(): "
  8988. "allocating namespace");
  8989. return(-1);
  8990. }
  8991. memset(cloneNs, 0, sizeof(xmlNs));
  8992. cloneNs->type = XML_LOCAL_NAMESPACE;
  8993. if (ns->href != NULL)
  8994. cloneNs->href = xmlStrdup(ns->href);
  8995. if (ns->prefix != NULL)
  8996. cloneNs->prefix = xmlStrdup(ns->prefix);
  8997. *cloneNsDefSlot = cloneNs;
  8998. cloneNsDefSlot = &(cloneNs->next);
  8999. /*
  9000. * Note that for custom handling of ns-references,
  9001. * the ns-decls need not be stored in the ns-map,
  9002. * since they won't be referenced by node->ns.
  9003. */
  9004. if ((ctxt == NULL) ||
  9005. (ctxt->getNsForNodeFunc == NULL))
  9006. {
  9007. /*
  9008. * Does it shadow any ns-decl?
  9009. */
  9010. if (XML_NSMAP_NOTEMPTY(nsMap)) {
  9011. XML_NSMAP_FOREACH(nsMap, mi) {
  9012. if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
  9013. (mi->shadowDepth == -1) &&
  9014. ((ns->prefix == mi->newNs->prefix) ||
  9015. xmlStrEqual(ns->prefix,
  9016. mi->newNs->prefix))) {
  9017. /*
  9018. * Mark as shadowed at the current
  9019. * depth.
  9020. */
  9021. mi->shadowDepth = depth;
  9022. }
  9023. }
  9024. }
  9025. /*
  9026. * Push mapping.
  9027. */
  9028. if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
  9029. ns, cloneNs, depth) == NULL)
  9030. goto internal_error;
  9031. }
  9032. }
  9033. }
  9034. /* cur->ns will be processed further down. */
  9035. break;
  9036. case XML_ATTRIBUTE_NODE:
  9037. /* IDs will be processed further down. */
  9038. /* cur->ns will be processed further down. */
  9039. break;
  9040. case XML_TEXT_NODE:
  9041. case XML_CDATA_SECTION_NODE:
  9042. /*
  9043. * Note that this will also cover the values of attributes.
  9044. */
  9045. DICT_COPY(cur->content, clone->content);
  9046. goto leave_node;
  9047. case XML_ENTITY_NODE:
  9048. /* TODO: What to do here? */
  9049. goto leave_node;
  9050. case XML_ENTITY_REF_NODE:
  9051. if (sourceDoc != destDoc) {
  9052. if ((destDoc->intSubset) || (destDoc->extSubset)) {
  9053. xmlEntityPtr ent;
  9054. /*
  9055. * Different doc: Assign new entity-node if available.
  9056. */
  9057. ent = xmlGetDocEntity(destDoc, cur->name);
  9058. if (ent != NULL) {
  9059. clone->content = ent->content;
  9060. clone->children = (xmlNodePtr) ent;
  9061. clone->last = (xmlNodePtr) ent;
  9062. }
  9063. }
  9064. } else {
  9065. /*
  9066. * Same doc: Use the current node's entity declaration
  9067. * and value.
  9068. */
  9069. clone->content = cur->content;
  9070. clone->children = cur->children;
  9071. clone->last = cur->last;
  9072. }
  9073. goto leave_node;
  9074. case XML_PI_NODE:
  9075. DICT_COPY(cur->content, clone->content);
  9076. goto leave_node;
  9077. case XML_COMMENT_NODE:
  9078. DICT_COPY(cur->content, clone->content);
  9079. goto leave_node;
  9080. default:
  9081. goto internal_error;
  9082. }
  9083. if (cur->ns == NULL)
  9084. goto end_ns_reference;
  9085. /* handle_ns_reference: */
  9086. /*
  9087. ** The following will take care of references to ns-decls ********
  9088. ** and is intended only for element- and attribute-nodes.
  9089. **
  9090. */
  9091. if (! parnsdone) {
  9092. if (destParent && (ctxt == NULL)) {
  9093. if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1)
  9094. goto internal_error;
  9095. }
  9096. parnsdone = 1;
  9097. }
  9098. /*
  9099. * Adopt ns-references.
  9100. */
  9101. if (XML_NSMAP_NOTEMPTY(nsMap)) {
  9102. /*
  9103. * Search for a mapping.
  9104. */
  9105. XML_NSMAP_FOREACH(nsMap, mi) {
  9106. if ((mi->shadowDepth == -1) &&
  9107. (cur->ns == mi->oldNs)) {
  9108. /*
  9109. * This is the nice case: a mapping was found.
  9110. */
  9111. clone->ns = mi->newNs;
  9112. goto end_ns_reference;
  9113. }
  9114. }
  9115. }
  9116. /*
  9117. * No matching namespace in scope. We need a new one.
  9118. */
  9119. if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) {
  9120. /*
  9121. * User-defined behaviour.
  9122. */
  9123. ns = ctxt->getNsForNodeFunc(ctxt, cur,
  9124. cur->ns->href, cur->ns->prefix);
  9125. /*
  9126. * Add user's mapping.
  9127. */
  9128. if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
  9129. cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
  9130. goto internal_error;
  9131. clone->ns = ns;
  9132. } else {
  9133. /*
  9134. * Acquire a normalized ns-decl and add it to the map.
  9135. */
  9136. if (xmlDOMWrapNSNormAcquireNormalizedNs(destDoc,
  9137. /* ns-decls on curElem or on destDoc->oldNs */
  9138. destParent ? curElem : NULL,
  9139. cur->ns, &ns,
  9140. &nsMap, depth,
  9141. /* if we need to search only in the ancestor-axis */
  9142. ancestorsOnly,
  9143. /* ns-decls must be prefixed for attributes. */
  9144. (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
  9145. goto internal_error;
  9146. clone->ns = ns;
  9147. }
  9148. end_ns_reference:
  9149. /*
  9150. * Some post-processing.
  9151. *
  9152. * Handle ID attributes.
  9153. */
  9154. if ((clone->type == XML_ATTRIBUTE_NODE) &&
  9155. (clone->parent != NULL))
  9156. {
  9157. if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) {
  9158. xmlChar *idVal;
  9159. idVal = xmlNodeListGetString(cur->doc, cur->children, 1);
  9160. if (idVal != NULL) {
  9161. if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) {
  9162. /* TODO: error message. */
  9163. xmlFree(idVal);
  9164. goto internal_error;
  9165. }
  9166. xmlFree(idVal);
  9167. }
  9168. }
  9169. }
  9170. /*
  9171. **
  9172. ** The following will traverse the tree **************************
  9173. **
  9174. *
  9175. * Walk the element's attributes before descending into child-nodes.
  9176. */
  9177. if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) {
  9178. prevClone = NULL;
  9179. parentClone = clone;
  9180. cur = (xmlNodePtr) cur->properties;
  9181. continue;
  9182. }
  9183. into_content:
  9184. /*
  9185. * Descend into child-nodes.
  9186. */
  9187. if (cur->children != NULL) {
  9188. if (deep || (cur->type == XML_ATTRIBUTE_NODE)) {
  9189. prevClone = NULL;
  9190. parentClone = clone;
  9191. cur = cur->children;
  9192. continue;
  9193. }
  9194. }
  9195. leave_node:
  9196. /*
  9197. * At this point we are done with the node, its content
  9198. * and an element-nodes's attribute-nodes.
  9199. */
  9200. if (cur == node)
  9201. break;
  9202. if ((cur->type == XML_ELEMENT_NODE) ||
  9203. (cur->type == XML_XINCLUDE_START) ||
  9204. (cur->type == XML_XINCLUDE_END)) {
  9205. /*
  9206. * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
  9207. */
  9208. if (XML_NSMAP_NOTEMPTY(nsMap)) {
  9209. /*
  9210. * Pop mappings.
  9211. */
  9212. while ((nsMap->last != NULL) &&
  9213. (nsMap->last->depth >= depth))
  9214. {
  9215. XML_NSMAP_POP(nsMap, mi)
  9216. }
  9217. /*
  9218. * Unshadow.
  9219. */
  9220. XML_NSMAP_FOREACH(nsMap, mi) {
  9221. if (mi->shadowDepth >= depth)
  9222. mi->shadowDepth = -1;
  9223. }
  9224. }
  9225. depth--;
  9226. }
  9227. if (cur->next != NULL) {
  9228. prevClone = clone;
  9229. cur = cur->next;
  9230. } else if (cur->type != XML_ATTRIBUTE_NODE) {
  9231. /*
  9232. * Set clone->last.
  9233. */
  9234. if (clone->parent != NULL)
  9235. clone->parent->last = clone;
  9236. clone = clone->parent;
  9237. if (clone != NULL)
  9238. parentClone = clone->parent;
  9239. /*
  9240. * Process parent --> next;
  9241. */
  9242. cur = cur->parent;
  9243. goto leave_node;
  9244. } else {
  9245. /* This is for attributes only. */
  9246. clone = clone->parent;
  9247. parentClone = clone->parent;
  9248. /*
  9249. * Process parent-element --> children.
  9250. */
  9251. cur = cur->parent;
  9252. goto into_content;
  9253. }
  9254. }
  9255. goto exit;
  9256. internal_error:
  9257. ret = -1;
  9258. exit:
  9259. /*
  9260. * Cleanup.
  9261. */
  9262. if (nsMap != NULL) {
  9263. if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
  9264. /*
  9265. * Just cleanup the map but don't free.
  9266. */
  9267. if (nsMap->first) {
  9268. if (nsMap->pool)
  9269. nsMap->last->next = nsMap->pool;
  9270. nsMap->pool = nsMap->first;
  9271. nsMap->first = NULL;
  9272. }
  9273. } else
  9274. xmlDOMWrapNsMapFree(nsMap);
  9275. }
  9276. /*
  9277. * TODO: Should we try a cleanup of the cloned node in case of a
  9278. * fatal error?
  9279. */
  9280. *resNode = resultClone;
  9281. return (ret);
  9282. }
  9283. /*
  9284. * xmlDOMWrapAdoptAttr:
  9285. * @ctxt: the optional context for custom processing
  9286. * @sourceDoc: the optional source document of attr
  9287. * @attr: the attribute-node to be adopted
  9288. * @destDoc: the destination doc for adoption
  9289. * @destParent: the optional new parent of @attr in @destDoc
  9290. * @options: option flags
  9291. *
  9292. * @attr is adopted by @destDoc.
  9293. * Ensures that ns-references point to @destDoc: either to
  9294. * elements->nsDef entries if @destParent is given, or to
  9295. * @destDoc->oldNs otherwise.
  9296. *
  9297. * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
  9298. */
  9299. static int
  9300. xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt,
  9301. xmlDocPtr sourceDoc,
  9302. xmlAttrPtr attr,
  9303. xmlDocPtr destDoc,
  9304. xmlNodePtr destParent,
  9305. int options ATTRIBUTE_UNUSED)
  9306. {
  9307. xmlNodePtr cur;
  9308. int adoptStr = 1;
  9309. if ((attr == NULL) || (destDoc == NULL))
  9310. return (-1);
  9311. attr->doc = destDoc;
  9312. if (attr->ns != NULL) {
  9313. xmlNsPtr ns = NULL;
  9314. if (ctxt != NULL) {
  9315. /* TODO: User defined. */
  9316. }
  9317. /* XML Namespace. */
  9318. if (IS_STR_XML(attr->ns->prefix)) {
  9319. ns = xmlTreeEnsureXMLDecl(destDoc);
  9320. } else if (destParent == NULL) {
  9321. /*
  9322. * Store in @destDoc->oldNs.
  9323. */
  9324. ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix);
  9325. } else {
  9326. /*
  9327. * Declare on @destParent.
  9328. */
  9329. if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href,
  9330. &ns, 1) == -1)
  9331. goto internal_error;
  9332. if (ns == NULL) {
  9333. ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent,
  9334. attr->ns->href, attr->ns->prefix, 1);
  9335. }
  9336. }
  9337. if (ns == NULL)
  9338. goto internal_error;
  9339. attr->ns = ns;
  9340. }
  9341. XML_TREE_ADOPT_STR(attr->name);
  9342. attr->atype = 0;
  9343. attr->psvi = NULL;
  9344. /*
  9345. * Walk content.
  9346. */
  9347. if (attr->children == NULL)
  9348. return (0);
  9349. cur = attr->children;
  9350. if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
  9351. goto internal_error;
  9352. while (cur != NULL) {
  9353. cur->doc = destDoc;
  9354. switch (cur->type) {
  9355. case XML_TEXT_NODE:
  9356. case XML_CDATA_SECTION_NODE:
  9357. XML_TREE_ADOPT_STR_2(cur->content)
  9358. break;
  9359. case XML_ENTITY_REF_NODE:
  9360. /*
  9361. * Remove reference to the entity-node.
  9362. */
  9363. cur->content = NULL;
  9364. cur->children = NULL;
  9365. cur->last = NULL;
  9366. if ((destDoc->intSubset) || (destDoc->extSubset)) {
  9367. xmlEntityPtr ent;
  9368. /*
  9369. * Assign new entity-node if available.
  9370. */
  9371. ent = xmlGetDocEntity(destDoc, cur->name);
  9372. if (ent != NULL) {
  9373. cur->content = ent->content;
  9374. cur->children = (xmlNodePtr) ent;
  9375. cur->last = (xmlNodePtr) ent;
  9376. }
  9377. }
  9378. break;
  9379. default:
  9380. break;
  9381. }
  9382. if (cur->children != NULL) {
  9383. cur = cur->children;
  9384. continue;
  9385. }
  9386. next_sibling:
  9387. if (cur == (xmlNodePtr) attr)
  9388. break;
  9389. if (cur->next != NULL)
  9390. cur = cur->next;
  9391. else {
  9392. cur = cur->parent;
  9393. goto next_sibling;
  9394. }
  9395. }
  9396. return (0);
  9397. internal_error:
  9398. return (-1);
  9399. }
  9400. /*
  9401. * xmlDOMWrapAdoptNode:
  9402. * @ctxt: the optional context for custom processing
  9403. * @sourceDoc: the optional sourceDoc
  9404. * @node: the node to start with
  9405. * @destDoc: the destination doc
  9406. * @destParent: the optional new parent of @node in @destDoc
  9407. * @options: option flags
  9408. *
  9409. * References of out-of scope ns-decls are remapped to point to @destDoc:
  9410. * 1) If @destParent is given, then nsDef entries on element-nodes are used
  9411. * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used
  9412. * This is the case when you have an unlinked node and just want to move it
  9413. * to the context of
  9414. *
  9415. * If @destParent is given, it ensures that the tree is namespace
  9416. * wellformed by creating additional ns-decls where needed.
  9417. * Note that, since prefixes of already existent ns-decls can be
  9418. * shadowed by this process, it could break QNames in attribute
  9419. * values or element content.
  9420. * NOTE: This function was not intensively tested.
  9421. *
  9422. * Returns 0 if the operation succeeded,
  9423. * 1 if a node of unsupported type was given,
  9424. * 2 if a node of not yet supported type was given and
  9425. * -1 on API/internal errors.
  9426. */
  9427. int
  9428. xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt,
  9429. xmlDocPtr sourceDoc,
  9430. xmlNodePtr node,
  9431. xmlDocPtr destDoc,
  9432. xmlNodePtr destParent,
  9433. int options)
  9434. {
  9435. if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
  9436. (destDoc == NULL) ||
  9437. ((destParent != NULL) && (destParent->doc != destDoc)))
  9438. return(-1);
  9439. /*
  9440. * Check node->doc sanity.
  9441. */
  9442. if ((node->doc != NULL) && (sourceDoc != NULL) &&
  9443. (node->doc != sourceDoc)) {
  9444. /*
  9445. * Might be an XIncluded node.
  9446. */
  9447. return (-1);
  9448. }
  9449. if (sourceDoc == NULL)
  9450. sourceDoc = node->doc;
  9451. if (sourceDoc == destDoc)
  9452. return (-1);
  9453. switch (node->type) {
  9454. case XML_ELEMENT_NODE:
  9455. case XML_ATTRIBUTE_NODE:
  9456. case XML_TEXT_NODE:
  9457. case XML_CDATA_SECTION_NODE:
  9458. case XML_ENTITY_REF_NODE:
  9459. case XML_PI_NODE:
  9460. case XML_COMMENT_NODE:
  9461. break;
  9462. case XML_DOCUMENT_FRAG_NODE:
  9463. /* TODO: Support document-fragment-nodes. */
  9464. return (2);
  9465. default:
  9466. return (1);
  9467. }
  9468. /*
  9469. * Unlink only if @node was not already added to @destParent.
  9470. */
  9471. if ((node->parent != NULL) && (destParent != node->parent))
  9472. xmlUnlinkNode(node);
  9473. if (node->type == XML_ELEMENT_NODE) {
  9474. return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node,
  9475. destDoc, destParent, options));
  9476. } else if (node->type == XML_ATTRIBUTE_NODE) {
  9477. return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc,
  9478. (xmlAttrPtr) node, destDoc, destParent, options));
  9479. } else {
  9480. xmlNodePtr cur = node;
  9481. int adoptStr = 1;
  9482. cur->doc = destDoc;
  9483. /*
  9484. * Optimize string adoption.
  9485. */
  9486. if ((sourceDoc != NULL) &&
  9487. (sourceDoc->dict == destDoc->dict))
  9488. adoptStr = 0;
  9489. switch (node->type) {
  9490. case XML_TEXT_NODE:
  9491. case XML_CDATA_SECTION_NODE:
  9492. XML_TREE_ADOPT_STR_2(node->content)
  9493. break;
  9494. case XML_ENTITY_REF_NODE:
  9495. /*
  9496. * Remove reference to the entity-node.
  9497. */
  9498. node->content = NULL;
  9499. node->children = NULL;
  9500. node->last = NULL;
  9501. if ((destDoc->intSubset) || (destDoc->extSubset)) {
  9502. xmlEntityPtr ent;
  9503. /*
  9504. * Assign new entity-node if available.
  9505. */
  9506. ent = xmlGetDocEntity(destDoc, node->name);
  9507. if (ent != NULL) {
  9508. node->content = ent->content;
  9509. node->children = (xmlNodePtr) ent;
  9510. node->last = (xmlNodePtr) ent;
  9511. }
  9512. }
  9513. XML_TREE_ADOPT_STR(node->name)
  9514. break;
  9515. case XML_PI_NODE: {
  9516. XML_TREE_ADOPT_STR(node->name)
  9517. XML_TREE_ADOPT_STR_2(node->content)
  9518. break;
  9519. }
  9520. default:
  9521. break;
  9522. }
  9523. }
  9524. return (0);
  9525. }