schematron.c 47 KB

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