relaxng.c 360 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104
  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 */