tree.c 248 KB

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