| 1 |
/* $Id: xml_validate.c,v 1.36 2005/01/07 01:52:43 mgrouch Exp $ */ |
| 2 |
|
| 3 |
/* |
| 4 |
|
| 5 |
XMLStarlet: Command Line Toolkit to query/edit/check/transform XML documents |
| 6 |
|
| 7 |
Copyright (c) 2002-2004 Mikhail Grushinskiy. All Rights Reserved. |
| 8 |
|
| 9 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 |
of this software and associated documentation files (the "Software"), to deal |
| 11 |
in the Software without restriction, including without limitation the rights |
| 12 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 |
copies of the Software, and to permit persons to whom the Software is |
| 14 |
furnished to do so, subject to the following conditions: |
| 15 |
|
| 16 |
The above copyright notice and this permission notice shall be included in |
| 17 |
all copies or substantial portions of the Software. |
| 18 |
|
| 19 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 |
THE SOFTWARE. |
| 26 |
|
| 27 |
*/ |
| 28 |
|
| 29 |
#include <config.h> |
| 30 |
|
| 31 |
#include <string.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <stdlib.h> |
| 34 |
|
| 35 |
#include "xmlstar.h" |
| 36 |
#include "trans.h" |
| 37 |
|
| 38 |
#ifdef LIBXML_SCHEMAS_ENABLED |
| 39 |
#include <libxml/xmlschemas.h> |
| 40 |
#include <libxml/xmlschemastypes.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
#ifdef LIBXML_SCHEMAS_ENABLED |
| 44 |
#include <libxml/relaxng.h> |
| 45 |
#endif |
| 46 |
|
| 47 |
#include <libxml/xmlreader.h> |
| 48 |
|
| 49 |
/* |
| 50 |
* TODO: Use cases |
| 51 |
* 1. find malfomed XML documents in a given set of XML files |
| 52 |
* 2. find XML documents which do not match DTD/XSD in a given set of XML files |
| 53 |
* 3. precompile DTD once |
| 54 |
*/ |
| 55 |
|
| 56 |
typedef struct _valOptions { |
| 57 |
char *dtd; /* External DTD URL or file name */ |
| 58 |
char *schema; /* External Schema URL or file name */ |
| 59 |
char *relaxng; /* External Relax-NG Schema URL or file name */ |
| 60 |
int err; /* Allow stderr messages */ |
| 61 |
int stop; /* Stop on first error */ |
| 62 |
int embed; /* Validate using embeded DTD */ |
| 63 |
int wellFormed; /* Check if well formed only */ |
| 64 |
int listGood; /* >0 list good, <0 list bad */ |
| 65 |
int show_val_res; /* display file names and valid/invalid message */ |
| 66 |
int nonet; /* disallow network access */ |
| 67 |
} valOptions; |
| 68 |
|
| 69 |
typedef valOptions *valOptionsPtr; |
| 70 |
|
| 71 |
/** |
| 72 |
* display short help message |
| 73 |
*/ |
| 74 |
void |
| 75 |
valUsage(int argc, char **argv, exit_status status) |
| 76 |
{ |
| 77 |
extern void fprint_validate_usage(FILE* o, const char* argv0); |
| 78 |
extern const char more_info[]; |
| 79 |
FILE *o = (status == EXIT_SUCCESS)? stdout : stderr; |
| 80 |
fprint_validate_usage(o, argv[0]); |
| 81 |
fprintf(o, "%s", more_info); |
| 82 |
exit(status); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Initialize global command line options |
| 87 |
*/ |
| 88 |
void |
| 89 |
valInitOptions(valOptionsPtr ops) |
| 90 |
{ |
| 91 |
ops->wellFormed = 1; |
| 92 |
ops->err = 0; |
| 93 |
ops->stop = 0; |
| 94 |
ops->embed = 0; |
| 95 |
ops->dtd = NULL; |
| 96 |
ops->schema = NULL; |
| 97 |
ops->relaxng = NULL; |
| 98 |
ops->nonet = 1; |
| 99 |
|
| 100 |
if (globalOptions.quiet) { |
| 101 |
ops->listGood = 0; |
| 102 |
ops->show_val_res = 0; |
| 103 |
} else { |
| 104 |
ops->listGood = -1; |
| 105 |
ops->show_val_res = 1; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Parse global command line options |
| 111 |
*/ |
| 112 |
int |
| 113 |
valParseOptions(valOptionsPtr ops, int argc, char **argv) |
| 114 |
{ |
| 115 |
int i; |
| 116 |
|
| 117 |
i = 2; |
| 118 |
while(i < argc) |
| 119 |
{ |
| 120 |
if (!strcmp(argv[i], "--well-formed") || !strcmp(argv[i], "-w")) |
| 121 |
{ |
| 122 |
ops->wellFormed = 1; |
| 123 |
i++; |
| 124 |
} |
| 125 |
else if (!strcmp(argv[i], "--err") || !strcmp(argv[i], "-e")) |
| 126 |
{ |
| 127 |
ops->err = 1; |
| 128 |
i++; |
| 129 |
} |
| 130 |
else if (!strcmp(argv[i], "--stop") || !strcmp(argv[i], "-S")) |
| 131 |
{ |
| 132 |
ops->stop = STOP; |
| 133 |
i++; |
| 134 |
} |
| 135 |
else if (!strcmp(argv[i], "--embed") || !strcmp(argv[i], "-E")) |
| 136 |
{ |
| 137 |
ops->embed = 1; |
| 138 |
i++; |
| 139 |
} |
| 140 |
else if (!strcmp(argv[i], "--list-good") || !strcmp(argv[i], "-g")) |
| 141 |
{ |
| 142 |
ops->listGood = 1; |
| 143 |
ops->show_val_res = 0; |
| 144 |
i++; |
| 145 |
} |
| 146 |
else if (!strcmp(argv[i], "--list-bad") || !strcmp(argv[i], "-b")) |
| 147 |
{ |
| 148 |
ops->listGood = -1; |
| 149 |
ops->show_val_res = 0; |
| 150 |
i++; |
| 151 |
} |
| 152 |
else if (!strcmp(argv[i], "--quiet") || !strcmp(argv[i], "-q")) |
| 153 |
{ |
| 154 |
ops->listGood = 0; |
| 155 |
ops->show_val_res = 0; |
| 156 |
i++; |
| 157 |
} |
| 158 |
else if (!strcmp(argv[i], "--dtd") || !strcmp(argv[i], "-d")) |
| 159 |
{ |
| 160 |
i++; |
| 161 |
if (i >= argc) valUsage(argc, argv, EXIT_BAD_ARGS); |
| 162 |
ops->dtd = argv[i]; |
| 163 |
i++; |
| 164 |
} |
| 165 |
else if (!strcmp(argv[i], "--xsd") || !strcmp(argv[i], "-s")) |
| 166 |
{ |
| 167 |
i++; |
| 168 |
if (i >= argc) valUsage(argc, argv, EXIT_BAD_ARGS); |
| 169 |
ops->schema = argv[i]; |
| 170 |
i++; |
| 171 |
} |
| 172 |
else if (!strcmp(argv[i], "--relaxng") || !strcmp(argv[i], "-r")) |
| 173 |
{ |
| 174 |
i++; |
| 175 |
if (i >= argc) valUsage(argc, argv, EXIT_BAD_ARGS); |
| 176 |
ops->relaxng = argv[i]; |
| 177 |
i++; |
| 178 |
} |
| 179 |
else if (!strcmp(argv[i], "--net")) |
| 180 |
{ |
| 181 |
ops->nonet = 0; |
| 182 |
i++; |
| 183 |
} |
| 184 |
else if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) |
| 185 |
{ |
| 186 |
valUsage(argc, argv, EXIT_SUCCESS); |
| 187 |
} |
| 188 |
else if (!strcmp(argv[i], "-")) |
| 189 |
{ |
| 190 |
i++; |
| 191 |
break; |
| 192 |
} |
| 193 |
else if (argv[i][0] == '-') |
| 194 |
{ |
| 195 |
valUsage(argc, argv, EXIT_BAD_ARGS); |
| 196 |
} |
| 197 |
else |
| 198 |
{ |
| 199 |
i++; |
| 200 |
break; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
return i-1; |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Validate XML document against DTD |
| 209 |
*/ |
| 210 |
int |
| 211 |
valAgainstDtd(valOptionsPtr ops, char* dtdvalid, xmlDocPtr doc, char* filename) |
| 212 |
{ |
| 213 |
int result = 0; |
| 214 |
|
| 215 |
if (dtdvalid != NULL) |
| 216 |
{ |
| 217 |
xmlDtdPtr dtd; |
| 218 |
|
| 219 |
#if !defined(LIBXML_VALID_ENABLED) |
| 220 |
xmlGenericError(xmlGenericErrorContext, |
| 221 |
"libxml2 has no validation support"); |
| 222 |
return 2; |
| 223 |
#endif |
| 224 |
dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid); |
| 225 |
if (dtd == NULL) |
| 226 |
{ |
| 227 |
xmlGenericError(xmlGenericErrorContext, |
| 228 |
"Could not parse DTD %s\n", dtdvalid); |
| 229 |
result = 2; |
| 230 |
} |
| 231 |
else |
| 232 |
{ |
| 233 |
xmlValidCtxtPtr cvp; |
| 234 |
|
| 235 |
if ((cvp = xmlNewValidCtxt()) == NULL) |
| 236 |
{ |
| 237 |
xmlGenericError(xmlGenericErrorContext, |
| 238 |
"Couldn't allocate validation context\n"); |
| 239 |
exit(-1); |
| 240 |
} |
| 241 |
|
| 242 |
if (ops->err) |
| 243 |
{ |
| 244 |
cvp->userData = (void *) stderr; |
| 245 |
cvp->error = (xmlValidityErrorFunc) fprintf; |
| 246 |
cvp->warning = (xmlValidityWarningFunc) fprintf; |
| 247 |
} |
| 248 |
else |
| 249 |
{ |
| 250 |
cvp->userData = (void *) NULL; |
| 251 |
cvp->error = (xmlValidityErrorFunc) NULL; |
| 252 |
cvp->warning = (xmlValidityWarningFunc) NULL; |
| 253 |
} |
| 254 |
|
| 255 |
if (!xmlValidateDtd(cvp, doc, dtd)) |
| 256 |
{ |
| 257 |
if ((ops->listGood < 0) && !ops->show_val_res) |
| 258 |
{ |
| 259 |
fprintf(stdout, "%s\n", filename); |
| 260 |
} |
| 261 |
else if (ops->listGood == 0) |
| 262 |
xmlGenericError(xmlGenericErrorContext, |
| 263 |
"%s: does not match %s\n", |
| 264 |
filename, dtdvalid); |
| 265 |
result = 3; |
| 266 |
} |
| 267 |
else |
| 268 |
{ |
| 269 |
if ((ops->listGood > 0) && !ops->show_val_res) |
| 270 |
{ |
| 271 |
fprintf(stdout, "%s\n", filename); |
| 272 |
} |
| 273 |
} |
| 274 |
xmlFreeDtd(dtd); |
| 275 |
xmlFreeValidCtxt(cvp); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
return result; |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* This is the main function for 'validate' option |
| 284 |
*/ |
| 285 |
int |
| 286 |
valMain(int argc, char **argv) |
| 287 |
{ |
| 288 |
int start; |
| 289 |
static valOptions ops; |
| 290 |
static ErrorInfo errorInfo; |
| 291 |
int invalidFound = 0; |
| 292 |
int options = XML_PARSE_DTDLOAD | XML_PARSE_DTDATTR; |
| 293 |
|
| 294 |
if (argc <= 2) valUsage(argc, argv, EXIT_BAD_ARGS); |
| 295 |
valInitOptions(&ops); |
| 296 |
start = valParseOptions(&ops, argc, argv); |
| 297 |
if (ops.nonet) options |= XML_PARSE_NONET; |
| 298 |
|
| 299 |
errorInfo.verbose = ops.err; |
| 300 |
xmlSetStructuredErrorFunc(&errorInfo, reportError); |
| 301 |
xmlLineNumbersDefault(1); |
| 302 |
|
| 303 |
if (ops.dtd) |
| 304 |
{ |
| 305 |
/* xmlReader doesn't work with external dtd, have to use SAX |
| 306 |
* interface */ |
| 307 |
int i; |
| 308 |
|
| 309 |
/* we have to exit() from the error reporting function to implement |
| 310 |
--stop */ |
| 311 |
errorInfo.stop = ops.stop; |
| 312 |
|
| 313 |
for (i=start; i<argc; i++) |
| 314 |
{ |
| 315 |
xmlDocPtr doc; |
| 316 |
int failed; |
| 317 |
|
| 318 |
failed = 0; |
| 319 |
doc = NULL; |
| 320 |
|
| 321 |
errorInfo.filename = argv[i]; |
| 322 |
doc = xmlReadFile(argv[i], NULL, options); |
| 323 |
if (doc) |
| 324 |
{ |
| 325 |
/* TODO: precompile DTD once */ |
| 326 |
failed = valAgainstDtd(&ops, ops.dtd, doc, argv[i]); |
| 327 |
xmlFreeDoc(doc); |
| 328 |
} |
| 329 |
else |
| 330 |
{ |
| 331 |
failed = 1; /* Malformed XML or could not open file */ |
| 332 |
if ((ops.listGood < 0) && !ops.show_val_res) |
| 333 |
{ |
| 334 |
fprintf(stdout, "%s\n", argv[i]); |
| 335 |
} |
| 336 |
} |
| 337 |
if (failed) invalidFound = 1; |
| 338 |
|
| 339 |
if (ops.show_val_res) |
| 340 |
{ |
| 341 |
if (!failed) |
| 342 |
fprintf(stdout, "%s - valid\n", argv[i]); |
| 343 |
else |
| 344 |
fprintf(stdout, "%s - invalid\n", argv[i]); |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
else if (ops.schema || ops.relaxng || ops.embed || ops.wellFormed) |
| 349 |
{ |
| 350 |
int i; |
| 351 |
xmlTextReaderPtr reader = NULL; |
| 352 |
|
| 353 |
#ifdef LIBXML_SCHEMAS_ENABLED |
| 354 |
xmlSchemaPtr schema = NULL; |
| 355 |
xmlSchemaParserCtxtPtr schemaParserCtxt = NULL; |
| 356 |
xmlSchemaValidCtxtPtr schemaCtxt = NULL; |
| 357 |
|
| 358 |
xmlRelaxNGPtr relaxng = NULL; |
| 359 |
xmlRelaxNGParserCtxtPtr relaxngParserCtxt = NULL; |
| 360 |
/* there is no xmlTextReaderRelaxNGValidateCtxt() !? */ |
| 361 |
|
| 362 |
/* TODO: Do not print debug stuff */ |
| 363 |
if (ops.schema) |
| 364 |
{ |
| 365 |
schemaParserCtxt = xmlSchemaNewParserCtxt(ops.schema); |
| 366 |
if (!schemaParserCtxt) |
| 367 |
{ |
| 368 |
invalidFound = 2; |
| 369 |
goto schemaCleanup; |
| 370 |
} |
| 371 |
errorInfo.filename = ops.schema; |
| 372 |
schema = xmlSchemaParse(schemaParserCtxt); |
| 373 |
if (!schema) |
| 374 |
{ |
| 375 |
invalidFound = 2; |
| 376 |
goto schemaCleanup; |
| 377 |
} |
| 378 |
|
| 379 |
xmlSchemaFreeParserCtxt(schemaParserCtxt); |
| 380 |
schemaCtxt = xmlSchemaNewValidCtxt(schema); |
| 381 |
if (!schemaCtxt) |
| 382 |
{ |
| 383 |
invalidFound = 2; |
| 384 |
goto schemaCleanup; |
| 385 |
} |
| 386 |
|
| 387 |
} |
| 388 |
else if (ops.relaxng) |
| 389 |
{ |
| 390 |
relaxngParserCtxt = xmlRelaxNGNewParserCtxt(ops.relaxng); |
| 391 |
if (!relaxngParserCtxt) |
| 392 |
{ |
| 393 |
invalidFound = 2; |
| 394 |
goto schemaCleanup; |
| 395 |
} |
| 396 |
|
| 397 |
errorInfo.filename = ops.relaxng; |
| 398 |
relaxng = xmlRelaxNGParse(relaxngParserCtxt); |
| 399 |
if (!relaxng) |
| 400 |
{ |
| 401 |
invalidFound = 2; |
| 402 |
goto schemaCleanup; |
| 403 |
} |
| 404 |
|
| 405 |
} |
| 406 |
#endif /* LIBXML_SCHEMAS_ENABLED */ |
| 407 |
|
| 408 |
for (i=start; i<argc; i++) |
| 409 |
{ |
| 410 |
int failed = 0; |
| 411 |
if (ops.embed) options |= XML_PARSE_DTDVALID; |
| 412 |
|
| 413 |
if (!reader) |
| 414 |
{ |
| 415 |
reader = xmlReaderForFile(argv[i], NULL, options); |
| 416 |
} |
| 417 |
else |
| 418 |
{ |
| 419 |
failed = xmlReaderNewFile(reader, argv[i], NULL, options); |
| 420 |
} |
| 421 |
|
| 422 |
errorInfo.xmlReader = reader; |
| 423 |
errorInfo.filename = argv[i]; |
| 424 |
|
| 425 |
/* It makes no sense to continue if we are not reporting errors |
| 426 |
* anyway. Note this doesn't apply to the --dtd case because the we |
| 427 |
* can't stop there without aborting the whole program (and |
| 428 |
* therefore we wouldn't be able to check multiple files). |
| 429 |
*/ |
| 430 |
if (!ops.err) |
| 431 |
ops.stop = STOP; |
| 432 |
|
| 433 |
if (reader && !failed) |
| 434 |
{ |
| 435 |
#ifdef LIBXML_SCHEMAS_ENABLED |
| 436 |
if (schemaCtxt) |
| 437 |
{ |
| 438 |
failed = xmlTextReaderSchemaValidateCtxt(reader, |
| 439 |
schemaCtxt, 0); |
| 440 |
} |
| 441 |
else if (relaxng) |
| 442 |
{ |
| 443 |
failed = xmlTextReaderRelaxNGSetSchema(reader, |
| 444 |
relaxng); |
| 445 |
} |
| 446 |
#endif /* LIBXML_SCHEMAS_ENABLED */ |
| 447 |
|
| 448 |
if (failed == 0) |
| 449 |
{ |
| 450 |
int more_nodes; |
| 451 |
int validating = (schema || relaxng || ops.embed); |
| 452 |
do |
| 453 |
{ |
| 454 |
more_nodes = xmlTextReaderRead(reader); |
| 455 |
failed = |
| 456 |
(more_nodes == -1)? 1 : |
| 457 |
(!validating)? 0 : |
| 458 |
xmlTextReaderIsValid(reader) != 1; |
| 459 |
} while (more_nodes == 1 && (!failed || !ops.stop)); |
| 460 |
} |
| 461 |
} |
| 462 |
else |
| 463 |
{ |
| 464 |
if (ops.err) |
| 465 |
fprintf(stderr, "couldn't read file '%s'\n", errorInfo.filename); |
| 466 |
failed = 1; /* could not open file */ |
| 467 |
} |
| 468 |
if (failed) invalidFound = 1; |
| 469 |
|
| 470 |
if (!ops.show_val_res) |
| 471 |
{ |
| 472 |
if ((ops.listGood > 0) && !failed) |
| 473 |
fprintf(stdout, "%s\n", argv[i]); |
| 474 |
if ((ops.listGood < 0) && failed) |
| 475 |
fprintf(stdout, "%s\n", argv[i]); |
| 476 |
} |
| 477 |
else |
| 478 |
{ |
| 479 |
if (!failed) |
| 480 |
fprintf(stdout, "%s - valid\n", argv[i]); |
| 481 |
else |
| 482 |
fprintf(stdout, "%s - invalid\n", argv[i]); |
| 483 |
} |
| 484 |
} |
| 485 |
errorInfo.xmlReader = NULL; |
| 486 |
xmlFreeTextReader(reader); |
| 487 |
|
| 488 |
#ifdef LIBXML_SCHEMAS_ENABLED |
| 489 |
schemaCleanup: |
| 490 |
xmlSchemaFreeValidCtxt(schemaCtxt); |
| 491 |
xmlRelaxNGFree(relaxng); |
| 492 |
xmlSchemaFree(schema); |
| 493 |
xmlRelaxNGCleanupTypes(); |
| 494 |
xmlSchemaCleanupTypes(); |
| 495 |
#endif /* LIBXML_SCHEMAS_ENABLED */ |
| 496 |
} |
| 497 |
|
| 498 |
xmlCleanupParser(); |
| 499 |
return invalidFound; |
| 500 |
} |