12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104 |
- /*
- * relaxng.c : implementation of the Relax-NG handling and validity checking
- *
- * See Copyright for the status of this software.
- *
- * Daniel Veillard <veillard@redhat.com>
- */
- /**
- * TODO:
- * - add support for DTD compatibility spec
- * http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html
- * - report better mem allocations pbms at runtime and abort immediately.
- */
- #define IN_LIBXML
- #include "libxml.h"
- #ifdef LIBXML_SCHEMAS_ENABLED
- #include <string.h>
- #include <stdio.h>
- #include <stddef.h>
- #include <libxml/xmlmemory.h>
- #include <libxml/parser.h>
- #include <libxml/parserInternals.h>
- #include <libxml/hash.h>
- #include <libxml/uri.h>
- #include <libxml/relaxng.h>
- #include <libxml/xmlschemastypes.h>
- #include <libxml/xmlautomata.h>
- #include <libxml/xmlregexp.h>
- #include <libxml/xmlschemastypes.h>
- /*
- * The Relax-NG namespace
- */
- static const xmlChar *xmlRelaxNGNs = (const xmlChar *)
- "http://relaxng.org/ns/structure/1.0";
- #define IS_RELAXNG(node, typ) \
- ((node != NULL) && (node->ns != NULL) && \
- (node->type == XML_ELEMENT_NODE) && \
- (xmlStrEqual(node->name, (const xmlChar *) typ)) && \
- (xmlStrEqual(node->ns->href, xmlRelaxNGNs)))
- #if 0
- #define DEBUG 1
- #define DEBUG_GRAMMAR 1
- #define DEBUG_CONTENT 1
- #define DEBUG_TYPE 1
- #define DEBUG_VALID 1
- #define DEBUG_INTERLEAVE 1
- #define DEBUG_LIST 1
- #define DEBUG_INCLUDE 1
- #define DEBUG_ERROR 1
- #define DEBUG_COMPILE 1
- #define DEBUG_PROGRESSIVE 1
- #endif
- #define MAX_ERROR 5
- #define TODO \
- xmlGenericError(xmlGenericErrorContext, \
- "Unimplemented block at %s:%d\n", \
- __FILE__, __LINE__);
- typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema;
- typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr;
- typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine;
- typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr;
- typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument;
- typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr;
- typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude;
- typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr;
- typedef enum {
- XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */
- XML_RELAXNG_COMBINE_CHOICE, /* choice */
- XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */
- } xmlRelaxNGCombine;
- typedef enum {
- XML_RELAXNG_CONTENT_ERROR = -1,
- XML_RELAXNG_CONTENT_EMPTY = 0,
- XML_RELAXNG_CONTENT_SIMPLE,
- XML_RELAXNG_CONTENT_COMPLEX
- } xmlRelaxNGContentType;
- typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar;
- typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr;
- struct _xmlRelaxNGGrammar {
- xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */
- xmlRelaxNGGrammarPtr children; /* the children grammar if any */
- xmlRelaxNGGrammarPtr next; /* the next grammar if any */
- xmlRelaxNGDefinePtr start; /* <start> content */
- xmlRelaxNGCombine combine; /* the default combine value */
- xmlRelaxNGDefinePtr startList; /* list of <start> definitions */
- xmlHashTablePtr defs; /* define* */
- xmlHashTablePtr refs; /* references */
- };
- typedef enum {
- XML_RELAXNG_NOOP = -1, /* a no operation from simplification */
- XML_RELAXNG_EMPTY = 0, /* an empty pattern */
- XML_RELAXNG_NOT_ALLOWED, /* not allowed top */
- XML_RELAXNG_EXCEPT, /* except present in nameclass defs */
- XML_RELAXNG_TEXT, /* textual content */
- XML_RELAXNG_ELEMENT, /* an element */
- XML_RELAXNG_DATATYPE, /* external data type definition */
- XML_RELAXNG_PARAM, /* external data type parameter */
- XML_RELAXNG_VALUE, /* value from an external data type definition */
- XML_RELAXNG_LIST, /* a list of patterns */
- XML_RELAXNG_ATTRIBUTE, /* an attribute following a pattern */
- XML_RELAXNG_DEF, /* a definition */
- XML_RELAXNG_REF, /* reference to a definition */
- XML_RELAXNG_EXTERNALREF, /* reference to an external def */
- XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */
- XML_RELAXNG_OPTIONAL, /* optional patterns */
- XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */
- XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */
- XML_RELAXNG_CHOICE, /* a choice between non empty patterns */
- XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */
- XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */
- XML_RELAXNG_START /* Used to keep track of starts on grammars */
- } xmlRelaxNGType;
- #define IS_NULLABLE (1 << 0)
- #define IS_NOT_NULLABLE (1 << 1)
- #define IS_INDETERMINIST (1 << 2)
- #define IS_MIXED (1 << 3)
- #define IS_TRIABLE (1 << 4)
- #define IS_PROCESSED (1 << 5)
- #define IS_COMPILABLE (1 << 6)
- #define IS_NOT_COMPILABLE (1 << 7)
- #define IS_EXTERNAL_REF (1 << 8)
- struct _xmlRelaxNGDefine {
- xmlRelaxNGType type; /* the type of definition */
- xmlNodePtr node; /* the node in the source */
- xmlChar *name; /* the element local name if present */
- xmlChar *ns; /* the namespace local name if present */
- xmlChar *value; /* value when available */
- void *data; /* data lib or specific pointer */
- xmlRelaxNGDefinePtr content; /* the expected content */
- xmlRelaxNGDefinePtr parent; /* the parent definition, if any */
- xmlRelaxNGDefinePtr next; /* list within grouping sequences */
- xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */
- xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */
- xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */
- short depth; /* used for the cycle detection */
- short dflags; /* define related flags */
- xmlRegexpPtr contModel; /* a compiled content model if available */
- };
- /**
- * _xmlRelaxNG:
- *
- * A RelaxNGs definition
- */
- struct _xmlRelaxNG {
- void *_private; /* unused by the library for users or bindings */
- xmlRelaxNGGrammarPtr topgrammar;
- xmlDocPtr doc;
- int idref; /* requires idref checking */
- xmlHashTablePtr defs; /* define */
- xmlHashTablePtr refs; /* references */
- xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
- xmlRelaxNGIncludePtr includes; /* all the includes loaded */
- int defNr; /* number of defines used */
- xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
- };
- #define XML_RELAXNG_IN_ATTRIBUTE (1 << 0)
- #define XML_RELAXNG_IN_ONEORMORE (1 << 1)
- #define XML_RELAXNG_IN_LIST (1 << 2)
- #define XML_RELAXNG_IN_DATAEXCEPT (1 << 3)
- #define XML_RELAXNG_IN_START (1 << 4)
- #define XML_RELAXNG_IN_OOMGROUP (1 << 5)
- #define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6)
- #define XML_RELAXNG_IN_EXTERNALREF (1 << 7)
- #define XML_RELAXNG_IN_ANYEXCEPT (1 << 8)
- #define XML_RELAXNG_IN_NSEXCEPT (1 << 9)
- struct _xmlRelaxNGParserCtxt {
- void *userData; /* user specific data block */
- xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
- xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
- xmlStructuredErrorFunc serror;
- xmlRelaxNGValidErr err;
- xmlRelaxNGPtr schema; /* The schema in use */
- xmlRelaxNGGrammarPtr grammar; /* the current grammar */
- xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */
- int flags; /* parser flags */
- int nbErrors; /* number of errors at parse time */
- int nbWarnings; /* number of warnings at parse time */
- const xmlChar *define; /* the current define scope */
- xmlRelaxNGDefinePtr def; /* the current define */
- int nbInterleaves;
- xmlHashTablePtr interleaves; /* keep track of all the interleaves */
- xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
- xmlRelaxNGIncludePtr includes; /* all the includes loaded */
- xmlChar *URL;
- xmlDocPtr document;
- int defNr; /* number of defines used */
- int defMax; /* number of defines allocated */
- xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
- const char *buffer;
- int size;
- /* the document stack */
- xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */
- int docNr; /* Depth of the parsing stack */
- int docMax; /* Max depth of the parsing stack */
- xmlRelaxNGDocumentPtr *docTab; /* array of docs */
- /* the include stack */
- xmlRelaxNGIncludePtr inc; /* Current parsed include */
- int incNr; /* Depth of the include parsing stack */
- int incMax; /* Max depth of the parsing stack */
- xmlRelaxNGIncludePtr *incTab; /* array of incs */
- int idref; /* requires idref checking */
- /* used to compile content models */
- xmlAutomataPtr am; /* the automata */
- xmlAutomataStatePtr state; /* used to build the automata */
- int crng; /* compact syntax and other flags */
- int freedoc; /* need to free the document */
- };
- #define FLAGS_IGNORABLE 1
- #define FLAGS_NEGATIVE 2
- #define FLAGS_MIXED_CONTENT 4
- #define FLAGS_NOERROR 8
- /**
- * xmlRelaxNGInterleaveGroup:
- *
- * A RelaxNGs partition set associated to lists of definitions
- */
- typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
- typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
- struct _xmlRelaxNGInterleaveGroup {
- xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
- xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
- xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
- };
- #define IS_DETERMINIST 1
- #define IS_NEEDCHECK 2
- /**
- * xmlRelaxNGPartitions:
- *
- * A RelaxNGs partition associated to an interleave group
- */
- typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
- typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
- struct _xmlRelaxNGPartition {
- int nbgroups; /* number of groups in the partitions */
- xmlHashTablePtr triage; /* hash table used to direct nodes to the
- * right group when possible */
- int flags; /* determinist ? */
- xmlRelaxNGInterleaveGroupPtr *groups;
- };
- /**
- * xmlRelaxNGValidState:
- *
- * A RelaxNGs validation state
- */
- #define MAX_ATTR 20
- typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
- typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
- struct _xmlRelaxNGValidState {
- xmlNodePtr node; /* the current node */
- xmlNodePtr seq; /* the sequence of children left to validate */
- int nbAttrs; /* the number of attributes */
- int maxAttrs; /* the size of attrs */
- int nbAttrLeft; /* the number of attributes left to validate */
- xmlChar *value; /* the value when operating on string */
- xmlChar *endvalue; /* the end value when operating on string */
- xmlAttrPtr *attrs; /* the array of attributes */
- };
- /**
- * xmlRelaxNGStates:
- *
- * A RelaxNGs container for validation state
- */
- typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
- typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
- struct _xmlRelaxNGStates {
- int nbState; /* the number of states */
- int maxState; /* the size of the array */
- xmlRelaxNGValidStatePtr *tabState;
- };
- #define ERROR_IS_DUP 1
- /**
- * xmlRelaxNGValidError:
- *
- * A RelaxNGs validation error
- */
- typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
- typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
- struct _xmlRelaxNGValidError {
- xmlRelaxNGValidErr err; /* the error number */
- int flags; /* flags */
- xmlNodePtr node; /* the current node */
- xmlNodePtr seq; /* the current child */
- const xmlChar *arg1; /* first arg */
- const xmlChar *arg2; /* second arg */
- };
- /**
- * xmlRelaxNGValidCtxt:
- *
- * A RelaxNGs validation context
- */
- struct _xmlRelaxNGValidCtxt {
- void *userData; /* user specific data block */
- xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
- xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
- xmlStructuredErrorFunc serror;
- int nbErrors; /* number of errors in validation */
- xmlRelaxNGPtr schema; /* The schema in use */
- xmlDocPtr doc; /* the document being validated */
- int flags; /* validation flags */
- int depth; /* validation depth */
- int idref; /* requires idref checking */
- int errNo; /* the first error found */
- /*
- * Errors accumulated in branches may have to be stacked to be
- * provided back when it's sure they affect validation.
- */
- xmlRelaxNGValidErrorPtr err; /* Last error */
- int errNr; /* Depth of the error stack */
- int errMax; /* Max depth of the error stack */
- xmlRelaxNGValidErrorPtr errTab; /* stack of errors */
- xmlRelaxNGValidStatePtr state; /* the current validation state */
- xmlRelaxNGStatesPtr states; /* the accumulated state list */
- xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
- int freeStatesNr;
- int freeStatesMax;
- xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
- /*
- * This is used for "progressive" validation
- */
- xmlRegExecCtxtPtr elem; /* the current element regexp */
- int elemNr; /* the number of element validated */
- int elemMax; /* the max depth of elements */
- xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */
- int pstate; /* progressive state */
- xmlNodePtr pnode; /* the current node */
- xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */
- int perr; /* signal error in content model
- * outside the regexp */
- };
- /**
- * xmlRelaxNGInclude:
- *
- * Structure associated to a RelaxNGs document element
- */
- struct _xmlRelaxNGInclude {
- xmlRelaxNGIncludePtr next; /* keep a chain of includes */
- xmlChar *href; /* the normalized href value */
- xmlDocPtr doc; /* the associated XML document */
- xmlRelaxNGDefinePtr content; /* the definitions */
- xmlRelaxNGPtr schema; /* the schema */
- };
- /**
- * xmlRelaxNGDocument:
- *
- * Structure associated to a RelaxNGs document element
- */
- struct _xmlRelaxNGDocument {
- xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
- xmlChar *href; /* the normalized href value */
- xmlDocPtr doc; /* the associated XML document */
- xmlRelaxNGDefinePtr content; /* the definitions */
- xmlRelaxNGPtr schema; /* the schema */
- int externalRef; /* 1 if an external ref */
- };
- /************************************************************************
- * *
- * Some factorized error routines *
- * *
- ************************************************************************/
- /**
- * xmlRngPErrMemory:
- * @ctxt: an Relax-NG parser context
- * @extra: extra information
- *
- * Handle a redefinition of attribute error
- */
- static void
- xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra)
- {
- xmlStructuredErrorFunc schannel = NULL;
- xmlGenericErrorFunc channel = NULL;
- void *data = NULL;
- if (ctxt != NULL) {
- if (ctxt->serror != NULL)
- schannel = ctxt->serror;
- else
- channel = ctxt->error;
- data = ctxt->userData;
- ctxt->nbErrors++;
- }
- if (extra)
- __xmlRaiseError(schannel, channel, data,
- NULL, NULL, XML_FROM_RELAXNGP,
- XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
- NULL, NULL, 0, 0,
- "Memory allocation failed : %s\n", extra);
- else
- __xmlRaiseError(schannel, channel, data,
- NULL, NULL, XML_FROM_RELAXNGP,
- XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
- NULL, NULL, 0, 0, "Memory allocation failed\n");
- }
- /**
- * xmlRngVErrMemory:
- * @ctxt: a Relax-NG validation context
- * @extra: extra information
- *
- * Handle a redefinition of attribute error
- */
- static void
- xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra)
- {
- xmlStructuredErrorFunc schannel = NULL;
- xmlGenericErrorFunc channel = NULL;
- void *data = NULL;
- if (ctxt != NULL) {
- if (ctxt->serror != NULL)
- schannel = ctxt->serror;
- else
- channel = ctxt->error;
- data = ctxt->userData;
- ctxt->nbErrors++;
- }
- if (extra)
- __xmlRaiseError(schannel, channel, data,
- NULL, NULL, XML_FROM_RELAXNGV,
- XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
- NULL, NULL, 0, 0,
- "Memory allocation failed : %s\n", extra);
- else
- __xmlRaiseError(schannel, channel, data,
- NULL, NULL, XML_FROM_RELAXNGV,
- XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
- NULL, NULL, 0, 0, "Memory allocation failed\n");
- }
- /**
- * xmlRngPErr:
- * @ctxt: a Relax-NG parser context
- * @node: the node raising the error
- * @error: the error code
- * @msg: message
- * @str1: extra info
- * @str2: extra info
- *
- * Handle a Relax NG Parsing error
- */
- static void LIBXML_ATTR_FORMAT(4,0)
- xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error,
- const char *msg, const xmlChar * str1, const xmlChar * str2)
- {
- xmlStructuredErrorFunc schannel = NULL;
- xmlGenericErrorFunc channel = NULL;
- void *data = NULL;
- if (ctxt != NULL) {
- if (ctxt->serror != NULL)
- schannel = ctxt->serror;
- else
- channel = ctxt->error;
- data = ctxt->userData;
- ctxt->nbErrors++;
- }
- __xmlRaiseError(schannel, channel, data,
- NULL, node, XML_FROM_RELAXNGP,
- error, XML_ERR_ERROR, NULL, 0,
- (const char *) str1, (const char *) str2, NULL, 0, 0,
- msg, str1, str2);
- }
- /**
- * xmlRngVErr:
- * @ctxt: a Relax-NG validation context
- * @node: the node raising the error
- * @error: the error code
- * @msg: message
- * @str1: extra info
- * @str2: extra info
- *
- * Handle a Relax NG Validation error
- */
- static void LIBXML_ATTR_FORMAT(4,0)
- xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error,
- const char *msg, const xmlChar * str1, const xmlChar * str2)
- {
- xmlStructuredErrorFunc schannel = NULL;
- xmlGenericErrorFunc channel = NULL;
- void *data = NULL;
- if (ctxt != NULL) {
- if (ctxt->serror != NULL)
- schannel = ctxt->serror;
- else
- channel = ctxt->error;
- data = ctxt->userData;
- ctxt->nbErrors++;
- }
- __xmlRaiseError(schannel, channel, data,
- NULL, node, XML_FROM_RELAXNGV,
- error, XML_ERR_ERROR, NULL, 0,
- (const char *) str1, (const char *) str2, NULL, 0, 0,
- msg, str1, str2);
- }
- /************************************************************************
- * *
- * Preliminary type checking interfaces *
- * *
- ************************************************************************/
- /**
- * xmlRelaxNGTypeHave:
- * @data: data needed for the library
- * @type: the type name
- * @value: the value to check
- *
- * Function provided by a type library to check if a type is exported
- *
- * Returns 1 if yes, 0 if no and -1 in case of error.
- */
- typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type);
- /**
- * xmlRelaxNGTypeCheck:
- * @data: data needed for the library
- * @type: the type name
- * @value: the value to check
- * @result: place to store the result if needed
- *
- * Function provided by a type library to check if a value match a type
- *
- * Returns 1 if yes, 0 if no and -1 in case of error.
- */
- typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type,
- const xmlChar * value, void **result,
- xmlNodePtr node);
- /**
- * xmlRelaxNGFacetCheck:
- * @data: data needed for the library
- * @type: the type name
- * @facet: the facet name
- * @val: the facet value
- * @strval: the string value
- * @value: the value to check
- *
- * Function provided by a type library to check a value facet
- *
- * Returns 1 if yes, 0 if no and -1 in case of error.
- */
- typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type,
- const xmlChar * facet,
- const xmlChar * val,
- const xmlChar * strval, void *value);
- /**
- * xmlRelaxNGTypeFree:
- * @data: data needed for the library
- * @result: the value to free
- *
- * Function provided by a type library to free a returned result
- */
- typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
- /**
- * xmlRelaxNGTypeCompare:
- * @data: data needed for the library
- * @type: the type name
- * @value1: the first value
- * @value2: the second value
- *
- * Function provided by a type library to compare two values accordingly
- * to a type.
- *
- * Returns 1 if yes, 0 if no and -1 in case of error.
- */
- typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type,
- const xmlChar * value1,
- xmlNodePtr ctxt1,
- void *comp1,
- const xmlChar * value2,
- xmlNodePtr ctxt2);
- typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
- typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
- struct _xmlRelaxNGTypeLibrary {
- const xmlChar *namespace; /* the datatypeLibrary value */
- void *data; /* data needed for the library */
- xmlRelaxNGTypeHave have; /* the export function */
- xmlRelaxNGTypeCheck check; /* the checking function */
- xmlRelaxNGTypeCompare comp; /* the compare function */
- xmlRelaxNGFacetCheck facet; /* the facet check function */
- xmlRelaxNGTypeFree freef; /* the freeing function */
- };
- /************************************************************************
- * *
- * Allocation functions *
- * *
- ************************************************************************/
- static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
- static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
- static void xmlRelaxNGNormExtSpace(xmlChar * value);
- static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
- static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt
- ATTRIBUTE_UNUSED,
- xmlRelaxNGValidStatePtr state1,
- xmlRelaxNGValidStatePtr state2);
- static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGValidStatePtr state);
- /**
- * xmlRelaxNGFreeDocument:
- * @docu: a document structure
- *
- * Deallocate a RelaxNG document structure.
- */
- static void
- xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
- {
- if (docu == NULL)
- return;
- if (docu->href != NULL)
- xmlFree(docu->href);
- if (docu->doc != NULL)
- xmlFreeDoc(docu->doc);
- if (docu->schema != NULL)
- xmlRelaxNGFreeInnerSchema(docu->schema);
- xmlFree(docu);
- }
- /**
- * xmlRelaxNGFreeDocumentList:
- * @docu: a list of document structure
- *
- * Deallocate a RelaxNG document structures.
- */
- static void
- xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
- {
- xmlRelaxNGDocumentPtr next;
- while (docu != NULL) {
- next = docu->next;
- xmlRelaxNGFreeDocument(docu);
- docu = next;
- }
- }
- /**
- * xmlRelaxNGFreeInclude:
- * @incl: a include structure
- *
- * Deallocate a RelaxNG include structure.
- */
- static void
- xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
- {
- if (incl == NULL)
- return;
- if (incl->href != NULL)
- xmlFree(incl->href);
- if (incl->doc != NULL)
- xmlFreeDoc(incl->doc);
- if (incl->schema != NULL)
- xmlRelaxNGFree(incl->schema);
- xmlFree(incl);
- }
- /**
- * xmlRelaxNGFreeIncludeList:
- * @incl: a include structure list
- *
- * Deallocate a RelaxNG include structure.
- */
- static void
- xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
- {
- xmlRelaxNGIncludePtr next;
- while (incl != NULL) {
- next = incl->next;
- xmlRelaxNGFreeInclude(incl);
- incl = next;
- }
- }
- /**
- * xmlRelaxNGNewRelaxNG:
- * @ctxt: a Relax-NG validation context (optional)
- *
- * Allocate a new RelaxNG structure.
- *
- * Returns the newly allocated structure or NULL in case or error
- */
- static xmlRelaxNGPtr
- xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
- {
- xmlRelaxNGPtr ret;
- ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
- if (ret == NULL) {
- xmlRngPErrMemory(ctxt, NULL);
- return (NULL);
- }
- memset(ret, 0, sizeof(xmlRelaxNG));
- return (ret);
- }
- /**
- * xmlRelaxNGFreeInnerSchema:
- * @schema: a schema structure
- *
- * Deallocate a RelaxNG schema structure.
- */
- static void
- xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
- {
- if (schema == NULL)
- return;
- if (schema->doc != NULL)
- xmlFreeDoc(schema->doc);
- if (schema->defTab != NULL) {
- int i;
- for (i = 0; i < schema->defNr; i++)
- xmlRelaxNGFreeDefine(schema->defTab[i]);
- xmlFree(schema->defTab);
- }
- xmlFree(schema);
- }
- /**
- * xmlRelaxNGFree:
- * @schema: a schema structure
- *
- * Deallocate a RelaxNG structure.
- */
- void
- xmlRelaxNGFree(xmlRelaxNGPtr schema)
- {
- if (schema == NULL)
- return;
- if (schema->topgrammar != NULL)
- xmlRelaxNGFreeGrammar(schema->topgrammar);
- if (schema->doc != NULL)
- xmlFreeDoc(schema->doc);
- if (schema->documents != NULL)
- xmlRelaxNGFreeDocumentList(schema->documents);
- if (schema->includes != NULL)
- xmlRelaxNGFreeIncludeList(schema->includes);
- if (schema->defTab != NULL) {
- int i;
- for (i = 0; i < schema->defNr; i++)
- xmlRelaxNGFreeDefine(schema->defTab[i]);
- xmlFree(schema->defTab);
- }
- xmlFree(schema);
- }
- /**
- * xmlRelaxNGNewGrammar:
- * @ctxt: a Relax-NG validation context (optional)
- *
- * Allocate a new RelaxNG grammar.
- *
- * Returns the newly allocated structure or NULL in case or error
- */
- static xmlRelaxNGGrammarPtr
- xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
- {
- xmlRelaxNGGrammarPtr ret;
- ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
- if (ret == NULL) {
- xmlRngPErrMemory(ctxt, NULL);
- return (NULL);
- }
- memset(ret, 0, sizeof(xmlRelaxNGGrammar));
- return (ret);
- }
- /**
- * xmlRelaxNGFreeGrammar:
- * @grammar: a grammar structure
- *
- * Deallocate a RelaxNG grammar structure.
- */
- static void
- xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
- {
- if (grammar == NULL)
- return;
- if (grammar->children != NULL) {
- xmlRelaxNGFreeGrammar(grammar->children);
- }
- if (grammar->next != NULL) {
- xmlRelaxNGFreeGrammar(grammar->next);
- }
- if (grammar->refs != NULL) {
- xmlHashFree(grammar->refs, NULL);
- }
- if (grammar->defs != NULL) {
- xmlHashFree(grammar->defs, NULL);
- }
- xmlFree(grammar);
- }
- /**
- * xmlRelaxNGNewDefine:
- * @ctxt: a Relax-NG validation context
- * @node: the node in the input document.
- *
- * Allocate a new RelaxNG define.
- *
- * Returns the newly allocated structure or NULL in case or error
- */
- static xmlRelaxNGDefinePtr
- xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
- {
- xmlRelaxNGDefinePtr ret;
- if (ctxt->defMax == 0) {
- ctxt->defMax = 16;
- ctxt->defNr = 0;
- ctxt->defTab = (xmlRelaxNGDefinePtr *)
- xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
- if (ctxt->defTab == NULL) {
- xmlRngPErrMemory(ctxt, "allocating define\n");
- return (NULL);
- }
- } else if (ctxt->defMax <= ctxt->defNr) {
- xmlRelaxNGDefinePtr *tmp;
- ctxt->defMax *= 2;
- tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
- ctxt->defMax *
- sizeof
- (xmlRelaxNGDefinePtr));
- if (tmp == NULL) {
- xmlRngPErrMemory(ctxt, "allocating define\n");
- return (NULL);
- }
- ctxt->defTab = tmp;
- }
- ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
- if (ret == NULL) {
- xmlRngPErrMemory(ctxt, "allocating define\n");
- return (NULL);
- }
- memset(ret, 0, sizeof(xmlRelaxNGDefine));
- ctxt->defTab[ctxt->defNr++] = ret;
- ret->node = node;
- ret->depth = -1;
- return (ret);
- }
- /**
- * xmlRelaxNGFreePartition:
- * @partitions: a partition set structure
- *
- * Deallocate RelaxNG partition set structures.
- */
- static void
- xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions)
- {
- xmlRelaxNGInterleaveGroupPtr group;
- int j;
- if (partitions != NULL) {
- if (partitions->groups != NULL) {
- for (j = 0; j < partitions->nbgroups; j++) {
- group = partitions->groups[j];
- if (group != NULL) {
- if (group->defs != NULL)
- xmlFree(group->defs);
- if (group->attrs != NULL)
- xmlFree(group->attrs);
- xmlFree(group);
- }
- }
- xmlFree(partitions->groups);
- }
- if (partitions->triage != NULL) {
- xmlHashFree(partitions->triage, NULL);
- }
- xmlFree(partitions);
- }
- }
- /**
- * xmlRelaxNGFreeDefine:
- * @define: a define structure
- *
- * Deallocate a RelaxNG define structure.
- */
- static void
- xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
- {
- if (define == NULL)
- return;
- if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) {
- xmlRelaxNGTypeLibraryPtr lib;
- lib = (xmlRelaxNGTypeLibraryPtr) define->data;
- if ((lib != NULL) && (lib->freef != NULL))
- lib->freef(lib->data, (void *) define->attrs);
- }
- if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE))
- xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
- if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE))
- xmlHashFree((xmlHashTablePtr) define->data, NULL);
- if (define->name != NULL)
- xmlFree(define->name);
- if (define->ns != NULL)
- xmlFree(define->ns);
- if (define->value != NULL)
- xmlFree(define->value);
- if (define->contModel != NULL)
- xmlRegFreeRegexp(define->contModel);
- xmlFree(define);
- }
- /**
- * xmlRelaxNGNewStates:
- * @ctxt: a Relax-NG validation context
- * @size: the default size for the container
- *
- * Allocate a new RelaxNG validation state container
- *
- * Returns the newly allocated structure or NULL in case or error
- */
- static xmlRelaxNGStatesPtr
- xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
- {
- xmlRelaxNGStatesPtr ret;
- if ((ctxt != NULL) &&
- (ctxt->freeStates != NULL) && (ctxt->freeStatesNr > 0)) {
- ctxt->freeStatesNr--;
- ret = ctxt->freeStates[ctxt->freeStatesNr];
- ret->nbState = 0;
- return (ret);
- }
- if (size < 16)
- size = 16;
- ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
- (size -
- 1) *
- sizeof(xmlRelaxNGValidStatePtr));
- if (ret == NULL) {
- xmlRngVErrMemory(ctxt, "allocating states\n");
- return (NULL);
- }
- ret->nbState = 0;
- ret->maxState = size;
- ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) *
- sizeof
- (xmlRelaxNGValidStatePtr));
- if (ret->tabState == NULL) {
- xmlRngVErrMemory(ctxt, "allocating states\n");
- xmlFree(ret);
- return (NULL);
- }
- return (ret);
- }
- /**
- * xmlRelaxNGAddStateUniq:
- * @ctxt: a Relax-NG validation context
- * @states: the states container
- * @state: the validation state
- *
- * Add a RelaxNG validation state to the container without checking
- * for unicity.
- *
- * Return 1 in case of success and 0 if this is a duplicate and -1 on error
- */
- static int
- xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGStatesPtr states,
- xmlRelaxNGValidStatePtr state)
- {
- if (state == NULL) {
- return (-1);
- }
- if (states->nbState >= states->maxState) {
- xmlRelaxNGValidStatePtr *tmp;
- int size;
- size = states->maxState * 2;
- tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
- (size) *
- sizeof
- (xmlRelaxNGValidStatePtr));
- if (tmp == NULL) {
- xmlRngVErrMemory(ctxt, "adding states\n");
- return (-1);
- }
- states->tabState = tmp;
- states->maxState = size;
- }
- states->tabState[states->nbState++] = state;
- return (1);
- }
- /**
- * xmlRelaxNGAddState:
- * @ctxt: a Relax-NG validation context
- * @states: the states container
- * @state: the validation state
- *
- * Add a RelaxNG validation state to the container
- *
- * Return 1 in case of success and 0 if this is a duplicate and -1 on error
- */
- static int
- xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGStatesPtr states,
- xmlRelaxNGValidStatePtr state)
- {
- int i;
- if (state == NULL || states == NULL) {
- return (-1);
- }
- if (states->nbState >= states->maxState) {
- xmlRelaxNGValidStatePtr *tmp;
- int size;
- size = states->maxState * 2;
- tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
- (size) *
- sizeof
- (xmlRelaxNGValidStatePtr));
- if (tmp == NULL) {
- xmlRngVErrMemory(ctxt, "adding states\n");
- return (-1);
- }
- states->tabState = tmp;
- states->maxState = size;
- }
- for (i = 0; i < states->nbState; i++) {
- if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
- xmlRelaxNGFreeValidState(ctxt, state);
- return (0);
- }
- }
- states->tabState[states->nbState++] = state;
- return (1);
- }
- /**
- * xmlRelaxNGFreeStates:
- * @ctxt: a Relax-NG validation context
- * @states: the container
- *
- * Free a RelaxNG validation state container
- */
- static void
- xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGStatesPtr states)
- {
- if (states == NULL)
- return;
- if ((ctxt != NULL) && (ctxt->freeStates == NULL)) {
- ctxt->freeStatesMax = 40;
- ctxt->freeStatesNr = 0;
- ctxt->freeStates = (xmlRelaxNGStatesPtr *)
- xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
- if (ctxt->freeStates == NULL) {
- xmlRngVErrMemory(ctxt, "storing states\n");
- }
- } else if ((ctxt != NULL)
- && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
- xmlRelaxNGStatesPtr *tmp;
- tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
- 2 * ctxt->freeStatesMax *
- sizeof
- (xmlRelaxNGStatesPtr));
- if (tmp == NULL) {
- xmlRngVErrMemory(ctxt, "storing states\n");
- xmlFree(states->tabState);
- xmlFree(states);
- return;
- }
- ctxt->freeStates = tmp;
- ctxt->freeStatesMax *= 2;
- }
- if ((ctxt == NULL) || (ctxt->freeStates == NULL)) {
- xmlFree(states->tabState);
- xmlFree(states);
- } else {
- ctxt->freeStates[ctxt->freeStatesNr++] = states;
- }
- }
- /**
- * xmlRelaxNGNewValidState:
- * @ctxt: a Relax-NG validation context
- * @node: the current node or NULL for the document
- *
- * Allocate a new RelaxNG validation state
- *
- * Returns the newly allocated structure or NULL in case or error
- */
- static xmlRelaxNGValidStatePtr
- xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
- {
- xmlRelaxNGValidStatePtr ret;
- xmlAttrPtr attr;
- xmlAttrPtr attrs[MAX_ATTR];
- int nbAttrs = 0;
- xmlNodePtr root = NULL;
- if (node == NULL) {
- root = xmlDocGetRootElement(ctxt->doc);
- if (root == NULL)
- return (NULL);
- } else {
- attr = node->properties;
- while (attr != NULL) {
- if (nbAttrs < MAX_ATTR)
- attrs[nbAttrs++] = attr;
- else
- nbAttrs++;
- attr = attr->next;
- }
- }
- if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
- ctxt->freeState->nbState--;
- ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
- } else {
- ret =
- (xmlRelaxNGValidStatePtr)
- xmlMalloc(sizeof(xmlRelaxNGValidState));
- if (ret == NULL) {
- xmlRngVErrMemory(ctxt, "allocating states\n");
- return (NULL);
- }
- memset(ret, 0, sizeof(xmlRelaxNGValidState));
- }
- ret->value = NULL;
- ret->endvalue = NULL;
- if (node == NULL) {
- ret->node = (xmlNodePtr) ctxt->doc;
- ret->seq = root;
- } else {
- ret->node = node;
- ret->seq = node->children;
- }
- ret->nbAttrs = 0;
- if (nbAttrs > 0) {
- if (ret->attrs == NULL) {
- if (nbAttrs < 4)
- ret->maxAttrs = 4;
- else
- ret->maxAttrs = nbAttrs;
- ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
- sizeof(xmlAttrPtr));
- if (ret->attrs == NULL) {
- xmlRngVErrMemory(ctxt, "allocating states\n");
- return (ret);
- }
- } else if (ret->maxAttrs < nbAttrs) {
- xmlAttrPtr *tmp;
- tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
- sizeof(xmlAttrPtr));
- if (tmp == NULL) {
- xmlRngVErrMemory(ctxt, "allocating states\n");
- return (ret);
- }
- ret->attrs = tmp;
- ret->maxAttrs = nbAttrs;
- }
- ret->nbAttrs = nbAttrs;
- if (nbAttrs < MAX_ATTR) {
- memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
- } else {
- attr = node->properties;
- nbAttrs = 0;
- while (attr != NULL) {
- ret->attrs[nbAttrs++] = attr;
- attr = attr->next;
- }
- }
- }
- ret->nbAttrLeft = ret->nbAttrs;
- return (ret);
- }
- /**
- * xmlRelaxNGCopyValidState:
- * @ctxt: a Relax-NG validation context
- * @state: a validation state
- *
- * Copy the validation state
- *
- * Returns the newly allocated structure or NULL in case or error
- */
- static xmlRelaxNGValidStatePtr
- xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGValidStatePtr state)
- {
- xmlRelaxNGValidStatePtr ret;
- unsigned int maxAttrs;
- xmlAttrPtr *attrs;
- if (state == NULL)
- return (NULL);
- if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
- ctxt->freeState->nbState--;
- ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
- } else {
- ret =
- (xmlRelaxNGValidStatePtr)
- xmlMalloc(sizeof(xmlRelaxNGValidState));
- if (ret == NULL) {
- xmlRngVErrMemory(ctxt, "allocating states\n");
- return (NULL);
- }
- memset(ret, 0, sizeof(xmlRelaxNGValidState));
- }
- attrs = ret->attrs;
- maxAttrs = ret->maxAttrs;
- memcpy(ret, state, sizeof(xmlRelaxNGValidState));
- ret->attrs = attrs;
- ret->maxAttrs = maxAttrs;
- if (state->nbAttrs > 0) {
- if (ret->attrs == NULL) {
- ret->maxAttrs = state->maxAttrs;
- ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
- sizeof(xmlAttrPtr));
- if (ret->attrs == NULL) {
- xmlRngVErrMemory(ctxt, "allocating states\n");
- ret->nbAttrs = 0;
- return (ret);
- }
- } else if (ret->maxAttrs < state->nbAttrs) {
- xmlAttrPtr *tmp;
- tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
- sizeof(xmlAttrPtr));
- if (tmp == NULL) {
- xmlRngVErrMemory(ctxt, "allocating states\n");
- ret->nbAttrs = 0;
- return (ret);
- }
- ret->maxAttrs = state->maxAttrs;
- ret->attrs = tmp;
- }
- memcpy(ret->attrs, state->attrs,
- state->nbAttrs * sizeof(xmlAttrPtr));
- }
- return (ret);
- }
- /**
- * xmlRelaxNGEqualValidState:
- * @ctxt: a Relax-NG validation context
- * @state1: a validation state
- * @state2: a validation state
- *
- * Compare the validation states for equality
- *
- * Returns 1 if equal, 0 otherwise
- */
- static int
- xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
- xmlRelaxNGValidStatePtr state1,
- xmlRelaxNGValidStatePtr state2)
- {
- int i;
- if ((state1 == NULL) || (state2 == NULL))
- return (0);
- if (state1 == state2)
- return (1);
- if (state1->node != state2->node)
- return (0);
- if (state1->seq != state2->seq)
- return (0);
- if (state1->nbAttrLeft != state2->nbAttrLeft)
- return (0);
- if (state1->nbAttrs != state2->nbAttrs)
- return (0);
- if (state1->endvalue != state2->endvalue)
- return (0);
- if ((state1->value != state2->value) &&
- (!xmlStrEqual(state1->value, state2->value)))
- return (0);
- for (i = 0; i < state1->nbAttrs; i++) {
- if (state1->attrs[i] != state2->attrs[i])
- return (0);
- }
- return (1);
- }
- /**
- * xmlRelaxNGFreeValidState:
- * @state: a validation state structure
- *
- * Deallocate a RelaxNG validation state structure.
- */
- static void
- xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGValidStatePtr state)
- {
- if (state == NULL)
- return;
- if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
- ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
- }
- if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
- if (state->attrs != NULL)
- xmlFree(state->attrs);
- xmlFree(state);
- } else {
- xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
- }
- }
- /************************************************************************
- * *
- * Semi internal functions *
- * *
- ************************************************************************/
- /**
- * xmlRelaxParserSetFlag:
- * @ctxt: a RelaxNG parser context
- * @flags: a set of flags values
- *
- * Semi private function used to pass information to a parser context
- * which are a combination of xmlRelaxNGParserFlag .
- *
- * Returns 0 if success and -1 in case of error
- */
- int
- xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags)
- {
- if (ctxt == NULL) return(-1);
- if (flags & XML_RELAXNGP_FREE_DOC) {
- ctxt->crng |= XML_RELAXNGP_FREE_DOC;
- flags -= XML_RELAXNGP_FREE_DOC;
- }
- if (flags & XML_RELAXNGP_CRNG) {
- ctxt->crng |= XML_RELAXNGP_CRNG;
- flags -= XML_RELAXNGP_CRNG;
- }
- if (flags != 0) return(-1);
- return(0);
- }
- /************************************************************************
- * *
- * Document functions *
- * *
- ************************************************************************/
- static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
- xmlDocPtr doc);
- /**
- * xmlRelaxNGIncludePush:
- * @ctxt: the parser context
- * @value: the element doc
- *
- * Pushes a new include on top of the include stack
- *
- * Returns 0 in case of error, the index in the stack otherwise
- */
- static int
- xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
- xmlRelaxNGIncludePtr value)
- {
- if (ctxt->incTab == NULL) {
- ctxt->incMax = 4;
- ctxt->incNr = 0;
- ctxt->incTab =
- (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax *
- sizeof(ctxt->incTab[0]));
- if (ctxt->incTab == NULL) {
- xmlRngPErrMemory(ctxt, "allocating include\n");
- return (0);
- }
- }
- if (ctxt->incNr >= ctxt->incMax) {
- ctxt->incMax *= 2;
- ctxt->incTab =
- (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
- ctxt->incMax *
- sizeof(ctxt->incTab[0]));
- if (ctxt->incTab == NULL) {
- xmlRngPErrMemory(ctxt, "allocating include\n");
- return (0);
- }
- }
- ctxt->incTab[ctxt->incNr] = value;
- ctxt->inc = value;
- return (ctxt->incNr++);
- }
- /**
- * xmlRelaxNGIncludePop:
- * @ctxt: the parser context
- *
- * Pops the top include from the include stack
- *
- * Returns the include just removed
- */
- static xmlRelaxNGIncludePtr
- xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
- {
- xmlRelaxNGIncludePtr ret;
- if (ctxt->incNr <= 0)
- return (NULL);
- ctxt->incNr--;
- if (ctxt->incNr > 0)
- ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
- else
- ctxt->inc = NULL;
- ret = ctxt->incTab[ctxt->incNr];
- ctxt->incTab[ctxt->incNr] = NULL;
- return (ret);
- }
- /**
- * xmlRelaxNGRemoveRedefine:
- * @ctxt: the parser context
- * @URL: the normalized URL
- * @target: the included target
- * @name: the define name to eliminate
- *
- * Applies the elimination algorithm of 4.7
- *
- * Returns 0 in case of error, 1 in case of success.
- */
- static int
- xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
- const xmlChar * URL ATTRIBUTE_UNUSED,
- xmlNodePtr target, const xmlChar * name)
- {
- int found = 0;
- xmlNodePtr tmp, tmp2;
- xmlChar *name2;
- #ifdef DEBUG_INCLUDE
- if (name == NULL)
- xmlGenericError(xmlGenericErrorContext,
- "Elimination of <include> start from %s\n", URL);
- else
- xmlGenericError(xmlGenericErrorContext,
- "Elimination of <include> define %s from %s\n",
- name, URL);
- #endif
- tmp = target;
- while (tmp != NULL) {
- tmp2 = tmp->next;
- if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
- found = 1;
- xmlUnlinkNode(tmp);
- xmlFreeNode(tmp);
- } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
- name2 = xmlGetProp(tmp, BAD_CAST "name");
- xmlRelaxNGNormExtSpace(name2);
- if (name2 != NULL) {
- if (xmlStrEqual(name, name2)) {
- found = 1;
- xmlUnlinkNode(tmp);
- xmlFreeNode(tmp);
- }
- xmlFree(name2);
- }
- } else if (IS_RELAXNG(tmp, "include")) {
- xmlChar *href = NULL;
- xmlRelaxNGDocumentPtr inc = tmp->psvi;
- if ((inc != NULL) && (inc->doc != NULL) &&
- (inc->doc->children != NULL)) {
- if (xmlStrEqual
- (inc->doc->children->name, BAD_CAST "grammar")) {
- #ifdef DEBUG_INCLUDE
- href = xmlGetProp(tmp, BAD_CAST "href");
- #endif
- if (xmlRelaxNGRemoveRedefine(ctxt, href,
- xmlDocGetRootElement(inc->doc)->children,
- name) == 1) {
- found = 1;
- }
- #ifdef DEBUG_INCLUDE
- if (href != NULL)
- xmlFree(href);
- #endif
- }
- }
- if (xmlRelaxNGRemoveRedefine(ctxt, URL, tmp->children, name) == 1) {
- found = 1;
- }
- }
- tmp = tmp2;
- }
- return (found);
- }
- /**
- * xmlRelaxNGLoadInclude:
- * @ctxt: the parser context
- * @URL: the normalized URL
- * @node: the include node.
- * @ns: the namespace passed from the context.
- *
- * First lookup if the document is already loaded into the parser context,
- * check against recursion. If not found the resource is loaded and
- * the content is preprocessed before being returned back to the caller.
- *
- * Returns the xmlRelaxNGIncludePtr or NULL in case of error
- */
- static xmlRelaxNGIncludePtr
- xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL,
- xmlNodePtr node, const xmlChar * ns)
- {
- xmlRelaxNGIncludePtr ret = NULL;
- xmlDocPtr doc;
- int i;
- xmlNodePtr root, cur;
- #ifdef DEBUG_INCLUDE
- xmlGenericError(xmlGenericErrorContext,
- "xmlRelaxNGLoadInclude(%s)\n", URL);
- #endif
- /*
- * check against recursion in the stack
- */
- for (i = 0; i < ctxt->incNr; i++) {
- if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
- xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE,
- "Detected an Include recursion for %s\n", URL,
- NULL);
- return (NULL);
- }
- }
- /*
- * load the document
- */
- doc = xmlReadFile((const char *) URL,NULL,0);
- if (doc == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR,
- "xmlRelaxNG: could not load %s\n", URL, NULL);
- return (NULL);
- }
- #ifdef DEBUG_INCLUDE
- xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL);
- #endif
- /*
- * Allocate the document structures and register it first.
- */
- ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
- if (ret == NULL) {
- xmlRngPErrMemory(ctxt, "allocating include\n");
- xmlFreeDoc(doc);
- return (NULL);
- }
- memset(ret, 0, sizeof(xmlRelaxNGInclude));
- ret->doc = doc;
- ret->href = xmlStrdup(URL);
- ret->next = ctxt->includes;
- ctxt->includes = ret;
- /*
- * transmit the ns if needed
- */
- if (ns != NULL) {
- root = xmlDocGetRootElement(doc);
- if (root != NULL) {
- if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
- xmlSetProp(root, BAD_CAST "ns", ns);
- }
- }
- }
- /*
- * push it on the stack
- */
- xmlRelaxNGIncludePush(ctxt, ret);
- /*
- * Some preprocessing of the document content, this include recursing
- * in the include stack.
- */
- #ifdef DEBUG_INCLUDE
- xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL);
- #endif
- doc = xmlRelaxNGCleanupDoc(ctxt, doc);
- if (doc == NULL) {
- ctxt->inc = NULL;
- return (NULL);
- }
- /*
- * Pop up the include from the stack
- */
- xmlRelaxNGIncludePop(ctxt);
- #ifdef DEBUG_INCLUDE
- xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL);
- #endif
- /*
- * Check that the top element is a grammar
- */
- root = xmlDocGetRootElement(doc);
- if (root == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_EMPTY,
- "xmlRelaxNG: included document is empty %s\n", URL,
- NULL);
- return (NULL);
- }
- if (!IS_RELAXNG(root, "grammar")) {
- xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
- "xmlRelaxNG: included document %s root is not a grammar\n",
- URL, NULL);
- return (NULL);
- }
- /*
- * Elimination of redefined rules in the include.
- */
- cur = node->children;
- while (cur != NULL) {
- if (IS_RELAXNG(cur, "start")) {
- int found = 0;
- found =
- xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
- if (!found) {
- xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING,
- "xmlRelaxNG: include %s has a start but not the included grammar\n",
- URL, NULL);
- }
- } else if (IS_RELAXNG(cur, "define")) {
- xmlChar *name;
- name = xmlGetProp(cur, BAD_CAST "name");
- if (name == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING,
- "xmlRelaxNG: include %s has define without name\n",
- URL, NULL);
- } else {
- int found;
- xmlRelaxNGNormExtSpace(name);
- found = xmlRelaxNGRemoveRedefine(ctxt, URL,
- root->children, name);
- if (!found) {
- xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING,
- "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
- URL, name);
- }
- xmlFree(name);
- }
- }
- if (IS_RELAXNG(cur, "div") && cur->children != NULL) {
- cur = cur->children;
- } else {
- if (cur->next != NULL) {
- cur = cur->next;
- } else {
- while (cur->parent != node && cur->parent->next == NULL) {
- cur = cur->parent;
- }
- cur = cur->parent != node ? cur->parent->next : NULL;
- }
- }
- }
- return (ret);
- }
- /**
- * xmlRelaxNGValidErrorPush:
- * @ctxt: the validation context
- * @err: the error code
- * @arg1: the first string argument
- * @arg2: the second string argument
- * @dup: arg need to be duplicated
- *
- * Pushes a new error on top of the error stack
- *
- * Returns 0 in case of error, the index in the stack otherwise
- */
- static int
- xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGValidErr err, const xmlChar * arg1,
- const xmlChar * arg2, int dup)
- {
- xmlRelaxNGValidErrorPtr cur;
- #ifdef DEBUG_ERROR
- xmlGenericError(xmlGenericErrorContext,
- "Pushing error %d at %d on stack\n", err, ctxt->errNr);
- #endif
- if (ctxt->errTab == NULL) {
- ctxt->errMax = 8;
- ctxt->errNr = 0;
- ctxt->errTab =
- (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax *
- sizeof
- (xmlRelaxNGValidError));
- if (ctxt->errTab == NULL) {
- xmlRngVErrMemory(ctxt, "pushing error\n");
- return (0);
- }
- ctxt->err = NULL;
- }
- if (ctxt->errNr >= ctxt->errMax) {
- ctxt->errMax *= 2;
- ctxt->errTab =
- (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
- ctxt->errMax *
- sizeof
- (xmlRelaxNGValidError));
- if (ctxt->errTab == NULL) {
- xmlRngVErrMemory(ctxt, "pushing error\n");
- return (0);
- }
- ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
- }
- if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
- (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err))
- return (ctxt->errNr);
- cur = &ctxt->errTab[ctxt->errNr];
- cur->err = err;
- if (dup) {
- cur->arg1 = xmlStrdup(arg1);
- cur->arg2 = xmlStrdup(arg2);
- cur->flags = ERROR_IS_DUP;
- } else {
- cur->arg1 = arg1;
- cur->arg2 = arg2;
- cur->flags = 0;
- }
- if (ctxt->state != NULL) {
- cur->node = ctxt->state->node;
- cur->seq = ctxt->state->seq;
- } else {
- cur->node = NULL;
- cur->seq = NULL;
- }
- ctxt->err = cur;
- return (ctxt->errNr++);
- }
- /**
- * xmlRelaxNGValidErrorPop:
- * @ctxt: the validation context
- *
- * Pops the top error from the error stack
- */
- static void
- xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
- {
- xmlRelaxNGValidErrorPtr cur;
- if (ctxt->errNr <= 0) {
- ctxt->err = NULL;
- return;
- }
- ctxt->errNr--;
- if (ctxt->errNr > 0)
- ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
- else
- ctxt->err = NULL;
- cur = &ctxt->errTab[ctxt->errNr];
- if (cur->flags & ERROR_IS_DUP) {
- if (cur->arg1 != NULL)
- xmlFree((xmlChar *) cur->arg1);
- cur->arg1 = NULL;
- if (cur->arg2 != NULL)
- xmlFree((xmlChar *) cur->arg2);
- cur->arg2 = NULL;
- cur->flags = 0;
- }
- }
- /**
- * xmlRelaxNGDocumentPush:
- * @ctxt: the parser context
- * @value: the element doc
- *
- * Pushes a new doc on top of the doc stack
- *
- * Returns 0 in case of error, the index in the stack otherwise
- */
- static int
- xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
- xmlRelaxNGDocumentPtr value)
- {
- if (ctxt->docTab == NULL) {
- ctxt->docMax = 4;
- ctxt->docNr = 0;
- ctxt->docTab =
- (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax *
- sizeof(ctxt->docTab[0]));
- if (ctxt->docTab == NULL) {
- xmlRngPErrMemory(ctxt, "adding document\n");
- return (0);
- }
- }
- if (ctxt->docNr >= ctxt->docMax) {
- ctxt->docMax *= 2;
- ctxt->docTab =
- (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
- ctxt->docMax *
- sizeof(ctxt->docTab[0]));
- if (ctxt->docTab == NULL) {
- xmlRngPErrMemory(ctxt, "adding document\n");
- return (0);
- }
- }
- ctxt->docTab[ctxt->docNr] = value;
- ctxt->doc = value;
- return (ctxt->docNr++);
- }
- /**
- * xmlRelaxNGDocumentPop:
- * @ctxt: the parser context
- *
- * Pops the top doc from the doc stack
- *
- * Returns the doc just removed
- */
- static xmlRelaxNGDocumentPtr
- xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
- {
- xmlRelaxNGDocumentPtr ret;
- if (ctxt->docNr <= 0)
- return (NULL);
- ctxt->docNr--;
- if (ctxt->docNr > 0)
- ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
- else
- ctxt->doc = NULL;
- ret = ctxt->docTab[ctxt->docNr];
- ctxt->docTab[ctxt->docNr] = NULL;
- return (ret);
- }
- /**
- * xmlRelaxNGLoadExternalRef:
- * @ctxt: the parser context
- * @URL: the normalized URL
- * @ns: the inherited ns if any
- *
- * First lookup if the document is already loaded into the parser context,
- * check against recursion. If not found the resource is loaded and
- * the content is preprocessed before being returned back to the caller.
- *
- * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
- */
- static xmlRelaxNGDocumentPtr
- xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt,
- const xmlChar * URL, const xmlChar * ns)
- {
- xmlRelaxNGDocumentPtr ret = NULL;
- xmlDocPtr doc;
- xmlNodePtr root;
- int i;
- /*
- * check against recursion in the stack
- */
- for (i = 0; i < ctxt->docNr; i++) {
- if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
- xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE,
- "Detected an externalRef recursion for %s\n", URL,
- NULL);
- return (NULL);
- }
- }
- /*
- * load the document
- */
- doc = xmlReadFile((const char *) URL,NULL,0);
- if (doc == NULL) {
- xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
- "xmlRelaxNG: could not load %s\n", URL, NULL);
- return (NULL);
- }
- /*
- * Allocate the document structures and register it first.
- */
- ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
- if (ret == NULL) {
- xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY,
- "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL);
- xmlFreeDoc(doc);
- return (NULL);
- }
- memset(ret, 0, sizeof(xmlRelaxNGDocument));
- ret->doc = doc;
- ret->href = xmlStrdup(URL);
- ret->next = ctxt->documents;
- ret->externalRef = 1;
- ctxt->documents = ret;
- /*
- * transmit the ns if needed
- */
- if (ns != NULL) {
- root = xmlDocGetRootElement(doc);
- if (root != NULL) {
- if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
- xmlSetProp(root, BAD_CAST "ns", ns);
- }
- }
- }
- /*
- * push it on the stack and register it in the hash table
- */
- xmlRelaxNGDocumentPush(ctxt, ret);
- /*
- * Some preprocessing of the document content
- */
- doc = xmlRelaxNGCleanupDoc(ctxt, doc);
- if (doc == NULL) {
- ctxt->doc = NULL;
- return (NULL);
- }
- xmlRelaxNGDocumentPop(ctxt);
- return (ret);
- }
- /************************************************************************
- * *
- * Error functions *
- * *
- ************************************************************************/
- #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
- #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
- #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
- #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
- #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
- static const char *
- xmlRelaxNGDefName(xmlRelaxNGDefinePtr def)
- {
- if (def == NULL)
- return ("none");
- switch (def->type) {
- case XML_RELAXNG_EMPTY:
- return ("empty");
- case XML_RELAXNG_NOT_ALLOWED:
- return ("notAllowed");
- case XML_RELAXNG_EXCEPT:
- return ("except");
- case XML_RELAXNG_TEXT:
- return ("text");
- case XML_RELAXNG_ELEMENT:
- return ("element");
- case XML_RELAXNG_DATATYPE:
- return ("datatype");
- case XML_RELAXNG_VALUE:
- return ("value");
- case XML_RELAXNG_LIST:
- return ("list");
- case XML_RELAXNG_ATTRIBUTE:
- return ("attribute");
- case XML_RELAXNG_DEF:
- return ("def");
- case XML_RELAXNG_REF:
- return ("ref");
- case XML_RELAXNG_EXTERNALREF:
- return ("externalRef");
- case XML_RELAXNG_PARENTREF:
- return ("parentRef");
- case XML_RELAXNG_OPTIONAL:
- return ("optional");
- case XML_RELAXNG_ZEROORMORE:
- return ("zeroOrMore");
- case XML_RELAXNG_ONEORMORE:
- return ("oneOrMore");
- case XML_RELAXNG_CHOICE:
- return ("choice");
- case XML_RELAXNG_GROUP:
- return ("group");
- case XML_RELAXNG_INTERLEAVE:
- return ("interleave");
- case XML_RELAXNG_START:
- return ("start");
- case XML_RELAXNG_NOOP:
- return ("noop");
- case XML_RELAXNG_PARAM:
- return ("param");
- }
- return ("unknown");
- }
- /**
- * xmlRelaxNGGetErrorString:
- * @err: the error code
- * @arg1: the first string argument
- * @arg2: the second string argument
- *
- * computes a formatted error string for the given error code and args
- *
- * Returns the error string, it must be deallocated by the caller
- */
- static xmlChar *
- xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1,
- const xmlChar * arg2)
- {
- char msg[1000];
- xmlChar *result;
- if (arg1 == NULL)
- arg1 = BAD_CAST "";
- if (arg2 == NULL)
- arg2 = BAD_CAST "";
- msg[0] = 0;
- switch (err) {
- case XML_RELAXNG_OK:
- return (NULL);
- case XML_RELAXNG_ERR_MEMORY:
- return (xmlCharStrdup("out of memory\n"));
- case XML_RELAXNG_ERR_TYPE:
- snprintf(msg, 1000, "failed to validate type %s\n", arg1);
- break;
- case XML_RELAXNG_ERR_TYPEVAL:
- snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1,
- arg2);
- break;
- case XML_RELAXNG_ERR_DUPID:
- snprintf(msg, 1000, "ID %s redefined\n", arg1);
- break;
- case XML_RELAXNG_ERR_TYPECMP:
- snprintf(msg, 1000, "failed to compare type %s\n", arg1);
- break;
- case XML_RELAXNG_ERR_NOSTATE:
- return (xmlCharStrdup("Internal error: no state\n"));
- case XML_RELAXNG_ERR_NODEFINE:
- return (xmlCharStrdup("Internal error: no define\n"));
- case XML_RELAXNG_ERR_INTERNAL:
- snprintf(msg, 1000, "Internal error: %s\n", arg1);
- break;
- case XML_RELAXNG_ERR_LISTEXTRA:
- snprintf(msg, 1000, "Extra data in list: %s\n", arg1);
- break;
- case XML_RELAXNG_ERR_INTERNODATA:
- return (xmlCharStrdup
- ("Internal: interleave block has no data\n"));
- case XML_RELAXNG_ERR_INTERSEQ:
- return (xmlCharStrdup("Invalid sequence in interleave\n"));
- case XML_RELAXNG_ERR_INTEREXTRA:
- snprintf(msg, 1000, "Extra element %s in interleave\n", arg1);
- break;
- case XML_RELAXNG_ERR_ELEMNAME:
- snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1,
- arg2);
- break;
- case XML_RELAXNG_ERR_ELEMNONS:
- snprintf(msg, 1000, "Expecting a namespace for element %s\n",
- arg1);
- break;
- case XML_RELAXNG_ERR_ELEMWRONGNS:
- snprintf(msg, 1000,
- "Element %s has wrong namespace: expecting %s\n", arg1,
- arg2);
- break;
- case XML_RELAXNG_ERR_ELEMWRONG:
- snprintf(msg, 1000, "Did not expect element %s there\n", arg1);
- break;
- case XML_RELAXNG_ERR_TEXTWRONG:
- snprintf(msg, 1000,
- "Did not expect text in element %s content\n", arg1);
- break;
- case XML_RELAXNG_ERR_ELEMEXTRANS:
- snprintf(msg, 1000, "Expecting no namespace for element %s\n",
- arg1);
- break;
- case XML_RELAXNG_ERR_ELEMNOTEMPTY:
- snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1);
- break;
- case XML_RELAXNG_ERR_NOELEM:
- snprintf(msg, 1000, "Expecting an element %s, got nothing\n",
- arg1);
- break;
- case XML_RELAXNG_ERR_NOTELEM:
- return (xmlCharStrdup("Expecting an element got text\n"));
- case XML_RELAXNG_ERR_ATTRVALID:
- snprintf(msg, 1000, "Element %s failed to validate attributes\n",
- arg1);
- break;
- case XML_RELAXNG_ERR_CONTENTVALID:
- snprintf(msg, 1000, "Element %s failed to validate content\n",
- arg1);
- break;
- case XML_RELAXNG_ERR_EXTRACONTENT:
- snprintf(msg, 1000, "Element %s has extra content: %s\n",
- arg1, arg2);
- break;
- case XML_RELAXNG_ERR_INVALIDATTR:
- snprintf(msg, 1000, "Invalid attribute %s for element %s\n",
- arg1, arg2);
- break;
- case XML_RELAXNG_ERR_LACKDATA:
- snprintf(msg, 1000, "Datatype element %s contains no data\n",
- arg1);
- break;
- case XML_RELAXNG_ERR_DATAELEM:
- snprintf(msg, 1000, "Datatype element %s has child elements\n",
- arg1);
- break;
- case XML_RELAXNG_ERR_VALELEM:
- snprintf(msg, 1000, "Value element %s has child elements\n",
- arg1);
- break;
- case XML_RELAXNG_ERR_LISTELEM:
- snprintf(msg, 1000, "List element %s has child elements\n",
- arg1);
- break;
- case XML_RELAXNG_ERR_DATATYPE:
- snprintf(msg, 1000, "Error validating datatype %s\n", arg1);
- break;
- case XML_RELAXNG_ERR_VALUE:
- snprintf(msg, 1000, "Error validating value %s\n", arg1);
- break;
- case XML_RELAXNG_ERR_LIST:
- return (xmlCharStrdup("Error validating list\n"));
- case XML_RELAXNG_ERR_NOGRAMMAR:
- return (xmlCharStrdup("No top grammar defined\n"));
- case XML_RELAXNG_ERR_EXTRADATA:
- return (xmlCharStrdup("Extra data in the document\n"));
- default:
- return (xmlCharStrdup("Unknown error !\n"));
- }
- if (msg[0] == 0) {
- snprintf(msg, 1000, "Unknown error code %d\n", err);
- }
- msg[1000 - 1] = 0;
- result = xmlCharStrdup(msg);
- return (xmlEscapeFormatString(&result));
- }
- /**
- * xmlRelaxNGShowValidError:
- * @ctxt: the validation context
- * @err: the error number
- * @node: the node
- * @child: the node child generating the problem.
- * @arg1: the first argument
- * @arg2: the second argument
- *
- * Show a validation error.
- */
- static void
- xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGValidErr err, xmlNodePtr node,
- xmlNodePtr child, const xmlChar * arg1,
- const xmlChar * arg2)
- {
- xmlChar *msg;
- if (ctxt->flags & FLAGS_NOERROR)
- return;
- #ifdef DEBUG_ERROR
- xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err);
- #endif
- msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
- if (msg == NULL)
- return;
- if (ctxt->errNo == XML_RELAXNG_OK)
- ctxt->errNo = err;
- xmlRngVErr(ctxt, (child == NULL ? node : child), err,
- (const char *) msg, arg1, arg2);
- xmlFree(msg);
- }
- /**
- * xmlRelaxNGPopErrors:
- * @ctxt: the validation context
- * @level: the error level in the stack
- *
- * pop and discard all errors until the given level is reached
- */
- static void
- xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level)
- {
- int i;
- xmlRelaxNGValidErrorPtr err;
- #ifdef DEBUG_ERROR
- xmlGenericError(xmlGenericErrorContext,
- "Pop errors till level %d\n", level);
- #endif
- for (i = level; i < ctxt->errNr; i++) {
- err = &ctxt->errTab[i];
- if (err->flags & ERROR_IS_DUP) {
- if (err->arg1 != NULL)
- xmlFree((xmlChar *) err->arg1);
- err->arg1 = NULL;
- if (err->arg2 != NULL)
- xmlFree((xmlChar *) err->arg2);
- err->arg2 = NULL;
- err->flags = 0;
- }
- }
- ctxt->errNr = level;
- if (ctxt->errNr <= 0)
- ctxt->err = NULL;
- }
- /**
- * xmlRelaxNGDumpValidError:
- * @ctxt: the validation context
- *
- * Show all validation error over a given index.
- */
- static void
- xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt)
- {
- int i, j, k;
- xmlRelaxNGValidErrorPtr err, dup;
- #ifdef DEBUG_ERROR
- xmlGenericError(xmlGenericErrorContext,
- "Dumping error stack %d errors\n", ctxt->errNr);
- #endif
- for (i = 0, k = 0; i < ctxt->errNr; i++) {
- err = &ctxt->errTab[i];
- if (k < MAX_ERROR) {
- for (j = 0; j < i; j++) {
- dup = &ctxt->errTab[j];
- if ((err->err == dup->err) && (err->node == dup->node) &&
- (xmlStrEqual(err->arg1, dup->arg1)) &&
- (xmlStrEqual(err->arg2, dup->arg2))) {
- goto skip;
- }
- }
- xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
- err->arg1, err->arg2);
- k++;
- }
- skip:
- if (err->flags & ERROR_IS_DUP) {
- if (err->arg1 != NULL)
- xmlFree((xmlChar *) err->arg1);
- err->arg1 = NULL;
- if (err->arg2 != NULL)
- xmlFree((xmlChar *) err->arg2);
- err->arg2 = NULL;
- err->flags = 0;
- }
- }
- ctxt->errNr = 0;
- }
- /**
- * xmlRelaxNGAddValidError:
- * @ctxt: the validation context
- * @err: the error number
- * @arg1: the first argument
- * @arg2: the second argument
- * @dup: need to dup the args
- *
- * Register a validation error, either generating it if it's sure
- * or stacking it for later handling if unsure.
- */
- static void
- xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGValidErr err, const xmlChar * arg1,
- const xmlChar * arg2, int dup)
- {
- if (ctxt == NULL)
- return;
- if (ctxt->flags & FLAGS_NOERROR)
- return;
- #ifdef DEBUG_ERROR
- xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err);
- #endif
- /*
- * generate the error directly
- */
- if (((ctxt->flags & FLAGS_IGNORABLE) == 0) ||
- (ctxt->flags & FLAGS_NEGATIVE)) {
- xmlNodePtr node, seq;
- /*
- * Flush first any stacked error which might be the
- * real cause of the problem.
- */
- if (ctxt->errNr != 0)
- xmlRelaxNGDumpValidError(ctxt);
- if (ctxt->state != NULL) {
- node = ctxt->state->node;
- seq = ctxt->state->seq;
- } else {
- node = seq = NULL;
- }
- if ((node == NULL) && (seq == NULL)) {
- node = ctxt->pnode;
- }
- xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
- }
- /*
- * Stack the error for later processing if needed
- */
- else {
- xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
- }
- }
- /************************************************************************
- * *
- * Type library hooks *
- * *
- ************************************************************************/
- static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
- const xmlChar * str);
- /**
- * xmlRelaxNGSchemaTypeHave:
- * @data: data needed for the library
- * @type: the type name
- *
- * Check if the given type is provided by
- * the W3C XMLSchema Datatype library.
- *
- * Returns 1 if yes, 0 if no and -1 in case of error.
- */
- static int
- xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type)
- {
- xmlSchemaTypePtr typ;
- if (type == NULL)
- return (-1);
- typ = xmlSchemaGetPredefinedType(type,
- BAD_CAST
- "http://www.w3.org/2001/XMLSchema");
- if (typ == NULL)
- return (0);
- return (1);
- }
- /**
- * xmlRelaxNGSchemaTypeCheck:
- * @data: data needed for the library
- * @type: the type name
- * @value: the value to check
- * @node: the node
- *
- * Check if the given type and value are validated by
- * the W3C XMLSchema Datatype library.
- *
- * Returns 1 if yes, 0 if no and -1 in case of error.
- */
- static int
- xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
- const xmlChar * type,
- const xmlChar * value,
- void **result, xmlNodePtr node)
- {
- xmlSchemaTypePtr typ;
- int ret;
- if ((type == NULL) || (value == NULL))
- return (-1);
- typ = xmlSchemaGetPredefinedType(type,
- BAD_CAST
- "http://www.w3.org/2001/XMLSchema");
- if (typ == NULL)
- return (-1);
- ret = xmlSchemaValPredefTypeNode(typ, value,
- (xmlSchemaValPtr *) result, node);
- if (ret == 2) /* special ID error code */
- return (2);
- if (ret == 0)
- return (1);
- if (ret > 0)
- return (0);
- return (-1);
- }
- /**
- * xmlRelaxNGSchemaFacetCheck:
- * @data: data needed for the library
- * @type: the type name
- * @facet: the facet name
- * @val: the facet value
- * @strval: the string value
- * @value: the value to check
- *
- * Function provided by a type library to check a value facet
- *
- * Returns 1 if yes, 0 if no and -1 in case of error.
- */
- static int
- xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED,
- const xmlChar * type, const xmlChar * facetname,
- const xmlChar * val, const xmlChar * strval,
- void *value)
- {
- xmlSchemaFacetPtr facet;
- xmlSchemaTypePtr typ;
- int ret;
- if ((type == NULL) || (strval == NULL))
- return (-1);
- typ = xmlSchemaGetPredefinedType(type,
- BAD_CAST
- "http://www.w3.org/2001/XMLSchema");
- if (typ == NULL)
- return (-1);
- facet = xmlSchemaNewFacet();
- if (facet == NULL)
- return (-1);
- if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
- facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
- } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
- facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
- } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
- facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
- } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
- facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
- } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
- facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
- } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
- facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
- } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
- facet->type = XML_SCHEMA_FACET_PATTERN;
- } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
- facet->type = XML_SCHEMA_FACET_ENUMERATION;
- } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
- facet->type = XML_SCHEMA_FACET_WHITESPACE;
- } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
- facet->type = XML_SCHEMA_FACET_LENGTH;
- } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
- facet->type = XML_SCHEMA_FACET_MAXLENGTH;
- } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
- facet->type = XML_SCHEMA_FACET_MINLENGTH;
- } else {
- xmlSchemaFreeFacet(facet);
- return (-1);
- }
- facet->value = val;
- ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
- if (ret != 0) {
- xmlSchemaFreeFacet(facet);
- return (-1);
- }
- ret = xmlSchemaValidateFacet(typ, facet, strval, value);
- xmlSchemaFreeFacet(facet);
- if (ret != 0)
- return (-1);
- return (0);
- }
- /**
- * xmlRelaxNGSchemaFreeValue:
- * @data: data needed for the library
- * @value: the value to free
- *
- * Function provided by a type library to free a Schemas value
- *
- * Returns 1 if yes, 0 if no and -1 in case of error.
- */
- static void
- xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value)
- {
- xmlSchemaFreeValue(value);
- }
- /**
- * xmlRelaxNGSchemaTypeCompare:
- * @data: data needed for the library
- * @type: the type name
- * @value1: the first value
- * @value2: the second value
- *
- * Compare two values for equality accordingly a type from the W3C XMLSchema
- * Datatype library.
- *
- * Returns 1 if equal, 0 if no and -1 in case of error.
- */
- static int
- xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
- const xmlChar * type,
- const xmlChar * value1,
- xmlNodePtr ctxt1,
- void *comp1,
- const xmlChar * value2, xmlNodePtr ctxt2)
- {
- int ret;
- xmlSchemaTypePtr typ;
- xmlSchemaValPtr res1 = NULL, res2 = NULL;
- if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
- return (-1);
- typ = xmlSchemaGetPredefinedType(type,
- BAD_CAST
- "http://www.w3.org/2001/XMLSchema");
- if (typ == NULL)
- return (-1);
- if (comp1 == NULL) {
- ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
- if (ret != 0)
- return (-1);
- if (res1 == NULL)
- return (-1);
- } else {
- res1 = (xmlSchemaValPtr) comp1;
- }
- ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
- if (ret != 0) {
- if (res1 != (xmlSchemaValPtr) comp1)
- xmlSchemaFreeValue(res1);
- return (-1);
- }
- ret = xmlSchemaCompareValues(res1, res2);
- if (res1 != (xmlSchemaValPtr) comp1)
- xmlSchemaFreeValue(res1);
- xmlSchemaFreeValue(res2);
- if (ret == -2)
- return (-1);
- if (ret == 0)
- return (1);
- return (0);
- }
- /**
- * xmlRelaxNGDefaultTypeHave:
- * @data: data needed for the library
- * @type: the type name
- *
- * Check if the given type is provided by
- * the default datatype library.
- *
- * Returns 1 if yes, 0 if no and -1 in case of error.
- */
- static int
- xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED,
- const xmlChar * type)
- {
- if (type == NULL)
- return (-1);
- if (xmlStrEqual(type, BAD_CAST "string"))
- return (1);
- if (xmlStrEqual(type, BAD_CAST "token"))
- return (1);
- return (0);
- }
- /**
- * xmlRelaxNGDefaultTypeCheck:
- * @data: data needed for the library
- * @type: the type name
- * @value: the value to check
- * @node: the node
- *
- * Check if the given type and value are validated by
- * the default datatype library.
- *
- * Returns 1 if yes, 0 if no and -1 in case of error.
- */
- static int
- xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
- const xmlChar * type ATTRIBUTE_UNUSED,
- const xmlChar * value ATTRIBUTE_UNUSED,
- void **result ATTRIBUTE_UNUSED,
- xmlNodePtr node ATTRIBUTE_UNUSED)
- {
- if (value == NULL)
- return (-1);
- if (xmlStrEqual(type, BAD_CAST "string"))
- return (1);
- if (xmlStrEqual(type, BAD_CAST "token")) {
- return (1);
- }
- return (0);
- }
- /**
- * xmlRelaxNGDefaultTypeCompare:
- * @data: data needed for the library
- * @type: the type name
- * @value1: the first value
- * @value2: the second value
- *
- * Compare two values accordingly a type from the default
- * datatype library.
- *
- * Returns 1 if yes, 0 if no and -1 in case of error.
- */
- static int
- xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
- const xmlChar * type,
- const xmlChar * value1,
- xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
- void *comp1 ATTRIBUTE_UNUSED,
- const xmlChar * value2,
- xmlNodePtr ctxt2 ATTRIBUTE_UNUSED)
- {
- int ret = -1;
- if (xmlStrEqual(type, BAD_CAST "string")) {
- ret = xmlStrEqual(value1, value2);
- } else if (xmlStrEqual(type, BAD_CAST "token")) {
- if (!xmlStrEqual(value1, value2)) {
- xmlChar *nval, *nvalue;
- /*
- * TODO: trivial optimizations are possible by
- * computing at compile-time
- */
- nval = xmlRelaxNGNormalize(NULL, value1);
- nvalue = xmlRelaxNGNormalize(NULL, value2);
- if ((nval == NULL) || (nvalue == NULL))
- ret = -1;
- else if (xmlStrEqual(nval, nvalue))
- ret = 1;
- else
- ret = 0;
- if (nval != NULL)
- xmlFree(nval);
- if (nvalue != NULL)
- xmlFree(nvalue);
- } else
- ret = 1;
- }
- return (ret);
- }
- static int xmlRelaxNGTypeInitialized = 0;
- static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
- /**
- * xmlRelaxNGFreeTypeLibrary:
- * @lib: the type library structure
- * @namespace: the URI bound to the library
- *
- * Free the structure associated to the type library
- */
- static void
- xmlRelaxNGFreeTypeLibrary(void *payload,
- const xmlChar * namespace ATTRIBUTE_UNUSED)
- {
- xmlRelaxNGTypeLibraryPtr lib = (xmlRelaxNGTypeLibraryPtr) payload;
- if (lib == NULL)
- return;
- if (lib->namespace != NULL)
- xmlFree((xmlChar *) lib->namespace);
- xmlFree(lib);
- }
- /**
- * xmlRelaxNGRegisterTypeLibrary:
- * @namespace: the URI bound to the library
- * @data: data associated to the library
- * @have: the provide function
- * @check: the checking function
- * @comp: the comparison function
- *
- * Register a new type library
- *
- * Returns 0 in case of success and -1 in case of error.
- */
- static int
- xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data,
- xmlRelaxNGTypeHave have,
- xmlRelaxNGTypeCheck check,
- xmlRelaxNGTypeCompare comp,
- xmlRelaxNGFacetCheck facet,
- xmlRelaxNGTypeFree freef)
- {
- xmlRelaxNGTypeLibraryPtr lib;
- int ret;
- if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
- (check == NULL) || (comp == NULL))
- return (-1);
- if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "Relax-NG types library '%s' already registered\n",
- namespace);
- return (-1);
- }
- lib =
- (xmlRelaxNGTypeLibraryPtr)
- xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
- if (lib == NULL) {
- xmlRngVErrMemory(NULL, "adding types library\n");
- return (-1);
- }
- memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
- lib->namespace = xmlStrdup(namespace);
- lib->data = data;
- lib->have = have;
- lib->comp = comp;
- lib->check = check;
- lib->facet = facet;
- lib->freef = freef;
- ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
- if (ret < 0) {
- xmlGenericError(xmlGenericErrorContext,
- "Relax-NG types library failed to register '%s'\n",
- namespace);
- xmlRelaxNGFreeTypeLibrary(lib, namespace);
- return (-1);
- }
- return (0);
- }
- /**
- * xmlRelaxNGInitTypes:
- *
- * Initialize the default type libraries.
- *
- * Returns 0 in case of success and -1 in case of error.
- */
- int
- xmlRelaxNGInitTypes(void)
- {
- if (xmlRelaxNGTypeInitialized != 0)
- return (0);
- xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
- if (xmlRelaxNGRegisteredTypes == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "Failed to allocate sh table for Relax-NG types\n");
- return (-1);
- }
- xmlRelaxNGRegisterTypeLibrary(BAD_CAST
- "http://www.w3.org/2001/XMLSchema-datatypes",
- NULL, xmlRelaxNGSchemaTypeHave,
- xmlRelaxNGSchemaTypeCheck,
- xmlRelaxNGSchemaTypeCompare,
- xmlRelaxNGSchemaFacetCheck,
- xmlRelaxNGSchemaFreeValue);
- xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL,
- xmlRelaxNGDefaultTypeHave,
- xmlRelaxNGDefaultTypeCheck,
- xmlRelaxNGDefaultTypeCompare, NULL,
- NULL);
- xmlRelaxNGTypeInitialized = 1;
- return (0);
- }
- /**
- * xmlRelaxNGCleanupTypes:
- *
- * Cleanup the default Schemas type library associated to RelaxNG
- */
- void
- xmlRelaxNGCleanupTypes(void)
- {
- xmlSchemaCleanupTypes();
- if (xmlRelaxNGTypeInitialized == 0)
- return;
- xmlHashFree(xmlRelaxNGRegisteredTypes, xmlRelaxNGFreeTypeLibrary);
- xmlRelaxNGTypeInitialized = 0;
- }
- /************************************************************************
- * *
- * Compiling element content into regexp *
- * *
- * Sometime the element content can be compiled into a pure regexp, *
- * This allows a faster execution and streamability at that level *
- * *
- ************************************************************************/
- /* from automata.c but not exported */
- void xmlAutomataSetFlags(xmlAutomataPtr am, int flags);
- static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
- xmlRelaxNGDefinePtr def);
- /**
- * xmlRelaxNGIsCompilable:
- * @define: the definition to check
- *
- * Check if a definition is nullable.
- *
- * Returns 1 if yes, 0 if no and -1 in case of error
- */
- static int
- xmlRelaxNGIsCompilable(xmlRelaxNGDefinePtr def)
- {
- int ret = -1;
- if (def == NULL) {
- return (-1);
- }
- if ((def->type != XML_RELAXNG_ELEMENT) &&
- (def->dflags & IS_COMPILABLE))
- return (1);
- if ((def->type != XML_RELAXNG_ELEMENT) &&
- (def->dflags & IS_NOT_COMPILABLE))
- return (0);
- switch (def->type) {
- case XML_RELAXNG_NOOP:
- ret = xmlRelaxNGIsCompilable(def->content);
- break;
- case XML_RELAXNG_TEXT:
- case XML_RELAXNG_EMPTY:
- ret = 1;
- break;
- case XML_RELAXNG_ELEMENT:
- /*
- * Check if the element content is compilable
- */
- if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
- ((def->dflags & IS_COMPILABLE) == 0)) {
- xmlRelaxNGDefinePtr list;
- list = def->content;
- while (list != NULL) {
- ret = xmlRelaxNGIsCompilable(list);
- if (ret != 1)
- break;
- list = list->next;
- }
- /*
- * Because the routine is recursive, we must guard against
- * discovering both COMPILABLE and NOT_COMPILABLE
- */
- if (ret == 0) {
- def->dflags &= ~IS_COMPILABLE;
- def->dflags |= IS_NOT_COMPILABLE;
- }
- if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE))
- def->dflags |= IS_COMPILABLE;
- #ifdef DEBUG_COMPILE
- if (ret == 1) {
- xmlGenericError(xmlGenericErrorContext,
- "element content for %s is compilable\n",
- def->name);
- } else if (ret == 0) {
- xmlGenericError(xmlGenericErrorContext,
- "element content for %s is not compilable\n",
- def->name);
- } else {
- xmlGenericError(xmlGenericErrorContext,
- "Problem in RelaxNGIsCompilable for element %s\n",
- def->name);
- }
- #endif
- }
- /*
- * All elements return a compilable status unless they
- * are generic like anyName
- */
- if ((def->nameClass != NULL) || (def->name == NULL))
- ret = 0;
- else
- ret = 1;
- return (ret);
- case XML_RELAXNG_REF:
- case XML_RELAXNG_EXTERNALREF:
- case XML_RELAXNG_PARENTREF:
- if (def->depth == -20) {
- return (1);
- } else {
- xmlRelaxNGDefinePtr list;
- def->depth = -20;
- list = def->content;
- while (list != NULL) {
- ret = xmlRelaxNGIsCompilable(list);
- if (ret != 1)
- break;
- list = list->next;
- }
- }
- break;
- case XML_RELAXNG_START:
- case XML_RELAXNG_OPTIONAL:
- case XML_RELAXNG_ZEROORMORE:
- case XML_RELAXNG_ONEORMORE:
- case XML_RELAXNG_CHOICE:
- case XML_RELAXNG_GROUP:
- case XML_RELAXNG_DEF:{
- xmlRelaxNGDefinePtr list;
- list = def->content;
- while (list != NULL) {
- ret = xmlRelaxNGIsCompilable(list);
- if (ret != 1)
- break;
- list = list->next;
- }
- break;
- }
- case XML_RELAXNG_EXCEPT:
- case XML_RELAXNG_ATTRIBUTE:
- case XML_RELAXNG_INTERLEAVE:
- case XML_RELAXNG_DATATYPE:
- case XML_RELAXNG_LIST:
- case XML_RELAXNG_PARAM:
- case XML_RELAXNG_VALUE:
- case XML_RELAXNG_NOT_ALLOWED:
- ret = 0;
- break;
- }
- if (ret == 0)
- def->dflags |= IS_NOT_COMPILABLE;
- if (ret == 1)
- def->dflags |= IS_COMPILABLE;
- #ifdef DEBUG_COMPILE
- if (ret == 1) {
- xmlGenericError(xmlGenericErrorContext,
- "RelaxNGIsCompilable %s : true\n",
- xmlRelaxNGDefName(def));
- } else if (ret == 0) {
- xmlGenericError(xmlGenericErrorContext,
- "RelaxNGIsCompilable %s : false\n",
- xmlRelaxNGDefName(def));
- } else {
- xmlGenericError(xmlGenericErrorContext,
- "Problem in RelaxNGIsCompilable %s\n",
- xmlRelaxNGDefName(def));
- }
- #endif
- return (ret);
- }
- /**
- * xmlRelaxNGCompile:
- * ctxt: the RelaxNG parser context
- * @define: the definition tree to compile
- *
- * Compile the set of definitions, it works recursively, till the
- * element boundaries, where it tries to compile the content if possible
- *
- * Returns 0 if success and -1 in case of error
- */
- static int
- xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
- {
- int ret = 0;
- xmlRelaxNGDefinePtr list;
- if ((ctxt == NULL) || (def == NULL))
- return (-1);
- switch (def->type) {
- case XML_RELAXNG_START:
- if ((xmlRelaxNGIsCompilable(def) == 1) && (def->depth != -25)) {
- xmlAutomataPtr oldam = ctxt->am;
- xmlAutomataStatePtr oldstate = ctxt->state;
- def->depth = -25;
- list = def->content;
- ctxt->am = xmlNewAutomata();
- if (ctxt->am == NULL)
- return (-1);
- /*
- * assume identical strings but not same pointer are different
- * atoms, needed for non-determinism detection
- * That way if 2 elements with the same name are in a choice
- * branch the automata is found non-deterministic and
- * we fallback to the normal validation which does the right
- * thing of exploring both choices.
- */
- xmlAutomataSetFlags(ctxt->am, 1);
- ctxt->state = xmlAutomataGetInitState(ctxt->am);
- while (list != NULL) {
- xmlRelaxNGCompile(ctxt, list);
- list = list->next;
- }
- xmlAutomataSetFinalState(ctxt->am, ctxt->state);
- if (xmlAutomataIsDeterminist(ctxt->am))
- def->contModel = xmlAutomataCompile(ctxt->am);
- xmlFreeAutomata(ctxt->am);
- ctxt->state = oldstate;
- ctxt->am = oldam;
- }
- break;
- case XML_RELAXNG_ELEMENT:
- if ((ctxt->am != NULL) && (def->name != NULL)) {
- ctxt->state = xmlAutomataNewTransition2(ctxt->am,
- ctxt->state, NULL,
- def->name, def->ns,
- def);
- }
- if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
- xmlAutomataPtr oldam = ctxt->am;
- xmlAutomataStatePtr oldstate = ctxt->state;
- def->depth = -25;
- list = def->content;
- ctxt->am = xmlNewAutomata();
- if (ctxt->am == NULL)
- return (-1);
- xmlAutomataSetFlags(ctxt->am, 1);
- ctxt->state = xmlAutomataGetInitState(ctxt->am);
- while (list != NULL) {
- xmlRelaxNGCompile(ctxt, list);
- list = list->next;
- }
- xmlAutomataSetFinalState(ctxt->am, ctxt->state);
- def->contModel = xmlAutomataCompile(ctxt->am);
- if (!xmlRegexpIsDeterminist(def->contModel)) {
- #ifdef DEBUG_COMPILE
- xmlGenericError(xmlGenericErrorContext,
- "Content model not determinist %s\n",
- def->name);
- #endif
- /*
- * we can only use the automata if it is determinist
- */
- xmlRegFreeRegexp(def->contModel);
- def->contModel = NULL;
- }
- xmlFreeAutomata(ctxt->am);
- ctxt->state = oldstate;
- ctxt->am = oldam;
- } else {
- xmlAutomataPtr oldam = ctxt->am;
- /*
- * we can't build the content model for this element content
- * but it still might be possible to build it for some of its
- * children, recurse.
- */
- ret = xmlRelaxNGTryCompile(ctxt, def);
- ctxt->am = oldam;
- }
- break;
- case XML_RELAXNG_NOOP:
- ret = xmlRelaxNGCompile(ctxt, def->content);
- break;
- case XML_RELAXNG_OPTIONAL:{
- xmlAutomataStatePtr oldstate = ctxt->state;
- list = def->content;
- while (list != NULL) {
- xmlRelaxNGCompile(ctxt, list);
- list = list->next;
- }
- xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
- break;
- }
- case XML_RELAXNG_ZEROORMORE:{
- xmlAutomataStatePtr oldstate;
- ctxt->state =
- xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
- oldstate = ctxt->state;
- list = def->content;
- while (list != NULL) {
- xmlRelaxNGCompile(ctxt, list);
- list = list->next;
- }
- xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
- ctxt->state =
- xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
- break;
- }
- case XML_RELAXNG_ONEORMORE:{
- xmlAutomataStatePtr oldstate;
- list = def->content;
- while (list != NULL) {
- xmlRelaxNGCompile(ctxt, list);
- list = list->next;
- }
- oldstate = ctxt->state;
- list = def->content;
- while (list != NULL) {
- xmlRelaxNGCompile(ctxt, list);
- list = list->next;
- }
- xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
- ctxt->state =
- xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
- break;
- }
- case XML_RELAXNG_CHOICE:{
- xmlAutomataStatePtr target = NULL;
- xmlAutomataStatePtr oldstate = ctxt->state;
- list = def->content;
- while (list != NULL) {
- ctxt->state = oldstate;
- ret = xmlRelaxNGCompile(ctxt, list);
- if (ret != 0)
- break;
- if (target == NULL)
- target = ctxt->state;
- else {
- xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
- target);
- }
- list = list->next;
- }
- ctxt->state = target;
- break;
- }
- case XML_RELAXNG_REF:
- case XML_RELAXNG_EXTERNALREF:
- case XML_RELAXNG_PARENTREF:
- case XML_RELAXNG_GROUP:
- case XML_RELAXNG_DEF:
- list = def->content;
- while (list != NULL) {
- ret = xmlRelaxNGCompile(ctxt, list);
- if (ret != 0)
- break;
- list = list->next;
- }
- break;
- case XML_RELAXNG_TEXT:{
- xmlAutomataStatePtr oldstate;
- ctxt->state =
- xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
- oldstate = ctxt->state;
- xmlRelaxNGCompile(ctxt, def->content);
- xmlAutomataNewTransition(ctxt->am, ctxt->state,
- ctxt->state, BAD_CAST "#text",
- NULL);
- ctxt->state =
- xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
- break;
- }
- case XML_RELAXNG_EMPTY:
- ctxt->state =
- xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
- break;
- case XML_RELAXNG_EXCEPT:
- case XML_RELAXNG_ATTRIBUTE:
- case XML_RELAXNG_INTERLEAVE:
- case XML_RELAXNG_NOT_ALLOWED:
- case XML_RELAXNG_DATATYPE:
- case XML_RELAXNG_LIST:
- case XML_RELAXNG_PARAM:
- case XML_RELAXNG_VALUE:
- /* This should not happen and generate an internal error */
- fprintf(stderr, "RNG internal error trying to compile %s\n",
- xmlRelaxNGDefName(def));
- break;
- }
- return (ret);
- }
- /**
- * xmlRelaxNGTryCompile:
- * ctxt: the RelaxNG parser context
- * @define: the definition tree to compile
- *
- * Try to compile the set of definitions, it works recursively,
- * possibly ignoring parts which cannot be compiled.
- *
- * Returns 0 if success and -1 in case of error
- */
- static int
- xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
- {
- int ret = 0;
- xmlRelaxNGDefinePtr list;
- if ((ctxt == NULL) || (def == NULL))
- return (-1);
- if ((def->type == XML_RELAXNG_START) ||
- (def->type == XML_RELAXNG_ELEMENT)) {
- ret = xmlRelaxNGIsCompilable(def);
- if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
- ctxt->am = NULL;
- ret = xmlRelaxNGCompile(ctxt, def);
- #ifdef DEBUG_PROGRESSIVE
- if (ret == 0) {
- if (def->type == XML_RELAXNG_START)
- xmlGenericError(xmlGenericErrorContext,
- "compiled the start\n");
- else
- xmlGenericError(xmlGenericErrorContext,
- "compiled element %s\n", def->name);
- } else {
- if (def->type == XML_RELAXNG_START)
- xmlGenericError(xmlGenericErrorContext,
- "failed to compile the start\n");
- else
- xmlGenericError(xmlGenericErrorContext,
- "failed to compile element %s\n",
- def->name);
- }
- #endif
- return (ret);
- }
- }
- switch (def->type) {
- case XML_RELAXNG_NOOP:
- ret = xmlRelaxNGTryCompile(ctxt, def->content);
- break;
- case XML_RELAXNG_TEXT:
- case XML_RELAXNG_DATATYPE:
- case XML_RELAXNG_LIST:
- case XML_RELAXNG_PARAM:
- case XML_RELAXNG_VALUE:
- case XML_RELAXNG_EMPTY:
- case XML_RELAXNG_ELEMENT:
- ret = 0;
- break;
- case XML_RELAXNG_OPTIONAL:
- case XML_RELAXNG_ZEROORMORE:
- case XML_RELAXNG_ONEORMORE:
- case XML_RELAXNG_CHOICE:
- case XML_RELAXNG_GROUP:
- case XML_RELAXNG_DEF:
- case XML_RELAXNG_START:
- case XML_RELAXNG_REF:
- case XML_RELAXNG_EXTERNALREF:
- case XML_RELAXNG_PARENTREF:
- list = def->content;
- while (list != NULL) {
- ret = xmlRelaxNGTryCompile(ctxt, list);
- if (ret != 0)
- break;
- list = list->next;
- }
- break;
- case XML_RELAXNG_EXCEPT:
- case XML_RELAXNG_ATTRIBUTE:
- case XML_RELAXNG_INTERLEAVE:
- case XML_RELAXNG_NOT_ALLOWED:
- ret = 0;
- break;
- }
- return (ret);
- }
- /************************************************************************
- * *
- * Parsing functions *
- * *
- ************************************************************************/
- static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr
- ctxt, xmlNodePtr node);
- static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr
- ctxt, xmlNodePtr node);
- static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr
- ctxt, xmlNodePtr nodes,
- int group);
- static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr
- ctxt, xmlNodePtr node);
- static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,
- xmlNodePtr node);
- static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
- xmlNodePtr nodes);
- static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr
- ctxt, xmlNodePtr node,
- xmlRelaxNGDefinePtr
- def);
- static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr
- ctxt, xmlNodePtr nodes);
- static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGDefinePtr define,
- xmlNodePtr elem);
- #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
- /**
- * xmlRelaxNGIsNullable:
- * @define: the definition to verify
- *
- * Check if a definition is nullable.
- *
- * Returns 1 if yes, 0 if no and -1 in case of error
- */
- static int
- xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)
- {
- int ret;
- if (define == NULL)
- return (-1);
- if (define->dflags & IS_NULLABLE)
- return (1);
- if (define->dflags & IS_NOT_NULLABLE)
- return (0);
- switch (define->type) {
- case XML_RELAXNG_EMPTY:
- case XML_RELAXNG_TEXT:
- ret = 1;
- break;
- case XML_RELAXNG_NOOP:
- case XML_RELAXNG_DEF:
- case XML_RELAXNG_REF:
- case XML_RELAXNG_EXTERNALREF:
- case XML_RELAXNG_PARENTREF:
- case XML_RELAXNG_ONEORMORE:
- ret = xmlRelaxNGIsNullable(define->content);
- break;
- case XML_RELAXNG_EXCEPT:
- case XML_RELAXNG_NOT_ALLOWED:
- case XML_RELAXNG_ELEMENT:
- case XML_RELAXNG_DATATYPE:
- case XML_RELAXNG_PARAM:
- case XML_RELAXNG_VALUE:
- case XML_RELAXNG_LIST:
- case XML_RELAXNG_ATTRIBUTE:
- ret = 0;
- break;
- case XML_RELAXNG_CHOICE:{
- xmlRelaxNGDefinePtr list = define->content;
- while (list != NULL) {
- ret = xmlRelaxNGIsNullable(list);
- if (ret != 0)
- goto done;
- list = list->next;
- }
- ret = 0;
- break;
- }
- case XML_RELAXNG_START:
- case XML_RELAXNG_INTERLEAVE:
- case XML_RELAXNG_GROUP:{
- xmlRelaxNGDefinePtr list = define->content;
- while (list != NULL) {
- ret = xmlRelaxNGIsNullable(list);
- if (ret != 1)
- goto done;
- list = list->next;
- }
- return (1);
- }
- default:
- return (-1);
- }
- done:
- if (ret == 0)
- define->dflags |= IS_NOT_NULLABLE;
- if (ret == 1)
- define->dflags |= IS_NULLABLE;
- return (ret);
- }
- /**
- * xmlRelaxNGIsBlank:
- * @str: a string
- *
- * Check if a string is ignorable c.f. 4.2. Whitespace
- *
- * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
- */
- static int
- xmlRelaxNGIsBlank(xmlChar * str)
- {
- if (str == NULL)
- return (1);
- while (*str != 0) {
- if (!(IS_BLANK_CH(*str)))
- return (0);
- str++;
- }
- return (1);
- }
- /**
- * xmlRelaxNGGetDataTypeLibrary:
- * @ctxt: a Relax-NG parser context
- * @node: the current data or value element
- *
- * Applies algorithm from 4.3. datatypeLibrary attribute
- *
- * Returns the datatypeLibrary value or NULL if not found
- */
- static xmlChar *
- xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
- xmlNodePtr node)
- {
- xmlChar *ret, *escape;
- if (node == NULL)
- return(NULL);
- if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
- ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
- if (ret != NULL) {
- if (ret[0] == 0) {
- xmlFree(ret);
- return (NULL);
- }
- escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
- if (escape == NULL) {
- return (ret);
- }
- xmlFree(ret);
- return (escape);
- }
- }
- node = node->parent;
- while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
- ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
- if (ret != NULL) {
- if (ret[0] == 0) {
- xmlFree(ret);
- return (NULL);
- }
- escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
- if (escape == NULL) {
- return (ret);
- }
- xmlFree(ret);
- return (escape);
- }
- node = node->parent;
- }
- return (NULL);
- }
- /**
- * xmlRelaxNGParseValue:
- * @ctxt: a Relax-NG parser context
- * @node: the data node.
- *
- * parse the content of a RelaxNG value node.
- *
- * Returns the definition pointer or NULL in case of error
- */
- static xmlRelaxNGDefinePtr
- xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
- {
- xmlRelaxNGDefinePtr def = NULL;
- xmlRelaxNGTypeLibraryPtr lib = NULL;
- xmlChar *type;
- xmlChar *library;
- int success = 0;
- def = xmlRelaxNGNewDefine(ctxt, node);
- if (def == NULL)
- return (NULL);
- def->type = XML_RELAXNG_VALUE;
- type = xmlGetProp(node, BAD_CAST "type");
- if (type != NULL) {
- xmlRelaxNGNormExtSpace(type);
- if (xmlValidateNCName(type, 0)) {
- xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
- "value type '%s' is not an NCName\n", type, NULL);
- }
- library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
- if (library == NULL)
- library =
- xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
- def->name = type;
- def->ns = library;
- lib = (xmlRelaxNGTypeLibraryPtr)
- xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
- if (lib == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
- "Use of unregistered type library '%s'\n", library,
- NULL);
- def->data = NULL;
- } else {
- def->data = lib;
- if (lib->have == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
- "Internal error with type library '%s': no 'have'\n",
- library, NULL);
- } else {
- success = lib->have(lib->data, def->name);
- if (success != 1) {
- xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
- "Error type '%s' is not exported by type library '%s'\n",
- def->name, library);
- }
- }
- }
- }
- if (node->children == NULL) {
- def->value = xmlStrdup(BAD_CAST "");
- } else if (((node->children->type != XML_TEXT_NODE) &&
- (node->children->type != XML_CDATA_SECTION_NODE)) ||
- (node->children->next != NULL)) {
- xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED,
- "Expecting a single text value for <value>content\n",
- NULL, NULL);
- } else if (def != NULL) {
- def->value = xmlNodeGetContent(node);
- if (def->value == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT,
- "Element <value> has no content\n", NULL, NULL);
- } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
- void *val = NULL;
- success =
- lib->check(lib->data, def->name, def->value, &val, node);
- if (success != 1) {
- xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE,
- "Value '%s' is not acceptable for type '%s'\n",
- def->value, def->name);
- } else {
- if (val != NULL)
- def->attrs = val;
- }
- }
- }
- return (def);
- }
- /**
- * xmlRelaxNGParseData:
- * @ctxt: a Relax-NG parser context
- * @node: the data node.
- *
- * parse the content of a RelaxNG data node.
- *
- * Returns the definition pointer or NULL in case of error
- */
- static xmlRelaxNGDefinePtr
- xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
- {
- xmlRelaxNGDefinePtr def = NULL, except;
- xmlRelaxNGDefinePtr param, lastparam = NULL;
- xmlRelaxNGTypeLibraryPtr lib;
- xmlChar *type;
- xmlChar *library;
- xmlNodePtr content;
- int tmp;
- type = xmlGetProp(node, BAD_CAST "type");
- if (type == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL,
- NULL);
- return (NULL);
- }
- xmlRelaxNGNormExtSpace(type);
- if (xmlValidateNCName(type, 0)) {
- xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
- "data type '%s' is not an NCName\n", type, NULL);
- }
- library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
- if (library == NULL)
- library =
- xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
- def = xmlRelaxNGNewDefine(ctxt, node);
- if (def == NULL) {
- xmlFree(library);
- xmlFree(type);
- return (NULL);
- }
- def->type = XML_RELAXNG_DATATYPE;
- def->name = type;
- def->ns = library;
- lib = (xmlRelaxNGTypeLibraryPtr)
- xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
- if (lib == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
- "Use of unregistered type library '%s'\n", library,
- NULL);
- def->data = NULL;
- } else {
- def->data = lib;
- if (lib->have == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
- "Internal error with type library '%s': no 'have'\n",
- library, NULL);
- } else {
- tmp = lib->have(lib->data, def->name);
- if (tmp != 1) {
- xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
- "Error type '%s' is not exported by type library '%s'\n",
- def->name, library);
- } else
- if ((xmlStrEqual
- (library,
- BAD_CAST
- "http://www.w3.org/2001/XMLSchema-datatypes"))
- && ((xmlStrEqual(def->name, BAD_CAST "IDREF"))
- || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
- ctxt->idref = 1;
- }
- }
- }
- content = node->children;
- /*
- * Handle optional params
- */
- while (content != NULL) {
- if (!xmlStrEqual(content->name, BAD_CAST "param"))
- break;
- if (xmlStrEqual(library,
- BAD_CAST "http://relaxng.org/ns/structure/1.0")) {
- xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN,
- "Type library '%s' does not allow type parameters\n",
- library, NULL);
- content = content->next;
- while ((content != NULL) &&
- (xmlStrEqual(content->name, BAD_CAST "param")))
- content = content->next;
- } else {
- param = xmlRelaxNGNewDefine(ctxt, node);
- if (param != NULL) {
- param->type = XML_RELAXNG_PARAM;
- param->name = xmlGetProp(content, BAD_CAST "name");
- if (param->name == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING,
- "param has no name\n", NULL, NULL);
- }
- param->value = xmlNodeGetContent(content);
- if (lastparam == NULL) {
- def->attrs = lastparam = param;
- } else {
- lastparam->next = param;
- lastparam = param;
- }
- if (lib != NULL) {
- }
- }
- content = content->next;
- }
- }
- /*
- * Handle optional except
- */
- if ((content != NULL)
- && (xmlStrEqual(content->name, BAD_CAST "except"))) {
- xmlNodePtr child;
- xmlRelaxNGDefinePtr tmp2, last = NULL;
- except = xmlRelaxNGNewDefine(ctxt, node);
- if (except == NULL) {
- return (def);
- }
- except->type = XML_RELAXNG_EXCEPT;
- child = content->children;
- def->content = except;
- if (child == NULL) {
- xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT,
- "except has no content\n", NULL, NULL);
- }
- while (child != NULL) {
- tmp2 = xmlRelaxNGParsePattern(ctxt, child);
- if (tmp2 != NULL) {
- if (last == NULL) {
- except->content = last = tmp2;
- } else {
- last->next = tmp2;
- last = tmp2;
- }
- }
- child = child->next;
- }
- content = content->next;
- }
- /*
- * Check there is no unhandled data
- */
- if (content != NULL) {
- xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT,
- "Element data has unexpected content %s\n",
- content->name, NULL);
- }
- return (def);
- }
- static const xmlChar *invalidName = BAD_CAST "\1";
- /**
- * xmlRelaxNGCompareNameClasses:
- * @defs1: the first element/attribute defs
- * @defs2: the second element/attribute defs
- * @name: the restriction on the name
- * @ns: the restriction on the namespace
- *
- * Compare the 2 lists of element definitions. The comparison is
- * that if both lists do not accept the same QNames, it returns 1
- * If the 2 lists can accept the same QName the comparison returns 0
- *
- * Returns 1 distinct, 0 if equal
- */
- static int
- xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
- xmlRelaxNGDefinePtr def2)
- {
- int ret = 1;
- xmlNode node;
- xmlNs ns;
- xmlRelaxNGValidCtxt ctxt;
- memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
- ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR;
- if ((def1->type == XML_RELAXNG_ELEMENT) ||
- (def1->type == XML_RELAXNG_ATTRIBUTE)) {
- if (def2->type == XML_RELAXNG_TEXT)
- return (1);
- if (def1->name != NULL) {
- node.name = def1->name;
- } else {
- node.name = invalidName;
- }
- if (def1->ns != NULL) {
- if (def1->ns[0] == 0) {
- node.ns = NULL;
- } else {
- node.ns = &ns;
- ns.href = def1->ns;
- }
- } else {
- node.ns = NULL;
- }
- if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
- if (def1->nameClass != NULL) {
- ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
- } else {
- ret = 0;
- }
- } else {
- ret = 1;
- }
- } else if (def1->type == XML_RELAXNG_TEXT) {
- if (def2->type == XML_RELAXNG_TEXT)
- return (0);
- return (1);
- } else if (def1->type == XML_RELAXNG_EXCEPT) {
- ret = xmlRelaxNGCompareNameClasses(def1->content, def2);
- if (ret == 0)
- ret = 1;
- else if (ret == 1)
- ret = 0;
- } else {
- TODO ret = 0;
- }
- if (ret == 0)
- return (ret);
- if ((def2->type == XML_RELAXNG_ELEMENT) ||
- (def2->type == XML_RELAXNG_ATTRIBUTE)) {
- if (def2->name != NULL) {
- node.name = def2->name;
- } else {
- node.name = invalidName;
- }
- node.ns = &ns;
- if (def2->ns != NULL) {
- if (def2->ns[0] == 0) {
- node.ns = NULL;
- } else {
- ns.href = def2->ns;
- }
- } else {
- ns.href = invalidName;
- }
- if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
- if (def2->nameClass != NULL) {
- ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
- } else {
- ret = 0;
- }
- } else {
- ret = 1;
- }
- } else {
- TODO ret = 0;
- }
- return (ret);
- }
- /**
- * xmlRelaxNGCompareElemDefLists:
- * @ctxt: a Relax-NG parser context
- * @defs1: the first list of element/attribute defs
- * @defs2: the second list of element/attribute defs
- *
- * Compare the 2 lists of element or attribute definitions. The comparison
- * is that if both lists do not accept the same QNames, it returns 1
- * If the 2 lists can accept the same QName the comparison returns 0
- *
- * Returns 1 distinct, 0 if equal
- */
- static int
- xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt
- ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1,
- xmlRelaxNGDefinePtr * def2)
- {
- xmlRelaxNGDefinePtr *basedef2 = def2;
- if ((def1 == NULL) || (def2 == NULL))
- return (1);
- if ((*def1 == NULL) || (*def2 == NULL))
- return (1);
- while (*def1 != NULL) {
- while ((*def2) != NULL) {
- if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
- return (0);
- def2++;
- }
- def2 = basedef2;
- def1++;
- }
- return (1);
- }
- /**
- * xmlRelaxNGGenerateAttributes:
- * @ctxt: a Relax-NG parser context
- * @def: the definition definition
- *
- * Check if the definition can only generate attributes
- *
- * Returns 1 if yes, 0 if no and -1 in case of error.
- */
- static int
- xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
- xmlRelaxNGDefinePtr def)
- {
- xmlRelaxNGDefinePtr parent, cur, tmp;
- /*
- * Don't run that check in case of error. Infinite recursion
- * becomes possible.
- */
- if (ctxt->nbErrors != 0)
- return (-1);
- parent = NULL;
- cur = def;
- while (cur != NULL) {
- if ((cur->type == XML_RELAXNG_ELEMENT) ||
- (cur->type == XML_RELAXNG_TEXT) ||
- (cur->type == XML_RELAXNG_DATATYPE) ||
- (cur->type == XML_RELAXNG_PARAM) ||
- (cur->type == XML_RELAXNG_LIST) ||
- (cur->type == XML_RELAXNG_VALUE) ||
- (cur->type == XML_RELAXNG_EMPTY))
- return (0);
- if ((cur->type == XML_RELAXNG_CHOICE) ||
- (cur->type == XML_RELAXNG_INTERLEAVE) ||
- (cur->type == XML_RELAXNG_GROUP) ||
- (cur->type == XML_RELAXNG_ONEORMORE) ||
- (cur->type == XML_RELAXNG_ZEROORMORE) ||
- (cur->type == XML_RELAXNG_OPTIONAL) ||
- (cur->type == XML_RELAXNG_PARENTREF) ||
- (cur->type == XML_RELAXNG_EXTERNALREF) ||
- (cur->type == XML_RELAXNG_REF) ||
- (cur->type == XML_RELAXNG_DEF)) {
- if (cur->content != NULL) {
- parent = cur;
- cur = cur->content;
- tmp = cur;
- while (tmp != NULL) {
- tmp->parent = parent;
- tmp = tmp->next;
- }
- continue;
- }
- }
- if (cur == def)
- break;
- if (cur->next != NULL) {
- cur = cur->next;
- continue;
- }
- do {
- cur = cur->parent;
- if (cur == NULL)
- break;
- if (cur == def)
- return (1);
- if (cur->next != NULL) {
- cur = cur->next;
- break;
- }
- } while (cur != NULL);
- }
- return (1);
- }
- /**
- * xmlRelaxNGGetElements:
- * @ctxt: a Relax-NG parser context
- * @def: the definition definition
- * @eora: gather elements (0), attributes (1) or elements and text (2)
- *
- * Compute the list of top elements a definition can generate
- *
- * Returns a list of elements or NULL if none was found.
- */
- static xmlRelaxNGDefinePtr *
- xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
- xmlRelaxNGDefinePtr def, int eora)
- {
- xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
- int len = 0;
- int max = 0;
- /*
- * Don't run that check in case of error. Infinite recursion
- * becomes possible.
- */
- if (ctxt->nbErrors != 0)
- return (NULL);
- parent = NULL;
- cur = def;
- while (cur != NULL) {
- if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
- (cur->type == XML_RELAXNG_TEXT))) ||
- ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE)) ||
- ((eora == 2) && ((cur->type == XML_RELAXNG_DATATYPE) ||
- (cur->type == XML_RELAXNG_ELEMENT) ||
- (cur->type == XML_RELAXNG_LIST) ||
- (cur->type == XML_RELAXNG_TEXT) ||
- (cur->type == XML_RELAXNG_VALUE)))) {
- if (ret == NULL) {
- max = 10;
- ret = (xmlRelaxNGDefinePtr *)
- xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
- if (ret == NULL) {
- xmlRngPErrMemory(ctxt, "getting element list\n");
- return (NULL);
- }
- } else if (max <= len) {
- xmlRelaxNGDefinePtr *temp;
- max *= 2;
- temp = xmlRealloc(ret,
- (max + 1) * sizeof(xmlRelaxNGDefinePtr));
- if (temp == NULL) {
- xmlRngPErrMemory(ctxt, "getting element list\n");
- xmlFree(ret);
- return (NULL);
- }
- ret = temp;
- }
- ret[len++] = cur;
- ret[len] = NULL;
- } else if ((cur->type == XML_RELAXNG_CHOICE) ||
- (cur->type == XML_RELAXNG_INTERLEAVE) ||
- (cur->type == XML_RELAXNG_GROUP) ||
- (cur->type == XML_RELAXNG_ONEORMORE) ||
- (cur->type == XML_RELAXNG_ZEROORMORE) ||
- (cur->type == XML_RELAXNG_OPTIONAL) ||
- (cur->type == XML_RELAXNG_PARENTREF) ||
- (cur->type == XML_RELAXNG_REF) ||
- (cur->type == XML_RELAXNG_DEF) ||
- (cur->type == XML_RELAXNG_EXTERNALREF)) {
- /*
- * Don't go within elements or attributes or string values.
- * Just gather the element top list
- */
- if (cur->content != NULL) {
- parent = cur;
- cur = cur->content;
- tmp = cur;
- while (tmp != NULL) {
- tmp->parent = parent;
- tmp = tmp->next;
- }
- continue;
- }
- }
- if (cur == def)
- break;
- if (cur->next != NULL) {
- cur = cur->next;
- continue;
- }
- do {
- cur = cur->parent;
- if (cur == NULL)
- break;
- if (cur == def)
- return (ret);
- if (cur->next != NULL) {
- cur = cur->next;
- break;
- }
- } while (cur != NULL);
- }
- return (ret);
- }
- /**
- * xmlRelaxNGCheckChoiceDeterminism:
- * @ctxt: a Relax-NG parser context
- * @def: the choice definition
- *
- * Also used to find indeterministic pattern in choice
- */
- static void
- xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
- xmlRelaxNGDefinePtr def)
- {
- xmlRelaxNGDefinePtr **list;
- xmlRelaxNGDefinePtr cur;
- int nbchild = 0, i, j, ret;
- int is_nullable = 0;
- int is_indeterminist = 0;
- xmlHashTablePtr triage = NULL;
- int is_triable = 1;
- if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE))
- return;
- if (def->dflags & IS_PROCESSED)
- return;
- /*
- * Don't run that check in case of error. Infinite recursion
- * becomes possible.
- */
- if (ctxt->nbErrors != 0)
- return;
- is_nullable = xmlRelaxNGIsNullable(def);
- cur = def->content;
- while (cur != NULL) {
- nbchild++;
- cur = cur->next;
- }
- list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
- sizeof(xmlRelaxNGDefinePtr
- *));
- if (list == NULL) {
- xmlRngPErrMemory(ctxt, "building choice\n");
- return;
- }
- i = 0;
- /*
- * a bit strong but safe
- */
- if (is_nullable == 0) {
- triage = xmlHashCreate(10);
- } else {
- is_triable = 0;
- }
- cur = def->content;
- while (cur != NULL) {
- list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
- if ((list[i] == NULL) || (list[i][0] == NULL)) {
- is_triable = 0;
- } else if (is_triable == 1) {
- xmlRelaxNGDefinePtr *tmp;
- int res;
- tmp = list[i];
- while ((*tmp != NULL) && (is_triable == 1)) {
- if ((*tmp)->type == XML_RELAXNG_TEXT) {
- res = xmlHashAddEntry2(triage,
- BAD_CAST "#text", NULL,
- (void *) cur);
- if (res != 0)
- is_triable = -1;
- } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
- ((*tmp)->name != NULL)) {
- if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
- res = xmlHashAddEntry2(triage,
- (*tmp)->name, NULL,
- (void *) cur);
- else
- res = xmlHashAddEntry2(triage,
- (*tmp)->name, (*tmp)->ns,
- (void *) cur);
- if (res != 0)
- is_triable = -1;
- } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
- if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
- res = xmlHashAddEntry2(triage,
- BAD_CAST "#any", NULL,
- (void *) cur);
- else
- res = xmlHashAddEntry2(triage,
- BAD_CAST "#any", (*tmp)->ns,
- (void *) cur);
- if (res != 0)
- is_triable = -1;
- } else {
- is_triable = -1;
- }
- tmp++;
- }
- }
- i++;
- cur = cur->next;
- }
- for (i = 0; i < nbchild; i++) {
- if (list[i] == NULL)
- continue;
- for (j = 0; j < i; j++) {
- if (list[j] == NULL)
- continue;
- ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
- if (ret == 0) {
- is_indeterminist = 1;
- }
- }
- }
- for (i = 0; i < nbchild; i++) {
- if (list[i] != NULL)
- xmlFree(list[i]);
- }
- xmlFree(list);
- if (is_indeterminist) {
- def->dflags |= IS_INDETERMINIST;
- }
- if (is_triable == 1) {
- def->dflags |= IS_TRIABLE;
- def->data = triage;
- } else if (triage != NULL) {
- xmlHashFree(triage, NULL);
- }
- def->dflags |= IS_PROCESSED;
- }
- /**
- * xmlRelaxNGCheckGroupAttrs:
- * @ctxt: a Relax-NG parser context
- * @def: the group definition
- *
- * Detects violations of rule 7.3
- */
- static void
- xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
- xmlRelaxNGDefinePtr def)
- {
- xmlRelaxNGDefinePtr **list;
- xmlRelaxNGDefinePtr cur;
- int nbchild = 0, i, j, ret;
- if ((def == NULL) ||
- ((def->type != XML_RELAXNG_GROUP) &&
- (def->type != XML_RELAXNG_ELEMENT)))
- return;
- if (def->dflags & IS_PROCESSED)
- return;
- /*
- * Don't run that check in case of error. Infinite recursion
- * becomes possible.
- */
- if (ctxt->nbErrors != 0)
- return;
- cur = def->attrs;
- while (cur != NULL) {
- nbchild++;
- cur = cur->next;
- }
- cur = def->content;
- while (cur != NULL) {
- nbchild++;
- cur = cur->next;
- }
- list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
- sizeof(xmlRelaxNGDefinePtr
- *));
- if (list == NULL) {
- xmlRngPErrMemory(ctxt, "building group\n");
- return;
- }
- i = 0;
- cur = def->attrs;
- while (cur != NULL) {
- list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
- i++;
- cur = cur->next;
- }
- cur = def->content;
- while (cur != NULL) {
- list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
- i++;
- cur = cur->next;
- }
- for (i = 0; i < nbchild; i++) {
- if (list[i] == NULL)
- continue;
- for (j = 0; j < i; j++) {
- if (list[j] == NULL)
- continue;
- ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
- if (ret == 0) {
- xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT,
- "Attributes conflicts in group\n", NULL, NULL);
- }
- }
- }
- for (i = 0; i < nbchild; i++) {
- if (list[i] != NULL)
- xmlFree(list[i]);
- }
- xmlFree(list);
- def->dflags |= IS_PROCESSED;
- }
- /**
- * xmlRelaxNGComputeInterleaves:
- * @def: the interleave definition
- * @ctxt: a Relax-NG parser context
- * @name: the definition name
- *
- * A lot of work for preprocessing interleave definitions
- * is potentially needed to get a decent execution speed at runtime
- * - trying to get a total order on the element nodes generated
- * by the interleaves, order the list of interleave definitions
- * following that order.
- * - if <text/> is used to handle mixed content, it is better to
- * flag this in the define and simplify the runtime checking
- * algorithm
- */
- static void
- xmlRelaxNGComputeInterleaves(void *payload, void *data,
- const xmlChar * name ATTRIBUTE_UNUSED)
- {
- xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload;
- xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
- xmlRelaxNGDefinePtr cur, *tmp;
- xmlRelaxNGPartitionPtr partitions = NULL;
- xmlRelaxNGInterleaveGroupPtr *groups = NULL;
- xmlRelaxNGInterleaveGroupPtr group;
- int i, j, ret, res;
- int nbgroups = 0;
- int nbchild = 0;
- int is_mixed = 0;
- int is_determinist = 1;
- /*
- * Don't run that check in case of error. Infinite recursion
- * becomes possible.
- */
- if (ctxt->nbErrors != 0)
- return;
- #ifdef DEBUG_INTERLEAVE
- xmlGenericError(xmlGenericErrorContext,
- "xmlRelaxNGComputeInterleaves(%s)\n", name);
- #endif
- cur = def->content;
- while (cur != NULL) {
- nbchild++;
- cur = cur->next;
- }
- #ifdef DEBUG_INTERLEAVE
- xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
- #endif
- groups = (xmlRelaxNGInterleaveGroupPtr *)
- xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
- if (groups == NULL)
- goto error;
- cur = def->content;
- while (cur != NULL) {
- groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
- xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
- if (groups[nbgroups] == NULL)
- goto error;
- if (cur->type == XML_RELAXNG_TEXT)
- is_mixed++;
- groups[nbgroups]->rule = cur;
- groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 2);
- groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
- nbgroups++;
- cur = cur->next;
- }
- #ifdef DEBUG_INTERLEAVE
- xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
- #endif
- /*
- * Let's check that all rules makes a partitions according to 7.4
- */
- partitions = (xmlRelaxNGPartitionPtr)
- xmlMalloc(sizeof(xmlRelaxNGPartition));
- if (partitions == NULL)
- goto error;
- memset(partitions, 0, sizeof(xmlRelaxNGPartition));
- partitions->nbgroups = nbgroups;
- partitions->triage = xmlHashCreate(nbgroups);
- for (i = 0; i < nbgroups; i++) {
- group = groups[i];
- for (j = i + 1; j < nbgroups; j++) {
- if (groups[j] == NULL)
- continue;
- ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
- groups[j]->defs);
- if (ret == 0) {
- xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT,
- "Element or text conflicts in interleave\n",
- NULL, NULL);
- }
- ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
- groups[j]->attrs);
- if (ret == 0) {
- xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT,
- "Attributes conflicts in interleave\n", NULL,
- NULL);
- }
- }
- tmp = group->defs;
- if ((tmp != NULL) && (*tmp != NULL)) {
- while (*tmp != NULL) {
- if ((*tmp)->type == XML_RELAXNG_TEXT) {
- res = xmlHashAddEntry2(partitions->triage,
- BAD_CAST "#text", NULL,
- (void *) (ptrdiff_t) (i + 1));
- if (res != 0)
- is_determinist = -1;
- } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
- ((*tmp)->name != NULL)) {
- if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
- res = xmlHashAddEntry2(partitions->triage,
- (*tmp)->name, NULL,
- (void *) (ptrdiff_t) (i + 1));
- else
- res = xmlHashAddEntry2(partitions->triage,
- (*tmp)->name, (*tmp)->ns,
- (void *) (ptrdiff_t) (i + 1));
- if (res != 0)
- is_determinist = -1;
- } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
- if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
- res = xmlHashAddEntry2(partitions->triage,
- BAD_CAST "#any", NULL,
- (void *) (ptrdiff_t) (i + 1));
- else
- res = xmlHashAddEntry2(partitions->triage,
- BAD_CAST "#any", (*tmp)->ns,
- (void *) (ptrdiff_t) (i + 1));
- if ((*tmp)->nameClass != NULL)
- is_determinist = 2;
- if (res != 0)
- is_determinist = -1;
- } else {
- is_determinist = -1;
- }
- tmp++;
- }
- } else {
- is_determinist = 0;
- }
- }
- partitions->groups = groups;
- /*
- * and save the partition list back in the def
- */
- def->data = partitions;
- if (is_mixed != 0)
- def->dflags |= IS_MIXED;
- if (is_determinist == 1)
- partitions->flags = IS_DETERMINIST;
- if (is_determinist == 2)
- partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
- return;
- error:
- xmlRngPErrMemory(ctxt, "in interleave computation\n");
- if (groups != NULL) {
- for (i = 0; i < nbgroups; i++)
- if (groups[i] != NULL) {
- if (groups[i]->defs != NULL)
- xmlFree(groups[i]->defs);
- xmlFree(groups[i]);
- }
- xmlFree(groups);
- }
- xmlRelaxNGFreePartition(partitions);
- }
- /**
- * xmlRelaxNGParseInterleave:
- * @ctxt: a Relax-NG parser context
- * @node: the data node.
- *
- * parse the content of a RelaxNG interleave node.
- *
- * Returns the definition pointer or NULL in case of error
- */
- static xmlRelaxNGDefinePtr
- xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
- {
- xmlRelaxNGDefinePtr def = NULL;
- xmlRelaxNGDefinePtr last = NULL, cur;
- xmlNodePtr child;
- def = xmlRelaxNGNewDefine(ctxt, node);
- if (def == NULL) {
- return (NULL);
- }
- def->type = XML_RELAXNG_INTERLEAVE;
- if (ctxt->interleaves == NULL)
- ctxt->interleaves = xmlHashCreate(10);
- if (ctxt->interleaves == NULL) {
- xmlRngPErrMemory(ctxt, "create interleaves\n");
- } else {
- char name[32];
- snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
- if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
- xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD,
- "Failed to add %s to hash table\n",
- (const xmlChar *) name, NULL);
- }
- }
- child = node->children;
- if (child == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT,
- "Element interleave is empty\n", NULL, NULL);
- }
- while (child != NULL) {
- if (IS_RELAXNG(child, "element")) {
- cur = xmlRelaxNGParseElement(ctxt, child);
- } else {
- cur = xmlRelaxNGParsePattern(ctxt, child);
- }
- if (cur != NULL) {
- cur->parent = def;
- if (last == NULL) {
- def->content = last = cur;
- } else {
- last->next = cur;
- last = cur;
- }
- }
- child = child->next;
- }
- return (def);
- }
- /**
- * xmlRelaxNGParseInclude:
- * @ctxt: a Relax-NG parser context
- * @node: the include node
- *
- * Integrate the content of an include node in the current grammar
- *
- * Returns 0 in case of success or -1 in case of error
- */
- static int
- xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
- {
- xmlRelaxNGIncludePtr incl;
- xmlNodePtr root;
- int ret = 0, tmp;
- incl = node->psvi;
- if (incl == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY,
- "Include node has no data\n", NULL, NULL);
- return (-1);
- }
- root = xmlDocGetRootElement(incl->doc);
- if (root == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n",
- NULL, NULL);
- return (-1);
- }
- if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
- xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
- "Include document root is not a grammar\n", NULL, NULL);
- return (-1);
- }
- /*
- * Merge the definition from both the include and the internal list
- */
- if (root->children != NULL) {
- tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
- if (tmp != 0)
- ret = -1;
- }
- if (node->children != NULL) {
- tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
- if (tmp != 0)
- ret = -1;
- }
- return (ret);
- }
- /**
- * xmlRelaxNGParseDefine:
- * @ctxt: a Relax-NG parser context
- * @node: the define node
- *
- * parse the content of a RelaxNG define element node.
- *
- * Returns 0 in case of success or -1 in case of error
- */
- static int
- xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
- {
- xmlChar *name;
- int ret = 0, tmp;
- xmlRelaxNGDefinePtr def;
- const xmlChar *olddefine;
- name = xmlGetProp(node, BAD_CAST "name");
- if (name == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING,
- "define has no name\n", NULL, NULL);
- } else {
- xmlRelaxNGNormExtSpace(name);
- if (xmlValidateNCName(name, 0)) {
- xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME,
- "define name '%s' is not an NCName\n", name, NULL);
- }
- def = xmlRelaxNGNewDefine(ctxt, node);
- if (def == NULL) {
- xmlFree(name);
- return (-1);
- }
- def->type = XML_RELAXNG_DEF;
- def->name = name;
- if (node->children == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY,
- "define has no children\n", NULL, NULL);
- } else {
- olddefine = ctxt->define;
- ctxt->define = name;
- def->content =
- xmlRelaxNGParsePatterns(ctxt, node->children, 0);
- ctxt->define = olddefine;
- }
- if (ctxt->grammar->defs == NULL)
- ctxt->grammar->defs = xmlHashCreate(10);
- if (ctxt->grammar->defs == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
- "Could not create definition hash\n", NULL, NULL);
- ret = -1;
- } else {
- tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
- if (tmp < 0) {
- xmlRelaxNGDefinePtr prev;
- prev = xmlHashLookup(ctxt->grammar->defs, name);
- if (prev == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
- "Internal error on define aggregation of %s\n",
- name, NULL);
- ret = -1;
- } else {
- while (prev->nextHash != NULL)
- prev = prev->nextHash;
- prev->nextHash = def;
- }
- }
- }
- }
- return (ret);
- }
- /**
- * xmlRelaxNGParseImportRef:
- * @payload: the parser context
- * @data: the current grammar
- * @name: the reference name
- *
- * Import import one references into the current grammar
- */
- static void
- xmlRelaxNGParseImportRef(void *payload, void *data, const xmlChar *name) {
- xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
- xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload;
- int tmp;
- def->dflags |= IS_EXTERNAL_REF;
- tmp = xmlHashAddEntry(ctxt->grammar->refs, name, def);
- if (tmp < 0) {
- xmlRelaxNGDefinePtr prev;
- prev = (xmlRelaxNGDefinePtr)
- xmlHashLookup(ctxt->grammar->refs, def->name);
- if (prev == NULL) {
- if (def->name != NULL) {
- xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
- "Error refs definitions '%s'\n",
- def->name, NULL);
- } else {
- xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
- "Error refs definitions\n",
- NULL, NULL);
- }
- } else {
- def->nextHash = prev->nextHash;
- prev->nextHash = def;
- }
- }
- }
- /**
- * xmlRelaxNGParseImportRefs:
- * @ctxt: the parser context
- * @grammar: the sub grammar
- *
- * Import references from the subgrammar into the current grammar
- *
- * Returns 0 in case of success, -1 in case of failure
- */
- static int
- xmlRelaxNGParseImportRefs(xmlRelaxNGParserCtxtPtr ctxt,
- xmlRelaxNGGrammarPtr grammar) {
- if ((ctxt == NULL) || (grammar == NULL) || (ctxt->grammar == NULL))
- return(-1);
- if (grammar->refs == NULL)
- return(0);
- if (ctxt->grammar->refs == NULL)
- ctxt->grammar->refs = xmlHashCreate(10);
- if (ctxt->grammar->refs == NULL) {
- xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
- "Could not create references hash\n", NULL, NULL);
- return(-1);
- }
- xmlHashScan(grammar->refs, xmlRelaxNGParseImportRef, ctxt);
- return(0);
- }
- /**
- * xmlRelaxNGProcessExternalRef:
- * @ctxt: the parser context
- * @node: the externalRef node
- *
- * Process and compile an externalRef node
- *
- * Returns the xmlRelaxNGDefinePtr or NULL in case of error
- */
- static xmlRelaxNGDefinePtr
- xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
- {
- xmlRelaxNGDocumentPtr docu;
- xmlNodePtr root, tmp;
- xmlChar *ns;
- int newNs = 0, oldflags;
- xmlRelaxNGDefinePtr def;
- docu = node->psvi;
- if (docu != NULL) {
- def = xmlRelaxNGNewDefine(ctxt, node);
- if (def == NULL)
- return (NULL);
- def->type = XML_RELAXNG_EXTERNALREF;
- if (docu->content == NULL) {
- /*
- * Then do the parsing for good
- */
- root = xmlDocGetRootElement(docu->doc);
- if (root == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY,
- "xmlRelaxNGParse: %s is empty\n", ctxt->URL,
- NULL);
- return (NULL);
- }
- /*
- * ns transmission rules
- */
- ns = xmlGetProp(root, BAD_CAST "ns");
- if (ns == NULL) {
- tmp = node;
- while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) {
- ns = xmlGetProp(tmp, BAD_CAST "ns");
- if (ns != NULL) {
- break;
- }
- tmp = tmp->parent;
- }
- if (ns != NULL) {
- xmlSetProp(root, BAD_CAST "ns", ns);
- newNs = 1;
- xmlFree(ns);
- }
- } else {
- xmlFree(ns);
- }
- /*
- * Parsing to get a precompiled schemas.
- */
- oldflags = ctxt->flags;
- ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
- docu->schema = xmlRelaxNGParseDocument(ctxt, root);
- ctxt->flags = oldflags;
- if ((docu->schema != NULL) &&
- (docu->schema->topgrammar != NULL)) {
- docu->content = docu->schema->topgrammar->start;
- if (docu->schema->topgrammar->refs)
- xmlRelaxNGParseImportRefs(ctxt, docu->schema->topgrammar);
- }
- /*
- * the externalRef may be reused in a different ns context
- */
- if (newNs == 1) {
- xmlUnsetProp(root, BAD_CAST "ns");
- }
- }
- def->content = docu->content;
- } else {
- def = NULL;
- }
- return (def);
- }
- /**
- * xmlRelaxNGParsePattern:
- * @ctxt: a Relax-NG parser context
- * @node: the pattern node.
- *
- * parse the content of a RelaxNG pattern node.
- *
- * Returns the definition pointer or NULL in case of error or if no
- * pattern is generated.
- */
- static xmlRelaxNGDefinePtr
- xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
- {
- xmlRelaxNGDefinePtr def = NULL;
- if (node == NULL) {
- return (NULL);
- }
- if (IS_RELAXNG(node, "element")) {
- def = xmlRelaxNGParseElement(ctxt, node);
- } else if (IS_RELAXNG(node, "attribute")) {
- def = xmlRelaxNGParseAttribute(ctxt, node);
- } else if (IS_RELAXNG(node, "empty")) {
- def = xmlRelaxNGNewDefine(ctxt, node);
- if (def == NULL)
- return (NULL);
- def->type = XML_RELAXNG_EMPTY;
- if (node->children != NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY,
- "empty: had a child node\n", NULL, NULL);
- }
- } else if (IS_RELAXNG(node, "text")) {
- def = xmlRelaxNGNewDefine(ctxt, node);
- if (def == NULL)
- return (NULL);
- def->type = XML_RELAXNG_TEXT;
- if (node->children != NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD,
- "text: had a child node\n", NULL, NULL);
- }
- } else if (IS_RELAXNG(node, "zeroOrMore")) {
- def = xmlRelaxNGNewDefine(ctxt, node);
- if (def == NULL)
- return (NULL);
- def->type = XML_RELAXNG_ZEROORMORE;
- if (node->children == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
- "Element %s is empty\n", node->name, NULL);
- } else {
- def->content =
- xmlRelaxNGParsePatterns(ctxt, node->children, 1);
- }
- } else if (IS_RELAXNG(node, "oneOrMore")) {
- def = xmlRelaxNGNewDefine(ctxt, node);
- if (def == NULL)
- return (NULL);
- def->type = XML_RELAXNG_ONEORMORE;
- if (node->children == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
- "Element %s is empty\n", node->name, NULL);
- } else {
- def->content =
- xmlRelaxNGParsePatterns(ctxt, node->children, 1);
- }
- } else if (IS_RELAXNG(node, "optional")) {
- def = xmlRelaxNGNewDefine(ctxt, node);
- if (def == NULL)
- return (NULL);
- def->type = XML_RELAXNG_OPTIONAL;
- if (node->children == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
- "Element %s is empty\n", node->name, NULL);
- } else {
- def->content =
- xmlRelaxNGParsePatterns(ctxt, node->children, 1);
- }
- } else if (IS_RELAXNG(node, "choice")) {
- def = xmlRelaxNGNewDefine(ctxt, node);
- if (def == NULL)
- return (NULL);
- def->type = XML_RELAXNG_CHOICE;
- if (node->children == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
- "Element %s is empty\n", node->name, NULL);
- } else {
- def->content =
- xmlRelaxNGParsePatterns(ctxt, node->children, 0);
- }
- } else if (IS_RELAXNG(node, "group")) {
- def = xmlRelaxNGNewDefine(ctxt, node);
- if (def == NULL)
- return (NULL);
- def->type = XML_RELAXNG_GROUP;
- if (node->children == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
- "Element %s is empty\n", node->name, NULL);
- } else {
- def->content =
- xmlRelaxNGParsePatterns(ctxt, node->children, 0);
- }
- } else if (IS_RELAXNG(node, "ref")) {
- def = xmlRelaxNGNewDefine(ctxt, node);
- if (def == NULL)
- return (NULL);
- def->type = XML_RELAXNG_REF;
- def->name = xmlGetProp(node, BAD_CAST "name");
- if (def->name == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n",
- NULL, NULL);
- } else {
- xmlRelaxNGNormExtSpace(def->name);
- if (xmlValidateNCName(def->name, 0)) {
- xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID,
- "ref name '%s' is not an NCName\n", def->name,
- NULL);
- }
- }
- if (node->children != NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n",
- NULL, NULL);
- }
- if (ctxt->grammar->refs == NULL)
- ctxt->grammar->refs = xmlHashCreate(10);
- if (ctxt->grammar->refs == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
- "Could not create references hash\n", NULL, NULL);
- def = NULL;
- } else {
- int tmp;
- tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
- if (tmp < 0) {
- xmlRelaxNGDefinePtr prev;
- prev = (xmlRelaxNGDefinePtr)
- xmlHashLookup(ctxt->grammar->refs, def->name);
- if (prev == NULL) {
- if (def->name != NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
- "Error refs definitions '%s'\n",
- def->name, NULL);
- } else {
- xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
- "Error refs definitions\n",
- NULL, NULL);
- }
- def = NULL;
- } else {
- def->nextHash = prev->nextHash;
- prev->nextHash = def;
- }
- }
- }
- } else if (IS_RELAXNG(node, "data")) {
- def = xmlRelaxNGParseData(ctxt, node);
- } else if (IS_RELAXNG(node, "value")) {
- def = xmlRelaxNGParseValue(ctxt, node);
- } else if (IS_RELAXNG(node, "list")) {
- def = xmlRelaxNGNewDefine(ctxt, node);
- if (def == NULL)
- return (NULL);
- def->type = XML_RELAXNG_LIST;
- if (node->children == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
- "Element %s is empty\n", node->name, NULL);
- } else {
- def->content =
- xmlRelaxNGParsePatterns(ctxt, node->children, 0);
- }
- } else if (IS_RELAXNG(node, "interleave")) {
- def = xmlRelaxNGParseInterleave(ctxt, node);
- } else if (IS_RELAXNG(node, "externalRef")) {
- def = xmlRelaxNGProcessExternalRef(ctxt, node);
- } else if (IS_RELAXNG(node, "notAllowed")) {
- def = xmlRelaxNGNewDefine(ctxt, node);
- if (def == NULL)
- return (NULL);
- def->type = XML_RELAXNG_NOT_ALLOWED;
- if (node->children != NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY,
- "xmlRelaxNGParse: notAllowed element is not empty\n",
- NULL, NULL);
- }
- } else if (IS_RELAXNG(node, "grammar")) {
- xmlRelaxNGGrammarPtr grammar, old;
- xmlRelaxNGGrammarPtr oldparent;
- #ifdef DEBUG_GRAMMAR
- xmlGenericError(xmlGenericErrorContext,
- "Found <grammar> pattern\n");
- #endif
- oldparent = ctxt->parentgrammar;
- old = ctxt->grammar;
- ctxt->parentgrammar = old;
- grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
- if (old != NULL) {
- ctxt->grammar = old;
- ctxt->parentgrammar = oldparent;
- #if 0
- if (grammar != NULL) {
- grammar->next = old->next;
- old->next = grammar;
- }
- #endif
- }
- if (grammar != NULL)
- def = grammar->start;
- else
- def = NULL;
- } else if (IS_RELAXNG(node, "parentRef")) {
- if (ctxt->parentgrammar == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT,
- "Use of parentRef without a parent grammar\n", NULL,
- NULL);
- return (NULL);
- }
- def = xmlRelaxNGNewDefine(ctxt, node);
- if (def == NULL)
- return (NULL);
- def->type = XML_RELAXNG_PARENTREF;
- def->name = xmlGetProp(node, BAD_CAST "name");
- if (def->name == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME,
- "parentRef has no name\n", NULL, NULL);
- } else {
- xmlRelaxNGNormExtSpace(def->name);
- if (xmlValidateNCName(def->name, 0)) {
- xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID,
- "parentRef name '%s' is not an NCName\n",
- def->name, NULL);
- }
- }
- if (node->children != NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY,
- "parentRef is not empty\n", NULL, NULL);
- }
- if (ctxt->parentgrammar->refs == NULL)
- ctxt->parentgrammar->refs = xmlHashCreate(10);
- if (ctxt->parentgrammar->refs == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
- "Could not create references hash\n", NULL, NULL);
- def = NULL;
- } else if (def->name != NULL) {
- int tmp;
- tmp =
- xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
- if (tmp < 0) {
- xmlRelaxNGDefinePtr prev;
- prev = (xmlRelaxNGDefinePtr)
- xmlHashLookup(ctxt->parentgrammar->refs, def->name);
- if (prev == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
- "Internal error parentRef definitions '%s'\n",
- def->name, NULL);
- def = NULL;
- } else {
- def->nextHash = prev->nextHash;
- prev->nextHash = def;
- }
- }
- }
- } else if (IS_RELAXNG(node, "mixed")) {
- if (node->children == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n",
- NULL, NULL);
- def = NULL;
- } else {
- def = xmlRelaxNGParseInterleave(ctxt, node);
- if (def != NULL) {
- xmlRelaxNGDefinePtr tmp;
- if ((def->content != NULL) && (def->content->next != NULL)) {
- tmp = xmlRelaxNGNewDefine(ctxt, node);
- if (tmp != NULL) {
- tmp->type = XML_RELAXNG_GROUP;
- tmp->content = def->content;
- def->content = tmp;
- }
- }
- tmp = xmlRelaxNGNewDefine(ctxt, node);
- if (tmp == NULL)
- return (def);
- tmp->type = XML_RELAXNG_TEXT;
- tmp->next = def->content;
- def->content = tmp;
- }
- }
- } else {
- xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT,
- "Unexpected node %s is not a pattern\n", node->name,
- NULL);
- def = NULL;
- }
- return (def);
- }
- /**
- * xmlRelaxNGParseAttribute:
- * @ctxt: a Relax-NG parser context
- * @node: the element node
- *
- * parse the content of a RelaxNG attribute node.
- *
- * Returns the definition pointer or NULL in case of error.
- */
- static xmlRelaxNGDefinePtr
- xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
- {
- xmlRelaxNGDefinePtr ret, cur;
- xmlNodePtr child;
- int old_flags;
- ret = xmlRelaxNGNewDefine(ctxt, node);
- if (ret == NULL)
- return (NULL);
- ret->type = XML_RELAXNG_ATTRIBUTE;
- ret->parent = ctxt->def;
- child = node->children;
- if (child == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY,
- "xmlRelaxNGParseattribute: attribute has no children\n",
- NULL, NULL);
- return (ret);
- }
- old_flags = ctxt->flags;
- ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
- cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
- if (cur != NULL)
- child = child->next;
- if (child != NULL) {
- cur = xmlRelaxNGParsePattern(ctxt, child);
- if (cur != NULL) {
- switch (cur->type) {
- case XML_RELAXNG_EMPTY:
- case XML_RELAXNG_NOT_ALLOWED:
- case XML_RELAXNG_TEXT:
- case XML_RELAXNG_ELEMENT:
- case XML_RELAXNG_DATATYPE:
- case XML_RELAXNG_VALUE:
- case XML_RELAXNG_LIST:
- case XML_RELAXNG_REF:
- case XML_RELAXNG_PARENTREF:
- case XML_RELAXNG_EXTERNALREF:
- case XML_RELAXNG_DEF:
- case XML_RELAXNG_ONEORMORE:
- case XML_RELAXNG_ZEROORMORE:
- case XML_RELAXNG_OPTIONAL:
- case XML_RELAXNG_CHOICE:
- case XML_RELAXNG_GROUP:
- case XML_RELAXNG_INTERLEAVE:
- case XML_RELAXNG_ATTRIBUTE:
- ret->content = cur;
- cur->parent = ret;
- break;
- case XML_RELAXNG_START:
- case XML_RELAXNG_PARAM:
- case XML_RELAXNG_EXCEPT:
- xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT,
- "attribute has invalid content\n", NULL,
- NULL);
- break;
- case XML_RELAXNG_NOOP:
- xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP,
- "RNG Internal error, noop found in attribute\n",
- NULL, NULL);
- break;
- }
- }
- child = child->next;
- }
- if (child != NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN,
- "attribute has multiple children\n", NULL, NULL);
- }
- ctxt->flags = old_flags;
- return (ret);
- }
- /**
- * xmlRelaxNGParseExceptNameClass:
- * @ctxt: a Relax-NG parser context
- * @node: the except node
- * @attr: 1 if within an attribute, 0 if within an element
- *
- * parse the content of a RelaxNG nameClass node.
- *
- * Returns the definition pointer or NULL in case of error.
- */
- static xmlRelaxNGDefinePtr
- xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
- xmlNodePtr node, int attr)
- {
- xmlRelaxNGDefinePtr ret, cur, last = NULL;
- xmlNodePtr child;
- if (!IS_RELAXNG(node, "except")) {
- xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING,
- "Expecting an except node\n", NULL, NULL);
- return (NULL);
- }
- if (node->next != NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE,
- "exceptNameClass allows only a single except node\n",
- NULL, NULL);
- }
- if (node->children == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n",
- NULL, NULL);
- return (NULL);
- }
- ret = xmlRelaxNGNewDefine(ctxt, node);
- if (ret == NULL)
- return (NULL);
- ret->type = XML_RELAXNG_EXCEPT;
- child = node->children;
- while (child != NULL) {
- cur = xmlRelaxNGNewDefine(ctxt, child);
- if (cur == NULL)
- break;
- if (attr)
- cur->type = XML_RELAXNG_ATTRIBUTE;
- else
- cur->type = XML_RELAXNG_ELEMENT;
- if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
- if (last == NULL) {
- ret->content = cur;
- } else {
- last->next = cur;
- }
- last = cur;
- }
- child = child->next;
- }
- return (ret);
- }
- /**
- * xmlRelaxNGParseNameClass:
- * @ctxt: a Relax-NG parser context
- * @node: the nameClass node
- * @def: the current definition
- *
- * parse the content of a RelaxNG nameClass node.
- *
- * Returns the definition pointer or NULL in case of error.
- */
- static xmlRelaxNGDefinePtr
- xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
- xmlRelaxNGDefinePtr def)
- {
- xmlRelaxNGDefinePtr ret, tmp;
- xmlChar *val;
- ret = def;
- if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
- (IS_RELAXNG(node, "nsName"))) {
- if ((def->type != XML_RELAXNG_ELEMENT) &&
- (def->type != XML_RELAXNG_ATTRIBUTE)) {
- ret = xmlRelaxNGNewDefine(ctxt, node);
- if (ret == NULL)
- return (NULL);
- ret->parent = def;
- if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
- ret->type = XML_RELAXNG_ATTRIBUTE;
- else
- ret->type = XML_RELAXNG_ELEMENT;
- }
- }
- if (IS_RELAXNG(node, "name")) {
- val = xmlNodeGetContent(node);
- xmlRelaxNGNormExtSpace(val);
- if (xmlValidateNCName(val, 0)) {
- if (node->parent != NULL)
- xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
- "Element %s name '%s' is not an NCName\n",
- node->parent->name, val);
- else
- xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
- "name '%s' is not an NCName\n",
- val, NULL);
- }
- ret->name = val;
- val = xmlGetProp(node, BAD_CAST "ns");
- ret->ns = val;
- if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
- (val != NULL) &&
- (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
- xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
- "Attribute with namespace '%s' is not allowed\n",
- val, NULL);
- }
- if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
- (val != NULL) &&
- (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
- xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME,
- "Attribute with QName 'xmlns' is not allowed\n",
- val, NULL);
- }
- } else if (IS_RELAXNG(node, "anyName")) {
- ret->name = NULL;
- ret->ns = NULL;
- if (node->children != NULL) {
- ret->nameClass =
- xmlRelaxNGParseExceptNameClass(ctxt, node->children,
- (def->type ==
- XML_RELAXNG_ATTRIBUTE));
- }
- } else if (IS_RELAXNG(node, "nsName")) {
- ret->name = NULL;
- ret->ns = xmlGetProp(node, BAD_CAST "ns");
- if (ret->ns == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS,
- "nsName has no ns attribute\n", NULL, NULL);
- }
- if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
- (ret->ns != NULL) &&
- (xmlStrEqual
- (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
- xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
- "Attribute with namespace '%s' is not allowed\n",
- ret->ns, NULL);
- }
- if (node->children != NULL) {
- ret->nameClass =
- xmlRelaxNGParseExceptNameClass(ctxt, node->children,
- (def->type ==
- XML_RELAXNG_ATTRIBUTE));
- }
- } else if (IS_RELAXNG(node, "choice")) {
- xmlNodePtr child;
- xmlRelaxNGDefinePtr last = NULL;
- if (def->type == XML_RELAXNG_CHOICE) {
- ret = def;
- } else {
- ret = xmlRelaxNGNewDefine(ctxt, node);
- if (ret == NULL)
- return (NULL);
- ret->parent = def;
- ret->type = XML_RELAXNG_CHOICE;
- }
- if (node->children == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY,
- "Element choice is empty\n", NULL, NULL);
- } else {
- child = node->children;
- while (child != NULL) {
- tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
- if (tmp != NULL) {
- if (last == NULL) {
- last = tmp;
- } else {
- last->next = tmp;
- last = tmp;
- }
- }
- child = child->next;
- }
- }
- } else {
- xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT,
- "expecting name, anyName, nsName or choice : got %s\n",
- (node == NULL ? (const xmlChar *) "nothing" : node->name),
- NULL);
- return (NULL);
- }
- if (ret != def) {
- if (def->nameClass == NULL) {
- def->nameClass = ret;
- } else {
- tmp = def->nameClass;
- while (tmp->next != NULL) {
- tmp = tmp->next;
- }
- tmp->next = ret;
- }
- }
- return (ret);
- }
- /**
- * xmlRelaxNGParseElement:
- * @ctxt: a Relax-NG parser context
- * @node: the element node
- *
- * parse the content of a RelaxNG element node.
- *
- * Returns the definition pointer or NULL in case of error.
- */
- static xmlRelaxNGDefinePtr
- xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
- {
- xmlRelaxNGDefinePtr ret, cur, last;
- xmlNodePtr child;
- const xmlChar *olddefine;
- ret = xmlRelaxNGNewDefine(ctxt, node);
- if (ret == NULL)
- return (NULL);
- ret->type = XML_RELAXNG_ELEMENT;
- ret->parent = ctxt->def;
- child = node->children;
- if (child == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY,
- "xmlRelaxNGParseElement: element has no children\n",
- NULL, NULL);
- return (ret);
- }
- cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
- if (cur != NULL)
- child = child->next;
- if (child == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT,
- "xmlRelaxNGParseElement: element has no content\n",
- NULL, NULL);
- return (ret);
- }
- olddefine = ctxt->define;
- ctxt->define = NULL;
- last = NULL;
- while (child != NULL) {
- cur = xmlRelaxNGParsePattern(ctxt, child);
- if (cur != NULL) {
- cur->parent = ret;
- switch (cur->type) {
- case XML_RELAXNG_EMPTY:
- case XML_RELAXNG_NOT_ALLOWED:
- case XML_RELAXNG_TEXT:
- case XML_RELAXNG_ELEMENT:
- case XML_RELAXNG_DATATYPE:
- case XML_RELAXNG_VALUE:
- case XML_RELAXNG_LIST:
- case XML_RELAXNG_REF:
- case XML_RELAXNG_PARENTREF:
- case XML_RELAXNG_EXTERNALREF:
- case XML_RELAXNG_DEF:
- case XML_RELAXNG_ZEROORMORE:
- case XML_RELAXNG_ONEORMORE:
- case XML_RELAXNG_OPTIONAL:
- case XML_RELAXNG_CHOICE:
- case XML_RELAXNG_GROUP:
- case XML_RELAXNG_INTERLEAVE:
- if (last == NULL) {
- ret->content = last = cur;
- } else {
- if ((last->type == XML_RELAXNG_ELEMENT) &&
- (ret->content == last)) {
- ret->content = xmlRelaxNGNewDefine(ctxt, node);
- if (ret->content != NULL) {
- ret->content->type = XML_RELAXNG_GROUP;
- ret->content->content = last;
- } else {
- ret->content = last;
- }
- }
- last->next = cur;
- last = cur;
- }
- break;
- case XML_RELAXNG_ATTRIBUTE:
- cur->next = ret->attrs;
- ret->attrs = cur;
- break;
- case XML_RELAXNG_START:
- xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
- "RNG Internal error, start found in element\n",
- NULL, NULL);
- break;
- case XML_RELAXNG_PARAM:
- xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
- "RNG Internal error, param found in element\n",
- NULL, NULL);
- break;
- case XML_RELAXNG_EXCEPT:
- xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
- "RNG Internal error, except found in element\n",
- NULL, NULL);
- break;
- case XML_RELAXNG_NOOP:
- xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
- "RNG Internal error, noop found in element\n",
- NULL, NULL);
- break;
- }
- }
- child = child->next;
- }
- ctxt->define = olddefine;
- return (ret);
- }
- /**
- * xmlRelaxNGParsePatterns:
- * @ctxt: a Relax-NG parser context
- * @nodes: list of nodes
- * @group: use an implicit <group> for elements
- *
- * parse the content of a RelaxNG start node.
- *
- * Returns the definition pointer or NULL in case of error.
- */
- static xmlRelaxNGDefinePtr
- xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
- int group)
- {
- xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
- parent = ctxt->def;
- while (nodes != NULL) {
- if (IS_RELAXNG(nodes, "element")) {
- cur = xmlRelaxNGParseElement(ctxt, nodes);
- if (cur == NULL)
- return (NULL);
- if (def == NULL) {
- def = last = cur;
- } else {
- if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
- (def == last)) {
- def = xmlRelaxNGNewDefine(ctxt, nodes);
- if (def == NULL)
- return (NULL);
- def->type = XML_RELAXNG_GROUP;
- def->content = last;
- }
- last->next = cur;
- last = cur;
- }
- cur->parent = parent;
- } else {
- cur = xmlRelaxNGParsePattern(ctxt, nodes);
- if (cur != NULL) {
- if (def == NULL) {
- def = last = cur;
- } else {
- last->next = cur;
- last = cur;
- }
- }
- }
- nodes = nodes->next;
- }
- return (def);
- }
- /**
- * xmlRelaxNGParseStart:
- * @ctxt: a Relax-NG parser context
- * @nodes: start children nodes
- *
- * parse the content of a RelaxNG start node.
- *
- * Returns 0 in case of success, -1 in case of error
- */
- static int
- xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
- {
- int ret = 0;
- xmlRelaxNGDefinePtr def = NULL, last;
- if (nodes == NULL) {
- xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n",
- NULL, NULL);
- return (-1);
- }
- if (IS_RELAXNG(nodes, "empty")) {
- def = xmlRelaxNGNewDefine(ctxt, nodes);
- if (def == NULL)
- return (-1);
- def->type = XML_RELAXNG_EMPTY;
- if (nodes->children != NULL) {
- xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT,
- "element empty is not empty\n", NULL, NULL);
- }
- } else if (IS_RELAXNG(nodes, "notAllowed")) {
- def = xmlRelaxNGNewDefine(ctxt, nodes);
- if (def == NULL)
- return (-1);
- def->type = XML_RELAXNG_NOT_ALLOWED;
- if (nodes->children != NULL) {
- xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY,
- "element notAllowed is not empty\n", NULL, NULL);
- }
- } else {
- def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
- }
- if (ctxt->grammar->start != NULL) {
- last = ctxt->grammar->start;
- while (last->next != NULL)
- last = last->next;
- last->next = def;
- } else {
- ctxt->grammar->start = def;
- }
- nodes = nodes->next;
- if (nodes != NULL) {
- xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT,
- "start more than one children\n", NULL, NULL);
- return (-1);
- }
- return (ret);
- }
- /**
- * xmlRelaxNGParseGrammarContent:
- * @ctxt: a Relax-NG parser context
- * @nodes: grammar children nodes
- *
- * parse the content of a RelaxNG grammar node.
- *
- * Returns 0 in case of success, -1 in case of error
- */
- static int
- xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
- xmlNodePtr nodes)
- {
- int ret = 0, tmp;
- if (nodes == NULL) {
- xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY,
- "grammar has no children\n", NULL, NULL);
- return (-1);
- }
- while (nodes != NULL) {
- if (IS_RELAXNG(nodes, "start")) {
- if (nodes->children == NULL) {
- xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY,
- "start has no children\n", NULL, NULL);
- } else {
- tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
- if (tmp != 0)
- ret = -1;
- }
- } else if (IS_RELAXNG(nodes, "define")) {
- tmp = xmlRelaxNGParseDefine(ctxt, nodes);
- if (tmp != 0)
- ret = -1;
- } else if (IS_RELAXNG(nodes, "include")) {
- tmp = xmlRelaxNGParseInclude(ctxt, nodes);
- if (tmp != 0)
- ret = -1;
- } else {
- xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
- "grammar has unexpected child %s\n", nodes->name,
- NULL);
- ret = -1;
- }
- nodes = nodes->next;
- }
- return (ret);
- }
- /**
- * xmlRelaxNGCheckReference:
- * @ref: the ref
- * @ctxt: a Relax-NG parser context
- * @name: the name associated to the defines
- *
- * Applies the 4.17. combine attribute rule for all the define
- * element of a given grammar using the same name.
- */
- static void
- xmlRelaxNGCheckReference(void *payload, void *data, const xmlChar * name)
- {
- xmlRelaxNGDefinePtr ref = (xmlRelaxNGDefinePtr) payload;
- xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
- xmlRelaxNGGrammarPtr grammar;
- xmlRelaxNGDefinePtr def, cur;
- /*
- * Those rules don't apply to imported ref from xmlRelaxNGParseImportRef
- */
- if (ref->dflags & IS_EXTERNAL_REF)
- return;
- grammar = ctxt->grammar;
- if (grammar == NULL) {
- xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
- "Internal error: no grammar in CheckReference %s\n",
- name, NULL);
- return;
- }
- if (ref->content != NULL) {
- xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
- "Internal error: reference has content in CheckReference %s\n",
- name, NULL);
- return;
- }
- if (grammar->defs != NULL) {
- def = xmlHashLookup(grammar->defs, name);
- if (def != NULL) {
- cur = ref;
- while (cur != NULL) {
- cur->content = def;
- cur = cur->nextHash;
- }
- } else {
- xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
- "Reference %s has no matching definition\n", name,
- NULL);
- }
- } else {
- xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
- "Reference %s has no matching definition\n", name,
- NULL);
- }
- }
- /**
- * xmlRelaxNGCheckCombine:
- * @define: the define(s) list
- * @ctxt: a Relax-NG parser context
- * @name: the name associated to the defines
- *
- * Applies the 4.17. combine attribute rule for all the define
- * element of a given grammar using the same name.
- */
- static void
- xmlRelaxNGCheckCombine(void *payload, void *data, const xmlChar * name)
- {
- xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) payload;
- xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
- xmlChar *combine;
- int choiceOrInterleave = -1;
- int missing = 0;
- xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
- if (define->nextHash == NULL)
- return;
- cur = define;
- while (cur != NULL) {
- combine = xmlGetProp(cur->node, BAD_CAST "combine");
- if (combine != NULL) {
- if (xmlStrEqual(combine, BAD_CAST "choice")) {
- if (choiceOrInterleave == -1)
- choiceOrInterleave = 1;
- else if (choiceOrInterleave == 0) {
- xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
- "Defines for %s use both 'choice' and 'interleave'\n",
- name, NULL);
- }
- } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
- if (choiceOrInterleave == -1)
- choiceOrInterleave = 0;
- else if (choiceOrInterleave == 1) {
- xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
- "Defines for %s use both 'choice' and 'interleave'\n",
- name, NULL);
- }
- } else {
- xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE,
- "Defines for %s use unknown combine value '%s''\n",
- name, combine);
- }
- xmlFree(combine);
- } else {
- if (missing == 0)
- missing = 1;
- else {
- xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE,
- "Some defines for %s needs the combine attribute\n",
- name, NULL);
- }
- }
- cur = cur->nextHash;
- }
- #ifdef DEBUG
- xmlGenericError(xmlGenericErrorContext,
- "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
- name, choiceOrInterleave);
- #endif
- if (choiceOrInterleave == -1)
- choiceOrInterleave = 0;
- cur = xmlRelaxNGNewDefine(ctxt, define->node);
- if (cur == NULL)
- return;
- if (choiceOrInterleave == 0)
- cur->type = XML_RELAXNG_INTERLEAVE;
- else
- cur->type = XML_RELAXNG_CHOICE;
- tmp = define;
- last = NULL;
- while (tmp != NULL) {
- if (tmp->content != NULL) {
- if (tmp->content->next != NULL) {
- /*
- * we need first to create a wrapper.
- */
- tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
- if (tmp2 == NULL)
- break;
- tmp2->type = XML_RELAXNG_GROUP;
- tmp2->content = tmp->content;
- } else {
- tmp2 = tmp->content;
- }
- if (last == NULL) {
- cur->content = tmp2;
- } else {
- last->next = tmp2;
- }
- last = tmp2;
- }
- tmp->content = cur;
- tmp = tmp->nextHash;
- }
- define->content = cur;
- if (choiceOrInterleave == 0) {
- if (ctxt->interleaves == NULL)
- ctxt->interleaves = xmlHashCreate(10);
- if (ctxt->interleaves == NULL) {
- xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
- "Failed to create interleaves hash table\n", NULL,
- NULL);
- } else {
- char tmpname[32];
- snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
- if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
- 0) {
- xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
- "Failed to add %s to hash table\n",
- (const xmlChar *) tmpname, NULL);
- }
- }
- }
- }
- /**
- * xmlRelaxNGCombineStart:
- * @ctxt: a Relax-NG parser context
- * @grammar: the grammar
- *
- * Applies the 4.17. combine rule for all the start
- * element of a given grammar.
- */
- static void
- xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
- xmlRelaxNGGrammarPtr grammar)
- {
- xmlRelaxNGDefinePtr starts;
- xmlChar *combine;
- int choiceOrInterleave = -1;
- int missing = 0;
- xmlRelaxNGDefinePtr cur;
- starts = grammar->start;
- if ((starts == NULL) || (starts->next == NULL))
- return;
- cur = starts;
- while (cur != NULL) {
- if ((cur->node == NULL) || (cur->node->parent == NULL) ||
- (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
- combine = NULL;
- xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING,
- "Internal error: start element not found\n", NULL,
- NULL);
- } else {
- combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
- }
- if (combine != NULL) {
- if (xmlStrEqual(combine, BAD_CAST "choice")) {
- if (choiceOrInterleave == -1)
- choiceOrInterleave = 1;
- else if (choiceOrInterleave == 0) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
- "<start> use both 'choice' and 'interleave'\n",
- NULL, NULL);
- }
- } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
- if (choiceOrInterleave == -1)
- choiceOrInterleave = 0;
- else if (choiceOrInterleave == 1) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
- "<start> use both 'choice' and 'interleave'\n",
- NULL, NULL);
- }
- } else {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE,
- "<start> uses unknown combine value '%s''\n",
- combine, NULL);
- }
- xmlFree(combine);
- } else {
- if (missing == 0)
- missing = 1;
- else {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE,
- "Some <start> element miss the combine attribute\n",
- NULL, NULL);
- }
- }
- cur = cur->next;
- }
- #ifdef DEBUG
- xmlGenericError(xmlGenericErrorContext,
- "xmlRelaxNGCombineStart(): merging <start>: %d\n",
- choiceOrInterleave);
- #endif
- if (choiceOrInterleave == -1)
- choiceOrInterleave = 0;
- cur = xmlRelaxNGNewDefine(ctxt, starts->node);
- if (cur == NULL)
- return;
- if (choiceOrInterleave == 0)
- cur->type = XML_RELAXNG_INTERLEAVE;
- else
- cur->type = XML_RELAXNG_CHOICE;
- cur->content = grammar->start;
- grammar->start = cur;
- if (choiceOrInterleave == 0) {
- if (ctxt->interleaves == NULL)
- ctxt->interleaves = xmlHashCreate(10);
- if (ctxt->interleaves == NULL) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
- "Failed to create interleaves hash table\n", NULL,
- NULL);
- } else {
- char tmpname[32];
- snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
- if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
- 0) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
- "Failed to add %s to hash table\n",
- (const xmlChar *) tmpname, NULL);
- }
- }
- }
- }
- /**
- * xmlRelaxNGCheckCycles:
- * @ctxt: a Relax-NG parser context
- * @nodes: grammar children nodes
- * @depth: the counter
- *
- * Check for cycles.
- *
- * Returns 0 if check passed, and -1 in case of error
- */
- static int
- xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
- xmlRelaxNGDefinePtr cur, int depth)
- {
- int ret = 0;
- while ((ret == 0) && (cur != NULL)) {
- if ((cur->type == XML_RELAXNG_REF) ||
- (cur->type == XML_RELAXNG_PARENTREF)) {
- if (cur->depth == -1) {
- cur->depth = depth;
- ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
- cur->depth = -2;
- } else if (depth == cur->depth) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE,
- "Detected a cycle in %s references\n",
- cur->name, NULL);
- return (-1);
- }
- } else if (cur->type == XML_RELAXNG_ELEMENT) {
- ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
- } else {
- ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
- }
- cur = cur->next;
- }
- return (ret);
- }
- /**
- * xmlRelaxNGTryUnlink:
- * @ctxt: a Relax-NG parser context
- * @cur: the definition to unlink
- * @parent: the parent definition
- * @prev: the previous sibling definition
- *
- * Try to unlink a definition. If not possible make it a NOOP
- *
- * Returns the new prev definition
- */
- static xmlRelaxNGDefinePtr
- xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
- xmlRelaxNGDefinePtr cur,
- xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev)
- {
- if (prev != NULL) {
- prev->next = cur->next;
- } else {
- if (parent != NULL) {
- if (parent->content == cur)
- parent->content = cur->next;
- else if (parent->attrs == cur)
- parent->attrs = cur->next;
- else if (parent->nameClass == cur)
- parent->nameClass = cur->next;
- } else {
- cur->type = XML_RELAXNG_NOOP;
- prev = cur;
- }
- }
- return (prev);
- }
- /**
- * xmlRelaxNGSimplify:
- * @ctxt: a Relax-NG parser context
- * @nodes: grammar children nodes
- *
- * Check for simplification of empty and notAllowed
- */
- static void
- xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
- xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent)
- {
- xmlRelaxNGDefinePtr prev = NULL;
- while (cur != NULL) {
- if ((cur->type == XML_RELAXNG_REF) ||
- (cur->type == XML_RELAXNG_PARENTREF)) {
- if (cur->depth != -3) {
- cur->depth = -3;
- xmlRelaxNGSimplify(ctxt, cur->content, cur);
- }
- } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
- cur->parent = parent;
- if ((parent != NULL) &&
- ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
- (parent->type == XML_RELAXNG_LIST) ||
- (parent->type == XML_RELAXNG_GROUP) ||
- (parent->type == XML_RELAXNG_INTERLEAVE) ||
- (parent->type == XML_RELAXNG_ONEORMORE) ||
- (parent->type == XML_RELAXNG_ZEROORMORE))) {
- parent->type = XML_RELAXNG_NOT_ALLOWED;
- break;
- }
- if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) {
- prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
- } else
- prev = cur;
- } else if (cur->type == XML_RELAXNG_EMPTY) {
- cur->parent = parent;
- if ((parent != NULL) &&
- ((parent->type == XML_RELAXNG_ONEORMORE) ||
- (parent->type == XML_RELAXNG_ZEROORMORE))) {
- parent->type = XML_RELAXNG_EMPTY;
- break;
- }
- if ((parent != NULL) &&
- ((parent->type == XML_RELAXNG_GROUP) ||
- (parent->type == XML_RELAXNG_INTERLEAVE))) {
- prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
- } else
- prev = cur;
- } else {
- cur->parent = parent;
- if (cur->content != NULL)
- xmlRelaxNGSimplify(ctxt, cur->content, cur);
- if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
- xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
- if (cur->nameClass != NULL)
- xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
- /*
- * On Elements, try to move attribute only generating rules on
- * the attrs rules.
- */
- if (cur->type == XML_RELAXNG_ELEMENT) {
- int attronly;
- xmlRelaxNGDefinePtr tmp, pre;
- while (cur->content != NULL) {
- attronly =
- xmlRelaxNGGenerateAttributes(ctxt, cur->content);
- if (attronly == 1) {
- /*
- * migrate cur->content to attrs
- */
- tmp = cur->content;
- cur->content = tmp->next;
- tmp->next = cur->attrs;
- cur->attrs = tmp;
- } else {
- /*
- * cur->content can generate elements or text
- */
- break;
- }
- }
- pre = cur->content;
- while ((pre != NULL) && (pre->next != NULL)) {
- tmp = pre->next;
- attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
- if (attronly == 1) {
- /*
- * migrate tmp to attrs
- */
- pre->next = tmp->next;
- tmp->next = cur->attrs;
- cur->attrs = tmp;
- } else {
- pre = tmp;
- }
- }
- }
- /*
- * This may result in a simplification
- */
- if ((cur->type == XML_RELAXNG_GROUP) ||
- (cur->type == XML_RELAXNG_INTERLEAVE)) {
- if (cur->content == NULL)
- cur->type = XML_RELAXNG_EMPTY;
- else if (cur->content->next == NULL) {
- if ((parent == NULL) && (prev == NULL)) {
- cur->type = XML_RELAXNG_NOOP;
- } else if (prev == NULL) {
- parent->content = cur->content;
- cur->content->next = cur->next;
- cur = cur->content;
- } else {
- cur->content->next = cur->next;
- prev->next = cur->content;
- cur = cur->content;
- }
- }
- }
- /*
- * the current node may have been transformed back
- */
- if ((cur->type == XML_RELAXNG_EXCEPT) &&
- (cur->content != NULL) &&
- (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
- prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
- } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
- if ((parent != NULL) &&
- ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
- (parent->type == XML_RELAXNG_LIST) ||
- (parent->type == XML_RELAXNG_GROUP) ||
- (parent->type == XML_RELAXNG_INTERLEAVE) ||
- (parent->type == XML_RELAXNG_ONEORMORE) ||
- (parent->type == XML_RELAXNG_ZEROORMORE))) {
- parent->type = XML_RELAXNG_NOT_ALLOWED;
- break;
- }
- if ((parent != NULL) &&
- (parent->type == XML_RELAXNG_CHOICE)) {
- prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
- } else
- prev = cur;
- } else if (cur->type == XML_RELAXNG_EMPTY) {
- if ((parent != NULL) &&
- ((parent->type == XML_RELAXNG_ONEORMORE) ||
- (parent->type == XML_RELAXNG_ZEROORMORE))) {
- parent->type = XML_RELAXNG_EMPTY;
- break;
- }
- if ((parent != NULL) &&
- ((parent->type == XML_RELAXNG_GROUP) ||
- (parent->type == XML_RELAXNG_INTERLEAVE) ||
- (parent->type == XML_RELAXNG_CHOICE))) {
- prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
- } else
- prev = cur;
- } else {
- prev = cur;
- }
- }
- cur = cur->next;
- }
- }
- /**
- * xmlRelaxNGGroupContentType:
- * @ct1: the first content type
- * @ct2: the second content type
- *
- * Try to group 2 content types
- *
- * Returns the content type
- */
- static xmlRelaxNGContentType
- xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
- xmlRelaxNGContentType ct2)
- {
- if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
- (ct2 == XML_RELAXNG_CONTENT_ERROR))
- return (XML_RELAXNG_CONTENT_ERROR);
- if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
- return (ct2);
- if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
- return (ct1);
- if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
- (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
- return (XML_RELAXNG_CONTENT_COMPLEX);
- return (XML_RELAXNG_CONTENT_ERROR);
- }
- /**
- * xmlRelaxNGMaxContentType:
- * @ct1: the first content type
- * @ct2: the second content type
- *
- * Compute the max content-type
- *
- * Returns the content type
- */
- static xmlRelaxNGContentType
- xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
- xmlRelaxNGContentType ct2)
- {
- if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
- (ct2 == XML_RELAXNG_CONTENT_ERROR))
- return (XML_RELAXNG_CONTENT_ERROR);
- if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
- (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
- return (XML_RELAXNG_CONTENT_SIMPLE);
- if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
- (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
- return (XML_RELAXNG_CONTENT_COMPLEX);
- return (XML_RELAXNG_CONTENT_EMPTY);
- }
- /**
- * xmlRelaxNGCheckRules:
- * @ctxt: a Relax-NG parser context
- * @cur: the current definition
- * @flags: some accumulated flags
- * @ptype: the parent type
- *
- * Check for rules in section 7.1 and 7.2
- *
- * Returns the content type of @cur
- */
- static xmlRelaxNGContentType
- xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
- xmlRelaxNGDefinePtr cur, int flags,
- xmlRelaxNGType ptype)
- {
- int nflags;
- xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
- while (cur != NULL) {
- ret = XML_RELAXNG_CONTENT_EMPTY;
- if ((cur->type == XML_RELAXNG_REF) ||
- (cur->type == XML_RELAXNG_PARENTREF)) {
- /*
- * This should actually be caught by list//element(ref) at the
- * element boundaries, c.f. Bug #159968 local refs are dropped
- * in step 4.19.
- */
- #if 0
- if (flags & XML_RELAXNG_IN_LIST) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF,
- "Found forbidden pattern list//ref\n", NULL,
- NULL);
- }
- #endif
- if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF,
- "Found forbidden pattern data/except//ref\n",
- NULL, NULL);
- }
- if (cur->content == NULL) {
- if (cur->type == XML_RELAXNG_PARENTREF)
- xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
- "Internal found no define for parent refs\n",
- NULL, NULL);
- else
- xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
- "Internal found no define for ref %s\n",
- (cur->name ? cur->name: BAD_CAST "null"), NULL);
- }
- if (cur->depth > -4) {
- cur->depth = -4;
- ret = xmlRelaxNGCheckRules(ctxt, cur->content,
- flags, cur->type);
- cur->depth = ret - 15;
- } else if (cur->depth == -4) {
- ret = XML_RELAXNG_CONTENT_COMPLEX;
- } else {
- ret = (xmlRelaxNGContentType) (cur->depth + 15);
- }
- } else if (cur->type == XML_RELAXNG_ELEMENT) {
- /*
- * The 7.3 Attribute derivation rule for groups is plugged there
- */
- xmlRelaxNGCheckGroupAttrs(ctxt, cur);
- if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM,
- "Found forbidden pattern data/except//element(ref)\n",
- NULL, NULL);
- }
- if (flags & XML_RELAXNG_IN_LIST) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM,
- "Found forbidden pattern list//element(ref)\n",
- NULL, NULL);
- }
- if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
- "Found forbidden pattern attribute//element(ref)\n",
- NULL, NULL);
- }
- if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
- "Found forbidden pattern attribute//element(ref)\n",
- NULL, NULL);
- }
- /*
- * reset since in the simple form elements are only child
- * of grammar/define
- */
- nflags = 0;
- ret =
- xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
- if (ret != XML_RELAXNG_CONTENT_EMPTY) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY,
- "Element %s attributes have a content type error\n",
- cur->name, NULL);
- }
- ret =
- xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
- cur->type);
- if (ret == XML_RELAXNG_CONTENT_ERROR) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR,
- "Element %s has a content type error\n",
- cur->name, NULL);
- } else {
- ret = XML_RELAXNG_CONTENT_COMPLEX;
- }
- } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
- if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR,
- "Found forbidden pattern attribute//attribute\n",
- NULL, NULL);
- }
- if (flags & XML_RELAXNG_IN_LIST) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR,
- "Found forbidden pattern list//attribute\n",
- NULL, NULL);
- }
- if (flags & XML_RELAXNG_IN_OOMGROUP) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR,
- "Found forbidden pattern oneOrMore//group//attribute\n",
- NULL, NULL);
- }
- if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR,
- "Found forbidden pattern oneOrMore//interleave//attribute\n",
- NULL, NULL);
- }
- if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR,
- "Found forbidden pattern data/except//attribute\n",
- NULL, NULL);
- }
- if (flags & XML_RELAXNG_IN_START) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR,
- "Found forbidden pattern start//attribute\n",
- NULL, NULL);
- }
- if ((!(flags & XML_RELAXNG_IN_ONEORMORE))
- && cur->name == NULL
- /* following is checking alternative name class readiness
- in case it went the "choice" route */
- && cur->nameClass == NULL) {
- if (cur->ns == NULL) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR,
- "Found anyName attribute without oneOrMore ancestor\n",
- NULL, NULL);
- } else {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR,
- "Found nsName attribute without oneOrMore ancestor\n",
- NULL, NULL);
- }
- }
- nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
- xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
- ret = XML_RELAXNG_CONTENT_EMPTY;
- } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
- (cur->type == XML_RELAXNG_ZEROORMORE)) {
- if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE,
- "Found forbidden pattern data/except//oneOrMore\n",
- NULL, NULL);
- }
- if (flags & XML_RELAXNG_IN_START) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE,
- "Found forbidden pattern start//oneOrMore\n",
- NULL, NULL);
- }
- nflags = flags | XML_RELAXNG_IN_ONEORMORE;
- ret =
- xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
- cur->type);
- ret = xmlRelaxNGGroupContentType(ret, ret);
- } else if (cur->type == XML_RELAXNG_LIST) {
- if (flags & XML_RELAXNG_IN_LIST) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST,
- "Found forbidden pattern list//list\n", NULL,
- NULL);
- }
- if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST,
- "Found forbidden pattern data/except//list\n",
- NULL, NULL);
- }
- if (flags & XML_RELAXNG_IN_START) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST,
- "Found forbidden pattern start//list\n", NULL,
- NULL);
- }
- nflags = flags | XML_RELAXNG_IN_LIST;
- ret =
- xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
- cur->type);
- } else if (cur->type == XML_RELAXNG_GROUP) {
- if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP,
- "Found forbidden pattern data/except//group\n",
- NULL, NULL);
- }
- if (flags & XML_RELAXNG_IN_START) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP,
- "Found forbidden pattern start//group\n", NULL,
- NULL);
- }
- if (flags & XML_RELAXNG_IN_ONEORMORE)
- nflags = flags | XML_RELAXNG_IN_OOMGROUP;
- else
- nflags = flags;
- ret =
- xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
- cur->type);
- /*
- * The 7.3 Attribute derivation rule for groups is plugged there
- */
- xmlRelaxNGCheckGroupAttrs(ctxt, cur);
- } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
- if (flags & XML_RELAXNG_IN_LIST) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE,
- "Found forbidden pattern list//interleave\n",
- NULL, NULL);
- }
- if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
- "Found forbidden pattern data/except//interleave\n",
- NULL, NULL);
- }
- if (flags & XML_RELAXNG_IN_START) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
- "Found forbidden pattern start//interleave\n",
- NULL, NULL);
- }
- if (flags & XML_RELAXNG_IN_ONEORMORE)
- nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
- else
- nflags = flags;
- ret =
- xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
- cur->type);
- } else if (cur->type == XML_RELAXNG_EXCEPT) {
- if ((cur->parent != NULL) &&
- (cur->parent->type == XML_RELAXNG_DATATYPE))
- nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
- else
- nflags = flags;
- ret =
- xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
- cur->type);
- } else if (cur->type == XML_RELAXNG_DATATYPE) {
- if (flags & XML_RELAXNG_IN_START) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA,
- "Found forbidden pattern start//data\n", NULL,
- NULL);
- }
- xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
- ret = XML_RELAXNG_CONTENT_SIMPLE;
- } else if (cur->type == XML_RELAXNG_VALUE) {
- if (flags & XML_RELAXNG_IN_START) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE,
- "Found forbidden pattern start//value\n", NULL,
- NULL);
- }
- xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
- ret = XML_RELAXNG_CONTENT_SIMPLE;
- } else if (cur->type == XML_RELAXNG_TEXT) {
- if (flags & XML_RELAXNG_IN_LIST) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT,
- "Found forbidden pattern list//text\n", NULL,
- NULL);
- }
- if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT,
- "Found forbidden pattern data/except//text\n",
- NULL, NULL);
- }
- if (flags & XML_RELAXNG_IN_START) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT,
- "Found forbidden pattern start//text\n", NULL,
- NULL);
- }
- ret = XML_RELAXNG_CONTENT_COMPLEX;
- } else if (cur->type == XML_RELAXNG_EMPTY) {
- if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY,
- "Found forbidden pattern data/except//empty\n",
- NULL, NULL);
- }
- if (flags & XML_RELAXNG_IN_START) {
- xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY,
- "Found forbidden pattern start//empty\n", NULL,
- NULL);
- }
- ret = XML_RELAXNG_CONTENT_EMPTY;
- } else if (cur->type == XML_RELAXNG_CHOICE) {
- xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
- ret =
- xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
- } else {
- ret =
- xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
- }
- cur = cur->next;
- if (ptype == XML_RELAXNG_GROUP) {
- val = xmlRelaxNGGroupContentType(val, ret);
- } else if (ptype == XML_RELAXNG_INTERLEAVE) {
- /*
- * TODO: scan complain that tmp is never used, seems on purpose
- * need double-checking
- */
- tmp = xmlRelaxNGGroupContentType(val, ret);
- if (tmp != XML_RELAXNG_CONTENT_ERROR)
- tmp = xmlRelaxNGMaxContentType(val, ret);
- } else if (ptype == XML_RELAXNG_CHOICE) {
- val = xmlRelaxNGMaxContentType(val, ret);
- } else if (ptype == XML_RELAXNG_LIST) {
- val = XML_RELAXNG_CONTENT_SIMPLE;
- } else if (ptype == XML_RELAXNG_EXCEPT) {
- if (ret == XML_RELAXNG_CONTENT_ERROR)
- val = XML_RELAXNG_CONTENT_ERROR;
- else
- val = XML_RELAXNG_CONTENT_SIMPLE;
- } else {
- val = xmlRelaxNGGroupContentType(val, ret);
- }
- }
- return (val);
- }
- /**
- * xmlRelaxNGParseGrammar:
- * @ctxt: a Relax-NG parser context
- * @nodes: grammar children nodes
- *
- * parse a Relax-NG <grammar> node
- *
- * Returns the internal xmlRelaxNGGrammarPtr built or
- * NULL in case of error
- */
- static xmlRelaxNGGrammarPtr
- xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
- {
- xmlRelaxNGGrammarPtr ret, tmp, old;
- #ifdef DEBUG_GRAMMAR
- xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
- #endif
- ret = xmlRelaxNGNewGrammar(ctxt);
- if (ret == NULL)
- return (NULL);
- /*
- * Link the new grammar in the tree
- */
- ret->parent = ctxt->grammar;
- if (ctxt->grammar != NULL) {
- tmp = ctxt->grammar->children;
- if (tmp == NULL) {
- ctxt->grammar->children = ret;
- } else {
- while (tmp->next != NULL)
- tmp = tmp->next;
- tmp->next = ret;
- }
- }
- old = ctxt->grammar;
- ctxt->grammar = ret;
- xmlRelaxNGParseGrammarContent(ctxt, nodes);
- ctxt->grammar = ret;
- if (ctxt->grammar == NULL) {
- xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
- "Failed to parse <grammar> content\n", NULL, NULL);
- } else if (ctxt->grammar->start == NULL) {
- xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START,
- "Element <grammar> has no <start>\n", NULL, NULL);
- }
- /*
- * Apply 4.17 merging rules to defines and starts
- */
- xmlRelaxNGCombineStart(ctxt, ret);
- if (ret->defs != NULL) {
- xmlHashScan(ret->defs, xmlRelaxNGCheckCombine, ctxt);
- }
- /*
- * link together defines and refs in this grammar
- */
- if (ret->refs != NULL) {
- xmlHashScan(ret->refs, xmlRelaxNGCheckReference, ctxt);
- }
- /* @@@@ */
- ctxt->grammar = old;
- return (ret);
- }
- /**
- * xmlRelaxNGParseDocument:
- * @ctxt: a Relax-NG parser context
- * @node: the root node of the RelaxNG schema
- *
- * parse a Relax-NG definition resource and build an internal
- * xmlRelaxNG structure which can be used to validate instances.
- *
- * Returns the internal XML RelaxNG structure built or
- * NULL in case of error
- */
- static xmlRelaxNGPtr
- xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
- {
- xmlRelaxNGPtr schema = NULL;
- const xmlChar *olddefine;
- xmlRelaxNGGrammarPtr old;
- if ((ctxt == NULL) || (node == NULL))
- return (NULL);
- schema = xmlRelaxNGNewRelaxNG(ctxt);
- if (schema == NULL)
- return (NULL);
- olddefine = ctxt->define;
- ctxt->define = NULL;
- if (IS_RELAXNG(node, "grammar")) {
- schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
- if (schema->topgrammar == NULL) {
- xmlRelaxNGFree(schema);
- return (NULL);
- }
- } else {
- xmlRelaxNGGrammarPtr tmp, ret;
- schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
- if (schema->topgrammar == NULL) {
- xmlRelaxNGFree(schema);
- return (NULL);
- }
- /*
- * Link the new grammar in the tree
- */
- ret->parent = ctxt->grammar;
- if (ctxt->grammar != NULL) {
- tmp = ctxt->grammar->children;
- if (tmp == NULL) {
- ctxt->grammar->children = ret;
- } else {
- while (tmp->next != NULL)
- tmp = tmp->next;
- tmp->next = ret;
- }
- }
- old = ctxt->grammar;
- ctxt->grammar = ret;
- xmlRelaxNGParseStart(ctxt, node);
- if (old != NULL)
- ctxt->grammar = old;
- }
- ctxt->define = olddefine;
- if (schema->topgrammar->start != NULL) {
- xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
- if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
- xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
- while ((schema->topgrammar->start != NULL) &&
- (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
- (schema->topgrammar->start->next != NULL))
- schema->topgrammar->start =
- schema->topgrammar->start->content;
- xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
- XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
- }
- }
- #ifdef DEBUG
- if (schema == NULL)
- xmlGenericError(xmlGenericErrorContext,
- "xmlRelaxNGParseDocument() failed\n");
- #endif
- return (schema);
- }
- /************************************************************************
- * *
- * Reading RelaxNGs *
- * *
- ************************************************************************/
- /**
- * xmlRelaxNGNewParserCtxt:
- * @URL: the location of the schema
- *
- * Create an XML RelaxNGs parse context for that file/resource expected
- * to contain an XML RelaxNGs file.
- *
- * Returns the parser context or NULL in case of error
- */
- xmlRelaxNGParserCtxtPtr
- xmlRelaxNGNewParserCtxt(const char *URL)
- {
- xmlRelaxNGParserCtxtPtr ret;
- if (URL == NULL)
- return (NULL);
- ret =
- (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
- if (ret == NULL) {
- xmlRngPErrMemory(NULL, "building parser\n");
- return (NULL);
- }
- memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
- ret->URL = xmlStrdup((const xmlChar *) URL);
- ret->error = xmlGenericError;
- ret->userData = xmlGenericErrorContext;
- return (ret);
- }
- /**
- * xmlRelaxNGNewMemParserCtxt:
- * @buffer: a pointer to a char array containing the schemas
- * @size: the size of the array
- *
- * Create an XML RelaxNGs parse context for that memory buffer expected
- * to contain an XML RelaxNGs file.
- *
- * Returns the parser context or NULL in case of error
- */
- xmlRelaxNGParserCtxtPtr
- xmlRelaxNGNewMemParserCtxt(const char *buffer, int size)
- {
- xmlRelaxNGParserCtxtPtr ret;
- if ((buffer == NULL) || (size <= 0))
- return (NULL);
- ret =
- (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
- if (ret == NULL) {
- xmlRngPErrMemory(NULL, "building parser\n");
- return (NULL);
- }
- memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
- ret->buffer = buffer;
- ret->size = size;
- ret->error = xmlGenericError;
- ret->userData = xmlGenericErrorContext;
- return (ret);
- }
- /**
- * xmlRelaxNGNewDocParserCtxt:
- * @doc: a preparsed document tree
- *
- * Create an XML RelaxNGs parser context for that document.
- * Note: since the process of compiling a RelaxNG schemas modifies the
- * document, the @doc parameter is duplicated internally.
- *
- * Returns the parser context or NULL in case of error
- */
- xmlRelaxNGParserCtxtPtr
- xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)
- {
- xmlRelaxNGParserCtxtPtr ret;
- xmlDocPtr copy;
- if (doc == NULL)
- return (NULL);
- copy = xmlCopyDoc(doc, 1);
- if (copy == NULL)
- return (NULL);
- ret =
- (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
- if (ret == NULL) {
- xmlRngPErrMemory(NULL, "building parser\n");
- xmlFreeDoc(copy);
- return (NULL);
- }
- memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
- ret->document = copy;
- ret->freedoc = 1;
- ret->userData = xmlGenericErrorContext;
- return (ret);
- }
- /**
- * xmlRelaxNGFreeParserCtxt:
- * @ctxt: the schema parser context
- *
- * Free the resources associated to the schema parser context
- */
- void
- xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)
- {
- if (ctxt == NULL)
- return;
- if (ctxt->URL != NULL)
- xmlFree(ctxt->URL);
- if (ctxt->doc != NULL)
- xmlRelaxNGFreeDocument(ctxt->doc);
- if (ctxt->interleaves != NULL)
- xmlHashFree(ctxt->interleaves, NULL);
- if (ctxt->documents != NULL)
- xmlRelaxNGFreeDocumentList(ctxt->documents);
- if (ctxt->includes != NULL)
- xmlRelaxNGFreeIncludeList(ctxt->includes);
- if (ctxt->docTab != NULL)
- xmlFree(ctxt->docTab);
- if (ctxt->incTab != NULL)
- xmlFree(ctxt->incTab);
- if (ctxt->defTab != NULL) {
- int i;
- for (i = 0; i < ctxt->defNr; i++)
- xmlRelaxNGFreeDefine(ctxt->defTab[i]);
- xmlFree(ctxt->defTab);
- }
- if ((ctxt->document != NULL) && (ctxt->freedoc))
- xmlFreeDoc(ctxt->document);
- xmlFree(ctxt);
- }
- /**
- * xmlRelaxNGNormExtSpace:
- * @value: a value
- *
- * Removes the leading and ending spaces of the value
- * The string is modified "in situ"
- */
- static void
- xmlRelaxNGNormExtSpace(xmlChar * value)
- {
- xmlChar *start = value;
- xmlChar *cur = value;
- if (value == NULL)
- return;
- while (IS_BLANK_CH(*cur))
- cur++;
- if (cur == start) {
- do {
- while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
- cur++;
- if (*cur == 0)
- return;
- start = cur;
- while (IS_BLANK_CH(*cur))
- cur++;
- if (*cur == 0) {
- *start = 0;
- return;
- }
- } while (1);
- } else {
- do {
- while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
- *start++ = *cur++;
- if (*cur == 0) {
- *start = 0;
- return;
- }
- /* don't try to normalize the inner spaces */
- while (IS_BLANK_CH(*cur))
- cur++;
- if (*cur == 0) {
- *start = 0;
- return;
- }
- *start++ = *cur++;
- } while (1);
- }
- }
- /**
- * xmlRelaxNGCleanupAttributes:
- * @ctxt: a Relax-NG parser context
- * @node: a Relax-NG node
- *
- * Check all the attributes on the given node
- */
- static void
- xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
- {
- xmlAttrPtr cur, next;
- cur = node->properties;
- while (cur != NULL) {
- next = cur->next;
- if ((cur->ns == NULL) ||
- (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
- if (xmlStrEqual(cur->name, BAD_CAST "name")) {
- if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
- (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
- (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
- (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
- (!xmlStrEqual(node->name, BAD_CAST "param")) &&
- (!xmlStrEqual(node->name, BAD_CAST "define"))) {
- xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
- "Attribute %s is not allowed on %s\n",
- cur->name, node->name);
- }
- } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
- if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
- (!xmlStrEqual(node->name, BAD_CAST "data"))) {
- xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
- "Attribute %s is not allowed on %s\n",
- cur->name, node->name);
- }
- } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
- if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
- (!xmlStrEqual(node->name, BAD_CAST "include"))) {
- xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
- "Attribute %s is not allowed on %s\n",
- cur->name, node->name);
- }
- } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
- if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
- (!xmlStrEqual(node->name, BAD_CAST "define"))) {
- xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
- "Attribute %s is not allowed on %s\n",
- cur->name, node->name);
- }
- } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
- xmlChar *val;
- xmlURIPtr uri;
- val = xmlNodeListGetString(node->doc, cur->children, 1);
- if (val != NULL) {
- if (val[0] != 0) {
- uri = xmlParseURI((const char *) val);
- if (uri == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI,
- "Attribute %s contains invalid URI %s\n",
- cur->name, val);
- } else {
- if (uri->scheme == NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE,
- "Attribute %s URI %s is not absolute\n",
- cur->name, val);
- }
- if (uri->fragment != NULL) {
- xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT,
- "Attribute %s URI %s has a fragment ID\n",
- cur->name, val);
- }
- xmlFreeURI(uri);
- }
- }
- xmlFree(val);
- }
- } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
- xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE,
- "Unknown attribute %s on %s\n", cur->name,
- node->name);
- }
- }
- cur = next;
- }
- }
- /**
- * xmlRelaxNGCleanupTree:
- * @ctxt: a Relax-NG parser context
- * @root: an xmlNodePtr subtree
- *
- * Cleanup the subtree from unwanted nodes for parsing, resolve
- * Include and externalRef lookups.
- */
- static void
- xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
- {
- xmlNodePtr cur, delete;
- delete = NULL;
- cur = root;
- while (cur != NULL) {
- if (delete != NULL) {
- xmlUnlinkNode(delete);
- xmlFreeNode(delete);
- delete = NULL;
- }
- if (cur->type == XML_ELEMENT_NODE) {
- /*
- * Simplification 4.1. Annotations
- */
- if ((cur->ns == NULL) ||
- (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
- if ((cur->parent != NULL) &&
- (cur->parent->type == XML_ELEMENT_NODE) &&
- ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
- (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
- (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
- xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT,
- "element %s doesn't allow foreign elements\n",
- cur->parent->name, NULL);
- }
- delete = cur;
- goto skip_children;
- } else {
- xmlRelaxNGCleanupAttributes(ctxt, cur);
- if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
- xmlChar *href, *ns, *base, *URL;
- xmlRelaxNGDocumentPtr docu;
- xmlNodePtr tmp;
- xmlURIPtr uri;
- ns = xmlGetProp(cur, BAD_CAST "ns");
- if (ns == NULL) {
- tmp = cur->parent;
- while ((tmp != NULL) &&
- (tmp->type == XML_ELEMENT_NODE)) {
- ns = xmlGetProp(tmp, BAD_CAST "ns");
- if (ns != NULL)
- break;
- tmp = tmp->parent;
- }
- }
- href = xmlGetProp(cur, BAD_CAST "href");
- if (href == NULL) {
- xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
- "xmlRelaxNGParse: externalRef has no href attribute\n",
- NULL, NULL);
- if (ns != NULL)
- xmlFree(ns);
- delete = cur;
- goto skip_children;
- }
- uri = xmlParseURI((const char *) href);
- if (uri == NULL) {
- xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
- "Incorrect URI for externalRef %s\n",
- href, NULL);
- if (ns != NULL)
- xmlFree(ns);
- if (href != NULL)
- xmlFree(href);
- delete = cur;
- goto skip_children;
- }
- if (uri->fragment != NULL) {
- xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
- "Fragment forbidden in URI for externalRef %s\n",
- href, NULL);
- if (ns != NULL)
- xmlFree(ns);
- xmlFreeURI(uri);
- if (href != NULL)
- xmlFree(href);
- delete = cur;
- goto skip_children;
- }
- xmlFreeURI(uri);
- base = xmlNodeGetBase(cur->doc, cur);
- URL = xmlBuildURI(href, base);
- if (URL == NULL) {
- xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
- "Failed to compute URL for externalRef %s\n",
- href, NULL);
- if (ns != NULL)
- xmlFree(ns);
- if (href != NULL)
- xmlFree(href);
- if (base != NULL)
- xmlFree(base);
- delete = cur;
- goto skip_children;
- }
- if (href != NULL)
- xmlFree(href);
- if (base != NULL)
- xmlFree(base);
- docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
- if (docu == NULL) {
- xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE,
- "Failed to load externalRef %s\n", URL,
- NULL);
- if (ns != NULL)
- xmlFree(ns);
- xmlFree(URL);
- delete = cur;
- goto skip_children;
- }
- if (ns != NULL)
- xmlFree(ns);
- xmlFree(URL);
- cur->psvi = docu;
- } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
- xmlChar *href, *ns, *base, *URL;
- xmlRelaxNGIncludePtr incl;
- xmlNodePtr tmp;
- href = xmlGetProp(cur, BAD_CAST "href");
- if (href == NULL) {
- xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
- "xmlRelaxNGParse: include has no href attribute\n",
- NULL, NULL);
- delete = cur;
- goto skip_children;
- }
- base = xmlNodeGetBase(cur->doc, cur);
- URL = xmlBuildURI(href, base);
- if (URL == NULL) {
- xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
- "Failed to compute URL for include %s\n",
- href, NULL);
- if (href != NULL)
- xmlFree(href);
- if (base != NULL)
- xmlFree(base);
- delete = cur;
- goto skip_children;
- }
- if (href != NULL)
- xmlFree(href);
- if (base != NULL)
- xmlFree(base);
- ns = xmlGetProp(cur, BAD_CAST "ns");
- if (ns == NULL) {
- tmp = cur->parent;
- while ((tmp != NULL) &&
- (tmp->type == XML_ELEMENT_NODE)) {
- ns = xmlGetProp(tmp, BAD_CAST "ns");
- if (ns != NULL)
- break;
- tmp = tmp->parent;
- }
- }
- incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
- if (ns != NULL)
- xmlFree(ns);
- if (incl == NULL) {
- xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE,
- "Failed to load include %s\n", URL,
- NULL);
- xmlFree(URL);
- delete = cur;
- goto skip_children;
- }
- xmlFree(URL);
- cur->psvi = incl;
- } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
- (xmlStrEqual(cur->name, BAD_CAST "attribute")))
- {
- xmlChar *name, *ns;
- xmlNodePtr text = NULL;
- /*
- * Simplification 4.8. name attribute of element
- * and attribute elements
- */
- name = xmlGetProp(cur, BAD_CAST "name");
- if (name != NULL) {
- if (cur->children == NULL) {
- text =
- xmlNewChild(cur, cur->ns, BAD_CAST "name",
- name);
- } else {
- xmlNodePtr node;
- node = xmlNewDocNode(cur->doc, cur->ns,
- BAD_CAST "name", NULL);
- if (node != NULL) {
- xmlAddPrevSibling(cur->children, node);
- text = xmlNewText(name);
- xmlAddChild(node, text);
- text = node;
- }
- }
- if (text == NULL) {
- xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE,
- "Failed to create a name %s element\n",
- name, NULL);
- }
- xmlUnsetProp(cur, BAD_CAST "name");
- xmlFree(name);
- ns = xmlGetProp(cur, BAD_CAST "ns");
- if (ns != NULL) {
- if (text != NULL) {
- xmlSetProp(text, BAD_CAST "ns", ns);
- /* xmlUnsetProp(cur, BAD_CAST "ns"); */
- }
- xmlFree(ns);
- } else if (xmlStrEqual(cur->name,
- BAD_CAST "attribute")) {
- xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
- }
- }
- } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
- (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
- (xmlStrEqual(cur->name, BAD_CAST "value"))) {
- /*
- * Simplification 4.8. name attribute of element
- * and attribute elements
- */
- if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
- xmlNodePtr node;
- xmlChar *ns = NULL;
- node = cur->parent;
- while ((node != NULL) &&
- (node->type == XML_ELEMENT_NODE)) {
- ns = xmlGetProp(node, BAD_CAST "ns");
- if (ns != NULL) {
- break;
- }
- node = node->parent;
- }
- if (ns == NULL) {
- xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
- } else {
- xmlSetProp(cur, BAD_CAST "ns", ns);
- xmlFree(ns);
- }
- }
- if (xmlStrEqual(cur->name, BAD_CAST "name")) {
- xmlChar *name, *local, *prefix;
- /*
- * Simplification: 4.10. QNames
- */
- name = xmlNodeGetContent(cur);
- if (name != NULL) {
- local = xmlSplitQName2(name, &prefix);
- if (local != NULL) {
- xmlNsPtr ns;
- ns = xmlSearchNs(cur->doc, cur, prefix);
- if (ns == NULL) {
- xmlRngPErr(ctxt, cur,
- XML_RNGP_PREFIX_UNDEFINED,
- "xmlRelaxNGParse: no namespace for prefix %s\n",
- prefix, NULL);
- } else {
- xmlSetProp(cur, BAD_CAST "ns",
- ns->href);
- xmlNodeSetContent(cur, local);
- }
- xmlFree(local);
- xmlFree(prefix);
- }
- xmlFree(name);
- }
- }
- /*
- * 4.16
- */
- if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
- if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
- xmlRngPErr(ctxt, cur,
- XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME,
- "Found nsName/except//nsName forbidden construct\n",
- NULL, NULL);
- }
- }
- } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
- (cur != root)) {
- int oldflags = ctxt->flags;
- /*
- * 4.16
- */
- if ((cur->parent != NULL) &&
- (xmlStrEqual
- (cur->parent->name, BAD_CAST "anyName"))) {
- ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
- xmlRelaxNGCleanupTree(ctxt, cur);
- ctxt->flags = oldflags;
- goto skip_children;
- } else if ((cur->parent != NULL) &&
- (xmlStrEqual
- (cur->parent->name, BAD_CAST "nsName"))) {
- ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
- xmlRelaxNGCleanupTree(ctxt, cur);
- ctxt->flags = oldflags;
- goto skip_children;
- }
- } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
- /*
- * 4.16
- */
- if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
- xmlRngPErr(ctxt, cur,
- XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME,
- "Found anyName/except//anyName forbidden construct\n",
- NULL, NULL);
- } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
- xmlRngPErr(ctxt, cur,
- XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME,
- "Found nsName/except//anyName forbidden construct\n",
- NULL, NULL);
- }
- }
- /*
- * This is not an else since "include" is transformed
- * into a div
- */
- if (xmlStrEqual(cur->name, BAD_CAST "div")) {
- xmlChar *ns;
- xmlNodePtr child, ins, tmp;
- /*
- * implements rule 4.11
- */
- ns = xmlGetProp(cur, BAD_CAST "ns");
- child = cur->children;
- ins = cur;
- while (child != NULL) {
- if (ns != NULL) {
- if (!xmlHasProp(child, BAD_CAST "ns")) {
- xmlSetProp(child, BAD_CAST "ns", ns);
- }
- }
- tmp = child->next;
- xmlUnlinkNode(child);
- ins = xmlAddNextSibling(ins, child);
- child = tmp;
- }
- if (ns != NULL)
- xmlFree(ns);
- /*
- * Since we are about to delete cur, if its nsDef is non-NULL we
- * need to preserve it (it contains the ns definitions for the
- * children we just moved). We'll just stick it on to the end
- * of cur->parent's list, since it's never going to be re-serialized
- * (bug 143738).
- */
- if ((cur->nsDef != NULL) && (cur->parent != NULL)) {
- xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef;
- while (parDef->next != NULL)
- parDef = parDef->next;
- parDef->next = cur->nsDef;
- cur->nsDef = NULL;
- }
- delete = cur;
- goto skip_children;
- }
- }
- }
- /*
- * Simplification 4.2 whitespaces
- */
- else if ((cur->type == XML_TEXT_NODE) ||
- (cur->type == XML_CDATA_SECTION_NODE)) {
- if (IS_BLANK_NODE(cur)) {
- if ((cur->parent != NULL) &&
- (cur->parent->type == XML_ELEMENT_NODE)) {
- if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value"))
- &&
- (!xmlStrEqual
- (cur->parent->name, BAD_CAST "param")))
- delete = cur;
- } else {
- delete = cur;
- goto skip_children;
- }
- }
- } else {
- delete = cur;
- goto skip_children;
- }
- /*
- * Skip to next node
- */
- if (cur->children != NULL) {
- if ((cur->children->type != XML_ENTITY_DECL) &&
- (cur->children->type != XML_ENTITY_REF_NODE) &&
- (cur->children->type != XML_ENTITY_NODE)) {
- cur = cur->children;
- continue;
- }
- }
- skip_children:
- if (cur->next != NULL) {
- cur = cur->next;
- continue;
- }
- do {
- cur = cur->parent;
- if (cur == NULL)
- break;
- if (cur == root) {
- cur = NULL;
- break;
- }
- if (cur->next != NULL) {
- cur = cur->next;
- break;
- }
- } while (cur != NULL);
- }
- if (delete != NULL) {
- xmlUnlinkNode(delete);
- xmlFreeNode(delete);
- delete = NULL;
- }
- }
- /**
- * xmlRelaxNGCleanupDoc:
- * @ctxt: a Relax-NG parser context
- * @doc: an xmldocPtr document pointer
- *
- * Cleanup the document from unwanted nodes for parsing, resolve
- * Include and externalRef lookups.
- *
- * Returns the cleaned up document or NULL in case of error
- */
- static xmlDocPtr
- xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
- {
- xmlNodePtr root;
- /*
- * Extract the root
- */
- root = xmlDocGetRootElement(doc);
- if (root == NULL) {
- xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
- ctxt->URL, NULL);
- return (NULL);
- }
- xmlRelaxNGCleanupTree(ctxt, root);
- return (doc);
- }
- /**
- * xmlRelaxNGParse:
- * @ctxt: a Relax-NG parser context
- *
- * parse a schema definition resource and build an internal
- * XML Schema structure which can be used to validate instances.
- *
- * Returns the internal XML RelaxNG structure built from the resource or
- * NULL in case of error
- */
- xmlRelaxNGPtr
- xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
- {
- xmlRelaxNGPtr ret = NULL;
- xmlDocPtr doc;
- xmlNodePtr root;
- xmlRelaxNGInitTypes();
- if (ctxt == NULL)
- return (NULL);
- /*
- * First step is to parse the input document into an DOM/Infoset
- */
- if (ctxt->URL != NULL) {
- doc = xmlReadFile((const char *) ctxt->URL,NULL,0);
- if (doc == NULL) {
- xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
- "xmlRelaxNGParse: could not load %s\n", ctxt->URL,
- NULL);
- return (NULL);
- }
- } else if (ctxt->buffer != NULL) {
- doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0);
- if (doc == NULL) {
- xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
- "xmlRelaxNGParse: could not parse schemas\n", NULL,
- NULL);
- return (NULL);
- }
- doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
- ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
- } else if (ctxt->document != NULL) {
- doc = ctxt->document;
- } else {
- xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
- "xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
- return (NULL);
- }
- ctxt->document = doc;
- /*
- * Some preprocessing of the document content
- */
- doc = xmlRelaxNGCleanupDoc(ctxt, doc);
- if (doc == NULL) {
- xmlFreeDoc(ctxt->document);
- ctxt->document = NULL;
- return (NULL);
- }
- /*
- * Then do the parsing for good
- */
- root = xmlDocGetRootElement(doc);
- if (root == NULL) {
- xmlRngPErr(ctxt, (xmlNodePtr) doc,
- XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
- (ctxt->URL ? ctxt->URL : BAD_CAST "schemas"), NULL);
- xmlFreeDoc(ctxt->document);
- ctxt->document = NULL;
- return (NULL);
- }
- ret = xmlRelaxNGParseDocument(ctxt, root);
- if (ret == NULL) {
- xmlFreeDoc(ctxt->document);
- ctxt->document = NULL;
- return (NULL);
- }
- /*
- * Check the ref/defines links
- */
- /*
- * try to preprocess interleaves
- */
- if (ctxt->interleaves != NULL) {
- xmlHashScan(ctxt->interleaves, xmlRelaxNGComputeInterleaves, ctxt);
- }
- /*
- * if there was a parsing error return NULL
- */
- if (ctxt->nbErrors > 0) {
- xmlRelaxNGFree(ret);
- ctxt->document = NULL;
- xmlFreeDoc(doc);
- return (NULL);
- }
- /*
- * try to compile (parts of) the schemas
- */
- if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
- if (ret->topgrammar->start->type != XML_RELAXNG_START) {
- xmlRelaxNGDefinePtr def;
- def = xmlRelaxNGNewDefine(ctxt, NULL);
- if (def != NULL) {
- def->type = XML_RELAXNG_START;
- def->content = ret->topgrammar->start;
- ret->topgrammar->start = def;
- }
- }
- xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
- }
- /*
- * Transfer the pointer for cleanup at the schema level.
- */
- ret->doc = doc;
- ctxt->document = NULL;
- ret->documents = ctxt->documents;
- ctxt->documents = NULL;
- ret->includes = ctxt->includes;
- ctxt->includes = NULL;
- ret->defNr = ctxt->defNr;
- ret->defTab = ctxt->defTab;
- ctxt->defTab = NULL;
- if (ctxt->idref == 1)
- ret->idref = 1;
- return (ret);
- }
- /**
- * xmlRelaxNGSetParserErrors:
- * @ctxt: a Relax-NG validation context
- * @err: the error callback
- * @warn: the warning callback
- * @ctx: contextual data for the callbacks
- *
- * Set the callback functions used to handle errors for a validation context
- */
- void
- xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
- xmlRelaxNGValidityErrorFunc err,
- xmlRelaxNGValidityWarningFunc warn, void *ctx)
- {
- if (ctxt == NULL)
- return;
- ctxt->error = err;
- ctxt->warning = warn;
- ctxt->serror = NULL;
- ctxt->userData = ctx;
- }
- /**
- * xmlRelaxNGGetParserErrors:
- * @ctxt: a Relax-NG validation context
- * @err: the error callback result
- * @warn: the warning callback result
- * @ctx: contextual data for the callbacks result
- *
- * Get the callback information used to handle errors for a validation context
- *
- * Returns -1 in case of failure, 0 otherwise.
- */
- int
- xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
- xmlRelaxNGValidityErrorFunc * err,
- xmlRelaxNGValidityWarningFunc * warn, void **ctx)
- {
- if (ctxt == NULL)
- return (-1);
- if (err != NULL)
- *err = ctxt->error;
- if (warn != NULL)
- *warn = ctxt->warning;
- if (ctx != NULL)
- *ctx = ctxt->userData;
- return (0);
- }
- /**
- * xmlRelaxNGSetParserStructuredErrors:
- * @ctxt: a Relax-NG parser context
- * @serror: the error callback
- * @ctx: contextual data for the callbacks
- *
- * Set the callback functions used to handle errors for a parsing context
- */
- void
- xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxtPtr ctxt,
- xmlStructuredErrorFunc serror,
- void *ctx)
- {
- if (ctxt == NULL)
- return;
- ctxt->serror = serror;
- ctxt->error = NULL;
- ctxt->warning = NULL;
- ctxt->userData = ctx;
- }
- #ifdef LIBXML_OUTPUT_ENABLED
- /************************************************************************
- * *
- * Dump back a compiled form *
- * *
- ************************************************************************/
- static void xmlRelaxNGDumpDefine(FILE * output,
- xmlRelaxNGDefinePtr define);
- /**
- * xmlRelaxNGDumpDefines:
- * @output: the file output
- * @defines: a list of define structures
- *
- * Dump a RelaxNG structure back
- */
- static void
- xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
- {
- while (defines != NULL) {
- xmlRelaxNGDumpDefine(output, defines);
- defines = defines->next;
- }
- }
- /**
- * xmlRelaxNGDumpDefine:
- * @output: the file output
- * @define: a define structure
- *
- * Dump a RelaxNG structure back
- */
- static void
- xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
- {
- if (define == NULL)
- return;
- switch (define->type) {
- case XML_RELAXNG_EMPTY:
- fprintf(output, "<empty/>\n");
- break;
- case XML_RELAXNG_NOT_ALLOWED:
- fprintf(output, "<notAllowed/>\n");
- break;
- case XML_RELAXNG_TEXT:
- fprintf(output, "<text/>\n");
- break;
- case XML_RELAXNG_ELEMENT:
- fprintf(output, "<element>\n");
- if (define->name != NULL) {
- fprintf(output, "<name");
- if (define->ns != NULL)
- fprintf(output, " ns=\"%s\"", define->ns);
- fprintf(output, ">%s</name>\n", define->name);
- }
- xmlRelaxNGDumpDefines(output, define->attrs);
- xmlRelaxNGDumpDefines(output, define->content);
- fprintf(output, "</element>\n");
- break;
- case XML_RELAXNG_LIST:
- fprintf(output, "<list>\n");
- xmlRelaxNGDumpDefines(output, define->content);
- fprintf(output, "</list>\n");
- break;
- case XML_RELAXNG_ONEORMORE:
- fprintf(output, "<oneOrMore>\n");
- xmlRelaxNGDumpDefines(output, define->content);
- fprintf(output, "</oneOrMore>\n");
- break;
- case XML_RELAXNG_ZEROORMORE:
- fprintf(output, "<zeroOrMore>\n");
- xmlRelaxNGDumpDefines(output, define->content);
- fprintf(output, "</zeroOrMore>\n");
- break;
- case XML_RELAXNG_CHOICE:
- fprintf(output, "<choice>\n");
- xmlRelaxNGDumpDefines(output, define->content);
- fprintf(output, "</choice>\n");
- break;
- case XML_RELAXNG_GROUP:
- fprintf(output, "<group>\n");
- xmlRelaxNGDumpDefines(output, define->content);
- fprintf(output, "</group>\n");
- break;
- case XML_RELAXNG_INTERLEAVE:
- fprintf(output, "<interleave>\n");
- xmlRelaxNGDumpDefines(output, define->content);
- fprintf(output, "</interleave>\n");
- break;
- case XML_RELAXNG_OPTIONAL:
- fprintf(output, "<optional>\n");
- xmlRelaxNGDumpDefines(output, define->content);
- fprintf(output, "</optional>\n");
- break;
- case XML_RELAXNG_ATTRIBUTE:
- fprintf(output, "<attribute>\n");
- xmlRelaxNGDumpDefines(output, define->content);
- fprintf(output, "</attribute>\n");
- break;
- case XML_RELAXNG_DEF:
- fprintf(output, "<define");
- if (define->name != NULL)
- fprintf(output, " name=\"%s\"", define->name);
- fprintf(output, ">\n");
- xmlRelaxNGDumpDefines(output, define->content);
- fprintf(output, "</define>\n");
- break;
- case XML_RELAXNG_REF:
- fprintf(output, "<ref");
- if (define->name != NULL)
- fprintf(output, " name=\"%s\"", define->name);
- fprintf(output, ">\n");
- xmlRelaxNGDumpDefines(output, define->content);
- fprintf(output, "</ref>\n");
- break;
- case XML_RELAXNG_PARENTREF:
- fprintf(output, "<parentRef");
- if (define->name != NULL)
- fprintf(output, " name=\"%s\"", define->name);
- fprintf(output, ">\n");
- xmlRelaxNGDumpDefines(output, define->content);
- fprintf(output, "</parentRef>\n");
- break;
- case XML_RELAXNG_EXTERNALREF:
- fprintf(output, "<externalRef>");
- xmlRelaxNGDumpDefines(output, define->content);
- fprintf(output, "</externalRef>\n");
- break;
- case XML_RELAXNG_DATATYPE:
- case XML_RELAXNG_VALUE:
- TODO break;
- case XML_RELAXNG_START:
- case XML_RELAXNG_EXCEPT:
- case XML_RELAXNG_PARAM:
- TODO break;
- case XML_RELAXNG_NOOP:
- xmlRelaxNGDumpDefines(output, define->content);
- break;
- }
- }
- /**
- * xmlRelaxNGDumpGrammar:
- * @output: the file output
- * @grammar: a grammar structure
- * @top: is this a top grammar
- *
- * Dump a RelaxNG structure back
- */
- static void
- xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
- {
- if (grammar == NULL)
- return;
- fprintf(output, "<grammar");
- if (top)
- fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
- switch (grammar->combine) {
- case XML_RELAXNG_COMBINE_UNDEFINED:
- break;
- case XML_RELAXNG_COMBINE_CHOICE:
- fprintf(output, " combine=\"choice\"");
- break;
- case XML_RELAXNG_COMBINE_INTERLEAVE:
- fprintf(output, " combine=\"interleave\"");
- break;
- default:
- fprintf(output, " <!-- invalid combine value -->");
- }
- fprintf(output, ">\n");
- if (grammar->start == NULL) {
- fprintf(output, " <!-- grammar had no start -->");
- } else {
- fprintf(output, "<start>\n");
- xmlRelaxNGDumpDefine(output, grammar->start);
- fprintf(output, "</start>\n");
- }
- /* TODO ? Dump the defines ? */
- fprintf(output, "</grammar>\n");
- }
- /**
- * xmlRelaxNGDump:
- * @output: the file output
- * @schema: a schema structure
- *
- * Dump a RelaxNG structure back
- */
- void
- xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
- {
- if (output == NULL)
- return;
- if (schema == NULL) {
- fprintf(output, "RelaxNG empty or failed to compile\n");
- return;
- }
- fprintf(output, "RelaxNG: ");
- if (schema->doc == NULL) {
- fprintf(output, "no document\n");
- } else if (schema->doc->URL != NULL) {
- fprintf(output, "%s\n", schema->doc->URL);
- } else {
- fprintf(output, "\n");
- }
- if (schema->topgrammar == NULL) {
- fprintf(output, "RelaxNG has no top grammar\n");
- return;
- }
- xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
- }
- /**
- * xmlRelaxNGDumpTree:
- * @output: the file output
- * @schema: a schema structure
- *
- * Dump the transformed RelaxNG tree.
- */
- void
- xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
- {
- if (output == NULL)
- return;
- if (schema == NULL) {
- fprintf(output, "RelaxNG empty or failed to compile\n");
- return;
- }
- if (schema->doc == NULL) {
- fprintf(output, "no document\n");
- } else {
- xmlDocDump(output, schema->doc);
- }
- }
- #endif /* LIBXML_OUTPUT_ENABLED */
- /************************************************************************
- * *
- * Validation of compiled content *
- * *
- ************************************************************************/
- static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGDefinePtr define);
- /**
- * xmlRelaxNGValidateCompiledCallback:
- * @exec: the regular expression instance
- * @token: the token which matched
- * @transdata: callback data, the define for the subelement if available
- @ @inputdata: callback data, the Relax NG validation context
- *
- * Handle the callback and if needed validate the element children.
- */
- static void
- xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
- const xmlChar * token,
- void *transdata, void *inputdata)
- {
- xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
- xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
- int ret;
- #ifdef DEBUG_COMPILE
- xmlGenericError(xmlGenericErrorContext,
- "Compiled callback for: '%s'\n", token);
- #endif
- if (ctxt == NULL) {
- fprintf(stderr, "callback on %s missing context\n", token);
- return;
- }
- if (define == NULL) {
- if (token[0] == '#')
- return;
- fprintf(stderr, "callback on %s missing define\n", token);
- if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
- ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
- return;
- }
- if ((ctxt == NULL) || (define == NULL)) {
- fprintf(stderr, "callback on %s missing info\n", token);
- if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
- ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
- return;
- } else if (define->type != XML_RELAXNG_ELEMENT) {
- fprintf(stderr, "callback on %s define is not element\n", token);
- if (ctxt->errNo == XML_RELAXNG_OK)
- ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
- return;
- }
- ret = xmlRelaxNGValidateDefinition(ctxt, define);
- if (ret != 0)
- ctxt->perr = ret;
- }
- /**
- * xmlRelaxNGValidateCompiledContent:
- * @ctxt: the RelaxNG validation context
- * @regexp: the regular expression as compiled
- * @content: list of children to test against the regexp
- *
- * Validate the content model of an element or start using the regexp
- *
- * Returns 0 in case of success, -1 in case of error.
- */
- static int
- xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRegexpPtr regexp, xmlNodePtr content)
- {
- xmlRegExecCtxtPtr exec;
- xmlNodePtr cur;
- int ret = 0;
- int oldperr;
- if ((ctxt == NULL) || (regexp == NULL))
- return (-1);
- oldperr = ctxt->perr;
- exec = xmlRegNewExecCtxt(regexp,
- xmlRelaxNGValidateCompiledCallback, ctxt);
- ctxt->perr = 0;
- cur = content;
- while (cur != NULL) {
- ctxt->state->seq = cur;
- switch (cur->type) {
- case XML_TEXT_NODE:
- case XML_CDATA_SECTION_NODE:
- if (xmlIsBlankNode(cur))
- break;
- ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
- if (ret < 0) {
- VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
- cur->parent->name);
- }
- break;
- case XML_ELEMENT_NODE:
- if (cur->ns != NULL) {
- ret = xmlRegExecPushString2(exec, cur->name,
- cur->ns->href, ctxt);
- } else {
- ret = xmlRegExecPushString(exec, cur->name, ctxt);
- }
- if (ret < 0) {
- VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
- }
- break;
- default:
- break;
- }
- if (ret < 0)
- break;
- /*
- * Switch to next element
- */
- cur = cur->next;
- }
- ret = xmlRegExecPushString(exec, NULL, NULL);
- if (ret == 1) {
- ret = 0;
- ctxt->state->seq = NULL;
- } else if (ret == 0) {
- /*
- * TODO: get some of the names needed to exit the current state of exec
- */
- VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
- ret = -1;
- if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
- xmlRelaxNGDumpValidError(ctxt);
- } else {
- ret = -1;
- }
- xmlRegFreeExecCtxt(exec);
- /*
- * There might be content model errors outside of the pure
- * regexp validation, e.g. for attribute values.
- */
- if ((ret == 0) && (ctxt->perr != 0)) {
- ret = ctxt->perr;
- }
- ctxt->perr = oldperr;
- return (ret);
- }
- /************************************************************************
- * *
- * Progressive validation of when possible *
- * *
- ************************************************************************/
- static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGDefinePtr defines);
- static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
- int dolog);
- static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
- /**
- * xmlRelaxNGElemPush:
- * @ctxt: the validation context
- * @exec: the regexp runtime for the new content model
- *
- * Push a new regexp for the current node content model on the stack
- *
- * Returns 0 in case of success and -1 in case of error.
- */
- static int
- xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
- {
- if (ctxt->elemTab == NULL) {
- ctxt->elemMax = 10;
- ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
- sizeof
- (xmlRegExecCtxtPtr));
- if (ctxt->elemTab == NULL) {
- xmlRngVErrMemory(ctxt, "validating\n");
- return (-1);
- }
- }
- if (ctxt->elemNr >= ctxt->elemMax) {
- ctxt->elemMax *= 2;
- ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
- ctxt->elemMax *
- sizeof
- (xmlRegExecCtxtPtr));
- if (ctxt->elemTab == NULL) {
- xmlRngVErrMemory(ctxt, "validating\n");
- return (-1);
- }
- }
- ctxt->elemTab[ctxt->elemNr++] = exec;
- ctxt->elem = exec;
- return (0);
- }
- /**
- * xmlRelaxNGElemPop:
- * @ctxt: the validation context
- *
- * Pop the regexp of the current node content model from the stack
- *
- * Returns the exec or NULL if empty
- */
- static xmlRegExecCtxtPtr
- xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
- {
- xmlRegExecCtxtPtr ret;
- if (ctxt->elemNr <= 0)
- return (NULL);
- ctxt->elemNr--;
- ret = ctxt->elemTab[ctxt->elemNr];
- ctxt->elemTab[ctxt->elemNr] = NULL;
- if (ctxt->elemNr > 0)
- ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
- else
- ctxt->elem = NULL;
- return (ret);
- }
- /**
- * xmlRelaxNGValidateProgressiveCallback:
- * @exec: the regular expression instance
- * @token: the token which matched
- * @transdata: callback data, the define for the subelement if available
- @ @inputdata: callback data, the Relax NG validation context
- *
- * Handle the callback and if needed validate the element children.
- * some of the in/out information are passed via the context in @inputdata.
- */
- static void
- xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
- ATTRIBUTE_UNUSED,
- const xmlChar * token,
- void *transdata, void *inputdata)
- {
- xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
- xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
- xmlRelaxNGValidStatePtr state, oldstate;
- xmlNodePtr node;
- int ret = 0, oldflags;
- #ifdef DEBUG_PROGRESSIVE
- xmlGenericError(xmlGenericErrorContext,
- "Progressive callback for: '%s'\n", token);
- #endif
- if (ctxt == NULL) {
- fprintf(stderr, "callback on %s missing context\n", token);
- return;
- }
- node = ctxt->pnode;
- ctxt->pstate = 1;
- if (define == NULL) {
- if (token[0] == '#')
- return;
- fprintf(stderr, "callback on %s missing define\n", token);
- if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
- ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
- ctxt->pstate = -1;
- return;
- }
- if ((ctxt == NULL) || (define == NULL)) {
- fprintf(stderr, "callback on %s missing info\n", token);
- if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
- ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
- ctxt->pstate = -1;
- return;
- } else if (define->type != XML_RELAXNG_ELEMENT) {
- fprintf(stderr, "callback on %s define is not element\n", token);
- if (ctxt->errNo == XML_RELAXNG_OK)
- ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
- ctxt->pstate = -1;
- return;
- }
- if (node->type != XML_ELEMENT_NODE) {
- VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
- if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
- xmlRelaxNGDumpValidError(ctxt);
- ctxt->pstate = -1;
- return;
- }
- if (define->contModel == NULL) {
- /*
- * this node cannot be validated in a streamable fashion
- */
- #ifdef DEBUG_PROGRESSIVE
- xmlGenericError(xmlGenericErrorContext,
- "Element '%s' validation is not streamable\n",
- token);
- #endif
- ctxt->pstate = 0;
- ctxt->pdef = define;
- return;
- }
- exec = xmlRegNewExecCtxt(define->contModel,
- xmlRelaxNGValidateProgressiveCallback, ctxt);
- if (exec == NULL) {
- ctxt->pstate = -1;
- return;
- }
- xmlRelaxNGElemPush(ctxt, exec);
- /*
- * Validate the attributes part of the content.
- */
- state = xmlRelaxNGNewValidState(ctxt, node);
- if (state == NULL) {
- ctxt->pstate = -1;
- return;
- }
- oldstate = ctxt->state;
- ctxt->state = state;
- if (define->attrs != NULL) {
- ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
- if (ret != 0) {
- ctxt->pstate = -1;
- VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
- }
- }
- if (ctxt->state != NULL) {
- ctxt->state->seq = NULL;
- ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
- if (ret != 0) {
- ctxt->pstate = -1;
- }
- xmlRelaxNGFreeValidState(ctxt, ctxt->state);
- } else if (ctxt->states != NULL) {
- int tmp = -1, i;
- oldflags = ctxt->flags;
- for (i = 0; i < ctxt->states->nbState; i++) {
- state = ctxt->states->tabState[i];
- ctxt->state = state;
- ctxt->state->seq = NULL;
- if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
- tmp = 0;
- break;
- }
- }
- if (tmp != 0) {
- /*
- * validation error, log the message for the "best" one
- */
- ctxt->flags |= FLAGS_IGNORABLE;
- xmlRelaxNGLogBestError(ctxt);
- }
- for (i = 0; i < ctxt->states->nbState; i++) {
- xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
- }
- xmlRelaxNGFreeStates(ctxt, ctxt->states);
- ctxt->states = NULL;
- if ((ret == 0) && (tmp == -1))
- ctxt->pstate = -1;
- ctxt->flags = oldflags;
- }
- if (ctxt->pstate == -1) {
- if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
- xmlRelaxNGDumpValidError(ctxt);
- }
- }
- ctxt->state = oldstate;
- }
- /**
- * xmlRelaxNGValidatePushElement:
- * @ctxt: the validation context
- * @doc: a document instance
- * @elem: an element instance
- *
- * Push a new element start on the RelaxNG validation stack.
- *
- * returns 1 if no validation problem was found or 0 if validating the
- * element requires a full node, and -1 in case of error.
- */
- int
- xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
- xmlDocPtr doc ATTRIBUTE_UNUSED,
- xmlNodePtr elem)
- {
- int ret = 1;
- if ((ctxt == NULL) || (elem == NULL))
- return (-1);
- #ifdef DEBUG_PROGRESSIVE
- xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
- #endif
- if (ctxt->elem == 0) {
- xmlRelaxNGPtr schema;
- xmlRelaxNGGrammarPtr grammar;
- xmlRegExecCtxtPtr exec;
- xmlRelaxNGDefinePtr define;
- schema = ctxt->schema;
- if (schema == NULL) {
- VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
- return (-1);
- }
- grammar = schema->topgrammar;
- if ((grammar == NULL) || (grammar->start == NULL)) {
- VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
- return (-1);
- }
- define = grammar->start;
- if (define->contModel == NULL) {
- ctxt->pdef = define;
- return (0);
- }
- exec = xmlRegNewExecCtxt(define->contModel,
- xmlRelaxNGValidateProgressiveCallback,
- ctxt);
- if (exec == NULL) {
- return (-1);
- }
- xmlRelaxNGElemPush(ctxt, exec);
- }
- ctxt->pnode = elem;
- ctxt->pstate = 0;
- if (elem->ns != NULL) {
- ret =
- xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
- ctxt);
- } else {
- ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
- }
- if (ret < 0) {
- VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
- } else {
- if (ctxt->pstate == 0)
- ret = 0;
- else if (ctxt->pstate < 0)
- ret = -1;
- else
- ret = 1;
- }
- #ifdef DEBUG_PROGRESSIVE
- if (ret < 0)
- xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
- elem->name);
- #endif
- return (ret);
- }
- /**
- * xmlRelaxNGValidatePushCData:
- * @ctxt: the RelaxNG validation context
- * @data: some character data read
- * @len: the length of the data
- *
- * check the CData parsed for validation in the current stack
- *
- * returns 1 if no validation problem was found or -1 otherwise
- */
- int
- xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
- const xmlChar * data, int len ATTRIBUTE_UNUSED)
- {
- int ret = 1;
- if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
- return (-1);
- #ifdef DEBUG_PROGRESSIVE
- xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
- #endif
- while (*data != 0) {
- if (!IS_BLANK_CH(*data))
- break;
- data++;
- }
- if (*data == 0)
- return (1);
- ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
- if (ret < 0) {
- VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
- #ifdef DEBUG_PROGRESSIVE
- xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
- #endif
- return (-1);
- }
- return (1);
- }
- /**
- * xmlRelaxNGValidatePopElement:
- * @ctxt: the RelaxNG validation context
- * @doc: a document instance
- * @elem: an element instance
- *
- * Pop the element end from the RelaxNG validation stack.
- *
- * returns 1 if no validation problem was found or 0 otherwise
- */
- int
- xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
- xmlDocPtr doc ATTRIBUTE_UNUSED,
- xmlNodePtr elem)
- {
- int ret;
- xmlRegExecCtxtPtr exec;
- if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
- return (-1);
- #ifdef DEBUG_PROGRESSIVE
- xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
- #endif
- /*
- * verify that we reached a terminal state of the content model.
- */
- exec = xmlRelaxNGElemPop(ctxt);
- ret = xmlRegExecPushString(exec, NULL, NULL);
- if (ret == 0) {
- /*
- * TODO: get some of the names needed to exit the current state of exec
- */
- VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
- ret = -1;
- } else if (ret < 0) {
- ret = -1;
- } else {
- ret = 1;
- }
- xmlRegFreeExecCtxt(exec);
- #ifdef DEBUG_PROGRESSIVE
- if (ret < 0)
- xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
- elem->name);
- #endif
- return (ret);
- }
- /**
- * xmlRelaxNGValidateFullElement:
- * @ctxt: the validation context
- * @doc: a document instance
- * @elem: an element instance
- *
- * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
- * 0 and the content of the node has been expanded.
- *
- * returns 1 if no validation problem was found or -1 in case of error.
- */
- int
- xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
- xmlDocPtr doc ATTRIBUTE_UNUSED,
- xmlNodePtr elem)
- {
- int ret;
- xmlRelaxNGValidStatePtr state;
- if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
- return (-1);
- #ifdef DEBUG_PROGRESSIVE
- xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
- #endif
- state = xmlRelaxNGNewValidState(ctxt, elem->parent);
- if (state == NULL) {
- return (-1);
- }
- state->seq = elem;
- ctxt->state = state;
- ctxt->errNo = XML_RELAXNG_OK;
- ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
- if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK))
- ret = -1;
- else
- ret = 1;
- xmlRelaxNGFreeValidState(ctxt, ctxt->state);
- ctxt->state = NULL;
- #ifdef DEBUG_PROGRESSIVE
- if (ret < 0)
- xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
- elem->name);
- #endif
- return (ret);
- }
- /************************************************************************
- * *
- * Generic interpreted validation implementation *
- * *
- ************************************************************************/
- static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGDefinePtr define);
- /**
- * xmlRelaxNGSkipIgnored:
- * @ctxt: a schema validation context
- * @node: the top node.
- *
- * Skip ignorable nodes in that context
- *
- * Returns the new sibling or NULL in case of error.
- */
- static xmlNodePtr
- xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
- xmlNodePtr node)
- {
- /*
- * TODO complete and handle entities
- */
- while ((node != NULL) &&
- ((node->type == XML_COMMENT_NODE) ||
- (node->type == XML_PI_NODE) ||
- (node->type == XML_XINCLUDE_START) ||
- (node->type == XML_XINCLUDE_END) ||
- (((node->type == XML_TEXT_NODE) ||
- (node->type == XML_CDATA_SECTION_NODE)) &&
- ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
- (IS_BLANK_NODE(node)))))) {
- node = node->next;
- }
- return (node);
- }
- /**
- * xmlRelaxNGNormalize:
- * @ctxt: a schema validation context
- * @str: the string to normalize
- *
- * Implements the normalizeWhiteSpace( s ) function from
- * section 6.2.9 of the spec
- *
- * Returns the new string or NULL in case of error.
- */
- static xmlChar *
- xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
- {
- xmlChar *ret, *p;
- const xmlChar *tmp;
- int len;
- if (str == NULL)
- return (NULL);
- tmp = str;
- while (*tmp != 0)
- tmp++;
- len = tmp - str;
- ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
- if (ret == NULL) {
- xmlRngVErrMemory(ctxt, "validating\n");
- return (NULL);
- }
- p = ret;
- while (IS_BLANK_CH(*str))
- str++;
- while (*str != 0) {
- if (IS_BLANK_CH(*str)) {
- while (IS_BLANK_CH(*str))
- str++;
- if (*str == 0)
- break;
- *p++ = ' ';
- } else
- *p++ = *str++;
- }
- *p = 0;
- return (ret);
- }
- /**
- * xmlRelaxNGValidateDatatype:
- * @ctxt: a Relax-NG validation context
- * @value: the string value
- * @type: the datatype definition
- * @node: the node
- *
- * Validate the given value against the datatype
- *
- * Returns 0 if the validation succeeded or an error code.
- */
- static int
- xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
- const xmlChar * value,
- xmlRelaxNGDefinePtr define, xmlNodePtr node)
- {
- int ret, tmp;
- xmlRelaxNGTypeLibraryPtr lib;
- void *result = NULL;
- xmlRelaxNGDefinePtr cur;
- if ((define == NULL) || (define->data == NULL)) {
- return (-1);
- }
- lib = (xmlRelaxNGTypeLibraryPtr) define->data;
- if (lib->check != NULL) {
- if ((define->attrs != NULL) &&
- (define->attrs->type == XML_RELAXNG_PARAM)) {
- ret =
- lib->check(lib->data, define->name, value, &result, node);
- } else {
- ret = lib->check(lib->data, define->name, value, NULL, node);
- }
- } else
- ret = -1;
- if (ret < 0) {
- VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
- if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
- lib->freef(lib->data, result);
- return (-1);
- } else if (ret == 1) {
- ret = 0;
- } else if (ret == 2) {
- VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
- } else {
- VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
- ret = -1;
- }
- cur = define->attrs;
- while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
- if (lib->facet != NULL) {
- tmp = lib->facet(lib->data, define->name, cur->name,
- cur->value, value, result);
- if (tmp != 0)
- ret = -1;
- }
- cur = cur->next;
- }
- if ((ret == 0) && (define->content != NULL)) {
- const xmlChar *oldvalue, *oldendvalue;
- oldvalue = ctxt->state->value;
- oldendvalue = ctxt->state->endvalue;
- ctxt->state->value = (xmlChar *) value;
- ctxt->state->endvalue = NULL;
- ret = xmlRelaxNGValidateValue(ctxt, define->content);
- ctxt->state->value = (xmlChar *) oldvalue;
- ctxt->state->endvalue = (xmlChar *) oldendvalue;
- }
- if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
- lib->freef(lib->data, result);
- return (ret);
- }
- /**
- * xmlRelaxNGNextValue:
- * @ctxt: a Relax-NG validation context
- *
- * Skip to the next value when validating within a list
- *
- * Returns 0 if the operation succeeded or an error code.
- */
- static int
- xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
- {
- xmlChar *cur;
- cur = ctxt->state->value;
- if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
- ctxt->state->value = NULL;
- ctxt->state->endvalue = NULL;
- return (0);
- }
- while (*cur != 0)
- cur++;
- while ((cur != ctxt->state->endvalue) && (*cur == 0))
- cur++;
- if (cur == ctxt->state->endvalue)
- ctxt->state->value = NULL;
- else
- ctxt->state->value = cur;
- return (0);
- }
- /**
- * xmlRelaxNGValidateValueList:
- * @ctxt: a Relax-NG validation context
- * @defines: the list of definitions to verify
- *
- * Validate the given set of definitions for the current value
- *
- * Returns 0 if the validation succeeded or an error code.
- */
- static int
- xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGDefinePtr defines)
- {
- int ret = 0;
- while (defines != NULL) {
- ret = xmlRelaxNGValidateValue(ctxt, defines);
- if (ret != 0)
- break;
- defines = defines->next;
- }
- return (ret);
- }
- /**
- * xmlRelaxNGValidateValue:
- * @ctxt: a Relax-NG validation context
- * @define: the definition to verify
- *
- * Validate the given definition for the current value
- *
- * Returns 0 if the validation succeeded or an error code.
- */
- static int
- xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGDefinePtr define)
- {
- int ret = 0, oldflags;
- xmlChar *value;
- value = ctxt->state->value;
- switch (define->type) {
- case XML_RELAXNG_EMPTY:{
- if ((value != NULL) && (value[0] != 0)) {
- int idx = 0;
- while (IS_BLANK_CH(value[idx]))
- idx++;
- if (value[idx] != 0)
- ret = -1;
- }
- break;
- }
- case XML_RELAXNG_TEXT:
- break;
- case XML_RELAXNG_VALUE:{
- if (!xmlStrEqual(value, define->value)) {
- if (define->name != NULL) {
- xmlRelaxNGTypeLibraryPtr lib;
- lib = (xmlRelaxNGTypeLibraryPtr) define->data;
- if ((lib != NULL) && (lib->comp != NULL)) {
- ret = lib->comp(lib->data, define->name,
- define->value, define->node,
- (void *) define->attrs,
- value, ctxt->state->node);
- } else
- ret = -1;
- if (ret < 0) {
- VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
- define->name);
- return (-1);
- } else if (ret == 1) {
- ret = 0;
- } else {
- ret = -1;
- }
- } else {
- xmlChar *nval, *nvalue;
- /*
- * TODO: trivial optimizations are possible by
- * computing at compile-time
- */
- nval = xmlRelaxNGNormalize(ctxt, define->value);
- nvalue = xmlRelaxNGNormalize(ctxt, value);
- if ((nval == NULL) || (nvalue == NULL) ||
- (!xmlStrEqual(nval, nvalue)))
- ret = -1;
- if (nval != NULL)
- xmlFree(nval);
- if (nvalue != NULL)
- xmlFree(nvalue);
- }
- }
- if (ret == 0)
- xmlRelaxNGNextValue(ctxt);
- break;
- }
- case XML_RELAXNG_DATATYPE:{
- ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
- ctxt->state->seq);
- if (ret == 0)
- xmlRelaxNGNextValue(ctxt);
- break;
- }
- case XML_RELAXNG_CHOICE:{
- xmlRelaxNGDefinePtr list = define->content;
- xmlChar *oldvalue;
- oldflags = ctxt->flags;
- ctxt->flags |= FLAGS_IGNORABLE;
- oldvalue = ctxt->state->value;
- while (list != NULL) {
- ret = xmlRelaxNGValidateValue(ctxt, list);
- if (ret == 0) {
- break;
- }
- ctxt->state->value = oldvalue;
- list = list->next;
- }
- ctxt->flags = oldflags;
- if (ret != 0) {
- if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
- xmlRelaxNGDumpValidError(ctxt);
- } else {
- if (ctxt->errNr > 0)
- xmlRelaxNGPopErrors(ctxt, 0);
- }
- break;
- }
- case XML_RELAXNG_LIST:{
- xmlRelaxNGDefinePtr list = define->content;
- xmlChar *oldvalue, *oldend, *val, *cur;
- #ifdef DEBUG_LIST
- int nb_values = 0;
- #endif
- oldvalue = ctxt->state->value;
- oldend = ctxt->state->endvalue;
- val = xmlStrdup(oldvalue);
- if (val == NULL) {
- val = xmlStrdup(BAD_CAST "");
- }
- if (val == NULL) {
- VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
- return (-1);
- }
- cur = val;
- while (*cur != 0) {
- if (IS_BLANK_CH(*cur)) {
- *cur = 0;
- cur++;
- #ifdef DEBUG_LIST
- nb_values++;
- #endif
- while (IS_BLANK_CH(*cur))
- *cur++ = 0;
- } else
- cur++;
- }
- #ifdef DEBUG_LIST
- xmlGenericError(xmlGenericErrorContext,
- "list value: '%s' found %d items\n",
- oldvalue, nb_values);
- nb_values = 0;
- #endif
- ctxt->state->endvalue = cur;
- cur = val;
- while ((*cur == 0) && (cur != ctxt->state->endvalue))
- cur++;
- ctxt->state->value = cur;
- while (list != NULL) {
- if (ctxt->state->value == ctxt->state->endvalue)
- ctxt->state->value = NULL;
- ret = xmlRelaxNGValidateValue(ctxt, list);
- if (ret != 0) {
- #ifdef DEBUG_LIST
- xmlGenericError(xmlGenericErrorContext,
- "Failed to validate value: '%s' with %d rule\n",
- ctxt->state->value, nb_values);
- #endif
- break;
- }
- #ifdef DEBUG_LIST
- nb_values++;
- #endif
- list = list->next;
- }
- if ((ret == 0) && (ctxt->state->value != NULL) &&
- (ctxt->state->value != ctxt->state->endvalue)) {
- VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA,
- ctxt->state->value);
- ret = -1;
- }
- xmlFree(val);
- ctxt->state->value = oldvalue;
- ctxt->state->endvalue = oldend;
- break;
- }
- case XML_RELAXNG_ONEORMORE:
- ret = xmlRelaxNGValidateValueList(ctxt, define->content);
- if (ret != 0) {
- break;
- }
- /* Falls through. */
- case XML_RELAXNG_ZEROORMORE:{
- xmlChar *cur, *temp;
- if ((ctxt->state->value == NULL) ||
- (*ctxt->state->value == 0)) {
- ret = 0;
- break;
- }
- oldflags = ctxt->flags;
- ctxt->flags |= FLAGS_IGNORABLE;
- cur = ctxt->state->value;
- temp = NULL;
- while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
- (temp != cur)) {
- temp = cur;
- ret =
- xmlRelaxNGValidateValueList(ctxt, define->content);
- if (ret != 0) {
- ctxt->state->value = temp;
- ret = 0;
- break;
- }
- cur = ctxt->state->value;
- }
- ctxt->flags = oldflags;
- if (ctxt->errNr > 0)
- xmlRelaxNGPopErrors(ctxt, 0);
- break;
- }
- case XML_RELAXNG_OPTIONAL:{
- xmlChar *temp;
- if ((ctxt->state->value == NULL) ||
- (*ctxt->state->value == 0)) {
- ret = 0;
- break;
- }
- oldflags = ctxt->flags;
- ctxt->flags |= FLAGS_IGNORABLE;
- temp = ctxt->state->value;
- ret = xmlRelaxNGValidateValue(ctxt, define->content);
- ctxt->flags = oldflags;
- if (ret != 0) {
- ctxt->state->value = temp;
- if (ctxt->errNr > 0)
- xmlRelaxNGPopErrors(ctxt, 0);
- ret = 0;
- break;
- }
- if (ctxt->errNr > 0)
- xmlRelaxNGPopErrors(ctxt, 0);
- break;
- }
- case XML_RELAXNG_EXCEPT:{
- xmlRelaxNGDefinePtr list;
- list = define->content;
- while (list != NULL) {
- ret = xmlRelaxNGValidateValue(ctxt, list);
- if (ret == 0) {
- ret = -1;
- break;
- } else
- ret = 0;
- list = list->next;
- }
- break;
- }
- case XML_RELAXNG_DEF:
- case XML_RELAXNG_GROUP:{
- xmlRelaxNGDefinePtr list;
- list = define->content;
- while (list != NULL) {
- ret = xmlRelaxNGValidateValue(ctxt, list);
- if (ret != 0) {
- ret = -1;
- break;
- } else
- ret = 0;
- list = list->next;
- }
- break;
- }
- case XML_RELAXNG_REF:
- case XML_RELAXNG_PARENTREF:
- if (define->content == NULL) {
- VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
- ret = -1;
- } else {
- ret = xmlRelaxNGValidateValue(ctxt, define->content);
- }
- break;
- default:
- TODO ret = -1;
- }
- return (ret);
- }
- /**
- * xmlRelaxNGValidateValueContent:
- * @ctxt: a Relax-NG validation context
- * @defines: the list of definitions to verify
- *
- * Validate the given definitions for the current value
- *
- * Returns 0 if the validation succeeded or an error code.
- */
- static int
- xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGDefinePtr defines)
- {
- int ret = 0;
- while (defines != NULL) {
- ret = xmlRelaxNGValidateValue(ctxt, defines);
- if (ret != 0)
- break;
- defines = defines->next;
- }
- return (ret);
- }
- /**
- * xmlRelaxNGAttributeMatch:
- * @ctxt: a Relax-NG validation context
- * @define: the definition to check
- * @prop: the attribute
- *
- * Check if the attribute matches the definition nameClass
- *
- * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
- */
- static int
- xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
- {
- int ret;
- if (define->name != NULL) {
- if (!xmlStrEqual(define->name, prop->name))
- return (0);
- }
- if (define->ns != NULL) {
- if (define->ns[0] == 0) {
- if (prop->ns != NULL)
- return (0);
- } else {
- if ((prop->ns == NULL) ||
- (!xmlStrEqual(define->ns, prop->ns->href)))
- return (0);
- }
- }
- if (define->nameClass == NULL)
- return (1);
- define = define->nameClass;
- if (define->type == XML_RELAXNG_EXCEPT) {
- xmlRelaxNGDefinePtr list;
- list = define->content;
- while (list != NULL) {
- ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
- if (ret == 1)
- return (0);
- if (ret < 0)
- return (ret);
- list = list->next;
- }
- } else if (define->type == XML_RELAXNG_CHOICE) {
- xmlRelaxNGDefinePtr list;
- list = define->nameClass;
- while (list != NULL) {
- ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
- if (ret == 1)
- return (1);
- if (ret < 0)
- return (ret);
- list = list->next;
- }
- return (0);
- } else {
- TODO}
- return (1);
- }
- /**
- * xmlRelaxNGValidateAttribute:
- * @ctxt: a Relax-NG validation context
- * @define: the definition to verify
- *
- * Validate the given attribute definition for that node
- *
- * Returns 0 if the validation succeeded or an error code.
- */
- static int
- xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGDefinePtr define)
- {
- int ret = 0, i;
- xmlChar *value, *oldvalue;
- xmlAttrPtr prop = NULL, tmp;
- xmlNodePtr oldseq;
- if (ctxt->state->nbAttrLeft <= 0)
- return (-1);
- if (define->name != NULL) {
- for (i = 0; i < ctxt->state->nbAttrs; i++) {
- tmp = ctxt->state->attrs[i];
- if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
- if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
- (tmp->ns == NULL)) ||
- ((tmp->ns != NULL) &&
- (xmlStrEqual(define->ns, tmp->ns->href)))) {
- prop = tmp;
- break;
- }
- }
- }
- if (prop != NULL) {
- value = xmlNodeListGetString(prop->doc, prop->children, 1);
- oldvalue = ctxt->state->value;
- oldseq = ctxt->state->seq;
- ctxt->state->seq = (xmlNodePtr) prop;
- ctxt->state->value = value;
- ctxt->state->endvalue = NULL;
- ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
- if (ctxt->state->value != NULL)
- value = ctxt->state->value;
- if (value != NULL)
- xmlFree(value);
- ctxt->state->value = oldvalue;
- ctxt->state->seq = oldseq;
- if (ret == 0) {
- /*
- * flag the attribute as processed
- */
- ctxt->state->attrs[i] = NULL;
- ctxt->state->nbAttrLeft--;
- }
- } else {
- ret = -1;
- }
- #ifdef DEBUG
- xmlGenericError(xmlGenericErrorContext,
- "xmlRelaxNGValidateAttribute(%s): %d\n",
- define->name, ret);
- #endif
- } else {
- for (i = 0; i < ctxt->state->nbAttrs; i++) {
- tmp = ctxt->state->attrs[i];
- if ((tmp != NULL) &&
- (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
- prop = tmp;
- break;
- }
- }
- if (prop != NULL) {
- value = xmlNodeListGetString(prop->doc, prop->children, 1);
- oldvalue = ctxt->state->value;
- oldseq = ctxt->state->seq;
- ctxt->state->seq = (xmlNodePtr) prop;
- ctxt->state->value = value;
- ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
- if (ctxt->state->value != NULL)
- value = ctxt->state->value;
- if (value != NULL)
- xmlFree(value);
- ctxt->state->value = oldvalue;
- ctxt->state->seq = oldseq;
- if (ret == 0) {
- /*
- * flag the attribute as processed
- */
- ctxt->state->attrs[i] = NULL;
- ctxt->state->nbAttrLeft--;
- }
- } else {
- ret = -1;
- }
- #ifdef DEBUG
- if (define->ns != NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
- define->ns, ret);
- } else {
- xmlGenericError(xmlGenericErrorContext,
- "xmlRelaxNGValidateAttribute(anyName): %d\n",
- ret);
- }
- #endif
- }
- return (ret);
- }
- /**
- * xmlRelaxNGValidateAttributeList:
- * @ctxt: a Relax-NG validation context
- * @define: the list of definition to verify
- *
- * Validate the given node against the list of attribute definitions
- *
- * Returns 0 if the validation succeeded or an error code.
- */
- static int
- xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGDefinePtr defines)
- {
- int ret = 0, res;
- int needmore = 0;
- xmlRelaxNGDefinePtr cur;
- cur = defines;
- while (cur != NULL) {
- if (cur->type == XML_RELAXNG_ATTRIBUTE) {
- if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
- ret = -1;
- } else
- needmore = 1;
- cur = cur->next;
- }
- if (!needmore)
- return (ret);
- cur = defines;
- while (cur != NULL) {
- if (cur->type != XML_RELAXNG_ATTRIBUTE) {
- if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
- res = xmlRelaxNGValidateDefinition(ctxt, cur);
- if (res < 0)
- ret = -1;
- } else {
- VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
- return (-1);
- }
- if (res == -1) /* continues on -2 */
- break;
- }
- cur = cur->next;
- }
- return (ret);
- }
- /**
- * xmlRelaxNGNodeMatchesList:
- * @node: the node
- * @list: a NULL terminated array of definitions
- *
- * Check if a node can be matched by one of the definitions
- *
- * Returns 1 if matches 0 otherwise
- */
- static int
- xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
- {
- xmlRelaxNGDefinePtr cur;
- int i = 0, tmp;
- if ((node == NULL) || (list == NULL))
- return (0);
- cur = list[i++];
- while (cur != NULL) {
- if ((node->type == XML_ELEMENT_NODE) &&
- (cur->type == XML_RELAXNG_ELEMENT)) {
- tmp = xmlRelaxNGElementMatch(NULL, cur, node);
- if (tmp == 1)
- return (1);
- } else if (((node->type == XML_TEXT_NODE) ||
- (node->type == XML_CDATA_SECTION_NODE)) &&
- ((cur->type == XML_RELAXNG_DATATYPE) ||
- (cur->type == XML_RELAXNG_LIST) ||
- (cur->type == XML_RELAXNG_TEXT) ||
- (cur->type == XML_RELAXNG_VALUE))) {
- return (1);
- }
- cur = list[i++];
- }
- return (0);
- }
- /**
- * xmlRelaxNGValidateInterleave:
- * @ctxt: a Relax-NG validation context
- * @define: the definition to verify
- *
- * Validate an interleave definition for a node.
- *
- * Returns 0 if the validation succeeded or an error code.
- */
- static int
- xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGDefinePtr define)
- {
- int ret = 0, i, nbgroups;
- int errNr = ctxt->errNr;
- int oldflags;
- xmlRelaxNGValidStatePtr oldstate;
- xmlRelaxNGPartitionPtr partitions;
- xmlRelaxNGInterleaveGroupPtr group = NULL;
- xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
- xmlNodePtr *list = NULL, *lasts = NULL;
- if (define->data != NULL) {
- partitions = (xmlRelaxNGPartitionPtr) define->data;
- nbgroups = partitions->nbgroups;
- } else {
- VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
- return (-1);
- }
- /*
- * Optimizations for MIXED
- */
- oldflags = ctxt->flags;
- if (define->dflags & IS_MIXED) {
- ctxt->flags |= FLAGS_MIXED_CONTENT;
- if (nbgroups == 2) {
- /*
- * this is a pure <mixed> case
- */
- if (ctxt->state != NULL)
- ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
- ctxt->state->seq);
- if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
- ret = xmlRelaxNGValidateDefinition(ctxt,
- partitions->groups[1]->
- rule);
- else
- ret = xmlRelaxNGValidateDefinition(ctxt,
- partitions->groups[0]->
- rule);
- if (ret == 0) {
- if (ctxt->state != NULL)
- ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
- ctxt->state->
- seq);
- }
- ctxt->flags = oldflags;
- return (ret);
- }
- }
- /*
- * Build arrays to store the first and last node of the chain
- * pertaining to each group
- */
- list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
- if (list == NULL) {
- xmlRngVErrMemory(ctxt, "validating\n");
- return (-1);
- }
- memset(list, 0, nbgroups * sizeof(xmlNodePtr));
- lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
- if (lasts == NULL) {
- xmlRngVErrMemory(ctxt, "validating\n");
- return (-1);
- }
- memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
- /*
- * Walk the sequence of children finding the right group and
- * sorting them in sequences.
- */
- cur = ctxt->state->seq;
- cur = xmlRelaxNGSkipIgnored(ctxt, cur);
- start = cur;
- while (cur != NULL) {
- ctxt->state->seq = cur;
- if ((partitions->triage != NULL) &&
- (partitions->flags & IS_DETERMINIST)) {
- void *tmp = NULL;
- if ((cur->type == XML_TEXT_NODE) ||
- (cur->type == XML_CDATA_SECTION_NODE)) {
- tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
- NULL);
- } else if (cur->type == XML_ELEMENT_NODE) {
- if (cur->ns != NULL) {
- tmp = xmlHashLookup2(partitions->triage, cur->name,
- cur->ns->href);
- if (tmp == NULL)
- tmp = xmlHashLookup2(partitions->triage,
- BAD_CAST "#any",
- cur->ns->href);
- } else
- tmp =
- xmlHashLookup2(partitions->triage, cur->name,
- NULL);
- if (tmp == NULL)
- tmp =
- xmlHashLookup2(partitions->triage, BAD_CAST "#any",
- NULL);
- }
- if (tmp == NULL) {
- i = nbgroups;
- } else {
- i = ((ptrdiff_t) tmp) - 1;
- if (partitions->flags & IS_NEEDCHECK) {
- group = partitions->groups[i];
- if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
- i = nbgroups;
- }
- }
- } else {
- for (i = 0; i < nbgroups; i++) {
- group = partitions->groups[i];
- if (group == NULL)
- continue;
- if (xmlRelaxNGNodeMatchesList(cur, group->defs))
- break;
- }
- }
- /*
- * We break as soon as an element not matched is found
- */
- if (i >= nbgroups) {
- break;
- }
- if (lasts[i] != NULL) {
- lasts[i]->next = cur;
- lasts[i] = cur;
- } else {
- list[i] = cur;
- lasts[i] = cur;
- }
- if (cur->next != NULL)
- lastchg = cur->next;
- else
- lastchg = cur;
- cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
- }
- if (ret != 0) {
- VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
- ret = -1;
- goto done;
- }
- lastelem = cur;
- oldstate = ctxt->state;
- for (i = 0; i < nbgroups; i++) {
- ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
- if (ctxt->state == NULL) {
- ret = -1;
- break;
- }
- group = partitions->groups[i];
- if (lasts[i] != NULL) {
- last = lasts[i]->next;
- lasts[i]->next = NULL;
- }
- ctxt->state->seq = list[i];
- ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
- if (ret != 0)
- break;
- if (ctxt->state != NULL) {
- cur = ctxt->state->seq;
- cur = xmlRelaxNGSkipIgnored(ctxt, cur);
- xmlRelaxNGFreeValidState(ctxt, oldstate);
- oldstate = ctxt->state;
- ctxt->state = NULL;
- if (cur != NULL
- /* there's a nasty violation of context-free unambiguities,
- since in open-name-class context, interleave in the
- production shall finish without caring about anything
- else that is OK to follow in that case -- it would
- otherwise get marked as "extra content" and would
- hence fail the validation, hence this perhaps
- dirty attempt to rectify such a situation */
- && (define->parent->type != XML_RELAXNG_DEF
- || !xmlStrEqual(define->parent->name,
- (const xmlChar *) "open-name-class"))) {
- VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
- ret = -1;
- ctxt->state = oldstate;
- goto done;
- }
- } else if (ctxt->states != NULL) {
- int j;
- int found = 0;
- int best = -1;
- int lowattr = -1;
- /*
- * PBM: what happen if there is attributes checks in the interleaves
- */
- for (j = 0; j < ctxt->states->nbState; j++) {
- cur = ctxt->states->tabState[j]->seq;
- cur = xmlRelaxNGSkipIgnored(ctxt, cur);
- if (cur == NULL) {
- if (found == 0) {
- lowattr = ctxt->states->tabState[j]->nbAttrLeft;
- best = j;
- }
- found = 1;
- if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
- /* try to keep the latest one to mach old heuristic */
- lowattr = ctxt->states->tabState[j]->nbAttrLeft;
- best = j;
- }
- if (lowattr == 0)
- break;
- } else if (found == 0) {
- if (lowattr == -1) {
- lowattr = ctxt->states->tabState[j]->nbAttrLeft;
- best = j;
- } else
- if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
- /* try to keep the latest one to mach old heuristic */
- lowattr = ctxt->states->tabState[j]->nbAttrLeft;
- best = j;
- }
- }
- }
- /*
- * BIG PBM: here we pick only one restarting point :-(
- */
- if (ctxt->states->nbState > 0) {
- xmlRelaxNGFreeValidState(ctxt, oldstate);
- if (best != -1) {
- oldstate = ctxt->states->tabState[best];
- ctxt->states->tabState[best] = NULL;
- } else {
- oldstate =
- ctxt->states->tabState[ctxt->states->nbState - 1];
- ctxt->states->tabState[ctxt->states->nbState - 1] = NULL;
- ctxt->states->nbState--;
- }
- }
- for (j = 0; j < ctxt->states->nbState ; j++) {
- xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
- }
- xmlRelaxNGFreeStates(ctxt, ctxt->states);
- ctxt->states = NULL;
- if (found == 0) {
- if (cur == NULL) {
- VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA,
- (const xmlChar *) "noname");
- } else {
- VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
- }
- ret = -1;
- ctxt->state = oldstate;
- goto done;
- }
- } else {
- ret = -1;
- break;
- }
- if (lasts[i] != NULL) {
- lasts[i]->next = last;
- }
- }
- if (ctxt->state != NULL)
- xmlRelaxNGFreeValidState(ctxt, ctxt->state);
- ctxt->state = oldstate;
- ctxt->state->seq = lastelem;
- if (ret != 0) {
- VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
- ret = -1;
- goto done;
- }
- done:
- ctxt->flags = oldflags;
- /*
- * builds the next links chain from the prev one
- */
- cur = lastchg;
- while (cur != NULL) {
- if ((cur == start) || (cur->prev == NULL))
- break;
- cur->prev->next = cur;
- cur = cur->prev;
- }
- if (ret == 0) {
- if (ctxt->errNr > errNr)
- xmlRelaxNGPopErrors(ctxt, errNr);
- }
- xmlFree(list);
- xmlFree(lasts);
- return (ret);
- }
- /**
- * xmlRelaxNGValidateDefinitionList:
- * @ctxt: a Relax-NG validation context
- * @define: the list of definition to verify
- *
- * Validate the given node content against the (list) of definitions
- *
- * Returns 0 if the validation succeeded or an error code.
- */
- static int
- xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGDefinePtr defines)
- {
- int ret = 0, res;
- if (defines == NULL) {
- VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
- BAD_CAST "NULL definition list");
- return (-1);
- }
- while (defines != NULL) {
- if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
- res = xmlRelaxNGValidateDefinition(ctxt, defines);
- if (res < 0)
- ret = -1;
- } else {
- VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
- return (-1);
- }
- if (res == -1) /* continues on -2 */
- break;
- defines = defines->next;
- }
- return (ret);
- }
- /**
- * xmlRelaxNGElementMatch:
- * @ctxt: a Relax-NG validation context
- * @define: the definition to check
- * @elem: the element
- *
- * Check if the element matches the definition nameClass
- *
- * Returns 1 if the element matches, 0 if no, or -1 in case of error
- */
- static int
- xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGDefinePtr define, xmlNodePtr elem)
- {
- int ret = 0, oldflags = 0;
- if (define->name != NULL) {
- if (!xmlStrEqual(elem->name, define->name)) {
- VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
- return (0);
- }
- }
- if ((define->ns != NULL) && (define->ns[0] != 0)) {
- if (elem->ns == NULL) {
- VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
- return (0);
- } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
- VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
- elem->name, define->ns);
- return (0);
- }
- } else if ((elem->ns != NULL) && (define->ns != NULL) &&
- (define->name == NULL)) {
- VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
- return (0);
- } else if ((elem->ns != NULL) && (define->name != NULL)) {
- VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
- return (0);
- }
- if (define->nameClass == NULL)
- return (1);
- define = define->nameClass;
- if (define->type == XML_RELAXNG_EXCEPT) {
- xmlRelaxNGDefinePtr list;
- if (ctxt != NULL) {
- oldflags = ctxt->flags;
- ctxt->flags |= FLAGS_IGNORABLE;
- }
- list = define->content;
- while (list != NULL) {
- ret = xmlRelaxNGElementMatch(ctxt, list, elem);
- if (ret == 1) {
- if (ctxt != NULL)
- ctxt->flags = oldflags;
- return (0);
- }
- if (ret < 0) {
- if (ctxt != NULL)
- ctxt->flags = oldflags;
- return (ret);
- }
- list = list->next;
- }
- ret = 1;
- if (ctxt != NULL) {
- ctxt->flags = oldflags;
- }
- } else if (define->type == XML_RELAXNG_CHOICE) {
- xmlRelaxNGDefinePtr list;
- if (ctxt != NULL) {
- oldflags = ctxt->flags;
- ctxt->flags |= FLAGS_IGNORABLE;
- }
- list = define->nameClass;
- while (list != NULL) {
- ret = xmlRelaxNGElementMatch(ctxt, list, elem);
- if (ret == 1) {
- if (ctxt != NULL)
- ctxt->flags = oldflags;
- return (1);
- }
- if (ret < 0) {
- if (ctxt != NULL)
- ctxt->flags = oldflags;
- return (ret);
- }
- list = list->next;
- }
- if (ctxt != NULL) {
- if (ret != 0) {
- if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
- xmlRelaxNGDumpValidError(ctxt);
- } else {
- if (ctxt->errNr > 0)
- xmlRelaxNGPopErrors(ctxt, 0);
- }
- }
- ret = 0;
- if (ctxt != NULL) {
- ctxt->flags = oldflags;
- }
- } else {
- TODO ret = -1;
- }
- return (ret);
- }
- /**
- * xmlRelaxNGBestState:
- * @ctxt: a Relax-NG validation context
- *
- * Find the "best" state in the ctxt->states list of states to report
- * errors about. I.e. a state with no element left in the child list
- * or the one with the less attributes left.
- * This is called only if a validation error was detected
- *
- * Returns the index of the "best" state or -1 in case of error
- */
- static int
- xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
- {
- xmlRelaxNGValidStatePtr state;
- int i, tmp;
- int best = -1;
- int value = 1000000;
- if ((ctxt == NULL) || (ctxt->states == NULL) ||
- (ctxt->states->nbState <= 0))
- return (-1);
- for (i = 0; i < ctxt->states->nbState; i++) {
- state = ctxt->states->tabState[i];
- if (state == NULL)
- continue;
- if (state->seq != NULL) {
- if ((best == -1) || (value > 100000)) {
- value = 100000;
- best = i;
- }
- } else {
- tmp = state->nbAttrLeft;
- if ((best == -1) || (value > tmp)) {
- value = tmp;
- best = i;
- }
- }
- }
- return (best);
- }
- /**
- * xmlRelaxNGLogBestError:
- * @ctxt: a Relax-NG validation context
- *
- * Find the "best" state in the ctxt->states list of states to report
- * errors about and log it.
- */
- static void
- xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
- {
- int best;
- if ((ctxt == NULL) || (ctxt->states == NULL) ||
- (ctxt->states->nbState <= 0))
- return;
- best = xmlRelaxNGBestState(ctxt);
- if ((best >= 0) && (best < ctxt->states->nbState)) {
- ctxt->state = ctxt->states->tabState[best];
- xmlRelaxNGValidateElementEnd(ctxt, 1);
- }
- }
- /**
- * xmlRelaxNGValidateElementEnd:
- * @ctxt: a Relax-NG validation context
- * @dolog: indicate that error logging should be done
- *
- * Validate the end of the element, implements check that
- * there is nothing left not consumed in the element content
- * or in the attribute list.
- *
- * Returns 0 if the validation succeeded or an error code.
- */
- static int
- xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog)
- {
- int i;
- xmlRelaxNGValidStatePtr state;
- state = ctxt->state;
- if (state->seq != NULL) {
- state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
- if (state->seq != NULL) {
- if (dolog) {
- VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
- state->node->name, state->seq->name);
- }
- return (-1);
- }
- }
- for (i = 0; i < state->nbAttrs; i++) {
- if (state->attrs[i] != NULL) {
- if (dolog) {
- VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
- state->attrs[i]->name, state->node->name);
- }
- return (-1 - i);
- }
- }
- return (0);
- }
- /**
- * xmlRelaxNGValidateState:
- * @ctxt: a Relax-NG validation context
- * @define: the definition to verify
- *
- * Validate the current state against the definition
- *
- * Returns 0 if the validation succeeded or an error code.
- */
- static int
- xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGDefinePtr define)
- {
- xmlNodePtr node;
- int ret = 0, i, tmp, oldflags, errNr;
- xmlRelaxNGValidStatePtr oldstate = NULL, state;
- if (define == NULL) {
- VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
- return (-1);
- }
- if (ctxt->state != NULL) {
- node = ctxt->state->seq;
- } else {
- node = NULL;
- }
- #ifdef DEBUG
- for (i = 0; i < ctxt->depth; i++)
- xmlGenericError(xmlGenericErrorContext, " ");
- xmlGenericError(xmlGenericErrorContext,
- "Start validating %s ", xmlRelaxNGDefName(define));
- if (define->name != NULL)
- xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
- if ((node != NULL) && (node->name != NULL))
- xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
- else
- xmlGenericError(xmlGenericErrorContext, "\n");
- #endif
- ctxt->depth++;
- switch (define->type) {
- case XML_RELAXNG_EMPTY:
- ret = 0;
- break;
- case XML_RELAXNG_NOT_ALLOWED:
- ret = -1;
- break;
- case XML_RELAXNG_TEXT:
- while ((node != NULL) &&
- ((node->type == XML_TEXT_NODE) ||
- (node->type == XML_COMMENT_NODE) ||
- (node->type == XML_PI_NODE) ||
- (node->type == XML_CDATA_SECTION_NODE)))
- node = node->next;
- ctxt->state->seq = node;
- break;
- case XML_RELAXNG_ELEMENT:
- errNr = ctxt->errNr;
- node = xmlRelaxNGSkipIgnored(ctxt, node);
- if (node == NULL) {
- VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
- ret = -1;
- if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
- xmlRelaxNGDumpValidError(ctxt);
- break;
- }
- if (node->type != XML_ELEMENT_NODE) {
- VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
- ret = -1;
- if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
- xmlRelaxNGDumpValidError(ctxt);
- break;
- }
- /*
- * This node was already validated successfully against
- * this definition.
- */
- if (node->psvi == define) {
- ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
- if (ctxt->errNr > errNr)
- xmlRelaxNGPopErrors(ctxt, errNr);
- if (ctxt->errNr != 0) {
- while ((ctxt->err != NULL) &&
- (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
- && (xmlStrEqual(ctxt->err->arg2, node->name)))
- ||
- ((ctxt->err->err ==
- XML_RELAXNG_ERR_ELEMEXTRANS)
- && (xmlStrEqual(ctxt->err->arg1, node->name)))
- || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
- || (ctxt->err->err ==
- XML_RELAXNG_ERR_NOTELEM)))
- xmlRelaxNGValidErrorPop(ctxt);
- }
- break;
- }
- ret = xmlRelaxNGElementMatch(ctxt, define, node);
- if (ret <= 0) {
- ret = -1;
- if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
- xmlRelaxNGDumpValidError(ctxt);
- break;
- }
- ret = 0;
- if (ctxt->errNr != 0) {
- if (ctxt->errNr > errNr)
- xmlRelaxNGPopErrors(ctxt, errNr);
- while ((ctxt->err != NULL) &&
- (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
- (xmlStrEqual(ctxt->err->arg2, node->name))) ||
- ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
- (xmlStrEqual(ctxt->err->arg1, node->name))) ||
- (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
- (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
- xmlRelaxNGValidErrorPop(ctxt);
- }
- errNr = ctxt->errNr;
- oldflags = ctxt->flags;
- if (ctxt->flags & FLAGS_MIXED_CONTENT) {
- ctxt->flags -= FLAGS_MIXED_CONTENT;
- }
- state = xmlRelaxNGNewValidState(ctxt, node);
- if (state == NULL) {
- ret = -1;
- if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
- xmlRelaxNGDumpValidError(ctxt);
- break;
- }
- oldstate = ctxt->state;
- ctxt->state = state;
- if (define->attrs != NULL) {
- tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
- if (tmp != 0) {
- ret = -1;
- VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
- }
- }
- if (define->contModel != NULL) {
- xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
- xmlRelaxNGStatesPtr tmpstates = ctxt->states;
- xmlNodePtr nseq;
- nstate = xmlRelaxNGNewValidState(ctxt, node);
- ctxt->state = nstate;
- ctxt->states = NULL;
- tmp = xmlRelaxNGValidateCompiledContent(ctxt,
- define->contModel,
- ctxt->state->seq);
- nseq = ctxt->state->seq;
- ctxt->state = tmpstate;
- ctxt->states = tmpstates;
- xmlRelaxNGFreeValidState(ctxt, nstate);
- #ifdef DEBUG_COMPILE
- xmlGenericError(xmlGenericErrorContext,
- "Validating content of '%s' : %d\n",
- define->name, tmp);
- #endif
- if (tmp != 0)
- ret = -1;
- if (ctxt->states != NULL) {
- tmp = -1;
- for (i = 0; i < ctxt->states->nbState; i++) {
- state = ctxt->states->tabState[i];
- ctxt->state = state;
- ctxt->state->seq = nseq;
- if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
- tmp = 0;
- break;
- }
- }
- if (tmp != 0) {
- /*
- * validation error, log the message for the "best" one
- */
- ctxt->flags |= FLAGS_IGNORABLE;
- xmlRelaxNGLogBestError(ctxt);
- }
- for (i = 0; i < ctxt->states->nbState; i++) {
- xmlRelaxNGFreeValidState(ctxt,
- ctxt->states->
- tabState[i]);
- }
- xmlRelaxNGFreeStates(ctxt, ctxt->states);
- ctxt->flags = oldflags;
- ctxt->states = NULL;
- if ((ret == 0) && (tmp == -1))
- ret = -1;
- } else {
- state = ctxt->state;
- if (ctxt->state != NULL)
- ctxt->state->seq = nseq;
- if (ret == 0)
- ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
- xmlRelaxNGFreeValidState(ctxt, state);
- }
- } else {
- if (define->content != NULL) {
- tmp = xmlRelaxNGValidateDefinitionList(ctxt,
- define->
- content);
- if (tmp != 0) {
- ret = -1;
- if (ctxt->state == NULL) {
- ctxt->state = oldstate;
- VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
- node->name);
- ctxt->state = NULL;
- } else {
- VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
- node->name);
- }
- }
- }
- if (ctxt->states != NULL) {
- tmp = -1;
- for (i = 0; i < ctxt->states->nbState; i++) {
- state = ctxt->states->tabState[i];
- ctxt->state = state;
- if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
- tmp = 0;
- break;
- }
- }
- if (tmp != 0) {
- /*
- * validation error, log the message for the "best" one
- */
- ctxt->flags |= FLAGS_IGNORABLE;
- xmlRelaxNGLogBestError(ctxt);
- }
- for (i = 0; i < ctxt->states->nbState; i++) {
- xmlRelaxNGFreeValidState(ctxt,
- ctxt->states->tabState[i]);
- ctxt->states->tabState[i] = NULL;
- }
- xmlRelaxNGFreeStates(ctxt, ctxt->states);
- ctxt->flags = oldflags;
- ctxt->states = NULL;
- if ((ret == 0) && (tmp == -1))
- ret = -1;
- } else {
- state = ctxt->state;
- if (ret == 0)
- ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
- xmlRelaxNGFreeValidState(ctxt, state);
- }
- }
- if (ret == 0) {
- node->psvi = define;
- }
- ctxt->flags = oldflags;
- ctxt->state = oldstate;
- if (oldstate != NULL)
- oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
- if (ret != 0) {
- if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
- xmlRelaxNGDumpValidError(ctxt);
- ret = 0;
- #if 0
- } else {
- ret = -2;
- #endif
- }
- } else {
- if (ctxt->errNr > errNr)
- xmlRelaxNGPopErrors(ctxt, errNr);
- }
- #ifdef DEBUG
- xmlGenericError(xmlGenericErrorContext,
- "xmlRelaxNGValidateDefinition(): validated %s : %d",
- node->name, ret);
- if (oldstate == NULL)
- xmlGenericError(xmlGenericErrorContext, ": no state\n");
- else if (oldstate->seq == NULL)
- xmlGenericError(xmlGenericErrorContext, ": done\n");
- else if (oldstate->seq->type == XML_ELEMENT_NODE)
- xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
- oldstate->seq->name);
- else
- xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
- oldstate->seq->name, oldstate->seq->type);
- #endif
- break;
- case XML_RELAXNG_OPTIONAL:{
- errNr = ctxt->errNr;
- oldflags = ctxt->flags;
- ctxt->flags |= FLAGS_IGNORABLE;
- oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
- ret =
- xmlRelaxNGValidateDefinitionList(ctxt,
- define->content);
- if (ret != 0) {
- if (ctxt->state != NULL)
- xmlRelaxNGFreeValidState(ctxt, ctxt->state);
- ctxt->state = oldstate;
- ctxt->flags = oldflags;
- ret = 0;
- if (ctxt->errNr > errNr)
- xmlRelaxNGPopErrors(ctxt, errNr);
- break;
- }
- if (ctxt->states != NULL) {
- xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
- } else {
- ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
- if (ctxt->states == NULL) {
- xmlRelaxNGFreeValidState(ctxt, oldstate);
- ctxt->flags = oldflags;
- ret = -1;
- if (ctxt->errNr > errNr)
- xmlRelaxNGPopErrors(ctxt, errNr);
- break;
- }
- xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
- xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
- ctxt->state = NULL;
- }
- ctxt->flags = oldflags;
- ret = 0;
- if (ctxt->errNr > errNr)
- xmlRelaxNGPopErrors(ctxt, errNr);
- break;
- }
- case XML_RELAXNG_ONEORMORE:
- errNr = ctxt->errNr;
- ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
- if (ret != 0) {
- break;
- }
- if (ctxt->errNr > errNr)
- xmlRelaxNGPopErrors(ctxt, errNr);
- /* Falls through. */
- case XML_RELAXNG_ZEROORMORE:{
- int progress;
- xmlRelaxNGStatesPtr states = NULL, res = NULL;
- int base, j;
- errNr = ctxt->errNr;
- res = xmlRelaxNGNewStates(ctxt, 1);
- if (res == NULL) {
- ret = -1;
- break;
- }
- /*
- * All the input states are also exit states
- */
- if (ctxt->state != NULL) {
- xmlRelaxNGAddStates(ctxt, res,
- xmlRelaxNGCopyValidState(ctxt,
- ctxt->
- state));
- } else {
- for (j = 0; j < ctxt->states->nbState; j++) {
- xmlRelaxNGAddStates(ctxt, res,
- xmlRelaxNGCopyValidState(ctxt,
- ctxt->states->tabState[j]));
- }
- }
- oldflags = ctxt->flags;
- ctxt->flags |= FLAGS_IGNORABLE;
- do {
- progress = 0;
- base = res->nbState;
- if (ctxt->states != NULL) {
- states = ctxt->states;
- for (i = 0; i < states->nbState; i++) {
- ctxt->state = states->tabState[i];
- ctxt->states = NULL;
- ret = xmlRelaxNGValidateDefinitionList(ctxt,
- define->
- content);
- if (ret == 0) {
- if (ctxt->state != NULL) {
- tmp = xmlRelaxNGAddStates(ctxt, res,
- ctxt->state);
- ctxt->state = NULL;
- if (tmp == 1)
- progress = 1;
- } else if (ctxt->states != NULL) {
- for (j = 0; j < ctxt->states->nbState;
- j++) {
- tmp =
- xmlRelaxNGAddStates(ctxt, res,
- ctxt->states->tabState[j]);
- if (tmp == 1)
- progress = 1;
- }
- xmlRelaxNGFreeStates(ctxt,
- ctxt->states);
- ctxt->states = NULL;
- }
- } else {
- if (ctxt->state != NULL) {
- xmlRelaxNGFreeValidState(ctxt,
- ctxt->state);
- ctxt->state = NULL;
- }
- }
- }
- } else {
- ret = xmlRelaxNGValidateDefinitionList(ctxt,
- define->
- content);
- if (ret != 0) {
- xmlRelaxNGFreeValidState(ctxt, ctxt->state);
- ctxt->state = NULL;
- } else {
- base = res->nbState;
- if (ctxt->state != NULL) {
- tmp = xmlRelaxNGAddStates(ctxt, res,
- ctxt->state);
- ctxt->state = NULL;
- if (tmp == 1)
- progress = 1;
- } else if (ctxt->states != NULL) {
- for (j = 0; j < ctxt->states->nbState; j++) {
- tmp = xmlRelaxNGAddStates(ctxt, res,
- ctxt->states->tabState[j]);
- if (tmp == 1)
- progress = 1;
- }
- if (states == NULL) {
- states = ctxt->states;
- } else {
- xmlRelaxNGFreeStates(ctxt,
- ctxt->states);
- }
- ctxt->states = NULL;
- }
- }
- }
- if (progress) {
- /*
- * Collect all the new nodes added at that step
- * and make them the new node set
- */
- if (res->nbState - base == 1) {
- ctxt->state = xmlRelaxNGCopyValidState(ctxt,
- res->
- tabState
- [base]);
- } else {
- if (states == NULL) {
- xmlRelaxNGNewStates(ctxt,
- res->nbState - base);
- states = ctxt->states;
- if (states == NULL) {
- progress = 0;
- break;
- }
- }
- states->nbState = 0;
- for (i = base; i < res->nbState; i++)
- xmlRelaxNGAddStates(ctxt, states,
- xmlRelaxNGCopyValidState
- (ctxt, res->tabState[i]));
- ctxt->states = states;
- }
- }
- } while (progress == 1);
- if (states != NULL) {
- xmlRelaxNGFreeStates(ctxt, states);
- }
- ctxt->states = res;
- ctxt->flags = oldflags;
- #if 0
- /*
- * errors may have to be propagated back...
- */
- if (ctxt->errNr > errNr)
- xmlRelaxNGPopErrors(ctxt, errNr);
- #endif
- ret = 0;
- break;
- }
- case XML_RELAXNG_CHOICE:{
- xmlRelaxNGDefinePtr list = NULL;
- xmlRelaxNGStatesPtr states = NULL;
- node = xmlRelaxNGSkipIgnored(ctxt, node);
- errNr = ctxt->errNr;
- if ((define->dflags & IS_TRIABLE) && (define->data != NULL) &&
- (node != NULL)) {
- /*
- * node == NULL can't be optimized since IS_TRIABLE
- * doesn't account for choice which may lead to
- * only attributes.
- */
- xmlHashTablePtr triage =
- (xmlHashTablePtr) define->data;
- /*
- * Something we can optimize cleanly there is only one
- * possible branch out !
- */
- if ((node->type == XML_TEXT_NODE) ||
- (node->type == XML_CDATA_SECTION_NODE)) {
- list =
- xmlHashLookup2(triage, BAD_CAST "#text", NULL);
- } else if (node->type == XML_ELEMENT_NODE) {
- if (node->ns != NULL) {
- list = xmlHashLookup2(triage, node->name,
- node->ns->href);
- if (list == NULL)
- list =
- xmlHashLookup2(triage, BAD_CAST "#any",
- node->ns->href);
- } else
- list =
- xmlHashLookup2(triage, node->name, NULL);
- if (list == NULL)
- list =
- xmlHashLookup2(triage, BAD_CAST "#any",
- NULL);
- }
- if (list == NULL) {
- ret = -1;
- VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name);
- break;
- }
- ret = xmlRelaxNGValidateDefinition(ctxt, list);
- if (ret == 0) {
- }
- break;
- }
- list = define->content;
- oldflags = ctxt->flags;
- ctxt->flags |= FLAGS_IGNORABLE;
- while (list != NULL) {
- oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
- ret = xmlRelaxNGValidateDefinition(ctxt, list);
- if (ret == 0) {
- if (states == NULL) {
- states = xmlRelaxNGNewStates(ctxt, 1);
- }
- if (ctxt->state != NULL) {
- xmlRelaxNGAddStates(ctxt, states, ctxt->state);
- } else if (ctxt->states != NULL) {
- for (i = 0; i < ctxt->states->nbState; i++) {
- xmlRelaxNGAddStates(ctxt, states,
- ctxt->states->
- tabState[i]);
- }
- xmlRelaxNGFreeStates(ctxt, ctxt->states);
- ctxt->states = NULL;
- }
- } else {
- xmlRelaxNGFreeValidState(ctxt, ctxt->state);
- }
- ctxt->state = oldstate;
- list = list->next;
- }
- if (states != NULL) {
- xmlRelaxNGFreeValidState(ctxt, oldstate);
- ctxt->states = states;
- ctxt->state = NULL;
- ret = 0;
- } else {
- ctxt->states = NULL;
- }
- ctxt->flags = oldflags;
- if (ret != 0) {
- if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
- xmlRelaxNGDumpValidError(ctxt);
- }
- } else {
- if (ctxt->errNr > errNr)
- xmlRelaxNGPopErrors(ctxt, errNr);
- }
- break;
- }
- case XML_RELAXNG_DEF:
- case XML_RELAXNG_GROUP:
- ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
- break;
- case XML_RELAXNG_INTERLEAVE:
- ret = xmlRelaxNGValidateInterleave(ctxt, define);
- break;
- case XML_RELAXNG_ATTRIBUTE:
- ret = xmlRelaxNGValidateAttribute(ctxt, define);
- break;
- case XML_RELAXNG_START:
- case XML_RELAXNG_NOOP:
- case XML_RELAXNG_REF:
- case XML_RELAXNG_EXTERNALREF:
- case XML_RELAXNG_PARENTREF:
- ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
- break;
- case XML_RELAXNG_DATATYPE:{
- xmlNodePtr child;
- xmlChar *content = NULL;
- child = node;
- while (child != NULL) {
- if (child->type == XML_ELEMENT_NODE) {
- VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
- node->parent->name);
- ret = -1;
- break;
- } else if ((child->type == XML_TEXT_NODE) ||
- (child->type == XML_CDATA_SECTION_NODE)) {
- content = xmlStrcat(content, child->content);
- }
- /* TODO: handle entities ... */
- child = child->next;
- }
- if (ret == -1) {
- if (content != NULL)
- xmlFree(content);
- break;
- }
- if (content == NULL) {
- content = xmlStrdup(BAD_CAST "");
- if (content == NULL) {
- xmlRngVErrMemory(ctxt, "validating\n");
- ret = -1;
- break;
- }
- }
- ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
- ctxt->state->seq);
- if (ret == -1) {
- VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
- } else if (ret == 0) {
- ctxt->state->seq = NULL;
- }
- if (content != NULL)
- xmlFree(content);
- break;
- }
- case XML_RELAXNG_VALUE:{
- xmlChar *content = NULL;
- xmlChar *oldvalue;
- xmlNodePtr child;
- child = node;
- while (child != NULL) {
- if (child->type == XML_ELEMENT_NODE) {
- VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
- node->parent->name);
- ret = -1;
- break;
- } else if ((child->type == XML_TEXT_NODE) ||
- (child->type == XML_CDATA_SECTION_NODE)) {
- content = xmlStrcat(content, child->content);
- }
- /* TODO: handle entities ... */
- child = child->next;
- }
- if (ret == -1) {
- if (content != NULL)
- xmlFree(content);
- break;
- }
- if (content == NULL) {
- content = xmlStrdup(BAD_CAST "");
- if (content == NULL) {
- xmlRngVErrMemory(ctxt, "validating\n");
- ret = -1;
- break;
- }
- }
- oldvalue = ctxt->state->value;
- ctxt->state->value = content;
- ret = xmlRelaxNGValidateValue(ctxt, define);
- ctxt->state->value = oldvalue;
- if (ret == -1) {
- VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
- } else if (ret == 0) {
- ctxt->state->seq = NULL;
- }
- if (content != NULL)
- xmlFree(content);
- break;
- }
- case XML_RELAXNG_LIST:{
- xmlChar *content;
- xmlNodePtr child;
- xmlChar *oldvalue, *oldendvalue;
- int len;
- /*
- * Make sure it's only text nodes
- */
- content = NULL;
- child = node;
- while (child != NULL) {
- if (child->type == XML_ELEMENT_NODE) {
- VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
- node->parent->name);
- ret = -1;
- break;
- } else if ((child->type == XML_TEXT_NODE) ||
- (child->type == XML_CDATA_SECTION_NODE)) {
- content = xmlStrcat(content, child->content);
- }
- /* TODO: handle entities ... */
- child = child->next;
- }
- if (ret == -1) {
- if (content != NULL)
- xmlFree(content);
- break;
- }
- if (content == NULL) {
- content = xmlStrdup(BAD_CAST "");
- if (content == NULL) {
- xmlRngVErrMemory(ctxt, "validating\n");
- ret = -1;
- break;
- }
- }
- len = xmlStrlen(content);
- oldvalue = ctxt->state->value;
- oldendvalue = ctxt->state->endvalue;
- ctxt->state->value = content;
- ctxt->state->endvalue = content + len;
- ret = xmlRelaxNGValidateValue(ctxt, define);
- ctxt->state->value = oldvalue;
- ctxt->state->endvalue = oldendvalue;
- if (ret == -1) {
- VALID_ERR(XML_RELAXNG_ERR_LIST);
- } else if ((ret == 0) && (node != NULL)) {
- ctxt->state->seq = node->next;
- }
- if (content != NULL)
- xmlFree(content);
- break;
- }
- case XML_RELAXNG_EXCEPT:
- case XML_RELAXNG_PARAM:
- TODO ret = -1;
- break;
- }
- ctxt->depth--;
- #ifdef DEBUG
- for (i = 0; i < ctxt->depth; i++)
- xmlGenericError(xmlGenericErrorContext, " ");
- xmlGenericError(xmlGenericErrorContext,
- "Validating %s ", xmlRelaxNGDefName(define));
- if (define->name != NULL)
- xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
- if (ret == 0)
- xmlGenericError(xmlGenericErrorContext, "succeeded\n");
- else
- xmlGenericError(xmlGenericErrorContext, "failed\n");
- #endif
- return (ret);
- }
- /**
- * xmlRelaxNGValidateDefinition:
- * @ctxt: a Relax-NG validation context
- * @define: the definition to verify
- *
- * Validate the current node lists against the definition
- *
- * Returns 0 if the validation succeeded or an error code.
- */
- static int
- xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGDefinePtr define)
- {
- xmlRelaxNGStatesPtr states, res;
- int i, j, k, ret, oldflags;
- /*
- * We should NOT have both ctxt->state and ctxt->states
- */
- if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
- TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
- ctxt->state = NULL;
- }
- if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
- if (ctxt->states != NULL) {
- ctxt->state = ctxt->states->tabState[0];
- xmlRelaxNGFreeStates(ctxt, ctxt->states);
- ctxt->states = NULL;
- }
- ret = xmlRelaxNGValidateState(ctxt, define);
- if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
- TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
- ctxt->state = NULL;
- }
- if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
- ctxt->state = ctxt->states->tabState[0];
- xmlRelaxNGFreeStates(ctxt, ctxt->states);
- ctxt->states = NULL;
- }
- return (ret);
- }
- states = ctxt->states;
- ctxt->states = NULL;
- res = NULL;
- j = 0;
- oldflags = ctxt->flags;
- ctxt->flags |= FLAGS_IGNORABLE;
- for (i = 0; i < states->nbState; i++) {
- ctxt->state = states->tabState[i];
- ctxt->states = NULL;
- ret = xmlRelaxNGValidateState(ctxt, define);
- /*
- * We should NOT have both ctxt->state and ctxt->states
- */
- if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
- TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
- ctxt->state = NULL;
- }
- if (ret == 0) {
- if (ctxt->states == NULL) {
- if (res != NULL) {
- /* add the state to the container */
- xmlRelaxNGAddStates(ctxt, res, ctxt->state);
- ctxt->state = NULL;
- } else {
- /* add the state directly in states */
- states->tabState[j++] = ctxt->state;
- ctxt->state = NULL;
- }
- } else {
- if (res == NULL) {
- /* make it the new container and copy other results */
- res = ctxt->states;
- ctxt->states = NULL;
- for (k = 0; k < j; k++)
- xmlRelaxNGAddStates(ctxt, res,
- states->tabState[k]);
- } else {
- /* add all the new results to res and reff the container */
- for (k = 0; k < ctxt->states->nbState; k++)
- xmlRelaxNGAddStates(ctxt, res,
- ctxt->states->tabState[k]);
- xmlRelaxNGFreeStates(ctxt, ctxt->states);
- ctxt->states = NULL;
- }
- }
- } else {
- if (ctxt->state != NULL) {
- xmlRelaxNGFreeValidState(ctxt, ctxt->state);
- ctxt->state = NULL;
- } else if (ctxt->states != NULL) {
- for (k = 0; k < ctxt->states->nbState; k++)
- xmlRelaxNGFreeValidState(ctxt,
- ctxt->states->tabState[k]);
- xmlRelaxNGFreeStates(ctxt, ctxt->states);
- ctxt->states = NULL;
- }
- }
- }
- ctxt->flags = oldflags;
- if (res != NULL) {
- xmlRelaxNGFreeStates(ctxt, states);
- ctxt->states = res;
- ret = 0;
- } else if (j > 1) {
- states->nbState = j;
- ctxt->states = states;
- ret = 0;
- } else if (j == 1) {
- ctxt->state = states->tabState[0];
- xmlRelaxNGFreeStates(ctxt, states);
- ret = 0;
- } else {
- ret = -1;
- xmlRelaxNGFreeStates(ctxt, states);
- if (ctxt->states != NULL) {
- xmlRelaxNGFreeStates(ctxt, ctxt->states);
- ctxt->states = NULL;
- }
- }
- if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
- TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
- ctxt->state = NULL;
- }
- return (ret);
- }
- /**
- * xmlRelaxNGValidateDocument:
- * @ctxt: a Relax-NG validation context
- * @doc: the document
- *
- * Validate the given document
- *
- * Returns 0 if the validation succeeded or an error code.
- */
- static int
- xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
- {
- int ret;
- xmlRelaxNGPtr schema;
- xmlRelaxNGGrammarPtr grammar;
- xmlRelaxNGValidStatePtr state;
- xmlNodePtr node;
- if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
- return (-1);
- ctxt->errNo = XML_RELAXNG_OK;
- schema = ctxt->schema;
- grammar = schema->topgrammar;
- if (grammar == NULL) {
- VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
- return (-1);
- }
- state = xmlRelaxNGNewValidState(ctxt, NULL);
- ctxt->state = state;
- ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
- if ((ctxt->state != NULL) && (state->seq != NULL)) {
- state = ctxt->state;
- node = state->seq;
- node = xmlRelaxNGSkipIgnored(ctxt, node);
- if (node != NULL) {
- if (ret != -1) {
- VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
- ret = -1;
- }
- }
- } else if (ctxt->states != NULL) {
- int i;
- int tmp = -1;
- for (i = 0; i < ctxt->states->nbState; i++) {
- state = ctxt->states->tabState[i];
- node = state->seq;
- node = xmlRelaxNGSkipIgnored(ctxt, node);
- if (node == NULL)
- tmp = 0;
- xmlRelaxNGFreeValidState(ctxt, state);
- }
- if (tmp == -1) {
- if (ret != -1) {
- VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
- ret = -1;
- }
- }
- }
- if (ctxt->state != NULL) {
- xmlRelaxNGFreeValidState(ctxt, ctxt->state);
- ctxt->state = NULL;
- }
- if (ret != 0)
- xmlRelaxNGDumpValidError(ctxt);
- #ifdef DEBUG
- else if (ctxt->errNr != 0) {
- ctxt->error(ctxt->userData,
- "%d Extra error messages left on stack !\n",
- ctxt->errNr);
- xmlRelaxNGDumpValidError(ctxt);
- }
- #endif
- #ifdef LIBXML_VALID_ENABLED
- if (ctxt->idref == 1) {
- xmlValidCtxt vctxt;
- memset(&vctxt, 0, sizeof(xmlValidCtxt));
- vctxt.valid = 1;
- vctxt.error = ctxt->error;
- vctxt.warning = ctxt->warning;
- vctxt.userData = ctxt->userData;
- if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
- ret = -1;
- }
- #endif /* LIBXML_VALID_ENABLED */
- if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
- ret = -1;
- return (ret);
- }
- /**
- * xmlRelaxNGCleanPSVI:
- * @node: an input element or document
- *
- * Call this routine to speed up XPath computation on static documents.
- * This stamps all the element nodes with the document order
- * Like for line information, the order is kept in the element->content
- * field, the value stored is actually - the node number (starting at -1)
- * to be able to differentiate from line numbers.
- *
- * Returns the number of elements found in the document or -1 in case
- * of error.
- */
- static void
- xmlRelaxNGCleanPSVI(xmlNodePtr node) {
- xmlNodePtr cur;
- if ((node == NULL) ||
- ((node->type != XML_ELEMENT_NODE) &&
- (node->type != XML_DOCUMENT_NODE) &&
- (node->type != XML_HTML_DOCUMENT_NODE)))
- return;
- if (node->type == XML_ELEMENT_NODE)
- node->psvi = NULL;
- cur = node->children;
- while (cur != NULL) {
- if (cur->type == XML_ELEMENT_NODE) {
- cur->psvi = NULL;
- if (cur->children != NULL) {
- cur = cur->children;
- continue;
- }
- }
- if (cur->next != NULL) {
- cur = cur->next;
- continue;
- }
- do {
- cur = cur->parent;
- if (cur == NULL)
- break;
- if (cur == node) {
- cur = NULL;
- break;
- }
- if (cur->next != NULL) {
- cur = cur->next;
- break;
- }
- } while (cur != NULL);
- }
- return;
- }
- /************************************************************************
- * *
- * Validation interfaces *
- * *
- ************************************************************************/
- /**
- * xmlRelaxNGNewValidCtxt:
- * @schema: a precompiled XML RelaxNGs
- *
- * Create an XML RelaxNGs validation context based on the given schema
- *
- * Returns the validation context or NULL in case of error
- */
- xmlRelaxNGValidCtxtPtr
- xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
- {
- xmlRelaxNGValidCtxtPtr ret;
- ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
- if (ret == NULL) {
- xmlRngVErrMemory(NULL, "building context\n");
- return (NULL);
- }
- memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
- ret->schema = schema;
- ret->error = xmlGenericError;
- ret->userData = xmlGenericErrorContext;
- ret->errNr = 0;
- ret->errMax = 0;
- ret->err = NULL;
- ret->errTab = NULL;
- if (schema != NULL)
- ret->idref = schema->idref;
- ret->states = NULL;
- ret->freeState = NULL;
- ret->freeStates = NULL;
- ret->errNo = XML_RELAXNG_OK;
- return (ret);
- }
- /**
- * xmlRelaxNGFreeValidCtxt:
- * @ctxt: the schema validation context
- *
- * Free the resources associated to the schema validation context
- */
- void
- xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
- {
- int k;
- if (ctxt == NULL)
- return;
- if (ctxt->states != NULL)
- xmlRelaxNGFreeStates(NULL, ctxt->states);
- if (ctxt->freeState != NULL) {
- for (k = 0; k < ctxt->freeState->nbState; k++) {
- xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
- }
- xmlRelaxNGFreeStates(NULL, ctxt->freeState);
- }
- if (ctxt->freeStates != NULL) {
- for (k = 0; k < ctxt->freeStatesNr; k++) {
- xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
- }
- xmlFree(ctxt->freeStates);
- }
- if (ctxt->errTab != NULL)
- xmlFree(ctxt->errTab);
- if (ctxt->elemTab != NULL) {
- xmlRegExecCtxtPtr exec;
- exec = xmlRelaxNGElemPop(ctxt);
- while (exec != NULL) {
- xmlRegFreeExecCtxt(exec);
- exec = xmlRelaxNGElemPop(ctxt);
- }
- xmlFree(ctxt->elemTab);
- }
- xmlFree(ctxt);
- }
- /**
- * xmlRelaxNGSetValidErrors:
- * @ctxt: a Relax-NG validation context
- * @err: the error function
- * @warn: the warning function
- * @ctx: the functions context
- *
- * Set the error and warning callback information
- */
- void
- xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGValidityErrorFunc err,
- xmlRelaxNGValidityWarningFunc warn, void *ctx)
- {
- if (ctxt == NULL)
- return;
- ctxt->error = err;
- ctxt->warning = warn;
- ctxt->userData = ctx;
- ctxt->serror = NULL;
- }
- /**
- * xmlRelaxNGSetValidStructuredErrors:
- * @ctxt: a Relax-NG validation context
- * @serror: the structured error function
- * @ctx: the functions context
- *
- * Set the structured error callback
- */
- void
- xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt,
- xmlStructuredErrorFunc serror, void *ctx)
- {
- if (ctxt == NULL)
- return;
- ctxt->serror = serror;
- ctxt->error = NULL;
- ctxt->warning = NULL;
- ctxt->userData = ctx;
- }
- /**
- * xmlRelaxNGGetValidErrors:
- * @ctxt: a Relax-NG validation context
- * @err: the error function result
- * @warn: the warning function result
- * @ctx: the functions context result
- *
- * Get the error and warning callback information
- *
- * Returns -1 in case of error and 0 otherwise
- */
- int
- xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
- xmlRelaxNGValidityErrorFunc * err,
- xmlRelaxNGValidityWarningFunc * warn, void **ctx)
- {
- if (ctxt == NULL)
- return (-1);
- if (err != NULL)
- *err = ctxt->error;
- if (warn != NULL)
- *warn = ctxt->warning;
- if (ctx != NULL)
- *ctx = ctxt->userData;
- return (0);
- }
- /**
- * xmlRelaxNGValidateDoc:
- * @ctxt: a Relax-NG validation context
- * @doc: a parsed document tree
- *
- * Validate a document tree in memory.
- *
- * Returns 0 if the document is valid, a positive error code
- * number otherwise and -1 in case of internal or API error.
- */
- int
- xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
- {
- int ret;
- if ((ctxt == NULL) || (doc == NULL))
- return (-1);
- ctxt->doc = doc;
- ret = xmlRelaxNGValidateDocument(ctxt, doc);
- /*
- * Remove all left PSVI
- */
- xmlRelaxNGCleanPSVI((xmlNodePtr) doc);
- /*
- * TODO: build error codes
- */
- if (ret == -1)
- return (1);
- return (ret);
- }
- #define bottom_relaxng
- #include "elfgcchack.h"
- #endif /* LIBXML_SCHEMAS_ENABLED */
|