Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * schemas.c : implementation of the XML Schema handling and |
| 3 | * schema validity checking |
| 4 | * |
| 5 | * See Copyright for the status of this software. |
| 6 | * |
| 7 | * Daniel Veillard <veillard@redhat.com> |
| 8 | */ |
| 9 | |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 10 | /* |
| 11 | * TODO: |
| 12 | * - when types are redefined in includes, check that all |
| 13 | * types in the redef list are equal |
| 14 | * -> need a type equality operation. |
| 15 | */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 16 | #define IN_LIBXML |
| 17 | #include "libxml.h" |
| 18 | |
| 19 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <libxml/xmlmemory.h> |
| 23 | #include <libxml/parser.h> |
| 24 | #include <libxml/parserInternals.h> |
| 25 | #include <libxml/hash.h> |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 26 | #include <libxml/uri.h> |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 27 | |
| 28 | #include <libxml/xmlschemas.h> |
| 29 | #include <libxml/schemasInternals.h> |
| 30 | #include <libxml/xmlschemastypes.h> |
| 31 | #include <libxml/xmlautomata.h> |
| 32 | #include <libxml/xmlregexp.h> |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 33 | #include <libxml/dict.h> |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 34 | |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 35 | /* #define DEBUG 1 */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 36 | |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 37 | /* #define DEBUG_CONTENT 1 */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 38 | |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 39 | /* #define DEBUG_TYPE 1 */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 40 | |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 41 | /* #define DEBUG_CONTENT_REGEXP 1 */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 42 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 43 | /* #define DEBUG_AUTOMATA 1 */ |
| 44 | |
| 45 | #define UNBOUNDED (1 << 30) |
| 46 | #define TODO \ |
| 47 | xmlGenericError(xmlGenericErrorContext, \ |
| 48 | "Unimplemented block at %s:%d\n", \ |
| 49 | __FILE__, __LINE__); |
| 50 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 51 | #define XML_SCHEMAS_DEFAULT_NAMESPACE (const xmlChar *)"the default namespace" |
| 52 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 53 | /* |
| 54 | * The XML Schemas namespaces |
| 55 | */ |
| 56 | static const xmlChar *xmlSchemaNs = (const xmlChar *) |
| 57 | "http://www.w3.org/2001/XMLSchema"; |
| 58 | |
| 59 | static const xmlChar *xmlSchemaInstanceNs = (const xmlChar *) |
| 60 | "http://www.w3.org/2001/XMLSchema-instance"; |
| 61 | |
| 62 | #define IS_SCHEMA(node, type) \ |
| 63 | ((node != NULL) && (node->ns != NULL) && \ |
| 64 | (xmlStrEqual(node->name, (const xmlChar *) type)) && \ |
| 65 | (xmlStrEqual(node->ns->href, xmlSchemaNs))) |
| 66 | |
| 67 | #define XML_SCHEMAS_PARSE_ERROR 1 |
| 68 | |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 69 | #define SCHEMAS_PARSE_OPTIONS XML_PARSE_NOENT |
| 70 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 71 | struct _xmlSchemaParserCtxt { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 72 | void *userData; /* user specific data block */ |
| 73 | xmlSchemaValidityErrorFunc error; /* the callback in case of errors */ |
| 74 | xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */ |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 75 | xmlSchemaValidError err; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 76 | int nberrors; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 77 | xmlStructuredErrorFunc serror; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 78 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 79 | xmlSchemaPtr topschema; /* The main schema */ |
| 80 | xmlHashTablePtr namespaces; /* Hash table of namespaces to schemas */ |
| 81 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 82 | xmlSchemaPtr schema; /* The schema in use */ |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 83 | const xmlChar *container; /* the current element, group, ... */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 84 | int counter; |
| 85 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 86 | const xmlChar *URL; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 87 | xmlDocPtr doc; |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 88 | int preserve; /* Whether the doc should be freed */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 89 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 90 | const char *buffer; |
| 91 | int size; |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 92 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 93 | /* |
| 94 | * Used to build complex element content models |
| 95 | */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 96 | xmlAutomataPtr am; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 97 | xmlAutomataStatePtr start; |
| 98 | xmlAutomataStatePtr end; |
| 99 | xmlAutomataStatePtr state; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 100 | |
| 101 | xmlDictPtr dict; /* dictionnary for interned string names */ |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 102 | int includes; /* the inclusion level, 0 for root or imports */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | |
| 106 | #define XML_SCHEMAS_ATTR_UNKNOWN 1 |
| 107 | #define XML_SCHEMAS_ATTR_CHECKED 2 |
| 108 | |
| 109 | typedef struct _xmlSchemaAttrState xmlSchemaAttrState; |
| 110 | typedef xmlSchemaAttrState *xmlSchemaAttrStatePtr; |
| 111 | struct _xmlSchemaAttrState { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 112 | xmlAttrPtr attr; |
| 113 | int state; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | /** |
| 117 | * xmlSchemaValidCtxt: |
| 118 | * |
| 119 | * A Schemas validation context |
| 120 | */ |
| 121 | |
| 122 | struct _xmlSchemaValidCtxt { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 123 | void *userData; /* user specific data block */ |
| 124 | xmlSchemaValidityErrorFunc error; /* the callback in case of errors */ |
| 125 | xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */ |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 126 | xmlStructuredErrorFunc serror; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 127 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 128 | xmlSchemaPtr schema; /* The schema in use */ |
| 129 | xmlDocPtr doc; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 130 | xmlParserInputBufferPtr input; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 131 | xmlCharEncoding enc; |
| 132 | xmlSAXHandlerPtr sax; |
| 133 | void *user_data; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 134 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 135 | xmlDocPtr myDoc; |
| 136 | int err; |
| 137 | int nberrors; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 138 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 139 | xmlNodePtr node; |
| 140 | xmlNodePtr cur; |
| 141 | xmlSchemaTypePtr type; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 142 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 143 | xmlRegExecCtxtPtr regexp; |
| 144 | xmlSchemaValPtr value; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 145 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 146 | int attrNr; |
| 147 | int attrBase; |
| 148 | int attrMax; |
| 149 | xmlSchemaAttrStatePtr attr; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 152 | /* |
| 153 | * These are the entries in the schemas importSchemas hash table |
| 154 | */ |
| 155 | typedef struct _xmlSchemaImport xmlSchemaImport; |
| 156 | typedef xmlSchemaImport *xmlSchemaImportPtr; |
| 157 | struct _xmlSchemaImport { |
| 158 | const xmlChar *schemaLocation; |
| 159 | xmlSchemaPtr schema; |
| 160 | }; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 161 | |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 162 | /* |
| 163 | * These are the entries associated to includes in a schemas |
| 164 | */ |
| 165 | typedef struct _xmlSchemaInclude xmlSchemaInclude; |
| 166 | typedef xmlSchemaInclude *xmlSchemaIncludePtr; |
| 167 | struct _xmlSchemaInclude { |
| 168 | xmlSchemaIncludePtr next; |
| 169 | |
| 170 | const xmlChar *schemaLocation; |
| 171 | xmlDocPtr doc; |
| 172 | }; |
| 173 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 174 | /************************************************************************ |
| 175 | * * |
| 176 | * Some predeclarations * |
| 177 | * * |
| 178 | ************************************************************************/ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 179 | static int xmlSchemaValidateSimpleValue(xmlSchemaValidCtxtPtr ctxt, |
| 180 | xmlSchemaTypePtr type, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 181 | const xmlChar * value); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 182 | |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 183 | static int xmlSchemaParseInclude(xmlSchemaParserCtxtPtr ctxt, |
| 184 | xmlSchemaPtr schema, |
| 185 | xmlNodePtr node); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 186 | /************************************************************************ |
| 187 | * * |
| 188 | * Datatype error handlers * |
| 189 | * * |
| 190 | ************************************************************************/ |
| 191 | |
| 192 | /** |
| 193 | * xmlSchemaPErrMemory: |
| 194 | * @node: a context node |
| 195 | * @extra: extra informations |
| 196 | * |
| 197 | * Handle an out of memory condition |
| 198 | */ |
| 199 | static void |
| 200 | xmlSchemaPErrMemory(xmlSchemaParserCtxtPtr ctxt, |
| 201 | const char *extra, xmlNodePtr node) |
| 202 | { |
| 203 | if (ctxt != NULL) |
| 204 | ctxt->nberrors++; |
| 205 | __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, node, NULL, |
| 206 | extra); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * xmlSchemaPErr: |
| 211 | * @ctxt: the parsing context |
| 212 | * @node: the context node |
| 213 | * @error: the error code |
| 214 | * @msg: the error message |
| 215 | * @str1: extra data |
| 216 | * @str2: extra data |
| 217 | * |
| 218 | * Handle a parser error |
| 219 | */ |
| 220 | static void |
| 221 | xmlSchemaPErr(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error, |
| 222 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 223 | { |
| 224 | xmlGenericErrorFunc channel = NULL; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 225 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 226 | void *data = NULL; |
| 227 | |
| 228 | if (ctxt != NULL) { |
| 229 | ctxt->nberrors++; |
| 230 | channel = ctxt->error; |
| 231 | data = ctxt->userData; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 232 | schannel = ctxt->serror; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 233 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 234 | __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 235 | error, XML_ERR_ERROR, NULL, 0, |
| 236 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 237 | msg, str1, str2); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * xmlSchemaPErr2: |
| 242 | * @ctxt: the parsing context |
| 243 | * @node: the context node |
| 244 | * @node: the current child |
| 245 | * @error: the error code |
| 246 | * @msg: the error message |
| 247 | * @str1: extra data |
| 248 | * @str2: extra data |
| 249 | * |
| 250 | * Handle a parser error |
| 251 | */ |
| 252 | static void |
| 253 | xmlSchemaPErr2(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, |
| 254 | xmlNodePtr child, int error, |
| 255 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 256 | { |
| 257 | if (child != NULL) |
| 258 | xmlSchemaPErr(ctxt, child, error, msg, str1, str2); |
| 259 | else |
| 260 | xmlSchemaPErr(ctxt, node, error, msg, str1, str2); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * xmlSchemaVTypeErrMemory: |
| 265 | * @node: a context node |
| 266 | * @extra: extra informations |
| 267 | * |
| 268 | * Handle an out of memory condition |
| 269 | */ |
| 270 | static void |
| 271 | xmlSchemaVErrMemory(xmlSchemaValidCtxtPtr ctxt, |
| 272 | const char *extra, xmlNodePtr node) |
| 273 | { |
| 274 | if (ctxt != NULL) { |
| 275 | ctxt->nberrors++; |
| 276 | ctxt->err = XML_SCHEMAS_ERR_INTERNAL; |
| 277 | } |
| 278 | __xmlSimpleError(XML_FROM_SCHEMASV, XML_ERR_NO_MEMORY, node, NULL, |
| 279 | extra); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * xmlSchemaVErr3: |
| 284 | * @ctxt: the validation context |
| 285 | * @node: the context node |
| 286 | * @error: the error code |
| 287 | * @msg: the error message |
| 288 | * @str1: extra data |
| 289 | * @str2: extra data |
| 290 | * @str3: extra data |
| 291 | * |
| 292 | * Handle a validation error |
| 293 | */ |
| 294 | static void |
| 295 | xmlSchemaVErr3(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node, int error, |
| 296 | const char *msg, const xmlChar *str1, const xmlChar *str2, |
| 297 | const xmlChar *str3) |
| 298 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 299 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 300 | xmlGenericErrorFunc channel = NULL; |
| 301 | void *data = NULL; |
| 302 | |
| 303 | if (ctxt != NULL) { |
| 304 | ctxt->nberrors++; |
| 305 | ctxt->err = error; |
| 306 | channel = ctxt->error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 307 | schannel = ctxt->serror; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 308 | data = ctxt->userData; |
| 309 | } |
| 310 | /* reajust to global error numbers */ |
| 311 | error += XML_SCHEMAV_NOROOT - XML_SCHEMAS_ERR_NOROOT; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 312 | __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASV, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 313 | error, XML_ERR_ERROR, NULL, 0, |
| 314 | (const char *) str1, (const char *) str2, |
| 315 | (const char *) str3, 0, 0, |
| 316 | msg, str1, str2, str3); |
| 317 | } |
| 318 | /** |
| 319 | * xmlSchemaVErr: |
| 320 | * @ctxt: the validation context |
| 321 | * @node: the context node |
| 322 | * @error: the error code |
| 323 | * @msg: the error message |
| 324 | * @str1: extra data |
| 325 | * @str2: extra data |
| 326 | * |
| 327 | * Handle a validation error |
| 328 | */ |
| 329 | static void |
| 330 | xmlSchemaVErr(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node, int error, |
| 331 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 332 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 333 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 334 | xmlGenericErrorFunc channel = NULL; |
| 335 | void *data = NULL; |
| 336 | |
| 337 | if (ctxt != NULL) { |
| 338 | ctxt->nberrors++; |
| 339 | ctxt->err = error; |
| 340 | channel = ctxt->error; |
| 341 | data = ctxt->userData; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 342 | schannel = ctxt->serror; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 343 | } |
| 344 | /* reajust to global error numbers */ |
| 345 | error += XML_SCHEMAV_NOROOT - XML_SCHEMAS_ERR_NOROOT; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 346 | __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASV, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 347 | error, XML_ERR_ERROR, NULL, 0, |
| 348 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 349 | msg, str1, str2); |
| 350 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 351 | |
| 352 | /************************************************************************ |
| 353 | * * |
| 354 | * Allocation functions * |
| 355 | * * |
| 356 | ************************************************************************/ |
| 357 | |
| 358 | /** |
| 359 | * xmlSchemaNewSchema: |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 360 | * @ctxt: a schema validation context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 361 | * |
| 362 | * Allocate a new Schema structure. |
| 363 | * |
| 364 | * Returns the newly allocated structure or NULL in case or error |
| 365 | */ |
| 366 | static xmlSchemaPtr |
| 367 | xmlSchemaNewSchema(xmlSchemaParserCtxtPtr ctxt) |
| 368 | { |
| 369 | xmlSchemaPtr ret; |
| 370 | |
| 371 | ret = (xmlSchemaPtr) xmlMalloc(sizeof(xmlSchema)); |
| 372 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 373 | xmlSchemaPErrMemory(ctxt, "allocating schema", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 374 | return (NULL); |
| 375 | } |
| 376 | memset(ret, 0, sizeof(xmlSchema)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 377 | xmlDictReference(ctxt->dict); |
| 378 | ret->dict = ctxt->dict; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 379 | |
| 380 | return (ret); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * xmlSchemaNewFacet: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 385 | * |
| 386 | * Allocate a new Facet structure. |
| 387 | * |
| 388 | * Returns the newly allocated structure or NULL in case or error |
| 389 | */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 390 | xmlSchemaFacetPtr |
| 391 | xmlSchemaNewFacet(void) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 392 | { |
| 393 | xmlSchemaFacetPtr ret; |
| 394 | |
| 395 | ret = (xmlSchemaFacetPtr) xmlMalloc(sizeof(xmlSchemaFacet)); |
| 396 | if (ret == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 397 | return (NULL); |
| 398 | } |
| 399 | memset(ret, 0, sizeof(xmlSchemaFacet)); |
| 400 | |
| 401 | return (ret); |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * xmlSchemaNewAnnot: |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 406 | * @ctxt: a schema validation context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 407 | * @node: a node |
| 408 | * |
| 409 | * Allocate a new annotation structure. |
| 410 | * |
| 411 | * Returns the newly allocated structure or NULL in case or error |
| 412 | */ |
| 413 | static xmlSchemaAnnotPtr |
| 414 | xmlSchemaNewAnnot(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node) |
| 415 | { |
| 416 | xmlSchemaAnnotPtr ret; |
| 417 | |
| 418 | ret = (xmlSchemaAnnotPtr) xmlMalloc(sizeof(xmlSchemaAnnot)); |
| 419 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 420 | xmlSchemaPErrMemory(ctxt, "allocating annotation", node); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 421 | return (NULL); |
| 422 | } |
| 423 | memset(ret, 0, sizeof(xmlSchemaAnnot)); |
| 424 | ret->content = node; |
| 425 | return (ret); |
| 426 | } |
| 427 | |
| 428 | /** |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 429 | * xmlSchemaFreeAnnot: |
| 430 | * @annot: a schema type structure |
| 431 | * |
| 432 | * Deallocate a annotation structure |
| 433 | */ |
| 434 | static void |
| 435 | xmlSchemaFreeAnnot(xmlSchemaAnnotPtr annot) |
| 436 | { |
| 437 | if (annot == NULL) |
| 438 | return; |
| 439 | xmlFree(annot); |
| 440 | } |
| 441 | |
| 442 | /** |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 443 | * xmlSchemaFreeImport: |
| 444 | * @import: a schema import structure |
| 445 | * |
| 446 | * Deallocate an import structure |
| 447 | */ |
| 448 | static void |
| 449 | xmlSchemaFreeImport(xmlSchemaImportPtr import) |
| 450 | { |
| 451 | if (import == NULL) |
| 452 | return; |
| 453 | |
| 454 | xmlSchemaFree(import->schema); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 455 | xmlFree(import); |
| 456 | } |
| 457 | |
| 458 | /** |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 459 | * xmlSchemaFreeInclude: |
| 460 | * @include: a schema include structure |
| 461 | * |
| 462 | * Deallocate an include structure |
| 463 | */ |
| 464 | static void |
| 465 | xmlSchemaFreeInclude(xmlSchemaIncludePtr include) |
| 466 | { |
| 467 | if (include == NULL) |
| 468 | return; |
| 469 | |
| 470 | xmlFreeDoc(include->doc); |
| 471 | xmlFree(include); |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * xmlSchemaFreeIncludeList: |
| 476 | * @includes: a schema include list |
| 477 | * |
| 478 | * Deallocate an include structure |
| 479 | */ |
| 480 | static void |
| 481 | xmlSchemaFreeIncludeList(xmlSchemaIncludePtr includes) |
| 482 | { |
| 483 | xmlSchemaIncludePtr next; |
| 484 | |
| 485 | while (includes != NULL) { |
| 486 | next = includes->next; |
| 487 | xmlSchemaFreeInclude(includes); |
| 488 | includes = next; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 493 | * xmlSchemaFreeNotation: |
| 494 | * @schema: a schema notation structure |
| 495 | * |
| 496 | * Deallocate a Schema Notation structure. |
| 497 | */ |
| 498 | static void |
| 499 | xmlSchemaFreeNotation(xmlSchemaNotationPtr nota) |
| 500 | { |
| 501 | if (nota == NULL) |
| 502 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 503 | xmlFree(nota); |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * xmlSchemaFreeAttribute: |
| 508 | * @schema: a schema attribute structure |
| 509 | * |
| 510 | * Deallocate a Schema Attribute structure. |
| 511 | */ |
| 512 | static void |
| 513 | xmlSchemaFreeAttribute(xmlSchemaAttributePtr attr) |
| 514 | { |
| 515 | if (attr == NULL) |
| 516 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 517 | xmlFree(attr); |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * xmlSchemaFreeAttributeGroup: |
| 522 | * @schema: a schema attribute group structure |
| 523 | * |
| 524 | * Deallocate a Schema Attribute Group structure. |
| 525 | */ |
| 526 | static void |
| 527 | xmlSchemaFreeAttributeGroup(xmlSchemaAttributeGroupPtr attr) |
| 528 | { |
| 529 | if (attr == NULL) |
| 530 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 531 | xmlFree(attr); |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * xmlSchemaFreeElement: |
| 536 | * @schema: a schema element structure |
| 537 | * |
| 538 | * Deallocate a Schema Element structure. |
| 539 | */ |
| 540 | static void |
| 541 | xmlSchemaFreeElement(xmlSchemaElementPtr elem) |
| 542 | { |
| 543 | if (elem == NULL) |
| 544 | return; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 545 | if (elem->annot != NULL) |
| 546 | xmlSchemaFreeAnnot(elem->annot); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 547 | if (elem->contModel != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 548 | xmlRegFreeRegexp(elem->contModel); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 549 | xmlFree(elem); |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * xmlSchemaFreeFacet: |
| 554 | * @facet: a schema facet structure |
| 555 | * |
| 556 | * Deallocate a Schema Facet structure. |
| 557 | */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 558 | void |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 559 | xmlSchemaFreeFacet(xmlSchemaFacetPtr facet) |
| 560 | { |
| 561 | if (facet == NULL) |
| 562 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 563 | if (facet->val != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 564 | xmlSchemaFreeValue(facet->val); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 565 | if (facet->regexp != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 566 | xmlRegFreeRegexp(facet->regexp); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 567 | if (facet->annot != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 568 | xmlSchemaFreeAnnot(facet->annot); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 569 | xmlFree(facet); |
| 570 | } |
| 571 | |
| 572 | /** |
| 573 | * xmlSchemaFreeType: |
| 574 | * @type: a schema type structure |
| 575 | * |
| 576 | * Deallocate a Schema Type structure. |
| 577 | */ |
| 578 | void |
| 579 | xmlSchemaFreeType(xmlSchemaTypePtr type) |
| 580 | { |
| 581 | if (type == NULL) |
| 582 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 583 | if (type->annot != NULL) |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 584 | xmlSchemaFreeAnnot(type->annot); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 585 | if (type->facets != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 586 | xmlSchemaFacetPtr facet, next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 587 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 588 | facet = type->facets; |
| 589 | while (facet != NULL) { |
| 590 | next = facet->next; |
| 591 | xmlSchemaFreeFacet(facet); |
| 592 | facet = next; |
| 593 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 594 | } |
| 595 | xmlFree(type); |
| 596 | } |
| 597 | |
| 598 | /** |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 599 | * xmlSchemaFreeTypeList: |
| 600 | * @type: a schema type structure |
| 601 | * |
| 602 | * Deallocate a Schema Type structure. |
| 603 | */ |
| 604 | static void |
| 605 | xmlSchemaFreeTypeList(xmlSchemaTypePtr type) |
| 606 | { |
| 607 | xmlSchemaTypePtr next; |
| 608 | |
| 609 | while (type != NULL) { |
| 610 | next = type->redef; |
| 611 | xmlSchemaFreeType(type); |
| 612 | type = next; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 617 | * xmlSchemaFree: |
| 618 | * @schema: a schema structure |
| 619 | * |
| 620 | * Deallocate a Schema structure. |
| 621 | */ |
| 622 | void |
| 623 | xmlSchemaFree(xmlSchemaPtr schema) |
| 624 | { |
| 625 | if (schema == NULL) |
| 626 | return; |
| 627 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 628 | if (schema->notaDecl != NULL) |
| 629 | xmlHashFree(schema->notaDecl, |
| 630 | (xmlHashDeallocator) xmlSchemaFreeNotation); |
| 631 | if (schema->attrDecl != NULL) |
| 632 | xmlHashFree(schema->attrDecl, |
| 633 | (xmlHashDeallocator) xmlSchemaFreeAttribute); |
| 634 | if (schema->attrgrpDecl != NULL) |
| 635 | xmlHashFree(schema->attrgrpDecl, |
| 636 | (xmlHashDeallocator) xmlSchemaFreeAttributeGroup); |
| 637 | if (schema->elemDecl != NULL) |
| 638 | xmlHashFree(schema->elemDecl, |
| 639 | (xmlHashDeallocator) xmlSchemaFreeElement); |
| 640 | if (schema->typeDecl != NULL) |
| 641 | xmlHashFree(schema->typeDecl, |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 642 | (xmlHashDeallocator) xmlSchemaFreeTypeList); |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 643 | if (schema->groupDecl != NULL) |
| 644 | xmlHashFree(schema->groupDecl, |
| 645 | (xmlHashDeallocator) xmlSchemaFreeType); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 646 | if (schema->schemasImports != NULL) |
| 647 | xmlHashFree(schema->schemasImports, |
| 648 | (xmlHashDeallocator) xmlSchemaFreeImport); |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 649 | if (schema->includes != NULL) { |
| 650 | xmlSchemaFreeIncludeList((xmlSchemaIncludePtr) schema->includes); |
| 651 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 652 | if (schema->annot != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 653 | xmlSchemaFreeAnnot(schema->annot); |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 654 | if (schema->doc != NULL && !schema->preserve) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 655 | xmlFreeDoc(schema->doc); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 656 | xmlDictFree(schema->dict); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 657 | |
| 658 | xmlFree(schema); |
| 659 | } |
| 660 | |
| 661 | /************************************************************************ |
| 662 | * * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 663 | * Debug functions * |
| 664 | * * |
| 665 | ************************************************************************/ |
| 666 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 667 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 668 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 669 | /** |
| 670 | * xmlSchemaElementDump: |
| 671 | * @elem: an element |
| 672 | * @output: the file output |
| 673 | * |
| 674 | * Dump the element |
| 675 | */ |
| 676 | static void |
| 677 | xmlSchemaElementDump(xmlSchemaElementPtr elem, FILE * output, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 678 | const xmlChar * name ATTRIBUTE_UNUSED, |
| 679 | const xmlChar * context ATTRIBUTE_UNUSED, |
| 680 | const xmlChar * namespace ATTRIBUTE_UNUSED) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 681 | { |
| 682 | if (elem == NULL) |
| 683 | return; |
| 684 | |
| 685 | fprintf(output, "Element "); |
| 686 | if (elem->flags & XML_SCHEMAS_ELEM_TOPLEVEL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 687 | fprintf(output, "toplevel "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 688 | fprintf(output, ": %s ", elem->name); |
| 689 | if (namespace != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 690 | fprintf(output, "namespace '%s' ", namespace); |
| 691 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 692 | if (elem->flags & XML_SCHEMAS_ELEM_NILLABLE) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 693 | fprintf(output, "nillable "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 694 | if (elem->flags & XML_SCHEMAS_ELEM_GLOBAL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 695 | fprintf(output, "global "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 696 | if (elem->flags & XML_SCHEMAS_ELEM_DEFAULT) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 697 | fprintf(output, "default "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 698 | if (elem->flags & XML_SCHEMAS_ELEM_FIXED) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 699 | fprintf(output, "fixed "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 700 | if (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 701 | fprintf(output, "abstract "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 702 | if (elem->flags & XML_SCHEMAS_ELEM_REF) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 703 | fprintf(output, "ref '%s' ", elem->ref); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 704 | if (elem->id != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 705 | fprintf(output, "id '%s' ", elem->id); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 706 | fprintf(output, "\n"); |
| 707 | if ((elem->minOccurs != 1) || (elem->maxOccurs != 1)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 708 | fprintf(output, " "); |
| 709 | if (elem->minOccurs != 1) |
| 710 | fprintf(output, "min: %d ", elem->minOccurs); |
| 711 | if (elem->maxOccurs >= UNBOUNDED) |
| 712 | fprintf(output, "max: unbounded\n"); |
| 713 | else if (elem->maxOccurs != 1) |
| 714 | fprintf(output, "max: %d\n", elem->maxOccurs); |
| 715 | else |
| 716 | fprintf(output, "\n"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 717 | } |
| 718 | if (elem->namedType != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 719 | fprintf(output, " type: %s", elem->namedType); |
| 720 | if (elem->namedTypeNs != NULL) |
| 721 | fprintf(output, " ns %s\n", elem->namedTypeNs); |
| 722 | else |
| 723 | fprintf(output, "\n"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 724 | } |
| 725 | if (elem->substGroup != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 726 | fprintf(output, " substitutionGroup: %s", elem->substGroup); |
| 727 | if (elem->substGroupNs != NULL) |
| 728 | fprintf(output, " ns %s\n", elem->substGroupNs); |
| 729 | else |
| 730 | fprintf(output, "\n"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 731 | } |
| 732 | if (elem->value != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 733 | fprintf(output, " default: %s", elem->value); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | /** |
| 737 | * xmlSchemaAnnotDump: |
| 738 | * @output: the file output |
| 739 | * @annot: a annotation |
| 740 | * |
| 741 | * Dump the annotation |
| 742 | */ |
| 743 | static void |
| 744 | xmlSchemaAnnotDump(FILE * output, xmlSchemaAnnotPtr annot) |
| 745 | { |
| 746 | xmlChar *content; |
| 747 | |
| 748 | if (annot == NULL) |
| 749 | return; |
| 750 | |
| 751 | content = xmlNodeGetContent(annot->content); |
| 752 | if (content != NULL) { |
| 753 | fprintf(output, " Annot: %s\n", content); |
| 754 | xmlFree(content); |
| 755 | } else |
| 756 | fprintf(output, " Annot: empty\n"); |
| 757 | } |
| 758 | |
| 759 | /** |
| 760 | * xmlSchemaTypeDump: |
| 761 | * @output: the file output |
| 762 | * @type: a type structure |
| 763 | * |
| 764 | * Dump a SchemaType structure |
| 765 | */ |
| 766 | static void |
| 767 | xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output) |
| 768 | { |
| 769 | if (type == NULL) { |
| 770 | fprintf(output, "Type: NULL\n"); |
| 771 | return; |
| 772 | } |
| 773 | fprintf(output, "Type: "); |
| 774 | if (type->name != NULL) |
| 775 | fprintf(output, "%s, ", type->name); |
| 776 | else |
| 777 | fprintf(output, "no name"); |
| 778 | switch (type->type) { |
| 779 | case XML_SCHEMA_TYPE_BASIC: |
| 780 | fprintf(output, "basic "); |
| 781 | break; |
| 782 | case XML_SCHEMA_TYPE_SIMPLE: |
| 783 | fprintf(output, "simple "); |
| 784 | break; |
| 785 | case XML_SCHEMA_TYPE_COMPLEX: |
| 786 | fprintf(output, "complex "); |
| 787 | break; |
| 788 | case XML_SCHEMA_TYPE_SEQUENCE: |
| 789 | fprintf(output, "sequence "); |
| 790 | break; |
| 791 | case XML_SCHEMA_TYPE_CHOICE: |
| 792 | fprintf(output, "choice "); |
| 793 | break; |
| 794 | case XML_SCHEMA_TYPE_ALL: |
| 795 | fprintf(output, "all "); |
| 796 | break; |
| 797 | case XML_SCHEMA_TYPE_UR: |
| 798 | fprintf(output, "ur "); |
| 799 | break; |
| 800 | case XML_SCHEMA_TYPE_RESTRICTION: |
| 801 | fprintf(output, "restriction "); |
| 802 | break; |
| 803 | case XML_SCHEMA_TYPE_EXTENSION: |
| 804 | fprintf(output, "extension "); |
| 805 | break; |
| 806 | default: |
| 807 | fprintf(output, "unknowntype%d ", type->type); |
| 808 | break; |
| 809 | } |
| 810 | if (type->base != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 811 | fprintf(output, "base %s, ", type->base); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 812 | } |
| 813 | switch (type->contentType) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 814 | case XML_SCHEMA_CONTENT_UNKNOWN: |
| 815 | fprintf(output, "unknown "); |
| 816 | break; |
| 817 | case XML_SCHEMA_CONTENT_EMPTY: |
| 818 | fprintf(output, "empty "); |
| 819 | break; |
| 820 | case XML_SCHEMA_CONTENT_ELEMENTS: |
| 821 | fprintf(output, "element "); |
| 822 | break; |
| 823 | case XML_SCHEMA_CONTENT_MIXED: |
| 824 | fprintf(output, "mixed "); |
| 825 | break; |
| 826 | case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS: |
| 827 | fprintf(output, "mixed_or_elems "); |
| 828 | break; |
| 829 | case XML_SCHEMA_CONTENT_BASIC: |
| 830 | fprintf(output, "basic "); |
| 831 | break; |
| 832 | case XML_SCHEMA_CONTENT_SIMPLE: |
| 833 | fprintf(output, "simple "); |
| 834 | break; |
| 835 | case XML_SCHEMA_CONTENT_ANY: |
| 836 | fprintf(output, "any "); |
| 837 | break; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 838 | } |
| 839 | fprintf(output, "\n"); |
| 840 | if ((type->minOccurs != 1) || (type->maxOccurs != 1)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 841 | fprintf(output, " "); |
| 842 | if (type->minOccurs != 1) |
| 843 | fprintf(output, "min: %d ", type->minOccurs); |
| 844 | if (type->maxOccurs >= UNBOUNDED) |
| 845 | fprintf(output, "max: unbounded\n"); |
| 846 | else if (type->maxOccurs != 1) |
| 847 | fprintf(output, "max: %d\n", type->maxOccurs); |
| 848 | else |
| 849 | fprintf(output, "\n"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 850 | } |
| 851 | if (type->annot != NULL) |
| 852 | xmlSchemaAnnotDump(output, type->annot); |
| 853 | if (type->subtypes != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 854 | xmlSchemaTypePtr sub = type->subtypes; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 855 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 856 | fprintf(output, " subtypes: "); |
| 857 | while (sub != NULL) { |
| 858 | fprintf(output, "%s ", sub->name); |
| 859 | sub = sub->next; |
| 860 | } |
| 861 | fprintf(output, "\n"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | } |
| 865 | |
| 866 | /** |
| 867 | * xmlSchemaDump: |
| 868 | * @output: the file output |
| 869 | * @schema: a schema structure |
| 870 | * |
| 871 | * Dump a Schema structure. |
| 872 | */ |
| 873 | void |
| 874 | xmlSchemaDump(FILE * output, xmlSchemaPtr schema) |
| 875 | { |
| 876 | if (schema == NULL) { |
| 877 | fprintf(output, "Schemas: NULL\n"); |
| 878 | return; |
| 879 | } |
| 880 | fprintf(output, "Schemas: "); |
| 881 | if (schema->name != NULL) |
| 882 | fprintf(output, "%s, ", schema->name); |
| 883 | else |
| 884 | fprintf(output, "no name, "); |
| 885 | if (schema->targetNamespace != NULL) |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 886 | fprintf(output, "%s", (const char *) schema->targetNamespace); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 887 | else |
| 888 | fprintf(output, "no target namespace"); |
| 889 | fprintf(output, "\n"); |
| 890 | if (schema->annot != NULL) |
| 891 | xmlSchemaAnnotDump(output, schema->annot); |
| 892 | |
| 893 | xmlHashScan(schema->typeDecl, (xmlHashScanner) xmlSchemaTypeDump, |
| 894 | output); |
| 895 | xmlHashScanFull(schema->elemDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 896 | (xmlHashScannerFull) xmlSchemaElementDump, output); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 897 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 898 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 899 | |
| 900 | /************************************************************************ |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 901 | * * |
| 902 | * Utilities * |
| 903 | * * |
| 904 | ************************************************************************/ |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 905 | |
| 906 | /** |
| 907 | * xmlSchemaGetProp: |
| 908 | * @ctxt: the parser context |
| 909 | * @node: the node |
| 910 | * @name: the property name |
| 911 | * |
| 912 | * Read a attribute value and internalize the string |
| 913 | * |
| 914 | * Returns the string or NULL if not present. |
| 915 | */ |
| 916 | static const xmlChar * |
| 917 | xmlSchemaGetProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, |
| 918 | const char *name) |
| 919 | { |
| 920 | xmlChar *val; |
| 921 | const xmlChar *ret; |
| 922 | |
| 923 | val = xmlGetProp(node, BAD_CAST name); |
| 924 | if (val == NULL) |
| 925 | return(NULL); |
| 926 | ret = xmlDictLookup(ctxt->dict, val, -1); |
| 927 | xmlFree(val); |
| 928 | return(ret); |
| 929 | } |
| 930 | |
| 931 | /** |
| 932 | * xmlSchemaGetNamespace: |
| 933 | * @ctxt: the parser context |
| 934 | * @schema: the schemas containing the declaration |
| 935 | * @node: the node |
| 936 | * @qname: the QName to analyze |
| 937 | * |
| 938 | * Find the namespace name for the given declaration. |
| 939 | * |
| 940 | * Returns the local name for that declaration, as well as the namespace name |
| 941 | */ |
| 942 | static const xmlChar * |
| 943 | xmlSchemaGetNamespace(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 944 | xmlNodePtr node, const xmlChar *qname, |
| 945 | const xmlChar **namespace) { |
| 946 | int len; |
| 947 | const xmlChar *name, *prefix, *def = NULL; |
| 948 | xmlNsPtr ns; |
| 949 | |
| 950 | *namespace = NULL; |
| 951 | |
| 952 | if (xmlStrEqual(node->name, BAD_CAST "element") || |
| 953 | xmlStrEqual(node->name, BAD_CAST "attribute") || |
| 954 | xmlStrEqual(node->name, BAD_CAST "simpleType") || |
| 955 | xmlStrEqual(node->name, BAD_CAST "complexType")) { |
| 956 | def = xmlSchemaGetProp(ctxt, node, "targetNamespace"); |
| 957 | } |
| 958 | |
| 959 | qname = xmlDictLookup(ctxt->dict, qname, -1); /* intern the string */ |
| 960 | name = xmlSplitQName3(qname, &len); |
| 961 | if (name == NULL) { |
| 962 | if (def == NULL) { |
| 963 | if (xmlStrEqual(node->name, BAD_CAST "element")) { |
| 964 | if (schema->flags & XML_SCHEMAS_QUALIF_ELEM) |
| 965 | *namespace = schema->targetNamespace; |
| 966 | } else if (xmlStrEqual(node->name, BAD_CAST "attribute")) { |
| 967 | if (schema->flags & XML_SCHEMAS_QUALIF_ATTR) |
| 968 | *namespace = schema->targetNamespace; |
| 969 | } else if ((xmlStrEqual(node->name, BAD_CAST "simpleType")) || |
| 970 | (xmlStrEqual(node->name, BAD_CAST "complexType"))) { |
| 971 | *namespace = schema->targetNamespace; |
| 972 | } |
| 973 | } else { |
| 974 | *namespace = def; |
| 975 | } |
| 976 | return(qname); |
| 977 | } |
| 978 | name = xmlDictLookup(ctxt->dict, name, -1); |
| 979 | prefix = xmlDictLookup(ctxt->dict, qname, len); |
| 980 | if (def != NULL) { |
| 981 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_DEF_AND_PREFIX, |
| 982 | "%s: presence of both prefix %s and targetNamespace\n", |
| 983 | node->name, prefix); |
| 984 | } |
| 985 | ns = xmlSearchNs(node->doc, node, prefix); |
| 986 | if (ns == NULL) { |
| 987 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_PREFIX_UNDEFINED, |
| 988 | "%s: the QName prefix %s is undefined\n", |
| 989 | node->name, prefix); |
| 990 | return(name); |
| 991 | } |
| 992 | *namespace = xmlDictLookup(ctxt->dict, ns->href, -1); |
| 993 | return(name); |
| 994 | } |
| 995 | |
| 996 | /************************************************************************ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 997 | * * |
| 998 | * Parsing functions * |
| 999 | * * |
| 1000 | ************************************************************************/ |
| 1001 | |
| 1002 | /** |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1003 | * xmlSchemaGetElem: |
| 1004 | * @schema: the schemas context |
| 1005 | * @name: the element name |
| 1006 | * @ns: the element namespace |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1007 | * @level: how deep is the request |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1008 | * |
| 1009 | * Lookup a an element in the schemas or the accessible schemas |
| 1010 | * |
| 1011 | * Returns the element definition or NULL if not found. |
| 1012 | */ |
| 1013 | static xmlSchemaElementPtr |
| 1014 | xmlSchemaGetElem(xmlSchemaPtr schema, const xmlChar * name, |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1015 | const xmlChar * namespace, int level) |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1016 | { |
| 1017 | xmlSchemaElementPtr ret; |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1018 | xmlSchemaImportPtr import = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1019 | |
| 1020 | if ((name == NULL) || (schema == NULL)) |
| 1021 | return (NULL); |
| 1022 | |
| 1023 | if (namespace == NULL) { |
| 1024 | ret = xmlHashLookup2(schema->elemDecl, name, namespace); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1025 | if ((ret != NULL) && |
| 1026 | ((level == 0) || (ret->flags & XML_SCHEMAS_ELEM_TOPLEVEL))) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1027 | return (ret); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1028 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1029 | } else if ((schema->flags & XML_SCHEMAS_QUALIF_ELEM) == 0) { |
| 1030 | if (xmlStrEqual(namespace, schema->targetNamespace)) |
| 1031 | ret = xmlHashLookup2(schema->elemDecl, name, NULL); |
| 1032 | else |
| 1033 | ret = xmlHashLookup2(schema->elemDecl, name, namespace); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1034 | if ((ret != NULL) && |
| 1035 | ((level == 0) || (ret->flags & XML_SCHEMAS_ELEM_TOPLEVEL))) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1036 | return (ret); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1037 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1038 | } else { |
| 1039 | ret = xmlHashLookup2(schema->elemDecl, name, namespace); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1040 | if ((ret != NULL) && |
| 1041 | ((level == 0) || (ret->flags & XML_SCHEMAS_ELEM_TOPLEVEL))) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1042 | return (ret); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1043 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1044 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1045 | if (level > 0) |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1046 | import = xmlHashLookup(schema->schemasImports, namespace); |
| 1047 | if (import != NULL) |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1048 | ret = xmlSchemaGetElem(import->schema, name, namespace, level + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1049 | #ifdef DEBUG |
| 1050 | if (ret == NULL) { |
| 1051 | if (namespace == NULL) |
| 1052 | fprintf(stderr, "Unable to lookup type %s", name); |
| 1053 | else |
| 1054 | fprintf(stderr, "Unable to lookup type %s:%s", name, |
| 1055 | namespace); |
| 1056 | } |
| 1057 | #endif |
| 1058 | return (ret); |
| 1059 | } |
| 1060 | |
| 1061 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1062 | * xmlSchemaGetType: |
| 1063 | * @schema: the schemas context |
| 1064 | * @name: the type name |
| 1065 | * @ns: the type namespace |
| 1066 | * |
| 1067 | * Lookup a type in the schemas or the predefined types |
| 1068 | * |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 1069 | * Returns the group definition or NULL if not found. |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1070 | */ |
| 1071 | static xmlSchemaTypePtr |
| 1072 | xmlSchemaGetType(xmlSchemaPtr schema, const xmlChar * name, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1073 | const xmlChar * namespace) |
| 1074 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1075 | xmlSchemaTypePtr ret; |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 1076 | xmlSchemaImportPtr import; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1077 | |
| 1078 | if (name == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1079 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1080 | if (schema != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1081 | ret = xmlHashLookup2(schema->typeDecl, name, namespace); |
| 1082 | if (ret != NULL) |
| 1083 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1084 | } |
| 1085 | ret = xmlSchemaGetPredefinedType(name, namespace); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 1086 | if (ret != NULL) |
| 1087 | return (ret); |
| 1088 | import = xmlHashLookup(schema->schemasImports, namespace); |
| 1089 | if (import != NULL) |
| 1090 | ret = xmlSchemaGetType(import->schema, name, namespace); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1091 | #ifdef DEBUG |
| 1092 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1093 | if (namespace == NULL) |
| 1094 | fprintf(stderr, "Unable to lookup type %s", name); |
| 1095 | else |
| 1096 | fprintf(stderr, "Unable to lookup type %s:%s", name, |
| 1097 | namespace); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1098 | } |
| 1099 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1100 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | /************************************************************************ |
| 1104 | * * |
| 1105 | * Parsing functions * |
| 1106 | * * |
| 1107 | ************************************************************************/ |
| 1108 | |
| 1109 | #define IS_BLANK_NODE(n) \ |
| 1110 | (((n)->type == XML_TEXT_NODE) && (xmlSchemaIsBlank((n)->content))) |
| 1111 | |
| 1112 | /** |
| 1113 | * xmlSchemaIsBlank: |
| 1114 | * @str: a string |
| 1115 | * |
| 1116 | * Check if a string is ignorable |
| 1117 | * |
| 1118 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 1119 | */ |
| 1120 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1121 | xmlSchemaIsBlank(xmlChar * str) |
| 1122 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1123 | if (str == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1124 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1125 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1126 | if (!(IS_BLANK_CH(*str))) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1127 | return (0); |
| 1128 | str++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1129 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1130 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | /** |
| 1134 | * xmlSchemaAddNotation: |
| 1135 | * @ctxt: a schema validation context |
| 1136 | * @schema: the schema being built |
| 1137 | * @name: the item name |
| 1138 | * |
| 1139 | * Add an XML schema Attrribute declaration |
| 1140 | * *WARNING* this interface is highly subject to change |
| 1141 | * |
| 1142 | * Returns the new struture or NULL in case of error |
| 1143 | */ |
| 1144 | static xmlSchemaNotationPtr |
| 1145 | xmlSchemaAddNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1146 | const xmlChar * name) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1147 | { |
| 1148 | xmlSchemaNotationPtr ret = NULL; |
| 1149 | int val; |
| 1150 | |
| 1151 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1152 | return (NULL); |
| 1153 | |
| 1154 | if (schema->notaDecl == NULL) |
| 1155 | schema->notaDecl = xmlHashCreate(10); |
| 1156 | if (schema->notaDecl == NULL) |
| 1157 | return (NULL); |
| 1158 | |
| 1159 | ret = (xmlSchemaNotationPtr) xmlMalloc(sizeof(xmlSchemaNotation)); |
| 1160 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1161 | xmlSchemaPErrMemory(ctxt, "add annotation", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1162 | return (NULL); |
| 1163 | } |
| 1164 | memset(ret, 0, sizeof(xmlSchemaNotation)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1165 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1166 | val = xmlHashAddEntry2(schema->notaDecl, name, schema->targetNamespace, |
| 1167 | ret); |
| 1168 | if (val != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1169 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1170 | XML_SCHEMAP_REDEFINED_NOTATION, |
| 1171 | "Notation %s already defined\n", |
| 1172 | name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1173 | xmlFree(ret); |
| 1174 | return (NULL); |
| 1175 | } |
| 1176 | return (ret); |
| 1177 | } |
| 1178 | |
| 1179 | |
| 1180 | /** |
| 1181 | * xmlSchemaAddAttribute: |
| 1182 | * @ctxt: a schema validation context |
| 1183 | * @schema: the schema being built |
| 1184 | * @name: the item name |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1185 | * @namespace: the namespace |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1186 | * |
| 1187 | * Add an XML schema Attrribute declaration |
| 1188 | * *WARNING* this interface is highly subject to change |
| 1189 | * |
| 1190 | * Returns the new struture or NULL in case of error |
| 1191 | */ |
| 1192 | static xmlSchemaAttributePtr |
| 1193 | xmlSchemaAddAttribute(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1194 | const xmlChar * name, const xmlChar * namespace) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1195 | { |
| 1196 | xmlSchemaAttributePtr ret = NULL; |
| 1197 | int val; |
| 1198 | |
| 1199 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1200 | return (NULL); |
| 1201 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1202 | #ifdef DEBUG |
| 1203 | fprintf(stderr, "Adding attribute %s\n", name); |
| 1204 | if (namespace != NULL) |
| 1205 | fprintf(stderr, " target namespace %s\n", namespace); |
| 1206 | #endif |
| 1207 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1208 | if (schema->attrDecl == NULL) |
| 1209 | schema->attrDecl = xmlHashCreate(10); |
| 1210 | if (schema->attrDecl == NULL) |
| 1211 | return (NULL); |
| 1212 | |
| 1213 | ret = (xmlSchemaAttributePtr) xmlMalloc(sizeof(xmlSchemaAttribute)); |
| 1214 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1215 | xmlSchemaPErrMemory(ctxt, "allocating attribute", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1216 | return (NULL); |
| 1217 | } |
| 1218 | memset(ret, 0, sizeof(xmlSchemaAttribute)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1219 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
| 1220 | ret->targetNamespace = xmlDictLookup(ctxt->dict, namespace, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1221 | val = xmlHashAddEntry3(schema->attrDecl, name, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1222 | schema->targetNamespace, ctxt->container, ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1223 | if (val != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1224 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1225 | XML_SCHEMAP_REDEFINED_ATTR, |
| 1226 | "Attribute %s already defined\n", |
| 1227 | name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1228 | xmlFree(ret); |
| 1229 | return (NULL); |
| 1230 | } |
| 1231 | return (ret); |
| 1232 | } |
| 1233 | |
| 1234 | /** |
| 1235 | * xmlSchemaAddAttributeGroup: |
| 1236 | * @ctxt: a schema validation context |
| 1237 | * @schema: the schema being built |
| 1238 | * @name: the item name |
| 1239 | * |
| 1240 | * Add an XML schema Attrribute Group declaration |
| 1241 | * |
| 1242 | * Returns the new struture or NULL in case of error |
| 1243 | */ |
| 1244 | static xmlSchemaAttributeGroupPtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1245 | xmlSchemaAddAttributeGroup(xmlSchemaParserCtxtPtr ctxt, |
| 1246 | xmlSchemaPtr schema, const xmlChar * name) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1247 | { |
| 1248 | xmlSchemaAttributeGroupPtr ret = NULL; |
| 1249 | int val; |
| 1250 | |
| 1251 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1252 | return (NULL); |
| 1253 | |
| 1254 | if (schema->attrgrpDecl == NULL) |
| 1255 | schema->attrgrpDecl = xmlHashCreate(10); |
| 1256 | if (schema->attrgrpDecl == NULL) |
| 1257 | return (NULL); |
| 1258 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1259 | ret = |
| 1260 | (xmlSchemaAttributeGroupPtr) |
| 1261 | xmlMalloc(sizeof(xmlSchemaAttributeGroup)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1262 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1263 | xmlSchemaPErrMemory(ctxt, "allocating attribute group", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1264 | return (NULL); |
| 1265 | } |
| 1266 | memset(ret, 0, sizeof(xmlSchemaAttributeGroup)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1267 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1268 | val = xmlHashAddEntry3(schema->attrgrpDecl, name, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1269 | schema->targetNamespace, ctxt->container, ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1270 | if (val != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1271 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1272 | XML_SCHEMAP_REDEFINED_ATTRGROUP, |
| 1273 | "Attribute group %s already defined\n", |
| 1274 | name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1275 | xmlFree(ret); |
| 1276 | return (NULL); |
| 1277 | } |
| 1278 | return (ret); |
| 1279 | } |
| 1280 | |
| 1281 | /** |
| 1282 | * xmlSchemaAddElement: |
| 1283 | * @ctxt: a schema validation context |
| 1284 | * @schema: the schema being built |
| 1285 | * @name: the type name |
| 1286 | * @namespace: the type namespace |
| 1287 | * |
| 1288 | * Add an XML schema Element declaration |
| 1289 | * *WARNING* this interface is highly subject to change |
| 1290 | * |
| 1291 | * Returns the new struture or NULL in case of error |
| 1292 | */ |
| 1293 | static xmlSchemaElementPtr |
| 1294 | xmlSchemaAddElement(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 1295 | const xmlChar * name, const xmlChar * namespace) |
| 1296 | { |
| 1297 | xmlSchemaElementPtr ret = NULL; |
| 1298 | int val; |
| 1299 | |
| 1300 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1301 | return (NULL); |
| 1302 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1303 | #ifdef DEBUG |
| 1304 | fprintf(stderr, "Adding element %s\n", name); |
| 1305 | if (namespace != NULL) |
| 1306 | fprintf(stderr, " target namespace %s\n", namespace); |
| 1307 | #endif |
| 1308 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1309 | if (schema->elemDecl == NULL) |
| 1310 | schema->elemDecl = xmlHashCreate(10); |
| 1311 | if (schema->elemDecl == NULL) |
| 1312 | return (NULL); |
| 1313 | |
| 1314 | ret = (xmlSchemaElementPtr) xmlMalloc(sizeof(xmlSchemaElement)); |
| 1315 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1316 | xmlSchemaPErrMemory(ctxt, "allocating element", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1317 | return (NULL); |
| 1318 | } |
| 1319 | memset(ret, 0, sizeof(xmlSchemaElement)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1320 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
| 1321 | ret->targetNamespace = xmlDictLookup(ctxt->dict, namespace, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1322 | val = xmlHashAddEntry3(schema->elemDecl, name, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1323 | namespace, ctxt->container, ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1324 | if (val != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1325 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1326 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1327 | snprintf(buf, 99, "privatieelem %d", ctxt->counter++ + 1); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1328 | val = xmlHashAddEntry3(schema->elemDecl, name, (xmlChar *) buf, |
| 1329 | namespace, ret); |
| 1330 | if (val != 0) { |
| 1331 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1332 | XML_SCHEMAP_REDEFINED_ELEMENT, |
| 1333 | "Element %s already defined\n", |
| 1334 | name, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1335 | xmlFree(ret); |
| 1336 | return (NULL); |
| 1337 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1338 | } |
| 1339 | return (ret); |
| 1340 | } |
| 1341 | |
| 1342 | /** |
| 1343 | * xmlSchemaAddType: |
| 1344 | * @ctxt: a schema validation context |
| 1345 | * @schema: the schema being built |
| 1346 | * @name: the item name |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1347 | * @namespace: the namespace |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1348 | * |
| 1349 | * Add an XML schema Simple Type definition |
| 1350 | * *WARNING* this interface is highly subject to change |
| 1351 | * |
| 1352 | * Returns the new struture or NULL in case of error |
| 1353 | */ |
| 1354 | static xmlSchemaTypePtr |
| 1355 | xmlSchemaAddType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1356 | const xmlChar * name, const xmlChar * namespace) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1357 | { |
| 1358 | xmlSchemaTypePtr ret = NULL; |
| 1359 | int val; |
| 1360 | |
| 1361 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1362 | return (NULL); |
| 1363 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1364 | #ifdef DEBUG |
| 1365 | fprintf(stderr, "Adding type %s\n", name); |
| 1366 | if (namespace != NULL) |
| 1367 | fprintf(stderr, " target namespace %s\n", namespace); |
| 1368 | #endif |
| 1369 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1370 | if (schema->typeDecl == NULL) |
| 1371 | schema->typeDecl = xmlHashCreate(10); |
| 1372 | if (schema->typeDecl == NULL) |
| 1373 | return (NULL); |
| 1374 | |
| 1375 | ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType)); |
| 1376 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1377 | xmlSchemaPErrMemory(ctxt, "allocating type", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1378 | return (NULL); |
| 1379 | } |
| 1380 | memset(ret, 0, sizeof(xmlSchemaType)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1381 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 1382 | ret->redef = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1383 | val = xmlHashAddEntry2(schema->typeDecl, name, namespace, ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1384 | if (val != 0) { |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 1385 | if (ctxt->includes == 0) { |
| 1386 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1387 | XML_SCHEMAP_REDEFINED_TYPE, |
| 1388 | "Type %s already defined\n", |
| 1389 | name, NULL); |
| 1390 | xmlFree(ret); |
| 1391 | return (NULL); |
| 1392 | } else { |
| 1393 | xmlSchemaTypePtr prev; |
| 1394 | |
| 1395 | prev = xmlHashLookup2(schema->typeDecl, name, namespace); |
| 1396 | if (prev == NULL) { |
| 1397 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1398 | XML_ERR_INTERNAL_ERROR, |
| 1399 | "Internal error on type %s definition\n", |
| 1400 | name, NULL); |
| 1401 | xmlFree(ret); |
| 1402 | return (NULL); |
| 1403 | } |
| 1404 | ret->redef = prev->redef; |
| 1405 | prev->redef = ret; |
| 1406 | } |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 1407 | } |
| 1408 | ret->minOccurs = 1; |
| 1409 | ret->maxOccurs = 1; |
| 1410 | |
| 1411 | return (ret); |
| 1412 | } |
| 1413 | |
| 1414 | /** |
| 1415 | * xmlSchemaAddGroup: |
| 1416 | * @ctxt: a schema validation context |
| 1417 | * @schema: the schema being built |
| 1418 | * @name: the group name |
| 1419 | * |
| 1420 | * Add an XML schema Group definition |
| 1421 | * |
| 1422 | * Returns the new struture or NULL in case of error |
| 1423 | */ |
| 1424 | static xmlSchemaTypePtr |
| 1425 | xmlSchemaAddGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1426 | const xmlChar * name) |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 1427 | { |
| 1428 | xmlSchemaTypePtr ret = NULL; |
| 1429 | int val; |
| 1430 | |
| 1431 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1432 | return (NULL); |
| 1433 | |
| 1434 | if (schema->groupDecl == NULL) |
| 1435 | schema->groupDecl = xmlHashCreate(10); |
| 1436 | if (schema->groupDecl == NULL) |
| 1437 | return (NULL); |
| 1438 | |
| 1439 | ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType)); |
| 1440 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1441 | xmlSchemaPErrMemory(ctxt, "adding group", NULL); |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 1442 | return (NULL); |
| 1443 | } |
| 1444 | memset(ret, 0, sizeof(xmlSchemaType)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1445 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1446 | val = |
| 1447 | xmlHashAddEntry2(schema->groupDecl, name, schema->targetNamespace, |
| 1448 | ret); |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 1449 | if (val != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1450 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1451 | XML_SCHEMAP_REDEFINED_GROUP, |
| 1452 | "Group %s already defined\n", |
| 1453 | name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1454 | xmlFree(ret); |
| 1455 | return (NULL); |
| 1456 | } |
| 1457 | ret->minOccurs = 1; |
| 1458 | ret->maxOccurs = 1; |
| 1459 | |
| 1460 | return (ret); |
| 1461 | } |
| 1462 | |
| 1463 | /************************************************************************ |
| 1464 | * * |
| 1465 | * Utilities for parsing * |
| 1466 | * * |
| 1467 | ************************************************************************/ |
| 1468 | |
| 1469 | /** |
| 1470 | * xmlGetQNameProp: |
| 1471 | * @ctxt: a schema validation context |
| 1472 | * @node: a subtree containing XML Schema informations |
| 1473 | * @name: the attribute name |
| 1474 | * @namespace: the result namespace if any |
| 1475 | * |
| 1476 | * Extract a QName Attribute value |
| 1477 | * |
| 1478 | * Returns the NCName or NULL if not found, and also update @namespace |
| 1479 | * with the namespace URI |
| 1480 | */ |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1481 | static const xmlChar * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1482 | xmlGetQNameProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1483 | const char *name, const xmlChar ** namespace) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1484 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1485 | const xmlChar *val; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1486 | xmlNsPtr ns; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1487 | const xmlChar *ret, *prefix; |
| 1488 | int len; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1489 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1490 | *namespace = NULL; |
| 1491 | val = xmlSchemaGetProp(ctxt, node, name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1492 | if (val == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1493 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1494 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1495 | ret = xmlSplitQName3(val, &len); |
| 1496 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1497 | return (val); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1498 | } |
| 1499 | ret = xmlDictLookup(ctxt->dict, ret, -1); |
| 1500 | prefix = xmlDictLookup(ctxt->dict, val, len); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1501 | |
| 1502 | ns = xmlSearchNs(node->doc, node, prefix); |
| 1503 | if (ns == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1504 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_PREFIX_UNDEFINED, |
| 1505 | "Attribute %s: the QName prefix %s is undefined\n", |
| 1506 | (const xmlChar *) name, prefix); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1507 | } else { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1508 | *namespace = xmlDictLookup(ctxt->dict, ns->href, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1509 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1510 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | /** |
| 1514 | * xmlGetMaxOccurs: |
| 1515 | * @ctxt: a schema validation context |
| 1516 | * @node: a subtree containing XML Schema informations |
| 1517 | * |
| 1518 | * Get the maxOccurs property |
| 1519 | * |
| 1520 | * Returns the default if not found, or the value |
| 1521 | */ |
| 1522 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1523 | xmlGetMaxOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node) |
| 1524 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1525 | const xmlChar *val, *cur; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1526 | int ret = 0; |
| 1527 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1528 | val = xmlSchemaGetProp(ctxt, node, "maxOccurs"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1529 | if (val == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1530 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1531 | |
| 1532 | if (xmlStrEqual(val, (const xmlChar *) "unbounded")) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1533 | return (UNBOUNDED); /* encoding it with -1 might be another option */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | cur = val; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1537 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1538 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1539 | while ((*cur >= '0') && (*cur <= '9')) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1540 | ret = ret * 10 + (*cur - '0'); |
| 1541 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1542 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1543 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1544 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1545 | if (*cur != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1546 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_INVALID_MAXOCCURS, |
| 1547 | "invalid value for maxOccurs: %s\n", val, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1548 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1549 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1550 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | /** |
| 1554 | * xmlGetMinOccurs: |
| 1555 | * @ctxt: a schema validation context |
| 1556 | * @node: a subtree containing XML Schema informations |
| 1557 | * |
| 1558 | * Get the minOccurs property |
| 1559 | * |
| 1560 | * Returns the default if not found, or the value |
| 1561 | */ |
| 1562 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1563 | xmlGetMinOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node) |
| 1564 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1565 | const xmlChar *val, *cur; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1566 | int ret = 0; |
| 1567 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1568 | val = xmlSchemaGetProp(ctxt, node, "minOccurs"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1569 | if (val == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1570 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1571 | |
| 1572 | cur = val; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1573 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1574 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1575 | while ((*cur >= '0') && (*cur <= '9')) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1576 | ret = ret * 10 + (*cur - '0'); |
| 1577 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1578 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1579 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1580 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1581 | if (*cur != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1582 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_INVALID_MINOCCURS, |
| 1583 | "invalid value for minOccurs: %s\n", val, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1584 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1585 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1586 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | /** |
| 1590 | * xmlGetBooleanProp: |
| 1591 | * @ctxt: a schema validation context |
| 1592 | * @node: a subtree containing XML Schema informations |
| 1593 | * @name: the attribute name |
| 1594 | * @def: the default value |
| 1595 | * |
| 1596 | * Get is a bolean property is set |
| 1597 | * |
| 1598 | * Returns the default if not found, 0 if found to be false, |
| 1599 | * 1 if found to be true |
| 1600 | */ |
| 1601 | static int |
| 1602 | xmlGetBooleanProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1603 | const char *name, int def) |
| 1604 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1605 | const xmlChar *val; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1606 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1607 | val = xmlSchemaGetProp(ctxt, node, name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1608 | if (val == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1609 | return (def); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1610 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1611 | if (xmlStrEqual(val, BAD_CAST "true")) |
| 1612 | def = 1; |
| 1613 | else if (xmlStrEqual(val, BAD_CAST "false")) |
| 1614 | def = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1615 | else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1616 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_INVALID_BOOLEAN, |
| 1617 | "Attribute %s: the value %s is not boolean\n", |
| 1618 | (const xmlChar *) name, val); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1619 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1620 | return (def); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | /************************************************************************ |
| 1624 | * * |
| 1625 | * Shema extraction from an Infoset * |
| 1626 | * * |
| 1627 | ************************************************************************/ |
| 1628 | static xmlSchemaTypePtr xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr |
| 1629 | ctxt, xmlSchemaPtr schema, |
| 1630 | xmlNodePtr node); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1631 | static xmlSchemaTypePtr xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr |
| 1632 | ctxt, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1633 | xmlSchemaPtr schema, |
| 1634 | xmlNodePtr node); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1635 | static xmlSchemaTypePtr xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr |
| 1636 | ctxt, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1637 | xmlSchemaPtr schema, |
| 1638 | xmlNodePtr node, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1639 | int simple); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1640 | static xmlSchemaTypePtr xmlSchemaParseSequence(xmlSchemaParserCtxtPtr ctxt, |
| 1641 | xmlSchemaPtr schema, |
| 1642 | xmlNodePtr node); |
| 1643 | static xmlSchemaTypePtr xmlSchemaParseAll(xmlSchemaParserCtxtPtr ctxt, |
| 1644 | xmlSchemaPtr schema, |
| 1645 | xmlNodePtr node); |
| 1646 | static xmlSchemaAttributePtr xmlSchemaParseAttribute(xmlSchemaParserCtxtPtr |
| 1647 | ctxt, |
| 1648 | xmlSchemaPtr schema, |
| 1649 | xmlNodePtr node); |
| 1650 | static xmlSchemaAttributeGroupPtr |
| 1651 | xmlSchemaParseAttributeGroup(xmlSchemaParserCtxtPtr ctxt, |
| 1652 | xmlSchemaPtr schema, xmlNodePtr node); |
| 1653 | static xmlSchemaTypePtr xmlSchemaParseChoice(xmlSchemaParserCtxtPtr ctxt, |
| 1654 | xmlSchemaPtr schema, |
| 1655 | xmlNodePtr node); |
| 1656 | static xmlSchemaTypePtr xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt, |
| 1657 | xmlSchemaPtr schema, |
| 1658 | xmlNodePtr node); |
| 1659 | static xmlSchemaAttributePtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1660 | xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt, |
| 1661 | xmlSchemaPtr schema, xmlNodePtr node); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1662 | |
| 1663 | /** |
| 1664 | * xmlSchemaParseAttrDecls: |
| 1665 | * @ctxt: a schema validation context |
| 1666 | * @schema: the schema being built |
| 1667 | * @node: a subtree containing XML Schema informations |
| 1668 | * @type: the hosting type |
| 1669 | * |
| 1670 | * parse a XML schema attrDecls declaration corresponding to |
| 1671 | * <!ENTITY % attrDecls |
| 1672 | * '((%attribute;| %attributeGroup;)*,(%anyAttribute;)?)'> |
| 1673 | */ |
| 1674 | static xmlNodePtr |
| 1675 | xmlSchemaParseAttrDecls(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 1676 | xmlNodePtr child, xmlSchemaTypePtr type) |
| 1677 | { |
| 1678 | xmlSchemaAttributePtr lastattr, attr; |
| 1679 | |
| 1680 | lastattr = NULL; |
| 1681 | while ((IS_SCHEMA(child, "attribute")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1682 | (IS_SCHEMA(child, "attributeGroup"))) { |
| 1683 | attr = NULL; |
| 1684 | if (IS_SCHEMA(child, "attribute")) { |
| 1685 | attr = xmlSchemaParseAttribute(ctxt, schema, child); |
| 1686 | } else if (IS_SCHEMA(child, "attributeGroup")) { |
| 1687 | attr = (xmlSchemaAttributePtr) |
| 1688 | xmlSchemaParseAttributeGroup(ctxt, schema, child); |
| 1689 | } |
| 1690 | if (attr != NULL) { |
| 1691 | if (lastattr == NULL) { |
| 1692 | type->attributes = attr; |
| 1693 | lastattr = attr; |
| 1694 | } else { |
| 1695 | lastattr->next = attr; |
| 1696 | lastattr = attr; |
| 1697 | } |
| 1698 | } |
| 1699 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1700 | } |
| 1701 | if (IS_SCHEMA(child, "anyAttribute")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1702 | attr = xmlSchemaParseAnyAttribute(ctxt, schema, child); |
| 1703 | if (attr != NULL) { |
| 1704 | if (lastattr == NULL) { |
| 1705 | type->attributes = attr; |
| 1706 | lastattr = attr; |
| 1707 | } else { |
| 1708 | lastattr->next = attr; |
| 1709 | lastattr = attr; |
| 1710 | } |
| 1711 | } |
| 1712 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1713 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1714 | return (child); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | /** |
| 1718 | * xmlSchemaParseAnnotation: |
| 1719 | * @ctxt: a schema validation context |
| 1720 | * @schema: the schema being built |
| 1721 | * @node: a subtree containing XML Schema informations |
| 1722 | * |
| 1723 | * parse a XML schema Attrribute declaration |
| 1724 | * *WARNING* this interface is highly subject to change |
| 1725 | * |
| 1726 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 1727 | * 1 in case of success. |
| 1728 | */ |
| 1729 | static xmlSchemaAnnotPtr |
| 1730 | xmlSchemaParseAnnotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 1731 | xmlNodePtr node) |
| 1732 | { |
| 1733 | xmlSchemaAnnotPtr ret; |
| 1734 | |
| 1735 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1736 | return (NULL); |
| 1737 | ret = xmlSchemaNewAnnot(ctxt, node); |
| 1738 | |
| 1739 | return (ret); |
| 1740 | } |
| 1741 | |
| 1742 | /** |
| 1743 | * xmlSchemaParseFacet: |
| 1744 | * @ctxt: a schema validation context |
| 1745 | * @schema: the schema being built |
| 1746 | * @node: a subtree containing XML Schema informations |
| 1747 | * |
| 1748 | * parse a XML schema Facet declaration |
| 1749 | * *WARNING* this interface is highly subject to change |
| 1750 | * |
| 1751 | * Returns the new type structure or NULL in case of error |
| 1752 | */ |
| 1753 | static xmlSchemaFacetPtr |
| 1754 | xmlSchemaParseFacet(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1755 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1756 | { |
| 1757 | xmlSchemaFacetPtr facet; |
| 1758 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1759 | const xmlChar *value; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1760 | |
| 1761 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1762 | return (NULL); |
| 1763 | |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 1764 | facet = xmlSchemaNewFacet(); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1765 | if (facet == NULL) { |
| 1766 | xmlSchemaPErrMemory(ctxt, "allocating facet", node); |
| 1767 | return (NULL); |
| 1768 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1769 | facet->node = node; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1770 | value = xmlSchemaGetProp(ctxt, node, "value"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1771 | if (value == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1772 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_FACET_NO_VALUE, |
| 1773 | "Facet %s has no value\n", node->name, NULL); |
| 1774 | xmlSchemaFreeFacet(facet); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1775 | return (NULL); |
| 1776 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1777 | if (IS_SCHEMA(node, "minInclusive")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1778 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1779 | } else if (IS_SCHEMA(node, "minExclusive")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1780 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1781 | } else if (IS_SCHEMA(node, "maxInclusive")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1782 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1783 | } else if (IS_SCHEMA(node, "maxExclusive")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1784 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1785 | } else if (IS_SCHEMA(node, "totalDigits")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1786 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1787 | } else if (IS_SCHEMA(node, "fractionDigits")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1788 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1789 | } else if (IS_SCHEMA(node, "pattern")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1790 | facet->type = XML_SCHEMA_FACET_PATTERN; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1791 | } else if (IS_SCHEMA(node, "enumeration")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1792 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1793 | } else if (IS_SCHEMA(node, "whiteSpace")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1794 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1795 | } else if (IS_SCHEMA(node, "length")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1796 | facet->type = XML_SCHEMA_FACET_LENGTH; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1797 | } else if (IS_SCHEMA(node, "maxLength")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1798 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
| 1799 | } else if (IS_SCHEMA(node, "minLength")) { |
| 1800 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
| 1801 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1802 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_TYPE, |
| 1803 | "Unknown facet type %s\n", node->name, NULL); |
| 1804 | xmlSchemaFreeFacet(facet); |
| 1805 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1806 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1807 | facet->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1808 | facet->value = value; |
| 1809 | child = node->children; |
| 1810 | |
| 1811 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1812 | facet->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 1813 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1814 | } |
| 1815 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1816 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_CHILD, |
| 1817 | "Facet %s has unexpected child content\n", |
| 1818 | node->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1819 | } |
| 1820 | return (facet); |
| 1821 | } |
| 1822 | |
| 1823 | /** |
| 1824 | * xmlSchemaParseAny: |
| 1825 | * @ctxt: a schema validation context |
| 1826 | * @schema: the schema being built |
| 1827 | * @node: a subtree containing XML Schema informations |
| 1828 | * |
| 1829 | * parse a XML schema Any declaration |
| 1830 | * *WARNING* this interface is highly subject to change |
| 1831 | * |
| 1832 | * Returns the new type structure or NULL in case of error |
| 1833 | */ |
| 1834 | static xmlSchemaTypePtr |
| 1835 | xmlSchemaParseAny(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 1836 | xmlNodePtr node) |
| 1837 | { |
| 1838 | xmlSchemaTypePtr type; |
| 1839 | xmlNodePtr child = NULL; |
| 1840 | xmlChar name[30]; |
| 1841 | |
| 1842 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1843 | return (NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1844 | snprintf((char *) name, 30, "any %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1845 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1846 | if (type == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1847 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1848 | type->node = node; |
| 1849 | type->type = XML_SCHEMA_TYPE_ANY; |
| 1850 | child = node->children; |
| 1851 | type->minOccurs = xmlGetMinOccurs(ctxt, node); |
| 1852 | type->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
| 1853 | |
| 1854 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1855 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 1856 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1857 | } |
| 1858 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1859 | xmlSchemaPErr2(ctxt, node, child, |
| 1860 | XML_SCHEMAP_UNKNOWN_SEQUENCE_CHILD, |
| 1861 | "Sequence %s has unexpected content\n", type->name, |
| 1862 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | return (type); |
| 1866 | } |
| 1867 | |
| 1868 | /** |
| 1869 | * xmlSchemaParseNotation: |
| 1870 | * @ctxt: a schema validation context |
| 1871 | * @schema: the schema being built |
| 1872 | * @node: a subtree containing XML Schema informations |
| 1873 | * |
| 1874 | * parse a XML schema Notation declaration |
| 1875 | * |
| 1876 | * Returns the new structure or NULL in case of error |
| 1877 | */ |
| 1878 | static xmlSchemaNotationPtr |
| 1879 | xmlSchemaParseNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1880 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1881 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1882 | const xmlChar *name; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1883 | xmlSchemaNotationPtr ret; |
| 1884 | xmlNodePtr child = NULL; |
| 1885 | |
| 1886 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1887 | return (NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1888 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1889 | if (name == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1890 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_NOTATION_NO_NAME, |
| 1891 | "Notation has no name\n", NULL, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1892 | return (NULL); |
| 1893 | } |
| 1894 | ret = xmlSchemaAddNotation(ctxt, schema, name); |
| 1895 | if (ret == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1896 | return (NULL); |
| 1897 | } |
| 1898 | child = node->children; |
| 1899 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1900 | ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 1901 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1902 | } |
| 1903 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1904 | xmlSchemaPErr2(ctxt, node, child, |
| 1905 | XML_SCHEMAP_UNKNOWN_NOTATION_CHILD, |
| 1906 | "notation %s has unexpected content\n", name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | return (ret); |
| 1910 | } |
| 1911 | |
| 1912 | /** |
| 1913 | * xmlSchemaParseAnyAttribute: |
| 1914 | * @ctxt: a schema validation context |
| 1915 | * @schema: the schema being built |
| 1916 | * @node: a subtree containing XML Schema informations |
| 1917 | * |
| 1918 | * parse a XML schema AnyAttrribute declaration |
| 1919 | * *WARNING* this interface is highly subject to change |
| 1920 | * |
| 1921 | * Returns an attribute def structure or NULL |
| 1922 | */ |
| 1923 | static xmlSchemaAttributePtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1924 | xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt, |
| 1925 | xmlSchemaPtr schema, xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1926 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1927 | const xmlChar *processContents; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1928 | xmlSchemaAttributePtr ret; |
| 1929 | xmlNodePtr child = NULL; |
| 1930 | char name[100]; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1931 | const xmlChar *local, *ns; |
| 1932 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1933 | |
| 1934 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1935 | return (NULL); |
| 1936 | |
| 1937 | snprintf(name, 99, "anyattr %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1938 | local = xmlSchemaGetNamespace(ctxt, schema, node, BAD_CAST "anyattr", &ns); |
| 1939 | ret = xmlSchemaAddAttribute(ctxt, schema, BAD_CAST name, ns); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1940 | if (ret == NULL) { |
| 1941 | return (NULL); |
| 1942 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1943 | ret->id = xmlSchemaGetProp(ctxt, node, "id"); |
| 1944 | processContents = xmlSchemaGetProp(ctxt, node, "processContents"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1945 | if ((processContents == NULL) |
| 1946 | || (xmlStrEqual(processContents, (const xmlChar *) "strict"))) { |
| 1947 | ret->occurs = XML_SCHEMAS_ANYATTR_STRICT; |
| 1948 | } else if (xmlStrEqual(processContents, (const xmlChar *) "skip")) { |
| 1949 | ret->occurs = XML_SCHEMAS_ANYATTR_SKIP; |
| 1950 | } else if (xmlStrEqual(processContents, (const xmlChar *) "lax")) { |
| 1951 | ret->occurs = XML_SCHEMAS_ANYATTR_LAX; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1952 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1953 | xmlSchemaPErr2(ctxt, node, child, |
| 1954 | XML_SCHEMAP_UNKNOWN_PROCESSCONTENT_CHILD, |
| 1955 | "anyAttribute has unexpected content for processContents: %s\n", |
| 1956 | processContents, NULL); |
| 1957 | ret->occurs = XML_SCHEMAS_ANYATTR_STRICT; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1958 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1959 | |
| 1960 | child = node->children; |
| 1961 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1962 | ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 1963 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1964 | } |
| 1965 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1966 | xmlSchemaPErr2(ctxt, node, child, |
| 1967 | XML_SCHEMAP_UNKNOWN_ANYATTRIBUTE_CHILD, |
| 1968 | "anyAttribute %s has unexpected content\n", |
| 1969 | (const xmlChar *) name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | return (ret); |
| 1973 | } |
| 1974 | |
| 1975 | |
| 1976 | /** |
| 1977 | * xmlSchemaParseAttribute: |
| 1978 | * @ctxt: a schema validation context |
| 1979 | * @schema: the schema being built |
| 1980 | * @node: a subtree containing XML Schema informations |
| 1981 | * |
| 1982 | * parse a XML schema Attrribute declaration |
| 1983 | * *WARNING* this interface is highly subject to change |
| 1984 | * |
| 1985 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 1986 | * 1 in case of success. |
| 1987 | */ |
| 1988 | static xmlSchemaAttributePtr |
| 1989 | xmlSchemaParseAttribute(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 1990 | xmlNodePtr node) |
| 1991 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1992 | const xmlChar *name, *refNs = NULL, *ref = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1993 | xmlSchemaAttributePtr ret; |
| 1994 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1995 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1996 | |
| 1997 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1998 | return (NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1999 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2000 | if (name == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2001 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2002 | ref = xmlGetQNameProp(ctxt, node, "ref", &refNs); |
| 2003 | if (ref == NULL) { |
| 2004 | xmlSchemaPErr2(ctxt, node, child, |
| 2005 | XML_SCHEMAP_ATTR_NONAME_NOREF, |
| 2006 | "Attribute has no name nor ref\n", NULL, NULL); |
| 2007 | return (NULL); |
| 2008 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2009 | if (refNs == NULL) |
| 2010 | refNs = schema->targetNamespace; |
| 2011 | snprintf(buf, 99, "anonattr %d", ctxt->counter++ + 1); |
| 2012 | name = (const xmlChar *) buf; |
| 2013 | ret = xmlSchemaAddAttribute(ctxt, schema, name, NULL); |
| 2014 | } else { |
| 2015 | const xmlChar *local, *ns; |
| 2016 | |
| 2017 | local = xmlSchemaGetNamespace(ctxt, schema, node, name, &ns); |
| 2018 | ret = xmlSchemaAddAttribute(ctxt, schema, local, ns); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2019 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2020 | if (ret == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2021 | return (NULL); |
| 2022 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2023 | ret->ref = ref; |
| 2024 | ret->refNs = refNs; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2025 | if ((ret->targetNamespace != NULL) && |
| 2026 | ((schema->flags & XML_SCHEMAS_QUALIF_ATTR) == 0) && |
| 2027 | (xmlStrEqual(ret->targetNamespace, schema->targetNamespace))) |
| 2028 | ret->flags |= XML_SCHEMAS_ATTR_NSDEFAULT; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2029 | ret->typeName = xmlGetQNameProp(ctxt, node, "type", &(ret->typeNs)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2030 | if ((ret->typeName != NULL) && (ret->typeNs == NULL)) |
| 2031 | ret->typeNs = schema->targetNamespace; |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 2032 | ret->node = node; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2033 | child = node->children; |
| 2034 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2035 | ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2036 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2037 | } |
| 2038 | if (IS_SCHEMA(child, "simpleType")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2039 | ret->subtypes = xmlSchemaParseSimpleType(ctxt, schema, child); |
| 2040 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2041 | } |
| 2042 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2043 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_ATTR_CHILD, |
| 2044 | "attribute %s has unexpected content\n", name, |
| 2045 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | return (ret); |
| 2049 | } |
| 2050 | |
| 2051 | /** |
| 2052 | * xmlSchemaParseAttributeGroup: |
| 2053 | * @ctxt: a schema validation context |
| 2054 | * @schema: the schema being built |
| 2055 | * @node: a subtree containing XML Schema informations |
| 2056 | * |
| 2057 | * parse a XML schema Attribute Group declaration |
| 2058 | * *WARNING* this interface is highly subject to change |
| 2059 | * |
| 2060 | * Returns the attribute group or NULL in case of error. |
| 2061 | */ |
| 2062 | static xmlSchemaAttributeGroupPtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2063 | xmlSchemaParseAttributeGroup(xmlSchemaParserCtxtPtr ctxt, |
| 2064 | xmlSchemaPtr schema, xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2065 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2066 | const xmlChar *name, *refNs = NULL, *ref = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2067 | xmlSchemaAttributeGroupPtr ret; |
| 2068 | xmlSchemaAttributePtr last = NULL, attr; |
| 2069 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2070 | const xmlChar *oldcontainer; |
| 2071 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2072 | |
| 2073 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2074 | return (NULL); |
| 2075 | oldcontainer = ctxt->container; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2076 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2077 | if (name == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2078 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2079 | ref = xmlGetQNameProp(ctxt, node, "ref", &refNs); |
| 2080 | if (ref == NULL) { |
| 2081 | xmlSchemaPErr2(ctxt, node, child, |
| 2082 | XML_SCHEMAP_ATTRGRP_NONAME_NOREF, |
| 2083 | "AttributeGroup has no name nor ref\n", NULL, |
| 2084 | NULL); |
| 2085 | return (NULL); |
| 2086 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2087 | if (refNs == NULL) |
| 2088 | refNs = schema->targetNamespace; |
| 2089 | snprintf(buf, 99, "anonattrgroup %d", ctxt->counter++ + 1); |
| 2090 | name = (const xmlChar *) buf; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2091 | if (name == NULL) { |
| 2092 | xmlSchemaPErrMemory(ctxt, "creating attribute group", node); |
| 2093 | return (NULL); |
| 2094 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2095 | } |
| 2096 | ret = xmlSchemaAddAttributeGroup(ctxt, schema, name); |
| 2097 | if (ret == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2098 | return (NULL); |
| 2099 | } |
| 2100 | ret->ref = ref; |
| 2101 | ret->refNs = refNs; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 2102 | ret->type = XML_SCHEMA_TYPE_ATTRIBUTEGROUP; |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 2103 | ret->node = node; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2104 | child = node->children; |
| 2105 | ctxt->container = name; |
| 2106 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2107 | ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2108 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2109 | } |
| 2110 | while ((IS_SCHEMA(child, "attribute")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2111 | (IS_SCHEMA(child, "attributeGroup"))) { |
| 2112 | attr = NULL; |
| 2113 | if (IS_SCHEMA(child, "attribute")) { |
| 2114 | attr = xmlSchemaParseAttribute(ctxt, schema, child); |
| 2115 | } else if (IS_SCHEMA(child, "attributeGroup")) { |
| 2116 | attr = (xmlSchemaAttributePtr) |
| 2117 | xmlSchemaParseAttributeGroup(ctxt, schema, child); |
| 2118 | } |
| 2119 | if (attr != NULL) { |
| 2120 | if (last == NULL) { |
| 2121 | ret->attributes = attr; |
| 2122 | last = attr; |
| 2123 | } else { |
| 2124 | last->next = attr; |
| 2125 | last = attr; |
| 2126 | } |
| 2127 | } |
| 2128 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2129 | } |
| 2130 | if (IS_SCHEMA(child, "anyAttribute")) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2131 | TODO |
| 2132 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2133 | } |
| 2134 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2135 | xmlSchemaPErr2(ctxt, node, child, |
| 2136 | XML_SCHEMAP_UNKNOWN_ATTRGRP_CHILD, |
| 2137 | "attribute group %s has unexpected content\n", name, |
| 2138 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2139 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2140 | ctxt->container = oldcontainer; |
| 2141 | return (ret); |
| 2142 | } |
| 2143 | |
| 2144 | /** |
| 2145 | * xmlSchemaParseElement: |
| 2146 | * @ctxt: a schema validation context |
| 2147 | * @schema: the schema being built |
| 2148 | * @node: a subtree containing XML Schema informations |
| 2149 | * |
| 2150 | * parse a XML schema Element declaration |
| 2151 | * *WARNING* this interface is highly subject to change |
| 2152 | * |
| 2153 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2154 | * 1 in case of success. |
| 2155 | */ |
| 2156 | static xmlSchemaElementPtr |
| 2157 | xmlSchemaParseElement(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 2158 | xmlNodePtr node, int toplevel) |
| 2159 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2160 | const xmlChar *name, *fixed; |
| 2161 | const xmlChar *refNs = NULL, *ref = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2162 | xmlSchemaElementPtr ret; |
| 2163 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2164 | const xmlChar *oldcontainer; |
| 2165 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2166 | |
| 2167 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2168 | return (NULL); |
| 2169 | oldcontainer = ctxt->container; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2170 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2171 | if (name == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2172 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2173 | ref = xmlGetQNameProp(ctxt, node, "ref", &refNs); |
| 2174 | if (ref == NULL) { |
| 2175 | xmlSchemaPErr2(ctxt, node, child, |
| 2176 | XML_SCHEMAP_ELEM_NONAME_NOREF, |
| 2177 | "Element has no name nor ref\n", NULL, NULL); |
| 2178 | return (NULL); |
| 2179 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2180 | if (refNs == NULL) |
| 2181 | refNs = schema->targetNamespace; |
| 2182 | snprintf(buf, 99, "anonelem %d", ctxt->counter++ + 1); |
| 2183 | name = (const xmlChar *) buf; |
| 2184 | ret = xmlSchemaAddElement(ctxt, schema, name, NULL); |
| 2185 | } else { |
| 2186 | const xmlChar *local, *ns; |
| 2187 | |
| 2188 | local = xmlSchemaGetNamespace(ctxt, schema, node, name, &ns); |
| 2189 | ret = xmlSchemaAddElement(ctxt, schema, local, ns); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2190 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2191 | if (ret != NULL) |
| 2192 | ret->node = node; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2193 | if (ret == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2194 | return (NULL); |
| 2195 | } |
| 2196 | ret->type = XML_SCHEMA_TYPE_ELEMENT; |
| 2197 | ret->ref = ref; |
| 2198 | ret->refNs = refNs; |
| 2199 | if (ref != NULL) |
| 2200 | ret->flags |= XML_SCHEMAS_ELEM_REF; |
| 2201 | if (toplevel) |
| 2202 | ret->flags |= XML_SCHEMAS_ELEM_TOPLEVEL; |
| 2203 | if (xmlGetBooleanProp(ctxt, node, "nillable", 0)) |
| 2204 | ret->flags |= XML_SCHEMAS_ELEM_NILLABLE; |
| 2205 | if (xmlGetBooleanProp(ctxt, node, "abstract", 0)) |
| 2206 | ret->flags |= XML_SCHEMAS_ELEM_NILLABLE; |
| 2207 | ctxt->container = name; |
| 2208 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2209 | ret->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2210 | ret->namedType = |
| 2211 | xmlGetQNameProp(ctxt, node, "type", &(ret->namedTypeNs)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2212 | if ((ret->namedType != NULL) && (ret->namedTypeNs == NULL)) |
| 2213 | ret->namedTypeNs = schema->targetNamespace; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2214 | ret->substGroup = |
| 2215 | xmlGetQNameProp(ctxt, node, "substitutionGroup", |
| 2216 | &(ret->substGroupNs)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2217 | fixed = xmlSchemaGetProp(ctxt, node, "fixed"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2218 | ret->minOccurs = xmlGetMinOccurs(ctxt, node); |
| 2219 | ret->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2220 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2221 | ret->value = xmlSchemaGetProp(ctxt, node, "default"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2222 | if ((ret->value != NULL) && (fixed != NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2223 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_ELEM_DEFAULT_FIXED, |
| 2224 | "Element %s has both default and fixed\n", |
| 2225 | ret->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2226 | } else if (fixed != NULL) { |
| 2227 | ret->flags |= XML_SCHEMAS_ELEM_FIXED; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2228 | ret->value = fixed; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
| 2231 | child = node->children; |
| 2232 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2233 | ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2234 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2235 | } |
| 2236 | if (IS_SCHEMA(child, "complexType")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2237 | ret->subtypes = xmlSchemaParseComplexType(ctxt, schema, child); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2238 | child = child->next; |
| 2239 | } else if (IS_SCHEMA(child, "simpleType")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2240 | ret->subtypes = xmlSchemaParseSimpleType(ctxt, schema, child); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2241 | child = child->next; |
| 2242 | } |
| 2243 | while ((IS_SCHEMA(child, "unique")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2244 | (IS_SCHEMA(child, "key")) || (IS_SCHEMA(child, "keyref"))) { |
| 2245 | TODO child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2246 | } |
| 2247 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2248 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_ELEM_CHILD, |
| 2249 | "element %s has unexpected content\n", name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | ctxt->container = oldcontainer; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2253 | return (ret); |
| 2254 | } |
| 2255 | |
| 2256 | /** |
| 2257 | * xmlSchemaParseUnion: |
| 2258 | * @ctxt: a schema validation context |
| 2259 | * @schema: the schema being built |
| 2260 | * @node: a subtree containing XML Schema informations |
| 2261 | * |
| 2262 | * parse a XML schema Union definition |
| 2263 | * *WARNING* this interface is highly subject to change |
| 2264 | * |
| 2265 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2266 | * 1 in case of success. |
| 2267 | */ |
| 2268 | static xmlSchemaTypePtr |
| 2269 | xmlSchemaParseUnion(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2270 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2271 | { |
| 2272 | xmlSchemaTypePtr type, subtype, last = NULL; |
| 2273 | xmlNodePtr child = NULL; |
| 2274 | xmlChar name[30]; |
| 2275 | |
| 2276 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2277 | return (NULL); |
| 2278 | |
| 2279 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2280 | snprintf((char *) name, 30, "union %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2281 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2282 | if (type == NULL) |
| 2283 | return (NULL); |
| 2284 | type->node = node; |
| 2285 | type->type = XML_SCHEMA_TYPE_LIST; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2286 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
| 2287 | type->ref = xmlSchemaGetProp(ctxt, node, "memberTypes"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2288 | |
| 2289 | child = node->children; |
| 2290 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2291 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2292 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2293 | } |
| 2294 | while (IS_SCHEMA(child, "simpleType")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2295 | subtype = (xmlSchemaTypePtr) |
| 2296 | xmlSchemaParseSimpleType(ctxt, schema, child); |
| 2297 | if (subtype != NULL) { |
| 2298 | if (last == NULL) { |
| 2299 | type->subtypes = subtype; |
| 2300 | last = subtype; |
| 2301 | } else { |
| 2302 | last->next = subtype; |
| 2303 | last = subtype; |
| 2304 | } |
| 2305 | last->next = NULL; |
| 2306 | } |
| 2307 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2308 | } |
| 2309 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2310 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_UNION_CHILD, |
| 2311 | "Union %s has unexpected content\n", type->name, |
| 2312 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2313 | } |
| 2314 | return (type); |
| 2315 | } |
| 2316 | |
| 2317 | /** |
| 2318 | * xmlSchemaParseList: |
| 2319 | * @ctxt: a schema validation context |
| 2320 | * @schema: the schema being built |
| 2321 | * @node: a subtree containing XML Schema informations |
| 2322 | * |
| 2323 | * parse a XML schema List definition |
| 2324 | * *WARNING* this interface is highly subject to change |
| 2325 | * |
| 2326 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2327 | * 1 in case of success. |
| 2328 | */ |
| 2329 | static xmlSchemaTypePtr |
| 2330 | xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2331 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2332 | { |
| 2333 | xmlSchemaTypePtr type, subtype; |
| 2334 | xmlNodePtr child = NULL; |
| 2335 | xmlChar name[30]; |
| 2336 | |
| 2337 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2338 | return (NULL); |
| 2339 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2340 | snprintf((char *) name, 30, "list %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2341 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2342 | if (type == NULL) |
| 2343 | return (NULL); |
| 2344 | type->node = node; |
| 2345 | type->type = XML_SCHEMA_TYPE_LIST; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2346 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2347 | type->ref = xmlGetQNameProp(ctxt, node, "ref", &(type->refNs)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2348 | if ((type->ref != NULL) && (type->refNs == NULL)) |
| 2349 | type->refNs = schema->targetNamespace; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2350 | |
| 2351 | child = node->children; |
| 2352 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2353 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2354 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2355 | } |
| 2356 | subtype = NULL; |
| 2357 | if (IS_SCHEMA(child, "simpleType")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2358 | subtype = (xmlSchemaTypePtr) |
| 2359 | xmlSchemaParseSimpleType(ctxt, schema, child); |
| 2360 | child = child->next; |
| 2361 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2362 | } |
| 2363 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2364 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_LIST_CHILD, |
| 2365 | "List %s has unexpected content\n", type->name, |
| 2366 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2367 | } |
| 2368 | return (type); |
| 2369 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2370 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2371 | /** |
| 2372 | * xmlSchemaParseSimpleType: |
| 2373 | * @ctxt: a schema validation context |
| 2374 | * @schema: the schema being built |
| 2375 | * @node: a subtree containing XML Schema informations |
| 2376 | * |
| 2377 | * parse a XML schema Simple Type definition |
| 2378 | * *WARNING* this interface is highly subject to change |
| 2379 | * |
| 2380 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2381 | * 1 in case of success. |
| 2382 | */ |
| 2383 | static xmlSchemaTypePtr |
| 2384 | xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 2385 | xmlNodePtr node) |
| 2386 | { |
| 2387 | xmlSchemaTypePtr type, subtype; |
| 2388 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2389 | const xmlChar *name; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2390 | |
| 2391 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2392 | return (NULL); |
| 2393 | |
| 2394 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2395 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2396 | if (name == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2397 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2398 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2399 | snprintf(buf, 99, "simpletype %d", ctxt->counter++ + 1); |
| 2400 | type = xmlSchemaAddType(ctxt, schema, (const xmlChar *)buf, NULL); |
| 2401 | } else { |
| 2402 | const xmlChar *local, *ns; |
| 2403 | |
| 2404 | local = xmlSchemaGetNamespace(ctxt, schema, node, name, &ns); |
| 2405 | type = xmlSchemaAddType(ctxt, schema, local, ns); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2406 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2407 | if (type == NULL) |
| 2408 | return (NULL); |
| 2409 | type->node = node; |
| 2410 | type->type = XML_SCHEMA_TYPE_SIMPLE; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2411 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2412 | |
| 2413 | child = node->children; |
| 2414 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2415 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2416 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2417 | } |
| 2418 | subtype = NULL; |
| 2419 | if (IS_SCHEMA(child, "restriction")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2420 | subtype = (xmlSchemaTypePtr) |
| 2421 | xmlSchemaParseRestriction(ctxt, schema, child, 1); |
| 2422 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2423 | } else if (IS_SCHEMA(child, "list")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2424 | subtype = (xmlSchemaTypePtr) |
| 2425 | xmlSchemaParseList(ctxt, schema, child); |
| 2426 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2427 | } else if (IS_SCHEMA(child, "union")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2428 | subtype = (xmlSchemaTypePtr) |
| 2429 | xmlSchemaParseUnion(ctxt, schema, child); |
| 2430 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2431 | } |
| 2432 | type->subtypes = subtype; |
| 2433 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2434 | xmlSchemaPErr2(ctxt, node, child, |
| 2435 | XML_SCHEMAP_UNKNOWN_SIMPLETYPE_CHILD, |
| 2436 | "SimpleType %s has unexpected content\n", |
| 2437 | type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2438 | } |
| 2439 | |
| 2440 | return (type); |
| 2441 | } |
| 2442 | |
| 2443 | |
| 2444 | /** |
| 2445 | * xmlSchemaParseGroup: |
| 2446 | * @ctxt: a schema validation context |
| 2447 | * @schema: the schema being built |
| 2448 | * @node: a subtree containing XML Schema informations |
| 2449 | * |
| 2450 | * parse a XML schema Group definition |
| 2451 | * *WARNING* this interface is highly subject to change |
| 2452 | * |
| 2453 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2454 | * 1 in case of success. |
| 2455 | */ |
| 2456 | static xmlSchemaTypePtr |
| 2457 | xmlSchemaParseGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2458 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2459 | { |
| 2460 | xmlSchemaTypePtr type, subtype; |
| 2461 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2462 | const xmlChar *name; |
| 2463 | const xmlChar *ref = NULL, *refNs = NULL; |
| 2464 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2465 | |
| 2466 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2467 | return (NULL); |
| 2468 | |
| 2469 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2470 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2471 | if (name == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2472 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2473 | ref = xmlGetQNameProp(ctxt, node, "ref", &refNs); |
| 2474 | if (ref == NULL) { |
| 2475 | xmlSchemaPErr2(ctxt, node, child, |
| 2476 | XML_SCHEMAP_GROUP_NONAME_NOREF, |
| 2477 | "Group has no name nor ref\n", NULL, NULL); |
| 2478 | return (NULL); |
| 2479 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2480 | if (refNs == NULL) |
| 2481 | refNs = schema->targetNamespace; |
| 2482 | snprintf(buf, 99, "anongroup %d", ctxt->counter++ + 1); |
| 2483 | name = (const xmlChar *) buf; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2484 | } |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 2485 | type = xmlSchemaAddGroup(ctxt, schema, name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2486 | if (type == NULL) |
| 2487 | return (NULL); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2488 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2489 | type->node = node; |
| 2490 | type->type = XML_SCHEMA_TYPE_GROUP; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2491 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2492 | type->ref = ref; |
| 2493 | type->refNs = refNs; |
| 2494 | type->minOccurs = xmlGetMinOccurs(ctxt, node); |
| 2495 | type->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
| 2496 | |
| 2497 | child = node->children; |
| 2498 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2499 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2500 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2501 | } |
| 2502 | subtype = NULL; |
| 2503 | if (IS_SCHEMA(child, "all")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2504 | subtype = (xmlSchemaTypePtr) |
| 2505 | xmlSchemaParseAll(ctxt, schema, child); |
| 2506 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2507 | } else if (IS_SCHEMA(child, "choice")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2508 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 2509 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2510 | } else if (IS_SCHEMA(child, "sequence")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2511 | subtype = (xmlSchemaTypePtr) |
| 2512 | xmlSchemaParseSequence(ctxt, schema, child); |
| 2513 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2514 | } |
| 2515 | if (subtype != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2516 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2517 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2518 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_GROUP_CHILD, |
| 2519 | "Group %s has unexpected content\n", type->name, |
| 2520 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2521 | } |
| 2522 | |
| 2523 | return (type); |
| 2524 | } |
| 2525 | |
| 2526 | /** |
| 2527 | * xmlSchemaParseAll: |
| 2528 | * @ctxt: a schema validation context |
| 2529 | * @schema: the schema being built |
| 2530 | * @node: a subtree containing XML Schema informations |
| 2531 | * |
| 2532 | * parse a XML schema All definition |
| 2533 | * *WARNING* this interface is highly subject to change |
| 2534 | * |
| 2535 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2536 | * 1 in case of success. |
| 2537 | */ |
| 2538 | static xmlSchemaTypePtr |
| 2539 | xmlSchemaParseAll(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2540 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2541 | { |
| 2542 | xmlSchemaTypePtr type, subtype, last = NULL; |
| 2543 | xmlNodePtr child = NULL; |
| 2544 | xmlChar name[30]; |
| 2545 | |
| 2546 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2547 | return (NULL); |
| 2548 | |
| 2549 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2550 | snprintf((char *) name, 30, "all%d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2551 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2552 | if (type == NULL) |
| 2553 | return (NULL); |
| 2554 | type->node = node; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 2555 | type->type = XML_SCHEMA_TYPE_ALL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2556 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2557 | type->minOccurs = xmlGetMinOccurs(ctxt, node); |
William M. Brack | b15351e | 2003-12-27 04:34:42 +0000 | [diff] [blame] | 2558 | if (type->minOccurs > 1) |
| 2559 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_INVALID_MINOCCURS, |
| 2560 | "invalid value for minOccurs (must be 0 or 1)\n", NULL, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2561 | type->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
William M. Brack | b15351e | 2003-12-27 04:34:42 +0000 | [diff] [blame] | 2562 | if (type->maxOccurs > 1) |
| 2563 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_INVALID_MAXOCCURS, |
| 2564 | "invalid value for maxOccurs (must be 0 or 1)\n", NULL, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2565 | |
| 2566 | child = node->children; |
| 2567 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2568 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2569 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2570 | } |
| 2571 | while (IS_SCHEMA(child, "element")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2572 | subtype = (xmlSchemaTypePtr) |
| 2573 | xmlSchemaParseElement(ctxt, schema, child, 0); |
| 2574 | if (subtype != NULL) { |
William M. Brack | b15351e | 2003-12-27 04:34:42 +0000 | [diff] [blame] | 2575 | if (subtype->minOccurs > 1) |
| 2576 | xmlSchemaPErr(ctxt, child, XML_SCHEMAP_INVALID_MINOCCURS, |
| 2577 | "invalid value for minOccurs (must be 0 or 1)\n", |
| 2578 | NULL, NULL); |
| 2579 | if (subtype->maxOccurs > 1) |
| 2580 | xmlSchemaPErr(ctxt, child, XML_SCHEMAP_INVALID_MAXOCCURS, |
| 2581 | "invalid value for maxOccurs (must be 0 or 1)\n", |
| 2582 | NULL, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2583 | if (last == NULL) { |
| 2584 | type->subtypes = subtype; |
| 2585 | last = subtype; |
| 2586 | } else { |
| 2587 | last->next = subtype; |
| 2588 | last = subtype; |
| 2589 | } |
| 2590 | last->next = NULL; |
| 2591 | } |
| 2592 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2593 | } |
| 2594 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2595 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_ALL_CHILD, |
| 2596 | "All %s has unexpected content\n", type->name, |
| 2597 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
| 2600 | return (type); |
| 2601 | } |
| 2602 | |
| 2603 | /** |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2604 | * xmlSchemaImportSchema |
| 2605 | * |
| 2606 | * @ctxt: a schema validation context |
| 2607 | * @schemaLocation: an URI defining where to find the imported schema |
| 2608 | * |
| 2609 | * import a XML schema |
| 2610 | * *WARNING* this interface is highly subject to change |
| 2611 | * |
| 2612 | * Returns -1 in case of error and 1 in case of success. |
| 2613 | */ |
| 2614 | static xmlSchemaImportPtr |
| 2615 | xmlSchemaImportSchema(xmlSchemaParserCtxtPtr ctxt, |
| 2616 | const xmlChar *schemaLocation) |
| 2617 | { |
| 2618 | xmlSchemaImportPtr import; |
| 2619 | xmlSchemaParserCtxtPtr newctxt; |
| 2620 | |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2621 | newctxt = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt)); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2622 | if (newctxt == NULL) { |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2623 | xmlSchemaPErrMemory(ctxt, "allocating schama parser context", |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2624 | NULL); |
| 2625 | return (NULL); |
| 2626 | } |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2627 | memset(newctxt, 0, sizeof(xmlSchemaParserCtxt)); |
| 2628 | /* Keep the same dictionnary for parsing, really */ |
| 2629 | xmlDictReference(ctxt->dict); |
| 2630 | newctxt->dict = ctxt->dict; |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 2631 | newctxt->includes = 0; |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2632 | newctxt->URL = xmlDictLookup(newctxt->dict, schemaLocation, -1); |
| 2633 | |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2634 | xmlSchemaSetParserErrors(newctxt, ctxt->error, ctxt->warning, |
| 2635 | ctxt->userData); |
| 2636 | |
| 2637 | import = (xmlSchemaImport*) xmlMalloc(sizeof(xmlSchemaImport)); |
| 2638 | if (import == NULL) { |
| 2639 | xmlSchemaPErrMemory(NULL, "allocating imported schema", |
| 2640 | NULL); |
| 2641 | xmlSchemaFreeParserCtxt(newctxt); |
| 2642 | return (NULL); |
| 2643 | } |
| 2644 | |
| 2645 | memset(import, 0, sizeof(xmlSchemaImport)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2646 | import->schemaLocation = xmlDictLookup(ctxt->dict, schemaLocation, -1); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2647 | import->schema = xmlSchemaParse(newctxt); |
| 2648 | |
| 2649 | if (import->schema == NULL) { |
| 2650 | /* FIXME use another error enum here ? */ |
| 2651 | xmlSchemaPErr(ctxt, NULL, XML_SCHEMAS_ERR_INTERNAL, |
| 2652 | "failed to import schema at location %s\n", |
| 2653 | schemaLocation, NULL); |
| 2654 | |
| 2655 | xmlSchemaFreeParserCtxt(newctxt); |
| 2656 | if (import->schemaLocation != NULL) |
| 2657 | xmlFree((xmlChar *)import->schemaLocation); |
| 2658 | xmlFree(import); |
| 2659 | return NULL; |
| 2660 | } |
| 2661 | |
| 2662 | xmlSchemaFreeParserCtxt(newctxt); |
| 2663 | return import; |
| 2664 | } |
| 2665 | |
| 2666 | |
| 2667 | /** |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2668 | * xmlSchemaParseImport: |
| 2669 | * @ctxt: a schema validation context |
| 2670 | * @schema: the schema being built |
| 2671 | * @node: a subtree containing XML Schema informations |
| 2672 | * |
| 2673 | * parse a XML schema Import definition |
| 2674 | * *WARNING* this interface is highly subject to change |
| 2675 | * |
| 2676 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2677 | * 1 in case of success. |
| 2678 | */ |
| 2679 | static int |
| 2680 | xmlSchemaParseImport(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2681 | xmlNodePtr node) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2682 | { |
| 2683 | xmlNodePtr child = NULL; |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2684 | xmlSchemaImportPtr import = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2685 | const xmlChar *namespace; |
| 2686 | const xmlChar *schemaLocation; |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2687 | const xmlChar *previous; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2688 | xmlURIPtr check; |
| 2689 | |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2690 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2691 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2692 | return (-1); |
| 2693 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2694 | namespace = xmlSchemaGetProp(ctxt, node, "namespace"); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2695 | if (namespace != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2696 | check = xmlParseURI((const char *) namespace); |
| 2697 | if (check == NULL) { |
| 2698 | xmlSchemaPErr2(ctxt, node, child, |
| 2699 | XML_SCHEMAP_IMPORT_NAMESPACE_NOT_URI, |
| 2700 | "Import namespace attribute is not an URI: %s\n", |
| 2701 | namespace, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2702 | return (-1); |
| 2703 | } else { |
| 2704 | xmlFreeURI(check); |
| 2705 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2706 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2707 | schemaLocation = xmlSchemaGetProp(ctxt, node, "schemaLocation"); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2708 | if (schemaLocation != NULL) { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2709 | xmlChar *base = NULL; |
| 2710 | xmlChar *URI = NULL; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2711 | check = xmlParseURI((const char *) schemaLocation); |
| 2712 | if (check == NULL) { |
| 2713 | xmlSchemaPErr2(ctxt, node, child, |
| 2714 | XML_SCHEMAP_IMPORT_SCHEMA_NOT_URI, |
| 2715 | "Import schemaLocation attribute is not an URI: %s\n", |
| 2716 | schemaLocation, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2717 | return (-1); |
| 2718 | } else { |
| 2719 | xmlFreeURI(check); |
| 2720 | } |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2721 | base = xmlNodeGetBase(node->doc, node); |
| 2722 | if (base == NULL) { |
| 2723 | URI = xmlBuildURI(schemaLocation, node->doc->URL); |
| 2724 | } else { |
| 2725 | URI = xmlBuildURI(schemaLocation, base); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2726 | xmlFree(base); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2727 | } |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2728 | if (URI != NULL) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2729 | schemaLocation = xmlDictLookup(ctxt->dict, URI, -1); |
| 2730 | xmlFree(URI); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2731 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2732 | } |
| 2733 | if (schema->schemasImports == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2734 | schema->schemasImports = xmlHashCreate(10); |
| 2735 | if (schema->schemasImports == NULL) { |
| 2736 | xmlSchemaPErr2(ctxt, node, child, |
| 2737 | XML_SCHEMAP_FAILED_BUILD_IMPORT, |
| 2738 | "Internal: failed to build import table\n", |
| 2739 | NULL, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2740 | return (-1); |
| 2741 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2742 | } |
| 2743 | if (namespace == NULL) { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2744 | import = xmlHashLookup(schema->schemasImports, |
| 2745 | XML_SCHEMAS_DEFAULT_NAMESPACE); |
| 2746 | if (import != NULL) |
| 2747 | previous = import->schemaLocation; |
| 2748 | else |
| 2749 | previous = NULL; |
| 2750 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2751 | if (schemaLocation != NULL) { |
| 2752 | if (previous != NULL) { |
| 2753 | if (!xmlStrEqual(schemaLocation, previous)) { |
| 2754 | xmlSchemaPErr2(ctxt, node, child, |
| 2755 | XML_SCHEMAP_IMPORT_REDEFINE_NSNAME, |
| 2756 | "Redefining import for default namespace with a different URI: %s\n", |
| 2757 | schemaLocation, NULL); |
| 2758 | } |
| 2759 | } else { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2760 | import = xmlSchemaImportSchema(ctxt, schemaLocation); |
| 2761 | if (import == NULL) { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2762 | return (-1); |
| 2763 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2764 | xmlHashAddEntry(schema->schemasImports, |
| 2765 | XML_SCHEMAS_DEFAULT_NAMESPACE, |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2766 | import); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2767 | } |
| 2768 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2769 | } else { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2770 | import = xmlHashLookup(schema->schemasImports, namespace); |
| 2771 | if (import != NULL) |
| 2772 | previous = import->schemaLocation; |
| 2773 | else |
| 2774 | previous = NULL; |
| 2775 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2776 | if (schemaLocation != NULL) { |
| 2777 | if (previous != NULL) { |
| 2778 | if (!xmlStrEqual(schemaLocation, previous)) { |
| 2779 | xmlSchemaPErr2(ctxt, node, child, |
| 2780 | XML_SCHEMAP_IMPORT_REDEFINE_NSNAME, |
| 2781 | "Redefining import for namespace %s with a different URI: %s\n", |
| 2782 | namespace, schemaLocation); |
| 2783 | } |
| 2784 | } else { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2785 | import = xmlSchemaImportSchema(ctxt, schemaLocation); |
| 2786 | if (import == NULL) { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2787 | return (-1); |
| 2788 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2789 | xmlHashAddEntry(schema->schemasImports, |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2790 | namespace, import); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2791 | } |
| 2792 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2793 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2794 | |
| 2795 | child = node->children; |
| 2796 | while (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2797 | /* |
| 2798 | * the annotations here are simply discarded ... |
| 2799 | */ |
| 2800 | child = child->next; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2801 | } |
| 2802 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2803 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_IMPORT_CHILD, |
| 2804 | "Import has unexpected content\n", NULL, NULL); |
| 2805 | return (-1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2806 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2807 | return (1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | /** |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2811 | * xmlSchemaCleanupDoc: |
| 2812 | * @ctxt: a schema validation context |
| 2813 | * @node: the root of the document. |
| 2814 | * |
| 2815 | * removes unwanted nodes in a schemas document tree |
| 2816 | */ |
| 2817 | static void |
| 2818 | xmlSchemaCleanupDoc(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr root) |
| 2819 | { |
| 2820 | xmlNodePtr delete, cur; |
| 2821 | |
| 2822 | if ((ctxt == NULL) || (root == NULL)) return; |
| 2823 | |
| 2824 | /* |
| 2825 | * Remove all the blank text nodes |
| 2826 | */ |
| 2827 | delete = NULL; |
| 2828 | cur = root; |
| 2829 | while (cur != NULL) { |
| 2830 | if (delete != NULL) { |
| 2831 | xmlUnlinkNode(delete); |
| 2832 | xmlFreeNode(delete); |
| 2833 | delete = NULL; |
| 2834 | } |
| 2835 | if (cur->type == XML_TEXT_NODE) { |
| 2836 | if (IS_BLANK_NODE(cur)) { |
| 2837 | if (xmlNodeGetSpacePreserve(cur) != 1) { |
| 2838 | delete = cur; |
| 2839 | } |
| 2840 | } |
| 2841 | } else if ((cur->type != XML_ELEMENT_NODE) && |
| 2842 | (cur->type != XML_CDATA_SECTION_NODE)) { |
| 2843 | delete = cur; |
| 2844 | goto skip_children; |
| 2845 | } |
| 2846 | |
| 2847 | /* |
| 2848 | * Skip to next node |
| 2849 | */ |
| 2850 | if (cur->children != NULL) { |
| 2851 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 2852 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 2853 | (cur->children->type != XML_ENTITY_NODE)) { |
| 2854 | cur = cur->children; |
| 2855 | continue; |
| 2856 | } |
| 2857 | } |
| 2858 | skip_children: |
| 2859 | if (cur->next != NULL) { |
| 2860 | cur = cur->next; |
| 2861 | continue; |
| 2862 | } |
| 2863 | |
| 2864 | do { |
| 2865 | cur = cur->parent; |
| 2866 | if (cur == NULL) |
| 2867 | break; |
| 2868 | if (cur == root) { |
| 2869 | cur = NULL; |
| 2870 | break; |
| 2871 | } |
| 2872 | if (cur->next != NULL) { |
| 2873 | cur = cur->next; |
| 2874 | break; |
| 2875 | } |
| 2876 | } while (cur != NULL); |
| 2877 | } |
| 2878 | if (delete != NULL) { |
| 2879 | xmlUnlinkNode(delete); |
| 2880 | xmlFreeNode(delete); |
| 2881 | delete = NULL; |
| 2882 | } |
| 2883 | } |
| 2884 | |
| 2885 | /** |
| 2886 | * xmlSchemaParseSchemaTopLevel: |
| 2887 | * @ctxt: a schema validation context |
| 2888 | * @schema: the schemas |
| 2889 | * @nodes: the list of top level nodes |
| 2890 | * |
| 2891 | * Returns the internal XML Schema structure built from the resource or |
| 2892 | * NULL in case of error |
| 2893 | */ |
| 2894 | static void |
| 2895 | xmlSchemaParseSchemaTopLevel(xmlSchemaParserCtxtPtr ctxt, |
| 2896 | xmlSchemaPtr schema, xmlNodePtr nodes) |
| 2897 | { |
| 2898 | xmlNodePtr child; |
| 2899 | xmlSchemaAnnotPtr annot; |
| 2900 | |
| 2901 | if ((ctxt == NULL) || (schema == NULL) || (nodes == NULL)) |
| 2902 | return; |
| 2903 | |
| 2904 | child = nodes; |
| 2905 | while ((IS_SCHEMA(child, "include")) || |
| 2906 | (IS_SCHEMA(child, "import")) || |
| 2907 | (IS_SCHEMA(child, "redefine")) || |
| 2908 | (IS_SCHEMA(child, "annotation"))) { |
| 2909 | if (IS_SCHEMA(child, "annotation")) { |
| 2910 | annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2911 | if (schema->annot == NULL) |
| 2912 | schema->annot = annot; |
| 2913 | else |
| 2914 | xmlSchemaFreeAnnot(annot); |
| 2915 | } else if (IS_SCHEMA(child, "import")) { |
| 2916 | xmlSchemaParseImport(ctxt, schema, child); |
| 2917 | } else if (IS_SCHEMA(child, "include")) { |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 2918 | ctxt->includes++; |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2919 | xmlSchemaParseInclude(ctxt, schema, child); |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 2920 | ctxt->includes--; |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2921 | } else if (IS_SCHEMA(child, "redefine")) { |
| 2922 | TODO |
| 2923 | } |
| 2924 | child = child->next; |
| 2925 | } |
| 2926 | while (child != NULL) { |
| 2927 | if (IS_SCHEMA(child, "complexType")) { |
| 2928 | xmlSchemaParseComplexType(ctxt, schema, child); |
| 2929 | child = child->next; |
| 2930 | } else if (IS_SCHEMA(child, "simpleType")) { |
| 2931 | xmlSchemaParseSimpleType(ctxt, schema, child); |
| 2932 | child = child->next; |
| 2933 | } else if (IS_SCHEMA(child, "element")) { |
| 2934 | xmlSchemaParseElement(ctxt, schema, child, 1); |
| 2935 | child = child->next; |
| 2936 | } else if (IS_SCHEMA(child, "attribute")) { |
| 2937 | xmlSchemaParseAttribute(ctxt, schema, child); |
| 2938 | child = child->next; |
| 2939 | } else if (IS_SCHEMA(child, "attributeGroup")) { |
| 2940 | xmlSchemaParseAttributeGroup(ctxt, schema, child); |
| 2941 | child = child->next; |
| 2942 | } else if (IS_SCHEMA(child, "group")) { |
| 2943 | xmlSchemaParseGroup(ctxt, schema, child); |
| 2944 | child = child->next; |
| 2945 | } else if (IS_SCHEMA(child, "notation")) { |
| 2946 | xmlSchemaParseNotation(ctxt, schema, child); |
| 2947 | child = child->next; |
| 2948 | } else { |
| 2949 | xmlSchemaPErr2(ctxt, NULL, child, |
| 2950 | XML_SCHEMAP_UNKNOWN_SCHEMAS_CHILD, |
| 2951 | "Schemas: unexpected element %s here \n", |
| 2952 | child->name, NULL); |
| 2953 | child = child->next; |
| 2954 | } |
| 2955 | while (IS_SCHEMA(child, "annotation")) { |
| 2956 | annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2957 | if (schema->annot == NULL) |
| 2958 | schema->annot = annot; |
| 2959 | else |
| 2960 | xmlSchemaFreeAnnot(annot); |
| 2961 | child = child->next; |
| 2962 | } |
| 2963 | } |
| 2964 | } |
| 2965 | |
| 2966 | /** |
| 2967 | * xmlSchemaParseInclude: |
| 2968 | * @ctxt: a schema validation context |
| 2969 | * @schema: the schema being built |
| 2970 | * @node: a subtree containing XML Schema informations |
| 2971 | * |
| 2972 | * parse a XML schema Include definition |
| 2973 | * |
| 2974 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2975 | * 1 in case of success. |
| 2976 | */ |
| 2977 | static int |
| 2978 | xmlSchemaParseInclude(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 2979 | xmlNodePtr node) |
| 2980 | { |
| 2981 | xmlNodePtr child = NULL; |
| 2982 | const xmlChar *schemaLocation; |
| 2983 | xmlURIPtr check; |
| 2984 | xmlDocPtr doc; |
| 2985 | xmlNodePtr root; |
| 2986 | xmlSchemaIncludePtr include; |
| 2987 | |
| 2988 | |
| 2989 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2990 | return (-1); |
| 2991 | |
| 2992 | /* |
| 2993 | * Preliminary step, extract the URI-Reference for the include and |
| 2994 | * make an URI from the base. |
| 2995 | */ |
| 2996 | schemaLocation = xmlSchemaGetProp(ctxt, node, "schemaLocation"); |
| 2997 | if (schemaLocation != NULL) { |
| 2998 | xmlChar *base = NULL; |
| 2999 | xmlChar *URI = NULL; |
| 3000 | check = xmlParseURI((const char *) schemaLocation); |
| 3001 | if (check == NULL) { |
| 3002 | xmlSchemaPErr2(ctxt, node, child, |
| 3003 | XML_SCHEMAP_INCLUDE_SCHEMA_NOT_URI, |
| 3004 | "Include schemaLocation attribute is not an URI: %s\n", |
| 3005 | schemaLocation, NULL); |
| 3006 | return (-1); |
| 3007 | } else { |
| 3008 | xmlFreeURI(check); |
| 3009 | } |
| 3010 | base = xmlNodeGetBase(node->doc, node); |
| 3011 | if (base == NULL) { |
| 3012 | URI = xmlBuildURI(schemaLocation, node->doc->URL); |
| 3013 | } else { |
| 3014 | URI = xmlBuildURI(schemaLocation, base); |
| 3015 | xmlFree(base); |
| 3016 | } |
| 3017 | if (URI != NULL) { |
| 3018 | schemaLocation = xmlDictLookup(ctxt->dict, URI, -1); |
| 3019 | xmlFree(URI); |
| 3020 | } |
| 3021 | } else { |
| 3022 | xmlSchemaPErr2(ctxt, node, child, |
| 3023 | XML_SCHEMAP_INCLUDE_SCHEMA_NO_URI, |
| 3024 | "Include schemaLocation attribute missing\n", |
| 3025 | NULL, NULL); |
| 3026 | return (-1); |
| 3027 | } |
| 3028 | |
| 3029 | child = node->children; |
| 3030 | while (IS_SCHEMA(child, "annotation")) { |
| 3031 | /* |
| 3032 | * the annotations here are simply discarded ... |
| 3033 | */ |
| 3034 | child = child->next; |
| 3035 | } |
| 3036 | if (child != NULL) { |
| 3037 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_INCLUDE_CHILD, |
| 3038 | "Include has unexpected content\n", NULL, NULL); |
| 3039 | return (-1); |
| 3040 | } |
| 3041 | |
| 3042 | /* |
| 3043 | * First step is to parse the input document into an DOM/Infoset |
| 3044 | */ |
| 3045 | doc = xmlReadFile((const char *) schemaLocation, NULL, |
| 3046 | SCHEMAS_PARSE_OPTIONS); |
| 3047 | if (doc == NULL) { |
| 3048 | xmlSchemaPErr(ctxt, NULL, |
| 3049 | XML_SCHEMAP_FAILED_LOAD, |
| 3050 | "xmlSchemaParse: could not load %s\n", |
| 3051 | ctxt->URL, NULL); |
| 3052 | return(-1); |
| 3053 | } |
| 3054 | |
| 3055 | /* |
| 3056 | * Then extract the root of the schema |
| 3057 | */ |
| 3058 | root = xmlDocGetRootElement(doc); |
| 3059 | if (root == NULL) { |
| 3060 | xmlSchemaPErr(ctxt, (xmlNodePtr) doc, |
| 3061 | XML_SCHEMAP_NOROOT, |
| 3062 | "schemas %s has no root", schemaLocation, NULL); |
| 3063 | xmlFreeDoc(doc); |
| 3064 | return (-1); |
| 3065 | } |
| 3066 | |
| 3067 | /* |
| 3068 | * Remove all the blank text nodes |
| 3069 | */ |
| 3070 | xmlSchemaCleanupDoc(ctxt, root); |
| 3071 | |
| 3072 | /* |
| 3073 | * Check the schemas top level element |
| 3074 | */ |
| 3075 | if (!IS_SCHEMA(root, "schema")) { |
| 3076 | xmlSchemaPErr(ctxt, (xmlNodePtr) doc, |
| 3077 | XML_SCHEMAP_NOT_SCHEMA, |
| 3078 | "File %s is not a schemas", schemaLocation, NULL); |
| 3079 | xmlFreeDoc(doc); |
| 3080 | return (-1); |
| 3081 | } |
| 3082 | |
| 3083 | /* |
| 3084 | * register the include |
| 3085 | */ |
| 3086 | include = (xmlSchemaIncludePtr) xmlMalloc(sizeof(xmlSchemaInclude)); |
| 3087 | if (include == NULL) { |
| 3088 | xmlSchemaPErrMemory(ctxt, "allocating included schema", NULL); |
| 3089 | xmlFreeDoc(doc); |
| 3090 | return (-1); |
| 3091 | } |
| 3092 | |
| 3093 | memset(include, 0, sizeof(xmlSchemaInclude)); |
| 3094 | include->schemaLocation = xmlDictLookup(ctxt->dict, schemaLocation, -1); |
| 3095 | include->doc = doc; |
| 3096 | include->next = schema->includes; |
| 3097 | schema->includes = include; |
| 3098 | |
| 3099 | |
| 3100 | /* |
| 3101 | * parse the declarations in the included file like if they |
| 3102 | * were in the original file. |
| 3103 | */ |
| 3104 | xmlSchemaParseSchemaTopLevel(ctxt, schema, root->children); |
| 3105 | |
| 3106 | return (1); |
| 3107 | } |
| 3108 | |
| 3109 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3110 | * xmlSchemaParseChoice: |
| 3111 | * @ctxt: a schema validation context |
| 3112 | * @schema: the schema being built |
| 3113 | * @node: a subtree containing XML Schema informations |
| 3114 | * |
| 3115 | * parse a XML schema Choice definition |
| 3116 | * *WARNING* this interface is highly subject to change |
| 3117 | * |
| 3118 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 3119 | * 1 in case of success. |
| 3120 | */ |
| 3121 | static xmlSchemaTypePtr |
| 3122 | xmlSchemaParseChoice(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3123 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3124 | { |
| 3125 | xmlSchemaTypePtr type, subtype, last = NULL; |
| 3126 | xmlNodePtr child = NULL; |
| 3127 | xmlChar name[30]; |
| 3128 | |
| 3129 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3130 | return (NULL); |
| 3131 | |
| 3132 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3133 | snprintf((char *) name, 30, "choice %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3134 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3135 | if (type == NULL) |
| 3136 | return (NULL); |
| 3137 | type->node = node; |
| 3138 | type->type = XML_SCHEMA_TYPE_CHOICE; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3139 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3140 | type->minOccurs = xmlGetMinOccurs(ctxt, node); |
| 3141 | type->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
| 3142 | |
| 3143 | child = node->children; |
| 3144 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3145 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3146 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3147 | } |
| 3148 | while ((IS_SCHEMA(child, "element")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3149 | (IS_SCHEMA(child, "group")) || |
| 3150 | (IS_SCHEMA(child, "any")) || |
| 3151 | (IS_SCHEMA(child, "choice")) || |
| 3152 | (IS_SCHEMA(child, "sequence"))) { |
| 3153 | subtype = NULL; |
| 3154 | if (IS_SCHEMA(child, "element")) { |
| 3155 | subtype = (xmlSchemaTypePtr) |
| 3156 | xmlSchemaParseElement(ctxt, schema, child, 0); |
| 3157 | } else if (IS_SCHEMA(child, "group")) { |
| 3158 | subtype = xmlSchemaParseGroup(ctxt, schema, child); |
| 3159 | } else if (IS_SCHEMA(child, "any")) { |
| 3160 | subtype = xmlSchemaParseAny(ctxt, schema, child); |
| 3161 | } else if (IS_SCHEMA(child, "sequence")) { |
| 3162 | subtype = xmlSchemaParseSequence(ctxt, schema, child); |
| 3163 | } else if (IS_SCHEMA(child, "choice")) { |
| 3164 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 3165 | } |
| 3166 | if (subtype != NULL) { |
| 3167 | if (last == NULL) { |
| 3168 | type->subtypes = subtype; |
| 3169 | last = subtype; |
| 3170 | } else { |
| 3171 | last->next = subtype; |
| 3172 | last = subtype; |
| 3173 | } |
| 3174 | last->next = NULL; |
| 3175 | } |
| 3176 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3177 | } |
| 3178 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3179 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_CHOICE_CHILD, |
| 3180 | "Choice %s has unexpected content\n", type->name, |
| 3181 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3182 | } |
| 3183 | |
| 3184 | return (type); |
| 3185 | } |
| 3186 | |
| 3187 | /** |
| 3188 | * xmlSchemaParseSequence: |
| 3189 | * @ctxt: a schema validation context |
| 3190 | * @schema: the schema being built |
| 3191 | * @node: a subtree containing XML Schema informations |
| 3192 | * |
| 3193 | * parse a XML schema Sequence definition |
| 3194 | * *WARNING* this interface is highly subject to change |
| 3195 | * |
| 3196 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 3197 | * 1 in case of success. |
| 3198 | */ |
| 3199 | static xmlSchemaTypePtr |
| 3200 | xmlSchemaParseSequence(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3201 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3202 | { |
| 3203 | xmlSchemaTypePtr type, subtype, last = NULL; |
| 3204 | xmlNodePtr child = NULL; |
| 3205 | xmlChar name[30]; |
| 3206 | |
| 3207 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3208 | return (NULL); |
| 3209 | |
| 3210 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3211 | snprintf((char *) name, 30, "sequence %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3212 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3213 | if (type == NULL) |
| 3214 | return (NULL); |
| 3215 | type->node = node; |
| 3216 | type->type = XML_SCHEMA_TYPE_SEQUENCE; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3217 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3218 | type->minOccurs = xmlGetMinOccurs(ctxt, node); |
| 3219 | type->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
| 3220 | |
| 3221 | child = node->children; |
| 3222 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3223 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3224 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3225 | } |
| 3226 | while ((IS_SCHEMA(child, "element")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3227 | (IS_SCHEMA(child, "group")) || |
| 3228 | (IS_SCHEMA(child, "any")) || |
| 3229 | (IS_SCHEMA(child, "choice")) || |
| 3230 | (IS_SCHEMA(child, "sequence"))) { |
| 3231 | subtype = NULL; |
| 3232 | if (IS_SCHEMA(child, "element")) { |
| 3233 | subtype = (xmlSchemaTypePtr) |
| 3234 | xmlSchemaParseElement(ctxt, schema, child, 0); |
| 3235 | } else if (IS_SCHEMA(child, "group")) { |
| 3236 | subtype = xmlSchemaParseGroup(ctxt, schema, child); |
| 3237 | } else if (IS_SCHEMA(child, "any")) { |
| 3238 | subtype = xmlSchemaParseAny(ctxt, schema, child); |
| 3239 | } else if (IS_SCHEMA(child, "choice")) { |
| 3240 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 3241 | } else if (IS_SCHEMA(child, "sequence")) { |
| 3242 | subtype = xmlSchemaParseSequence(ctxt, schema, child); |
| 3243 | } |
| 3244 | if (subtype != NULL) { |
| 3245 | if (last == NULL) { |
| 3246 | type->subtypes = subtype; |
| 3247 | last = subtype; |
| 3248 | } else { |
| 3249 | last->next = subtype; |
| 3250 | last = subtype; |
| 3251 | } |
| 3252 | last->next = NULL; |
| 3253 | } |
| 3254 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3255 | } |
| 3256 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3257 | xmlSchemaPErr2(ctxt, node, child, |
| 3258 | XML_SCHEMAP_UNKNOWN_SEQUENCE_CHILD, |
| 3259 | "Sequence %s has unexpected content\n", type->name, |
| 3260 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3261 | } |
| 3262 | |
| 3263 | return (type); |
| 3264 | } |
| 3265 | |
| 3266 | /** |
| 3267 | * xmlSchemaParseRestriction: |
| 3268 | * @ctxt: a schema validation context |
| 3269 | * @schema: the schema being built |
| 3270 | * @node: a subtree containing XML Schema informations |
| 3271 | * @simple: is that part of a simple type. |
| 3272 | * |
| 3273 | * parse a XML schema Restriction definition |
| 3274 | * *WARNING* this interface is highly subject to change |
| 3275 | * |
| 3276 | * Returns the type definition or NULL in case of error |
| 3277 | */ |
| 3278 | static xmlSchemaTypePtr |
| 3279 | xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 3280 | xmlNodePtr node, int simple) |
| 3281 | { |
| 3282 | xmlSchemaTypePtr type, subtype; |
| 3283 | xmlSchemaFacetPtr facet, lastfacet = NULL; |
| 3284 | xmlNodePtr child = NULL; |
| 3285 | xmlChar name[30]; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3286 | const xmlChar *oldcontainer; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3287 | |
| 3288 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3289 | return (NULL); |
| 3290 | |
| 3291 | oldcontainer = ctxt->container; |
| 3292 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3293 | snprintf((char *) name, 30, "restriction %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3294 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3295 | if (type == NULL) |
| 3296 | return (NULL); |
| 3297 | type->node = node; |
| 3298 | type->type = XML_SCHEMA_TYPE_RESTRICTION; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3299 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3300 | type->base = xmlGetQNameProp(ctxt, node, "base", &(type->baseNs)); |
| 3301 | if ((!simple) && (type->base == NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3302 | xmlSchemaPErr2(ctxt, node, child, |
| 3303 | XML_SCHEMAP_RESTRICTION_NONAME_NOREF, |
| 3304 | "Restriction %s has no base\n", type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3305 | } |
| 3306 | ctxt->container = name; |
| 3307 | |
| 3308 | child = node->children; |
| 3309 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3310 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3311 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3312 | } |
| 3313 | subtype = NULL; |
| 3314 | |
| 3315 | if (IS_SCHEMA(child, "all")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3316 | subtype = (xmlSchemaTypePtr) |
| 3317 | xmlSchemaParseAll(ctxt, schema, child); |
| 3318 | child = child->next; |
| 3319 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3320 | } else if (IS_SCHEMA(child, "choice")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3321 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 3322 | child = child->next; |
| 3323 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3324 | } else if (IS_SCHEMA(child, "sequence")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3325 | subtype = (xmlSchemaTypePtr) |
| 3326 | xmlSchemaParseSequence(ctxt, schema, child); |
| 3327 | child = child->next; |
| 3328 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3329 | } else if (IS_SCHEMA(child, "group")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3330 | subtype = (xmlSchemaTypePtr) |
| 3331 | xmlSchemaParseGroup(ctxt, schema, child); |
| 3332 | child = child->next; |
| 3333 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3334 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3335 | if (IS_SCHEMA(child, "simpleType")) { |
| 3336 | subtype = (xmlSchemaTypePtr) |
| 3337 | xmlSchemaParseSimpleType(ctxt, schema, child); |
| 3338 | child = child->next; |
| 3339 | type->baseType = subtype; |
| 3340 | } |
| 3341 | /* |
| 3342 | * Facets |
| 3343 | */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3344 | while ((IS_SCHEMA(child, "minInclusive")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3345 | (IS_SCHEMA(child, "minExclusive")) || |
| 3346 | (IS_SCHEMA(child, "maxInclusive")) || |
| 3347 | (IS_SCHEMA(child, "maxExclusive")) || |
| 3348 | (IS_SCHEMA(child, "totalDigits")) || |
| 3349 | (IS_SCHEMA(child, "fractionDigits")) || |
| 3350 | (IS_SCHEMA(child, "pattern")) || |
| 3351 | (IS_SCHEMA(child, "enumeration")) || |
| 3352 | (IS_SCHEMA(child, "whiteSpace")) || |
| 3353 | (IS_SCHEMA(child, "length")) || |
| 3354 | (IS_SCHEMA(child, "maxLength")) || |
| 3355 | (IS_SCHEMA(child, "minLength"))) { |
| 3356 | facet = xmlSchemaParseFacet(ctxt, schema, child); |
| 3357 | if (facet != NULL) { |
| 3358 | if (lastfacet == NULL) { |
| 3359 | type->facets = facet; |
| 3360 | lastfacet = facet; |
| 3361 | } else { |
| 3362 | lastfacet->next = facet; |
| 3363 | lastfacet = facet; |
| 3364 | } |
| 3365 | lastfacet->next = NULL; |
| 3366 | } |
| 3367 | child = child->next; |
| 3368 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3369 | } |
| 3370 | child = xmlSchemaParseAttrDecls(ctxt, schema, child, type); |
| 3371 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3372 | xmlSchemaPErr2(ctxt, node, child, |
| 3373 | XML_SCHEMAP_UNKNOWN_RESTRICTION_CHILD, |
| 3374 | "Restriction %s has unexpected content\n", |
| 3375 | type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3376 | } |
| 3377 | ctxt->container = oldcontainer; |
| 3378 | return (type); |
| 3379 | } |
| 3380 | |
| 3381 | /** |
| 3382 | * xmlSchemaParseExtension: |
| 3383 | * @ctxt: a schema validation context |
| 3384 | * @schema: the schema being built |
| 3385 | * @node: a subtree containing XML Schema informations |
| 3386 | * |
| 3387 | * parse a XML schema Extension definition |
| 3388 | * *WARNING* this interface is highly subject to change |
| 3389 | * |
| 3390 | * Returns the type definition or NULL in case of error |
| 3391 | */ |
| 3392 | static xmlSchemaTypePtr |
| 3393 | xmlSchemaParseExtension(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3394 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3395 | { |
| 3396 | xmlSchemaTypePtr type, subtype; |
| 3397 | xmlNodePtr child = NULL; |
| 3398 | xmlChar name[30]; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3399 | const xmlChar *oldcontainer; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3400 | |
| 3401 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3402 | return (NULL); |
| 3403 | |
| 3404 | oldcontainer = ctxt->container; |
| 3405 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3406 | snprintf((char *) name, 30, "extension %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3407 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3408 | if (type == NULL) |
| 3409 | return (NULL); |
| 3410 | type->node = node; |
| 3411 | type->type = XML_SCHEMA_TYPE_EXTENSION; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3412 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3413 | ctxt->container = name; |
| 3414 | |
| 3415 | type->base = xmlGetQNameProp(ctxt, node, "base", &(type->baseNs)); |
| 3416 | if (type->base == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3417 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_EXTENSION_NO_BASE, |
| 3418 | "Extension %s has no base\n", type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3419 | } |
| 3420 | child = node->children; |
| 3421 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3422 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3423 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3424 | } |
| 3425 | subtype = NULL; |
| 3426 | |
| 3427 | if (IS_SCHEMA(child, "all")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3428 | subtype = xmlSchemaParseAll(ctxt, schema, child); |
| 3429 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3430 | } else if (IS_SCHEMA(child, "choice")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3431 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 3432 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3433 | } else if (IS_SCHEMA(child, "sequence")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3434 | subtype = xmlSchemaParseSequence(ctxt, schema, child); |
| 3435 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3436 | } else if (IS_SCHEMA(child, "group")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3437 | subtype = xmlSchemaParseGroup(ctxt, schema, child); |
| 3438 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3439 | } |
| 3440 | if (subtype != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3441 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3442 | child = xmlSchemaParseAttrDecls(ctxt, schema, child, type); |
| 3443 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3444 | xmlSchemaPErr2(ctxt, node, child, |
| 3445 | XML_SCHEMAP_UNKNOWN_EXTENSION_CHILD, |
| 3446 | "Extension %s has unexpected content\n", type->name, |
| 3447 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3448 | } |
| 3449 | ctxt->container = oldcontainer; |
| 3450 | return (type); |
| 3451 | } |
| 3452 | |
| 3453 | /** |
| 3454 | * xmlSchemaParseSimpleContent: |
| 3455 | * @ctxt: a schema validation context |
| 3456 | * @schema: the schema being built |
| 3457 | * @node: a subtree containing XML Schema informations |
| 3458 | * |
| 3459 | * parse a XML schema SimpleContent definition |
| 3460 | * *WARNING* this interface is highly subject to change |
| 3461 | * |
| 3462 | * Returns the type definition or NULL in case of error |
| 3463 | */ |
| 3464 | static xmlSchemaTypePtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3465 | xmlSchemaParseSimpleContent(xmlSchemaParserCtxtPtr ctxt, |
| 3466 | xmlSchemaPtr schema, xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3467 | { |
| 3468 | xmlSchemaTypePtr type, subtype; |
| 3469 | xmlNodePtr child = NULL; |
| 3470 | xmlChar name[30]; |
| 3471 | |
| 3472 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3473 | return (NULL); |
| 3474 | |
| 3475 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3476 | snprintf((char *) name, 30, "complexContent %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3477 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3478 | if (type == NULL) |
| 3479 | return (NULL); |
| 3480 | type->node = node; |
| 3481 | type->type = XML_SCHEMA_TYPE_SIMPLE_CONTENT; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3482 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3483 | |
| 3484 | child = node->children; |
| 3485 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3486 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3487 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3488 | } |
| 3489 | subtype = NULL; |
| 3490 | if (IS_SCHEMA(child, "restriction")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3491 | subtype = (xmlSchemaTypePtr) |
| 3492 | xmlSchemaParseRestriction(ctxt, schema, child, 0); |
| 3493 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3494 | } else if (IS_SCHEMA(child, "extension")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3495 | subtype = (xmlSchemaTypePtr) |
| 3496 | xmlSchemaParseExtension(ctxt, schema, child); |
| 3497 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3498 | } |
| 3499 | type->subtypes = subtype; |
| 3500 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3501 | xmlSchemaPErr2(ctxt, node, child, |
| 3502 | XML_SCHEMAP_UNKNOWN_SIMPLECONTENT_CHILD, |
| 3503 | "SimpleContent %s has unexpected content\n", |
| 3504 | type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3505 | } |
| 3506 | return (type); |
| 3507 | } |
| 3508 | |
| 3509 | /** |
| 3510 | * xmlSchemaParseComplexContent: |
| 3511 | * @ctxt: a schema validation context |
| 3512 | * @schema: the schema being built |
| 3513 | * @node: a subtree containing XML Schema informations |
| 3514 | * |
| 3515 | * parse a XML schema ComplexContent definition |
| 3516 | * *WARNING* this interface is highly subject to change |
| 3517 | * |
| 3518 | * Returns the type definition or NULL in case of error |
| 3519 | */ |
| 3520 | static xmlSchemaTypePtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3521 | xmlSchemaParseComplexContent(xmlSchemaParserCtxtPtr ctxt, |
| 3522 | xmlSchemaPtr schema, xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3523 | { |
| 3524 | xmlSchemaTypePtr type, subtype; |
| 3525 | xmlNodePtr child = NULL; |
| 3526 | xmlChar name[30]; |
| 3527 | |
| 3528 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3529 | return (NULL); |
| 3530 | |
| 3531 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3532 | snprintf((char *) name, 30, "complexContent %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3533 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3534 | if (type == NULL) |
| 3535 | return (NULL); |
| 3536 | type->node = node; |
| 3537 | type->type = XML_SCHEMA_TYPE_COMPLEX_CONTENT; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3538 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3539 | |
| 3540 | child = node->children; |
| 3541 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3542 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3543 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3544 | } |
| 3545 | subtype = NULL; |
| 3546 | if (IS_SCHEMA(child, "restriction")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3547 | subtype = (xmlSchemaTypePtr) |
| 3548 | xmlSchemaParseRestriction(ctxt, schema, child, 0); |
| 3549 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3550 | } else if (IS_SCHEMA(child, "extension")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3551 | subtype = (xmlSchemaTypePtr) |
| 3552 | xmlSchemaParseExtension(ctxt, schema, child); |
| 3553 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3554 | } |
| 3555 | type->subtypes = subtype; |
| 3556 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3557 | xmlSchemaPErr2(ctxt, node, child, |
| 3558 | XML_SCHEMAP_UNKNOWN_COMPLEXCONTENT_CHILD, |
| 3559 | "ComplexContent %s has unexpected content\n", |
| 3560 | type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3561 | } |
| 3562 | return (type); |
| 3563 | } |
| 3564 | |
| 3565 | /** |
| 3566 | * xmlSchemaParseComplexType: |
| 3567 | * @ctxt: a schema validation context |
| 3568 | * @schema: the schema being built |
| 3569 | * @node: a subtree containing XML Schema informations |
| 3570 | * |
| 3571 | * parse a XML schema Complex Type definition |
| 3572 | * *WARNING* this interface is highly subject to change |
| 3573 | * |
| 3574 | * Returns the type definition or NULL in case of error |
| 3575 | */ |
| 3576 | static xmlSchemaTypePtr |
| 3577 | xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 3578 | xmlNodePtr node) |
| 3579 | { |
| 3580 | xmlSchemaTypePtr type, subtype; |
| 3581 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3582 | const xmlChar *name; |
| 3583 | const xmlChar *oldcontainer; |
| 3584 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3585 | |
| 3586 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3587 | return (NULL); |
| 3588 | |
| 3589 | oldcontainer = ctxt->container; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3590 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3591 | if (name == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3592 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3593 | snprintf(buf, 99, "anontype %d", ctxt->counter++ + 1); |
| 3594 | name = (const xmlChar *)buf; |
| 3595 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
| 3596 | } else { |
| 3597 | const xmlChar *local, *ns; |
| 3598 | |
| 3599 | local = xmlSchemaGetNamespace(ctxt, schema, node, name, &ns); |
| 3600 | type = xmlSchemaAddType(ctxt, schema, local, ns); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3601 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3602 | if (type == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3603 | return (NULL); |
| 3604 | } |
| 3605 | type->node = node; |
| 3606 | type->type = XML_SCHEMA_TYPE_COMPLEX; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3607 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3608 | ctxt->container = name; |
| 3609 | |
| 3610 | child = node->children; |
| 3611 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3612 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3613 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3614 | } |
| 3615 | if (IS_SCHEMA(child, "simpleContent")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3616 | type->subtypes = xmlSchemaParseSimpleContent(ctxt, schema, child); |
| 3617 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3618 | } else if (IS_SCHEMA(child, "complexContent")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3619 | type->subtypes = xmlSchemaParseComplexContent(ctxt, schema, child); |
| 3620 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3621 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3622 | subtype = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3623 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3624 | if (IS_SCHEMA(child, "all")) { |
| 3625 | subtype = xmlSchemaParseAll(ctxt, schema, child); |
| 3626 | child = child->next; |
| 3627 | } else if (IS_SCHEMA(child, "choice")) { |
| 3628 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 3629 | child = child->next; |
| 3630 | } else if (IS_SCHEMA(child, "sequence")) { |
| 3631 | subtype = xmlSchemaParseSequence(ctxt, schema, child); |
| 3632 | child = child->next; |
| 3633 | } else if (IS_SCHEMA(child, "group")) { |
| 3634 | subtype = xmlSchemaParseGroup(ctxt, schema, child); |
| 3635 | child = child->next; |
| 3636 | } |
| 3637 | if (subtype != NULL) |
| 3638 | type->subtypes = subtype; |
| 3639 | child = xmlSchemaParseAttrDecls(ctxt, schema, child, type); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3640 | } |
| 3641 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3642 | xmlSchemaPErr2(ctxt, node, child, |
| 3643 | XML_SCHEMAP_UNKNOWN_COMPLEXTYPE_CHILD, |
| 3644 | "ComplexType %s has unexpected content\n", |
| 3645 | type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3646 | } |
| 3647 | ctxt->container = oldcontainer; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3648 | return (type); |
| 3649 | } |
| 3650 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3651 | /** |
| 3652 | * xmlSchemaParseSchema: |
| 3653 | * @ctxt: a schema validation context |
| 3654 | * @node: a subtree containing XML Schema informations |
| 3655 | * |
| 3656 | * parse a XML schema definition from a node set |
| 3657 | * *WARNING* this interface is highly subject to change |
| 3658 | * |
| 3659 | * Returns the internal XML Schema structure built from the resource or |
| 3660 | * NULL in case of error |
| 3661 | */ |
| 3662 | static xmlSchemaPtr |
| 3663 | xmlSchemaParseSchema(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node) |
| 3664 | { |
| 3665 | xmlSchemaPtr schema = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3666 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3667 | const xmlChar *val; |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 3668 | int nberrors; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3669 | |
| 3670 | if ((ctxt == NULL) || (node == NULL)) |
| 3671 | return (NULL); |
| 3672 | |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 3673 | nberrors = ctxt->nberrors; |
| 3674 | ctxt->nberrors = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3675 | if (IS_SCHEMA(node, "schema")) { |
| 3676 | schema = xmlSchemaNewSchema(ctxt); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3677 | if (schema == NULL) |
| 3678 | return (NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3679 | val = xmlSchemaGetProp(ctxt, node, "targetNamespace"); |
| 3680 | if (val != NULL) { |
| 3681 | schema->targetNamespace = xmlDictLookup(ctxt->dict, val, -1); |
| 3682 | } else { |
| 3683 | schema->targetNamespace = NULL; |
| 3684 | } |
| 3685 | schema->id = xmlSchemaGetProp(ctxt, node, "id"); |
| 3686 | schema->version = xmlSchemaGetProp(ctxt, node, "version"); |
| 3687 | val = xmlSchemaGetProp(ctxt, node, "elementFormDefault"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3688 | if (val != NULL) { |
| 3689 | if (xmlStrEqual(val, BAD_CAST "qualified")) |
| 3690 | schema->flags |= XML_SCHEMAS_QUALIF_ELEM; |
| 3691 | else if (!xmlStrEqual(val, BAD_CAST "unqualified")) { |
| 3692 | xmlSchemaPErr2(ctxt, node, child, |
| 3693 | XML_SCHEMAP_ELEMFORMDEFAULT_VALUE, |
| 3694 | "Invalid value %s for elementFormDefault\n", |
| 3695 | val, NULL); |
| 3696 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3697 | } else { |
| 3698 | schema->flags |= XML_SCHEMAS_QUALIF_ELEM; |
| 3699 | } |
| 3700 | val = xmlSchemaGetProp(ctxt, node, "attributeFormDefault"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3701 | if (val != NULL) { |
| 3702 | if (xmlStrEqual(val, BAD_CAST "qualified")) |
| 3703 | schema->flags |= XML_SCHEMAS_QUALIF_ATTR; |
| 3704 | else if (!xmlStrEqual(val, BAD_CAST "unqualified")) { |
| 3705 | xmlSchemaPErr2(ctxt, node, child, |
| 3706 | XML_SCHEMAP_ATTRFORMDEFAULT_VALUE, |
| 3707 | "Invalid value %s for attributeFormDefault\n", |
| 3708 | val, NULL); |
| 3709 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3710 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3711 | |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 3712 | xmlSchemaParseSchemaTopLevel(ctxt, schema, node->children); |
| 3713 | } else { |
| 3714 | xmlDocPtr doc; |
| 3715 | |
| 3716 | doc = node->doc; |
| 3717 | |
| 3718 | if ((doc != NULL) && (doc->URL != NULL)) { |
| 3719 | xmlSchemaPErr(ctxt, (xmlNodePtr) doc, |
| 3720 | XML_SCHEMAP_NOT_SCHEMA, |
| 3721 | "File %s is not a schemas", doc->URL, NULL); |
| 3722 | } else { |
| 3723 | xmlSchemaPErr(ctxt, (xmlNodePtr) doc, |
| 3724 | XML_SCHEMAP_NOT_SCHEMA, |
| 3725 | "File is not a schemas", NULL, NULL); |
| 3726 | } |
| 3727 | return(NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3728 | } |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 3729 | if (ctxt->nberrors != 0) { |
| 3730 | if (schema != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3731 | xmlSchemaFree(schema); |
| 3732 | schema = NULL; |
| 3733 | } |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 3734 | } |
| 3735 | ctxt->nberrors = nberrors; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3736 | #ifdef DEBUG |
| 3737 | if (schema == NULL) |
| 3738 | xmlGenericError(xmlGenericErrorContext, |
| 3739 | "xmlSchemaParse() failed\n"); |
| 3740 | #endif |
| 3741 | |
| 3742 | return (schema); |
| 3743 | } |
| 3744 | |
| 3745 | /************************************************************************ |
| 3746 | * * |
| 3747 | * Validating using Schemas * |
| 3748 | * * |
| 3749 | ************************************************************************/ |
| 3750 | |
| 3751 | /************************************************************************ |
| 3752 | * * |
| 3753 | * Reading/Writing Schemas * |
| 3754 | * * |
| 3755 | ************************************************************************/ |
| 3756 | |
| 3757 | /** |
| 3758 | * xmlSchemaNewParserCtxt: |
| 3759 | * @URL: the location of the schema |
| 3760 | * |
| 3761 | * Create an XML Schemas parse context for that file/resource expected |
| 3762 | * to contain an XML Schemas file. |
| 3763 | * |
| 3764 | * Returns the parser context or NULL in case of error |
| 3765 | */ |
| 3766 | xmlSchemaParserCtxtPtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3767 | xmlSchemaNewParserCtxt(const char *URL) |
| 3768 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3769 | xmlSchemaParserCtxtPtr ret; |
| 3770 | |
| 3771 | if (URL == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3772 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3773 | |
| 3774 | ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt)); |
| 3775 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3776 | xmlSchemaPErrMemory(NULL, "allocating schama parser context", |
| 3777 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3778 | return (NULL); |
| 3779 | } |
| 3780 | memset(ret, 0, sizeof(xmlSchemaParserCtxt)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3781 | ret->dict = xmlDictCreate(); |
| 3782 | ret->URL = xmlDictLookup(ret->dict, (const xmlChar *) URL, -1); |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 3783 | ret->includes = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3784 | return (ret); |
| 3785 | } |
| 3786 | |
| 3787 | /** |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 3788 | * xmlSchemaNewMemParserCtxt: |
| 3789 | * @buffer: a pointer to a char array containing the schemas |
| 3790 | * @size: the size of the array |
| 3791 | * |
| 3792 | * Create an XML Schemas parse context for that memory buffer expected |
| 3793 | * to contain an XML Schemas file. |
| 3794 | * |
| 3795 | * Returns the parser context or NULL in case of error |
| 3796 | */ |
| 3797 | xmlSchemaParserCtxtPtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3798 | xmlSchemaNewMemParserCtxt(const char *buffer, int size) |
| 3799 | { |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 3800 | xmlSchemaParserCtxtPtr ret; |
| 3801 | |
| 3802 | if ((buffer == NULL) || (size <= 0)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3803 | return (NULL); |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 3804 | |
| 3805 | ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt)); |
| 3806 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3807 | xmlSchemaPErrMemory(NULL, "allocating schama parser context", |
| 3808 | NULL); |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 3809 | return (NULL); |
| 3810 | } |
| 3811 | memset(ret, 0, sizeof(xmlSchemaParserCtxt)); |
| 3812 | ret->buffer = buffer; |
| 3813 | ret->size = size; |
William M. Brack | cf9eadf | 2003-12-25 13:24:05 +0000 | [diff] [blame] | 3814 | ret->dict = xmlDictCreate(); |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 3815 | return (ret); |
| 3816 | } |
| 3817 | |
| 3818 | /** |
Daniel Veillard | 9d75150 | 2003-10-29 13:21:47 +0000 | [diff] [blame] | 3819 | * xmlSchemaNewDocParserCtxt: |
| 3820 | * @doc: a preparsed document tree |
| 3821 | * |
| 3822 | * Create an XML Schemas parse context for that document. |
| 3823 | * NB. The document may be modified during the parsing process. |
| 3824 | * |
| 3825 | * Returns the parser context or NULL in case of error |
| 3826 | */ |
| 3827 | xmlSchemaParserCtxtPtr |
| 3828 | xmlSchemaNewDocParserCtxt(xmlDocPtr doc) |
| 3829 | { |
| 3830 | xmlSchemaParserCtxtPtr ret; |
| 3831 | |
| 3832 | if (doc == NULL) |
| 3833 | return (NULL); |
| 3834 | |
| 3835 | ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt)); |
| 3836 | if (ret == NULL) { |
| 3837 | xmlSchemaPErrMemory(NULL, "allocating schema parser context", |
| 3838 | NULL); |
| 3839 | return (NULL); |
| 3840 | } |
| 3841 | memset(ret, 0, sizeof(xmlSchemaParserCtxt)); |
| 3842 | ret->doc = doc; |
William M. Brack | cf9eadf | 2003-12-25 13:24:05 +0000 | [diff] [blame] | 3843 | ret->dict = xmlDictCreate(); |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 3844 | /* The application has responsibility for the document */ |
| 3845 | ret->preserve = 1; |
Daniel Veillard | 9d75150 | 2003-10-29 13:21:47 +0000 | [diff] [blame] | 3846 | |
| 3847 | return (ret); |
| 3848 | } |
| 3849 | |
| 3850 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3851 | * xmlSchemaFreeParserCtxt: |
| 3852 | * @ctxt: the schema parser context |
| 3853 | * |
| 3854 | * Free the resources associated to the schema parser context |
| 3855 | */ |
| 3856 | void |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3857 | xmlSchemaFreeParserCtxt(xmlSchemaParserCtxtPtr ctxt) |
| 3858 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3859 | if (ctxt == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3860 | return; |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 3861 | if (ctxt->doc != NULL && !ctxt->preserve) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3862 | xmlFreeDoc(ctxt->doc); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3863 | xmlDictFree(ctxt->dict); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3864 | xmlFree(ctxt); |
| 3865 | } |
| 3866 | |
| 3867 | /************************************************************************ |
| 3868 | * * |
| 3869 | * Building the content models * |
| 3870 | * * |
| 3871 | ************************************************************************/ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3872 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3873 | /** |
| 3874 | * xmlSchemaBuildAContentModel: |
| 3875 | * @type: the schema type definition |
| 3876 | * @ctxt: the schema parser context |
| 3877 | * @name: the element name whose content is being built |
| 3878 | * |
| 3879 | * Generate the automata sequence needed for that type |
| 3880 | */ |
| 3881 | static void |
| 3882 | xmlSchemaBuildAContentModel(xmlSchemaTypePtr type, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3883 | xmlSchemaParserCtxtPtr ctxt, |
| 3884 | const xmlChar * name) |
| 3885 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3886 | if (type == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3887 | xmlGenericError(xmlGenericErrorContext, |
| 3888 | "Found unexpected type = NULL in %s content model\n", |
| 3889 | name); |
| 3890 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3891 | } |
| 3892 | switch (type->type) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3893 | case XML_SCHEMA_TYPE_ANY: |
| 3894 | /* TODO : handle the namespace too */ |
| 3895 | /* TODO : make that a specific transition type */ |
| 3896 | TODO ctxt->state = |
| 3897 | xmlAutomataNewTransition(ctxt->am, ctxt->state, NULL, |
| 3898 | BAD_CAST "*", NULL); |
| 3899 | break; |
| 3900 | case XML_SCHEMA_TYPE_ELEMENT:{ |
| 3901 | xmlSchemaElementPtr elem = (xmlSchemaElementPtr) type; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3902 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3903 | /* TODO : handle the namespace too */ |
| 3904 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3905 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3906 | if (elem->maxOccurs >= UNBOUNDED) { |
| 3907 | if (elem->minOccurs > 1) { |
| 3908 | xmlAutomataStatePtr tmp; |
| 3909 | int counter; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3910 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3911 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
| 3912 | oldstate, |
| 3913 | NULL); |
| 3914 | oldstate = ctxt->state; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3915 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3916 | counter = xmlAutomataNewCounter(ctxt->am, |
| 3917 | elem->minOccurs - |
| 3918 | 1, UNBOUNDED); |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3919 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3920 | if (elem->refDecl != NULL) { |
| 3921 | xmlSchemaBuildAContentModel((xmlSchemaTypePtr) |
| 3922 | elem->refDecl, |
| 3923 | ctxt, |
| 3924 | elem->refDecl-> |
| 3925 | name); |
| 3926 | } else { |
| 3927 | ctxt->state = |
| 3928 | xmlAutomataNewTransition(ctxt->am, |
| 3929 | ctxt->state, NULL, |
| 3930 | elem->name, type); |
| 3931 | } |
| 3932 | tmp = ctxt->state; |
| 3933 | xmlAutomataNewCountedTrans(ctxt->am, tmp, oldstate, |
| 3934 | counter); |
| 3935 | ctxt->state = |
| 3936 | xmlAutomataNewCounterTrans(ctxt->am, tmp, NULL, |
| 3937 | counter); |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3938 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3939 | } else { |
| 3940 | if (elem->refDecl != NULL) { |
| 3941 | xmlSchemaBuildAContentModel((xmlSchemaTypePtr) |
| 3942 | elem->refDecl, |
| 3943 | ctxt, |
| 3944 | elem->refDecl-> |
| 3945 | name); |
| 3946 | } else { |
| 3947 | ctxt->state = |
| 3948 | xmlAutomataNewTransition(ctxt->am, |
| 3949 | ctxt->state, NULL, |
| 3950 | elem->name, type); |
| 3951 | } |
| 3952 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
| 3953 | oldstate); |
| 3954 | if (elem->minOccurs == 0) { |
| 3955 | /* basically an elem* */ |
| 3956 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 3957 | ctxt->state); |
| 3958 | } |
| 3959 | } |
| 3960 | } else if ((elem->maxOccurs > 1) || (elem->minOccurs > 1)) { |
| 3961 | xmlAutomataStatePtr tmp; |
| 3962 | int counter; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3963 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3964 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
| 3965 | oldstate, NULL); |
| 3966 | oldstate = ctxt->state; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3967 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3968 | counter = xmlAutomataNewCounter(ctxt->am, |
| 3969 | elem->minOccurs - 1, |
| 3970 | elem->maxOccurs - 1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3971 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3972 | if (elem->refDecl != NULL) { |
| 3973 | xmlSchemaBuildAContentModel((xmlSchemaTypePtr) |
| 3974 | elem->refDecl, ctxt, |
| 3975 | elem->refDecl->name); |
| 3976 | } else { |
| 3977 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
| 3978 | ctxt->state, |
| 3979 | NULL, |
| 3980 | elem->name, |
| 3981 | type); |
| 3982 | } |
| 3983 | tmp = ctxt->state; |
| 3984 | xmlAutomataNewCountedTrans(ctxt->am, tmp, oldstate, |
| 3985 | counter); |
| 3986 | ctxt->state = xmlAutomataNewCounterTrans(ctxt->am, tmp, |
| 3987 | NULL, |
| 3988 | counter); |
| 3989 | if (elem->minOccurs == 0) { |
| 3990 | /* basically an elem? */ |
| 3991 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 3992 | ctxt->state); |
| 3993 | } |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 3994 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3995 | } else { |
| 3996 | if (elem->refDecl != NULL) { |
| 3997 | xmlSchemaBuildAContentModel((xmlSchemaTypePtr) |
| 3998 | elem->refDecl, ctxt, |
| 3999 | elem->refDecl->name); |
| 4000 | } else { |
| 4001 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
| 4002 | ctxt->state, |
| 4003 | NULL, |
| 4004 | elem->name, |
| 4005 | type); |
| 4006 | } |
| 4007 | if (elem->minOccurs == 0) { |
| 4008 | /* basically an elem? */ |
| 4009 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 4010 | ctxt->state); |
| 4011 | } |
| 4012 | } |
| 4013 | break; |
| 4014 | } |
| 4015 | case XML_SCHEMA_TYPE_SEQUENCE:{ |
| 4016 | xmlSchemaTypePtr subtypes; |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4017 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4018 | /* |
| 4019 | * If max and min occurances are default (1) then |
| 4020 | * simply iterate over the subtypes |
| 4021 | */ |
| 4022 | if ((type->minOccurs == 1) && (type->maxOccurs == 1)) { |
| 4023 | subtypes = type->subtypes; |
| 4024 | while (subtypes != NULL) { |
| 4025 | xmlSchemaBuildAContentModel(subtypes, ctxt, name); |
| 4026 | subtypes = subtypes->next; |
| 4027 | } |
| 4028 | } else { |
| 4029 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4030 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4031 | if (type->maxOccurs >= UNBOUNDED) { |
| 4032 | if (type->minOccurs > 1) { |
| 4033 | xmlAutomataStatePtr tmp; |
| 4034 | int counter; |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4035 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4036 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
| 4037 | oldstate, |
| 4038 | NULL); |
| 4039 | oldstate = ctxt->state; |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4040 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4041 | counter = xmlAutomataNewCounter(ctxt->am, |
| 4042 | type-> |
| 4043 | minOccurs - 1, |
| 4044 | UNBOUNDED); |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4045 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4046 | subtypes = type->subtypes; |
| 4047 | while (subtypes != NULL) { |
| 4048 | xmlSchemaBuildAContentModel(subtypes, ctxt, |
| 4049 | name); |
| 4050 | subtypes = subtypes->next; |
| 4051 | } |
| 4052 | tmp = ctxt->state; |
| 4053 | xmlAutomataNewCountedTrans(ctxt->am, tmp, |
| 4054 | oldstate, counter); |
| 4055 | ctxt->state = |
| 4056 | xmlAutomataNewCounterTrans(ctxt->am, tmp, |
| 4057 | NULL, counter); |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4058 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4059 | } else { |
| 4060 | subtypes = type->subtypes; |
| 4061 | while (subtypes != NULL) { |
| 4062 | xmlSchemaBuildAContentModel(subtypes, ctxt, |
| 4063 | name); |
| 4064 | subtypes = subtypes->next; |
| 4065 | } |
| 4066 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
| 4067 | oldstate); |
| 4068 | if (type->minOccurs == 0) { |
| 4069 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 4070 | ctxt->state); |
| 4071 | } |
| 4072 | } |
| 4073 | } else if ((type->maxOccurs > 1) |
| 4074 | || (type->minOccurs > 1)) { |
| 4075 | xmlAutomataStatePtr tmp; |
| 4076 | int counter; |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4077 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4078 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
| 4079 | oldstate, |
| 4080 | NULL); |
| 4081 | oldstate = ctxt->state; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4082 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4083 | counter = xmlAutomataNewCounter(ctxt->am, |
| 4084 | type->minOccurs - |
| 4085 | 1, |
| 4086 | type->maxOccurs - |
| 4087 | 1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4088 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4089 | subtypes = type->subtypes; |
| 4090 | while (subtypes != NULL) { |
| 4091 | xmlSchemaBuildAContentModel(subtypes, ctxt, |
| 4092 | name); |
| 4093 | subtypes = subtypes->next; |
| 4094 | } |
| 4095 | tmp = ctxt->state; |
| 4096 | xmlAutomataNewCountedTrans(ctxt->am, tmp, oldstate, |
| 4097 | counter); |
| 4098 | ctxt->state = |
| 4099 | xmlAutomataNewCounterTrans(ctxt->am, tmp, NULL, |
| 4100 | counter); |
| 4101 | if (type->minOccurs == 0) { |
| 4102 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 4103 | ctxt->state); |
| 4104 | } |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 4105 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4106 | } else { |
| 4107 | subtypes = type->subtypes; |
| 4108 | while (subtypes != NULL) { |
| 4109 | xmlSchemaBuildAContentModel(subtypes, ctxt, |
| 4110 | name); |
| 4111 | subtypes = subtypes->next; |
| 4112 | } |
| 4113 | if (type->minOccurs == 0) { |
| 4114 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 4115 | ctxt->state); |
| 4116 | } |
| 4117 | } |
| 4118 | } |
| 4119 | break; |
| 4120 | } |
| 4121 | case XML_SCHEMA_TYPE_CHOICE:{ |
| 4122 | xmlSchemaTypePtr subtypes; |
| 4123 | xmlAutomataStatePtr start, end; |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 4124 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4125 | start = ctxt->state; |
| 4126 | end = xmlAutomataNewState(ctxt->am); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4127 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4128 | /* |
| 4129 | * iterate over the subtypes and remerge the end with an |
| 4130 | * epsilon transition |
| 4131 | */ |
| 4132 | if (type->maxOccurs == 1) { |
| 4133 | subtypes = type->subtypes; |
| 4134 | while (subtypes != NULL) { |
| 4135 | ctxt->state = start; |
| 4136 | xmlSchemaBuildAContentModel(subtypes, ctxt, name); |
| 4137 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, end); |
| 4138 | subtypes = subtypes->next; |
| 4139 | } |
| 4140 | } else { |
| 4141 | int counter; |
| 4142 | xmlAutomataStatePtr hop; |
| 4143 | int maxOccurs = type->maxOccurs == UNBOUNDED ? |
| 4144 | UNBOUNDED : type->maxOccurs - 1; |
| 4145 | int minOccurs = |
| 4146 | type->minOccurs < 1 ? 0 : type->minOccurs - 1; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4147 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4148 | /* |
| 4149 | * use a counter to keep track of the number of transtions |
| 4150 | * which went through the choice. |
| 4151 | */ |
| 4152 | counter = |
| 4153 | xmlAutomataNewCounter(ctxt->am, minOccurs, |
| 4154 | maxOccurs); |
| 4155 | hop = xmlAutomataNewState(ctxt->am); |
Daniel Veillard | 6231e84 | 2002-04-18 11:54:04 +0000 | [diff] [blame] | 4156 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4157 | subtypes = type->subtypes; |
| 4158 | while (subtypes != NULL) { |
| 4159 | ctxt->state = start; |
| 4160 | xmlSchemaBuildAContentModel(subtypes, ctxt, name); |
| 4161 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, hop); |
| 4162 | subtypes = subtypes->next; |
| 4163 | } |
| 4164 | xmlAutomataNewCountedTrans(ctxt->am, hop, start, |
| 4165 | counter); |
| 4166 | xmlAutomataNewCounterTrans(ctxt->am, hop, end, |
| 4167 | counter); |
| 4168 | } |
| 4169 | if (type->minOccurs == 0) { |
| 4170 | xmlAutomataNewEpsilon(ctxt->am, start, end); |
| 4171 | } |
| 4172 | ctxt->state = end; |
| 4173 | break; |
| 4174 | } |
| 4175 | case XML_SCHEMA_TYPE_ALL:{ |
| 4176 | xmlAutomataStatePtr start; |
| 4177 | xmlSchemaTypePtr subtypes; |
| 4178 | xmlSchemaElementPtr elem = (xmlSchemaElementPtr) type; |
| 4179 | int lax; |
| 4180 | |
| 4181 | subtypes = type->subtypes; |
| 4182 | if (subtypes == NULL) |
| 4183 | break; |
| 4184 | start = ctxt->state; |
| 4185 | while (subtypes != NULL) { |
| 4186 | ctxt->state = start; |
| 4187 | elem = (xmlSchemaElementPtr) subtypes; |
| 4188 | |
| 4189 | /* TODO : handle the namespace too */ |
| 4190 | if ((elem->minOccurs == 1) && (elem->maxOccurs == 1)) { |
| 4191 | xmlAutomataNewOnceTrans(ctxt->am, ctxt->state, |
| 4192 | ctxt->state, elem->name, 1, |
| 4193 | 1, subtypes); |
| 4194 | } else { |
| 4195 | xmlAutomataNewCountTrans(ctxt->am, ctxt->state, |
| 4196 | ctxt->state, elem->name, |
| 4197 | elem->minOccurs, |
| 4198 | elem->maxOccurs, |
| 4199 | subtypes); |
| 4200 | } |
| 4201 | subtypes = subtypes->next; |
| 4202 | } |
| 4203 | lax = type->minOccurs == 0; |
| 4204 | ctxt->state = |
| 4205 | xmlAutomataNewAllTrans(ctxt->am, ctxt->state, NULL, |
| 4206 | lax); |
| 4207 | break; |
| 4208 | } |
| 4209 | case XML_SCHEMA_TYPE_RESTRICTION: |
| 4210 | if (type->subtypes != NULL) |
| 4211 | xmlSchemaBuildAContentModel(type->subtypes, ctxt, name); |
| 4212 | break; |
| 4213 | case XML_SCHEMA_TYPE_EXTENSION: |
| 4214 | if (type->baseType != NULL) { |
| 4215 | xmlSchemaTypePtr subtypes; |
| 4216 | |
| 4217 | xmlSchemaBuildAContentModel(type->baseType, ctxt, name); |
| 4218 | subtypes = type->subtypes; |
| 4219 | while (subtypes != NULL) { |
| 4220 | xmlSchemaBuildAContentModel(subtypes, ctxt, name); |
| 4221 | subtypes = subtypes->next; |
| 4222 | } |
| 4223 | } else if (type->subtypes != NULL) |
| 4224 | xmlSchemaBuildAContentModel(type->subtypes, ctxt, name); |
| 4225 | break; |
| 4226 | case XML_SCHEMA_TYPE_GROUP: |
| 4227 | if (type->subtypes == NULL) { |
| 4228 | } |
| 4229 | case XML_SCHEMA_TYPE_COMPLEX: |
| 4230 | case XML_SCHEMA_TYPE_COMPLEX_CONTENT: |
| 4231 | if (type->subtypes != NULL) |
| 4232 | xmlSchemaBuildAContentModel(type->subtypes, ctxt, name); |
| 4233 | break; |
| 4234 | default: |
| 4235 | xmlGenericError(xmlGenericErrorContext, |
| 4236 | "Found unexpected type %d in %s content model\n", |
| 4237 | type->type, name); |
| 4238 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4239 | } |
| 4240 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4241 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4242 | /** |
| 4243 | * xmlSchemaBuildContentModel: |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4244 | * @elem: the element |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4245 | * @ctxt: the schema parser context |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4246 | * @name: the element name |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4247 | * |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4248 | * Builds the content model of the element. |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4249 | */ |
| 4250 | static void |
| 4251 | xmlSchemaBuildContentModel(xmlSchemaElementPtr elem, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4252 | xmlSchemaParserCtxtPtr ctxt, |
| 4253 | const xmlChar * name) |
| 4254 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4255 | xmlAutomataStatePtr start; |
| 4256 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4257 | if (elem->contModel != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4258 | return; |
Daniel Veillard | 88c5891 | 2002-04-23 07:12:20 +0000 | [diff] [blame] | 4259 | if (elem->subtypes == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4260 | elem->contentType = XML_SCHEMA_CONTENT_ANY; |
| 4261 | return; |
Daniel Veillard | 88c5891 | 2002-04-23 07:12:20 +0000 | [diff] [blame] | 4262 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4263 | if (elem->subtypes->type != XML_SCHEMA_TYPE_COMPLEX) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4264 | return; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 4265 | if ((elem->subtypes->contentType == XML_SCHEMA_CONTENT_BASIC) || |
| 4266 | (elem->subtypes->contentType == XML_SCHEMA_CONTENT_SIMPLE)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4267 | return; |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 4268 | |
| 4269 | #ifdef DEBUG_CONTENT |
| 4270 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4271 | "Building content model for %s\n", name); |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 4272 | #endif |
| 4273 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4274 | ctxt->am = xmlNewAutomata(); |
| 4275 | if (ctxt->am == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4276 | xmlGenericError(xmlGenericErrorContext, |
| 4277 | "Cannot create automata for elem %s\n", name); |
| 4278 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4279 | } |
| 4280 | start = ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 4281 | xmlSchemaBuildAContentModel(elem->subtypes, ctxt, name); |
| 4282 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
Daniel Veillard | 4402ab4 | 2002-09-12 16:02:56 +0000 | [diff] [blame] | 4283 | elem->contModel = xmlAutomataCompile(ctxt->am); |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4284 | if (elem->contModel == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4285 | xmlSchemaPErr(ctxt, elem->node, XML_SCHEMAS_ERR_INTERNAL, |
| 4286 | "failed to compile %s content model\n", name, NULL); |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4287 | } else if (xmlRegexpIsDeterminist(elem->contModel) != 1) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4288 | xmlSchemaPErr(ctxt, elem->node, XML_SCHEMAS_ERR_NOTDETERMINIST, |
| 4289 | "Content model of %s is not determinist:\n", name, |
| 4290 | NULL); |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 4291 | } else { |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 4292 | #ifdef DEBUG_CONTENT_REGEXP |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4293 | xmlGenericError(xmlGenericErrorContext, |
| 4294 | "Content model of %s:\n", name); |
| 4295 | xmlRegexpPrint(stderr, elem->contModel); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4296 | #endif |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 4297 | } |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4298 | ctxt->state = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4299 | xmlFreeAutomata(ctxt->am); |
| 4300 | ctxt->am = NULL; |
| 4301 | } |
| 4302 | |
| 4303 | /** |
| 4304 | * xmlSchemaRefFixupCallback: |
| 4305 | * @elem: the schema element context |
| 4306 | * @ctxt: the schema parser context |
| 4307 | * |
| 4308 | * Free the resources associated to the schema parser context |
| 4309 | */ |
| 4310 | static void |
| 4311 | xmlSchemaRefFixupCallback(xmlSchemaElementPtr elem, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4312 | xmlSchemaParserCtxtPtr ctxt, |
| 4313 | const xmlChar * name, |
| 4314 | const xmlChar * context ATTRIBUTE_UNUSED, |
| 4315 | const xmlChar * namespace ATTRIBUTE_UNUSED) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4316 | { |
| 4317 | if ((ctxt == NULL) || (elem == NULL)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4318 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4319 | if (elem->ref != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4320 | xmlSchemaElementPtr elemDecl; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4321 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4322 | if (elem->subtypes != NULL) { |
| 4323 | xmlSchemaPErr(ctxt, elem->node, |
| 4324 | XML_SCHEMAP_INVALID_REF_AND_SUBTYPE, |
| 4325 | "Schemas: element %s have both ref and subtype\n", |
| 4326 | name, NULL); |
| 4327 | return; |
| 4328 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 4329 | elemDecl = xmlSchemaGetElem(ctxt->schema, elem->ref, elem->refNs, 0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4330 | |
| 4331 | if (elemDecl == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4332 | xmlSchemaPErr(ctxt, elem->node, XML_SCHEMAP_UNKNOWN_REF, |
| 4333 | "Schemas: element %s ref to %s not found\n", |
| 4334 | name, elem->ref); |
| 4335 | return; |
| 4336 | } |
| 4337 | elem->refDecl = elemDecl; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4338 | } else if (elem->namedType != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4339 | xmlSchemaTypePtr typeDecl; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4340 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4341 | if (elem->subtypes != NULL) { |
| 4342 | xmlSchemaPErr(ctxt, elem->node, XML_SCHEMAP_TYPE_AND_SUBTYPE, |
| 4343 | "Schemas: element %s have both type and subtype\n", |
| 4344 | name, NULL); |
| 4345 | return; |
| 4346 | } |
| 4347 | typeDecl = xmlSchemaGetType(ctxt->schema, elem->namedType, |
| 4348 | elem->namedTypeNs); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4349 | |
| 4350 | if (typeDecl == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4351 | xmlSchemaPErr(ctxt, elem->node, XML_SCHEMAP_UNKNOWN_TYPE, |
| 4352 | "Schemas: element %s type %s not found\n", name, |
| 4353 | elem->namedType); |
| 4354 | return; |
| 4355 | } |
| 4356 | elem->subtypes = typeDecl; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4357 | } |
| 4358 | } |
| 4359 | |
| 4360 | /** |
| 4361 | * xmlSchemaTypeFixup: |
| 4362 | * @typeDecl: the schema type definition |
| 4363 | * @ctxt: the schema parser context |
| 4364 | * |
| 4365 | * Fixes the content model of the type. |
| 4366 | */ |
| 4367 | static void |
| 4368 | xmlSchemaTypeFixup(xmlSchemaTypePtr typeDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4369 | xmlSchemaParserCtxtPtr ctxt, const xmlChar * name) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4370 | { |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 4371 | if (typeDecl == NULL) |
| 4372 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4373 | if (name == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4374 | name = typeDecl->name; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4375 | if (typeDecl->contentType == XML_SCHEMA_CONTENT_UNKNOWN) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4376 | switch (typeDecl->type) { |
| 4377 | case XML_SCHEMA_TYPE_SIMPLE_CONTENT:{ |
| 4378 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, NULL); |
| 4379 | if (typeDecl->subtypes != NULL) |
| 4380 | typeDecl->contentType = |
| 4381 | typeDecl->subtypes->contentType; |
| 4382 | break; |
| 4383 | } |
| 4384 | case XML_SCHEMA_TYPE_RESTRICTION:{ |
| 4385 | if (typeDecl->subtypes != NULL) |
| 4386 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4387 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4388 | if (typeDecl->base != NULL) { |
| 4389 | xmlSchemaTypePtr baseType; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4390 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4391 | baseType = |
| 4392 | xmlSchemaGetType(ctxt->schema, typeDecl->base, |
| 4393 | typeDecl->baseNs); |
| 4394 | if (baseType == NULL) { |
| 4395 | xmlSchemaPErr(ctxt, typeDecl->node, |
| 4396 | XML_SCHEMAP_UNKNOWN_BASE_TYPE, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4397 | "Schemas: type %s base type %s not found\n", |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4398 | name, typeDecl->base); |
| 4399 | } |
| 4400 | typeDecl->baseType = baseType; |
| 4401 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 4402 | if (typeDecl->subtypes == NULL) |
| 4403 | if (typeDecl->baseType != NULL) |
| 4404 | typeDecl->contentType = |
| 4405 | typeDecl->baseType->contentType; |
| 4406 | else |
| 4407 | /* 1.1.1 */ |
| 4408 | typeDecl->contentType = XML_SCHEMA_CONTENT_EMPTY; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4409 | else if ((typeDecl->subtypes->subtypes == NULL) && |
| 4410 | ((typeDecl->subtypes->type == |
| 4411 | XML_SCHEMA_TYPE_ALL) |
| 4412 | || (typeDecl->subtypes->type == |
| 4413 | XML_SCHEMA_TYPE_SEQUENCE))) |
| 4414 | /* 1.1.2 */ |
| 4415 | typeDecl->contentType = XML_SCHEMA_CONTENT_EMPTY; |
| 4416 | else if ((typeDecl->subtypes->type == |
| 4417 | XML_SCHEMA_TYPE_CHOICE) |
| 4418 | && (typeDecl->subtypes->subtypes == NULL)) |
| 4419 | /* 1.1.3 */ |
| 4420 | typeDecl->contentType = XML_SCHEMA_CONTENT_EMPTY; |
| 4421 | else { |
| 4422 | /* 1.2 and 2.X are applied at the other layer */ |
| 4423 | typeDecl->contentType = |
| 4424 | XML_SCHEMA_CONTENT_ELEMENTS; |
| 4425 | } |
| 4426 | break; |
| 4427 | } |
| 4428 | case XML_SCHEMA_TYPE_EXTENSION:{ |
| 4429 | xmlSchemaContentType explicitContentType; |
| 4430 | xmlSchemaTypePtr base; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4431 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4432 | if (typeDecl->base != NULL) { |
| 4433 | xmlSchemaTypePtr baseType; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4434 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4435 | baseType = |
| 4436 | xmlSchemaGetType(ctxt->schema, typeDecl->base, |
| 4437 | typeDecl->baseNs); |
| 4438 | if (baseType == NULL) { |
| 4439 | xmlSchemaPErr(ctxt, typeDecl->node, |
| 4440 | XML_SCHEMAP_UNKNOWN_BASE_TYPE, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4441 | "Schemas: type %s base type %s not found\n", |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4442 | name, typeDecl->base); |
| 4443 | } |
| 4444 | typeDecl->baseType = baseType; |
| 4445 | } |
| 4446 | if (typeDecl->subtypes != NULL) |
| 4447 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4448 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4449 | explicitContentType = XML_SCHEMA_CONTENT_ELEMENTS; |
| 4450 | if (typeDecl->subtypes == NULL) |
| 4451 | /* 1.1.1 */ |
| 4452 | explicitContentType = XML_SCHEMA_CONTENT_EMPTY; |
| 4453 | else if ((typeDecl->subtypes->subtypes == NULL) && |
| 4454 | ((typeDecl->subtypes->type == |
| 4455 | XML_SCHEMA_TYPE_ALL) |
| 4456 | || (typeDecl->subtypes->type == |
| 4457 | XML_SCHEMA_TYPE_SEQUENCE))) |
| 4458 | /* 1.1.2 */ |
| 4459 | explicitContentType = XML_SCHEMA_CONTENT_EMPTY; |
| 4460 | else if ((typeDecl->subtypes->type == |
| 4461 | XML_SCHEMA_TYPE_CHOICE) |
| 4462 | && (typeDecl->subtypes->subtypes == NULL)) |
| 4463 | /* 1.1.3 */ |
| 4464 | explicitContentType = XML_SCHEMA_CONTENT_EMPTY; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4465 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4466 | base = xmlSchemaGetType(ctxt->schema, typeDecl->base, |
| 4467 | typeDecl->baseNs); |
| 4468 | if (base == NULL) { |
| 4469 | xmlSchemaPErr(ctxt, typeDecl->node, |
| 4470 | XML_SCHEMAP_UNKNOWN_BASE_TYPE, |
| 4471 | "Schemas: base type %s of type %s not found\n", |
| 4472 | typeDecl->base, name); |
| 4473 | return; |
| 4474 | } |
| 4475 | xmlSchemaTypeFixup(base, ctxt, NULL); |
| 4476 | if (explicitContentType == XML_SCHEMA_CONTENT_EMPTY) { |
| 4477 | /* 2.1 */ |
| 4478 | typeDecl->contentType = base->contentType; |
| 4479 | } else if (base->contentType == |
| 4480 | XML_SCHEMA_CONTENT_EMPTY) { |
| 4481 | /* 2.2 imbitable ! */ |
| 4482 | typeDecl->contentType = |
| 4483 | XML_SCHEMA_CONTENT_ELEMENTS; |
| 4484 | } else { |
| 4485 | /* 2.3 imbitable pareil ! */ |
| 4486 | typeDecl->contentType = |
| 4487 | XML_SCHEMA_CONTENT_ELEMENTS; |
| 4488 | } |
| 4489 | break; |
| 4490 | } |
| 4491 | case XML_SCHEMA_TYPE_COMPLEX:{ |
| 4492 | if (typeDecl->subtypes == NULL) { |
| 4493 | typeDecl->contentType = XML_SCHEMA_CONTENT_EMPTY; |
| 4494 | } else { |
| 4495 | if (typeDecl->flags & XML_SCHEMAS_TYPE_MIXED) |
| 4496 | typeDecl->contentType = |
| 4497 | XML_SCHEMA_CONTENT_MIXED; |
| 4498 | else { |
| 4499 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, |
| 4500 | NULL); |
| 4501 | if (typeDecl->subtypes != NULL) |
| 4502 | typeDecl->contentType = |
| 4503 | typeDecl->subtypes->contentType; |
| 4504 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 4505 | if (typeDecl->attributes == NULL) |
| 4506 | typeDecl->attributes = |
| 4507 | typeDecl->subtypes->attributes; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4508 | } |
| 4509 | break; |
| 4510 | } |
| 4511 | case XML_SCHEMA_TYPE_COMPLEX_CONTENT:{ |
| 4512 | if (typeDecl->subtypes == NULL) { |
| 4513 | typeDecl->contentType = XML_SCHEMA_CONTENT_EMPTY; |
| 4514 | } else { |
| 4515 | if (typeDecl->flags & XML_SCHEMAS_TYPE_MIXED) |
| 4516 | typeDecl->contentType = |
| 4517 | XML_SCHEMA_CONTENT_MIXED; |
| 4518 | else { |
| 4519 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, |
| 4520 | NULL); |
| 4521 | if (typeDecl->subtypes != NULL) |
| 4522 | typeDecl->contentType = |
| 4523 | typeDecl->subtypes->contentType; |
| 4524 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 4525 | if (typeDecl->attributes == NULL) |
| 4526 | typeDecl->attributes = |
| 4527 | typeDecl->subtypes->attributes; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4528 | } |
| 4529 | break; |
| 4530 | } |
| 4531 | case XML_SCHEMA_TYPE_SEQUENCE: |
| 4532 | case XML_SCHEMA_TYPE_GROUP: |
| 4533 | case XML_SCHEMA_TYPE_ALL: |
| 4534 | case XML_SCHEMA_TYPE_CHOICE: |
| 4535 | typeDecl->contentType = XML_SCHEMA_CONTENT_ELEMENTS; |
| 4536 | break; |
| 4537 | case XML_SCHEMA_TYPE_BASIC: |
| 4538 | case XML_SCHEMA_TYPE_ANY: |
| 4539 | case XML_SCHEMA_TYPE_FACET: |
| 4540 | case XML_SCHEMA_TYPE_SIMPLE: |
| 4541 | case XML_SCHEMA_TYPE_UR: |
| 4542 | case XML_SCHEMA_TYPE_ELEMENT: |
| 4543 | case XML_SCHEMA_TYPE_ATTRIBUTE: |
| 4544 | case XML_SCHEMA_TYPE_ATTRIBUTEGROUP: |
| 4545 | case XML_SCHEMA_TYPE_NOTATION: |
| 4546 | case XML_SCHEMA_TYPE_LIST: |
| 4547 | case XML_SCHEMA_TYPE_UNION: |
| 4548 | case XML_SCHEMA_FACET_MININCLUSIVE: |
| 4549 | case XML_SCHEMA_FACET_MINEXCLUSIVE: |
| 4550 | case XML_SCHEMA_FACET_MAXINCLUSIVE: |
| 4551 | case XML_SCHEMA_FACET_MAXEXCLUSIVE: |
| 4552 | case XML_SCHEMA_FACET_TOTALDIGITS: |
| 4553 | case XML_SCHEMA_FACET_FRACTIONDIGITS: |
| 4554 | case XML_SCHEMA_FACET_PATTERN: |
| 4555 | case XML_SCHEMA_FACET_ENUMERATION: |
| 4556 | case XML_SCHEMA_FACET_WHITESPACE: |
| 4557 | case XML_SCHEMA_FACET_LENGTH: |
| 4558 | case XML_SCHEMA_FACET_MAXLENGTH: |
| 4559 | case XML_SCHEMA_FACET_MINLENGTH: |
| 4560 | typeDecl->contentType = XML_SCHEMA_CONTENT_SIMPLE; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 4561 | if (typeDecl->subtypes != NULL) |
| 4562 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4563 | break; |
| 4564 | } |
| 4565 | } |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4566 | #ifdef DEBUG_TYPE |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 4567 | if (typeDecl->node != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4568 | xmlGenericError(xmlGenericErrorContext, |
| 4569 | "Type of %s : %s:%d :", name, |
| 4570 | typeDecl->node->doc->URL, |
| 4571 | xmlGetLineNo(typeDecl->node)); |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 4572 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4573 | xmlGenericError(xmlGenericErrorContext, "Type of %s :", name); |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 4574 | } |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4575 | switch (typeDecl->contentType) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4576 | case XML_SCHEMA_CONTENT_SIMPLE: |
| 4577 | xmlGenericError(xmlGenericErrorContext, "simple\n"); |
| 4578 | break; |
| 4579 | case XML_SCHEMA_CONTENT_ELEMENTS: |
| 4580 | xmlGenericError(xmlGenericErrorContext, "elements\n"); |
| 4581 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4582 | case XML_SCHEMA_CONTENT_UNKNOWN: |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4583 | xmlGenericError(xmlGenericErrorContext, "unknown !!!\n"); |
| 4584 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4585 | case XML_SCHEMA_CONTENT_EMPTY: |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4586 | xmlGenericError(xmlGenericErrorContext, "empty\n"); |
| 4587 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4588 | case XML_SCHEMA_CONTENT_MIXED: |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4589 | xmlGenericError(xmlGenericErrorContext, "mixed\n"); |
| 4590 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4591 | case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS: |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4592 | xmlGenericError(xmlGenericErrorContext, "mixed or elems\n"); |
| 4593 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4594 | case XML_SCHEMA_CONTENT_BASIC: |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4595 | xmlGenericError(xmlGenericErrorContext, "basic\n"); |
| 4596 | break; |
| 4597 | default: |
| 4598 | xmlGenericError(xmlGenericErrorContext, |
| 4599 | "not registered !!!\n"); |
| 4600 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4601 | } |
| 4602 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4603 | } |
| 4604 | |
| 4605 | /** |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4606 | * xmlSchemaCheckFacet: |
| 4607 | * @facet: the facet |
| 4608 | * @typeDecl: the schema type definition |
| 4609 | * @ctxt: the schema parser context or NULL |
| 4610 | * @name: name of the type |
| 4611 | * |
| 4612 | * Checks the default values types, especially for facets |
| 4613 | * |
| 4614 | * Returns 0 if okay or -1 in cae of error |
| 4615 | */ |
| 4616 | int |
| 4617 | xmlSchemaCheckFacet(xmlSchemaFacetPtr facet, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4618 | xmlSchemaTypePtr typeDecl, |
| 4619 | xmlSchemaParserCtxtPtr ctxt, const xmlChar * name) |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4620 | { |
| 4621 | static xmlSchemaTypePtr nonNegativeIntegerType = NULL; |
| 4622 | int ret = 0; |
| 4623 | |
| 4624 | if (nonNegativeIntegerType == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4625 | nonNegativeIntegerType = |
| 4626 | xmlSchemaGetPredefinedType(BAD_CAST "nonNegativeInteger", |
| 4627 | xmlSchemaNs); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4628 | } |
| 4629 | switch (facet->type) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4630 | case XML_SCHEMA_FACET_MININCLUSIVE: |
| 4631 | case XML_SCHEMA_FACET_MINEXCLUSIVE: |
| 4632 | case XML_SCHEMA_FACET_MAXINCLUSIVE: |
| 4633 | case XML_SCHEMA_FACET_MAXEXCLUSIVE:{ |
| 4634 | /* |
| 4635 | * Okay we need to validate the value |
| 4636 | * at that point. |
| 4637 | */ |
| 4638 | xmlSchemaValidCtxtPtr vctxt; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4639 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4640 | vctxt = xmlSchemaNewValidCtxt(NULL); |
| 4641 | if (vctxt == NULL) |
| 4642 | break; |
| 4643 | xmlSchemaValidateSimpleValue(vctxt, typeDecl, |
| 4644 | facet->value); |
| 4645 | facet->val = vctxt->value; |
| 4646 | vctxt->value = NULL; |
| 4647 | if (facet->val == NULL) { |
| 4648 | /* error code */ |
| 4649 | if (ctxt != NULL) { |
| 4650 | xmlSchemaPErr(ctxt, facet->node, |
| 4651 | XML_SCHEMAP_INVALID_FACET, |
| 4652 | "Schemas: type %s facet value %s invalid\n", |
| 4653 | name, facet->value); |
| 4654 | } |
| 4655 | ret = -1; |
| 4656 | } |
| 4657 | xmlSchemaFreeValidCtxt(vctxt); |
| 4658 | break; |
| 4659 | } |
| 4660 | case XML_SCHEMA_FACET_ENUMERATION:{ |
| 4661 | /* |
| 4662 | * Okay we need to validate the value |
| 4663 | * at that point. |
| 4664 | */ |
| 4665 | xmlSchemaValidCtxtPtr vctxt; |
| 4666 | int tmp; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4667 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4668 | vctxt = xmlSchemaNewValidCtxt(NULL); |
| 4669 | if (vctxt == NULL) |
| 4670 | break; |
| 4671 | tmp = xmlSchemaValidateSimpleValue(vctxt, typeDecl, |
| 4672 | facet->value); |
| 4673 | if (tmp != 0) { |
| 4674 | if (ctxt != NULL) { |
| 4675 | xmlSchemaPErr(ctxt, facet->node, |
| 4676 | XML_SCHEMAP_INVALID_ENUM, |
| 4677 | "Schemas: type %s enumeration value %s invalid\n", |
| 4678 | name, facet->value); |
| 4679 | } |
| 4680 | ret = -1; |
| 4681 | } |
| 4682 | xmlSchemaFreeValidCtxt(vctxt); |
| 4683 | break; |
| 4684 | } |
| 4685 | case XML_SCHEMA_FACET_PATTERN: |
| 4686 | facet->regexp = xmlRegexpCompile(facet->value); |
| 4687 | if (facet->regexp == NULL) { |
| 4688 | xmlSchemaPErr(ctxt, typeDecl->node, |
| 4689 | XML_SCHEMAP_REGEXP_INVALID, |
| 4690 | "Schemas: type %s facet regexp %s invalid\n", |
| 4691 | name, facet->value); |
| 4692 | ret = -1; |
| 4693 | } |
| 4694 | break; |
| 4695 | case XML_SCHEMA_FACET_TOTALDIGITS: |
| 4696 | case XML_SCHEMA_FACET_FRACTIONDIGITS: |
| 4697 | case XML_SCHEMA_FACET_LENGTH: |
| 4698 | case XML_SCHEMA_FACET_MAXLENGTH: |
| 4699 | case XML_SCHEMA_FACET_MINLENGTH:{ |
| 4700 | int tmp; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4701 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4702 | tmp = |
| 4703 | xmlSchemaValidatePredefinedType(nonNegativeIntegerType, |
| 4704 | facet->value, |
| 4705 | &facet->val); |
| 4706 | if (tmp != 0) { |
| 4707 | /* error code */ |
| 4708 | if (ctxt != NULL) { |
| 4709 | xmlSchemaPErr(ctxt, facet->node, |
| 4710 | XML_SCHEMAP_INVALID_FACET_VALUE, |
| 4711 | "Schemas: type %s facet value %s invalid\n", |
| 4712 | name, facet->value); |
| 4713 | } |
| 4714 | ret = -1; |
| 4715 | } |
| 4716 | break; |
| 4717 | } |
| 4718 | case XML_SCHEMA_FACET_WHITESPACE:{ |
| 4719 | if (xmlStrEqual(facet->value, BAD_CAST "preserve")) { |
| 4720 | facet->whitespace = XML_SCHEMAS_FACET_PRESERVE; |
| 4721 | } else if (xmlStrEqual(facet->value, BAD_CAST "replace")) { |
| 4722 | facet->whitespace = XML_SCHEMAS_FACET_REPLACE; |
| 4723 | } else if (xmlStrEqual(facet->value, BAD_CAST "collapse")) { |
| 4724 | facet->whitespace = XML_SCHEMAS_FACET_COLLAPSE; |
| 4725 | } else { |
| 4726 | if (ctxt != NULL) { |
| 4727 | xmlSchemaPErr(ctxt, facet->node, |
| 4728 | XML_SCHEMAP_INVALID_WHITE_SPACE, |
| 4729 | "Schemas: type %s whiteSpace value %s invalid\n", |
| 4730 | name, facet->value); |
| 4731 | } |
| 4732 | ret = -1; |
| 4733 | } |
| 4734 | } |
| 4735 | default: |
| 4736 | break; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4737 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4738 | return (ret); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4739 | } |
| 4740 | |
| 4741 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4742 | * xmlSchemaCheckDefaults: |
| 4743 | * @typeDecl: the schema type definition |
| 4744 | * @ctxt: the schema parser context |
| 4745 | * |
| 4746 | * Checks the default values types, especially for facets |
| 4747 | */ |
| 4748 | static void |
| 4749 | xmlSchemaCheckDefaults(xmlSchemaTypePtr typeDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4750 | xmlSchemaParserCtxtPtr ctxt, const xmlChar * name) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4751 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4752 | if (name == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4753 | name = typeDecl->name; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4754 | if (typeDecl->type == XML_SCHEMA_TYPE_RESTRICTION) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4755 | if (typeDecl->facets != NULL) { |
| 4756 | xmlSchemaFacetPtr facet = typeDecl->facets; |
| 4757 | |
| 4758 | while (facet != NULL) { |
| 4759 | xmlSchemaCheckFacet(facet, typeDecl, ctxt, name); |
| 4760 | facet = facet->next; |
| 4761 | } |
| 4762 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4763 | } |
| 4764 | } |
| 4765 | |
| 4766 | /** |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4767 | * xmlSchemaAttrGrpFixup: |
| 4768 | * @attrgrpDecl: the schema attribute definition |
| 4769 | * @ctxt: the schema parser context |
| 4770 | * @name: the attribute name |
| 4771 | * |
| 4772 | * Fixes finish doing the computations on the attributes definitions |
| 4773 | */ |
| 4774 | static void |
| 4775 | xmlSchemaAttrGrpFixup(xmlSchemaAttributeGroupPtr attrgrpDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4776 | xmlSchemaParserCtxtPtr ctxt, const xmlChar * name) |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4777 | { |
| 4778 | if (name == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4779 | name = attrgrpDecl->name; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4780 | if (attrgrpDecl->attributes != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4781 | return; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4782 | if (attrgrpDecl->ref != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4783 | xmlSchemaAttributeGroupPtr ref; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4784 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4785 | ref = xmlHashLookup2(ctxt->schema->attrgrpDecl, attrgrpDecl->ref, |
| 4786 | attrgrpDecl->refNs); |
| 4787 | if (ref == NULL) { |
| 4788 | xmlSchemaPErr(ctxt, attrgrpDecl->node, |
| 4789 | XML_SCHEMAP_UNKNOWN_ATTRIBUTE_GROUP, |
| 4790 | "Schemas: attribute group %s reference %s not found\n", |
| 4791 | name, attrgrpDecl->ref); |
| 4792 | return; |
| 4793 | } |
| 4794 | xmlSchemaAttrGrpFixup(ref, ctxt, NULL); |
| 4795 | attrgrpDecl->attributes = ref->attributes; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4796 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4797 | xmlSchemaPErr(ctxt, attrgrpDecl->node, XML_SCHEMAP_NOATTR_NOREF, |
| 4798 | "Schemas: attribute %s has no attributes nor reference\n", |
| 4799 | name, NULL); |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4800 | } |
| 4801 | } |
| 4802 | |
| 4803 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4804 | * xmlSchemaAttrFixup: |
| 4805 | * @attrDecl: the schema attribute definition |
| 4806 | * @ctxt: the schema parser context |
| 4807 | * @name: the attribute name |
| 4808 | * |
| 4809 | * Fixes finish doing the computations on the attributes definitions |
| 4810 | */ |
| 4811 | static void |
| 4812 | xmlSchemaAttrFixup(xmlSchemaAttributePtr attrDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4813 | xmlSchemaParserCtxtPtr ctxt, const xmlChar * name) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4814 | { |
| 4815 | if (name == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4816 | name = attrDecl->name; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4817 | if (attrDecl->subtypes != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4818 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4819 | if (attrDecl->typeName != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4820 | xmlSchemaTypePtr type; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4821 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4822 | type = xmlSchemaGetType(ctxt->schema, attrDecl->typeName, |
| 4823 | attrDecl->typeNs); |
| 4824 | if (type == NULL) { |
| 4825 | xmlSchemaPErr(ctxt, attrDecl->node, XML_SCHEMAP_UNKNOWN_TYPE, |
| 4826 | "Schemas: attribute %s type %s not found\n", |
| 4827 | name, attrDecl->typeName); |
| 4828 | } |
| 4829 | attrDecl->subtypes = type; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4830 | } else if (attrDecl->ref != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4831 | xmlSchemaAttributePtr ref; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4832 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4833 | ref = xmlHashLookup2(ctxt->schema->attrDecl, attrDecl->ref, |
| 4834 | attrDecl->refNs); |
| 4835 | if (ref == NULL) { |
| 4836 | xmlSchemaPErr(ctxt, attrDecl->node, XML_SCHEMAP_UNKNOWN_REF, |
| 4837 | "Schemas: attribute %s reference %s not found\n", |
| 4838 | name, attrDecl->ref); |
| 4839 | return; |
| 4840 | } |
| 4841 | xmlSchemaAttrFixup(ref, ctxt, NULL); |
| 4842 | attrDecl->subtypes = ref->subtypes; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4843 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4844 | xmlSchemaPErr(ctxt, attrDecl->node, XML_SCHEMAP_NOTYPE_NOREF, |
| 4845 | "Schemas: attribute %s has no type nor reference\n", |
| 4846 | name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4847 | } |
| 4848 | } |
| 4849 | |
| 4850 | /** |
| 4851 | * xmlSchemaParse: |
| 4852 | * @ctxt: a schema validation context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4853 | * |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4854 | * parse a schema definition resource and build an internal |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4855 | * XML Shema struture which can be used to validate instances. |
| 4856 | * *WARNING* this interface is highly subject to change |
| 4857 | * |
| 4858 | * Returns the internal XML Schema structure built from the resource or |
| 4859 | * NULL in case of error |
| 4860 | */ |
| 4861 | xmlSchemaPtr |
| 4862 | xmlSchemaParse(xmlSchemaParserCtxtPtr ctxt) |
| 4863 | { |
| 4864 | xmlSchemaPtr ret = NULL; |
| 4865 | xmlDocPtr doc; |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 4866 | xmlNodePtr root; |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 4867 | int nberrors; |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 4868 | int preserve = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4869 | |
| 4870 | xmlSchemaInitTypes(); |
| 4871 | |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 4872 | if (ctxt == NULL) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4873 | return (NULL); |
| 4874 | |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 4875 | nberrors = ctxt->nberrors; |
| 4876 | ctxt->nberrors = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4877 | ctxt->counter = 0; |
| 4878 | ctxt->container = NULL; |
| 4879 | |
| 4880 | /* |
| 4881 | * First step is to parse the input document into an DOM/Infoset |
| 4882 | */ |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 4883 | if (ctxt->URL != NULL) { |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 4884 | doc = xmlReadFile((const char *) ctxt->URL, NULL, |
| 4885 | SCHEMAS_PARSE_OPTIONS); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4886 | if (doc == NULL) { |
| 4887 | xmlSchemaPErr(ctxt, NULL, |
| 4888 | XML_SCHEMAP_FAILED_LOAD, |
| 4889 | "xmlSchemaParse: could not load %s\n", |
| 4890 | ctxt->URL, NULL); |
| 4891 | return (NULL); |
| 4892 | } |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 4893 | } else if (ctxt->buffer != NULL) { |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 4894 | doc = xmlReadMemory(ctxt->buffer, ctxt->size, NULL, NULL, |
| 4895 | SCHEMAS_PARSE_OPTIONS); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4896 | if (doc == NULL) { |
| 4897 | xmlSchemaPErr(ctxt, NULL, |
| 4898 | XML_SCHEMAP_FAILED_PARSE, |
| 4899 | "xmlSchemaParse: could not parse\n", |
| 4900 | NULL, NULL); |
| 4901 | return (NULL); |
| 4902 | } |
| 4903 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
Daniel Veillard | 6576528 | 2004-01-08 16:59:30 +0000 | [diff] [blame] | 4904 | ctxt->URL = xmlDictLookup(ctxt->dict, BAD_CAST "in_memory_buffer", -1); |
Daniel Veillard | 9d75150 | 2003-10-29 13:21:47 +0000 | [diff] [blame] | 4905 | } else if (ctxt->doc != NULL) { |
| 4906 | doc = ctxt->doc; |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 4907 | preserve = 1; |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 4908 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4909 | xmlSchemaPErr(ctxt, NULL, |
| 4910 | XML_SCHEMAP_NOTHING_TO_PARSE, |
| 4911 | "xmlSchemaParse: could not parse\n", |
| 4912 | NULL, NULL); |
| 4913 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
| 4916 | /* |
| 4917 | * Then extract the root and Schema parse it |
| 4918 | */ |
| 4919 | root = xmlDocGetRootElement(doc); |
| 4920 | if (root == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4921 | xmlSchemaPErr(ctxt, (xmlNodePtr) doc, |
| 4922 | XML_SCHEMAP_NOROOT, |
| 4923 | "schemas has no root", NULL, NULL); |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 4924 | if (!preserve) { |
| 4925 | xmlFreeDoc(doc); |
| 4926 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4927 | return (NULL); |
| 4928 | } |
| 4929 | |
| 4930 | /* |
| 4931 | * Remove all the blank text nodes |
| 4932 | */ |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 4933 | xmlSchemaCleanupDoc(ctxt, root); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4934 | |
| 4935 | /* |
| 4936 | * Then do the parsing for good |
| 4937 | */ |
| 4938 | ret = xmlSchemaParseSchema(ctxt, root); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 4939 | if (ret == NULL) { |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 4940 | if (!preserve) { |
| 4941 | xmlFreeDoc(doc); |
| 4942 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4943 | return (NULL); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 4944 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4945 | ret->doc = doc; |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 4946 | ret->preserve = preserve; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4947 | |
| 4948 | /* |
| 4949 | * Then fix all the references. |
| 4950 | */ |
| 4951 | ctxt->schema = ret; |
| 4952 | xmlHashScanFull(ret->elemDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4953 | (xmlHashScannerFull) xmlSchemaRefFixupCallback, ctxt); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4954 | |
| 4955 | /* |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 4956 | * Then fixup all attributes declarations |
| 4957 | */ |
| 4958 | xmlHashScan(ret->attrDecl, (xmlHashScanner) xmlSchemaAttrFixup, ctxt); |
| 4959 | |
| 4960 | /* |
| 4961 | * Then fixup all attributes group declarations |
| 4962 | */ |
| 4963 | xmlHashScan(ret->attrgrpDecl, (xmlHashScanner) xmlSchemaAttrGrpFixup, |
| 4964 | ctxt); |
| 4965 | |
| 4966 | /* |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4967 | * Then fixup all types properties |
| 4968 | */ |
| 4969 | xmlHashScan(ret->typeDecl, (xmlHashScanner) xmlSchemaTypeFixup, ctxt); |
| 4970 | |
| 4971 | /* |
| 4972 | * Then build the content model for all elements |
| 4973 | */ |
| 4974 | xmlHashScan(ret->elemDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4975 | (xmlHashScanner) xmlSchemaBuildContentModel, ctxt); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4976 | |
| 4977 | /* |
| 4978 | * Then check the defaults part of the type like facets values |
| 4979 | */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4980 | xmlHashScan(ret->typeDecl, (xmlHashScanner) xmlSchemaCheckDefaults, |
| 4981 | ctxt); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4982 | |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 4983 | if (ctxt->nberrors != 0) { |
| 4984 | xmlSchemaFree(ret); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4985 | ret = NULL; |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 4986 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4987 | return (ret); |
| 4988 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4989 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4990 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4991 | * xmlSchemaSetParserErrors: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4992 | * @ctxt: a schema validation context |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4993 | * @err: the error callback |
| 4994 | * @warn: the warning callback |
| 4995 | * @ctx: contextual data for the callbacks |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4996 | * |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4997 | * Set the callback functions used to handle errors for a validation context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4998 | */ |
| 4999 | void |
| 5000 | xmlSchemaSetParserErrors(xmlSchemaParserCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5001 | xmlSchemaValidityErrorFunc err, |
| 5002 | xmlSchemaValidityWarningFunc warn, void *ctx) |
| 5003 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5004 | if (ctxt == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5005 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5006 | ctxt->error = err; |
| 5007 | ctxt->warning = warn; |
| 5008 | ctxt->userData = ctx; |
| 5009 | } |
| 5010 | |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5011 | /** |
| 5012 | * xmlSchemaFacetTypeToString: |
| 5013 | * @type: the facet type |
| 5014 | * |
| 5015 | * Convert the xmlSchemaTypeType to a char string. |
| 5016 | * |
| 5017 | * Returns the char string representation of the facet type if the |
| 5018 | * type is a facet and an "Internal Error" string otherwise. |
| 5019 | */ |
| 5020 | static const char * |
| 5021 | xmlSchemaFacetTypeToString(xmlSchemaTypeType type) |
| 5022 | { |
| 5023 | switch (type) { |
| 5024 | case XML_SCHEMA_FACET_PATTERN: |
| 5025 | return ("pattern"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5026 | case XML_SCHEMA_FACET_MAXEXCLUSIVE: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5027 | return ("maxExclusive"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5028 | case XML_SCHEMA_FACET_MAXINCLUSIVE: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5029 | return ("maxInclusive"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5030 | case XML_SCHEMA_FACET_MINEXCLUSIVE: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5031 | return ("minExclusive"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5032 | case XML_SCHEMA_FACET_MININCLUSIVE: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5033 | return ("minInclusive"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5034 | case XML_SCHEMA_FACET_WHITESPACE: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5035 | return ("whiteSpace"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5036 | case XML_SCHEMA_FACET_ENUMERATION: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5037 | return ("enumeration"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5038 | case XML_SCHEMA_FACET_LENGTH: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5039 | return ("length"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5040 | case XML_SCHEMA_FACET_MAXLENGTH: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5041 | return ("maxLength"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5042 | case XML_SCHEMA_FACET_MINLENGTH: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5043 | return ("minLength"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5044 | case XML_SCHEMA_FACET_TOTALDIGITS: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5045 | return ("totalDigits"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5046 | case XML_SCHEMA_FACET_FRACTIONDIGITS: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5047 | return ("fractionDigits"); |
| 5048 | default: |
| 5049 | break; |
| 5050 | } |
| 5051 | return ("Internal Error"); |
| 5052 | } |
| 5053 | |
| 5054 | /** |
| 5055 | * xmlSchemaValidateFacets: |
| 5056 | * @ctxt: a schema validation context |
| 5057 | * @base: the base type |
| 5058 | * @facets: the list of facets to check |
| 5059 | * @value: the lexical repr of the value to validate |
| 5060 | * @val: the precomputed value |
| 5061 | * |
| 5062 | * Check a value against all facet conditions |
| 5063 | * |
| 5064 | * Returns 0 if the element is schemas valid, a positive error code |
| 5065 | * number otherwise and -1 in case of internal or API error. |
| 5066 | */ |
| 5067 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5068 | xmlSchemaValidateFacets(xmlSchemaValidCtxtPtr ctxt, |
| 5069 | xmlSchemaTypePtr base, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5070 | xmlSchemaFacetPtr facets, const xmlChar * value) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5071 | { |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5072 | int ret = 0; |
| 5073 | int tmp = 0; |
| 5074 | xmlSchemaTypeType type; |
| 5075 | xmlSchemaFacetPtr facet = facets; |
| 5076 | |
| 5077 | while (facet != NULL) { |
| 5078 | type = facet->type; |
| 5079 | if (type == XML_SCHEMA_FACET_ENUMERATION) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5080 | tmp = 1; |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5081 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5082 | while (facet != NULL) { |
| 5083 | tmp = |
| 5084 | xmlSchemaValidateFacet(base, facet, value, |
| 5085 | ctxt->value); |
| 5086 | if (tmp == 0) { |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5087 | return 0; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5088 | } |
| 5089 | facet = facet->next; |
| 5090 | } |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5091 | } else |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5092 | tmp = xmlSchemaValidateFacet(base, facet, value, ctxt->value); |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5093 | |
| 5094 | if (tmp != 0) { |
| 5095 | ret = tmp; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5096 | xmlSchemaVErr(ctxt, ctxt->cur, XML_SCHEMAS_ERR_FACET, "Failed to validate type with facet %s\n", (const xmlChar *) xmlSchemaFacetTypeToString(type), NULL); |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5097 | } |
| 5098 | if (facet != NULL) |
| 5099 | facet = facet->next; |
| 5100 | } |
| 5101 | return (ret); |
| 5102 | } |
| 5103 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5104 | /************************************************************************ |
| 5105 | * * |
| 5106 | * Simple type validation * |
| 5107 | * * |
| 5108 | ************************************************************************/ |
| 5109 | |
| 5110 | /** |
| 5111 | * xmlSchemaValidateSimpleValue: |
| 5112 | * @ctxt: a schema validation context |
| 5113 | * @type: the type declaration |
| 5114 | * @value: the value to validate |
| 5115 | * |
| 5116 | * Validate a value against a simple type |
| 5117 | * |
| 5118 | * Returns 0 if the value is valid, a positive error code |
| 5119 | * number otherwise and -1 in case of internal or API error. |
| 5120 | */ |
| 5121 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5122 | xmlSchemaValidateSimpleValue(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5123 | xmlSchemaTypePtr type, const xmlChar * value) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5124 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5125 | int ret = 0; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5126 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5127 | /* |
| 5128 | * First normalize the value accordingly to Schema Datatype |
| 5129 | * 4.3.6 whiteSpace definition of the whiteSpace facet of type |
| 5130 | */ |
| 5131 | /* |
| 5132 | * Then check the normalized value against the lexical space of the |
| 5133 | * type. |
| 5134 | */ |
| 5135 | if (type->type == XML_SCHEMA_TYPE_BASIC) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5136 | if (ctxt->value != NULL) { |
| 5137 | xmlSchemaFreeValue(ctxt->value); |
| 5138 | ctxt->value = NULL; |
| 5139 | } |
| 5140 | ret = xmlSchemaValPredefTypeNode(type, value, &(ctxt->value), |
| 5141 | ctxt->cur); |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5142 | if (ret != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5143 | xmlSchemaVErr(ctxt, ctxt->cur, XML_SCHEMAS_ERR_VALUE, "Failed to validate basic type %s\n", type->name, NULL); |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5144 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5145 | } else if (type->type == XML_SCHEMA_TYPE_RESTRICTION) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5146 | xmlSchemaTypePtr base; |
| 5147 | xmlSchemaFacetPtr facet; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5148 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5149 | base = type->baseType; |
| 5150 | if (base != NULL) { |
| 5151 | ret = xmlSchemaValidateSimpleValue(ctxt, base, value); |
| 5152 | } else if (type->subtypes != NULL) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5153 | TODO |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5154 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5155 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5156 | /* |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 5157 | * Do not validate facets or attributes when working on |
| 5158 | * building the Schemas |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5159 | */ |
| 5160 | if (ctxt->schema != NULL) { |
| 5161 | if (ret == 0) { |
| 5162 | facet = type->facets; |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5163 | ret = xmlSchemaValidateFacets(ctxt, base, facet, value); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5164 | } |
| 5165 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5166 | } else if (type->type == XML_SCHEMA_TYPE_SIMPLE) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5167 | xmlSchemaTypePtr base; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5168 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5169 | base = type->subtypes; |
| 5170 | if (base != NULL) { |
| 5171 | ret = xmlSchemaValidateSimpleValue(ctxt, base, value); |
| 5172 | } else { |
| 5173 | TODO} |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5174 | } else if (type->type == XML_SCHEMA_TYPE_LIST) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5175 | xmlSchemaTypePtr base; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5176 | const xmlChar *cur, *end; |
| 5177 | xmlChar *tmp; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5178 | int ret2; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5179 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5180 | base = type->subtypes; |
| 5181 | if (base == NULL) { |
| 5182 | xmlSchemaVErr(ctxt, type->node, XML_SCHEMAS_ERR_INTERNAL, |
| 5183 | "Internal: List type %s has no base type\n", |
| 5184 | type->name, NULL); |
| 5185 | return (-1); |
| 5186 | } |
| 5187 | cur = value; |
| 5188 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5189 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5190 | cur++; |
| 5191 | end = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5192 | while ((*end != 0) && (!(IS_BLANK_CH(*end)))) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5193 | end++; |
| 5194 | if (end == cur) |
| 5195 | break; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5196 | tmp = xmlStrndup(cur, end - cur); |
| 5197 | ret2 = xmlSchemaValidateSimpleValue(ctxt, base, tmp); |
| 5198 | xmlFree(tmp); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5199 | if (ret2 != 0) |
| 5200 | ret = 1; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5201 | cur = end; |
| 5202 | } while (*cur != 0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5203 | } else { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5204 | TODO |
| 5205 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5206 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5207 | } |
| 5208 | |
| 5209 | /************************************************************************ |
| 5210 | * * |
| 5211 | * DOM Validation code * |
| 5212 | * * |
| 5213 | ************************************************************************/ |
| 5214 | |
| 5215 | static int xmlSchemaValidateContent(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5216 | xmlNodePtr node); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5217 | static int xmlSchemaValidateAttributes(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5218 | xmlNodePtr elem, |
| 5219 | xmlSchemaAttributePtr attributes); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5220 | static int xmlSchemaValidateType(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5221 | xmlNodePtr elem, |
| 5222 | xmlSchemaElementPtr elemDecl, |
| 5223 | xmlSchemaTypePtr type); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5224 | |
| 5225 | /** |
| 5226 | * xmlSchemaRegisterAttributes: |
| 5227 | * @ctxt: a schema validation context |
| 5228 | * @attrs: a list of attributes |
| 5229 | * |
| 5230 | * Register the list of attributes as the set to be validated on that element |
| 5231 | * |
| 5232 | * Returns -1 in case of error, 0 otherwise |
| 5233 | */ |
| 5234 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5235 | xmlSchemaRegisterAttributes(xmlSchemaValidCtxtPtr ctxt, xmlAttrPtr attrs) |
| 5236 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5237 | while (attrs != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5238 | if ((attrs->ns != NULL) && |
| 5239 | (xmlStrEqual(attrs->ns->href, xmlSchemaInstanceNs))) { |
| 5240 | attrs = attrs->next; |
| 5241 | continue; |
| 5242 | } |
| 5243 | if (ctxt->attrNr >= ctxt->attrMax) { |
| 5244 | xmlSchemaAttrStatePtr tmp; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5245 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5246 | ctxt->attrMax *= 2; |
| 5247 | tmp = (xmlSchemaAttrStatePtr) |
| 5248 | xmlRealloc(ctxt->attr, ctxt->attrMax * |
| 5249 | sizeof(xmlSchemaAttrState)); |
| 5250 | if (tmp == NULL) { |
| 5251 | xmlSchemaVErrMemory(ctxt, "registering attributes", NULL); |
| 5252 | ctxt->attrMax /= 2; |
| 5253 | return (-1); |
| 5254 | } |
| 5255 | ctxt->attr = tmp; |
| 5256 | } |
| 5257 | ctxt->attr[ctxt->attrNr].attr = attrs; |
| 5258 | ctxt->attr[ctxt->attrNr].state = XML_SCHEMAS_ATTR_UNKNOWN; |
| 5259 | ctxt->attrNr++; |
| 5260 | attrs = attrs->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5261 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5262 | return (0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5263 | } |
| 5264 | |
| 5265 | /** |
| 5266 | * xmlSchemaCheckAttributes: |
| 5267 | * @ctxt: a schema validation context |
| 5268 | * @node: the node carrying it. |
| 5269 | * |
| 5270 | * Check that the registered set of attributes on the current node |
| 5271 | * has been properly validated. |
| 5272 | * |
| 5273 | * Returns 0 if validity constraints are met, 1 otherwise. |
| 5274 | */ |
| 5275 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5276 | xmlSchemaCheckAttributes(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5277 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5278 | int ret = 0; |
| 5279 | int i; |
| 5280 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5281 | for (i = ctxt->attrBase; i < ctxt->attrNr; i++) { |
| 5282 | if (ctxt->attr[i].attr == NULL) |
| 5283 | break; |
| 5284 | if (ctxt->attr[i].state == XML_SCHEMAS_ATTR_UNKNOWN) { |
| 5285 | ret = 1; |
| 5286 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_ATTRUNKNOWN, "Attribute %s on %s is unknown\n", ctxt->attr[i].attr->name, node->name); |
| 5287 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5288 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5289 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5290 | } |
| 5291 | |
| 5292 | /** |
| 5293 | * xmlSchemaValidateSimpleContent: |
| 5294 | * @ctxt: a schema validation context |
| 5295 | * @elem: an element |
| 5296 | * @type: the type declaration |
| 5297 | * |
| 5298 | * Validate the content of an element expected to be a simple type |
| 5299 | * |
| 5300 | * Returns 0 if the element is schemas valid, a positive error code |
| 5301 | * number otherwise and -1 in case of internal or API error. |
| 5302 | */ |
| 5303 | static int |
| 5304 | xmlSchemaValidateSimpleContent(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5305 | xmlNodePtr node ATTRIBUTE_UNUSED) |
| 5306 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5307 | xmlNodePtr child; |
| 5308 | xmlSchemaTypePtr type, base; |
| 5309 | xmlChar *value; |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5310 | int ret = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5311 | |
| 5312 | child = ctxt->node; |
| 5313 | type = ctxt->type; |
| 5314 | |
| 5315 | /* |
| 5316 | * Validation Rule: Element Locally Valid (Type): 3.1.3 |
| 5317 | */ |
| 5318 | value = xmlNodeGetContent(child); |
| 5319 | /* xmlSchemaValidateSimpleValue(ctxt, type, value); */ |
| 5320 | switch (type->type) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5321 | case XML_SCHEMA_TYPE_RESTRICTION:{ |
| 5322 | xmlSchemaFacetPtr facet; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5323 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5324 | base = type->baseType; |
| 5325 | if (base != NULL) { |
| 5326 | ret = xmlSchemaValidateSimpleValue(ctxt, base, value); |
| 5327 | } else { |
| 5328 | TODO} |
| 5329 | if (ret == 0) { |
| 5330 | facet = type->facets; |
| 5331 | ret = |
| 5332 | xmlSchemaValidateFacets(ctxt, base, facet, value); |
| 5333 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 5334 | if ((ret == 0) && (type->attributes != NULL)) { |
| 5335 | ret = xmlSchemaValidateAttributes(ctxt, node, |
| 5336 | type->attributes); |
| 5337 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5338 | break; |
| 5339 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5340 | case XML_SCHEMA_TYPE_EXTENSION:{ |
| 5341 | TODO |
| 5342 | break; |
| 5343 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5344 | default: |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5345 | TODO |
| 5346 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5347 | if (value != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5348 | xmlFree(value); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5349 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5350 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5351 | } |
| 5352 | |
| 5353 | /** |
| 5354 | * xmlSchemaValidateCheckNodeList |
| 5355 | * @nodelist: the list of nodes |
| 5356 | * |
| 5357 | * Check the node list is only made of text nodes and entities pointing |
| 5358 | * to text nodes |
| 5359 | * |
| 5360 | * Returns 1 if true, 0 if false and -1 in case of error |
| 5361 | */ |
| 5362 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5363 | xmlSchemaValidateCheckNodeList(xmlNodePtr nodelist) |
| 5364 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5365 | while (nodelist != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5366 | if (nodelist->type == XML_ENTITY_REF_NODE) { |
| 5367 | TODO /* implement recursion in the entity content */ |
| 5368 | } |
| 5369 | if ((nodelist->type != XML_TEXT_NODE) && |
| 5370 | (nodelist->type != XML_COMMENT_NODE) && |
| 5371 | (nodelist->type != XML_PI_NODE) && |
| 5372 | (nodelist->type != XML_PI_NODE)) { |
| 5373 | return (0); |
| 5374 | } |
| 5375 | nodelist = nodelist->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5376 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5377 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5378 | } |
| 5379 | |
| 5380 | /** |
| 5381 | * xmlSchemaSkipIgnored: |
| 5382 | * @ctxt: a schema validation context |
| 5383 | * @type: the current type context |
| 5384 | * @node: the top node. |
| 5385 | * |
| 5386 | * Skip ignorable nodes in that context |
| 5387 | * |
| 5388 | * Returns the new sibling |
| 5389 | * number otherwise and -1 in case of internal or API error. |
| 5390 | */ |
| 5391 | static xmlNodePtr |
Daniel Veillard | dda8f1b | 2002-09-26 09:47:36 +0000 | [diff] [blame] | 5392 | xmlSchemaSkipIgnored(xmlSchemaValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5393 | xmlSchemaTypePtr type, xmlNodePtr node) |
| 5394 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5395 | int mixed = 0; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5396 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5397 | /* |
| 5398 | * TODO complete and handle entities |
| 5399 | */ |
| 5400 | mixed = ((type->contentType == XML_SCHEMA_CONTENT_MIXED) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5401 | (type->contentType == XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5402 | while ((node != NULL) && |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5403 | ((node->type == XML_COMMENT_NODE) || |
| 5404 | ((mixed == 1) && (node->type == XML_TEXT_NODE)) || |
| 5405 | (((type->contentType == XML_SCHEMA_CONTENT_ELEMENTS) && |
| 5406 | (node->type == XML_TEXT_NODE) && (IS_BLANK_NODE(node)))))) { |
| 5407 | node = node->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5408 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5409 | return (node); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5410 | } |
| 5411 | |
| 5412 | /** |
| 5413 | * xmlSchemaValidateCallback: |
| 5414 | * @ctxt: a schema validation context |
| 5415 | * @name: the name of the element detected (might be NULL) |
| 5416 | * @type: the type |
| 5417 | * |
| 5418 | * A transition has been made in the automata associated to an element |
| 5419 | * content model |
| 5420 | */ |
| 5421 | static void |
| 5422 | xmlSchemaValidateCallback(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5423 | const xmlChar * name ATTRIBUTE_UNUSED, |
| 5424 | xmlSchemaTypePtr type, xmlNodePtr node) |
| 5425 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5426 | xmlSchemaTypePtr oldtype = ctxt->type; |
| 5427 | xmlNodePtr oldnode = ctxt->node; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5428 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5429 | #ifdef DEBUG_CONTENT |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 5430 | xmlGenericError(xmlGenericErrorContext, |
| 5431 | "xmlSchemaValidateCallback: %s, %s, %s\n", |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5432 | name, type->name, node->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5433 | #endif |
| 5434 | ctxt->type = type; |
| 5435 | ctxt->node = node; |
| 5436 | xmlSchemaValidateContent(ctxt, node); |
| 5437 | ctxt->type = oldtype; |
| 5438 | ctxt->node = oldnode; |
| 5439 | } |
| 5440 | |
| 5441 | |
| 5442 | #if 0 |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5443 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5444 | /** |
| 5445 | * xmlSchemaValidateSimpleRestrictionType: |
| 5446 | * @ctxt: a schema validation context |
| 5447 | * @node: the top node. |
| 5448 | * |
| 5449 | * Validate the content of a restriction type. |
| 5450 | * |
| 5451 | * Returns 0 if the element is schemas valid, a positive error code |
| 5452 | * number otherwise and -1 in case of internal or API error. |
| 5453 | */ |
| 5454 | static int |
| 5455 | xmlSchemaValidateSimpleRestrictionType(xmlSchemaValidCtxtPtr ctxt, |
| 5456 | xmlNodePtr node) |
| 5457 | { |
| 5458 | xmlNodePtr child; |
| 5459 | xmlSchemaTypePtr type; |
| 5460 | int ret; |
| 5461 | |
| 5462 | child = ctxt->node; |
| 5463 | type = ctxt->type; |
| 5464 | |
| 5465 | if ((ctxt == NULL) || (type == NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5466 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INTERNAL, "Internal error: xmlSchemaValidateSimpleRestrictionType %s\n", node->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5467 | return (-1); |
| 5468 | } |
| 5469 | /* |
| 5470 | * Only text and text based entities references shall be found there |
| 5471 | */ |
| 5472 | ret = xmlSchemaValidateCheckNodeList(child); |
| 5473 | if (ret < 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5474 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INTERNAL, "Internal error: xmlSchemaValidateSimpleType %s content\n", node->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5475 | return (-1); |
| 5476 | } else if (ret == 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5477 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_NOTSIMPLE, "Element %s content is not a simple type\n", node->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5478 | return (-1); |
| 5479 | } |
| 5480 | ctxt->type = type->subtypes; |
| 5481 | xmlSchemaValidateContent(ctxt, node); |
| 5482 | ctxt->type = type; |
| 5483 | return (ret); |
| 5484 | } |
| 5485 | #endif |
| 5486 | |
| 5487 | /** |
| 5488 | * xmlSchemaValidateSimpleType: |
| 5489 | * @ctxt: a schema validation context |
| 5490 | * @node: the top node. |
| 5491 | * |
| 5492 | * Validate the content of an simple type. |
| 5493 | * |
| 5494 | * Returns 0 if the element is schemas valid, a positive error code |
| 5495 | * number otherwise and -1 in case of internal or API error. |
| 5496 | */ |
| 5497 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5498 | xmlSchemaValidateSimpleType(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5499 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5500 | xmlNodePtr child; |
| 5501 | xmlSchemaTypePtr type; |
| 5502 | xmlAttrPtr attr; |
| 5503 | int ret; |
| 5504 | |
| 5505 | child = ctxt->node; |
| 5506 | type = ctxt->type; |
| 5507 | |
| 5508 | if ((ctxt == NULL) || (type == NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5509 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INTERNAL, "Internal error: xmlSchemaValidateSimpleType %s\n", node->name, NULL); |
| 5510 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5511 | } |
| 5512 | /* |
| 5513 | * Only text and text based entities references shall be found there |
| 5514 | */ |
| 5515 | ret = xmlSchemaValidateCheckNodeList(child); |
| 5516 | if (ret < 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5517 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INTERNAL, "Internal error: xmlSchemaValidateSimpleType %s content\n", node->name, NULL); |
| 5518 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5519 | } else if (ret == 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5520 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_NOTSIMPLE, "Element %s content is not a simple type\n", node->name, NULL); |
| 5521 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5522 | } |
| 5523 | /* |
| 5524 | * Validation Rule: Element Locally Valid (Type): 3.1.1 |
| 5525 | */ |
| 5526 | attr = node->properties; |
| 5527 | while (attr != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5528 | if ((attr->ns == NULL) || |
| 5529 | (!xmlStrEqual(attr->ns->href, xmlSchemaInstanceNs)) || |
| 5530 | ((!xmlStrEqual(attr->name, BAD_CAST "type")) && |
| 5531 | (!xmlStrEqual(attr->name, BAD_CAST "nil")) && |
| 5532 | (!xmlStrEqual(attr->name, BAD_CAST "schemasLocation")) && |
| 5533 | (!xmlStrEqual |
| 5534 | (attr->name, BAD_CAST "noNamespaceSchemaLocation")))) { |
| 5535 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INVALIDATTR, "Element %s: attribute %s should not be present\n", node->name, attr->name); |
| 5536 | return (ctxt->err); |
| 5537 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5538 | } |
| 5539 | |
| 5540 | ctxt->type = type->subtypes; |
| 5541 | ret = xmlSchemaValidateSimpleContent(ctxt, node); |
| 5542 | ctxt->type = type; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5543 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5544 | } |
| 5545 | |
| 5546 | /** |
| 5547 | * xmlSchemaValidateElementType: |
| 5548 | * @ctxt: a schema validation context |
| 5549 | * @node: the top node. |
| 5550 | * |
| 5551 | * Validate the content of an element type. |
| 5552 | * Validation Rule: Element Locally Valid (Complex Type) |
| 5553 | * |
| 5554 | * Returns 0 if the element is schemas valid, a positive error code |
| 5555 | * number otherwise and -1 in case of internal or API error. |
| 5556 | */ |
| 5557 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5558 | xmlSchemaValidateElementType(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5559 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5560 | xmlNodePtr child; |
| 5561 | xmlSchemaTypePtr type; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5562 | xmlRegExecCtxtPtr oldregexp; /* cont model of the parent */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5563 | xmlSchemaElementPtr decl; |
| 5564 | int ret, attrBase; |
| 5565 | |
| 5566 | oldregexp = ctxt->regexp; |
| 5567 | |
| 5568 | child = ctxt->node; |
| 5569 | type = ctxt->type; |
| 5570 | |
| 5571 | if ((ctxt == NULL) || (type == NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5572 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INTERNAL, "Internal error: xmlSchemaValidateElementType\n", node->name, NULL); |
| 5573 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5574 | } |
| 5575 | if (child == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5576 | if (type->minOccurs > 0) { |
| 5577 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_MISSING, "Element %s: missing child %s\n", node->name, type->name); |
| 5578 | } |
| 5579 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5580 | } |
| 5581 | |
| 5582 | /* |
| 5583 | * Verify the element matches |
| 5584 | */ |
| 5585 | if (!xmlStrEqual(child->name, type->name)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5586 | xmlSchemaVErr3(ctxt, node, XML_SCHEMAS_ERR_WRONGELEM, "Element %s: missing child %s found %s\n", node->name, type->name, child->name); |
| 5587 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5588 | } |
| 5589 | /* |
| 5590 | * Verify the attributes |
| 5591 | */ |
| 5592 | attrBase = ctxt->attrBase; |
| 5593 | ctxt->attrBase = ctxt->attrNr; |
| 5594 | xmlSchemaRegisterAttributes(ctxt, child->properties); |
| 5595 | xmlSchemaValidateAttributes(ctxt, child, type->attributes); |
| 5596 | /* |
| 5597 | * Verify the element content recursively |
| 5598 | */ |
| 5599 | decl = (xmlSchemaElementPtr) type; |
| 5600 | oldregexp = ctxt->regexp; |
| 5601 | if (decl->contModel != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5602 | ctxt->regexp = xmlRegNewExecCtxt(decl->contModel, |
| 5603 | (xmlRegExecCallbacks) |
| 5604 | xmlSchemaValidateCallback, ctxt); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5605 | #ifdef DEBUG_AUTOMATA |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5606 | xmlGenericError(xmlGenericErrorContext, "====> %s\n", node->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5607 | #endif |
| 5608 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5609 | xmlSchemaValidateType(ctxt, child, (xmlSchemaElementPtr) type, |
| 5610 | type->subtypes); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5611 | |
| 5612 | if (decl->contModel != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5613 | ret = xmlRegExecPushString(ctxt->regexp, NULL, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5614 | #ifdef DEBUG_AUTOMATA |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5615 | xmlGenericError(xmlGenericErrorContext, |
| 5616 | "====> %s : %d\n", node->name, ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5617 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5618 | if (ret == 0) { |
| 5619 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_ELEMCONT, "Element %s content check failed\n", node->name, NULL); |
| 5620 | } else if (ret < 0) { |
| 5621 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_ELEMCONT, "Element %s content check failure\n", node->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5622 | #ifdef DEBUG_CONTENT |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5623 | } else { |
| 5624 | xmlGenericError(xmlGenericErrorContext, |
| 5625 | "Element %s content check succeeded\n", |
| 5626 | node->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5627 | |
| 5628 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5629 | } |
| 5630 | xmlRegFreeExecCtxt(ctxt->regexp); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5631 | } |
| 5632 | /* |
| 5633 | * Verify that all attributes were Schemas-validated |
| 5634 | */ |
| 5635 | xmlSchemaCheckAttributes(ctxt, node); |
| 5636 | ctxt->attrNr = ctxt->attrBase; |
| 5637 | ctxt->attrBase = attrBase; |
| 5638 | |
| 5639 | ctxt->regexp = oldregexp; |
| 5640 | |
| 5641 | ctxt->node = child; |
| 5642 | ctxt->type = type; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5643 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5644 | } |
| 5645 | |
| 5646 | /** |
| 5647 | * xmlSchemaValidateBasicType: |
| 5648 | * @ctxt: a schema validation context |
| 5649 | * @node: the top node. |
| 5650 | * |
| 5651 | * Validate the content of an element expected to be a basic type type |
| 5652 | * |
| 5653 | * Returns 0 if the element is schemas valid, a positive error code |
| 5654 | * number otherwise and -1 in case of internal or API error. |
| 5655 | */ |
| 5656 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5657 | xmlSchemaValidateBasicType(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5658 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5659 | int ret; |
| 5660 | xmlNodePtr child, cur; |
| 5661 | xmlSchemaTypePtr type; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5662 | xmlChar *value; /* lexical representation */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5663 | |
| 5664 | child = ctxt->node; |
| 5665 | type = ctxt->type; |
| 5666 | |
| 5667 | if ((ctxt == NULL) || (type == NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5668 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INTERNAL, "Internal error: xmlSchemaValidateBasicType\n", node->name, NULL); |
| 5669 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5670 | } |
| 5671 | /* |
| 5672 | * First check the content model of the node. |
| 5673 | */ |
| 5674 | cur = child; |
| 5675 | while (cur != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5676 | switch (cur->type) { |
| 5677 | case XML_TEXT_NODE: |
| 5678 | case XML_CDATA_SECTION_NODE: |
| 5679 | case XML_PI_NODE: |
| 5680 | case XML_COMMENT_NODE: |
| 5681 | case XML_XINCLUDE_START: |
| 5682 | case XML_XINCLUDE_END: |
| 5683 | break; |
| 5684 | case XML_ENTITY_REF_NODE: |
| 5685 | case XML_ENTITY_NODE: |
| 5686 | TODO break; |
| 5687 | case XML_ELEMENT_NODE: |
| 5688 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INVALIDELEM, "Element %s: child %s should not be present\n", node->name, cur->name); |
| 5689 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5690 | case XML_ATTRIBUTE_NODE: |
| 5691 | case XML_DOCUMENT_NODE: |
| 5692 | case XML_DOCUMENT_TYPE_NODE: |
| 5693 | case XML_DOCUMENT_FRAG_NODE: |
| 5694 | case XML_NOTATION_NODE: |
| 5695 | case XML_HTML_DOCUMENT_NODE: |
| 5696 | case XML_DTD_NODE: |
| 5697 | case XML_ELEMENT_DECL: |
| 5698 | case XML_ATTRIBUTE_DECL: |
| 5699 | case XML_ENTITY_DECL: |
| 5700 | case XML_NAMESPACE_DECL: |
| 5701 | #ifdef LIBXML_DOCB_ENABLED |
| 5702 | case XML_DOCB_DOCUMENT_NODE: |
| 5703 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5704 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INVALIDELEM, "Element %s: node type of node unexpected here\n", node->name, NULL); |
| 5705 | return (ctxt->err); |
| 5706 | } |
| 5707 | cur = cur->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5708 | } |
| 5709 | if (child == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5710 | value = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5711 | else |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5712 | value = xmlNodeGetContent(child->parent); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5713 | |
| 5714 | if (ctxt->value != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5715 | xmlSchemaFreeValue(ctxt->value); |
| 5716 | ctxt->value = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5717 | } |
| 5718 | ret = xmlSchemaValidatePredefinedType(type, value, &(ctxt->value)); |
| 5719 | if (value != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5720 | xmlFree(value); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5721 | if (ret != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5722 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_VALUE, "Element %s: failed to validate basic type %s\n", node->name, type->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5723 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5724 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5725 | } |
| 5726 | |
| 5727 | /** |
| 5728 | * xmlSchemaValidateComplexType: |
| 5729 | * @ctxt: a schema validation context |
| 5730 | * @node: the top node. |
| 5731 | * |
| 5732 | * Validate the content of an element expected to be a complex type type |
| 5733 | * xmlschema-1.html#cvc-complex-type |
| 5734 | * Validation Rule: Element Locally Valid (Complex Type) |
| 5735 | * |
| 5736 | * Returns 0 if the element is schemas valid, a positive error code |
| 5737 | * number otherwise and -1 in case of internal or API error. |
| 5738 | */ |
| 5739 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5740 | xmlSchemaValidateComplexType(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5741 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5742 | xmlNodePtr child; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 5743 | xmlSchemaTypePtr type, subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5744 | int ret; |
| 5745 | |
| 5746 | child = ctxt->node; |
| 5747 | type = ctxt->type; |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 5748 | ctxt->cur = node; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5749 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5750 | switch (type->contentType) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5751 | case XML_SCHEMA_CONTENT_EMPTY: |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5752 | if (type->baseType != NULL) { |
| 5753 | } else if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5754 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_NOTEMPTY, "Element %s is supposed to be empty\n", node->name, NULL); |
| 5755 | } |
| 5756 | if (type->attributes != NULL) { |
| 5757 | xmlSchemaValidateAttributes(ctxt, node, type->attributes); |
| 5758 | } |
| 5759 | subtype = type->subtypes; |
| 5760 | while (subtype != NULL) { |
| 5761 | ctxt->type = subtype; |
| 5762 | xmlSchemaValidateComplexType(ctxt, node); |
| 5763 | subtype = subtype->next; |
| 5764 | } |
| 5765 | break; |
| 5766 | case XML_SCHEMA_CONTENT_ELEMENTS: |
| 5767 | case XML_SCHEMA_CONTENT_MIXED: |
| 5768 | case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS: |
| 5769 | /* |
| 5770 | * Skip ignorable nodes in that context |
| 5771 | */ |
| 5772 | child = xmlSchemaSkipIgnored(ctxt, type, child); |
| 5773 | while (child != NULL) { |
| 5774 | if (child->type == XML_ELEMENT_NODE) { |
| 5775 | ret = xmlRegExecPushString(ctxt->regexp, |
| 5776 | child->name, child); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5777 | #ifdef DEBUG_AUTOMATA |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5778 | if (ret < 0) |
| 5779 | xmlGenericError(xmlGenericErrorContext, |
| 5780 | " --> %s Error\n", child->name); |
| 5781 | else |
| 5782 | xmlGenericError(xmlGenericErrorContext, |
| 5783 | " --> %s\n", child->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5784 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5785 | } |
| 5786 | child = child->next; |
| 5787 | /* |
| 5788 | * Skip ignorable nodes in that context |
| 5789 | */ |
| 5790 | child = xmlSchemaSkipIgnored(ctxt, type, child); |
| 5791 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 5792 | if (type->attributes != NULL) { |
| 5793 | xmlSchemaValidateAttributes(ctxt, node, type->attributes); |
| 5794 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5795 | break; |
| 5796 | case XML_SCHEMA_CONTENT_BASIC:{ |
| 5797 | if (type->subtypes != NULL) { |
| 5798 | ctxt->type = type->subtypes; |
| 5799 | xmlSchemaValidateComplexType(ctxt, node); |
| 5800 | } |
| 5801 | if (type->baseType != NULL) { |
| 5802 | ctxt->type = type->baseType; |
| 5803 | xmlSchemaValidateBasicType(ctxt, node); |
| 5804 | } |
| 5805 | if (type->attributes != NULL) { |
| 5806 | xmlSchemaValidateAttributes(ctxt, node, |
| 5807 | type->attributes); |
| 5808 | } |
| 5809 | ctxt->type = type; |
| 5810 | break; |
| 5811 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5812 | case XML_SCHEMA_CONTENT_SIMPLE:{ |
| 5813 | if (type->subtypes != NULL) { |
| 5814 | ctxt->type = type->subtypes; |
| 5815 | xmlSchemaValidateComplexType(ctxt, node); |
| 5816 | } |
| 5817 | if (type->baseType != NULL) { |
| 5818 | ctxt->type = type->baseType; |
| 5819 | xmlSchemaValidateComplexType(ctxt, node); |
| 5820 | } |
| 5821 | if (type->attributes != NULL) { |
| 5822 | xmlSchemaValidateAttributes(ctxt, node, |
| 5823 | type->attributes); |
| 5824 | } |
| 5825 | ctxt->type = type; |
| 5826 | break; |
| 5827 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5828 | default: |
| 5829 | TODO xmlGenericError(xmlGenericErrorContext, |
| 5830 | "unimplemented content type %d\n", |
| 5831 | type->contentType); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5832 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5833 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5834 | } |
| 5835 | |
| 5836 | /** |
| 5837 | * xmlSchemaValidateContent: |
| 5838 | * @ctxt: a schema validation context |
| 5839 | * @elem: an element |
| 5840 | * @type: the type declaration |
| 5841 | * |
| 5842 | * Validate the content of an element against the type. |
| 5843 | * |
| 5844 | * Returns 0 if the element is schemas valid, a positive error code |
| 5845 | * number otherwise and -1 in case of internal or API error. |
| 5846 | */ |
| 5847 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5848 | xmlSchemaValidateContent(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5849 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5850 | xmlNodePtr child; |
| 5851 | xmlSchemaTypePtr type; |
| 5852 | |
| 5853 | child = ctxt->node; |
| 5854 | type = ctxt->type; |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 5855 | ctxt->cur = node; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5856 | |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 5857 | xmlSchemaValidateAttributes(ctxt, node, type->attributes); |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 5858 | ctxt->cur = node; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 5859 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5860 | switch (type->type) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5861 | case XML_SCHEMA_TYPE_ANY: |
| 5862 | /* Any type will do it, fine */ |
| 5863 | TODO /* handle recursivity */ |
| 5864 | break; |
| 5865 | case XML_SCHEMA_TYPE_COMPLEX: |
| 5866 | xmlSchemaValidateComplexType(ctxt, node); |
| 5867 | break; |
| 5868 | case XML_SCHEMA_TYPE_ELEMENT:{ |
| 5869 | xmlSchemaElementPtr decl = (xmlSchemaElementPtr) type; |
| 5870 | |
| 5871 | /* |
| 5872 | * Handle element reference here |
| 5873 | */ |
| 5874 | if (decl->ref != NULL) { |
| 5875 | if (decl->refDecl == NULL) { |
| 5876 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INTERNAL, "Internal error: element reference %s not resolved\n", decl->ref, NULL); |
| 5877 | return (-1); |
| 5878 | } |
| 5879 | ctxt->type = (xmlSchemaTypePtr) decl->refDecl; |
| 5880 | decl = decl->refDecl; |
| 5881 | } |
| 5882 | xmlSchemaValidateElementType(ctxt, node); |
| 5883 | ctxt->type = type; |
| 5884 | break; |
| 5885 | } |
| 5886 | case XML_SCHEMA_TYPE_BASIC: |
| 5887 | xmlSchemaValidateBasicType(ctxt, node); |
| 5888 | break; |
| 5889 | case XML_SCHEMA_TYPE_FACET: |
| 5890 | TODO break; |
| 5891 | case XML_SCHEMA_TYPE_SIMPLE: |
| 5892 | xmlSchemaValidateSimpleType(ctxt, node); |
| 5893 | break; |
| 5894 | case XML_SCHEMA_TYPE_SEQUENCE: |
| 5895 | TODO break; |
| 5896 | case XML_SCHEMA_TYPE_CHOICE: |
| 5897 | TODO break; |
| 5898 | case XML_SCHEMA_TYPE_ALL: |
| 5899 | TODO break; |
| 5900 | case XML_SCHEMA_TYPE_SIMPLE_CONTENT: |
| 5901 | TODO break; |
| 5902 | case XML_SCHEMA_TYPE_COMPLEX_CONTENT: |
| 5903 | TODO break; |
| 5904 | case XML_SCHEMA_TYPE_UR: |
| 5905 | TODO break; |
| 5906 | case XML_SCHEMA_TYPE_RESTRICTION: |
| 5907 | /*xmlSchemaValidateRestrictionType(ctxt, node); */ |
| 5908 | TODO break; |
| 5909 | case XML_SCHEMA_TYPE_EXTENSION: |
| 5910 | TODO break; |
| 5911 | case XML_SCHEMA_TYPE_ATTRIBUTE: |
| 5912 | TODO break; |
| 5913 | case XML_SCHEMA_TYPE_GROUP: |
| 5914 | TODO break; |
| 5915 | case XML_SCHEMA_TYPE_NOTATION: |
| 5916 | TODO break; |
| 5917 | case XML_SCHEMA_TYPE_LIST: |
| 5918 | TODO break; |
| 5919 | case XML_SCHEMA_TYPE_UNION: |
| 5920 | TODO break; |
| 5921 | case XML_SCHEMA_FACET_MININCLUSIVE: |
| 5922 | TODO break; |
| 5923 | case XML_SCHEMA_FACET_MINEXCLUSIVE: |
| 5924 | TODO break; |
| 5925 | case XML_SCHEMA_FACET_MAXINCLUSIVE: |
| 5926 | TODO break; |
| 5927 | case XML_SCHEMA_FACET_MAXEXCLUSIVE: |
| 5928 | TODO break; |
| 5929 | case XML_SCHEMA_FACET_TOTALDIGITS: |
| 5930 | TODO break; |
| 5931 | case XML_SCHEMA_FACET_FRACTIONDIGITS: |
| 5932 | TODO break; |
| 5933 | case XML_SCHEMA_FACET_PATTERN: |
| 5934 | TODO break; |
| 5935 | case XML_SCHEMA_FACET_ENUMERATION: |
| 5936 | TODO break; |
| 5937 | case XML_SCHEMA_FACET_WHITESPACE: |
| 5938 | TODO break; |
| 5939 | case XML_SCHEMA_FACET_LENGTH: |
| 5940 | TODO break; |
| 5941 | case XML_SCHEMA_FACET_MAXLENGTH: |
| 5942 | TODO break; |
| 5943 | case XML_SCHEMA_FACET_MINLENGTH: |
| 5944 | TODO break; |
| 5945 | case XML_SCHEMA_TYPE_ATTRIBUTEGROUP: |
| 5946 | TODO break; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5947 | } |
| 5948 | xmlSchemaValidateAttributes(ctxt, node, type->attributes); |
| 5949 | |
| 5950 | if (ctxt->node == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5951 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5952 | ctxt->node = ctxt->node->next; |
| 5953 | ctxt->type = type->next; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5954 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5955 | } |
| 5956 | |
| 5957 | /** |
| 5958 | * xmlSchemaValidateType: |
| 5959 | * @ctxt: a schema validation context |
| 5960 | * @elem: an element |
| 5961 | * @type: the list of type declarations |
| 5962 | * |
| 5963 | * Validate the content of an element against the types. |
| 5964 | * |
| 5965 | * Returns 0 if the element is schemas valid, a positive error code |
| 5966 | * number otherwise and -1 in case of internal or API error. |
| 5967 | */ |
| 5968 | static int |
| 5969 | xmlSchemaValidateType(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5970 | xmlSchemaElementPtr elemDecl, xmlSchemaTypePtr type) |
| 5971 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5972 | xmlChar *nil; |
| 5973 | |
Daniel Veillard | 2db8c12 | 2003-07-08 12:16:59 +0000 | [diff] [blame] | 5974 | if ((elem == NULL) || (type == NULL) || (elemDecl == NULL)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5975 | return (0); |
Daniel Veillard | 2db8c12 | 2003-07-08 12:16:59 +0000 | [diff] [blame] | 5976 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5977 | /* |
| 5978 | * 3.3.4 : 2 |
| 5979 | */ |
| 5980 | if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5981 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_ISABSTRACT, "Element %s is abstract\n", elem->name, NULL); |
| 5982 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5983 | } |
| 5984 | /* |
| 5985 | * 3.3.4: 3 |
| 5986 | */ |
| 5987 | nil = xmlGetNsProp(elem, BAD_CAST "nil", xmlSchemaInstanceNs); |
| 5988 | if (elemDecl->flags & XML_SCHEMAS_ELEM_NILLABLE) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5989 | /* 3.3.4: 3.2 */ |
| 5990 | if (xmlStrEqual(nil, BAD_CAST "true")) { |
| 5991 | if (elem->children != NULL) { |
| 5992 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_NOTEMPTY, "Element %s is not empty\n", elem->name, NULL); |
| 5993 | return (ctxt->err); |
| 5994 | } |
| 5995 | if ((elemDecl->flags & XML_SCHEMAS_ELEM_FIXED) && |
| 5996 | (elemDecl->value != NULL)) { |
| 5997 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_HAVEDEFAULT, "Empty element %s cannot get a fixed value\n", elem->name, NULL); |
| 5998 | return (ctxt->err); |
| 5999 | } |
| 6000 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6001 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6002 | /* 3.3.4: 3.1 */ |
| 6003 | if (nil != NULL) { |
| 6004 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_NOTNILLABLE, "Element %s with xs:nil but not nillable\n", elem->name, NULL); |
| 6005 | xmlFree(nil); |
| 6006 | return (ctxt->err); |
| 6007 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6008 | } |
| 6009 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6010 | /* TODO 3.3.4: 4 if the element carries xs:type */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6011 | |
| 6012 | ctxt->type = elemDecl->subtypes; |
| 6013 | ctxt->node = elem->children; |
| 6014 | xmlSchemaValidateContent(ctxt, elem); |
| 6015 | xmlSchemaValidateAttributes(ctxt, elem, elemDecl->attributes); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6016 | |
| 6017 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6018 | } |
| 6019 | |
| 6020 | |
| 6021 | /** |
| 6022 | * xmlSchemaValidateAttributes: |
| 6023 | * @ctxt: a schema validation context |
| 6024 | * @elem: an element |
| 6025 | * @attributes: the list of attribute declarations |
| 6026 | * |
| 6027 | * Validate the attributes of an element. |
| 6028 | * |
| 6029 | * Returns 0 if the element is schemas valid, a positive error code |
| 6030 | * number otherwise and -1 in case of internal or API error. |
| 6031 | */ |
| 6032 | static int |
| 6033 | xmlSchemaValidateAttributes(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6034 | xmlSchemaAttributePtr attributes) |
| 6035 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6036 | int i, ret; |
| 6037 | xmlAttrPtr attr; |
| 6038 | xmlChar *value; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 6039 | xmlSchemaAttributeGroupPtr group = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6040 | |
| 6041 | if (attributes == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6042 | return (0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6043 | while (attributes != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6044 | /* |
| 6045 | * Handle attribute groups |
| 6046 | */ |
| 6047 | if (attributes->type == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) { |
| 6048 | group = (xmlSchemaAttributeGroupPtr) attributes; |
| 6049 | xmlSchemaValidateAttributes(ctxt, elem, group->attributes); |
| 6050 | attributes = group->next; |
| 6051 | continue; |
| 6052 | } |
| 6053 | for (i = ctxt->attrBase; i < ctxt->attrNr; i++) { |
| 6054 | attr = ctxt->attr[i].attr; |
| 6055 | if (attr == NULL) |
| 6056 | continue; |
| 6057 | if (attributes->ref != NULL) { |
| 6058 | if (!xmlStrEqual(attr->name, attributes->ref)) |
| 6059 | continue; |
| 6060 | if (attr->ns != NULL) { |
| 6061 | if ((attributes->refNs == NULL) || |
| 6062 | (!xmlStrEqual(attr->ns->href, attributes->refNs))) |
| 6063 | continue; |
| 6064 | } else if (attributes->refNs != NULL) { |
| 6065 | continue; |
| 6066 | } |
| 6067 | } else { |
| 6068 | if (!xmlStrEqual(attr->name, attributes->name)) |
| 6069 | continue; |
| 6070 | /* |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6071 | * handle the namespaces checks here |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6072 | */ |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6073 | if (attr->ns == NULL) { |
| 6074 | /* |
| 6075 | * accept an unqualified attribute only if the declaration |
| 6076 | * is unqualified or if the schemas allowed it. |
| 6077 | */ |
| 6078 | if ((attributes->targetNamespace != NULL) && |
| 6079 | ((attributes->flags & XML_SCHEMAS_ATTR_NSDEFAULT) == 0)) |
| 6080 | continue; |
| 6081 | } else { |
| 6082 | if (attributes->targetNamespace == NULL) |
| 6083 | continue; |
| 6084 | if (!xmlStrEqual(attributes->targetNamespace, |
| 6085 | attr->ns->href)) |
| 6086 | continue; |
| 6087 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6088 | } |
| 6089 | ctxt->cur = (xmlNodePtr) attributes; |
| 6090 | if (attributes->subtypes == NULL) { |
| 6091 | xmlSchemaVErr(ctxt, (xmlNodePtr) attr, XML_SCHEMAS_ERR_INTERNAL, "Internal error: attribute %s type not resolved\n", attr->name, NULL); |
| 6092 | continue; |
| 6093 | } |
| 6094 | value = xmlNodeListGetString(elem->doc, attr->children, 1); |
| 6095 | ret = xmlSchemaValidateSimpleValue(ctxt, attributes->subtypes, |
| 6096 | value); |
| 6097 | if (ret != 0) { |
| 6098 | xmlSchemaVErr(ctxt, (xmlNodePtr) attr, XML_SCHEMAS_ERR_ATTRINVALID, "attribute %s on %s does not match type\n", attr->name, elem->name); |
| 6099 | } else { |
| 6100 | ctxt->attr[i].state = XML_SCHEMAS_ATTR_CHECKED; |
| 6101 | } |
| 6102 | if (value != NULL) { |
| 6103 | xmlFree(value); |
| 6104 | } |
| 6105 | } |
| 6106 | attributes = attributes->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6107 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6108 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6109 | } |
| 6110 | |
| 6111 | /** |
| 6112 | * xmlSchemaValidateElement: |
| 6113 | * @ctxt: a schema validation context |
| 6114 | * @elem: an element |
| 6115 | * |
| 6116 | * Validate an element in a tree |
| 6117 | * |
| 6118 | * Returns 0 if the element is schemas valid, a positive error code |
| 6119 | * number otherwise and -1 in case of internal or API error. |
| 6120 | */ |
| 6121 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6122 | xmlSchemaValidateElement(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem) |
| 6123 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6124 | xmlSchemaElementPtr elemDecl; |
| 6125 | int ret, attrBase; |
| 6126 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6127 | if (elem->ns != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6128 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6129 | elem->name, elem->ns->href, NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6130 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6131 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6132 | elem->name, NULL, NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6133 | } |
| 6134 | /* |
| 6135 | * special case whe elementFormDefault is unqualified for top-level elem. |
| 6136 | */ |
| 6137 | if ((elemDecl == NULL) && (elem->ns != NULL) && |
| 6138 | (elem->parent != NULL) && (elem->parent->type != XML_ELEMENT_NODE) && |
| 6139 | (xmlStrEqual(ctxt->schema->targetNamespace, elem->ns->href)) && |
| 6140 | ((ctxt->schema->flags & XML_SCHEMAS_QUALIF_ELEM) == 0)) { |
| 6141 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6142 | elem->name, NULL, NULL); |
| 6143 | } |
| 6144 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6145 | /* |
| 6146 | * 3.3.4 : 1 |
| 6147 | */ |
| 6148 | if (elemDecl == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6149 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_UNDECLAREDELEM, "Element %s not declared\n", elem->name, NULL); |
| 6150 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6151 | } |
| 6152 | if (elemDecl->subtypes == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6153 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_NOTYPE, "Element %s has no type\n", elem->name, NULL); |
| 6154 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6155 | } |
| 6156 | /* |
| 6157 | * Verify the attributes |
| 6158 | */ |
| 6159 | attrBase = ctxt->attrBase; |
| 6160 | ctxt->attrBase = ctxt->attrNr; |
| 6161 | xmlSchemaRegisterAttributes(ctxt, elem->properties); |
| 6162 | xmlSchemaValidateAttributes(ctxt, elem, elemDecl->attributes); |
| 6163 | /* |
| 6164 | * Verify the element content recursively |
| 6165 | */ |
| 6166 | if (elemDecl->contModel != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6167 | ctxt->regexp = xmlRegNewExecCtxt(elemDecl->contModel, |
| 6168 | (xmlRegExecCallbacks) |
| 6169 | xmlSchemaValidateCallback, ctxt); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6170 | #ifdef DEBUG_AUTOMATA |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6171 | xmlGenericError(xmlGenericErrorContext, "====> %s\n", elem->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6172 | #endif |
| 6173 | } |
| 6174 | xmlSchemaValidateType(ctxt, elem, elemDecl, elemDecl->subtypes); |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 6175 | if (elemDecl->contModel != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6176 | ret = xmlRegExecPushString(ctxt->regexp, NULL, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6177 | #ifdef DEBUG_AUTOMATA |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6178 | xmlGenericError(xmlGenericErrorContext, |
| 6179 | "====> %s : %d\n", elem->name, ret); |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 6180 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6181 | if (ret == 0) { |
| 6182 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_ELEMCONT, "Element %s content check failed\n", elem->name, NULL); |
| 6183 | } else if (ret < 0) { |
| 6184 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_ELEMCONT, "Element %s content check failed\n", elem->name, NULL); |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 6185 | #ifdef DEBUG_CONTENT |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6186 | } else { |
| 6187 | xmlGenericError(xmlGenericErrorContext, |
| 6188 | "Element %s content check succeeded\n", |
| 6189 | elem->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6190 | |
| 6191 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6192 | } |
| 6193 | xmlRegFreeExecCtxt(ctxt->regexp); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6194 | } |
| 6195 | /* |
| 6196 | * Verify that all attributes were Schemas-validated |
| 6197 | */ |
| 6198 | xmlSchemaCheckAttributes(ctxt, elem); |
| 6199 | ctxt->attrNr = ctxt->attrBase; |
| 6200 | ctxt->attrBase = attrBase; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6201 | |
| 6202 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6203 | } |
| 6204 | |
| 6205 | /** |
| 6206 | * xmlSchemaValidateDocument: |
| 6207 | * @ctxt: a schema validation context |
| 6208 | * @doc: a parsed document tree |
| 6209 | * |
| 6210 | * Validate a document tree in memory. |
| 6211 | * |
| 6212 | * Returns 0 if the document is schemas valid, a positive error code |
| 6213 | * number otherwise and -1 in case of internal or API error. |
| 6214 | */ |
| 6215 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6216 | xmlSchemaValidateDocument(xmlSchemaValidCtxtPtr ctxt, xmlDocPtr doc) |
| 6217 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6218 | xmlNodePtr root; |
| 6219 | xmlSchemaElementPtr elemDecl; |
| 6220 | |
| 6221 | root = xmlDocGetRootElement(doc); |
| 6222 | if (root == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6223 | xmlSchemaVErr(ctxt, (xmlNodePtr) doc, XML_SCHEMAS_ERR_NOROOT, "document has no root\n", NULL, NULL); |
| 6224 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6225 | } |
| 6226 | if (root->ns != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6227 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6228 | root->name, root->ns->href, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6229 | else |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6230 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6231 | root->name, NULL, NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6232 | /* |
| 6233 | * special case whe elementFormDefault is unqualified for top-level elem. |
| 6234 | */ |
| 6235 | if ((elemDecl == NULL) && (root->ns != NULL) && |
| 6236 | (xmlStrEqual(ctxt->schema->targetNamespace, root->ns->href)) && |
| 6237 | ((ctxt->schema->flags & XML_SCHEMAS_QUALIF_ELEM) == 0)) { |
| 6238 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6239 | root->name, NULL, NULL); |
| 6240 | } |
| 6241 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6242 | if (elemDecl == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6243 | xmlSchemaVErr(ctxt, root, XML_SCHEMAS_ERR_UNDECLAREDELEM, "Element %s not declared\n", root->name, NULL); |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 6244 | } else if ((elemDecl->flags & XML_SCHEMAS_ELEM_TOPLEVEL) == 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6245 | xmlSchemaVErr(ctxt, root, XML_SCHEMAS_ERR_NOTTOPLEVEL, "Root element %s not toplevel\n", root->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6246 | } |
| 6247 | /* |
| 6248 | * Okay, start the recursive validation |
| 6249 | */ |
| 6250 | xmlSchemaValidateElement(ctxt, root); |
| 6251 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6252 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6253 | } |
| 6254 | |
| 6255 | /************************************************************************ |
| 6256 | * * |
| 6257 | * SAX Validation code * |
| 6258 | * * |
| 6259 | ************************************************************************/ |
| 6260 | |
| 6261 | /************************************************************************ |
| 6262 | * * |
| 6263 | * Validation interfaces * |
| 6264 | * * |
| 6265 | ************************************************************************/ |
| 6266 | |
| 6267 | /** |
| 6268 | * xmlSchemaNewValidCtxt: |
| 6269 | * @schema: a precompiled XML Schemas |
| 6270 | * |
| 6271 | * Create an XML Schemas validation context based on the given schema |
| 6272 | * |
| 6273 | * Returns the validation context or NULL in case of error |
| 6274 | */ |
| 6275 | xmlSchemaValidCtxtPtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6276 | xmlSchemaNewValidCtxt(xmlSchemaPtr schema) |
| 6277 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6278 | xmlSchemaValidCtxtPtr ret; |
| 6279 | |
| 6280 | ret = (xmlSchemaValidCtxtPtr) xmlMalloc(sizeof(xmlSchemaValidCtxt)); |
| 6281 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6282 | xmlSchemaVErrMemory(NULL, "allocating validation context", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6283 | return (NULL); |
| 6284 | } |
| 6285 | memset(ret, 0, sizeof(xmlSchemaValidCtxt)); |
| 6286 | ret->schema = schema; |
| 6287 | ret->attrNr = 0; |
| 6288 | ret->attrMax = 10; |
| 6289 | ret->attr = (xmlSchemaAttrStatePtr) xmlMalloc(ret->attrMax * |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6290 | sizeof |
| 6291 | (xmlSchemaAttrState)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6292 | if (ret->attr == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6293 | xmlSchemaVErrMemory(NULL, "allocating validation context", NULL); |
| 6294 | free(ret); |
| 6295 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6296 | } |
| 6297 | memset(ret->attr, 0, ret->attrMax * sizeof(xmlSchemaAttrState)); |
| 6298 | return (ret); |
| 6299 | } |
| 6300 | |
| 6301 | /** |
| 6302 | * xmlSchemaFreeValidCtxt: |
| 6303 | * @ctxt: the schema validation context |
| 6304 | * |
| 6305 | * Free the resources associated to the schema validation context |
| 6306 | */ |
| 6307 | void |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6308 | xmlSchemaFreeValidCtxt(xmlSchemaValidCtxtPtr ctxt) |
| 6309 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6310 | if (ctxt == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6311 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6312 | if (ctxt->attr != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6313 | xmlFree(ctxt->attr); |
Daniel Veillard | 88c5891 | 2002-04-23 07:12:20 +0000 | [diff] [blame] | 6314 | if (ctxt->value != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6315 | xmlSchemaFreeValue(ctxt->value); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6316 | xmlFree(ctxt); |
| 6317 | } |
| 6318 | |
| 6319 | /** |
| 6320 | * xmlSchemaSetValidErrors: |
| 6321 | * @ctxt: a schema validation context |
| 6322 | * @err: the error function |
| 6323 | * @warn: the warning function |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 6324 | * @ctx: the functions context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6325 | * |
| 6326 | * Set the error and warning callback informations |
| 6327 | */ |
| 6328 | void |
| 6329 | xmlSchemaSetValidErrors(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6330 | xmlSchemaValidityErrorFunc err, |
| 6331 | xmlSchemaValidityWarningFunc warn, void *ctx) |
| 6332 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6333 | if (ctxt == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6334 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6335 | ctxt->error = err; |
| 6336 | ctxt->warning = warn; |
| 6337 | ctxt->userData = ctx; |
| 6338 | } |
| 6339 | |
| 6340 | /** |
| 6341 | * xmlSchemaValidateDoc: |
| 6342 | * @ctxt: a schema validation context |
| 6343 | * @doc: a parsed document tree |
| 6344 | * |
| 6345 | * Validate a document tree in memory. |
| 6346 | * |
| 6347 | * Returns 0 if the document is schemas valid, a positive error code |
| 6348 | * number otherwise and -1 in case of internal or API error. |
| 6349 | */ |
| 6350 | int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6351 | xmlSchemaValidateDoc(xmlSchemaValidCtxtPtr ctxt, xmlDocPtr doc) |
| 6352 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6353 | int ret; |
| 6354 | |
| 6355 | if ((ctxt == NULL) || (doc == NULL)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6356 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6357 | |
| 6358 | ctxt->doc = doc; |
| 6359 | ret = xmlSchemaValidateDocument(ctxt, doc); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6360 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6361 | } |
| 6362 | |
| 6363 | /** |
| 6364 | * xmlSchemaValidateStream: |
| 6365 | * @ctxt: a schema validation context |
| 6366 | * @input: the input to use for reading the data |
| 6367 | * @enc: an optional encoding information |
| 6368 | * @sax: a SAX handler for the resulting events |
| 6369 | * @user_data: the context to provide to the SAX handler. |
| 6370 | * |
| 6371 | * Validate a document tree in memory. |
| 6372 | * |
| 6373 | * Returns 0 if the document is schemas valid, a positive error code |
| 6374 | * number otherwise and -1 in case of internal or API error. |
| 6375 | */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6376 | int |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6377 | xmlSchemaValidateStream(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6378 | xmlParserInputBufferPtr input, xmlCharEncoding enc, |
| 6379 | xmlSAXHandlerPtr sax, void *user_data) |
| 6380 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6381 | if ((ctxt == NULL) || (input == NULL)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6382 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6383 | ctxt->input = input; |
| 6384 | ctxt->enc = enc; |
| 6385 | ctxt->sax = sax; |
| 6386 | ctxt->user_data = user_data; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6387 | TODO return (0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6388 | } |
| 6389 | |
| 6390 | #endif /* LIBXML_SCHEMAS_ENABLED */ |