tree.c 257 KB

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