xmlreader.c 159 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762
  1. /*
  2. * xmlreader.c: implements the xmlTextReader streaming node API
  3. *
  4. * NOTE:
  5. * XmlTextReader.Normalization Property won't be supported, since
  6. * it makes the parser non compliant to the XML recommendation
  7. *
  8. * See Copyright for the status of this software.
  9. *
  10. * daniel@veillard.com
  11. */
  12. /*
  13. * TODOs:
  14. * - XML Schemas validation
  15. */
  16. #define IN_LIBXML
  17. #include "libxml.h"
  18. #ifdef LIBXML_READER_ENABLED
  19. #include <string.h> /* for memset() only ! */
  20. #include <stdarg.h>
  21. #ifdef HAVE_CTYPE_H
  22. #include <ctype.h>
  23. #endif
  24. #ifdef HAVE_STDLIB_H
  25. #include <stdlib.h>
  26. #endif
  27. #include <libxml/xmlmemory.h>
  28. #include <libxml/xmlIO.h>
  29. #include <libxml/xmlreader.h>
  30. #include <libxml/parserInternals.h>
  31. #ifdef LIBXML_SCHEMAS_ENABLED
  32. #include <libxml/relaxng.h>
  33. #include <libxml/xmlschemas.h>
  34. #endif
  35. #include <libxml/uri.h>
  36. #ifdef LIBXML_XINCLUDE_ENABLED
  37. #include <libxml/xinclude.h>
  38. #endif
  39. #ifdef LIBXML_PATTERN_ENABLED
  40. #include <libxml/pattern.h>
  41. #endif
  42. #define MAX_ERR_MSG_SIZE 64000
  43. /*
  44. * The following VA_COPY was coded following an example in
  45. * the Samba project. It may not be sufficient for some
  46. * esoteric implementations of va_list (i.e. it may need
  47. * something involving a memcpy) but (hopefully) will be
  48. * sufficient for libxml2.
  49. */
  50. #ifndef VA_COPY
  51. #ifdef HAVE_VA_COPY
  52. #define VA_COPY(dest, src) va_copy(dest, src)
  53. #else
  54. #ifdef HAVE___VA_COPY
  55. #define VA_COPY(dest,src) __va_copy(dest, src)
  56. #else
  57. #define VA_COPY(dest,src) (dest) = (src)
  58. #endif
  59. #endif
  60. #endif
  61. /* #define DEBUG_CALLBACKS */
  62. /* #define DEBUG_READER */
  63. /**
  64. * TODO:
  65. *
  66. * macro to flag unimplemented blocks
  67. */
  68. #define TODO \
  69. xmlGenericError(xmlGenericErrorContext, \
  70. "Unimplemented block at %s:%d\n", \
  71. __FILE__, __LINE__);
  72. #ifdef DEBUG_READER
  73. #define DUMP_READER xmlTextReaderDebug(reader);
  74. #else
  75. #define DUMP_READER
  76. #endif
  77. #define CHUNK_SIZE 512
  78. /************************************************************************
  79. * *
  80. * The parser: maps the Text Reader API on top of the existing *
  81. * parsing routines building a tree *
  82. * *
  83. ************************************************************************/
  84. #define XML_TEXTREADER_INPUT 1
  85. #define XML_TEXTREADER_CTXT 2
  86. typedef enum {
  87. XML_TEXTREADER_NONE = -1,
  88. XML_TEXTREADER_START= 0,
  89. XML_TEXTREADER_ELEMENT= 1,
  90. XML_TEXTREADER_END= 2,
  91. XML_TEXTREADER_EMPTY= 3,
  92. XML_TEXTREADER_BACKTRACK= 4,
  93. XML_TEXTREADER_DONE= 5,
  94. XML_TEXTREADER_ERROR= 6
  95. } xmlTextReaderState;
  96. typedef enum {
  97. XML_TEXTREADER_NOT_VALIDATE = 0,
  98. XML_TEXTREADER_VALIDATE_DTD = 1,
  99. XML_TEXTREADER_VALIDATE_RNG = 2,
  100. XML_TEXTREADER_VALIDATE_XSD = 4
  101. } xmlTextReaderValidate;
  102. struct _xmlTextReader {
  103. int mode; /* the parsing mode */
  104. xmlDocPtr doc; /* when walking an existing doc */
  105. xmlTextReaderValidate validate;/* is there any validation */
  106. int allocs; /* what structure were deallocated */
  107. xmlTextReaderState state;
  108. xmlParserCtxtPtr ctxt; /* the parser context */
  109. xmlSAXHandlerPtr sax; /* the parser SAX callbacks */
  110. xmlParserInputBufferPtr input; /* the input */
  111. startElementSAXFunc startElement;/* initial SAX callbacks */
  112. endElementSAXFunc endElement; /* idem */
  113. startElementNsSAX2Func startElementNs;/* idem */
  114. endElementNsSAX2Func endElementNs; /* idem */
  115. charactersSAXFunc characters;
  116. cdataBlockSAXFunc cdataBlock;
  117. unsigned int base; /* base of the segment in the input */
  118. unsigned int cur; /* current position in the input */
  119. xmlNodePtr node; /* current node */
  120. xmlNodePtr curnode;/* current attribute node */
  121. int depth; /* depth of the current node */
  122. xmlNodePtr faketext;/* fake xmlNs chld */
  123. int preserve;/* preserve the resulting document */
  124. xmlBufferPtr buffer; /* used to return const xmlChar * */
  125. xmlDictPtr dict; /* the context dictionnary */
  126. /* entity stack when traversing entities content */
  127. xmlNodePtr ent; /* Current Entity Ref Node */
  128. int entNr; /* Depth of the entities stack */
  129. int entMax; /* Max depth of the entities stack */
  130. xmlNodePtr *entTab; /* array of entities */
  131. /* error handling */
  132. xmlTextReaderErrorFunc errorFunc; /* callback function */
  133. void *errorFuncArg; /* callback function user argument */
  134. #ifdef LIBXML_SCHEMAS_ENABLED
  135. /* Handling of RelaxNG validation */
  136. xmlRelaxNGPtr rngSchemas; /* The Relax NG schemas */
  137. xmlRelaxNGValidCtxtPtr rngValidCtxt;/* The Relax NG validation context */
  138. int rngValidErrors;/* The number of errors detected */
  139. xmlNodePtr rngFullNode; /* the node if RNG not progressive */
  140. /* Handling of Schemas validation */
  141. xmlSchemaPtr xsdSchemas; /* The Schemas schemas */
  142. xmlSchemaValidCtxtPtr xsdValidCtxt;/* The Schemas validation context */
  143. int xsdPreserveCtxt; /* 1 if the context was provided by the user */
  144. int xsdValidErrors;/* The number of errors detected */
  145. xmlSchemaSAXPlugPtr xsdPlug; /* the schemas plug in SAX pipeline */
  146. #endif
  147. #ifdef LIBXML_XINCLUDE_ENABLED
  148. /* Handling of XInclude processing */
  149. int xinclude; /* is xinclude asked for */
  150. const xmlChar * xinclude_name; /* the xinclude name from dict */
  151. xmlXIncludeCtxtPtr xincctxt; /* the xinclude context */
  152. int in_xinclude; /* counts for xinclude */
  153. #endif
  154. #ifdef LIBXML_PATTERN_ENABLED
  155. int patternNr; /* number of preserve patterns */
  156. int patternMax; /* max preserve patterns */
  157. xmlPatternPtr *patternTab; /* array of preserve patterns */
  158. #endif
  159. int preserves; /* level of preserves */
  160. int parserFlags; /* the set of options set */
  161. /* Structured error handling */
  162. xmlStructuredErrorFunc sErrorFunc; /* callback function */
  163. };
  164. #define NODE_IS_EMPTY 0x1
  165. #define NODE_IS_PRESERVED 0x2
  166. #define NODE_IS_SPRESERVED 0x4
  167. /**
  168. * CONSTSTR:
  169. *
  170. * Macro used to return an interned string
  171. */
  172. #define CONSTSTR(str) xmlDictLookup(reader->dict, (str), -1)
  173. #define CONSTQSTR(p, str) xmlDictQLookup(reader->dict, (p), (str))
  174. static int xmlTextReaderReadTree(xmlTextReaderPtr reader);
  175. static int xmlTextReaderNextTree(xmlTextReaderPtr reader);
  176. /************************************************************************
  177. * *
  178. * Our own version of the freeing routines as we recycle nodes *
  179. * *
  180. ************************************************************************/
  181. /**
  182. * DICT_FREE:
  183. * @str: a string
  184. *
  185. * Free a string if it is not owned by the "dict" dictionnary in the
  186. * current scope
  187. */
  188. #define DICT_FREE(str) \
  189. if ((str) && ((!dict) || \
  190. (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
  191. xmlFree((char *)(str));
  192. static void xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur);
  193. static void xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur);
  194. /**
  195. * xmlFreeID:
  196. * @not: A id
  197. *
  198. * Deallocate the memory used by an id definition
  199. */
  200. static void
  201. xmlFreeID(xmlIDPtr id) {
  202. xmlDictPtr dict = NULL;
  203. if (id == NULL) return;
  204. if (id->doc != NULL)
  205. dict = id->doc->dict;
  206. if (id->value != NULL)
  207. DICT_FREE(id->value)
  208. xmlFree(id);
  209. }
  210. /**
  211. * xmlTextReaderRemoveID:
  212. * @doc: the document
  213. * @attr: the attribute
  214. *
  215. * Remove the given attribute from the ID table maintained internally.
  216. *
  217. * Returns -1 if the lookup failed and 0 otherwise
  218. */
  219. static int
  220. xmlTextReaderRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
  221. xmlIDTablePtr table;
  222. xmlIDPtr id;
  223. xmlChar *ID;
  224. if (doc == NULL) return(-1);
  225. if (attr == NULL) return(-1);
  226. table = (xmlIDTablePtr) doc->ids;
  227. if (table == NULL)
  228. return(-1);
  229. ID = xmlNodeListGetString(doc, attr->children, 1);
  230. if (ID == NULL)
  231. return(-1);
  232. id = xmlHashLookup(table, ID);
  233. xmlFree(ID);
  234. if (id == NULL || id->attr != attr) {
  235. return(-1);
  236. }
  237. id->name = attr->name;
  238. id->attr = NULL;
  239. return(0);
  240. }
  241. /**
  242. * xmlTextReaderFreeProp:
  243. * @reader: the xmlTextReaderPtr used
  244. * @cur: the node
  245. *
  246. * Free a node.
  247. */
  248. static void
  249. xmlTextReaderFreeProp(xmlTextReaderPtr reader, xmlAttrPtr cur) {
  250. xmlDictPtr dict;
  251. dict = reader->ctxt->dict;
  252. if (cur == NULL) return;
  253. if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
  254. xmlDeregisterNodeDefaultValue((xmlNodePtr) cur);
  255. /* Check for ID removal -> leading to invalid references ! */
  256. if ((cur->parent != NULL) && (cur->parent->doc != NULL) &&
  257. ((cur->parent->doc->intSubset != NULL) ||
  258. (cur->parent->doc->extSubset != NULL))) {
  259. if (xmlIsID(cur->parent->doc, cur->parent, cur))
  260. xmlTextReaderRemoveID(cur->parent->doc, cur);
  261. }
  262. if (cur->children != NULL)
  263. xmlTextReaderFreeNodeList(reader, cur->children);
  264. DICT_FREE(cur->name);
  265. if ((reader != NULL) && (reader->ctxt != NULL) &&
  266. (reader->ctxt->freeAttrsNr < 100)) {
  267. cur->next = reader->ctxt->freeAttrs;
  268. reader->ctxt->freeAttrs = cur;
  269. reader->ctxt->freeAttrsNr++;
  270. } else {
  271. xmlFree(cur);
  272. }
  273. }
  274. /**
  275. * xmlTextReaderFreePropList:
  276. * @reader: the xmlTextReaderPtr used
  277. * @cur: the first property in the list
  278. *
  279. * Free a property and all its siblings, all the children are freed too.
  280. */
  281. static void
  282. xmlTextReaderFreePropList(xmlTextReaderPtr reader, xmlAttrPtr cur) {
  283. xmlAttrPtr next;
  284. if (cur == NULL) return;
  285. while (cur != NULL) {
  286. next = cur->next;
  287. xmlTextReaderFreeProp(reader, cur);
  288. cur = next;
  289. }
  290. }
  291. /**
  292. * xmlTextReaderFreeNodeList:
  293. * @reader: the xmlTextReaderPtr used
  294. * @cur: the first node in the list
  295. *
  296. * Free a node and all its siblings, this is a recursive behaviour, all
  297. * the children are freed too.
  298. */
  299. static void
  300. xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur) {
  301. xmlNodePtr next;
  302. xmlDictPtr dict;
  303. dict = reader->ctxt->dict;
  304. if (cur == NULL) return;
  305. if (cur->type == XML_NAMESPACE_DECL) {
  306. xmlFreeNsList((xmlNsPtr) cur);
  307. return;
  308. }
  309. if ((cur->type == XML_DOCUMENT_NODE) ||
  310. (cur->type == XML_HTML_DOCUMENT_NODE)) {
  311. xmlFreeDoc((xmlDocPtr) cur);
  312. return;
  313. }
  314. while (cur != NULL) {
  315. next = cur->next;
  316. /* unroll to speed up freeing the document */
  317. if (cur->type != XML_DTD_NODE) {
  318. if ((cur->children != NULL) &&
  319. (cur->type != XML_ENTITY_REF_NODE)) {
  320. if (cur->children->parent == cur)
  321. xmlTextReaderFreeNodeList(reader, cur->children);
  322. cur->children = NULL;
  323. }
  324. if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
  325. xmlDeregisterNodeDefaultValue(cur);
  326. if (((cur->type == XML_ELEMENT_NODE) ||
  327. (cur->type == XML_XINCLUDE_START) ||
  328. (cur->type == XML_XINCLUDE_END)) &&
  329. (cur->properties != NULL))
  330. xmlTextReaderFreePropList(reader, cur->properties);
  331. if ((cur->content != (xmlChar *) &(cur->properties)) &&
  332. (cur->type != XML_ELEMENT_NODE) &&
  333. (cur->type != XML_XINCLUDE_START) &&
  334. (cur->type != XML_XINCLUDE_END) &&
  335. (cur->type != XML_ENTITY_REF_NODE)) {
  336. DICT_FREE(cur->content);
  337. }
  338. if (((cur->type == XML_ELEMENT_NODE) ||
  339. (cur->type == XML_XINCLUDE_START) ||
  340. (cur->type == XML_XINCLUDE_END)) &&
  341. (cur->nsDef != NULL))
  342. xmlFreeNsList(cur->nsDef);
  343. /*
  344. * we don't free element names here they are interned now
  345. */
  346. if ((cur->type != XML_TEXT_NODE) &&
  347. (cur->type != XML_COMMENT_NODE))
  348. DICT_FREE(cur->name);
  349. if (((cur->type == XML_ELEMENT_NODE) ||
  350. (cur->type == XML_TEXT_NODE)) &&
  351. (reader != NULL) && (reader->ctxt != NULL) &&
  352. (reader->ctxt->freeElemsNr < 100)) {
  353. cur->next = reader->ctxt->freeElems;
  354. reader->ctxt->freeElems = cur;
  355. reader->ctxt->freeElemsNr++;
  356. } else {
  357. xmlFree(cur);
  358. }
  359. }
  360. cur = next;
  361. }
  362. }
  363. /**
  364. * xmlTextReaderFreeNode:
  365. * @reader: the xmlTextReaderPtr used
  366. * @cur: the node
  367. *
  368. * Free a node, this is a recursive behaviour, all the children are freed too.
  369. * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
  370. */
  371. static void
  372. xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur) {
  373. xmlDictPtr dict;
  374. dict = reader->ctxt->dict;
  375. if (cur->type == XML_DTD_NODE) {
  376. xmlFreeDtd((xmlDtdPtr) cur);
  377. return;
  378. }
  379. if (cur->type == XML_NAMESPACE_DECL) {
  380. xmlFreeNs((xmlNsPtr) cur);
  381. return;
  382. }
  383. if (cur->type == XML_ATTRIBUTE_NODE) {
  384. xmlTextReaderFreeProp(reader, (xmlAttrPtr) cur);
  385. return;
  386. }
  387. if ((cur->children != NULL) &&
  388. (cur->type != XML_ENTITY_REF_NODE)) {
  389. if (cur->children->parent == cur)
  390. xmlTextReaderFreeNodeList(reader, cur->children);
  391. cur->children = NULL;
  392. }
  393. if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
  394. xmlDeregisterNodeDefaultValue(cur);
  395. if (((cur->type == XML_ELEMENT_NODE) ||
  396. (cur->type == XML_XINCLUDE_START) ||
  397. (cur->type == XML_XINCLUDE_END)) &&
  398. (cur->properties != NULL))
  399. xmlTextReaderFreePropList(reader, cur->properties);
  400. if ((cur->content != (xmlChar *) &(cur->properties)) &&
  401. (cur->type != XML_ELEMENT_NODE) &&
  402. (cur->type != XML_XINCLUDE_START) &&
  403. (cur->type != XML_XINCLUDE_END) &&
  404. (cur->type != XML_ENTITY_REF_NODE)) {
  405. DICT_FREE(cur->content);
  406. }
  407. if (((cur->type == XML_ELEMENT_NODE) ||
  408. (cur->type == XML_XINCLUDE_START) ||
  409. (cur->type == XML_XINCLUDE_END)) &&
  410. (cur->nsDef != NULL))
  411. xmlFreeNsList(cur->nsDef);
  412. /*
  413. * we don't free names here they are interned now
  414. */
  415. if ((cur->type != XML_TEXT_NODE) &&
  416. (cur->type != XML_COMMENT_NODE))
  417. DICT_FREE(cur->name);
  418. if (((cur->type == XML_ELEMENT_NODE) ||
  419. (cur->type == XML_TEXT_NODE)) &&
  420. (reader != NULL) && (reader->ctxt != NULL) &&
  421. (reader->ctxt->freeElemsNr < 100)) {
  422. cur->next = reader->ctxt->freeElems;
  423. reader->ctxt->freeElems = cur;
  424. reader->ctxt->freeElemsNr++;
  425. } else {
  426. xmlFree(cur);
  427. }
  428. }
  429. /**
  430. * xmlTextReaderFreeIDTable:
  431. * @table: An id table
  432. *
  433. * Deallocate the memory used by an ID hash table.
  434. */
  435. static void
  436. xmlTextReaderFreeIDTable(xmlIDTablePtr table) {
  437. xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
  438. }
  439. /**
  440. * xmlTextReaderFreeDoc:
  441. * @reader: the xmlTextReaderPtr used
  442. * @cur: pointer to the document
  443. *
  444. * Free up all the structures used by a document, tree included.
  445. */
  446. static void
  447. xmlTextReaderFreeDoc(xmlTextReaderPtr reader, xmlDocPtr cur) {
  448. xmlDtdPtr extSubset, intSubset;
  449. if (cur == NULL) return;
  450. if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
  451. xmlDeregisterNodeDefaultValue((xmlNodePtr) cur);
  452. /*
  453. * Do this before freeing the children list to avoid ID lookups
  454. */
  455. if (cur->ids != NULL) xmlTextReaderFreeIDTable((xmlIDTablePtr) cur->ids);
  456. cur->ids = NULL;
  457. if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
  458. cur->refs = NULL;
  459. extSubset = cur->extSubset;
  460. intSubset = cur->intSubset;
  461. if (intSubset == extSubset)
  462. extSubset = NULL;
  463. if (extSubset != NULL) {
  464. xmlUnlinkNode((xmlNodePtr) cur->extSubset);
  465. cur->extSubset = NULL;
  466. xmlFreeDtd(extSubset);
  467. }
  468. if (intSubset != NULL) {
  469. xmlUnlinkNode((xmlNodePtr) cur->intSubset);
  470. cur->intSubset = NULL;
  471. xmlFreeDtd(intSubset);
  472. }
  473. if (cur->children != NULL) xmlTextReaderFreeNodeList(reader, cur->children);
  474. if (cur->version != NULL) xmlFree((char *) cur->version);
  475. if (cur->name != NULL) xmlFree((char *) cur->name);
  476. if (cur->encoding != NULL) xmlFree((char *) cur->encoding);
  477. if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
  478. if (cur->URL != NULL) xmlFree((char *) cur->URL);
  479. if (cur->dict != NULL) xmlDictFree(cur->dict);
  480. xmlFree(cur);
  481. }
  482. /************************************************************************
  483. * *
  484. * The reader core parser *
  485. * *
  486. ************************************************************************/
  487. #ifdef DEBUG_READER
  488. static void
  489. xmlTextReaderDebug(xmlTextReaderPtr reader) {
  490. if ((reader == NULL) || (reader->ctxt == NULL)) {
  491. fprintf(stderr, "xmlTextReader NULL\n");
  492. return;
  493. }
  494. fprintf(stderr, "xmlTextReader: state %d depth %d ",
  495. reader->state, reader->depth);
  496. if (reader->node == NULL) {
  497. fprintf(stderr, "node = NULL\n");
  498. } else {
  499. fprintf(stderr, "node %s\n", reader->node->name);
  500. }
  501. fprintf(stderr, " input: base %d, cur %d, depth %d: ",
  502. reader->base, reader->cur, reader->ctxt->nodeNr);
  503. if (reader->input->buffer == NULL) {
  504. fprintf(stderr, "buffer is NULL\n");
  505. } else {
  506. #ifdef LIBXML_DEBUG_ENABLED
  507. xmlDebugDumpString(stderr,
  508. &reader->input->buffer->content[reader->cur]);
  509. #endif
  510. fprintf(stderr, "\n");
  511. }
  512. }
  513. #endif
  514. /**
  515. * xmlTextReaderEntPush:
  516. * @reader: the xmlTextReaderPtr used
  517. * @value: the entity reference node
  518. *
  519. * Pushes a new entity reference node on top of the entities stack
  520. *
  521. * Returns 0 in case of error, the index in the stack otherwise
  522. */
  523. static int
  524. xmlTextReaderEntPush(xmlTextReaderPtr reader, xmlNodePtr value)
  525. {
  526. if (reader->entMax <= 0) {
  527. reader->entMax = 10;
  528. reader->entTab = (xmlNodePtr *) xmlMalloc(reader->entMax *
  529. sizeof(reader->entTab[0]));
  530. if (reader->entTab == NULL) {
  531. xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
  532. return (0);
  533. }
  534. }
  535. if (reader->entNr >= reader->entMax) {
  536. reader->entMax *= 2;
  537. reader->entTab =
  538. (xmlNodePtr *) xmlRealloc(reader->entTab,
  539. reader->entMax *
  540. sizeof(reader->entTab[0]));
  541. if (reader->entTab == NULL) {
  542. xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
  543. return (0);
  544. }
  545. }
  546. reader->entTab[reader->entNr] = value;
  547. reader->ent = value;
  548. return (reader->entNr++);
  549. }
  550. /**
  551. * xmlTextReaderEntPop:
  552. * @reader: the xmlTextReaderPtr used
  553. *
  554. * Pops the top element entity from the entities stack
  555. *
  556. * Returns the entity just removed
  557. */
  558. static xmlNodePtr
  559. xmlTextReaderEntPop(xmlTextReaderPtr reader)
  560. {
  561. xmlNodePtr ret;
  562. if (reader->entNr <= 0)
  563. return (NULL);
  564. reader->entNr--;
  565. if (reader->entNr > 0)
  566. reader->ent = reader->entTab[reader->entNr - 1];
  567. else
  568. reader->ent = NULL;
  569. ret = reader->entTab[reader->entNr];
  570. reader->entTab[reader->entNr] = NULL;
  571. return (ret);
  572. }
  573. /**
  574. * xmlTextReaderStartElement:
  575. * @ctx: the user data (XML parser context)
  576. * @fullname: The element name, including namespace prefix
  577. * @atts: An array of name/value attributes pairs, NULL terminated
  578. *
  579. * called when an opening tag has been processed.
  580. */
  581. static void
  582. xmlTextReaderStartElement(void *ctx, const xmlChar *fullname,
  583. const xmlChar **atts) {
  584. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  585. xmlTextReaderPtr reader = ctxt->_private;
  586. #ifdef DEBUG_CALLBACKS
  587. printf("xmlTextReaderStartElement(%s)\n", fullname);
  588. #endif
  589. if ((reader != NULL) && (reader->startElement != NULL)) {
  590. reader->startElement(ctx, fullname, atts);
  591. if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
  592. (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
  593. (ctxt->input->cur[1] == '>'))
  594. ctxt->node->extra = NODE_IS_EMPTY;
  595. }
  596. if (reader != NULL)
  597. reader->state = XML_TEXTREADER_ELEMENT;
  598. }
  599. /**
  600. * xmlTextReaderEndElement:
  601. * @ctx: the user data (XML parser context)
  602. * @fullname: The element name, including namespace prefix
  603. *
  604. * called when an ending tag has been processed.
  605. */
  606. static void
  607. xmlTextReaderEndElement(void *ctx, const xmlChar *fullname) {
  608. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  609. xmlTextReaderPtr reader = ctxt->_private;
  610. #ifdef DEBUG_CALLBACKS
  611. printf("xmlTextReaderEndElement(%s)\n", fullname);
  612. #endif
  613. if ((reader != NULL) && (reader->endElement != NULL)) {
  614. reader->endElement(ctx, fullname);
  615. }
  616. }
  617. /**
  618. * xmlTextReaderStartElementNs:
  619. * @ctx: the user data (XML parser context)
  620. * @localname: the local name of the element
  621. * @prefix: the element namespace prefix if available
  622. * @URI: the element namespace name if available
  623. * @nb_namespaces: number of namespace definitions on that node
  624. * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
  625. * @nb_attributes: the number of attributes on that node
  626. * nb_defaulted: the number of defaulted attributes.
  627. * @attributes: pointer to the array of (localname/prefix/URI/value/end)
  628. * attribute values.
  629. *
  630. * called when an opening tag has been processed.
  631. */
  632. static void
  633. xmlTextReaderStartElementNs(void *ctx,
  634. const xmlChar *localname,
  635. const xmlChar *prefix,
  636. const xmlChar *URI,
  637. int nb_namespaces,
  638. const xmlChar **namespaces,
  639. int nb_attributes,
  640. int nb_defaulted,
  641. const xmlChar **attributes)
  642. {
  643. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  644. xmlTextReaderPtr reader = ctxt->_private;
  645. #ifdef DEBUG_CALLBACKS
  646. printf("xmlTextReaderStartElementNs(%s)\n", localname);
  647. #endif
  648. if ((reader != NULL) && (reader->startElementNs != NULL)) {
  649. reader->startElementNs(ctx, localname, prefix, URI, nb_namespaces,
  650. namespaces, nb_attributes, nb_defaulted,
  651. attributes);
  652. if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
  653. (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
  654. (ctxt->input->cur[1] == '>'))
  655. ctxt->node->extra = NODE_IS_EMPTY;
  656. }
  657. if (reader != NULL)
  658. reader->state = XML_TEXTREADER_ELEMENT;
  659. }
  660. /**
  661. * xmlTextReaderEndElementNs:
  662. * @ctx: the user data (XML parser context)
  663. * @localname: the local name of the element
  664. * @prefix: the element namespace prefix if available
  665. * @URI: the element namespace name if available
  666. *
  667. * called when an ending tag has been processed.
  668. */
  669. static void
  670. xmlTextReaderEndElementNs(void *ctx,
  671. const xmlChar * localname,
  672. const xmlChar * prefix,
  673. const xmlChar * URI)
  674. {
  675. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  676. xmlTextReaderPtr reader = ctxt->_private;
  677. #ifdef DEBUG_CALLBACKS
  678. printf("xmlTextReaderEndElementNs(%s)\n", localname);
  679. #endif
  680. if ((reader != NULL) && (reader->endElementNs != NULL)) {
  681. reader->endElementNs(ctx, localname, prefix, URI);
  682. }
  683. }
  684. /**
  685. * xmlTextReaderCharacters:
  686. * @ctx: the user data (XML parser context)
  687. * @ch: a xmlChar string
  688. * @len: the number of xmlChar
  689. *
  690. * receiving some chars from the parser.
  691. */
  692. static void
  693. xmlTextReaderCharacters(void *ctx, const xmlChar *ch, int len)
  694. {
  695. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  696. xmlTextReaderPtr reader = ctxt->_private;
  697. #ifdef DEBUG_CALLBACKS
  698. printf("xmlTextReaderCharacters()\n");
  699. #endif
  700. if ((reader != NULL) && (reader->characters != NULL)) {
  701. reader->characters(ctx, ch, len);
  702. }
  703. }
  704. /**
  705. * xmlTextReaderCDataBlock:
  706. * @ctx: the user data (XML parser context)
  707. * @value: The pcdata content
  708. * @len: the block length
  709. *
  710. * called when a pcdata block has been parsed
  711. */
  712. static void
  713. xmlTextReaderCDataBlock(void *ctx, const xmlChar *ch, int len)
  714. {
  715. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  716. xmlTextReaderPtr reader = ctxt->_private;
  717. #ifdef DEBUG_CALLBACKS
  718. printf("xmlTextReaderCDataBlock()\n");
  719. #endif
  720. if ((reader != NULL) && (reader->cdataBlock != NULL)) {
  721. reader->cdataBlock(ctx, ch, len);
  722. }
  723. }
  724. /**
  725. * xmlTextReaderPushData:
  726. * @reader: the xmlTextReaderPtr used
  727. *
  728. * Push data down the progressive parser until a significant callback
  729. * got raised.
  730. *
  731. * Returns -1 in case of failure, 0 otherwise
  732. */
  733. static int
  734. xmlTextReaderPushData(xmlTextReaderPtr reader) {
  735. xmlBufferPtr inbuf;
  736. int val, s;
  737. xmlTextReaderState oldstate;
  738. if ((reader->input == NULL) || (reader->input->buffer == NULL))
  739. return(-1);
  740. oldstate = reader->state;
  741. reader->state = XML_TEXTREADER_NONE;
  742. inbuf = reader->input->buffer;
  743. while (reader->state == XML_TEXTREADER_NONE) {
  744. if (inbuf->use < reader->cur + CHUNK_SIZE) {
  745. /*
  746. * Refill the buffer unless we are at the end of the stream
  747. */
  748. if (reader->mode != XML_TEXTREADER_MODE_EOF) {
  749. val = xmlParserInputBufferRead(reader->input, 4096);
  750. if ((val == 0) &&
  751. (inbuf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)) {
  752. if (inbuf->use == reader->cur) {
  753. reader->mode = XML_TEXTREADER_MODE_EOF;
  754. reader->state = oldstate;
  755. }
  756. } else if (val < 0) {
  757. reader->mode = XML_TEXTREADER_MODE_EOF;
  758. reader->state = oldstate;
  759. if ((oldstate != XML_TEXTREADER_START) ||
  760. (reader->ctxt->myDoc != NULL))
  761. return(val);
  762. } else if (val == 0) {
  763. /* mark the end of the stream and process the remains */
  764. reader->mode = XML_TEXTREADER_MODE_EOF;
  765. break;
  766. }
  767. } else
  768. break;
  769. }
  770. /*
  771. * parse by block of CHUNK_SIZE bytes, various tests show that
  772. * it's the best tradeoff at least on a 1.2GH Duron
  773. */
  774. if (inbuf->use >= reader->cur + CHUNK_SIZE) {
  775. val = xmlParseChunk(reader->ctxt,
  776. (const char *) &inbuf->content[reader->cur],
  777. CHUNK_SIZE, 0);
  778. reader->cur += CHUNK_SIZE;
  779. if ((val != 0) || (reader->ctxt->wellFormed == 0))
  780. return(-1);
  781. } else {
  782. s = inbuf->use - reader->cur;
  783. val = xmlParseChunk(reader->ctxt,
  784. (const char *) &inbuf->content[reader->cur],
  785. s, 0);
  786. reader->cur += s;
  787. if ((val != 0) || (reader->ctxt->wellFormed == 0))
  788. return(-1);
  789. break;
  790. }
  791. }
  792. /*
  793. * Discard the consumed input when needed and possible
  794. */
  795. if (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE) {
  796. if (inbuf->alloc != XML_BUFFER_ALLOC_IMMUTABLE) {
  797. if ((reader->cur >= 4096) &&
  798. (inbuf->use - reader->cur <= CHUNK_SIZE)) {
  799. val = xmlBufferShrink(inbuf, reader->cur);
  800. if (val >= 0) {
  801. reader->cur -= val;
  802. }
  803. }
  804. }
  805. }
  806. /*
  807. * At the end of the stream signal that the work is done to the Push
  808. * parser.
  809. */
  810. else if (reader->mode == XML_TEXTREADER_MODE_EOF) {
  811. if (reader->state != XML_TEXTREADER_DONE) {
  812. s = inbuf->use - reader->cur;
  813. val = xmlParseChunk(reader->ctxt,
  814. (const char *) &inbuf->content[reader->cur],
  815. s, 1);
  816. reader->cur = inbuf->use;
  817. reader->state = XML_TEXTREADER_DONE;
  818. if ((val != 0) || (reader->ctxt->wellFormed == 0))
  819. return(-1);
  820. }
  821. }
  822. reader->state = oldstate;
  823. return(0);
  824. }
  825. #ifdef LIBXML_REGEXP_ENABLED
  826. /**
  827. * xmlTextReaderValidatePush:
  828. * @reader: the xmlTextReaderPtr used
  829. *
  830. * Push the current node for validation
  831. */
  832. static void
  833. xmlTextReaderValidatePush(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
  834. xmlNodePtr node = reader->node;
  835. #ifdef LIBXML_VALID_ENABLED
  836. if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
  837. (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
  838. if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
  839. reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
  840. reader->ctxt->myDoc, node, node->name);
  841. } else {
  842. /* TODO use the BuildQName interface */
  843. xmlChar *qname;
  844. qname = xmlStrdup(node->ns->prefix);
  845. qname = xmlStrcat(qname, BAD_CAST ":");
  846. qname = xmlStrcat(qname, node->name);
  847. reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
  848. reader->ctxt->myDoc, node, qname);
  849. if (qname != NULL)
  850. xmlFree(qname);
  851. }
  852. }
  853. #endif /* LIBXML_VALID_ENABLED */
  854. #ifdef LIBXML_SCHEMAS_ENABLED
  855. if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
  856. (reader->rngValidCtxt != NULL)) {
  857. int ret;
  858. if (reader->rngFullNode != NULL) return;
  859. ret = xmlRelaxNGValidatePushElement(reader->rngValidCtxt,
  860. reader->ctxt->myDoc,
  861. node);
  862. if (ret == 0) {
  863. /*
  864. * this element requires a full tree
  865. */
  866. node = xmlTextReaderExpand(reader);
  867. if (node == NULL) {
  868. printf("Expand failed !\n");
  869. ret = -1;
  870. } else {
  871. ret = xmlRelaxNGValidateFullElement(reader->rngValidCtxt,
  872. reader->ctxt->myDoc,
  873. node);
  874. reader->rngFullNode = node;
  875. }
  876. }
  877. if (ret != 1)
  878. reader->rngValidErrors++;
  879. }
  880. #endif
  881. }
  882. /**
  883. * xmlTextReaderValidateCData:
  884. * @reader: the xmlTextReaderPtr used
  885. * @data: pointer to the CData
  886. * @len: lenght of the CData block in bytes.
  887. *
  888. * Push some CData for validation
  889. */
  890. static void
  891. xmlTextReaderValidateCData(xmlTextReaderPtr reader,
  892. const xmlChar *data, int len) {
  893. #ifdef LIBXML_VALID_ENABLED
  894. if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
  895. (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
  896. reader->ctxt->valid &= xmlValidatePushCData(&reader->ctxt->vctxt,
  897. data, len);
  898. }
  899. #endif /* LIBXML_VALID_ENABLED */
  900. #ifdef LIBXML_SCHEMAS_ENABLED
  901. if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
  902. (reader->rngValidCtxt != NULL)) {
  903. int ret;
  904. if (reader->rngFullNode != NULL) return;
  905. ret = xmlRelaxNGValidatePushCData(reader->rngValidCtxt, data, len);
  906. if (ret != 1)
  907. reader->rngValidErrors++;
  908. }
  909. #endif
  910. }
  911. /**
  912. * xmlTextReaderValidatePop:
  913. * @reader: the xmlTextReaderPtr used
  914. *
  915. * Pop the current node from validation
  916. */
  917. static void
  918. xmlTextReaderValidatePop(xmlTextReaderPtr reader) {
  919. xmlNodePtr node = reader->node;
  920. #ifdef LIBXML_VALID_ENABLED
  921. if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
  922. (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
  923. if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
  924. reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
  925. reader->ctxt->myDoc, node, node->name);
  926. } else {
  927. /* TODO use the BuildQName interface */
  928. xmlChar *qname;
  929. qname = xmlStrdup(node->ns->prefix);
  930. qname = xmlStrcat(qname, BAD_CAST ":");
  931. qname = xmlStrcat(qname, node->name);
  932. reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
  933. reader->ctxt->myDoc, node, qname);
  934. if (qname != NULL)
  935. xmlFree(qname);
  936. }
  937. }
  938. #endif /* LIBXML_VALID_ENABLED */
  939. #ifdef LIBXML_SCHEMAS_ENABLED
  940. if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
  941. (reader->rngValidCtxt != NULL)) {
  942. int ret;
  943. if (reader->rngFullNode != NULL) {
  944. if (node == reader->rngFullNode)
  945. reader->rngFullNode = NULL;
  946. return;
  947. }
  948. ret = xmlRelaxNGValidatePopElement(reader->rngValidCtxt,
  949. reader->ctxt->myDoc,
  950. node);
  951. if (ret != 1)
  952. reader->rngValidErrors++;
  953. }
  954. #endif
  955. }
  956. /**
  957. * xmlTextReaderValidateEntity:
  958. * @reader: the xmlTextReaderPtr used
  959. *
  960. * Handle the validation when an entity reference is encountered and
  961. * entity substitution is not activated. As a result the parser interface
  962. * must walk through the entity and do the validation calls
  963. */
  964. static void
  965. xmlTextReaderValidateEntity(xmlTextReaderPtr reader) {
  966. xmlNodePtr oldnode = reader->node;
  967. xmlNodePtr node = reader->node;
  968. xmlParserCtxtPtr ctxt = reader->ctxt;
  969. do {
  970. if (node->type == XML_ENTITY_REF_NODE) {
  971. /*
  972. * Case where the underlying tree is not availble, lookup the entity
  973. * and walk it.
  974. */
  975. if ((node->children == NULL) && (ctxt->sax != NULL) &&
  976. (ctxt->sax->getEntity != NULL)) {
  977. node->children = (xmlNodePtr)
  978. ctxt->sax->getEntity(ctxt, node->name);
  979. }
  980. if ((node->children != NULL) &&
  981. (node->children->type == XML_ENTITY_DECL) &&
  982. (node->children->children != NULL)) {
  983. xmlTextReaderEntPush(reader, node);
  984. node = node->children->children;
  985. continue;
  986. } else {
  987. /*
  988. * The error has probably be raised already.
  989. */
  990. if (node == oldnode)
  991. break;
  992. node = node->next;
  993. }
  994. #ifdef LIBXML_REGEXP_ENABLED
  995. } else if (node->type == XML_ELEMENT_NODE) {
  996. reader->node = node;
  997. xmlTextReaderValidatePush(reader);
  998. } else if ((node->type == XML_TEXT_NODE) ||
  999. (node->type == XML_CDATA_SECTION_NODE)) {
  1000. xmlTextReaderValidateCData(reader, node->content,
  1001. xmlStrlen(node->content));
  1002. #endif
  1003. }
  1004. /*
  1005. * go to next node
  1006. */
  1007. if (node->children != NULL) {
  1008. node = node->children;
  1009. continue;
  1010. } else if (node->type == XML_ELEMENT_NODE) {
  1011. xmlTextReaderValidatePop(reader);
  1012. }
  1013. if (node->next != NULL) {
  1014. node = node->next;
  1015. continue;
  1016. }
  1017. do {
  1018. node = node->parent;
  1019. if (node->type == XML_ELEMENT_NODE) {
  1020. xmlNodePtr tmp;
  1021. if (reader->entNr == 0) {
  1022. while ((tmp = node->last) != NULL) {
  1023. if ((tmp->extra & NODE_IS_PRESERVED) == 0) {
  1024. xmlUnlinkNode(tmp);
  1025. xmlTextReaderFreeNode(reader, tmp);
  1026. } else
  1027. break;
  1028. }
  1029. }
  1030. reader->node = node;
  1031. xmlTextReaderValidatePop(reader);
  1032. }
  1033. if ((node->type == XML_ENTITY_DECL) &&
  1034. (reader->ent != NULL) && (reader->ent->children == node)) {
  1035. node = xmlTextReaderEntPop(reader);
  1036. }
  1037. if (node == oldnode)
  1038. break;
  1039. if (node->next != NULL) {
  1040. node = node->next;
  1041. break;
  1042. }
  1043. } while ((node != NULL) && (node != oldnode));
  1044. } while ((node != NULL) && (node != oldnode));
  1045. reader->node = oldnode;
  1046. }
  1047. #endif /* LIBXML_REGEXP_ENABLED */
  1048. /**
  1049. * xmlTextReaderGetSuccessor:
  1050. * @cur: the current node
  1051. *
  1052. * Get the successor of a node if available.
  1053. *
  1054. * Returns the successor node or NULL
  1055. */
  1056. static xmlNodePtr
  1057. xmlTextReaderGetSuccessor(xmlNodePtr cur) {
  1058. if (cur == NULL) return(NULL) ; /* ERROR */
  1059. if (cur->next != NULL) return(cur->next) ;
  1060. do {
  1061. cur = cur->parent;
  1062. if (cur == NULL) break;
  1063. if (cur->next != NULL) return(cur->next);
  1064. } while (cur != NULL);
  1065. return(cur);
  1066. }
  1067. /**
  1068. * xmlTextReaderDoExpand:
  1069. * @reader: the xmlTextReaderPtr used
  1070. *
  1071. * Makes sure that the current node is fully read as well as all its
  1072. * descendant. It means the full DOM subtree must be available at the
  1073. * end of the call.
  1074. *
  1075. * Returns 1 if the node was expanded successfully, 0 if there is no more
  1076. * nodes to read, or -1 in case of error
  1077. */
  1078. static int
  1079. xmlTextReaderDoExpand(xmlTextReaderPtr reader) {
  1080. int val;
  1081. if ((reader == NULL) || (reader->node == NULL) || (reader->ctxt == NULL))
  1082. return(-1);
  1083. do {
  1084. if (reader->ctxt->instate == XML_PARSER_EOF) return(1);
  1085. if (xmlTextReaderGetSuccessor(reader->node) != NULL)
  1086. return(1);
  1087. if (reader->ctxt->nodeNr < reader->depth)
  1088. return(1);
  1089. if (reader->mode == XML_TEXTREADER_MODE_EOF)
  1090. return(1);
  1091. val = xmlTextReaderPushData(reader);
  1092. if (val < 0){
  1093. reader->mode = XML_TEXTREADER_MODE_ERROR;
  1094. return(-1);
  1095. }
  1096. } while(reader->mode != XML_TEXTREADER_MODE_EOF);
  1097. return(1);
  1098. }
  1099. /**
  1100. * xmlTextReaderCollectSiblings:
  1101. * @node: the first child
  1102. *
  1103. * Traverse depth-first through all sibling nodes and their children
  1104. * nodes and concatenate their content. This is an auxiliary function
  1105. * to xmlTextReaderReadString.
  1106. *
  1107. * Returns a string containing the content, or NULL in case of error.
  1108. */
  1109. static xmlChar *
  1110. xmlTextReaderCollectSiblings(xmlNodePtr node)
  1111. {
  1112. xmlBufferPtr buffer;
  1113. xmlChar *ret;
  1114. buffer = xmlBufferCreate();
  1115. if (buffer == NULL)
  1116. return NULL;
  1117. for ( ; node != NULL; node = node->next) {
  1118. switch (node->type) {
  1119. case XML_TEXT_NODE:
  1120. case XML_CDATA_SECTION_NODE:
  1121. xmlBufferCat(buffer, node->content);
  1122. break;
  1123. case XML_ELEMENT_NODE: {
  1124. xmlChar *tmp;
  1125. tmp = xmlTextReaderCollectSiblings(node->children);
  1126. xmlBufferCat(buffer, tmp);
  1127. xmlFree(tmp);
  1128. break;
  1129. }
  1130. default:
  1131. break;
  1132. }
  1133. }
  1134. ret = buffer->content;
  1135. buffer->content = NULL;
  1136. xmlBufferFree(buffer);
  1137. return(ret);
  1138. }
  1139. /**
  1140. * xmlTextReaderRead:
  1141. * @reader: the xmlTextReaderPtr used
  1142. *
  1143. * Moves the position of the current instance to the next node in
  1144. * the stream, exposing its properties.
  1145. *
  1146. * Returns 1 if the node was read successfully, 0 if there is no more
  1147. * nodes to read, or -1 in case of error
  1148. */
  1149. int
  1150. xmlTextReaderRead(xmlTextReaderPtr reader) {
  1151. int val, olddepth = 0;
  1152. xmlTextReaderState oldstate = XML_TEXTREADER_START;
  1153. xmlNodePtr oldnode = NULL;
  1154. if (reader == NULL)
  1155. return(-1);
  1156. reader->curnode = NULL;
  1157. if (reader->doc != NULL)
  1158. return(xmlTextReaderReadTree(reader));
  1159. if (reader->ctxt == NULL)
  1160. return(-1);
  1161. if (reader->ctxt->wellFormed != 1)
  1162. return(-1);
  1163. #ifdef DEBUG_READER
  1164. fprintf(stderr, "\nREAD ");
  1165. DUMP_READER
  1166. #endif
  1167. if (reader->mode == XML_TEXTREADER_MODE_INITIAL) {
  1168. reader->mode = XML_TEXTREADER_MODE_INTERACTIVE;
  1169. /*
  1170. * Initial state
  1171. */
  1172. do {
  1173. val = xmlTextReaderPushData(reader);
  1174. if (val < 0){
  1175. reader->mode = XML_TEXTREADER_MODE_ERROR;
  1176. reader->state = XML_TEXTREADER_ERROR;
  1177. return(-1);
  1178. }
  1179. } while ((reader->ctxt->node == NULL) &&
  1180. ((reader->mode != XML_TEXTREADER_MODE_EOF) &&
  1181. (reader->state != XML_TEXTREADER_DONE)));
  1182. if (reader->ctxt->node == NULL) {
  1183. if (reader->ctxt->myDoc != NULL) {
  1184. reader->node = reader->ctxt->myDoc->children;
  1185. }
  1186. if (reader->node == NULL){
  1187. reader->mode = XML_TEXTREADER_MODE_ERROR;
  1188. reader->state = XML_TEXTREADER_ERROR;
  1189. return(-1);
  1190. }
  1191. reader->state = XML_TEXTREADER_ELEMENT;
  1192. } else {
  1193. if (reader->ctxt->myDoc != NULL) {
  1194. reader->node = reader->ctxt->myDoc->children;
  1195. }
  1196. if (reader->node == NULL)
  1197. reader->node = reader->ctxt->nodeTab[0];
  1198. reader->state = XML_TEXTREADER_ELEMENT;
  1199. }
  1200. reader->depth = 0;
  1201. reader->ctxt->parseMode = XML_PARSE_READER;
  1202. goto node_found;
  1203. }
  1204. oldstate = reader->state;
  1205. olddepth = reader->ctxt->nodeNr;
  1206. oldnode = reader->node;
  1207. get_next_node:
  1208. if (reader->node == NULL) {
  1209. if (reader->mode == XML_TEXTREADER_MODE_EOF)
  1210. return(0);
  1211. else
  1212. return(-1);
  1213. }
  1214. /*
  1215. * If we are not backtracking on ancestors or examined nodes,
  1216. * that the parser didn't finished or that we arent at the end
  1217. * of stream, continue processing.
  1218. */
  1219. while ((reader->node != NULL) && (reader->node->next == NULL) &&
  1220. (reader->ctxt->nodeNr == olddepth) &&
  1221. ((oldstate == XML_TEXTREADER_BACKTRACK) ||
  1222. (reader->node->children == NULL) ||
  1223. (reader->node->type == XML_ENTITY_REF_NODE) ||
  1224. ((reader->node->children != NULL) &&
  1225. (reader->node->children->type == XML_TEXT_NODE) &&
  1226. (reader->node->children->next == NULL)) ||
  1227. (reader->node->type == XML_DTD_NODE) ||
  1228. (reader->node->type == XML_DOCUMENT_NODE) ||
  1229. (reader->node->type == XML_HTML_DOCUMENT_NODE)) &&
  1230. ((reader->ctxt->node == NULL) ||
  1231. (reader->ctxt->node == reader->node) ||
  1232. (reader->ctxt->node == reader->node->parent)) &&
  1233. (reader->ctxt->instate != XML_PARSER_EOF)) {
  1234. val = xmlTextReaderPushData(reader);
  1235. if (val < 0){
  1236. reader->mode = XML_TEXTREADER_MODE_ERROR;
  1237. reader->state = XML_TEXTREADER_ERROR;
  1238. return(-1);
  1239. }
  1240. if (reader->node == NULL)
  1241. goto node_end;
  1242. }
  1243. if (oldstate != XML_TEXTREADER_BACKTRACK) {
  1244. if ((reader->node->children != NULL) &&
  1245. (reader->node->type != XML_ENTITY_REF_NODE) &&
  1246. (reader->node->type != XML_XINCLUDE_START) &&
  1247. (reader->node->type != XML_DTD_NODE)) {
  1248. reader->node = reader->node->children;
  1249. reader->depth++;
  1250. reader->state = XML_TEXTREADER_ELEMENT;
  1251. goto node_found;
  1252. }
  1253. }
  1254. if (reader->node->next != NULL) {
  1255. if ((oldstate == XML_TEXTREADER_ELEMENT) &&
  1256. (reader->node->type == XML_ELEMENT_NODE) &&
  1257. (reader->node->children == NULL) &&
  1258. ((reader->node->extra & NODE_IS_EMPTY) == 0)
  1259. #ifdef LIBXML_XINCLUDE_ENABLED
  1260. && (reader->in_xinclude <= 0)
  1261. #endif
  1262. ) {
  1263. reader->state = XML_TEXTREADER_END;
  1264. goto node_found;
  1265. }
  1266. #ifdef LIBXML_REGEXP_ENABLED
  1267. if ((reader->validate) &&
  1268. (reader->node->type == XML_ELEMENT_NODE))
  1269. xmlTextReaderValidatePop(reader);
  1270. #endif /* LIBXML_REGEXP_ENABLED */
  1271. if ((reader->preserves > 0) &&
  1272. (reader->node->extra & NODE_IS_SPRESERVED))
  1273. reader->preserves--;
  1274. reader->node = reader->node->next;
  1275. reader->state = XML_TEXTREADER_ELEMENT;
  1276. /*
  1277. * Cleanup of the old node
  1278. */
  1279. if ((reader->preserves == 0) &&
  1280. #ifdef LIBXML_XINCLUDE_ENABLED
  1281. (reader->in_xinclude == 0) &&
  1282. #endif
  1283. (reader->entNr == 0) &&
  1284. (reader->node->prev != NULL) &&
  1285. (reader->node->prev->type != XML_DTD_NODE) &&
  1286. (reader->entNr == 0)) {
  1287. xmlNodePtr tmp = reader->node->prev;
  1288. if ((tmp->extra & NODE_IS_PRESERVED) == 0) {
  1289. xmlUnlinkNode(tmp);
  1290. xmlTextReaderFreeNode(reader, tmp);
  1291. }
  1292. }
  1293. goto node_found;
  1294. }
  1295. if ((oldstate == XML_TEXTREADER_ELEMENT) &&
  1296. (reader->node->type == XML_ELEMENT_NODE) &&
  1297. (reader->node->children == NULL) &&
  1298. ((reader->node->extra & NODE_IS_EMPTY) == 0)) {;
  1299. reader->state = XML_TEXTREADER_END;
  1300. goto node_found;
  1301. }
  1302. #ifdef LIBXML_REGEXP_ENABLED
  1303. if ((reader->validate) && (reader->node->type == XML_ELEMENT_NODE))
  1304. xmlTextReaderValidatePop(reader);
  1305. #endif /* LIBXML_REGEXP_ENABLED */
  1306. if ((reader->preserves > 0) &&
  1307. (reader->node->extra & NODE_IS_SPRESERVED))
  1308. reader->preserves--;
  1309. reader->node = reader->node->parent;
  1310. if ((reader->node == NULL) ||
  1311. (reader->node->type == XML_DOCUMENT_NODE) ||
  1312. #ifdef LIBXML_DOCB_ENABLED
  1313. (reader->node->type == XML_DOCB_DOCUMENT_NODE) ||
  1314. #endif
  1315. (reader->node->type == XML_HTML_DOCUMENT_NODE)) {
  1316. if (reader->mode != XML_TEXTREADER_MODE_EOF) {
  1317. val = xmlParseChunk(reader->ctxt, "", 0, 1);
  1318. reader->state = XML_TEXTREADER_DONE;
  1319. if (val != 0)
  1320. return(-1);
  1321. }
  1322. reader->node = NULL;
  1323. reader->depth = -1;
  1324. /*
  1325. * Cleanup of the old node
  1326. */
  1327. if ((oldnode != NULL) && (reader->preserves == 0) &&
  1328. #ifdef LIBXML_XINCLUDE_ENABLED
  1329. (reader->in_xinclude == 0) &&
  1330. #endif
  1331. (reader->entNr == 0) &&
  1332. (oldnode->type != XML_DTD_NODE) &&
  1333. ((oldnode->extra & NODE_IS_PRESERVED) == 0) &&
  1334. (reader->entNr == 0)) {
  1335. xmlUnlinkNode(oldnode);
  1336. xmlTextReaderFreeNode(reader, oldnode);
  1337. }
  1338. goto node_end;
  1339. }
  1340. if ((reader->preserves == 0) &&
  1341. #ifdef LIBXML_XINCLUDE_ENABLED
  1342. (reader->in_xinclude == 0) &&
  1343. #endif
  1344. (reader->entNr == 0) &&
  1345. (reader->node->last != NULL) &&
  1346. ((reader->node->last->extra & NODE_IS_PRESERVED) == 0)) {
  1347. xmlNodePtr tmp = reader->node->last;
  1348. xmlUnlinkNode(tmp);
  1349. xmlTextReaderFreeNode(reader, tmp);
  1350. }
  1351. reader->depth--;
  1352. reader->state = XML_TEXTREADER_BACKTRACK;
  1353. node_found:
  1354. DUMP_READER
  1355. /*
  1356. * If we are in the middle of a piece of CDATA make sure it's finished
  1357. */
  1358. if ((reader->node != NULL) &&
  1359. (reader->node->next == NULL) &&
  1360. ((reader->node->type == XML_TEXT_NODE) ||
  1361. (reader->node->type == XML_CDATA_SECTION_NODE))) {
  1362. if (xmlTextReaderExpand(reader) == NULL)
  1363. return -1;
  1364. }
  1365. #ifdef LIBXML_XINCLUDE_ENABLED
  1366. /*
  1367. * Handle XInclude if asked for
  1368. */
  1369. if ((reader->xinclude) && (reader->node != NULL) &&
  1370. (reader->node->type == XML_ELEMENT_NODE) &&
  1371. (reader->node->ns != NULL) &&
  1372. ((xmlStrEqual(reader->node->ns->href, XINCLUDE_NS)) ||
  1373. (xmlStrEqual(reader->node->ns->href, XINCLUDE_OLD_NS)))) {
  1374. if (reader->xincctxt == NULL) {
  1375. reader->xincctxt = xmlXIncludeNewContext(reader->ctxt->myDoc);
  1376. xmlXIncludeSetFlags(reader->xincctxt,
  1377. reader->parserFlags & (~XML_PARSE_NOXINCNODE));
  1378. }
  1379. /*
  1380. * expand that node and process it
  1381. */
  1382. if (xmlTextReaderExpand(reader) == NULL)
  1383. return -1;
  1384. xmlXIncludeProcessNode(reader->xincctxt, reader->node);
  1385. }
  1386. if ((reader->node != NULL) && (reader->node->type == XML_XINCLUDE_START)) {
  1387. reader->in_xinclude++;
  1388. goto get_next_node;
  1389. }
  1390. if ((reader->node != NULL) && (reader->node->type == XML_XINCLUDE_END)) {
  1391. reader->in_xinclude--;
  1392. goto get_next_node;
  1393. }
  1394. #endif
  1395. /*
  1396. * Handle entities enter and exit when in entity replacement mode
  1397. */
  1398. if ((reader->node != NULL) &&
  1399. (reader->node->type == XML_ENTITY_REF_NODE) &&
  1400. (reader->ctxt != NULL) && (reader->ctxt->replaceEntities == 1)) {
  1401. /*
  1402. * Case where the underlying tree is not availble, lookup the entity
  1403. * and walk it.
  1404. */
  1405. if ((reader->node->children == NULL) && (reader->ctxt->sax != NULL) &&
  1406. (reader->ctxt->sax->getEntity != NULL)) {
  1407. reader->node->children = (xmlNodePtr)
  1408. reader->ctxt->sax->getEntity(reader->ctxt, reader->node->name);
  1409. }
  1410. if ((reader->node->children != NULL) &&
  1411. (reader->node->children->type == XML_ENTITY_DECL) &&
  1412. (reader->node->children->children != NULL)) {
  1413. xmlTextReaderEntPush(reader, reader->node);
  1414. reader->node = reader->node->children->children;
  1415. }
  1416. #ifdef LIBXML_REGEXP_ENABLED
  1417. } else if ((reader->node != NULL) &&
  1418. (reader->node->type == XML_ENTITY_REF_NODE) &&
  1419. (reader->ctxt != NULL) && (reader->validate)) {
  1420. xmlTextReaderValidateEntity(reader);
  1421. #endif /* LIBXML_REGEXP_ENABLED */
  1422. }
  1423. if ((reader->node != NULL) &&
  1424. (reader->node->type == XML_ENTITY_DECL) &&
  1425. (reader->ent != NULL) && (reader->ent->children == reader->node)) {
  1426. reader->node = xmlTextReaderEntPop(reader);
  1427. reader->depth++;
  1428. goto get_next_node;
  1429. }
  1430. #ifdef LIBXML_REGEXP_ENABLED
  1431. if ((reader->validate) && (reader->node != NULL)) {
  1432. xmlNodePtr node = reader->node;
  1433. if ((node->type == XML_ELEMENT_NODE) &&
  1434. ((reader->state != XML_TEXTREADER_END) &&
  1435. (reader->state != XML_TEXTREADER_BACKTRACK))) {
  1436. xmlTextReaderValidatePush(reader);
  1437. } else if ((node->type == XML_TEXT_NODE) ||
  1438. (node->type == XML_CDATA_SECTION_NODE)) {
  1439. xmlTextReaderValidateCData(reader, node->content,
  1440. xmlStrlen(node->content));
  1441. }
  1442. }
  1443. #endif /* LIBXML_REGEXP_ENABLED */
  1444. #ifdef LIBXML_PATTERN_ENABLED
  1445. if ((reader->patternNr > 0) && (reader->state != XML_TEXTREADER_END) &&
  1446. (reader->state != XML_TEXTREADER_BACKTRACK)) {
  1447. int i;
  1448. for (i = 0;i < reader->patternNr;i++) {
  1449. if (xmlPatternMatch(reader->patternTab[i], reader->node) == 1) {
  1450. xmlTextReaderPreserve(reader);
  1451. break;
  1452. }
  1453. }
  1454. }
  1455. #endif /* LIBXML_PATTERN_ENABLED */
  1456. #ifdef LIBXML_SCHEMAS_ENABLED
  1457. if ((reader->validate == XML_TEXTREADER_VALIDATE_XSD) &&
  1458. (reader->xsdValidErrors == 0) &&
  1459. (reader->xsdValidCtxt != NULL)) {
  1460. reader->xsdValidErrors = !xmlSchemaIsValid(reader->xsdValidCtxt);
  1461. }
  1462. #endif /* LIBXML_PATTERN_ENABLED */
  1463. return(1);
  1464. node_end:
  1465. reader->state = XML_TEXTREADER_DONE;
  1466. return(0);
  1467. }
  1468. /**
  1469. * xmlTextReaderReadState:
  1470. * @reader: the xmlTextReaderPtr used
  1471. *
  1472. * Gets the read state of the reader.
  1473. *
  1474. * Returns the state value, or -1 in case of error
  1475. */
  1476. int
  1477. xmlTextReaderReadState(xmlTextReaderPtr reader) {
  1478. if (reader == NULL)
  1479. return(-1);
  1480. return(reader->mode);
  1481. }
  1482. /**
  1483. * xmlTextReaderExpand:
  1484. * @reader: the xmlTextReaderPtr used
  1485. *
  1486. * Reads the contents of the current node and the full subtree. It then makes
  1487. * the subtree available until the next xmlTextReaderRead() call
  1488. *
  1489. * Returns a node pointer valid until the next xmlTextReaderRead() call
  1490. * or NULL in case of error.
  1491. */
  1492. xmlNodePtr
  1493. xmlTextReaderExpand(xmlTextReaderPtr reader) {
  1494. if ((reader == NULL) || (reader->node == NULL))
  1495. return(NULL);
  1496. if (reader->doc != NULL)
  1497. return(reader->node);
  1498. if (reader->ctxt == NULL)
  1499. return(NULL);
  1500. if (xmlTextReaderDoExpand(reader) < 0)
  1501. return(NULL);
  1502. return(reader->node);
  1503. }
  1504. /**
  1505. * xmlTextReaderNext:
  1506. * @reader: the xmlTextReaderPtr used
  1507. *
  1508. * Skip to the node following the current one in document order while
  1509. * avoiding the subtree if any.
  1510. *
  1511. * Returns 1 if the node was read successfully, 0 if there is no more
  1512. * nodes to read, or -1 in case of error
  1513. */
  1514. int
  1515. xmlTextReaderNext(xmlTextReaderPtr reader) {
  1516. int ret;
  1517. xmlNodePtr cur;
  1518. if (reader == NULL)
  1519. return(-1);
  1520. if (reader->doc != NULL)
  1521. return(xmlTextReaderNextTree(reader));
  1522. cur = reader->node;
  1523. if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
  1524. return(xmlTextReaderRead(reader));
  1525. if (reader->state == XML_TEXTREADER_END || reader->state == XML_TEXTREADER_BACKTRACK)
  1526. return(xmlTextReaderRead(reader));
  1527. if (cur->extra & NODE_IS_EMPTY)
  1528. return(xmlTextReaderRead(reader));
  1529. do {
  1530. ret = xmlTextReaderRead(reader);
  1531. if (ret != 1)
  1532. return(ret);
  1533. } while (reader->node != cur);
  1534. return(xmlTextReaderRead(reader));
  1535. }
  1536. #ifdef LIBXML_WRITER_ENABLED
  1537. /**
  1538. * xmlTextReaderReadInnerXml:
  1539. * @reader: the xmlTextReaderPtr used
  1540. *
  1541. * Reads the contents of the current node, including child nodes and markup.
  1542. *
  1543. * Returns a string containing the XML content, or NULL if the current node
  1544. * is neither an element nor attribute, or has no child nodes. The
  1545. * string must be deallocated by the caller.
  1546. */
  1547. xmlChar *
  1548. xmlTextReaderReadInnerXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED)
  1549. {
  1550. xmlChar *resbuf;
  1551. xmlNodePtr node, cur_node;
  1552. xmlBufferPtr buff, buff2;
  1553. xmlDocPtr doc;
  1554. if (xmlTextReaderExpand(reader) == NULL) {
  1555. return NULL;
  1556. }
  1557. doc = reader->doc;
  1558. buff = xmlBufferCreate();
  1559. for (cur_node = reader->node->children; cur_node != NULL;
  1560. cur_node = cur_node->next) {
  1561. node = xmlDocCopyNode(cur_node, doc, 1);
  1562. buff2 = xmlBufferCreate();
  1563. if (xmlNodeDump(buff2, doc, node, 0, 0) == -1) {
  1564. xmlFreeNode(node);
  1565. xmlBufferFree(buff2);
  1566. xmlBufferFree(buff);
  1567. return NULL;
  1568. }
  1569. xmlBufferCat(buff, buff2->content);
  1570. xmlFreeNode(node);
  1571. xmlBufferFree(buff2);
  1572. }
  1573. resbuf = buff->content;
  1574. buff->content = NULL;
  1575. xmlBufferFree(buff);
  1576. return resbuf;
  1577. }
  1578. #endif
  1579. #ifdef LIBXML_WRITER_ENABLED
  1580. /**
  1581. * xmlTextReaderReadOuterXml:
  1582. * @reader: the xmlTextReaderPtr used
  1583. *
  1584. * Reads the contents of the current node, including child nodes and markup.
  1585. *
  1586. * Returns a string containing the XML content, or NULL if the current node
  1587. * is neither an element nor attribute, or has no child nodes. The
  1588. * string must be deallocated by the caller.
  1589. */
  1590. xmlChar *
  1591. xmlTextReaderReadOuterXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED)
  1592. {
  1593. xmlChar *resbuf;
  1594. xmlNodePtr node;
  1595. xmlBufferPtr buff;
  1596. xmlDocPtr doc;
  1597. node = reader->node;
  1598. doc = reader->doc;
  1599. if (xmlTextReaderExpand(reader) == NULL) {
  1600. return NULL;
  1601. }
  1602. node = xmlDocCopyNode(node, doc, 1);
  1603. buff = xmlBufferCreate();
  1604. if (xmlNodeDump(buff, doc, node, 0, 0) == -1) {
  1605. xmlFreeNode(node);
  1606. xmlBufferFree(buff);
  1607. return NULL;
  1608. }
  1609. resbuf = buff->content;
  1610. buff->content = NULL;
  1611. xmlFreeNode(node);
  1612. xmlBufferFree(buff);
  1613. return resbuf;
  1614. }
  1615. #endif
  1616. /**
  1617. * xmlTextReaderReadString:
  1618. * @reader: the xmlTextReaderPtr used
  1619. *
  1620. * Reads the contents of an element or a text node as a string.
  1621. *
  1622. * Returns a string containing the contents of the Element or Text node,
  1623. * or NULL if the reader is positioned on any other type of node.
  1624. * The string must be deallocated by the caller.
  1625. */
  1626. xmlChar *
  1627. xmlTextReaderReadString(xmlTextReaderPtr reader)
  1628. {
  1629. xmlNodePtr node;
  1630. if ((reader == NULL) || (reader->node == NULL))
  1631. return(NULL);
  1632. node = (reader->curnode != NULL) ? reader->curnode : reader->node;
  1633. switch (node->type) {
  1634. case XML_TEXT_NODE:
  1635. if (node->content != NULL)
  1636. return(xmlStrdup(node->content));
  1637. break;
  1638. case XML_ELEMENT_NODE:
  1639. if (xmlTextReaderDoExpand(reader) != -1) {
  1640. return xmlTextReaderCollectSiblings(node->children);
  1641. }
  1642. case XML_ATTRIBUTE_NODE:
  1643. TODO
  1644. break;
  1645. default:
  1646. break;
  1647. }
  1648. return(NULL);
  1649. }
  1650. #if 0
  1651. /**
  1652. * xmlTextReaderReadBase64:
  1653. * @reader: the xmlTextReaderPtr used
  1654. * @array: a byte array to store the content.
  1655. * @offset: the zero-based index into array where the method should
  1656. * begin to write.
  1657. * @len: the number of bytes to write.
  1658. *
  1659. * Reads and decodes the Base64 encoded contents of an element and
  1660. * stores the result in a byte buffer.
  1661. *
  1662. * Returns the number of bytes written to array, or zero if the current
  1663. * instance is not positioned on an element or -1 in case of error.
  1664. */
  1665. int
  1666. xmlTextReaderReadBase64(xmlTextReaderPtr reader,
  1667. unsigned char *array ATTRIBUTE_UNUSED,
  1668. int offset ATTRIBUTE_UNUSED,
  1669. int len ATTRIBUTE_UNUSED) {
  1670. if ((reader == NULL) || (reader->ctxt == NULL))
  1671. return(-1);
  1672. if (reader->ctxt->wellFormed != 1)
  1673. return(-1);
  1674. if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
  1675. return(0);
  1676. TODO
  1677. return(0);
  1678. }
  1679. /**
  1680. * xmlTextReaderReadBinHex:
  1681. * @reader: the xmlTextReaderPtr used
  1682. * @array: a byte array to store the content.
  1683. * @offset: the zero-based index into array where the method should
  1684. * begin to write.
  1685. * @len: the number of bytes to write.
  1686. *
  1687. * Reads and decodes the BinHex encoded contents of an element and
  1688. * stores the result in a byte buffer.
  1689. *
  1690. * Returns the number of bytes written to array, or zero if the current
  1691. * instance is not positioned on an element or -1 in case of error.
  1692. */
  1693. int
  1694. xmlTextReaderReadBinHex(xmlTextReaderPtr reader,
  1695. unsigned char *array ATTRIBUTE_UNUSED,
  1696. int offset ATTRIBUTE_UNUSED,
  1697. int len ATTRIBUTE_UNUSED) {
  1698. if ((reader == NULL) || (reader->ctxt == NULL))
  1699. return(-1);
  1700. if (reader->ctxt->wellFormed != 1)
  1701. return(-1);
  1702. if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
  1703. return(0);
  1704. TODO
  1705. return(0);
  1706. }
  1707. #endif
  1708. /************************************************************************
  1709. * *
  1710. * Operating on a preparsed tree *
  1711. * *
  1712. ************************************************************************/
  1713. static int
  1714. xmlTextReaderNextTree(xmlTextReaderPtr reader)
  1715. {
  1716. if (reader == NULL)
  1717. return(-1);
  1718. if (reader->state == XML_TEXTREADER_END)
  1719. return(0);
  1720. if (reader->node == NULL) {
  1721. if (reader->doc->children == NULL) {
  1722. reader->state = XML_TEXTREADER_END;
  1723. return(0);
  1724. }
  1725. reader->node = reader->doc->children;
  1726. reader->state = XML_TEXTREADER_START;
  1727. return(1);
  1728. }
  1729. if (reader->state != XML_TEXTREADER_BACKTRACK) {
  1730. /* Here removed traversal to child, because we want to skip the subtree,
  1731. replace with traversal to sibling to skip subtree */
  1732. if (reader->node->next != 0) {
  1733. /* Move to sibling if present,skipping sub-tree */
  1734. reader->node = reader->node->next;
  1735. reader->state = XML_TEXTREADER_START;
  1736. return(1);
  1737. }
  1738. /* if reader->node->next is NULL mean no subtree for current node,
  1739. so need to move to sibling of parent node if present */
  1740. if ((reader->node->type == XML_ELEMENT_NODE) ||
  1741. (reader->node->type == XML_ATTRIBUTE_NODE)) {
  1742. reader->state = XML_TEXTREADER_BACKTRACK;
  1743. /* This will move to parent if present */
  1744. xmlTextReaderRead(reader);
  1745. }
  1746. }
  1747. if (reader->node->next != 0) {
  1748. reader->node = reader->node->next;
  1749. reader->state = XML_TEXTREADER_START;
  1750. return(1);
  1751. }
  1752. if (reader->node->parent != 0) {
  1753. if (reader->node->parent->type == XML_DOCUMENT_NODE) {
  1754. reader->state = XML_TEXTREADER_END;
  1755. return(0);
  1756. }
  1757. reader->node = reader->node->parent;
  1758. reader->depth--;
  1759. reader->state = XML_TEXTREADER_BACKTRACK;
  1760. /* Repeat process to move to sibling of parent node if present */
  1761. xmlTextReaderNextTree(reader);
  1762. }
  1763. reader->state = XML_TEXTREADER_END;
  1764. return(1);
  1765. }
  1766. /**
  1767. * xmlTextReaderReadTree:
  1768. * @reader: the xmlTextReaderPtr used
  1769. *
  1770. * Moves the position of the current instance to the next node in
  1771. * the stream, exposing its properties.
  1772. *
  1773. * Returns 1 if the node was read successfully, 0 if there is no more
  1774. * nodes to read, or -1 in case of error
  1775. */
  1776. static int
  1777. xmlTextReaderReadTree(xmlTextReaderPtr reader) {
  1778. if (reader->state == XML_TEXTREADER_END)
  1779. return(0);
  1780. next_node:
  1781. if (reader->node == NULL) {
  1782. if (reader->doc->children == NULL) {
  1783. reader->state = XML_TEXTREADER_END;
  1784. return(0);
  1785. }
  1786. reader->node = reader->doc->children;
  1787. reader->state = XML_TEXTREADER_START;
  1788. goto found_node;
  1789. }
  1790. if ((reader->state != XML_TEXTREADER_BACKTRACK) &&
  1791. (reader->node->type != XML_DTD_NODE) &&
  1792. (reader->node->type != XML_XINCLUDE_START) &&
  1793. (reader->node->type != XML_ENTITY_REF_NODE)) {
  1794. if (reader->node->children != NULL) {
  1795. reader->node = reader->node->children;
  1796. reader->depth++;
  1797. reader->state = XML_TEXTREADER_START;
  1798. goto found_node;
  1799. }
  1800. if (reader->node->type == XML_ATTRIBUTE_NODE) {
  1801. reader->state = XML_TEXTREADER_BACKTRACK;
  1802. goto found_node;
  1803. }
  1804. }
  1805. if (reader->node->next != NULL) {
  1806. reader->node = reader->node->next;
  1807. reader->state = XML_TEXTREADER_START;
  1808. goto found_node;
  1809. }
  1810. if (reader->node->parent != NULL) {
  1811. if ((reader->node->parent->type == XML_DOCUMENT_NODE) ||
  1812. (reader->node->parent->type == XML_HTML_DOCUMENT_NODE)) {
  1813. reader->state = XML_TEXTREADER_END;
  1814. return(0);
  1815. }
  1816. reader->node = reader->node->parent;
  1817. reader->depth--;
  1818. reader->state = XML_TEXTREADER_BACKTRACK;
  1819. goto found_node;
  1820. }
  1821. reader->state = XML_TEXTREADER_END;
  1822. found_node:
  1823. if ((reader->node->type == XML_XINCLUDE_START) ||
  1824. (reader->node->type == XML_XINCLUDE_END))
  1825. goto next_node;
  1826. return(1);
  1827. }
  1828. /**
  1829. * xmlTextReaderNextSibling:
  1830. * @reader: the xmlTextReaderPtr used
  1831. *
  1832. * Skip to the node following the current one in document order while
  1833. * avoiding the subtree if any.
  1834. * Currently implemented only for Readers built on a document
  1835. *
  1836. * Returns 1 if the node was read successfully, 0 if there is no more
  1837. * nodes to read, or -1 in case of error
  1838. */
  1839. int
  1840. xmlTextReaderNextSibling(xmlTextReaderPtr reader) {
  1841. if (reader == NULL)
  1842. return(-1);
  1843. if (reader->doc == NULL) {
  1844. /* TODO */
  1845. return(-1);
  1846. }
  1847. if (reader->state == XML_TEXTREADER_END)
  1848. return(0);
  1849. if (reader->node == NULL)
  1850. return(xmlTextReaderNextTree(reader));
  1851. if (reader->node->next != NULL) {
  1852. reader->node = reader->node->next;
  1853. reader->state = XML_TEXTREADER_START;
  1854. return(1);
  1855. }
  1856. return(0);
  1857. }
  1858. /************************************************************************
  1859. * *
  1860. * Constructor and destructors *
  1861. * *
  1862. ************************************************************************/
  1863. /**
  1864. * xmlNewTextReader:
  1865. * @input: the xmlParserInputBufferPtr used to read data
  1866. * @URI: the URI information for the source if available
  1867. *
  1868. * Create an xmlTextReader structure fed with @input
  1869. *
  1870. * Returns the new xmlTextReaderPtr or NULL in case of error
  1871. */
  1872. xmlTextReaderPtr
  1873. xmlNewTextReader(xmlParserInputBufferPtr input, const char *URI) {
  1874. xmlTextReaderPtr ret;
  1875. if (input == NULL)
  1876. return(NULL);
  1877. ret = xmlMalloc(sizeof(xmlTextReader));
  1878. if (ret == NULL) {
  1879. xmlGenericError(xmlGenericErrorContext,
  1880. "xmlNewTextReader : malloc failed\n");
  1881. return(NULL);
  1882. }
  1883. memset(ret, 0, sizeof(xmlTextReader));
  1884. ret->doc = NULL;
  1885. ret->entTab = NULL;
  1886. ret->entMax = 0;
  1887. ret->entNr = 0;
  1888. ret->input = input;
  1889. ret->buffer = xmlBufferCreateSize(100);
  1890. if (ret->buffer == NULL) {
  1891. xmlFree(ret);
  1892. xmlGenericError(xmlGenericErrorContext,
  1893. "xmlNewTextReader : malloc failed\n");
  1894. return(NULL);
  1895. }
  1896. ret->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
  1897. if (ret->sax == NULL) {
  1898. xmlBufferFree(ret->buffer);
  1899. xmlFree(ret);
  1900. xmlGenericError(xmlGenericErrorContext,
  1901. "xmlNewTextReader : malloc failed\n");
  1902. return(NULL);
  1903. }
  1904. xmlSAXVersion(ret->sax, 2);
  1905. ret->startElement = ret->sax->startElement;
  1906. ret->sax->startElement = xmlTextReaderStartElement;
  1907. ret->endElement = ret->sax->endElement;
  1908. ret->sax->endElement = xmlTextReaderEndElement;
  1909. #ifdef LIBXML_SAX1_ENABLED
  1910. if (ret->sax->initialized == XML_SAX2_MAGIC) {
  1911. #endif /* LIBXML_SAX1_ENABLED */
  1912. ret->startElementNs = ret->sax->startElementNs;
  1913. ret->sax->startElementNs = xmlTextReaderStartElementNs;
  1914. ret->endElementNs = ret->sax->endElementNs;
  1915. ret->sax->endElementNs = xmlTextReaderEndElementNs;
  1916. #ifdef LIBXML_SAX1_ENABLED
  1917. } else {
  1918. ret->startElementNs = NULL;
  1919. ret->endElementNs = NULL;
  1920. }
  1921. #endif /* LIBXML_SAX1_ENABLED */
  1922. ret->characters = ret->sax->characters;
  1923. ret->sax->characters = xmlTextReaderCharacters;
  1924. ret->sax->ignorableWhitespace = xmlTextReaderCharacters;
  1925. ret->cdataBlock = ret->sax->cdataBlock;
  1926. ret->sax->cdataBlock = xmlTextReaderCDataBlock;
  1927. ret->mode = XML_TEXTREADER_MODE_INITIAL;
  1928. ret->node = NULL;
  1929. ret->curnode = NULL;
  1930. if (ret->input->buffer->use < 4) {
  1931. xmlParserInputBufferRead(input, 4);
  1932. }
  1933. if (ret->input->buffer->use >= 4) {
  1934. ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL,
  1935. (const char *) ret->input->buffer->content, 4, URI);
  1936. ret->base = 0;
  1937. ret->cur = 4;
  1938. } else {
  1939. ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, NULL, 0, URI);
  1940. ret->base = 0;
  1941. ret->cur = 0;
  1942. }
  1943. if (ret->ctxt == NULL) {
  1944. xmlGenericError(xmlGenericErrorContext,
  1945. "xmlNewTextReader : malloc failed\n");
  1946. xmlBufferFree(ret->buffer);
  1947. xmlFree(ret->sax);
  1948. xmlFree(ret);
  1949. return(NULL);
  1950. }
  1951. ret->ctxt->parseMode = XML_PARSE_READER;
  1952. ret->ctxt->_private = ret;
  1953. ret->ctxt->linenumbers = 1;
  1954. ret->ctxt->dictNames = 1;
  1955. ret->allocs = XML_TEXTREADER_CTXT;
  1956. /*
  1957. * use the parser dictionnary to allocate all elements and attributes names
  1958. */
  1959. ret->ctxt->docdict = 1;
  1960. ret->dict = ret->ctxt->dict;
  1961. #ifdef LIBXML_XINCLUDE_ENABLED
  1962. ret->xinclude = 0;
  1963. #endif
  1964. #ifdef LIBXML_PATTERN_ENABLED
  1965. ret->patternMax = 0;
  1966. ret->patternTab = NULL;
  1967. #endif
  1968. return(ret);
  1969. }
  1970. /**
  1971. * xmlNewTextReaderFilename:
  1972. * @URI: the URI of the resource to process
  1973. *
  1974. * Create an xmlTextReader structure fed with the resource at @URI
  1975. *
  1976. * Returns the new xmlTextReaderPtr or NULL in case of error
  1977. */
  1978. xmlTextReaderPtr
  1979. xmlNewTextReaderFilename(const char *URI) {
  1980. xmlParserInputBufferPtr input;
  1981. xmlTextReaderPtr ret;
  1982. char *directory = NULL;
  1983. input = xmlParserInputBufferCreateFilename(URI, XML_CHAR_ENCODING_NONE);
  1984. if (input == NULL)
  1985. return(NULL);
  1986. ret = xmlNewTextReader(input, URI);
  1987. if (ret == NULL) {
  1988. xmlFreeParserInputBuffer(input);
  1989. return(NULL);
  1990. }
  1991. ret->allocs |= XML_TEXTREADER_INPUT;
  1992. if (ret->ctxt->directory == NULL)
  1993. directory = xmlParserGetDirectory(URI);
  1994. if ((ret->ctxt->directory == NULL) && (directory != NULL))
  1995. ret->ctxt->directory = (char *) xmlStrdup((xmlChar *) directory);
  1996. if (directory != NULL)
  1997. xmlFree(directory);
  1998. return(ret);
  1999. }
  2000. /**
  2001. * xmlFreeTextReader:
  2002. * @reader: the xmlTextReaderPtr
  2003. *
  2004. * Deallocate all the resources associated to the reader
  2005. */
  2006. void
  2007. xmlFreeTextReader(xmlTextReaderPtr reader) {
  2008. if (reader == NULL)
  2009. return;
  2010. #ifdef LIBXML_SCHEMAS_ENABLED
  2011. if (reader->rngSchemas != NULL) {
  2012. xmlRelaxNGFree(reader->rngSchemas);
  2013. reader->rngSchemas = NULL;
  2014. }
  2015. if (reader->rngValidCtxt != NULL) {
  2016. xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
  2017. reader->rngValidCtxt = NULL;
  2018. }
  2019. if (reader->xsdPlug != NULL) {
  2020. xmlSchemaSAXUnplug(reader->xsdPlug);
  2021. reader->xsdPlug = NULL;
  2022. }
  2023. if (reader->xsdValidCtxt != NULL) {
  2024. if (! reader->xsdPreserveCtxt)
  2025. xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
  2026. reader->xsdValidCtxt = NULL;
  2027. }
  2028. if (reader->xsdSchemas != NULL) {
  2029. xmlSchemaFree(reader->xsdSchemas);
  2030. reader->xsdSchemas = NULL;
  2031. }
  2032. #endif
  2033. #ifdef LIBXML_XINCLUDE_ENABLED
  2034. if (reader->xincctxt != NULL)
  2035. xmlXIncludeFreeContext(reader->xincctxt);
  2036. #endif
  2037. #ifdef LIBXML_PATTERN_ENABLED
  2038. if (reader->patternTab != NULL) {
  2039. int i;
  2040. for (i = 0;i < reader->patternNr;i++) {
  2041. if (reader->patternTab[i] != NULL)
  2042. xmlFreePattern(reader->patternTab[i]);
  2043. }
  2044. xmlFree(reader->patternTab);
  2045. }
  2046. #endif
  2047. if (reader->faketext != NULL) {
  2048. xmlFreeNode(reader->faketext);
  2049. }
  2050. if (reader->ctxt != NULL) {
  2051. if (reader->dict == reader->ctxt->dict)
  2052. reader->dict = NULL;
  2053. if (reader->ctxt->myDoc != NULL) {
  2054. if (reader->preserve == 0)
  2055. xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
  2056. reader->ctxt->myDoc = NULL;
  2057. }
  2058. if ((reader->ctxt->vctxt.vstateTab != NULL) &&
  2059. (reader->ctxt->vctxt.vstateMax > 0)){
  2060. xmlFree(reader->ctxt->vctxt.vstateTab);
  2061. reader->ctxt->vctxt.vstateTab = NULL;
  2062. reader->ctxt->vctxt.vstateMax = 0;
  2063. }
  2064. if (reader->allocs & XML_TEXTREADER_CTXT)
  2065. xmlFreeParserCtxt(reader->ctxt);
  2066. }
  2067. if (reader->sax != NULL)
  2068. xmlFree(reader->sax);
  2069. if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT))
  2070. xmlFreeParserInputBuffer(reader->input);
  2071. if (reader->buffer != NULL)
  2072. xmlBufferFree(reader->buffer);
  2073. if (reader->entTab != NULL)
  2074. xmlFree(reader->entTab);
  2075. if (reader->dict != NULL)
  2076. xmlDictFree(reader->dict);
  2077. xmlFree(reader);
  2078. }
  2079. /************************************************************************
  2080. * *
  2081. * Methods for XmlTextReader *
  2082. * *
  2083. ************************************************************************/
  2084. /**
  2085. * xmlTextReaderClose:
  2086. * @reader: the xmlTextReaderPtr used
  2087. *
  2088. * This method releases any resources allocated by the current instance
  2089. * changes the state to Closed and close any underlying input.
  2090. *
  2091. * Returns 0 or -1 in case of error
  2092. */
  2093. int
  2094. xmlTextReaderClose(xmlTextReaderPtr reader) {
  2095. if (reader == NULL)
  2096. return(-1);
  2097. reader->node = NULL;
  2098. reader->curnode = NULL;
  2099. reader->mode = XML_TEXTREADER_MODE_CLOSED;
  2100. if (reader->ctxt != NULL) {
  2101. xmlStopParser(reader->ctxt);
  2102. if (reader->ctxt->myDoc != NULL) {
  2103. if (reader->preserve == 0)
  2104. xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
  2105. reader->ctxt->myDoc = NULL;
  2106. }
  2107. }
  2108. if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT)) {
  2109. xmlFreeParserInputBuffer(reader->input);
  2110. reader->allocs -= XML_TEXTREADER_INPUT;
  2111. }
  2112. return(0);
  2113. }
  2114. /**
  2115. * xmlTextReaderGetAttributeNo:
  2116. * @reader: the xmlTextReaderPtr used
  2117. * @no: the zero-based index of the attribute relative to the containing element
  2118. *
  2119. * Provides the value of the attribute with the specified index relative
  2120. * to the containing element.
  2121. *
  2122. * Returns a string containing the value of the specified attribute, or NULL
  2123. * in case of error. The string must be deallocated by the caller.
  2124. */
  2125. xmlChar *
  2126. xmlTextReaderGetAttributeNo(xmlTextReaderPtr reader, int no) {
  2127. xmlChar *ret;
  2128. int i;
  2129. xmlAttrPtr cur;
  2130. xmlNsPtr ns;
  2131. if (reader == NULL)
  2132. return(NULL);
  2133. if (reader->node == NULL)
  2134. return(NULL);
  2135. if (reader->curnode != NULL)
  2136. return(NULL);
  2137. /* TODO: handle the xmlDecl */
  2138. if (reader->node->type != XML_ELEMENT_NODE)
  2139. return(NULL);
  2140. ns = reader->node->nsDef;
  2141. for (i = 0;(i < no) && (ns != NULL);i++) {
  2142. ns = ns->next;
  2143. }
  2144. if (ns != NULL)
  2145. return(xmlStrdup(ns->href));
  2146. cur = reader->node->properties;
  2147. if (cur == NULL)
  2148. return(NULL);
  2149. for (;i < no;i++) {
  2150. cur = cur->next;
  2151. if (cur == NULL)
  2152. return(NULL);
  2153. }
  2154. /* TODO walk the DTD if present */
  2155. ret = xmlNodeListGetString(reader->node->doc, cur->children, 1);
  2156. if (ret == NULL) return(xmlStrdup((xmlChar *)""));
  2157. return(ret);
  2158. }
  2159. /**
  2160. * xmlTextReaderGetAttribute:
  2161. * @reader: the xmlTextReaderPtr used
  2162. * @name: the qualified name of the attribute.
  2163. *
  2164. * Provides the value of the attribute with the specified qualified name.
  2165. *
  2166. * Returns a string containing the value of the specified attribute, or NULL
  2167. * in case of error. The string must be deallocated by the caller.
  2168. */
  2169. xmlChar *
  2170. xmlTextReaderGetAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
  2171. xmlChar *prefix = NULL;
  2172. xmlChar *localname;
  2173. xmlNsPtr ns;
  2174. xmlChar *ret = NULL;
  2175. if ((reader == NULL) || (name == NULL))
  2176. return(NULL);
  2177. if (reader->node == NULL)
  2178. return(NULL);
  2179. if (reader->curnode != NULL)
  2180. return(NULL);
  2181. /* TODO: handle the xmlDecl */
  2182. if (reader->node->type != XML_ELEMENT_NODE)
  2183. return(NULL);
  2184. localname = xmlSplitQName2(name, &prefix);
  2185. if (localname == NULL) {
  2186. /*
  2187. * Namespace default decl
  2188. */
  2189. if (xmlStrEqual(name, BAD_CAST "xmlns")) {
  2190. ns = reader->node->nsDef;
  2191. while (ns != NULL) {
  2192. if (ns->prefix == NULL) {
  2193. return(xmlStrdup(ns->href));
  2194. }
  2195. ns = ns->next;
  2196. }
  2197. return NULL;
  2198. }
  2199. return(xmlGetNoNsProp(reader->node, name));
  2200. }
  2201. /*
  2202. * Namespace default decl
  2203. */
  2204. if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
  2205. ns = reader->node->nsDef;
  2206. while (ns != NULL) {
  2207. if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) {
  2208. ret = xmlStrdup(ns->href);
  2209. break;
  2210. }
  2211. ns = ns->next;
  2212. }
  2213. } else {
  2214. ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
  2215. if (ns != NULL)
  2216. ret = xmlGetNsProp(reader->node, localname, ns->href);
  2217. }
  2218. xmlFree(localname);
  2219. if (prefix != NULL)
  2220. xmlFree(prefix);
  2221. return(ret);
  2222. }
  2223. /**
  2224. * xmlTextReaderGetAttributeNs:
  2225. * @reader: the xmlTextReaderPtr used
  2226. * @localName: the local name of the attribute.
  2227. * @namespaceURI: the namespace URI of the attribute.
  2228. *
  2229. * Provides the value of the specified attribute
  2230. *
  2231. * Returns a string containing the value of the specified attribute, or NULL
  2232. * in case of error. The string must be deallocated by the caller.
  2233. */
  2234. xmlChar *
  2235. xmlTextReaderGetAttributeNs(xmlTextReaderPtr reader, const xmlChar *localName,
  2236. const xmlChar *namespaceURI) {
  2237. xmlChar *prefix = NULL;
  2238. xmlNsPtr ns;
  2239. if ((reader == NULL) || (localName == NULL))
  2240. return(NULL);
  2241. if (reader->node == NULL)
  2242. return(NULL);
  2243. if (reader->curnode != NULL)
  2244. return(NULL);
  2245. /* TODO: handle the xmlDecl */
  2246. if (reader->node->type != XML_ELEMENT_NODE)
  2247. return(NULL);
  2248. if (xmlStrEqual(namespaceURI, BAD_CAST "http://www.w3.org/2000/xmlns/")) {
  2249. if (! xmlStrEqual(localName, BAD_CAST "xmlns")) {
  2250. prefix = BAD_CAST localName;
  2251. }
  2252. ns = reader->node->nsDef;
  2253. while (ns != NULL) {
  2254. if ((prefix == NULL && ns->prefix == NULL) ||
  2255. ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localName)))) {
  2256. return xmlStrdup(ns->href);
  2257. }
  2258. ns = ns->next;
  2259. }
  2260. return NULL;
  2261. }
  2262. return(xmlGetNsProp(reader->node, localName, namespaceURI));
  2263. }
  2264. /**
  2265. * xmlTextReaderGetRemainder:
  2266. * @reader: the xmlTextReaderPtr used
  2267. *
  2268. * Method to get the remainder of the buffered XML. this method stops the
  2269. * parser, set its state to End Of File and return the input stream with
  2270. * what is left that the parser did not use.
  2271. *
  2272. * The implementation is not good, the parser certainly procgressed past
  2273. * what's left in reader->input, and there is an allocation problem. Best
  2274. * would be to rewrite it differently.
  2275. *
  2276. * Returns the xmlParserInputBufferPtr attached to the XML or NULL
  2277. * in case of error.
  2278. */
  2279. xmlParserInputBufferPtr
  2280. xmlTextReaderGetRemainder(xmlTextReaderPtr reader) {
  2281. xmlParserInputBufferPtr ret = NULL;
  2282. if (reader == NULL)
  2283. return(NULL);
  2284. if (reader->node == NULL)
  2285. return(NULL);
  2286. reader->node = NULL;
  2287. reader->curnode = NULL;
  2288. reader->mode = XML_TEXTREADER_MODE_EOF;
  2289. if (reader->ctxt != NULL) {
  2290. xmlStopParser(reader->ctxt);
  2291. if (reader->ctxt->myDoc != NULL) {
  2292. if (reader->preserve == 0)
  2293. xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
  2294. reader->ctxt->myDoc = NULL;
  2295. }
  2296. }
  2297. if (reader->allocs & XML_TEXTREADER_INPUT) {
  2298. ret = reader->input;
  2299. reader->input = NULL;
  2300. reader->allocs -= XML_TEXTREADER_INPUT;
  2301. } else {
  2302. /*
  2303. * Hum, one may need to duplicate the data structure because
  2304. * without reference counting the input may be freed twice:
  2305. * - by the layer which allocated it.
  2306. * - by the layer to which would have been returned to.
  2307. */
  2308. TODO
  2309. return(NULL);
  2310. }
  2311. return(ret);
  2312. }
  2313. /**
  2314. * xmlTextReaderLookupNamespace:
  2315. * @reader: the xmlTextReaderPtr used
  2316. * @prefix: the prefix whose namespace URI is to be resolved. To return
  2317. * the default namespace, specify NULL
  2318. *
  2319. * Resolves a namespace prefix in the scope of the current element.
  2320. *
  2321. * Returns a string containing the namespace URI to which the prefix maps
  2322. * or NULL in case of error. The string must be deallocated by the caller.
  2323. */
  2324. xmlChar *
  2325. xmlTextReaderLookupNamespace(xmlTextReaderPtr reader, const xmlChar *prefix) {
  2326. xmlNsPtr ns;
  2327. if (reader == NULL)
  2328. return(NULL);
  2329. if (reader->node == NULL)
  2330. return(NULL);
  2331. ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
  2332. if (ns == NULL)
  2333. return(NULL);
  2334. return(xmlStrdup(ns->href));
  2335. }
  2336. /**
  2337. * xmlTextReaderMoveToAttributeNo:
  2338. * @reader: the xmlTextReaderPtr used
  2339. * @no: the zero-based index of the attribute relative to the containing
  2340. * element.
  2341. *
  2342. * Moves the position of the current instance to the attribute with
  2343. * the specified index relative to the containing element.
  2344. *
  2345. * Returns 1 in case of success, -1 in case of error, 0 if not found
  2346. */
  2347. int
  2348. xmlTextReaderMoveToAttributeNo(xmlTextReaderPtr reader, int no) {
  2349. int i;
  2350. xmlAttrPtr cur;
  2351. xmlNsPtr ns;
  2352. if (reader == NULL)
  2353. return(-1);
  2354. if (reader->node == NULL)
  2355. return(-1);
  2356. /* TODO: handle the xmlDecl */
  2357. if (reader->node->type != XML_ELEMENT_NODE)
  2358. return(-1);
  2359. reader->curnode = NULL;
  2360. ns = reader->node->nsDef;
  2361. for (i = 0;(i < no) && (ns != NULL);i++) {
  2362. ns = ns->next;
  2363. }
  2364. if (ns != NULL) {
  2365. reader->curnode = (xmlNodePtr) ns;
  2366. return(1);
  2367. }
  2368. cur = reader->node->properties;
  2369. if (cur == NULL)
  2370. return(0);
  2371. for (;i < no;i++) {
  2372. cur = cur->next;
  2373. if (cur == NULL)
  2374. return(0);
  2375. }
  2376. /* TODO walk the DTD if present */
  2377. reader->curnode = (xmlNodePtr) cur;
  2378. return(1);
  2379. }
  2380. /**
  2381. * xmlTextReaderMoveToAttribute:
  2382. * @reader: the xmlTextReaderPtr used
  2383. * @name: the qualified name of the attribute.
  2384. *
  2385. * Moves the position of the current instance to the attribute with
  2386. * the specified qualified name.
  2387. *
  2388. * Returns 1 in case of success, -1 in case of error, 0 if not found
  2389. */
  2390. int
  2391. xmlTextReaderMoveToAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
  2392. xmlChar *prefix = NULL;
  2393. xmlChar *localname;
  2394. xmlNsPtr ns;
  2395. xmlAttrPtr prop;
  2396. if ((reader == NULL) || (name == NULL))
  2397. return(-1);
  2398. if (reader->node == NULL)
  2399. return(-1);
  2400. /* TODO: handle the xmlDecl */
  2401. if (reader->node->type != XML_ELEMENT_NODE)
  2402. return(0);
  2403. localname = xmlSplitQName2(name, &prefix);
  2404. if (localname == NULL) {
  2405. /*
  2406. * Namespace default decl
  2407. */
  2408. if (xmlStrEqual(name, BAD_CAST "xmlns")) {
  2409. ns = reader->node->nsDef;
  2410. while (ns != NULL) {
  2411. if (ns->prefix == NULL) {
  2412. reader->curnode = (xmlNodePtr) ns;
  2413. return(1);
  2414. }
  2415. ns = ns->next;
  2416. }
  2417. return(0);
  2418. }
  2419. prop = reader->node->properties;
  2420. while (prop != NULL) {
  2421. /*
  2422. * One need to have
  2423. * - same attribute names
  2424. * - and the attribute carrying that namespace
  2425. */
  2426. if ((xmlStrEqual(prop->name, name)) &&
  2427. ((prop->ns == NULL) || (prop->ns->prefix == NULL))) {
  2428. reader->curnode = (xmlNodePtr) prop;
  2429. return(1);
  2430. }
  2431. prop = prop->next;
  2432. }
  2433. return(0);
  2434. }
  2435. /*
  2436. * Namespace default decl
  2437. */
  2438. if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
  2439. ns = reader->node->nsDef;
  2440. while (ns != NULL) {
  2441. if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) {
  2442. reader->curnode = (xmlNodePtr) ns;
  2443. goto found;
  2444. }
  2445. ns = ns->next;
  2446. }
  2447. goto not_found;
  2448. }
  2449. prop = reader->node->properties;
  2450. while (prop != NULL) {
  2451. /*
  2452. * One need to have
  2453. * - same attribute names
  2454. * - and the attribute carrying that namespace
  2455. */
  2456. if ((xmlStrEqual(prop->name, localname)) &&
  2457. (prop->ns != NULL) && (xmlStrEqual(prop->ns->prefix, prefix))) {
  2458. reader->curnode = (xmlNodePtr) prop;
  2459. goto found;
  2460. }
  2461. prop = prop->next;
  2462. }
  2463. not_found:
  2464. if (localname != NULL)
  2465. xmlFree(localname);
  2466. if (prefix != NULL)
  2467. xmlFree(prefix);
  2468. return(0);
  2469. found:
  2470. if (localname != NULL)
  2471. xmlFree(localname);
  2472. if (prefix != NULL)
  2473. xmlFree(prefix);
  2474. return(1);
  2475. }
  2476. /**
  2477. * xmlTextReaderMoveToAttributeNs:
  2478. * @reader: the xmlTextReaderPtr used
  2479. * @localName: the local name of the attribute.
  2480. * @namespaceURI: the namespace URI of the attribute.
  2481. *
  2482. * Moves the position of the current instance to the attribute with the
  2483. * specified local name and namespace URI.
  2484. *
  2485. * Returns 1 in case of success, -1 in case of error, 0 if not found
  2486. */
  2487. int
  2488. xmlTextReaderMoveToAttributeNs(xmlTextReaderPtr reader,
  2489. const xmlChar *localName, const xmlChar *namespaceURI) {
  2490. xmlAttrPtr prop;
  2491. xmlNodePtr node;
  2492. xmlNsPtr ns;
  2493. xmlChar *prefix = NULL;
  2494. if ((reader == NULL) || (localName == NULL) || (namespaceURI == NULL))
  2495. return(-1);
  2496. if (reader->node == NULL)
  2497. return(-1);
  2498. if (reader->node->type != XML_ELEMENT_NODE)
  2499. return(0);
  2500. node = reader->node;
  2501. if (xmlStrEqual(namespaceURI, BAD_CAST "http://www.w3.org/2000/xmlns/")) {
  2502. if (! xmlStrEqual(localName, BAD_CAST "xmlns")) {
  2503. prefix = BAD_CAST localName;
  2504. }
  2505. ns = reader->node->nsDef;
  2506. while (ns != NULL) {
  2507. if ((prefix == NULL && ns->prefix == NULL) ||
  2508. ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localName)))) {
  2509. reader->curnode = (xmlNodePtr) ns;
  2510. return(1);
  2511. }
  2512. ns = ns->next;
  2513. }
  2514. return(0);
  2515. }
  2516. prop = node->properties;
  2517. while (prop != NULL) {
  2518. /*
  2519. * One need to have
  2520. * - same attribute names
  2521. * - and the attribute carrying that namespace
  2522. */
  2523. if (xmlStrEqual(prop->name, localName) &&
  2524. ((prop->ns != NULL) &&
  2525. (xmlStrEqual(prop->ns->href, namespaceURI)))) {
  2526. reader->curnode = (xmlNodePtr) prop;
  2527. return(1);
  2528. }
  2529. prop = prop->next;
  2530. }
  2531. return(0);
  2532. }
  2533. /**
  2534. * xmlTextReaderMoveToFirstAttribute:
  2535. * @reader: the xmlTextReaderPtr used
  2536. *
  2537. * Moves the position of the current instance to the first attribute
  2538. * associated with the current node.
  2539. *
  2540. * Returns 1 in case of success, -1 in case of error, 0 if not found
  2541. */
  2542. int
  2543. xmlTextReaderMoveToFirstAttribute(xmlTextReaderPtr reader) {
  2544. if (reader == NULL)
  2545. return(-1);
  2546. if (reader->node == NULL)
  2547. return(-1);
  2548. if (reader->node->type != XML_ELEMENT_NODE)
  2549. return(0);
  2550. if (reader->node->nsDef != NULL) {
  2551. reader->curnode = (xmlNodePtr) reader->node->nsDef;
  2552. return(1);
  2553. }
  2554. if (reader->node->properties != NULL) {
  2555. reader->curnode = (xmlNodePtr) reader->node->properties;
  2556. return(1);
  2557. }
  2558. return(0);
  2559. }
  2560. /**
  2561. * xmlTextReaderMoveToNextAttribute:
  2562. * @reader: the xmlTextReaderPtr used
  2563. *
  2564. * Moves the position of the current instance to the next attribute
  2565. * associated with the current node.
  2566. *
  2567. * Returns 1 in case of success, -1 in case of error, 0 if not found
  2568. */
  2569. int
  2570. xmlTextReaderMoveToNextAttribute(xmlTextReaderPtr reader) {
  2571. if (reader == NULL)
  2572. return(-1);
  2573. if (reader->node == NULL)
  2574. return(-1);
  2575. if (reader->node->type != XML_ELEMENT_NODE)
  2576. return(0);
  2577. if (reader->curnode == NULL)
  2578. return(xmlTextReaderMoveToFirstAttribute(reader));
  2579. if (reader->curnode->type == XML_NAMESPACE_DECL) {
  2580. xmlNsPtr ns = (xmlNsPtr) reader->curnode;
  2581. if (ns->next != NULL) {
  2582. reader->curnode = (xmlNodePtr) ns->next;
  2583. return(1);
  2584. }
  2585. if (reader->node->properties != NULL) {
  2586. reader->curnode = (xmlNodePtr) reader->node->properties;
  2587. return(1);
  2588. }
  2589. return(0);
  2590. } else if ((reader->curnode->type == XML_ATTRIBUTE_NODE) &&
  2591. (reader->curnode->next != NULL)) {
  2592. reader->curnode = reader->curnode->next;
  2593. return(1);
  2594. }
  2595. return(0);
  2596. }
  2597. /**
  2598. * xmlTextReaderMoveToElement:
  2599. * @reader: the xmlTextReaderPtr used
  2600. *
  2601. * Moves the position of the current instance to the node that
  2602. * contains the current Attribute node.
  2603. *
  2604. * Returns 1 in case of success, -1 in case of error, 0 if not moved
  2605. */
  2606. int
  2607. xmlTextReaderMoveToElement(xmlTextReaderPtr reader) {
  2608. if (reader == NULL)
  2609. return(-1);
  2610. if (reader->node == NULL)
  2611. return(-1);
  2612. if (reader->node->type != XML_ELEMENT_NODE)
  2613. return(0);
  2614. if (reader->curnode != NULL) {
  2615. reader->curnode = NULL;
  2616. return(1);
  2617. }
  2618. return(0);
  2619. }
  2620. /**
  2621. * xmlTextReaderReadAttributeValue:
  2622. * @reader: the xmlTextReaderPtr used
  2623. *
  2624. * Parses an attribute value into one or more Text and EntityReference nodes.
  2625. *
  2626. * Returns 1 in case of success, 0 if the reader was not positionned on an
  2627. * ttribute node or all the attribute values have been read, or -1
  2628. * in case of error.
  2629. */
  2630. int
  2631. xmlTextReaderReadAttributeValue(xmlTextReaderPtr reader) {
  2632. if (reader == NULL)
  2633. return(-1);
  2634. if (reader->node == NULL)
  2635. return(-1);
  2636. if (reader->curnode == NULL)
  2637. return(0);
  2638. if (reader->curnode->type == XML_ATTRIBUTE_NODE) {
  2639. if (reader->curnode->children == NULL)
  2640. return(0);
  2641. reader->curnode = reader->curnode->children;
  2642. } else if (reader->curnode->type == XML_NAMESPACE_DECL) {
  2643. xmlNsPtr ns = (xmlNsPtr) reader->curnode;
  2644. if (reader->faketext == NULL) {
  2645. reader->faketext = xmlNewDocText(reader->node->doc,
  2646. ns->href);
  2647. } else {
  2648. if ((reader->faketext->content != NULL) &&
  2649. (reader->faketext->content !=
  2650. (xmlChar *) &(reader->faketext->properties)))
  2651. xmlFree(reader->faketext->content);
  2652. reader->faketext->content = xmlStrdup(ns->href);
  2653. }
  2654. reader->curnode = reader->faketext;
  2655. } else {
  2656. if (reader->curnode->next == NULL)
  2657. return(0);
  2658. reader->curnode = reader->curnode->next;
  2659. }
  2660. return(1);
  2661. }
  2662. /**
  2663. * xmlTextReaderConstEncoding:
  2664. * @reader: the xmlTextReaderPtr used
  2665. *
  2666. * Determine the encoding of the document being read.
  2667. *
  2668. * Returns a string containing the encoding of the document or NULL in
  2669. * case of error. The string is deallocated with the reader.
  2670. */
  2671. const xmlChar *
  2672. xmlTextReaderConstEncoding(xmlTextReaderPtr reader) {
  2673. xmlDocPtr doc = NULL;
  2674. if (reader == NULL)
  2675. return(NULL);
  2676. if (reader->doc != NULL)
  2677. doc = reader->doc;
  2678. else if (reader->ctxt != NULL)
  2679. doc = reader->ctxt->myDoc;
  2680. if (doc == NULL)
  2681. return(NULL);
  2682. if (doc->encoding == NULL)
  2683. return(NULL);
  2684. else
  2685. return(CONSTSTR(doc->encoding));
  2686. }
  2687. /************************************************************************
  2688. * *
  2689. * Acces API to the current node *
  2690. * *
  2691. ************************************************************************/
  2692. /**
  2693. * xmlTextReaderAttributeCount:
  2694. * @reader: the xmlTextReaderPtr used
  2695. *
  2696. * Provides the number of attributes of the current node
  2697. *
  2698. * Returns 0 i no attributes, -1 in case of error or the attribute count
  2699. */
  2700. int
  2701. xmlTextReaderAttributeCount(xmlTextReaderPtr reader) {
  2702. int ret;
  2703. xmlAttrPtr attr;
  2704. xmlNsPtr ns;
  2705. xmlNodePtr node;
  2706. if (reader == NULL)
  2707. return(-1);
  2708. if (reader->node == NULL)
  2709. return(0);
  2710. if (reader->curnode != NULL)
  2711. node = reader->curnode;
  2712. else
  2713. node = reader->node;
  2714. if (node->type != XML_ELEMENT_NODE)
  2715. return(0);
  2716. if ((reader->state == XML_TEXTREADER_END) ||
  2717. (reader->state == XML_TEXTREADER_BACKTRACK))
  2718. return(0);
  2719. ret = 0;
  2720. attr = node->properties;
  2721. while (attr != NULL) {
  2722. ret++;
  2723. attr = attr->next;
  2724. }
  2725. ns = node->nsDef;
  2726. while (ns != NULL) {
  2727. ret++;
  2728. ns = ns->next;
  2729. }
  2730. return(ret);
  2731. }
  2732. /**
  2733. * xmlTextReaderNodeType:
  2734. * @reader: the xmlTextReaderPtr used
  2735. *
  2736. * Get the node type of the current node
  2737. * Reference:
  2738. * http://www.gnu.org/software/dotgnu/pnetlib-doc/System/Xml/XmlNodeType.html
  2739. *
  2740. * Returns the xmlNodeType of the current node or -1 in case of error
  2741. */
  2742. int
  2743. xmlTextReaderNodeType(xmlTextReaderPtr reader) {
  2744. xmlNodePtr node;
  2745. if (reader == NULL)
  2746. return(-1);
  2747. if (reader->node == NULL)
  2748. return(XML_READER_TYPE_NONE);
  2749. if (reader->curnode != NULL)
  2750. node = reader->curnode;
  2751. else
  2752. node = reader->node;
  2753. switch (node->type) {
  2754. case XML_ELEMENT_NODE:
  2755. if ((reader->state == XML_TEXTREADER_END) ||
  2756. (reader->state == XML_TEXTREADER_BACKTRACK))
  2757. return(XML_READER_TYPE_END_ELEMENT);
  2758. return(XML_READER_TYPE_ELEMENT);
  2759. case XML_NAMESPACE_DECL:
  2760. case XML_ATTRIBUTE_NODE:
  2761. return(XML_READER_TYPE_ATTRIBUTE);
  2762. case XML_TEXT_NODE:
  2763. if (xmlIsBlankNode(reader->node)) {
  2764. if (xmlNodeGetSpacePreserve(reader->node))
  2765. return(XML_READER_TYPE_SIGNIFICANT_WHITESPACE);
  2766. else
  2767. return(XML_READER_TYPE_WHITESPACE);
  2768. } else {
  2769. return(XML_READER_TYPE_TEXT);
  2770. }
  2771. case XML_CDATA_SECTION_NODE:
  2772. return(XML_READER_TYPE_CDATA);
  2773. case XML_ENTITY_REF_NODE:
  2774. return(XML_READER_TYPE_ENTITY_REFERENCE);
  2775. case XML_ENTITY_NODE:
  2776. return(XML_READER_TYPE_ENTITY);
  2777. case XML_PI_NODE:
  2778. return(XML_READER_TYPE_PROCESSING_INSTRUCTION);
  2779. case XML_COMMENT_NODE:
  2780. return(XML_READER_TYPE_COMMENT);
  2781. case XML_DOCUMENT_NODE:
  2782. case XML_HTML_DOCUMENT_NODE:
  2783. #ifdef LIBXML_DOCB_ENABLED
  2784. case XML_DOCB_DOCUMENT_NODE:
  2785. #endif
  2786. return(XML_READER_TYPE_DOCUMENT);
  2787. case XML_DOCUMENT_FRAG_NODE:
  2788. return(XML_READER_TYPE_DOCUMENT_FRAGMENT);
  2789. case XML_NOTATION_NODE:
  2790. return(XML_READER_TYPE_NOTATION);
  2791. case XML_DOCUMENT_TYPE_NODE:
  2792. case XML_DTD_NODE:
  2793. return(XML_READER_TYPE_DOCUMENT_TYPE);
  2794. case XML_ELEMENT_DECL:
  2795. case XML_ATTRIBUTE_DECL:
  2796. case XML_ENTITY_DECL:
  2797. case XML_XINCLUDE_START:
  2798. case XML_XINCLUDE_END:
  2799. return(XML_READER_TYPE_NONE);
  2800. }
  2801. return(-1);
  2802. }
  2803. /**
  2804. * xmlTextReaderIsEmptyElement:
  2805. * @reader: the xmlTextReaderPtr used
  2806. *
  2807. * Check if the current node is empty
  2808. *
  2809. * Returns 1 if empty, 0 if not and -1 in case of error
  2810. */
  2811. int
  2812. xmlTextReaderIsEmptyElement(xmlTextReaderPtr reader) {
  2813. if ((reader == NULL) || (reader->node == NULL))
  2814. return(-1);
  2815. if (reader->node->type != XML_ELEMENT_NODE)
  2816. return(0);
  2817. if (reader->curnode != NULL)
  2818. return(0);
  2819. if (reader->node->children != NULL)
  2820. return(0);
  2821. if (reader->state == XML_TEXTREADER_END)
  2822. return(0);
  2823. if (reader->doc != NULL)
  2824. return(1);
  2825. #ifdef LIBXML_XINCLUDE_ENABLED
  2826. if (reader->in_xinclude > 0)
  2827. return(1);
  2828. #endif
  2829. return((reader->node->extra & NODE_IS_EMPTY) != 0);
  2830. }
  2831. /**
  2832. * xmlTextReaderLocalName:
  2833. * @reader: the xmlTextReaderPtr used
  2834. *
  2835. * The local name of the node.
  2836. *
  2837. * Returns the local name or NULL if not available,
  2838. * if non NULL it need to be freed by the caller.
  2839. */
  2840. xmlChar *
  2841. xmlTextReaderLocalName(xmlTextReaderPtr reader) {
  2842. xmlNodePtr node;
  2843. if ((reader == NULL) || (reader->node == NULL))
  2844. return(NULL);
  2845. if (reader->curnode != NULL)
  2846. node = reader->curnode;
  2847. else
  2848. node = reader->node;
  2849. if (node->type == XML_NAMESPACE_DECL) {
  2850. xmlNsPtr ns = (xmlNsPtr) node;
  2851. if (ns->prefix == NULL)
  2852. return(xmlStrdup(BAD_CAST "xmlns"));
  2853. else
  2854. return(xmlStrdup(ns->prefix));
  2855. }
  2856. if ((node->type != XML_ELEMENT_NODE) &&
  2857. (node->type != XML_ATTRIBUTE_NODE))
  2858. return(xmlTextReaderName(reader));
  2859. return(xmlStrdup(node->name));
  2860. }
  2861. /**
  2862. * xmlTextReaderConstLocalName:
  2863. * @reader: the xmlTextReaderPtr used
  2864. *
  2865. * The local name of the node.
  2866. *
  2867. * Returns the local name or NULL if not available, the
  2868. * string will be deallocated with the reader.
  2869. */
  2870. const xmlChar *
  2871. xmlTextReaderConstLocalName(xmlTextReaderPtr reader) {
  2872. xmlNodePtr node;
  2873. if ((reader == NULL) || (reader->node == NULL))
  2874. return(NULL);
  2875. if (reader->curnode != NULL)
  2876. node = reader->curnode;
  2877. else
  2878. node = reader->node;
  2879. if (node->type == XML_NAMESPACE_DECL) {
  2880. xmlNsPtr ns = (xmlNsPtr) node;
  2881. if (ns->prefix == NULL)
  2882. return(CONSTSTR(BAD_CAST "xmlns"));
  2883. else
  2884. return(ns->prefix);
  2885. }
  2886. if ((node->type != XML_ELEMENT_NODE) &&
  2887. (node->type != XML_ATTRIBUTE_NODE))
  2888. return(xmlTextReaderConstName(reader));
  2889. return(node->name);
  2890. }
  2891. /**
  2892. * xmlTextReaderName:
  2893. * @reader: the xmlTextReaderPtr used
  2894. *
  2895. * The qualified name of the node, equal to Prefix :LocalName.
  2896. *
  2897. * Returns the local name or NULL if not available,
  2898. * if non NULL it need to be freed by the caller.
  2899. */
  2900. xmlChar *
  2901. xmlTextReaderName(xmlTextReaderPtr reader) {
  2902. xmlNodePtr node;
  2903. xmlChar *ret;
  2904. if ((reader == NULL) || (reader->node == NULL))
  2905. return(NULL);
  2906. if (reader->curnode != NULL)
  2907. node = reader->curnode;
  2908. else
  2909. node = reader->node;
  2910. switch (node->type) {
  2911. case XML_ELEMENT_NODE:
  2912. case XML_ATTRIBUTE_NODE:
  2913. if ((node->ns == NULL) ||
  2914. (node->ns->prefix == NULL))
  2915. return(xmlStrdup(node->name));
  2916. ret = xmlStrdup(node->ns->prefix);
  2917. ret = xmlStrcat(ret, BAD_CAST ":");
  2918. ret = xmlStrcat(ret, node->name);
  2919. return(ret);
  2920. case XML_TEXT_NODE:
  2921. return(xmlStrdup(BAD_CAST "#text"));
  2922. case XML_CDATA_SECTION_NODE:
  2923. return(xmlStrdup(BAD_CAST "#cdata-section"));
  2924. case XML_ENTITY_NODE:
  2925. case XML_ENTITY_REF_NODE:
  2926. return(xmlStrdup(node->name));
  2927. case XML_PI_NODE:
  2928. return(xmlStrdup(node->name));
  2929. case XML_COMMENT_NODE:
  2930. return(xmlStrdup(BAD_CAST "#comment"));
  2931. case XML_DOCUMENT_NODE:
  2932. case XML_HTML_DOCUMENT_NODE:
  2933. #ifdef LIBXML_DOCB_ENABLED
  2934. case XML_DOCB_DOCUMENT_NODE:
  2935. #endif
  2936. return(xmlStrdup(BAD_CAST "#document"));
  2937. case XML_DOCUMENT_FRAG_NODE:
  2938. return(xmlStrdup(BAD_CAST "#document-fragment"));
  2939. case XML_NOTATION_NODE:
  2940. return(xmlStrdup(node->name));
  2941. case XML_DOCUMENT_TYPE_NODE:
  2942. case XML_DTD_NODE:
  2943. return(xmlStrdup(node->name));
  2944. case XML_NAMESPACE_DECL: {
  2945. xmlNsPtr ns = (xmlNsPtr) node;
  2946. ret = xmlStrdup(BAD_CAST "xmlns");
  2947. if (ns->prefix == NULL)
  2948. return(ret);
  2949. ret = xmlStrcat(ret, BAD_CAST ":");
  2950. ret = xmlStrcat(ret, ns->prefix);
  2951. return(ret);
  2952. }
  2953. case XML_ELEMENT_DECL:
  2954. case XML_ATTRIBUTE_DECL:
  2955. case XML_ENTITY_DECL:
  2956. case XML_XINCLUDE_START:
  2957. case XML_XINCLUDE_END:
  2958. return(NULL);
  2959. }
  2960. return(NULL);
  2961. }
  2962. /**
  2963. * xmlTextReaderConstName:
  2964. * @reader: the xmlTextReaderPtr used
  2965. *
  2966. * The qualified name of the node, equal to Prefix :LocalName.
  2967. *
  2968. * Returns the local name or NULL if not available, the string is
  2969. * deallocated with the reader.
  2970. */
  2971. const xmlChar *
  2972. xmlTextReaderConstName(xmlTextReaderPtr reader) {
  2973. xmlNodePtr node;
  2974. if ((reader == NULL) || (reader->node == NULL))
  2975. return(NULL);
  2976. if (reader->curnode != NULL)
  2977. node = reader->curnode;
  2978. else
  2979. node = reader->node;
  2980. switch (node->type) {
  2981. case XML_ELEMENT_NODE:
  2982. case XML_ATTRIBUTE_NODE:
  2983. if ((node->ns == NULL) ||
  2984. (node->ns->prefix == NULL))
  2985. return(node->name);
  2986. return(CONSTQSTR(node->ns->prefix, node->name));
  2987. case XML_TEXT_NODE:
  2988. return(CONSTSTR(BAD_CAST "#text"));
  2989. case XML_CDATA_SECTION_NODE:
  2990. return(CONSTSTR(BAD_CAST "#cdata-section"));
  2991. case XML_ENTITY_NODE:
  2992. case XML_ENTITY_REF_NODE:
  2993. return(CONSTSTR(node->name));
  2994. case XML_PI_NODE:
  2995. return(CONSTSTR(node->name));
  2996. case XML_COMMENT_NODE:
  2997. return(CONSTSTR(BAD_CAST "#comment"));
  2998. case XML_DOCUMENT_NODE:
  2999. case XML_HTML_DOCUMENT_NODE:
  3000. #ifdef LIBXML_DOCB_ENABLED
  3001. case XML_DOCB_DOCUMENT_NODE:
  3002. #endif
  3003. return(CONSTSTR(BAD_CAST "#document"));
  3004. case XML_DOCUMENT_FRAG_NODE:
  3005. return(CONSTSTR(BAD_CAST "#document-fragment"));
  3006. case XML_NOTATION_NODE:
  3007. return(CONSTSTR(node->name));
  3008. case XML_DOCUMENT_TYPE_NODE:
  3009. case XML_DTD_NODE:
  3010. return(CONSTSTR(node->name));
  3011. case XML_NAMESPACE_DECL: {
  3012. xmlNsPtr ns = (xmlNsPtr) node;
  3013. if (ns->prefix == NULL)
  3014. return(CONSTSTR(BAD_CAST "xmlns"));
  3015. return(CONSTQSTR(BAD_CAST "xmlns", ns->prefix));
  3016. }
  3017. case XML_ELEMENT_DECL:
  3018. case XML_ATTRIBUTE_DECL:
  3019. case XML_ENTITY_DECL:
  3020. case XML_XINCLUDE_START:
  3021. case XML_XINCLUDE_END:
  3022. return(NULL);
  3023. }
  3024. return(NULL);
  3025. }
  3026. /**
  3027. * xmlTextReaderPrefix:
  3028. * @reader: the xmlTextReaderPtr used
  3029. *
  3030. * A shorthand reference to the namespace associated with the node.
  3031. *
  3032. * Returns the prefix or NULL if not available,
  3033. * if non NULL it need to be freed by the caller.
  3034. */
  3035. xmlChar *
  3036. xmlTextReaderPrefix(xmlTextReaderPtr reader) {
  3037. xmlNodePtr node;
  3038. if ((reader == NULL) || (reader->node == NULL))
  3039. return(NULL);
  3040. if (reader->curnode != NULL)
  3041. node = reader->curnode;
  3042. else
  3043. node = reader->node;
  3044. if (node->type == XML_NAMESPACE_DECL) {
  3045. xmlNsPtr ns = (xmlNsPtr) node;
  3046. if (ns->prefix == NULL)
  3047. return(NULL);
  3048. return(xmlStrdup(BAD_CAST "xmlns"));
  3049. }
  3050. if ((node->type != XML_ELEMENT_NODE) &&
  3051. (node->type != XML_ATTRIBUTE_NODE))
  3052. return(NULL);
  3053. if ((node->ns != NULL) && (node->ns->prefix != NULL))
  3054. return(xmlStrdup(node->ns->prefix));
  3055. return(NULL);
  3056. }
  3057. /**
  3058. * xmlTextReaderConstPrefix:
  3059. * @reader: the xmlTextReaderPtr used
  3060. *
  3061. * A shorthand reference to the namespace associated with the node.
  3062. *
  3063. * Returns the prefix or NULL if not available, the string is deallocated
  3064. * with the reader.
  3065. */
  3066. const xmlChar *
  3067. xmlTextReaderConstPrefix(xmlTextReaderPtr reader) {
  3068. xmlNodePtr node;
  3069. if ((reader == NULL) || (reader->node == NULL))
  3070. return(NULL);
  3071. if (reader->curnode != NULL)
  3072. node = reader->curnode;
  3073. else
  3074. node = reader->node;
  3075. if (node->type == XML_NAMESPACE_DECL) {
  3076. xmlNsPtr ns = (xmlNsPtr) node;
  3077. if (ns->prefix == NULL)
  3078. return(NULL);
  3079. return(CONSTSTR(BAD_CAST "xmlns"));
  3080. }
  3081. if ((node->type != XML_ELEMENT_NODE) &&
  3082. (node->type != XML_ATTRIBUTE_NODE))
  3083. return(NULL);
  3084. if ((node->ns != NULL) && (node->ns->prefix != NULL))
  3085. return(CONSTSTR(node->ns->prefix));
  3086. return(NULL);
  3087. }
  3088. /**
  3089. * xmlTextReaderNamespaceUri:
  3090. * @reader: the xmlTextReaderPtr used
  3091. *
  3092. * The URI defining the namespace associated with the node.
  3093. *
  3094. * Returns the namespace URI or NULL if not available,
  3095. * if non NULL it need to be freed by the caller.
  3096. */
  3097. xmlChar *
  3098. xmlTextReaderNamespaceUri(xmlTextReaderPtr reader) {
  3099. xmlNodePtr node;
  3100. if ((reader == NULL) || (reader->node == NULL))
  3101. return(NULL);
  3102. if (reader->curnode != NULL)
  3103. node = reader->curnode;
  3104. else
  3105. node = reader->node;
  3106. if (node->type == XML_NAMESPACE_DECL)
  3107. return(xmlStrdup(BAD_CAST "http://www.w3.org/2000/xmlns/"));
  3108. if ((node->type != XML_ELEMENT_NODE) &&
  3109. (node->type != XML_ATTRIBUTE_NODE))
  3110. return(NULL);
  3111. if (node->ns != NULL)
  3112. return(xmlStrdup(node->ns->href));
  3113. return(NULL);
  3114. }
  3115. /**
  3116. * xmlTextReaderConstNamespaceUri:
  3117. * @reader: the xmlTextReaderPtr used
  3118. *
  3119. * The URI defining the namespace associated with the node.
  3120. *
  3121. * Returns the namespace URI or NULL if not available, the string
  3122. * will be deallocated with the reader
  3123. */
  3124. const xmlChar *
  3125. xmlTextReaderConstNamespaceUri(xmlTextReaderPtr reader) {
  3126. xmlNodePtr node;
  3127. if ((reader == NULL) || (reader->node == NULL))
  3128. return(NULL);
  3129. if (reader->curnode != NULL)
  3130. node = reader->curnode;
  3131. else
  3132. node = reader->node;
  3133. if (node->type == XML_NAMESPACE_DECL)
  3134. return(CONSTSTR(BAD_CAST "http://www.w3.org/2000/xmlns/"));
  3135. if ((node->type != XML_ELEMENT_NODE) &&
  3136. (node->type != XML_ATTRIBUTE_NODE))
  3137. return(NULL);
  3138. if (node->ns != NULL)
  3139. return(CONSTSTR(node->ns->href));
  3140. return(NULL);
  3141. }
  3142. /**
  3143. * xmlTextReaderBaseUri:
  3144. * @reader: the xmlTextReaderPtr used
  3145. *
  3146. * The base URI of the node.
  3147. *
  3148. * Returns the base URI or NULL if not available,
  3149. * if non NULL it need to be freed by the caller.
  3150. */
  3151. xmlChar *
  3152. xmlTextReaderBaseUri(xmlTextReaderPtr reader) {
  3153. if ((reader == NULL) || (reader->node == NULL))
  3154. return(NULL);
  3155. return(xmlNodeGetBase(NULL, reader->node));
  3156. }
  3157. /**
  3158. * xmlTextReaderConstBaseUri:
  3159. * @reader: the xmlTextReaderPtr used
  3160. *
  3161. * The base URI of the node.
  3162. *
  3163. * Returns the base URI or NULL if not available, the string
  3164. * will be deallocated with the reader
  3165. */
  3166. const xmlChar *
  3167. xmlTextReaderConstBaseUri(xmlTextReaderPtr reader) {
  3168. xmlChar *tmp;
  3169. const xmlChar *ret;
  3170. if ((reader == NULL) || (reader->node == NULL))
  3171. return(NULL);
  3172. tmp = xmlNodeGetBase(NULL, reader->node);
  3173. if (tmp == NULL)
  3174. return(NULL);
  3175. ret = CONSTSTR(tmp);
  3176. xmlFree(tmp);
  3177. return(ret);
  3178. }
  3179. /**
  3180. * xmlTextReaderDepth:
  3181. * @reader: the xmlTextReaderPtr used
  3182. *
  3183. * The depth of the node in the tree.
  3184. *
  3185. * Returns the depth or -1 in case of error
  3186. */
  3187. int
  3188. xmlTextReaderDepth(xmlTextReaderPtr reader) {
  3189. if (reader == NULL)
  3190. return(-1);
  3191. if (reader->node == NULL)
  3192. return(0);
  3193. if (reader->curnode != NULL) {
  3194. if ((reader->curnode->type == XML_ATTRIBUTE_NODE) ||
  3195. (reader->curnode->type == XML_NAMESPACE_DECL))
  3196. return(reader->depth + 1);
  3197. return(reader->depth + 2);
  3198. }
  3199. return(reader->depth);
  3200. }
  3201. /**
  3202. * xmlTextReaderHasAttributes:
  3203. * @reader: the xmlTextReaderPtr used
  3204. *
  3205. * Whether the node has attributes.
  3206. *
  3207. * Returns 1 if true, 0 if false, and -1 in case or error
  3208. */
  3209. int
  3210. xmlTextReaderHasAttributes(xmlTextReaderPtr reader) {
  3211. xmlNodePtr node;
  3212. if (reader == NULL)
  3213. return(-1);
  3214. if (reader->node == NULL)
  3215. return(0);
  3216. if (reader->curnode != NULL)
  3217. node = reader->curnode;
  3218. else
  3219. node = reader->node;
  3220. if ((node->type == XML_ELEMENT_NODE) &&
  3221. ((node->properties != NULL) || (node->nsDef != NULL)))
  3222. return(1);
  3223. /* TODO: handle the xmlDecl */
  3224. return(0);
  3225. }
  3226. /**
  3227. * xmlTextReaderHasValue:
  3228. * @reader: the xmlTextReaderPtr used
  3229. *
  3230. * Whether the node can have a text value.
  3231. *
  3232. * Returns 1 if true, 0 if false, and -1 in case or error
  3233. */
  3234. int
  3235. xmlTextReaderHasValue(xmlTextReaderPtr reader) {
  3236. xmlNodePtr node;
  3237. if (reader == NULL)
  3238. return(-1);
  3239. if (reader->node == NULL)
  3240. return(0);
  3241. if (reader->curnode != NULL)
  3242. node = reader->curnode;
  3243. else
  3244. node = reader->node;
  3245. switch (node->type) {
  3246. case XML_ATTRIBUTE_NODE:
  3247. case XML_TEXT_NODE:
  3248. case XML_CDATA_SECTION_NODE:
  3249. case XML_PI_NODE:
  3250. case XML_COMMENT_NODE:
  3251. case XML_NAMESPACE_DECL:
  3252. return(1);
  3253. default:
  3254. break;
  3255. }
  3256. return(0);
  3257. }
  3258. /**
  3259. * xmlTextReaderValue:
  3260. * @reader: the xmlTextReaderPtr used
  3261. *
  3262. * Provides the text value of the node if present
  3263. *
  3264. * Returns the string or NULL if not available. The result must be deallocated
  3265. * with xmlFree()
  3266. */
  3267. xmlChar *
  3268. xmlTextReaderValue(xmlTextReaderPtr reader) {
  3269. xmlNodePtr node;
  3270. if (reader == NULL)
  3271. return(NULL);
  3272. if (reader->node == NULL)
  3273. return(NULL);
  3274. if (reader->curnode != NULL)
  3275. node = reader->curnode;
  3276. else
  3277. node = reader->node;
  3278. switch (node->type) {
  3279. case XML_NAMESPACE_DECL:
  3280. return(xmlStrdup(((xmlNsPtr) node)->href));
  3281. case XML_ATTRIBUTE_NODE:{
  3282. xmlAttrPtr attr = (xmlAttrPtr) node;
  3283. if (attr->parent != NULL)
  3284. return (xmlNodeListGetString
  3285. (attr->parent->doc, attr->children, 1));
  3286. else
  3287. return (xmlNodeListGetString(NULL, attr->children, 1));
  3288. break;
  3289. }
  3290. case XML_TEXT_NODE:
  3291. case XML_CDATA_SECTION_NODE:
  3292. case XML_PI_NODE:
  3293. case XML_COMMENT_NODE:
  3294. if (node->content != NULL)
  3295. return (xmlStrdup(node->content));
  3296. default:
  3297. break;
  3298. }
  3299. return(NULL);
  3300. }
  3301. /**
  3302. * xmlTextReaderConstValue:
  3303. * @reader: the xmlTextReaderPtr used
  3304. *
  3305. * Provides the text value of the node if present
  3306. *
  3307. * Returns the string or NULL if not available. The result will be
  3308. * deallocated on the next Read() operation.
  3309. */
  3310. const xmlChar *
  3311. xmlTextReaderConstValue(xmlTextReaderPtr reader) {
  3312. xmlNodePtr node;
  3313. if (reader == NULL)
  3314. return(NULL);
  3315. if (reader->node == NULL)
  3316. return(NULL);
  3317. if (reader->curnode != NULL)
  3318. node = reader->curnode;
  3319. else
  3320. node = reader->node;
  3321. switch (node->type) {
  3322. case XML_NAMESPACE_DECL:
  3323. return(((xmlNsPtr) node)->href);
  3324. case XML_ATTRIBUTE_NODE:{
  3325. xmlAttrPtr attr = (xmlAttrPtr) node;
  3326. if ((attr->children != NULL) &&
  3327. (attr->children->type == XML_TEXT_NODE) &&
  3328. (attr->children->next == NULL))
  3329. return(attr->children->content);
  3330. else {
  3331. if (reader->buffer == NULL)
  3332. reader->buffer = xmlBufferCreateSize(100);
  3333. if (reader->buffer == NULL) {
  3334. xmlGenericError(xmlGenericErrorContext,
  3335. "xmlTextReaderSetup : malloc failed\n");
  3336. return (NULL);
  3337. }
  3338. reader->buffer->use = 0;
  3339. xmlNodeBufGetContent(reader->buffer, node);
  3340. return(reader->buffer->content);
  3341. }
  3342. break;
  3343. }
  3344. case XML_TEXT_NODE:
  3345. case XML_CDATA_SECTION_NODE:
  3346. case XML_PI_NODE:
  3347. case XML_COMMENT_NODE:
  3348. return(node->content);
  3349. default:
  3350. break;
  3351. }
  3352. return(NULL);
  3353. }
  3354. /**
  3355. * xmlTextReaderIsDefault:
  3356. * @reader: the xmlTextReaderPtr used
  3357. *
  3358. * Whether an Attribute node was generated from the default value
  3359. * defined in the DTD or schema.
  3360. *
  3361. * Returns 0 if not defaulted, 1 if defaulted, and -1 in case of error
  3362. */
  3363. int
  3364. xmlTextReaderIsDefault(xmlTextReaderPtr reader) {
  3365. if (reader == NULL)
  3366. return(-1);
  3367. return(0);
  3368. }
  3369. /**
  3370. * xmlTextReaderQuoteChar:
  3371. * @reader: the xmlTextReaderPtr used
  3372. *
  3373. * The quotation mark character used to enclose the value of an attribute.
  3374. *
  3375. * Returns " or ' and -1 in case of error
  3376. */
  3377. int
  3378. xmlTextReaderQuoteChar(xmlTextReaderPtr reader) {
  3379. if (reader == NULL)
  3380. return(-1);
  3381. /* TODO maybe lookup the attribute value for " first */
  3382. return((int) '"');
  3383. }
  3384. /**
  3385. * xmlTextReaderXmlLang:
  3386. * @reader: the xmlTextReaderPtr used
  3387. *
  3388. * The xml:lang scope within which the node resides.
  3389. *
  3390. * Returns the xml:lang value or NULL if none exists.,
  3391. * if non NULL it need to be freed by the caller.
  3392. */
  3393. xmlChar *
  3394. xmlTextReaderXmlLang(xmlTextReaderPtr reader) {
  3395. if (reader == NULL)
  3396. return(NULL);
  3397. if (reader->node == NULL)
  3398. return(NULL);
  3399. return(xmlNodeGetLang(reader->node));
  3400. }
  3401. /**
  3402. * xmlTextReaderConstXmlLang:
  3403. * @reader: the xmlTextReaderPtr used
  3404. *
  3405. * The xml:lang scope within which the node resides.
  3406. *
  3407. * Returns the xml:lang value or NULL if none exists.
  3408. */
  3409. const xmlChar *
  3410. xmlTextReaderConstXmlLang(xmlTextReaderPtr reader) {
  3411. xmlChar *tmp;
  3412. const xmlChar *ret;
  3413. if (reader == NULL)
  3414. return(NULL);
  3415. if (reader->node == NULL)
  3416. return(NULL);
  3417. tmp = xmlNodeGetLang(reader->node);
  3418. if (tmp == NULL)
  3419. return(NULL);
  3420. ret = CONSTSTR(tmp);
  3421. xmlFree(tmp);
  3422. return(ret);
  3423. }
  3424. /**
  3425. * xmlTextReaderConstString:
  3426. * @reader: the xmlTextReaderPtr used
  3427. * @str: the string to intern.
  3428. *
  3429. * Get an interned string from the reader, allows for example to
  3430. * speedup string name comparisons
  3431. *
  3432. * Returns an interned copy of the string or NULL in case of error. The
  3433. * string will be deallocated with the reader.
  3434. */
  3435. const xmlChar *
  3436. xmlTextReaderConstString(xmlTextReaderPtr reader, const xmlChar *str) {
  3437. if (reader == NULL)
  3438. return(NULL);
  3439. return(CONSTSTR(str));
  3440. }
  3441. /**
  3442. * xmlTextReaderNormalization:
  3443. * @reader: the xmlTextReaderPtr used
  3444. *
  3445. * The value indicating whether to normalize white space and attribute values.
  3446. * Since attribute value and end of line normalizations are a MUST in the XML
  3447. * specification only the value true is accepted. The broken bahaviour of
  3448. * accepting out of range character entities like &#0; is of course not
  3449. * supported either.
  3450. *
  3451. * Returns 1 or -1 in case of error.
  3452. */
  3453. int
  3454. xmlTextReaderNormalization(xmlTextReaderPtr reader) {
  3455. if (reader == NULL)
  3456. return(-1);
  3457. return(1);
  3458. }
  3459. /************************************************************************
  3460. * *
  3461. * Extensions to the base APIs *
  3462. * *
  3463. ************************************************************************/
  3464. /**
  3465. * xmlTextReaderSetParserProp:
  3466. * @reader: the xmlTextReaderPtr used
  3467. * @prop: the xmlParserProperties to set
  3468. * @value: usually 0 or 1 to (de)activate it
  3469. *
  3470. * Change the parser processing behaviour by changing some of its internal
  3471. * properties. Note that some properties can only be changed before any
  3472. * read has been done.
  3473. *
  3474. * Returns 0 if the call was successful, or -1 in case of error
  3475. */
  3476. int
  3477. xmlTextReaderSetParserProp(xmlTextReaderPtr reader, int prop, int value) {
  3478. xmlParserProperties p = (xmlParserProperties) prop;
  3479. xmlParserCtxtPtr ctxt;
  3480. if ((reader == NULL) || (reader->ctxt == NULL))
  3481. return(-1);
  3482. ctxt = reader->ctxt;
  3483. switch (p) {
  3484. case XML_PARSER_LOADDTD:
  3485. if (value != 0) {
  3486. if (ctxt->loadsubset == 0) {
  3487. if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
  3488. return(-1);
  3489. ctxt->loadsubset = XML_DETECT_IDS;
  3490. }
  3491. } else {
  3492. ctxt->loadsubset = 0;
  3493. }
  3494. return(0);
  3495. case XML_PARSER_DEFAULTATTRS:
  3496. if (value != 0) {
  3497. ctxt->loadsubset |= XML_COMPLETE_ATTRS;
  3498. } else {
  3499. if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
  3500. ctxt->loadsubset -= XML_COMPLETE_ATTRS;
  3501. }
  3502. return(0);
  3503. case XML_PARSER_VALIDATE:
  3504. if (value != 0) {
  3505. ctxt->validate = 1;
  3506. reader->validate = XML_TEXTREADER_VALIDATE_DTD;
  3507. } else {
  3508. ctxt->validate = 0;
  3509. }
  3510. return(0);
  3511. case XML_PARSER_SUBST_ENTITIES:
  3512. if (value != 0) {
  3513. ctxt->replaceEntities = 1;
  3514. } else {
  3515. ctxt->replaceEntities = 0;
  3516. }
  3517. return(0);
  3518. }
  3519. return(-1);
  3520. }
  3521. /**
  3522. * xmlTextReaderGetParserProp:
  3523. * @reader: the xmlTextReaderPtr used
  3524. * @prop: the xmlParserProperties to get
  3525. *
  3526. * Read the parser internal property.
  3527. *
  3528. * Returns the value, usually 0 or 1, or -1 in case of error.
  3529. */
  3530. int
  3531. xmlTextReaderGetParserProp(xmlTextReaderPtr reader, int prop) {
  3532. xmlParserProperties p = (xmlParserProperties) prop;
  3533. xmlParserCtxtPtr ctxt;
  3534. if ((reader == NULL) || (reader->ctxt == NULL))
  3535. return(-1);
  3536. ctxt = reader->ctxt;
  3537. switch (p) {
  3538. case XML_PARSER_LOADDTD:
  3539. if ((ctxt->loadsubset != 0) || (ctxt->validate != 0))
  3540. return(1);
  3541. return(0);
  3542. case XML_PARSER_DEFAULTATTRS:
  3543. if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
  3544. return(1);
  3545. return(0);
  3546. case XML_PARSER_VALIDATE:
  3547. return(reader->validate);
  3548. case XML_PARSER_SUBST_ENTITIES:
  3549. return(ctxt->replaceEntities);
  3550. }
  3551. return(-1);
  3552. }
  3553. /**
  3554. * xmlTextReaderGetParserLineNumber:
  3555. * @reader: the user data (XML reader context)
  3556. *
  3557. * Provide the line number of the current parsing point.
  3558. *
  3559. * Returns an int or 0 if not available
  3560. */
  3561. int
  3562. xmlTextReaderGetParserLineNumber(xmlTextReaderPtr reader)
  3563. {
  3564. if ((reader == NULL) || (reader->ctxt == NULL) ||
  3565. (reader->ctxt->input == NULL)) {
  3566. return (0);
  3567. }
  3568. return (reader->ctxt->input->line);
  3569. }
  3570. /**
  3571. * xmlTextReaderGetParserColumnNumber:
  3572. * @reader: the user data (XML reader context)
  3573. *
  3574. * Provide the column number of the current parsing point.
  3575. *
  3576. * Returns an int or 0 if not available
  3577. */
  3578. int
  3579. xmlTextReaderGetParserColumnNumber(xmlTextReaderPtr reader)
  3580. {
  3581. if ((reader == NULL) || (reader->ctxt == NULL) ||
  3582. (reader->ctxt->input == NULL)) {
  3583. return (0);
  3584. }
  3585. return (reader->ctxt->input->col);
  3586. }
  3587. /**
  3588. * xmlTextReaderCurrentNode:
  3589. * @reader: the xmlTextReaderPtr used
  3590. *
  3591. * Hacking interface allowing to get the xmlNodePtr correponding to the
  3592. * current node being accessed by the xmlTextReader. This is dangerous
  3593. * because the underlying node may be destroyed on the next Reads.
  3594. *
  3595. * Returns the xmlNodePtr or NULL in case of error.
  3596. */
  3597. xmlNodePtr
  3598. xmlTextReaderCurrentNode(xmlTextReaderPtr reader) {
  3599. if (reader == NULL)
  3600. return(NULL);
  3601. if (reader->curnode != NULL)
  3602. return(reader->curnode);
  3603. return(reader->node);
  3604. }
  3605. /**
  3606. * xmlTextReaderPreserve:
  3607. * @reader: the xmlTextReaderPtr used
  3608. *
  3609. * This tells the XML Reader to preserve the current node.
  3610. * The caller must also use xmlTextReaderCurrentDoc() to
  3611. * keep an handle on the resulting document once parsing has finished
  3612. *
  3613. * Returns the xmlNodePtr or NULL in case of error.
  3614. */
  3615. xmlNodePtr
  3616. xmlTextReaderPreserve(xmlTextReaderPtr reader) {
  3617. xmlNodePtr cur, parent;
  3618. if (reader == NULL)
  3619. return(NULL);
  3620. if (reader->curnode != NULL)
  3621. cur = reader->curnode;
  3622. else
  3623. cur = reader->node;
  3624. if (cur == NULL)
  3625. return(NULL);
  3626. if ((cur->type != XML_DOCUMENT_NODE) && (cur->type != XML_DTD_NODE)) {
  3627. cur->extra |= NODE_IS_PRESERVED;
  3628. cur->extra |= NODE_IS_SPRESERVED;
  3629. }
  3630. reader->preserves++;
  3631. parent = cur->parent;;
  3632. while (parent != NULL) {
  3633. if (parent->type == XML_ELEMENT_NODE)
  3634. parent->extra |= NODE_IS_PRESERVED;
  3635. parent = parent->parent;
  3636. }
  3637. return(cur);
  3638. }
  3639. #ifdef LIBXML_PATTERN_ENABLED
  3640. /**
  3641. * xmlTextReaderPreservePattern:
  3642. * @reader: the xmlTextReaderPtr used
  3643. * @pattern: an XPath subset pattern
  3644. * @namespaces: the prefix definitions, array of [URI, prefix] or NULL
  3645. *
  3646. * This tells the XML Reader to preserve all nodes matched by the
  3647. * pattern. The caller must also use xmlTextReaderCurrentDoc() to
  3648. * keep an handle on the resulting document once parsing has finished
  3649. *
  3650. * Returns a positive number in case of success and -1 in case of error
  3651. */
  3652. int
  3653. xmlTextReaderPreservePattern(xmlTextReaderPtr reader, const xmlChar *pattern,
  3654. const xmlChar **namespaces)
  3655. {
  3656. xmlPatternPtr comp;
  3657. if ((reader == NULL) || (pattern == NULL))
  3658. return(-1);
  3659. comp = xmlPatterncompile(pattern, reader->dict, 0, namespaces);
  3660. if (comp == NULL)
  3661. return(-1);
  3662. if (reader->patternMax <= 0) {
  3663. reader->patternMax = 4;
  3664. reader->patternTab = (xmlPatternPtr *) xmlMalloc(reader->patternMax *
  3665. sizeof(reader->patternTab[0]));
  3666. if (reader->patternTab == NULL) {
  3667. xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
  3668. return (-1);
  3669. }
  3670. }
  3671. if (reader->patternNr >= reader->patternMax) {
  3672. xmlPatternPtr *tmp;
  3673. reader->patternMax *= 2;
  3674. tmp = (xmlPatternPtr *) xmlRealloc(reader->patternTab,
  3675. reader->patternMax *
  3676. sizeof(reader->patternTab[0]));
  3677. if (tmp == NULL) {
  3678. xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
  3679. reader->patternMax /= 2;
  3680. return (-1);
  3681. }
  3682. reader->patternTab = tmp;
  3683. }
  3684. reader->patternTab[reader->patternNr] = comp;
  3685. return(reader->patternNr++);
  3686. }
  3687. #endif
  3688. /**
  3689. * xmlTextReaderCurrentDoc:
  3690. * @reader: the xmlTextReaderPtr used
  3691. *
  3692. * Hacking interface allowing to get the xmlDocPtr correponding to the
  3693. * current document being accessed by the xmlTextReader.
  3694. * NOTE: as a result of this call, the reader will not destroy the
  3695. * associated XML document and calling xmlFreeDoc() on the result
  3696. * is needed once the reader parsing has finished.
  3697. *
  3698. * Returns the xmlDocPtr or NULL in case of error.
  3699. */
  3700. xmlDocPtr
  3701. xmlTextReaderCurrentDoc(xmlTextReaderPtr reader) {
  3702. if (reader == NULL)
  3703. return(NULL);
  3704. if (reader->doc != NULL)
  3705. return(reader->doc);
  3706. if ((reader->ctxt == NULL) || (reader->ctxt->myDoc == NULL))
  3707. return(NULL);
  3708. reader->preserve = 1;
  3709. return(reader->ctxt->myDoc);
  3710. }
  3711. #ifdef LIBXML_SCHEMAS_ENABLED
  3712. static char *xmlTextReaderBuildMessage(const char *msg, va_list ap);
  3713. static void XMLCDECL
  3714. xmlTextReaderValidityError(void *ctxt, const char *msg, ...);
  3715. static void XMLCDECL
  3716. xmlTextReaderValidityWarning(void *ctxt, const char *msg, ...);
  3717. static void XMLCDECL
  3718. xmlTextReaderValidityErrorRelay(void *ctx, const char *msg, ...)
  3719. {
  3720. xmlTextReaderPtr reader = (xmlTextReaderPtr) ctx;
  3721. char *str;
  3722. va_list ap;
  3723. va_start(ap, msg);
  3724. str = xmlTextReaderBuildMessage(msg, ap);
  3725. if (!reader->errorFunc) {
  3726. xmlTextReaderValidityError(ctx, "%s", str);
  3727. } else {
  3728. reader->errorFunc(reader->errorFuncArg, str,
  3729. XML_PARSER_SEVERITY_VALIDITY_ERROR,
  3730. NULL /* locator */ );
  3731. }
  3732. if (str != NULL)
  3733. xmlFree(str);
  3734. va_end(ap);
  3735. }
  3736. static void XMLCDECL
  3737. xmlTextReaderValidityWarningRelay(void *ctx, const char *msg, ...)
  3738. {
  3739. xmlTextReaderPtr reader = (xmlTextReaderPtr) ctx;
  3740. char *str;
  3741. va_list ap;
  3742. va_start(ap, msg);
  3743. str = xmlTextReaderBuildMessage(msg, ap);
  3744. if (!reader->errorFunc) {
  3745. xmlTextReaderValidityWarning(ctx, "%s", str);
  3746. } else {
  3747. reader->errorFunc(reader->errorFuncArg, str,
  3748. XML_PARSER_SEVERITY_VALIDITY_WARNING,
  3749. NULL /* locator */ );
  3750. }
  3751. if (str != NULL)
  3752. xmlFree(str);
  3753. va_end(ap);
  3754. }
  3755. static void
  3756. xmlTextReaderStructuredError(void *ctxt, xmlErrorPtr error);
  3757. static void
  3758. xmlTextReaderValidityStructuredRelay(void *userData, xmlErrorPtr error)
  3759. {
  3760. xmlTextReaderPtr reader = (xmlTextReaderPtr) userData;
  3761. if (reader->sErrorFunc) {
  3762. reader->sErrorFunc(reader->errorFuncArg, error);
  3763. } else {
  3764. xmlTextReaderStructuredError(reader, error);
  3765. }
  3766. }
  3767. /**
  3768. * xmlTextReaderRelaxNGSetSchema:
  3769. * @reader: the xmlTextReaderPtr used
  3770. * @schema: a precompiled RelaxNG schema
  3771. *
  3772. * Use RelaxNG to validate the document as it is processed.
  3773. * Activation is only possible before the first Read().
  3774. * if @schema is NULL, then RelaxNG validation is desactivated.
  3775. @ The @schema should not be freed until the reader is deallocated
  3776. * or its use has been deactivated.
  3777. *
  3778. * Returns 0 in case the RelaxNG validation could be (des)activated and
  3779. * -1 in case of error.
  3780. */
  3781. int
  3782. xmlTextReaderRelaxNGSetSchema(xmlTextReaderPtr reader, xmlRelaxNGPtr schema) {
  3783. if (reader == NULL)
  3784. return(-1);
  3785. if (schema == NULL) {
  3786. if (reader->rngSchemas != NULL) {
  3787. xmlRelaxNGFree(reader->rngSchemas);
  3788. reader->rngSchemas = NULL;
  3789. }
  3790. if (reader->rngValidCtxt != NULL) {
  3791. xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
  3792. reader->rngValidCtxt = NULL;
  3793. }
  3794. return(0);
  3795. }
  3796. if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
  3797. return(-1);
  3798. if (reader->rngSchemas != NULL) {
  3799. xmlRelaxNGFree(reader->rngSchemas);
  3800. reader->rngSchemas = NULL;
  3801. }
  3802. if (reader->rngValidCtxt != NULL) {
  3803. xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
  3804. reader->rngValidCtxt = NULL;
  3805. }
  3806. reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(schema);
  3807. if (reader->rngValidCtxt == NULL)
  3808. return(-1);
  3809. if (reader->errorFunc != NULL) {
  3810. xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
  3811. xmlTextReaderValidityErrorRelay,
  3812. xmlTextReaderValidityWarningRelay,
  3813. reader);
  3814. }
  3815. if (reader->sErrorFunc != NULL) {
  3816. xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt,
  3817. xmlTextReaderValidityStructuredRelay,
  3818. reader);
  3819. }
  3820. reader->rngValidErrors = 0;
  3821. reader->rngFullNode = NULL;
  3822. reader->validate = XML_TEXTREADER_VALIDATE_RNG;
  3823. return(0);
  3824. }
  3825. /**
  3826. * xmlTextReaderSetSchema:
  3827. * @reader: the xmlTextReaderPtr used
  3828. * @schema: a precompiled Schema schema
  3829. *
  3830. * Use XSD Schema to validate the document as it is processed.
  3831. * Activation is only possible before the first Read().
  3832. * if @schema is NULL, then Schema validation is desactivated.
  3833. @ The @schema should not be freed until the reader is deallocated
  3834. * or its use has been deactivated.
  3835. *
  3836. * Returns 0 in case the Schema validation could be (des)activated and
  3837. * -1 in case of error.
  3838. */
  3839. int
  3840. xmlTextReaderSetSchema(xmlTextReaderPtr reader, xmlSchemaPtr schema) {
  3841. if (reader == NULL)
  3842. return(-1);
  3843. if (schema == NULL) {
  3844. if (reader->xsdPlug != NULL) {
  3845. xmlSchemaSAXUnplug(reader->xsdPlug);
  3846. reader->xsdPlug = NULL;
  3847. }
  3848. if (reader->xsdValidCtxt != NULL) {
  3849. if (! reader->xsdPreserveCtxt)
  3850. xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
  3851. reader->xsdValidCtxt = NULL;
  3852. }
  3853. reader->xsdPreserveCtxt = 0;
  3854. if (reader->xsdSchemas != NULL) {
  3855. xmlSchemaFree(reader->xsdSchemas);
  3856. reader->xsdSchemas = NULL;
  3857. }
  3858. return(0);
  3859. }
  3860. if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
  3861. return(-1);
  3862. if (reader->xsdPlug != NULL) {
  3863. xmlSchemaSAXUnplug(reader->xsdPlug);
  3864. reader->xsdPlug = NULL;
  3865. }
  3866. if (reader->xsdValidCtxt != NULL) {
  3867. if (! reader->xsdPreserveCtxt)
  3868. xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
  3869. reader->xsdValidCtxt = NULL;
  3870. }
  3871. reader->xsdPreserveCtxt = 0;
  3872. if (reader->xsdSchemas != NULL) {
  3873. xmlSchemaFree(reader->xsdSchemas);
  3874. reader->xsdSchemas = NULL;
  3875. }
  3876. reader->xsdValidCtxt = xmlSchemaNewValidCtxt(schema);
  3877. if (reader->xsdValidCtxt == NULL) {
  3878. xmlSchemaFree(reader->xsdSchemas);
  3879. reader->xsdSchemas = NULL;
  3880. return(-1);
  3881. }
  3882. reader->xsdPlug = xmlSchemaSAXPlug(reader->xsdValidCtxt,
  3883. &(reader->ctxt->sax),
  3884. &(reader->ctxt->userData));
  3885. if (reader->xsdPlug == NULL) {
  3886. xmlSchemaFree(reader->xsdSchemas);
  3887. reader->xsdSchemas = NULL;
  3888. xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
  3889. reader->xsdValidCtxt = NULL;
  3890. return(-1);
  3891. }
  3892. if (reader->errorFunc != NULL) {
  3893. xmlSchemaSetValidErrors(reader->xsdValidCtxt,
  3894. xmlTextReaderValidityErrorRelay,
  3895. xmlTextReaderValidityWarningRelay,
  3896. reader);
  3897. }
  3898. if (reader->sErrorFunc != NULL) {
  3899. xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt,
  3900. xmlTextReaderValidityStructuredRelay,
  3901. reader);
  3902. }
  3903. reader->xsdValidErrors = 0;
  3904. reader->validate = XML_TEXTREADER_VALIDATE_XSD;
  3905. return(0);
  3906. }
  3907. /**
  3908. * xmlTextReaderRelaxNGValidate:
  3909. * @reader: the xmlTextReaderPtr used
  3910. * @rng: the path to a RelaxNG schema or NULL
  3911. *
  3912. * Use RelaxNG to validate the document as it is processed.
  3913. * Activation is only possible before the first Read().
  3914. * if @rng is NULL, then RelaxNG validation is deactivated.
  3915. *
  3916. * Returns 0 in case the RelaxNG validation could be (de)activated and
  3917. * -1 in case of error.
  3918. */
  3919. int
  3920. xmlTextReaderRelaxNGValidate(xmlTextReaderPtr reader, const char *rng) {
  3921. xmlRelaxNGParserCtxtPtr ctxt;
  3922. if (reader == NULL)
  3923. return(-1);
  3924. if (rng == NULL) {
  3925. if (reader->rngValidCtxt != NULL) {
  3926. xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
  3927. reader->rngValidCtxt = NULL;
  3928. }
  3929. if (reader->rngSchemas != NULL) {
  3930. xmlRelaxNGFree(reader->rngSchemas);
  3931. reader->rngSchemas = NULL;
  3932. }
  3933. return(0);
  3934. }
  3935. if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
  3936. return(-1);
  3937. if (reader->rngSchemas != NULL) {
  3938. xmlRelaxNGFree(reader->rngSchemas);
  3939. reader->rngSchemas = NULL;
  3940. }
  3941. if (reader->rngValidCtxt != NULL) {
  3942. xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
  3943. reader->rngValidCtxt = NULL;
  3944. }
  3945. ctxt = xmlRelaxNGNewParserCtxt(rng);
  3946. if (reader->errorFunc != NULL) {
  3947. xmlRelaxNGSetParserErrors(ctxt,
  3948. xmlTextReaderValidityErrorRelay,
  3949. xmlTextReaderValidityWarningRelay,
  3950. reader);
  3951. }
  3952. if (reader->sErrorFunc != NULL) {
  3953. xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt,
  3954. xmlTextReaderValidityStructuredRelay,
  3955. reader);
  3956. }
  3957. reader->rngSchemas = xmlRelaxNGParse(ctxt);
  3958. xmlRelaxNGFreeParserCtxt(ctxt);
  3959. if (reader->rngSchemas == NULL)
  3960. return(-1);
  3961. reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(reader->rngSchemas);
  3962. if (reader->rngValidCtxt == NULL) {
  3963. xmlRelaxNGFree(reader->rngSchemas);
  3964. reader->rngSchemas = NULL;
  3965. return(-1);
  3966. }
  3967. if (reader->errorFunc != NULL) {
  3968. xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
  3969. xmlTextReaderValidityErrorRelay,
  3970. xmlTextReaderValidityWarningRelay,
  3971. reader);
  3972. }
  3973. if (reader->sErrorFunc != NULL) {
  3974. xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt,
  3975. xmlTextReaderValidityStructuredRelay,
  3976. reader);
  3977. }
  3978. reader->rngValidErrors = 0;
  3979. reader->rngFullNode = NULL;
  3980. reader->validate = XML_TEXTREADER_VALIDATE_RNG;
  3981. return(0);
  3982. }
  3983. /**
  3984. * xmlTextReaderSchemaValidateInternal:
  3985. * @reader: the xmlTextReaderPtr used
  3986. * @xsd: the path to a W3C XSD schema or NULL
  3987. * @ctxt: the XML Schema validation context or NULL
  3988. * @options: options (not used yet)
  3989. *
  3990. * Validate the document as it is processed using XML Schema.
  3991. * Activation is only possible before the first Read().
  3992. * If both @xsd and @ctxt are NULL then XML Schema validation is deactivated.
  3993. *
  3994. * Returns 0 in case the schemas validation could be (de)activated and
  3995. * -1 in case of error.
  3996. */
  3997. static int
  3998. xmlTextReaderSchemaValidateInternal(xmlTextReaderPtr reader,
  3999. const char *xsd,
  4000. xmlSchemaValidCtxtPtr ctxt,
  4001. int options ATTRIBUTE_UNUSED)
  4002. {
  4003. if (reader == NULL)
  4004. return(-1);
  4005. if ((xsd != NULL) && (ctxt != NULL))
  4006. return(-1);
  4007. if (((xsd != NULL) || (ctxt != NULL)) &&
  4008. ((reader->mode != XML_TEXTREADER_MODE_INITIAL) ||
  4009. (reader->ctxt == NULL)))
  4010. return(-1);
  4011. /* Cleanup previous validation stuff. */
  4012. if (reader->xsdPlug != NULL) {
  4013. xmlSchemaSAXUnplug(reader->xsdPlug);
  4014. reader->xsdPlug = NULL;
  4015. }
  4016. if (reader->xsdValidCtxt != NULL) {
  4017. if (! reader->xsdPreserveCtxt)
  4018. xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
  4019. reader->xsdValidCtxt = NULL;
  4020. }
  4021. reader->xsdPreserveCtxt = 0;
  4022. if (reader->xsdSchemas != NULL) {
  4023. xmlSchemaFree(reader->xsdSchemas);
  4024. reader->xsdSchemas = NULL;
  4025. }
  4026. if ((xsd == NULL) && (ctxt == NULL)) {
  4027. /* We just want to deactivate the validation, so get out. */
  4028. return(0);
  4029. }
  4030. if (xsd != NULL) {
  4031. xmlSchemaParserCtxtPtr pctxt;
  4032. /* Parse the schema and create validation environment. */
  4033. pctxt = xmlSchemaNewParserCtxt(xsd);
  4034. if (reader->errorFunc != NULL) {
  4035. xmlSchemaSetParserErrors(pctxt,
  4036. xmlTextReaderValidityErrorRelay,
  4037. xmlTextReaderValidityWarningRelay,
  4038. reader);
  4039. }
  4040. reader->xsdSchemas = xmlSchemaParse(pctxt);
  4041. xmlSchemaFreeParserCtxt(pctxt);
  4042. if (reader->xsdSchemas == NULL)
  4043. return(-1);
  4044. reader->xsdValidCtxt = xmlSchemaNewValidCtxt(reader->xsdSchemas);
  4045. if (reader->xsdValidCtxt == NULL) {
  4046. xmlSchemaFree(reader->xsdSchemas);
  4047. reader->xsdSchemas = NULL;
  4048. return(-1);
  4049. }
  4050. reader->xsdPlug = xmlSchemaSAXPlug(reader->xsdValidCtxt,
  4051. &(reader->ctxt->sax),
  4052. &(reader->ctxt->userData));
  4053. if (reader->xsdPlug == NULL) {
  4054. xmlSchemaFree(reader->xsdSchemas);
  4055. reader->xsdSchemas = NULL;
  4056. xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
  4057. reader->xsdValidCtxt = NULL;
  4058. return(-1);
  4059. }
  4060. } else {
  4061. /* Use the given validation context. */
  4062. reader->xsdValidCtxt = ctxt;
  4063. reader->xsdPreserveCtxt = 1;
  4064. reader->xsdPlug = xmlSchemaSAXPlug(reader->xsdValidCtxt,
  4065. &(reader->ctxt->sax),
  4066. &(reader->ctxt->userData));
  4067. if (reader->xsdPlug == NULL) {
  4068. reader->xsdValidCtxt = NULL;
  4069. reader->xsdPreserveCtxt = 0;
  4070. return(-1);
  4071. }
  4072. }
  4073. /*
  4074. * Redirect the validation context's error channels to use
  4075. * the reader channels.
  4076. * TODO: In case the user provides the validation context we
  4077. * could make this redirection optional.
  4078. */
  4079. if (reader->errorFunc != NULL) {
  4080. xmlSchemaSetValidErrors(reader->xsdValidCtxt,
  4081. xmlTextReaderValidityErrorRelay,
  4082. xmlTextReaderValidityWarningRelay,
  4083. reader);
  4084. }
  4085. if (reader->sErrorFunc != NULL) {
  4086. xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt,
  4087. xmlTextReaderValidityStructuredRelay,
  4088. reader);
  4089. }
  4090. reader->xsdValidErrors = 0;
  4091. reader->validate = XML_TEXTREADER_VALIDATE_XSD;
  4092. return(0);
  4093. }
  4094. /**
  4095. * xmlTextReaderSchemaValidateCtxt:
  4096. * @reader: the xmlTextReaderPtr used
  4097. * @ctxt: the XML Schema validation context or NULL
  4098. * @options: options (not used yet)
  4099. *
  4100. * Use W3C XSD schema context to validate the document as it is processed.
  4101. * Activation is only possible before the first Read().
  4102. * If @ctxt is NULL, then XML Schema validation is deactivated.
  4103. *
  4104. * Returns 0 in case the schemas validation could be (de)activated and
  4105. * -1 in case of error.
  4106. */
  4107. int
  4108. xmlTextReaderSchemaValidateCtxt(xmlTextReaderPtr reader,
  4109. xmlSchemaValidCtxtPtr ctxt,
  4110. int options)
  4111. {
  4112. return(xmlTextReaderSchemaValidateInternal(reader, NULL, ctxt, options));
  4113. }
  4114. /**
  4115. * xmlTextReaderSchemaValidate:
  4116. * @reader: the xmlTextReaderPtr used
  4117. * @xsd: the path to a W3C XSD schema or NULL
  4118. *
  4119. * Use W3C XSD schema to validate the document as it is processed.
  4120. * Activation is only possible before the first Read().
  4121. * If @xsd is NULL, then XML Schema validation is deactivated.
  4122. *
  4123. * Returns 0 in case the schemas validation could be (de)activated and
  4124. * -1 in case of error.
  4125. */
  4126. int
  4127. xmlTextReaderSchemaValidate(xmlTextReaderPtr reader, const char *xsd)
  4128. {
  4129. return(xmlTextReaderSchemaValidateInternal(reader, xsd, NULL, 0));
  4130. }
  4131. #endif
  4132. /**
  4133. * xmlTextReaderIsNamespaceDecl:
  4134. * @reader: the xmlTextReaderPtr used
  4135. *
  4136. * Determine whether the current node is a namespace declaration
  4137. * rather than a regular attribute.
  4138. *
  4139. * Returns 1 if the current node is a namespace declaration, 0 if it
  4140. * is a regular attribute or other type of node, or -1 in case of
  4141. * error.
  4142. */
  4143. int
  4144. xmlTextReaderIsNamespaceDecl(xmlTextReaderPtr reader) {
  4145. xmlNodePtr node;
  4146. if (reader == NULL)
  4147. return(-1);
  4148. if (reader->node == NULL)
  4149. return(-1);
  4150. if (reader->curnode != NULL)
  4151. node = reader->curnode;
  4152. else
  4153. node = reader->node;
  4154. if (XML_NAMESPACE_DECL == node->type)
  4155. return(1);
  4156. else
  4157. return(0);
  4158. }
  4159. /**
  4160. * xmlTextReaderConstXmlVersion:
  4161. * @reader: the xmlTextReaderPtr used
  4162. *
  4163. * Determine the XML version of the document being read.
  4164. *
  4165. * Returns a string containing the XML version of the document or NULL
  4166. * in case of error. The string is deallocated with the reader.
  4167. */
  4168. const xmlChar *
  4169. xmlTextReaderConstXmlVersion(xmlTextReaderPtr reader) {
  4170. xmlDocPtr doc = NULL;
  4171. if (reader == NULL)
  4172. return(NULL);
  4173. if (reader->doc != NULL)
  4174. doc = reader->doc;
  4175. else if (reader->ctxt != NULL)
  4176. doc = reader->ctxt->myDoc;
  4177. if (doc == NULL)
  4178. return(NULL);
  4179. if (doc->version == NULL)
  4180. return(NULL);
  4181. else
  4182. return(CONSTSTR(doc->version));
  4183. }
  4184. /**
  4185. * xmlTextReaderStandalone:
  4186. * @reader: the xmlTextReaderPtr used
  4187. *
  4188. * Determine the standalone status of the document being read.
  4189. *
  4190. * Returns 1 if the document was declared to be standalone, 0 if it
  4191. * was declared to be not standalone, or -1 if the document did not
  4192. * specify its standalone status or in case of error.
  4193. */
  4194. int
  4195. xmlTextReaderStandalone(xmlTextReaderPtr reader) {
  4196. xmlDocPtr doc = NULL;
  4197. if (reader == NULL)
  4198. return(-1);
  4199. if (reader->doc != NULL)
  4200. doc = reader->doc;
  4201. else if (reader->ctxt != NULL)
  4202. doc = reader->ctxt->myDoc;
  4203. if (doc == NULL)
  4204. return(-1);
  4205. return(doc->standalone);
  4206. }
  4207. /************************************************************************
  4208. * *
  4209. * Error Handling Extensions *
  4210. * *
  4211. ************************************************************************/
  4212. /* helper to build a xmlMalloc'ed string from a format and va_list */
  4213. static char *
  4214. xmlTextReaderBuildMessage(const char *msg, va_list ap) {
  4215. int size = 0;
  4216. int chars;
  4217. char *larger;
  4218. char *str = NULL;
  4219. va_list aq;
  4220. while (1) {
  4221. VA_COPY(aq, ap);
  4222. chars = vsnprintf(str, size, msg, aq);
  4223. va_end(aq);
  4224. if (chars < 0) {
  4225. xmlGenericError(xmlGenericErrorContext, "vsnprintf failed !\n");
  4226. if (str)
  4227. xmlFree(str);
  4228. return NULL;
  4229. }
  4230. if ((chars < size) || (size == MAX_ERR_MSG_SIZE))
  4231. break;
  4232. if (chars < MAX_ERR_MSG_SIZE)
  4233. size = chars + 1;
  4234. else
  4235. size = MAX_ERR_MSG_SIZE;
  4236. if ((larger = (char *) xmlRealloc(str, size)) == NULL) {
  4237. xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
  4238. if (str)
  4239. xmlFree(str);
  4240. return NULL;
  4241. }
  4242. str = larger;
  4243. }
  4244. return str;
  4245. }
  4246. /**
  4247. * xmlTextReaderLocatorLineNumber:
  4248. * @locator: the xmlTextReaderLocatorPtr used
  4249. *
  4250. * Obtain the line number for the given locator.
  4251. *
  4252. * Returns the line number or -1 in case of error.
  4253. */
  4254. int
  4255. xmlTextReaderLocatorLineNumber(xmlTextReaderLocatorPtr locator) {
  4256. /* we know that locator is a xmlParserCtxtPtr */
  4257. xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
  4258. int ret = -1;
  4259. if (locator == NULL)
  4260. return(-1);
  4261. if (ctx->node != NULL) {
  4262. ret = xmlGetLineNo(ctx->node);
  4263. }
  4264. else {
  4265. /* inspired from error.c */
  4266. xmlParserInputPtr input;
  4267. input = ctx->input;
  4268. if ((input->filename == NULL) && (ctx->inputNr > 1))
  4269. input = ctx->inputTab[ctx->inputNr - 2];
  4270. if (input != NULL) {
  4271. ret = input->line;
  4272. }
  4273. else {
  4274. ret = -1;
  4275. }
  4276. }
  4277. return ret;
  4278. }
  4279. /**
  4280. * xmlTextReaderLocatorBaseURI:
  4281. * @locator: the xmlTextReaderLocatorPtr used
  4282. *
  4283. * Obtain the base URI for the given locator.
  4284. *
  4285. * Returns the base URI or NULL in case of error,
  4286. * if non NULL it need to be freed by the caller.
  4287. */
  4288. xmlChar *
  4289. xmlTextReaderLocatorBaseURI(xmlTextReaderLocatorPtr locator) {
  4290. /* we know that locator is a xmlParserCtxtPtr */
  4291. xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
  4292. xmlChar *ret = NULL;
  4293. if (locator == NULL)
  4294. return(NULL);
  4295. if (ctx->node != NULL) {
  4296. ret = xmlNodeGetBase(NULL,ctx->node);
  4297. }
  4298. else {
  4299. /* inspired from error.c */
  4300. xmlParserInputPtr input;
  4301. input = ctx->input;
  4302. if ((input->filename == NULL) && (ctx->inputNr > 1))
  4303. input = ctx->inputTab[ctx->inputNr - 2];
  4304. if (input != NULL) {
  4305. ret = xmlStrdup(BAD_CAST input->filename);
  4306. }
  4307. else {
  4308. ret = NULL;
  4309. }
  4310. }
  4311. return ret;
  4312. }
  4313. static void
  4314. xmlTextReaderGenericError(void *ctxt, xmlParserSeverities severity,
  4315. char *str)
  4316. {
  4317. xmlParserCtxtPtr ctx = (xmlParserCtxtPtr) ctxt;
  4318. xmlTextReaderPtr reader = (xmlTextReaderPtr) ctx->_private;
  4319. if (str != NULL) {
  4320. if (reader->errorFunc)
  4321. reader->errorFunc(reader->errorFuncArg, str, severity,
  4322. (xmlTextReaderLocatorPtr) ctx);
  4323. xmlFree(str);
  4324. }
  4325. }
  4326. static void
  4327. xmlTextReaderStructuredError(void *ctxt, xmlErrorPtr error)
  4328. {
  4329. xmlParserCtxtPtr ctx = (xmlParserCtxtPtr) ctxt;
  4330. xmlTextReaderPtr reader = (xmlTextReaderPtr) ctx->_private;
  4331. if (error && reader->sErrorFunc) {
  4332. reader->sErrorFunc(reader->errorFuncArg, (xmlErrorPtr) error);
  4333. }
  4334. }
  4335. static void XMLCDECL
  4336. xmlTextReaderError(void *ctxt, const char *msg, ...)
  4337. {
  4338. va_list ap;
  4339. va_start(ap, msg);
  4340. xmlTextReaderGenericError(ctxt,
  4341. XML_PARSER_SEVERITY_ERROR,
  4342. xmlTextReaderBuildMessage(msg, ap));
  4343. va_end(ap);
  4344. }
  4345. static void XMLCDECL
  4346. xmlTextReaderWarning(void *ctxt, const char *msg, ...)
  4347. {
  4348. va_list ap;
  4349. va_start(ap, msg);
  4350. xmlTextReaderGenericError(ctxt,
  4351. XML_PARSER_SEVERITY_WARNING,
  4352. xmlTextReaderBuildMessage(msg, ap));
  4353. va_end(ap);
  4354. }
  4355. static void XMLCDECL
  4356. xmlTextReaderValidityError(void *ctxt, const char *msg, ...)
  4357. {
  4358. va_list ap;
  4359. int len = xmlStrlen((const xmlChar *) msg);
  4360. if ((len > 1) && (msg[len - 2] != ':')) {
  4361. /*
  4362. * some callbacks only report locator information:
  4363. * skip them (mimicking behaviour in error.c)
  4364. */
  4365. va_start(ap, msg);
  4366. xmlTextReaderGenericError(ctxt,
  4367. XML_PARSER_SEVERITY_VALIDITY_ERROR,
  4368. xmlTextReaderBuildMessage(msg, ap));
  4369. va_end(ap);
  4370. }
  4371. }
  4372. static void XMLCDECL
  4373. xmlTextReaderValidityWarning(void *ctxt, const char *msg, ...)
  4374. {
  4375. va_list ap;
  4376. int len = xmlStrlen((const xmlChar *) msg);
  4377. if ((len != 0) && (msg[len - 1] != ':')) {
  4378. /*
  4379. * some callbacks only report locator information:
  4380. * skip them (mimicking behaviour in error.c)
  4381. */
  4382. va_start(ap, msg);
  4383. xmlTextReaderGenericError(ctxt,
  4384. XML_PARSER_SEVERITY_VALIDITY_WARNING,
  4385. xmlTextReaderBuildMessage(msg, ap));
  4386. va_end(ap);
  4387. }
  4388. }
  4389. /**
  4390. * xmlTextReaderSetErrorHandler:
  4391. * @reader: the xmlTextReaderPtr used
  4392. * @f: the callback function to call on error and warnings
  4393. * @arg: a user argument to pass to the callback function
  4394. *
  4395. * Register a callback function that will be called on error and warnings.
  4396. *
  4397. * If @f is NULL, the default error and warning handlers are restored.
  4398. */
  4399. void
  4400. xmlTextReaderSetErrorHandler(xmlTextReaderPtr reader,
  4401. xmlTextReaderErrorFunc f, void *arg)
  4402. {
  4403. if (f != NULL) {
  4404. reader->ctxt->sax->error = xmlTextReaderError;
  4405. reader->ctxt->sax->serror = NULL;
  4406. reader->ctxt->vctxt.error = xmlTextReaderValidityError;
  4407. reader->ctxt->sax->warning = xmlTextReaderWarning;
  4408. reader->ctxt->vctxt.warning = xmlTextReaderValidityWarning;
  4409. reader->errorFunc = f;
  4410. reader->sErrorFunc = NULL;
  4411. reader->errorFuncArg = arg;
  4412. #ifdef LIBXML_SCHEMAS_ENABLED
  4413. if (reader->rngValidCtxt) {
  4414. xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
  4415. xmlTextReaderValidityErrorRelay,
  4416. xmlTextReaderValidityWarningRelay,
  4417. reader);
  4418. xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt, NULL,
  4419. reader);
  4420. }
  4421. if (reader->xsdValidCtxt) {
  4422. xmlSchemaSetValidErrors(reader->xsdValidCtxt,
  4423. xmlTextReaderValidityErrorRelay,
  4424. xmlTextReaderValidityWarningRelay,
  4425. reader);
  4426. xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt, NULL,
  4427. reader);
  4428. }
  4429. #endif
  4430. } else {
  4431. /* restore defaults */
  4432. reader->ctxt->sax->error = xmlParserError;
  4433. reader->ctxt->vctxt.error = xmlParserValidityError;
  4434. reader->ctxt->sax->warning = xmlParserWarning;
  4435. reader->ctxt->vctxt.warning = xmlParserValidityWarning;
  4436. reader->errorFunc = NULL;
  4437. reader->sErrorFunc = NULL;
  4438. reader->errorFuncArg = NULL;
  4439. #ifdef LIBXML_SCHEMAS_ENABLED
  4440. if (reader->rngValidCtxt) {
  4441. xmlRelaxNGSetValidErrors(reader->rngValidCtxt, NULL, NULL,
  4442. reader);
  4443. xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt, NULL,
  4444. reader);
  4445. }
  4446. if (reader->xsdValidCtxt) {
  4447. xmlSchemaSetValidErrors(reader->xsdValidCtxt, NULL, NULL,
  4448. reader);
  4449. xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt, NULL,
  4450. reader);
  4451. }
  4452. #endif
  4453. }
  4454. }
  4455. /**
  4456. * xmlTextReaderSetStructuredErrorHandler:
  4457. * @reader: the xmlTextReaderPtr used
  4458. * @f: the callback function to call on error and warnings
  4459. * @arg: a user argument to pass to the callback function
  4460. *
  4461. * Register a callback function that will be called on error and warnings.
  4462. *
  4463. * If @f is NULL, the default error and warning handlers are restored.
  4464. */
  4465. void
  4466. xmlTextReaderSetStructuredErrorHandler(xmlTextReaderPtr reader,
  4467. xmlStructuredErrorFunc f, void *arg)
  4468. {
  4469. if (f != NULL) {
  4470. reader->ctxt->sax->error = NULL;
  4471. reader->ctxt->sax->serror = xmlTextReaderStructuredError;
  4472. reader->ctxt->vctxt.error = xmlTextReaderValidityError;
  4473. reader->ctxt->sax->warning = xmlTextReaderWarning;
  4474. reader->ctxt->vctxt.warning = xmlTextReaderValidityWarning;
  4475. reader->sErrorFunc = f;
  4476. reader->errorFunc = NULL;
  4477. reader->errorFuncArg = arg;
  4478. #ifdef LIBXML_SCHEMAS_ENABLED
  4479. if (reader->rngValidCtxt) {
  4480. xmlRelaxNGSetValidErrors(reader->rngValidCtxt, NULL, NULL,
  4481. reader);
  4482. xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt,
  4483. xmlTextReaderValidityStructuredRelay,
  4484. reader);
  4485. }
  4486. if (reader->xsdValidCtxt) {
  4487. xmlSchemaSetValidErrors(reader->xsdValidCtxt, NULL, NULL,
  4488. reader);
  4489. xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt,
  4490. xmlTextReaderValidityStructuredRelay,
  4491. reader);
  4492. }
  4493. #endif
  4494. } else {
  4495. /* restore defaults */
  4496. reader->ctxt->sax->error = xmlParserError;
  4497. reader->ctxt->sax->serror = NULL;
  4498. reader->ctxt->vctxt.error = xmlParserValidityError;
  4499. reader->ctxt->sax->warning = xmlParserWarning;
  4500. reader->ctxt->vctxt.warning = xmlParserValidityWarning;
  4501. reader->errorFunc = NULL;
  4502. reader->sErrorFunc = NULL;
  4503. reader->errorFuncArg = NULL;
  4504. #ifdef LIBXML_SCHEMAS_ENABLED
  4505. if (reader->rngValidCtxt) {
  4506. xmlRelaxNGSetValidErrors(reader->rngValidCtxt, NULL, NULL,
  4507. reader);
  4508. xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt, NULL,
  4509. reader);
  4510. }
  4511. if (reader->xsdValidCtxt) {
  4512. xmlSchemaSetValidErrors(reader->xsdValidCtxt, NULL, NULL,
  4513. reader);
  4514. xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt, NULL,
  4515. reader);
  4516. }
  4517. #endif
  4518. }
  4519. }
  4520. /**
  4521. * xmlTextReaderIsValid:
  4522. * @reader: the xmlTextReaderPtr used
  4523. *
  4524. * Retrieve the validity status from the parser context
  4525. *
  4526. * Returns the flag value 1 if valid, 0 if no, and -1 in case of error
  4527. */
  4528. int
  4529. xmlTextReaderIsValid(xmlTextReaderPtr reader)
  4530. {
  4531. if (reader == NULL)
  4532. return (-1);
  4533. #ifdef LIBXML_SCHEMAS_ENABLED
  4534. if (reader->validate == XML_TEXTREADER_VALIDATE_RNG)
  4535. return (reader->rngValidErrors == 0);
  4536. if (reader->validate == XML_TEXTREADER_VALIDATE_XSD)
  4537. return (reader->xsdValidErrors == 0);
  4538. #endif
  4539. if ((reader->ctxt != NULL) && (reader->ctxt->validate == 1))
  4540. return (reader->ctxt->valid);
  4541. return (0);
  4542. }
  4543. /**
  4544. * xmlTextReaderGetErrorHandler:
  4545. * @reader: the xmlTextReaderPtr used
  4546. * @f: the callback function or NULL is no callback has been registered
  4547. * @arg: a user argument
  4548. *
  4549. * Retrieve the error callback function and user argument.
  4550. */
  4551. void
  4552. xmlTextReaderGetErrorHandler(xmlTextReaderPtr reader,
  4553. xmlTextReaderErrorFunc * f, void **arg)
  4554. {
  4555. if (f != NULL)
  4556. *f = reader->errorFunc;
  4557. if (arg != NULL)
  4558. *arg = reader->errorFuncArg;
  4559. }
  4560. /************************************************************************
  4561. * *
  4562. * New set (2.6.0) of simpler and more flexible APIs *
  4563. * *
  4564. ************************************************************************/
  4565. /**
  4566. * xmlTextReaderSetup:
  4567. * @reader: an XML reader
  4568. * @input: xmlParserInputBufferPtr used to feed the reader, will
  4569. * be destroyed with it.
  4570. * @URL: the base URL to use for the document
  4571. * @encoding: the document encoding, or NULL
  4572. * @options: a combination of xmlParserOption
  4573. *
  4574. * Setup an XML reader with new options
  4575. *
  4576. * Returns 0 in case of success and -1 in case of error.
  4577. */
  4578. int
  4579. xmlTextReaderSetup(xmlTextReaderPtr reader,
  4580. xmlParserInputBufferPtr input, const char *URL,
  4581. const char *encoding, int options)
  4582. {
  4583. if (reader == NULL) {
  4584. if (input != NULL)
  4585. xmlFreeParserInputBuffer(input);
  4586. return (-1);
  4587. }
  4588. /*
  4589. * we force the generation of compact text nodes on the reader
  4590. * since usr applications should never modify the tree
  4591. */
  4592. options |= XML_PARSE_COMPACT;
  4593. reader->doc = NULL;
  4594. reader->entNr = 0;
  4595. reader->parserFlags = options;
  4596. reader->validate = XML_TEXTREADER_NOT_VALIDATE;
  4597. if ((input != NULL) && (reader->input != NULL) &&
  4598. (reader->allocs & XML_TEXTREADER_INPUT)) {
  4599. xmlFreeParserInputBuffer(reader->input);
  4600. reader->input = NULL;
  4601. reader->allocs -= XML_TEXTREADER_INPUT;
  4602. }
  4603. if (input != NULL) {
  4604. reader->input = input;
  4605. reader->allocs |= XML_TEXTREADER_INPUT;
  4606. }
  4607. if (reader->buffer == NULL)
  4608. reader->buffer = xmlBufferCreateSize(100);
  4609. if (reader->buffer == NULL) {
  4610. xmlGenericError(xmlGenericErrorContext,
  4611. "xmlTextReaderSetup : malloc failed\n");
  4612. return (-1);
  4613. }
  4614. if (reader->sax == NULL)
  4615. reader->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
  4616. if (reader->sax == NULL) {
  4617. xmlGenericError(xmlGenericErrorContext,
  4618. "xmlTextReaderSetup : malloc failed\n");
  4619. return (-1);
  4620. }
  4621. xmlSAXVersion(reader->sax, 2);
  4622. reader->startElement = reader->sax->startElement;
  4623. reader->sax->startElement = xmlTextReaderStartElement;
  4624. reader->endElement = reader->sax->endElement;
  4625. reader->sax->endElement = xmlTextReaderEndElement;
  4626. #ifdef LIBXML_SAX1_ENABLED
  4627. if (reader->sax->initialized == XML_SAX2_MAGIC) {
  4628. #endif /* LIBXML_SAX1_ENABLED */
  4629. reader->startElementNs = reader->sax->startElementNs;
  4630. reader->sax->startElementNs = xmlTextReaderStartElementNs;
  4631. reader->endElementNs = reader->sax->endElementNs;
  4632. reader->sax->endElementNs = xmlTextReaderEndElementNs;
  4633. #ifdef LIBXML_SAX1_ENABLED
  4634. } else {
  4635. reader->startElementNs = NULL;
  4636. reader->endElementNs = NULL;
  4637. }
  4638. #endif /* LIBXML_SAX1_ENABLED */
  4639. reader->characters = reader->sax->characters;
  4640. reader->sax->characters = xmlTextReaderCharacters;
  4641. reader->sax->ignorableWhitespace = xmlTextReaderCharacters;
  4642. reader->cdataBlock = reader->sax->cdataBlock;
  4643. reader->sax->cdataBlock = xmlTextReaderCDataBlock;
  4644. reader->mode = XML_TEXTREADER_MODE_INITIAL;
  4645. reader->node = NULL;
  4646. reader->curnode = NULL;
  4647. if (input != NULL) {
  4648. if (reader->input->buffer->use < 4) {
  4649. xmlParserInputBufferRead(input, 4);
  4650. }
  4651. if (reader->ctxt == NULL) {
  4652. if (reader->input->buffer->use >= 4) {
  4653. reader->ctxt = xmlCreatePushParserCtxt(reader->sax, NULL,
  4654. (const char *) reader->input->buffer->content, 4, URL);
  4655. reader->base = 0;
  4656. reader->cur = 4;
  4657. } else {
  4658. reader->ctxt =
  4659. xmlCreatePushParserCtxt(reader->sax, NULL, NULL, 0, URL);
  4660. reader->base = 0;
  4661. reader->cur = 0;
  4662. }
  4663. } else {
  4664. xmlParserInputPtr inputStream;
  4665. xmlParserInputBufferPtr buf;
  4666. xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
  4667. xmlCtxtReset(reader->ctxt);
  4668. buf = xmlAllocParserInputBuffer(enc);
  4669. if (buf == NULL) return(-1);
  4670. inputStream = xmlNewInputStream(reader->ctxt);
  4671. if (inputStream == NULL) {
  4672. xmlFreeParserInputBuffer(buf);
  4673. return(-1);
  4674. }
  4675. if (URL == NULL)
  4676. inputStream->filename = NULL;
  4677. else
  4678. inputStream->filename = (char *)
  4679. xmlCanonicPath((const xmlChar *) URL);
  4680. inputStream->buf = buf;
  4681. inputStream->base = inputStream->buf->buffer->content;
  4682. inputStream->cur = inputStream->buf->buffer->content;
  4683. inputStream->end =
  4684. &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
  4685. inputPush(reader->ctxt, inputStream);
  4686. reader->cur = 0;
  4687. }
  4688. if (reader->ctxt == NULL) {
  4689. xmlGenericError(xmlGenericErrorContext,
  4690. "xmlTextReaderSetup : malloc failed\n");
  4691. return (-1);
  4692. }
  4693. }
  4694. if (reader->dict != NULL) {
  4695. if (reader->ctxt->dict != NULL) {
  4696. if (reader->dict != reader->ctxt->dict) {
  4697. xmlDictFree(reader->dict);
  4698. reader->dict = reader->ctxt->dict;
  4699. }
  4700. } else {
  4701. reader->ctxt->dict = reader->dict;
  4702. }
  4703. } else {
  4704. if (reader->ctxt->dict == NULL)
  4705. reader->ctxt->dict = xmlDictCreate();
  4706. reader->dict = reader->ctxt->dict;
  4707. }
  4708. reader->ctxt->_private = reader;
  4709. reader->ctxt->linenumbers = 1;
  4710. reader->ctxt->dictNames = 1;
  4711. /*
  4712. * use the parser dictionnary to allocate all elements and attributes names
  4713. */
  4714. reader->ctxt->docdict = 1;
  4715. reader->ctxt->parseMode = XML_PARSE_READER;
  4716. #ifdef LIBXML_XINCLUDE_ENABLED
  4717. if (reader->xincctxt != NULL) {
  4718. xmlXIncludeFreeContext(reader->xincctxt);
  4719. reader->xincctxt = NULL;
  4720. }
  4721. if (options & XML_PARSE_XINCLUDE) {
  4722. reader->xinclude = 1;
  4723. reader->xinclude_name = xmlDictLookup(reader->dict, XINCLUDE_NODE, -1);
  4724. options -= XML_PARSE_XINCLUDE;
  4725. } else
  4726. reader->xinclude = 0;
  4727. reader->in_xinclude = 0;
  4728. #endif
  4729. #ifdef LIBXML_PATTERN_ENABLED
  4730. if (reader->patternTab == NULL) {
  4731. reader->patternNr = 0;
  4732. reader->patternMax = 0;
  4733. }
  4734. while (reader->patternNr > 0) {
  4735. reader->patternNr--;
  4736. if (reader->patternTab[reader->patternNr] != NULL) {
  4737. xmlFreePattern(reader->patternTab[reader->patternNr]);
  4738. reader->patternTab[reader->patternNr] = NULL;
  4739. }
  4740. }
  4741. #endif
  4742. if (options & XML_PARSE_DTDVALID)
  4743. reader->validate = XML_TEXTREADER_VALIDATE_DTD;
  4744. xmlCtxtUseOptions(reader->ctxt, options);
  4745. if (encoding != NULL) {
  4746. xmlCharEncodingHandlerPtr hdlr;
  4747. hdlr = xmlFindCharEncodingHandler(encoding);
  4748. if (hdlr != NULL)
  4749. xmlSwitchToEncoding(reader->ctxt, hdlr);
  4750. }
  4751. if ((URL != NULL) && (reader->ctxt->input != NULL) &&
  4752. (reader->ctxt->input->filename == NULL))
  4753. reader->ctxt->input->filename = (char *)
  4754. xmlStrdup((const xmlChar *) URL);
  4755. reader->doc = NULL;
  4756. return (0);
  4757. }
  4758. /**
  4759. * xmlTextReaderByteConsumed:
  4760. * @reader: an XML reader
  4761. *
  4762. * This function provides the current index of the parser used
  4763. * by the reader, relative to the start of the current entity.
  4764. * This function actually just wraps a call to xmlBytesConsumed()
  4765. * for the parser context associated with the reader.
  4766. * See xmlBytesConsumed() for more information.
  4767. *
  4768. * Returns the index in bytes from the beginning of the entity or -1
  4769. * in case the index could not be computed.
  4770. */
  4771. long
  4772. xmlTextReaderByteConsumed(xmlTextReaderPtr reader) {
  4773. if ((reader == NULL) || (reader->ctxt == NULL))
  4774. return(-1);
  4775. return(xmlByteConsumed(reader->ctxt));
  4776. }
  4777. /**
  4778. * xmlReaderWalker:
  4779. * @doc: a preparsed document
  4780. *
  4781. * Create an xmltextReader for a preparsed document.
  4782. *
  4783. * Returns the new reader or NULL in case of error.
  4784. */
  4785. xmlTextReaderPtr
  4786. xmlReaderWalker(xmlDocPtr doc)
  4787. {
  4788. xmlTextReaderPtr ret;
  4789. if (doc == NULL)
  4790. return(NULL);
  4791. ret = xmlMalloc(sizeof(xmlTextReader));
  4792. if (ret == NULL) {
  4793. xmlGenericError(xmlGenericErrorContext,
  4794. "xmlNewTextReader : malloc failed\n");
  4795. return(NULL);
  4796. }
  4797. memset(ret, 0, sizeof(xmlTextReader));
  4798. ret->entNr = 0;
  4799. ret->input = NULL;
  4800. ret->mode = XML_TEXTREADER_MODE_INITIAL;
  4801. ret->node = NULL;
  4802. ret->curnode = NULL;
  4803. ret->base = 0;
  4804. ret->cur = 0;
  4805. ret->allocs = XML_TEXTREADER_CTXT;
  4806. ret->doc = doc;
  4807. ret->state = XML_TEXTREADER_START;
  4808. ret->dict = xmlDictCreate();
  4809. return(ret);
  4810. }
  4811. /**
  4812. * xmlReaderForDoc:
  4813. * @cur: a pointer to a zero terminated string
  4814. * @URL: the base URL to use for the document
  4815. * @encoding: the document encoding, or NULL
  4816. * @options: a combination of xmlParserOption
  4817. *
  4818. * Create an xmltextReader for an XML in-memory document.
  4819. * The parsing flags @options are a combination of xmlParserOption.
  4820. *
  4821. * Returns the new reader or NULL in case of error.
  4822. */
  4823. xmlTextReaderPtr
  4824. xmlReaderForDoc(const xmlChar * cur, const char *URL, const char *encoding,
  4825. int options)
  4826. {
  4827. int len;
  4828. if (cur == NULL)
  4829. return (NULL);
  4830. len = xmlStrlen(cur);
  4831. return (xmlReaderForMemory
  4832. ((const char *) cur, len, URL, encoding, options));
  4833. }
  4834. /**
  4835. * xmlReaderForFile:
  4836. * @filename: a file or URL
  4837. * @encoding: the document encoding, or NULL
  4838. * @options: a combination of xmlParserOption
  4839. *
  4840. * parse an XML file from the filesystem or the network.
  4841. * The parsing flags @options are a combination of xmlParserOption.
  4842. *
  4843. * Returns the new reader or NULL in case of error.
  4844. */
  4845. xmlTextReaderPtr
  4846. xmlReaderForFile(const char *filename, const char *encoding, int options)
  4847. {
  4848. xmlTextReaderPtr reader;
  4849. reader = xmlNewTextReaderFilename(filename);
  4850. if (reader == NULL)
  4851. return (NULL);
  4852. xmlTextReaderSetup(reader, NULL, NULL, encoding, options);
  4853. return (reader);
  4854. }
  4855. /**
  4856. * xmlReaderForMemory:
  4857. * @buffer: a pointer to a char array
  4858. * @size: the size of the array
  4859. * @URL: the base URL to use for the document
  4860. * @encoding: the document encoding, or NULL
  4861. * @options: a combination of xmlParserOption
  4862. *
  4863. * Create an xmltextReader for an XML in-memory document.
  4864. * The parsing flags @options are a combination of xmlParserOption.
  4865. *
  4866. * Returns the new reader or NULL in case of error.
  4867. */
  4868. xmlTextReaderPtr
  4869. xmlReaderForMemory(const char *buffer, int size, const char *URL,
  4870. const char *encoding, int options)
  4871. {
  4872. xmlTextReaderPtr reader;
  4873. xmlParserInputBufferPtr buf;
  4874. buf = xmlParserInputBufferCreateStatic(buffer, size,
  4875. XML_CHAR_ENCODING_NONE);
  4876. if (buf == NULL) {
  4877. return (NULL);
  4878. }
  4879. reader = xmlNewTextReader(buf, URL);
  4880. if (reader == NULL) {
  4881. xmlFreeParserInputBuffer(buf);
  4882. return (NULL);
  4883. }
  4884. reader->allocs |= XML_TEXTREADER_INPUT;
  4885. xmlTextReaderSetup(reader, NULL, URL, encoding, options);
  4886. return (reader);
  4887. }
  4888. /**
  4889. * xmlReaderForFd:
  4890. * @fd: an open file descriptor
  4891. * @URL: the base URL to use for the document
  4892. * @encoding: the document encoding, or NULL
  4893. * @options: a combination of xmlParserOption
  4894. *
  4895. * Create an xmltextReader for an XML from a file descriptor.
  4896. * The parsing flags @options are a combination of xmlParserOption.
  4897. * NOTE that the file descriptor will not be closed when the
  4898. * reader is closed or reset.
  4899. *
  4900. * Returns the new reader or NULL in case of error.
  4901. */
  4902. xmlTextReaderPtr
  4903. xmlReaderForFd(int fd, const char *URL, const char *encoding, int options)
  4904. {
  4905. xmlTextReaderPtr reader;
  4906. xmlParserInputBufferPtr input;
  4907. if (fd < 0)
  4908. return (NULL);
  4909. input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
  4910. if (input == NULL)
  4911. return (NULL);
  4912. input->closecallback = NULL;
  4913. reader = xmlNewTextReader(input, URL);
  4914. if (reader == NULL) {
  4915. xmlFreeParserInputBuffer(input);
  4916. return (NULL);
  4917. }
  4918. reader->allocs |= XML_TEXTREADER_INPUT;
  4919. xmlTextReaderSetup(reader, NULL, URL, encoding, options);
  4920. return (reader);
  4921. }
  4922. /**
  4923. * xmlReaderForIO:
  4924. * @ioread: an I/O read function
  4925. * @ioclose: an I/O close function
  4926. * @ioctx: an I/O handler
  4927. * @URL: the base URL to use for the document
  4928. * @encoding: the document encoding, or NULL
  4929. * @options: a combination of xmlParserOption
  4930. *
  4931. * Create an xmltextReader for an XML document from I/O functions and source.
  4932. * The parsing flags @options are a combination of xmlParserOption.
  4933. *
  4934. * Returns the new reader or NULL in case of error.
  4935. */
  4936. xmlTextReaderPtr
  4937. xmlReaderForIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
  4938. void *ioctx, const char *URL, const char *encoding,
  4939. int options)
  4940. {
  4941. xmlTextReaderPtr reader;
  4942. xmlParserInputBufferPtr input;
  4943. if (ioread == NULL)
  4944. return (NULL);
  4945. input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
  4946. XML_CHAR_ENCODING_NONE);
  4947. if (input == NULL)
  4948. return (NULL);
  4949. reader = xmlNewTextReader(input, URL);
  4950. if (reader == NULL) {
  4951. xmlFreeParserInputBuffer(input);
  4952. return (NULL);
  4953. }
  4954. reader->allocs |= XML_TEXTREADER_INPUT;
  4955. xmlTextReaderSetup(reader, NULL, URL, encoding, options);
  4956. return (reader);
  4957. }
  4958. /**
  4959. * xmlReaderNewWalker:
  4960. * @reader: an XML reader
  4961. * @doc: a preparsed document
  4962. *
  4963. * Setup an xmltextReader to parse a preparsed XML document.
  4964. * This reuses the existing @reader xmlTextReader.
  4965. *
  4966. * Returns 0 in case of success and -1 in case of error
  4967. */
  4968. int
  4969. xmlReaderNewWalker(xmlTextReaderPtr reader, xmlDocPtr doc)
  4970. {
  4971. if (doc == NULL)
  4972. return (-1);
  4973. if (reader == NULL)
  4974. return (-1);
  4975. if (reader->input != NULL) {
  4976. xmlFreeParserInputBuffer(reader->input);
  4977. }
  4978. if (reader->ctxt != NULL) {
  4979. xmlCtxtReset(reader->ctxt);
  4980. }
  4981. reader->entNr = 0;
  4982. reader->input = NULL;
  4983. reader->mode = XML_TEXTREADER_MODE_INITIAL;
  4984. reader->node = NULL;
  4985. reader->curnode = NULL;
  4986. reader->base = 0;
  4987. reader->cur = 0;
  4988. reader->allocs = XML_TEXTREADER_CTXT;
  4989. reader->doc = doc;
  4990. reader->state = XML_TEXTREADER_START;
  4991. if (reader->dict == NULL) {
  4992. if ((reader->ctxt != NULL) && (reader->ctxt->dict != NULL))
  4993. reader->dict = reader->ctxt->dict;
  4994. else
  4995. reader->dict = xmlDictCreate();
  4996. }
  4997. return(0);
  4998. }
  4999. /**
  5000. * xmlReaderNewDoc:
  5001. * @reader: an XML reader
  5002. * @cur: a pointer to a zero terminated string
  5003. * @URL: the base URL to use for the document
  5004. * @encoding: the document encoding, or NULL
  5005. * @options: a combination of xmlParserOption
  5006. *
  5007. * Setup an xmltextReader to parse an XML in-memory document.
  5008. * The parsing flags @options are a combination of xmlParserOption.
  5009. * This reuses the existing @reader xmlTextReader.
  5010. *
  5011. * Returns 0 in case of success and -1 in case of error
  5012. */
  5013. int
  5014. xmlReaderNewDoc(xmlTextReaderPtr reader, const xmlChar * cur,
  5015. const char *URL, const char *encoding, int options)
  5016. {
  5017. int len;
  5018. if (cur == NULL)
  5019. return (-1);
  5020. if (reader == NULL)
  5021. return (-1);
  5022. len = xmlStrlen(cur);
  5023. return (xmlReaderNewMemory(reader, (const char *)cur, len,
  5024. URL, encoding, options));
  5025. }
  5026. /**
  5027. * xmlReaderNewFile:
  5028. * @reader: an XML reader
  5029. * @filename: a file or URL
  5030. * @encoding: the document encoding, or NULL
  5031. * @options: a combination of xmlParserOption
  5032. *
  5033. * parse an XML file from the filesystem or the network.
  5034. * The parsing flags @options are a combination of xmlParserOption.
  5035. * This reuses the existing @reader xmlTextReader.
  5036. *
  5037. * Returns 0 in case of success and -1 in case of error
  5038. */
  5039. int
  5040. xmlReaderNewFile(xmlTextReaderPtr reader, const char *filename,
  5041. const char *encoding, int options)
  5042. {
  5043. xmlParserInputBufferPtr input;
  5044. if (filename == NULL)
  5045. return (-1);
  5046. if (reader == NULL)
  5047. return (-1);
  5048. input =
  5049. xmlParserInputBufferCreateFilename(filename,
  5050. XML_CHAR_ENCODING_NONE);
  5051. if (input == NULL)
  5052. return (-1);
  5053. return (xmlTextReaderSetup(reader, input, filename, encoding, options));
  5054. }
  5055. /**
  5056. * xmlReaderNewMemory:
  5057. * @reader: an XML reader
  5058. * @buffer: a pointer to a char array
  5059. * @size: the size of the array
  5060. * @URL: the base URL to use for the document
  5061. * @encoding: the document encoding, or NULL
  5062. * @options: a combination of xmlParserOption
  5063. *
  5064. * Setup an xmltextReader to parse an XML in-memory document.
  5065. * The parsing flags @options are a combination of xmlParserOption.
  5066. * This reuses the existing @reader xmlTextReader.
  5067. *
  5068. * Returns 0 in case of success and -1 in case of error
  5069. */
  5070. int
  5071. xmlReaderNewMemory(xmlTextReaderPtr reader, const char *buffer, int size,
  5072. const char *URL, const char *encoding, int options)
  5073. {
  5074. xmlParserInputBufferPtr input;
  5075. if (reader == NULL)
  5076. return (-1);
  5077. if (buffer == NULL)
  5078. return (-1);
  5079. input = xmlParserInputBufferCreateStatic(buffer, size,
  5080. XML_CHAR_ENCODING_NONE);
  5081. if (input == NULL) {
  5082. return (-1);
  5083. }
  5084. return (xmlTextReaderSetup(reader, input, URL, encoding, options));
  5085. }
  5086. /**
  5087. * xmlReaderNewFd:
  5088. * @reader: an XML reader
  5089. * @fd: an open file descriptor
  5090. * @URL: the base URL to use for the document
  5091. * @encoding: the document encoding, or NULL
  5092. * @options: a combination of xmlParserOption
  5093. *
  5094. * Setup an xmltextReader to parse an XML from a file descriptor.
  5095. * NOTE that the file descriptor will not be closed when the
  5096. * reader is closed or reset.
  5097. * The parsing flags @options are a combination of xmlParserOption.
  5098. * This reuses the existing @reader xmlTextReader.
  5099. *
  5100. * Returns 0 in case of success and -1 in case of error
  5101. */
  5102. int
  5103. xmlReaderNewFd(xmlTextReaderPtr reader, int fd,
  5104. const char *URL, const char *encoding, int options)
  5105. {
  5106. xmlParserInputBufferPtr input;
  5107. if (fd < 0)
  5108. return (-1);
  5109. if (reader == NULL)
  5110. return (-1);
  5111. input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
  5112. if (input == NULL)
  5113. return (-1);
  5114. input->closecallback = NULL;
  5115. return (xmlTextReaderSetup(reader, input, URL, encoding, options));
  5116. }
  5117. /**
  5118. * xmlReaderNewIO:
  5119. * @reader: an XML reader
  5120. * @ioread: an I/O read function
  5121. * @ioclose: an I/O close function
  5122. * @ioctx: an I/O handler
  5123. * @URL: the base URL to use for the document
  5124. * @encoding: the document encoding, or NULL
  5125. * @options: a combination of xmlParserOption
  5126. *
  5127. * Setup an xmltextReader to parse an XML document from I/O functions
  5128. * and source.
  5129. * The parsing flags @options are a combination of xmlParserOption.
  5130. * This reuses the existing @reader xmlTextReader.
  5131. *
  5132. * Returns 0 in case of success and -1 in case of error
  5133. */
  5134. int
  5135. xmlReaderNewIO(xmlTextReaderPtr reader, xmlInputReadCallback ioread,
  5136. xmlInputCloseCallback ioclose, void *ioctx,
  5137. const char *URL, const char *encoding, int options)
  5138. {
  5139. xmlParserInputBufferPtr input;
  5140. if (ioread == NULL)
  5141. return (-1);
  5142. if (reader == NULL)
  5143. return (-1);
  5144. input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
  5145. XML_CHAR_ENCODING_NONE);
  5146. if (input == NULL)
  5147. return (-1);
  5148. return (xmlTextReaderSetup(reader, input, URL, encoding, options));
  5149. }
  5150. /************************************************************************
  5151. * *
  5152. * Utilities *
  5153. * *
  5154. ************************************************************************/
  5155. #ifdef NOT_USED_YET
  5156. /**
  5157. * xmlBase64Decode:
  5158. * @in: the input buffer
  5159. * @inlen: the size of the input (in), the size read from it (out)
  5160. * @to: the output buffer
  5161. * @tolen: the size of the output (in), the size written to (out)
  5162. *
  5163. * Base64 decoder, reads from @in and save in @to
  5164. * TODO: tell jody when this is actually exported
  5165. *
  5166. * Returns 0 if all the input was consumer, 1 if the Base64 end was reached,
  5167. * 2 if there wasn't enough space on the output or -1 in case of error.
  5168. */
  5169. static int
  5170. xmlBase64Decode(const unsigned char *in, unsigned long *inlen,
  5171. unsigned char *to, unsigned long *tolen)
  5172. {
  5173. unsigned long incur; /* current index in in[] */
  5174. unsigned long inblk; /* last block index in in[] */
  5175. unsigned long outcur; /* current index in out[] */
  5176. unsigned long inmax; /* size of in[] */
  5177. unsigned long outmax; /* size of out[] */
  5178. unsigned char cur; /* the current value read from in[] */
  5179. unsigned char intmp[4], outtmp[4]; /* temporary buffers for the convert */
  5180. int nbintmp; /* number of byte in intmp[] */
  5181. int is_ignore; /* cur should be ignored */
  5182. int is_end = 0; /* the end of the base64 was found */
  5183. int retval = 1;
  5184. int i;
  5185. if ((in == NULL) || (inlen == NULL) || (to == NULL) || (tolen == NULL))
  5186. return (-1);
  5187. incur = 0;
  5188. inblk = 0;
  5189. outcur = 0;
  5190. inmax = *inlen;
  5191. outmax = *tolen;
  5192. nbintmp = 0;
  5193. while (1) {
  5194. if (incur >= inmax)
  5195. break;
  5196. cur = in[incur++];
  5197. is_ignore = 0;
  5198. if ((cur >= 'A') && (cur <= 'Z'))
  5199. cur = cur - 'A';
  5200. else if ((cur >= 'a') && (cur <= 'z'))
  5201. cur = cur - 'a' + 26;
  5202. else if ((cur >= '0') && (cur <= '9'))
  5203. cur = cur - '0' + 52;
  5204. else if (cur == '+')
  5205. cur = 62;
  5206. else if (cur == '/')
  5207. cur = 63;
  5208. else if (cur == '.')
  5209. cur = 0;
  5210. else if (cur == '=') /*no op , end of the base64 stream */
  5211. is_end = 1;
  5212. else {
  5213. is_ignore = 1;
  5214. if (nbintmp == 0)
  5215. inblk = incur;
  5216. }
  5217. if (!is_ignore) {
  5218. int nbouttmp = 3;
  5219. int is_break = 0;
  5220. if (is_end) {
  5221. if (nbintmp == 0)
  5222. break;
  5223. if ((nbintmp == 1) || (nbintmp == 2))
  5224. nbouttmp = 1;
  5225. else
  5226. nbouttmp = 2;
  5227. nbintmp = 3;
  5228. is_break = 1;
  5229. }
  5230. intmp[nbintmp++] = cur;
  5231. /*
  5232. * if intmp is full, push the 4byte sequence as a 3 byte
  5233. * sequence out
  5234. */
  5235. if (nbintmp == 4) {
  5236. nbintmp = 0;
  5237. outtmp[0] = (intmp[0] << 2) | ((intmp[1] & 0x30) >> 4);
  5238. outtmp[1] =
  5239. ((intmp[1] & 0x0F) << 4) | ((intmp[2] & 0x3C) >> 2);
  5240. outtmp[2] = ((intmp[2] & 0x03) << 6) | (intmp[3] & 0x3F);
  5241. if (outcur + 3 >= outmax) {
  5242. retval = 2;
  5243. break;
  5244. }
  5245. for (i = 0; i < nbouttmp; i++)
  5246. to[outcur++] = outtmp[i];
  5247. inblk = incur;
  5248. }
  5249. if (is_break) {
  5250. retval = 0;
  5251. break;
  5252. }
  5253. }
  5254. }
  5255. *tolen = outcur;
  5256. *inlen = inblk;
  5257. return (retval);
  5258. }
  5259. /*
  5260. * Test routine for the xmlBase64Decode function
  5261. */
  5262. #if 0
  5263. int
  5264. main(int argc, char **argv)
  5265. {
  5266. char *input = " VW4 gcGV0 \n aXQgdGVzdCAuCg== ";
  5267. char output[100];
  5268. char output2[100];
  5269. char output3[100];
  5270. unsigned long inlen = strlen(input);
  5271. unsigned long outlen = 100;
  5272. int ret;
  5273. unsigned long cons, tmp, tmp2, prod;
  5274. /*
  5275. * Direct
  5276. */
  5277. ret = xmlBase64Decode(input, &inlen, output, &outlen);
  5278. output[outlen] = 0;
  5279. printf("ret: %d, inlen: %ld , outlen: %ld, output: '%s'\n", ret, inlen,
  5280. outlen, output)indent: Standard input:179: Error:Unmatched #endif
  5281. ;
  5282. /*
  5283. * output chunking
  5284. */
  5285. cons = 0;
  5286. prod = 0;
  5287. while (cons < inlen) {
  5288. tmp = 5;
  5289. tmp2 = inlen - cons;
  5290. printf("%ld %ld\n", cons, prod);
  5291. ret = xmlBase64Decode(&input[cons], &tmp2, &output2[prod], &tmp);
  5292. cons += tmp2;
  5293. prod += tmp;
  5294. printf("%ld %ld\n", cons, prod);
  5295. }
  5296. output2[outlen] = 0;
  5297. printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons,
  5298. prod, output2);
  5299. /*
  5300. * input chunking
  5301. */
  5302. cons = 0;
  5303. prod = 0;
  5304. while (cons < inlen) {
  5305. tmp = 100 - prod;
  5306. tmp2 = inlen - cons;
  5307. if (tmp2 > 5)
  5308. tmp2 = 5;
  5309. printf("%ld %ld\n", cons, prod);
  5310. ret = xmlBase64Decode(&input[cons], &tmp2, &output3[prod], &tmp);
  5311. cons += tmp2;
  5312. prod += tmp;
  5313. printf("%ld %ld\n", cons, prod);
  5314. }
  5315. output3[outlen] = 0;
  5316. printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons,
  5317. prod, output3);
  5318. return (0);
  5319. }
  5320. #endif
  5321. #endif /* NOT_USED_YET */
  5322. #define bottom_xmlreader
  5323. #include "elfgcchack.h"
  5324. #endif /* LIBXML_READER_ENABLED */