relaxng.c 356 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001
  1. /*
  2. * relaxng.c : implementation of the Relax-NG handling and validity checking
  3. *
  4. * See Copyright for the status of this software.
  5. *
  6. * Daniel Veillard <veillard@redhat.com>
  7. */
  8. /**
  9. * TODO:
  10. * - add support for DTD compatibility spec
  11. * http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html
  12. * - report better mem allocations pbms at runtime and abort immediately.
  13. */
  14. #define IN_LIBXML
  15. #include "libxml.h"
  16. #ifdef LIBXML_SCHEMAS_ENABLED
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <libxml/xmlmemory.h>
  20. #include <libxml/parser.h>
  21. #include <libxml/parserInternals.h>
  22. #include <libxml/hash.h>
  23. #include <libxml/uri.h>
  24. #include <libxml/relaxng.h>
  25. #include <libxml/xmlschemastypes.h>
  26. #include <libxml/xmlautomata.h>
  27. #include <libxml/xmlregexp.h>
  28. #include <libxml/xmlschemastypes.h>
  29. /*
  30. * The Relax-NG namespace
  31. */
  32. static const xmlChar *xmlRelaxNGNs = (const xmlChar *)
  33. "http://relaxng.org/ns/structure/1.0";
  34. #define IS_RELAXNG(node, type) \
  35. ((node != NULL) && (node->ns != NULL) && \
  36. (xmlStrEqual(node->name, (const xmlChar *) type)) && \
  37. (xmlStrEqual(node->ns->href, xmlRelaxNGNs)))
  38. #if 0
  39. #define DEBUG 1
  40. #define DEBUG_GRAMMAR 1
  41. #define DEBUG_CONTENT 1
  42. #define DEBUG_TYPE 1
  43. #define DEBUG_VALID 1
  44. #define DEBUG_INTERLEAVE 1
  45. #define DEBUG_LIST 1
  46. #define DEBUG_INCLUDE 1
  47. #define DEBUG_ERROR 1
  48. #define DEBUG_COMPILE 1
  49. #define DEBUG_PROGRESSIVE 1
  50. #endif
  51. #define MAX_ERROR 5
  52. #define TODO \
  53. xmlGenericError(xmlGenericErrorContext, \
  54. "Unimplemented block at %s:%d\n", \
  55. __FILE__, __LINE__);
  56. typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema;
  57. typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr;
  58. typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine;
  59. typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr;
  60. typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument;
  61. typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr;
  62. typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude;
  63. typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr;
  64. typedef enum {
  65. XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */
  66. XML_RELAXNG_COMBINE_CHOICE, /* choice */
  67. XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */
  68. } xmlRelaxNGCombine;
  69. typedef enum {
  70. XML_RELAXNG_CONTENT_ERROR = -1,
  71. XML_RELAXNG_CONTENT_EMPTY = 0,
  72. XML_RELAXNG_CONTENT_SIMPLE,
  73. XML_RELAXNG_CONTENT_COMPLEX
  74. } xmlRelaxNGContentType;
  75. typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar;
  76. typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr;
  77. struct _xmlRelaxNGGrammar {
  78. xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */
  79. xmlRelaxNGGrammarPtr children; /* the children grammar if any */
  80. xmlRelaxNGGrammarPtr next; /* the next grammar if any */
  81. xmlRelaxNGDefinePtr start; /* <start> content */
  82. xmlRelaxNGCombine combine; /* the default combine value */
  83. xmlRelaxNGDefinePtr startList; /* list of <start> definitions */
  84. xmlHashTablePtr defs; /* define* */
  85. xmlHashTablePtr refs; /* references */
  86. };
  87. typedef enum {
  88. XML_RELAXNG_NOOP = -1, /* a no operation from simplification */
  89. XML_RELAXNG_EMPTY = 0, /* an empty pattern */
  90. XML_RELAXNG_NOT_ALLOWED, /* not allowed top */
  91. XML_RELAXNG_EXCEPT, /* except present in nameclass defs */
  92. XML_RELAXNG_TEXT, /* textual content */
  93. XML_RELAXNG_ELEMENT, /* an element */
  94. XML_RELAXNG_DATATYPE, /* extenal data type definition */
  95. XML_RELAXNG_PARAM, /* extenal data type parameter */
  96. XML_RELAXNG_VALUE, /* value from an extenal data type definition */
  97. XML_RELAXNG_LIST, /* a list of patterns */
  98. XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */
  99. XML_RELAXNG_DEF, /* a definition */
  100. XML_RELAXNG_REF, /* reference to a definition */
  101. XML_RELAXNG_EXTERNALREF, /* reference to an external def */
  102. XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */
  103. XML_RELAXNG_OPTIONAL, /* optional patterns */
  104. XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */
  105. XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */
  106. XML_RELAXNG_CHOICE, /* a choice between non empty patterns */
  107. XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */
  108. XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */
  109. XML_RELAXNG_START /* Used to keep track of starts on grammars */
  110. } xmlRelaxNGType;
  111. #define IS_NULLABLE (1 << 0)
  112. #define IS_NOT_NULLABLE (1 << 1)
  113. #define IS_INDETERMINIST (1 << 2)
  114. #define IS_MIXED (1 << 3)
  115. #define IS_TRIABLE (1 << 4)
  116. #define IS_PROCESSED (1 << 5)
  117. #define IS_COMPILABLE (1 << 6)
  118. #define IS_NOT_COMPILABLE (1 << 7)
  119. #define IS_EXTERNAL_REF (1 << 8)
  120. struct _xmlRelaxNGDefine {
  121. xmlRelaxNGType type; /* the type of definition */
  122. xmlNodePtr node; /* the node in the source */
  123. xmlChar *name; /* the element local name if present */
  124. xmlChar *ns; /* the namespace local name if present */
  125. xmlChar *value; /* value when available */
  126. void *data; /* data lib or specific pointer */
  127. xmlRelaxNGDefinePtr content; /* the expected content */
  128. xmlRelaxNGDefinePtr parent; /* the parent definition, if any */
  129. xmlRelaxNGDefinePtr next; /* list within grouping sequences */
  130. xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */
  131. xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */
  132. xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */
  133. short depth; /* used for the cycle detection */
  134. short dflags; /* define related flags */
  135. xmlRegexpPtr contModel; /* a compiled content model if available */
  136. };
  137. /**
  138. * _xmlRelaxNG:
  139. *
  140. * A RelaxNGs definition
  141. */
  142. struct _xmlRelaxNG {
  143. void *_private; /* unused by the library for users or bindings */
  144. xmlRelaxNGGrammarPtr topgrammar;
  145. xmlDocPtr doc;
  146. int idref; /* requires idref checking */
  147. xmlHashTablePtr defs; /* define */
  148. xmlHashTablePtr refs; /* references */
  149. xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
  150. xmlRelaxNGIncludePtr includes; /* all the includes loaded */
  151. int defNr; /* number of defines used */
  152. xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
  153. };
  154. #define XML_RELAXNG_IN_ATTRIBUTE (1 << 0)
  155. #define XML_RELAXNG_IN_ONEORMORE (1 << 1)
  156. #define XML_RELAXNG_IN_LIST (1 << 2)
  157. #define XML_RELAXNG_IN_DATAEXCEPT (1 << 3)
  158. #define XML_RELAXNG_IN_START (1 << 4)
  159. #define XML_RELAXNG_IN_OOMGROUP (1 << 5)
  160. #define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6)
  161. #define XML_RELAXNG_IN_EXTERNALREF (1 << 7)
  162. #define XML_RELAXNG_IN_ANYEXCEPT (1 << 8)
  163. #define XML_RELAXNG_IN_NSEXCEPT (1 << 9)
  164. struct _xmlRelaxNGParserCtxt {
  165. void *userData; /* user specific data block */
  166. xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
  167. xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
  168. xmlStructuredErrorFunc serror;
  169. xmlRelaxNGValidErr err;
  170. xmlRelaxNGPtr schema; /* The schema in use */
  171. xmlRelaxNGGrammarPtr grammar; /* the current grammar */
  172. xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */
  173. int flags; /* parser flags */
  174. int nbErrors; /* number of errors at parse time */
  175. int nbWarnings; /* number of warnings at parse time */
  176. const xmlChar *define; /* the current define scope */
  177. xmlRelaxNGDefinePtr def; /* the current define */
  178. int nbInterleaves;
  179. xmlHashTablePtr interleaves; /* keep track of all the interleaves */
  180. xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
  181. xmlRelaxNGIncludePtr includes; /* all the includes loaded */
  182. xmlChar *URL;
  183. xmlDocPtr document;
  184. int defNr; /* number of defines used */
  185. int defMax; /* number of defines aloocated */
  186. xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
  187. const char *buffer;
  188. int size;
  189. /* the document stack */
  190. xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */
  191. int docNr; /* Depth of the parsing stack */
  192. int docMax; /* Max depth of the parsing stack */
  193. xmlRelaxNGDocumentPtr *docTab; /* array of docs */
  194. /* the include stack */
  195. xmlRelaxNGIncludePtr inc; /* Current parsed include */
  196. int incNr; /* Depth of the include parsing stack */
  197. int incMax; /* Max depth of the parsing stack */
  198. xmlRelaxNGIncludePtr *incTab; /* array of incs */
  199. int idref; /* requires idref checking */
  200. /* used to compile content models */
  201. xmlAutomataPtr am; /* the automata */
  202. xmlAutomataStatePtr state; /* used to build the automata */
  203. int crng; /* compact syntax and other flags */
  204. int freedoc; /* need to free the document */
  205. };
  206. #define FLAGS_IGNORABLE 1
  207. #define FLAGS_NEGATIVE 2
  208. #define FLAGS_MIXED_CONTENT 4
  209. #define FLAGS_NOERROR 8
  210. /**
  211. * xmlRelaxNGInterleaveGroup:
  212. *
  213. * A RelaxNGs partition set associated to lists of definitions
  214. */
  215. typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
  216. typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
  217. struct _xmlRelaxNGInterleaveGroup {
  218. xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
  219. xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
  220. xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
  221. };
  222. #define IS_DETERMINIST 1
  223. #define IS_NEEDCHECK 2
  224. /**
  225. * xmlRelaxNGPartitions:
  226. *
  227. * A RelaxNGs partition associated to an interleave group
  228. */
  229. typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
  230. typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
  231. struct _xmlRelaxNGPartition {
  232. int nbgroups; /* number of groups in the partitions */
  233. xmlHashTablePtr triage; /* hash table used to direct nodes to the
  234. * right group when possible */
  235. int flags; /* determinist ? */
  236. xmlRelaxNGInterleaveGroupPtr *groups;
  237. };
  238. /**
  239. * xmlRelaxNGValidState:
  240. *
  241. * A RelaxNGs validation state
  242. */
  243. #define MAX_ATTR 20
  244. typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
  245. typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
  246. struct _xmlRelaxNGValidState {
  247. xmlNodePtr node; /* the current node */
  248. xmlNodePtr seq; /* the sequence of children left to validate */
  249. int nbAttrs; /* the number of attributes */
  250. int maxAttrs; /* the size of attrs */
  251. int nbAttrLeft; /* the number of attributes left to validate */
  252. xmlChar *value; /* the value when operating on string */
  253. xmlChar *endvalue; /* the end value when operating on string */
  254. xmlAttrPtr *attrs; /* the array of attributes */
  255. };
  256. /**
  257. * xmlRelaxNGStates:
  258. *
  259. * A RelaxNGs container for validation state
  260. */
  261. typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
  262. typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
  263. struct _xmlRelaxNGStates {
  264. int nbState; /* the number of states */
  265. int maxState; /* the size of the array */
  266. xmlRelaxNGValidStatePtr *tabState;
  267. };
  268. #define ERROR_IS_DUP 1
  269. /**
  270. * xmlRelaxNGValidError:
  271. *
  272. * A RelaxNGs validation error
  273. */
  274. typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
  275. typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
  276. struct _xmlRelaxNGValidError {
  277. xmlRelaxNGValidErr err; /* the error number */
  278. int flags; /* flags */
  279. xmlNodePtr node; /* the current node */
  280. xmlNodePtr seq; /* the current child */
  281. const xmlChar *arg1; /* first arg */
  282. const xmlChar *arg2; /* second arg */
  283. };
  284. /**
  285. * xmlRelaxNGValidCtxt:
  286. *
  287. * A RelaxNGs validation context
  288. */
  289. struct _xmlRelaxNGValidCtxt {
  290. void *userData; /* user specific data block */
  291. xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
  292. xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
  293. xmlStructuredErrorFunc serror;
  294. int nbErrors; /* number of errors in validation */
  295. xmlRelaxNGPtr schema; /* The schema in use */
  296. xmlDocPtr doc; /* the document being validated */
  297. int flags; /* validation flags */
  298. int depth; /* validation depth */
  299. int idref; /* requires idref checking */
  300. int errNo; /* the first error found */
  301. /*
  302. * Errors accumulated in branches may have to be stacked to be
  303. * provided back when it's sure they affect validation.
  304. */
  305. xmlRelaxNGValidErrorPtr err; /* Last error */
  306. int errNr; /* Depth of the error stack */
  307. int errMax; /* Max depth of the error stack */
  308. xmlRelaxNGValidErrorPtr errTab; /* stack of errors */
  309. xmlRelaxNGValidStatePtr state; /* the current validation state */
  310. xmlRelaxNGStatesPtr states; /* the accumulated state list */
  311. xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
  312. int freeStatesNr;
  313. int freeStatesMax;
  314. xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
  315. /*
  316. * This is used for "progressive" validation
  317. */
  318. xmlRegExecCtxtPtr elem; /* the current element regexp */
  319. int elemNr; /* the number of element validated */
  320. int elemMax; /* the max depth of elements */
  321. xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */
  322. int pstate; /* progressive state */
  323. xmlNodePtr pnode; /* the current node */
  324. xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */
  325. int perr; /* signal error in content model
  326. * outside the regexp */
  327. };
  328. /**
  329. * xmlRelaxNGInclude:
  330. *
  331. * Structure associated to a RelaxNGs document element
  332. */
  333. struct _xmlRelaxNGInclude {
  334. xmlRelaxNGIncludePtr next; /* keep a chain of includes */
  335. xmlChar *href; /* the normalized href value */
  336. xmlDocPtr doc; /* the associated XML document */
  337. xmlRelaxNGDefinePtr content; /* the definitions */
  338. xmlRelaxNGPtr schema; /* the schema */
  339. };
  340. /**
  341. * xmlRelaxNGDocument:
  342. *
  343. * Structure associated to a RelaxNGs document element
  344. */
  345. struct _xmlRelaxNGDocument {
  346. xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
  347. xmlChar *href; /* the normalized href value */
  348. xmlDocPtr doc; /* the associated XML document */
  349. xmlRelaxNGDefinePtr content; /* the definitions */
  350. xmlRelaxNGPtr schema; /* the schema */
  351. int externalRef; /* 1 if an external ref */
  352. };
  353. /************************************************************************
  354. * *
  355. * Some factorized error routines *
  356. * *
  357. ************************************************************************/
  358. /**
  359. * xmlRngPErrMemory:
  360. * @ctxt: an Relax-NG parser context
  361. * @extra: extra informations
  362. *
  363. * Handle a redefinition of attribute error
  364. */
  365. static void
  366. xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra)
  367. {
  368. xmlStructuredErrorFunc schannel = NULL;
  369. xmlGenericErrorFunc channel = NULL;
  370. void *data = NULL;
  371. if (ctxt != NULL) {
  372. if (ctxt->serror != NULL)
  373. schannel = ctxt->serror;
  374. else
  375. channel = ctxt->error;
  376. data = ctxt->userData;
  377. ctxt->nbErrors++;
  378. }
  379. if (extra)
  380. __xmlRaiseError(schannel, channel, data,
  381. NULL, NULL, XML_FROM_RELAXNGP,
  382. XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
  383. NULL, NULL, 0, 0,
  384. "Memory allocation failed : %s\n", extra);
  385. else
  386. __xmlRaiseError(schannel, channel, data,
  387. NULL, NULL, XML_FROM_RELAXNGP,
  388. XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
  389. NULL, NULL, 0, 0, "Memory allocation failed\n");
  390. }
  391. /**
  392. * xmlRngVErrMemory:
  393. * @ctxt: a Relax-NG validation context
  394. * @extra: extra informations
  395. *
  396. * Handle a redefinition of attribute error
  397. */
  398. static void
  399. xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra)
  400. {
  401. xmlStructuredErrorFunc schannel = NULL;
  402. xmlGenericErrorFunc channel = NULL;
  403. void *data = NULL;
  404. if (ctxt != NULL) {
  405. if (ctxt->serror != NULL)
  406. schannel = ctxt->serror;
  407. else
  408. channel = ctxt->error;
  409. data = ctxt->userData;
  410. ctxt->nbErrors++;
  411. }
  412. if (extra)
  413. __xmlRaiseError(schannel, channel, data,
  414. NULL, NULL, XML_FROM_RELAXNGV,
  415. XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
  416. NULL, NULL, 0, 0,
  417. "Memory allocation failed : %s\n", extra);
  418. else
  419. __xmlRaiseError(schannel, channel, data,
  420. NULL, NULL, XML_FROM_RELAXNGV,
  421. XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
  422. NULL, NULL, 0, 0, "Memory allocation failed\n");
  423. }
  424. /**
  425. * xmlRngPErr:
  426. * @ctxt: a Relax-NG parser context
  427. * @node: the node raising the error
  428. * @error: the error code
  429. * @msg: message
  430. * @str1: extra info
  431. * @str2: extra info
  432. *
  433. * Handle a Relax NG Parsing error
  434. */
  435. static void
  436. xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error,
  437. const char *msg, const xmlChar * str1, const xmlChar * str2)
  438. {
  439. xmlStructuredErrorFunc schannel = NULL;
  440. xmlGenericErrorFunc channel = NULL;
  441. void *data = NULL;
  442. if (ctxt != NULL) {
  443. if (ctxt->serror != NULL)
  444. schannel = ctxt->serror;
  445. else
  446. channel = ctxt->error;
  447. data = ctxt->userData;
  448. ctxt->nbErrors++;
  449. }
  450. __xmlRaiseError(schannel, channel, data,
  451. NULL, node, XML_FROM_RELAXNGP,
  452. error, XML_ERR_ERROR, NULL, 0,
  453. (const char *) str1, (const char *) str2, NULL, 0, 0,
  454. msg, str1, str2);
  455. }
  456. /**
  457. * xmlRngVErr:
  458. * @ctxt: a Relax-NG validation context
  459. * @node: the node raising the error
  460. * @error: the error code
  461. * @msg: message
  462. * @str1: extra info
  463. * @str2: extra info
  464. *
  465. * Handle a Relax NG Validation error
  466. */
  467. static void
  468. xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error,
  469. const char *msg, const xmlChar * str1, const xmlChar * str2)
  470. {
  471. xmlStructuredErrorFunc schannel = NULL;
  472. xmlGenericErrorFunc channel = NULL;
  473. void *data = NULL;
  474. if (ctxt != NULL) {
  475. if (ctxt->serror != NULL)
  476. schannel = ctxt->serror;
  477. else
  478. channel = ctxt->error;
  479. data = ctxt->userData;
  480. ctxt->nbErrors++;
  481. }
  482. __xmlRaiseError(schannel, channel, data,
  483. NULL, node, XML_FROM_RELAXNGV,
  484. error, XML_ERR_ERROR, NULL, 0,
  485. (const char *) str1, (const char *) str2, NULL, 0, 0,
  486. msg, str1, str2);
  487. }
  488. /************************************************************************
  489. * *
  490. * Preliminary type checking interfaces *
  491. * *
  492. ************************************************************************/
  493. /**
  494. * xmlRelaxNGTypeHave:
  495. * @data: data needed for the library
  496. * @type: the type name
  497. * @value: the value to check
  498. *
  499. * Function provided by a type library to check if a type is exported
  500. *
  501. * Returns 1 if yes, 0 if no and -1 in case of error.
  502. */
  503. typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type);
  504. /**
  505. * xmlRelaxNGTypeCheck:
  506. * @data: data needed for the library
  507. * @type: the type name
  508. * @value: the value to check
  509. * @result: place to store the result if needed
  510. *
  511. * Function provided by a type library to check if a value match a type
  512. *
  513. * Returns 1 if yes, 0 if no and -1 in case of error.
  514. */
  515. typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type,
  516. const xmlChar * value, void **result,
  517. xmlNodePtr node);
  518. /**
  519. * xmlRelaxNGFacetCheck:
  520. * @data: data needed for the library
  521. * @type: the type name
  522. * @facet: the facet name
  523. * @val: the facet value
  524. * @strval: the string value
  525. * @value: the value to check
  526. *
  527. * Function provided by a type library to check a value facet
  528. *
  529. * Returns 1 if yes, 0 if no and -1 in case of error.
  530. */
  531. typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type,
  532. const xmlChar * facet,
  533. const xmlChar * val,
  534. const xmlChar * strval, void *value);
  535. /**
  536. * xmlRelaxNGTypeFree:
  537. * @data: data needed for the library
  538. * @result: the value to free
  539. *
  540. * Function provided by a type library to free a returned result
  541. */
  542. typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
  543. /**
  544. * xmlRelaxNGTypeCompare:
  545. * @data: data needed for the library
  546. * @type: the type name
  547. * @value1: the first value
  548. * @value2: the second value
  549. *
  550. * Function provided by a type library to compare two values accordingly
  551. * to a type.
  552. *
  553. * Returns 1 if yes, 0 if no and -1 in case of error.
  554. */
  555. typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type,
  556. const xmlChar * value1,
  557. xmlNodePtr ctxt1,
  558. void *comp1,
  559. const xmlChar * value2,
  560. xmlNodePtr ctxt2);
  561. typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
  562. typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
  563. struct _xmlRelaxNGTypeLibrary {
  564. const xmlChar *namespace; /* the datatypeLibrary value */
  565. void *data; /* data needed for the library */
  566. xmlRelaxNGTypeHave have; /* the export function */
  567. xmlRelaxNGTypeCheck check; /* the checking function */
  568. xmlRelaxNGTypeCompare comp; /* the compare function */
  569. xmlRelaxNGFacetCheck facet; /* the facet check function */
  570. xmlRelaxNGTypeFree freef; /* the freeing function */
  571. };
  572. /************************************************************************
  573. * *
  574. * Allocation functions *
  575. * *
  576. ************************************************************************/
  577. static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
  578. static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
  579. static void xmlRelaxNGNormExtSpace(xmlChar * value);
  580. static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
  581. static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt
  582. ATTRIBUTE_UNUSED,
  583. xmlRelaxNGValidStatePtr state1,
  584. xmlRelaxNGValidStatePtr state2);
  585. static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
  586. xmlRelaxNGValidStatePtr state);
  587. /**
  588. * xmlRelaxNGFreeDocument:
  589. * @docu: a document structure
  590. *
  591. * Deallocate a RelaxNG document structure.
  592. */
  593. static void
  594. xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
  595. {
  596. if (docu == NULL)
  597. return;
  598. if (docu->href != NULL)
  599. xmlFree(docu->href);
  600. if (docu->doc != NULL)
  601. xmlFreeDoc(docu->doc);
  602. if (docu->schema != NULL)
  603. xmlRelaxNGFreeInnerSchema(docu->schema);
  604. xmlFree(docu);
  605. }
  606. /**
  607. * xmlRelaxNGFreeDocumentList:
  608. * @docu: a list of document structure
  609. *
  610. * Deallocate a RelaxNG document structures.
  611. */
  612. static void
  613. xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
  614. {
  615. xmlRelaxNGDocumentPtr next;
  616. while (docu != NULL) {
  617. next = docu->next;
  618. xmlRelaxNGFreeDocument(docu);
  619. docu = next;
  620. }
  621. }
  622. /**
  623. * xmlRelaxNGFreeInclude:
  624. * @incl: a include structure
  625. *
  626. * Deallocate a RelaxNG include structure.
  627. */
  628. static void
  629. xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
  630. {
  631. if (incl == NULL)
  632. return;
  633. if (incl->href != NULL)
  634. xmlFree(incl->href);
  635. if (incl->doc != NULL)
  636. xmlFreeDoc(incl->doc);
  637. if (incl->schema != NULL)
  638. xmlRelaxNGFree(incl->schema);
  639. xmlFree(incl);
  640. }
  641. /**
  642. * xmlRelaxNGFreeIncludeList:
  643. * @incl: a include structure list
  644. *
  645. * Deallocate a RelaxNG include structure.
  646. */
  647. static void
  648. xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
  649. {
  650. xmlRelaxNGIncludePtr next;
  651. while (incl != NULL) {
  652. next = incl->next;
  653. xmlRelaxNGFreeInclude(incl);
  654. incl = next;
  655. }
  656. }
  657. /**
  658. * xmlRelaxNGNewRelaxNG:
  659. * @ctxt: a Relax-NG validation context (optional)
  660. *
  661. * Allocate a new RelaxNG structure.
  662. *
  663. * Returns the newly allocated structure or NULL in case or error
  664. */
  665. static xmlRelaxNGPtr
  666. xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
  667. {
  668. xmlRelaxNGPtr ret;
  669. ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
  670. if (ret == NULL) {
  671. xmlRngPErrMemory(ctxt, NULL);
  672. return (NULL);
  673. }
  674. memset(ret, 0, sizeof(xmlRelaxNG));
  675. return (ret);
  676. }
  677. /**
  678. * xmlRelaxNGFreeInnerSchema:
  679. * @schema: a schema structure
  680. *
  681. * Deallocate a RelaxNG schema structure.
  682. */
  683. static void
  684. xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
  685. {
  686. if (schema == NULL)
  687. return;
  688. if (schema->doc != NULL)
  689. xmlFreeDoc(schema->doc);
  690. if (schema->defTab != NULL) {
  691. int i;
  692. for (i = 0; i < schema->defNr; i++)
  693. xmlRelaxNGFreeDefine(schema->defTab[i]);
  694. xmlFree(schema->defTab);
  695. }
  696. xmlFree(schema);
  697. }
  698. /**
  699. * xmlRelaxNGFree:
  700. * @schema: a schema structure
  701. *
  702. * Deallocate a RelaxNG structure.
  703. */
  704. void
  705. xmlRelaxNGFree(xmlRelaxNGPtr schema)
  706. {
  707. if (schema == NULL)
  708. return;
  709. if (schema->topgrammar != NULL)
  710. xmlRelaxNGFreeGrammar(schema->topgrammar);
  711. if (schema->doc != NULL)
  712. xmlFreeDoc(schema->doc);
  713. if (schema->documents != NULL)
  714. xmlRelaxNGFreeDocumentList(schema->documents);
  715. if (schema->includes != NULL)
  716. xmlRelaxNGFreeIncludeList(schema->includes);
  717. if (schema->defTab != NULL) {
  718. int i;
  719. for (i = 0; i < schema->defNr; i++)
  720. xmlRelaxNGFreeDefine(schema->defTab[i]);
  721. xmlFree(schema->defTab);
  722. }
  723. xmlFree(schema);
  724. }
  725. /**
  726. * xmlRelaxNGNewGrammar:
  727. * @ctxt: a Relax-NG validation context (optional)
  728. *
  729. * Allocate a new RelaxNG grammar.
  730. *
  731. * Returns the newly allocated structure or NULL in case or error
  732. */
  733. static xmlRelaxNGGrammarPtr
  734. xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
  735. {
  736. xmlRelaxNGGrammarPtr ret;
  737. ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
  738. if (ret == NULL) {
  739. xmlRngPErrMemory(ctxt, NULL);
  740. return (NULL);
  741. }
  742. memset(ret, 0, sizeof(xmlRelaxNGGrammar));
  743. return (ret);
  744. }
  745. /**
  746. * xmlRelaxNGFreeGrammar:
  747. * @grammar: a grammar structure
  748. *
  749. * Deallocate a RelaxNG grammar structure.
  750. */
  751. static void
  752. xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
  753. {
  754. if (grammar == NULL)
  755. return;
  756. if (grammar->children != NULL) {
  757. xmlRelaxNGFreeGrammar(grammar->children);
  758. }
  759. if (grammar->next != NULL) {
  760. xmlRelaxNGFreeGrammar(grammar->next);
  761. }
  762. if (grammar->refs != NULL) {
  763. xmlHashFree(grammar->refs, NULL);
  764. }
  765. if (grammar->defs != NULL) {
  766. xmlHashFree(grammar->defs, NULL);
  767. }
  768. xmlFree(grammar);
  769. }
  770. /**
  771. * xmlRelaxNGNewDefine:
  772. * @ctxt: a Relax-NG validation context
  773. * @node: the node in the input document.
  774. *
  775. * Allocate a new RelaxNG define.
  776. *
  777. * Returns the newly allocated structure or NULL in case or error
  778. */
  779. static xmlRelaxNGDefinePtr
  780. xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
  781. {
  782. xmlRelaxNGDefinePtr ret;
  783. if (ctxt->defMax == 0) {
  784. ctxt->defMax = 16;
  785. ctxt->defNr = 0;
  786. ctxt->defTab = (xmlRelaxNGDefinePtr *)
  787. xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
  788. if (ctxt->defTab == NULL) {
  789. xmlRngPErrMemory(ctxt, "allocating define\n");
  790. return (NULL);
  791. }
  792. } else if (ctxt->defMax <= ctxt->defNr) {
  793. xmlRelaxNGDefinePtr *tmp;
  794. ctxt->defMax *= 2;
  795. tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
  796. ctxt->defMax *
  797. sizeof
  798. (xmlRelaxNGDefinePtr));
  799. if (tmp == NULL) {
  800. xmlRngPErrMemory(ctxt, "allocating define\n");
  801. return (NULL);
  802. }
  803. ctxt->defTab = tmp;
  804. }
  805. ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
  806. if (ret == NULL) {
  807. xmlRngPErrMemory(ctxt, "allocating define\n");
  808. return (NULL);
  809. }
  810. memset(ret, 0, sizeof(xmlRelaxNGDefine));
  811. ctxt->defTab[ctxt->defNr++] = ret;
  812. ret->node = node;
  813. ret->depth = -1;
  814. return (ret);
  815. }
  816. /**
  817. * xmlRelaxNGFreePartition:
  818. * @partitions: a partition set structure
  819. *
  820. * Deallocate RelaxNG partition set structures.
  821. */
  822. static void
  823. xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions)
  824. {
  825. xmlRelaxNGInterleaveGroupPtr group;
  826. int j;
  827. if (partitions != NULL) {
  828. if (partitions->groups != NULL) {
  829. for (j = 0; j < partitions->nbgroups; j++) {
  830. group = partitions->groups[j];
  831. if (group != NULL) {
  832. if (group->defs != NULL)
  833. xmlFree(group->defs);
  834. if (group->attrs != NULL)
  835. xmlFree(group->attrs);
  836. xmlFree(group);
  837. }
  838. }
  839. xmlFree(partitions->groups);
  840. }
  841. if (partitions->triage != NULL) {
  842. xmlHashFree(partitions->triage, NULL);
  843. }
  844. xmlFree(partitions);
  845. }
  846. }
  847. /**
  848. * xmlRelaxNGFreeDefine:
  849. * @define: a define structure
  850. *
  851. * Deallocate a RelaxNG define structure.
  852. */
  853. static void
  854. xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
  855. {
  856. if (define == NULL)
  857. return;
  858. if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) {
  859. xmlRelaxNGTypeLibraryPtr lib;
  860. lib = (xmlRelaxNGTypeLibraryPtr) define->data;
  861. if ((lib != NULL) && (lib->freef != NULL))
  862. lib->freef(lib->data, (void *) define->attrs);
  863. }
  864. if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE))
  865. xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
  866. if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE))
  867. xmlHashFree((xmlHashTablePtr) define->data, NULL);
  868. if (define->name != NULL)
  869. xmlFree(define->name);
  870. if (define->ns != NULL)
  871. xmlFree(define->ns);
  872. if (define->value != NULL)
  873. xmlFree(define->value);
  874. if (define->contModel != NULL)
  875. xmlRegFreeRegexp(define->contModel);
  876. xmlFree(define);
  877. }
  878. /**
  879. * xmlRelaxNGNewStates:
  880. * @ctxt: a Relax-NG validation context
  881. * @size: the default size for the container
  882. *
  883. * Allocate a new RelaxNG validation state container
  884. *
  885. * Returns the newly allocated structure or NULL in case or error
  886. */
  887. static xmlRelaxNGStatesPtr
  888. xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
  889. {
  890. xmlRelaxNGStatesPtr ret;
  891. if ((ctxt != NULL) &&
  892. (ctxt->freeStates != NULL) && (ctxt->freeStatesNr > 0)) {
  893. ctxt->freeStatesNr--;
  894. ret = ctxt->freeStates[ctxt->freeStatesNr];
  895. ret->nbState = 0;
  896. return (ret);
  897. }
  898. if (size < 16)
  899. size = 16;
  900. ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
  901. (size -
  902. 1) *
  903. sizeof(xmlRelaxNGValidStatePtr));
  904. if (ret == NULL) {
  905. xmlRngVErrMemory(ctxt, "allocating states\n");
  906. return (NULL);
  907. }
  908. ret->nbState = 0;
  909. ret->maxState = size;
  910. ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) *
  911. sizeof
  912. (xmlRelaxNGValidStatePtr));
  913. if (ret->tabState == NULL) {
  914. xmlRngVErrMemory(ctxt, "allocating states\n");
  915. xmlFree(ret);
  916. return (NULL);
  917. }
  918. return (ret);
  919. }
  920. /**
  921. * xmlRelaxNGAddStateUniq:
  922. * @ctxt: a Relax-NG validation context
  923. * @states: the states container
  924. * @state: the validation state
  925. *
  926. * Add a RelaxNG validation state to the container without checking
  927. * for unicity.
  928. *
  929. * Return 1 in case of success and 0 if this is a duplicate and -1 on error
  930. */
  931. static int
  932. xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
  933. xmlRelaxNGStatesPtr states,
  934. xmlRelaxNGValidStatePtr state)
  935. {
  936. if (state == NULL) {
  937. return (-1);
  938. }
  939. if (states->nbState >= states->maxState) {
  940. xmlRelaxNGValidStatePtr *tmp;
  941. int size;
  942. size = states->maxState * 2;
  943. tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
  944. (size) *
  945. sizeof
  946. (xmlRelaxNGValidStatePtr));
  947. if (tmp == NULL) {
  948. xmlRngVErrMemory(ctxt, "adding states\n");
  949. return (-1);
  950. }
  951. states->tabState = tmp;
  952. states->maxState = size;
  953. }
  954. states->tabState[states->nbState++] = state;
  955. return (1);
  956. }
  957. /**
  958. * xmlRelaxNGAddState:
  959. * @ctxt: a Relax-NG validation context
  960. * @states: the states container
  961. * @state: the validation state
  962. *
  963. * Add a RelaxNG validation state to the container
  964. *
  965. * Return 1 in case of success and 0 if this is a duplicate and -1 on error
  966. */
  967. static int
  968. xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt,
  969. xmlRelaxNGStatesPtr states,
  970. xmlRelaxNGValidStatePtr state)
  971. {
  972. int i;
  973. if (state == NULL) {
  974. return (-1);
  975. }
  976. if (states->nbState >= states->maxState) {
  977. xmlRelaxNGValidStatePtr *tmp;
  978. int size;
  979. size = states->maxState * 2;
  980. tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
  981. (size) *
  982. sizeof
  983. (xmlRelaxNGValidStatePtr));
  984. if (tmp == NULL) {
  985. xmlRngVErrMemory(ctxt, "adding states\n");
  986. return (-1);
  987. }
  988. states->tabState = tmp;
  989. states->maxState = size;
  990. }
  991. for (i = 0; i < states->nbState; i++) {
  992. if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
  993. xmlRelaxNGFreeValidState(ctxt, state);
  994. return (0);
  995. }
  996. }
  997. states->tabState[states->nbState++] = state;
  998. return (1);
  999. }
  1000. /**
  1001. * xmlRelaxNGFreeStates:
  1002. * @ctxt: a Relax-NG validation context
  1003. * @states: teh container
  1004. *
  1005. * Free a RelaxNG validation state container
  1006. */
  1007. static void
  1008. xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
  1009. xmlRelaxNGStatesPtr states)
  1010. {
  1011. if (states == NULL)
  1012. return;
  1013. if ((ctxt != NULL) && (ctxt->freeStates == NULL)) {
  1014. ctxt->freeStatesMax = 40;
  1015. ctxt->freeStatesNr = 0;
  1016. ctxt->freeStates = (xmlRelaxNGStatesPtr *)
  1017. xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
  1018. if (ctxt->freeStates == NULL) {
  1019. xmlRngVErrMemory(ctxt, "storing states\n");
  1020. }
  1021. } else if ((ctxt != NULL)
  1022. && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
  1023. xmlRelaxNGStatesPtr *tmp;
  1024. tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
  1025. 2 * ctxt->freeStatesMax *
  1026. sizeof
  1027. (xmlRelaxNGStatesPtr));
  1028. if (tmp == NULL) {
  1029. xmlRngVErrMemory(ctxt, "storing states\n");
  1030. xmlFree(states->tabState);
  1031. xmlFree(states);
  1032. return;
  1033. }
  1034. ctxt->freeStates = tmp;
  1035. ctxt->freeStatesMax *= 2;
  1036. }
  1037. if ((ctxt == NULL) || (ctxt->freeStates == NULL)) {
  1038. xmlFree(states->tabState);
  1039. xmlFree(states);
  1040. } else {
  1041. ctxt->freeStates[ctxt->freeStatesNr++] = states;
  1042. }
  1043. }
  1044. /**
  1045. * xmlRelaxNGNewValidState:
  1046. * @ctxt: a Relax-NG validation context
  1047. * @node: the current node or NULL for the document
  1048. *
  1049. * Allocate a new RelaxNG validation state
  1050. *
  1051. * Returns the newly allocated structure or NULL in case or error
  1052. */
  1053. static xmlRelaxNGValidStatePtr
  1054. xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
  1055. {
  1056. xmlRelaxNGValidStatePtr ret;
  1057. xmlAttrPtr attr;
  1058. xmlAttrPtr attrs[MAX_ATTR];
  1059. int nbAttrs = 0;
  1060. xmlNodePtr root = NULL;
  1061. if (node == NULL) {
  1062. root = xmlDocGetRootElement(ctxt->doc);
  1063. if (root == NULL)
  1064. return (NULL);
  1065. } else {
  1066. attr = node->properties;
  1067. while (attr != NULL) {
  1068. if (nbAttrs < MAX_ATTR)
  1069. attrs[nbAttrs++] = attr;
  1070. else
  1071. nbAttrs++;
  1072. attr = attr->next;
  1073. }
  1074. }
  1075. if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
  1076. ctxt->freeState->nbState--;
  1077. ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
  1078. } else {
  1079. ret =
  1080. (xmlRelaxNGValidStatePtr)
  1081. xmlMalloc(sizeof(xmlRelaxNGValidState));
  1082. if (ret == NULL) {
  1083. xmlRngVErrMemory(ctxt, "allocating states\n");
  1084. return (NULL);
  1085. }
  1086. memset(ret, 0, sizeof(xmlRelaxNGValidState));
  1087. }
  1088. ret->value = NULL;
  1089. ret->endvalue = NULL;
  1090. if (node == NULL) {
  1091. ret->node = (xmlNodePtr) ctxt->doc;
  1092. ret->seq = root;
  1093. } else {
  1094. ret->node = node;
  1095. ret->seq = node->children;
  1096. }
  1097. ret->nbAttrs = 0;
  1098. if (nbAttrs > 0) {
  1099. if (ret->attrs == NULL) {
  1100. if (nbAttrs < 4)
  1101. ret->maxAttrs = 4;
  1102. else
  1103. ret->maxAttrs = nbAttrs;
  1104. ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
  1105. sizeof(xmlAttrPtr));
  1106. if (ret->attrs == NULL) {
  1107. xmlRngVErrMemory(ctxt, "allocating states\n");
  1108. return (ret);
  1109. }
  1110. } else if (ret->maxAttrs < nbAttrs) {
  1111. xmlAttrPtr *tmp;
  1112. tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
  1113. sizeof(xmlAttrPtr));
  1114. if (tmp == NULL) {
  1115. xmlRngVErrMemory(ctxt, "allocating states\n");
  1116. return (ret);
  1117. }
  1118. ret->attrs = tmp;
  1119. ret->maxAttrs = nbAttrs;
  1120. }
  1121. ret->nbAttrs = nbAttrs;
  1122. if (nbAttrs < MAX_ATTR) {
  1123. memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
  1124. } else {
  1125. attr = node->properties;
  1126. nbAttrs = 0;
  1127. while (attr != NULL) {
  1128. ret->attrs[nbAttrs++] = attr;
  1129. attr = attr->next;
  1130. }
  1131. }
  1132. }
  1133. ret->nbAttrLeft = ret->nbAttrs;
  1134. return (ret);
  1135. }
  1136. /**
  1137. * xmlRelaxNGCopyValidState:
  1138. * @ctxt: a Relax-NG validation context
  1139. * @state: a validation state
  1140. *
  1141. * Copy the validation state
  1142. *
  1143. * Returns the newly allocated structure or NULL in case or error
  1144. */
  1145. static xmlRelaxNGValidStatePtr
  1146. xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
  1147. xmlRelaxNGValidStatePtr state)
  1148. {
  1149. xmlRelaxNGValidStatePtr ret;
  1150. unsigned int maxAttrs;
  1151. xmlAttrPtr *attrs;
  1152. if (state == NULL)
  1153. return (NULL);
  1154. if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
  1155. ctxt->freeState->nbState--;
  1156. ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
  1157. } else {
  1158. ret =
  1159. (xmlRelaxNGValidStatePtr)
  1160. xmlMalloc(sizeof(xmlRelaxNGValidState));
  1161. if (ret == NULL) {
  1162. xmlRngVErrMemory(ctxt, "allocating states\n");
  1163. return (NULL);
  1164. }
  1165. memset(ret, 0, sizeof(xmlRelaxNGValidState));
  1166. }
  1167. attrs = ret->attrs;
  1168. maxAttrs = ret->maxAttrs;
  1169. memcpy(ret, state, sizeof(xmlRelaxNGValidState));
  1170. ret->attrs = attrs;
  1171. ret->maxAttrs = maxAttrs;
  1172. if (state->nbAttrs > 0) {
  1173. if (ret->attrs == NULL) {
  1174. ret->maxAttrs = state->maxAttrs;
  1175. ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
  1176. sizeof(xmlAttrPtr));
  1177. if (ret->attrs == NULL) {
  1178. xmlRngVErrMemory(ctxt, "allocating states\n");
  1179. ret->nbAttrs = 0;
  1180. return (ret);
  1181. }
  1182. } else if (ret->maxAttrs < state->nbAttrs) {
  1183. xmlAttrPtr *tmp;
  1184. tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
  1185. sizeof(xmlAttrPtr));
  1186. if (tmp == NULL) {
  1187. xmlRngVErrMemory(ctxt, "allocating states\n");
  1188. ret->nbAttrs = 0;
  1189. return (ret);
  1190. }
  1191. ret->maxAttrs = state->maxAttrs;
  1192. ret->attrs = tmp;
  1193. }
  1194. memcpy(ret->attrs, state->attrs,
  1195. state->nbAttrs * sizeof(xmlAttrPtr));
  1196. }
  1197. return (ret);
  1198. }
  1199. /**
  1200. * xmlRelaxNGEqualValidState:
  1201. * @ctxt: a Relax-NG validation context
  1202. * @state1: a validation state
  1203. * @state2: a validation state
  1204. *
  1205. * Compare the validation states for equality
  1206. *
  1207. * Returns 1 if equald, 0 otherwise
  1208. */
  1209. static int
  1210. xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
  1211. xmlRelaxNGValidStatePtr state1,
  1212. xmlRelaxNGValidStatePtr state2)
  1213. {
  1214. int i;
  1215. if ((state1 == NULL) || (state2 == NULL))
  1216. return (0);
  1217. if (state1 == state2)
  1218. return (1);
  1219. if (state1->node != state2->node)
  1220. return (0);
  1221. if (state1->seq != state2->seq)
  1222. return (0);
  1223. if (state1->nbAttrLeft != state2->nbAttrLeft)
  1224. return (0);
  1225. if (state1->nbAttrs != state2->nbAttrs)
  1226. return (0);
  1227. if (state1->endvalue != state2->endvalue)
  1228. return (0);
  1229. if ((state1->value != state2->value) &&
  1230. (!xmlStrEqual(state1->value, state2->value)))
  1231. return (0);
  1232. for (i = 0; i < state1->nbAttrs; i++) {
  1233. if (state1->attrs[i] != state2->attrs[i])
  1234. return (0);
  1235. }
  1236. return (1);
  1237. }
  1238. /**
  1239. * xmlRelaxNGFreeValidState:
  1240. * @state: a validation state structure
  1241. *
  1242. * Deallocate a RelaxNG validation state structure.
  1243. */
  1244. static void
  1245. xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
  1246. xmlRelaxNGValidStatePtr state)
  1247. {
  1248. if (state == NULL)
  1249. return;
  1250. if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
  1251. ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
  1252. }
  1253. if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
  1254. if (state->attrs != NULL)
  1255. xmlFree(state->attrs);
  1256. xmlFree(state);
  1257. } else {
  1258. xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
  1259. }
  1260. }
  1261. /************************************************************************
  1262. * *
  1263. * Semi internal functions *
  1264. * *
  1265. ************************************************************************/
  1266. /**
  1267. * xmlRelaxParserSetFlag:
  1268. * @ctxt: a RelaxNG parser context
  1269. * @flags: a set of flags values
  1270. *
  1271. * Semi private function used to pass informations to a parser context
  1272. * which are a combination of xmlRelaxNGParserFlag .
  1273. *
  1274. * Returns 0 if success and -1 in case of error
  1275. */
  1276. int
  1277. xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags)
  1278. {
  1279. if (ctxt == NULL) return(-1);
  1280. if (flags & XML_RELAXNGP_FREE_DOC) {
  1281. ctxt->crng |= XML_RELAXNGP_FREE_DOC;
  1282. flags -= XML_RELAXNGP_FREE_DOC;
  1283. }
  1284. if (flags & XML_RELAXNGP_CRNG) {
  1285. ctxt->crng |= XML_RELAXNGP_CRNG;
  1286. flags -= XML_RELAXNGP_CRNG;
  1287. }
  1288. if (flags != 0) return(-1);
  1289. return(0);
  1290. }
  1291. /************************************************************************
  1292. * *
  1293. * Document functions *
  1294. * *
  1295. ************************************************************************/
  1296. static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
  1297. xmlDocPtr doc);
  1298. /**
  1299. * xmlRelaxNGIncludePush:
  1300. * @ctxt: the parser context
  1301. * @value: the element doc
  1302. *
  1303. * Pushes a new include on top of the include stack
  1304. *
  1305. * Returns 0 in case of error, the index in the stack otherwise
  1306. */
  1307. static int
  1308. xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
  1309. xmlRelaxNGIncludePtr value)
  1310. {
  1311. if (ctxt->incTab == NULL) {
  1312. ctxt->incMax = 4;
  1313. ctxt->incNr = 0;
  1314. ctxt->incTab =
  1315. (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax *
  1316. sizeof(ctxt->incTab[0]));
  1317. if (ctxt->incTab == NULL) {
  1318. xmlRngPErrMemory(ctxt, "allocating include\n");
  1319. return (0);
  1320. }
  1321. }
  1322. if (ctxt->incNr >= ctxt->incMax) {
  1323. ctxt->incMax *= 2;
  1324. ctxt->incTab =
  1325. (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
  1326. ctxt->incMax *
  1327. sizeof(ctxt->incTab[0]));
  1328. if (ctxt->incTab == NULL) {
  1329. xmlRngPErrMemory(ctxt, "allocating include\n");
  1330. return (0);
  1331. }
  1332. }
  1333. ctxt->incTab[ctxt->incNr] = value;
  1334. ctxt->inc = value;
  1335. return (ctxt->incNr++);
  1336. }
  1337. /**
  1338. * xmlRelaxNGIncludePop:
  1339. * @ctxt: the parser context
  1340. *
  1341. * Pops the top include from the include stack
  1342. *
  1343. * Returns the include just removed
  1344. */
  1345. static xmlRelaxNGIncludePtr
  1346. xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
  1347. {
  1348. xmlRelaxNGIncludePtr ret;
  1349. if (ctxt->incNr <= 0)
  1350. return (NULL);
  1351. ctxt->incNr--;
  1352. if (ctxt->incNr > 0)
  1353. ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
  1354. else
  1355. ctxt->inc = NULL;
  1356. ret = ctxt->incTab[ctxt->incNr];
  1357. ctxt->incTab[ctxt->incNr] = NULL;
  1358. return (ret);
  1359. }
  1360. /**
  1361. * xmlRelaxNGRemoveRedefine:
  1362. * @ctxt: the parser context
  1363. * @URL: the normalized URL
  1364. * @target: the included target
  1365. * @name: the define name to eliminate
  1366. *
  1367. * Applies the elimination algorithm of 4.7
  1368. *
  1369. * Returns 0 in case of error, 1 in case of success.
  1370. */
  1371. static int
  1372. xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
  1373. const xmlChar * URL ATTRIBUTE_UNUSED,
  1374. xmlNodePtr target, const xmlChar * name)
  1375. {
  1376. int found = 0;
  1377. xmlNodePtr tmp, tmp2;
  1378. xmlChar *name2;
  1379. #ifdef DEBUG_INCLUDE
  1380. if (name == NULL)
  1381. xmlGenericError(xmlGenericErrorContext,
  1382. "Elimination of <include> start from %s\n", URL);
  1383. else
  1384. xmlGenericError(xmlGenericErrorContext,
  1385. "Elimination of <include> define %s from %s\n",
  1386. name, URL);
  1387. #endif
  1388. tmp = target;
  1389. while (tmp != NULL) {
  1390. tmp2 = tmp->next;
  1391. if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
  1392. found = 1;
  1393. xmlUnlinkNode(tmp);
  1394. xmlFreeNode(tmp);
  1395. } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
  1396. name2 = xmlGetProp(tmp, BAD_CAST "name");
  1397. xmlRelaxNGNormExtSpace(name2);
  1398. if (name2 != NULL) {
  1399. if (xmlStrEqual(name, name2)) {
  1400. found = 1;
  1401. xmlUnlinkNode(tmp);
  1402. xmlFreeNode(tmp);
  1403. }
  1404. xmlFree(name2);
  1405. }
  1406. } else if (IS_RELAXNG(tmp, "include")) {
  1407. xmlChar *href = NULL;
  1408. xmlRelaxNGDocumentPtr inc = tmp->psvi;
  1409. if ((inc != NULL) && (inc->doc != NULL) &&
  1410. (inc->doc->children != NULL)) {
  1411. if (xmlStrEqual
  1412. (inc->doc->children->name, BAD_CAST "grammar")) {
  1413. #ifdef DEBUG_INCLUDE
  1414. href = xmlGetProp(tmp, BAD_CAST "href");
  1415. #endif
  1416. if (xmlRelaxNGRemoveRedefine(ctxt, href,
  1417. inc->doc->children->
  1418. children, name) == 1) {
  1419. found = 1;
  1420. }
  1421. #ifdef DEBUG_INCLUDE
  1422. if (href != NULL)
  1423. xmlFree(href);
  1424. #endif
  1425. }
  1426. }
  1427. }
  1428. tmp = tmp2;
  1429. }
  1430. return (found);
  1431. }
  1432. /**
  1433. * xmlRelaxNGLoadInclude:
  1434. * @ctxt: the parser context
  1435. * @URL: the normalized URL
  1436. * @node: the include node.
  1437. * @ns: the namespace passed from the context.
  1438. *
  1439. * First lookup if the document is already loaded into the parser context,
  1440. * check against recursion. If not found the resource is loaded and
  1441. * the content is preprocessed before being returned back to the caller.
  1442. *
  1443. * Returns the xmlRelaxNGIncludePtr or NULL in case of error
  1444. */
  1445. static xmlRelaxNGIncludePtr
  1446. xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL,
  1447. xmlNodePtr node, const xmlChar * ns)
  1448. {
  1449. xmlRelaxNGIncludePtr ret = NULL;
  1450. xmlDocPtr doc;
  1451. int i;
  1452. xmlNodePtr root, cur;
  1453. #ifdef DEBUG_INCLUDE
  1454. xmlGenericError(xmlGenericErrorContext,
  1455. "xmlRelaxNGLoadInclude(%s)\n", URL);
  1456. #endif
  1457. /*
  1458. * check against recursion in the stack
  1459. */
  1460. for (i = 0; i < ctxt->incNr; i++) {
  1461. if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
  1462. xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE,
  1463. "Detected an Include recursion for %s\n", URL,
  1464. NULL);
  1465. return (NULL);
  1466. }
  1467. }
  1468. /*
  1469. * load the document
  1470. */
  1471. doc = xmlReadFile((const char *) URL,NULL,0);
  1472. if (doc == NULL) {
  1473. xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR,
  1474. "xmlRelaxNG: could not load %s\n", URL, NULL);
  1475. return (NULL);
  1476. }
  1477. #ifdef DEBUG_INCLUDE
  1478. xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL);
  1479. #endif
  1480. /*
  1481. * Allocate the document structures and register it first.
  1482. */
  1483. ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
  1484. if (ret == NULL) {
  1485. xmlRngPErrMemory(ctxt, "allocating include\n");
  1486. xmlFreeDoc(doc);
  1487. return (NULL);
  1488. }
  1489. memset(ret, 0, sizeof(xmlRelaxNGInclude));
  1490. ret->doc = doc;
  1491. ret->href = xmlStrdup(URL);
  1492. ret->next = ctxt->includes;
  1493. ctxt->includes = ret;
  1494. /*
  1495. * transmit the ns if needed
  1496. */
  1497. if (ns != NULL) {
  1498. root = xmlDocGetRootElement(doc);
  1499. if (root != NULL) {
  1500. if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
  1501. xmlSetProp(root, BAD_CAST "ns", ns);
  1502. }
  1503. }
  1504. }
  1505. /*
  1506. * push it on the stack
  1507. */
  1508. xmlRelaxNGIncludePush(ctxt, ret);
  1509. /*
  1510. * Some preprocessing of the document content, this include recursing
  1511. * in the include stack.
  1512. */
  1513. #ifdef DEBUG_INCLUDE
  1514. xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL);
  1515. #endif
  1516. doc = xmlRelaxNGCleanupDoc(ctxt, doc);
  1517. if (doc == NULL) {
  1518. ctxt->inc = NULL;
  1519. return (NULL);
  1520. }
  1521. /*
  1522. * Pop up the include from the stack
  1523. */
  1524. xmlRelaxNGIncludePop(ctxt);
  1525. #ifdef DEBUG_INCLUDE
  1526. xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL);
  1527. #endif
  1528. /*
  1529. * Check that the top element is a grammar
  1530. */
  1531. root = xmlDocGetRootElement(doc);
  1532. if (root == NULL) {
  1533. xmlRngPErr(ctxt, node, XML_RNGP_EMPTY,
  1534. "xmlRelaxNG: included document is empty %s\n", URL,
  1535. NULL);
  1536. return (NULL);
  1537. }
  1538. if (!IS_RELAXNG(root, "grammar")) {
  1539. xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
  1540. "xmlRelaxNG: included document %s root is not a grammar\n",
  1541. URL, NULL);
  1542. return (NULL);
  1543. }
  1544. /*
  1545. * Elimination of redefined rules in the include.
  1546. */
  1547. cur = node->children;
  1548. while (cur != NULL) {
  1549. if (IS_RELAXNG(cur, "start")) {
  1550. int found = 0;
  1551. found =
  1552. xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
  1553. if (!found) {
  1554. xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING,
  1555. "xmlRelaxNG: include %s has a start but not the included grammar\n",
  1556. URL, NULL);
  1557. }
  1558. } else if (IS_RELAXNG(cur, "define")) {
  1559. xmlChar *name;
  1560. name = xmlGetProp(cur, BAD_CAST "name");
  1561. if (name == NULL) {
  1562. xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING,
  1563. "xmlRelaxNG: include %s has define without name\n",
  1564. URL, NULL);
  1565. } else {
  1566. int found;
  1567. xmlRelaxNGNormExtSpace(name);
  1568. found = xmlRelaxNGRemoveRedefine(ctxt, URL,
  1569. root->children, name);
  1570. if (!found) {
  1571. xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING,
  1572. "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
  1573. URL, name);
  1574. }
  1575. xmlFree(name);
  1576. }
  1577. }
  1578. cur = cur->next;
  1579. }
  1580. return (ret);
  1581. }
  1582. /**
  1583. * xmlRelaxNGValidErrorPush:
  1584. * @ctxt: the validation context
  1585. * @err: the error code
  1586. * @arg1: the first string argument
  1587. * @arg2: the second string argument
  1588. * @dup: arg need to be duplicated
  1589. *
  1590. * Pushes a new error on top of the error stack
  1591. *
  1592. * Returns 0 in case of error, the index in the stack otherwise
  1593. */
  1594. static int
  1595. xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt,
  1596. xmlRelaxNGValidErr err, const xmlChar * arg1,
  1597. const xmlChar * arg2, int dup)
  1598. {
  1599. xmlRelaxNGValidErrorPtr cur;
  1600. #ifdef DEBUG_ERROR
  1601. xmlGenericError(xmlGenericErrorContext,
  1602. "Pushing error %d at %d on stack\n", err, ctxt->errNr);
  1603. #endif
  1604. if (ctxt->errTab == NULL) {
  1605. ctxt->errMax = 8;
  1606. ctxt->errNr = 0;
  1607. ctxt->errTab =
  1608. (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax *
  1609. sizeof
  1610. (xmlRelaxNGValidError));
  1611. if (ctxt->errTab == NULL) {
  1612. xmlRngVErrMemory(ctxt, "pushing error\n");
  1613. return (0);
  1614. }
  1615. ctxt->err = NULL;
  1616. }
  1617. if (ctxt->errNr >= ctxt->errMax) {
  1618. ctxt->errMax *= 2;
  1619. ctxt->errTab =
  1620. (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
  1621. ctxt->errMax *
  1622. sizeof
  1623. (xmlRelaxNGValidError));
  1624. if (ctxt->errTab == NULL) {
  1625. xmlRngVErrMemory(ctxt, "pushing error\n");
  1626. return (0);
  1627. }
  1628. ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
  1629. }
  1630. if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
  1631. (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err))
  1632. return (ctxt->errNr);
  1633. cur = &ctxt->errTab[ctxt->errNr];
  1634. cur->err = err;
  1635. if (dup) {
  1636. cur->arg1 = xmlStrdup(arg1);
  1637. cur->arg2 = xmlStrdup(arg2);
  1638. cur->flags = ERROR_IS_DUP;
  1639. } else {
  1640. cur->arg1 = arg1;
  1641. cur->arg2 = arg2;
  1642. cur->flags = 0;
  1643. }
  1644. if (ctxt->state != NULL) {
  1645. cur->node = ctxt->state->node;
  1646. cur->seq = ctxt->state->seq;
  1647. } else {
  1648. cur->node = NULL;
  1649. cur->seq = NULL;
  1650. }
  1651. ctxt->err = cur;
  1652. return (ctxt->errNr++);
  1653. }
  1654. /**
  1655. * xmlRelaxNGValidErrorPop:
  1656. * @ctxt: the validation context
  1657. *
  1658. * Pops the top error from the error stack
  1659. */
  1660. static void
  1661. xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
  1662. {
  1663. xmlRelaxNGValidErrorPtr cur;
  1664. if (ctxt->errNr <= 0) {
  1665. ctxt->err = NULL;
  1666. return;
  1667. }
  1668. ctxt->errNr--;
  1669. if (ctxt->errNr > 0)
  1670. ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
  1671. else
  1672. ctxt->err = NULL;
  1673. cur = &ctxt->errTab[ctxt->errNr];
  1674. if (cur->flags & ERROR_IS_DUP) {
  1675. if (cur->arg1 != NULL)
  1676. xmlFree((xmlChar *) cur->arg1);
  1677. cur->arg1 = NULL;
  1678. if (cur->arg2 != NULL)
  1679. xmlFree((xmlChar *) cur->arg2);
  1680. cur->arg2 = NULL;
  1681. cur->flags = 0;
  1682. }
  1683. }
  1684. /**
  1685. * xmlRelaxNGDocumentPush:
  1686. * @ctxt: the parser context
  1687. * @value: the element doc
  1688. *
  1689. * Pushes a new doc on top of the doc stack
  1690. *
  1691. * Returns 0 in case of error, the index in the stack otherwise
  1692. */
  1693. static int
  1694. xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
  1695. xmlRelaxNGDocumentPtr value)
  1696. {
  1697. if (ctxt->docTab == NULL) {
  1698. ctxt->docMax = 4;
  1699. ctxt->docNr = 0;
  1700. ctxt->docTab =
  1701. (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax *
  1702. sizeof(ctxt->docTab[0]));
  1703. if (ctxt->docTab == NULL) {
  1704. xmlRngPErrMemory(ctxt, "adding document\n");
  1705. return (0);
  1706. }
  1707. }
  1708. if (ctxt->docNr >= ctxt->docMax) {
  1709. ctxt->docMax *= 2;
  1710. ctxt->docTab =
  1711. (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
  1712. ctxt->docMax *
  1713. sizeof(ctxt->docTab[0]));
  1714. if (ctxt->docTab == NULL) {
  1715. xmlRngPErrMemory(ctxt, "adding document\n");
  1716. return (0);
  1717. }
  1718. }
  1719. ctxt->docTab[ctxt->docNr] = value;
  1720. ctxt->doc = value;
  1721. return (ctxt->docNr++);
  1722. }
  1723. /**
  1724. * xmlRelaxNGDocumentPop:
  1725. * @ctxt: the parser context
  1726. *
  1727. * Pops the top doc from the doc stack
  1728. *
  1729. * Returns the doc just removed
  1730. */
  1731. static xmlRelaxNGDocumentPtr
  1732. xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
  1733. {
  1734. xmlRelaxNGDocumentPtr ret;
  1735. if (ctxt->docNr <= 0)
  1736. return (NULL);
  1737. ctxt->docNr--;
  1738. if (ctxt->docNr > 0)
  1739. ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
  1740. else
  1741. ctxt->doc = NULL;
  1742. ret = ctxt->docTab[ctxt->docNr];
  1743. ctxt->docTab[ctxt->docNr] = NULL;
  1744. return (ret);
  1745. }
  1746. /**
  1747. * xmlRelaxNGLoadExternalRef:
  1748. * @ctxt: the parser context
  1749. * @URL: the normalized URL
  1750. * @ns: the inherited ns if any
  1751. *
  1752. * First lookup if the document is already loaded into the parser context,
  1753. * check against recursion. If not found the resource is loaded and
  1754. * the content is preprocessed before being returned back to the caller.
  1755. *
  1756. * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
  1757. */
  1758. static xmlRelaxNGDocumentPtr
  1759. xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt,
  1760. const xmlChar * URL, const xmlChar * ns)
  1761. {
  1762. xmlRelaxNGDocumentPtr ret = NULL;
  1763. xmlDocPtr doc;
  1764. xmlNodePtr root;
  1765. int i;
  1766. /*
  1767. * check against recursion in the stack
  1768. */
  1769. for (i = 0; i < ctxt->docNr; i++) {
  1770. if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
  1771. xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE,
  1772. "Detected an externalRef recursion for %s\n", URL,
  1773. NULL);
  1774. return (NULL);
  1775. }
  1776. }
  1777. /*
  1778. * load the document
  1779. */
  1780. doc = xmlReadFile((const char *) URL,NULL,0);
  1781. if (doc == NULL) {
  1782. xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
  1783. "xmlRelaxNG: could not load %s\n", URL, NULL);
  1784. return (NULL);
  1785. }
  1786. /*
  1787. * Allocate the document structures and register it first.
  1788. */
  1789. ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
  1790. if (ret == NULL) {
  1791. xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY,
  1792. "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL);
  1793. xmlFreeDoc(doc);
  1794. return (NULL);
  1795. }
  1796. memset(ret, 0, sizeof(xmlRelaxNGDocument));
  1797. ret->doc = doc;
  1798. ret->href = xmlStrdup(URL);
  1799. ret->next = ctxt->documents;
  1800. ret->externalRef = 1;
  1801. ctxt->documents = ret;
  1802. /*
  1803. * transmit the ns if needed
  1804. */
  1805. if (ns != NULL) {
  1806. root = xmlDocGetRootElement(doc);
  1807. if (root != NULL) {
  1808. if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
  1809. xmlSetProp(root, BAD_CAST "ns", ns);
  1810. }
  1811. }
  1812. }
  1813. /*
  1814. * push it on the stack and register it in the hash table
  1815. */
  1816. xmlRelaxNGDocumentPush(ctxt, ret);
  1817. /*
  1818. * Some preprocessing of the document content
  1819. */
  1820. doc = xmlRelaxNGCleanupDoc(ctxt, doc);
  1821. if (doc == NULL) {
  1822. ctxt->doc = NULL;
  1823. return (NULL);
  1824. }
  1825. xmlRelaxNGDocumentPop(ctxt);
  1826. return (ret);
  1827. }
  1828. /************************************************************************
  1829. * *
  1830. * Error functions *
  1831. * *
  1832. ************************************************************************/
  1833. #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
  1834. #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
  1835. #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
  1836. #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
  1837. #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
  1838. static const char *
  1839. xmlRelaxNGDefName(xmlRelaxNGDefinePtr def)
  1840. {
  1841. if (def == NULL)
  1842. return ("none");
  1843. switch (def->type) {
  1844. case XML_RELAXNG_EMPTY:
  1845. return ("empty");
  1846. case XML_RELAXNG_NOT_ALLOWED:
  1847. return ("notAllowed");
  1848. case XML_RELAXNG_EXCEPT:
  1849. return ("except");
  1850. case XML_RELAXNG_TEXT:
  1851. return ("text");
  1852. case XML_RELAXNG_ELEMENT:
  1853. return ("element");
  1854. case XML_RELAXNG_DATATYPE:
  1855. return ("datatype");
  1856. case XML_RELAXNG_VALUE:
  1857. return ("value");
  1858. case XML_RELAXNG_LIST:
  1859. return ("list");
  1860. case XML_RELAXNG_ATTRIBUTE:
  1861. return ("attribute");
  1862. case XML_RELAXNG_DEF:
  1863. return ("def");
  1864. case XML_RELAXNG_REF:
  1865. return ("ref");
  1866. case XML_RELAXNG_EXTERNALREF:
  1867. return ("externalRef");
  1868. case XML_RELAXNG_PARENTREF:
  1869. return ("parentRef");
  1870. case XML_RELAXNG_OPTIONAL:
  1871. return ("optional");
  1872. case XML_RELAXNG_ZEROORMORE:
  1873. return ("zeroOrMore");
  1874. case XML_RELAXNG_ONEORMORE:
  1875. return ("oneOrMore");
  1876. case XML_RELAXNG_CHOICE:
  1877. return ("choice");
  1878. case XML_RELAXNG_GROUP:
  1879. return ("group");
  1880. case XML_RELAXNG_INTERLEAVE:
  1881. return ("interleave");
  1882. case XML_RELAXNG_START:
  1883. return ("start");
  1884. case XML_RELAXNG_NOOP:
  1885. return ("noop");
  1886. case XML_RELAXNG_PARAM:
  1887. return ("param");
  1888. }
  1889. return ("unknown");
  1890. }
  1891. /**
  1892. * xmlRelaxNGGetErrorString:
  1893. * @err: the error code
  1894. * @arg1: the first string argument
  1895. * @arg2: the second string argument
  1896. *
  1897. * computes a formatted error string for the given error code and args
  1898. *
  1899. * Returns the error string, it must be deallocated by the caller
  1900. */
  1901. static xmlChar *
  1902. xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1,
  1903. const xmlChar * arg2)
  1904. {
  1905. char msg[1000];
  1906. if (arg1 == NULL)
  1907. arg1 = BAD_CAST "";
  1908. if (arg2 == NULL)
  1909. arg2 = BAD_CAST "";
  1910. msg[0] = 0;
  1911. switch (err) {
  1912. case XML_RELAXNG_OK:
  1913. return (NULL);
  1914. case XML_RELAXNG_ERR_MEMORY:
  1915. return (xmlCharStrdup("out of memory\n"));
  1916. case XML_RELAXNG_ERR_TYPE:
  1917. snprintf(msg, 1000, "failed to validate type %s\n", arg1);
  1918. break;
  1919. case XML_RELAXNG_ERR_TYPEVAL:
  1920. snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1,
  1921. arg2);
  1922. break;
  1923. case XML_RELAXNG_ERR_DUPID:
  1924. snprintf(msg, 1000, "ID %s redefined\n", arg1);
  1925. break;
  1926. case XML_RELAXNG_ERR_TYPECMP:
  1927. snprintf(msg, 1000, "failed to compare type %s\n", arg1);
  1928. break;
  1929. case XML_RELAXNG_ERR_NOSTATE:
  1930. return (xmlCharStrdup("Internal error: no state\n"));
  1931. case XML_RELAXNG_ERR_NODEFINE:
  1932. return (xmlCharStrdup("Internal error: no define\n"));
  1933. case XML_RELAXNG_ERR_INTERNAL:
  1934. snprintf(msg, 1000, "Internal error: %s\n", arg1);
  1935. break;
  1936. case XML_RELAXNG_ERR_LISTEXTRA:
  1937. snprintf(msg, 1000, "Extra data in list: %s\n", arg1);
  1938. break;
  1939. case XML_RELAXNG_ERR_INTERNODATA:
  1940. return (xmlCharStrdup
  1941. ("Internal: interleave block has no data\n"));
  1942. case XML_RELAXNG_ERR_INTERSEQ:
  1943. return (xmlCharStrdup("Invalid sequence in interleave\n"));
  1944. case XML_RELAXNG_ERR_INTEREXTRA:
  1945. snprintf(msg, 1000, "Extra element %s in interleave\n", arg1);
  1946. break;
  1947. case XML_RELAXNG_ERR_ELEMNAME:
  1948. snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1,
  1949. arg2);
  1950. break;
  1951. case XML_RELAXNG_ERR_ELEMNONS:
  1952. snprintf(msg, 1000, "Expecting a namespace for element %s\n",
  1953. arg1);
  1954. break;
  1955. case XML_RELAXNG_ERR_ELEMWRONGNS:
  1956. snprintf(msg, 1000,
  1957. "Element %s has wrong namespace: expecting %s\n", arg1,
  1958. arg2);
  1959. break;
  1960. case XML_RELAXNG_ERR_ELEMWRONG:
  1961. snprintf(msg, 1000, "Did not expect element %s there\n", arg1);
  1962. break;
  1963. case XML_RELAXNG_ERR_TEXTWRONG:
  1964. snprintf(msg, 1000,
  1965. "Did not expect text in element %s content\n", arg1);
  1966. break;
  1967. case XML_RELAXNG_ERR_ELEMEXTRANS:
  1968. snprintf(msg, 1000, "Expecting no namespace for element %s\n",
  1969. arg1);
  1970. break;
  1971. case XML_RELAXNG_ERR_ELEMNOTEMPTY:
  1972. snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1);
  1973. break;
  1974. case XML_RELAXNG_ERR_NOELEM:
  1975. snprintf(msg, 1000, "Expecting an element %s, got nothing\n",
  1976. arg1);
  1977. break;
  1978. case XML_RELAXNG_ERR_NOTELEM:
  1979. return (xmlCharStrdup("Expecting an element got text\n"));
  1980. case XML_RELAXNG_ERR_ATTRVALID:
  1981. snprintf(msg, 1000, "Element %s failed to validate attributes\n",
  1982. arg1);
  1983. break;
  1984. case XML_RELAXNG_ERR_CONTENTVALID:
  1985. snprintf(msg, 1000, "Element %s failed to validate content\n",
  1986. arg1);
  1987. break;
  1988. case XML_RELAXNG_ERR_EXTRACONTENT:
  1989. snprintf(msg, 1000, "Element %s has extra content: %s\n",
  1990. arg1, arg2);
  1991. break;
  1992. case XML_RELAXNG_ERR_INVALIDATTR:
  1993. snprintf(msg, 1000, "Invalid attribute %s for element %s\n",
  1994. arg1, arg2);
  1995. break;
  1996. case XML_RELAXNG_ERR_LACKDATA:
  1997. snprintf(msg, 1000, "Datatype element %s contains no data\n",
  1998. arg1);
  1999. break;
  2000. case XML_RELAXNG_ERR_DATAELEM:
  2001. snprintf(msg, 1000, "Datatype element %s has child elements\n",
  2002. arg1);
  2003. break;
  2004. case XML_RELAXNG_ERR_VALELEM:
  2005. snprintf(msg, 1000, "Value element %s has child elements\n",
  2006. arg1);
  2007. break;
  2008. case XML_RELAXNG_ERR_LISTELEM:
  2009. snprintf(msg, 1000, "List element %s has child elements\n",
  2010. arg1);
  2011. break;
  2012. case XML_RELAXNG_ERR_DATATYPE:
  2013. snprintf(msg, 1000, "Error validating datatype %s\n", arg1);
  2014. break;
  2015. case XML_RELAXNG_ERR_VALUE:
  2016. snprintf(msg, 1000, "Error validating value %s\n", arg1);
  2017. break;
  2018. case XML_RELAXNG_ERR_LIST:
  2019. return (xmlCharStrdup("Error validating list\n"));
  2020. case XML_RELAXNG_ERR_NOGRAMMAR:
  2021. return (xmlCharStrdup("No top grammar defined\n"));
  2022. case XML_RELAXNG_ERR_EXTRADATA:
  2023. return (xmlCharStrdup("Extra data in the document\n"));
  2024. default:
  2025. return (xmlCharStrdup("Unknown error !\n"));
  2026. }
  2027. if (msg[0] == 0) {
  2028. snprintf(msg, 1000, "Unknown error code %d\n", err);
  2029. }
  2030. msg[1000 - 1] = 0;
  2031. return (xmlStrdup((xmlChar *) msg));
  2032. }
  2033. /**
  2034. * xmlRelaxNGShowValidError:
  2035. * @ctxt: the validation context
  2036. * @err: the error number
  2037. * @node: the node
  2038. * @child: the node child generating the problem.
  2039. * @arg1: the first argument
  2040. * @arg2: the second argument
  2041. *
  2042. * Show a validation error.
  2043. */
  2044. static void
  2045. xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt,
  2046. xmlRelaxNGValidErr err, xmlNodePtr node,
  2047. xmlNodePtr child, const xmlChar * arg1,
  2048. const xmlChar * arg2)
  2049. {
  2050. xmlChar *msg;
  2051. if (ctxt->flags & FLAGS_NOERROR)
  2052. return;
  2053. #ifdef DEBUG_ERROR
  2054. xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err);
  2055. #endif
  2056. msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
  2057. if (msg == NULL)
  2058. return;
  2059. if (ctxt->errNo == XML_RELAXNG_OK)
  2060. ctxt->errNo = err;
  2061. xmlRngVErr(ctxt, (child == NULL ? node : child), err,
  2062. (const char *) msg, arg1, arg2);
  2063. xmlFree(msg);
  2064. }
  2065. /**
  2066. * xmlRelaxNGPopErrors:
  2067. * @ctxt: the validation context
  2068. * @level: the error level in the stack
  2069. *
  2070. * pop and discard all errors until the given level is reached
  2071. */
  2072. static void
  2073. xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level)
  2074. {
  2075. int i;
  2076. xmlRelaxNGValidErrorPtr err;
  2077. #ifdef DEBUG_ERROR
  2078. xmlGenericError(xmlGenericErrorContext,
  2079. "Pop errors till level %d\n", level);
  2080. #endif
  2081. for (i = level; i < ctxt->errNr; i++) {
  2082. err = &ctxt->errTab[i];
  2083. if (err->flags & ERROR_IS_DUP) {
  2084. if (err->arg1 != NULL)
  2085. xmlFree((xmlChar *) err->arg1);
  2086. err->arg1 = NULL;
  2087. if (err->arg2 != NULL)
  2088. xmlFree((xmlChar *) err->arg2);
  2089. err->arg2 = NULL;
  2090. err->flags = 0;
  2091. }
  2092. }
  2093. ctxt->errNr = level;
  2094. if (ctxt->errNr <= 0)
  2095. ctxt->err = NULL;
  2096. }
  2097. /**
  2098. * xmlRelaxNGDumpValidError:
  2099. * @ctxt: the validation context
  2100. *
  2101. * Show all validation error over a given index.
  2102. */
  2103. static void
  2104. xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt)
  2105. {
  2106. int i, j, k;
  2107. xmlRelaxNGValidErrorPtr err, dup;
  2108. #ifdef DEBUG_ERROR
  2109. xmlGenericError(xmlGenericErrorContext,
  2110. "Dumping error stack %d errors\n", ctxt->errNr);
  2111. #endif
  2112. for (i = 0, k = 0; i < ctxt->errNr; i++) {
  2113. err = &ctxt->errTab[i];
  2114. if (k < MAX_ERROR) {
  2115. for (j = 0; j < i; j++) {
  2116. dup = &ctxt->errTab[j];
  2117. if ((err->err == dup->err) && (err->node == dup->node) &&
  2118. (xmlStrEqual(err->arg1, dup->arg1)) &&
  2119. (xmlStrEqual(err->arg2, dup->arg2))) {
  2120. goto skip;
  2121. }
  2122. }
  2123. xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
  2124. err->arg1, err->arg2);
  2125. k++;
  2126. }
  2127. skip:
  2128. if (err->flags & ERROR_IS_DUP) {
  2129. if (err->arg1 != NULL)
  2130. xmlFree((xmlChar *) err->arg1);
  2131. err->arg1 = NULL;
  2132. if (err->arg2 != NULL)
  2133. xmlFree((xmlChar *) err->arg2);
  2134. err->arg2 = NULL;
  2135. err->flags = 0;
  2136. }
  2137. }
  2138. ctxt->errNr = 0;
  2139. }
  2140. /**
  2141. * xmlRelaxNGAddValidError:
  2142. * @ctxt: the validation context
  2143. * @err: the error number
  2144. * @arg1: the first argument
  2145. * @arg2: the second argument
  2146. * @dup: need to dup the args
  2147. *
  2148. * Register a validation error, either generating it if it's sure
  2149. * or stacking it for later handling if unsure.
  2150. */
  2151. static void
  2152. xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt,
  2153. xmlRelaxNGValidErr err, const xmlChar * arg1,
  2154. const xmlChar * arg2, int dup)
  2155. {
  2156. if (ctxt == NULL)
  2157. return;
  2158. if (ctxt->flags & FLAGS_NOERROR)
  2159. return;
  2160. #ifdef DEBUG_ERROR
  2161. xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err);
  2162. #endif
  2163. /*
  2164. * generate the error directly
  2165. */
  2166. if (((ctxt->flags & FLAGS_IGNORABLE) == 0) ||
  2167. (ctxt->flags & FLAGS_NEGATIVE)) {
  2168. xmlNodePtr node, seq;
  2169. /*
  2170. * Flush first any stacked error which might be the
  2171. * real cause of the problem.
  2172. */
  2173. if (ctxt->errNr != 0)
  2174. xmlRelaxNGDumpValidError(ctxt);
  2175. if (ctxt->state != NULL) {
  2176. node = ctxt->state->node;
  2177. seq = ctxt->state->seq;
  2178. } else {
  2179. node = seq = NULL;
  2180. }
  2181. if ((node == NULL) && (seq == NULL)) {
  2182. node = ctxt->pnode;
  2183. }
  2184. xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
  2185. }
  2186. /*
  2187. * Stack the error for later processing if needed
  2188. */
  2189. else {
  2190. xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
  2191. }
  2192. }
  2193. /************************************************************************
  2194. * *
  2195. * Type library hooks *
  2196. * *
  2197. ************************************************************************/
  2198. static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
  2199. const xmlChar * str);
  2200. /**
  2201. * xmlRelaxNGSchemaTypeHave:
  2202. * @data: data needed for the library
  2203. * @type: the type name
  2204. *
  2205. * Check if the given type is provided by
  2206. * the W3C XMLSchema Datatype library.
  2207. *
  2208. * Returns 1 if yes, 0 if no and -1 in case of error.
  2209. */
  2210. static int
  2211. xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type)
  2212. {
  2213. xmlSchemaTypePtr typ;
  2214. if (type == NULL)
  2215. return (-1);
  2216. typ = xmlSchemaGetPredefinedType(type,
  2217. BAD_CAST
  2218. "http://www.w3.org/2001/XMLSchema");
  2219. if (typ == NULL)
  2220. return (0);
  2221. return (1);
  2222. }
  2223. /**
  2224. * xmlRelaxNGSchemaTypeCheck:
  2225. * @data: data needed for the library
  2226. * @type: the type name
  2227. * @value: the value to check
  2228. * @node: the node
  2229. *
  2230. * Check if the given type and value are validated by
  2231. * the W3C XMLSchema Datatype library.
  2232. *
  2233. * Returns 1 if yes, 0 if no and -1 in case of error.
  2234. */
  2235. static int
  2236. xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
  2237. const xmlChar * type,
  2238. const xmlChar * value,
  2239. void **result, xmlNodePtr node)
  2240. {
  2241. xmlSchemaTypePtr typ;
  2242. int ret;
  2243. if ((type == NULL) || (value == NULL))
  2244. return (-1);
  2245. typ = xmlSchemaGetPredefinedType(type,
  2246. BAD_CAST
  2247. "http://www.w3.org/2001/XMLSchema");
  2248. if (typ == NULL)
  2249. return (-1);
  2250. ret = xmlSchemaValPredefTypeNode(typ, value,
  2251. (xmlSchemaValPtr *) result, node);
  2252. if (ret == 2) /* special ID error code */
  2253. return (2);
  2254. if (ret == 0)
  2255. return (1);
  2256. if (ret > 0)
  2257. return (0);
  2258. return (-1);
  2259. }
  2260. /**
  2261. * xmlRelaxNGSchemaFacetCheck:
  2262. * @data: data needed for the library
  2263. * @type: the type name
  2264. * @facet: the facet name
  2265. * @val: the facet value
  2266. * @strval: the string value
  2267. * @value: the value to check
  2268. *
  2269. * Function provided by a type library to check a value facet
  2270. *
  2271. * Returns 1 if yes, 0 if no and -1 in case of error.
  2272. */
  2273. static int
  2274. xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED,
  2275. const xmlChar * type, const xmlChar * facetname,
  2276. const xmlChar * val, const xmlChar * strval,
  2277. void *value)
  2278. {
  2279. xmlSchemaFacetPtr facet;
  2280. xmlSchemaTypePtr typ;
  2281. int ret;
  2282. if ((type == NULL) || (strval == NULL))
  2283. return (-1);
  2284. typ = xmlSchemaGetPredefinedType(type,
  2285. BAD_CAST
  2286. "http://www.w3.org/2001/XMLSchema");
  2287. if (typ == NULL)
  2288. return (-1);
  2289. facet = xmlSchemaNewFacet();
  2290. if (facet == NULL)
  2291. return (-1);
  2292. if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
  2293. facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
  2294. } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
  2295. facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
  2296. } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
  2297. facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
  2298. } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
  2299. facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
  2300. } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
  2301. facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
  2302. } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
  2303. facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
  2304. } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
  2305. facet->type = XML_SCHEMA_FACET_PATTERN;
  2306. } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
  2307. facet->type = XML_SCHEMA_FACET_ENUMERATION;
  2308. } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
  2309. facet->type = XML_SCHEMA_FACET_WHITESPACE;
  2310. } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
  2311. facet->type = XML_SCHEMA_FACET_LENGTH;
  2312. } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
  2313. facet->type = XML_SCHEMA_FACET_MAXLENGTH;
  2314. } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
  2315. facet->type = XML_SCHEMA_FACET_MINLENGTH;
  2316. } else {
  2317. xmlSchemaFreeFacet(facet);
  2318. return (-1);
  2319. }
  2320. facet->value = val;
  2321. ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
  2322. if (ret != 0) {
  2323. xmlSchemaFreeFacet(facet);
  2324. return (-1);
  2325. }
  2326. ret = xmlSchemaValidateFacet(typ, facet, strval, value);
  2327. xmlSchemaFreeFacet(facet);
  2328. if (ret != 0)
  2329. return (-1);
  2330. return (0);
  2331. }
  2332. /**
  2333. * xmlRelaxNGSchemaFreeValue:
  2334. * @data: data needed for the library
  2335. * @value: the value to free
  2336. *
  2337. * Function provided by a type library to free a Schemas value
  2338. *
  2339. * Returns 1 if yes, 0 if no and -1 in case of error.
  2340. */
  2341. static void
  2342. xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value)
  2343. {
  2344. xmlSchemaFreeValue(value);
  2345. }
  2346. /**
  2347. * xmlRelaxNGSchemaTypeCompare:
  2348. * @data: data needed for the library
  2349. * @type: the type name
  2350. * @value1: the first value
  2351. * @value2: the second value
  2352. *
  2353. * Compare two values for equality accordingly a type from the W3C XMLSchema
  2354. * Datatype library.
  2355. *
  2356. * Returns 1 if equal, 0 if no and -1 in case of error.
  2357. */
  2358. static int
  2359. xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
  2360. const xmlChar * type,
  2361. const xmlChar * value1,
  2362. xmlNodePtr ctxt1,
  2363. void *comp1,
  2364. const xmlChar * value2, xmlNodePtr ctxt2)
  2365. {
  2366. int ret;
  2367. xmlSchemaTypePtr typ;
  2368. xmlSchemaValPtr res1 = NULL, res2 = NULL;
  2369. if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
  2370. return (-1);
  2371. typ = xmlSchemaGetPredefinedType(type,
  2372. BAD_CAST
  2373. "http://www.w3.org/2001/XMLSchema");
  2374. if (typ == NULL)
  2375. return (-1);
  2376. if (comp1 == NULL) {
  2377. ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
  2378. if (ret != 0)
  2379. return (-1);
  2380. if (res1 == NULL)
  2381. return (-1);
  2382. } else {
  2383. res1 = (xmlSchemaValPtr) comp1;
  2384. }
  2385. ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
  2386. if (ret != 0) {
  2387. if ((comp1 == NULL) && (res1 != NULL))
  2388. xmlSchemaFreeValue(res1);
  2389. return (-1);
  2390. }
  2391. if (res1 == NULL) {
  2392. return (-1);
  2393. }
  2394. ret = xmlSchemaCompareValues(res1, res2);
  2395. if (res1 != (xmlSchemaValPtr) comp1)
  2396. xmlSchemaFreeValue(res1);
  2397. xmlSchemaFreeValue(res2);
  2398. if (ret == -2)
  2399. return (-1);
  2400. if (ret == 0)
  2401. return (1);
  2402. return (0);
  2403. }
  2404. /**
  2405. * xmlRelaxNGDefaultTypeHave:
  2406. * @data: data needed for the library
  2407. * @type: the type name
  2408. *
  2409. * Check if the given type is provided by
  2410. * the default datatype library.
  2411. *
  2412. * Returns 1 if yes, 0 if no and -1 in case of error.
  2413. */
  2414. static int
  2415. xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED,
  2416. const xmlChar * type)
  2417. {
  2418. if (type == NULL)
  2419. return (-1);
  2420. if (xmlStrEqual(type, BAD_CAST "string"))
  2421. return (1);
  2422. if (xmlStrEqual(type, BAD_CAST "token"))
  2423. return (1);
  2424. return (0);
  2425. }
  2426. /**
  2427. * xmlRelaxNGDefaultTypeCheck:
  2428. * @data: data needed for the library
  2429. * @type: the type name
  2430. * @value: the value to check
  2431. * @node: the node
  2432. *
  2433. * Check if the given type and value are validated by
  2434. * the default datatype library.
  2435. *
  2436. * Returns 1 if yes, 0 if no and -1 in case of error.
  2437. */
  2438. static int
  2439. xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
  2440. const xmlChar * type ATTRIBUTE_UNUSED,
  2441. const xmlChar * value ATTRIBUTE_UNUSED,
  2442. void **result ATTRIBUTE_UNUSED,
  2443. xmlNodePtr node ATTRIBUTE_UNUSED)
  2444. {
  2445. if (value == NULL)
  2446. return (-1);
  2447. if (xmlStrEqual(type, BAD_CAST "string"))
  2448. return (1);
  2449. if (xmlStrEqual(type, BAD_CAST "token")) {
  2450. return (1);
  2451. }
  2452. return (0);
  2453. }
  2454. /**
  2455. * xmlRelaxNGDefaultTypeCompare:
  2456. * @data: data needed for the library
  2457. * @type: the type name
  2458. * @value1: the first value
  2459. * @value2: the second value
  2460. *
  2461. * Compare two values accordingly a type from the default
  2462. * datatype library.
  2463. *
  2464. * Returns 1 if yes, 0 if no and -1 in case of error.
  2465. */
  2466. static int
  2467. xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
  2468. const xmlChar * type,
  2469. const xmlChar * value1,
  2470. xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
  2471. void *comp1 ATTRIBUTE_UNUSED,
  2472. const xmlChar * value2,
  2473. xmlNodePtr ctxt2 ATTRIBUTE_UNUSED)
  2474. {
  2475. int ret = -1;
  2476. if (xmlStrEqual(type, BAD_CAST "string")) {
  2477. ret = xmlStrEqual(value1, value2);
  2478. } else if (xmlStrEqual(type, BAD_CAST "token")) {
  2479. if (!xmlStrEqual(value1, value2)) {
  2480. xmlChar *nval, *nvalue;
  2481. /*
  2482. * TODO: trivial optimizations are possible by
  2483. * computing at compile-time
  2484. */
  2485. nval = xmlRelaxNGNormalize(NULL, value1);
  2486. nvalue = xmlRelaxNGNormalize(NULL, value2);
  2487. if ((nval == NULL) || (nvalue == NULL))
  2488. ret = -1;
  2489. else if (xmlStrEqual(nval, nvalue))
  2490. ret = 1;
  2491. else
  2492. ret = 0;
  2493. if (nval != NULL)
  2494. xmlFree(nval);
  2495. if (nvalue != NULL)
  2496. xmlFree(nvalue);
  2497. } else
  2498. ret = 1;
  2499. }
  2500. return (ret);
  2501. }
  2502. static int xmlRelaxNGTypeInitialized = 0;
  2503. static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
  2504. /**
  2505. * xmlRelaxNGFreeTypeLibrary:
  2506. * @lib: the type library structure
  2507. * @namespace: the URI bound to the library
  2508. *
  2509. * Free the structure associated to the type library
  2510. */
  2511. static void
  2512. xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
  2513. const xmlChar * namespace ATTRIBUTE_UNUSED)
  2514. {
  2515. if (lib == NULL)
  2516. return;
  2517. if (lib->namespace != NULL)
  2518. xmlFree((xmlChar *) lib->namespace);
  2519. xmlFree(lib);
  2520. }
  2521. /**
  2522. * xmlRelaxNGRegisterTypeLibrary:
  2523. * @namespace: the URI bound to the library
  2524. * @data: data associated to the library
  2525. * @have: the provide function
  2526. * @check: the checking function
  2527. * @comp: the comparison function
  2528. *
  2529. * Register a new type library
  2530. *
  2531. * Returns 0 in case of success and -1 in case of error.
  2532. */
  2533. static int
  2534. xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data,
  2535. xmlRelaxNGTypeHave have,
  2536. xmlRelaxNGTypeCheck check,
  2537. xmlRelaxNGTypeCompare comp,
  2538. xmlRelaxNGFacetCheck facet,
  2539. xmlRelaxNGTypeFree freef)
  2540. {
  2541. xmlRelaxNGTypeLibraryPtr lib;
  2542. int ret;
  2543. if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
  2544. (check == NULL) || (comp == NULL))
  2545. return (-1);
  2546. if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
  2547. xmlGenericError(xmlGenericErrorContext,
  2548. "Relax-NG types library '%s' already registered\n",
  2549. namespace);
  2550. return (-1);
  2551. }
  2552. lib =
  2553. (xmlRelaxNGTypeLibraryPtr)
  2554. xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
  2555. if (lib == NULL) {
  2556. xmlRngVErrMemory(NULL, "adding types library\n");
  2557. return (-1);
  2558. }
  2559. memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
  2560. lib->namespace = xmlStrdup(namespace);
  2561. lib->data = data;
  2562. lib->have = have;
  2563. lib->comp = comp;
  2564. lib->check = check;
  2565. lib->facet = facet;
  2566. lib->freef = freef;
  2567. ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
  2568. if (ret < 0) {
  2569. xmlGenericError(xmlGenericErrorContext,
  2570. "Relax-NG types library failed to register '%s'\n",
  2571. namespace);
  2572. xmlRelaxNGFreeTypeLibrary(lib, namespace);
  2573. return (-1);
  2574. }
  2575. return (0);
  2576. }
  2577. /**
  2578. * xmlRelaxNGInitTypes:
  2579. *
  2580. * Initilize the default type libraries.
  2581. *
  2582. * Returns 0 in case of success and -1 in case of error.
  2583. */
  2584. int
  2585. xmlRelaxNGInitTypes(void)
  2586. {
  2587. if (xmlRelaxNGTypeInitialized != 0)
  2588. return (0);
  2589. xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
  2590. if (xmlRelaxNGRegisteredTypes == NULL) {
  2591. xmlGenericError(xmlGenericErrorContext,
  2592. "Failed to allocate sh table for Relax-NG types\n");
  2593. return (-1);
  2594. }
  2595. xmlRelaxNGRegisterTypeLibrary(BAD_CAST
  2596. "http://www.w3.org/2001/XMLSchema-datatypes",
  2597. NULL, xmlRelaxNGSchemaTypeHave,
  2598. xmlRelaxNGSchemaTypeCheck,
  2599. xmlRelaxNGSchemaTypeCompare,
  2600. xmlRelaxNGSchemaFacetCheck,
  2601. xmlRelaxNGSchemaFreeValue);
  2602. xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL,
  2603. xmlRelaxNGDefaultTypeHave,
  2604. xmlRelaxNGDefaultTypeCheck,
  2605. xmlRelaxNGDefaultTypeCompare, NULL,
  2606. NULL);
  2607. xmlRelaxNGTypeInitialized = 1;
  2608. return (0);
  2609. }
  2610. /**
  2611. * xmlRelaxNGCleanupTypes:
  2612. *
  2613. * Cleanup the default Schemas type library associated to RelaxNG
  2614. */
  2615. void
  2616. xmlRelaxNGCleanupTypes(void)
  2617. {
  2618. xmlSchemaCleanupTypes();
  2619. if (xmlRelaxNGTypeInitialized == 0)
  2620. return;
  2621. xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
  2622. xmlRelaxNGFreeTypeLibrary);
  2623. xmlRelaxNGTypeInitialized = 0;
  2624. }
  2625. /************************************************************************
  2626. * *
  2627. * Compiling element content into regexp *
  2628. * *
  2629. * Sometime the element content can be compiled into a pure regexp, *
  2630. * This allows a faster execution and streamability at that level *
  2631. * *
  2632. ************************************************************************/
  2633. /* from automata.c but not exported */
  2634. void xmlAutomataSetFlags(xmlAutomataPtr am, int flags);
  2635. static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
  2636. xmlRelaxNGDefinePtr def);
  2637. /**
  2638. * xmlRelaxNGIsCompileable:
  2639. * @define: the definition to check
  2640. *
  2641. * Check if a definition is nullable.
  2642. *
  2643. * Returns 1 if yes, 0 if no and -1 in case of error
  2644. */
  2645. static int
  2646. xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def)
  2647. {
  2648. int ret = -1;
  2649. if (def == NULL) {
  2650. return (-1);
  2651. }
  2652. if ((def->type != XML_RELAXNG_ELEMENT) &&
  2653. (def->dflags & IS_COMPILABLE))
  2654. return (1);
  2655. if ((def->type != XML_RELAXNG_ELEMENT) &&
  2656. (def->dflags & IS_NOT_COMPILABLE))
  2657. return (0);
  2658. switch (def->type) {
  2659. case XML_RELAXNG_NOOP:
  2660. ret = xmlRelaxNGIsCompileable(def->content);
  2661. break;
  2662. case XML_RELAXNG_TEXT:
  2663. case XML_RELAXNG_EMPTY:
  2664. ret = 1;
  2665. break;
  2666. case XML_RELAXNG_ELEMENT:
  2667. /*
  2668. * Check if the element content is compileable
  2669. */
  2670. if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
  2671. ((def->dflags & IS_COMPILABLE) == 0)) {
  2672. xmlRelaxNGDefinePtr list;
  2673. list = def->content;
  2674. while (list != NULL) {
  2675. ret = xmlRelaxNGIsCompileable(list);
  2676. if (ret != 1)
  2677. break;
  2678. list = list->next;
  2679. }
  2680. /*
  2681. * Because the routine is recursive, we must guard against
  2682. * discovering both COMPILABLE and NOT_COMPILABLE
  2683. */
  2684. if (ret == 0) {
  2685. def->dflags &= ~IS_COMPILABLE;
  2686. def->dflags |= IS_NOT_COMPILABLE;
  2687. }
  2688. if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE))
  2689. def->dflags |= IS_COMPILABLE;
  2690. #ifdef DEBUG_COMPILE
  2691. if (ret == 1) {
  2692. xmlGenericError(xmlGenericErrorContext,
  2693. "element content for %s is compilable\n",
  2694. def->name);
  2695. } else if (ret == 0) {
  2696. xmlGenericError(xmlGenericErrorContext,
  2697. "element content for %s is not compilable\n",
  2698. def->name);
  2699. } else {
  2700. xmlGenericError(xmlGenericErrorContext,
  2701. "Problem in RelaxNGIsCompileable for element %s\n",
  2702. def->name);
  2703. }
  2704. #endif
  2705. }
  2706. /*
  2707. * All elements return a compileable status unless they
  2708. * are generic like anyName
  2709. */
  2710. if ((def->nameClass != NULL) || (def->name == NULL))
  2711. ret = 0;
  2712. else
  2713. ret = 1;
  2714. return (ret);
  2715. case XML_RELAXNG_REF:
  2716. case XML_RELAXNG_EXTERNALREF:
  2717. case XML_RELAXNG_PARENTREF:
  2718. if (def->depth == -20) {
  2719. return (1);
  2720. } else {
  2721. xmlRelaxNGDefinePtr list;
  2722. def->depth = -20;
  2723. list = def->content;
  2724. while (list != NULL) {
  2725. ret = xmlRelaxNGIsCompileable(list);
  2726. if (ret != 1)
  2727. break;
  2728. list = list->next;
  2729. }
  2730. }
  2731. break;
  2732. case XML_RELAXNG_START:
  2733. case XML_RELAXNG_OPTIONAL:
  2734. case XML_RELAXNG_ZEROORMORE:
  2735. case XML_RELAXNG_ONEORMORE:
  2736. case XML_RELAXNG_CHOICE:
  2737. case XML_RELAXNG_GROUP:
  2738. case XML_RELAXNG_DEF:{
  2739. xmlRelaxNGDefinePtr list;
  2740. list = def->content;
  2741. while (list != NULL) {
  2742. ret = xmlRelaxNGIsCompileable(list);
  2743. if (ret != 1)
  2744. break;
  2745. list = list->next;
  2746. }
  2747. break;
  2748. }
  2749. case XML_RELAXNG_EXCEPT:
  2750. case XML_RELAXNG_ATTRIBUTE:
  2751. case XML_RELAXNG_INTERLEAVE:
  2752. case XML_RELAXNG_DATATYPE:
  2753. case XML_RELAXNG_LIST:
  2754. case XML_RELAXNG_PARAM:
  2755. case XML_RELAXNG_VALUE:
  2756. case XML_RELAXNG_NOT_ALLOWED:
  2757. ret = 0;
  2758. break;
  2759. }
  2760. if (ret == 0)
  2761. def->dflags |= IS_NOT_COMPILABLE;
  2762. if (ret == 1)
  2763. def->dflags |= IS_COMPILABLE;
  2764. #ifdef DEBUG_COMPILE
  2765. if (ret == 1) {
  2766. xmlGenericError(xmlGenericErrorContext,
  2767. "RelaxNGIsCompileable %s : true\n",
  2768. xmlRelaxNGDefName(def));
  2769. } else if (ret == 0) {
  2770. xmlGenericError(xmlGenericErrorContext,
  2771. "RelaxNGIsCompileable %s : false\n",
  2772. xmlRelaxNGDefName(def));
  2773. } else {
  2774. xmlGenericError(xmlGenericErrorContext,
  2775. "Problem in RelaxNGIsCompileable %s\n",
  2776. xmlRelaxNGDefName(def));
  2777. }
  2778. #endif
  2779. return (ret);
  2780. }
  2781. /**
  2782. * xmlRelaxNGCompile:
  2783. * ctxt: the RelaxNG parser context
  2784. * @define: the definition tree to compile
  2785. *
  2786. * Compile the set of definitions, it works recursively, till the
  2787. * element boundaries, where it tries to compile the content if possible
  2788. *
  2789. * Returns 0 if success and -1 in case of error
  2790. */
  2791. static int
  2792. xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
  2793. {
  2794. int ret = 0;
  2795. xmlRelaxNGDefinePtr list;
  2796. if ((ctxt == NULL) || (def == NULL))
  2797. return (-1);
  2798. switch (def->type) {
  2799. case XML_RELAXNG_START:
  2800. if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) {
  2801. xmlAutomataPtr oldam = ctxt->am;
  2802. xmlAutomataStatePtr oldstate = ctxt->state;
  2803. def->depth = -25;
  2804. list = def->content;
  2805. ctxt->am = xmlNewAutomata();
  2806. if (ctxt->am == NULL)
  2807. return (-1);
  2808. /*
  2809. * assume identical strings but not same pointer are different
  2810. * atoms, needed for non-determinism detection
  2811. * That way if 2 elements with the same name are in a choice
  2812. * branch the automata is found non-deterministic and
  2813. * we fallback to the normal validation which does the right
  2814. * thing of exploring both choices.
  2815. */
  2816. xmlAutomataSetFlags(ctxt->am, 1);
  2817. ctxt->state = xmlAutomataGetInitState(ctxt->am);
  2818. while (list != NULL) {
  2819. xmlRelaxNGCompile(ctxt, list);
  2820. list = list->next;
  2821. }
  2822. xmlAutomataSetFinalState(ctxt->am, ctxt->state);
  2823. def->contModel = xmlAutomataCompile(ctxt->am);
  2824. xmlRegexpIsDeterminist(def->contModel);
  2825. xmlFreeAutomata(ctxt->am);
  2826. ctxt->state = oldstate;
  2827. ctxt->am = oldam;
  2828. }
  2829. break;
  2830. case XML_RELAXNG_ELEMENT:
  2831. if ((ctxt->am != NULL) && (def->name != NULL)) {
  2832. ctxt->state = xmlAutomataNewTransition2(ctxt->am,
  2833. ctxt->state, NULL,
  2834. def->name, def->ns,
  2835. def);
  2836. }
  2837. if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
  2838. xmlAutomataPtr oldam = ctxt->am;
  2839. xmlAutomataStatePtr oldstate = ctxt->state;
  2840. def->depth = -25;
  2841. list = def->content;
  2842. ctxt->am = xmlNewAutomata();
  2843. if (ctxt->am == NULL)
  2844. return (-1);
  2845. xmlAutomataSetFlags(ctxt->am, 1);
  2846. ctxt->state = xmlAutomataGetInitState(ctxt->am);
  2847. while (list != NULL) {
  2848. xmlRelaxNGCompile(ctxt, list);
  2849. list = list->next;
  2850. }
  2851. xmlAutomataSetFinalState(ctxt->am, ctxt->state);
  2852. def->contModel = xmlAutomataCompile(ctxt->am);
  2853. if (!xmlRegexpIsDeterminist(def->contModel)) {
  2854. #ifdef DEBUG_COMPILE
  2855. xmlGenericError(xmlGenericErrorContext,
  2856. "Content model not determinist %s\n",
  2857. def->name);
  2858. #endif
  2859. /*
  2860. * we can only use the automata if it is determinist
  2861. */
  2862. xmlRegFreeRegexp(def->contModel);
  2863. def->contModel = NULL;
  2864. }
  2865. xmlFreeAutomata(ctxt->am);
  2866. ctxt->state = oldstate;
  2867. ctxt->am = oldam;
  2868. } else {
  2869. xmlAutomataPtr oldam = ctxt->am;
  2870. /*
  2871. * we can't build the content model for this element content
  2872. * but it still might be possible to build it for some of its
  2873. * children, recurse.
  2874. */
  2875. ret = xmlRelaxNGTryCompile(ctxt, def);
  2876. ctxt->am = oldam;
  2877. }
  2878. break;
  2879. case XML_RELAXNG_NOOP:
  2880. ret = xmlRelaxNGCompile(ctxt, def->content);
  2881. break;
  2882. case XML_RELAXNG_OPTIONAL:{
  2883. xmlAutomataStatePtr oldstate = ctxt->state;
  2884. list = def->content;
  2885. while (list != NULL) {
  2886. xmlRelaxNGCompile(ctxt, list);
  2887. list = list->next;
  2888. }
  2889. xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
  2890. break;
  2891. }
  2892. case XML_RELAXNG_ZEROORMORE:{
  2893. xmlAutomataStatePtr oldstate;
  2894. ctxt->state =
  2895. xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
  2896. oldstate = ctxt->state;
  2897. list = def->content;
  2898. while (list != NULL) {
  2899. xmlRelaxNGCompile(ctxt, list);
  2900. list = list->next;
  2901. }
  2902. xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
  2903. ctxt->state =
  2904. xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
  2905. break;
  2906. }
  2907. case XML_RELAXNG_ONEORMORE:{
  2908. xmlAutomataStatePtr oldstate;
  2909. list = def->content;
  2910. while (list != NULL) {
  2911. xmlRelaxNGCompile(ctxt, list);
  2912. list = list->next;
  2913. }
  2914. oldstate = ctxt->state;
  2915. list = def->content;
  2916. while (list != NULL) {
  2917. xmlRelaxNGCompile(ctxt, list);
  2918. list = list->next;
  2919. }
  2920. xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
  2921. ctxt->state =
  2922. xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
  2923. break;
  2924. }
  2925. case XML_RELAXNG_CHOICE:{
  2926. xmlAutomataStatePtr target = NULL;
  2927. xmlAutomataStatePtr oldstate = ctxt->state;
  2928. list = def->content;
  2929. while (list != NULL) {
  2930. ctxt->state = oldstate;
  2931. ret = xmlRelaxNGCompile(ctxt, list);
  2932. if (ret != 0)
  2933. break;
  2934. if (target == NULL)
  2935. target = ctxt->state;
  2936. else {
  2937. xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
  2938. target);
  2939. }
  2940. list = list->next;
  2941. }
  2942. ctxt->state = target;
  2943. break;
  2944. }
  2945. case XML_RELAXNG_REF:
  2946. case XML_RELAXNG_EXTERNALREF:
  2947. case XML_RELAXNG_PARENTREF:
  2948. case XML_RELAXNG_GROUP:
  2949. case XML_RELAXNG_DEF:
  2950. list = def->content;
  2951. while (list != NULL) {
  2952. ret = xmlRelaxNGCompile(ctxt, list);
  2953. if (ret != 0)
  2954. break;
  2955. list = list->next;
  2956. }
  2957. break;
  2958. case XML_RELAXNG_TEXT:{
  2959. xmlAutomataStatePtr oldstate;
  2960. ctxt->state =
  2961. xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
  2962. oldstate = ctxt->state;
  2963. xmlRelaxNGCompile(ctxt, def->content);
  2964. xmlAutomataNewTransition(ctxt->am, ctxt->state,
  2965. ctxt->state, BAD_CAST "#text",
  2966. NULL);
  2967. ctxt->state =
  2968. xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
  2969. break;
  2970. }
  2971. case XML_RELAXNG_EMPTY:
  2972. ctxt->state =
  2973. xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
  2974. break;
  2975. case XML_RELAXNG_EXCEPT:
  2976. case XML_RELAXNG_ATTRIBUTE:
  2977. case XML_RELAXNG_INTERLEAVE:
  2978. case XML_RELAXNG_NOT_ALLOWED:
  2979. case XML_RELAXNG_DATATYPE:
  2980. case XML_RELAXNG_LIST:
  2981. case XML_RELAXNG_PARAM:
  2982. case XML_RELAXNG_VALUE:
  2983. /* This should not happen and generate an internal error */
  2984. fprintf(stderr, "RNG internal error trying to compile %s\n",
  2985. xmlRelaxNGDefName(def));
  2986. break;
  2987. }
  2988. return (ret);
  2989. }
  2990. /**
  2991. * xmlRelaxNGTryCompile:
  2992. * ctxt: the RelaxNG parser context
  2993. * @define: the definition tree to compile
  2994. *
  2995. * Try to compile the set of definitions, it works recursively,
  2996. * possibly ignoring parts which cannot be compiled.
  2997. *
  2998. * Returns 0 if success and -1 in case of error
  2999. */
  3000. static int
  3001. xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
  3002. {
  3003. int ret = 0;
  3004. xmlRelaxNGDefinePtr list;
  3005. if ((ctxt == NULL) || (def == NULL))
  3006. return (-1);
  3007. if ((def->type == XML_RELAXNG_START) ||
  3008. (def->type == XML_RELAXNG_ELEMENT)) {
  3009. ret = xmlRelaxNGIsCompileable(def);
  3010. if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
  3011. ctxt->am = NULL;
  3012. ret = xmlRelaxNGCompile(ctxt, def);
  3013. #ifdef DEBUG_PROGRESSIVE
  3014. if (ret == 0) {
  3015. if (def->type == XML_RELAXNG_START)
  3016. xmlGenericError(xmlGenericErrorContext,
  3017. "compiled the start\n");
  3018. else
  3019. xmlGenericError(xmlGenericErrorContext,
  3020. "compiled element %s\n", def->name);
  3021. } else {
  3022. if (def->type == XML_RELAXNG_START)
  3023. xmlGenericError(xmlGenericErrorContext,
  3024. "failed to compile the start\n");
  3025. else
  3026. xmlGenericError(xmlGenericErrorContext,
  3027. "failed to compile element %s\n",
  3028. def->name);
  3029. }
  3030. #endif
  3031. return (ret);
  3032. }
  3033. }
  3034. switch (def->type) {
  3035. case XML_RELAXNG_NOOP:
  3036. ret = xmlRelaxNGTryCompile(ctxt, def->content);
  3037. break;
  3038. case XML_RELAXNG_TEXT:
  3039. case XML_RELAXNG_DATATYPE:
  3040. case XML_RELAXNG_LIST:
  3041. case XML_RELAXNG_PARAM:
  3042. case XML_RELAXNG_VALUE:
  3043. case XML_RELAXNG_EMPTY:
  3044. case XML_RELAXNG_ELEMENT:
  3045. ret = 0;
  3046. break;
  3047. case XML_RELAXNG_OPTIONAL:
  3048. case XML_RELAXNG_ZEROORMORE:
  3049. case XML_RELAXNG_ONEORMORE:
  3050. case XML_RELAXNG_CHOICE:
  3051. case XML_RELAXNG_GROUP:
  3052. case XML_RELAXNG_DEF:
  3053. case XML_RELAXNG_START:
  3054. case XML_RELAXNG_REF:
  3055. case XML_RELAXNG_EXTERNALREF:
  3056. case XML_RELAXNG_PARENTREF:
  3057. list = def->content;
  3058. while (list != NULL) {
  3059. ret = xmlRelaxNGTryCompile(ctxt, list);
  3060. if (ret != 0)
  3061. break;
  3062. list = list->next;
  3063. }
  3064. break;
  3065. case XML_RELAXNG_EXCEPT:
  3066. case XML_RELAXNG_ATTRIBUTE:
  3067. case XML_RELAXNG_INTERLEAVE:
  3068. case XML_RELAXNG_NOT_ALLOWED:
  3069. ret = 0;
  3070. break;
  3071. }
  3072. return (ret);
  3073. }
  3074. /************************************************************************
  3075. * *
  3076. * Parsing functions *
  3077. * *
  3078. ************************************************************************/
  3079. static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr
  3080. ctxt, xmlNodePtr node);
  3081. static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr
  3082. ctxt, xmlNodePtr node);
  3083. static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr
  3084. ctxt, xmlNodePtr nodes,
  3085. int group);
  3086. static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr
  3087. ctxt, xmlNodePtr node);
  3088. static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,
  3089. xmlNodePtr node);
  3090. static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
  3091. xmlNodePtr nodes);
  3092. static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr
  3093. ctxt, xmlNodePtr node,
  3094. xmlRelaxNGDefinePtr
  3095. def);
  3096. static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr
  3097. ctxt, xmlNodePtr nodes);
  3098. static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
  3099. xmlRelaxNGDefinePtr define,
  3100. xmlNodePtr elem);
  3101. #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
  3102. /**
  3103. * xmlRelaxNGIsNullable:
  3104. * @define: the definition to verify
  3105. *
  3106. * Check if a definition is nullable.
  3107. *
  3108. * Returns 1 if yes, 0 if no and -1 in case of error
  3109. */
  3110. static int
  3111. xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)
  3112. {
  3113. int ret;
  3114. if (define == NULL)
  3115. return (-1);
  3116. if (define->dflags & IS_NULLABLE)
  3117. return (1);
  3118. if (define->dflags & IS_NOT_NULLABLE)
  3119. return (0);
  3120. switch (define->type) {
  3121. case XML_RELAXNG_EMPTY:
  3122. case XML_RELAXNG_TEXT:
  3123. ret = 1;
  3124. break;
  3125. case XML_RELAXNG_NOOP:
  3126. case XML_RELAXNG_DEF:
  3127. case XML_RELAXNG_REF:
  3128. case XML_RELAXNG_EXTERNALREF:
  3129. case XML_RELAXNG_PARENTREF:
  3130. case XML_RELAXNG_ONEORMORE:
  3131. ret = xmlRelaxNGIsNullable(define->content);
  3132. break;
  3133. case XML_RELAXNG_EXCEPT:
  3134. case XML_RELAXNG_NOT_ALLOWED:
  3135. case XML_RELAXNG_ELEMENT:
  3136. case XML_RELAXNG_DATATYPE:
  3137. case XML_RELAXNG_PARAM:
  3138. case XML_RELAXNG_VALUE:
  3139. case XML_RELAXNG_LIST:
  3140. case XML_RELAXNG_ATTRIBUTE:
  3141. ret = 0;
  3142. break;
  3143. case XML_RELAXNG_CHOICE:{
  3144. xmlRelaxNGDefinePtr list = define->content;
  3145. while (list != NULL) {
  3146. ret = xmlRelaxNGIsNullable(list);
  3147. if (ret != 0)
  3148. goto done;
  3149. list = list->next;
  3150. }
  3151. ret = 0;
  3152. break;
  3153. }
  3154. case XML_RELAXNG_START:
  3155. case XML_RELAXNG_INTERLEAVE:
  3156. case XML_RELAXNG_GROUP:{
  3157. xmlRelaxNGDefinePtr list = define->content;
  3158. while (list != NULL) {
  3159. ret = xmlRelaxNGIsNullable(list);
  3160. if (ret != 1)
  3161. goto done;
  3162. list = list->next;
  3163. }
  3164. return (1);
  3165. }
  3166. default:
  3167. return (-1);
  3168. }
  3169. done:
  3170. if (ret == 0)
  3171. define->dflags |= IS_NOT_NULLABLE;
  3172. if (ret == 1)
  3173. define->dflags |= IS_NULLABLE;
  3174. return (ret);
  3175. }
  3176. /**
  3177. * xmlRelaxNGIsBlank:
  3178. * @str: a string
  3179. *
  3180. * Check if a string is ignorable c.f. 4.2. Whitespace
  3181. *
  3182. * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
  3183. */
  3184. static int
  3185. xmlRelaxNGIsBlank(xmlChar * str)
  3186. {
  3187. if (str == NULL)
  3188. return (1);
  3189. while (*str != 0) {
  3190. if (!(IS_BLANK_CH(*str)))
  3191. return (0);
  3192. str++;
  3193. }
  3194. return (1);
  3195. }
  3196. /**
  3197. * xmlRelaxNGGetDataTypeLibrary:
  3198. * @ctxt: a Relax-NG parser context
  3199. * @node: the current data or value element
  3200. *
  3201. * Applies algorithm from 4.3. datatypeLibrary attribute
  3202. *
  3203. * Returns the datatypeLibary value or NULL if not found
  3204. */
  3205. static xmlChar *
  3206. xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
  3207. xmlNodePtr node)
  3208. {
  3209. xmlChar *ret, *escape;
  3210. if (node == NULL)
  3211. return(NULL);
  3212. if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
  3213. ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
  3214. if (ret != NULL) {
  3215. if (ret[0] == 0) {
  3216. xmlFree(ret);
  3217. return (NULL);
  3218. }
  3219. escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
  3220. if (escape == NULL) {
  3221. return (ret);
  3222. }
  3223. xmlFree(ret);
  3224. return (escape);
  3225. }
  3226. }
  3227. node = node->parent;
  3228. while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
  3229. ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
  3230. if (ret != NULL) {
  3231. if (ret[0] == 0) {
  3232. xmlFree(ret);
  3233. return (NULL);
  3234. }
  3235. escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
  3236. if (escape == NULL) {
  3237. return (ret);
  3238. }
  3239. xmlFree(ret);
  3240. return (escape);
  3241. }
  3242. node = node->parent;
  3243. }
  3244. return (NULL);
  3245. }
  3246. /**
  3247. * xmlRelaxNGParseValue:
  3248. * @ctxt: a Relax-NG parser context
  3249. * @node: the data node.
  3250. *
  3251. * parse the content of a RelaxNG value node.
  3252. *
  3253. * Returns the definition pointer or NULL in case of error
  3254. */
  3255. static xmlRelaxNGDefinePtr
  3256. xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
  3257. {
  3258. xmlRelaxNGDefinePtr def = NULL;
  3259. xmlRelaxNGTypeLibraryPtr lib = NULL;
  3260. xmlChar *type;
  3261. xmlChar *library;
  3262. int success = 0;
  3263. def = xmlRelaxNGNewDefine(ctxt, node);
  3264. if (def == NULL)
  3265. return (NULL);
  3266. def->type = XML_RELAXNG_VALUE;
  3267. type = xmlGetProp(node, BAD_CAST "type");
  3268. if (type != NULL) {
  3269. xmlRelaxNGNormExtSpace(type);
  3270. if (xmlValidateNCName(type, 0)) {
  3271. xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
  3272. "value type '%s' is not an NCName\n", type, NULL);
  3273. }
  3274. library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
  3275. if (library == NULL)
  3276. library =
  3277. xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
  3278. def->name = type;
  3279. def->ns = library;
  3280. lib = (xmlRelaxNGTypeLibraryPtr)
  3281. xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
  3282. if (lib == NULL) {
  3283. xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
  3284. "Use of unregistered type library '%s'\n", library,
  3285. NULL);
  3286. def->data = NULL;
  3287. } else {
  3288. def->data = lib;
  3289. if (lib->have == NULL) {
  3290. xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
  3291. "Internal error with type library '%s': no 'have'\n",
  3292. library, NULL);
  3293. } else {
  3294. success = lib->have(lib->data, def->name);
  3295. if (success != 1) {
  3296. xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
  3297. "Error type '%s' is not exported by type library '%s'\n",
  3298. def->name, library);
  3299. }
  3300. }
  3301. }
  3302. }
  3303. if (node->children == NULL) {
  3304. def->value = xmlStrdup(BAD_CAST "");
  3305. } else if (((node->children->type != XML_TEXT_NODE) &&
  3306. (node->children->type != XML_CDATA_SECTION_NODE)) ||
  3307. (node->children->next != NULL)) {
  3308. xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED,
  3309. "Expecting a single text value for <value>content\n",
  3310. NULL, NULL);
  3311. } else if (def != NULL) {
  3312. def->value = xmlNodeGetContent(node);
  3313. if (def->value == NULL) {
  3314. xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT,
  3315. "Element <value> has no content\n", NULL, NULL);
  3316. } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
  3317. void *val = NULL;
  3318. success =
  3319. lib->check(lib->data, def->name, def->value, &val, node);
  3320. if (success != 1) {
  3321. xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE,
  3322. "Value '%s' is not acceptable for type '%s'\n",
  3323. def->value, def->name);
  3324. } else {
  3325. if (val != NULL)
  3326. def->attrs = val;
  3327. }
  3328. }
  3329. }
  3330. return (def);
  3331. }
  3332. /**
  3333. * xmlRelaxNGParseData:
  3334. * @ctxt: a Relax-NG parser context
  3335. * @node: the data node.
  3336. *
  3337. * parse the content of a RelaxNG data node.
  3338. *
  3339. * Returns the definition pointer or NULL in case of error
  3340. */
  3341. static xmlRelaxNGDefinePtr
  3342. xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
  3343. {
  3344. xmlRelaxNGDefinePtr def = NULL, except;
  3345. xmlRelaxNGDefinePtr param, lastparam = NULL;
  3346. xmlRelaxNGTypeLibraryPtr lib;
  3347. xmlChar *type;
  3348. xmlChar *library;
  3349. xmlNodePtr content;
  3350. int tmp;
  3351. type = xmlGetProp(node, BAD_CAST "type");
  3352. if (type == NULL) {
  3353. xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL,
  3354. NULL);
  3355. return (NULL);
  3356. }
  3357. xmlRelaxNGNormExtSpace(type);
  3358. if (xmlValidateNCName(type, 0)) {
  3359. xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
  3360. "data type '%s' is not an NCName\n", type, NULL);
  3361. }
  3362. library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
  3363. if (library == NULL)
  3364. library =
  3365. xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
  3366. def = xmlRelaxNGNewDefine(ctxt, node);
  3367. if (def == NULL) {
  3368. xmlFree(type);
  3369. return (NULL);
  3370. }
  3371. def->type = XML_RELAXNG_DATATYPE;
  3372. def->name = type;
  3373. def->ns = library;
  3374. lib = (xmlRelaxNGTypeLibraryPtr)
  3375. xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
  3376. if (lib == NULL) {
  3377. xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
  3378. "Use of unregistered type library '%s'\n", library,
  3379. NULL);
  3380. def->data = NULL;
  3381. } else {
  3382. def->data = lib;
  3383. if (lib->have == NULL) {
  3384. xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
  3385. "Internal error with type library '%s': no 'have'\n",
  3386. library, NULL);
  3387. } else {
  3388. tmp = lib->have(lib->data, def->name);
  3389. if (tmp != 1) {
  3390. xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
  3391. "Error type '%s' is not exported by type library '%s'\n",
  3392. def->name, library);
  3393. } else
  3394. if ((xmlStrEqual
  3395. (library,
  3396. BAD_CAST
  3397. "http://www.w3.org/2001/XMLSchema-datatypes"))
  3398. && ((xmlStrEqual(def->name, BAD_CAST "IDREF"))
  3399. || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
  3400. ctxt->idref = 1;
  3401. }
  3402. }
  3403. }
  3404. content = node->children;
  3405. /*
  3406. * Handle optional params
  3407. */
  3408. while (content != NULL) {
  3409. if (!xmlStrEqual(content->name, BAD_CAST "param"))
  3410. break;
  3411. if (xmlStrEqual(library,
  3412. BAD_CAST "http://relaxng.org/ns/structure/1.0")) {
  3413. xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN,
  3414. "Type library '%s' does not allow type parameters\n",
  3415. library, NULL);
  3416. content = content->next;
  3417. while ((content != NULL) &&
  3418. (xmlStrEqual(content->name, BAD_CAST "param")))
  3419. content = content->next;
  3420. } else {
  3421. param = xmlRelaxNGNewDefine(ctxt, node);
  3422. if (param != NULL) {
  3423. param->type = XML_RELAXNG_PARAM;
  3424. param->name = xmlGetProp(content, BAD_CAST "name");
  3425. if (param->name == NULL) {
  3426. xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING,
  3427. "param has no name\n", NULL, NULL);
  3428. }
  3429. param->value = xmlNodeGetContent(content);
  3430. if (lastparam == NULL) {
  3431. def->attrs = lastparam = param;
  3432. } else {
  3433. lastparam->next = param;
  3434. lastparam = param;
  3435. }
  3436. if (lib != NULL) {
  3437. }
  3438. }
  3439. content = content->next;
  3440. }
  3441. }
  3442. /*
  3443. * Handle optional except
  3444. */
  3445. if ((content != NULL)
  3446. && (xmlStrEqual(content->name, BAD_CAST "except"))) {
  3447. xmlNodePtr child;
  3448. xmlRelaxNGDefinePtr tmp2, last = NULL;
  3449. except = xmlRelaxNGNewDefine(ctxt, node);
  3450. if (except == NULL) {
  3451. return (def);
  3452. }
  3453. except->type = XML_RELAXNG_EXCEPT;
  3454. child = content->children;
  3455. def->content = except;
  3456. if (child == NULL) {
  3457. xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT,
  3458. "except has no content\n", NULL, NULL);
  3459. }
  3460. while (child != NULL) {
  3461. tmp2 = xmlRelaxNGParsePattern(ctxt, child);
  3462. if (tmp2 != NULL) {
  3463. if (last == NULL) {
  3464. except->content = last = tmp2;
  3465. } else {
  3466. last->next = tmp2;
  3467. last = tmp2;
  3468. }
  3469. }
  3470. child = child->next;
  3471. }
  3472. content = content->next;
  3473. }
  3474. /*
  3475. * Check there is no unhandled data
  3476. */
  3477. if (content != NULL) {
  3478. xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT,
  3479. "Element data has unexpected content %s\n",
  3480. content->name, NULL);
  3481. }
  3482. return (def);
  3483. }
  3484. static const xmlChar *invalidName = BAD_CAST "\1";
  3485. /**
  3486. * xmlRelaxNGCompareNameClasses:
  3487. * @defs1: the first element/attribute defs
  3488. * @defs2: the second element/attribute defs
  3489. * @name: the restriction on the name
  3490. * @ns: the restriction on the namespace
  3491. *
  3492. * Compare the 2 lists of element definitions. The comparison is
  3493. * that if both lists do not accept the same QNames, it returns 1
  3494. * If the 2 lists can accept the same QName the comparison returns 0
  3495. *
  3496. * Returns 1 disttinct, 0 if equal
  3497. */
  3498. static int
  3499. xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
  3500. xmlRelaxNGDefinePtr def2)
  3501. {
  3502. int ret = 1;
  3503. xmlNode node;
  3504. xmlNs ns;
  3505. xmlRelaxNGValidCtxt ctxt;
  3506. memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
  3507. ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR;
  3508. if ((def1->type == XML_RELAXNG_ELEMENT) ||
  3509. (def1->type == XML_RELAXNG_ATTRIBUTE)) {
  3510. if (def2->type == XML_RELAXNG_TEXT)
  3511. return (1);
  3512. if (def1->name != NULL) {
  3513. node.name = def1->name;
  3514. } else {
  3515. node.name = invalidName;
  3516. }
  3517. if (def1->ns != NULL) {
  3518. if (def1->ns[0] == 0) {
  3519. node.ns = NULL;
  3520. } else {
  3521. node.ns = &ns;
  3522. ns.href = def1->ns;
  3523. }
  3524. } else {
  3525. node.ns = NULL;
  3526. }
  3527. if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
  3528. if (def1->nameClass != NULL) {
  3529. ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
  3530. } else {
  3531. ret = 0;
  3532. }
  3533. } else {
  3534. ret = 1;
  3535. }
  3536. } else if (def1->type == XML_RELAXNG_TEXT) {
  3537. if (def2->type == XML_RELAXNG_TEXT)
  3538. return (0);
  3539. return (1);
  3540. } else if (def1->type == XML_RELAXNG_EXCEPT) {
  3541. TODO ret = 0;
  3542. } else {
  3543. TODO ret = 0;
  3544. }
  3545. if (ret == 0)
  3546. return (ret);
  3547. if ((def2->type == XML_RELAXNG_ELEMENT) ||
  3548. (def2->type == XML_RELAXNG_ATTRIBUTE)) {
  3549. if (def2->name != NULL) {
  3550. node.name = def2->name;
  3551. } else {
  3552. node.name = invalidName;
  3553. }
  3554. node.ns = &ns;
  3555. if (def2->ns != NULL) {
  3556. if (def2->ns[0] == 0) {
  3557. node.ns = NULL;
  3558. } else {
  3559. ns.href = def2->ns;
  3560. }
  3561. } else {
  3562. ns.href = invalidName;
  3563. }
  3564. if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
  3565. if (def2->nameClass != NULL) {
  3566. ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
  3567. } else {
  3568. ret = 0;
  3569. }
  3570. } else {
  3571. ret = 1;
  3572. }
  3573. } else {
  3574. TODO ret = 0;
  3575. }
  3576. return (ret);
  3577. }
  3578. /**
  3579. * xmlRelaxNGCompareElemDefLists:
  3580. * @ctxt: a Relax-NG parser context
  3581. * @defs1: the first list of element/attribute defs
  3582. * @defs2: the second list of element/attribute defs
  3583. *
  3584. * Compare the 2 lists of element or attribute definitions. The comparison
  3585. * is that if both lists do not accept the same QNames, it returns 1
  3586. * If the 2 lists can accept the same QName the comparison returns 0
  3587. *
  3588. * Returns 1 disttinct, 0 if equal
  3589. */
  3590. static int
  3591. xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt
  3592. ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1,
  3593. xmlRelaxNGDefinePtr * def2)
  3594. {
  3595. xmlRelaxNGDefinePtr *basedef2 = def2;
  3596. if ((def1 == NULL) || (def2 == NULL))
  3597. return (1);
  3598. if ((*def1 == NULL) || (*def2 == NULL))
  3599. return (1);
  3600. while (*def1 != NULL) {
  3601. while ((*def2) != NULL) {
  3602. if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
  3603. return (0);
  3604. def2++;
  3605. }
  3606. def2 = basedef2;
  3607. def1++;
  3608. }
  3609. return (1);
  3610. }
  3611. /**
  3612. * xmlRelaxNGGenerateAttributes:
  3613. * @ctxt: a Relax-NG parser context
  3614. * @def: the definition definition
  3615. *
  3616. * Check if the definition can only generate attributes
  3617. *
  3618. * Returns 1 if yes, 0 if no and -1 in case of error.
  3619. */
  3620. static int
  3621. xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
  3622. xmlRelaxNGDefinePtr def)
  3623. {
  3624. xmlRelaxNGDefinePtr parent, cur, tmp;
  3625. /*
  3626. * Don't run that check in case of error. Infinite recursion
  3627. * becomes possible.
  3628. */
  3629. if (ctxt->nbErrors != 0)
  3630. return (-1);
  3631. parent = NULL;
  3632. cur = def;
  3633. while (cur != NULL) {
  3634. if ((cur->type == XML_RELAXNG_ELEMENT) ||
  3635. (cur->type == XML_RELAXNG_TEXT) ||
  3636. (cur->type == XML_RELAXNG_DATATYPE) ||
  3637. (cur->type == XML_RELAXNG_PARAM) ||
  3638. (cur->type == XML_RELAXNG_LIST) ||
  3639. (cur->type == XML_RELAXNG_VALUE) ||
  3640. (cur->type == XML_RELAXNG_EMPTY))
  3641. return (0);
  3642. if ((cur->type == XML_RELAXNG_CHOICE) ||
  3643. (cur->type == XML_RELAXNG_INTERLEAVE) ||
  3644. (cur->type == XML_RELAXNG_GROUP) ||
  3645. (cur->type == XML_RELAXNG_ONEORMORE) ||
  3646. (cur->type == XML_RELAXNG_ZEROORMORE) ||
  3647. (cur->type == XML_RELAXNG_OPTIONAL) ||
  3648. (cur->type == XML_RELAXNG_PARENTREF) ||
  3649. (cur->type == XML_RELAXNG_EXTERNALREF) ||
  3650. (cur->type == XML_RELAXNG_REF) ||
  3651. (cur->type == XML_RELAXNG_DEF)) {
  3652. if (cur->content != NULL) {
  3653. parent = cur;
  3654. cur = cur->content;
  3655. tmp = cur;
  3656. while (tmp != NULL) {
  3657. tmp->parent = parent;
  3658. tmp = tmp->next;
  3659. }
  3660. continue;
  3661. }
  3662. }
  3663. if (cur == def)
  3664. break;
  3665. if (cur->next != NULL) {
  3666. cur = cur->next;
  3667. continue;
  3668. }
  3669. do {
  3670. cur = cur->parent;
  3671. if (cur == NULL)
  3672. break;
  3673. if (cur == def)
  3674. return (1);
  3675. if (cur->next != NULL) {
  3676. cur = cur->next;
  3677. break;
  3678. }
  3679. } while (cur != NULL);
  3680. }
  3681. return (1);
  3682. }
  3683. /**
  3684. * xmlRelaxNGGetElements:
  3685. * @ctxt: a Relax-NG parser context
  3686. * @def: the definition definition
  3687. * @eora: gather elements (0) or attributes (1)
  3688. *
  3689. * Compute the list of top elements a definition can generate
  3690. *
  3691. * Returns a list of elements or NULL if none was found.
  3692. */
  3693. static xmlRelaxNGDefinePtr *
  3694. xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
  3695. xmlRelaxNGDefinePtr def, int eora)
  3696. {
  3697. xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
  3698. int len = 0;
  3699. int max = 0;
  3700. /*
  3701. * Don't run that check in case of error. Infinite recursion
  3702. * becomes possible.
  3703. */
  3704. if (ctxt->nbErrors != 0)
  3705. return (NULL);
  3706. parent = NULL;
  3707. cur = def;
  3708. while (cur != NULL) {
  3709. if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
  3710. (cur->type == XML_RELAXNG_TEXT))) ||
  3711. ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) {
  3712. if (ret == NULL) {
  3713. max = 10;
  3714. ret = (xmlRelaxNGDefinePtr *)
  3715. xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
  3716. if (ret == NULL) {
  3717. xmlRngPErrMemory(ctxt, "getting element list\n");
  3718. return (NULL);
  3719. }
  3720. } else if (max <= len) {
  3721. xmlRelaxNGDefinePtr *temp;
  3722. max *= 2;
  3723. temp = xmlRealloc(ret,
  3724. (max + 1) * sizeof(xmlRelaxNGDefinePtr));
  3725. if (temp == NULL) {
  3726. xmlRngPErrMemory(ctxt, "getting element list\n");
  3727. xmlFree(ret);
  3728. return (NULL);
  3729. }
  3730. ret = temp;
  3731. }
  3732. ret[len++] = cur;
  3733. ret[len] = NULL;
  3734. } else if ((cur->type == XML_RELAXNG_CHOICE) ||
  3735. (cur->type == XML_RELAXNG_INTERLEAVE) ||
  3736. (cur->type == XML_RELAXNG_GROUP) ||
  3737. (cur->type == XML_RELAXNG_ONEORMORE) ||
  3738. (cur->type == XML_RELAXNG_ZEROORMORE) ||
  3739. (cur->type == XML_RELAXNG_OPTIONAL) ||
  3740. (cur->type == XML_RELAXNG_PARENTREF) ||
  3741. (cur->type == XML_RELAXNG_REF) ||
  3742. (cur->type == XML_RELAXNG_DEF) ||
  3743. (cur->type == XML_RELAXNG_EXTERNALREF)) {
  3744. /*
  3745. * Don't go within elements or attributes or string values.
  3746. * Just gather the element top list
  3747. */
  3748. if (cur->content != NULL) {
  3749. parent = cur;
  3750. cur = cur->content;
  3751. tmp = cur;
  3752. while (tmp != NULL) {
  3753. tmp->parent = parent;
  3754. tmp = tmp->next;
  3755. }
  3756. continue;
  3757. }
  3758. }
  3759. if (cur == def)
  3760. break;
  3761. if (cur->next != NULL) {
  3762. cur = cur->next;
  3763. continue;
  3764. }
  3765. do {
  3766. cur = cur->parent;
  3767. if (cur == NULL)
  3768. break;
  3769. if (cur == def)
  3770. return (ret);
  3771. if (cur->next != NULL) {
  3772. cur = cur->next;
  3773. break;
  3774. }
  3775. } while (cur != NULL);
  3776. }
  3777. return (ret);
  3778. }
  3779. /**
  3780. * xmlRelaxNGCheckChoiceDeterminism:
  3781. * @ctxt: a Relax-NG parser context
  3782. * @def: the choice definition
  3783. *
  3784. * Also used to find indeterministic pattern in choice
  3785. */
  3786. static void
  3787. xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
  3788. xmlRelaxNGDefinePtr def)
  3789. {
  3790. xmlRelaxNGDefinePtr **list;
  3791. xmlRelaxNGDefinePtr cur;
  3792. int nbchild = 0, i, j, ret;
  3793. int is_nullable = 0;
  3794. int is_indeterminist = 0;
  3795. xmlHashTablePtr triage = NULL;
  3796. int is_triable = 1;
  3797. if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE))
  3798. return;
  3799. if (def->dflags & IS_PROCESSED)
  3800. return;
  3801. /*
  3802. * Don't run that check in case of error. Infinite recursion
  3803. * becomes possible.
  3804. */
  3805. if (ctxt->nbErrors != 0)
  3806. return;
  3807. is_nullable = xmlRelaxNGIsNullable(def);
  3808. cur = def->content;
  3809. while (cur != NULL) {
  3810. nbchild++;
  3811. cur = cur->next;
  3812. }
  3813. list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
  3814. sizeof(xmlRelaxNGDefinePtr
  3815. *));
  3816. if (list == NULL) {
  3817. xmlRngPErrMemory(ctxt, "building choice\n");
  3818. return;
  3819. }
  3820. i = 0;
  3821. /*
  3822. * a bit strong but safe
  3823. */
  3824. if (is_nullable == 0) {
  3825. triage = xmlHashCreate(10);
  3826. } else {
  3827. is_triable = 0;
  3828. }
  3829. cur = def->content;
  3830. while (cur != NULL) {
  3831. list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
  3832. if ((list[i] == NULL) || (list[i][0] == NULL)) {
  3833. is_triable = 0;
  3834. } else if (is_triable == 1) {
  3835. xmlRelaxNGDefinePtr *tmp;
  3836. int res;
  3837. tmp = list[i];
  3838. while ((*tmp != NULL) && (is_triable == 1)) {
  3839. if ((*tmp)->type == XML_RELAXNG_TEXT) {
  3840. res = xmlHashAddEntry2(triage,
  3841. BAD_CAST "#text", NULL,
  3842. (void *) cur);
  3843. if (res != 0)
  3844. is_triable = -1;
  3845. } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
  3846. ((*tmp)->name != NULL)) {
  3847. if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
  3848. res = xmlHashAddEntry2(triage,
  3849. (*tmp)->name, NULL,
  3850. (void *) cur);
  3851. else
  3852. res = xmlHashAddEntry2(triage,
  3853. (*tmp)->name, (*tmp)->ns,
  3854. (void *) cur);
  3855. if (res != 0)
  3856. is_triable = -1;
  3857. } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
  3858. if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
  3859. res = xmlHashAddEntry2(triage,
  3860. BAD_CAST "#any", NULL,
  3861. (void *) cur);
  3862. else
  3863. res = xmlHashAddEntry2(triage,
  3864. BAD_CAST "#any", (*tmp)->ns,
  3865. (void *) cur);
  3866. if (res != 0)
  3867. is_triable = -1;
  3868. } else {
  3869. is_triable = -1;
  3870. }
  3871. tmp++;
  3872. }
  3873. }
  3874. i++;
  3875. cur = cur->next;
  3876. }
  3877. for (i = 0; i < nbchild; i++) {
  3878. if (list[i] == NULL)
  3879. continue;
  3880. for (j = 0; j < i; j++) {
  3881. if (list[j] == NULL)
  3882. continue;
  3883. ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
  3884. if (ret == 0) {
  3885. is_indeterminist = 1;
  3886. }
  3887. }
  3888. }
  3889. for (i = 0; i < nbchild; i++) {
  3890. if (list[i] != NULL)
  3891. xmlFree(list[i]);
  3892. }
  3893. xmlFree(list);
  3894. if (is_indeterminist) {
  3895. def->dflags |= IS_INDETERMINIST;
  3896. }
  3897. if (is_triable == 1) {
  3898. def->dflags |= IS_TRIABLE;
  3899. def->data = triage;
  3900. } else if (triage != NULL) {
  3901. xmlHashFree(triage, NULL);
  3902. }
  3903. def->dflags |= IS_PROCESSED;
  3904. }
  3905. /**
  3906. * xmlRelaxNGCheckGroupAttrs:
  3907. * @ctxt: a Relax-NG parser context
  3908. * @def: the group definition
  3909. *
  3910. * Detects violations of rule 7.3
  3911. */
  3912. static void
  3913. xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
  3914. xmlRelaxNGDefinePtr def)
  3915. {
  3916. xmlRelaxNGDefinePtr **list;
  3917. xmlRelaxNGDefinePtr cur;
  3918. int nbchild = 0, i, j, ret;
  3919. if ((def == NULL) ||
  3920. ((def->type != XML_RELAXNG_GROUP) &&
  3921. (def->type != XML_RELAXNG_ELEMENT)))
  3922. return;
  3923. if (def->dflags & IS_PROCESSED)
  3924. return;
  3925. /*
  3926. * Don't run that check in case of error. Infinite recursion
  3927. * becomes possible.
  3928. */
  3929. if (ctxt->nbErrors != 0)
  3930. return;
  3931. cur = def->attrs;
  3932. while (cur != NULL) {
  3933. nbchild++;
  3934. cur = cur->next;
  3935. }
  3936. cur = def->content;
  3937. while (cur != NULL) {
  3938. nbchild++;
  3939. cur = cur->next;
  3940. }
  3941. list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
  3942. sizeof(xmlRelaxNGDefinePtr
  3943. *));
  3944. if (list == NULL) {
  3945. xmlRngPErrMemory(ctxt, "building group\n");
  3946. return;
  3947. }
  3948. i = 0;
  3949. cur = def->attrs;
  3950. while (cur != NULL) {
  3951. list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
  3952. i++;
  3953. cur = cur->next;
  3954. }
  3955. cur = def->content;
  3956. while (cur != NULL) {
  3957. list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
  3958. i++;
  3959. cur = cur->next;
  3960. }
  3961. for (i = 0; i < nbchild; i++) {
  3962. if (list[i] == NULL)
  3963. continue;
  3964. for (j = 0; j < i; j++) {
  3965. if (list[j] == NULL)
  3966. continue;
  3967. ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
  3968. if (ret == 0) {
  3969. xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT,
  3970. "Attributes conflicts in group\n", NULL, NULL);
  3971. }
  3972. }
  3973. }
  3974. for (i = 0; i < nbchild; i++) {
  3975. if (list[i] != NULL)
  3976. xmlFree(list[i]);
  3977. }
  3978. xmlFree(list);
  3979. def->dflags |= IS_PROCESSED;
  3980. }
  3981. /**
  3982. * xmlRelaxNGComputeInterleaves:
  3983. * @def: the interleave definition
  3984. * @ctxt: a Relax-NG parser context
  3985. * @name: the definition name
  3986. *
  3987. * A lot of work for preprocessing interleave definitions
  3988. * is potentially needed to get a decent execution speed at runtime
  3989. * - trying to get a total order on the element nodes generated
  3990. * by the interleaves, order the list of interleave definitions
  3991. * following that order.
  3992. * - if <text/> is used to handle mixed content, it is better to
  3993. * flag this in the define and simplify the runtime checking
  3994. * algorithm
  3995. */
  3996. static void
  3997. xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
  3998. xmlRelaxNGParserCtxtPtr ctxt,
  3999. xmlChar * name ATTRIBUTE_UNUSED)
  4000. {
  4001. xmlRelaxNGDefinePtr cur, *tmp;
  4002. xmlRelaxNGPartitionPtr partitions = NULL;
  4003. xmlRelaxNGInterleaveGroupPtr *groups = NULL;
  4004. xmlRelaxNGInterleaveGroupPtr group;
  4005. int i, j, ret, res;
  4006. int nbgroups = 0;
  4007. int nbchild = 0;
  4008. int is_mixed = 0;
  4009. int is_determinist = 1;
  4010. /*
  4011. * Don't run that check in case of error. Infinite recursion
  4012. * becomes possible.
  4013. */
  4014. if (ctxt->nbErrors != 0)
  4015. return;
  4016. #ifdef DEBUG_INTERLEAVE
  4017. xmlGenericError(xmlGenericErrorContext,
  4018. "xmlRelaxNGComputeInterleaves(%s)\n", name);
  4019. #endif
  4020. cur = def->content;
  4021. while (cur != NULL) {
  4022. nbchild++;
  4023. cur = cur->next;
  4024. }
  4025. #ifdef DEBUG_INTERLEAVE
  4026. xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
  4027. #endif
  4028. groups = (xmlRelaxNGInterleaveGroupPtr *)
  4029. xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
  4030. if (groups == NULL)
  4031. goto error;
  4032. cur = def->content;
  4033. while (cur != NULL) {
  4034. groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
  4035. xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
  4036. if (groups[nbgroups] == NULL)
  4037. goto error;
  4038. if (cur->type == XML_RELAXNG_TEXT)
  4039. is_mixed++;
  4040. groups[nbgroups]->rule = cur;
  4041. groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
  4042. groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
  4043. nbgroups++;
  4044. cur = cur->next;
  4045. }
  4046. #ifdef DEBUG_INTERLEAVE
  4047. xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
  4048. #endif
  4049. /*
  4050. * Let's check that all rules makes a partitions according to 7.4
  4051. */
  4052. partitions = (xmlRelaxNGPartitionPtr)
  4053. xmlMalloc(sizeof(xmlRelaxNGPartition));
  4054. if (partitions == NULL)
  4055. goto error;
  4056. memset(partitions, 0, sizeof(xmlRelaxNGPartition));
  4057. partitions->nbgroups = nbgroups;
  4058. partitions->triage = xmlHashCreate(nbgroups);
  4059. for (i = 0; i < nbgroups; i++) {
  4060. group = groups[i];
  4061. for (j = i + 1; j < nbgroups; j++) {
  4062. if (groups[j] == NULL)
  4063. continue;
  4064. ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
  4065. groups[j]->defs);
  4066. if (ret == 0) {
  4067. xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT,
  4068. "Element or text conflicts in interleave\n",
  4069. NULL, NULL);
  4070. }
  4071. ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
  4072. groups[j]->attrs);
  4073. if (ret == 0) {
  4074. xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT,
  4075. "Attributes conflicts in interleave\n", NULL,
  4076. NULL);
  4077. }
  4078. }
  4079. tmp = group->defs;
  4080. if ((tmp != NULL) && (*tmp != NULL)) {
  4081. while (*tmp != NULL) {
  4082. if ((*tmp)->type == XML_RELAXNG_TEXT) {
  4083. res = xmlHashAddEntry2(partitions->triage,
  4084. BAD_CAST "#text", NULL,
  4085. (void *) (long) (i + 1));
  4086. if (res != 0)
  4087. is_determinist = -1;
  4088. } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
  4089. ((*tmp)->name != NULL)) {
  4090. if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
  4091. res = xmlHashAddEntry2(partitions->triage,
  4092. (*tmp)->name, NULL,
  4093. (void *) (long) (i + 1));
  4094. else
  4095. res = xmlHashAddEntry2(partitions->triage,
  4096. (*tmp)->name, (*tmp)->ns,
  4097. (void *) (long) (i + 1));
  4098. if (res != 0)
  4099. is_determinist = -1;
  4100. } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
  4101. if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
  4102. res = xmlHashAddEntry2(partitions->triage,
  4103. BAD_CAST "#any", NULL,
  4104. (void *) (long) (i + 1));
  4105. else
  4106. res = xmlHashAddEntry2(partitions->triage,
  4107. BAD_CAST "#any", (*tmp)->ns,
  4108. (void *) (long) (i + 1));
  4109. if ((*tmp)->nameClass != NULL)
  4110. is_determinist = 2;
  4111. if (res != 0)
  4112. is_determinist = -1;
  4113. } else {
  4114. is_determinist = -1;
  4115. }
  4116. tmp++;
  4117. }
  4118. } else {
  4119. is_determinist = 0;
  4120. }
  4121. }
  4122. partitions->groups = groups;
  4123. /*
  4124. * and save the partition list back in the def
  4125. */
  4126. def->data = partitions;
  4127. if (is_mixed != 0)
  4128. def->dflags |= IS_MIXED;
  4129. if (is_determinist == 1)
  4130. partitions->flags = IS_DETERMINIST;
  4131. if (is_determinist == 2)
  4132. partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
  4133. return;
  4134. error:
  4135. xmlRngPErrMemory(ctxt, "in interleave computation\n");
  4136. if (groups != NULL) {
  4137. for (i = 0; i < nbgroups; i++)
  4138. if (groups[i] != NULL) {
  4139. if (groups[i]->defs != NULL)
  4140. xmlFree(groups[i]->defs);
  4141. xmlFree(groups[i]);
  4142. }
  4143. xmlFree(groups);
  4144. }
  4145. xmlRelaxNGFreePartition(partitions);
  4146. }
  4147. /**
  4148. * xmlRelaxNGParseInterleave:
  4149. * @ctxt: a Relax-NG parser context
  4150. * @node: the data node.
  4151. *
  4152. * parse the content of a RelaxNG interleave node.
  4153. *
  4154. * Returns the definition pointer or NULL in case of error
  4155. */
  4156. static xmlRelaxNGDefinePtr
  4157. xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
  4158. {
  4159. xmlRelaxNGDefinePtr def = NULL;
  4160. xmlRelaxNGDefinePtr last = NULL, cur;
  4161. xmlNodePtr child;
  4162. def = xmlRelaxNGNewDefine(ctxt, node);
  4163. if (def == NULL) {
  4164. return (NULL);
  4165. }
  4166. def->type = XML_RELAXNG_INTERLEAVE;
  4167. if (ctxt->interleaves == NULL)
  4168. ctxt->interleaves = xmlHashCreate(10);
  4169. if (ctxt->interleaves == NULL) {
  4170. xmlRngPErrMemory(ctxt, "create interleaves\n");
  4171. } else {
  4172. char name[32];
  4173. snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
  4174. if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
  4175. xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD,
  4176. "Failed to add %s to hash table\n",
  4177. (const xmlChar *) name, NULL);
  4178. }
  4179. }
  4180. child = node->children;
  4181. if (child == NULL) {
  4182. xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT,
  4183. "Element interleave is empty\n", NULL, NULL);
  4184. }
  4185. while (child != NULL) {
  4186. if (IS_RELAXNG(child, "element")) {
  4187. cur = xmlRelaxNGParseElement(ctxt, child);
  4188. } else {
  4189. cur = xmlRelaxNGParsePattern(ctxt, child);
  4190. }
  4191. if (cur != NULL) {
  4192. cur->parent = def;
  4193. if (last == NULL) {
  4194. def->content = last = cur;
  4195. } else {
  4196. last->next = cur;
  4197. last = cur;
  4198. }
  4199. }
  4200. child = child->next;
  4201. }
  4202. return (def);
  4203. }
  4204. /**
  4205. * xmlRelaxNGParseInclude:
  4206. * @ctxt: a Relax-NG parser context
  4207. * @node: the include node
  4208. *
  4209. * Integrate the content of an include node in the current grammar
  4210. *
  4211. * Returns 0 in case of success or -1 in case of error
  4212. */
  4213. static int
  4214. xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
  4215. {
  4216. xmlRelaxNGIncludePtr incl;
  4217. xmlNodePtr root;
  4218. int ret = 0, tmp;
  4219. incl = node->psvi;
  4220. if (incl == NULL) {
  4221. xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY,
  4222. "Include node has no data\n", NULL, NULL);
  4223. return (-1);
  4224. }
  4225. root = xmlDocGetRootElement(incl->doc);
  4226. if (root == NULL) {
  4227. xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n",
  4228. NULL, NULL);
  4229. return (-1);
  4230. }
  4231. if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
  4232. xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
  4233. "Include document root is not a grammar\n", NULL, NULL);
  4234. return (-1);
  4235. }
  4236. /*
  4237. * Merge the definition from both the include and the internal list
  4238. */
  4239. if (root->children != NULL) {
  4240. tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
  4241. if (tmp != 0)
  4242. ret = -1;
  4243. }
  4244. if (node->children != NULL) {
  4245. tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
  4246. if (tmp != 0)
  4247. ret = -1;
  4248. }
  4249. return (ret);
  4250. }
  4251. /**
  4252. * xmlRelaxNGParseDefine:
  4253. * @ctxt: a Relax-NG parser context
  4254. * @node: the define node
  4255. *
  4256. * parse the content of a RelaxNG define element node.
  4257. *
  4258. * Returns 0 in case of success or -1 in case of error
  4259. */
  4260. static int
  4261. xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
  4262. {
  4263. xmlChar *name;
  4264. int ret = 0, tmp;
  4265. xmlRelaxNGDefinePtr def;
  4266. const xmlChar *olddefine;
  4267. name = xmlGetProp(node, BAD_CAST "name");
  4268. if (name == NULL) {
  4269. xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING,
  4270. "define has no name\n", NULL, NULL);
  4271. } else {
  4272. xmlRelaxNGNormExtSpace(name);
  4273. if (xmlValidateNCName(name, 0)) {
  4274. xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME,
  4275. "define name '%s' is not an NCName\n", name, NULL);
  4276. }
  4277. def = xmlRelaxNGNewDefine(ctxt, node);
  4278. if (def == NULL) {
  4279. xmlFree(name);
  4280. return (-1);
  4281. }
  4282. def->type = XML_RELAXNG_DEF;
  4283. def->name = name;
  4284. if (node->children == NULL) {
  4285. xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY,
  4286. "define has no children\n", NULL, NULL);
  4287. } else {
  4288. olddefine = ctxt->define;
  4289. ctxt->define = name;
  4290. def->content =
  4291. xmlRelaxNGParsePatterns(ctxt, node->children, 0);
  4292. ctxt->define = olddefine;
  4293. }
  4294. if (ctxt->grammar->defs == NULL)
  4295. ctxt->grammar->defs = xmlHashCreate(10);
  4296. if (ctxt->grammar->defs == NULL) {
  4297. xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
  4298. "Could not create definition hash\n", NULL, NULL);
  4299. ret = -1;
  4300. } else {
  4301. tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
  4302. if (tmp < 0) {
  4303. xmlRelaxNGDefinePtr prev;
  4304. prev = xmlHashLookup(ctxt->grammar->defs, name);
  4305. if (prev == NULL) {
  4306. xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
  4307. "Internal error on define aggregation of %s\n",
  4308. name, NULL);
  4309. ret = -1;
  4310. } else {
  4311. while (prev->nextHash != NULL)
  4312. prev = prev->nextHash;
  4313. prev->nextHash = def;
  4314. }
  4315. }
  4316. }
  4317. }
  4318. return (ret);
  4319. }
  4320. /**
  4321. * xmlRelaxNGParseImportRef:
  4322. * @payload: the parser context
  4323. * @data: the current grammar
  4324. * @name: the reference name
  4325. *
  4326. * Import import one references into the current grammar
  4327. */
  4328. static void
  4329. xmlRelaxNGParseImportRef(void *payload, void *data, xmlChar *name) {
  4330. xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
  4331. xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload;
  4332. int tmp;
  4333. def->dflags |= IS_EXTERNAL_REF;
  4334. tmp = xmlHashAddEntry(ctxt->grammar->refs, name, def);
  4335. if (tmp < 0) {
  4336. xmlRelaxNGDefinePtr prev;
  4337. prev = (xmlRelaxNGDefinePtr)
  4338. xmlHashLookup(ctxt->grammar->refs, def->name);
  4339. if (prev == NULL) {
  4340. if (def->name != NULL) {
  4341. xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
  4342. "Error refs definitions '%s'\n",
  4343. def->name, NULL);
  4344. } else {
  4345. xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
  4346. "Error refs definitions\n",
  4347. NULL, NULL);
  4348. }
  4349. } else {
  4350. def->nextHash = prev->nextHash;
  4351. prev->nextHash = def;
  4352. }
  4353. }
  4354. }
  4355. /**
  4356. * xmlRelaxNGParseImportRefs:
  4357. * @ctxt: the parser context
  4358. * @grammar: the sub grammar
  4359. *
  4360. * Import references from the subgrammar into the current grammar
  4361. *
  4362. * Returns 0 in case of success, -1 in case of failure
  4363. */
  4364. static int
  4365. xmlRelaxNGParseImportRefs(xmlRelaxNGParserCtxtPtr ctxt,
  4366. xmlRelaxNGGrammarPtr grammar) {
  4367. if ((ctxt == NULL) || (grammar == NULL) || (ctxt->grammar == NULL))
  4368. return(-1);
  4369. if (grammar->refs == NULL)
  4370. return(0);
  4371. if (ctxt->grammar->refs == NULL)
  4372. ctxt->grammar->refs = xmlHashCreate(10);
  4373. if (ctxt->grammar->refs == NULL) {
  4374. xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
  4375. "Could not create references hash\n", NULL, NULL);
  4376. return(-1);
  4377. }
  4378. xmlHashScan(grammar->refs, xmlRelaxNGParseImportRef, ctxt);
  4379. return(0);
  4380. }
  4381. /**
  4382. * xmlRelaxNGProcessExternalRef:
  4383. * @ctxt: the parser context
  4384. * @node: the externlRef node
  4385. *
  4386. * Process and compile an externlRef node
  4387. *
  4388. * Returns the xmlRelaxNGDefinePtr or NULL in case of error
  4389. */
  4390. static xmlRelaxNGDefinePtr
  4391. xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
  4392. {
  4393. xmlRelaxNGDocumentPtr docu;
  4394. xmlNodePtr root, tmp;
  4395. xmlChar *ns;
  4396. int newNs = 0, oldflags;
  4397. xmlRelaxNGDefinePtr def;
  4398. docu = node->psvi;
  4399. if (docu != NULL) {
  4400. def = xmlRelaxNGNewDefine(ctxt, node);
  4401. if (def == NULL)
  4402. return (NULL);
  4403. def->type = XML_RELAXNG_EXTERNALREF;
  4404. if (docu->content == NULL) {
  4405. /*
  4406. * Then do the parsing for good
  4407. */
  4408. root = xmlDocGetRootElement(docu->doc);
  4409. if (root == NULL) {
  4410. xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY,
  4411. "xmlRelaxNGParse: %s is empty\n", ctxt->URL,
  4412. NULL);
  4413. return (NULL);
  4414. }
  4415. /*
  4416. * ns transmission rules
  4417. */
  4418. ns = xmlGetProp(root, BAD_CAST "ns");
  4419. if (ns == NULL) {
  4420. tmp = node;
  4421. while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) {
  4422. ns = xmlGetProp(tmp, BAD_CAST "ns");
  4423. if (ns != NULL) {
  4424. break;
  4425. }
  4426. tmp = tmp->parent;
  4427. }
  4428. if (ns != NULL) {
  4429. xmlSetProp(root, BAD_CAST "ns", ns);
  4430. newNs = 1;
  4431. xmlFree(ns);
  4432. }
  4433. } else {
  4434. xmlFree(ns);
  4435. }
  4436. /*
  4437. * Parsing to get a precompiled schemas.
  4438. */
  4439. oldflags = ctxt->flags;
  4440. ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
  4441. docu->schema = xmlRelaxNGParseDocument(ctxt, root);
  4442. ctxt->flags = oldflags;
  4443. if ((docu->schema != NULL) &&
  4444. (docu->schema->topgrammar != NULL)) {
  4445. docu->content = docu->schema->topgrammar->start;
  4446. if (docu->schema->topgrammar->refs)
  4447. xmlRelaxNGParseImportRefs(ctxt, docu->schema->topgrammar);
  4448. }
  4449. /*
  4450. * the externalRef may be reused in a different ns context
  4451. */
  4452. if (newNs == 1) {
  4453. xmlUnsetProp(root, BAD_CAST "ns");
  4454. }
  4455. }
  4456. def->content = docu->content;
  4457. } else {
  4458. def = NULL;
  4459. }
  4460. return (def);
  4461. }
  4462. /**
  4463. * xmlRelaxNGParsePattern:
  4464. * @ctxt: a Relax-NG parser context
  4465. * @node: the pattern node.
  4466. *
  4467. * parse the content of a RelaxNG pattern node.
  4468. *
  4469. * Returns the definition pointer or NULL in case of error or if no
  4470. * pattern is generated.
  4471. */
  4472. static xmlRelaxNGDefinePtr
  4473. xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
  4474. {
  4475. xmlRelaxNGDefinePtr def = NULL;
  4476. if (node == NULL) {
  4477. return (NULL);
  4478. }
  4479. if (IS_RELAXNG(node, "element")) {
  4480. def = xmlRelaxNGParseElement(ctxt, node);
  4481. } else if (IS_RELAXNG(node, "attribute")) {
  4482. def = xmlRelaxNGParseAttribute(ctxt, node);
  4483. } else if (IS_RELAXNG(node, "empty")) {
  4484. def = xmlRelaxNGNewDefine(ctxt, node);
  4485. if (def == NULL)
  4486. return (NULL);
  4487. def->type = XML_RELAXNG_EMPTY;
  4488. if (node->children != NULL) {
  4489. xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY,
  4490. "empty: had a child node\n", NULL, NULL);
  4491. }
  4492. } else if (IS_RELAXNG(node, "text")) {
  4493. def = xmlRelaxNGNewDefine(ctxt, node);
  4494. if (def == NULL)
  4495. return (NULL);
  4496. def->type = XML_RELAXNG_TEXT;
  4497. if (node->children != NULL) {
  4498. xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD,
  4499. "text: had a child node\n", NULL, NULL);
  4500. }
  4501. } else if (IS_RELAXNG(node, "zeroOrMore")) {
  4502. def = xmlRelaxNGNewDefine(ctxt, node);
  4503. if (def == NULL)
  4504. return (NULL);
  4505. def->type = XML_RELAXNG_ZEROORMORE;
  4506. if (node->children == NULL) {
  4507. xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
  4508. "Element %s is empty\n", node->name, NULL);
  4509. } else {
  4510. def->content =
  4511. xmlRelaxNGParsePatterns(ctxt, node->children, 1);
  4512. }
  4513. } else if (IS_RELAXNG(node, "oneOrMore")) {
  4514. def = xmlRelaxNGNewDefine(ctxt, node);
  4515. if (def == NULL)
  4516. return (NULL);
  4517. def->type = XML_RELAXNG_ONEORMORE;
  4518. if (node->children == NULL) {
  4519. xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
  4520. "Element %s is empty\n", node->name, NULL);
  4521. } else {
  4522. def->content =
  4523. xmlRelaxNGParsePatterns(ctxt, node->children, 1);
  4524. }
  4525. } else if (IS_RELAXNG(node, "optional")) {
  4526. def = xmlRelaxNGNewDefine(ctxt, node);
  4527. if (def == NULL)
  4528. return (NULL);
  4529. def->type = XML_RELAXNG_OPTIONAL;
  4530. if (node->children == NULL) {
  4531. xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
  4532. "Element %s is empty\n", node->name, NULL);
  4533. } else {
  4534. def->content =
  4535. xmlRelaxNGParsePatterns(ctxt, node->children, 1);
  4536. }
  4537. } else if (IS_RELAXNG(node, "choice")) {
  4538. def = xmlRelaxNGNewDefine(ctxt, node);
  4539. if (def == NULL)
  4540. return (NULL);
  4541. def->type = XML_RELAXNG_CHOICE;
  4542. if (node->children == NULL) {
  4543. xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
  4544. "Element %s is empty\n", node->name, NULL);
  4545. } else {
  4546. def->content =
  4547. xmlRelaxNGParsePatterns(ctxt, node->children, 0);
  4548. }
  4549. } else if (IS_RELAXNG(node, "group")) {
  4550. def = xmlRelaxNGNewDefine(ctxt, node);
  4551. if (def == NULL)
  4552. return (NULL);
  4553. def->type = XML_RELAXNG_GROUP;
  4554. if (node->children == NULL) {
  4555. xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
  4556. "Element %s is empty\n", node->name, NULL);
  4557. } else {
  4558. def->content =
  4559. xmlRelaxNGParsePatterns(ctxt, node->children, 0);
  4560. }
  4561. } else if (IS_RELAXNG(node, "ref")) {
  4562. def = xmlRelaxNGNewDefine(ctxt, node);
  4563. if (def == NULL)
  4564. return (NULL);
  4565. def->type = XML_RELAXNG_REF;
  4566. def->name = xmlGetProp(node, BAD_CAST "name");
  4567. if (def->name == NULL) {
  4568. xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n",
  4569. NULL, NULL);
  4570. } else {
  4571. xmlRelaxNGNormExtSpace(def->name);
  4572. if (xmlValidateNCName(def->name, 0)) {
  4573. xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID,
  4574. "ref name '%s' is not an NCName\n", def->name,
  4575. NULL);
  4576. }
  4577. }
  4578. if (node->children != NULL) {
  4579. xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n",
  4580. NULL, NULL);
  4581. }
  4582. if (ctxt->grammar->refs == NULL)
  4583. ctxt->grammar->refs = xmlHashCreate(10);
  4584. if (ctxt->grammar->refs == NULL) {
  4585. xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
  4586. "Could not create references hash\n", NULL, NULL);
  4587. def = NULL;
  4588. } else {
  4589. int tmp;
  4590. tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
  4591. if (tmp < 0) {
  4592. xmlRelaxNGDefinePtr prev;
  4593. prev = (xmlRelaxNGDefinePtr)
  4594. xmlHashLookup(ctxt->grammar->refs, def->name);
  4595. if (prev == NULL) {
  4596. if (def->name != NULL) {
  4597. xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
  4598. "Error refs definitions '%s'\n",
  4599. def->name, NULL);
  4600. } else {
  4601. xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
  4602. "Error refs definitions\n",
  4603. NULL, NULL);
  4604. }
  4605. def = NULL;
  4606. } else {
  4607. def->nextHash = prev->nextHash;
  4608. prev->nextHash = def;
  4609. }
  4610. }
  4611. }
  4612. } else if (IS_RELAXNG(node, "data")) {
  4613. def = xmlRelaxNGParseData(ctxt, node);
  4614. } else if (IS_RELAXNG(node, "value")) {
  4615. def = xmlRelaxNGParseValue(ctxt, node);
  4616. } else if (IS_RELAXNG(node, "list")) {
  4617. def = xmlRelaxNGNewDefine(ctxt, node);
  4618. if (def == NULL)
  4619. return (NULL);
  4620. def->type = XML_RELAXNG_LIST;
  4621. if (node->children == NULL) {
  4622. xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
  4623. "Element %s is empty\n", node->name, NULL);
  4624. } else {
  4625. def->content =
  4626. xmlRelaxNGParsePatterns(ctxt, node->children, 0);
  4627. }
  4628. } else if (IS_RELAXNG(node, "interleave")) {
  4629. def = xmlRelaxNGParseInterleave(ctxt, node);
  4630. } else if (IS_RELAXNG(node, "externalRef")) {
  4631. def = xmlRelaxNGProcessExternalRef(ctxt, node);
  4632. } else if (IS_RELAXNG(node, "notAllowed")) {
  4633. def = xmlRelaxNGNewDefine(ctxt, node);
  4634. if (def == NULL)
  4635. return (NULL);
  4636. def->type = XML_RELAXNG_NOT_ALLOWED;
  4637. if (node->children != NULL) {
  4638. xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY,
  4639. "xmlRelaxNGParse: notAllowed element is not empty\n",
  4640. NULL, NULL);
  4641. }
  4642. } else if (IS_RELAXNG(node, "grammar")) {
  4643. xmlRelaxNGGrammarPtr grammar, old;
  4644. xmlRelaxNGGrammarPtr oldparent;
  4645. #ifdef DEBUG_GRAMMAR
  4646. xmlGenericError(xmlGenericErrorContext,
  4647. "Found <grammar> pattern\n");
  4648. #endif
  4649. oldparent = ctxt->parentgrammar;
  4650. old = ctxt->grammar;
  4651. ctxt->parentgrammar = old;
  4652. grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
  4653. if (old != NULL) {
  4654. ctxt->grammar = old;
  4655. ctxt->parentgrammar = oldparent;
  4656. #if 0
  4657. if (grammar != NULL) {
  4658. grammar->next = old->next;
  4659. old->next = grammar;
  4660. }
  4661. #endif
  4662. }
  4663. if (grammar != NULL)
  4664. def = grammar->start;
  4665. else
  4666. def = NULL;
  4667. } else if (IS_RELAXNG(node, "parentRef")) {
  4668. if (ctxt->parentgrammar == NULL) {
  4669. xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT,
  4670. "Use of parentRef without a parent grammar\n", NULL,
  4671. NULL);
  4672. return (NULL);
  4673. }
  4674. def = xmlRelaxNGNewDefine(ctxt, node);
  4675. if (def == NULL)
  4676. return (NULL);
  4677. def->type = XML_RELAXNG_PARENTREF;
  4678. def->name = xmlGetProp(node, BAD_CAST "name");
  4679. if (def->name == NULL) {
  4680. xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME,
  4681. "parentRef has no name\n", NULL, NULL);
  4682. } else {
  4683. xmlRelaxNGNormExtSpace(def->name);
  4684. if (xmlValidateNCName(def->name, 0)) {
  4685. xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID,
  4686. "parentRef name '%s' is not an NCName\n",
  4687. def->name, NULL);
  4688. }
  4689. }
  4690. if (node->children != NULL) {
  4691. xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY,
  4692. "parentRef is not empty\n", NULL, NULL);
  4693. }
  4694. if (ctxt->parentgrammar->refs == NULL)
  4695. ctxt->parentgrammar->refs = xmlHashCreate(10);
  4696. if (ctxt->parentgrammar->refs == NULL) {
  4697. xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
  4698. "Could not create references hash\n", NULL, NULL);
  4699. def = NULL;
  4700. } else if (def->name != NULL) {
  4701. int tmp;
  4702. tmp =
  4703. xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
  4704. if (tmp < 0) {
  4705. xmlRelaxNGDefinePtr prev;
  4706. prev = (xmlRelaxNGDefinePtr)
  4707. xmlHashLookup(ctxt->parentgrammar->refs, def->name);
  4708. if (prev == NULL) {
  4709. xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
  4710. "Internal error parentRef definitions '%s'\n",
  4711. def->name, NULL);
  4712. def = NULL;
  4713. } else {
  4714. def->nextHash = prev->nextHash;
  4715. prev->nextHash = def;
  4716. }
  4717. }
  4718. }
  4719. } else if (IS_RELAXNG(node, "mixed")) {
  4720. if (node->children == NULL) {
  4721. xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n",
  4722. NULL, NULL);
  4723. def = NULL;
  4724. } else {
  4725. def = xmlRelaxNGParseInterleave(ctxt, node);
  4726. if (def != NULL) {
  4727. xmlRelaxNGDefinePtr tmp;
  4728. if ((def->content != NULL) && (def->content->next != NULL)) {
  4729. tmp = xmlRelaxNGNewDefine(ctxt, node);
  4730. if (tmp != NULL) {
  4731. tmp->type = XML_RELAXNG_GROUP;
  4732. tmp->content = def->content;
  4733. def->content = tmp;
  4734. }
  4735. }
  4736. tmp = xmlRelaxNGNewDefine(ctxt, node);
  4737. if (tmp == NULL)
  4738. return (def);
  4739. tmp->type = XML_RELAXNG_TEXT;
  4740. tmp->next = def->content;
  4741. def->content = tmp;
  4742. }
  4743. }
  4744. } else {
  4745. xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT,
  4746. "Unexpected node %s is not a pattern\n", node->name,
  4747. NULL);
  4748. def = NULL;
  4749. }
  4750. return (def);
  4751. }
  4752. /**
  4753. * xmlRelaxNGParseAttribute:
  4754. * @ctxt: a Relax-NG parser context
  4755. * @node: the element node
  4756. *
  4757. * parse the content of a RelaxNG attribute node.
  4758. *
  4759. * Returns the definition pointer or NULL in case of error.
  4760. */
  4761. static xmlRelaxNGDefinePtr
  4762. xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
  4763. {
  4764. xmlRelaxNGDefinePtr ret, cur;
  4765. xmlNodePtr child;
  4766. int old_flags;
  4767. ret = xmlRelaxNGNewDefine(ctxt, node);
  4768. if (ret == NULL)
  4769. return (NULL);
  4770. ret->type = XML_RELAXNG_ATTRIBUTE;
  4771. ret->parent = ctxt->def;
  4772. child = node->children;
  4773. if (child == NULL) {
  4774. xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY,
  4775. "xmlRelaxNGParseattribute: attribute has no children\n",
  4776. NULL, NULL);
  4777. return (ret);
  4778. }
  4779. old_flags = ctxt->flags;
  4780. ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
  4781. cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
  4782. if (cur != NULL)
  4783. child = child->next;
  4784. if (child != NULL) {
  4785. cur = xmlRelaxNGParsePattern(ctxt, child);
  4786. if (cur != NULL) {
  4787. switch (cur->type) {
  4788. case XML_RELAXNG_EMPTY:
  4789. case XML_RELAXNG_NOT_ALLOWED:
  4790. case XML_RELAXNG_TEXT:
  4791. case XML_RELAXNG_ELEMENT:
  4792. case XML_RELAXNG_DATATYPE:
  4793. case XML_RELAXNG_VALUE:
  4794. case XML_RELAXNG_LIST:
  4795. case XML_RELAXNG_REF:
  4796. case XML_RELAXNG_PARENTREF:
  4797. case XML_RELAXNG_EXTERNALREF:
  4798. case XML_RELAXNG_DEF:
  4799. case XML_RELAXNG_ONEORMORE:
  4800. case XML_RELAXNG_ZEROORMORE:
  4801. case XML_RELAXNG_OPTIONAL:
  4802. case XML_RELAXNG_CHOICE:
  4803. case XML_RELAXNG_GROUP:
  4804. case XML_RELAXNG_INTERLEAVE:
  4805. case XML_RELAXNG_ATTRIBUTE:
  4806. ret->content = cur;
  4807. cur->parent = ret;
  4808. break;
  4809. case XML_RELAXNG_START:
  4810. case XML_RELAXNG_PARAM:
  4811. case XML_RELAXNG_EXCEPT:
  4812. xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT,
  4813. "attribute has invalid content\n", NULL,
  4814. NULL);
  4815. break;
  4816. case XML_RELAXNG_NOOP:
  4817. xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP,
  4818. "RNG Internal error, noop found in attribute\n",
  4819. NULL, NULL);
  4820. break;
  4821. }
  4822. }
  4823. child = child->next;
  4824. }
  4825. if (child != NULL) {
  4826. xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN,
  4827. "attribute has multiple children\n", NULL, NULL);
  4828. }
  4829. ctxt->flags = old_flags;
  4830. return (ret);
  4831. }
  4832. /**
  4833. * xmlRelaxNGParseExceptNameClass:
  4834. * @ctxt: a Relax-NG parser context
  4835. * @node: the except node
  4836. * @attr: 1 if within an attribute, 0 if within an element
  4837. *
  4838. * parse the content of a RelaxNG nameClass node.
  4839. *
  4840. * Returns the definition pointer or NULL in case of error.
  4841. */
  4842. static xmlRelaxNGDefinePtr
  4843. xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
  4844. xmlNodePtr node, int attr)
  4845. {
  4846. xmlRelaxNGDefinePtr ret, cur, last = NULL;
  4847. xmlNodePtr child;
  4848. if (!IS_RELAXNG(node, "except")) {
  4849. xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING,
  4850. "Expecting an except node\n", NULL, NULL);
  4851. return (NULL);
  4852. }
  4853. if (node->next != NULL) {
  4854. xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE,
  4855. "exceptNameClass allows only a single except node\n",
  4856. NULL, NULL);
  4857. }
  4858. if (node->children == NULL) {
  4859. xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n",
  4860. NULL, NULL);
  4861. return (NULL);
  4862. }
  4863. ret = xmlRelaxNGNewDefine(ctxt, node);
  4864. if (ret == NULL)
  4865. return (NULL);
  4866. ret->type = XML_RELAXNG_EXCEPT;
  4867. child = node->children;
  4868. while (child != NULL) {
  4869. cur = xmlRelaxNGNewDefine(ctxt, child);
  4870. if (cur == NULL)
  4871. break;
  4872. if (attr)
  4873. cur->type = XML_RELAXNG_ATTRIBUTE;
  4874. else
  4875. cur->type = XML_RELAXNG_ELEMENT;
  4876. if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
  4877. if (last == NULL) {
  4878. ret->content = cur;
  4879. } else {
  4880. last->next = cur;
  4881. }
  4882. last = cur;
  4883. }
  4884. child = child->next;
  4885. }
  4886. return (ret);
  4887. }
  4888. /**
  4889. * xmlRelaxNGParseNameClass:
  4890. * @ctxt: a Relax-NG parser context
  4891. * @node: the nameClass node
  4892. * @def: the current definition
  4893. *
  4894. * parse the content of a RelaxNG nameClass node.
  4895. *
  4896. * Returns the definition pointer or NULL in case of error.
  4897. */
  4898. static xmlRelaxNGDefinePtr
  4899. xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
  4900. xmlRelaxNGDefinePtr def)
  4901. {
  4902. xmlRelaxNGDefinePtr ret, tmp;
  4903. xmlChar *val;
  4904. ret = def;
  4905. if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
  4906. (IS_RELAXNG(node, "nsName"))) {
  4907. if ((def->type != XML_RELAXNG_ELEMENT) &&
  4908. (def->type != XML_RELAXNG_ATTRIBUTE)) {
  4909. ret = xmlRelaxNGNewDefine(ctxt, node);
  4910. if (ret == NULL)
  4911. return (NULL);
  4912. ret->parent = def;
  4913. if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
  4914. ret->type = XML_RELAXNG_ATTRIBUTE;
  4915. else
  4916. ret->type = XML_RELAXNG_ELEMENT;
  4917. }
  4918. }
  4919. if (IS_RELAXNG(node, "name")) {
  4920. val = xmlNodeGetContent(node);
  4921. xmlRelaxNGNormExtSpace(val);
  4922. if (xmlValidateNCName(val, 0)) {
  4923. if (node->parent != NULL)
  4924. xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
  4925. "Element %s name '%s' is not an NCName\n",
  4926. node->parent->name, val);
  4927. else
  4928. xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
  4929. "name '%s' is not an NCName\n",
  4930. val, NULL);
  4931. }
  4932. ret->name = val;
  4933. val = xmlGetProp(node, BAD_CAST "ns");
  4934. ret->ns = val;
  4935. if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
  4936. (val != NULL) &&
  4937. (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
  4938. xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
  4939. "Attribute with namespace '%s' is not allowed\n",
  4940. val, NULL);
  4941. }
  4942. if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
  4943. (val != NULL) &&
  4944. (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
  4945. xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME,
  4946. "Attribute with QName 'xmlns' is not allowed\n",
  4947. val, NULL);
  4948. }
  4949. } else if (IS_RELAXNG(node, "anyName")) {
  4950. ret->name = NULL;
  4951. ret->ns = NULL;
  4952. if (node->children != NULL) {
  4953. ret->nameClass =
  4954. xmlRelaxNGParseExceptNameClass(ctxt, node->children,
  4955. (def->type ==
  4956. XML_RELAXNG_ATTRIBUTE));
  4957. }
  4958. } else if (IS_RELAXNG(node, "nsName")) {
  4959. ret->name = NULL;
  4960. ret->ns = xmlGetProp(node, BAD_CAST "ns");
  4961. if (ret->ns == NULL) {
  4962. xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS,
  4963. "nsName has no ns attribute\n", NULL, NULL);
  4964. }
  4965. if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
  4966. (ret->ns != NULL) &&
  4967. (xmlStrEqual
  4968. (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
  4969. xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
  4970. "Attribute with namespace '%s' is not allowed\n",
  4971. ret->ns, NULL);
  4972. }
  4973. if (node->children != NULL) {
  4974. ret->nameClass =
  4975. xmlRelaxNGParseExceptNameClass(ctxt, node->children,
  4976. (def->type ==
  4977. XML_RELAXNG_ATTRIBUTE));
  4978. }
  4979. } else if (IS_RELAXNG(node, "choice")) {
  4980. xmlNodePtr child;
  4981. xmlRelaxNGDefinePtr last = NULL;
  4982. ret = xmlRelaxNGNewDefine(ctxt, node);
  4983. if (ret == NULL)
  4984. return (NULL);
  4985. ret->parent = def;
  4986. ret->type = XML_RELAXNG_CHOICE;
  4987. if (node->children == NULL) {
  4988. xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY,
  4989. "Element choice is empty\n", NULL, NULL);
  4990. } else {
  4991. child = node->children;
  4992. while (child != NULL) {
  4993. tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
  4994. if (tmp != NULL) {
  4995. if (last == NULL) {
  4996. last = ret->nameClass = tmp;
  4997. } else {
  4998. last->next = tmp;
  4999. last = tmp;
  5000. }
  5001. }
  5002. child = child->next;
  5003. }
  5004. }
  5005. } else {
  5006. xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT,
  5007. "expecting name, anyName, nsName or choice : got %s\n",
  5008. (node == NULL ? "nothing" : node->name), NULL);
  5009. return (NULL);
  5010. }
  5011. if (ret != def) {
  5012. if (def->nameClass == NULL) {
  5013. def->nameClass = ret;
  5014. } else {
  5015. tmp = def->nameClass;
  5016. while (tmp->next != NULL) {
  5017. tmp = tmp->next;
  5018. }
  5019. tmp->next = ret;
  5020. }
  5021. }
  5022. return (ret);
  5023. }
  5024. /**
  5025. * xmlRelaxNGParseElement:
  5026. * @ctxt: a Relax-NG parser context
  5027. * @node: the element node
  5028. *
  5029. * parse the content of a RelaxNG element node.
  5030. *
  5031. * Returns the definition pointer or NULL in case of error.
  5032. */
  5033. static xmlRelaxNGDefinePtr
  5034. xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
  5035. {
  5036. xmlRelaxNGDefinePtr ret, cur, last;
  5037. xmlNodePtr child;
  5038. const xmlChar *olddefine;
  5039. ret = xmlRelaxNGNewDefine(ctxt, node);
  5040. if (ret == NULL)
  5041. return (NULL);
  5042. ret->type = XML_RELAXNG_ELEMENT;
  5043. ret->parent = ctxt->def;
  5044. child = node->children;
  5045. if (child == NULL) {
  5046. xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY,
  5047. "xmlRelaxNGParseElement: element has no children\n",
  5048. NULL, NULL);
  5049. return (ret);
  5050. }
  5051. cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
  5052. if (cur != NULL)
  5053. child = child->next;
  5054. if (child == NULL) {
  5055. xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT,
  5056. "xmlRelaxNGParseElement: element has no content\n",
  5057. NULL, NULL);
  5058. return (ret);
  5059. }
  5060. olddefine = ctxt->define;
  5061. ctxt->define = NULL;
  5062. last = NULL;
  5063. while (child != NULL) {
  5064. cur = xmlRelaxNGParsePattern(ctxt, child);
  5065. if (cur != NULL) {
  5066. cur->parent = ret;
  5067. switch (cur->type) {
  5068. case XML_RELAXNG_EMPTY:
  5069. case XML_RELAXNG_NOT_ALLOWED:
  5070. case XML_RELAXNG_TEXT:
  5071. case XML_RELAXNG_ELEMENT:
  5072. case XML_RELAXNG_DATATYPE:
  5073. case XML_RELAXNG_VALUE:
  5074. case XML_RELAXNG_LIST:
  5075. case XML_RELAXNG_REF:
  5076. case XML_RELAXNG_PARENTREF:
  5077. case XML_RELAXNG_EXTERNALREF:
  5078. case XML_RELAXNG_DEF:
  5079. case XML_RELAXNG_ZEROORMORE:
  5080. case XML_RELAXNG_ONEORMORE:
  5081. case XML_RELAXNG_OPTIONAL:
  5082. case XML_RELAXNG_CHOICE:
  5083. case XML_RELAXNG_GROUP:
  5084. case XML_RELAXNG_INTERLEAVE:
  5085. if (last == NULL) {
  5086. ret->content = last = cur;
  5087. } else {
  5088. if ((last->type == XML_RELAXNG_ELEMENT) &&
  5089. (ret->content == last)) {
  5090. ret->content = xmlRelaxNGNewDefine(ctxt, node);
  5091. if (ret->content != NULL) {
  5092. ret->content->type = XML_RELAXNG_GROUP;
  5093. ret->content->content = last;
  5094. } else {
  5095. ret->content = last;
  5096. }
  5097. }
  5098. last->next = cur;
  5099. last = cur;
  5100. }
  5101. break;
  5102. case XML_RELAXNG_ATTRIBUTE:
  5103. cur->next = ret->attrs;
  5104. ret->attrs = cur;
  5105. break;
  5106. case XML_RELAXNG_START:
  5107. xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
  5108. "RNG Internal error, start found in element\n",
  5109. NULL, NULL);
  5110. break;
  5111. case XML_RELAXNG_PARAM:
  5112. xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
  5113. "RNG Internal error, param found in element\n",
  5114. NULL, NULL);
  5115. break;
  5116. case XML_RELAXNG_EXCEPT:
  5117. xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
  5118. "RNG Internal error, except found in element\n",
  5119. NULL, NULL);
  5120. break;
  5121. case XML_RELAXNG_NOOP:
  5122. xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
  5123. "RNG Internal error, noop found in element\n",
  5124. NULL, NULL);
  5125. break;
  5126. }
  5127. }
  5128. child = child->next;
  5129. }
  5130. ctxt->define = olddefine;
  5131. return (ret);
  5132. }
  5133. /**
  5134. * xmlRelaxNGParsePatterns:
  5135. * @ctxt: a Relax-NG parser context
  5136. * @nodes: list of nodes
  5137. * @group: use an implicit <group> for elements
  5138. *
  5139. * parse the content of a RelaxNG start node.
  5140. *
  5141. * Returns the definition pointer or NULL in case of error.
  5142. */
  5143. static xmlRelaxNGDefinePtr
  5144. xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
  5145. int group)
  5146. {
  5147. xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
  5148. parent = ctxt->def;
  5149. while (nodes != NULL) {
  5150. if (IS_RELAXNG(nodes, "element")) {
  5151. cur = xmlRelaxNGParseElement(ctxt, nodes);
  5152. if (def == NULL) {
  5153. def = last = cur;
  5154. } else {
  5155. if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
  5156. (def == last)) {
  5157. def = xmlRelaxNGNewDefine(ctxt, nodes);
  5158. def->type = XML_RELAXNG_GROUP;
  5159. def->content = last;
  5160. }
  5161. last->next = cur;
  5162. last = cur;
  5163. }
  5164. cur->parent = parent;
  5165. } else {
  5166. cur = xmlRelaxNGParsePattern(ctxt, nodes);
  5167. if (cur != NULL) {
  5168. if (def == NULL) {
  5169. def = last = cur;
  5170. } else {
  5171. last->next = cur;
  5172. last = cur;
  5173. }
  5174. }
  5175. }
  5176. nodes = nodes->next;
  5177. }
  5178. return (def);
  5179. }
  5180. /**
  5181. * xmlRelaxNGParseStart:
  5182. * @ctxt: a Relax-NG parser context
  5183. * @nodes: start children nodes
  5184. *
  5185. * parse the content of a RelaxNG start node.
  5186. *
  5187. * Returns 0 in case of success, -1 in case of error
  5188. */
  5189. static int
  5190. xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
  5191. {
  5192. int ret = 0;
  5193. xmlRelaxNGDefinePtr def = NULL, last;
  5194. if (nodes == NULL) {
  5195. xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n",
  5196. NULL, NULL);
  5197. return (-1);
  5198. }
  5199. if (IS_RELAXNG(nodes, "empty")) {
  5200. def = xmlRelaxNGNewDefine(ctxt, nodes);
  5201. if (def == NULL)
  5202. return (-1);
  5203. def->type = XML_RELAXNG_EMPTY;
  5204. if (nodes->children != NULL) {
  5205. xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT,
  5206. "element empty is not empty\n", NULL, NULL);
  5207. }
  5208. } else if (IS_RELAXNG(nodes, "notAllowed")) {
  5209. def = xmlRelaxNGNewDefine(ctxt, nodes);
  5210. if (def == NULL)
  5211. return (-1);
  5212. def->type = XML_RELAXNG_NOT_ALLOWED;
  5213. if (nodes->children != NULL) {
  5214. xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY,
  5215. "element notAllowed is not empty\n", NULL, NULL);
  5216. }
  5217. } else {
  5218. def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
  5219. }
  5220. if (ctxt->grammar->start != NULL) {
  5221. last = ctxt->grammar->start;
  5222. while (last->next != NULL)
  5223. last = last->next;
  5224. last->next = def;
  5225. } else {
  5226. ctxt->grammar->start = def;
  5227. }
  5228. nodes = nodes->next;
  5229. if (nodes != NULL) {
  5230. xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT,
  5231. "start more than one children\n", NULL, NULL);
  5232. return (-1);
  5233. }
  5234. return (ret);
  5235. }
  5236. /**
  5237. * xmlRelaxNGParseGrammarContent:
  5238. * @ctxt: a Relax-NG parser context
  5239. * @nodes: grammar children nodes
  5240. *
  5241. * parse the content of a RelaxNG grammar node.
  5242. *
  5243. * Returns 0 in case of success, -1 in case of error
  5244. */
  5245. static int
  5246. xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
  5247. xmlNodePtr nodes)
  5248. {
  5249. int ret = 0, tmp;
  5250. if (nodes == NULL) {
  5251. xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY,
  5252. "grammar has no children\n", NULL, NULL);
  5253. return (-1);
  5254. }
  5255. while (nodes != NULL) {
  5256. if (IS_RELAXNG(nodes, "start")) {
  5257. if (nodes->children == NULL) {
  5258. xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY,
  5259. "start has no children\n", NULL, NULL);
  5260. } else {
  5261. tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
  5262. if (tmp != 0)
  5263. ret = -1;
  5264. }
  5265. } else if (IS_RELAXNG(nodes, "define")) {
  5266. tmp = xmlRelaxNGParseDefine(ctxt, nodes);
  5267. if (tmp != 0)
  5268. ret = -1;
  5269. } else if (IS_RELAXNG(nodes, "include")) {
  5270. tmp = xmlRelaxNGParseInclude(ctxt, nodes);
  5271. if (tmp != 0)
  5272. ret = -1;
  5273. } else {
  5274. xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
  5275. "grammar has unexpected child %s\n", nodes->name,
  5276. NULL);
  5277. ret = -1;
  5278. }
  5279. nodes = nodes->next;
  5280. }
  5281. return (ret);
  5282. }
  5283. /**
  5284. * xmlRelaxNGCheckReference:
  5285. * @ref: the ref
  5286. * @ctxt: a Relax-NG parser context
  5287. * @name: the name associated to the defines
  5288. *
  5289. * Applies the 4.17. combine attribute rule for all the define
  5290. * element of a given grammar using the same name.
  5291. */
  5292. static void
  5293. xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
  5294. xmlRelaxNGParserCtxtPtr ctxt,
  5295. const xmlChar * name)
  5296. {
  5297. xmlRelaxNGGrammarPtr grammar;
  5298. xmlRelaxNGDefinePtr def, cur;
  5299. /*
  5300. * Those rules don't apply to imported ref from xmlRelaxNGParseImportRef
  5301. */
  5302. if (ref->dflags & IS_EXTERNAL_REF)
  5303. return;
  5304. grammar = ctxt->grammar;
  5305. if (grammar == NULL) {
  5306. xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
  5307. "Internal error: no grammar in CheckReference %s\n",
  5308. name, NULL);
  5309. return;
  5310. }
  5311. if (ref->content != NULL) {
  5312. xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
  5313. "Internal error: reference has content in CheckReference %s\n",
  5314. name, NULL);
  5315. return;
  5316. }
  5317. if (grammar->defs != NULL) {
  5318. def = xmlHashLookup(grammar->defs, name);
  5319. if (def != NULL) {
  5320. cur = ref;
  5321. while (cur != NULL) {
  5322. cur->content = def;
  5323. cur = cur->nextHash;
  5324. }
  5325. } else {
  5326. xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
  5327. "Reference %s has no matching definition\n", name,
  5328. NULL);
  5329. }
  5330. } else {
  5331. xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
  5332. "Reference %s has no matching definition\n", name,
  5333. NULL);
  5334. }
  5335. }
  5336. /**
  5337. * xmlRelaxNGCheckCombine:
  5338. * @define: the define(s) list
  5339. * @ctxt: a Relax-NG parser context
  5340. * @name: the name associated to the defines
  5341. *
  5342. * Applies the 4.17. combine attribute rule for all the define
  5343. * element of a given grammar using the same name.
  5344. */
  5345. static void
  5346. xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
  5347. xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name)
  5348. {
  5349. xmlChar *combine;
  5350. int choiceOrInterleave = -1;
  5351. int missing = 0;
  5352. xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
  5353. if (define->nextHash == NULL)
  5354. return;
  5355. cur = define;
  5356. while (cur != NULL) {
  5357. combine = xmlGetProp(cur->node, BAD_CAST "combine");
  5358. if (combine != NULL) {
  5359. if (xmlStrEqual(combine, BAD_CAST "choice")) {
  5360. if (choiceOrInterleave == -1)
  5361. choiceOrInterleave = 1;
  5362. else if (choiceOrInterleave == 0) {
  5363. xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
  5364. "Defines for %s use both 'choice' and 'interleave'\n",
  5365. name, NULL);
  5366. }
  5367. } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
  5368. if (choiceOrInterleave == -1)
  5369. choiceOrInterleave = 0;
  5370. else if (choiceOrInterleave == 1) {
  5371. xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
  5372. "Defines for %s use both 'choice' and 'interleave'\n",
  5373. name, NULL);
  5374. }
  5375. } else {
  5376. xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE,
  5377. "Defines for %s use unknown combine value '%s''\n",
  5378. name, combine);
  5379. }
  5380. xmlFree(combine);
  5381. } else {
  5382. if (missing == 0)
  5383. missing = 1;
  5384. else {
  5385. xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE,
  5386. "Some defines for %s needs the combine attribute\n",
  5387. name, NULL);
  5388. }
  5389. }
  5390. cur = cur->nextHash;
  5391. }
  5392. #ifdef DEBUG
  5393. xmlGenericError(xmlGenericErrorContext,
  5394. "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
  5395. name, choiceOrInterleave);
  5396. #endif
  5397. if (choiceOrInterleave == -1)
  5398. choiceOrInterleave = 0;
  5399. cur = xmlRelaxNGNewDefine(ctxt, define->node);
  5400. if (cur == NULL)
  5401. return;
  5402. if (choiceOrInterleave == 0)
  5403. cur->type = XML_RELAXNG_INTERLEAVE;
  5404. else
  5405. cur->type = XML_RELAXNG_CHOICE;
  5406. tmp = define;
  5407. last = NULL;
  5408. while (tmp != NULL) {
  5409. if (tmp->content != NULL) {
  5410. if (tmp->content->next != NULL) {
  5411. /*
  5412. * we need first to create a wrapper.
  5413. */
  5414. tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
  5415. if (tmp2 == NULL)
  5416. break;
  5417. tmp2->type = XML_RELAXNG_GROUP;
  5418. tmp2->content = tmp->content;
  5419. } else {
  5420. tmp2 = tmp->content;
  5421. }
  5422. if (last == NULL) {
  5423. cur->content = tmp2;
  5424. } else {
  5425. last->next = tmp2;
  5426. }
  5427. last = tmp2;
  5428. }
  5429. tmp->content = cur;
  5430. tmp = tmp->nextHash;
  5431. }
  5432. define->content = cur;
  5433. if (choiceOrInterleave == 0) {
  5434. if (ctxt->interleaves == NULL)
  5435. ctxt->interleaves = xmlHashCreate(10);
  5436. if (ctxt->interleaves == NULL) {
  5437. xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
  5438. "Failed to create interleaves hash table\n", NULL,
  5439. NULL);
  5440. } else {
  5441. char tmpname[32];
  5442. snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
  5443. if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
  5444. 0) {
  5445. xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
  5446. "Failed to add %s to hash table\n",
  5447. (const xmlChar *) tmpname, NULL);
  5448. }
  5449. }
  5450. }
  5451. }
  5452. /**
  5453. * xmlRelaxNGCombineStart:
  5454. * @ctxt: a Relax-NG parser context
  5455. * @grammar: the grammar
  5456. *
  5457. * Applies the 4.17. combine rule for all the start
  5458. * element of a given grammar.
  5459. */
  5460. static void
  5461. xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
  5462. xmlRelaxNGGrammarPtr grammar)
  5463. {
  5464. xmlRelaxNGDefinePtr starts;
  5465. xmlChar *combine;
  5466. int choiceOrInterleave = -1;
  5467. int missing = 0;
  5468. xmlRelaxNGDefinePtr cur;
  5469. starts = grammar->start;
  5470. if ((starts == NULL) || (starts->next == NULL))
  5471. return;
  5472. cur = starts;
  5473. while (cur != NULL) {
  5474. if ((cur->node == NULL) || (cur->node->parent == NULL) ||
  5475. (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
  5476. combine = NULL;
  5477. xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING,
  5478. "Internal error: start element not found\n", NULL,
  5479. NULL);
  5480. } else {
  5481. combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
  5482. }
  5483. if (combine != NULL) {
  5484. if (xmlStrEqual(combine, BAD_CAST "choice")) {
  5485. if (choiceOrInterleave == -1)
  5486. choiceOrInterleave = 1;
  5487. else if (choiceOrInterleave == 0) {
  5488. xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
  5489. "<start> use both 'choice' and 'interleave'\n",
  5490. NULL, NULL);
  5491. }
  5492. } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
  5493. if (choiceOrInterleave == -1)
  5494. choiceOrInterleave = 0;
  5495. else if (choiceOrInterleave == 1) {
  5496. xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
  5497. "<start> use both 'choice' and 'interleave'\n",
  5498. NULL, NULL);
  5499. }
  5500. } else {
  5501. xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE,
  5502. "<start> uses unknown combine value '%s''\n",
  5503. combine, NULL);
  5504. }
  5505. xmlFree(combine);
  5506. } else {
  5507. if (missing == 0)
  5508. missing = 1;
  5509. else {
  5510. xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE,
  5511. "Some <start> element miss the combine attribute\n",
  5512. NULL, NULL);
  5513. }
  5514. }
  5515. cur = cur->next;
  5516. }
  5517. #ifdef DEBUG
  5518. xmlGenericError(xmlGenericErrorContext,
  5519. "xmlRelaxNGCombineStart(): merging <start>: %d\n",
  5520. choiceOrInterleave);
  5521. #endif
  5522. if (choiceOrInterleave == -1)
  5523. choiceOrInterleave = 0;
  5524. cur = xmlRelaxNGNewDefine(ctxt, starts->node);
  5525. if (cur == NULL)
  5526. return;
  5527. if (choiceOrInterleave == 0)
  5528. cur->type = XML_RELAXNG_INTERLEAVE;
  5529. else
  5530. cur->type = XML_RELAXNG_CHOICE;
  5531. cur->content = grammar->start;
  5532. grammar->start = cur;
  5533. if (choiceOrInterleave == 0) {
  5534. if (ctxt->interleaves == NULL)
  5535. ctxt->interleaves = xmlHashCreate(10);
  5536. if (ctxt->interleaves == NULL) {
  5537. xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
  5538. "Failed to create interleaves hash table\n", NULL,
  5539. NULL);
  5540. } else {
  5541. char tmpname[32];
  5542. snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
  5543. if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
  5544. 0) {
  5545. xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
  5546. "Failed to add %s to hash table\n",
  5547. (const xmlChar *) tmpname, NULL);
  5548. }
  5549. }
  5550. }
  5551. }
  5552. /**
  5553. * xmlRelaxNGCheckCycles:
  5554. * @ctxt: a Relax-NG parser context
  5555. * @nodes: grammar children nodes
  5556. * @depth: the counter
  5557. *
  5558. * Check for cycles.
  5559. *
  5560. * Returns 0 if check passed, and -1 in case of error
  5561. */
  5562. static int
  5563. xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
  5564. xmlRelaxNGDefinePtr cur, int depth)
  5565. {
  5566. int ret = 0;
  5567. while ((ret == 0) && (cur != NULL)) {
  5568. if ((cur->type == XML_RELAXNG_REF) ||
  5569. (cur->type == XML_RELAXNG_PARENTREF)) {
  5570. if (cur->depth == -1) {
  5571. cur->depth = depth;
  5572. ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
  5573. cur->depth = -2;
  5574. } else if (depth == cur->depth) {
  5575. xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE,
  5576. "Detected a cycle in %s references\n",
  5577. cur->name, NULL);
  5578. return (-1);
  5579. }
  5580. } else if (cur->type == XML_RELAXNG_ELEMENT) {
  5581. ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
  5582. } else {
  5583. ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
  5584. }
  5585. cur = cur->next;
  5586. }
  5587. return (ret);
  5588. }
  5589. /**
  5590. * xmlRelaxNGTryUnlink:
  5591. * @ctxt: a Relax-NG parser context
  5592. * @cur: the definition to unlink
  5593. * @parent: the parent definition
  5594. * @prev: the previous sibling definition
  5595. *
  5596. * Try to unlink a definition. If not possble make it a NOOP
  5597. *
  5598. * Returns the new prev definition
  5599. */
  5600. static xmlRelaxNGDefinePtr
  5601. xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
  5602. xmlRelaxNGDefinePtr cur,
  5603. xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev)
  5604. {
  5605. if (prev != NULL) {
  5606. prev->next = cur->next;
  5607. } else {
  5608. if (parent != NULL) {
  5609. if (parent->content == cur)
  5610. parent->content = cur->next;
  5611. else if (parent->attrs == cur)
  5612. parent->attrs = cur->next;
  5613. else if (parent->nameClass == cur)
  5614. parent->nameClass = cur->next;
  5615. } else {
  5616. cur->type = XML_RELAXNG_NOOP;
  5617. prev = cur;
  5618. }
  5619. }
  5620. return (prev);
  5621. }
  5622. /**
  5623. * xmlRelaxNGSimplify:
  5624. * @ctxt: a Relax-NG parser context
  5625. * @nodes: grammar children nodes
  5626. *
  5627. * Check for simplification of empty and notAllowed
  5628. */
  5629. static void
  5630. xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
  5631. xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent)
  5632. {
  5633. xmlRelaxNGDefinePtr prev = NULL;
  5634. while (cur != NULL) {
  5635. if ((cur->type == XML_RELAXNG_REF) ||
  5636. (cur->type == XML_RELAXNG_PARENTREF)) {
  5637. if (cur->depth != -3) {
  5638. cur->depth = -3;
  5639. xmlRelaxNGSimplify(ctxt, cur->content, cur);
  5640. }
  5641. } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
  5642. cur->parent = parent;
  5643. if ((parent != NULL) &&
  5644. ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
  5645. (parent->type == XML_RELAXNG_LIST) ||
  5646. (parent->type == XML_RELAXNG_GROUP) ||
  5647. (parent->type == XML_RELAXNG_INTERLEAVE) ||
  5648. (parent->type == XML_RELAXNG_ONEORMORE) ||
  5649. (parent->type == XML_RELAXNG_ZEROORMORE))) {
  5650. parent->type = XML_RELAXNG_NOT_ALLOWED;
  5651. break;
  5652. }
  5653. if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) {
  5654. prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
  5655. } else
  5656. prev = cur;
  5657. } else if (cur->type == XML_RELAXNG_EMPTY) {
  5658. cur->parent = parent;
  5659. if ((parent != NULL) &&
  5660. ((parent->type == XML_RELAXNG_ONEORMORE) ||
  5661. (parent->type == XML_RELAXNG_ZEROORMORE))) {
  5662. parent->type = XML_RELAXNG_EMPTY;
  5663. break;
  5664. }
  5665. if ((parent != NULL) &&
  5666. ((parent->type == XML_RELAXNG_GROUP) ||
  5667. (parent->type == XML_RELAXNG_INTERLEAVE))) {
  5668. prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
  5669. } else
  5670. prev = cur;
  5671. } else {
  5672. cur->parent = parent;
  5673. if (cur->content != NULL)
  5674. xmlRelaxNGSimplify(ctxt, cur->content, cur);
  5675. if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
  5676. xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
  5677. if (cur->nameClass != NULL)
  5678. xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
  5679. /*
  5680. * On Elements, try to move attribute only generating rules on
  5681. * the attrs rules.
  5682. */
  5683. if (cur->type == XML_RELAXNG_ELEMENT) {
  5684. int attronly;
  5685. xmlRelaxNGDefinePtr tmp, pre;
  5686. while (cur->content != NULL) {
  5687. attronly =
  5688. xmlRelaxNGGenerateAttributes(ctxt, cur->content);
  5689. if (attronly == 1) {
  5690. /*
  5691. * migrate cur->content to attrs
  5692. */
  5693. tmp = cur->content;
  5694. cur->content = tmp->next;
  5695. tmp->next = cur->attrs;
  5696. cur->attrs = tmp;
  5697. } else {
  5698. /*
  5699. * cur->content can generate elements or text
  5700. */
  5701. break;
  5702. }
  5703. }
  5704. pre = cur->content;
  5705. while ((pre != NULL) && (pre->next != NULL)) {
  5706. tmp = pre->next;
  5707. attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
  5708. if (attronly == 1) {
  5709. /*
  5710. * migrate tmp to attrs
  5711. */
  5712. pre->next = tmp->next;
  5713. tmp->next = cur->attrs;
  5714. cur->attrs = tmp;
  5715. } else {
  5716. pre = tmp;
  5717. }
  5718. }
  5719. }
  5720. /*
  5721. * This may result in a simplification
  5722. */
  5723. if ((cur->type == XML_RELAXNG_GROUP) ||
  5724. (cur->type == XML_RELAXNG_INTERLEAVE)) {
  5725. if (cur->content == NULL)
  5726. cur->type = XML_RELAXNG_EMPTY;
  5727. else if (cur->content->next == NULL) {
  5728. if ((parent == NULL) && (prev == NULL)) {
  5729. cur->type = XML_RELAXNG_NOOP;
  5730. } else if (prev == NULL) {
  5731. parent->content = cur->content;
  5732. cur->content->next = cur->next;
  5733. cur = cur->content;
  5734. } else {
  5735. cur->content->next = cur->next;
  5736. prev->next = cur->content;
  5737. cur = cur->content;
  5738. }
  5739. }
  5740. }
  5741. /*
  5742. * the current node may have been transformed back
  5743. */
  5744. if ((cur->type == XML_RELAXNG_EXCEPT) &&
  5745. (cur->content != NULL) &&
  5746. (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
  5747. prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
  5748. } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
  5749. if ((parent != NULL) &&
  5750. ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
  5751. (parent->type == XML_RELAXNG_LIST) ||
  5752. (parent->type == XML_RELAXNG_GROUP) ||
  5753. (parent->type == XML_RELAXNG_INTERLEAVE) ||
  5754. (parent->type == XML_RELAXNG_ONEORMORE) ||
  5755. (parent->type == XML_RELAXNG_ZEROORMORE))) {
  5756. parent->type = XML_RELAXNG_NOT_ALLOWED;
  5757. break;
  5758. }
  5759. if ((parent != NULL) &&
  5760. (parent->type == XML_RELAXNG_CHOICE)) {
  5761. prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
  5762. } else
  5763. prev = cur;
  5764. } else if (cur->type == XML_RELAXNG_EMPTY) {
  5765. if ((parent != NULL) &&
  5766. ((parent->type == XML_RELAXNG_ONEORMORE) ||
  5767. (parent->type == XML_RELAXNG_ZEROORMORE))) {
  5768. parent->type = XML_RELAXNG_EMPTY;
  5769. break;
  5770. }
  5771. if ((parent != NULL) &&
  5772. ((parent->type == XML_RELAXNG_GROUP) ||
  5773. (parent->type == XML_RELAXNG_INTERLEAVE) ||
  5774. (parent->type == XML_RELAXNG_CHOICE))) {
  5775. prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
  5776. } else
  5777. prev = cur;
  5778. } else {
  5779. prev = cur;
  5780. }
  5781. }
  5782. cur = cur->next;
  5783. }
  5784. }
  5785. /**
  5786. * xmlRelaxNGGroupContentType:
  5787. * @ct1: the first content type
  5788. * @ct2: the second content type
  5789. *
  5790. * Try to group 2 content types
  5791. *
  5792. * Returns the content type
  5793. */
  5794. static xmlRelaxNGContentType
  5795. xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
  5796. xmlRelaxNGContentType ct2)
  5797. {
  5798. if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
  5799. (ct2 == XML_RELAXNG_CONTENT_ERROR))
  5800. return (XML_RELAXNG_CONTENT_ERROR);
  5801. if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
  5802. return (ct2);
  5803. if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
  5804. return (ct1);
  5805. if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
  5806. (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
  5807. return (XML_RELAXNG_CONTENT_COMPLEX);
  5808. return (XML_RELAXNG_CONTENT_ERROR);
  5809. }
  5810. /**
  5811. * xmlRelaxNGMaxContentType:
  5812. * @ct1: the first content type
  5813. * @ct2: the second content type
  5814. *
  5815. * Compute the max content-type
  5816. *
  5817. * Returns the content type
  5818. */
  5819. static xmlRelaxNGContentType
  5820. xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
  5821. xmlRelaxNGContentType ct2)
  5822. {
  5823. if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
  5824. (ct2 == XML_RELAXNG_CONTENT_ERROR))
  5825. return (XML_RELAXNG_CONTENT_ERROR);
  5826. if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
  5827. (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
  5828. return (XML_RELAXNG_CONTENT_SIMPLE);
  5829. if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
  5830. (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
  5831. return (XML_RELAXNG_CONTENT_COMPLEX);
  5832. return (XML_RELAXNG_CONTENT_EMPTY);
  5833. }
  5834. /**
  5835. * xmlRelaxNGCheckRules:
  5836. * @ctxt: a Relax-NG parser context
  5837. * @cur: the current definition
  5838. * @flags: some accumulated flags
  5839. * @ptype: the parent type
  5840. *
  5841. * Check for rules in section 7.1 and 7.2
  5842. *
  5843. * Returns the content type of @cur
  5844. */
  5845. static xmlRelaxNGContentType
  5846. xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
  5847. xmlRelaxNGDefinePtr cur, int flags,
  5848. xmlRelaxNGType ptype)
  5849. {
  5850. int nflags;
  5851. xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
  5852. while (cur != NULL) {
  5853. ret = XML_RELAXNG_CONTENT_EMPTY;
  5854. if ((cur->type == XML_RELAXNG_REF) ||
  5855. (cur->type == XML_RELAXNG_PARENTREF)) {
  5856. /*
  5857. * This should actually be caught by list//element(ref) at the
  5858. * element boundaries, c.f. Bug #159968 local refs are dropped
  5859. * in step 4.19.
  5860. */
  5861. #if 0
  5862. if (flags & XML_RELAXNG_IN_LIST) {
  5863. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF,
  5864. "Found forbidden pattern list//ref\n", NULL,
  5865. NULL);
  5866. }
  5867. #endif
  5868. if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
  5869. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF,
  5870. "Found forbidden pattern data/except//ref\n",
  5871. NULL, NULL);
  5872. }
  5873. if (cur->content == NULL) {
  5874. if (cur->type == XML_RELAXNG_PARENTREF)
  5875. xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
  5876. "Internal found no define for parent refs\n",
  5877. NULL, NULL);
  5878. else
  5879. xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
  5880. "Internal found no define for ref %s\n",
  5881. (cur->name ? cur->name: BAD_CAST "null"), NULL);
  5882. }
  5883. if (cur->depth > -4) {
  5884. cur->depth = -4;
  5885. ret = xmlRelaxNGCheckRules(ctxt, cur->content,
  5886. flags, cur->type);
  5887. cur->depth = ret - 15;
  5888. } else if (cur->depth == -4) {
  5889. ret = XML_RELAXNG_CONTENT_COMPLEX;
  5890. } else {
  5891. ret = (xmlRelaxNGContentType) (cur->depth + 15);
  5892. }
  5893. } else if (cur->type == XML_RELAXNG_ELEMENT) {
  5894. /*
  5895. * The 7.3 Attribute derivation rule for groups is plugged there
  5896. */
  5897. xmlRelaxNGCheckGroupAttrs(ctxt, cur);
  5898. if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
  5899. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM,
  5900. "Found forbidden pattern data/except//element(ref)\n",
  5901. NULL, NULL);
  5902. }
  5903. if (flags & XML_RELAXNG_IN_LIST) {
  5904. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM,
  5905. "Found forbidden pattern list//element(ref)\n",
  5906. NULL, NULL);
  5907. }
  5908. if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
  5909. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
  5910. "Found forbidden pattern attribute//element(ref)\n",
  5911. NULL, NULL);
  5912. }
  5913. if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
  5914. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
  5915. "Found forbidden pattern attribute//element(ref)\n",
  5916. NULL, NULL);
  5917. }
  5918. /*
  5919. * reset since in the simple form elements are only child
  5920. * of grammar/define
  5921. */
  5922. nflags = 0;
  5923. ret =
  5924. xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
  5925. if (ret != XML_RELAXNG_CONTENT_EMPTY) {
  5926. xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY,
  5927. "Element %s attributes have a content type error\n",
  5928. cur->name, NULL);
  5929. }
  5930. ret =
  5931. xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
  5932. cur->type);
  5933. if (ret == XML_RELAXNG_CONTENT_ERROR) {
  5934. xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR,
  5935. "Element %s has a content type error\n",
  5936. cur->name, NULL);
  5937. } else {
  5938. ret = XML_RELAXNG_CONTENT_COMPLEX;
  5939. }
  5940. } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
  5941. if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
  5942. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR,
  5943. "Found forbidden pattern attribute//attribute\n",
  5944. NULL, NULL);
  5945. }
  5946. if (flags & XML_RELAXNG_IN_LIST) {
  5947. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR,
  5948. "Found forbidden pattern list//attribute\n",
  5949. NULL, NULL);
  5950. }
  5951. if (flags & XML_RELAXNG_IN_OOMGROUP) {
  5952. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR,
  5953. "Found forbidden pattern oneOrMore//group//attribute\n",
  5954. NULL, NULL);
  5955. }
  5956. if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
  5957. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR,
  5958. "Found forbidden pattern oneOrMore//interleave//attribute\n",
  5959. NULL, NULL);
  5960. }
  5961. if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
  5962. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR,
  5963. "Found forbidden pattern data/except//attribute\n",
  5964. NULL, NULL);
  5965. }
  5966. if (flags & XML_RELAXNG_IN_START) {
  5967. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR,
  5968. "Found forbidden pattern start//attribute\n",
  5969. NULL, NULL);
  5970. }
  5971. if ((!(flags & XML_RELAXNG_IN_ONEORMORE))
  5972. && (cur->name == NULL)) {
  5973. if (cur->ns == NULL) {
  5974. xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR,
  5975. "Found anyName attribute without oneOrMore ancestor\n",
  5976. NULL, NULL);
  5977. } else {
  5978. xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR,
  5979. "Found nsName attribute without oneOrMore ancestor\n",
  5980. NULL, NULL);
  5981. }
  5982. }
  5983. nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
  5984. xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
  5985. ret = XML_RELAXNG_CONTENT_EMPTY;
  5986. } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
  5987. (cur->type == XML_RELAXNG_ZEROORMORE)) {
  5988. if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
  5989. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE,
  5990. "Found forbidden pattern data/except//oneOrMore\n",
  5991. NULL, NULL);
  5992. }
  5993. if (flags & XML_RELAXNG_IN_START) {
  5994. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE,
  5995. "Found forbidden pattern start//oneOrMore\n",
  5996. NULL, NULL);
  5997. }
  5998. nflags = flags | XML_RELAXNG_IN_ONEORMORE;
  5999. ret =
  6000. xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
  6001. cur->type);
  6002. ret = xmlRelaxNGGroupContentType(ret, ret);
  6003. } else if (cur->type == XML_RELAXNG_LIST) {
  6004. if (flags & XML_RELAXNG_IN_LIST) {
  6005. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST,
  6006. "Found forbidden pattern list//list\n", NULL,
  6007. NULL);
  6008. }
  6009. if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
  6010. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST,
  6011. "Found forbidden pattern data/except//list\n",
  6012. NULL, NULL);
  6013. }
  6014. if (flags & XML_RELAXNG_IN_START) {
  6015. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST,
  6016. "Found forbidden pattern start//list\n", NULL,
  6017. NULL);
  6018. }
  6019. nflags = flags | XML_RELAXNG_IN_LIST;
  6020. ret =
  6021. xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
  6022. cur->type);
  6023. } else if (cur->type == XML_RELAXNG_GROUP) {
  6024. if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
  6025. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP,
  6026. "Found forbidden pattern data/except//group\n",
  6027. NULL, NULL);
  6028. }
  6029. if (flags & XML_RELAXNG_IN_START) {
  6030. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP,
  6031. "Found forbidden pattern start//group\n", NULL,
  6032. NULL);
  6033. }
  6034. if (flags & XML_RELAXNG_IN_ONEORMORE)
  6035. nflags = flags | XML_RELAXNG_IN_OOMGROUP;
  6036. else
  6037. nflags = flags;
  6038. ret =
  6039. xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
  6040. cur->type);
  6041. /*
  6042. * The 7.3 Attribute derivation rule for groups is plugged there
  6043. */
  6044. xmlRelaxNGCheckGroupAttrs(ctxt, cur);
  6045. } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
  6046. if (flags & XML_RELAXNG_IN_LIST) {
  6047. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE,
  6048. "Found forbidden pattern list//interleave\n",
  6049. NULL, NULL);
  6050. }
  6051. if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
  6052. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
  6053. "Found forbidden pattern data/except//interleave\n",
  6054. NULL, NULL);
  6055. }
  6056. if (flags & XML_RELAXNG_IN_START) {
  6057. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
  6058. "Found forbidden pattern start//interleave\n",
  6059. NULL, NULL);
  6060. }
  6061. if (flags & XML_RELAXNG_IN_ONEORMORE)
  6062. nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
  6063. else
  6064. nflags = flags;
  6065. ret =
  6066. xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
  6067. cur->type);
  6068. } else if (cur->type == XML_RELAXNG_EXCEPT) {
  6069. if ((cur->parent != NULL) &&
  6070. (cur->parent->type == XML_RELAXNG_DATATYPE))
  6071. nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
  6072. else
  6073. nflags = flags;
  6074. ret =
  6075. xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
  6076. cur->type);
  6077. } else if (cur->type == XML_RELAXNG_DATATYPE) {
  6078. if (flags & XML_RELAXNG_IN_START) {
  6079. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA,
  6080. "Found forbidden pattern start//data\n", NULL,
  6081. NULL);
  6082. }
  6083. xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
  6084. ret = XML_RELAXNG_CONTENT_SIMPLE;
  6085. } else if (cur->type == XML_RELAXNG_VALUE) {
  6086. if (flags & XML_RELAXNG_IN_START) {
  6087. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE,
  6088. "Found forbidden pattern start//value\n", NULL,
  6089. NULL);
  6090. }
  6091. xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
  6092. ret = XML_RELAXNG_CONTENT_SIMPLE;
  6093. } else if (cur->type == XML_RELAXNG_TEXT) {
  6094. if (flags & XML_RELAXNG_IN_LIST) {
  6095. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT,
  6096. "Found forbidden pattern list//text\n", NULL,
  6097. NULL);
  6098. }
  6099. if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
  6100. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT,
  6101. "Found forbidden pattern data/except//text\n",
  6102. NULL, NULL);
  6103. }
  6104. if (flags & XML_RELAXNG_IN_START) {
  6105. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT,
  6106. "Found forbidden pattern start//text\n", NULL,
  6107. NULL);
  6108. }
  6109. ret = XML_RELAXNG_CONTENT_COMPLEX;
  6110. } else if (cur->type == XML_RELAXNG_EMPTY) {
  6111. if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
  6112. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY,
  6113. "Found forbidden pattern data/except//empty\n",
  6114. NULL, NULL);
  6115. }
  6116. if (flags & XML_RELAXNG_IN_START) {
  6117. xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY,
  6118. "Found forbidden pattern start//empty\n", NULL,
  6119. NULL);
  6120. }
  6121. ret = XML_RELAXNG_CONTENT_EMPTY;
  6122. } else if (cur->type == XML_RELAXNG_CHOICE) {
  6123. xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
  6124. ret =
  6125. xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
  6126. } else {
  6127. ret =
  6128. xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
  6129. }
  6130. cur = cur->next;
  6131. if (ptype == XML_RELAXNG_GROUP) {
  6132. val = xmlRelaxNGGroupContentType(val, ret);
  6133. } else if (ptype == XML_RELAXNG_INTERLEAVE) {
  6134. /*
  6135. * TODO: scan complain that tmp is never used, seems on purpose
  6136. * need double-checking
  6137. */
  6138. tmp = xmlRelaxNGGroupContentType(val, ret);
  6139. if (tmp != XML_RELAXNG_CONTENT_ERROR)
  6140. tmp = xmlRelaxNGMaxContentType(val, ret);
  6141. } else if (ptype == XML_RELAXNG_CHOICE) {
  6142. val = xmlRelaxNGMaxContentType(val, ret);
  6143. } else if (ptype == XML_RELAXNG_LIST) {
  6144. val = XML_RELAXNG_CONTENT_SIMPLE;
  6145. } else if (ptype == XML_RELAXNG_EXCEPT) {
  6146. if (ret == XML_RELAXNG_CONTENT_ERROR)
  6147. val = XML_RELAXNG_CONTENT_ERROR;
  6148. else
  6149. val = XML_RELAXNG_CONTENT_SIMPLE;
  6150. } else {
  6151. val = xmlRelaxNGGroupContentType(val, ret);
  6152. }
  6153. }
  6154. return (val);
  6155. }
  6156. /**
  6157. * xmlRelaxNGParseGrammar:
  6158. * @ctxt: a Relax-NG parser context
  6159. * @nodes: grammar children nodes
  6160. *
  6161. * parse a Relax-NG <grammar> node
  6162. *
  6163. * Returns the internal xmlRelaxNGGrammarPtr built or
  6164. * NULL in case of error
  6165. */
  6166. static xmlRelaxNGGrammarPtr
  6167. xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
  6168. {
  6169. xmlRelaxNGGrammarPtr ret, tmp, old;
  6170. #ifdef DEBUG_GRAMMAR
  6171. xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
  6172. #endif
  6173. ret = xmlRelaxNGNewGrammar(ctxt);
  6174. if (ret == NULL)
  6175. return (NULL);
  6176. /*
  6177. * Link the new grammar in the tree
  6178. */
  6179. ret->parent = ctxt->grammar;
  6180. if (ctxt->grammar != NULL) {
  6181. tmp = ctxt->grammar->children;
  6182. if (tmp == NULL) {
  6183. ctxt->grammar->children = ret;
  6184. } else {
  6185. while (tmp->next != NULL)
  6186. tmp = tmp->next;
  6187. tmp->next = ret;
  6188. }
  6189. }
  6190. old = ctxt->grammar;
  6191. ctxt->grammar = ret;
  6192. xmlRelaxNGParseGrammarContent(ctxt, nodes);
  6193. ctxt->grammar = ret;
  6194. if (ctxt->grammar == NULL) {
  6195. xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
  6196. "Failed to parse <grammar> content\n", NULL, NULL);
  6197. } else if (ctxt->grammar->start == NULL) {
  6198. xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START,
  6199. "Element <grammar> has no <start>\n", NULL, NULL);
  6200. }
  6201. /*
  6202. * Apply 4.17 mergingd rules to defines and starts
  6203. */
  6204. xmlRelaxNGCombineStart(ctxt, ret);
  6205. if (ret->defs != NULL) {
  6206. xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
  6207. ctxt);
  6208. }
  6209. /*
  6210. * link together defines and refs in this grammar
  6211. */
  6212. if (ret->refs != NULL) {
  6213. xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
  6214. ctxt);
  6215. }
  6216. /* @@@@ */
  6217. ctxt->grammar = old;
  6218. return (ret);
  6219. }
  6220. /**
  6221. * xmlRelaxNGParseDocument:
  6222. * @ctxt: a Relax-NG parser context
  6223. * @node: the root node of the RelaxNG schema
  6224. *
  6225. * parse a Relax-NG definition resource and build an internal
  6226. * xmlRelaxNG struture which can be used to validate instances.
  6227. *
  6228. * Returns the internal XML RelaxNG structure built or
  6229. * NULL in case of error
  6230. */
  6231. static xmlRelaxNGPtr
  6232. xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
  6233. {
  6234. xmlRelaxNGPtr schema = NULL;
  6235. const xmlChar *olddefine;
  6236. xmlRelaxNGGrammarPtr old;
  6237. if ((ctxt == NULL) || (node == NULL))
  6238. return (NULL);
  6239. schema = xmlRelaxNGNewRelaxNG(ctxt);
  6240. if (schema == NULL)
  6241. return (NULL);
  6242. olddefine = ctxt->define;
  6243. ctxt->define = NULL;
  6244. if (IS_RELAXNG(node, "grammar")) {
  6245. schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
  6246. } else {
  6247. xmlRelaxNGGrammarPtr tmp, ret;
  6248. schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
  6249. if (schema->topgrammar == NULL) {
  6250. return (schema);
  6251. }
  6252. /*
  6253. * Link the new grammar in the tree
  6254. */
  6255. ret->parent = ctxt->grammar;
  6256. if (ctxt->grammar != NULL) {
  6257. tmp = ctxt->grammar->children;
  6258. if (tmp == NULL) {
  6259. ctxt->grammar->children = ret;
  6260. } else {
  6261. while (tmp->next != NULL)
  6262. tmp = tmp->next;
  6263. tmp->next = ret;
  6264. }
  6265. }
  6266. old = ctxt->grammar;
  6267. ctxt->grammar = ret;
  6268. xmlRelaxNGParseStart(ctxt, node);
  6269. if (old != NULL)
  6270. ctxt->grammar = old;
  6271. }
  6272. ctxt->define = olddefine;
  6273. if (schema->topgrammar->start != NULL) {
  6274. xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
  6275. if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
  6276. xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
  6277. while ((schema->topgrammar->start != NULL) &&
  6278. (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
  6279. (schema->topgrammar->start->next != NULL))
  6280. schema->topgrammar->start =
  6281. schema->topgrammar->start->content;
  6282. xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
  6283. XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
  6284. }
  6285. }
  6286. #ifdef DEBUG
  6287. if (schema == NULL)
  6288. xmlGenericError(xmlGenericErrorContext,
  6289. "xmlRelaxNGParseDocument() failed\n");
  6290. #endif
  6291. return (schema);
  6292. }
  6293. /************************************************************************
  6294. * *
  6295. * Reading RelaxNGs *
  6296. * *
  6297. ************************************************************************/
  6298. /**
  6299. * xmlRelaxNGNewParserCtxt:
  6300. * @URL: the location of the schema
  6301. *
  6302. * Create an XML RelaxNGs parse context for that file/resource expected
  6303. * to contain an XML RelaxNGs file.
  6304. *
  6305. * Returns the parser context or NULL in case of error
  6306. */
  6307. xmlRelaxNGParserCtxtPtr
  6308. xmlRelaxNGNewParserCtxt(const char *URL)
  6309. {
  6310. xmlRelaxNGParserCtxtPtr ret;
  6311. if (URL == NULL)
  6312. return (NULL);
  6313. ret =
  6314. (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
  6315. if (ret == NULL) {
  6316. xmlRngPErrMemory(NULL, "building parser\n");
  6317. return (NULL);
  6318. }
  6319. memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
  6320. ret->URL = xmlStrdup((const xmlChar *) URL);
  6321. ret->error = xmlGenericError;
  6322. ret->userData = xmlGenericErrorContext;
  6323. return (ret);
  6324. }
  6325. /**
  6326. * xmlRelaxNGNewMemParserCtxt:
  6327. * @buffer: a pointer to a char array containing the schemas
  6328. * @size: the size of the array
  6329. *
  6330. * Create an XML RelaxNGs parse context for that memory buffer expected
  6331. * to contain an XML RelaxNGs file.
  6332. *
  6333. * Returns the parser context or NULL in case of error
  6334. */
  6335. xmlRelaxNGParserCtxtPtr
  6336. xmlRelaxNGNewMemParserCtxt(const char *buffer, int size)
  6337. {
  6338. xmlRelaxNGParserCtxtPtr ret;
  6339. if ((buffer == NULL) || (size <= 0))
  6340. return (NULL);
  6341. ret =
  6342. (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
  6343. if (ret == NULL) {
  6344. xmlRngPErrMemory(NULL, "building parser\n");
  6345. return (NULL);
  6346. }
  6347. memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
  6348. ret->buffer = buffer;
  6349. ret->size = size;
  6350. ret->error = xmlGenericError;
  6351. ret->userData = xmlGenericErrorContext;
  6352. return (ret);
  6353. }
  6354. /**
  6355. * xmlRelaxNGNewDocParserCtxt:
  6356. * @doc: a preparsed document tree
  6357. *
  6358. * Create an XML RelaxNGs parser context for that document.
  6359. * Note: since the process of compiling a RelaxNG schemas modifies the
  6360. * document, the @doc parameter is duplicated internally.
  6361. *
  6362. * Returns the parser context or NULL in case of error
  6363. */
  6364. xmlRelaxNGParserCtxtPtr
  6365. xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)
  6366. {
  6367. xmlRelaxNGParserCtxtPtr ret;
  6368. xmlDocPtr copy;
  6369. if (doc == NULL)
  6370. return (NULL);
  6371. copy = xmlCopyDoc(doc, 1);
  6372. if (copy == NULL)
  6373. return (NULL);
  6374. ret =
  6375. (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
  6376. if (ret == NULL) {
  6377. xmlRngPErrMemory(NULL, "building parser\n");
  6378. return (NULL);
  6379. }
  6380. memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
  6381. ret->document = copy;
  6382. ret->freedoc = 1;
  6383. ret->userData = xmlGenericErrorContext;
  6384. return (ret);
  6385. }
  6386. /**
  6387. * xmlRelaxNGFreeParserCtxt:
  6388. * @ctxt: the schema parser context
  6389. *
  6390. * Free the resources associated to the schema parser context
  6391. */
  6392. void
  6393. xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)
  6394. {
  6395. if (ctxt == NULL)
  6396. return;
  6397. if (ctxt->URL != NULL)
  6398. xmlFree(ctxt->URL);
  6399. if (ctxt->doc != NULL)
  6400. xmlRelaxNGFreeDocument(ctxt->doc);
  6401. if (ctxt->interleaves != NULL)
  6402. xmlHashFree(ctxt->interleaves, NULL);
  6403. if (ctxt->documents != NULL)
  6404. xmlRelaxNGFreeDocumentList(ctxt->documents);
  6405. if (ctxt->includes != NULL)
  6406. xmlRelaxNGFreeIncludeList(ctxt->includes);
  6407. if (ctxt->docTab != NULL)
  6408. xmlFree(ctxt->docTab);
  6409. if (ctxt->incTab != NULL)
  6410. xmlFree(ctxt->incTab);
  6411. if (ctxt->defTab != NULL) {
  6412. int i;
  6413. for (i = 0; i < ctxt->defNr; i++)
  6414. xmlRelaxNGFreeDefine(ctxt->defTab[i]);
  6415. xmlFree(ctxt->defTab);
  6416. }
  6417. if ((ctxt->document != NULL) && (ctxt->freedoc))
  6418. xmlFreeDoc(ctxt->document);
  6419. xmlFree(ctxt);
  6420. }
  6421. /**
  6422. * xmlRelaxNGNormExtSpace:
  6423. * @value: a value
  6424. *
  6425. * Removes the leading and ending spaces of the value
  6426. * The string is modified "in situ"
  6427. */
  6428. static void
  6429. xmlRelaxNGNormExtSpace(xmlChar * value)
  6430. {
  6431. xmlChar *start = value;
  6432. xmlChar *cur = value;
  6433. if (value == NULL)
  6434. return;
  6435. while (IS_BLANK_CH(*cur))
  6436. cur++;
  6437. if (cur == start) {
  6438. do {
  6439. while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
  6440. cur++;
  6441. if (*cur == 0)
  6442. return;
  6443. start = cur;
  6444. while (IS_BLANK_CH(*cur))
  6445. cur++;
  6446. if (*cur == 0) {
  6447. *start = 0;
  6448. return;
  6449. }
  6450. } while (1);
  6451. } else {
  6452. do {
  6453. while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
  6454. *start++ = *cur++;
  6455. if (*cur == 0) {
  6456. *start = 0;
  6457. return;
  6458. }
  6459. /* don't try to normalize the inner spaces */
  6460. while (IS_BLANK_CH(*cur))
  6461. cur++;
  6462. if (*cur == 0) {
  6463. *start = 0;
  6464. return;
  6465. }
  6466. *start++ = *cur++;
  6467. } while (1);
  6468. }
  6469. }
  6470. /**
  6471. * xmlRelaxNGCleanupAttributes:
  6472. * @ctxt: a Relax-NG parser context
  6473. * @node: a Relax-NG node
  6474. *
  6475. * Check all the attributes on the given node
  6476. */
  6477. static void
  6478. xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
  6479. {
  6480. xmlAttrPtr cur, next;
  6481. cur = node->properties;
  6482. while (cur != NULL) {
  6483. next = cur->next;
  6484. if ((cur->ns == NULL) ||
  6485. (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
  6486. if (xmlStrEqual(cur->name, BAD_CAST "name")) {
  6487. if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
  6488. (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
  6489. (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
  6490. (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
  6491. (!xmlStrEqual(node->name, BAD_CAST "param")) &&
  6492. (!xmlStrEqual(node->name, BAD_CAST "define"))) {
  6493. xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
  6494. "Attribute %s is not allowed on %s\n",
  6495. cur->name, node->name);
  6496. }
  6497. } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
  6498. if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
  6499. (!xmlStrEqual(node->name, BAD_CAST "data"))) {
  6500. xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
  6501. "Attribute %s is not allowed on %s\n",
  6502. cur->name, node->name);
  6503. }
  6504. } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
  6505. if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
  6506. (!xmlStrEqual(node->name, BAD_CAST "include"))) {
  6507. xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
  6508. "Attribute %s is not allowed on %s\n",
  6509. cur->name, node->name);
  6510. }
  6511. } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
  6512. if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
  6513. (!xmlStrEqual(node->name, BAD_CAST "define"))) {
  6514. xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
  6515. "Attribute %s is not allowed on %s\n",
  6516. cur->name, node->name);
  6517. }
  6518. } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
  6519. xmlChar *val;
  6520. xmlURIPtr uri;
  6521. val = xmlNodeListGetString(node->doc, cur->children, 1);
  6522. if (val != NULL) {
  6523. if (val[0] != 0) {
  6524. uri = xmlParseURI((const char *) val);
  6525. if (uri == NULL) {
  6526. xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI,
  6527. "Attribute %s contains invalid URI %s\n",
  6528. cur->name, val);
  6529. } else {
  6530. if (uri->scheme == NULL) {
  6531. xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE,
  6532. "Attribute %s URI %s is not absolute\n",
  6533. cur->name, val);
  6534. }
  6535. if (uri->fragment != NULL) {
  6536. xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT,
  6537. "Attribute %s URI %s has a fragment ID\n",
  6538. cur->name, val);
  6539. }
  6540. xmlFreeURI(uri);
  6541. }
  6542. }
  6543. xmlFree(val);
  6544. }
  6545. } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
  6546. xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE,
  6547. "Unknown attribute %s on %s\n", cur->name,
  6548. node->name);
  6549. }
  6550. }
  6551. cur = next;
  6552. }
  6553. }
  6554. /**
  6555. * xmlRelaxNGCleanupTree:
  6556. * @ctxt: a Relax-NG parser context
  6557. * @root: an xmlNodePtr subtree
  6558. *
  6559. * Cleanup the subtree from unwanted nodes for parsing, resolve
  6560. * Include and externalRef lookups.
  6561. */
  6562. static void
  6563. xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
  6564. {
  6565. xmlNodePtr cur, delete;
  6566. delete = NULL;
  6567. cur = root;
  6568. while (cur != NULL) {
  6569. if (delete != NULL) {
  6570. xmlUnlinkNode(delete);
  6571. xmlFreeNode(delete);
  6572. delete = NULL;
  6573. }
  6574. if (cur->type == XML_ELEMENT_NODE) {
  6575. /*
  6576. * Simplification 4.1. Annotations
  6577. */
  6578. if ((cur->ns == NULL) ||
  6579. (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
  6580. if ((cur->parent != NULL) &&
  6581. (cur->parent->type == XML_ELEMENT_NODE) &&
  6582. ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
  6583. (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
  6584. (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
  6585. xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT,
  6586. "element %s doesn't allow foreign elements\n",
  6587. cur->parent->name, NULL);
  6588. }
  6589. delete = cur;
  6590. goto skip_children;
  6591. } else {
  6592. xmlRelaxNGCleanupAttributes(ctxt, cur);
  6593. if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
  6594. xmlChar *href, *ns, *base, *URL;
  6595. xmlRelaxNGDocumentPtr docu;
  6596. xmlNodePtr tmp;
  6597. xmlURIPtr uri;
  6598. ns = xmlGetProp(cur, BAD_CAST "ns");
  6599. if (ns == NULL) {
  6600. tmp = cur->parent;
  6601. while ((tmp != NULL) &&
  6602. (tmp->type == XML_ELEMENT_NODE)) {
  6603. ns = xmlGetProp(tmp, BAD_CAST "ns");
  6604. if (ns != NULL)
  6605. break;
  6606. tmp = tmp->parent;
  6607. }
  6608. }
  6609. href = xmlGetProp(cur, BAD_CAST "href");
  6610. if (href == NULL) {
  6611. xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
  6612. "xmlRelaxNGParse: externalRef has no href attribute\n",
  6613. NULL, NULL);
  6614. if (ns != NULL)
  6615. xmlFree(ns);
  6616. delete = cur;
  6617. goto skip_children;
  6618. }
  6619. uri = xmlParseURI((const char *) href);
  6620. if (uri == NULL) {
  6621. xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
  6622. "Incorrect URI for externalRef %s\n",
  6623. href, NULL);
  6624. if (ns != NULL)
  6625. xmlFree(ns);
  6626. if (href != NULL)
  6627. xmlFree(href);
  6628. delete = cur;
  6629. goto skip_children;
  6630. }
  6631. if (uri->fragment != NULL) {
  6632. xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
  6633. "Fragment forbidden in URI for externalRef %s\n",
  6634. href, NULL);
  6635. if (ns != NULL)
  6636. xmlFree(ns);
  6637. xmlFreeURI(uri);
  6638. if (href != NULL)
  6639. xmlFree(href);
  6640. delete = cur;
  6641. goto skip_children;
  6642. }
  6643. xmlFreeURI(uri);
  6644. base = xmlNodeGetBase(cur->doc, cur);
  6645. URL = xmlBuildURI(href, base);
  6646. if (URL == NULL) {
  6647. xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
  6648. "Failed to compute URL for externalRef %s\n",
  6649. href, NULL);
  6650. if (ns != NULL)
  6651. xmlFree(ns);
  6652. if (href != NULL)
  6653. xmlFree(href);
  6654. if (base != NULL)
  6655. xmlFree(base);
  6656. delete = cur;
  6657. goto skip_children;
  6658. }
  6659. if (href != NULL)
  6660. xmlFree(href);
  6661. if (base != NULL)
  6662. xmlFree(base);
  6663. docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
  6664. if (docu == NULL) {
  6665. xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE,
  6666. "Failed to load externalRef %s\n", URL,
  6667. NULL);
  6668. if (ns != NULL)
  6669. xmlFree(ns);
  6670. xmlFree(URL);
  6671. delete = cur;
  6672. goto skip_children;
  6673. }
  6674. if (ns != NULL)
  6675. xmlFree(ns);
  6676. xmlFree(URL);
  6677. cur->psvi = docu;
  6678. } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
  6679. xmlChar *href, *ns, *base, *URL;
  6680. xmlRelaxNGIncludePtr incl;
  6681. xmlNodePtr tmp;
  6682. href = xmlGetProp(cur, BAD_CAST "href");
  6683. if (href == NULL) {
  6684. xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
  6685. "xmlRelaxNGParse: include has no href attribute\n",
  6686. NULL, NULL);
  6687. delete = cur;
  6688. goto skip_children;
  6689. }
  6690. base = xmlNodeGetBase(cur->doc, cur);
  6691. URL = xmlBuildURI(href, base);
  6692. if (URL == NULL) {
  6693. xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
  6694. "Failed to compute URL for include %s\n",
  6695. href, NULL);
  6696. if (href != NULL)
  6697. xmlFree(href);
  6698. if (base != NULL)
  6699. xmlFree(base);
  6700. delete = cur;
  6701. goto skip_children;
  6702. }
  6703. if (href != NULL)
  6704. xmlFree(href);
  6705. if (base != NULL)
  6706. xmlFree(base);
  6707. ns = xmlGetProp(cur, BAD_CAST "ns");
  6708. if (ns == NULL) {
  6709. tmp = cur->parent;
  6710. while ((tmp != NULL) &&
  6711. (tmp->type == XML_ELEMENT_NODE)) {
  6712. ns = xmlGetProp(tmp, BAD_CAST "ns");
  6713. if (ns != NULL)
  6714. break;
  6715. tmp = tmp->parent;
  6716. }
  6717. }
  6718. incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
  6719. if (ns != NULL)
  6720. xmlFree(ns);
  6721. if (incl == NULL) {
  6722. xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE,
  6723. "Failed to load include %s\n", URL,
  6724. NULL);
  6725. xmlFree(URL);
  6726. delete = cur;
  6727. goto skip_children;
  6728. }
  6729. xmlFree(URL);
  6730. cur->psvi = incl;
  6731. } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
  6732. (xmlStrEqual(cur->name, BAD_CAST "attribute")))
  6733. {
  6734. xmlChar *name, *ns;
  6735. xmlNodePtr text = NULL;
  6736. /*
  6737. * Simplification 4.8. name attribute of element
  6738. * and attribute elements
  6739. */
  6740. name = xmlGetProp(cur, BAD_CAST "name");
  6741. if (name != NULL) {
  6742. if (cur->children == NULL) {
  6743. text =
  6744. xmlNewChild(cur, cur->ns, BAD_CAST "name",
  6745. name);
  6746. } else {
  6747. xmlNodePtr node;
  6748. node = xmlNewDocNode(cur->doc, cur->ns,
  6749. BAD_CAST "name", NULL);
  6750. if (node != NULL) {
  6751. xmlAddPrevSibling(cur->children, node);
  6752. text = xmlNewText(name);
  6753. xmlAddChild(node, text);
  6754. text = node;
  6755. }
  6756. }
  6757. if (text == NULL) {
  6758. xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE,
  6759. "Failed to create a name %s element\n",
  6760. name, NULL);
  6761. }
  6762. xmlUnsetProp(cur, BAD_CAST "name");
  6763. xmlFree(name);
  6764. ns = xmlGetProp(cur, BAD_CAST "ns");
  6765. if (ns != NULL) {
  6766. if (text != NULL) {
  6767. xmlSetProp(text, BAD_CAST "ns", ns);
  6768. /* xmlUnsetProp(cur, BAD_CAST "ns"); */
  6769. }
  6770. xmlFree(ns);
  6771. } else if (xmlStrEqual(cur->name,
  6772. BAD_CAST "attribute")) {
  6773. xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
  6774. }
  6775. }
  6776. } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
  6777. (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
  6778. (xmlStrEqual(cur->name, BAD_CAST "value"))) {
  6779. /*
  6780. * Simplification 4.8. name attribute of element
  6781. * and attribute elements
  6782. */
  6783. if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
  6784. xmlNodePtr node;
  6785. xmlChar *ns = NULL;
  6786. node = cur->parent;
  6787. while ((node != NULL) &&
  6788. (node->type == XML_ELEMENT_NODE)) {
  6789. ns = xmlGetProp(node, BAD_CAST "ns");
  6790. if (ns != NULL) {
  6791. break;
  6792. }
  6793. node = node->parent;
  6794. }
  6795. if (ns == NULL) {
  6796. xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
  6797. } else {
  6798. xmlSetProp(cur, BAD_CAST "ns", ns);
  6799. xmlFree(ns);
  6800. }
  6801. }
  6802. if (xmlStrEqual(cur->name, BAD_CAST "name")) {
  6803. xmlChar *name, *local, *prefix;
  6804. /*
  6805. * Simplification: 4.10. QNames
  6806. */
  6807. name = xmlNodeGetContent(cur);
  6808. if (name != NULL) {
  6809. local = xmlSplitQName2(name, &prefix);
  6810. if (local != NULL) {
  6811. xmlNsPtr ns;
  6812. ns = xmlSearchNs(cur->doc, cur, prefix);
  6813. if (ns == NULL) {
  6814. xmlRngPErr(ctxt, cur,
  6815. XML_RNGP_PREFIX_UNDEFINED,
  6816. "xmlRelaxNGParse: no namespace for prefix %s\n",
  6817. prefix, NULL);
  6818. } else {
  6819. xmlSetProp(cur, BAD_CAST "ns",
  6820. ns->href);
  6821. xmlNodeSetContent(cur, local);
  6822. }
  6823. xmlFree(local);
  6824. xmlFree(prefix);
  6825. }
  6826. xmlFree(name);
  6827. }
  6828. }
  6829. /*
  6830. * 4.16
  6831. */
  6832. if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
  6833. if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
  6834. xmlRngPErr(ctxt, cur,
  6835. XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME,
  6836. "Found nsName/except//nsName forbidden construct\n",
  6837. NULL, NULL);
  6838. }
  6839. }
  6840. } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
  6841. (cur != root)) {
  6842. int oldflags = ctxt->flags;
  6843. /*
  6844. * 4.16
  6845. */
  6846. if ((cur->parent != NULL) &&
  6847. (xmlStrEqual
  6848. (cur->parent->name, BAD_CAST "anyName"))) {
  6849. ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
  6850. xmlRelaxNGCleanupTree(ctxt, cur);
  6851. ctxt->flags = oldflags;
  6852. goto skip_children;
  6853. } else if ((cur->parent != NULL) &&
  6854. (xmlStrEqual
  6855. (cur->parent->name, BAD_CAST "nsName"))) {
  6856. ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
  6857. xmlRelaxNGCleanupTree(ctxt, cur);
  6858. ctxt->flags = oldflags;
  6859. goto skip_children;
  6860. }
  6861. } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
  6862. /*
  6863. * 4.16
  6864. */
  6865. if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
  6866. xmlRngPErr(ctxt, cur,
  6867. XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME,
  6868. "Found anyName/except//anyName forbidden construct\n",
  6869. NULL, NULL);
  6870. } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
  6871. xmlRngPErr(ctxt, cur,
  6872. XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME,
  6873. "Found nsName/except//anyName forbidden construct\n",
  6874. NULL, NULL);
  6875. }
  6876. }
  6877. /*
  6878. * Thisd is not an else since "include" is transformed
  6879. * into a div
  6880. */
  6881. if (xmlStrEqual(cur->name, BAD_CAST "div")) {
  6882. xmlChar *ns;
  6883. xmlNodePtr child, ins, tmp;
  6884. /*
  6885. * implements rule 4.11
  6886. */
  6887. ns = xmlGetProp(cur, BAD_CAST "ns");
  6888. child = cur->children;
  6889. ins = cur;
  6890. while (child != NULL) {
  6891. if (ns != NULL) {
  6892. if (!xmlHasProp(child, BAD_CAST "ns")) {
  6893. xmlSetProp(child, BAD_CAST "ns", ns);
  6894. }
  6895. }
  6896. tmp = child->next;
  6897. xmlUnlinkNode(child);
  6898. ins = xmlAddNextSibling(ins, child);
  6899. child = tmp;
  6900. }
  6901. if (ns != NULL)
  6902. xmlFree(ns);
  6903. /*
  6904. * Since we are about to delete cur, if it's nsDef is non-NULL we
  6905. * need to preserve it (it contains the ns definitions for the
  6906. * children we just moved). We'll just stick it on to the end
  6907. * of cur->parent's list, since it's never going to be re-serialized
  6908. * (bug 143738).
  6909. */
  6910. if (cur->nsDef != NULL) {
  6911. xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef;
  6912. while (parDef->next != NULL)
  6913. parDef = parDef->next;
  6914. parDef->next = cur->nsDef;
  6915. cur->nsDef = NULL;
  6916. }
  6917. delete = cur;
  6918. goto skip_children;
  6919. }
  6920. }
  6921. }
  6922. /*
  6923. * Simplification 4.2 whitespaces
  6924. */
  6925. else if ((cur->type == XML_TEXT_NODE) ||
  6926. (cur->type == XML_CDATA_SECTION_NODE)) {
  6927. if (IS_BLANK_NODE(cur)) {
  6928. if (cur->parent->type == XML_ELEMENT_NODE) {
  6929. if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value"))
  6930. &&
  6931. (!xmlStrEqual
  6932. (cur->parent->name, BAD_CAST "param")))
  6933. delete = cur;
  6934. } else {
  6935. delete = cur;
  6936. goto skip_children;
  6937. }
  6938. }
  6939. } else {
  6940. delete = cur;
  6941. goto skip_children;
  6942. }
  6943. /*
  6944. * Skip to next node
  6945. */
  6946. if (cur->children != NULL) {
  6947. if ((cur->children->type != XML_ENTITY_DECL) &&
  6948. (cur->children->type != XML_ENTITY_REF_NODE) &&
  6949. (cur->children->type != XML_ENTITY_NODE)) {
  6950. cur = cur->children;
  6951. continue;
  6952. }
  6953. }
  6954. skip_children:
  6955. if (cur->next != NULL) {
  6956. cur = cur->next;
  6957. continue;
  6958. }
  6959. do {
  6960. cur = cur->parent;
  6961. if (cur == NULL)
  6962. break;
  6963. if (cur == root) {
  6964. cur = NULL;
  6965. break;
  6966. }
  6967. if (cur->next != NULL) {
  6968. cur = cur->next;
  6969. break;
  6970. }
  6971. } while (cur != NULL);
  6972. }
  6973. if (delete != NULL) {
  6974. xmlUnlinkNode(delete);
  6975. xmlFreeNode(delete);
  6976. delete = NULL;
  6977. }
  6978. }
  6979. /**
  6980. * xmlRelaxNGCleanupDoc:
  6981. * @ctxt: a Relax-NG parser context
  6982. * @doc: an xmldocPtr document pointer
  6983. *
  6984. * Cleanup the document from unwanted nodes for parsing, resolve
  6985. * Include and externalRef lookups.
  6986. *
  6987. * Returns the cleaned up document or NULL in case of error
  6988. */
  6989. static xmlDocPtr
  6990. xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
  6991. {
  6992. xmlNodePtr root;
  6993. /*
  6994. * Extract the root
  6995. */
  6996. root = xmlDocGetRootElement(doc);
  6997. if (root == NULL) {
  6998. xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
  6999. ctxt->URL, NULL);
  7000. return (NULL);
  7001. }
  7002. xmlRelaxNGCleanupTree(ctxt, root);
  7003. return (doc);
  7004. }
  7005. /**
  7006. * xmlRelaxNGParse:
  7007. * @ctxt: a Relax-NG parser context
  7008. *
  7009. * parse a schema definition resource and build an internal
  7010. * XML Shema struture which can be used to validate instances.
  7011. *
  7012. * Returns the internal XML RelaxNG structure built from the resource or
  7013. * NULL in case of error
  7014. */
  7015. xmlRelaxNGPtr
  7016. xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
  7017. {
  7018. xmlRelaxNGPtr ret = NULL;
  7019. xmlDocPtr doc;
  7020. xmlNodePtr root;
  7021. xmlRelaxNGInitTypes();
  7022. if (ctxt == NULL)
  7023. return (NULL);
  7024. /*
  7025. * First step is to parse the input document into an DOM/Infoset
  7026. */
  7027. if (ctxt->URL != NULL) {
  7028. doc = xmlReadFile((const char *) ctxt->URL,NULL,0);
  7029. if (doc == NULL) {
  7030. xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
  7031. "xmlRelaxNGParse: could not load %s\n", ctxt->URL,
  7032. NULL);
  7033. return (NULL);
  7034. }
  7035. } else if (ctxt->buffer != NULL) {
  7036. doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0);
  7037. if (doc == NULL) {
  7038. xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
  7039. "xmlRelaxNGParse: could not parse schemas\n", NULL,
  7040. NULL);
  7041. return (NULL);
  7042. }
  7043. doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
  7044. ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
  7045. } else if (ctxt->document != NULL) {
  7046. doc = ctxt->document;
  7047. } else {
  7048. xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
  7049. "xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
  7050. return (NULL);
  7051. }
  7052. ctxt->document = doc;
  7053. /*
  7054. * Some preprocessing of the document content
  7055. */
  7056. doc = xmlRelaxNGCleanupDoc(ctxt, doc);
  7057. if (doc == NULL) {
  7058. xmlFreeDoc(ctxt->document);
  7059. ctxt->document = NULL;
  7060. return (NULL);
  7061. }
  7062. /*
  7063. * Then do the parsing for good
  7064. */
  7065. root = xmlDocGetRootElement(doc);
  7066. if (root == NULL) {
  7067. xmlRngPErr(ctxt, (xmlNodePtr) doc,
  7068. XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
  7069. (ctxt->URL ? ctxt->URL : BAD_CAST "schemas"), NULL);
  7070. xmlFreeDoc(ctxt->document);
  7071. ctxt->document = NULL;
  7072. return (NULL);
  7073. }
  7074. ret = xmlRelaxNGParseDocument(ctxt, root);
  7075. if (ret == NULL) {
  7076. xmlFreeDoc(ctxt->document);
  7077. ctxt->document = NULL;
  7078. return (NULL);
  7079. }
  7080. /*
  7081. * Check the ref/defines links
  7082. */
  7083. /*
  7084. * try to preprocess interleaves
  7085. */
  7086. if (ctxt->interleaves != NULL) {
  7087. xmlHashScan(ctxt->interleaves,
  7088. (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt);
  7089. }
  7090. /*
  7091. * if there was a parsing error return NULL
  7092. */
  7093. if (ctxt->nbErrors > 0) {
  7094. xmlRelaxNGFree(ret);
  7095. ctxt->document = NULL;
  7096. xmlFreeDoc(doc);
  7097. return (NULL);
  7098. }
  7099. /*
  7100. * try to compile (parts of) the schemas
  7101. */
  7102. if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
  7103. if (ret->topgrammar->start->type != XML_RELAXNG_START) {
  7104. xmlRelaxNGDefinePtr def;
  7105. def = xmlRelaxNGNewDefine(ctxt, NULL);
  7106. if (def != NULL) {
  7107. def->type = XML_RELAXNG_START;
  7108. def->content = ret->topgrammar->start;
  7109. ret->topgrammar->start = def;
  7110. }
  7111. }
  7112. xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
  7113. }
  7114. /*
  7115. * Transfer the pointer for cleanup at the schema level.
  7116. */
  7117. ret->doc = doc;
  7118. ctxt->document = NULL;
  7119. ret->documents = ctxt->documents;
  7120. ctxt->documents = NULL;
  7121. ret->includes = ctxt->includes;
  7122. ctxt->includes = NULL;
  7123. ret->defNr = ctxt->defNr;
  7124. ret->defTab = ctxt->defTab;
  7125. ctxt->defTab = NULL;
  7126. if (ctxt->idref == 1)
  7127. ret->idref = 1;
  7128. return (ret);
  7129. }
  7130. /**
  7131. * xmlRelaxNGSetParserErrors:
  7132. * @ctxt: a Relax-NG validation context
  7133. * @err: the error callback
  7134. * @warn: the warning callback
  7135. * @ctx: contextual data for the callbacks
  7136. *
  7137. * Set the callback functions used to handle errors for a validation context
  7138. */
  7139. void
  7140. xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
  7141. xmlRelaxNGValidityErrorFunc err,
  7142. xmlRelaxNGValidityWarningFunc warn, void *ctx)
  7143. {
  7144. if (ctxt == NULL)
  7145. return;
  7146. ctxt->error = err;
  7147. ctxt->warning = warn;
  7148. ctxt->serror = NULL;
  7149. ctxt->userData = ctx;
  7150. }
  7151. /**
  7152. * xmlRelaxNGGetParserErrors:
  7153. * @ctxt: a Relax-NG validation context
  7154. * @err: the error callback result
  7155. * @warn: the warning callback result
  7156. * @ctx: contextual data for the callbacks result
  7157. *
  7158. * Get the callback information used to handle errors for a validation context
  7159. *
  7160. * Returns -1 in case of failure, 0 otherwise.
  7161. */
  7162. int
  7163. xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
  7164. xmlRelaxNGValidityErrorFunc * err,
  7165. xmlRelaxNGValidityWarningFunc * warn, void **ctx)
  7166. {
  7167. if (ctxt == NULL)
  7168. return (-1);
  7169. if (err != NULL)
  7170. *err = ctxt->error;
  7171. if (warn != NULL)
  7172. *warn = ctxt->warning;
  7173. if (ctx != NULL)
  7174. *ctx = ctxt->userData;
  7175. return (0);
  7176. }
  7177. /**
  7178. * xmlRelaxNGSetParserStructuredErrors:
  7179. * @ctxt: a Relax-NG parser context
  7180. * @serror: the error callback
  7181. * @ctx: contextual data for the callbacks
  7182. *
  7183. * Set the callback functions used to handle errors for a parsing context
  7184. */
  7185. void
  7186. xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxtPtr ctxt,
  7187. xmlStructuredErrorFunc serror,
  7188. void *ctx)
  7189. {
  7190. if (ctxt == NULL)
  7191. return;
  7192. ctxt->serror = serror;
  7193. ctxt->error = NULL;
  7194. ctxt->warning = NULL;
  7195. ctxt->userData = ctx;
  7196. }
  7197. #ifdef LIBXML_OUTPUT_ENABLED
  7198. /************************************************************************
  7199. * *
  7200. * Dump back a compiled form *
  7201. * *
  7202. ************************************************************************/
  7203. static void xmlRelaxNGDumpDefine(FILE * output,
  7204. xmlRelaxNGDefinePtr define);
  7205. /**
  7206. * xmlRelaxNGDumpDefines:
  7207. * @output: the file output
  7208. * @defines: a list of define structures
  7209. *
  7210. * Dump a RelaxNG structure back
  7211. */
  7212. static void
  7213. xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
  7214. {
  7215. while (defines != NULL) {
  7216. xmlRelaxNGDumpDefine(output, defines);
  7217. defines = defines->next;
  7218. }
  7219. }
  7220. /**
  7221. * xmlRelaxNGDumpDefine:
  7222. * @output: the file output
  7223. * @define: a define structure
  7224. *
  7225. * Dump a RelaxNG structure back
  7226. */
  7227. static void
  7228. xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
  7229. {
  7230. if (define == NULL)
  7231. return;
  7232. switch (define->type) {
  7233. case XML_RELAXNG_EMPTY:
  7234. fprintf(output, "<empty/>\n");
  7235. break;
  7236. case XML_RELAXNG_NOT_ALLOWED:
  7237. fprintf(output, "<notAllowed/>\n");
  7238. break;
  7239. case XML_RELAXNG_TEXT:
  7240. fprintf(output, "<text/>\n");
  7241. break;
  7242. case XML_RELAXNG_ELEMENT:
  7243. fprintf(output, "<element>\n");
  7244. if (define->name != NULL) {
  7245. fprintf(output, "<name");
  7246. if (define->ns != NULL)
  7247. fprintf(output, " ns=\"%s\"", define->ns);
  7248. fprintf(output, ">%s</name>\n", define->name);
  7249. }
  7250. xmlRelaxNGDumpDefines(output, define->attrs);
  7251. xmlRelaxNGDumpDefines(output, define->content);
  7252. fprintf(output, "</element>\n");
  7253. break;
  7254. case XML_RELAXNG_LIST:
  7255. fprintf(output, "<list>\n");
  7256. xmlRelaxNGDumpDefines(output, define->content);
  7257. fprintf(output, "</list>\n");
  7258. break;
  7259. case XML_RELAXNG_ONEORMORE:
  7260. fprintf(output, "<oneOrMore>\n");
  7261. xmlRelaxNGDumpDefines(output, define->content);
  7262. fprintf(output, "</oneOrMore>\n");
  7263. break;
  7264. case XML_RELAXNG_ZEROORMORE:
  7265. fprintf(output, "<zeroOrMore>\n");
  7266. xmlRelaxNGDumpDefines(output, define->content);
  7267. fprintf(output, "</zeroOrMore>\n");
  7268. break;
  7269. case XML_RELAXNG_CHOICE:
  7270. fprintf(output, "<choice>\n");
  7271. xmlRelaxNGDumpDefines(output, define->content);
  7272. fprintf(output, "</choice>\n");
  7273. break;
  7274. case XML_RELAXNG_GROUP:
  7275. fprintf(output, "<group>\n");
  7276. xmlRelaxNGDumpDefines(output, define->content);
  7277. fprintf(output, "</group>\n");
  7278. break;
  7279. case XML_RELAXNG_INTERLEAVE:
  7280. fprintf(output, "<interleave>\n");
  7281. xmlRelaxNGDumpDefines(output, define->content);
  7282. fprintf(output, "</interleave>\n");
  7283. break;
  7284. case XML_RELAXNG_OPTIONAL:
  7285. fprintf(output, "<optional>\n");
  7286. xmlRelaxNGDumpDefines(output, define->content);
  7287. fprintf(output, "</optional>\n");
  7288. break;
  7289. case XML_RELAXNG_ATTRIBUTE:
  7290. fprintf(output, "<attribute>\n");
  7291. xmlRelaxNGDumpDefines(output, define->content);
  7292. fprintf(output, "</attribute>\n");
  7293. break;
  7294. case XML_RELAXNG_DEF:
  7295. fprintf(output, "<define");
  7296. if (define->name != NULL)
  7297. fprintf(output, " name=\"%s\"", define->name);
  7298. fprintf(output, ">\n");
  7299. xmlRelaxNGDumpDefines(output, define->content);
  7300. fprintf(output, "</define>\n");
  7301. break;
  7302. case XML_RELAXNG_REF:
  7303. fprintf(output, "<ref");
  7304. if (define->name != NULL)
  7305. fprintf(output, " name=\"%s\"", define->name);
  7306. fprintf(output, ">\n");
  7307. xmlRelaxNGDumpDefines(output, define->content);
  7308. fprintf(output, "</ref>\n");
  7309. break;
  7310. case XML_RELAXNG_PARENTREF:
  7311. fprintf(output, "<parentRef");
  7312. if (define->name != NULL)
  7313. fprintf(output, " name=\"%s\"", define->name);
  7314. fprintf(output, ">\n");
  7315. xmlRelaxNGDumpDefines(output, define->content);
  7316. fprintf(output, "</parentRef>\n");
  7317. break;
  7318. case XML_RELAXNG_EXTERNALREF:
  7319. fprintf(output, "<externalRef>");
  7320. xmlRelaxNGDumpDefines(output, define->content);
  7321. fprintf(output, "</externalRef>\n");
  7322. break;
  7323. case XML_RELAXNG_DATATYPE:
  7324. case XML_RELAXNG_VALUE:
  7325. TODO break;
  7326. case XML_RELAXNG_START:
  7327. case XML_RELAXNG_EXCEPT:
  7328. case XML_RELAXNG_PARAM:
  7329. TODO break;
  7330. case XML_RELAXNG_NOOP:
  7331. xmlRelaxNGDumpDefines(output, define->content);
  7332. break;
  7333. }
  7334. }
  7335. /**
  7336. * xmlRelaxNGDumpGrammar:
  7337. * @output: the file output
  7338. * @grammar: a grammar structure
  7339. * @top: is this a top grammar
  7340. *
  7341. * Dump a RelaxNG structure back
  7342. */
  7343. static void
  7344. xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
  7345. {
  7346. if (grammar == NULL)
  7347. return;
  7348. fprintf(output, "<grammar");
  7349. if (top)
  7350. fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
  7351. switch (grammar->combine) {
  7352. case XML_RELAXNG_COMBINE_UNDEFINED:
  7353. break;
  7354. case XML_RELAXNG_COMBINE_CHOICE:
  7355. fprintf(output, " combine=\"choice\"");
  7356. break;
  7357. case XML_RELAXNG_COMBINE_INTERLEAVE:
  7358. fprintf(output, " combine=\"interleave\"");
  7359. break;
  7360. default:
  7361. fprintf(output, " <!-- invalid combine value -->");
  7362. }
  7363. fprintf(output, ">\n");
  7364. if (grammar->start == NULL) {
  7365. fprintf(output, " <!-- grammar had no start -->");
  7366. } else {
  7367. fprintf(output, "<start>\n");
  7368. xmlRelaxNGDumpDefine(output, grammar->start);
  7369. fprintf(output, "</start>\n");
  7370. }
  7371. /* TODO ? Dump the defines ? */
  7372. fprintf(output, "</grammar>\n");
  7373. }
  7374. /**
  7375. * xmlRelaxNGDump:
  7376. * @output: the file output
  7377. * @schema: a schema structure
  7378. *
  7379. * Dump a RelaxNG structure back
  7380. */
  7381. void
  7382. xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
  7383. {
  7384. if (output == NULL)
  7385. return;
  7386. if (schema == NULL) {
  7387. fprintf(output, "RelaxNG empty or failed to compile\n");
  7388. return;
  7389. }
  7390. fprintf(output, "RelaxNG: ");
  7391. if (schema->doc == NULL) {
  7392. fprintf(output, "no document\n");
  7393. } else if (schema->doc->URL != NULL) {
  7394. fprintf(output, "%s\n", schema->doc->URL);
  7395. } else {
  7396. fprintf(output, "\n");
  7397. }
  7398. if (schema->topgrammar == NULL) {
  7399. fprintf(output, "RelaxNG has no top grammar\n");
  7400. return;
  7401. }
  7402. xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
  7403. }
  7404. /**
  7405. * xmlRelaxNGDumpTree:
  7406. * @output: the file output
  7407. * @schema: a schema structure
  7408. *
  7409. * Dump the transformed RelaxNG tree.
  7410. */
  7411. void
  7412. xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
  7413. {
  7414. if (output == NULL)
  7415. return;
  7416. if (schema == NULL) {
  7417. fprintf(output, "RelaxNG empty or failed to compile\n");
  7418. return;
  7419. }
  7420. if (schema->doc == NULL) {
  7421. fprintf(output, "no document\n");
  7422. } else {
  7423. xmlDocDump(output, schema->doc);
  7424. }
  7425. }
  7426. #endif /* LIBXML_OUTPUT_ENABLED */
  7427. /************************************************************************
  7428. * *
  7429. * Validation of compiled content *
  7430. * *
  7431. ************************************************************************/
  7432. static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
  7433. xmlRelaxNGDefinePtr define);
  7434. /**
  7435. * xmlRelaxNGValidateCompiledCallback:
  7436. * @exec: the regular expression instance
  7437. * @token: the token which matched
  7438. * @transdata: callback data, the define for the subelement if available
  7439. @ @inputdata: callback data, the Relax NG validation context
  7440. *
  7441. * Handle the callback and if needed validate the element children.
  7442. */
  7443. static void
  7444. xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
  7445. const xmlChar * token,
  7446. void *transdata, void *inputdata)
  7447. {
  7448. xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
  7449. xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
  7450. int ret;
  7451. #ifdef DEBUG_COMPILE
  7452. xmlGenericError(xmlGenericErrorContext,
  7453. "Compiled callback for: '%s'\n", token);
  7454. #endif
  7455. if (ctxt == NULL) {
  7456. fprintf(stderr, "callback on %s missing context\n", token);
  7457. return;
  7458. }
  7459. if (define == NULL) {
  7460. if (token[0] == '#')
  7461. return;
  7462. fprintf(stderr, "callback on %s missing define\n", token);
  7463. if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
  7464. ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
  7465. return;
  7466. }
  7467. if ((ctxt == NULL) || (define == NULL)) {
  7468. fprintf(stderr, "callback on %s missing info\n", token);
  7469. if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
  7470. ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
  7471. return;
  7472. } else if (define->type != XML_RELAXNG_ELEMENT) {
  7473. fprintf(stderr, "callback on %s define is not element\n", token);
  7474. if (ctxt->errNo == XML_RELAXNG_OK)
  7475. ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
  7476. return;
  7477. }
  7478. ret = xmlRelaxNGValidateDefinition(ctxt, define);
  7479. if (ret != 0)
  7480. ctxt->perr = ret;
  7481. }
  7482. /**
  7483. * xmlRelaxNGValidateCompiledContent:
  7484. * @ctxt: the RelaxNG validation context
  7485. * @regexp: the regular expression as compiled
  7486. * @content: list of children to test against the regexp
  7487. *
  7488. * Validate the content model of an element or start using the regexp
  7489. *
  7490. * Returns 0 in case of success, -1 in case of error.
  7491. */
  7492. static int
  7493. xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
  7494. xmlRegexpPtr regexp, xmlNodePtr content)
  7495. {
  7496. xmlRegExecCtxtPtr exec;
  7497. xmlNodePtr cur;
  7498. int ret = 0;
  7499. int oldperr;
  7500. if ((ctxt == NULL) || (regexp == NULL))
  7501. return (-1);
  7502. oldperr = ctxt->perr;
  7503. exec = xmlRegNewExecCtxt(regexp,
  7504. xmlRelaxNGValidateCompiledCallback, ctxt);
  7505. ctxt->perr = 0;
  7506. cur = content;
  7507. while (cur != NULL) {
  7508. ctxt->state->seq = cur;
  7509. switch (cur->type) {
  7510. case XML_TEXT_NODE:
  7511. case XML_CDATA_SECTION_NODE:
  7512. if (xmlIsBlankNode(cur))
  7513. break;
  7514. ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
  7515. if (ret < 0) {
  7516. VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
  7517. cur->parent->name);
  7518. }
  7519. break;
  7520. case XML_ELEMENT_NODE:
  7521. if (cur->ns != NULL) {
  7522. ret = xmlRegExecPushString2(exec, cur->name,
  7523. cur->ns->href, ctxt);
  7524. } else {
  7525. ret = xmlRegExecPushString(exec, cur->name, ctxt);
  7526. }
  7527. if (ret < 0) {
  7528. VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
  7529. }
  7530. break;
  7531. default:
  7532. break;
  7533. }
  7534. if (ret < 0)
  7535. break;
  7536. /*
  7537. * Switch to next element
  7538. */
  7539. cur = cur->next;
  7540. }
  7541. ret = xmlRegExecPushString(exec, NULL, NULL);
  7542. if (ret == 1) {
  7543. ret = 0;
  7544. ctxt->state->seq = NULL;
  7545. } else if (ret == 0) {
  7546. /*
  7547. * TODO: get some of the names needed to exit the current state of exec
  7548. */
  7549. VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
  7550. ret = -1;
  7551. if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
  7552. xmlRelaxNGDumpValidError(ctxt);
  7553. } else {
  7554. ret = -1;
  7555. }
  7556. xmlRegFreeExecCtxt(exec);
  7557. /*
  7558. * There might be content model errors outside of the pure
  7559. * regexp validation, e.g. for attribute values.
  7560. */
  7561. if ((ret == 0) && (ctxt->perr != 0)) {
  7562. ret = ctxt->perr;
  7563. }
  7564. ctxt->perr = oldperr;
  7565. return (ret);
  7566. }
  7567. /************************************************************************
  7568. * *
  7569. * Progressive validation of when possible *
  7570. * *
  7571. ************************************************************************/
  7572. static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
  7573. xmlRelaxNGDefinePtr defines);
  7574. static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
  7575. int dolog);
  7576. static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
  7577. /**
  7578. * xmlRelaxNGElemPush:
  7579. * @ctxt: the validation context
  7580. * @exec: the regexp runtime for the new content model
  7581. *
  7582. * Push a new regexp for the current node content model on the stack
  7583. *
  7584. * Returns 0 in case of success and -1 in case of error.
  7585. */
  7586. static int
  7587. xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
  7588. {
  7589. if (ctxt->elemTab == NULL) {
  7590. ctxt->elemMax = 10;
  7591. ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
  7592. sizeof
  7593. (xmlRegExecCtxtPtr));
  7594. if (ctxt->elemTab == NULL) {
  7595. xmlRngVErrMemory(ctxt, "validating\n");
  7596. return (-1);
  7597. }
  7598. }
  7599. if (ctxt->elemNr >= ctxt->elemMax) {
  7600. ctxt->elemMax *= 2;
  7601. ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
  7602. ctxt->elemMax *
  7603. sizeof
  7604. (xmlRegExecCtxtPtr));
  7605. if (ctxt->elemTab == NULL) {
  7606. xmlRngVErrMemory(ctxt, "validating\n");
  7607. return (-1);
  7608. }
  7609. }
  7610. ctxt->elemTab[ctxt->elemNr++] = exec;
  7611. ctxt->elem = exec;
  7612. return (0);
  7613. }
  7614. /**
  7615. * xmlRelaxNGElemPop:
  7616. * @ctxt: the validation context
  7617. *
  7618. * Pop the regexp of the current node content model from the stack
  7619. *
  7620. * Returns the exec or NULL if empty
  7621. */
  7622. static xmlRegExecCtxtPtr
  7623. xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
  7624. {
  7625. xmlRegExecCtxtPtr ret;
  7626. if (ctxt->elemNr <= 0)
  7627. return (NULL);
  7628. ctxt->elemNr--;
  7629. ret = ctxt->elemTab[ctxt->elemNr];
  7630. ctxt->elemTab[ctxt->elemNr] = NULL;
  7631. if (ctxt->elemNr > 0)
  7632. ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
  7633. else
  7634. ctxt->elem = NULL;
  7635. return (ret);
  7636. }
  7637. /**
  7638. * xmlRelaxNGValidateProgressiveCallback:
  7639. * @exec: the regular expression instance
  7640. * @token: the token which matched
  7641. * @transdata: callback data, the define for the subelement if available
  7642. @ @inputdata: callback data, the Relax NG validation context
  7643. *
  7644. * Handle the callback and if needed validate the element children.
  7645. * some of the in/out informations are passed via the context in @inputdata.
  7646. */
  7647. static void
  7648. xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
  7649. ATTRIBUTE_UNUSED,
  7650. const xmlChar * token,
  7651. void *transdata, void *inputdata)
  7652. {
  7653. xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
  7654. xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
  7655. xmlRelaxNGValidStatePtr state, oldstate;
  7656. xmlNodePtr node;
  7657. int ret = 0, oldflags;
  7658. #ifdef DEBUG_PROGRESSIVE
  7659. xmlGenericError(xmlGenericErrorContext,
  7660. "Progressive callback for: '%s'\n", token);
  7661. #endif
  7662. if (ctxt == NULL) {
  7663. fprintf(stderr, "callback on %s missing context\n", token);
  7664. return;
  7665. }
  7666. node = ctxt->pnode;
  7667. ctxt->pstate = 1;
  7668. if (define == NULL) {
  7669. if (token[0] == '#')
  7670. return;
  7671. fprintf(stderr, "callback on %s missing define\n", token);
  7672. if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
  7673. ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
  7674. ctxt->pstate = -1;
  7675. return;
  7676. }
  7677. if ((ctxt == NULL) || (define == NULL)) {
  7678. fprintf(stderr, "callback on %s missing info\n", token);
  7679. if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
  7680. ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
  7681. ctxt->pstate = -1;
  7682. return;
  7683. } else if (define->type != XML_RELAXNG_ELEMENT) {
  7684. fprintf(stderr, "callback on %s define is not element\n", token);
  7685. if (ctxt->errNo == XML_RELAXNG_OK)
  7686. ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
  7687. ctxt->pstate = -1;
  7688. return;
  7689. }
  7690. if (node->type != XML_ELEMENT_NODE) {
  7691. VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
  7692. if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
  7693. xmlRelaxNGDumpValidError(ctxt);
  7694. ctxt->pstate = -1;
  7695. return;
  7696. }
  7697. if (define->contModel == NULL) {
  7698. /*
  7699. * this node cannot be validated in a streamable fashion
  7700. */
  7701. #ifdef DEBUG_PROGRESSIVE
  7702. xmlGenericError(xmlGenericErrorContext,
  7703. "Element '%s' validation is not streamable\n",
  7704. token);
  7705. #endif
  7706. ctxt->pstate = 0;
  7707. ctxt->pdef = define;
  7708. return;
  7709. }
  7710. exec = xmlRegNewExecCtxt(define->contModel,
  7711. xmlRelaxNGValidateProgressiveCallback, ctxt);
  7712. if (exec == NULL) {
  7713. ctxt->pstate = -1;
  7714. return;
  7715. }
  7716. xmlRelaxNGElemPush(ctxt, exec);
  7717. /*
  7718. * Validate the attributes part of the content.
  7719. */
  7720. state = xmlRelaxNGNewValidState(ctxt, node);
  7721. if (state == NULL) {
  7722. ctxt->pstate = -1;
  7723. return;
  7724. }
  7725. oldstate = ctxt->state;
  7726. ctxt->state = state;
  7727. if (define->attrs != NULL) {
  7728. ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
  7729. if (ret != 0) {
  7730. ctxt->pstate = -1;
  7731. VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
  7732. }
  7733. }
  7734. if (ctxt->state != NULL) {
  7735. ctxt->state->seq = NULL;
  7736. ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
  7737. if (ret != 0) {
  7738. ctxt->pstate = -1;
  7739. }
  7740. xmlRelaxNGFreeValidState(ctxt, ctxt->state);
  7741. } else if (ctxt->states != NULL) {
  7742. int tmp = -1, i;
  7743. oldflags = ctxt->flags;
  7744. for (i = 0; i < ctxt->states->nbState; i++) {
  7745. state = ctxt->states->tabState[i];
  7746. ctxt->state = state;
  7747. ctxt->state->seq = NULL;
  7748. if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
  7749. tmp = 0;
  7750. break;
  7751. }
  7752. }
  7753. if (tmp != 0) {
  7754. /*
  7755. * validation error, log the message for the "best" one
  7756. */
  7757. ctxt->flags |= FLAGS_IGNORABLE;
  7758. xmlRelaxNGLogBestError(ctxt);
  7759. }
  7760. for (i = 0; i < ctxt->states->nbState; i++) {
  7761. xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
  7762. }
  7763. xmlRelaxNGFreeStates(ctxt, ctxt->states);
  7764. ctxt->states = NULL;
  7765. if ((ret == 0) && (tmp == -1))
  7766. ctxt->pstate = -1;
  7767. ctxt->flags = oldflags;
  7768. }
  7769. if (ctxt->pstate == -1) {
  7770. if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
  7771. xmlRelaxNGDumpValidError(ctxt);
  7772. }
  7773. }
  7774. ctxt->state = oldstate;
  7775. }
  7776. /**
  7777. * xmlRelaxNGValidatePushElement:
  7778. * @ctxt: the validation context
  7779. * @doc: a document instance
  7780. * @elem: an element instance
  7781. *
  7782. * Push a new element start on the RelaxNG validation stack.
  7783. *
  7784. * returns 1 if no validation problem was found or 0 if validating the
  7785. * element requires a full node, and -1 in case of error.
  7786. */
  7787. int
  7788. xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
  7789. xmlDocPtr doc ATTRIBUTE_UNUSED,
  7790. xmlNodePtr elem)
  7791. {
  7792. int ret = 1;
  7793. if ((ctxt == NULL) || (elem == NULL))
  7794. return (-1);
  7795. #ifdef DEBUG_PROGRESSIVE
  7796. xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
  7797. #endif
  7798. if (ctxt->elem == 0) {
  7799. xmlRelaxNGPtr schema;
  7800. xmlRelaxNGGrammarPtr grammar;
  7801. xmlRegExecCtxtPtr exec;
  7802. xmlRelaxNGDefinePtr define;
  7803. schema = ctxt->schema;
  7804. if (schema == NULL) {
  7805. VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
  7806. return (-1);
  7807. }
  7808. grammar = schema->topgrammar;
  7809. if ((grammar == NULL) || (grammar->start == NULL)) {
  7810. VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
  7811. return (-1);
  7812. }
  7813. define = grammar->start;
  7814. if (define->contModel == NULL) {
  7815. ctxt->pdef = define;
  7816. return (0);
  7817. }
  7818. exec = xmlRegNewExecCtxt(define->contModel,
  7819. xmlRelaxNGValidateProgressiveCallback,
  7820. ctxt);
  7821. if (exec == NULL) {
  7822. return (-1);
  7823. }
  7824. xmlRelaxNGElemPush(ctxt, exec);
  7825. }
  7826. ctxt->pnode = elem;
  7827. ctxt->pstate = 0;
  7828. if (elem->ns != NULL) {
  7829. ret =
  7830. xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
  7831. ctxt);
  7832. } else {
  7833. ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
  7834. }
  7835. if (ret < 0) {
  7836. VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
  7837. } else {
  7838. if (ctxt->pstate == 0)
  7839. ret = 0;
  7840. else if (ctxt->pstate < 0)
  7841. ret = -1;
  7842. else
  7843. ret = 1;
  7844. }
  7845. #ifdef DEBUG_PROGRESSIVE
  7846. if (ret < 0)
  7847. xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
  7848. elem->name);
  7849. #endif
  7850. return (ret);
  7851. }
  7852. /**
  7853. * xmlRelaxNGValidatePushCData:
  7854. * @ctxt: the RelaxNG validation context
  7855. * @data: some character data read
  7856. * @len: the lenght of the data
  7857. *
  7858. * check the CData parsed for validation in the current stack
  7859. *
  7860. * returns 1 if no validation problem was found or -1 otherwise
  7861. */
  7862. int
  7863. xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
  7864. const xmlChar * data, int len ATTRIBUTE_UNUSED)
  7865. {
  7866. int ret = 1;
  7867. if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
  7868. return (-1);
  7869. #ifdef DEBUG_PROGRESSIVE
  7870. xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
  7871. #endif
  7872. while (*data != 0) {
  7873. if (!IS_BLANK_CH(*data))
  7874. break;
  7875. data++;
  7876. }
  7877. if (*data == 0)
  7878. return (1);
  7879. ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
  7880. if (ret < 0) {
  7881. VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
  7882. #ifdef DEBUG_PROGRESSIVE
  7883. xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
  7884. #endif
  7885. return (-1);
  7886. }
  7887. return (1);
  7888. }
  7889. /**
  7890. * xmlRelaxNGValidatePopElement:
  7891. * @ctxt: the RelaxNG validation context
  7892. * @doc: a document instance
  7893. * @elem: an element instance
  7894. *
  7895. * Pop the element end from the RelaxNG validation stack.
  7896. *
  7897. * returns 1 if no validation problem was found or 0 otherwise
  7898. */
  7899. int
  7900. xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
  7901. xmlDocPtr doc ATTRIBUTE_UNUSED,
  7902. xmlNodePtr elem)
  7903. {
  7904. int ret;
  7905. xmlRegExecCtxtPtr exec;
  7906. if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
  7907. return (-1);
  7908. #ifdef DEBUG_PROGRESSIVE
  7909. xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
  7910. #endif
  7911. /*
  7912. * verify that we reached a terminal state of the content model.
  7913. */
  7914. exec = xmlRelaxNGElemPop(ctxt);
  7915. ret = xmlRegExecPushString(exec, NULL, NULL);
  7916. if (ret == 0) {
  7917. /*
  7918. * TODO: get some of the names needed to exit the current state of exec
  7919. */
  7920. VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
  7921. ret = -1;
  7922. } else if (ret < 0) {
  7923. ret = -1;
  7924. } else {
  7925. ret = 1;
  7926. }
  7927. xmlRegFreeExecCtxt(exec);
  7928. #ifdef DEBUG_PROGRESSIVE
  7929. if (ret < 0)
  7930. xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
  7931. elem->name);
  7932. #endif
  7933. return (ret);
  7934. }
  7935. /**
  7936. * xmlRelaxNGValidateFullElement:
  7937. * @ctxt: the validation context
  7938. * @doc: a document instance
  7939. * @elem: an element instance
  7940. *
  7941. * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
  7942. * 0 and the content of the node has been expanded.
  7943. *
  7944. * returns 1 if no validation problem was found or -1 in case of error.
  7945. */
  7946. int
  7947. xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
  7948. xmlDocPtr doc ATTRIBUTE_UNUSED,
  7949. xmlNodePtr elem)
  7950. {
  7951. int ret;
  7952. xmlRelaxNGValidStatePtr state;
  7953. if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
  7954. return (-1);
  7955. #ifdef DEBUG_PROGRESSIVE
  7956. xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
  7957. #endif
  7958. state = xmlRelaxNGNewValidState(ctxt, elem->parent);
  7959. if (state == NULL) {
  7960. return (-1);
  7961. }
  7962. state->seq = elem;
  7963. ctxt->state = state;
  7964. ctxt->errNo = XML_RELAXNG_OK;
  7965. ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
  7966. if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK))
  7967. ret = -1;
  7968. else
  7969. ret = 1;
  7970. xmlRelaxNGFreeValidState(ctxt, ctxt->state);
  7971. ctxt->state = NULL;
  7972. #ifdef DEBUG_PROGRESSIVE
  7973. if (ret < 0)
  7974. xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
  7975. elem->name);
  7976. #endif
  7977. return (ret);
  7978. }
  7979. /************************************************************************
  7980. * *
  7981. * Generic interpreted validation implementation *
  7982. * *
  7983. ************************************************************************/
  7984. static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
  7985. xmlRelaxNGDefinePtr define);
  7986. /**
  7987. * xmlRelaxNGSkipIgnored:
  7988. * @ctxt: a schema validation context
  7989. * @node: the top node.
  7990. *
  7991. * Skip ignorable nodes in that context
  7992. *
  7993. * Returns the new sibling or NULL in case of error.
  7994. */
  7995. static xmlNodePtr
  7996. xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
  7997. xmlNodePtr node)
  7998. {
  7999. /*
  8000. * TODO complete and handle entities
  8001. */
  8002. while ((node != NULL) &&
  8003. ((node->type == XML_COMMENT_NODE) ||
  8004. (node->type == XML_PI_NODE) ||
  8005. (node->type == XML_XINCLUDE_START) ||
  8006. (node->type == XML_XINCLUDE_END) ||
  8007. (((node->type == XML_TEXT_NODE) ||
  8008. (node->type == XML_CDATA_SECTION_NODE)) &&
  8009. ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
  8010. (IS_BLANK_NODE(node)))))) {
  8011. node = node->next;
  8012. }
  8013. return (node);
  8014. }
  8015. /**
  8016. * xmlRelaxNGNormalize:
  8017. * @ctxt: a schema validation context
  8018. * @str: the string to normalize
  8019. *
  8020. * Implements the normalizeWhiteSpace( s ) function from
  8021. * section 6.2.9 of the spec
  8022. *
  8023. * Returns the new string or NULL in case of error.
  8024. */
  8025. static xmlChar *
  8026. xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
  8027. {
  8028. xmlChar *ret, *p;
  8029. const xmlChar *tmp;
  8030. int len;
  8031. if (str == NULL)
  8032. return (NULL);
  8033. tmp = str;
  8034. while (*tmp != 0)
  8035. tmp++;
  8036. len = tmp - str;
  8037. ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
  8038. if (ret == NULL) {
  8039. xmlRngVErrMemory(ctxt, "validating\n");
  8040. return (NULL);
  8041. }
  8042. p = ret;
  8043. while (IS_BLANK_CH(*str))
  8044. str++;
  8045. while (*str != 0) {
  8046. if (IS_BLANK_CH(*str)) {
  8047. while (IS_BLANK_CH(*str))
  8048. str++;
  8049. if (*str == 0)
  8050. break;
  8051. *p++ = ' ';
  8052. } else
  8053. *p++ = *str++;
  8054. }
  8055. *p = 0;
  8056. return (ret);
  8057. }
  8058. /**
  8059. * xmlRelaxNGValidateDatatype:
  8060. * @ctxt: a Relax-NG validation context
  8061. * @value: the string value
  8062. * @type: the datatype definition
  8063. * @node: the node
  8064. *
  8065. * Validate the given value against the dataype
  8066. *
  8067. * Returns 0 if the validation succeeded or an error code.
  8068. */
  8069. static int
  8070. xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
  8071. const xmlChar * value,
  8072. xmlRelaxNGDefinePtr define, xmlNodePtr node)
  8073. {
  8074. int ret, tmp;
  8075. xmlRelaxNGTypeLibraryPtr lib;
  8076. void *result = NULL;
  8077. xmlRelaxNGDefinePtr cur;
  8078. if ((define == NULL) || (define->data == NULL)) {
  8079. return (-1);
  8080. }
  8081. lib = (xmlRelaxNGTypeLibraryPtr) define->data;
  8082. if (lib->check != NULL) {
  8083. if ((define->attrs != NULL) &&
  8084. (define->attrs->type == XML_RELAXNG_PARAM)) {
  8085. ret =
  8086. lib->check(lib->data, define->name, value, &result, node);
  8087. } else {
  8088. ret = lib->check(lib->data, define->name, value, NULL, node);
  8089. }
  8090. } else
  8091. ret = -1;
  8092. if (ret < 0) {
  8093. VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
  8094. if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
  8095. lib->freef(lib->data, result);
  8096. return (-1);
  8097. } else if (ret == 1) {
  8098. ret = 0;
  8099. } else if (ret == 2) {
  8100. VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
  8101. } else {
  8102. VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
  8103. ret = -1;
  8104. }
  8105. cur = define->attrs;
  8106. while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
  8107. if (lib->facet != NULL) {
  8108. tmp = lib->facet(lib->data, define->name, cur->name,
  8109. cur->value, value, result);
  8110. if (tmp != 0)
  8111. ret = -1;
  8112. }
  8113. cur = cur->next;
  8114. }
  8115. if ((ret == 0) && (define->content != NULL)) {
  8116. const xmlChar *oldvalue, *oldendvalue;
  8117. oldvalue = ctxt->state->value;
  8118. oldendvalue = ctxt->state->endvalue;
  8119. ctxt->state->value = (xmlChar *) value;
  8120. ctxt->state->endvalue = NULL;
  8121. ret = xmlRelaxNGValidateValue(ctxt, define->content);
  8122. ctxt->state->value = (xmlChar *) oldvalue;
  8123. ctxt->state->endvalue = (xmlChar *) oldendvalue;
  8124. }
  8125. if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
  8126. lib->freef(lib->data, result);
  8127. return (ret);
  8128. }
  8129. /**
  8130. * xmlRelaxNGNextValue:
  8131. * @ctxt: a Relax-NG validation context
  8132. *
  8133. * Skip to the next value when validating within a list
  8134. *
  8135. * Returns 0 if the operation succeeded or an error code.
  8136. */
  8137. static int
  8138. xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
  8139. {
  8140. xmlChar *cur;
  8141. cur = ctxt->state->value;
  8142. if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
  8143. ctxt->state->value = NULL;
  8144. ctxt->state->endvalue = NULL;
  8145. return (0);
  8146. }
  8147. while (*cur != 0)
  8148. cur++;
  8149. while ((cur != ctxt->state->endvalue) && (*cur == 0))
  8150. cur++;
  8151. if (cur == ctxt->state->endvalue)
  8152. ctxt->state->value = NULL;
  8153. else
  8154. ctxt->state->value = cur;
  8155. return (0);
  8156. }
  8157. /**
  8158. * xmlRelaxNGValidateValueList:
  8159. * @ctxt: a Relax-NG validation context
  8160. * @defines: the list of definitions to verify
  8161. *
  8162. * Validate the given set of definitions for the current value
  8163. *
  8164. * Returns 0 if the validation succeeded or an error code.
  8165. */
  8166. static int
  8167. xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
  8168. xmlRelaxNGDefinePtr defines)
  8169. {
  8170. int ret = 0;
  8171. while (defines != NULL) {
  8172. ret = xmlRelaxNGValidateValue(ctxt, defines);
  8173. if (ret != 0)
  8174. break;
  8175. defines = defines->next;
  8176. }
  8177. return (ret);
  8178. }
  8179. /**
  8180. * xmlRelaxNGValidateValue:
  8181. * @ctxt: a Relax-NG validation context
  8182. * @define: the definition to verify
  8183. *
  8184. * Validate the given definition for the current value
  8185. *
  8186. * Returns 0 if the validation succeeded or an error code.
  8187. */
  8188. static int
  8189. xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
  8190. xmlRelaxNGDefinePtr define)
  8191. {
  8192. int ret = 0, oldflags;
  8193. xmlChar *value;
  8194. value = ctxt->state->value;
  8195. switch (define->type) {
  8196. case XML_RELAXNG_EMPTY:{
  8197. if ((value != NULL) && (value[0] != 0)) {
  8198. int idx = 0;
  8199. while (IS_BLANK_CH(value[idx]))
  8200. idx++;
  8201. if (value[idx] != 0)
  8202. ret = -1;
  8203. }
  8204. break;
  8205. }
  8206. case XML_RELAXNG_TEXT:
  8207. break;
  8208. case XML_RELAXNG_VALUE:{
  8209. if (!xmlStrEqual(value, define->value)) {
  8210. if (define->name != NULL) {
  8211. xmlRelaxNGTypeLibraryPtr lib;
  8212. lib = (xmlRelaxNGTypeLibraryPtr) define->data;
  8213. if ((lib != NULL) && (lib->comp != NULL)) {
  8214. ret = lib->comp(lib->data, define->name,
  8215. define->value, define->node,
  8216. (void *) define->attrs,
  8217. value, ctxt->state->node);
  8218. } else
  8219. ret = -1;
  8220. if (ret < 0) {
  8221. VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
  8222. define->name);
  8223. return (-1);
  8224. } else if (ret == 1) {
  8225. ret = 0;
  8226. } else {
  8227. ret = -1;
  8228. }
  8229. } else {
  8230. xmlChar *nval, *nvalue;
  8231. /*
  8232. * TODO: trivial optimizations are possible by
  8233. * computing at compile-time
  8234. */
  8235. nval = xmlRelaxNGNormalize(ctxt, define->value);
  8236. nvalue = xmlRelaxNGNormalize(ctxt, value);
  8237. if ((nval == NULL) || (nvalue == NULL) ||
  8238. (!xmlStrEqual(nval, nvalue)))
  8239. ret = -1;
  8240. if (nval != NULL)
  8241. xmlFree(nval);
  8242. if (nvalue != NULL)
  8243. xmlFree(nvalue);
  8244. }
  8245. }
  8246. if (ret == 0)
  8247. xmlRelaxNGNextValue(ctxt);
  8248. break;
  8249. }
  8250. case XML_RELAXNG_DATATYPE:{
  8251. ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
  8252. ctxt->state->seq);
  8253. if (ret == 0)
  8254. xmlRelaxNGNextValue(ctxt);
  8255. break;
  8256. }
  8257. case XML_RELAXNG_CHOICE:{
  8258. xmlRelaxNGDefinePtr list = define->content;
  8259. xmlChar *oldvalue;
  8260. oldflags = ctxt->flags;
  8261. ctxt->flags |= FLAGS_IGNORABLE;
  8262. oldvalue = ctxt->state->value;
  8263. while (list != NULL) {
  8264. ret = xmlRelaxNGValidateValue(ctxt, list);
  8265. if (ret == 0) {
  8266. break;
  8267. }
  8268. ctxt->state->value = oldvalue;
  8269. list = list->next;
  8270. }
  8271. ctxt->flags = oldflags;
  8272. if (ret != 0) {
  8273. if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
  8274. xmlRelaxNGDumpValidError(ctxt);
  8275. } else {
  8276. if (ctxt->errNr > 0)
  8277. xmlRelaxNGPopErrors(ctxt, 0);
  8278. }
  8279. break;
  8280. }
  8281. case XML_RELAXNG_LIST:{
  8282. xmlRelaxNGDefinePtr list = define->content;
  8283. xmlChar *oldvalue, *oldend, *val, *cur;
  8284. #ifdef DEBUG_LIST
  8285. int nb_values = 0;
  8286. #endif
  8287. oldvalue = ctxt->state->value;
  8288. oldend = ctxt->state->endvalue;
  8289. val = xmlStrdup(oldvalue);
  8290. if (val == NULL) {
  8291. val = xmlStrdup(BAD_CAST "");
  8292. }
  8293. if (val == NULL) {
  8294. VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
  8295. return (-1);
  8296. }
  8297. cur = val;
  8298. while (*cur != 0) {
  8299. if (IS_BLANK_CH(*cur)) {
  8300. *cur = 0;
  8301. cur++;
  8302. #ifdef DEBUG_LIST
  8303. nb_values++;
  8304. #endif
  8305. while (IS_BLANK_CH(*cur))
  8306. *cur++ = 0;
  8307. } else
  8308. cur++;
  8309. }
  8310. #ifdef DEBUG_LIST
  8311. xmlGenericError(xmlGenericErrorContext,
  8312. "list value: '%s' found %d items\n",
  8313. oldvalue, nb_values);
  8314. nb_values = 0;
  8315. #endif
  8316. ctxt->state->endvalue = cur;
  8317. cur = val;
  8318. while ((*cur == 0) && (cur != ctxt->state->endvalue))
  8319. cur++;
  8320. ctxt->state->value = cur;
  8321. while (list != NULL) {
  8322. if (ctxt->state->value == ctxt->state->endvalue)
  8323. ctxt->state->value = NULL;
  8324. ret = xmlRelaxNGValidateValue(ctxt, list);
  8325. if (ret != 0) {
  8326. #ifdef DEBUG_LIST
  8327. xmlGenericError(xmlGenericErrorContext,
  8328. "Failed to validate value: '%s' with %d rule\n",
  8329. ctxt->state->value, nb_values);
  8330. #endif
  8331. break;
  8332. }
  8333. #ifdef DEBUG_LIST
  8334. nb_values++;
  8335. #endif
  8336. list = list->next;
  8337. }
  8338. if ((ret == 0) && (ctxt->state->value != NULL) &&
  8339. (ctxt->state->value != ctxt->state->endvalue)) {
  8340. VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA,
  8341. ctxt->state->value);
  8342. ret = -1;
  8343. }
  8344. xmlFree(val);
  8345. ctxt->state->value = oldvalue;
  8346. ctxt->state->endvalue = oldend;
  8347. break;
  8348. }
  8349. case XML_RELAXNG_ONEORMORE:
  8350. ret = xmlRelaxNGValidateValueList(ctxt, define->content);
  8351. if (ret != 0) {
  8352. break;
  8353. }
  8354. /* no break on purpose */
  8355. case XML_RELAXNG_ZEROORMORE:{
  8356. xmlChar *cur, *temp;
  8357. oldflags = ctxt->flags;
  8358. ctxt->flags |= FLAGS_IGNORABLE;
  8359. cur = ctxt->state->value;
  8360. temp = NULL;
  8361. while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
  8362. (temp != cur)) {
  8363. temp = cur;
  8364. ret =
  8365. xmlRelaxNGValidateValueList(ctxt, define->content);
  8366. if (ret != 0) {
  8367. ctxt->state->value = temp;
  8368. ret = 0;
  8369. break;
  8370. }
  8371. cur = ctxt->state->value;
  8372. }
  8373. ctxt->flags = oldflags;
  8374. if (ctxt->errNr > 0)
  8375. xmlRelaxNGPopErrors(ctxt, 0);
  8376. break;
  8377. }
  8378. case XML_RELAXNG_EXCEPT:{
  8379. xmlRelaxNGDefinePtr list;
  8380. list = define->content;
  8381. while (list != NULL) {
  8382. ret = xmlRelaxNGValidateValue(ctxt, list);
  8383. if (ret == 0) {
  8384. ret = -1;
  8385. break;
  8386. } else
  8387. ret = 0;
  8388. list = list->next;
  8389. }
  8390. break;
  8391. }
  8392. case XML_RELAXNG_DEF:
  8393. case XML_RELAXNG_GROUP:{
  8394. xmlRelaxNGDefinePtr list;
  8395. list = define->content;
  8396. while (list != NULL) {
  8397. ret = xmlRelaxNGValidateValue(ctxt, list);
  8398. if (ret != 0) {
  8399. ret = -1;
  8400. break;
  8401. } else
  8402. ret = 0;
  8403. list = list->next;
  8404. }
  8405. break;
  8406. }
  8407. case XML_RELAXNG_REF:
  8408. case XML_RELAXNG_PARENTREF:
  8409. if (define->content == NULL) {
  8410. VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
  8411. ret = -1;
  8412. } else {
  8413. ret = xmlRelaxNGValidateValue(ctxt, define->content);
  8414. }
  8415. break;
  8416. default:
  8417. TODO ret = -1;
  8418. }
  8419. return (ret);
  8420. }
  8421. /**
  8422. * xmlRelaxNGValidateValueContent:
  8423. * @ctxt: a Relax-NG validation context
  8424. * @defines: the list of definitions to verify
  8425. *
  8426. * Validate the given definitions for the current value
  8427. *
  8428. * Returns 0 if the validation succeeded or an error code.
  8429. */
  8430. static int
  8431. xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
  8432. xmlRelaxNGDefinePtr defines)
  8433. {
  8434. int ret = 0;
  8435. while (defines != NULL) {
  8436. ret = xmlRelaxNGValidateValue(ctxt, defines);
  8437. if (ret != 0)
  8438. break;
  8439. defines = defines->next;
  8440. }
  8441. return (ret);
  8442. }
  8443. /**
  8444. * xmlRelaxNGAttributeMatch:
  8445. * @ctxt: a Relax-NG validation context
  8446. * @define: the definition to check
  8447. * @prop: the attribute
  8448. *
  8449. * Check if the attribute matches the definition nameClass
  8450. *
  8451. * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
  8452. */
  8453. static int
  8454. xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
  8455. xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
  8456. {
  8457. int ret;
  8458. if (define->name != NULL) {
  8459. if (!xmlStrEqual(define->name, prop->name))
  8460. return (0);
  8461. }
  8462. if (define->ns != NULL) {
  8463. if (define->ns[0] == 0) {
  8464. if (prop->ns != NULL)
  8465. return (0);
  8466. } else {
  8467. if ((prop->ns == NULL) ||
  8468. (!xmlStrEqual(define->ns, prop->ns->href)))
  8469. return (0);
  8470. }
  8471. }
  8472. if (define->nameClass == NULL)
  8473. return (1);
  8474. define = define->nameClass;
  8475. if (define->type == XML_RELAXNG_EXCEPT) {
  8476. xmlRelaxNGDefinePtr list;
  8477. list = define->content;
  8478. while (list != NULL) {
  8479. ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
  8480. if (ret == 1)
  8481. return (0);
  8482. if (ret < 0)
  8483. return (ret);
  8484. list = list->next;
  8485. }
  8486. } else {
  8487. TODO}
  8488. return (1);
  8489. }
  8490. /**
  8491. * xmlRelaxNGValidateAttribute:
  8492. * @ctxt: a Relax-NG validation context
  8493. * @define: the definition to verify
  8494. *
  8495. * Validate the given attribute definition for that node
  8496. *
  8497. * Returns 0 if the validation succeeded or an error code.
  8498. */
  8499. static int
  8500. xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
  8501. xmlRelaxNGDefinePtr define)
  8502. {
  8503. int ret = 0, i;
  8504. xmlChar *value, *oldvalue;
  8505. xmlAttrPtr prop = NULL, tmp;
  8506. xmlNodePtr oldseq;
  8507. if (ctxt->state->nbAttrLeft <= 0)
  8508. return (-1);
  8509. if (define->name != NULL) {
  8510. for (i = 0; i < ctxt->state->nbAttrs; i++) {
  8511. tmp = ctxt->state->attrs[i];
  8512. if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
  8513. if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
  8514. (tmp->ns == NULL)) ||
  8515. ((tmp->ns != NULL) &&
  8516. (xmlStrEqual(define->ns, tmp->ns->href)))) {
  8517. prop = tmp;
  8518. break;
  8519. }
  8520. }
  8521. }
  8522. if (prop != NULL) {
  8523. value = xmlNodeListGetString(prop->doc, prop->children, 1);
  8524. oldvalue = ctxt->state->value;
  8525. oldseq = ctxt->state->seq;
  8526. ctxt->state->seq = (xmlNodePtr) prop;
  8527. ctxt->state->value = value;
  8528. ctxt->state->endvalue = NULL;
  8529. ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
  8530. if (ctxt->state->value != NULL)
  8531. value = ctxt->state->value;
  8532. if (value != NULL)
  8533. xmlFree(value);
  8534. ctxt->state->value = oldvalue;
  8535. ctxt->state->seq = oldseq;
  8536. if (ret == 0) {
  8537. /*
  8538. * flag the attribute as processed
  8539. */
  8540. ctxt->state->attrs[i] = NULL;
  8541. ctxt->state->nbAttrLeft--;
  8542. }
  8543. } else {
  8544. ret = -1;
  8545. }
  8546. #ifdef DEBUG
  8547. xmlGenericError(xmlGenericErrorContext,
  8548. "xmlRelaxNGValidateAttribute(%s): %d\n",
  8549. define->name, ret);
  8550. #endif
  8551. } else {
  8552. for (i = 0; i < ctxt->state->nbAttrs; i++) {
  8553. tmp = ctxt->state->attrs[i];
  8554. if ((tmp != NULL) &&
  8555. (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
  8556. prop = tmp;
  8557. break;
  8558. }
  8559. }
  8560. if (prop != NULL) {
  8561. value = xmlNodeListGetString(prop->doc, prop->children, 1);
  8562. oldvalue = ctxt->state->value;
  8563. oldseq = ctxt->state->seq;
  8564. ctxt->state->seq = (xmlNodePtr) prop;
  8565. ctxt->state->value = value;
  8566. ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
  8567. if (ctxt->state->value != NULL)
  8568. value = ctxt->state->value;
  8569. if (value != NULL)
  8570. xmlFree(value);
  8571. ctxt->state->value = oldvalue;
  8572. ctxt->state->seq = oldseq;
  8573. if (ret == 0) {
  8574. /*
  8575. * flag the attribute as processed
  8576. */
  8577. ctxt->state->attrs[i] = NULL;
  8578. ctxt->state->nbAttrLeft--;
  8579. }
  8580. } else {
  8581. ret = -1;
  8582. }
  8583. #ifdef DEBUG
  8584. if (define->ns != NULL) {
  8585. xmlGenericError(xmlGenericErrorContext,
  8586. "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
  8587. define->ns, ret);
  8588. } else {
  8589. xmlGenericError(xmlGenericErrorContext,
  8590. "xmlRelaxNGValidateAttribute(anyName): %d\n",
  8591. ret);
  8592. }
  8593. #endif
  8594. }
  8595. return (ret);
  8596. }
  8597. /**
  8598. * xmlRelaxNGValidateAttributeList:
  8599. * @ctxt: a Relax-NG validation context
  8600. * @define: the list of definition to verify
  8601. *
  8602. * Validate the given node against the list of attribute definitions
  8603. *
  8604. * Returns 0 if the validation succeeded or an error code.
  8605. */
  8606. static int
  8607. xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
  8608. xmlRelaxNGDefinePtr defines)
  8609. {
  8610. int ret = 0, res;
  8611. int needmore = 0;
  8612. xmlRelaxNGDefinePtr cur;
  8613. cur = defines;
  8614. while (cur != NULL) {
  8615. if (cur->type == XML_RELAXNG_ATTRIBUTE) {
  8616. if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
  8617. ret = -1;
  8618. } else
  8619. needmore = 1;
  8620. cur = cur->next;
  8621. }
  8622. if (!needmore)
  8623. return (ret);
  8624. cur = defines;
  8625. while (cur != NULL) {
  8626. if (cur->type != XML_RELAXNG_ATTRIBUTE) {
  8627. if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
  8628. res = xmlRelaxNGValidateDefinition(ctxt, cur);
  8629. if (res < 0)
  8630. ret = -1;
  8631. } else {
  8632. VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
  8633. return (-1);
  8634. }
  8635. if (res == -1) /* continues on -2 */
  8636. break;
  8637. }
  8638. cur = cur->next;
  8639. }
  8640. return (ret);
  8641. }
  8642. /**
  8643. * xmlRelaxNGNodeMatchesList:
  8644. * @node: the node
  8645. * @list: a NULL terminated array of definitions
  8646. *
  8647. * Check if a node can be matched by one of the definitions
  8648. *
  8649. * Returns 1 if matches 0 otherwise
  8650. */
  8651. static int
  8652. xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
  8653. {
  8654. xmlRelaxNGDefinePtr cur;
  8655. int i = 0, tmp;
  8656. if ((node == NULL) || (list == NULL))
  8657. return (0);
  8658. cur = list[i++];
  8659. while (cur != NULL) {
  8660. if ((node->type == XML_ELEMENT_NODE) &&
  8661. (cur->type == XML_RELAXNG_ELEMENT)) {
  8662. tmp = xmlRelaxNGElementMatch(NULL, cur, node);
  8663. if (tmp == 1)
  8664. return (1);
  8665. } else if (((node->type == XML_TEXT_NODE) ||
  8666. (node->type == XML_CDATA_SECTION_NODE)) &&
  8667. (cur->type == XML_RELAXNG_TEXT)) {
  8668. return (1);
  8669. }
  8670. cur = list[i++];
  8671. }
  8672. return (0);
  8673. }
  8674. /**
  8675. * xmlRelaxNGValidateInterleave:
  8676. * @ctxt: a Relax-NG validation context
  8677. * @define: the definition to verify
  8678. *
  8679. * Validate an interleave definition for a node.
  8680. *
  8681. * Returns 0 if the validation succeeded or an error code.
  8682. */
  8683. static int
  8684. xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
  8685. xmlRelaxNGDefinePtr define)
  8686. {
  8687. int ret = 0, i, nbgroups;
  8688. int errNr = ctxt->errNr;
  8689. int oldflags;
  8690. xmlRelaxNGValidStatePtr oldstate;
  8691. xmlRelaxNGPartitionPtr partitions;
  8692. xmlRelaxNGInterleaveGroupPtr group = NULL;
  8693. xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
  8694. xmlNodePtr *list = NULL, *lasts = NULL;
  8695. if (define->data != NULL) {
  8696. partitions = (xmlRelaxNGPartitionPtr) define->data;
  8697. nbgroups = partitions->nbgroups;
  8698. } else {
  8699. VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
  8700. return (-1);
  8701. }
  8702. /*
  8703. * Optimizations for MIXED
  8704. */
  8705. oldflags = ctxt->flags;
  8706. if (define->dflags & IS_MIXED) {
  8707. ctxt->flags |= FLAGS_MIXED_CONTENT;
  8708. if (nbgroups == 2) {
  8709. /*
  8710. * this is a pure <mixed> case
  8711. */
  8712. if (ctxt->state != NULL)
  8713. ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
  8714. ctxt->state->seq);
  8715. if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
  8716. ret = xmlRelaxNGValidateDefinition(ctxt,
  8717. partitions->groups[1]->
  8718. rule);
  8719. else
  8720. ret = xmlRelaxNGValidateDefinition(ctxt,
  8721. partitions->groups[0]->
  8722. rule);
  8723. if (ret == 0) {
  8724. if (ctxt->state != NULL)
  8725. ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
  8726. ctxt->state->
  8727. seq);
  8728. }
  8729. ctxt->flags = oldflags;
  8730. return (ret);
  8731. }
  8732. }
  8733. /*
  8734. * Build arrays to store the first and last node of the chain
  8735. * pertaining to each group
  8736. */
  8737. list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
  8738. if (list == NULL) {
  8739. xmlRngVErrMemory(ctxt, "validating\n");
  8740. return (-1);
  8741. }
  8742. memset(list, 0, nbgroups * sizeof(xmlNodePtr));
  8743. lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
  8744. if (lasts == NULL) {
  8745. xmlRngVErrMemory(ctxt, "validating\n");
  8746. return (-1);
  8747. }
  8748. memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
  8749. /*
  8750. * Walk the sequence of children finding the right group and
  8751. * sorting them in sequences.
  8752. */
  8753. cur = ctxt->state->seq;
  8754. cur = xmlRelaxNGSkipIgnored(ctxt, cur);
  8755. start = cur;
  8756. while (cur != NULL) {
  8757. ctxt->state->seq = cur;
  8758. if ((partitions->triage != NULL) &&
  8759. (partitions->flags & IS_DETERMINIST)) {
  8760. void *tmp = NULL;
  8761. if ((cur->type == XML_TEXT_NODE) ||
  8762. (cur->type == XML_CDATA_SECTION_NODE)) {
  8763. tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
  8764. NULL);
  8765. } else if (cur->type == XML_ELEMENT_NODE) {
  8766. if (cur->ns != NULL) {
  8767. tmp = xmlHashLookup2(partitions->triage, cur->name,
  8768. cur->ns->href);
  8769. if (tmp == NULL)
  8770. tmp = xmlHashLookup2(partitions->triage,
  8771. BAD_CAST "#any",
  8772. cur->ns->href);
  8773. } else
  8774. tmp =
  8775. xmlHashLookup2(partitions->triage, cur->name,
  8776. NULL);
  8777. if (tmp == NULL)
  8778. tmp =
  8779. xmlHashLookup2(partitions->triage, BAD_CAST "#any",
  8780. NULL);
  8781. }
  8782. if (tmp == NULL) {
  8783. i = nbgroups;
  8784. } else {
  8785. i = ((long) tmp) - 1;
  8786. if (partitions->flags & IS_NEEDCHECK) {
  8787. group = partitions->groups[i];
  8788. if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
  8789. i = nbgroups;
  8790. }
  8791. }
  8792. } else {
  8793. for (i = 0; i < nbgroups; i++) {
  8794. group = partitions->groups[i];
  8795. if (group == NULL)
  8796. continue;
  8797. if (xmlRelaxNGNodeMatchesList(cur, group->defs))
  8798. break;
  8799. }
  8800. }
  8801. /*
  8802. * We break as soon as an element not matched is found
  8803. */
  8804. if (i >= nbgroups) {
  8805. break;
  8806. }
  8807. if (lasts[i] != NULL) {
  8808. lasts[i]->next = cur;
  8809. lasts[i] = cur;
  8810. } else {
  8811. list[i] = cur;
  8812. lasts[i] = cur;
  8813. }
  8814. if (cur->next != NULL)
  8815. lastchg = cur->next;
  8816. else
  8817. lastchg = cur;
  8818. cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
  8819. }
  8820. if (ret != 0) {
  8821. VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
  8822. ret = -1;
  8823. goto done;
  8824. }
  8825. lastelem = cur;
  8826. oldstate = ctxt->state;
  8827. for (i = 0; i < nbgroups; i++) {
  8828. ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
  8829. group = partitions->groups[i];
  8830. if (lasts[i] != NULL) {
  8831. last = lasts[i]->next;
  8832. lasts[i]->next = NULL;
  8833. }
  8834. ctxt->state->seq = list[i];
  8835. ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
  8836. if (ret != 0)
  8837. break;
  8838. if (ctxt->state != NULL) {
  8839. cur = ctxt->state->seq;
  8840. cur = xmlRelaxNGSkipIgnored(ctxt, cur);
  8841. xmlRelaxNGFreeValidState(ctxt, oldstate);
  8842. oldstate = ctxt->state;
  8843. ctxt->state = NULL;
  8844. if (cur != NULL) {
  8845. VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
  8846. ret = -1;
  8847. ctxt->state = oldstate;
  8848. goto done;
  8849. }
  8850. } else if (ctxt->states != NULL) {
  8851. int j;
  8852. int found = 0;
  8853. int best = -1;
  8854. int lowattr = -1;
  8855. /*
  8856. * PBM: what happen if there is attributes checks in the interleaves
  8857. */
  8858. for (j = 0; j < ctxt->states->nbState; j++) {
  8859. cur = ctxt->states->tabState[j]->seq;
  8860. cur = xmlRelaxNGSkipIgnored(ctxt, cur);
  8861. if (cur == NULL) {
  8862. if (found == 0) {
  8863. lowattr = ctxt->states->tabState[j]->nbAttrLeft;
  8864. best = j;
  8865. }
  8866. found = 1;
  8867. if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
  8868. /* try to keep the latest one to mach old heuristic */
  8869. lowattr = ctxt->states->tabState[j]->nbAttrLeft;
  8870. best = j;
  8871. }
  8872. if (lowattr == 0)
  8873. break;
  8874. } else if (found == 0) {
  8875. if (lowattr == -1) {
  8876. lowattr = ctxt->states->tabState[j]->nbAttrLeft;
  8877. best = j;
  8878. } else
  8879. if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
  8880. /* try to keep the latest one to mach old heuristic */
  8881. lowattr = ctxt->states->tabState[j]->nbAttrLeft;
  8882. best = j;
  8883. }
  8884. }
  8885. }
  8886. /*
  8887. * BIG PBM: here we pick only one restarting point :-(
  8888. */
  8889. if (ctxt->states->nbState > 0) {
  8890. xmlRelaxNGFreeValidState(ctxt, oldstate);
  8891. if (best != -1) {
  8892. oldstate = ctxt->states->tabState[best];
  8893. ctxt->states->tabState[best] = NULL;
  8894. } else {
  8895. oldstate =
  8896. ctxt->states->tabState[ctxt->states->nbState - 1];
  8897. ctxt->states->tabState[ctxt->states->nbState - 1] = NULL;
  8898. ctxt->states->nbState--;
  8899. }
  8900. }
  8901. for (j = 0; j < ctxt->states->nbState ; j++) {
  8902. xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
  8903. }
  8904. xmlRelaxNGFreeStates(ctxt, ctxt->states);
  8905. ctxt->states = NULL;
  8906. if (found == 0) {
  8907. if (cur == NULL) {
  8908. VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, "noname");
  8909. } else {
  8910. VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
  8911. }
  8912. ret = -1;
  8913. ctxt->state = oldstate;
  8914. goto done;
  8915. }
  8916. } else {
  8917. ret = -1;
  8918. break;
  8919. }
  8920. if (lasts[i] != NULL) {
  8921. lasts[i]->next = last;
  8922. }
  8923. }
  8924. if (ctxt->state != NULL)
  8925. xmlRelaxNGFreeValidState(ctxt, ctxt->state);
  8926. ctxt->state = oldstate;
  8927. ctxt->state->seq = lastelem;
  8928. if (ret != 0) {
  8929. VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
  8930. ret = -1;
  8931. goto done;
  8932. }
  8933. done:
  8934. ctxt->flags = oldflags;
  8935. /*
  8936. * builds the next links chain from the prev one
  8937. */
  8938. cur = lastchg;
  8939. while (cur != NULL) {
  8940. if ((cur == start) || (cur->prev == NULL))
  8941. break;
  8942. cur->prev->next = cur;
  8943. cur = cur->prev;
  8944. }
  8945. if (ret == 0) {
  8946. if (ctxt->errNr > errNr)
  8947. xmlRelaxNGPopErrors(ctxt, errNr);
  8948. }
  8949. xmlFree(list);
  8950. xmlFree(lasts);
  8951. return (ret);
  8952. }
  8953. /**
  8954. * xmlRelaxNGValidateDefinitionList:
  8955. * @ctxt: a Relax-NG validation context
  8956. * @define: the list of definition to verify
  8957. *
  8958. * Validate the given node content against the (list) of definitions
  8959. *
  8960. * Returns 0 if the validation succeeded or an error code.
  8961. */
  8962. static int
  8963. xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
  8964. xmlRelaxNGDefinePtr defines)
  8965. {
  8966. int ret = 0, res;
  8967. if (defines == NULL) {
  8968. VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
  8969. BAD_CAST "NULL definition list");
  8970. return (-1);
  8971. }
  8972. while (defines != NULL) {
  8973. if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
  8974. res = xmlRelaxNGValidateDefinition(ctxt, defines);
  8975. if (res < 0)
  8976. ret = -1;
  8977. } else {
  8978. VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
  8979. return (-1);
  8980. }
  8981. if (res == -1) /* continues on -2 */
  8982. break;
  8983. defines = defines->next;
  8984. }
  8985. return (ret);
  8986. }
  8987. /**
  8988. * xmlRelaxNGElementMatch:
  8989. * @ctxt: a Relax-NG validation context
  8990. * @define: the definition to check
  8991. * @elem: the element
  8992. *
  8993. * Check if the element matches the definition nameClass
  8994. *
  8995. * Returns 1 if the element matches, 0 if no, or -1 in case of error
  8996. */
  8997. static int
  8998. xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
  8999. xmlRelaxNGDefinePtr define, xmlNodePtr elem)
  9000. {
  9001. int ret = 0, oldflags = 0;
  9002. if (define->name != NULL) {
  9003. if (!xmlStrEqual(elem->name, define->name)) {
  9004. VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
  9005. return (0);
  9006. }
  9007. }
  9008. if ((define->ns != NULL) && (define->ns[0] != 0)) {
  9009. if (elem->ns == NULL) {
  9010. VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
  9011. return (0);
  9012. } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
  9013. VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
  9014. elem->name, define->ns);
  9015. return (0);
  9016. }
  9017. } else if ((elem->ns != NULL) && (define->ns != NULL) &&
  9018. (define->name == NULL)) {
  9019. VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
  9020. return (0);
  9021. } else if ((elem->ns != NULL) && (define->name != NULL)) {
  9022. VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
  9023. return (0);
  9024. }
  9025. if (define->nameClass == NULL)
  9026. return (1);
  9027. define = define->nameClass;
  9028. if (define->type == XML_RELAXNG_EXCEPT) {
  9029. xmlRelaxNGDefinePtr list;
  9030. if (ctxt != NULL) {
  9031. oldflags = ctxt->flags;
  9032. ctxt->flags |= FLAGS_IGNORABLE;
  9033. }
  9034. list = define->content;
  9035. while (list != NULL) {
  9036. ret = xmlRelaxNGElementMatch(ctxt, list, elem);
  9037. if (ret == 1) {
  9038. if (ctxt != NULL)
  9039. ctxt->flags = oldflags;
  9040. return (0);
  9041. }
  9042. if (ret < 0) {
  9043. if (ctxt != NULL)
  9044. ctxt->flags = oldflags;
  9045. return (ret);
  9046. }
  9047. list = list->next;
  9048. }
  9049. ret = 1;
  9050. if (ctxt != NULL) {
  9051. ctxt->flags = oldflags;
  9052. }
  9053. } else if (define->type == XML_RELAXNG_CHOICE) {
  9054. xmlRelaxNGDefinePtr list;
  9055. if (ctxt != NULL) {
  9056. oldflags = ctxt->flags;
  9057. ctxt->flags |= FLAGS_IGNORABLE;
  9058. }
  9059. list = define->nameClass;
  9060. while (list != NULL) {
  9061. ret = xmlRelaxNGElementMatch(ctxt, list, elem);
  9062. if (ret == 1) {
  9063. if (ctxt != NULL)
  9064. ctxt->flags = oldflags;
  9065. return (1);
  9066. }
  9067. if (ret < 0) {
  9068. if (ctxt != NULL)
  9069. ctxt->flags = oldflags;
  9070. return (ret);
  9071. }
  9072. list = list->next;
  9073. }
  9074. if (ctxt != NULL) {
  9075. if (ret != 0) {
  9076. if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
  9077. xmlRelaxNGDumpValidError(ctxt);
  9078. } else {
  9079. if (ctxt->errNr > 0)
  9080. xmlRelaxNGPopErrors(ctxt, 0);
  9081. }
  9082. }
  9083. ret = 0;
  9084. if (ctxt != NULL) {
  9085. ctxt->flags = oldflags;
  9086. }
  9087. } else {
  9088. TODO ret = -1;
  9089. }
  9090. return (ret);
  9091. }
  9092. /**
  9093. * xmlRelaxNGBestState:
  9094. * @ctxt: a Relax-NG validation context
  9095. *
  9096. * Find the "best" state in the ctxt->states list of states to report
  9097. * errors about. I.e. a state with no element left in the child list
  9098. * or the one with the less attributes left.
  9099. * This is called only if a falidation error was detected
  9100. *
  9101. * Returns the index of the "best" state or -1 in case of error
  9102. */
  9103. static int
  9104. xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
  9105. {
  9106. xmlRelaxNGValidStatePtr state;
  9107. int i, tmp;
  9108. int best = -1;
  9109. int value = 1000000;
  9110. if ((ctxt == NULL) || (ctxt->states == NULL) ||
  9111. (ctxt->states->nbState <= 0))
  9112. return (-1);
  9113. for (i = 0; i < ctxt->states->nbState; i++) {
  9114. state = ctxt->states->tabState[i];
  9115. if (state == NULL)
  9116. continue;
  9117. if (state->seq != NULL) {
  9118. if ((best == -1) || (value > 100000)) {
  9119. value = 100000;
  9120. best = i;
  9121. }
  9122. } else {
  9123. tmp = state->nbAttrLeft;
  9124. if ((best == -1) || (value > tmp)) {
  9125. value = tmp;
  9126. best = i;
  9127. }
  9128. }
  9129. }
  9130. return (best);
  9131. }
  9132. /**
  9133. * xmlRelaxNGLogBestError:
  9134. * @ctxt: a Relax-NG validation context
  9135. *
  9136. * Find the "best" state in the ctxt->states list of states to report
  9137. * errors about and log it.
  9138. */
  9139. static void
  9140. xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
  9141. {
  9142. int best;
  9143. if ((ctxt == NULL) || (ctxt->states == NULL) ||
  9144. (ctxt->states->nbState <= 0))
  9145. return;
  9146. best = xmlRelaxNGBestState(ctxt);
  9147. if ((best >= 0) && (best < ctxt->states->nbState)) {
  9148. ctxt->state = ctxt->states->tabState[best];
  9149. xmlRelaxNGValidateElementEnd(ctxt, 1);
  9150. }
  9151. }
  9152. /**
  9153. * xmlRelaxNGValidateElementEnd:
  9154. * @ctxt: a Relax-NG validation context
  9155. * @dolog: indicate that error logging should be done
  9156. *
  9157. * Validate the end of the element, implements check that
  9158. * there is nothing left not consumed in the element content
  9159. * or in the attribute list.
  9160. *
  9161. * Returns 0 if the validation succeeded or an error code.
  9162. */
  9163. static int
  9164. xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog)
  9165. {
  9166. int i;
  9167. xmlRelaxNGValidStatePtr state;
  9168. state = ctxt->state;
  9169. if (state->seq != NULL) {
  9170. state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
  9171. if (state->seq != NULL) {
  9172. if (dolog) {
  9173. VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
  9174. state->node->name, state->seq->name);
  9175. }
  9176. return (-1);
  9177. }
  9178. }
  9179. for (i = 0; i < state->nbAttrs; i++) {
  9180. if (state->attrs[i] != NULL) {
  9181. if (dolog) {
  9182. VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
  9183. state->attrs[i]->name, state->node->name);
  9184. }
  9185. return (-1 - i);
  9186. }
  9187. }
  9188. return (0);
  9189. }
  9190. /**
  9191. * xmlRelaxNGValidateState:
  9192. * @ctxt: a Relax-NG validation context
  9193. * @define: the definition to verify
  9194. *
  9195. * Validate the current state against the definition
  9196. *
  9197. * Returns 0 if the validation succeeded or an error code.
  9198. */
  9199. static int
  9200. xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
  9201. xmlRelaxNGDefinePtr define)
  9202. {
  9203. xmlNodePtr node;
  9204. int ret = 0, i, tmp, oldflags, errNr;
  9205. xmlRelaxNGValidStatePtr oldstate = NULL, state;
  9206. if (define == NULL) {
  9207. VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
  9208. return (-1);
  9209. }
  9210. if (ctxt->state != NULL) {
  9211. node = ctxt->state->seq;
  9212. } else {
  9213. node = NULL;
  9214. }
  9215. #ifdef DEBUG
  9216. for (i = 0; i < ctxt->depth; i++)
  9217. xmlGenericError(xmlGenericErrorContext, " ");
  9218. xmlGenericError(xmlGenericErrorContext,
  9219. "Start validating %s ", xmlRelaxNGDefName(define));
  9220. if (define->name != NULL)
  9221. xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
  9222. if ((node != NULL) && (node->name != NULL))
  9223. xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
  9224. else
  9225. xmlGenericError(xmlGenericErrorContext, "\n");
  9226. #endif
  9227. ctxt->depth++;
  9228. switch (define->type) {
  9229. case XML_RELAXNG_EMPTY:
  9230. node = xmlRelaxNGSkipIgnored(ctxt, node);
  9231. ret = 0;
  9232. break;
  9233. case XML_RELAXNG_NOT_ALLOWED:
  9234. ret = -1;
  9235. break;
  9236. case XML_RELAXNG_TEXT:
  9237. while ((node != NULL) &&
  9238. ((node->type == XML_TEXT_NODE) ||
  9239. (node->type == XML_COMMENT_NODE) ||
  9240. (node->type == XML_PI_NODE) ||
  9241. (node->type == XML_CDATA_SECTION_NODE)))
  9242. node = node->next;
  9243. ctxt->state->seq = node;
  9244. break;
  9245. case XML_RELAXNG_ELEMENT:
  9246. errNr = ctxt->errNr;
  9247. node = xmlRelaxNGSkipIgnored(ctxt, node);
  9248. if (node == NULL) {
  9249. VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
  9250. ret = -1;
  9251. if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
  9252. xmlRelaxNGDumpValidError(ctxt);
  9253. break;
  9254. }
  9255. if (node->type != XML_ELEMENT_NODE) {
  9256. VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
  9257. ret = -1;
  9258. if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
  9259. xmlRelaxNGDumpValidError(ctxt);
  9260. break;
  9261. }
  9262. /*
  9263. * This node was already validated successfully against
  9264. * this definition.
  9265. */
  9266. if (node->psvi == define) {
  9267. ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
  9268. if (ctxt->errNr > errNr)
  9269. xmlRelaxNGPopErrors(ctxt, errNr);
  9270. if (ctxt->errNr != 0) {
  9271. while ((ctxt->err != NULL) &&
  9272. (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
  9273. && (xmlStrEqual(ctxt->err->arg2, node->name)))
  9274. ||
  9275. ((ctxt->err->err ==
  9276. XML_RELAXNG_ERR_ELEMEXTRANS)
  9277. && (xmlStrEqual(ctxt->err->arg1, node->name)))
  9278. || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
  9279. || (ctxt->err->err ==
  9280. XML_RELAXNG_ERR_NOTELEM)))
  9281. xmlRelaxNGValidErrorPop(ctxt);
  9282. }
  9283. break;
  9284. }
  9285. ret = xmlRelaxNGElementMatch(ctxt, define, node);
  9286. if (ret <= 0) {
  9287. ret = -1;
  9288. if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
  9289. xmlRelaxNGDumpValidError(ctxt);
  9290. break;
  9291. }
  9292. ret = 0;
  9293. if (ctxt->errNr != 0) {
  9294. if (ctxt->errNr > errNr)
  9295. xmlRelaxNGPopErrors(ctxt, errNr);
  9296. while ((ctxt->err != NULL) &&
  9297. (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
  9298. (xmlStrEqual(ctxt->err->arg2, node->name))) ||
  9299. ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
  9300. (xmlStrEqual(ctxt->err->arg1, node->name))) ||
  9301. (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
  9302. (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
  9303. xmlRelaxNGValidErrorPop(ctxt);
  9304. }
  9305. errNr = ctxt->errNr;
  9306. oldflags = ctxt->flags;
  9307. if (ctxt->flags & FLAGS_MIXED_CONTENT) {
  9308. ctxt->flags -= FLAGS_MIXED_CONTENT;
  9309. }
  9310. state = xmlRelaxNGNewValidState(ctxt, node);
  9311. if (state == NULL) {
  9312. ret = -1;
  9313. if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
  9314. xmlRelaxNGDumpValidError(ctxt);
  9315. break;
  9316. }
  9317. oldstate = ctxt->state;
  9318. ctxt->state = state;
  9319. if (define->attrs != NULL) {
  9320. tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
  9321. if (tmp != 0) {
  9322. ret = -1;
  9323. VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
  9324. }
  9325. }
  9326. if (define->contModel != NULL) {
  9327. xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
  9328. xmlRelaxNGStatesPtr tmpstates = ctxt->states;
  9329. xmlNodePtr nseq;
  9330. nstate = xmlRelaxNGNewValidState(ctxt, node);
  9331. ctxt->state = nstate;
  9332. ctxt->states = NULL;
  9333. tmp = xmlRelaxNGValidateCompiledContent(ctxt,
  9334. define->contModel,
  9335. ctxt->state->seq);
  9336. nseq = ctxt->state->seq;
  9337. ctxt->state = tmpstate;
  9338. ctxt->states = tmpstates;
  9339. xmlRelaxNGFreeValidState(ctxt, nstate);
  9340. #ifdef DEBUG_COMPILE
  9341. xmlGenericError(xmlGenericErrorContext,
  9342. "Validating content of '%s' : %d\n",
  9343. define->name, tmp);
  9344. #endif
  9345. if (tmp != 0)
  9346. ret = -1;
  9347. if (ctxt->states != NULL) {
  9348. tmp = -1;
  9349. for (i = 0; i < ctxt->states->nbState; i++) {
  9350. state = ctxt->states->tabState[i];
  9351. ctxt->state = state;
  9352. ctxt->state->seq = nseq;
  9353. if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
  9354. tmp = 0;
  9355. break;
  9356. }
  9357. }
  9358. if (tmp != 0) {
  9359. /*
  9360. * validation error, log the message for the "best" one
  9361. */
  9362. ctxt->flags |= FLAGS_IGNORABLE;
  9363. xmlRelaxNGLogBestError(ctxt);
  9364. }
  9365. for (i = 0; i < ctxt->states->nbState; i++) {
  9366. xmlRelaxNGFreeValidState(ctxt,
  9367. ctxt->states->
  9368. tabState[i]);
  9369. }
  9370. xmlRelaxNGFreeStates(ctxt, ctxt->states);
  9371. ctxt->flags = oldflags;
  9372. ctxt->states = NULL;
  9373. if ((ret == 0) && (tmp == -1))
  9374. ret = -1;
  9375. } else {
  9376. state = ctxt->state;
  9377. if (ctxt->state != NULL)
  9378. ctxt->state->seq = nseq;
  9379. if (ret == 0)
  9380. ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
  9381. xmlRelaxNGFreeValidState(ctxt, state);
  9382. }
  9383. } else {
  9384. if (define->content != NULL) {
  9385. tmp = xmlRelaxNGValidateDefinitionList(ctxt,
  9386. define->
  9387. content);
  9388. if (tmp != 0) {
  9389. ret = -1;
  9390. if (ctxt->state == NULL) {
  9391. ctxt->state = oldstate;
  9392. VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
  9393. node->name);
  9394. ctxt->state = NULL;
  9395. } else {
  9396. VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
  9397. node->name);
  9398. }
  9399. }
  9400. }
  9401. if (ctxt->states != NULL) {
  9402. tmp = -1;
  9403. for (i = 0; i < ctxt->states->nbState; i++) {
  9404. state = ctxt->states->tabState[i];
  9405. ctxt->state = state;
  9406. if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
  9407. tmp = 0;
  9408. break;
  9409. }
  9410. }
  9411. if (tmp != 0) {
  9412. /*
  9413. * validation error, log the message for the "best" one
  9414. */
  9415. ctxt->flags |= FLAGS_IGNORABLE;
  9416. xmlRelaxNGLogBestError(ctxt);
  9417. }
  9418. for (i = 0; i < ctxt->states->nbState; i++) {
  9419. xmlRelaxNGFreeValidState(ctxt,
  9420. ctxt->states->tabState[i]);
  9421. ctxt->states->tabState[i] = NULL;
  9422. }
  9423. xmlRelaxNGFreeStates(ctxt, ctxt->states);
  9424. ctxt->flags = oldflags;
  9425. ctxt->states = NULL;
  9426. if ((ret == 0) && (tmp == -1))
  9427. ret = -1;
  9428. } else {
  9429. state = ctxt->state;
  9430. if (ret == 0)
  9431. ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
  9432. xmlRelaxNGFreeValidState(ctxt, state);
  9433. }
  9434. }
  9435. if (ret == 0) {
  9436. node->psvi = define;
  9437. }
  9438. ctxt->flags = oldflags;
  9439. ctxt->state = oldstate;
  9440. if (oldstate != NULL)
  9441. oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
  9442. if (ret != 0) {
  9443. if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
  9444. xmlRelaxNGDumpValidError(ctxt);
  9445. ret = 0;
  9446. #if 0
  9447. } else {
  9448. ret = -2;
  9449. #endif
  9450. }
  9451. } else {
  9452. if (ctxt->errNr > errNr)
  9453. xmlRelaxNGPopErrors(ctxt, errNr);
  9454. }
  9455. #ifdef DEBUG
  9456. xmlGenericError(xmlGenericErrorContext,
  9457. "xmlRelaxNGValidateDefinition(): validated %s : %d",
  9458. node->name, ret);
  9459. if (oldstate == NULL)
  9460. xmlGenericError(xmlGenericErrorContext, ": no state\n");
  9461. else if (oldstate->seq == NULL)
  9462. xmlGenericError(xmlGenericErrorContext, ": done\n");
  9463. else if (oldstate->seq->type == XML_ELEMENT_NODE)
  9464. xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
  9465. oldstate->seq->name);
  9466. else
  9467. xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
  9468. oldstate->seq->name, oldstate->seq->type);
  9469. #endif
  9470. break;
  9471. case XML_RELAXNG_OPTIONAL:{
  9472. errNr = ctxt->errNr;
  9473. oldflags = ctxt->flags;
  9474. ctxt->flags |= FLAGS_IGNORABLE;
  9475. oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
  9476. ret =
  9477. xmlRelaxNGValidateDefinitionList(ctxt,
  9478. define->content);
  9479. if (ret != 0) {
  9480. if (ctxt->state != NULL)
  9481. xmlRelaxNGFreeValidState(ctxt, ctxt->state);
  9482. ctxt->state = oldstate;
  9483. ctxt->flags = oldflags;
  9484. ret = 0;
  9485. if (ctxt->errNr > errNr)
  9486. xmlRelaxNGPopErrors(ctxt, errNr);
  9487. break;
  9488. }
  9489. if (ctxt->states != NULL) {
  9490. xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
  9491. } else {
  9492. ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
  9493. if (ctxt->states == NULL) {
  9494. xmlRelaxNGFreeValidState(ctxt, oldstate);
  9495. ctxt->flags = oldflags;
  9496. ret = -1;
  9497. if (ctxt->errNr > errNr)
  9498. xmlRelaxNGPopErrors(ctxt, errNr);
  9499. break;
  9500. }
  9501. xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
  9502. xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
  9503. ctxt->state = NULL;
  9504. }
  9505. ctxt->flags = oldflags;
  9506. ret = 0;
  9507. if (ctxt->errNr > errNr)
  9508. xmlRelaxNGPopErrors(ctxt, errNr);
  9509. break;
  9510. }
  9511. case XML_RELAXNG_ONEORMORE:
  9512. errNr = ctxt->errNr;
  9513. ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
  9514. if (ret != 0) {
  9515. break;
  9516. }
  9517. if (ctxt->errNr > errNr)
  9518. xmlRelaxNGPopErrors(ctxt, errNr);
  9519. /* no break on purpose */
  9520. case XML_RELAXNG_ZEROORMORE:{
  9521. int progress;
  9522. xmlRelaxNGStatesPtr states = NULL, res = NULL;
  9523. int base, j;
  9524. errNr = ctxt->errNr;
  9525. res = xmlRelaxNGNewStates(ctxt, 1);
  9526. if (res == NULL) {
  9527. ret = -1;
  9528. break;
  9529. }
  9530. /*
  9531. * All the input states are also exit states
  9532. */
  9533. if (ctxt->state != NULL) {
  9534. xmlRelaxNGAddStates(ctxt, res,
  9535. xmlRelaxNGCopyValidState(ctxt,
  9536. ctxt->
  9537. state));
  9538. } else {
  9539. for (j = 0; j < ctxt->states->nbState; j++) {
  9540. xmlRelaxNGAddStates(ctxt, res,
  9541. xmlRelaxNGCopyValidState(ctxt,
  9542. ctxt->states->tabState[j]));
  9543. }
  9544. }
  9545. oldflags = ctxt->flags;
  9546. ctxt->flags |= FLAGS_IGNORABLE;
  9547. do {
  9548. progress = 0;
  9549. base = res->nbState;
  9550. if (ctxt->states != NULL) {
  9551. states = ctxt->states;
  9552. for (i = 0; i < states->nbState; i++) {
  9553. ctxt->state = states->tabState[i];
  9554. ctxt->states = NULL;
  9555. ret = xmlRelaxNGValidateDefinitionList(ctxt,
  9556. define->
  9557. content);
  9558. if (ret == 0) {
  9559. if (ctxt->state != NULL) {
  9560. tmp = xmlRelaxNGAddStates(ctxt, res,
  9561. ctxt->state);
  9562. ctxt->state = NULL;
  9563. if (tmp == 1)
  9564. progress = 1;
  9565. } else if (ctxt->states != NULL) {
  9566. for (j = 0; j < ctxt->states->nbState;
  9567. j++) {
  9568. tmp =
  9569. xmlRelaxNGAddStates(ctxt, res,
  9570. ctxt->states->tabState[j]);
  9571. if (tmp == 1)
  9572. progress = 1;
  9573. }
  9574. xmlRelaxNGFreeStates(ctxt,
  9575. ctxt->states);
  9576. ctxt->states = NULL;
  9577. }
  9578. } else {
  9579. if (ctxt->state != NULL) {
  9580. xmlRelaxNGFreeValidState(ctxt,
  9581. ctxt->state);
  9582. ctxt->state = NULL;
  9583. }
  9584. }
  9585. }
  9586. } else {
  9587. ret = xmlRelaxNGValidateDefinitionList(ctxt,
  9588. define->
  9589. content);
  9590. if (ret != 0) {
  9591. xmlRelaxNGFreeValidState(ctxt, ctxt->state);
  9592. ctxt->state = NULL;
  9593. } else {
  9594. base = res->nbState;
  9595. if (ctxt->state != NULL) {
  9596. tmp = xmlRelaxNGAddStates(ctxt, res,
  9597. ctxt->state);
  9598. ctxt->state = NULL;
  9599. if (tmp == 1)
  9600. progress = 1;
  9601. } else if (ctxt->states != NULL) {
  9602. for (j = 0; j < ctxt->states->nbState; j++) {
  9603. tmp = xmlRelaxNGAddStates(ctxt, res,
  9604. ctxt->states->tabState[j]);
  9605. if (tmp == 1)
  9606. progress = 1;
  9607. }
  9608. if (states == NULL) {
  9609. states = ctxt->states;
  9610. } else {
  9611. xmlRelaxNGFreeStates(ctxt,
  9612. ctxt->states);
  9613. }
  9614. ctxt->states = NULL;
  9615. }
  9616. }
  9617. }
  9618. if (progress) {
  9619. /*
  9620. * Collect all the new nodes added at that step
  9621. * and make them the new node set
  9622. */
  9623. if (res->nbState - base == 1) {
  9624. ctxt->state = xmlRelaxNGCopyValidState(ctxt,
  9625. res->
  9626. tabState
  9627. [base]);
  9628. } else {
  9629. if (states == NULL) {
  9630. xmlRelaxNGNewStates(ctxt,
  9631. res->nbState - base);
  9632. states = ctxt->states;
  9633. if (states == NULL) {
  9634. progress = 0;
  9635. break;
  9636. }
  9637. }
  9638. states->nbState = 0;
  9639. for (i = base; i < res->nbState; i++)
  9640. xmlRelaxNGAddStates(ctxt, states,
  9641. xmlRelaxNGCopyValidState
  9642. (ctxt, res->tabState[i]));
  9643. ctxt->states = states;
  9644. }
  9645. }
  9646. } while (progress == 1);
  9647. if (states != NULL) {
  9648. xmlRelaxNGFreeStates(ctxt, states);
  9649. }
  9650. ctxt->states = res;
  9651. ctxt->flags = oldflags;
  9652. #if 0
  9653. /*
  9654. * errors may have to be propagated back...
  9655. */
  9656. if (ctxt->errNr > errNr)
  9657. xmlRelaxNGPopErrors(ctxt, errNr);
  9658. #endif
  9659. ret = 0;
  9660. break;
  9661. }
  9662. case XML_RELAXNG_CHOICE:{
  9663. xmlRelaxNGDefinePtr list = NULL;
  9664. xmlRelaxNGStatesPtr states = NULL;
  9665. node = xmlRelaxNGSkipIgnored(ctxt, node);
  9666. errNr = ctxt->errNr;
  9667. if ((define->dflags & IS_TRIABLE) && (define->data != NULL) &&
  9668. (node != NULL)) {
  9669. /*
  9670. * node == NULL can't be optimized since IS_TRIABLE
  9671. * doesn't account for choice which may lead to
  9672. * only attributes.
  9673. */
  9674. xmlHashTablePtr triage =
  9675. (xmlHashTablePtr) define->data;
  9676. /*
  9677. * Something we can optimize cleanly there is only one
  9678. * possble branch out !
  9679. */
  9680. if ((node->type == XML_TEXT_NODE) ||
  9681. (node->type == XML_CDATA_SECTION_NODE)) {
  9682. list =
  9683. xmlHashLookup2(triage, BAD_CAST "#text", NULL);
  9684. } else if (node->type == XML_ELEMENT_NODE) {
  9685. if (node->ns != NULL) {
  9686. list = xmlHashLookup2(triage, node->name,
  9687. node->ns->href);
  9688. if (list == NULL)
  9689. list =
  9690. xmlHashLookup2(triage, BAD_CAST "#any",
  9691. node->ns->href);
  9692. } else
  9693. list =
  9694. xmlHashLookup2(triage, node->name, NULL);
  9695. if (list == NULL)
  9696. list =
  9697. xmlHashLookup2(triage, BAD_CAST "#any",
  9698. NULL);
  9699. }
  9700. if (list == NULL) {
  9701. ret = -1;
  9702. VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name);
  9703. break;
  9704. }
  9705. ret = xmlRelaxNGValidateDefinition(ctxt, list);
  9706. if (ret == 0) {
  9707. }
  9708. break;
  9709. }
  9710. list = define->content;
  9711. oldflags = ctxt->flags;
  9712. ctxt->flags |= FLAGS_IGNORABLE;
  9713. while (list != NULL) {
  9714. oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
  9715. ret = xmlRelaxNGValidateDefinition(ctxt, list);
  9716. if (ret == 0) {
  9717. if (states == NULL) {
  9718. states = xmlRelaxNGNewStates(ctxt, 1);
  9719. }
  9720. if (ctxt->state != NULL) {
  9721. xmlRelaxNGAddStates(ctxt, states, ctxt->state);
  9722. } else if (ctxt->states != NULL) {
  9723. for (i = 0; i < ctxt->states->nbState; i++) {
  9724. xmlRelaxNGAddStates(ctxt, states,
  9725. ctxt->states->
  9726. tabState[i]);
  9727. }
  9728. xmlRelaxNGFreeStates(ctxt, ctxt->states);
  9729. ctxt->states = NULL;
  9730. }
  9731. } else {
  9732. xmlRelaxNGFreeValidState(ctxt, ctxt->state);
  9733. }
  9734. ctxt->state = oldstate;
  9735. list = list->next;
  9736. }
  9737. if (states != NULL) {
  9738. xmlRelaxNGFreeValidState(ctxt, oldstate);
  9739. ctxt->states = states;
  9740. ctxt->state = NULL;
  9741. ret = 0;
  9742. } else {
  9743. ctxt->states = NULL;
  9744. }
  9745. ctxt->flags = oldflags;
  9746. if (ret != 0) {
  9747. if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
  9748. xmlRelaxNGDumpValidError(ctxt);
  9749. }
  9750. } else {
  9751. if (ctxt->errNr > errNr)
  9752. xmlRelaxNGPopErrors(ctxt, errNr);
  9753. }
  9754. break;
  9755. }
  9756. case XML_RELAXNG_DEF:
  9757. case XML_RELAXNG_GROUP:
  9758. ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
  9759. break;
  9760. case XML_RELAXNG_INTERLEAVE:
  9761. ret = xmlRelaxNGValidateInterleave(ctxt, define);
  9762. break;
  9763. case XML_RELAXNG_ATTRIBUTE:
  9764. ret = xmlRelaxNGValidateAttribute(ctxt, define);
  9765. break;
  9766. case XML_RELAXNG_START:
  9767. case XML_RELAXNG_NOOP:
  9768. case XML_RELAXNG_REF:
  9769. case XML_RELAXNG_EXTERNALREF:
  9770. case XML_RELAXNG_PARENTREF:
  9771. ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
  9772. break;
  9773. case XML_RELAXNG_DATATYPE:{
  9774. xmlNodePtr child;
  9775. xmlChar *content = NULL;
  9776. child = node;
  9777. while (child != NULL) {
  9778. if (child->type == XML_ELEMENT_NODE) {
  9779. VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
  9780. node->parent->name);
  9781. ret = -1;
  9782. break;
  9783. } else if ((child->type == XML_TEXT_NODE) ||
  9784. (child->type == XML_CDATA_SECTION_NODE)) {
  9785. content = xmlStrcat(content, child->content);
  9786. }
  9787. /* TODO: handle entities ... */
  9788. child = child->next;
  9789. }
  9790. if (ret == -1) {
  9791. if (content != NULL)
  9792. xmlFree(content);
  9793. break;
  9794. }
  9795. if (content == NULL) {
  9796. content = xmlStrdup(BAD_CAST "");
  9797. if (content == NULL) {
  9798. xmlRngVErrMemory(ctxt, "validating\n");
  9799. ret = -1;
  9800. break;
  9801. }
  9802. }
  9803. ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
  9804. ctxt->state->seq);
  9805. if (ret == -1) {
  9806. VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
  9807. } else if (ret == 0) {
  9808. ctxt->state->seq = NULL;
  9809. }
  9810. if (content != NULL)
  9811. xmlFree(content);
  9812. break;
  9813. }
  9814. case XML_RELAXNG_VALUE:{
  9815. xmlChar *content = NULL;
  9816. xmlChar *oldvalue;
  9817. xmlNodePtr child;
  9818. child = node;
  9819. while (child != NULL) {
  9820. if (child->type == XML_ELEMENT_NODE) {
  9821. VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
  9822. node->parent->name);
  9823. ret = -1;
  9824. break;
  9825. } else if ((child->type == XML_TEXT_NODE) ||
  9826. (child->type == XML_CDATA_SECTION_NODE)) {
  9827. content = xmlStrcat(content, child->content);
  9828. }
  9829. /* TODO: handle entities ... */
  9830. child = child->next;
  9831. }
  9832. if (ret == -1) {
  9833. if (content != NULL)
  9834. xmlFree(content);
  9835. break;
  9836. }
  9837. if (content == NULL) {
  9838. content = xmlStrdup(BAD_CAST "");
  9839. if (content == NULL) {
  9840. xmlRngVErrMemory(ctxt, "validating\n");
  9841. ret = -1;
  9842. break;
  9843. }
  9844. }
  9845. oldvalue = ctxt->state->value;
  9846. ctxt->state->value = content;
  9847. ret = xmlRelaxNGValidateValue(ctxt, define);
  9848. ctxt->state->value = oldvalue;
  9849. if (ret == -1) {
  9850. VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
  9851. } else if (ret == 0) {
  9852. ctxt->state->seq = NULL;
  9853. }
  9854. if (content != NULL)
  9855. xmlFree(content);
  9856. break;
  9857. }
  9858. case XML_RELAXNG_LIST:{
  9859. xmlChar *content;
  9860. xmlNodePtr child;
  9861. xmlChar *oldvalue, *oldendvalue;
  9862. int len;
  9863. /*
  9864. * Make sure it's only text nodes
  9865. */
  9866. content = NULL;
  9867. child = node;
  9868. while (child != NULL) {
  9869. if (child->type == XML_ELEMENT_NODE) {
  9870. VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
  9871. node->parent->name);
  9872. ret = -1;
  9873. break;
  9874. } else if ((child->type == XML_TEXT_NODE) ||
  9875. (child->type == XML_CDATA_SECTION_NODE)) {
  9876. content = xmlStrcat(content, child->content);
  9877. }
  9878. /* TODO: handle entities ... */
  9879. child = child->next;
  9880. }
  9881. if (ret == -1) {
  9882. if (content != NULL)
  9883. xmlFree(content);
  9884. break;
  9885. }
  9886. if (content == NULL) {
  9887. content = xmlStrdup(BAD_CAST "");
  9888. if (content == NULL) {
  9889. xmlRngVErrMemory(ctxt, "validating\n");
  9890. ret = -1;
  9891. break;
  9892. }
  9893. }
  9894. len = xmlStrlen(content);
  9895. oldvalue = ctxt->state->value;
  9896. oldendvalue = ctxt->state->endvalue;
  9897. ctxt->state->value = content;
  9898. ctxt->state->endvalue = content + len;
  9899. ret = xmlRelaxNGValidateValue(ctxt, define);
  9900. ctxt->state->value = oldvalue;
  9901. ctxt->state->endvalue = oldendvalue;
  9902. if (ret == -1) {
  9903. VALID_ERR(XML_RELAXNG_ERR_LIST);
  9904. } else if ((ret == 0) && (node != NULL)) {
  9905. ctxt->state->seq = node->next;
  9906. }
  9907. if (content != NULL)
  9908. xmlFree(content);
  9909. break;
  9910. }
  9911. case XML_RELAXNG_EXCEPT:
  9912. case XML_RELAXNG_PARAM:
  9913. TODO ret = -1;
  9914. break;
  9915. }
  9916. ctxt->depth--;
  9917. #ifdef DEBUG
  9918. for (i = 0; i < ctxt->depth; i++)
  9919. xmlGenericError(xmlGenericErrorContext, " ");
  9920. xmlGenericError(xmlGenericErrorContext,
  9921. "Validating %s ", xmlRelaxNGDefName(define));
  9922. if (define->name != NULL)
  9923. xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
  9924. if (ret == 0)
  9925. xmlGenericError(xmlGenericErrorContext, "suceeded\n");
  9926. else
  9927. xmlGenericError(xmlGenericErrorContext, "failed\n");
  9928. #endif
  9929. return (ret);
  9930. }
  9931. /**
  9932. * xmlRelaxNGValidateDefinition:
  9933. * @ctxt: a Relax-NG validation context
  9934. * @define: the definition to verify
  9935. *
  9936. * Validate the current node lists against the definition
  9937. *
  9938. * Returns 0 if the validation succeeded or an error code.
  9939. */
  9940. static int
  9941. xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
  9942. xmlRelaxNGDefinePtr define)
  9943. {
  9944. xmlRelaxNGStatesPtr states, res;
  9945. int i, j, k, ret, oldflags;
  9946. /*
  9947. * We should NOT have both ctxt->state and ctxt->states
  9948. */
  9949. if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
  9950. TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
  9951. ctxt->state = NULL;
  9952. }
  9953. if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
  9954. if (ctxt->states != NULL) {
  9955. ctxt->state = ctxt->states->tabState[0];
  9956. xmlRelaxNGFreeStates(ctxt, ctxt->states);
  9957. ctxt->states = NULL;
  9958. }
  9959. ret = xmlRelaxNGValidateState(ctxt, define);
  9960. if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
  9961. TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
  9962. ctxt->state = NULL;
  9963. }
  9964. if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
  9965. ctxt->state = ctxt->states->tabState[0];
  9966. xmlRelaxNGFreeStates(ctxt, ctxt->states);
  9967. ctxt->states = NULL;
  9968. }
  9969. return (ret);
  9970. }
  9971. states = ctxt->states;
  9972. ctxt->states = NULL;
  9973. res = NULL;
  9974. j = 0;
  9975. oldflags = ctxt->flags;
  9976. ctxt->flags |= FLAGS_IGNORABLE;
  9977. for (i = 0; i < states->nbState; i++) {
  9978. ctxt->state = states->tabState[i];
  9979. ctxt->states = NULL;
  9980. ret = xmlRelaxNGValidateState(ctxt, define);
  9981. /*
  9982. * We should NOT have both ctxt->state and ctxt->states
  9983. */
  9984. if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
  9985. TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
  9986. ctxt->state = NULL;
  9987. }
  9988. if (ret == 0) {
  9989. if (ctxt->states == NULL) {
  9990. if (res != NULL) {
  9991. /* add the state to the container */
  9992. xmlRelaxNGAddStates(ctxt, res, ctxt->state);
  9993. ctxt->state = NULL;
  9994. } else {
  9995. /* add the state directly in states */
  9996. states->tabState[j++] = ctxt->state;
  9997. ctxt->state = NULL;
  9998. }
  9999. } else {
  10000. if (res == NULL) {
  10001. /* make it the new container and copy other results */
  10002. res = ctxt->states;
  10003. ctxt->states = NULL;
  10004. for (k = 0; k < j; k++)
  10005. xmlRelaxNGAddStates(ctxt, res,
  10006. states->tabState[k]);
  10007. } else {
  10008. /* add all the new results to res and reff the container */
  10009. for (k = 0; k < ctxt->states->nbState; k++)
  10010. xmlRelaxNGAddStates(ctxt, res,
  10011. ctxt->states->tabState[k]);
  10012. xmlRelaxNGFreeStates(ctxt, ctxt->states);
  10013. ctxt->states = NULL;
  10014. }
  10015. }
  10016. } else {
  10017. if (ctxt->state != NULL) {
  10018. xmlRelaxNGFreeValidState(ctxt, ctxt->state);
  10019. ctxt->state = NULL;
  10020. } else if (ctxt->states != NULL) {
  10021. for (k = 0; k < ctxt->states->nbState; k++)
  10022. xmlRelaxNGFreeValidState(ctxt,
  10023. ctxt->states->tabState[k]);
  10024. xmlRelaxNGFreeStates(ctxt, ctxt->states);
  10025. ctxt->states = NULL;
  10026. }
  10027. }
  10028. }
  10029. ctxt->flags = oldflags;
  10030. if (res != NULL) {
  10031. xmlRelaxNGFreeStates(ctxt, states);
  10032. ctxt->states = res;
  10033. ret = 0;
  10034. } else if (j > 1) {
  10035. states->nbState = j;
  10036. ctxt->states = states;
  10037. ret = 0;
  10038. } else if (j == 1) {
  10039. ctxt->state = states->tabState[0];
  10040. xmlRelaxNGFreeStates(ctxt, states);
  10041. ret = 0;
  10042. } else {
  10043. ret = -1;
  10044. xmlRelaxNGFreeStates(ctxt, states);
  10045. if (ctxt->states != NULL) {
  10046. xmlRelaxNGFreeStates(ctxt, ctxt->states);
  10047. ctxt->states = NULL;
  10048. }
  10049. }
  10050. if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
  10051. TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
  10052. ctxt->state = NULL;
  10053. }
  10054. return (ret);
  10055. }
  10056. /**
  10057. * xmlRelaxNGValidateDocument:
  10058. * @ctxt: a Relax-NG validation context
  10059. * @doc: the document
  10060. *
  10061. * Validate the given document
  10062. *
  10063. * Returns 0 if the validation succeeded or an error code.
  10064. */
  10065. static int
  10066. xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
  10067. {
  10068. int ret;
  10069. xmlRelaxNGPtr schema;
  10070. xmlRelaxNGGrammarPtr grammar;
  10071. xmlRelaxNGValidStatePtr state;
  10072. xmlNodePtr node;
  10073. if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
  10074. return (-1);
  10075. ctxt->errNo = XML_RELAXNG_OK;
  10076. schema = ctxt->schema;
  10077. grammar = schema->topgrammar;
  10078. if (grammar == NULL) {
  10079. VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
  10080. return (-1);
  10081. }
  10082. state = xmlRelaxNGNewValidState(ctxt, NULL);
  10083. ctxt->state = state;
  10084. ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
  10085. if ((ctxt->state != NULL) && (state->seq != NULL)) {
  10086. state = ctxt->state;
  10087. node = state->seq;
  10088. node = xmlRelaxNGSkipIgnored(ctxt, node);
  10089. if (node != NULL) {
  10090. if (ret != -1) {
  10091. VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
  10092. ret = -1;
  10093. }
  10094. }
  10095. } else if (ctxt->states != NULL) {
  10096. int i;
  10097. int tmp = -1;
  10098. for (i = 0; i < ctxt->states->nbState; i++) {
  10099. state = ctxt->states->tabState[i];
  10100. node = state->seq;
  10101. node = xmlRelaxNGSkipIgnored(ctxt, node);
  10102. if (node == NULL)
  10103. tmp = 0;
  10104. xmlRelaxNGFreeValidState(ctxt, state);
  10105. }
  10106. if (tmp == -1) {
  10107. if (ret != -1) {
  10108. VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
  10109. ret = -1;
  10110. }
  10111. }
  10112. }
  10113. if (ctxt->state != NULL) {
  10114. xmlRelaxNGFreeValidState(ctxt, ctxt->state);
  10115. ctxt->state = NULL;
  10116. }
  10117. if (ret != 0)
  10118. xmlRelaxNGDumpValidError(ctxt);
  10119. #ifdef DEBUG
  10120. else if (ctxt->errNr != 0) {
  10121. ctxt->error(ctxt->userData,
  10122. "%d Extra error messages left on stack !\n",
  10123. ctxt->errNr);
  10124. xmlRelaxNGDumpValidError(ctxt);
  10125. }
  10126. #endif
  10127. #ifdef LIBXML_VALID_ENABLED
  10128. if (ctxt->idref == 1) {
  10129. xmlValidCtxt vctxt;
  10130. memset(&vctxt, 0, sizeof(xmlValidCtxt));
  10131. vctxt.valid = 1;
  10132. vctxt.error = ctxt->error;
  10133. vctxt.warning = ctxt->warning;
  10134. vctxt.userData = ctxt->userData;
  10135. if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
  10136. ret = -1;
  10137. }
  10138. #endif /* LIBXML_VALID_ENABLED */
  10139. if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
  10140. ret = -1;
  10141. return (ret);
  10142. }
  10143. /**
  10144. * xmlRelaxNGCleanPSVI:
  10145. * @node: an input element or document
  10146. *
  10147. * Call this routine to speed up XPath computation on static documents.
  10148. * This stamps all the element nodes with the document order
  10149. * Like for line information, the order is kept in the element->content
  10150. * field, the value stored is actually - the node number (starting at -1)
  10151. * to be able to differentiate from line numbers.
  10152. *
  10153. * Returns the number of elements found in the document or -1 in case
  10154. * of error.
  10155. */
  10156. static void
  10157. xmlRelaxNGCleanPSVI(xmlNodePtr node) {
  10158. xmlNodePtr cur;
  10159. if ((node == NULL) ||
  10160. ((node->type != XML_ELEMENT_NODE) &&
  10161. (node->type != XML_DOCUMENT_NODE) &&
  10162. (node->type != XML_HTML_DOCUMENT_NODE)))
  10163. return;
  10164. if (node->type == XML_ELEMENT_NODE)
  10165. node->psvi = NULL;
  10166. cur = node->children;
  10167. while (cur != NULL) {
  10168. if (cur->type == XML_ELEMENT_NODE) {
  10169. cur->psvi = NULL;
  10170. if (cur->children != NULL) {
  10171. cur = cur->children;
  10172. continue;
  10173. }
  10174. }
  10175. if (cur->next != NULL) {
  10176. cur = cur->next;
  10177. continue;
  10178. }
  10179. do {
  10180. cur = cur->parent;
  10181. if (cur == NULL)
  10182. break;
  10183. if (cur == node) {
  10184. cur = NULL;
  10185. break;
  10186. }
  10187. if (cur->next != NULL) {
  10188. cur = cur->next;
  10189. break;
  10190. }
  10191. } while (cur != NULL);
  10192. }
  10193. return;
  10194. }
  10195. /************************************************************************
  10196. * *
  10197. * Validation interfaces *
  10198. * *
  10199. ************************************************************************/
  10200. /**
  10201. * xmlRelaxNGNewValidCtxt:
  10202. * @schema: a precompiled XML RelaxNGs
  10203. *
  10204. * Create an XML RelaxNGs validation context based on the given schema
  10205. *
  10206. * Returns the validation context or NULL in case of error
  10207. */
  10208. xmlRelaxNGValidCtxtPtr
  10209. xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
  10210. {
  10211. xmlRelaxNGValidCtxtPtr ret;
  10212. ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
  10213. if (ret == NULL) {
  10214. xmlRngVErrMemory(NULL, "building context\n");
  10215. return (NULL);
  10216. }
  10217. memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
  10218. ret->schema = schema;
  10219. ret->error = xmlGenericError;
  10220. ret->userData = xmlGenericErrorContext;
  10221. ret->errNr = 0;
  10222. ret->errMax = 0;
  10223. ret->err = NULL;
  10224. ret->errTab = NULL;
  10225. if (schema != NULL)
  10226. ret->idref = schema->idref;
  10227. ret->states = NULL;
  10228. ret->freeState = NULL;
  10229. ret->freeStates = NULL;
  10230. ret->errNo = XML_RELAXNG_OK;
  10231. return (ret);
  10232. }
  10233. /**
  10234. * xmlRelaxNGFreeValidCtxt:
  10235. * @ctxt: the schema validation context
  10236. *
  10237. * Free the resources associated to the schema validation context
  10238. */
  10239. void
  10240. xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
  10241. {
  10242. int k;
  10243. if (ctxt == NULL)
  10244. return;
  10245. if (ctxt->states != NULL)
  10246. xmlRelaxNGFreeStates(NULL, ctxt->states);
  10247. if (ctxt->freeState != NULL) {
  10248. for (k = 0; k < ctxt->freeState->nbState; k++) {
  10249. xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
  10250. }
  10251. xmlRelaxNGFreeStates(NULL, ctxt->freeState);
  10252. }
  10253. if (ctxt->freeStates != NULL) {
  10254. for (k = 0; k < ctxt->freeStatesNr; k++) {
  10255. xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
  10256. }
  10257. xmlFree(ctxt->freeStates);
  10258. }
  10259. if (ctxt->errTab != NULL)
  10260. xmlFree(ctxt->errTab);
  10261. if (ctxt->elemTab != NULL) {
  10262. xmlRegExecCtxtPtr exec;
  10263. exec = xmlRelaxNGElemPop(ctxt);
  10264. while (exec != NULL) {
  10265. xmlRegFreeExecCtxt(exec);
  10266. exec = xmlRelaxNGElemPop(ctxt);
  10267. }
  10268. xmlFree(ctxt->elemTab);
  10269. }
  10270. xmlFree(ctxt);
  10271. }
  10272. /**
  10273. * xmlRelaxNGSetValidErrors:
  10274. * @ctxt: a Relax-NG validation context
  10275. * @err: the error function
  10276. * @warn: the warning function
  10277. * @ctx: the functions context
  10278. *
  10279. * Set the error and warning callback informations
  10280. */
  10281. void
  10282. xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
  10283. xmlRelaxNGValidityErrorFunc err,
  10284. xmlRelaxNGValidityWarningFunc warn, void *ctx)
  10285. {
  10286. if (ctxt == NULL)
  10287. return;
  10288. ctxt->error = err;
  10289. ctxt->warning = warn;
  10290. ctxt->userData = ctx;
  10291. ctxt->serror = NULL;
  10292. }
  10293. /**
  10294. * xmlRelaxNGSetValidStructuredErrors:
  10295. * @ctxt: a Relax-NG validation context
  10296. * @serror: the structured error function
  10297. * @ctx: the functions context
  10298. *
  10299. * Set the structured error callback
  10300. */
  10301. void
  10302. xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt,
  10303. xmlStructuredErrorFunc serror, void *ctx)
  10304. {
  10305. if (ctxt == NULL)
  10306. return;
  10307. ctxt->serror = serror;
  10308. ctxt->error = NULL;
  10309. ctxt->warning = NULL;
  10310. ctxt->userData = ctx;
  10311. }
  10312. /**
  10313. * xmlRelaxNGGetValidErrors:
  10314. * @ctxt: a Relax-NG validation context
  10315. * @err: the error function result
  10316. * @warn: the warning function result
  10317. * @ctx: the functions context result
  10318. *
  10319. * Get the error and warning callback informations
  10320. *
  10321. * Returns -1 in case of error and 0 otherwise
  10322. */
  10323. int
  10324. xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
  10325. xmlRelaxNGValidityErrorFunc * err,
  10326. xmlRelaxNGValidityWarningFunc * warn, void **ctx)
  10327. {
  10328. if (ctxt == NULL)
  10329. return (-1);
  10330. if (err != NULL)
  10331. *err = ctxt->error;
  10332. if (warn != NULL)
  10333. *warn = ctxt->warning;
  10334. if (ctx != NULL)
  10335. *ctx = ctxt->userData;
  10336. return (0);
  10337. }
  10338. /**
  10339. * xmlRelaxNGValidateDoc:
  10340. * @ctxt: a Relax-NG validation context
  10341. * @doc: a parsed document tree
  10342. *
  10343. * Validate a document tree in memory.
  10344. *
  10345. * Returns 0 if the document is valid, a positive error code
  10346. * number otherwise and -1 in case of internal or API error.
  10347. */
  10348. int
  10349. xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
  10350. {
  10351. int ret;
  10352. if ((ctxt == NULL) || (doc == NULL))
  10353. return (-1);
  10354. ctxt->doc = doc;
  10355. ret = xmlRelaxNGValidateDocument(ctxt, doc);
  10356. /*
  10357. * Remove all left PSVI
  10358. */
  10359. xmlRelaxNGCleanPSVI((xmlNodePtr) doc);
  10360. /*
  10361. * TODO: build error codes
  10362. */
  10363. if (ret == -1)
  10364. return (1);
  10365. return (ret);
  10366. }
  10367. #define bottom_relaxng
  10368. #include "elfgcchack.h"
  10369. #endif /* LIBXML_SCHEMAS_ENABLED */