tree.c 248 KB

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