schematron.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787
  1. /*
  2. * schematron.c : implementation of the Schematron schema validity checking
  3. *
  4. * See Copyright for the status of this software.
  5. *
  6. * Daniel Veillard <daniel@veillard.com>
  7. */
  8. /*
  9. * TODO:
  10. * + double check the semantic, especially
  11. * - multiple rules applying in a single pattern/node
  12. * - the semantic of libxml2 patterns vs. XSLT production referenced
  13. * by the spec.
  14. * + export of results in SVRL
  15. * + full parsing and coverage of the spec, conformance of the input to the
  16. * spec
  17. * + divergences between the draft and the ISO proposed standard :-(
  18. * + hook and test include
  19. * + try and compare with the XSLT version
  20. */
  21. #define IN_LIBXML
  22. #include "libxml.h"
  23. #ifdef LIBXML_SCHEMATRON_ENABLED
  24. #include <string.h>
  25. #include <libxml/parser.h>
  26. #include <libxml/tree.h>
  27. #include <libxml/uri.h>
  28. #include <libxml/xpath.h>
  29. #include <libxml/xpathInternals.h>
  30. #include <libxml/pattern.h>
  31. #include <libxml/schematron.h>
  32. #define SCHEMATRON_PARSE_OPTIONS XML_PARSE_NOENT
  33. #define SCT_OLD_NS BAD_CAST "http://www.ascc.net/xml/schematron"
  34. #define XML_SCHEMATRON_NS BAD_CAST "http://purl.oclc.org/dsdl/schematron"
  35. static const xmlChar *xmlSchematronNs = XML_SCHEMATRON_NS;
  36. static const xmlChar *xmlOldSchematronNs = SCT_OLD_NS;
  37. #define IS_SCHEMATRON(node, elem) \
  38. ((node != NULL) && (node->type == XML_ELEMENT_NODE ) && \
  39. (node->ns != NULL) && \
  40. (xmlStrEqual(node->name, (const xmlChar *) elem)) && \
  41. ((xmlStrEqual(node->ns->href, xmlSchematronNs)) || \
  42. (xmlStrEqual(node->ns->href, xmlOldSchematronNs))))
  43. #define NEXT_SCHEMATRON(node) \
  44. while (node != NULL) { \
  45. if ((node->type == XML_ELEMENT_NODE ) && (node->ns != NULL) && \
  46. ((xmlStrEqual(node->ns->href, xmlSchematronNs)) || \
  47. (xmlStrEqual(node->ns->href, xmlOldSchematronNs)))) \
  48. break; \
  49. node = node->next; \
  50. }
  51. /**
  52. * TODO:
  53. *
  54. * macro to flag unimplemented blocks
  55. */
  56. #define TODO \
  57. xmlGenericError(xmlGenericErrorContext, \
  58. "Unimplemented block at %s:%d\n", \
  59. __FILE__, __LINE__);
  60. typedef enum {
  61. XML_SCHEMATRON_ASSERT=1,
  62. XML_SCHEMATRON_REPORT=2
  63. } xmlSchematronTestType;
  64. /**
  65. * _xmlSchematronTest:
  66. *
  67. * A Schematrons test, either an assert or a report
  68. */
  69. typedef struct _xmlSchematronTest xmlSchematronTest;
  70. typedef xmlSchematronTest *xmlSchematronTestPtr;
  71. struct _xmlSchematronTest {
  72. xmlSchematronTestPtr next; /* the next test in the list */
  73. xmlSchematronTestType type; /* the test type */
  74. xmlNodePtr node; /* the node in the tree */
  75. xmlChar *test; /* the expression to test */
  76. xmlXPathCompExprPtr comp; /* the compiled expression */
  77. xmlChar *report; /* the message to report */
  78. };
  79. /**
  80. * _xmlSchematronRule:
  81. *
  82. * A Schematrons rule
  83. */
  84. typedef struct _xmlSchematronRule xmlSchematronRule;
  85. typedef xmlSchematronRule *xmlSchematronRulePtr;
  86. struct _xmlSchematronRule {
  87. xmlSchematronRulePtr next; /* the next rule in the list */
  88. xmlSchematronRulePtr patnext;/* the next rule in the pattern list */
  89. xmlNodePtr node; /* the node in the tree */
  90. xmlChar *context; /* the context evaluation rule */
  91. xmlSchematronTestPtr tests; /* the list of tests */
  92. xmlPatternPtr pattern; /* the compiled pattern associated */
  93. xmlChar *report; /* the message to report */
  94. };
  95. /**
  96. * _xmlSchematronPattern:
  97. *
  98. * A Schematrons pattern
  99. */
  100. typedef struct _xmlSchematronPattern xmlSchematronPattern;
  101. typedef xmlSchematronPattern *xmlSchematronPatternPtr;
  102. struct _xmlSchematronPattern {
  103. xmlSchematronPatternPtr next;/* the next pattern in the list */
  104. xmlSchematronRulePtr rules; /* the list of rules */
  105. xmlChar *name; /* the name of the pattern */
  106. };
  107. /**
  108. * _xmlSchematron:
  109. *
  110. * A Schematrons definition
  111. */
  112. struct _xmlSchematron {
  113. const xmlChar *name; /* schema name */
  114. int preserve; /* was the document passed by the user */
  115. xmlDocPtr doc; /* pointer to the parsed document */
  116. int flags; /* specific to this schematron */
  117. void *_private; /* unused by the library */
  118. xmlDictPtr dict; /* the dictionary used internally */
  119. const xmlChar *title; /* the title if any */
  120. int nbNs; /* the number of namespaces */
  121. int nbPattern; /* the number of patterns */
  122. xmlSchematronPatternPtr patterns;/* the patterns found */
  123. xmlSchematronRulePtr rules; /* the rules gathered */
  124. int nbNamespaces; /* number of namespaces in the array */
  125. int maxNamespaces; /* size of the array */
  126. const xmlChar **namespaces; /* the array of namespaces */
  127. };
  128. /**
  129. * xmlSchematronValidCtxt:
  130. *
  131. * A Schematrons validation context
  132. */
  133. struct _xmlSchematronValidCtxt {
  134. int type;
  135. int flags; /* an or of xmlSchematronValidOptions */
  136. xmlDictPtr dict;
  137. int nberrors;
  138. int err;
  139. xmlSchematronPtr schema;
  140. xmlXPathContextPtr xctxt;
  141. FILE *outputFile; /* if using XML_SCHEMATRON_OUT_FILE */
  142. xmlBufferPtr outputBuffer; /* if using XML_SCHEMATRON_OUT_BUFFER */
  143. #ifdef LIBXML_OUTPUT_ENABLED
  144. xmlOutputWriteCallback iowrite; /* if using XML_SCHEMATRON_OUT_IO */
  145. xmlOutputCloseCallback ioclose;
  146. #endif
  147. void *ioctx;
  148. /* error reporting data */
  149. void *userData; /* user specific data block */
  150. xmlSchematronValidityErrorFunc error;/* the callback in case of errors */
  151. xmlSchematronValidityWarningFunc warning;/* callback in case of warning */
  152. xmlStructuredErrorFunc serror; /* the structured function */
  153. };
  154. struct _xmlSchematronParserCtxt {
  155. int type;
  156. const xmlChar *URL;
  157. xmlDocPtr doc;
  158. int preserve; /* Whether the doc should be freed */
  159. const char *buffer;
  160. int size;
  161. xmlDictPtr dict; /* dictionary for interned string names */
  162. int nberrors;
  163. int err;
  164. xmlXPathContextPtr xctxt; /* the XPath context used for compilation */
  165. xmlSchematronPtr schema;
  166. int nbNamespaces; /* number of namespaces in the array */
  167. int maxNamespaces; /* size of the array */
  168. const xmlChar **namespaces; /* the array of namespaces */
  169. int nbIncludes; /* number of includes in the array */
  170. int maxIncludes; /* size of the array */
  171. xmlNodePtr *includes; /* the array of includes */
  172. /* error reporting data */
  173. void *userData; /* user specific data block */
  174. xmlSchematronValidityErrorFunc error;/* the callback in case of errors */
  175. xmlSchematronValidityWarningFunc warning;/* callback in case of warning */
  176. xmlStructuredErrorFunc serror; /* the structured function */
  177. };
  178. #define XML_STRON_CTXT_PARSER 1
  179. #define XML_STRON_CTXT_VALIDATOR 2
  180. /************************************************************************
  181. * *
  182. * Error reporting *
  183. * *
  184. ************************************************************************/
  185. /**
  186. * xmlSchematronPErrMemory:
  187. * @node: a context node
  188. * @extra: extra information
  189. *
  190. * Handle an out of memory condition
  191. */
  192. static void
  193. xmlSchematronPErrMemory(xmlSchematronParserCtxtPtr ctxt,
  194. const char *extra, xmlNodePtr node)
  195. {
  196. if (ctxt != NULL)
  197. ctxt->nberrors++;
  198. __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, node, NULL,
  199. extra);
  200. }
  201. /**
  202. * xmlSchematronPErr:
  203. * @ctxt: the parsing context
  204. * @node: the context node
  205. * @error: the error code
  206. * @msg: the error message
  207. * @str1: extra data
  208. * @str2: extra data
  209. *
  210. * Handle a parser error
  211. */
  212. static void LIBXML_ATTR_FORMAT(4,0)
  213. xmlSchematronPErr(xmlSchematronParserCtxtPtr ctxt, xmlNodePtr node, int error,
  214. const char *msg, const xmlChar * str1, const xmlChar * str2)
  215. {
  216. xmlGenericErrorFunc channel = NULL;
  217. xmlStructuredErrorFunc schannel = NULL;
  218. void *data = NULL;
  219. if (ctxt != NULL) {
  220. ctxt->nberrors++;
  221. channel = ctxt->error;
  222. data = ctxt->userData;
  223. schannel = ctxt->serror;
  224. }
  225. __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
  226. error, XML_ERR_ERROR, NULL, 0,
  227. (const char *) str1, (const char *) str2, NULL, 0, 0,
  228. msg, str1, str2);
  229. }
  230. /**
  231. * xmlSchematronVTypeErrMemory:
  232. * @node: a context node
  233. * @extra: extra information
  234. *
  235. * Handle an out of memory condition
  236. */
  237. static void
  238. xmlSchematronVErrMemory(xmlSchematronValidCtxtPtr ctxt,
  239. const char *extra, xmlNodePtr node)
  240. {
  241. if (ctxt != NULL) {
  242. ctxt->nberrors++;
  243. ctxt->err = XML_SCHEMAV_INTERNAL;
  244. }
  245. __xmlSimpleError(XML_FROM_SCHEMASV, XML_ERR_NO_MEMORY, node, NULL,
  246. extra);
  247. }
  248. /************************************************************************
  249. * *
  250. * Parsing and compilation of the Schematrontrons *
  251. * *
  252. ************************************************************************/
  253. /**
  254. * xmlSchematronAddTest:
  255. * @ctxt: the schema parsing context
  256. * @type: the type of test
  257. * @rule: the parent rule
  258. * @node: the node hosting the test
  259. * @test: the associated test
  260. * @report: the associated report string
  261. *
  262. * Add a test to a schematron
  263. *
  264. * Returns the new pointer or NULL in case of error
  265. */
  266. static xmlSchematronTestPtr
  267. xmlSchematronAddTest(xmlSchematronParserCtxtPtr ctxt,
  268. xmlSchematronTestType type,
  269. xmlSchematronRulePtr rule,
  270. xmlNodePtr node, xmlChar *test, xmlChar *report)
  271. {
  272. xmlSchematronTestPtr ret;
  273. xmlXPathCompExprPtr comp;
  274. if ((ctxt == NULL) || (rule == NULL) || (node == NULL) ||
  275. (test == NULL))
  276. return(NULL);
  277. /*
  278. * try first to compile the test expression
  279. */
  280. comp = xmlXPathCtxtCompile(ctxt->xctxt, test);
  281. if (comp == NULL) {
  282. xmlSchematronPErr(ctxt, node,
  283. XML_SCHEMAP_NOROOT,
  284. "Failed to compile test expression %s",
  285. test, NULL);
  286. return(NULL);
  287. }
  288. ret = (xmlSchematronTestPtr) xmlMalloc(sizeof(xmlSchematronTest));
  289. if (ret == NULL) {
  290. xmlSchematronPErrMemory(ctxt, "allocating schema test", node);
  291. return (NULL);
  292. }
  293. memset(ret, 0, sizeof(xmlSchematronTest));
  294. ret->type = type;
  295. ret->node = node;
  296. ret->test = test;
  297. ret->comp = comp;
  298. ret->report = report;
  299. ret->next = NULL;
  300. if (rule->tests == NULL) {
  301. rule->tests = ret;
  302. } else {
  303. xmlSchematronTestPtr prev = rule->tests;
  304. while (prev->next != NULL)
  305. prev = prev->next;
  306. prev->next = ret;
  307. }
  308. return (ret);
  309. }
  310. /**
  311. * xmlSchematronFreeTests:
  312. * @tests: a list of tests
  313. *
  314. * Free a list of tests.
  315. */
  316. static void
  317. xmlSchematronFreeTests(xmlSchematronTestPtr tests) {
  318. xmlSchematronTestPtr next;
  319. while (tests != NULL) {
  320. next = tests->next;
  321. if (tests->test != NULL)
  322. xmlFree(tests->test);
  323. if (tests->comp != NULL)
  324. xmlXPathFreeCompExpr(tests->comp);
  325. if (tests->report != NULL)
  326. xmlFree(tests->report);
  327. xmlFree(tests);
  328. tests = next;
  329. }
  330. }
  331. /**
  332. * xmlSchematronAddRule:
  333. * @ctxt: the schema parsing context
  334. * @schema: a schema structure
  335. * @node: the node hosting the rule
  336. * @context: the associated context string
  337. * @report: the associated report string
  338. *
  339. * Add a rule to a schematron
  340. *
  341. * Returns the new pointer or NULL in case of error
  342. */
  343. static xmlSchematronRulePtr
  344. xmlSchematronAddRule(xmlSchematronParserCtxtPtr ctxt, xmlSchematronPtr schema,
  345. xmlSchematronPatternPtr pat, xmlNodePtr node,
  346. xmlChar *context, xmlChar *report)
  347. {
  348. xmlSchematronRulePtr ret;
  349. xmlPatternPtr pattern;
  350. if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
  351. (context == NULL))
  352. return(NULL);
  353. /*
  354. * Try first to compile the pattern
  355. */
  356. pattern = xmlPatterncompile(context, ctxt->dict, XML_PATTERN_XPATH,
  357. ctxt->namespaces);
  358. if (pattern == NULL) {
  359. xmlSchematronPErr(ctxt, node,
  360. XML_SCHEMAP_NOROOT,
  361. "Failed to compile context expression %s",
  362. context, NULL);
  363. }
  364. ret = (xmlSchematronRulePtr) xmlMalloc(sizeof(xmlSchematronRule));
  365. if (ret == NULL) {
  366. xmlSchematronPErrMemory(ctxt, "allocating schema rule", node);
  367. return (NULL);
  368. }
  369. memset(ret, 0, sizeof(xmlSchematronRule));
  370. ret->node = node;
  371. ret->context = context;
  372. ret->pattern = pattern;
  373. ret->report = report;
  374. ret->next = NULL;
  375. if (schema->rules == NULL) {
  376. schema->rules = ret;
  377. } else {
  378. xmlSchematronRulePtr prev = schema->rules;
  379. while (prev->next != NULL)
  380. prev = prev->next;
  381. prev->next = ret;
  382. }
  383. ret->patnext = NULL;
  384. if (pat->rules == NULL) {
  385. pat->rules = ret;
  386. } else {
  387. xmlSchematronRulePtr prev = pat->rules;
  388. while (prev->patnext != NULL)
  389. prev = prev->patnext;
  390. prev->patnext = ret;
  391. }
  392. return (ret);
  393. }
  394. /**
  395. * xmlSchematronFreeRules:
  396. * @rules: a list of rules
  397. *
  398. * Free a list of rules.
  399. */
  400. static void
  401. xmlSchematronFreeRules(xmlSchematronRulePtr rules) {
  402. xmlSchematronRulePtr next;
  403. while (rules != NULL) {
  404. next = rules->next;
  405. if (rules->tests)
  406. xmlSchematronFreeTests(rules->tests);
  407. if (rules->context != NULL)
  408. xmlFree(rules->context);
  409. if (rules->pattern)
  410. xmlFreePattern(rules->pattern);
  411. if (rules->report != NULL)
  412. xmlFree(rules->report);
  413. xmlFree(rules);
  414. rules = next;
  415. }
  416. }
  417. /**
  418. * xmlSchematronAddPattern:
  419. * @ctxt: the schema parsing context
  420. * @schema: a schema structure
  421. * @node: the node hosting the pattern
  422. * @id: the id or name of the pattern
  423. *
  424. * Add a pattern to a schematron
  425. *
  426. * Returns the new pointer or NULL in case of error
  427. */
  428. static xmlSchematronPatternPtr
  429. xmlSchematronAddPattern(xmlSchematronParserCtxtPtr ctxt,
  430. xmlSchematronPtr schema, xmlNodePtr node, xmlChar *name)
  431. {
  432. xmlSchematronPatternPtr ret;
  433. if ((ctxt == NULL) || (schema == NULL) || (node == NULL) || (name == NULL))
  434. return(NULL);
  435. ret = (xmlSchematronPatternPtr) xmlMalloc(sizeof(xmlSchematronPattern));
  436. if (ret == NULL) {
  437. xmlSchematronPErrMemory(ctxt, "allocating schema pattern", node);
  438. return (NULL);
  439. }
  440. memset(ret, 0, sizeof(xmlSchematronPattern));
  441. ret->name = name;
  442. ret->next = NULL;
  443. if (schema->patterns == NULL) {
  444. schema->patterns = ret;
  445. } else {
  446. xmlSchematronPatternPtr prev = schema->patterns;
  447. while (prev->next != NULL)
  448. prev = prev->next;
  449. prev->next = ret;
  450. }
  451. return (ret);
  452. }
  453. /**
  454. * xmlSchematronFreePatterns:
  455. * @patterns: a list of patterns
  456. *
  457. * Free a list of patterns.
  458. */
  459. static void
  460. xmlSchematronFreePatterns(xmlSchematronPatternPtr patterns) {
  461. xmlSchematronPatternPtr next;
  462. while (patterns != NULL) {
  463. next = patterns->next;
  464. if (patterns->name != NULL)
  465. xmlFree(patterns->name);
  466. xmlFree(patterns);
  467. patterns = next;
  468. }
  469. }
  470. /**
  471. * xmlSchematronNewSchematron:
  472. * @ctxt: a schema validation context
  473. *
  474. * Allocate a new Schematron structure.
  475. *
  476. * Returns the newly allocated structure or NULL in case or error
  477. */
  478. static xmlSchematronPtr
  479. xmlSchematronNewSchematron(xmlSchematronParserCtxtPtr ctxt)
  480. {
  481. xmlSchematronPtr ret;
  482. ret = (xmlSchematronPtr) xmlMalloc(sizeof(xmlSchematron));
  483. if (ret == NULL) {
  484. xmlSchematronPErrMemory(ctxt, "allocating schema", NULL);
  485. return (NULL);
  486. }
  487. memset(ret, 0, sizeof(xmlSchematron));
  488. ret->dict = ctxt->dict;
  489. xmlDictReference(ret->dict);
  490. return (ret);
  491. }
  492. /**
  493. * xmlSchematronFree:
  494. * @schema: a schema structure
  495. *
  496. * Deallocate a Schematron structure.
  497. */
  498. void
  499. xmlSchematronFree(xmlSchematronPtr schema)
  500. {
  501. if (schema == NULL)
  502. return;
  503. if ((schema->doc != NULL) && (!(schema->preserve)))
  504. xmlFreeDoc(schema->doc);
  505. if (schema->namespaces != NULL)
  506. xmlFree((char **) schema->namespaces);
  507. xmlSchematronFreeRules(schema->rules);
  508. xmlSchematronFreePatterns(schema->patterns);
  509. xmlDictFree(schema->dict);
  510. xmlFree(schema);
  511. }
  512. /**
  513. * xmlSchematronNewParserCtxt:
  514. * @URL: the location of the schema
  515. *
  516. * Create an XML Schematrons parse context for that file/resource expected
  517. * to contain an XML Schematrons file.
  518. *
  519. * Returns the parser context or NULL in case of error
  520. */
  521. xmlSchematronParserCtxtPtr
  522. xmlSchematronNewParserCtxt(const char *URL)
  523. {
  524. xmlSchematronParserCtxtPtr ret;
  525. if (URL == NULL)
  526. return (NULL);
  527. ret =
  528. (xmlSchematronParserCtxtPtr)
  529. xmlMalloc(sizeof(xmlSchematronParserCtxt));
  530. if (ret == NULL) {
  531. xmlSchematronPErrMemory(NULL, "allocating schema parser context",
  532. NULL);
  533. return (NULL);
  534. }
  535. memset(ret, 0, sizeof(xmlSchematronParserCtxt));
  536. ret->type = XML_STRON_CTXT_PARSER;
  537. ret->dict = xmlDictCreate();
  538. ret->URL = xmlDictLookup(ret->dict, (const xmlChar *) URL, -1);
  539. ret->includes = NULL;
  540. ret->xctxt = xmlXPathNewContext(NULL);
  541. if (ret->xctxt == NULL) {
  542. xmlSchematronPErrMemory(NULL, "allocating schema parser XPath context",
  543. NULL);
  544. xmlSchematronFreeParserCtxt(ret);
  545. return (NULL);
  546. }
  547. ret->xctxt->flags = XML_XPATH_CHECKNS;
  548. return (ret);
  549. }
  550. /**
  551. * xmlSchematronNewMemParserCtxt:
  552. * @buffer: a pointer to a char array containing the schemas
  553. * @size: the size of the array
  554. *
  555. * Create an XML Schematrons parse context for that memory buffer expected
  556. * to contain an XML Schematrons file.
  557. *
  558. * Returns the parser context or NULL in case of error
  559. */
  560. xmlSchematronParserCtxtPtr
  561. xmlSchematronNewMemParserCtxt(const char *buffer, int size)
  562. {
  563. xmlSchematronParserCtxtPtr ret;
  564. if ((buffer == NULL) || (size <= 0))
  565. return (NULL);
  566. ret =
  567. (xmlSchematronParserCtxtPtr)
  568. xmlMalloc(sizeof(xmlSchematronParserCtxt));
  569. if (ret == NULL) {
  570. xmlSchematronPErrMemory(NULL, "allocating schema parser context",
  571. NULL);
  572. return (NULL);
  573. }
  574. memset(ret, 0, sizeof(xmlSchematronParserCtxt));
  575. ret->buffer = buffer;
  576. ret->size = size;
  577. ret->dict = xmlDictCreate();
  578. ret->xctxt = xmlXPathNewContext(NULL);
  579. if (ret->xctxt == NULL) {
  580. xmlSchematronPErrMemory(NULL, "allocating schema parser XPath context",
  581. NULL);
  582. xmlSchematronFreeParserCtxt(ret);
  583. return (NULL);
  584. }
  585. return (ret);
  586. }
  587. /**
  588. * xmlSchematronNewDocParserCtxt:
  589. * @doc: a preparsed document tree
  590. *
  591. * Create an XML Schematrons parse context for that document.
  592. * NB. The document may be modified during the parsing process.
  593. *
  594. * Returns the parser context or NULL in case of error
  595. */
  596. xmlSchematronParserCtxtPtr
  597. xmlSchematronNewDocParserCtxt(xmlDocPtr doc)
  598. {
  599. xmlSchematronParserCtxtPtr ret;
  600. if (doc == NULL)
  601. return (NULL);
  602. ret =
  603. (xmlSchematronParserCtxtPtr)
  604. xmlMalloc(sizeof(xmlSchematronParserCtxt));
  605. if (ret == NULL) {
  606. xmlSchematronPErrMemory(NULL, "allocating schema parser context",
  607. NULL);
  608. return (NULL);
  609. }
  610. memset(ret, 0, sizeof(xmlSchematronParserCtxt));
  611. ret->doc = doc;
  612. ret->dict = xmlDictCreate();
  613. /* The application has responsibility for the document */
  614. ret->preserve = 1;
  615. ret->xctxt = xmlXPathNewContext(doc);
  616. if (ret->xctxt == NULL) {
  617. xmlSchematronPErrMemory(NULL, "allocating schema parser XPath context",
  618. NULL);
  619. xmlSchematronFreeParserCtxt(ret);
  620. return (NULL);
  621. }
  622. return (ret);
  623. }
  624. /**
  625. * xmlSchematronFreeParserCtxt:
  626. * @ctxt: the schema parser context
  627. *
  628. * Free the resources associated to the schema parser context
  629. */
  630. void
  631. xmlSchematronFreeParserCtxt(xmlSchematronParserCtxtPtr ctxt)
  632. {
  633. if (ctxt == NULL)
  634. return;
  635. if (ctxt->doc != NULL && !ctxt->preserve)
  636. xmlFreeDoc(ctxt->doc);
  637. if (ctxt->xctxt != NULL) {
  638. xmlXPathFreeContext(ctxt->xctxt);
  639. }
  640. if (ctxt->namespaces != NULL)
  641. xmlFree((char **) ctxt->namespaces);
  642. xmlDictFree(ctxt->dict);
  643. xmlFree(ctxt);
  644. }
  645. #if 0
  646. /**
  647. * xmlSchematronPushInclude:
  648. * @ctxt: the schema parser context
  649. * @doc: the included document
  650. * @cur: the current include node
  651. *
  652. * Add an included document
  653. */
  654. static void
  655. xmlSchematronPushInclude(xmlSchematronParserCtxtPtr ctxt,
  656. xmlDocPtr doc, xmlNodePtr cur)
  657. {
  658. if (ctxt->includes == NULL) {
  659. ctxt->maxIncludes = 10;
  660. ctxt->includes = (xmlNodePtr *)
  661. xmlMalloc(ctxt->maxIncludes * 2 * sizeof(xmlNodePtr));
  662. if (ctxt->includes == NULL) {
  663. xmlSchematronPErrMemory(NULL, "allocating parser includes",
  664. NULL);
  665. return;
  666. }
  667. ctxt->nbIncludes = 0;
  668. } else if (ctxt->nbIncludes + 2 >= ctxt->maxIncludes) {
  669. xmlNodePtr *tmp;
  670. tmp = (xmlNodePtr *)
  671. xmlRealloc(ctxt->includes, ctxt->maxIncludes * 4 *
  672. sizeof(xmlNodePtr));
  673. if (tmp == NULL) {
  674. xmlSchematronPErrMemory(NULL, "allocating parser includes",
  675. NULL);
  676. return;
  677. }
  678. ctxt->includes = tmp;
  679. ctxt->maxIncludes *= 2;
  680. }
  681. ctxt->includes[2 * ctxt->nbIncludes] = cur;
  682. ctxt->includes[2 * ctxt->nbIncludes + 1] = (xmlNodePtr) doc;
  683. ctxt->nbIncludes++;
  684. }
  685. /**
  686. * xmlSchematronPopInclude:
  687. * @ctxt: the schema parser context
  688. *
  689. * Pop an include level. The included document is being freed
  690. *
  691. * Returns the node immediately following the include or NULL if the
  692. * include list was empty.
  693. */
  694. static xmlNodePtr
  695. xmlSchematronPopInclude(xmlSchematronParserCtxtPtr ctxt)
  696. {
  697. xmlDocPtr doc;
  698. xmlNodePtr ret;
  699. if (ctxt->nbIncludes <= 0)
  700. return(NULL);
  701. ctxt->nbIncludes--;
  702. doc = (xmlDocPtr) ctxt->includes[2 * ctxt->nbIncludes + 1];
  703. ret = ctxt->includes[2 * ctxt->nbIncludes];
  704. xmlFreeDoc(doc);
  705. if (ret != NULL)
  706. ret = ret->next;
  707. if (ret == NULL)
  708. return(xmlSchematronPopInclude(ctxt));
  709. return(ret);
  710. }
  711. #endif
  712. /**
  713. * xmlSchematronAddNamespace:
  714. * @ctxt: the schema parser context
  715. * @prefix: the namespace prefix
  716. * @ns: the namespace name
  717. *
  718. * Add a namespace definition in the context
  719. */
  720. static void
  721. xmlSchematronAddNamespace(xmlSchematronParserCtxtPtr ctxt,
  722. const xmlChar *prefix, const xmlChar *ns)
  723. {
  724. if (ctxt->namespaces == NULL) {
  725. ctxt->maxNamespaces = 10;
  726. ctxt->namespaces = (const xmlChar **)
  727. xmlMalloc(ctxt->maxNamespaces * 2 * sizeof(const xmlChar *));
  728. if (ctxt->namespaces == NULL) {
  729. xmlSchematronPErrMemory(NULL, "allocating parser namespaces",
  730. NULL);
  731. return;
  732. }
  733. ctxt->nbNamespaces = 0;
  734. } else if (ctxt->nbNamespaces + 2 >= ctxt->maxNamespaces) {
  735. const xmlChar **tmp;
  736. tmp = (const xmlChar **)
  737. xmlRealloc((xmlChar **) ctxt->namespaces, ctxt->maxNamespaces * 4 *
  738. sizeof(const xmlChar *));
  739. if (tmp == NULL) {
  740. xmlSchematronPErrMemory(NULL, "allocating parser namespaces",
  741. NULL);
  742. return;
  743. }
  744. ctxt->namespaces = tmp;
  745. ctxt->maxNamespaces *= 2;
  746. }
  747. ctxt->namespaces[2 * ctxt->nbNamespaces] =
  748. xmlDictLookup(ctxt->dict, ns, -1);
  749. ctxt->namespaces[2 * ctxt->nbNamespaces + 1] =
  750. xmlDictLookup(ctxt->dict, prefix, -1);
  751. ctxt->nbNamespaces++;
  752. ctxt->namespaces[2 * ctxt->nbNamespaces] = NULL;
  753. ctxt->namespaces[2 * ctxt->nbNamespaces + 1] = NULL;
  754. }
  755. /**
  756. * xmlSchematronParseRule:
  757. * @ctxt: a schema validation context
  758. * @rule: the rule node
  759. *
  760. * parse a rule element
  761. */
  762. static void
  763. xmlSchematronParseRule(xmlSchematronParserCtxtPtr ctxt,
  764. xmlSchematronPatternPtr pattern,
  765. xmlNodePtr rule)
  766. {
  767. xmlNodePtr cur;
  768. int nbChecks = 0;
  769. xmlChar *test;
  770. xmlChar *context;
  771. xmlChar *report;
  772. xmlSchematronRulePtr ruleptr;
  773. xmlSchematronTestPtr testptr;
  774. if ((ctxt == NULL) || (rule == NULL)) return;
  775. context = xmlGetNoNsProp(rule, BAD_CAST "context");
  776. if (context == NULL) {
  777. xmlSchematronPErr(ctxt, rule,
  778. XML_SCHEMAP_NOROOT,
  779. "rule has no context attribute",
  780. NULL, NULL);
  781. return;
  782. } else if (context[0] == 0) {
  783. xmlSchematronPErr(ctxt, rule,
  784. XML_SCHEMAP_NOROOT,
  785. "rule has an empty context attribute",
  786. NULL, NULL);
  787. xmlFree(context);
  788. return;
  789. } else {
  790. ruleptr = xmlSchematronAddRule(ctxt, ctxt->schema, pattern,
  791. rule, context, NULL);
  792. if (ruleptr == NULL) {
  793. xmlFree(context);
  794. return;
  795. }
  796. }
  797. cur = rule->children;
  798. NEXT_SCHEMATRON(cur);
  799. while (cur != NULL) {
  800. if (IS_SCHEMATRON(cur, "assert")) {
  801. nbChecks++;
  802. test = xmlGetNoNsProp(cur, BAD_CAST "test");
  803. if (test == NULL) {
  804. xmlSchematronPErr(ctxt, cur,
  805. XML_SCHEMAP_NOROOT,
  806. "assert has no test attribute",
  807. NULL, NULL);
  808. } else if (test[0] == 0) {
  809. xmlSchematronPErr(ctxt, cur,
  810. XML_SCHEMAP_NOROOT,
  811. "assert has an empty test attribute",
  812. NULL, NULL);
  813. xmlFree(test);
  814. } else {
  815. /* TODO will need dynamic processing instead */
  816. report = xmlNodeGetContent(cur);
  817. testptr = xmlSchematronAddTest(ctxt, XML_SCHEMATRON_ASSERT,
  818. ruleptr, cur, test, report);
  819. if (testptr == NULL)
  820. xmlFree(test);
  821. }
  822. } else if (IS_SCHEMATRON(cur, "report")) {
  823. nbChecks++;
  824. test = xmlGetNoNsProp(cur, BAD_CAST "test");
  825. if (test == NULL) {
  826. xmlSchematronPErr(ctxt, cur,
  827. XML_SCHEMAP_NOROOT,
  828. "assert has no test attribute",
  829. NULL, NULL);
  830. } else if (test[0] == 0) {
  831. xmlSchematronPErr(ctxt, cur,
  832. XML_SCHEMAP_NOROOT,
  833. "assert has an empty test attribute",
  834. NULL, NULL);
  835. xmlFree(test);
  836. } else {
  837. /* TODO will need dynamic processing instead */
  838. report = xmlNodeGetContent(cur);
  839. testptr = xmlSchematronAddTest(ctxt, XML_SCHEMATRON_REPORT,
  840. ruleptr, cur, test, report);
  841. if (testptr == NULL)
  842. xmlFree(test);
  843. }
  844. } else {
  845. xmlSchematronPErr(ctxt, cur,
  846. XML_SCHEMAP_NOROOT,
  847. "Expecting an assert or a report element instead of %s",
  848. cur->name, NULL);
  849. }
  850. cur = cur->next;
  851. NEXT_SCHEMATRON(cur);
  852. }
  853. if (nbChecks == 0) {
  854. xmlSchematronPErr(ctxt, rule,
  855. XML_SCHEMAP_NOROOT,
  856. "rule has no assert nor report element", NULL, NULL);
  857. }
  858. }
  859. /**
  860. * xmlSchematronParsePattern:
  861. * @ctxt: a schema validation context
  862. * @pat: the pattern node
  863. *
  864. * parse a pattern element
  865. */
  866. static void
  867. xmlSchematronParsePattern(xmlSchematronParserCtxtPtr ctxt, xmlNodePtr pat)
  868. {
  869. xmlNodePtr cur;
  870. xmlSchematronPatternPtr pattern;
  871. int nbRules = 0;
  872. xmlChar *id;
  873. if ((ctxt == NULL) || (pat == NULL)) return;
  874. id = xmlGetNoNsProp(pat, BAD_CAST "id");
  875. if (id == NULL) {
  876. id = xmlGetNoNsProp(pat, BAD_CAST "name");
  877. }
  878. pattern = xmlSchematronAddPattern(ctxt, ctxt->schema, pat, id);
  879. if (pattern == NULL) {
  880. if (id != NULL)
  881. xmlFree(id);
  882. return;
  883. }
  884. cur = pat->children;
  885. NEXT_SCHEMATRON(cur);
  886. while (cur != NULL) {
  887. if (IS_SCHEMATRON(cur, "rule")) {
  888. xmlSchematronParseRule(ctxt, pattern, cur);
  889. nbRules++;
  890. } else {
  891. xmlSchematronPErr(ctxt, cur,
  892. XML_SCHEMAP_NOROOT,
  893. "Expecting a rule element instead of %s", cur->name, NULL);
  894. }
  895. cur = cur->next;
  896. NEXT_SCHEMATRON(cur);
  897. }
  898. if (nbRules == 0) {
  899. xmlSchematronPErr(ctxt, pat,
  900. XML_SCHEMAP_NOROOT,
  901. "Pattern has no rule element", NULL, NULL);
  902. }
  903. }
  904. #if 0
  905. /**
  906. * xmlSchematronLoadInclude:
  907. * @ctxt: a schema validation context
  908. * @cur: the include element
  909. *
  910. * Load the include document, Push the current pointer
  911. *
  912. * Returns the updated node pointer
  913. */
  914. static xmlNodePtr
  915. xmlSchematronLoadInclude(xmlSchematronParserCtxtPtr ctxt, xmlNodePtr cur)
  916. {
  917. xmlNodePtr ret = NULL;
  918. xmlDocPtr doc = NULL;
  919. xmlChar *href = NULL;
  920. xmlChar *base = NULL;
  921. xmlChar *URI = NULL;
  922. if ((ctxt == NULL) || (cur == NULL))
  923. return(NULL);
  924. href = xmlGetNoNsProp(cur, BAD_CAST "href");
  925. if (href == NULL) {
  926. xmlSchematronPErr(ctxt, cur,
  927. XML_SCHEMAP_NOROOT,
  928. "Include has no href attribute", NULL, NULL);
  929. return(cur->next);
  930. }
  931. /* do the URI base composition, load and find the root */
  932. base = xmlNodeGetBase(cur->doc, cur);
  933. URI = xmlBuildURI(href, base);
  934. doc = xmlReadFile((const char *) URI, NULL, SCHEMATRON_PARSE_OPTIONS);
  935. if (doc == NULL) {
  936. xmlSchematronPErr(ctxt, cur,
  937. XML_SCHEMAP_FAILED_LOAD,
  938. "could not load include '%s'.\n",
  939. URI, NULL);
  940. goto done;
  941. }
  942. ret = xmlDocGetRootElement(doc);
  943. if (ret == NULL) {
  944. xmlSchematronPErr(ctxt, cur,
  945. XML_SCHEMAP_FAILED_LOAD,
  946. "could not find root from include '%s'.\n",
  947. URI, NULL);
  948. goto done;
  949. }
  950. /* Success, push the include for rollback on exit */
  951. xmlSchematronPushInclude(ctxt, doc, cur);
  952. done:
  953. if (ret == NULL) {
  954. if (doc != NULL)
  955. xmlFreeDoc(doc);
  956. }
  957. xmlFree(href);
  958. if (base != NULL)
  959. xmlFree(base);
  960. if (URI != NULL)
  961. xmlFree(URI);
  962. return(ret);
  963. }
  964. #endif
  965. /**
  966. * xmlSchematronParse:
  967. * @ctxt: a schema validation context
  968. *
  969. * parse a schema definition resource and build an internal
  970. * XML Schema structure which can be used to validate instances.
  971. *
  972. * Returns the internal XML Schematron structure built from the resource or
  973. * NULL in case of error
  974. */
  975. xmlSchematronPtr
  976. xmlSchematronParse(xmlSchematronParserCtxtPtr ctxt)
  977. {
  978. xmlSchematronPtr ret = NULL;
  979. xmlDocPtr doc;
  980. xmlNodePtr root, cur;
  981. int preserve = 0;
  982. if (ctxt == NULL)
  983. return (NULL);
  984. ctxt->nberrors = 0;
  985. /*
  986. * First step is to parse the input document into an DOM/Infoset
  987. */
  988. if (ctxt->URL != NULL) {
  989. doc = xmlReadFile((const char *) ctxt->URL, NULL,
  990. SCHEMATRON_PARSE_OPTIONS);
  991. if (doc == NULL) {
  992. xmlSchematronPErr(ctxt, NULL,
  993. XML_SCHEMAP_FAILED_LOAD,
  994. "xmlSchematronParse: could not load '%s'.\n",
  995. ctxt->URL, NULL);
  996. return (NULL);
  997. }
  998. ctxt->preserve = 0;
  999. } else if (ctxt->buffer != NULL) {
  1000. doc = xmlReadMemory(ctxt->buffer, ctxt->size, NULL, NULL,
  1001. SCHEMATRON_PARSE_OPTIONS);
  1002. if (doc == NULL) {
  1003. xmlSchematronPErr(ctxt, NULL,
  1004. XML_SCHEMAP_FAILED_PARSE,
  1005. "xmlSchematronParse: could not parse.\n",
  1006. NULL, NULL);
  1007. return (NULL);
  1008. }
  1009. doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
  1010. ctxt->URL = xmlDictLookup(ctxt->dict, BAD_CAST "in_memory_buffer", -1);
  1011. ctxt->preserve = 0;
  1012. } else if (ctxt->doc != NULL) {
  1013. doc = ctxt->doc;
  1014. preserve = 1;
  1015. ctxt->preserve = 1;
  1016. } else {
  1017. xmlSchematronPErr(ctxt, NULL,
  1018. XML_SCHEMAP_NOTHING_TO_PARSE,
  1019. "xmlSchematronParse: could not parse.\n",
  1020. NULL, NULL);
  1021. return (NULL);
  1022. }
  1023. /*
  1024. * Then extract the root and Schematron parse it
  1025. */
  1026. root = xmlDocGetRootElement(doc);
  1027. if (root == NULL) {
  1028. xmlSchematronPErr(ctxt, (xmlNodePtr) doc,
  1029. XML_SCHEMAP_NOROOT,
  1030. "The schema has no document element.\n", NULL, NULL);
  1031. if (!preserve) {
  1032. xmlFreeDoc(doc);
  1033. }
  1034. return (NULL);
  1035. }
  1036. if (!IS_SCHEMATRON(root, "schema")) {
  1037. xmlSchematronPErr(ctxt, root,
  1038. XML_SCHEMAP_NOROOT,
  1039. "The XML document '%s' is not a XML schematron document",
  1040. ctxt->URL, NULL);
  1041. goto exit;
  1042. }
  1043. ret = xmlSchematronNewSchematron(ctxt);
  1044. if (ret == NULL)
  1045. goto exit;
  1046. ctxt->schema = ret;
  1047. /*
  1048. * scan the schema elements
  1049. */
  1050. cur = root->children;
  1051. NEXT_SCHEMATRON(cur);
  1052. if (IS_SCHEMATRON(cur, "title")) {
  1053. xmlChar *title = xmlNodeGetContent(cur);
  1054. if (title != NULL) {
  1055. ret->title = xmlDictLookup(ret->dict, title, -1);
  1056. xmlFree(title);
  1057. }
  1058. cur = cur->next;
  1059. NEXT_SCHEMATRON(cur);
  1060. }
  1061. while (IS_SCHEMATRON(cur, "ns")) {
  1062. xmlChar *prefix = xmlGetNoNsProp(cur, BAD_CAST "prefix");
  1063. xmlChar *uri = xmlGetNoNsProp(cur, BAD_CAST "uri");
  1064. if ((uri == NULL) || (uri[0] == 0)) {
  1065. xmlSchematronPErr(ctxt, cur,
  1066. XML_SCHEMAP_NOROOT,
  1067. "ns element has no uri", NULL, NULL);
  1068. }
  1069. if ((prefix == NULL) || (prefix[0] == 0)) {
  1070. xmlSchematronPErr(ctxt, cur,
  1071. XML_SCHEMAP_NOROOT,
  1072. "ns element has no prefix", NULL, NULL);
  1073. }
  1074. if ((prefix) && (uri)) {
  1075. xmlXPathRegisterNs(ctxt->xctxt, prefix, uri);
  1076. xmlSchematronAddNamespace(ctxt, prefix, uri);
  1077. ret->nbNs++;
  1078. }
  1079. if (uri)
  1080. xmlFree(uri);
  1081. if (prefix)
  1082. xmlFree(prefix);
  1083. cur = cur->next;
  1084. NEXT_SCHEMATRON(cur);
  1085. }
  1086. while (cur != NULL) {
  1087. if (IS_SCHEMATRON(cur, "pattern")) {
  1088. xmlSchematronParsePattern(ctxt, cur);
  1089. ret->nbPattern++;
  1090. } else {
  1091. xmlSchematronPErr(ctxt, cur,
  1092. XML_SCHEMAP_NOROOT,
  1093. "Expecting a pattern element instead of %s", cur->name, NULL);
  1094. }
  1095. cur = cur->next;
  1096. NEXT_SCHEMATRON(cur);
  1097. }
  1098. if (ret->nbPattern == 0) {
  1099. xmlSchematronPErr(ctxt, root,
  1100. XML_SCHEMAP_NOROOT,
  1101. "The schematron document '%s' has no pattern",
  1102. ctxt->URL, NULL);
  1103. goto exit;
  1104. }
  1105. /* the original document must be kept for reporting */
  1106. ret->doc = doc;
  1107. if (preserve) {
  1108. ret->preserve = 1;
  1109. }
  1110. preserve = 1;
  1111. exit:
  1112. if (!preserve) {
  1113. xmlFreeDoc(doc);
  1114. }
  1115. if (ret != NULL) {
  1116. if (ctxt->nberrors != 0) {
  1117. xmlSchematronFree(ret);
  1118. ret = NULL;
  1119. } else {
  1120. ret->namespaces = ctxt->namespaces;
  1121. ret->nbNamespaces = ctxt->nbNamespaces;
  1122. ctxt->namespaces = NULL;
  1123. }
  1124. }
  1125. return (ret);
  1126. }
  1127. /************************************************************************
  1128. * *
  1129. * Schematrontron Reports handler *
  1130. * *
  1131. ************************************************************************/
  1132. static xmlNodePtr
  1133. xmlSchematronGetNode(xmlSchematronValidCtxtPtr ctxt,
  1134. xmlNodePtr cur, const xmlChar *xpath) {
  1135. xmlNodePtr node = NULL;
  1136. xmlXPathObjectPtr ret;
  1137. if ((ctxt == NULL) || (cur == NULL) || (xpath == NULL))
  1138. return(NULL);
  1139. ctxt->xctxt->doc = cur->doc;
  1140. ctxt->xctxt->node = cur;
  1141. ret = xmlXPathEval(xpath, ctxt->xctxt);
  1142. if (ret == NULL)
  1143. return(NULL);
  1144. if ((ret->type == XPATH_NODESET) &&
  1145. (ret->nodesetval != NULL) && (ret->nodesetval->nodeNr > 0))
  1146. node = ret->nodesetval->nodeTab[0];
  1147. xmlXPathFreeObject(ret);
  1148. return(node);
  1149. }
  1150. /**
  1151. * xmlSchematronReportOutput:
  1152. * @ctxt: the validation context
  1153. * @cur: the current node tested
  1154. * @msg: the message output
  1155. *
  1156. * Output part of the report to whatever channel the user selected
  1157. */
  1158. static void
  1159. xmlSchematronReportOutput(xmlSchematronValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
  1160. xmlNodePtr cur ATTRIBUTE_UNUSED,
  1161. const char *msg) {
  1162. /* TODO */
  1163. fprintf(stderr, "%s", msg);
  1164. }
  1165. /**
  1166. * xmlSchematronFormatReport:
  1167. * @ctxt: the validation context
  1168. * @test: the test node
  1169. * @cur: the current node tested
  1170. *
  1171. * Build the string being reported to the user.
  1172. *
  1173. * Returns a report string or NULL in case of error. The string needs
  1174. * to be deallocated by the caller
  1175. */
  1176. static xmlChar *
  1177. xmlSchematronFormatReport(xmlSchematronValidCtxtPtr ctxt,
  1178. xmlNodePtr test, xmlNodePtr cur) {
  1179. xmlChar *ret = NULL;
  1180. xmlNodePtr child, node;
  1181. if ((test == NULL) || (cur == NULL))
  1182. return(ret);
  1183. child = test->children;
  1184. while (child != NULL) {
  1185. if ((child->type == XML_TEXT_NODE) ||
  1186. (child->type == XML_CDATA_SECTION_NODE))
  1187. ret = xmlStrcat(ret, child->content);
  1188. else if (IS_SCHEMATRON(child, "name")) {
  1189. xmlChar *path;
  1190. path = xmlGetNoNsProp(child, BAD_CAST "path");
  1191. node = cur;
  1192. if (path != NULL) {
  1193. node = xmlSchematronGetNode(ctxt, cur, path);
  1194. if (node == NULL)
  1195. node = cur;
  1196. xmlFree(path);
  1197. }
  1198. if ((node->ns == NULL) || (node->ns->prefix == NULL))
  1199. ret = xmlStrcat(ret, node->name);
  1200. else {
  1201. ret = xmlStrcat(ret, node->ns->prefix);
  1202. ret = xmlStrcat(ret, BAD_CAST ":");
  1203. ret = xmlStrcat(ret, node->name);
  1204. }
  1205. } else {
  1206. child = child->next;
  1207. continue;
  1208. }
  1209. /*
  1210. * remove superfluous \n
  1211. */
  1212. if (ret != NULL) {
  1213. int len = xmlStrlen(ret);
  1214. xmlChar c;
  1215. if (len > 0) {
  1216. c = ret[len - 1];
  1217. if ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t')) {
  1218. while ((c == ' ') || (c == '\n') ||
  1219. (c == '\r') || (c == '\t')) {
  1220. len--;
  1221. if (len == 0)
  1222. break;
  1223. c = ret[len - 1];
  1224. }
  1225. ret[len] = ' ';
  1226. ret[len + 1] = 0;
  1227. }
  1228. }
  1229. }
  1230. child = child->next;
  1231. }
  1232. return(ret);
  1233. }
  1234. /**
  1235. * xmlSchematronReportSuccess:
  1236. * @ctxt: the validation context
  1237. * @test: the compiled test
  1238. * @cur: the current node tested
  1239. * @success: boolean value for the result
  1240. *
  1241. * called from the validation engine when an assert or report test have
  1242. * been done.
  1243. */
  1244. static void
  1245. xmlSchematronReportSuccess(xmlSchematronValidCtxtPtr ctxt,
  1246. xmlSchematronTestPtr test, xmlNodePtr cur, xmlSchematronPatternPtr pattern, int success) {
  1247. if ((ctxt == NULL) || (cur == NULL) || (test == NULL))
  1248. return;
  1249. /* if quiet and not SVRL report only failures */
  1250. if ((ctxt->flags & XML_SCHEMATRON_OUT_QUIET) &&
  1251. ((ctxt->flags & XML_SCHEMATRON_OUT_XML) == 0) &&
  1252. (test->type == XML_SCHEMATRON_REPORT))
  1253. return;
  1254. if (ctxt->flags & XML_SCHEMATRON_OUT_XML) {
  1255. TODO
  1256. } else {
  1257. xmlChar *path;
  1258. char msg[1000];
  1259. long line;
  1260. const xmlChar *report = NULL;
  1261. if (((test->type == XML_SCHEMATRON_REPORT) & (!success)) ||
  1262. ((test->type == XML_SCHEMATRON_ASSERT) & (success)))
  1263. return;
  1264. line = xmlGetLineNo(cur);
  1265. path = xmlGetNodePath(cur);
  1266. if (path == NULL)
  1267. path = (xmlChar *) cur->name;
  1268. #if 0
  1269. if ((test->report != NULL) && (test->report[0] != 0))
  1270. report = test->report;
  1271. #endif
  1272. if (test->node != NULL)
  1273. report = xmlSchematronFormatReport(ctxt, test->node, cur);
  1274. if (report == NULL) {
  1275. if (test->type == XML_SCHEMATRON_ASSERT) {
  1276. report = xmlStrdup((const xmlChar *) "node failed assert");
  1277. } else {
  1278. report = xmlStrdup((const xmlChar *) "node failed report");
  1279. }
  1280. }
  1281. snprintf(msg, 999, "%s line %ld: %s\n", (const char *) path,
  1282. line, (const char *) report);
  1283. if (ctxt->flags & XML_SCHEMATRON_OUT_ERROR) {
  1284. xmlStructuredErrorFunc schannel = NULL;
  1285. xmlGenericErrorFunc channel = NULL;
  1286. void *data = NULL;
  1287. if (ctxt != NULL) {
  1288. if (ctxt->serror != NULL)
  1289. schannel = ctxt->serror;
  1290. else
  1291. channel = ctxt->error;
  1292. data = ctxt->userData;
  1293. }
  1294. __xmlRaiseError(schannel, channel, data,
  1295. NULL, cur, XML_FROM_SCHEMATRONV,
  1296. (test->type == XML_SCHEMATRON_ASSERT)?XML_SCHEMATRONV_ASSERT:XML_SCHEMATRONV_REPORT,
  1297. XML_ERR_ERROR, NULL, line,
  1298. (pattern == NULL)?NULL:((const char *) pattern->name),
  1299. (const char *) path,
  1300. (const char *) report, 0, 0,
  1301. "%s", msg);
  1302. } else {
  1303. xmlSchematronReportOutput(ctxt, cur, &msg[0]);
  1304. }
  1305. xmlFree((char *) report);
  1306. if ((path != NULL) && (path != (xmlChar *) cur->name))
  1307. xmlFree(path);
  1308. }
  1309. }
  1310. /**
  1311. * xmlSchematronReportPattern:
  1312. * @ctxt: the validation context
  1313. * @pattern: the current pattern
  1314. *
  1315. * called from the validation engine when starting to check a pattern
  1316. */
  1317. static void
  1318. xmlSchematronReportPattern(xmlSchematronValidCtxtPtr ctxt,
  1319. xmlSchematronPatternPtr pattern) {
  1320. if ((ctxt == NULL) || (pattern == NULL))
  1321. return;
  1322. if ((ctxt->flags & XML_SCHEMATRON_OUT_QUIET) || (ctxt->flags & XML_SCHEMATRON_OUT_ERROR)) /* Error gives pattern name as part of error */
  1323. return;
  1324. if (ctxt->flags & XML_SCHEMATRON_OUT_XML) {
  1325. TODO
  1326. } else {
  1327. char msg[1000];
  1328. if (pattern->name == NULL)
  1329. return;
  1330. snprintf(msg, 999, "Pattern: %s\n", (const char *) pattern->name);
  1331. xmlSchematronReportOutput(ctxt, NULL, &msg[0]);
  1332. }
  1333. }
  1334. /************************************************************************
  1335. * *
  1336. * Validation against a Schematrontron *
  1337. * *
  1338. ************************************************************************/
  1339. /**
  1340. * xmlSchematronSetValidStructuredErrors:
  1341. * @ctxt: a Schematron validation context
  1342. * @serror: the structured error function
  1343. * @ctx: the functions context
  1344. *
  1345. * Set the structured error callback
  1346. */
  1347. void
  1348. xmlSchematronSetValidStructuredErrors(xmlSchematronValidCtxtPtr ctxt,
  1349. xmlStructuredErrorFunc serror, void *ctx)
  1350. {
  1351. if (ctxt == NULL)
  1352. return;
  1353. ctxt->serror = serror;
  1354. ctxt->error = NULL;
  1355. ctxt->warning = NULL;
  1356. ctxt->userData = ctx;
  1357. }
  1358. /**
  1359. * xmlSchematronNewValidCtxt:
  1360. * @schema: a precompiled XML Schematrons
  1361. * @options: a set of xmlSchematronValidOptions
  1362. *
  1363. * Create an XML Schematrons validation context based on the given schema.
  1364. *
  1365. * Returns the validation context or NULL in case of error
  1366. */
  1367. xmlSchematronValidCtxtPtr
  1368. xmlSchematronNewValidCtxt(xmlSchematronPtr schema, int options)
  1369. {
  1370. int i;
  1371. xmlSchematronValidCtxtPtr ret;
  1372. ret = (xmlSchematronValidCtxtPtr) xmlMalloc(sizeof(xmlSchematronValidCtxt));
  1373. if (ret == NULL) {
  1374. xmlSchematronVErrMemory(NULL, "allocating validation context",
  1375. NULL);
  1376. return (NULL);
  1377. }
  1378. memset(ret, 0, sizeof(xmlSchematronValidCtxt));
  1379. ret->type = XML_STRON_CTXT_VALIDATOR;
  1380. ret->schema = schema;
  1381. ret->xctxt = xmlXPathNewContext(NULL);
  1382. ret->flags = options;
  1383. if (ret->xctxt == NULL) {
  1384. xmlSchematronPErrMemory(NULL, "allocating schema parser XPath context",
  1385. NULL);
  1386. xmlSchematronFreeValidCtxt(ret);
  1387. return (NULL);
  1388. }
  1389. for (i = 0;i < schema->nbNamespaces;i++) {
  1390. if ((schema->namespaces[2 * i] == NULL) ||
  1391. (schema->namespaces[2 * i + 1] == NULL))
  1392. break;
  1393. xmlXPathRegisterNs(ret->xctxt, schema->namespaces[2 * i + 1],
  1394. schema->namespaces[2 * i]);
  1395. }
  1396. return (ret);
  1397. }
  1398. /**
  1399. * xmlSchematronFreeValidCtxt:
  1400. * @ctxt: the schema validation context
  1401. *
  1402. * Free the resources associated to the schema validation context
  1403. */
  1404. void
  1405. xmlSchematronFreeValidCtxt(xmlSchematronValidCtxtPtr ctxt)
  1406. {
  1407. if (ctxt == NULL)
  1408. return;
  1409. if (ctxt->xctxt != NULL)
  1410. xmlXPathFreeContext(ctxt->xctxt);
  1411. if (ctxt->dict != NULL)
  1412. xmlDictFree(ctxt->dict);
  1413. xmlFree(ctxt);
  1414. }
  1415. static xmlNodePtr
  1416. xmlSchematronNextNode(xmlNodePtr cur) {
  1417. if (cur->children != NULL) {
  1418. /*
  1419. * Do not descend on entities declarations
  1420. */
  1421. if (cur->children->type != XML_ENTITY_DECL) {
  1422. cur = cur->children;
  1423. /*
  1424. * Skip DTDs
  1425. */
  1426. if (cur->type != XML_DTD_NODE)
  1427. return(cur);
  1428. }
  1429. }
  1430. while (cur->next != NULL) {
  1431. cur = cur->next;
  1432. if ((cur->type != XML_ENTITY_DECL) &&
  1433. (cur->type != XML_DTD_NODE))
  1434. return(cur);
  1435. }
  1436. do {
  1437. cur = cur->parent;
  1438. if (cur == NULL) break;
  1439. if (cur->type == XML_DOCUMENT_NODE) return(NULL);
  1440. if (cur->next != NULL) {
  1441. cur = cur->next;
  1442. return(cur);
  1443. }
  1444. } while (cur != NULL);
  1445. return(cur);
  1446. }
  1447. /**
  1448. * xmlSchematronRunTest:
  1449. * @ctxt: the schema validation context
  1450. * @test: the current test
  1451. * @instance: the document instance tree
  1452. * @cur: the current node in the instance
  1453. *
  1454. * Validate a rule against a tree instance at a given position
  1455. *
  1456. * Returns 1 in case of success, 0 if error and -1 in case of internal error
  1457. */
  1458. static int
  1459. xmlSchematronRunTest(xmlSchematronValidCtxtPtr ctxt,
  1460. xmlSchematronTestPtr test, xmlDocPtr instance, xmlNodePtr cur, xmlSchematronPatternPtr pattern)
  1461. {
  1462. xmlXPathObjectPtr ret;
  1463. int failed;
  1464. failed = 0;
  1465. ctxt->xctxt->doc = instance;
  1466. ctxt->xctxt->node = cur;
  1467. ret = xmlXPathCompiledEval(test->comp, ctxt->xctxt);
  1468. if (ret == NULL) {
  1469. failed = 1;
  1470. } else {
  1471. switch (ret->type) {
  1472. case XPATH_XSLT_TREE:
  1473. case XPATH_NODESET:
  1474. if ((ret->nodesetval == NULL) ||
  1475. (ret->nodesetval->nodeNr == 0))
  1476. failed = 1;
  1477. break;
  1478. case XPATH_BOOLEAN:
  1479. failed = !ret->boolval;
  1480. break;
  1481. case XPATH_NUMBER:
  1482. if ((xmlXPathIsNaN(ret->floatval)) ||
  1483. (ret->floatval == 0.0))
  1484. failed = 1;
  1485. break;
  1486. case XPATH_STRING:
  1487. if ((ret->stringval == NULL) ||
  1488. (ret->stringval[0] == 0))
  1489. failed = 1;
  1490. break;
  1491. case XPATH_UNDEFINED:
  1492. case XPATH_POINT:
  1493. case XPATH_RANGE:
  1494. case XPATH_LOCATIONSET:
  1495. case XPATH_USERS:
  1496. failed = 1;
  1497. break;
  1498. }
  1499. xmlXPathFreeObject(ret);
  1500. }
  1501. if ((failed) && (test->type == XML_SCHEMATRON_ASSERT))
  1502. ctxt->nberrors++;
  1503. else if ((!failed) && (test->type == XML_SCHEMATRON_REPORT))
  1504. ctxt->nberrors++;
  1505. xmlSchematronReportSuccess(ctxt, test, cur, pattern, !failed);
  1506. return(!failed);
  1507. }
  1508. /**
  1509. * xmlSchematronValidateDoc:
  1510. * @ctxt: the schema validation context
  1511. * @instance: the document instance tree
  1512. *
  1513. * Validate a tree instance against the schematron
  1514. *
  1515. * Returns 0 in case of success, -1 in case of internal error
  1516. * and an error count otherwise.
  1517. */
  1518. int
  1519. xmlSchematronValidateDoc(xmlSchematronValidCtxtPtr ctxt, xmlDocPtr instance)
  1520. {
  1521. xmlNodePtr cur, root;
  1522. xmlSchematronPatternPtr pattern;
  1523. xmlSchematronRulePtr rule;
  1524. xmlSchematronTestPtr test;
  1525. if ((ctxt == NULL) || (ctxt->schema == NULL) ||
  1526. (ctxt->schema->rules == NULL) || (instance == NULL))
  1527. return(-1);
  1528. ctxt->nberrors = 0;
  1529. root = xmlDocGetRootElement(instance);
  1530. if (root == NULL) {
  1531. TODO
  1532. ctxt->nberrors++;
  1533. return(1);
  1534. }
  1535. if ((ctxt->flags & XML_SCHEMATRON_OUT_QUIET) ||
  1536. (ctxt->flags == 0)) {
  1537. /*
  1538. * we are just trying to assert the validity of the document,
  1539. * speed primes over the output, run in a single pass
  1540. */
  1541. cur = root;
  1542. while (cur != NULL) {
  1543. rule = ctxt->schema->rules;
  1544. while (rule != NULL) {
  1545. if (xmlPatternMatch(rule->pattern, cur) == 1) {
  1546. test = rule->tests;
  1547. while (test != NULL) {
  1548. xmlSchematronRunTest(ctxt, test, instance, cur, (xmlSchematronPatternPtr)rule->pattern);
  1549. test = test->next;
  1550. }
  1551. }
  1552. rule = rule->next;
  1553. }
  1554. cur = xmlSchematronNextNode(cur);
  1555. }
  1556. } else {
  1557. /*
  1558. * Process all contexts one at a time
  1559. */
  1560. pattern = ctxt->schema->patterns;
  1561. while (pattern != NULL) {
  1562. xmlSchematronReportPattern(ctxt, pattern);
  1563. /*
  1564. * TODO convert the pattern rule to a direct XPath and
  1565. * compute directly instead of using the pattern matching
  1566. * over the full document...
  1567. * Check the exact semantic
  1568. */
  1569. cur = root;
  1570. while (cur != NULL) {
  1571. rule = pattern->rules;
  1572. while (rule != NULL) {
  1573. if (xmlPatternMatch(rule->pattern, cur) == 1) {
  1574. test = rule->tests;
  1575. while (test != NULL) {
  1576. xmlSchematronRunTest(ctxt, test, instance, cur, pattern);
  1577. test = test->next;
  1578. }
  1579. }
  1580. rule = rule->patnext;
  1581. }
  1582. cur = xmlSchematronNextNode(cur);
  1583. }
  1584. pattern = pattern->next;
  1585. }
  1586. }
  1587. return(ctxt->nberrors);
  1588. }
  1589. #ifdef STANDALONE
  1590. int
  1591. main(void)
  1592. {
  1593. int ret;
  1594. xmlDocPtr instance;
  1595. xmlSchematronParserCtxtPtr pctxt;
  1596. xmlSchematronValidCtxtPtr vctxt;
  1597. xmlSchematronPtr schema = NULL;
  1598. pctxt = xmlSchematronNewParserCtxt("tst.sct");
  1599. if (pctxt == NULL) {
  1600. fprintf(stderr, "failed to build schematron parser\n");
  1601. } else {
  1602. schema = xmlSchematronParse(pctxt);
  1603. if (schema == NULL) {
  1604. fprintf(stderr, "failed to compile schematron\n");
  1605. }
  1606. xmlSchematronFreeParserCtxt(pctxt);
  1607. }
  1608. instance = xmlReadFile("tst.sct", NULL,
  1609. XML_PARSE_NOENT | XML_PARSE_NOCDATA);
  1610. if (instance == NULL) {
  1611. fprintf(stderr, "failed to parse instance\n");
  1612. }
  1613. if ((schema != NULL) && (instance != NULL)) {
  1614. vctxt = xmlSchematronNewValidCtxt(schema);
  1615. if (vctxt == NULL) {
  1616. fprintf(stderr, "failed to build schematron validator\n");
  1617. } else {
  1618. ret = xmlSchematronValidateDoc(vctxt, instance);
  1619. xmlSchematronFreeValidCtxt(vctxt);
  1620. }
  1621. }
  1622. xmlSchematronFree(schema);
  1623. xmlFreeDoc(instance);
  1624. xmlCleanupParser();
  1625. xmlMemoryDump();
  1626. return (0);
  1627. }
  1628. #endif
  1629. #define bottom_schematron
  1630. #include "elfgcchack.h"
  1631. #endif /* LIBXML_SCHEMATRON_ENABLED */