tree.c 257 KB

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