tree.c 256 KB

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