relaxng.c 360 KB

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