tree.c 257 KB

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