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 | |
| 10 | #define IN_LIBXML |
| 11 | #include "libxml.h" |
| 12 | |
| 13 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 14 | |
| 15 | #include <string.h> |
| 16 | #include <libxml/xmlmemory.h> |
| 17 | #include <libxml/parser.h> |
| 18 | #include <libxml/parserInternals.h> |
| 19 | #include <libxml/hash.h> |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 20 | #include <libxml/uri.h> |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 21 | |
| 22 | #include <libxml/xmlschemas.h> |
| 23 | #include <libxml/schemasInternals.h> |
| 24 | #include <libxml/xmlschemastypes.h> |
| 25 | #include <libxml/xmlautomata.h> |
| 26 | #include <libxml/xmlregexp.h> |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 27 | #include <libxml/dict.h> |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 28 | |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 29 | /* #define DEBUG 1 */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 30 | |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 31 | /* #define DEBUG_CONTENT 1 */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 32 | |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 33 | /* #define DEBUG_TYPE 1 */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 34 | |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 35 | /* #define DEBUG_CONTENT_REGEXP 1 */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 36 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 37 | /* #define DEBUG_AUTOMATA 1 */ |
| 38 | |
| 39 | #define UNBOUNDED (1 << 30) |
| 40 | #define TODO \ |
| 41 | xmlGenericError(xmlGenericErrorContext, \ |
| 42 | "Unimplemented block at %s:%d\n", \ |
| 43 | __FILE__, __LINE__); |
| 44 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 45 | #define XML_SCHEMAS_DEFAULT_NAMESPACE (const xmlChar *)"the default namespace" |
| 46 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 47 | /* |
| 48 | * The XML Schemas namespaces |
| 49 | */ |
| 50 | static const xmlChar *xmlSchemaNs = (const xmlChar *) |
| 51 | "http://www.w3.org/2001/XMLSchema"; |
| 52 | |
| 53 | static const xmlChar *xmlSchemaInstanceNs = (const xmlChar *) |
| 54 | "http://www.w3.org/2001/XMLSchema-instance"; |
| 55 | |
| 56 | #define IS_SCHEMA(node, type) \ |
| 57 | ((node != NULL) && (node->ns != NULL) && \ |
| 58 | (xmlStrEqual(node->name, (const xmlChar *) type)) && \ |
| 59 | (xmlStrEqual(node->ns->href, xmlSchemaNs))) |
| 60 | |
| 61 | #define XML_SCHEMAS_PARSE_ERROR 1 |
| 62 | |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 63 | #define SCHEMAS_PARSE_OPTIONS XML_PARSE_NOENT |
| 64 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 65 | struct _xmlSchemaParserCtxt { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 66 | void *userData; /* user specific data block */ |
| 67 | xmlSchemaValidityErrorFunc error; /* the callback in case of errors */ |
| 68 | xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */ |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 69 | xmlSchemaValidError err; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 70 | int nberrors; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 71 | xmlStructuredErrorFunc serror; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 72 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 73 | xmlSchemaPtr topschema; /* The main schema */ |
| 74 | xmlHashTablePtr namespaces; /* Hash table of namespaces to schemas */ |
| 75 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 76 | xmlSchemaPtr schema; /* The schema in use */ |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 77 | const xmlChar *container; /* the current element, group, ... */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 78 | int counter; |
| 79 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 80 | const xmlChar *URL; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 81 | xmlDocPtr doc; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 82 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 83 | const char *buffer; |
| 84 | int size; |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 85 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 86 | /* |
| 87 | * Used to build complex element content models |
| 88 | */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 89 | xmlAutomataPtr am; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 90 | xmlAutomataStatePtr start; |
| 91 | xmlAutomataStatePtr end; |
| 92 | xmlAutomataStatePtr state; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 93 | |
| 94 | xmlDictPtr dict; /* dictionnary for interned string names */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | |
| 98 | #define XML_SCHEMAS_ATTR_UNKNOWN 1 |
| 99 | #define XML_SCHEMAS_ATTR_CHECKED 2 |
| 100 | |
| 101 | typedef struct _xmlSchemaAttrState xmlSchemaAttrState; |
| 102 | typedef xmlSchemaAttrState *xmlSchemaAttrStatePtr; |
| 103 | struct _xmlSchemaAttrState { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 104 | xmlAttrPtr attr; |
| 105 | int state; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | /** |
| 109 | * xmlSchemaValidCtxt: |
| 110 | * |
| 111 | * A Schemas validation context |
| 112 | */ |
| 113 | |
| 114 | struct _xmlSchemaValidCtxt { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 115 | void *userData; /* user specific data block */ |
| 116 | xmlSchemaValidityErrorFunc error; /* the callback in case of errors */ |
| 117 | xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */ |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 118 | xmlStructuredErrorFunc serror; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 119 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 120 | xmlSchemaPtr schema; /* The schema in use */ |
| 121 | xmlDocPtr doc; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 122 | xmlParserInputBufferPtr input; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 123 | xmlCharEncoding enc; |
| 124 | xmlSAXHandlerPtr sax; |
| 125 | void *user_data; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 126 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 127 | xmlDocPtr myDoc; |
| 128 | int err; |
| 129 | int nberrors; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 130 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 131 | xmlNodePtr node; |
| 132 | xmlNodePtr cur; |
| 133 | xmlSchemaTypePtr type; |
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 | xmlRegExecCtxtPtr regexp; |
| 136 | xmlSchemaValPtr value; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 137 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 138 | int attrNr; |
| 139 | int attrBase; |
| 140 | int attrMax; |
| 141 | xmlSchemaAttrStatePtr attr; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 144 | /* |
| 145 | * These are the entries in the schemas importSchemas hash table |
| 146 | */ |
| 147 | typedef struct _xmlSchemaImport xmlSchemaImport; |
| 148 | typedef xmlSchemaImport *xmlSchemaImportPtr; |
| 149 | struct _xmlSchemaImport { |
| 150 | const xmlChar *schemaLocation; |
| 151 | xmlSchemaPtr schema; |
| 152 | }; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 153 | |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 154 | /* |
| 155 | * These are the entries associated to includes in a schemas |
| 156 | */ |
| 157 | typedef struct _xmlSchemaInclude xmlSchemaInclude; |
| 158 | typedef xmlSchemaInclude *xmlSchemaIncludePtr; |
| 159 | struct _xmlSchemaInclude { |
| 160 | xmlSchemaIncludePtr next; |
| 161 | |
| 162 | const xmlChar *schemaLocation; |
| 163 | xmlDocPtr doc; |
| 164 | }; |
| 165 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 166 | /************************************************************************ |
| 167 | * * |
| 168 | * Some predeclarations * |
| 169 | * * |
| 170 | ************************************************************************/ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 171 | static int xmlSchemaValidateSimpleValue(xmlSchemaValidCtxtPtr ctxt, |
| 172 | xmlSchemaTypePtr type, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 173 | const xmlChar * value); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 174 | |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 175 | static int xmlSchemaParseInclude(xmlSchemaParserCtxtPtr ctxt, |
| 176 | xmlSchemaPtr schema, |
| 177 | xmlNodePtr node); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 178 | /************************************************************************ |
| 179 | * * |
| 180 | * Datatype error handlers * |
| 181 | * * |
| 182 | ************************************************************************/ |
| 183 | |
| 184 | /** |
| 185 | * xmlSchemaPErrMemory: |
| 186 | * @node: a context node |
| 187 | * @extra: extra informations |
| 188 | * |
| 189 | * Handle an out of memory condition |
| 190 | */ |
| 191 | static void |
| 192 | xmlSchemaPErrMemory(xmlSchemaParserCtxtPtr ctxt, |
| 193 | const char *extra, xmlNodePtr node) |
| 194 | { |
| 195 | if (ctxt != NULL) |
| 196 | ctxt->nberrors++; |
| 197 | __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, node, NULL, |
| 198 | extra); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * xmlSchemaPErr: |
| 203 | * @ctxt: the parsing context |
| 204 | * @node: the context node |
| 205 | * @error: the error code |
| 206 | * @msg: the error message |
| 207 | * @str1: extra data |
| 208 | * @str2: extra data |
| 209 | * |
| 210 | * Handle a parser error |
| 211 | */ |
| 212 | static void |
| 213 | xmlSchemaPErr(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error, |
| 214 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 215 | { |
| 216 | xmlGenericErrorFunc channel = NULL; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 217 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 218 | void *data = NULL; |
| 219 | |
| 220 | if (ctxt != NULL) { |
| 221 | ctxt->nberrors++; |
| 222 | channel = ctxt->error; |
| 223 | data = ctxt->userData; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 224 | schannel = ctxt->serror; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 225 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 226 | __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 227 | error, XML_ERR_ERROR, NULL, 0, |
| 228 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 229 | msg, str1, str2); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * xmlSchemaPErr2: |
| 234 | * @ctxt: the parsing context |
| 235 | * @node: the context node |
| 236 | * @node: the current child |
| 237 | * @error: the error code |
| 238 | * @msg: the error message |
| 239 | * @str1: extra data |
| 240 | * @str2: extra data |
| 241 | * |
| 242 | * Handle a parser error |
| 243 | */ |
| 244 | static void |
| 245 | xmlSchemaPErr2(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, |
| 246 | xmlNodePtr child, int error, |
| 247 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 248 | { |
| 249 | if (child != NULL) |
| 250 | xmlSchemaPErr(ctxt, child, error, msg, str1, str2); |
| 251 | else |
| 252 | xmlSchemaPErr(ctxt, node, error, msg, str1, str2); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * xmlSchemaVTypeErrMemory: |
| 257 | * @node: a context node |
| 258 | * @extra: extra informations |
| 259 | * |
| 260 | * Handle an out of memory condition |
| 261 | */ |
| 262 | static void |
| 263 | xmlSchemaVErrMemory(xmlSchemaValidCtxtPtr ctxt, |
| 264 | const char *extra, xmlNodePtr node) |
| 265 | { |
| 266 | if (ctxt != NULL) { |
| 267 | ctxt->nberrors++; |
| 268 | ctxt->err = XML_SCHEMAS_ERR_INTERNAL; |
| 269 | } |
| 270 | __xmlSimpleError(XML_FROM_SCHEMASV, XML_ERR_NO_MEMORY, node, NULL, |
| 271 | extra); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * xmlSchemaVErr3: |
| 276 | * @ctxt: the validation context |
| 277 | * @node: the context node |
| 278 | * @error: the error code |
| 279 | * @msg: the error message |
| 280 | * @str1: extra data |
| 281 | * @str2: extra data |
| 282 | * @str3: extra data |
| 283 | * |
| 284 | * Handle a validation error |
| 285 | */ |
| 286 | static void |
| 287 | xmlSchemaVErr3(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node, int error, |
| 288 | const char *msg, const xmlChar *str1, const xmlChar *str2, |
| 289 | const xmlChar *str3) |
| 290 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 291 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 292 | xmlGenericErrorFunc channel = NULL; |
| 293 | void *data = NULL; |
| 294 | |
| 295 | if (ctxt != NULL) { |
| 296 | ctxt->nberrors++; |
| 297 | ctxt->err = error; |
| 298 | channel = ctxt->error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 299 | schannel = ctxt->serror; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 300 | data = ctxt->userData; |
| 301 | } |
| 302 | /* reajust to global error numbers */ |
| 303 | error += XML_SCHEMAV_NOROOT - XML_SCHEMAS_ERR_NOROOT; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 304 | __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASV, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 305 | error, XML_ERR_ERROR, NULL, 0, |
| 306 | (const char *) str1, (const char *) str2, |
| 307 | (const char *) str3, 0, 0, |
| 308 | msg, str1, str2, str3); |
| 309 | } |
| 310 | /** |
| 311 | * xmlSchemaVErr: |
| 312 | * @ctxt: the validation context |
| 313 | * @node: the context node |
| 314 | * @error: the error code |
| 315 | * @msg: the error message |
| 316 | * @str1: extra data |
| 317 | * @str2: extra data |
| 318 | * |
| 319 | * Handle a validation error |
| 320 | */ |
| 321 | static void |
| 322 | xmlSchemaVErr(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node, int error, |
| 323 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 324 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 325 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 326 | xmlGenericErrorFunc channel = NULL; |
| 327 | void *data = NULL; |
| 328 | |
| 329 | if (ctxt != NULL) { |
| 330 | ctxt->nberrors++; |
| 331 | ctxt->err = error; |
| 332 | channel = ctxt->error; |
| 333 | data = ctxt->userData; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 334 | schannel = ctxt->serror; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 335 | } |
| 336 | /* reajust to global error numbers */ |
| 337 | error += XML_SCHEMAV_NOROOT - XML_SCHEMAS_ERR_NOROOT; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 338 | __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASV, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 339 | error, XML_ERR_ERROR, NULL, 0, |
| 340 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 341 | msg, str1, str2); |
| 342 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 343 | |
| 344 | /************************************************************************ |
| 345 | * * |
| 346 | * Allocation functions * |
| 347 | * * |
| 348 | ************************************************************************/ |
| 349 | |
| 350 | /** |
| 351 | * xmlSchemaNewSchema: |
| 352 | * @ctxt: a schema validation context (optional) |
| 353 | * |
| 354 | * Allocate a new Schema structure. |
| 355 | * |
| 356 | * Returns the newly allocated structure or NULL in case or error |
| 357 | */ |
| 358 | static xmlSchemaPtr |
| 359 | xmlSchemaNewSchema(xmlSchemaParserCtxtPtr ctxt) |
| 360 | { |
| 361 | xmlSchemaPtr ret; |
| 362 | |
| 363 | ret = (xmlSchemaPtr) xmlMalloc(sizeof(xmlSchema)); |
| 364 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 365 | xmlSchemaPErrMemory(ctxt, "allocating schema", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 366 | return (NULL); |
| 367 | } |
| 368 | memset(ret, 0, sizeof(xmlSchema)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 369 | xmlDictReference(ctxt->dict); |
| 370 | ret->dict = ctxt->dict; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 371 | |
| 372 | return (ret); |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * xmlSchemaNewFacet: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 377 | * |
| 378 | * Allocate a new Facet structure. |
| 379 | * |
| 380 | * Returns the newly allocated structure or NULL in case or error |
| 381 | */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 382 | xmlSchemaFacetPtr |
| 383 | xmlSchemaNewFacet(void) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 384 | { |
| 385 | xmlSchemaFacetPtr ret; |
| 386 | |
| 387 | ret = (xmlSchemaFacetPtr) xmlMalloc(sizeof(xmlSchemaFacet)); |
| 388 | if (ret == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 389 | return (NULL); |
| 390 | } |
| 391 | memset(ret, 0, sizeof(xmlSchemaFacet)); |
| 392 | |
| 393 | return (ret); |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * xmlSchemaNewAnnot: |
| 398 | * @ctxt: a schema validation context (optional) |
| 399 | * @node: a node |
| 400 | * |
| 401 | * Allocate a new annotation structure. |
| 402 | * |
| 403 | * Returns the newly allocated structure or NULL in case or error |
| 404 | */ |
| 405 | static xmlSchemaAnnotPtr |
| 406 | xmlSchemaNewAnnot(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node) |
| 407 | { |
| 408 | xmlSchemaAnnotPtr ret; |
| 409 | |
| 410 | ret = (xmlSchemaAnnotPtr) xmlMalloc(sizeof(xmlSchemaAnnot)); |
| 411 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 412 | xmlSchemaPErrMemory(ctxt, "allocating annotation", node); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 413 | return (NULL); |
| 414 | } |
| 415 | memset(ret, 0, sizeof(xmlSchemaAnnot)); |
| 416 | ret->content = node; |
| 417 | return (ret); |
| 418 | } |
| 419 | |
| 420 | /** |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 421 | * xmlSchemaFreeAnnot: |
| 422 | * @annot: a schema type structure |
| 423 | * |
| 424 | * Deallocate a annotation structure |
| 425 | */ |
| 426 | static void |
| 427 | xmlSchemaFreeAnnot(xmlSchemaAnnotPtr annot) |
| 428 | { |
| 429 | if (annot == NULL) |
| 430 | return; |
| 431 | xmlFree(annot); |
| 432 | } |
| 433 | |
| 434 | /** |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 435 | * xmlSchemaFreeImport: |
| 436 | * @import: a schema import structure |
| 437 | * |
| 438 | * Deallocate an import structure |
| 439 | */ |
| 440 | static void |
| 441 | xmlSchemaFreeImport(xmlSchemaImportPtr import) |
| 442 | { |
| 443 | if (import == NULL) |
| 444 | return; |
| 445 | |
| 446 | xmlSchemaFree(import->schema); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 447 | xmlFree(import); |
| 448 | } |
| 449 | |
| 450 | /** |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 451 | * xmlSchemaFreeInclude: |
| 452 | * @include: a schema include structure |
| 453 | * |
| 454 | * Deallocate an include structure |
| 455 | */ |
| 456 | static void |
| 457 | xmlSchemaFreeInclude(xmlSchemaIncludePtr include) |
| 458 | { |
| 459 | if (include == NULL) |
| 460 | return; |
| 461 | |
| 462 | xmlFreeDoc(include->doc); |
| 463 | xmlFree(include); |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * xmlSchemaFreeIncludeList: |
| 468 | * @includes: a schema include list |
| 469 | * |
| 470 | * Deallocate an include structure |
| 471 | */ |
| 472 | static void |
| 473 | xmlSchemaFreeIncludeList(xmlSchemaIncludePtr includes) |
| 474 | { |
| 475 | xmlSchemaIncludePtr next; |
| 476 | |
| 477 | while (includes != NULL) { |
| 478 | next = includes->next; |
| 479 | xmlSchemaFreeInclude(includes); |
| 480 | includes = next; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 485 | * xmlSchemaFreeNotation: |
| 486 | * @schema: a schema notation structure |
| 487 | * |
| 488 | * Deallocate a Schema Notation structure. |
| 489 | */ |
| 490 | static void |
| 491 | xmlSchemaFreeNotation(xmlSchemaNotationPtr nota) |
| 492 | { |
| 493 | if (nota == NULL) |
| 494 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 495 | xmlFree(nota); |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * xmlSchemaFreeAttribute: |
| 500 | * @schema: a schema attribute structure |
| 501 | * |
| 502 | * Deallocate a Schema Attribute structure. |
| 503 | */ |
| 504 | static void |
| 505 | xmlSchemaFreeAttribute(xmlSchemaAttributePtr attr) |
| 506 | { |
| 507 | if (attr == NULL) |
| 508 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 509 | xmlFree(attr); |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * xmlSchemaFreeAttributeGroup: |
| 514 | * @schema: a schema attribute group structure |
| 515 | * |
| 516 | * Deallocate a Schema Attribute Group structure. |
| 517 | */ |
| 518 | static void |
| 519 | xmlSchemaFreeAttributeGroup(xmlSchemaAttributeGroupPtr attr) |
| 520 | { |
| 521 | if (attr == NULL) |
| 522 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 523 | xmlFree(attr); |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * xmlSchemaFreeElement: |
| 528 | * @schema: a schema element structure |
| 529 | * |
| 530 | * Deallocate a Schema Element structure. |
| 531 | */ |
| 532 | static void |
| 533 | xmlSchemaFreeElement(xmlSchemaElementPtr elem) |
| 534 | { |
| 535 | if (elem == NULL) |
| 536 | return; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 537 | if (elem->annot != NULL) |
| 538 | xmlSchemaFreeAnnot(elem->annot); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 539 | if (elem->contModel != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 540 | xmlRegFreeRegexp(elem->contModel); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 541 | xmlFree(elem); |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * xmlSchemaFreeFacet: |
| 546 | * @facet: a schema facet structure |
| 547 | * |
| 548 | * Deallocate a Schema Facet structure. |
| 549 | */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 550 | void |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 551 | xmlSchemaFreeFacet(xmlSchemaFacetPtr facet) |
| 552 | { |
| 553 | if (facet == NULL) |
| 554 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 555 | if (facet->val != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 556 | xmlSchemaFreeValue(facet->val); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 557 | if (facet->regexp != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 558 | xmlRegFreeRegexp(facet->regexp); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 559 | if (facet->annot != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 560 | xmlSchemaFreeAnnot(facet->annot); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 561 | xmlFree(facet); |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * xmlSchemaFreeType: |
| 566 | * @type: a schema type structure |
| 567 | * |
| 568 | * Deallocate a Schema Type structure. |
| 569 | */ |
| 570 | void |
| 571 | xmlSchemaFreeType(xmlSchemaTypePtr type) |
| 572 | { |
| 573 | if (type == NULL) |
| 574 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 575 | if (type->annot != NULL) |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 576 | xmlSchemaFreeAnnot(type->annot); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 577 | if (type->facets != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 578 | xmlSchemaFacetPtr facet, next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 579 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 580 | facet = type->facets; |
| 581 | while (facet != NULL) { |
| 582 | next = facet->next; |
| 583 | xmlSchemaFreeFacet(facet); |
| 584 | facet = next; |
| 585 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 586 | } |
| 587 | xmlFree(type); |
| 588 | } |
| 589 | |
| 590 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 591 | * xmlSchemaFree: |
| 592 | * @schema: a schema structure |
| 593 | * |
| 594 | * Deallocate a Schema structure. |
| 595 | */ |
| 596 | void |
| 597 | xmlSchemaFree(xmlSchemaPtr schema) |
| 598 | { |
| 599 | if (schema == NULL) |
| 600 | return; |
| 601 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 602 | if (schema->notaDecl != NULL) |
| 603 | xmlHashFree(schema->notaDecl, |
| 604 | (xmlHashDeallocator) xmlSchemaFreeNotation); |
| 605 | if (schema->attrDecl != NULL) |
| 606 | xmlHashFree(schema->attrDecl, |
| 607 | (xmlHashDeallocator) xmlSchemaFreeAttribute); |
| 608 | if (schema->attrgrpDecl != NULL) |
| 609 | xmlHashFree(schema->attrgrpDecl, |
| 610 | (xmlHashDeallocator) xmlSchemaFreeAttributeGroup); |
| 611 | if (schema->elemDecl != NULL) |
| 612 | xmlHashFree(schema->elemDecl, |
| 613 | (xmlHashDeallocator) xmlSchemaFreeElement); |
| 614 | if (schema->typeDecl != NULL) |
| 615 | xmlHashFree(schema->typeDecl, |
| 616 | (xmlHashDeallocator) xmlSchemaFreeType); |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 617 | if (schema->groupDecl != NULL) |
| 618 | xmlHashFree(schema->groupDecl, |
| 619 | (xmlHashDeallocator) xmlSchemaFreeType); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 620 | if (schema->schemasImports != NULL) |
| 621 | xmlHashFree(schema->schemasImports, |
| 622 | (xmlHashDeallocator) xmlSchemaFreeImport); |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 623 | if (schema->includes != NULL) { |
| 624 | xmlSchemaFreeIncludeList((xmlSchemaIncludePtr) schema->includes); |
| 625 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 626 | if (schema->annot != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 627 | xmlSchemaFreeAnnot(schema->annot); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 628 | if (schema->doc != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 629 | xmlFreeDoc(schema->doc); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 630 | xmlDictFree(schema->dict); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 631 | |
| 632 | xmlFree(schema); |
| 633 | } |
| 634 | |
| 635 | /************************************************************************ |
| 636 | * * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 637 | * Debug functions * |
| 638 | * * |
| 639 | ************************************************************************/ |
| 640 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 641 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 642 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 643 | /** |
| 644 | * xmlSchemaElementDump: |
| 645 | * @elem: an element |
| 646 | * @output: the file output |
| 647 | * |
| 648 | * Dump the element |
| 649 | */ |
| 650 | static void |
| 651 | xmlSchemaElementDump(xmlSchemaElementPtr elem, FILE * output, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 652 | const xmlChar * name ATTRIBUTE_UNUSED, |
| 653 | const xmlChar * context ATTRIBUTE_UNUSED, |
| 654 | const xmlChar * namespace ATTRIBUTE_UNUSED) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 655 | { |
| 656 | if (elem == NULL) |
| 657 | return; |
| 658 | |
| 659 | fprintf(output, "Element "); |
| 660 | if (elem->flags & XML_SCHEMAS_ELEM_TOPLEVEL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 661 | fprintf(output, "toplevel "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 662 | fprintf(output, ": %s ", elem->name); |
| 663 | if (namespace != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 664 | fprintf(output, "namespace '%s' ", namespace); |
| 665 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 666 | if (elem->flags & XML_SCHEMAS_ELEM_NILLABLE) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 667 | fprintf(output, "nillable "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 668 | if (elem->flags & XML_SCHEMAS_ELEM_GLOBAL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 669 | fprintf(output, "global "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 670 | if (elem->flags & XML_SCHEMAS_ELEM_DEFAULT) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 671 | fprintf(output, "default "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 672 | if (elem->flags & XML_SCHEMAS_ELEM_FIXED) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 673 | fprintf(output, "fixed "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 674 | if (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 675 | fprintf(output, "abstract "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 676 | if (elem->flags & XML_SCHEMAS_ELEM_REF) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 677 | fprintf(output, "ref '%s' ", elem->ref); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 678 | if (elem->id != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 679 | fprintf(output, "id '%s' ", elem->id); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 680 | fprintf(output, "\n"); |
| 681 | if ((elem->minOccurs != 1) || (elem->maxOccurs != 1)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 682 | fprintf(output, " "); |
| 683 | if (elem->minOccurs != 1) |
| 684 | fprintf(output, "min: %d ", elem->minOccurs); |
| 685 | if (elem->maxOccurs >= UNBOUNDED) |
| 686 | fprintf(output, "max: unbounded\n"); |
| 687 | else if (elem->maxOccurs != 1) |
| 688 | fprintf(output, "max: %d\n", elem->maxOccurs); |
| 689 | else |
| 690 | fprintf(output, "\n"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 691 | } |
| 692 | if (elem->namedType != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 693 | fprintf(output, " type: %s", elem->namedType); |
| 694 | if (elem->namedTypeNs != NULL) |
| 695 | fprintf(output, " ns %s\n", elem->namedTypeNs); |
| 696 | else |
| 697 | fprintf(output, "\n"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 698 | } |
| 699 | if (elem->substGroup != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 700 | fprintf(output, " substitutionGroup: %s", elem->substGroup); |
| 701 | if (elem->substGroupNs != NULL) |
| 702 | fprintf(output, " ns %s\n", elem->substGroupNs); |
| 703 | else |
| 704 | fprintf(output, "\n"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 705 | } |
| 706 | if (elem->value != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 707 | fprintf(output, " default: %s", elem->value); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /** |
| 711 | * xmlSchemaAnnotDump: |
| 712 | * @output: the file output |
| 713 | * @annot: a annotation |
| 714 | * |
| 715 | * Dump the annotation |
| 716 | */ |
| 717 | static void |
| 718 | xmlSchemaAnnotDump(FILE * output, xmlSchemaAnnotPtr annot) |
| 719 | { |
| 720 | xmlChar *content; |
| 721 | |
| 722 | if (annot == NULL) |
| 723 | return; |
| 724 | |
| 725 | content = xmlNodeGetContent(annot->content); |
| 726 | if (content != NULL) { |
| 727 | fprintf(output, " Annot: %s\n", content); |
| 728 | xmlFree(content); |
| 729 | } else |
| 730 | fprintf(output, " Annot: empty\n"); |
| 731 | } |
| 732 | |
| 733 | /** |
| 734 | * xmlSchemaTypeDump: |
| 735 | * @output: the file output |
| 736 | * @type: a type structure |
| 737 | * |
| 738 | * Dump a SchemaType structure |
| 739 | */ |
| 740 | static void |
| 741 | xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output) |
| 742 | { |
| 743 | if (type == NULL) { |
| 744 | fprintf(output, "Type: NULL\n"); |
| 745 | return; |
| 746 | } |
| 747 | fprintf(output, "Type: "); |
| 748 | if (type->name != NULL) |
| 749 | fprintf(output, "%s, ", type->name); |
| 750 | else |
| 751 | fprintf(output, "no name"); |
| 752 | switch (type->type) { |
| 753 | case XML_SCHEMA_TYPE_BASIC: |
| 754 | fprintf(output, "basic "); |
| 755 | break; |
| 756 | case XML_SCHEMA_TYPE_SIMPLE: |
| 757 | fprintf(output, "simple "); |
| 758 | break; |
| 759 | case XML_SCHEMA_TYPE_COMPLEX: |
| 760 | fprintf(output, "complex "); |
| 761 | break; |
| 762 | case XML_SCHEMA_TYPE_SEQUENCE: |
| 763 | fprintf(output, "sequence "); |
| 764 | break; |
| 765 | case XML_SCHEMA_TYPE_CHOICE: |
| 766 | fprintf(output, "choice "); |
| 767 | break; |
| 768 | case XML_SCHEMA_TYPE_ALL: |
| 769 | fprintf(output, "all "); |
| 770 | break; |
| 771 | case XML_SCHEMA_TYPE_UR: |
| 772 | fprintf(output, "ur "); |
| 773 | break; |
| 774 | case XML_SCHEMA_TYPE_RESTRICTION: |
| 775 | fprintf(output, "restriction "); |
| 776 | break; |
| 777 | case XML_SCHEMA_TYPE_EXTENSION: |
| 778 | fprintf(output, "extension "); |
| 779 | break; |
| 780 | default: |
| 781 | fprintf(output, "unknowntype%d ", type->type); |
| 782 | break; |
| 783 | } |
| 784 | if (type->base != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 785 | fprintf(output, "base %s, ", type->base); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 786 | } |
| 787 | switch (type->contentType) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 788 | case XML_SCHEMA_CONTENT_UNKNOWN: |
| 789 | fprintf(output, "unknown "); |
| 790 | break; |
| 791 | case XML_SCHEMA_CONTENT_EMPTY: |
| 792 | fprintf(output, "empty "); |
| 793 | break; |
| 794 | case XML_SCHEMA_CONTENT_ELEMENTS: |
| 795 | fprintf(output, "element "); |
| 796 | break; |
| 797 | case XML_SCHEMA_CONTENT_MIXED: |
| 798 | fprintf(output, "mixed "); |
| 799 | break; |
| 800 | case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS: |
| 801 | fprintf(output, "mixed_or_elems "); |
| 802 | break; |
| 803 | case XML_SCHEMA_CONTENT_BASIC: |
| 804 | fprintf(output, "basic "); |
| 805 | break; |
| 806 | case XML_SCHEMA_CONTENT_SIMPLE: |
| 807 | fprintf(output, "simple "); |
| 808 | break; |
| 809 | case XML_SCHEMA_CONTENT_ANY: |
| 810 | fprintf(output, "any "); |
| 811 | break; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 812 | } |
| 813 | fprintf(output, "\n"); |
| 814 | if ((type->minOccurs != 1) || (type->maxOccurs != 1)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 815 | fprintf(output, " "); |
| 816 | if (type->minOccurs != 1) |
| 817 | fprintf(output, "min: %d ", type->minOccurs); |
| 818 | if (type->maxOccurs >= UNBOUNDED) |
| 819 | fprintf(output, "max: unbounded\n"); |
| 820 | else if (type->maxOccurs != 1) |
| 821 | fprintf(output, "max: %d\n", type->maxOccurs); |
| 822 | else |
| 823 | fprintf(output, "\n"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 824 | } |
| 825 | if (type->annot != NULL) |
| 826 | xmlSchemaAnnotDump(output, type->annot); |
| 827 | if (type->subtypes != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 828 | xmlSchemaTypePtr sub = type->subtypes; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 829 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 830 | fprintf(output, " subtypes: "); |
| 831 | while (sub != NULL) { |
| 832 | fprintf(output, "%s ", sub->name); |
| 833 | sub = sub->next; |
| 834 | } |
| 835 | fprintf(output, "\n"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | } |
| 839 | |
| 840 | /** |
| 841 | * xmlSchemaDump: |
| 842 | * @output: the file output |
| 843 | * @schema: a schema structure |
| 844 | * |
| 845 | * Dump a Schema structure. |
| 846 | */ |
| 847 | void |
| 848 | xmlSchemaDump(FILE * output, xmlSchemaPtr schema) |
| 849 | { |
| 850 | if (schema == NULL) { |
| 851 | fprintf(output, "Schemas: NULL\n"); |
| 852 | return; |
| 853 | } |
| 854 | fprintf(output, "Schemas: "); |
| 855 | if (schema->name != NULL) |
| 856 | fprintf(output, "%s, ", schema->name); |
| 857 | else |
| 858 | fprintf(output, "no name, "); |
| 859 | if (schema->targetNamespace != NULL) |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 860 | fprintf(output, "%s", (const char *) schema->targetNamespace); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 861 | else |
| 862 | fprintf(output, "no target namespace"); |
| 863 | fprintf(output, "\n"); |
| 864 | if (schema->annot != NULL) |
| 865 | xmlSchemaAnnotDump(output, schema->annot); |
| 866 | |
| 867 | xmlHashScan(schema->typeDecl, (xmlHashScanner) xmlSchemaTypeDump, |
| 868 | output); |
| 869 | xmlHashScanFull(schema->elemDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 870 | (xmlHashScannerFull) xmlSchemaElementDump, output); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 871 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 872 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 873 | |
| 874 | /************************************************************************ |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 875 | * * |
| 876 | * Utilities * |
| 877 | * * |
| 878 | ************************************************************************/ |
| 879 | /** |
| 880 | * numberedString: |
| 881 | * @prefix: the string prefix |
| 882 | * @number: the number |
| 883 | * |
| 884 | * Build a new numbered string |
| 885 | * |
| 886 | * Returns the new string |
| 887 | */ |
| 888 | |
| 889 | /** |
| 890 | * xmlSchemaGetProp: |
| 891 | * @ctxt: the parser context |
| 892 | * @node: the node |
| 893 | * @name: the property name |
| 894 | * |
| 895 | * Read a attribute value and internalize the string |
| 896 | * |
| 897 | * Returns the string or NULL if not present. |
| 898 | */ |
| 899 | static const xmlChar * |
| 900 | xmlSchemaGetProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, |
| 901 | const char *name) |
| 902 | { |
| 903 | xmlChar *val; |
| 904 | const xmlChar *ret; |
| 905 | |
| 906 | val = xmlGetProp(node, BAD_CAST name); |
| 907 | if (val == NULL) |
| 908 | return(NULL); |
| 909 | ret = xmlDictLookup(ctxt->dict, val, -1); |
| 910 | xmlFree(val); |
| 911 | return(ret); |
| 912 | } |
| 913 | |
| 914 | /** |
| 915 | * xmlSchemaGetNamespace: |
| 916 | * @ctxt: the parser context |
| 917 | * @schema: the schemas containing the declaration |
| 918 | * @node: the node |
| 919 | * @qname: the QName to analyze |
| 920 | * |
| 921 | * Find the namespace name for the given declaration. |
| 922 | * |
| 923 | * Returns the local name for that declaration, as well as the namespace name |
| 924 | */ |
| 925 | static const xmlChar * |
| 926 | xmlSchemaGetNamespace(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 927 | xmlNodePtr node, const xmlChar *qname, |
| 928 | const xmlChar **namespace) { |
| 929 | int len; |
| 930 | const xmlChar *name, *prefix, *def = NULL; |
| 931 | xmlNsPtr ns; |
| 932 | |
| 933 | *namespace = NULL; |
| 934 | |
| 935 | if (xmlStrEqual(node->name, BAD_CAST "element") || |
| 936 | xmlStrEqual(node->name, BAD_CAST "attribute") || |
| 937 | xmlStrEqual(node->name, BAD_CAST "simpleType") || |
| 938 | xmlStrEqual(node->name, BAD_CAST "complexType")) { |
| 939 | def = xmlSchemaGetProp(ctxt, node, "targetNamespace"); |
| 940 | } |
| 941 | |
| 942 | qname = xmlDictLookup(ctxt->dict, qname, -1); /* intern the string */ |
| 943 | name = xmlSplitQName3(qname, &len); |
| 944 | if (name == NULL) { |
| 945 | if (def == NULL) { |
| 946 | if (xmlStrEqual(node->name, BAD_CAST "element")) { |
| 947 | if (schema->flags & XML_SCHEMAS_QUALIF_ELEM) |
| 948 | *namespace = schema->targetNamespace; |
| 949 | } else if (xmlStrEqual(node->name, BAD_CAST "attribute")) { |
| 950 | if (schema->flags & XML_SCHEMAS_QUALIF_ATTR) |
| 951 | *namespace = schema->targetNamespace; |
| 952 | } else if ((xmlStrEqual(node->name, BAD_CAST "simpleType")) || |
| 953 | (xmlStrEqual(node->name, BAD_CAST "complexType"))) { |
| 954 | *namespace = schema->targetNamespace; |
| 955 | } |
| 956 | } else { |
| 957 | *namespace = def; |
| 958 | } |
| 959 | return(qname); |
| 960 | } |
| 961 | name = xmlDictLookup(ctxt->dict, name, -1); |
| 962 | prefix = xmlDictLookup(ctxt->dict, qname, len); |
| 963 | if (def != NULL) { |
| 964 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_DEF_AND_PREFIX, |
| 965 | "%s: presence of both prefix %s and targetNamespace\n", |
| 966 | node->name, prefix); |
| 967 | } |
| 968 | ns = xmlSearchNs(node->doc, node, prefix); |
| 969 | if (ns == NULL) { |
| 970 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_PREFIX_UNDEFINED, |
| 971 | "%s: the QName prefix %s is undefined\n", |
| 972 | node->name, prefix); |
| 973 | return(name); |
| 974 | } |
| 975 | *namespace = xmlDictLookup(ctxt->dict, ns->href, -1); |
| 976 | return(name); |
| 977 | } |
| 978 | |
| 979 | /************************************************************************ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 980 | * * |
| 981 | * Parsing functions * |
| 982 | * * |
| 983 | ************************************************************************/ |
| 984 | |
| 985 | /** |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 986 | * xmlSchemaGetElem: |
| 987 | * @schema: the schemas context |
| 988 | * @name: the element name |
| 989 | * @ns: the element namespace |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 990 | * @level: how deep is the request |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 991 | * |
| 992 | * Lookup a an element in the schemas or the accessible schemas |
| 993 | * |
| 994 | * Returns the element definition or NULL if not found. |
| 995 | */ |
| 996 | static xmlSchemaElementPtr |
| 997 | xmlSchemaGetElem(xmlSchemaPtr schema, const xmlChar * name, |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 998 | const xmlChar * namespace, int level) |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 999 | { |
| 1000 | xmlSchemaElementPtr ret; |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1001 | xmlSchemaImportPtr import = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1002 | |
| 1003 | if ((name == NULL) || (schema == NULL)) |
| 1004 | return (NULL); |
| 1005 | |
| 1006 | if (namespace == NULL) { |
| 1007 | ret = xmlHashLookup2(schema->elemDecl, name, namespace); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1008 | if ((ret != NULL) && |
| 1009 | ((level == 0) || (ret->flags & XML_SCHEMAS_ELEM_TOPLEVEL))) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1010 | return (ret); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1011 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1012 | } else if ((schema->flags & XML_SCHEMAS_QUALIF_ELEM) == 0) { |
| 1013 | if (xmlStrEqual(namespace, schema->targetNamespace)) |
| 1014 | ret = xmlHashLookup2(schema->elemDecl, name, NULL); |
| 1015 | else |
| 1016 | ret = xmlHashLookup2(schema->elemDecl, name, namespace); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1017 | if ((ret != NULL) && |
| 1018 | ((level == 0) || (ret->flags & XML_SCHEMAS_ELEM_TOPLEVEL))) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1019 | return (ret); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1020 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1021 | } else { |
| 1022 | ret = xmlHashLookup2(schema->elemDecl, name, namespace); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1023 | if ((ret != NULL) && |
| 1024 | ((level == 0) || (ret->flags & XML_SCHEMAS_ELEM_TOPLEVEL))) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1025 | return (ret); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1026 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1027 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1028 | if (level > 0) |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1029 | import = xmlHashLookup(schema->schemasImports, namespace); |
| 1030 | if (import != NULL) |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1031 | ret = xmlSchemaGetElem(import->schema, name, namespace, level + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1032 | #ifdef DEBUG |
| 1033 | if (ret == NULL) { |
| 1034 | if (namespace == NULL) |
| 1035 | fprintf(stderr, "Unable to lookup type %s", name); |
| 1036 | else |
| 1037 | fprintf(stderr, "Unable to lookup type %s:%s", name, |
| 1038 | namespace); |
| 1039 | } |
| 1040 | #endif |
| 1041 | return (ret); |
| 1042 | } |
| 1043 | |
| 1044 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1045 | * xmlSchemaGetType: |
| 1046 | * @schema: the schemas context |
| 1047 | * @name: the type name |
| 1048 | * @ns: the type namespace |
| 1049 | * |
| 1050 | * Lookup a type in the schemas or the predefined types |
| 1051 | * |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 1052 | * Returns the group definition or NULL if not found. |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1053 | */ |
| 1054 | static xmlSchemaTypePtr |
| 1055 | xmlSchemaGetType(xmlSchemaPtr schema, const xmlChar * name, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1056 | const xmlChar * namespace) |
| 1057 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1058 | xmlSchemaTypePtr ret; |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 1059 | xmlSchemaImportPtr import; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1060 | |
| 1061 | if (name == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1062 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1063 | if (schema != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1064 | ret = xmlHashLookup2(schema->typeDecl, name, namespace); |
| 1065 | if (ret != NULL) |
| 1066 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1067 | } |
| 1068 | ret = xmlSchemaGetPredefinedType(name, namespace); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 1069 | if (ret != NULL) |
| 1070 | return (ret); |
| 1071 | import = xmlHashLookup(schema->schemasImports, namespace); |
| 1072 | if (import != NULL) |
| 1073 | ret = xmlSchemaGetType(import->schema, name, namespace); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1074 | #ifdef DEBUG |
| 1075 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1076 | if (namespace == NULL) |
| 1077 | fprintf(stderr, "Unable to lookup type %s", name); |
| 1078 | else |
| 1079 | fprintf(stderr, "Unable to lookup type %s:%s", name, |
| 1080 | namespace); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1081 | } |
| 1082 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1083 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | /************************************************************************ |
| 1087 | * * |
| 1088 | * Parsing functions * |
| 1089 | * * |
| 1090 | ************************************************************************/ |
| 1091 | |
| 1092 | #define IS_BLANK_NODE(n) \ |
| 1093 | (((n)->type == XML_TEXT_NODE) && (xmlSchemaIsBlank((n)->content))) |
| 1094 | |
| 1095 | /** |
| 1096 | * xmlSchemaIsBlank: |
| 1097 | * @str: a string |
| 1098 | * |
| 1099 | * Check if a string is ignorable |
| 1100 | * |
| 1101 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 1102 | */ |
| 1103 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1104 | xmlSchemaIsBlank(xmlChar * str) |
| 1105 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1106 | if (str == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1107 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1108 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1109 | if (!(IS_BLANK_CH(*str))) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1110 | return (0); |
| 1111 | str++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1112 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1113 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | /** |
| 1117 | * xmlSchemaAddNotation: |
| 1118 | * @ctxt: a schema validation context |
| 1119 | * @schema: the schema being built |
| 1120 | * @name: the item name |
| 1121 | * |
| 1122 | * Add an XML schema Attrribute declaration |
| 1123 | * *WARNING* this interface is highly subject to change |
| 1124 | * |
| 1125 | * Returns the new struture or NULL in case of error |
| 1126 | */ |
| 1127 | static xmlSchemaNotationPtr |
| 1128 | xmlSchemaAddNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1129 | const xmlChar * name) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1130 | { |
| 1131 | xmlSchemaNotationPtr ret = NULL; |
| 1132 | int val; |
| 1133 | |
| 1134 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1135 | return (NULL); |
| 1136 | |
| 1137 | if (schema->notaDecl == NULL) |
| 1138 | schema->notaDecl = xmlHashCreate(10); |
| 1139 | if (schema->notaDecl == NULL) |
| 1140 | return (NULL); |
| 1141 | |
| 1142 | ret = (xmlSchemaNotationPtr) xmlMalloc(sizeof(xmlSchemaNotation)); |
| 1143 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1144 | xmlSchemaPErrMemory(ctxt, "add annotation", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1145 | return (NULL); |
| 1146 | } |
| 1147 | memset(ret, 0, sizeof(xmlSchemaNotation)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1148 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1149 | val = xmlHashAddEntry2(schema->notaDecl, name, schema->targetNamespace, |
| 1150 | ret); |
| 1151 | if (val != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1152 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1153 | XML_SCHEMAP_REDEFINED_NOTATION, |
| 1154 | "Notation %s already defined\n", |
| 1155 | name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1156 | xmlFree(ret); |
| 1157 | return (NULL); |
| 1158 | } |
| 1159 | return (ret); |
| 1160 | } |
| 1161 | |
| 1162 | |
| 1163 | /** |
| 1164 | * xmlSchemaAddAttribute: |
| 1165 | * @ctxt: a schema validation context |
| 1166 | * @schema: the schema being built |
| 1167 | * @name: the item name |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1168 | * @namespace: the namespace |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1169 | * |
| 1170 | * Add an XML schema Attrribute declaration |
| 1171 | * *WARNING* this interface is highly subject to change |
| 1172 | * |
| 1173 | * Returns the new struture or NULL in case of error |
| 1174 | */ |
| 1175 | static xmlSchemaAttributePtr |
| 1176 | xmlSchemaAddAttribute(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1177 | const xmlChar * name, const xmlChar * namespace) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1178 | { |
| 1179 | xmlSchemaAttributePtr ret = NULL; |
| 1180 | int val; |
| 1181 | |
| 1182 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1183 | return (NULL); |
| 1184 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1185 | #ifdef DEBUG |
| 1186 | fprintf(stderr, "Adding attribute %s\n", name); |
| 1187 | if (namespace != NULL) |
| 1188 | fprintf(stderr, " target namespace %s\n", namespace); |
| 1189 | #endif |
| 1190 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1191 | if (schema->attrDecl == NULL) |
| 1192 | schema->attrDecl = xmlHashCreate(10); |
| 1193 | if (schema->attrDecl == NULL) |
| 1194 | return (NULL); |
| 1195 | |
| 1196 | ret = (xmlSchemaAttributePtr) xmlMalloc(sizeof(xmlSchemaAttribute)); |
| 1197 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1198 | xmlSchemaPErrMemory(ctxt, "allocating attribute", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1199 | return (NULL); |
| 1200 | } |
| 1201 | memset(ret, 0, sizeof(xmlSchemaAttribute)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1202 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
| 1203 | ret->targetNamespace = xmlDictLookup(ctxt->dict, namespace, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1204 | val = xmlHashAddEntry3(schema->attrDecl, name, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1205 | schema->targetNamespace, ctxt->container, ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1206 | if (val != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1207 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1208 | XML_SCHEMAP_REDEFINED_ATTR, |
| 1209 | "Attribute %s already defined\n", |
| 1210 | name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1211 | xmlFree(ret); |
| 1212 | return (NULL); |
| 1213 | } |
| 1214 | return (ret); |
| 1215 | } |
| 1216 | |
| 1217 | /** |
| 1218 | * xmlSchemaAddAttributeGroup: |
| 1219 | * @ctxt: a schema validation context |
| 1220 | * @schema: the schema being built |
| 1221 | * @name: the item name |
| 1222 | * |
| 1223 | * Add an XML schema Attrribute Group declaration |
| 1224 | * |
| 1225 | * Returns the new struture or NULL in case of error |
| 1226 | */ |
| 1227 | static xmlSchemaAttributeGroupPtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1228 | xmlSchemaAddAttributeGroup(xmlSchemaParserCtxtPtr ctxt, |
| 1229 | xmlSchemaPtr schema, const xmlChar * name) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1230 | { |
| 1231 | xmlSchemaAttributeGroupPtr ret = NULL; |
| 1232 | int val; |
| 1233 | |
| 1234 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1235 | return (NULL); |
| 1236 | |
| 1237 | if (schema->attrgrpDecl == NULL) |
| 1238 | schema->attrgrpDecl = xmlHashCreate(10); |
| 1239 | if (schema->attrgrpDecl == NULL) |
| 1240 | return (NULL); |
| 1241 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1242 | ret = |
| 1243 | (xmlSchemaAttributeGroupPtr) |
| 1244 | xmlMalloc(sizeof(xmlSchemaAttributeGroup)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1245 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1246 | xmlSchemaPErrMemory(ctxt, "allocating attribute group", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1247 | return (NULL); |
| 1248 | } |
| 1249 | memset(ret, 0, sizeof(xmlSchemaAttributeGroup)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1250 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1251 | val = xmlHashAddEntry3(schema->attrgrpDecl, name, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1252 | schema->targetNamespace, ctxt->container, ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1253 | if (val != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1254 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1255 | XML_SCHEMAP_REDEFINED_ATTRGROUP, |
| 1256 | "Attribute group %s already defined\n", |
| 1257 | name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1258 | xmlFree(ret); |
| 1259 | return (NULL); |
| 1260 | } |
| 1261 | return (ret); |
| 1262 | } |
| 1263 | |
| 1264 | /** |
| 1265 | * xmlSchemaAddElement: |
| 1266 | * @ctxt: a schema validation context |
| 1267 | * @schema: the schema being built |
| 1268 | * @name: the type name |
| 1269 | * @namespace: the type namespace |
| 1270 | * |
| 1271 | * Add an XML schema Element declaration |
| 1272 | * *WARNING* this interface is highly subject to change |
| 1273 | * |
| 1274 | * Returns the new struture or NULL in case of error |
| 1275 | */ |
| 1276 | static xmlSchemaElementPtr |
| 1277 | xmlSchemaAddElement(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 1278 | const xmlChar * name, const xmlChar * namespace) |
| 1279 | { |
| 1280 | xmlSchemaElementPtr ret = NULL; |
| 1281 | int val; |
| 1282 | |
| 1283 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1284 | return (NULL); |
| 1285 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1286 | #ifdef DEBUG |
| 1287 | fprintf(stderr, "Adding element %s\n", name); |
| 1288 | if (namespace != NULL) |
| 1289 | fprintf(stderr, " target namespace %s\n", namespace); |
| 1290 | #endif |
| 1291 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1292 | if (schema->elemDecl == NULL) |
| 1293 | schema->elemDecl = xmlHashCreate(10); |
| 1294 | if (schema->elemDecl == NULL) |
| 1295 | return (NULL); |
| 1296 | |
| 1297 | ret = (xmlSchemaElementPtr) xmlMalloc(sizeof(xmlSchemaElement)); |
| 1298 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1299 | xmlSchemaPErrMemory(ctxt, "allocating element", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1300 | return (NULL); |
| 1301 | } |
| 1302 | memset(ret, 0, sizeof(xmlSchemaElement)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1303 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
| 1304 | ret->targetNamespace = xmlDictLookup(ctxt->dict, namespace, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1305 | val = xmlHashAddEntry3(schema->elemDecl, name, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1306 | namespace, ctxt->container, ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1307 | if (val != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1308 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1309 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1310 | snprintf(buf, 99, "privatieelem %d", ctxt->counter++ + 1); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1311 | val = xmlHashAddEntry3(schema->elemDecl, name, (xmlChar *) buf, |
| 1312 | namespace, ret); |
| 1313 | if (val != 0) { |
| 1314 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1315 | XML_SCHEMAP_REDEFINED_ELEMENT, |
| 1316 | "Element %s already defined\n", |
| 1317 | name, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1318 | xmlFree(ret); |
| 1319 | return (NULL); |
| 1320 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1321 | } |
| 1322 | return (ret); |
| 1323 | } |
| 1324 | |
| 1325 | /** |
| 1326 | * xmlSchemaAddType: |
| 1327 | * @ctxt: a schema validation context |
| 1328 | * @schema: the schema being built |
| 1329 | * @name: the item name |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1330 | * @namespace: the namespace |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1331 | * |
| 1332 | * Add an XML schema Simple Type definition |
| 1333 | * *WARNING* this interface is highly subject to change |
| 1334 | * |
| 1335 | * Returns the new struture or NULL in case of error |
| 1336 | */ |
| 1337 | static xmlSchemaTypePtr |
| 1338 | xmlSchemaAddType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1339 | const xmlChar * name, const xmlChar * namespace) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1340 | { |
| 1341 | xmlSchemaTypePtr ret = NULL; |
| 1342 | int val; |
| 1343 | |
| 1344 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1345 | return (NULL); |
| 1346 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1347 | #ifdef DEBUG |
| 1348 | fprintf(stderr, "Adding type %s\n", name); |
| 1349 | if (namespace != NULL) |
| 1350 | fprintf(stderr, " target namespace %s\n", namespace); |
| 1351 | #endif |
| 1352 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1353 | if (schema->typeDecl == NULL) |
| 1354 | schema->typeDecl = xmlHashCreate(10); |
| 1355 | if (schema->typeDecl == NULL) |
| 1356 | return (NULL); |
| 1357 | |
| 1358 | ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType)); |
| 1359 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1360 | xmlSchemaPErrMemory(ctxt, "allocating type", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1361 | return (NULL); |
| 1362 | } |
| 1363 | memset(ret, 0, sizeof(xmlSchemaType)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1364 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
| 1365 | val = xmlHashAddEntry2(schema->typeDecl, name, namespace, ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1366 | if (val != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1367 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1368 | XML_SCHEMAP_REDEFINED_TYPE, |
| 1369 | "Type %s already defined\n", |
| 1370 | name, NULL); |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 1371 | xmlFree(ret); |
| 1372 | return (NULL); |
| 1373 | } |
| 1374 | ret->minOccurs = 1; |
| 1375 | ret->maxOccurs = 1; |
| 1376 | |
| 1377 | return (ret); |
| 1378 | } |
| 1379 | |
| 1380 | /** |
| 1381 | * xmlSchemaAddGroup: |
| 1382 | * @ctxt: a schema validation context |
| 1383 | * @schema: the schema being built |
| 1384 | * @name: the group name |
| 1385 | * |
| 1386 | * Add an XML schema Group definition |
| 1387 | * |
| 1388 | * Returns the new struture or NULL in case of error |
| 1389 | */ |
| 1390 | static xmlSchemaTypePtr |
| 1391 | xmlSchemaAddGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1392 | const xmlChar * name) |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 1393 | { |
| 1394 | xmlSchemaTypePtr ret = NULL; |
| 1395 | int val; |
| 1396 | |
| 1397 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1398 | return (NULL); |
| 1399 | |
| 1400 | if (schema->groupDecl == NULL) |
| 1401 | schema->groupDecl = xmlHashCreate(10); |
| 1402 | if (schema->groupDecl == NULL) |
| 1403 | return (NULL); |
| 1404 | |
| 1405 | ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType)); |
| 1406 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1407 | xmlSchemaPErrMemory(ctxt, "adding group", NULL); |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 1408 | return (NULL); |
| 1409 | } |
| 1410 | memset(ret, 0, sizeof(xmlSchemaType)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1411 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1412 | val = |
| 1413 | xmlHashAddEntry2(schema->groupDecl, name, schema->targetNamespace, |
| 1414 | ret); |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 1415 | if (val != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1416 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1417 | XML_SCHEMAP_REDEFINED_GROUP, |
| 1418 | "Group %s already defined\n", |
| 1419 | name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1420 | xmlFree(ret); |
| 1421 | return (NULL); |
| 1422 | } |
| 1423 | ret->minOccurs = 1; |
| 1424 | ret->maxOccurs = 1; |
| 1425 | |
| 1426 | return (ret); |
| 1427 | } |
| 1428 | |
| 1429 | /************************************************************************ |
| 1430 | * * |
| 1431 | * Utilities for parsing * |
| 1432 | * * |
| 1433 | ************************************************************************/ |
| 1434 | |
| 1435 | /** |
| 1436 | * xmlGetQNameProp: |
| 1437 | * @ctxt: a schema validation context |
| 1438 | * @node: a subtree containing XML Schema informations |
| 1439 | * @name: the attribute name |
| 1440 | * @namespace: the result namespace if any |
| 1441 | * |
| 1442 | * Extract a QName Attribute value |
| 1443 | * |
| 1444 | * Returns the NCName or NULL if not found, and also update @namespace |
| 1445 | * with the namespace URI |
| 1446 | */ |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1447 | static const xmlChar * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1448 | xmlGetQNameProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1449 | const char *name, const xmlChar ** namespace) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1450 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1451 | const xmlChar *val; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1452 | xmlNsPtr ns; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1453 | const xmlChar *ret, *prefix; |
| 1454 | int len; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1455 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1456 | *namespace = NULL; |
| 1457 | val = xmlSchemaGetProp(ctxt, node, name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1458 | if (val == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1459 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1460 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1461 | ret = xmlSplitQName3(val, &len); |
| 1462 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1463 | return (val); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1464 | } |
| 1465 | ret = xmlDictLookup(ctxt->dict, ret, -1); |
| 1466 | prefix = xmlDictLookup(ctxt->dict, val, len); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1467 | |
| 1468 | ns = xmlSearchNs(node->doc, node, prefix); |
| 1469 | if (ns == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1470 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_PREFIX_UNDEFINED, |
| 1471 | "Attribute %s: the QName prefix %s is undefined\n", |
| 1472 | (const xmlChar *) name, prefix); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1473 | } else { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1474 | *namespace = xmlDictLookup(ctxt->dict, ns->href, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1475 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1476 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | /** |
| 1480 | * xmlGetMaxOccurs: |
| 1481 | * @ctxt: a schema validation context |
| 1482 | * @node: a subtree containing XML Schema informations |
| 1483 | * |
| 1484 | * Get the maxOccurs property |
| 1485 | * |
| 1486 | * Returns the default if not found, or the value |
| 1487 | */ |
| 1488 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1489 | xmlGetMaxOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node) |
| 1490 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1491 | const xmlChar *val, *cur; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1492 | int ret = 0; |
| 1493 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1494 | val = xmlSchemaGetProp(ctxt, node, "maxOccurs"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1495 | if (val == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1496 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1497 | |
| 1498 | if (xmlStrEqual(val, (const xmlChar *) "unbounded")) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1499 | return (UNBOUNDED); /* encoding it with -1 might be another option */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | cur = val; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1503 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1504 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1505 | while ((*cur >= '0') && (*cur <= '9')) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1506 | ret = ret * 10 + (*cur - '0'); |
| 1507 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1508 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1509 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1510 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1511 | if (*cur != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1512 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_INVALID_MAXOCCURS, |
| 1513 | "invalid value for maxOccurs: %s\n", val, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1514 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1515 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1516 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | /** |
| 1520 | * xmlGetMinOccurs: |
| 1521 | * @ctxt: a schema validation context |
| 1522 | * @node: a subtree containing XML Schema informations |
| 1523 | * |
| 1524 | * Get the minOccurs property |
| 1525 | * |
| 1526 | * Returns the default if not found, or the value |
| 1527 | */ |
| 1528 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1529 | xmlGetMinOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node) |
| 1530 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1531 | const xmlChar *val, *cur; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1532 | int ret = 0; |
| 1533 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1534 | val = xmlSchemaGetProp(ctxt, node, "minOccurs"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1535 | if (val == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1536 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1537 | |
| 1538 | cur = val; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1539 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1540 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1541 | while ((*cur >= '0') && (*cur <= '9')) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1542 | ret = ret * 10 + (*cur - '0'); |
| 1543 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1544 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1545 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1546 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1547 | if (*cur != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1548 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_INVALID_MINOCCURS, |
| 1549 | "invalid value for minOccurs: %s\n", val, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1550 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1551 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1552 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | /** |
| 1556 | * xmlGetBooleanProp: |
| 1557 | * @ctxt: a schema validation context |
| 1558 | * @node: a subtree containing XML Schema informations |
| 1559 | * @name: the attribute name |
| 1560 | * @def: the default value |
| 1561 | * |
| 1562 | * Get is a bolean property is set |
| 1563 | * |
| 1564 | * Returns the default if not found, 0 if found to be false, |
| 1565 | * 1 if found to be true |
| 1566 | */ |
| 1567 | static int |
| 1568 | xmlGetBooleanProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1569 | const char *name, int def) |
| 1570 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1571 | const xmlChar *val; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1572 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1573 | val = xmlSchemaGetProp(ctxt, node, name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1574 | if (val == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1575 | return (def); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1576 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1577 | if (xmlStrEqual(val, BAD_CAST "true")) |
| 1578 | def = 1; |
| 1579 | else if (xmlStrEqual(val, BAD_CAST "false")) |
| 1580 | def = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1581 | else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1582 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_INVALID_BOOLEAN, |
| 1583 | "Attribute %s: the value %s is not boolean\n", |
| 1584 | (const xmlChar *) name, val); |
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 (def); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | /************************************************************************ |
| 1590 | * * |
| 1591 | * Shema extraction from an Infoset * |
| 1592 | * * |
| 1593 | ************************************************************************/ |
| 1594 | static xmlSchemaTypePtr xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr |
| 1595 | ctxt, xmlSchemaPtr schema, |
| 1596 | xmlNodePtr node); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1597 | static xmlSchemaTypePtr xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr |
| 1598 | ctxt, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1599 | xmlSchemaPtr schema, |
| 1600 | xmlNodePtr node); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1601 | static xmlSchemaTypePtr xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr |
| 1602 | ctxt, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1603 | xmlSchemaPtr schema, |
| 1604 | xmlNodePtr node, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1605 | int simple); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1606 | static xmlSchemaTypePtr xmlSchemaParseSequence(xmlSchemaParserCtxtPtr ctxt, |
| 1607 | xmlSchemaPtr schema, |
| 1608 | xmlNodePtr node); |
| 1609 | static xmlSchemaTypePtr xmlSchemaParseAll(xmlSchemaParserCtxtPtr ctxt, |
| 1610 | xmlSchemaPtr schema, |
| 1611 | xmlNodePtr node); |
| 1612 | static xmlSchemaAttributePtr xmlSchemaParseAttribute(xmlSchemaParserCtxtPtr |
| 1613 | ctxt, |
| 1614 | xmlSchemaPtr schema, |
| 1615 | xmlNodePtr node); |
| 1616 | static xmlSchemaAttributeGroupPtr |
| 1617 | xmlSchemaParseAttributeGroup(xmlSchemaParserCtxtPtr ctxt, |
| 1618 | xmlSchemaPtr schema, xmlNodePtr node); |
| 1619 | static xmlSchemaTypePtr xmlSchemaParseChoice(xmlSchemaParserCtxtPtr ctxt, |
| 1620 | xmlSchemaPtr schema, |
| 1621 | xmlNodePtr node); |
| 1622 | static xmlSchemaTypePtr xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt, |
| 1623 | xmlSchemaPtr schema, |
| 1624 | xmlNodePtr node); |
| 1625 | static xmlSchemaAttributePtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1626 | xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt, |
| 1627 | xmlSchemaPtr schema, xmlNodePtr node); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1628 | |
| 1629 | /** |
| 1630 | * xmlSchemaParseAttrDecls: |
| 1631 | * @ctxt: a schema validation context |
| 1632 | * @schema: the schema being built |
| 1633 | * @node: a subtree containing XML Schema informations |
| 1634 | * @type: the hosting type |
| 1635 | * |
| 1636 | * parse a XML schema attrDecls declaration corresponding to |
| 1637 | * <!ENTITY % attrDecls |
| 1638 | * '((%attribute;| %attributeGroup;)*,(%anyAttribute;)?)'> |
| 1639 | */ |
| 1640 | static xmlNodePtr |
| 1641 | xmlSchemaParseAttrDecls(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 1642 | xmlNodePtr child, xmlSchemaTypePtr type) |
| 1643 | { |
| 1644 | xmlSchemaAttributePtr lastattr, attr; |
| 1645 | |
| 1646 | lastattr = NULL; |
| 1647 | while ((IS_SCHEMA(child, "attribute")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1648 | (IS_SCHEMA(child, "attributeGroup"))) { |
| 1649 | attr = NULL; |
| 1650 | if (IS_SCHEMA(child, "attribute")) { |
| 1651 | attr = xmlSchemaParseAttribute(ctxt, schema, child); |
| 1652 | } else if (IS_SCHEMA(child, "attributeGroup")) { |
| 1653 | attr = (xmlSchemaAttributePtr) |
| 1654 | xmlSchemaParseAttributeGroup(ctxt, schema, child); |
| 1655 | } |
| 1656 | if (attr != NULL) { |
| 1657 | if (lastattr == NULL) { |
| 1658 | type->attributes = attr; |
| 1659 | lastattr = attr; |
| 1660 | } else { |
| 1661 | lastattr->next = attr; |
| 1662 | lastattr = attr; |
| 1663 | } |
| 1664 | } |
| 1665 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1666 | } |
| 1667 | if (IS_SCHEMA(child, "anyAttribute")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1668 | attr = xmlSchemaParseAnyAttribute(ctxt, schema, child); |
| 1669 | if (attr != NULL) { |
| 1670 | if (lastattr == NULL) { |
| 1671 | type->attributes = attr; |
| 1672 | lastattr = attr; |
| 1673 | } else { |
| 1674 | lastattr->next = attr; |
| 1675 | lastattr = attr; |
| 1676 | } |
| 1677 | } |
| 1678 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1679 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1680 | return (child); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | /** |
| 1684 | * xmlSchemaParseAnnotation: |
| 1685 | * @ctxt: a schema validation context |
| 1686 | * @schema: the schema being built |
| 1687 | * @node: a subtree containing XML Schema informations |
| 1688 | * |
| 1689 | * parse a XML schema Attrribute declaration |
| 1690 | * *WARNING* this interface is highly subject to change |
| 1691 | * |
| 1692 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 1693 | * 1 in case of success. |
| 1694 | */ |
| 1695 | static xmlSchemaAnnotPtr |
| 1696 | xmlSchemaParseAnnotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 1697 | xmlNodePtr node) |
| 1698 | { |
| 1699 | xmlSchemaAnnotPtr ret; |
| 1700 | |
| 1701 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1702 | return (NULL); |
| 1703 | ret = xmlSchemaNewAnnot(ctxt, node); |
| 1704 | |
| 1705 | return (ret); |
| 1706 | } |
| 1707 | |
| 1708 | /** |
| 1709 | * xmlSchemaParseFacet: |
| 1710 | * @ctxt: a schema validation context |
| 1711 | * @schema: the schema being built |
| 1712 | * @node: a subtree containing XML Schema informations |
| 1713 | * |
| 1714 | * parse a XML schema Facet declaration |
| 1715 | * *WARNING* this interface is highly subject to change |
| 1716 | * |
| 1717 | * Returns the new type structure or NULL in case of error |
| 1718 | */ |
| 1719 | static xmlSchemaFacetPtr |
| 1720 | xmlSchemaParseFacet(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1721 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1722 | { |
| 1723 | xmlSchemaFacetPtr facet; |
| 1724 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1725 | const xmlChar *value; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1726 | |
| 1727 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1728 | return (NULL); |
| 1729 | |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 1730 | facet = xmlSchemaNewFacet(); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1731 | if (facet == NULL) { |
| 1732 | xmlSchemaPErrMemory(ctxt, "allocating facet", node); |
| 1733 | return (NULL); |
| 1734 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1735 | facet->node = node; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1736 | value = xmlSchemaGetProp(ctxt, node, "value"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1737 | if (value == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1738 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_FACET_NO_VALUE, |
| 1739 | "Facet %s has no value\n", node->name, NULL); |
| 1740 | xmlSchemaFreeFacet(facet); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1741 | return (NULL); |
| 1742 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1743 | if (IS_SCHEMA(node, "minInclusive")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1744 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1745 | } else if (IS_SCHEMA(node, "minExclusive")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1746 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1747 | } else if (IS_SCHEMA(node, "maxInclusive")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1748 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1749 | } else if (IS_SCHEMA(node, "maxExclusive")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1750 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1751 | } else if (IS_SCHEMA(node, "totalDigits")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1752 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1753 | } else if (IS_SCHEMA(node, "fractionDigits")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1754 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1755 | } else if (IS_SCHEMA(node, "pattern")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1756 | facet->type = XML_SCHEMA_FACET_PATTERN; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1757 | } else if (IS_SCHEMA(node, "enumeration")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1758 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1759 | } else if (IS_SCHEMA(node, "whiteSpace")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1760 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1761 | } else if (IS_SCHEMA(node, "length")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1762 | facet->type = XML_SCHEMA_FACET_LENGTH; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1763 | } else if (IS_SCHEMA(node, "maxLength")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1764 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
| 1765 | } else if (IS_SCHEMA(node, "minLength")) { |
| 1766 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
| 1767 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1768 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_TYPE, |
| 1769 | "Unknown facet type %s\n", node->name, NULL); |
| 1770 | xmlSchemaFreeFacet(facet); |
| 1771 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1772 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1773 | facet->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1774 | facet->value = value; |
| 1775 | child = node->children; |
| 1776 | |
| 1777 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1778 | facet->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 1779 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1780 | } |
| 1781 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1782 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_CHILD, |
| 1783 | "Facet %s has unexpected child content\n", |
| 1784 | node->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1785 | } |
| 1786 | return (facet); |
| 1787 | } |
| 1788 | |
| 1789 | /** |
| 1790 | * xmlSchemaParseAny: |
| 1791 | * @ctxt: a schema validation context |
| 1792 | * @schema: the schema being built |
| 1793 | * @node: a subtree containing XML Schema informations |
| 1794 | * |
| 1795 | * parse a XML schema Any declaration |
| 1796 | * *WARNING* this interface is highly subject to change |
| 1797 | * |
| 1798 | * Returns the new type structure or NULL in case of error |
| 1799 | */ |
| 1800 | static xmlSchemaTypePtr |
| 1801 | xmlSchemaParseAny(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 1802 | xmlNodePtr node) |
| 1803 | { |
| 1804 | xmlSchemaTypePtr type; |
| 1805 | xmlNodePtr child = NULL; |
| 1806 | xmlChar name[30]; |
| 1807 | |
| 1808 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1809 | return (NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1810 | snprintf((char *) name, 30, "any %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1811 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1812 | if (type == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1813 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1814 | type->node = node; |
| 1815 | type->type = XML_SCHEMA_TYPE_ANY; |
| 1816 | child = node->children; |
| 1817 | type->minOccurs = xmlGetMinOccurs(ctxt, node); |
| 1818 | type->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
| 1819 | |
| 1820 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1821 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 1822 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1823 | } |
| 1824 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1825 | xmlSchemaPErr2(ctxt, node, child, |
| 1826 | XML_SCHEMAP_UNKNOWN_SEQUENCE_CHILD, |
| 1827 | "Sequence %s has unexpected content\n", type->name, |
| 1828 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | return (type); |
| 1832 | } |
| 1833 | |
| 1834 | /** |
| 1835 | * xmlSchemaParseNotation: |
| 1836 | * @ctxt: a schema validation context |
| 1837 | * @schema: the schema being built |
| 1838 | * @node: a subtree containing XML Schema informations |
| 1839 | * |
| 1840 | * parse a XML schema Notation declaration |
| 1841 | * |
| 1842 | * Returns the new structure or NULL in case of error |
| 1843 | */ |
| 1844 | static xmlSchemaNotationPtr |
| 1845 | xmlSchemaParseNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1846 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1847 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1848 | const xmlChar *name; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1849 | xmlSchemaNotationPtr ret; |
| 1850 | xmlNodePtr child = NULL; |
| 1851 | |
| 1852 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1853 | return (NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1854 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1855 | if (name == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1856 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_NOTATION_NO_NAME, |
| 1857 | "Notation has no name\n", NULL, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1858 | return (NULL); |
| 1859 | } |
| 1860 | ret = xmlSchemaAddNotation(ctxt, schema, name); |
| 1861 | if (ret == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1862 | return (NULL); |
| 1863 | } |
| 1864 | child = node->children; |
| 1865 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1866 | ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 1867 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1868 | } |
| 1869 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1870 | xmlSchemaPErr2(ctxt, node, child, |
| 1871 | XML_SCHEMAP_UNKNOWN_NOTATION_CHILD, |
| 1872 | "notation %s has unexpected content\n", name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | return (ret); |
| 1876 | } |
| 1877 | |
| 1878 | /** |
| 1879 | * xmlSchemaParseAnyAttribute: |
| 1880 | * @ctxt: a schema validation context |
| 1881 | * @schema: the schema being built |
| 1882 | * @node: a subtree containing XML Schema informations |
| 1883 | * |
| 1884 | * parse a XML schema AnyAttrribute declaration |
| 1885 | * *WARNING* this interface is highly subject to change |
| 1886 | * |
| 1887 | * Returns an attribute def structure or NULL |
| 1888 | */ |
| 1889 | static xmlSchemaAttributePtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1890 | xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt, |
| 1891 | xmlSchemaPtr schema, xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1892 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1893 | const xmlChar *processContents; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1894 | xmlSchemaAttributePtr ret; |
| 1895 | xmlNodePtr child = NULL; |
| 1896 | char name[100]; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1897 | const xmlChar *local, *ns; |
| 1898 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1899 | |
| 1900 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1901 | return (NULL); |
| 1902 | |
| 1903 | snprintf(name, 99, "anyattr %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1904 | local = xmlSchemaGetNamespace(ctxt, schema, node, BAD_CAST "anyattr", &ns); |
| 1905 | ret = xmlSchemaAddAttribute(ctxt, schema, BAD_CAST name, ns); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1906 | if (ret == NULL) { |
| 1907 | return (NULL); |
| 1908 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1909 | ret->id = xmlSchemaGetProp(ctxt, node, "id"); |
| 1910 | processContents = xmlSchemaGetProp(ctxt, node, "processContents"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1911 | if ((processContents == NULL) |
| 1912 | || (xmlStrEqual(processContents, (const xmlChar *) "strict"))) { |
| 1913 | ret->occurs = XML_SCHEMAS_ANYATTR_STRICT; |
| 1914 | } else if (xmlStrEqual(processContents, (const xmlChar *) "skip")) { |
| 1915 | ret->occurs = XML_SCHEMAS_ANYATTR_SKIP; |
| 1916 | } else if (xmlStrEqual(processContents, (const xmlChar *) "lax")) { |
| 1917 | ret->occurs = XML_SCHEMAS_ANYATTR_LAX; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1918 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1919 | xmlSchemaPErr2(ctxt, node, child, |
| 1920 | XML_SCHEMAP_UNKNOWN_PROCESSCONTENT_CHILD, |
| 1921 | "anyAttribute has unexpected content for processContents: %s\n", |
| 1922 | processContents, NULL); |
| 1923 | ret->occurs = XML_SCHEMAS_ANYATTR_STRICT; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1924 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1925 | |
| 1926 | child = node->children; |
| 1927 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1928 | ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 1929 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1930 | } |
| 1931 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1932 | xmlSchemaPErr2(ctxt, node, child, |
| 1933 | XML_SCHEMAP_UNKNOWN_ANYATTRIBUTE_CHILD, |
| 1934 | "anyAttribute %s has unexpected content\n", |
| 1935 | (const xmlChar *) name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | return (ret); |
| 1939 | } |
| 1940 | |
| 1941 | |
| 1942 | /** |
| 1943 | * xmlSchemaParseAttribute: |
| 1944 | * @ctxt: a schema validation context |
| 1945 | * @schema: the schema being built |
| 1946 | * @node: a subtree containing XML Schema informations |
| 1947 | * |
| 1948 | * parse a XML schema Attrribute declaration |
| 1949 | * *WARNING* this interface is highly subject to change |
| 1950 | * |
| 1951 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 1952 | * 1 in case of success. |
| 1953 | */ |
| 1954 | static xmlSchemaAttributePtr |
| 1955 | xmlSchemaParseAttribute(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 1956 | xmlNodePtr node) |
| 1957 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1958 | const xmlChar *name, *refNs = NULL, *ref = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1959 | xmlSchemaAttributePtr ret; |
| 1960 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1961 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1962 | |
| 1963 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1964 | return (NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1965 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1966 | if (name == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1967 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1968 | ref = xmlGetQNameProp(ctxt, node, "ref", &refNs); |
| 1969 | if (ref == NULL) { |
| 1970 | xmlSchemaPErr2(ctxt, node, child, |
| 1971 | XML_SCHEMAP_ATTR_NONAME_NOREF, |
| 1972 | "Attribute has no name nor ref\n", NULL, NULL); |
| 1973 | return (NULL); |
| 1974 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1975 | if (refNs == NULL) |
| 1976 | refNs = schema->targetNamespace; |
| 1977 | snprintf(buf, 99, "anonattr %d", ctxt->counter++ + 1); |
| 1978 | name = (const xmlChar *) buf; |
| 1979 | ret = xmlSchemaAddAttribute(ctxt, schema, name, NULL); |
| 1980 | } else { |
| 1981 | const xmlChar *local, *ns; |
| 1982 | |
| 1983 | local = xmlSchemaGetNamespace(ctxt, schema, node, name, &ns); |
| 1984 | ret = xmlSchemaAddAttribute(ctxt, schema, local, ns); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1985 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1986 | if (ret == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1987 | return (NULL); |
| 1988 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1989 | ret->ref = ref; |
| 1990 | ret->refNs = refNs; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1991 | if ((ret->targetNamespace != NULL) && |
| 1992 | ((schema->flags & XML_SCHEMAS_QUALIF_ATTR) == 0) && |
| 1993 | (xmlStrEqual(ret->targetNamespace, schema->targetNamespace))) |
| 1994 | ret->flags |= XML_SCHEMAS_ATTR_NSDEFAULT; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1995 | ret->typeName = xmlGetQNameProp(ctxt, node, "type", &(ret->typeNs)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1996 | if ((ret->typeName != NULL) && (ret->typeNs == NULL)) |
| 1997 | ret->typeNs = schema->targetNamespace; |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 1998 | ret->node = node; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1999 | child = node->children; |
| 2000 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2001 | ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2002 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2003 | } |
| 2004 | if (IS_SCHEMA(child, "simpleType")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2005 | ret->subtypes = xmlSchemaParseSimpleType(ctxt, schema, child); |
| 2006 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2007 | } |
| 2008 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2009 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_ATTR_CHILD, |
| 2010 | "attribute %s has unexpected content\n", name, |
| 2011 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
| 2014 | return (ret); |
| 2015 | } |
| 2016 | |
| 2017 | /** |
| 2018 | * xmlSchemaParseAttributeGroup: |
| 2019 | * @ctxt: a schema validation context |
| 2020 | * @schema: the schema being built |
| 2021 | * @node: a subtree containing XML Schema informations |
| 2022 | * |
| 2023 | * parse a XML schema Attribute Group declaration |
| 2024 | * *WARNING* this interface is highly subject to change |
| 2025 | * |
| 2026 | * Returns the attribute group or NULL in case of error. |
| 2027 | */ |
| 2028 | static xmlSchemaAttributeGroupPtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2029 | xmlSchemaParseAttributeGroup(xmlSchemaParserCtxtPtr ctxt, |
| 2030 | xmlSchemaPtr schema, xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2031 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2032 | const xmlChar *name, *refNs = NULL, *ref = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2033 | xmlSchemaAttributeGroupPtr ret; |
| 2034 | xmlSchemaAttributePtr last = NULL, attr; |
| 2035 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2036 | const xmlChar *oldcontainer; |
| 2037 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2038 | |
| 2039 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2040 | return (NULL); |
| 2041 | oldcontainer = ctxt->container; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2042 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2043 | if (name == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2044 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2045 | ref = xmlGetQNameProp(ctxt, node, "ref", &refNs); |
| 2046 | if (ref == NULL) { |
| 2047 | xmlSchemaPErr2(ctxt, node, child, |
| 2048 | XML_SCHEMAP_ATTRGRP_NONAME_NOREF, |
| 2049 | "AttributeGroup has no name nor ref\n", NULL, |
| 2050 | NULL); |
| 2051 | return (NULL); |
| 2052 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2053 | if (refNs == NULL) |
| 2054 | refNs = schema->targetNamespace; |
| 2055 | snprintf(buf, 99, "anonattrgroup %d", ctxt->counter++ + 1); |
| 2056 | name = (const xmlChar *) buf; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2057 | if (name == NULL) { |
| 2058 | xmlSchemaPErrMemory(ctxt, "creating attribute group", node); |
| 2059 | return (NULL); |
| 2060 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2061 | } |
| 2062 | ret = xmlSchemaAddAttributeGroup(ctxt, schema, name); |
| 2063 | if (ret == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2064 | return (NULL); |
| 2065 | } |
| 2066 | ret->ref = ref; |
| 2067 | ret->refNs = refNs; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 2068 | ret->type = XML_SCHEMA_TYPE_ATTRIBUTEGROUP; |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 2069 | ret->node = node; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2070 | child = node->children; |
| 2071 | ctxt->container = name; |
| 2072 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2073 | ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2074 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2075 | } |
| 2076 | while ((IS_SCHEMA(child, "attribute")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2077 | (IS_SCHEMA(child, "attributeGroup"))) { |
| 2078 | attr = NULL; |
| 2079 | if (IS_SCHEMA(child, "attribute")) { |
| 2080 | attr = xmlSchemaParseAttribute(ctxt, schema, child); |
| 2081 | } else if (IS_SCHEMA(child, "attributeGroup")) { |
| 2082 | attr = (xmlSchemaAttributePtr) |
| 2083 | xmlSchemaParseAttributeGroup(ctxt, schema, child); |
| 2084 | } |
| 2085 | if (attr != NULL) { |
| 2086 | if (last == NULL) { |
| 2087 | ret->attributes = attr; |
| 2088 | last = attr; |
| 2089 | } else { |
| 2090 | last->next = attr; |
| 2091 | last = attr; |
| 2092 | } |
| 2093 | } |
| 2094 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2095 | } |
| 2096 | if (IS_SCHEMA(child, "anyAttribute")) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2097 | TODO |
| 2098 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2099 | } |
| 2100 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2101 | xmlSchemaPErr2(ctxt, node, child, |
| 2102 | XML_SCHEMAP_UNKNOWN_ATTRGRP_CHILD, |
| 2103 | "attribute group %s has unexpected content\n", name, |
| 2104 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2105 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2106 | ctxt->container = oldcontainer; |
| 2107 | return (ret); |
| 2108 | } |
| 2109 | |
| 2110 | /** |
| 2111 | * xmlSchemaParseElement: |
| 2112 | * @ctxt: a schema validation context |
| 2113 | * @schema: the schema being built |
| 2114 | * @node: a subtree containing XML Schema informations |
| 2115 | * |
| 2116 | * parse a XML schema Element declaration |
| 2117 | * *WARNING* this interface is highly subject to change |
| 2118 | * |
| 2119 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2120 | * 1 in case of success. |
| 2121 | */ |
| 2122 | static xmlSchemaElementPtr |
| 2123 | xmlSchemaParseElement(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 2124 | xmlNodePtr node, int toplevel) |
| 2125 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2126 | const xmlChar *name, *fixed; |
| 2127 | const xmlChar *refNs = NULL, *ref = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2128 | xmlSchemaElementPtr ret; |
| 2129 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2130 | const xmlChar *oldcontainer; |
| 2131 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2132 | |
| 2133 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2134 | return (NULL); |
| 2135 | oldcontainer = ctxt->container; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2136 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2137 | if (name == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2138 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2139 | ref = xmlGetQNameProp(ctxt, node, "ref", &refNs); |
| 2140 | if (ref == NULL) { |
| 2141 | xmlSchemaPErr2(ctxt, node, child, |
| 2142 | XML_SCHEMAP_ELEM_NONAME_NOREF, |
| 2143 | "Element has no name nor ref\n", NULL, NULL); |
| 2144 | return (NULL); |
| 2145 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2146 | if (refNs == NULL) |
| 2147 | refNs = schema->targetNamespace; |
| 2148 | snprintf(buf, 99, "anonelem %d", ctxt->counter++ + 1); |
| 2149 | name = (const xmlChar *) buf; |
| 2150 | ret = xmlSchemaAddElement(ctxt, schema, name, NULL); |
| 2151 | } else { |
| 2152 | const xmlChar *local, *ns; |
| 2153 | |
| 2154 | local = xmlSchemaGetNamespace(ctxt, schema, node, name, &ns); |
| 2155 | ret = xmlSchemaAddElement(ctxt, schema, local, ns); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2156 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2157 | if (ret != NULL) |
| 2158 | ret->node = node; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2159 | if (ret == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2160 | return (NULL); |
| 2161 | } |
| 2162 | ret->type = XML_SCHEMA_TYPE_ELEMENT; |
| 2163 | ret->ref = ref; |
| 2164 | ret->refNs = refNs; |
| 2165 | if (ref != NULL) |
| 2166 | ret->flags |= XML_SCHEMAS_ELEM_REF; |
| 2167 | if (toplevel) |
| 2168 | ret->flags |= XML_SCHEMAS_ELEM_TOPLEVEL; |
| 2169 | if (xmlGetBooleanProp(ctxt, node, "nillable", 0)) |
| 2170 | ret->flags |= XML_SCHEMAS_ELEM_NILLABLE; |
| 2171 | if (xmlGetBooleanProp(ctxt, node, "abstract", 0)) |
| 2172 | ret->flags |= XML_SCHEMAS_ELEM_NILLABLE; |
| 2173 | ctxt->container = name; |
| 2174 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2175 | ret->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2176 | ret->namedType = |
| 2177 | xmlGetQNameProp(ctxt, node, "type", &(ret->namedTypeNs)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2178 | if ((ret->namedType != NULL) && (ret->namedTypeNs == NULL)) |
| 2179 | ret->namedTypeNs = schema->targetNamespace; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2180 | ret->substGroup = |
| 2181 | xmlGetQNameProp(ctxt, node, "substitutionGroup", |
| 2182 | &(ret->substGroupNs)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2183 | fixed = xmlSchemaGetProp(ctxt, node, "fixed"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2184 | ret->minOccurs = xmlGetMinOccurs(ctxt, node); |
| 2185 | ret->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2186 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2187 | ret->value = xmlSchemaGetProp(ctxt, node, "default"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2188 | if ((ret->value != NULL) && (fixed != NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2189 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_ELEM_DEFAULT_FIXED, |
| 2190 | "Element %s has both default and fixed\n", |
| 2191 | ret->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2192 | } else if (fixed != NULL) { |
| 2193 | ret->flags |= XML_SCHEMAS_ELEM_FIXED; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2194 | ret->value = fixed; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | child = node->children; |
| 2198 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2199 | ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2200 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2201 | } |
| 2202 | if (IS_SCHEMA(child, "complexType")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2203 | ret->subtypes = xmlSchemaParseComplexType(ctxt, schema, child); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2204 | child = child->next; |
| 2205 | } else if (IS_SCHEMA(child, "simpleType")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2206 | ret->subtypes = xmlSchemaParseSimpleType(ctxt, schema, child); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2207 | child = child->next; |
| 2208 | } |
| 2209 | while ((IS_SCHEMA(child, "unique")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2210 | (IS_SCHEMA(child, "key")) || (IS_SCHEMA(child, "keyref"))) { |
| 2211 | TODO child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2212 | } |
| 2213 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2214 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_ELEM_CHILD, |
| 2215 | "element %s has unexpected content\n", name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | ctxt->container = oldcontainer; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2219 | return (ret); |
| 2220 | } |
| 2221 | |
| 2222 | /** |
| 2223 | * xmlSchemaParseUnion: |
| 2224 | * @ctxt: a schema validation context |
| 2225 | * @schema: the schema being built |
| 2226 | * @node: a subtree containing XML Schema informations |
| 2227 | * |
| 2228 | * parse a XML schema Union definition |
| 2229 | * *WARNING* this interface is highly subject to change |
| 2230 | * |
| 2231 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2232 | * 1 in case of success. |
| 2233 | */ |
| 2234 | static xmlSchemaTypePtr |
| 2235 | xmlSchemaParseUnion(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2236 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2237 | { |
| 2238 | xmlSchemaTypePtr type, subtype, last = NULL; |
| 2239 | xmlNodePtr child = NULL; |
| 2240 | xmlChar name[30]; |
| 2241 | |
| 2242 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2243 | return (NULL); |
| 2244 | |
| 2245 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2246 | snprintf((char *) name, 30, "union %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2247 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2248 | if (type == NULL) |
| 2249 | return (NULL); |
| 2250 | type->node = node; |
| 2251 | type->type = XML_SCHEMA_TYPE_LIST; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2252 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
| 2253 | type->ref = xmlSchemaGetProp(ctxt, node, "memberTypes"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2254 | |
| 2255 | child = node->children; |
| 2256 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2257 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2258 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2259 | } |
| 2260 | while (IS_SCHEMA(child, "simpleType")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2261 | subtype = (xmlSchemaTypePtr) |
| 2262 | xmlSchemaParseSimpleType(ctxt, schema, child); |
| 2263 | if (subtype != NULL) { |
| 2264 | if (last == NULL) { |
| 2265 | type->subtypes = subtype; |
| 2266 | last = subtype; |
| 2267 | } else { |
| 2268 | last->next = subtype; |
| 2269 | last = subtype; |
| 2270 | } |
| 2271 | last->next = NULL; |
| 2272 | } |
| 2273 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2274 | } |
| 2275 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2276 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_UNION_CHILD, |
| 2277 | "Union %s has unexpected content\n", type->name, |
| 2278 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2279 | } |
| 2280 | return (type); |
| 2281 | } |
| 2282 | |
| 2283 | /** |
| 2284 | * xmlSchemaParseList: |
| 2285 | * @ctxt: a schema validation context |
| 2286 | * @schema: the schema being built |
| 2287 | * @node: a subtree containing XML Schema informations |
| 2288 | * |
| 2289 | * parse a XML schema List definition |
| 2290 | * *WARNING* this interface is highly subject to change |
| 2291 | * |
| 2292 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2293 | * 1 in case of success. |
| 2294 | */ |
| 2295 | static xmlSchemaTypePtr |
| 2296 | xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2297 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2298 | { |
| 2299 | xmlSchemaTypePtr type, subtype; |
| 2300 | xmlNodePtr child = NULL; |
| 2301 | xmlChar name[30]; |
| 2302 | |
| 2303 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2304 | return (NULL); |
| 2305 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2306 | snprintf((char *) name, 30, "list %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2307 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2308 | if (type == NULL) |
| 2309 | return (NULL); |
| 2310 | type->node = node; |
| 2311 | type->type = XML_SCHEMA_TYPE_LIST; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2312 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2313 | type->ref = xmlGetQNameProp(ctxt, node, "ref", &(type->refNs)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2314 | if ((type->ref != NULL) && (type->refNs == NULL)) |
| 2315 | type->refNs = schema->targetNamespace; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2316 | |
| 2317 | child = node->children; |
| 2318 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2319 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2320 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2321 | } |
| 2322 | subtype = NULL; |
| 2323 | if (IS_SCHEMA(child, "simpleType")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2324 | subtype = (xmlSchemaTypePtr) |
| 2325 | xmlSchemaParseSimpleType(ctxt, schema, child); |
| 2326 | child = child->next; |
| 2327 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2328 | } |
| 2329 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2330 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_LIST_CHILD, |
| 2331 | "List %s has unexpected content\n", type->name, |
| 2332 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2333 | } |
| 2334 | return (type); |
| 2335 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2336 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2337 | /** |
| 2338 | * xmlSchemaParseSimpleType: |
| 2339 | * @ctxt: a schema validation context |
| 2340 | * @schema: the schema being built |
| 2341 | * @node: a subtree containing XML Schema informations |
| 2342 | * |
| 2343 | * parse a XML schema Simple Type definition |
| 2344 | * *WARNING* this interface is highly subject to change |
| 2345 | * |
| 2346 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2347 | * 1 in case of success. |
| 2348 | */ |
| 2349 | static xmlSchemaTypePtr |
| 2350 | xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 2351 | xmlNodePtr node) |
| 2352 | { |
| 2353 | xmlSchemaTypePtr type, subtype; |
| 2354 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2355 | const xmlChar *name; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2356 | |
| 2357 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2358 | return (NULL); |
| 2359 | |
| 2360 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2361 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2362 | if (name == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2363 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2364 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2365 | snprintf(buf, 99, "simpletype %d", ctxt->counter++ + 1); |
| 2366 | type = xmlSchemaAddType(ctxt, schema, (const xmlChar *)buf, NULL); |
| 2367 | } else { |
| 2368 | const xmlChar *local, *ns; |
| 2369 | |
| 2370 | local = xmlSchemaGetNamespace(ctxt, schema, node, name, &ns); |
| 2371 | type = xmlSchemaAddType(ctxt, schema, local, ns); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2372 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2373 | if (type == NULL) |
| 2374 | return (NULL); |
| 2375 | type->node = node; |
| 2376 | type->type = XML_SCHEMA_TYPE_SIMPLE; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2377 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2378 | |
| 2379 | child = node->children; |
| 2380 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2381 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2382 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2383 | } |
| 2384 | subtype = NULL; |
| 2385 | if (IS_SCHEMA(child, "restriction")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2386 | subtype = (xmlSchemaTypePtr) |
| 2387 | xmlSchemaParseRestriction(ctxt, schema, child, 1); |
| 2388 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2389 | } else if (IS_SCHEMA(child, "list")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2390 | subtype = (xmlSchemaTypePtr) |
| 2391 | xmlSchemaParseList(ctxt, schema, child); |
| 2392 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2393 | } else if (IS_SCHEMA(child, "union")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2394 | subtype = (xmlSchemaTypePtr) |
| 2395 | xmlSchemaParseUnion(ctxt, schema, child); |
| 2396 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2397 | } |
| 2398 | type->subtypes = subtype; |
| 2399 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2400 | xmlSchemaPErr2(ctxt, node, child, |
| 2401 | XML_SCHEMAP_UNKNOWN_SIMPLETYPE_CHILD, |
| 2402 | "SimpleType %s has unexpected content\n", |
| 2403 | type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
| 2406 | return (type); |
| 2407 | } |
| 2408 | |
| 2409 | |
| 2410 | /** |
| 2411 | * xmlSchemaParseGroup: |
| 2412 | * @ctxt: a schema validation context |
| 2413 | * @schema: the schema being built |
| 2414 | * @node: a subtree containing XML Schema informations |
| 2415 | * |
| 2416 | * parse a XML schema Group definition |
| 2417 | * *WARNING* this interface is highly subject to change |
| 2418 | * |
| 2419 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2420 | * 1 in case of success. |
| 2421 | */ |
| 2422 | static xmlSchemaTypePtr |
| 2423 | xmlSchemaParseGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2424 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2425 | { |
| 2426 | xmlSchemaTypePtr type, subtype; |
| 2427 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2428 | const xmlChar *name; |
| 2429 | const xmlChar *ref = NULL, *refNs = NULL; |
| 2430 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2431 | |
| 2432 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2433 | return (NULL); |
| 2434 | |
| 2435 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2436 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2437 | if (name == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2438 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2439 | ref = xmlGetQNameProp(ctxt, node, "ref", &refNs); |
| 2440 | if (ref == NULL) { |
| 2441 | xmlSchemaPErr2(ctxt, node, child, |
| 2442 | XML_SCHEMAP_GROUP_NONAME_NOREF, |
| 2443 | "Group has no name nor ref\n", NULL, NULL); |
| 2444 | return (NULL); |
| 2445 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2446 | if (refNs == NULL) |
| 2447 | refNs = schema->targetNamespace; |
| 2448 | snprintf(buf, 99, "anongroup %d", ctxt->counter++ + 1); |
| 2449 | name = (const xmlChar *) buf; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2450 | } |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 2451 | type = xmlSchemaAddGroup(ctxt, schema, name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2452 | if (type == NULL) |
| 2453 | return (NULL); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2454 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2455 | type->node = node; |
| 2456 | type->type = XML_SCHEMA_TYPE_GROUP; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2457 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2458 | type->ref = ref; |
| 2459 | type->refNs = refNs; |
| 2460 | type->minOccurs = xmlGetMinOccurs(ctxt, node); |
| 2461 | type->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
| 2462 | |
| 2463 | child = node->children; |
| 2464 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2465 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2466 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2467 | } |
| 2468 | subtype = NULL; |
| 2469 | if (IS_SCHEMA(child, "all")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2470 | subtype = (xmlSchemaTypePtr) |
| 2471 | xmlSchemaParseAll(ctxt, schema, child); |
| 2472 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2473 | } else if (IS_SCHEMA(child, "choice")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2474 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 2475 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2476 | } else if (IS_SCHEMA(child, "sequence")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2477 | subtype = (xmlSchemaTypePtr) |
| 2478 | xmlSchemaParseSequence(ctxt, schema, child); |
| 2479 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2480 | } |
| 2481 | if (subtype != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2482 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2483 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2484 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_GROUP_CHILD, |
| 2485 | "Group %s has unexpected content\n", type->name, |
| 2486 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
| 2489 | return (type); |
| 2490 | } |
| 2491 | |
| 2492 | /** |
| 2493 | * xmlSchemaParseAll: |
| 2494 | * @ctxt: a schema validation context |
| 2495 | * @schema: the schema being built |
| 2496 | * @node: a subtree containing XML Schema informations |
| 2497 | * |
| 2498 | * parse a XML schema All definition |
| 2499 | * *WARNING* this interface is highly subject to change |
| 2500 | * |
| 2501 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2502 | * 1 in case of success. |
| 2503 | */ |
| 2504 | static xmlSchemaTypePtr |
| 2505 | xmlSchemaParseAll(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2506 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2507 | { |
| 2508 | xmlSchemaTypePtr type, subtype, last = NULL; |
| 2509 | xmlNodePtr child = NULL; |
| 2510 | xmlChar name[30]; |
| 2511 | |
| 2512 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2513 | return (NULL); |
| 2514 | |
| 2515 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2516 | snprintf((char *) name, 30, "all%d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2517 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2518 | if (type == NULL) |
| 2519 | return (NULL); |
| 2520 | type->node = node; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 2521 | type->type = XML_SCHEMA_TYPE_ALL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2522 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2523 | type->minOccurs = xmlGetMinOccurs(ctxt, node); |
| 2524 | type->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
| 2525 | |
| 2526 | child = node->children; |
| 2527 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2528 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2529 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2530 | } |
| 2531 | while (IS_SCHEMA(child, "element")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2532 | subtype = (xmlSchemaTypePtr) |
| 2533 | xmlSchemaParseElement(ctxt, schema, child, 0); |
| 2534 | if (subtype != NULL) { |
| 2535 | if (last == NULL) { |
| 2536 | type->subtypes = subtype; |
| 2537 | last = subtype; |
| 2538 | } else { |
| 2539 | last->next = subtype; |
| 2540 | last = subtype; |
| 2541 | } |
| 2542 | last->next = NULL; |
| 2543 | } |
| 2544 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2545 | } |
| 2546 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2547 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_ALL_CHILD, |
| 2548 | "All %s has unexpected content\n", type->name, |
| 2549 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
| 2552 | return (type); |
| 2553 | } |
| 2554 | |
| 2555 | /** |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2556 | * xmlSchemaImportSchema |
| 2557 | * |
| 2558 | * @ctxt: a schema validation context |
| 2559 | * @schemaLocation: an URI defining where to find the imported schema |
| 2560 | * |
| 2561 | * import a XML schema |
| 2562 | * *WARNING* this interface is highly subject to change |
| 2563 | * |
| 2564 | * Returns -1 in case of error and 1 in case of success. |
| 2565 | */ |
| 2566 | static xmlSchemaImportPtr |
| 2567 | xmlSchemaImportSchema(xmlSchemaParserCtxtPtr ctxt, |
| 2568 | const xmlChar *schemaLocation) |
| 2569 | { |
| 2570 | xmlSchemaImportPtr import; |
| 2571 | xmlSchemaParserCtxtPtr newctxt; |
| 2572 | |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2573 | newctxt = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt)); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2574 | if (newctxt == NULL) { |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2575 | xmlSchemaPErrMemory(ctxt, "allocating schama parser context", |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2576 | NULL); |
| 2577 | return (NULL); |
| 2578 | } |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2579 | memset(newctxt, 0, sizeof(xmlSchemaParserCtxt)); |
| 2580 | /* Keep the same dictionnary for parsing, really */ |
| 2581 | xmlDictReference(ctxt->dict); |
| 2582 | newctxt->dict = ctxt->dict; |
| 2583 | newctxt->URL = xmlDictLookup(newctxt->dict, schemaLocation, -1); |
| 2584 | |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2585 | xmlSchemaSetParserErrors(newctxt, ctxt->error, ctxt->warning, |
| 2586 | ctxt->userData); |
| 2587 | |
| 2588 | import = (xmlSchemaImport*) xmlMalloc(sizeof(xmlSchemaImport)); |
| 2589 | if (import == NULL) { |
| 2590 | xmlSchemaPErrMemory(NULL, "allocating imported schema", |
| 2591 | NULL); |
| 2592 | xmlSchemaFreeParserCtxt(newctxt); |
| 2593 | return (NULL); |
| 2594 | } |
| 2595 | |
| 2596 | memset(import, 0, sizeof(xmlSchemaImport)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2597 | import->schemaLocation = xmlDictLookup(ctxt->dict, schemaLocation, -1); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2598 | import->schema = xmlSchemaParse(newctxt); |
| 2599 | |
| 2600 | if (import->schema == NULL) { |
| 2601 | /* FIXME use another error enum here ? */ |
| 2602 | xmlSchemaPErr(ctxt, NULL, XML_SCHEMAS_ERR_INTERNAL, |
| 2603 | "failed to import schema at location %s\n", |
| 2604 | schemaLocation, NULL); |
| 2605 | |
| 2606 | xmlSchemaFreeParserCtxt(newctxt); |
| 2607 | if (import->schemaLocation != NULL) |
| 2608 | xmlFree((xmlChar *)import->schemaLocation); |
| 2609 | xmlFree(import); |
| 2610 | return NULL; |
| 2611 | } |
| 2612 | |
| 2613 | xmlSchemaFreeParserCtxt(newctxt); |
| 2614 | return import; |
| 2615 | } |
| 2616 | |
| 2617 | |
| 2618 | /** |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2619 | * xmlSchemaParseImport: |
| 2620 | * @ctxt: a schema validation context |
| 2621 | * @schema: the schema being built |
| 2622 | * @node: a subtree containing XML Schema informations |
| 2623 | * |
| 2624 | * parse a XML schema Import definition |
| 2625 | * *WARNING* this interface is highly subject to change |
| 2626 | * |
| 2627 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2628 | * 1 in case of success. |
| 2629 | */ |
| 2630 | static int |
| 2631 | xmlSchemaParseImport(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2632 | xmlNodePtr node) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2633 | { |
| 2634 | xmlNodePtr child = NULL; |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2635 | xmlSchemaImportPtr import = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2636 | const xmlChar *namespace; |
| 2637 | const xmlChar *schemaLocation; |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2638 | const xmlChar *previous; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2639 | xmlURIPtr check; |
| 2640 | |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2641 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2642 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2643 | return (-1); |
| 2644 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2645 | namespace = xmlSchemaGetProp(ctxt, node, "namespace"); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2646 | if (namespace != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2647 | check = xmlParseURI((const char *) namespace); |
| 2648 | if (check == NULL) { |
| 2649 | xmlSchemaPErr2(ctxt, node, child, |
| 2650 | XML_SCHEMAP_IMPORT_NAMESPACE_NOT_URI, |
| 2651 | "Import namespace attribute is not an URI: %s\n", |
| 2652 | namespace, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2653 | return (-1); |
| 2654 | } else { |
| 2655 | xmlFreeURI(check); |
| 2656 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2657 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2658 | schemaLocation = xmlSchemaGetProp(ctxt, node, "schemaLocation"); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2659 | if (schemaLocation != NULL) { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2660 | xmlChar *base = NULL; |
| 2661 | xmlChar *URI = NULL; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2662 | check = xmlParseURI((const char *) schemaLocation); |
| 2663 | if (check == NULL) { |
| 2664 | xmlSchemaPErr2(ctxt, node, child, |
| 2665 | XML_SCHEMAP_IMPORT_SCHEMA_NOT_URI, |
| 2666 | "Import schemaLocation attribute is not an URI: %s\n", |
| 2667 | schemaLocation, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2668 | return (-1); |
| 2669 | } else { |
| 2670 | xmlFreeURI(check); |
| 2671 | } |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2672 | base = xmlNodeGetBase(node->doc, node); |
| 2673 | if (base == NULL) { |
| 2674 | URI = xmlBuildURI(schemaLocation, node->doc->URL); |
| 2675 | } else { |
| 2676 | URI = xmlBuildURI(schemaLocation, base); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2677 | xmlFree(base); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2678 | } |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2679 | if (URI != NULL) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2680 | schemaLocation = xmlDictLookup(ctxt->dict, URI, -1); |
| 2681 | xmlFree(URI); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2682 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2683 | } |
| 2684 | if (schema->schemasImports == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2685 | schema->schemasImports = xmlHashCreate(10); |
| 2686 | if (schema->schemasImports == NULL) { |
| 2687 | xmlSchemaPErr2(ctxt, node, child, |
| 2688 | XML_SCHEMAP_FAILED_BUILD_IMPORT, |
| 2689 | "Internal: failed to build import table\n", |
| 2690 | NULL, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2691 | return (-1); |
| 2692 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2693 | } |
| 2694 | if (namespace == NULL) { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2695 | import = xmlHashLookup(schema->schemasImports, |
| 2696 | XML_SCHEMAS_DEFAULT_NAMESPACE); |
| 2697 | if (import != NULL) |
| 2698 | previous = import->schemaLocation; |
| 2699 | else |
| 2700 | previous = NULL; |
| 2701 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2702 | if (schemaLocation != NULL) { |
| 2703 | if (previous != NULL) { |
| 2704 | if (!xmlStrEqual(schemaLocation, previous)) { |
| 2705 | xmlSchemaPErr2(ctxt, node, child, |
| 2706 | XML_SCHEMAP_IMPORT_REDEFINE_NSNAME, |
| 2707 | "Redefining import for default namespace with a different URI: %s\n", |
| 2708 | schemaLocation, NULL); |
| 2709 | } |
| 2710 | } else { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2711 | import = xmlSchemaImportSchema(ctxt, schemaLocation); |
| 2712 | if (import == NULL) { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2713 | return (-1); |
| 2714 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2715 | xmlHashAddEntry(schema->schemasImports, |
| 2716 | XML_SCHEMAS_DEFAULT_NAMESPACE, |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2717 | import); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2718 | } |
| 2719 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2720 | } else { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2721 | import = xmlHashLookup(schema->schemasImports, namespace); |
| 2722 | if (import != NULL) |
| 2723 | previous = import->schemaLocation; |
| 2724 | else |
| 2725 | previous = NULL; |
| 2726 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2727 | if (schemaLocation != NULL) { |
| 2728 | if (previous != NULL) { |
| 2729 | if (!xmlStrEqual(schemaLocation, previous)) { |
| 2730 | xmlSchemaPErr2(ctxt, node, child, |
| 2731 | XML_SCHEMAP_IMPORT_REDEFINE_NSNAME, |
| 2732 | "Redefining import for namespace %s with a different URI: %s\n", |
| 2733 | namespace, schemaLocation); |
| 2734 | } |
| 2735 | } else { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2736 | import = xmlSchemaImportSchema(ctxt, schemaLocation); |
| 2737 | if (import == NULL) { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2738 | return (-1); |
| 2739 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2740 | xmlHashAddEntry(schema->schemasImports, |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2741 | namespace, import); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2742 | } |
| 2743 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2744 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2745 | |
| 2746 | child = node->children; |
| 2747 | while (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2748 | /* |
| 2749 | * the annotations here are simply discarded ... |
| 2750 | */ |
| 2751 | child = child->next; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2752 | } |
| 2753 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2754 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_IMPORT_CHILD, |
| 2755 | "Import has unexpected content\n", NULL, NULL); |
| 2756 | return (-1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2757 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2758 | return (1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2759 | } |
| 2760 | |
| 2761 | /** |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2762 | * xmlSchemaCleanupDoc: |
| 2763 | * @ctxt: a schema validation context |
| 2764 | * @node: the root of the document. |
| 2765 | * |
| 2766 | * removes unwanted nodes in a schemas document tree |
| 2767 | */ |
| 2768 | static void |
| 2769 | xmlSchemaCleanupDoc(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr root) |
| 2770 | { |
| 2771 | xmlNodePtr delete, cur; |
| 2772 | |
| 2773 | if ((ctxt == NULL) || (root == NULL)) return; |
| 2774 | |
| 2775 | /* |
| 2776 | * Remove all the blank text nodes |
| 2777 | */ |
| 2778 | delete = NULL; |
| 2779 | cur = root; |
| 2780 | while (cur != NULL) { |
| 2781 | if (delete != NULL) { |
| 2782 | xmlUnlinkNode(delete); |
| 2783 | xmlFreeNode(delete); |
| 2784 | delete = NULL; |
| 2785 | } |
| 2786 | if (cur->type == XML_TEXT_NODE) { |
| 2787 | if (IS_BLANK_NODE(cur)) { |
| 2788 | if (xmlNodeGetSpacePreserve(cur) != 1) { |
| 2789 | delete = cur; |
| 2790 | } |
| 2791 | } |
| 2792 | } else if ((cur->type != XML_ELEMENT_NODE) && |
| 2793 | (cur->type != XML_CDATA_SECTION_NODE)) { |
| 2794 | delete = cur; |
| 2795 | goto skip_children; |
| 2796 | } |
| 2797 | |
| 2798 | /* |
| 2799 | * Skip to next node |
| 2800 | */ |
| 2801 | if (cur->children != NULL) { |
| 2802 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 2803 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 2804 | (cur->children->type != XML_ENTITY_NODE)) { |
| 2805 | cur = cur->children; |
| 2806 | continue; |
| 2807 | } |
| 2808 | } |
| 2809 | skip_children: |
| 2810 | if (cur->next != NULL) { |
| 2811 | cur = cur->next; |
| 2812 | continue; |
| 2813 | } |
| 2814 | |
| 2815 | do { |
| 2816 | cur = cur->parent; |
| 2817 | if (cur == NULL) |
| 2818 | break; |
| 2819 | if (cur == root) { |
| 2820 | cur = NULL; |
| 2821 | break; |
| 2822 | } |
| 2823 | if (cur->next != NULL) { |
| 2824 | cur = cur->next; |
| 2825 | break; |
| 2826 | } |
| 2827 | } while (cur != NULL); |
| 2828 | } |
| 2829 | if (delete != NULL) { |
| 2830 | xmlUnlinkNode(delete); |
| 2831 | xmlFreeNode(delete); |
| 2832 | delete = NULL; |
| 2833 | } |
| 2834 | } |
| 2835 | |
| 2836 | /** |
| 2837 | * xmlSchemaParseSchemaTopLevel: |
| 2838 | * @ctxt: a schema validation context |
| 2839 | * @schema: the schemas |
| 2840 | * @nodes: the list of top level nodes |
| 2841 | * |
| 2842 | * Returns the internal XML Schema structure built from the resource or |
| 2843 | * NULL in case of error |
| 2844 | */ |
| 2845 | static void |
| 2846 | xmlSchemaParseSchemaTopLevel(xmlSchemaParserCtxtPtr ctxt, |
| 2847 | xmlSchemaPtr schema, xmlNodePtr nodes) |
| 2848 | { |
| 2849 | xmlNodePtr child; |
| 2850 | xmlSchemaAnnotPtr annot; |
| 2851 | |
| 2852 | if ((ctxt == NULL) || (schema == NULL) || (nodes == NULL)) |
| 2853 | return; |
| 2854 | |
| 2855 | child = nodes; |
| 2856 | while ((IS_SCHEMA(child, "include")) || |
| 2857 | (IS_SCHEMA(child, "import")) || |
| 2858 | (IS_SCHEMA(child, "redefine")) || |
| 2859 | (IS_SCHEMA(child, "annotation"))) { |
| 2860 | if (IS_SCHEMA(child, "annotation")) { |
| 2861 | annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2862 | if (schema->annot == NULL) |
| 2863 | schema->annot = annot; |
| 2864 | else |
| 2865 | xmlSchemaFreeAnnot(annot); |
| 2866 | } else if (IS_SCHEMA(child, "import")) { |
| 2867 | xmlSchemaParseImport(ctxt, schema, child); |
| 2868 | } else if (IS_SCHEMA(child, "include")) { |
| 2869 | xmlSchemaParseInclude(ctxt, schema, child); |
| 2870 | } else if (IS_SCHEMA(child, "redefine")) { |
| 2871 | TODO |
| 2872 | } |
| 2873 | child = child->next; |
| 2874 | } |
| 2875 | while (child != NULL) { |
| 2876 | if (IS_SCHEMA(child, "complexType")) { |
| 2877 | xmlSchemaParseComplexType(ctxt, schema, child); |
| 2878 | child = child->next; |
| 2879 | } else if (IS_SCHEMA(child, "simpleType")) { |
| 2880 | xmlSchemaParseSimpleType(ctxt, schema, child); |
| 2881 | child = child->next; |
| 2882 | } else if (IS_SCHEMA(child, "element")) { |
| 2883 | xmlSchemaParseElement(ctxt, schema, child, 1); |
| 2884 | child = child->next; |
| 2885 | } else if (IS_SCHEMA(child, "attribute")) { |
| 2886 | xmlSchemaParseAttribute(ctxt, schema, child); |
| 2887 | child = child->next; |
| 2888 | } else if (IS_SCHEMA(child, "attributeGroup")) { |
| 2889 | xmlSchemaParseAttributeGroup(ctxt, schema, child); |
| 2890 | child = child->next; |
| 2891 | } else if (IS_SCHEMA(child, "group")) { |
| 2892 | xmlSchemaParseGroup(ctxt, schema, child); |
| 2893 | child = child->next; |
| 2894 | } else if (IS_SCHEMA(child, "notation")) { |
| 2895 | xmlSchemaParseNotation(ctxt, schema, child); |
| 2896 | child = child->next; |
| 2897 | } else { |
| 2898 | xmlSchemaPErr2(ctxt, NULL, child, |
| 2899 | XML_SCHEMAP_UNKNOWN_SCHEMAS_CHILD, |
| 2900 | "Schemas: unexpected element %s here \n", |
| 2901 | child->name, NULL); |
| 2902 | child = child->next; |
| 2903 | } |
| 2904 | while (IS_SCHEMA(child, "annotation")) { |
| 2905 | annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2906 | if (schema->annot == NULL) |
| 2907 | schema->annot = annot; |
| 2908 | else |
| 2909 | xmlSchemaFreeAnnot(annot); |
| 2910 | child = child->next; |
| 2911 | } |
| 2912 | } |
| 2913 | } |
| 2914 | |
| 2915 | /** |
| 2916 | * xmlSchemaParseInclude: |
| 2917 | * @ctxt: a schema validation context |
| 2918 | * @schema: the schema being built |
| 2919 | * @node: a subtree containing XML Schema informations |
| 2920 | * |
| 2921 | * parse a XML schema Include definition |
| 2922 | * |
| 2923 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2924 | * 1 in case of success. |
| 2925 | */ |
| 2926 | static int |
| 2927 | xmlSchemaParseInclude(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 2928 | xmlNodePtr node) |
| 2929 | { |
| 2930 | xmlNodePtr child = NULL; |
| 2931 | const xmlChar *schemaLocation; |
| 2932 | xmlURIPtr check; |
| 2933 | xmlDocPtr doc; |
| 2934 | xmlNodePtr root; |
| 2935 | xmlSchemaIncludePtr include; |
| 2936 | |
| 2937 | |
| 2938 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2939 | return (-1); |
| 2940 | |
| 2941 | /* |
| 2942 | * Preliminary step, extract the URI-Reference for the include and |
| 2943 | * make an URI from the base. |
| 2944 | */ |
| 2945 | schemaLocation = xmlSchemaGetProp(ctxt, node, "schemaLocation"); |
| 2946 | if (schemaLocation != NULL) { |
| 2947 | xmlChar *base = NULL; |
| 2948 | xmlChar *URI = NULL; |
| 2949 | check = xmlParseURI((const char *) schemaLocation); |
| 2950 | if (check == NULL) { |
| 2951 | xmlSchemaPErr2(ctxt, node, child, |
| 2952 | XML_SCHEMAP_INCLUDE_SCHEMA_NOT_URI, |
| 2953 | "Include schemaLocation attribute is not an URI: %s\n", |
| 2954 | schemaLocation, NULL); |
| 2955 | return (-1); |
| 2956 | } else { |
| 2957 | xmlFreeURI(check); |
| 2958 | } |
| 2959 | base = xmlNodeGetBase(node->doc, node); |
| 2960 | if (base == NULL) { |
| 2961 | URI = xmlBuildURI(schemaLocation, node->doc->URL); |
| 2962 | } else { |
| 2963 | URI = xmlBuildURI(schemaLocation, base); |
| 2964 | xmlFree(base); |
| 2965 | } |
| 2966 | if (URI != NULL) { |
| 2967 | schemaLocation = xmlDictLookup(ctxt->dict, URI, -1); |
| 2968 | xmlFree(URI); |
| 2969 | } |
| 2970 | } else { |
| 2971 | xmlSchemaPErr2(ctxt, node, child, |
| 2972 | XML_SCHEMAP_INCLUDE_SCHEMA_NO_URI, |
| 2973 | "Include schemaLocation attribute missing\n", |
| 2974 | NULL, NULL); |
| 2975 | return (-1); |
| 2976 | } |
| 2977 | |
| 2978 | child = node->children; |
| 2979 | while (IS_SCHEMA(child, "annotation")) { |
| 2980 | /* |
| 2981 | * the annotations here are simply discarded ... |
| 2982 | */ |
| 2983 | child = child->next; |
| 2984 | } |
| 2985 | if (child != NULL) { |
| 2986 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_INCLUDE_CHILD, |
| 2987 | "Include has unexpected content\n", NULL, NULL); |
| 2988 | return (-1); |
| 2989 | } |
| 2990 | |
| 2991 | /* |
| 2992 | * First step is to parse the input document into an DOM/Infoset |
| 2993 | */ |
| 2994 | doc = xmlReadFile((const char *) schemaLocation, NULL, |
| 2995 | SCHEMAS_PARSE_OPTIONS); |
| 2996 | if (doc == NULL) { |
| 2997 | xmlSchemaPErr(ctxt, NULL, |
| 2998 | XML_SCHEMAP_FAILED_LOAD, |
| 2999 | "xmlSchemaParse: could not load %s\n", |
| 3000 | ctxt->URL, NULL); |
| 3001 | return(-1); |
| 3002 | } |
| 3003 | |
| 3004 | /* |
| 3005 | * Then extract the root of the schema |
| 3006 | */ |
| 3007 | root = xmlDocGetRootElement(doc); |
| 3008 | if (root == NULL) { |
| 3009 | xmlSchemaPErr(ctxt, (xmlNodePtr) doc, |
| 3010 | XML_SCHEMAP_NOROOT, |
| 3011 | "schemas %s has no root", schemaLocation, NULL); |
| 3012 | xmlFreeDoc(doc); |
| 3013 | return (-1); |
| 3014 | } |
| 3015 | |
| 3016 | /* |
| 3017 | * Remove all the blank text nodes |
| 3018 | */ |
| 3019 | xmlSchemaCleanupDoc(ctxt, root); |
| 3020 | |
| 3021 | /* |
| 3022 | * Check the schemas top level element |
| 3023 | */ |
| 3024 | if (!IS_SCHEMA(root, "schema")) { |
| 3025 | xmlSchemaPErr(ctxt, (xmlNodePtr) doc, |
| 3026 | XML_SCHEMAP_NOT_SCHEMA, |
| 3027 | "File %s is not a schemas", schemaLocation, NULL); |
| 3028 | xmlFreeDoc(doc); |
| 3029 | return (-1); |
| 3030 | } |
| 3031 | |
| 3032 | /* |
| 3033 | * register the include |
| 3034 | */ |
| 3035 | include = (xmlSchemaIncludePtr) xmlMalloc(sizeof(xmlSchemaInclude)); |
| 3036 | if (include == NULL) { |
| 3037 | xmlSchemaPErrMemory(ctxt, "allocating included schema", NULL); |
| 3038 | xmlFreeDoc(doc); |
| 3039 | return (-1); |
| 3040 | } |
| 3041 | |
| 3042 | memset(include, 0, sizeof(xmlSchemaInclude)); |
| 3043 | include->schemaLocation = xmlDictLookup(ctxt->dict, schemaLocation, -1); |
| 3044 | include->doc = doc; |
| 3045 | include->next = schema->includes; |
| 3046 | schema->includes = include; |
| 3047 | |
| 3048 | |
| 3049 | /* |
| 3050 | * parse the declarations in the included file like if they |
| 3051 | * were in the original file. |
| 3052 | */ |
| 3053 | xmlSchemaParseSchemaTopLevel(ctxt, schema, root->children); |
| 3054 | |
| 3055 | return (1); |
| 3056 | } |
| 3057 | |
| 3058 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3059 | * xmlSchemaParseChoice: |
| 3060 | * @ctxt: a schema validation context |
| 3061 | * @schema: the schema being built |
| 3062 | * @node: a subtree containing XML Schema informations |
| 3063 | * |
| 3064 | * parse a XML schema Choice definition |
| 3065 | * *WARNING* this interface is highly subject to change |
| 3066 | * |
| 3067 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 3068 | * 1 in case of success. |
| 3069 | */ |
| 3070 | static xmlSchemaTypePtr |
| 3071 | xmlSchemaParseChoice(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3072 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3073 | { |
| 3074 | xmlSchemaTypePtr type, subtype, last = NULL; |
| 3075 | xmlNodePtr child = NULL; |
| 3076 | xmlChar name[30]; |
| 3077 | |
| 3078 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3079 | return (NULL); |
| 3080 | |
| 3081 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3082 | snprintf((char *) name, 30, "choice %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3083 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3084 | if (type == NULL) |
| 3085 | return (NULL); |
| 3086 | type->node = node; |
| 3087 | type->type = XML_SCHEMA_TYPE_CHOICE; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3088 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3089 | type->minOccurs = xmlGetMinOccurs(ctxt, node); |
| 3090 | type->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
| 3091 | |
| 3092 | child = node->children; |
| 3093 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3094 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3095 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3096 | } |
| 3097 | while ((IS_SCHEMA(child, "element")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3098 | (IS_SCHEMA(child, "group")) || |
| 3099 | (IS_SCHEMA(child, "any")) || |
| 3100 | (IS_SCHEMA(child, "choice")) || |
| 3101 | (IS_SCHEMA(child, "sequence"))) { |
| 3102 | subtype = NULL; |
| 3103 | if (IS_SCHEMA(child, "element")) { |
| 3104 | subtype = (xmlSchemaTypePtr) |
| 3105 | xmlSchemaParseElement(ctxt, schema, child, 0); |
| 3106 | } else if (IS_SCHEMA(child, "group")) { |
| 3107 | subtype = xmlSchemaParseGroup(ctxt, schema, child); |
| 3108 | } else if (IS_SCHEMA(child, "any")) { |
| 3109 | subtype = xmlSchemaParseAny(ctxt, schema, child); |
| 3110 | } else if (IS_SCHEMA(child, "sequence")) { |
| 3111 | subtype = xmlSchemaParseSequence(ctxt, schema, child); |
| 3112 | } else if (IS_SCHEMA(child, "choice")) { |
| 3113 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 3114 | } |
| 3115 | if (subtype != NULL) { |
| 3116 | if (last == NULL) { |
| 3117 | type->subtypes = subtype; |
| 3118 | last = subtype; |
| 3119 | } else { |
| 3120 | last->next = subtype; |
| 3121 | last = subtype; |
| 3122 | } |
| 3123 | last->next = NULL; |
| 3124 | } |
| 3125 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3126 | } |
| 3127 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3128 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_CHOICE_CHILD, |
| 3129 | "Choice %s has unexpected content\n", type->name, |
| 3130 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
| 3133 | return (type); |
| 3134 | } |
| 3135 | |
| 3136 | /** |
| 3137 | * xmlSchemaParseSequence: |
| 3138 | * @ctxt: a schema validation context |
| 3139 | * @schema: the schema being built |
| 3140 | * @node: a subtree containing XML Schema informations |
| 3141 | * |
| 3142 | * parse a XML schema Sequence definition |
| 3143 | * *WARNING* this interface is highly subject to change |
| 3144 | * |
| 3145 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 3146 | * 1 in case of success. |
| 3147 | */ |
| 3148 | static xmlSchemaTypePtr |
| 3149 | xmlSchemaParseSequence(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3150 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3151 | { |
| 3152 | xmlSchemaTypePtr type, subtype, last = NULL; |
| 3153 | xmlNodePtr child = NULL; |
| 3154 | xmlChar name[30]; |
| 3155 | |
| 3156 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3157 | return (NULL); |
| 3158 | |
| 3159 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3160 | snprintf((char *) name, 30, "sequence %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3161 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3162 | if (type == NULL) |
| 3163 | return (NULL); |
| 3164 | type->node = node; |
| 3165 | type->type = XML_SCHEMA_TYPE_SEQUENCE; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3166 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3167 | type->minOccurs = xmlGetMinOccurs(ctxt, node); |
| 3168 | type->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
| 3169 | |
| 3170 | child = node->children; |
| 3171 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3172 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3173 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3174 | } |
| 3175 | while ((IS_SCHEMA(child, "element")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3176 | (IS_SCHEMA(child, "group")) || |
| 3177 | (IS_SCHEMA(child, "any")) || |
| 3178 | (IS_SCHEMA(child, "choice")) || |
| 3179 | (IS_SCHEMA(child, "sequence"))) { |
| 3180 | subtype = NULL; |
| 3181 | if (IS_SCHEMA(child, "element")) { |
| 3182 | subtype = (xmlSchemaTypePtr) |
| 3183 | xmlSchemaParseElement(ctxt, schema, child, 0); |
| 3184 | } else if (IS_SCHEMA(child, "group")) { |
| 3185 | subtype = xmlSchemaParseGroup(ctxt, schema, child); |
| 3186 | } else if (IS_SCHEMA(child, "any")) { |
| 3187 | subtype = xmlSchemaParseAny(ctxt, schema, child); |
| 3188 | } else if (IS_SCHEMA(child, "choice")) { |
| 3189 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 3190 | } else if (IS_SCHEMA(child, "sequence")) { |
| 3191 | subtype = xmlSchemaParseSequence(ctxt, schema, child); |
| 3192 | } |
| 3193 | if (subtype != NULL) { |
| 3194 | if (last == NULL) { |
| 3195 | type->subtypes = subtype; |
| 3196 | last = subtype; |
| 3197 | } else { |
| 3198 | last->next = subtype; |
| 3199 | last = subtype; |
| 3200 | } |
| 3201 | last->next = NULL; |
| 3202 | } |
| 3203 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3204 | } |
| 3205 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3206 | xmlSchemaPErr2(ctxt, node, child, |
| 3207 | XML_SCHEMAP_UNKNOWN_SEQUENCE_CHILD, |
| 3208 | "Sequence %s has unexpected content\n", type->name, |
| 3209 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3210 | } |
| 3211 | |
| 3212 | return (type); |
| 3213 | } |
| 3214 | |
| 3215 | /** |
| 3216 | * xmlSchemaParseRestriction: |
| 3217 | * @ctxt: a schema validation context |
| 3218 | * @schema: the schema being built |
| 3219 | * @node: a subtree containing XML Schema informations |
| 3220 | * @simple: is that part of a simple type. |
| 3221 | * |
| 3222 | * parse a XML schema Restriction definition |
| 3223 | * *WARNING* this interface is highly subject to change |
| 3224 | * |
| 3225 | * Returns the type definition or NULL in case of error |
| 3226 | */ |
| 3227 | static xmlSchemaTypePtr |
| 3228 | xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 3229 | xmlNodePtr node, int simple) |
| 3230 | { |
| 3231 | xmlSchemaTypePtr type, subtype; |
| 3232 | xmlSchemaFacetPtr facet, lastfacet = NULL; |
| 3233 | xmlNodePtr child = NULL; |
| 3234 | xmlChar name[30]; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3235 | const xmlChar *oldcontainer; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3236 | |
| 3237 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3238 | return (NULL); |
| 3239 | |
| 3240 | oldcontainer = ctxt->container; |
| 3241 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3242 | snprintf((char *) name, 30, "restriction %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3243 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3244 | if (type == NULL) |
| 3245 | return (NULL); |
| 3246 | type->node = node; |
| 3247 | type->type = XML_SCHEMA_TYPE_RESTRICTION; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3248 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3249 | type->base = xmlGetQNameProp(ctxt, node, "base", &(type->baseNs)); |
| 3250 | if ((!simple) && (type->base == NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3251 | xmlSchemaPErr2(ctxt, node, child, |
| 3252 | XML_SCHEMAP_RESTRICTION_NONAME_NOREF, |
| 3253 | "Restriction %s has no base\n", type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3254 | } |
| 3255 | ctxt->container = name; |
| 3256 | |
| 3257 | child = node->children; |
| 3258 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3259 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3260 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3261 | } |
| 3262 | subtype = NULL; |
| 3263 | |
| 3264 | if (IS_SCHEMA(child, "all")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3265 | subtype = (xmlSchemaTypePtr) |
| 3266 | xmlSchemaParseAll(ctxt, schema, child); |
| 3267 | child = child->next; |
| 3268 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3269 | } else if (IS_SCHEMA(child, "choice")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3270 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 3271 | child = child->next; |
| 3272 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3273 | } else if (IS_SCHEMA(child, "sequence")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3274 | subtype = (xmlSchemaTypePtr) |
| 3275 | xmlSchemaParseSequence(ctxt, schema, child); |
| 3276 | child = child->next; |
| 3277 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3278 | } else if (IS_SCHEMA(child, "group")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3279 | subtype = (xmlSchemaTypePtr) |
| 3280 | xmlSchemaParseGroup(ctxt, schema, child); |
| 3281 | child = child->next; |
| 3282 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3283 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3284 | if (IS_SCHEMA(child, "simpleType")) { |
| 3285 | subtype = (xmlSchemaTypePtr) |
| 3286 | xmlSchemaParseSimpleType(ctxt, schema, child); |
| 3287 | child = child->next; |
| 3288 | type->baseType = subtype; |
| 3289 | } |
| 3290 | /* |
| 3291 | * Facets |
| 3292 | */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3293 | while ((IS_SCHEMA(child, "minInclusive")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3294 | (IS_SCHEMA(child, "minExclusive")) || |
| 3295 | (IS_SCHEMA(child, "maxInclusive")) || |
| 3296 | (IS_SCHEMA(child, "maxExclusive")) || |
| 3297 | (IS_SCHEMA(child, "totalDigits")) || |
| 3298 | (IS_SCHEMA(child, "fractionDigits")) || |
| 3299 | (IS_SCHEMA(child, "pattern")) || |
| 3300 | (IS_SCHEMA(child, "enumeration")) || |
| 3301 | (IS_SCHEMA(child, "whiteSpace")) || |
| 3302 | (IS_SCHEMA(child, "length")) || |
| 3303 | (IS_SCHEMA(child, "maxLength")) || |
| 3304 | (IS_SCHEMA(child, "minLength"))) { |
| 3305 | facet = xmlSchemaParseFacet(ctxt, schema, child); |
| 3306 | if (facet != NULL) { |
| 3307 | if (lastfacet == NULL) { |
| 3308 | type->facets = facet; |
| 3309 | lastfacet = facet; |
| 3310 | } else { |
| 3311 | lastfacet->next = facet; |
| 3312 | lastfacet = facet; |
| 3313 | } |
| 3314 | lastfacet->next = NULL; |
| 3315 | } |
| 3316 | child = child->next; |
| 3317 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3318 | } |
| 3319 | child = xmlSchemaParseAttrDecls(ctxt, schema, child, type); |
| 3320 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3321 | xmlSchemaPErr2(ctxt, node, child, |
| 3322 | XML_SCHEMAP_UNKNOWN_RESTRICTION_CHILD, |
| 3323 | "Restriction %s has unexpected content\n", |
| 3324 | type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3325 | } |
| 3326 | ctxt->container = oldcontainer; |
| 3327 | return (type); |
| 3328 | } |
| 3329 | |
| 3330 | /** |
| 3331 | * xmlSchemaParseExtension: |
| 3332 | * @ctxt: a schema validation context |
| 3333 | * @schema: the schema being built |
| 3334 | * @node: a subtree containing XML Schema informations |
| 3335 | * |
| 3336 | * parse a XML schema Extension definition |
| 3337 | * *WARNING* this interface is highly subject to change |
| 3338 | * |
| 3339 | * Returns the type definition or NULL in case of error |
| 3340 | */ |
| 3341 | static xmlSchemaTypePtr |
| 3342 | xmlSchemaParseExtension(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3343 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3344 | { |
| 3345 | xmlSchemaTypePtr type, subtype; |
| 3346 | xmlNodePtr child = NULL; |
| 3347 | xmlChar name[30]; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3348 | const xmlChar *oldcontainer; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3349 | |
| 3350 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3351 | return (NULL); |
| 3352 | |
| 3353 | oldcontainer = ctxt->container; |
| 3354 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3355 | snprintf((char *) name, 30, "extension %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3356 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3357 | if (type == NULL) |
| 3358 | return (NULL); |
| 3359 | type->node = node; |
| 3360 | type->type = XML_SCHEMA_TYPE_EXTENSION; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3361 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3362 | ctxt->container = name; |
| 3363 | |
| 3364 | type->base = xmlGetQNameProp(ctxt, node, "base", &(type->baseNs)); |
| 3365 | if (type->base == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3366 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_EXTENSION_NO_BASE, |
| 3367 | "Extension %s has no base\n", type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3368 | } |
| 3369 | child = node->children; |
| 3370 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3371 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3372 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3373 | } |
| 3374 | subtype = NULL; |
| 3375 | |
| 3376 | if (IS_SCHEMA(child, "all")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3377 | subtype = xmlSchemaParseAll(ctxt, schema, child); |
| 3378 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3379 | } else if (IS_SCHEMA(child, "choice")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3380 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 3381 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3382 | } else if (IS_SCHEMA(child, "sequence")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3383 | subtype = xmlSchemaParseSequence(ctxt, schema, child); |
| 3384 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3385 | } else if (IS_SCHEMA(child, "group")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3386 | subtype = xmlSchemaParseGroup(ctxt, schema, child); |
| 3387 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3388 | } |
| 3389 | if (subtype != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3390 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3391 | child = xmlSchemaParseAttrDecls(ctxt, schema, child, type); |
| 3392 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3393 | xmlSchemaPErr2(ctxt, node, child, |
| 3394 | XML_SCHEMAP_UNKNOWN_EXTENSION_CHILD, |
| 3395 | "Extension %s has unexpected content\n", type->name, |
| 3396 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3397 | } |
| 3398 | ctxt->container = oldcontainer; |
| 3399 | return (type); |
| 3400 | } |
| 3401 | |
| 3402 | /** |
| 3403 | * xmlSchemaParseSimpleContent: |
| 3404 | * @ctxt: a schema validation context |
| 3405 | * @schema: the schema being built |
| 3406 | * @node: a subtree containing XML Schema informations |
| 3407 | * |
| 3408 | * parse a XML schema SimpleContent definition |
| 3409 | * *WARNING* this interface is highly subject to change |
| 3410 | * |
| 3411 | * Returns the type definition or NULL in case of error |
| 3412 | */ |
| 3413 | static xmlSchemaTypePtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3414 | xmlSchemaParseSimpleContent(xmlSchemaParserCtxtPtr ctxt, |
| 3415 | xmlSchemaPtr schema, xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3416 | { |
| 3417 | xmlSchemaTypePtr type, subtype; |
| 3418 | xmlNodePtr child = NULL; |
| 3419 | xmlChar name[30]; |
| 3420 | |
| 3421 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3422 | return (NULL); |
| 3423 | |
| 3424 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3425 | snprintf((char *) name, 30, "complexContent %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3426 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3427 | if (type == NULL) |
| 3428 | return (NULL); |
| 3429 | type->node = node; |
| 3430 | type->type = XML_SCHEMA_TYPE_SIMPLE_CONTENT; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3431 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3432 | |
| 3433 | child = node->children; |
| 3434 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3435 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3436 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3437 | } |
| 3438 | subtype = NULL; |
| 3439 | if (IS_SCHEMA(child, "restriction")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3440 | subtype = (xmlSchemaTypePtr) |
| 3441 | xmlSchemaParseRestriction(ctxt, schema, child, 0); |
| 3442 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3443 | } else if (IS_SCHEMA(child, "extension")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3444 | subtype = (xmlSchemaTypePtr) |
| 3445 | xmlSchemaParseExtension(ctxt, schema, child); |
| 3446 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3447 | } |
| 3448 | type->subtypes = subtype; |
| 3449 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3450 | xmlSchemaPErr2(ctxt, node, child, |
| 3451 | XML_SCHEMAP_UNKNOWN_SIMPLECONTENT_CHILD, |
| 3452 | "SimpleContent %s has unexpected content\n", |
| 3453 | type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3454 | } |
| 3455 | return (type); |
| 3456 | } |
| 3457 | |
| 3458 | /** |
| 3459 | * xmlSchemaParseComplexContent: |
| 3460 | * @ctxt: a schema validation context |
| 3461 | * @schema: the schema being built |
| 3462 | * @node: a subtree containing XML Schema informations |
| 3463 | * |
| 3464 | * parse a XML schema ComplexContent definition |
| 3465 | * *WARNING* this interface is highly subject to change |
| 3466 | * |
| 3467 | * Returns the type definition or NULL in case of error |
| 3468 | */ |
| 3469 | static xmlSchemaTypePtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3470 | xmlSchemaParseComplexContent(xmlSchemaParserCtxtPtr ctxt, |
| 3471 | xmlSchemaPtr schema, xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3472 | { |
| 3473 | xmlSchemaTypePtr type, subtype; |
| 3474 | xmlNodePtr child = NULL; |
| 3475 | xmlChar name[30]; |
| 3476 | |
| 3477 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3478 | return (NULL); |
| 3479 | |
| 3480 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3481 | snprintf((char *) name, 30, "complexContent %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3482 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3483 | if (type == NULL) |
| 3484 | return (NULL); |
| 3485 | type->node = node; |
| 3486 | type->type = XML_SCHEMA_TYPE_COMPLEX_CONTENT; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3487 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3488 | |
| 3489 | child = node->children; |
| 3490 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3491 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3492 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3493 | } |
| 3494 | subtype = NULL; |
| 3495 | if (IS_SCHEMA(child, "restriction")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3496 | subtype = (xmlSchemaTypePtr) |
| 3497 | xmlSchemaParseRestriction(ctxt, schema, child, 0); |
| 3498 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3499 | } else if (IS_SCHEMA(child, "extension")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3500 | subtype = (xmlSchemaTypePtr) |
| 3501 | xmlSchemaParseExtension(ctxt, schema, child); |
| 3502 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3503 | } |
| 3504 | type->subtypes = subtype; |
| 3505 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3506 | xmlSchemaPErr2(ctxt, node, child, |
| 3507 | XML_SCHEMAP_UNKNOWN_COMPLEXCONTENT_CHILD, |
| 3508 | "ComplexContent %s has unexpected content\n", |
| 3509 | type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3510 | } |
| 3511 | return (type); |
| 3512 | } |
| 3513 | |
| 3514 | /** |
| 3515 | * xmlSchemaParseComplexType: |
| 3516 | * @ctxt: a schema validation context |
| 3517 | * @schema: the schema being built |
| 3518 | * @node: a subtree containing XML Schema informations |
| 3519 | * |
| 3520 | * parse a XML schema Complex Type definition |
| 3521 | * *WARNING* this interface is highly subject to change |
| 3522 | * |
| 3523 | * Returns the type definition or NULL in case of error |
| 3524 | */ |
| 3525 | static xmlSchemaTypePtr |
| 3526 | xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 3527 | xmlNodePtr node) |
| 3528 | { |
| 3529 | xmlSchemaTypePtr type, subtype; |
| 3530 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3531 | const xmlChar *name; |
| 3532 | const xmlChar *oldcontainer; |
| 3533 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3534 | |
| 3535 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3536 | return (NULL); |
| 3537 | |
| 3538 | oldcontainer = ctxt->container; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3539 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3540 | if (name == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3541 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3542 | snprintf(buf, 99, "anontype %d", ctxt->counter++ + 1); |
| 3543 | name = (const xmlChar *)buf; |
| 3544 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
| 3545 | } else { |
| 3546 | const xmlChar *local, *ns; |
| 3547 | |
| 3548 | local = xmlSchemaGetNamespace(ctxt, schema, node, name, &ns); |
| 3549 | type = xmlSchemaAddType(ctxt, schema, local, ns); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3550 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3551 | if (type == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3552 | return (NULL); |
| 3553 | } |
| 3554 | type->node = node; |
| 3555 | type->type = XML_SCHEMA_TYPE_COMPLEX; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3556 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3557 | ctxt->container = name; |
| 3558 | |
| 3559 | child = node->children; |
| 3560 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3561 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3562 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3563 | } |
| 3564 | if (IS_SCHEMA(child, "simpleContent")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3565 | type->subtypes = xmlSchemaParseSimpleContent(ctxt, schema, child); |
| 3566 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3567 | } else if (IS_SCHEMA(child, "complexContent")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3568 | type->subtypes = xmlSchemaParseComplexContent(ctxt, schema, child); |
| 3569 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3570 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3571 | subtype = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3572 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3573 | if (IS_SCHEMA(child, "all")) { |
| 3574 | subtype = xmlSchemaParseAll(ctxt, schema, child); |
| 3575 | child = child->next; |
| 3576 | } else if (IS_SCHEMA(child, "choice")) { |
| 3577 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 3578 | child = child->next; |
| 3579 | } else if (IS_SCHEMA(child, "sequence")) { |
| 3580 | subtype = xmlSchemaParseSequence(ctxt, schema, child); |
| 3581 | child = child->next; |
| 3582 | } else if (IS_SCHEMA(child, "group")) { |
| 3583 | subtype = xmlSchemaParseGroup(ctxt, schema, child); |
| 3584 | child = child->next; |
| 3585 | } |
| 3586 | if (subtype != NULL) |
| 3587 | type->subtypes = subtype; |
| 3588 | child = xmlSchemaParseAttrDecls(ctxt, schema, child, type); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3589 | } |
| 3590 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3591 | xmlSchemaPErr2(ctxt, node, child, |
| 3592 | XML_SCHEMAP_UNKNOWN_COMPLEXTYPE_CHILD, |
| 3593 | "ComplexType %s has unexpected content\n", |
| 3594 | type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3595 | } |
| 3596 | ctxt->container = oldcontainer; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3597 | return (type); |
| 3598 | } |
| 3599 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3600 | /** |
| 3601 | * xmlSchemaParseSchema: |
| 3602 | * @ctxt: a schema validation context |
| 3603 | * @node: a subtree containing XML Schema informations |
| 3604 | * |
| 3605 | * parse a XML schema definition from a node set |
| 3606 | * *WARNING* this interface is highly subject to change |
| 3607 | * |
| 3608 | * Returns the internal XML Schema structure built from the resource or |
| 3609 | * NULL in case of error |
| 3610 | */ |
| 3611 | static xmlSchemaPtr |
| 3612 | xmlSchemaParseSchema(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node) |
| 3613 | { |
| 3614 | xmlSchemaPtr schema = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3615 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3616 | const xmlChar *val; |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 3617 | int nberrors; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3618 | |
| 3619 | if ((ctxt == NULL) || (node == NULL)) |
| 3620 | return (NULL); |
| 3621 | |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 3622 | nberrors = ctxt->nberrors; |
| 3623 | ctxt->nberrors = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3624 | if (IS_SCHEMA(node, "schema")) { |
| 3625 | schema = xmlSchemaNewSchema(ctxt); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3626 | if (schema == NULL) |
| 3627 | return (NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3628 | val = xmlSchemaGetProp(ctxt, node, "targetNamespace"); |
| 3629 | if (val != NULL) { |
| 3630 | schema->targetNamespace = xmlDictLookup(ctxt->dict, val, -1); |
| 3631 | } else { |
| 3632 | schema->targetNamespace = NULL; |
| 3633 | } |
| 3634 | schema->id = xmlSchemaGetProp(ctxt, node, "id"); |
| 3635 | schema->version = xmlSchemaGetProp(ctxt, node, "version"); |
| 3636 | val = xmlSchemaGetProp(ctxt, node, "elementFormDefault"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3637 | if (val != NULL) { |
| 3638 | if (xmlStrEqual(val, BAD_CAST "qualified")) |
| 3639 | schema->flags |= XML_SCHEMAS_QUALIF_ELEM; |
| 3640 | else if (!xmlStrEqual(val, BAD_CAST "unqualified")) { |
| 3641 | xmlSchemaPErr2(ctxt, node, child, |
| 3642 | XML_SCHEMAP_ELEMFORMDEFAULT_VALUE, |
| 3643 | "Invalid value %s for elementFormDefault\n", |
| 3644 | val, NULL); |
| 3645 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3646 | } else { |
| 3647 | schema->flags |= XML_SCHEMAS_QUALIF_ELEM; |
| 3648 | } |
| 3649 | val = xmlSchemaGetProp(ctxt, node, "attributeFormDefault"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3650 | if (val != NULL) { |
| 3651 | if (xmlStrEqual(val, BAD_CAST "qualified")) |
| 3652 | schema->flags |= XML_SCHEMAS_QUALIF_ATTR; |
| 3653 | else if (!xmlStrEqual(val, BAD_CAST "unqualified")) { |
| 3654 | xmlSchemaPErr2(ctxt, node, child, |
| 3655 | XML_SCHEMAP_ATTRFORMDEFAULT_VALUE, |
| 3656 | "Invalid value %s for attributeFormDefault\n", |
| 3657 | val, NULL); |
| 3658 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3659 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3660 | |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 3661 | xmlSchemaParseSchemaTopLevel(ctxt, schema, node->children); |
| 3662 | } else { |
| 3663 | xmlDocPtr doc; |
| 3664 | |
| 3665 | doc = node->doc; |
| 3666 | |
| 3667 | if ((doc != NULL) && (doc->URL != NULL)) { |
| 3668 | xmlSchemaPErr(ctxt, (xmlNodePtr) doc, |
| 3669 | XML_SCHEMAP_NOT_SCHEMA, |
| 3670 | "File %s is not a schemas", doc->URL, NULL); |
| 3671 | } else { |
| 3672 | xmlSchemaPErr(ctxt, (xmlNodePtr) doc, |
| 3673 | XML_SCHEMAP_NOT_SCHEMA, |
| 3674 | "File is not a schemas", NULL, NULL); |
| 3675 | } |
| 3676 | return(NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3677 | } |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 3678 | if (ctxt->nberrors != 0) { |
| 3679 | if (schema != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3680 | xmlSchemaFree(schema); |
| 3681 | schema = NULL; |
| 3682 | } |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 3683 | } |
| 3684 | ctxt->nberrors = nberrors; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3685 | #ifdef DEBUG |
| 3686 | if (schema == NULL) |
| 3687 | xmlGenericError(xmlGenericErrorContext, |
| 3688 | "xmlSchemaParse() failed\n"); |
| 3689 | #endif |
| 3690 | |
| 3691 | return (schema); |
| 3692 | } |
| 3693 | |
| 3694 | /************************************************************************ |
| 3695 | * * |
| 3696 | * Validating using Schemas * |
| 3697 | * * |
| 3698 | ************************************************************************/ |
| 3699 | |
| 3700 | /************************************************************************ |
| 3701 | * * |
| 3702 | * Reading/Writing Schemas * |
| 3703 | * * |
| 3704 | ************************************************************************/ |
| 3705 | |
| 3706 | /** |
| 3707 | * xmlSchemaNewParserCtxt: |
| 3708 | * @URL: the location of the schema |
| 3709 | * |
| 3710 | * Create an XML Schemas parse context for that file/resource expected |
| 3711 | * to contain an XML Schemas file. |
| 3712 | * |
| 3713 | * Returns the parser context or NULL in case of error |
| 3714 | */ |
| 3715 | xmlSchemaParserCtxtPtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3716 | xmlSchemaNewParserCtxt(const char *URL) |
| 3717 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3718 | xmlSchemaParserCtxtPtr ret; |
| 3719 | |
| 3720 | if (URL == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3721 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3722 | |
| 3723 | ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt)); |
| 3724 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3725 | xmlSchemaPErrMemory(NULL, "allocating schama parser context", |
| 3726 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3727 | return (NULL); |
| 3728 | } |
| 3729 | memset(ret, 0, sizeof(xmlSchemaParserCtxt)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3730 | ret->dict = xmlDictCreate(); |
| 3731 | ret->URL = xmlDictLookup(ret->dict, (const xmlChar *) URL, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3732 | return (ret); |
| 3733 | } |
| 3734 | |
| 3735 | /** |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 3736 | * xmlSchemaNewMemParserCtxt: |
| 3737 | * @buffer: a pointer to a char array containing the schemas |
| 3738 | * @size: the size of the array |
| 3739 | * |
| 3740 | * Create an XML Schemas parse context for that memory buffer expected |
| 3741 | * to contain an XML Schemas file. |
| 3742 | * |
| 3743 | * Returns the parser context or NULL in case of error |
| 3744 | */ |
| 3745 | xmlSchemaParserCtxtPtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3746 | xmlSchemaNewMemParserCtxt(const char *buffer, int size) |
| 3747 | { |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 3748 | xmlSchemaParserCtxtPtr ret; |
| 3749 | |
| 3750 | if ((buffer == NULL) || (size <= 0)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3751 | return (NULL); |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 3752 | |
| 3753 | ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt)); |
| 3754 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3755 | xmlSchemaPErrMemory(NULL, "allocating schama parser context", |
| 3756 | NULL); |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 3757 | return (NULL); |
| 3758 | } |
| 3759 | memset(ret, 0, sizeof(xmlSchemaParserCtxt)); |
| 3760 | ret->buffer = buffer; |
| 3761 | ret->size = size; |
| 3762 | return (ret); |
| 3763 | } |
| 3764 | |
| 3765 | /** |
Daniel Veillard | 9d75150 | 2003-10-29 13:21:47 +0000 | [diff] [blame] | 3766 | * xmlSchemaNewDocParserCtxt: |
| 3767 | * @doc: a preparsed document tree |
| 3768 | * |
| 3769 | * Create an XML Schemas parse context for that document. |
| 3770 | * NB. The document may be modified during the parsing process. |
| 3771 | * |
| 3772 | * Returns the parser context or NULL in case of error |
| 3773 | */ |
| 3774 | xmlSchemaParserCtxtPtr |
| 3775 | xmlSchemaNewDocParserCtxt(xmlDocPtr doc) |
| 3776 | { |
| 3777 | xmlSchemaParserCtxtPtr ret; |
| 3778 | |
| 3779 | if (doc == NULL) |
| 3780 | return (NULL); |
| 3781 | |
| 3782 | ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt)); |
| 3783 | if (ret == NULL) { |
| 3784 | xmlSchemaPErrMemory(NULL, "allocating schema parser context", |
| 3785 | NULL); |
| 3786 | return (NULL); |
| 3787 | } |
| 3788 | memset(ret, 0, sizeof(xmlSchemaParserCtxt)); |
| 3789 | ret->doc = doc; |
| 3790 | |
| 3791 | return (ret); |
| 3792 | } |
| 3793 | |
| 3794 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3795 | * xmlSchemaFreeParserCtxt: |
| 3796 | * @ctxt: the schema parser context |
| 3797 | * |
| 3798 | * Free the resources associated to the schema parser context |
| 3799 | */ |
| 3800 | void |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3801 | xmlSchemaFreeParserCtxt(xmlSchemaParserCtxtPtr ctxt) |
| 3802 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3803 | if (ctxt == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3804 | return; |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 3805 | if (ctxt->doc != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3806 | xmlFreeDoc(ctxt->doc); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3807 | xmlDictFree(ctxt->dict); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3808 | xmlFree(ctxt); |
| 3809 | } |
| 3810 | |
| 3811 | /************************************************************************ |
| 3812 | * * |
| 3813 | * Building the content models * |
| 3814 | * * |
| 3815 | ************************************************************************/ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3816 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3817 | /** |
| 3818 | * xmlSchemaBuildAContentModel: |
| 3819 | * @type: the schema type definition |
| 3820 | * @ctxt: the schema parser context |
| 3821 | * @name: the element name whose content is being built |
| 3822 | * |
| 3823 | * Generate the automata sequence needed for that type |
| 3824 | */ |
| 3825 | static void |
| 3826 | xmlSchemaBuildAContentModel(xmlSchemaTypePtr type, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3827 | xmlSchemaParserCtxtPtr ctxt, |
| 3828 | const xmlChar * name) |
| 3829 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3830 | if (type == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3831 | xmlGenericError(xmlGenericErrorContext, |
| 3832 | "Found unexpected type = NULL in %s content model\n", |
| 3833 | name); |
| 3834 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3835 | } |
| 3836 | switch (type->type) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3837 | case XML_SCHEMA_TYPE_ANY: |
| 3838 | /* TODO : handle the namespace too */ |
| 3839 | /* TODO : make that a specific transition type */ |
| 3840 | TODO ctxt->state = |
| 3841 | xmlAutomataNewTransition(ctxt->am, ctxt->state, NULL, |
| 3842 | BAD_CAST "*", NULL); |
| 3843 | break; |
| 3844 | case XML_SCHEMA_TYPE_ELEMENT:{ |
| 3845 | xmlSchemaElementPtr elem = (xmlSchemaElementPtr) type; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3846 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3847 | /* TODO : handle the namespace too */ |
| 3848 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3849 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3850 | if (elem->maxOccurs >= UNBOUNDED) { |
| 3851 | if (elem->minOccurs > 1) { |
| 3852 | xmlAutomataStatePtr tmp; |
| 3853 | int counter; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3854 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3855 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
| 3856 | oldstate, |
| 3857 | NULL); |
| 3858 | oldstate = ctxt->state; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3859 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3860 | counter = xmlAutomataNewCounter(ctxt->am, |
| 3861 | elem->minOccurs - |
| 3862 | 1, UNBOUNDED); |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3863 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3864 | if (elem->refDecl != NULL) { |
| 3865 | xmlSchemaBuildAContentModel((xmlSchemaTypePtr) |
| 3866 | elem->refDecl, |
| 3867 | ctxt, |
| 3868 | elem->refDecl-> |
| 3869 | name); |
| 3870 | } else { |
| 3871 | ctxt->state = |
| 3872 | xmlAutomataNewTransition(ctxt->am, |
| 3873 | ctxt->state, NULL, |
| 3874 | elem->name, type); |
| 3875 | } |
| 3876 | tmp = ctxt->state; |
| 3877 | xmlAutomataNewCountedTrans(ctxt->am, tmp, oldstate, |
| 3878 | counter); |
| 3879 | ctxt->state = |
| 3880 | xmlAutomataNewCounterTrans(ctxt->am, tmp, NULL, |
| 3881 | counter); |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3882 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3883 | } else { |
| 3884 | if (elem->refDecl != NULL) { |
| 3885 | xmlSchemaBuildAContentModel((xmlSchemaTypePtr) |
| 3886 | elem->refDecl, |
| 3887 | ctxt, |
| 3888 | elem->refDecl-> |
| 3889 | name); |
| 3890 | } else { |
| 3891 | ctxt->state = |
| 3892 | xmlAutomataNewTransition(ctxt->am, |
| 3893 | ctxt->state, NULL, |
| 3894 | elem->name, type); |
| 3895 | } |
| 3896 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
| 3897 | oldstate); |
| 3898 | if (elem->minOccurs == 0) { |
| 3899 | /* basically an elem* */ |
| 3900 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 3901 | ctxt->state); |
| 3902 | } |
| 3903 | } |
| 3904 | } else if ((elem->maxOccurs > 1) || (elem->minOccurs > 1)) { |
| 3905 | xmlAutomataStatePtr tmp; |
| 3906 | int counter; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3907 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3908 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
| 3909 | oldstate, NULL); |
| 3910 | oldstate = ctxt->state; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3911 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3912 | counter = xmlAutomataNewCounter(ctxt->am, |
| 3913 | elem->minOccurs - 1, |
| 3914 | elem->maxOccurs - 1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3915 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3916 | if (elem->refDecl != NULL) { |
| 3917 | xmlSchemaBuildAContentModel((xmlSchemaTypePtr) |
| 3918 | elem->refDecl, ctxt, |
| 3919 | elem->refDecl->name); |
| 3920 | } else { |
| 3921 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
| 3922 | ctxt->state, |
| 3923 | NULL, |
| 3924 | elem->name, |
| 3925 | type); |
| 3926 | } |
| 3927 | tmp = ctxt->state; |
| 3928 | xmlAutomataNewCountedTrans(ctxt->am, tmp, oldstate, |
| 3929 | counter); |
| 3930 | ctxt->state = xmlAutomataNewCounterTrans(ctxt->am, tmp, |
| 3931 | NULL, |
| 3932 | counter); |
| 3933 | if (elem->minOccurs == 0) { |
| 3934 | /* basically an elem? */ |
| 3935 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 3936 | ctxt->state); |
| 3937 | } |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +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, ctxt, |
| 3943 | elem->refDecl->name); |
| 3944 | } else { |
| 3945 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
| 3946 | ctxt->state, |
| 3947 | NULL, |
| 3948 | elem->name, |
| 3949 | type); |
| 3950 | } |
| 3951 | if (elem->minOccurs == 0) { |
| 3952 | /* basically an elem? */ |
| 3953 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 3954 | ctxt->state); |
| 3955 | } |
| 3956 | } |
| 3957 | break; |
| 3958 | } |
| 3959 | case XML_SCHEMA_TYPE_SEQUENCE:{ |
| 3960 | xmlSchemaTypePtr subtypes; |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 3961 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3962 | /* |
| 3963 | * If max and min occurances are default (1) then |
| 3964 | * simply iterate over the subtypes |
| 3965 | */ |
| 3966 | if ((type->minOccurs == 1) && (type->maxOccurs == 1)) { |
| 3967 | subtypes = type->subtypes; |
| 3968 | while (subtypes != NULL) { |
| 3969 | xmlSchemaBuildAContentModel(subtypes, ctxt, name); |
| 3970 | subtypes = subtypes->next; |
| 3971 | } |
| 3972 | } else { |
| 3973 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 3974 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3975 | if (type->maxOccurs >= UNBOUNDED) { |
| 3976 | if (type->minOccurs > 1) { |
| 3977 | xmlAutomataStatePtr tmp; |
| 3978 | int counter; |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 3979 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3980 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
| 3981 | oldstate, |
| 3982 | NULL); |
| 3983 | oldstate = ctxt->state; |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 3984 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3985 | counter = xmlAutomataNewCounter(ctxt->am, |
| 3986 | type-> |
| 3987 | minOccurs - 1, |
| 3988 | UNBOUNDED); |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 3989 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3990 | subtypes = type->subtypes; |
| 3991 | while (subtypes != NULL) { |
| 3992 | xmlSchemaBuildAContentModel(subtypes, ctxt, |
| 3993 | name); |
| 3994 | subtypes = subtypes->next; |
| 3995 | } |
| 3996 | tmp = ctxt->state; |
| 3997 | xmlAutomataNewCountedTrans(ctxt->am, tmp, |
| 3998 | oldstate, counter); |
| 3999 | ctxt->state = |
| 4000 | xmlAutomataNewCounterTrans(ctxt->am, tmp, |
| 4001 | NULL, counter); |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4002 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4003 | } else { |
| 4004 | subtypes = type->subtypes; |
| 4005 | while (subtypes != NULL) { |
| 4006 | xmlSchemaBuildAContentModel(subtypes, ctxt, |
| 4007 | name); |
| 4008 | subtypes = subtypes->next; |
| 4009 | } |
| 4010 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
| 4011 | oldstate); |
| 4012 | if (type->minOccurs == 0) { |
| 4013 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 4014 | ctxt->state); |
| 4015 | } |
| 4016 | } |
| 4017 | } else if ((type->maxOccurs > 1) |
| 4018 | || (type->minOccurs > 1)) { |
| 4019 | xmlAutomataStatePtr tmp; |
| 4020 | int counter; |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4021 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4022 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
| 4023 | oldstate, |
| 4024 | NULL); |
| 4025 | oldstate = ctxt->state; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4026 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4027 | counter = xmlAutomataNewCounter(ctxt->am, |
| 4028 | type->minOccurs - |
| 4029 | 1, |
| 4030 | type->maxOccurs - |
| 4031 | 1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4032 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4033 | subtypes = type->subtypes; |
| 4034 | while (subtypes != NULL) { |
| 4035 | xmlSchemaBuildAContentModel(subtypes, ctxt, |
| 4036 | name); |
| 4037 | subtypes = subtypes->next; |
| 4038 | } |
| 4039 | tmp = ctxt->state; |
| 4040 | xmlAutomataNewCountedTrans(ctxt->am, tmp, oldstate, |
| 4041 | counter); |
| 4042 | ctxt->state = |
| 4043 | xmlAutomataNewCounterTrans(ctxt->am, tmp, NULL, |
| 4044 | counter); |
| 4045 | if (type->minOccurs == 0) { |
| 4046 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 4047 | ctxt->state); |
| 4048 | } |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 4049 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4050 | } else { |
| 4051 | subtypes = type->subtypes; |
| 4052 | while (subtypes != NULL) { |
| 4053 | xmlSchemaBuildAContentModel(subtypes, ctxt, |
| 4054 | name); |
| 4055 | subtypes = subtypes->next; |
| 4056 | } |
| 4057 | if (type->minOccurs == 0) { |
| 4058 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 4059 | ctxt->state); |
| 4060 | } |
| 4061 | } |
| 4062 | } |
| 4063 | break; |
| 4064 | } |
| 4065 | case XML_SCHEMA_TYPE_CHOICE:{ |
| 4066 | xmlSchemaTypePtr subtypes; |
| 4067 | xmlAutomataStatePtr start, end; |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 4068 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4069 | start = ctxt->state; |
| 4070 | end = xmlAutomataNewState(ctxt->am); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4071 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4072 | /* |
| 4073 | * iterate over the subtypes and remerge the end with an |
| 4074 | * epsilon transition |
| 4075 | */ |
| 4076 | if (type->maxOccurs == 1) { |
| 4077 | subtypes = type->subtypes; |
| 4078 | while (subtypes != NULL) { |
| 4079 | ctxt->state = start; |
| 4080 | xmlSchemaBuildAContentModel(subtypes, ctxt, name); |
| 4081 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, end); |
| 4082 | subtypes = subtypes->next; |
| 4083 | } |
| 4084 | } else { |
| 4085 | int counter; |
| 4086 | xmlAutomataStatePtr hop; |
| 4087 | int maxOccurs = type->maxOccurs == UNBOUNDED ? |
| 4088 | UNBOUNDED : type->maxOccurs - 1; |
| 4089 | int minOccurs = |
| 4090 | type->minOccurs < 1 ? 0 : type->minOccurs - 1; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4091 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4092 | /* |
| 4093 | * use a counter to keep track of the number of transtions |
| 4094 | * which went through the choice. |
| 4095 | */ |
| 4096 | counter = |
| 4097 | xmlAutomataNewCounter(ctxt->am, minOccurs, |
| 4098 | maxOccurs); |
| 4099 | hop = xmlAutomataNewState(ctxt->am); |
Daniel Veillard | 6231e84 | 2002-04-18 11:54:04 +0000 | [diff] [blame] | 4100 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4101 | subtypes = type->subtypes; |
| 4102 | while (subtypes != NULL) { |
| 4103 | ctxt->state = start; |
| 4104 | xmlSchemaBuildAContentModel(subtypes, ctxt, name); |
| 4105 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, hop); |
| 4106 | subtypes = subtypes->next; |
| 4107 | } |
| 4108 | xmlAutomataNewCountedTrans(ctxt->am, hop, start, |
| 4109 | counter); |
| 4110 | xmlAutomataNewCounterTrans(ctxt->am, hop, end, |
| 4111 | counter); |
| 4112 | } |
| 4113 | if (type->minOccurs == 0) { |
| 4114 | xmlAutomataNewEpsilon(ctxt->am, start, end); |
| 4115 | } |
| 4116 | ctxt->state = end; |
| 4117 | break; |
| 4118 | } |
| 4119 | case XML_SCHEMA_TYPE_ALL:{ |
| 4120 | xmlAutomataStatePtr start; |
| 4121 | xmlSchemaTypePtr subtypes; |
| 4122 | xmlSchemaElementPtr elem = (xmlSchemaElementPtr) type; |
| 4123 | int lax; |
| 4124 | |
| 4125 | subtypes = type->subtypes; |
| 4126 | if (subtypes == NULL) |
| 4127 | break; |
| 4128 | start = ctxt->state; |
| 4129 | while (subtypes != NULL) { |
| 4130 | ctxt->state = start; |
| 4131 | elem = (xmlSchemaElementPtr) subtypes; |
| 4132 | |
| 4133 | /* TODO : handle the namespace too */ |
| 4134 | if ((elem->minOccurs == 1) && (elem->maxOccurs == 1)) { |
| 4135 | xmlAutomataNewOnceTrans(ctxt->am, ctxt->state, |
| 4136 | ctxt->state, elem->name, 1, |
| 4137 | 1, subtypes); |
| 4138 | } else { |
| 4139 | xmlAutomataNewCountTrans(ctxt->am, ctxt->state, |
| 4140 | ctxt->state, elem->name, |
| 4141 | elem->minOccurs, |
| 4142 | elem->maxOccurs, |
| 4143 | subtypes); |
| 4144 | } |
| 4145 | subtypes = subtypes->next; |
| 4146 | } |
| 4147 | lax = type->minOccurs == 0; |
| 4148 | ctxt->state = |
| 4149 | xmlAutomataNewAllTrans(ctxt->am, ctxt->state, NULL, |
| 4150 | lax); |
| 4151 | break; |
| 4152 | } |
| 4153 | case XML_SCHEMA_TYPE_RESTRICTION: |
| 4154 | if (type->subtypes != NULL) |
| 4155 | xmlSchemaBuildAContentModel(type->subtypes, ctxt, name); |
| 4156 | break; |
| 4157 | case XML_SCHEMA_TYPE_EXTENSION: |
| 4158 | if (type->baseType != NULL) { |
| 4159 | xmlSchemaTypePtr subtypes; |
| 4160 | |
| 4161 | xmlSchemaBuildAContentModel(type->baseType, ctxt, name); |
| 4162 | subtypes = type->subtypes; |
| 4163 | while (subtypes != NULL) { |
| 4164 | xmlSchemaBuildAContentModel(subtypes, ctxt, name); |
| 4165 | subtypes = subtypes->next; |
| 4166 | } |
| 4167 | } else if (type->subtypes != NULL) |
| 4168 | xmlSchemaBuildAContentModel(type->subtypes, ctxt, name); |
| 4169 | break; |
| 4170 | case XML_SCHEMA_TYPE_GROUP: |
| 4171 | if (type->subtypes == NULL) { |
| 4172 | } |
| 4173 | case XML_SCHEMA_TYPE_COMPLEX: |
| 4174 | case XML_SCHEMA_TYPE_COMPLEX_CONTENT: |
| 4175 | if (type->subtypes != NULL) |
| 4176 | xmlSchemaBuildAContentModel(type->subtypes, ctxt, name); |
| 4177 | break; |
| 4178 | default: |
| 4179 | xmlGenericError(xmlGenericErrorContext, |
| 4180 | "Found unexpected type %d in %s content model\n", |
| 4181 | type->type, name); |
| 4182 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4183 | } |
| 4184 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4185 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4186 | /** |
| 4187 | * xmlSchemaBuildContentModel: |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4188 | * @elem: the element |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4189 | * @ctxt: the schema parser context |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4190 | * @name: the element name |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4191 | * |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4192 | * Builds the content model of the element. |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4193 | */ |
| 4194 | static void |
| 4195 | xmlSchemaBuildContentModel(xmlSchemaElementPtr elem, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4196 | xmlSchemaParserCtxtPtr ctxt, |
| 4197 | const xmlChar * name) |
| 4198 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4199 | xmlAutomataStatePtr start; |
| 4200 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4201 | if (elem->contModel != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4202 | return; |
Daniel Veillard | 88c5891 | 2002-04-23 07:12:20 +0000 | [diff] [blame] | 4203 | if (elem->subtypes == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4204 | elem->contentType = XML_SCHEMA_CONTENT_ANY; |
| 4205 | return; |
Daniel Veillard | 88c5891 | 2002-04-23 07:12:20 +0000 | [diff] [blame] | 4206 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4207 | if (elem->subtypes->type != XML_SCHEMA_TYPE_COMPLEX) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4208 | return; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 4209 | if ((elem->subtypes->contentType == XML_SCHEMA_CONTENT_BASIC) || |
| 4210 | (elem->subtypes->contentType == XML_SCHEMA_CONTENT_SIMPLE)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4211 | return; |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 4212 | |
| 4213 | #ifdef DEBUG_CONTENT |
| 4214 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4215 | "Building content model for %s\n", name); |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 4216 | #endif |
| 4217 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4218 | ctxt->am = xmlNewAutomata(); |
| 4219 | if (ctxt->am == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4220 | xmlGenericError(xmlGenericErrorContext, |
| 4221 | "Cannot create automata for elem %s\n", name); |
| 4222 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4223 | } |
| 4224 | start = ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 4225 | xmlSchemaBuildAContentModel(elem->subtypes, ctxt, name); |
| 4226 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
Daniel Veillard | 4402ab4 | 2002-09-12 16:02:56 +0000 | [diff] [blame] | 4227 | elem->contModel = xmlAutomataCompile(ctxt->am); |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4228 | if (elem->contModel == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4229 | xmlSchemaPErr(ctxt, elem->node, XML_SCHEMAS_ERR_INTERNAL, |
| 4230 | "failed to compile %s content model\n", name, NULL); |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4231 | } else if (xmlRegexpIsDeterminist(elem->contModel) != 1) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4232 | xmlSchemaPErr(ctxt, elem->node, XML_SCHEMAS_ERR_NOTDETERMINIST, |
| 4233 | "Content model of %s is not determinist:\n", name, |
| 4234 | NULL); |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 4235 | } else { |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 4236 | #ifdef DEBUG_CONTENT_REGEXP |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4237 | xmlGenericError(xmlGenericErrorContext, |
| 4238 | "Content model of %s:\n", name); |
| 4239 | xmlRegexpPrint(stderr, elem->contModel); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4240 | #endif |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 4241 | } |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4242 | ctxt->state = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4243 | xmlFreeAutomata(ctxt->am); |
| 4244 | ctxt->am = NULL; |
| 4245 | } |
| 4246 | |
| 4247 | /** |
| 4248 | * xmlSchemaRefFixupCallback: |
| 4249 | * @elem: the schema element context |
| 4250 | * @ctxt: the schema parser context |
| 4251 | * |
| 4252 | * Free the resources associated to the schema parser context |
| 4253 | */ |
| 4254 | static void |
| 4255 | xmlSchemaRefFixupCallback(xmlSchemaElementPtr elem, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4256 | xmlSchemaParserCtxtPtr ctxt, |
| 4257 | const xmlChar * name, |
| 4258 | const xmlChar * context ATTRIBUTE_UNUSED, |
| 4259 | const xmlChar * namespace ATTRIBUTE_UNUSED) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4260 | { |
| 4261 | if ((ctxt == NULL) || (elem == NULL)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4262 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4263 | if (elem->ref != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4264 | xmlSchemaElementPtr elemDecl; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4265 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4266 | if (elem->subtypes != NULL) { |
| 4267 | xmlSchemaPErr(ctxt, elem->node, |
| 4268 | XML_SCHEMAP_INVALID_REF_AND_SUBTYPE, |
| 4269 | "Schemas: element %s have both ref and subtype\n", |
| 4270 | name, NULL); |
| 4271 | return; |
| 4272 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 4273 | elemDecl = xmlSchemaGetElem(ctxt->schema, elem->ref, elem->refNs, 0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4274 | |
| 4275 | if (elemDecl == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4276 | xmlSchemaPErr(ctxt, elem->node, XML_SCHEMAP_UNKNOWN_REF, |
| 4277 | "Schemas: element %s ref to %s not found\n", |
| 4278 | name, elem->ref); |
| 4279 | return; |
| 4280 | } |
| 4281 | elem->refDecl = elemDecl; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4282 | } else if (elem->namedType != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4283 | xmlSchemaTypePtr typeDecl; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4284 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4285 | if (elem->subtypes != NULL) { |
| 4286 | xmlSchemaPErr(ctxt, elem->node, XML_SCHEMAP_TYPE_AND_SUBTYPE, |
| 4287 | "Schemas: element %s have both type and subtype\n", |
| 4288 | name, NULL); |
| 4289 | return; |
| 4290 | } |
| 4291 | typeDecl = xmlSchemaGetType(ctxt->schema, elem->namedType, |
| 4292 | elem->namedTypeNs); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4293 | |
| 4294 | if (typeDecl == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4295 | xmlSchemaPErr(ctxt, elem->node, XML_SCHEMAP_UNKNOWN_TYPE, |
| 4296 | "Schemas: element %s type %s not found\n", name, |
| 4297 | elem->namedType); |
| 4298 | return; |
| 4299 | } |
| 4300 | elem->subtypes = typeDecl; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4301 | } |
| 4302 | } |
| 4303 | |
| 4304 | /** |
| 4305 | * xmlSchemaTypeFixup: |
| 4306 | * @typeDecl: the schema type definition |
| 4307 | * @ctxt: the schema parser context |
| 4308 | * |
| 4309 | * Fixes the content model of the type. |
| 4310 | */ |
| 4311 | static void |
| 4312 | xmlSchemaTypeFixup(xmlSchemaTypePtr typeDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4313 | xmlSchemaParserCtxtPtr ctxt, const xmlChar * name) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4314 | { |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 4315 | if (typeDecl == NULL) |
| 4316 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4317 | if (name == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4318 | name = typeDecl->name; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4319 | if (typeDecl->contentType == XML_SCHEMA_CONTENT_UNKNOWN) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4320 | switch (typeDecl->type) { |
| 4321 | case XML_SCHEMA_TYPE_SIMPLE_CONTENT:{ |
| 4322 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, NULL); |
| 4323 | if (typeDecl->subtypes != NULL) |
| 4324 | typeDecl->contentType = |
| 4325 | typeDecl->subtypes->contentType; |
| 4326 | break; |
| 4327 | } |
| 4328 | case XML_SCHEMA_TYPE_RESTRICTION:{ |
| 4329 | if (typeDecl->subtypes != NULL) |
| 4330 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4331 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4332 | if (typeDecl->base != NULL) { |
| 4333 | xmlSchemaTypePtr baseType; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4334 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4335 | baseType = |
| 4336 | xmlSchemaGetType(ctxt->schema, typeDecl->base, |
| 4337 | typeDecl->baseNs); |
| 4338 | if (baseType == NULL) { |
| 4339 | xmlSchemaPErr(ctxt, typeDecl->node, |
| 4340 | XML_SCHEMAP_UNKNOWN_BASE_TYPE, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4341 | "Schemas: type %s base type %s not found\n", |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4342 | name, typeDecl->base); |
| 4343 | } |
| 4344 | typeDecl->baseType = baseType; |
| 4345 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 4346 | if (typeDecl->subtypes == NULL) |
| 4347 | if (typeDecl->baseType != NULL) |
| 4348 | typeDecl->contentType = |
| 4349 | typeDecl->baseType->contentType; |
| 4350 | else |
| 4351 | /* 1.1.1 */ |
| 4352 | typeDecl->contentType = XML_SCHEMA_CONTENT_EMPTY; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4353 | else if ((typeDecl->subtypes->subtypes == NULL) && |
| 4354 | ((typeDecl->subtypes->type == |
| 4355 | XML_SCHEMA_TYPE_ALL) |
| 4356 | || (typeDecl->subtypes->type == |
| 4357 | XML_SCHEMA_TYPE_SEQUENCE))) |
| 4358 | /* 1.1.2 */ |
| 4359 | typeDecl->contentType = XML_SCHEMA_CONTENT_EMPTY; |
| 4360 | else if ((typeDecl->subtypes->type == |
| 4361 | XML_SCHEMA_TYPE_CHOICE) |
| 4362 | && (typeDecl->subtypes->subtypes == NULL)) |
| 4363 | /* 1.1.3 */ |
| 4364 | typeDecl->contentType = XML_SCHEMA_CONTENT_EMPTY; |
| 4365 | else { |
| 4366 | /* 1.2 and 2.X are applied at the other layer */ |
| 4367 | typeDecl->contentType = |
| 4368 | XML_SCHEMA_CONTENT_ELEMENTS; |
| 4369 | } |
| 4370 | break; |
| 4371 | } |
| 4372 | case XML_SCHEMA_TYPE_EXTENSION:{ |
| 4373 | xmlSchemaContentType explicitContentType; |
| 4374 | xmlSchemaTypePtr base; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4375 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4376 | if (typeDecl->base != NULL) { |
| 4377 | xmlSchemaTypePtr baseType; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4378 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4379 | baseType = |
| 4380 | xmlSchemaGetType(ctxt->schema, typeDecl->base, |
| 4381 | typeDecl->baseNs); |
| 4382 | if (baseType == NULL) { |
| 4383 | xmlSchemaPErr(ctxt, typeDecl->node, |
| 4384 | XML_SCHEMAP_UNKNOWN_BASE_TYPE, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4385 | "Schemas: type %s base type %s not found\n", |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4386 | name, typeDecl->base); |
| 4387 | } |
| 4388 | typeDecl->baseType = baseType; |
| 4389 | } |
| 4390 | if (typeDecl->subtypes != NULL) |
| 4391 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4392 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4393 | explicitContentType = XML_SCHEMA_CONTENT_ELEMENTS; |
| 4394 | if (typeDecl->subtypes == NULL) |
| 4395 | /* 1.1.1 */ |
| 4396 | explicitContentType = XML_SCHEMA_CONTENT_EMPTY; |
| 4397 | else if ((typeDecl->subtypes->subtypes == NULL) && |
| 4398 | ((typeDecl->subtypes->type == |
| 4399 | XML_SCHEMA_TYPE_ALL) |
| 4400 | || (typeDecl->subtypes->type == |
| 4401 | XML_SCHEMA_TYPE_SEQUENCE))) |
| 4402 | /* 1.1.2 */ |
| 4403 | explicitContentType = XML_SCHEMA_CONTENT_EMPTY; |
| 4404 | else if ((typeDecl->subtypes->type == |
| 4405 | XML_SCHEMA_TYPE_CHOICE) |
| 4406 | && (typeDecl->subtypes->subtypes == NULL)) |
| 4407 | /* 1.1.3 */ |
| 4408 | explicitContentType = XML_SCHEMA_CONTENT_EMPTY; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4409 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4410 | base = xmlSchemaGetType(ctxt->schema, typeDecl->base, |
| 4411 | typeDecl->baseNs); |
| 4412 | if (base == NULL) { |
| 4413 | xmlSchemaPErr(ctxt, typeDecl->node, |
| 4414 | XML_SCHEMAP_UNKNOWN_BASE_TYPE, |
| 4415 | "Schemas: base type %s of type %s not found\n", |
| 4416 | typeDecl->base, name); |
| 4417 | return; |
| 4418 | } |
| 4419 | xmlSchemaTypeFixup(base, ctxt, NULL); |
| 4420 | if (explicitContentType == XML_SCHEMA_CONTENT_EMPTY) { |
| 4421 | /* 2.1 */ |
| 4422 | typeDecl->contentType = base->contentType; |
| 4423 | } else if (base->contentType == |
| 4424 | XML_SCHEMA_CONTENT_EMPTY) { |
| 4425 | /* 2.2 imbitable ! */ |
| 4426 | typeDecl->contentType = |
| 4427 | XML_SCHEMA_CONTENT_ELEMENTS; |
| 4428 | } else { |
| 4429 | /* 2.3 imbitable pareil ! */ |
| 4430 | typeDecl->contentType = |
| 4431 | XML_SCHEMA_CONTENT_ELEMENTS; |
| 4432 | } |
| 4433 | break; |
| 4434 | } |
| 4435 | case XML_SCHEMA_TYPE_COMPLEX:{ |
| 4436 | if (typeDecl->subtypes == NULL) { |
| 4437 | typeDecl->contentType = XML_SCHEMA_CONTENT_EMPTY; |
| 4438 | } else { |
| 4439 | if (typeDecl->flags & XML_SCHEMAS_TYPE_MIXED) |
| 4440 | typeDecl->contentType = |
| 4441 | XML_SCHEMA_CONTENT_MIXED; |
| 4442 | else { |
| 4443 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, |
| 4444 | NULL); |
| 4445 | if (typeDecl->subtypes != NULL) |
| 4446 | typeDecl->contentType = |
| 4447 | typeDecl->subtypes->contentType; |
| 4448 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 4449 | if (typeDecl->attributes == NULL) |
| 4450 | typeDecl->attributes = |
| 4451 | typeDecl->subtypes->attributes; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4452 | } |
| 4453 | break; |
| 4454 | } |
| 4455 | case XML_SCHEMA_TYPE_COMPLEX_CONTENT:{ |
| 4456 | if (typeDecl->subtypes == NULL) { |
| 4457 | typeDecl->contentType = XML_SCHEMA_CONTENT_EMPTY; |
| 4458 | } else { |
| 4459 | if (typeDecl->flags & XML_SCHEMAS_TYPE_MIXED) |
| 4460 | typeDecl->contentType = |
| 4461 | XML_SCHEMA_CONTENT_MIXED; |
| 4462 | else { |
| 4463 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, |
| 4464 | NULL); |
| 4465 | if (typeDecl->subtypes != NULL) |
| 4466 | typeDecl->contentType = |
| 4467 | typeDecl->subtypes->contentType; |
| 4468 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 4469 | if (typeDecl->attributes == NULL) |
| 4470 | typeDecl->attributes = |
| 4471 | typeDecl->subtypes->attributes; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4472 | } |
| 4473 | break; |
| 4474 | } |
| 4475 | case XML_SCHEMA_TYPE_SEQUENCE: |
| 4476 | case XML_SCHEMA_TYPE_GROUP: |
| 4477 | case XML_SCHEMA_TYPE_ALL: |
| 4478 | case XML_SCHEMA_TYPE_CHOICE: |
| 4479 | typeDecl->contentType = XML_SCHEMA_CONTENT_ELEMENTS; |
| 4480 | break; |
| 4481 | case XML_SCHEMA_TYPE_BASIC: |
| 4482 | case XML_SCHEMA_TYPE_ANY: |
| 4483 | case XML_SCHEMA_TYPE_FACET: |
| 4484 | case XML_SCHEMA_TYPE_SIMPLE: |
| 4485 | case XML_SCHEMA_TYPE_UR: |
| 4486 | case XML_SCHEMA_TYPE_ELEMENT: |
| 4487 | case XML_SCHEMA_TYPE_ATTRIBUTE: |
| 4488 | case XML_SCHEMA_TYPE_ATTRIBUTEGROUP: |
| 4489 | case XML_SCHEMA_TYPE_NOTATION: |
| 4490 | case XML_SCHEMA_TYPE_LIST: |
| 4491 | case XML_SCHEMA_TYPE_UNION: |
| 4492 | case XML_SCHEMA_FACET_MININCLUSIVE: |
| 4493 | case XML_SCHEMA_FACET_MINEXCLUSIVE: |
| 4494 | case XML_SCHEMA_FACET_MAXINCLUSIVE: |
| 4495 | case XML_SCHEMA_FACET_MAXEXCLUSIVE: |
| 4496 | case XML_SCHEMA_FACET_TOTALDIGITS: |
| 4497 | case XML_SCHEMA_FACET_FRACTIONDIGITS: |
| 4498 | case XML_SCHEMA_FACET_PATTERN: |
| 4499 | case XML_SCHEMA_FACET_ENUMERATION: |
| 4500 | case XML_SCHEMA_FACET_WHITESPACE: |
| 4501 | case XML_SCHEMA_FACET_LENGTH: |
| 4502 | case XML_SCHEMA_FACET_MAXLENGTH: |
| 4503 | case XML_SCHEMA_FACET_MINLENGTH: |
| 4504 | typeDecl->contentType = XML_SCHEMA_CONTENT_SIMPLE; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 4505 | if (typeDecl->subtypes != NULL) |
| 4506 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4507 | break; |
| 4508 | } |
| 4509 | } |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4510 | #ifdef DEBUG_TYPE |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 4511 | if (typeDecl->node != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4512 | xmlGenericError(xmlGenericErrorContext, |
| 4513 | "Type of %s : %s:%d :", name, |
| 4514 | typeDecl->node->doc->URL, |
| 4515 | xmlGetLineNo(typeDecl->node)); |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 4516 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4517 | xmlGenericError(xmlGenericErrorContext, "Type of %s :", name); |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 4518 | } |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4519 | switch (typeDecl->contentType) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4520 | case XML_SCHEMA_CONTENT_SIMPLE: |
| 4521 | xmlGenericError(xmlGenericErrorContext, "simple\n"); |
| 4522 | break; |
| 4523 | case XML_SCHEMA_CONTENT_ELEMENTS: |
| 4524 | xmlGenericError(xmlGenericErrorContext, "elements\n"); |
| 4525 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4526 | case XML_SCHEMA_CONTENT_UNKNOWN: |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4527 | xmlGenericError(xmlGenericErrorContext, "unknown !!!\n"); |
| 4528 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4529 | case XML_SCHEMA_CONTENT_EMPTY: |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4530 | xmlGenericError(xmlGenericErrorContext, "empty\n"); |
| 4531 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4532 | case XML_SCHEMA_CONTENT_MIXED: |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4533 | xmlGenericError(xmlGenericErrorContext, "mixed\n"); |
| 4534 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4535 | case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS: |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4536 | xmlGenericError(xmlGenericErrorContext, "mixed or elems\n"); |
| 4537 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4538 | case XML_SCHEMA_CONTENT_BASIC: |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4539 | xmlGenericError(xmlGenericErrorContext, "basic\n"); |
| 4540 | break; |
| 4541 | default: |
| 4542 | xmlGenericError(xmlGenericErrorContext, |
| 4543 | "not registered !!!\n"); |
| 4544 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4545 | } |
| 4546 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4547 | } |
| 4548 | |
| 4549 | /** |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4550 | * xmlSchemaCheckFacet: |
| 4551 | * @facet: the facet |
| 4552 | * @typeDecl: the schema type definition |
| 4553 | * @ctxt: the schema parser context or NULL |
| 4554 | * @name: name of the type |
| 4555 | * |
| 4556 | * Checks the default values types, especially for facets |
| 4557 | * |
| 4558 | * Returns 0 if okay or -1 in cae of error |
| 4559 | */ |
| 4560 | int |
| 4561 | xmlSchemaCheckFacet(xmlSchemaFacetPtr facet, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4562 | xmlSchemaTypePtr typeDecl, |
| 4563 | xmlSchemaParserCtxtPtr ctxt, const xmlChar * name) |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4564 | { |
| 4565 | static xmlSchemaTypePtr nonNegativeIntegerType = NULL; |
| 4566 | int ret = 0; |
| 4567 | |
| 4568 | if (nonNegativeIntegerType == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4569 | nonNegativeIntegerType = |
| 4570 | xmlSchemaGetPredefinedType(BAD_CAST "nonNegativeInteger", |
| 4571 | xmlSchemaNs); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4572 | } |
| 4573 | switch (facet->type) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4574 | case XML_SCHEMA_FACET_MININCLUSIVE: |
| 4575 | case XML_SCHEMA_FACET_MINEXCLUSIVE: |
| 4576 | case XML_SCHEMA_FACET_MAXINCLUSIVE: |
| 4577 | case XML_SCHEMA_FACET_MAXEXCLUSIVE:{ |
| 4578 | /* |
| 4579 | * Okay we need to validate the value |
| 4580 | * at that point. |
| 4581 | */ |
| 4582 | xmlSchemaValidCtxtPtr vctxt; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4583 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4584 | vctxt = xmlSchemaNewValidCtxt(NULL); |
| 4585 | if (vctxt == NULL) |
| 4586 | break; |
| 4587 | xmlSchemaValidateSimpleValue(vctxt, typeDecl, |
| 4588 | facet->value); |
| 4589 | facet->val = vctxt->value; |
| 4590 | vctxt->value = NULL; |
| 4591 | if (facet->val == NULL) { |
| 4592 | /* error code */ |
| 4593 | if (ctxt != NULL) { |
| 4594 | xmlSchemaPErr(ctxt, facet->node, |
| 4595 | XML_SCHEMAP_INVALID_FACET, |
| 4596 | "Schemas: type %s facet value %s invalid\n", |
| 4597 | name, facet->value); |
| 4598 | } |
| 4599 | ret = -1; |
| 4600 | } |
| 4601 | xmlSchemaFreeValidCtxt(vctxt); |
| 4602 | break; |
| 4603 | } |
| 4604 | case XML_SCHEMA_FACET_ENUMERATION:{ |
| 4605 | /* |
| 4606 | * Okay we need to validate the value |
| 4607 | * at that point. |
| 4608 | */ |
| 4609 | xmlSchemaValidCtxtPtr vctxt; |
| 4610 | int tmp; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4611 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4612 | vctxt = xmlSchemaNewValidCtxt(NULL); |
| 4613 | if (vctxt == NULL) |
| 4614 | break; |
| 4615 | tmp = xmlSchemaValidateSimpleValue(vctxt, typeDecl, |
| 4616 | facet->value); |
| 4617 | if (tmp != 0) { |
| 4618 | if (ctxt != NULL) { |
| 4619 | xmlSchemaPErr(ctxt, facet->node, |
| 4620 | XML_SCHEMAP_INVALID_ENUM, |
| 4621 | "Schemas: type %s enumeration value %s invalid\n", |
| 4622 | name, facet->value); |
| 4623 | } |
| 4624 | ret = -1; |
| 4625 | } |
| 4626 | xmlSchemaFreeValidCtxt(vctxt); |
| 4627 | break; |
| 4628 | } |
| 4629 | case XML_SCHEMA_FACET_PATTERN: |
| 4630 | facet->regexp = xmlRegexpCompile(facet->value); |
| 4631 | if (facet->regexp == NULL) { |
| 4632 | xmlSchemaPErr(ctxt, typeDecl->node, |
| 4633 | XML_SCHEMAP_REGEXP_INVALID, |
| 4634 | "Schemas: type %s facet regexp %s invalid\n", |
| 4635 | name, facet->value); |
| 4636 | ret = -1; |
| 4637 | } |
| 4638 | break; |
| 4639 | case XML_SCHEMA_FACET_TOTALDIGITS: |
| 4640 | case XML_SCHEMA_FACET_FRACTIONDIGITS: |
| 4641 | case XML_SCHEMA_FACET_LENGTH: |
| 4642 | case XML_SCHEMA_FACET_MAXLENGTH: |
| 4643 | case XML_SCHEMA_FACET_MINLENGTH:{ |
| 4644 | int tmp; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4645 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4646 | tmp = |
| 4647 | xmlSchemaValidatePredefinedType(nonNegativeIntegerType, |
| 4648 | facet->value, |
| 4649 | &facet->val); |
| 4650 | if (tmp != 0) { |
| 4651 | /* error code */ |
| 4652 | if (ctxt != NULL) { |
| 4653 | xmlSchemaPErr(ctxt, facet->node, |
| 4654 | XML_SCHEMAP_INVALID_FACET_VALUE, |
| 4655 | "Schemas: type %s facet value %s invalid\n", |
| 4656 | name, facet->value); |
| 4657 | } |
| 4658 | ret = -1; |
| 4659 | } |
| 4660 | break; |
| 4661 | } |
| 4662 | case XML_SCHEMA_FACET_WHITESPACE:{ |
| 4663 | if (xmlStrEqual(facet->value, BAD_CAST "preserve")) { |
| 4664 | facet->whitespace = XML_SCHEMAS_FACET_PRESERVE; |
| 4665 | } else if (xmlStrEqual(facet->value, BAD_CAST "replace")) { |
| 4666 | facet->whitespace = XML_SCHEMAS_FACET_REPLACE; |
| 4667 | } else if (xmlStrEqual(facet->value, BAD_CAST "collapse")) { |
| 4668 | facet->whitespace = XML_SCHEMAS_FACET_COLLAPSE; |
| 4669 | } else { |
| 4670 | if (ctxt != NULL) { |
| 4671 | xmlSchemaPErr(ctxt, facet->node, |
| 4672 | XML_SCHEMAP_INVALID_WHITE_SPACE, |
| 4673 | "Schemas: type %s whiteSpace value %s invalid\n", |
| 4674 | name, facet->value); |
| 4675 | } |
| 4676 | ret = -1; |
| 4677 | } |
| 4678 | } |
| 4679 | default: |
| 4680 | break; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4681 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4682 | return (ret); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4683 | } |
| 4684 | |
| 4685 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4686 | * xmlSchemaCheckDefaults: |
| 4687 | * @typeDecl: the schema type definition |
| 4688 | * @ctxt: the schema parser context |
| 4689 | * |
| 4690 | * Checks the default values types, especially for facets |
| 4691 | */ |
| 4692 | static void |
| 4693 | xmlSchemaCheckDefaults(xmlSchemaTypePtr typeDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4694 | xmlSchemaParserCtxtPtr ctxt, const xmlChar * name) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4695 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4696 | if (name == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4697 | name = typeDecl->name; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4698 | if (typeDecl->type == XML_SCHEMA_TYPE_RESTRICTION) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4699 | if (typeDecl->facets != NULL) { |
| 4700 | xmlSchemaFacetPtr facet = typeDecl->facets; |
| 4701 | |
| 4702 | while (facet != NULL) { |
| 4703 | xmlSchemaCheckFacet(facet, typeDecl, ctxt, name); |
| 4704 | facet = facet->next; |
| 4705 | } |
| 4706 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4707 | } |
| 4708 | } |
| 4709 | |
| 4710 | /** |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4711 | * xmlSchemaAttrGrpFixup: |
| 4712 | * @attrgrpDecl: the schema attribute definition |
| 4713 | * @ctxt: the schema parser context |
| 4714 | * @name: the attribute name |
| 4715 | * |
| 4716 | * Fixes finish doing the computations on the attributes definitions |
| 4717 | */ |
| 4718 | static void |
| 4719 | xmlSchemaAttrGrpFixup(xmlSchemaAttributeGroupPtr attrgrpDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4720 | xmlSchemaParserCtxtPtr ctxt, const xmlChar * name) |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4721 | { |
| 4722 | if (name == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4723 | name = attrgrpDecl->name; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4724 | if (attrgrpDecl->attributes != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4725 | return; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4726 | if (attrgrpDecl->ref != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4727 | xmlSchemaAttributeGroupPtr ref; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4728 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4729 | ref = xmlHashLookup2(ctxt->schema->attrgrpDecl, attrgrpDecl->ref, |
| 4730 | attrgrpDecl->refNs); |
| 4731 | if (ref == NULL) { |
| 4732 | xmlSchemaPErr(ctxt, attrgrpDecl->node, |
| 4733 | XML_SCHEMAP_UNKNOWN_ATTRIBUTE_GROUP, |
| 4734 | "Schemas: attribute group %s reference %s not found\n", |
| 4735 | name, attrgrpDecl->ref); |
| 4736 | return; |
| 4737 | } |
| 4738 | xmlSchemaAttrGrpFixup(ref, ctxt, NULL); |
| 4739 | attrgrpDecl->attributes = ref->attributes; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4740 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4741 | xmlSchemaPErr(ctxt, attrgrpDecl->node, XML_SCHEMAP_NOATTR_NOREF, |
| 4742 | "Schemas: attribute %s has no attributes nor reference\n", |
| 4743 | name, NULL); |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4744 | } |
| 4745 | } |
| 4746 | |
| 4747 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4748 | * xmlSchemaAttrFixup: |
| 4749 | * @attrDecl: the schema attribute definition |
| 4750 | * @ctxt: the schema parser context |
| 4751 | * @name: the attribute name |
| 4752 | * |
| 4753 | * Fixes finish doing the computations on the attributes definitions |
| 4754 | */ |
| 4755 | static void |
| 4756 | xmlSchemaAttrFixup(xmlSchemaAttributePtr attrDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4757 | xmlSchemaParserCtxtPtr ctxt, const xmlChar * name) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4758 | { |
| 4759 | if (name == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4760 | name = attrDecl->name; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4761 | if (attrDecl->subtypes != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4762 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4763 | if (attrDecl->typeName != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4764 | xmlSchemaTypePtr type; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4765 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4766 | type = xmlSchemaGetType(ctxt->schema, attrDecl->typeName, |
| 4767 | attrDecl->typeNs); |
| 4768 | if (type == NULL) { |
| 4769 | xmlSchemaPErr(ctxt, attrDecl->node, XML_SCHEMAP_UNKNOWN_TYPE, |
| 4770 | "Schemas: attribute %s type %s not found\n", |
| 4771 | name, attrDecl->typeName); |
| 4772 | } |
| 4773 | attrDecl->subtypes = type; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4774 | } else if (attrDecl->ref != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4775 | xmlSchemaAttributePtr ref; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4776 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4777 | ref = xmlHashLookup2(ctxt->schema->attrDecl, attrDecl->ref, |
| 4778 | attrDecl->refNs); |
| 4779 | if (ref == NULL) { |
| 4780 | xmlSchemaPErr(ctxt, attrDecl->node, XML_SCHEMAP_UNKNOWN_REF, |
| 4781 | "Schemas: attribute %s reference %s not found\n", |
| 4782 | name, attrDecl->ref); |
| 4783 | return; |
| 4784 | } |
| 4785 | xmlSchemaAttrFixup(ref, ctxt, NULL); |
| 4786 | attrDecl->subtypes = ref->subtypes; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4787 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4788 | xmlSchemaPErr(ctxt, attrDecl->node, XML_SCHEMAP_NOTYPE_NOREF, |
| 4789 | "Schemas: attribute %s has no type nor reference\n", |
| 4790 | name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4791 | } |
| 4792 | } |
| 4793 | |
| 4794 | /** |
| 4795 | * xmlSchemaParse: |
| 4796 | * @ctxt: a schema validation context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4797 | * |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4798 | * parse a schema definition resource and build an internal |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4799 | * XML Shema struture which can be used to validate instances. |
| 4800 | * *WARNING* this interface is highly subject to change |
| 4801 | * |
| 4802 | * Returns the internal XML Schema structure built from the resource or |
| 4803 | * NULL in case of error |
| 4804 | */ |
| 4805 | xmlSchemaPtr |
| 4806 | xmlSchemaParse(xmlSchemaParserCtxtPtr ctxt) |
| 4807 | { |
| 4808 | xmlSchemaPtr ret = NULL; |
| 4809 | xmlDocPtr doc; |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 4810 | xmlNodePtr root; |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 4811 | int nberrors; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4812 | |
| 4813 | xmlSchemaInitTypes(); |
| 4814 | |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 4815 | if (ctxt == NULL) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4816 | return (NULL); |
| 4817 | |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 4818 | nberrors = ctxt->nberrors; |
| 4819 | ctxt->nberrors = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4820 | ctxt->counter = 0; |
| 4821 | ctxt->container = NULL; |
| 4822 | |
| 4823 | /* |
| 4824 | * First step is to parse the input document into an DOM/Infoset |
| 4825 | */ |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 4826 | if (ctxt->URL != NULL) { |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 4827 | doc = xmlReadFile((const char *) ctxt->URL, NULL, |
| 4828 | SCHEMAS_PARSE_OPTIONS); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4829 | if (doc == NULL) { |
| 4830 | xmlSchemaPErr(ctxt, NULL, |
| 4831 | XML_SCHEMAP_FAILED_LOAD, |
| 4832 | "xmlSchemaParse: could not load %s\n", |
| 4833 | ctxt->URL, NULL); |
| 4834 | return (NULL); |
| 4835 | } |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 4836 | } else if (ctxt->buffer != NULL) { |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 4837 | doc = xmlReadMemory(ctxt->buffer, ctxt->size, NULL, NULL, |
| 4838 | SCHEMAS_PARSE_OPTIONS); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4839 | if (doc == NULL) { |
| 4840 | xmlSchemaPErr(ctxt, NULL, |
| 4841 | XML_SCHEMAP_FAILED_PARSE, |
| 4842 | "xmlSchemaParse: could not parse\n", |
| 4843 | NULL, NULL); |
| 4844 | return (NULL); |
| 4845 | } |
| 4846 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 4847 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
Daniel Veillard | 9d75150 | 2003-10-29 13:21:47 +0000 | [diff] [blame] | 4848 | } else if (ctxt->doc != NULL) { |
| 4849 | doc = ctxt->doc; |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 4850 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4851 | xmlSchemaPErr(ctxt, NULL, |
| 4852 | XML_SCHEMAP_NOTHING_TO_PARSE, |
| 4853 | "xmlSchemaParse: could not parse\n", |
| 4854 | NULL, NULL); |
| 4855 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4856 | } |
| 4857 | |
| 4858 | /* |
| 4859 | * Then extract the root and Schema parse it |
| 4860 | */ |
| 4861 | root = xmlDocGetRootElement(doc); |
| 4862 | if (root == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4863 | xmlSchemaPErr(ctxt, (xmlNodePtr) doc, |
| 4864 | XML_SCHEMAP_NOROOT, |
| 4865 | "schemas has no root", NULL, NULL); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 4866 | xmlFreeDoc(doc); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4867 | return (NULL); |
| 4868 | } |
| 4869 | |
| 4870 | /* |
| 4871 | * Remove all the blank text nodes |
| 4872 | */ |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 4873 | xmlSchemaCleanupDoc(ctxt, root); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4874 | |
| 4875 | /* |
| 4876 | * Then do the parsing for good |
| 4877 | */ |
| 4878 | ret = xmlSchemaParseSchema(ctxt, root); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 4879 | if (ret == NULL) { |
| 4880 | xmlFreeDoc(doc); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4881 | return (NULL); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 4882 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4883 | ret->doc = doc; |
| 4884 | |
| 4885 | /* |
| 4886 | * Then fix all the references. |
| 4887 | */ |
| 4888 | ctxt->schema = ret; |
| 4889 | xmlHashScanFull(ret->elemDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4890 | (xmlHashScannerFull) xmlSchemaRefFixupCallback, ctxt); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4891 | |
| 4892 | /* |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 4893 | * Then fixup all attributes declarations |
| 4894 | */ |
| 4895 | xmlHashScan(ret->attrDecl, (xmlHashScanner) xmlSchemaAttrFixup, ctxt); |
| 4896 | |
| 4897 | /* |
| 4898 | * Then fixup all attributes group declarations |
| 4899 | */ |
| 4900 | xmlHashScan(ret->attrgrpDecl, (xmlHashScanner) xmlSchemaAttrGrpFixup, |
| 4901 | ctxt); |
| 4902 | |
| 4903 | /* |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4904 | * Then fixup all types properties |
| 4905 | */ |
| 4906 | xmlHashScan(ret->typeDecl, (xmlHashScanner) xmlSchemaTypeFixup, ctxt); |
| 4907 | |
| 4908 | /* |
| 4909 | * Then build the content model for all elements |
| 4910 | */ |
| 4911 | xmlHashScan(ret->elemDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4912 | (xmlHashScanner) xmlSchemaBuildContentModel, ctxt); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4913 | |
| 4914 | /* |
| 4915 | * Then check the defaults part of the type like facets values |
| 4916 | */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4917 | xmlHashScan(ret->typeDecl, (xmlHashScanner) xmlSchemaCheckDefaults, |
| 4918 | ctxt); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4919 | |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 4920 | if (ctxt->nberrors != 0) { |
| 4921 | xmlSchemaFree(ret); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4922 | ret = NULL; |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 4923 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4924 | return (ret); |
| 4925 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4926 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4927 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4928 | * xmlSchemaSetParserErrors: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4929 | * @ctxt: a schema validation context |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4930 | * @err: the error callback |
| 4931 | * @warn: the warning callback |
| 4932 | * @ctx: contextual data for the callbacks |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4933 | * |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4934 | * Set the callback functions used to handle errors for a validation context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4935 | */ |
| 4936 | void |
| 4937 | xmlSchemaSetParserErrors(xmlSchemaParserCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4938 | xmlSchemaValidityErrorFunc err, |
| 4939 | xmlSchemaValidityWarningFunc warn, void *ctx) |
| 4940 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4941 | if (ctxt == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4942 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4943 | ctxt->error = err; |
| 4944 | ctxt->warning = warn; |
| 4945 | ctxt->userData = ctx; |
| 4946 | } |
| 4947 | |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 4948 | /** |
| 4949 | * xmlSchemaFacetTypeToString: |
| 4950 | * @type: the facet type |
| 4951 | * |
| 4952 | * Convert the xmlSchemaTypeType to a char string. |
| 4953 | * |
| 4954 | * Returns the char string representation of the facet type if the |
| 4955 | * type is a facet and an "Internal Error" string otherwise. |
| 4956 | */ |
| 4957 | static const char * |
| 4958 | xmlSchemaFacetTypeToString(xmlSchemaTypeType type) |
| 4959 | { |
| 4960 | switch (type) { |
| 4961 | case XML_SCHEMA_FACET_PATTERN: |
| 4962 | return ("pattern"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4963 | case XML_SCHEMA_FACET_MAXEXCLUSIVE: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 4964 | return ("maxExclusive"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4965 | case XML_SCHEMA_FACET_MAXINCLUSIVE: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 4966 | return ("maxInclusive"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4967 | case XML_SCHEMA_FACET_MINEXCLUSIVE: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 4968 | return ("minExclusive"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4969 | case XML_SCHEMA_FACET_MININCLUSIVE: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 4970 | return ("minInclusive"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4971 | case XML_SCHEMA_FACET_WHITESPACE: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 4972 | return ("whiteSpace"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4973 | case XML_SCHEMA_FACET_ENUMERATION: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 4974 | return ("enumeration"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4975 | case XML_SCHEMA_FACET_LENGTH: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 4976 | return ("length"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4977 | case XML_SCHEMA_FACET_MAXLENGTH: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 4978 | return ("maxLength"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4979 | case XML_SCHEMA_FACET_MINLENGTH: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 4980 | return ("minLength"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4981 | case XML_SCHEMA_FACET_TOTALDIGITS: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 4982 | return ("totalDigits"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4983 | case XML_SCHEMA_FACET_FRACTIONDIGITS: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 4984 | return ("fractionDigits"); |
| 4985 | default: |
| 4986 | break; |
| 4987 | } |
| 4988 | return ("Internal Error"); |
| 4989 | } |
| 4990 | |
| 4991 | /** |
| 4992 | * xmlSchemaValidateFacets: |
| 4993 | * @ctxt: a schema validation context |
| 4994 | * @base: the base type |
| 4995 | * @facets: the list of facets to check |
| 4996 | * @value: the lexical repr of the value to validate |
| 4997 | * @val: the precomputed value |
| 4998 | * |
| 4999 | * Check a value against all facet conditions |
| 5000 | * |
| 5001 | * Returns 0 if the element is schemas valid, a positive error code |
| 5002 | * number otherwise and -1 in case of internal or API error. |
| 5003 | */ |
| 5004 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5005 | xmlSchemaValidateFacets(xmlSchemaValidCtxtPtr ctxt, |
| 5006 | xmlSchemaTypePtr base, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5007 | xmlSchemaFacetPtr facets, const xmlChar * value) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5008 | { |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5009 | int ret = 0; |
| 5010 | int tmp = 0; |
| 5011 | xmlSchemaTypeType type; |
| 5012 | xmlSchemaFacetPtr facet = facets; |
| 5013 | |
| 5014 | while (facet != NULL) { |
| 5015 | type = facet->type; |
| 5016 | if (type == XML_SCHEMA_FACET_ENUMERATION) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5017 | tmp = 1; |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5018 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5019 | while (facet != NULL) { |
| 5020 | tmp = |
| 5021 | xmlSchemaValidateFacet(base, facet, value, |
| 5022 | ctxt->value); |
| 5023 | if (tmp == 0) { |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5024 | return 0; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5025 | } |
| 5026 | facet = facet->next; |
| 5027 | } |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5028 | } else |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5029 | tmp = xmlSchemaValidateFacet(base, facet, value, ctxt->value); |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5030 | |
| 5031 | if (tmp != 0) { |
| 5032 | ret = tmp; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5033 | 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] | 5034 | } |
| 5035 | if (facet != NULL) |
| 5036 | facet = facet->next; |
| 5037 | } |
| 5038 | return (ret); |
| 5039 | } |
| 5040 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5041 | /************************************************************************ |
| 5042 | * * |
| 5043 | * Simple type validation * |
| 5044 | * * |
| 5045 | ************************************************************************/ |
| 5046 | |
| 5047 | /** |
| 5048 | * xmlSchemaValidateSimpleValue: |
| 5049 | * @ctxt: a schema validation context |
| 5050 | * @type: the type declaration |
| 5051 | * @value: the value to validate |
| 5052 | * |
| 5053 | * Validate a value against a simple type |
| 5054 | * |
| 5055 | * Returns 0 if the value is valid, a positive error code |
| 5056 | * number otherwise and -1 in case of internal or API error. |
| 5057 | */ |
| 5058 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5059 | xmlSchemaValidateSimpleValue(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5060 | xmlSchemaTypePtr type, const xmlChar * value) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5061 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5062 | int ret = 0; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5063 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5064 | /* |
| 5065 | * First normalize the value accordingly to Schema Datatype |
| 5066 | * 4.3.6 whiteSpace definition of the whiteSpace facet of type |
| 5067 | */ |
| 5068 | /* |
| 5069 | * Then check the normalized value against the lexical space of the |
| 5070 | * type. |
| 5071 | */ |
| 5072 | if (type->type == XML_SCHEMA_TYPE_BASIC) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5073 | if (ctxt->value != NULL) { |
| 5074 | xmlSchemaFreeValue(ctxt->value); |
| 5075 | ctxt->value = NULL; |
| 5076 | } |
| 5077 | ret = xmlSchemaValPredefTypeNode(type, value, &(ctxt->value), |
| 5078 | ctxt->cur); |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5079 | if (ret != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5080 | 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] | 5081 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5082 | } else if (type->type == XML_SCHEMA_TYPE_RESTRICTION) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5083 | xmlSchemaTypePtr base; |
| 5084 | xmlSchemaFacetPtr facet; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5085 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5086 | base = type->baseType; |
| 5087 | if (base != NULL) { |
| 5088 | ret = xmlSchemaValidateSimpleValue(ctxt, base, value); |
| 5089 | } else if (type->subtypes != NULL) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5090 | TODO |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5091 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5092 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5093 | /* |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 5094 | * Do not validate facets or attributes when working on |
| 5095 | * building the Schemas |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5096 | */ |
| 5097 | if (ctxt->schema != NULL) { |
| 5098 | if (ret == 0) { |
| 5099 | facet = type->facets; |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5100 | ret = xmlSchemaValidateFacets(ctxt, base, facet, value); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5101 | } |
| 5102 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5103 | } else if (type->type == XML_SCHEMA_TYPE_SIMPLE) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5104 | xmlSchemaTypePtr base; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5105 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5106 | base = type->subtypes; |
| 5107 | if (base != NULL) { |
| 5108 | ret = xmlSchemaValidateSimpleValue(ctxt, base, value); |
| 5109 | } else { |
| 5110 | TODO} |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5111 | } else if (type->type == XML_SCHEMA_TYPE_LIST) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5112 | xmlSchemaTypePtr base; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5113 | const xmlChar *cur, *end; |
| 5114 | xmlChar *tmp; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5115 | int ret2; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5116 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5117 | base = type->subtypes; |
| 5118 | if (base == NULL) { |
| 5119 | xmlSchemaVErr(ctxt, type->node, XML_SCHEMAS_ERR_INTERNAL, |
| 5120 | "Internal: List type %s has no base type\n", |
| 5121 | type->name, NULL); |
| 5122 | return (-1); |
| 5123 | } |
| 5124 | cur = value; |
| 5125 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5126 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5127 | cur++; |
| 5128 | end = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5129 | while ((*end != 0) && (!(IS_BLANK_CH(*end)))) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5130 | end++; |
| 5131 | if (end == cur) |
| 5132 | break; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5133 | tmp = xmlStrndup(cur, end - cur); |
| 5134 | ret2 = xmlSchemaValidateSimpleValue(ctxt, base, tmp); |
| 5135 | xmlFree(tmp); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5136 | if (ret2 != 0) |
| 5137 | ret = 1; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5138 | cur = end; |
| 5139 | } while (*cur != 0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5140 | } else { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5141 | TODO |
| 5142 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5143 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5144 | } |
| 5145 | |
| 5146 | /************************************************************************ |
| 5147 | * * |
| 5148 | * DOM Validation code * |
| 5149 | * * |
| 5150 | ************************************************************************/ |
| 5151 | |
| 5152 | static int xmlSchemaValidateContent(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5153 | xmlNodePtr node); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5154 | static int xmlSchemaValidateAttributes(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5155 | xmlNodePtr elem, |
| 5156 | xmlSchemaAttributePtr attributes); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5157 | static int xmlSchemaValidateType(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5158 | xmlNodePtr elem, |
| 5159 | xmlSchemaElementPtr elemDecl, |
| 5160 | xmlSchemaTypePtr type); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5161 | |
| 5162 | /** |
| 5163 | * xmlSchemaRegisterAttributes: |
| 5164 | * @ctxt: a schema validation context |
| 5165 | * @attrs: a list of attributes |
| 5166 | * |
| 5167 | * Register the list of attributes as the set to be validated on that element |
| 5168 | * |
| 5169 | * Returns -1 in case of error, 0 otherwise |
| 5170 | */ |
| 5171 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5172 | xmlSchemaRegisterAttributes(xmlSchemaValidCtxtPtr ctxt, xmlAttrPtr attrs) |
| 5173 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5174 | while (attrs != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5175 | if ((attrs->ns != NULL) && |
| 5176 | (xmlStrEqual(attrs->ns->href, xmlSchemaInstanceNs))) { |
| 5177 | attrs = attrs->next; |
| 5178 | continue; |
| 5179 | } |
| 5180 | if (ctxt->attrNr >= ctxt->attrMax) { |
| 5181 | xmlSchemaAttrStatePtr tmp; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5182 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5183 | ctxt->attrMax *= 2; |
| 5184 | tmp = (xmlSchemaAttrStatePtr) |
| 5185 | xmlRealloc(ctxt->attr, ctxt->attrMax * |
| 5186 | sizeof(xmlSchemaAttrState)); |
| 5187 | if (tmp == NULL) { |
| 5188 | xmlSchemaVErrMemory(ctxt, "registering attributes", NULL); |
| 5189 | ctxt->attrMax /= 2; |
| 5190 | return (-1); |
| 5191 | } |
| 5192 | ctxt->attr = tmp; |
| 5193 | } |
| 5194 | ctxt->attr[ctxt->attrNr].attr = attrs; |
| 5195 | ctxt->attr[ctxt->attrNr].state = XML_SCHEMAS_ATTR_UNKNOWN; |
| 5196 | ctxt->attrNr++; |
| 5197 | attrs = attrs->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5198 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5199 | return (0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5200 | } |
| 5201 | |
| 5202 | /** |
| 5203 | * xmlSchemaCheckAttributes: |
| 5204 | * @ctxt: a schema validation context |
| 5205 | * @node: the node carrying it. |
| 5206 | * |
| 5207 | * Check that the registered set of attributes on the current node |
| 5208 | * has been properly validated. |
| 5209 | * |
| 5210 | * Returns 0 if validity constraints are met, 1 otherwise. |
| 5211 | */ |
| 5212 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5213 | xmlSchemaCheckAttributes(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5214 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5215 | int ret = 0; |
| 5216 | int i; |
| 5217 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5218 | for (i = ctxt->attrBase; i < ctxt->attrNr; i++) { |
| 5219 | if (ctxt->attr[i].attr == NULL) |
| 5220 | break; |
| 5221 | if (ctxt->attr[i].state == XML_SCHEMAS_ATTR_UNKNOWN) { |
| 5222 | ret = 1; |
| 5223 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_ATTRUNKNOWN, "Attribute %s on %s is unknown\n", ctxt->attr[i].attr->name, node->name); |
| 5224 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5225 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5226 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5227 | } |
| 5228 | |
| 5229 | /** |
| 5230 | * xmlSchemaValidateSimpleContent: |
| 5231 | * @ctxt: a schema validation context |
| 5232 | * @elem: an element |
| 5233 | * @type: the type declaration |
| 5234 | * |
| 5235 | * Validate the content of an element expected to be a simple type |
| 5236 | * |
| 5237 | * Returns 0 if the element is schemas valid, a positive error code |
| 5238 | * number otherwise and -1 in case of internal or API error. |
| 5239 | */ |
| 5240 | static int |
| 5241 | xmlSchemaValidateSimpleContent(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5242 | xmlNodePtr node ATTRIBUTE_UNUSED) |
| 5243 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5244 | xmlNodePtr child; |
| 5245 | xmlSchemaTypePtr type, base; |
| 5246 | xmlChar *value; |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5247 | int ret = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5248 | |
| 5249 | child = ctxt->node; |
| 5250 | type = ctxt->type; |
| 5251 | |
| 5252 | /* |
| 5253 | * Validation Rule: Element Locally Valid (Type): 3.1.3 |
| 5254 | */ |
| 5255 | value = xmlNodeGetContent(child); |
| 5256 | /* xmlSchemaValidateSimpleValue(ctxt, type, value); */ |
| 5257 | switch (type->type) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5258 | case XML_SCHEMA_TYPE_RESTRICTION:{ |
| 5259 | xmlSchemaFacetPtr facet; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5260 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5261 | base = type->baseType; |
| 5262 | if (base != NULL) { |
| 5263 | ret = xmlSchemaValidateSimpleValue(ctxt, base, value); |
| 5264 | } else { |
| 5265 | TODO} |
| 5266 | if (ret == 0) { |
| 5267 | facet = type->facets; |
| 5268 | ret = |
| 5269 | xmlSchemaValidateFacets(ctxt, base, facet, value); |
| 5270 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 5271 | if ((ret == 0) && (type->attributes != NULL)) { |
| 5272 | ret = xmlSchemaValidateAttributes(ctxt, node, |
| 5273 | type->attributes); |
| 5274 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5275 | break; |
| 5276 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5277 | case XML_SCHEMA_TYPE_EXTENSION:{ |
| 5278 | TODO |
| 5279 | break; |
| 5280 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5281 | default: |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5282 | TODO |
| 5283 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5284 | if (value != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5285 | xmlFree(value); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5286 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5287 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5288 | } |
| 5289 | |
| 5290 | /** |
| 5291 | * xmlSchemaValidateCheckNodeList |
| 5292 | * @nodelist: the list of nodes |
| 5293 | * |
| 5294 | * Check the node list is only made of text nodes and entities pointing |
| 5295 | * to text nodes |
| 5296 | * |
| 5297 | * Returns 1 if true, 0 if false and -1 in case of error |
| 5298 | */ |
| 5299 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5300 | xmlSchemaValidateCheckNodeList(xmlNodePtr nodelist) |
| 5301 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5302 | while (nodelist != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5303 | if (nodelist->type == XML_ENTITY_REF_NODE) { |
| 5304 | TODO /* implement recursion in the entity content */ |
| 5305 | } |
| 5306 | if ((nodelist->type != XML_TEXT_NODE) && |
| 5307 | (nodelist->type != XML_COMMENT_NODE) && |
| 5308 | (nodelist->type != XML_PI_NODE) && |
| 5309 | (nodelist->type != XML_PI_NODE)) { |
| 5310 | return (0); |
| 5311 | } |
| 5312 | nodelist = nodelist->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5313 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5314 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5315 | } |
| 5316 | |
| 5317 | /** |
| 5318 | * xmlSchemaSkipIgnored: |
| 5319 | * @ctxt: a schema validation context |
| 5320 | * @type: the current type context |
| 5321 | * @node: the top node. |
| 5322 | * |
| 5323 | * Skip ignorable nodes in that context |
| 5324 | * |
| 5325 | * Returns the new sibling |
| 5326 | * number otherwise and -1 in case of internal or API error. |
| 5327 | */ |
| 5328 | static xmlNodePtr |
Daniel Veillard | dda8f1b | 2002-09-26 09:47:36 +0000 | [diff] [blame] | 5329 | xmlSchemaSkipIgnored(xmlSchemaValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5330 | xmlSchemaTypePtr type, xmlNodePtr node) |
| 5331 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5332 | int mixed = 0; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5333 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5334 | /* |
| 5335 | * TODO complete and handle entities |
| 5336 | */ |
| 5337 | mixed = ((type->contentType == XML_SCHEMA_CONTENT_MIXED) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5338 | (type->contentType == XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5339 | while ((node != NULL) && |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5340 | ((node->type == XML_COMMENT_NODE) || |
| 5341 | ((mixed == 1) && (node->type == XML_TEXT_NODE)) || |
| 5342 | (((type->contentType == XML_SCHEMA_CONTENT_ELEMENTS) && |
| 5343 | (node->type == XML_TEXT_NODE) && (IS_BLANK_NODE(node)))))) { |
| 5344 | node = node->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5345 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5346 | return (node); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5347 | } |
| 5348 | |
| 5349 | /** |
| 5350 | * xmlSchemaValidateCallback: |
| 5351 | * @ctxt: a schema validation context |
| 5352 | * @name: the name of the element detected (might be NULL) |
| 5353 | * @type: the type |
| 5354 | * |
| 5355 | * A transition has been made in the automata associated to an element |
| 5356 | * content model |
| 5357 | */ |
| 5358 | static void |
| 5359 | xmlSchemaValidateCallback(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5360 | const xmlChar * name ATTRIBUTE_UNUSED, |
| 5361 | xmlSchemaTypePtr type, xmlNodePtr node) |
| 5362 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5363 | xmlSchemaTypePtr oldtype = ctxt->type; |
| 5364 | xmlNodePtr oldnode = ctxt->node; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5365 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5366 | #ifdef DEBUG_CONTENT |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 5367 | xmlGenericError(xmlGenericErrorContext, |
| 5368 | "xmlSchemaValidateCallback: %s, %s, %s\n", |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5369 | name, type->name, node->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5370 | #endif |
| 5371 | ctxt->type = type; |
| 5372 | ctxt->node = node; |
| 5373 | xmlSchemaValidateContent(ctxt, node); |
| 5374 | ctxt->type = oldtype; |
| 5375 | ctxt->node = oldnode; |
| 5376 | } |
| 5377 | |
| 5378 | |
| 5379 | #if 0 |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5380 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5381 | /** |
| 5382 | * xmlSchemaValidateSimpleRestrictionType: |
| 5383 | * @ctxt: a schema validation context |
| 5384 | * @node: the top node. |
| 5385 | * |
| 5386 | * Validate the content of a restriction type. |
| 5387 | * |
| 5388 | * Returns 0 if the element is schemas valid, a positive error code |
| 5389 | * number otherwise and -1 in case of internal or API error. |
| 5390 | */ |
| 5391 | static int |
| 5392 | xmlSchemaValidateSimpleRestrictionType(xmlSchemaValidCtxtPtr ctxt, |
| 5393 | xmlNodePtr node) |
| 5394 | { |
| 5395 | xmlNodePtr child; |
| 5396 | xmlSchemaTypePtr type; |
| 5397 | int ret; |
| 5398 | |
| 5399 | child = ctxt->node; |
| 5400 | type = ctxt->type; |
| 5401 | |
| 5402 | if ((ctxt == NULL) || (type == NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5403 | 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] | 5404 | return (-1); |
| 5405 | } |
| 5406 | /* |
| 5407 | * Only text and text based entities references shall be found there |
| 5408 | */ |
| 5409 | ret = xmlSchemaValidateCheckNodeList(child); |
| 5410 | if (ret < 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5411 | 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] | 5412 | return (-1); |
| 5413 | } else if (ret == 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5414 | 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] | 5415 | return (-1); |
| 5416 | } |
| 5417 | ctxt->type = type->subtypes; |
| 5418 | xmlSchemaValidateContent(ctxt, node); |
| 5419 | ctxt->type = type; |
| 5420 | return (ret); |
| 5421 | } |
| 5422 | #endif |
| 5423 | |
| 5424 | /** |
| 5425 | * xmlSchemaValidateSimpleType: |
| 5426 | * @ctxt: a schema validation context |
| 5427 | * @node: the top node. |
| 5428 | * |
| 5429 | * Validate the content of an simple type. |
| 5430 | * |
| 5431 | * Returns 0 if the element is schemas valid, a positive error code |
| 5432 | * number otherwise and -1 in case of internal or API error. |
| 5433 | */ |
| 5434 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5435 | xmlSchemaValidateSimpleType(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5436 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5437 | xmlNodePtr child; |
| 5438 | xmlSchemaTypePtr type; |
| 5439 | xmlAttrPtr attr; |
| 5440 | int ret; |
| 5441 | |
| 5442 | child = ctxt->node; |
| 5443 | type = ctxt->type; |
| 5444 | |
| 5445 | if ((ctxt == NULL) || (type == NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5446 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INTERNAL, "Internal error: xmlSchemaValidateSimpleType %s\n", node->name, NULL); |
| 5447 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5448 | } |
| 5449 | /* |
| 5450 | * Only text and text based entities references shall be found there |
| 5451 | */ |
| 5452 | ret = xmlSchemaValidateCheckNodeList(child); |
| 5453 | if (ret < 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5454 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INTERNAL, "Internal error: xmlSchemaValidateSimpleType %s content\n", node->name, NULL); |
| 5455 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5456 | } else if (ret == 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5457 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_NOTSIMPLE, "Element %s content is not a simple type\n", node->name, NULL); |
| 5458 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5459 | } |
| 5460 | /* |
| 5461 | * Validation Rule: Element Locally Valid (Type): 3.1.1 |
| 5462 | */ |
| 5463 | attr = node->properties; |
| 5464 | while (attr != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5465 | if ((attr->ns == NULL) || |
| 5466 | (!xmlStrEqual(attr->ns->href, xmlSchemaInstanceNs)) || |
| 5467 | ((!xmlStrEqual(attr->name, BAD_CAST "type")) && |
| 5468 | (!xmlStrEqual(attr->name, BAD_CAST "nil")) && |
| 5469 | (!xmlStrEqual(attr->name, BAD_CAST "schemasLocation")) && |
| 5470 | (!xmlStrEqual |
| 5471 | (attr->name, BAD_CAST "noNamespaceSchemaLocation")))) { |
| 5472 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INVALIDATTR, "Element %s: attribute %s should not be present\n", node->name, attr->name); |
| 5473 | return (ctxt->err); |
| 5474 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5475 | } |
| 5476 | |
| 5477 | ctxt->type = type->subtypes; |
| 5478 | ret = xmlSchemaValidateSimpleContent(ctxt, node); |
| 5479 | ctxt->type = type; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5480 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5481 | } |
| 5482 | |
| 5483 | /** |
| 5484 | * xmlSchemaValidateElementType: |
| 5485 | * @ctxt: a schema validation context |
| 5486 | * @node: the top node. |
| 5487 | * |
| 5488 | * Validate the content of an element type. |
| 5489 | * Validation Rule: Element Locally Valid (Complex Type) |
| 5490 | * |
| 5491 | * Returns 0 if the element is schemas valid, a positive error code |
| 5492 | * number otherwise and -1 in case of internal or API error. |
| 5493 | */ |
| 5494 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5495 | xmlSchemaValidateElementType(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5496 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5497 | xmlNodePtr child; |
| 5498 | xmlSchemaTypePtr type; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5499 | xmlRegExecCtxtPtr oldregexp; /* cont model of the parent */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5500 | xmlSchemaElementPtr decl; |
| 5501 | int ret, attrBase; |
| 5502 | |
| 5503 | oldregexp = ctxt->regexp; |
| 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: xmlSchemaValidateElementType\n", node->name, NULL); |
| 5510 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5511 | } |
| 5512 | if (child == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5513 | if (type->minOccurs > 0) { |
| 5514 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_MISSING, "Element %s: missing child %s\n", node->name, type->name); |
| 5515 | } |
| 5516 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5517 | } |
| 5518 | |
| 5519 | /* |
| 5520 | * Verify the element matches |
| 5521 | */ |
| 5522 | if (!xmlStrEqual(child->name, type->name)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5523 | xmlSchemaVErr3(ctxt, node, XML_SCHEMAS_ERR_WRONGELEM, "Element %s: missing child %s found %s\n", node->name, type->name, child->name); |
| 5524 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5525 | } |
| 5526 | /* |
| 5527 | * Verify the attributes |
| 5528 | */ |
| 5529 | attrBase = ctxt->attrBase; |
| 5530 | ctxt->attrBase = ctxt->attrNr; |
| 5531 | xmlSchemaRegisterAttributes(ctxt, child->properties); |
| 5532 | xmlSchemaValidateAttributes(ctxt, child, type->attributes); |
| 5533 | /* |
| 5534 | * Verify the element content recursively |
| 5535 | */ |
| 5536 | decl = (xmlSchemaElementPtr) type; |
| 5537 | oldregexp = ctxt->regexp; |
| 5538 | if (decl->contModel != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5539 | ctxt->regexp = xmlRegNewExecCtxt(decl->contModel, |
| 5540 | (xmlRegExecCallbacks) |
| 5541 | xmlSchemaValidateCallback, ctxt); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5542 | #ifdef DEBUG_AUTOMATA |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5543 | xmlGenericError(xmlGenericErrorContext, "====> %s\n", node->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5544 | #endif |
| 5545 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5546 | xmlSchemaValidateType(ctxt, child, (xmlSchemaElementPtr) type, |
| 5547 | type->subtypes); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5548 | |
| 5549 | if (decl->contModel != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5550 | ret = xmlRegExecPushString(ctxt->regexp, NULL, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5551 | #ifdef DEBUG_AUTOMATA |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5552 | xmlGenericError(xmlGenericErrorContext, |
| 5553 | "====> %s : %d\n", node->name, ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5554 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5555 | if (ret == 0) { |
| 5556 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_ELEMCONT, "Element %s content check failed\n", node->name, NULL); |
| 5557 | } else if (ret < 0) { |
| 5558 | 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] | 5559 | #ifdef DEBUG_CONTENT |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5560 | } else { |
| 5561 | xmlGenericError(xmlGenericErrorContext, |
| 5562 | "Element %s content check succeeded\n", |
| 5563 | node->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5564 | |
| 5565 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5566 | } |
| 5567 | xmlRegFreeExecCtxt(ctxt->regexp); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5568 | } |
| 5569 | /* |
| 5570 | * Verify that all attributes were Schemas-validated |
| 5571 | */ |
| 5572 | xmlSchemaCheckAttributes(ctxt, node); |
| 5573 | ctxt->attrNr = ctxt->attrBase; |
| 5574 | ctxt->attrBase = attrBase; |
| 5575 | |
| 5576 | ctxt->regexp = oldregexp; |
| 5577 | |
| 5578 | ctxt->node = child; |
| 5579 | ctxt->type = type; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5580 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5581 | } |
| 5582 | |
| 5583 | /** |
| 5584 | * xmlSchemaValidateBasicType: |
| 5585 | * @ctxt: a schema validation context |
| 5586 | * @node: the top node. |
| 5587 | * |
| 5588 | * Validate the content of an element expected to be a basic type type |
| 5589 | * |
| 5590 | * Returns 0 if the element is schemas valid, a positive error code |
| 5591 | * number otherwise and -1 in case of internal or API error. |
| 5592 | */ |
| 5593 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5594 | xmlSchemaValidateBasicType(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5595 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5596 | int ret; |
| 5597 | xmlNodePtr child, cur; |
| 5598 | xmlSchemaTypePtr type; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5599 | xmlChar *value; /* lexical representation */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5600 | |
| 5601 | child = ctxt->node; |
| 5602 | type = ctxt->type; |
| 5603 | |
| 5604 | if ((ctxt == NULL) || (type == NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5605 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INTERNAL, "Internal error: xmlSchemaValidateBasicType\n", node->name, NULL); |
| 5606 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5607 | } |
| 5608 | /* |
| 5609 | * First check the content model of the node. |
| 5610 | */ |
| 5611 | cur = child; |
| 5612 | while (cur != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5613 | switch (cur->type) { |
| 5614 | case XML_TEXT_NODE: |
| 5615 | case XML_CDATA_SECTION_NODE: |
| 5616 | case XML_PI_NODE: |
| 5617 | case XML_COMMENT_NODE: |
| 5618 | case XML_XINCLUDE_START: |
| 5619 | case XML_XINCLUDE_END: |
| 5620 | break; |
| 5621 | case XML_ENTITY_REF_NODE: |
| 5622 | case XML_ENTITY_NODE: |
| 5623 | TODO break; |
| 5624 | case XML_ELEMENT_NODE: |
| 5625 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INVALIDELEM, "Element %s: child %s should not be present\n", node->name, cur->name); |
| 5626 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5627 | case XML_ATTRIBUTE_NODE: |
| 5628 | case XML_DOCUMENT_NODE: |
| 5629 | case XML_DOCUMENT_TYPE_NODE: |
| 5630 | case XML_DOCUMENT_FRAG_NODE: |
| 5631 | case XML_NOTATION_NODE: |
| 5632 | case XML_HTML_DOCUMENT_NODE: |
| 5633 | case XML_DTD_NODE: |
| 5634 | case XML_ELEMENT_DECL: |
| 5635 | case XML_ATTRIBUTE_DECL: |
| 5636 | case XML_ENTITY_DECL: |
| 5637 | case XML_NAMESPACE_DECL: |
| 5638 | #ifdef LIBXML_DOCB_ENABLED |
| 5639 | case XML_DOCB_DOCUMENT_NODE: |
| 5640 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5641 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INVALIDELEM, "Element %s: node type of node unexpected here\n", node->name, NULL); |
| 5642 | return (ctxt->err); |
| 5643 | } |
| 5644 | cur = cur->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5645 | } |
| 5646 | if (child == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5647 | value = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5648 | else |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5649 | value = xmlNodeGetContent(child->parent); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5650 | |
| 5651 | if (ctxt->value != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5652 | xmlSchemaFreeValue(ctxt->value); |
| 5653 | ctxt->value = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5654 | } |
| 5655 | ret = xmlSchemaValidatePredefinedType(type, value, &(ctxt->value)); |
| 5656 | if (value != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5657 | xmlFree(value); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5658 | if (ret != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5659 | 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] | 5660 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5661 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5662 | } |
| 5663 | |
| 5664 | /** |
| 5665 | * xmlSchemaValidateComplexType: |
| 5666 | * @ctxt: a schema validation context |
| 5667 | * @node: the top node. |
| 5668 | * |
| 5669 | * Validate the content of an element expected to be a complex type type |
| 5670 | * xmlschema-1.html#cvc-complex-type |
| 5671 | * Validation Rule: Element Locally Valid (Complex Type) |
| 5672 | * |
| 5673 | * Returns 0 if the element is schemas valid, a positive error code |
| 5674 | * number otherwise and -1 in case of internal or API error. |
| 5675 | */ |
| 5676 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5677 | xmlSchemaValidateComplexType(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5678 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5679 | xmlNodePtr child; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 5680 | xmlSchemaTypePtr type, subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5681 | int ret; |
| 5682 | |
| 5683 | child = ctxt->node; |
| 5684 | type = ctxt->type; |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 5685 | ctxt->cur = node; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5686 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5687 | switch (type->contentType) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5688 | case XML_SCHEMA_CONTENT_EMPTY: |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5689 | if (type->baseType != NULL) { |
| 5690 | } else if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5691 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_NOTEMPTY, "Element %s is supposed to be empty\n", node->name, NULL); |
| 5692 | } |
| 5693 | if (type->attributes != NULL) { |
| 5694 | xmlSchemaValidateAttributes(ctxt, node, type->attributes); |
| 5695 | } |
| 5696 | subtype = type->subtypes; |
| 5697 | while (subtype != NULL) { |
| 5698 | ctxt->type = subtype; |
| 5699 | xmlSchemaValidateComplexType(ctxt, node); |
| 5700 | subtype = subtype->next; |
| 5701 | } |
| 5702 | break; |
| 5703 | case XML_SCHEMA_CONTENT_ELEMENTS: |
| 5704 | case XML_SCHEMA_CONTENT_MIXED: |
| 5705 | case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS: |
| 5706 | /* |
| 5707 | * Skip ignorable nodes in that context |
| 5708 | */ |
| 5709 | child = xmlSchemaSkipIgnored(ctxt, type, child); |
| 5710 | while (child != NULL) { |
| 5711 | if (child->type == XML_ELEMENT_NODE) { |
| 5712 | ret = xmlRegExecPushString(ctxt->regexp, |
| 5713 | child->name, child); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5714 | #ifdef DEBUG_AUTOMATA |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5715 | if (ret < 0) |
| 5716 | xmlGenericError(xmlGenericErrorContext, |
| 5717 | " --> %s Error\n", child->name); |
| 5718 | else |
| 5719 | xmlGenericError(xmlGenericErrorContext, |
| 5720 | " --> %s\n", child->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5721 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5722 | } |
| 5723 | child = child->next; |
| 5724 | /* |
| 5725 | * Skip ignorable nodes in that context |
| 5726 | */ |
| 5727 | child = xmlSchemaSkipIgnored(ctxt, type, child); |
| 5728 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 5729 | if (type->attributes != NULL) { |
| 5730 | xmlSchemaValidateAttributes(ctxt, node, type->attributes); |
| 5731 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5732 | break; |
| 5733 | case XML_SCHEMA_CONTENT_BASIC:{ |
| 5734 | if (type->subtypes != NULL) { |
| 5735 | ctxt->type = type->subtypes; |
| 5736 | xmlSchemaValidateComplexType(ctxt, node); |
| 5737 | } |
| 5738 | if (type->baseType != NULL) { |
| 5739 | ctxt->type = type->baseType; |
| 5740 | xmlSchemaValidateBasicType(ctxt, node); |
| 5741 | } |
| 5742 | if (type->attributes != NULL) { |
| 5743 | xmlSchemaValidateAttributes(ctxt, node, |
| 5744 | type->attributes); |
| 5745 | } |
| 5746 | ctxt->type = type; |
| 5747 | break; |
| 5748 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5749 | case XML_SCHEMA_CONTENT_SIMPLE:{ |
| 5750 | if (type->subtypes != NULL) { |
| 5751 | ctxt->type = type->subtypes; |
| 5752 | xmlSchemaValidateComplexType(ctxt, node); |
| 5753 | } |
| 5754 | if (type->baseType != NULL) { |
| 5755 | ctxt->type = type->baseType; |
| 5756 | xmlSchemaValidateComplexType(ctxt, node); |
| 5757 | } |
| 5758 | if (type->attributes != NULL) { |
| 5759 | xmlSchemaValidateAttributes(ctxt, node, |
| 5760 | type->attributes); |
| 5761 | } |
| 5762 | ctxt->type = type; |
| 5763 | break; |
| 5764 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5765 | default: |
| 5766 | TODO xmlGenericError(xmlGenericErrorContext, |
| 5767 | "unimplemented content type %d\n", |
| 5768 | type->contentType); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5769 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5770 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5771 | } |
| 5772 | |
| 5773 | /** |
| 5774 | * xmlSchemaValidateContent: |
| 5775 | * @ctxt: a schema validation context |
| 5776 | * @elem: an element |
| 5777 | * @type: the type declaration |
| 5778 | * |
| 5779 | * Validate the content of an element against the type. |
| 5780 | * |
| 5781 | * Returns 0 if the element is schemas valid, a positive error code |
| 5782 | * number otherwise and -1 in case of internal or API error. |
| 5783 | */ |
| 5784 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5785 | xmlSchemaValidateContent(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5786 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5787 | xmlNodePtr child; |
| 5788 | xmlSchemaTypePtr type; |
| 5789 | |
| 5790 | child = ctxt->node; |
| 5791 | type = ctxt->type; |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 5792 | ctxt->cur = node; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5793 | |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 5794 | xmlSchemaValidateAttributes(ctxt, node, type->attributes); |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 5795 | ctxt->cur = node; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 5796 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5797 | switch (type->type) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5798 | case XML_SCHEMA_TYPE_ANY: |
| 5799 | /* Any type will do it, fine */ |
| 5800 | TODO /* handle recursivity */ |
| 5801 | break; |
| 5802 | case XML_SCHEMA_TYPE_COMPLEX: |
| 5803 | xmlSchemaValidateComplexType(ctxt, node); |
| 5804 | break; |
| 5805 | case XML_SCHEMA_TYPE_ELEMENT:{ |
| 5806 | xmlSchemaElementPtr decl = (xmlSchemaElementPtr) type; |
| 5807 | |
| 5808 | /* |
| 5809 | * Handle element reference here |
| 5810 | */ |
| 5811 | if (decl->ref != NULL) { |
| 5812 | if (decl->refDecl == NULL) { |
| 5813 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INTERNAL, "Internal error: element reference %s not resolved\n", decl->ref, NULL); |
| 5814 | return (-1); |
| 5815 | } |
| 5816 | ctxt->type = (xmlSchemaTypePtr) decl->refDecl; |
| 5817 | decl = decl->refDecl; |
| 5818 | } |
| 5819 | xmlSchemaValidateElementType(ctxt, node); |
| 5820 | ctxt->type = type; |
| 5821 | break; |
| 5822 | } |
| 5823 | case XML_SCHEMA_TYPE_BASIC: |
| 5824 | xmlSchemaValidateBasicType(ctxt, node); |
| 5825 | break; |
| 5826 | case XML_SCHEMA_TYPE_FACET: |
| 5827 | TODO break; |
| 5828 | case XML_SCHEMA_TYPE_SIMPLE: |
| 5829 | xmlSchemaValidateSimpleType(ctxt, node); |
| 5830 | break; |
| 5831 | case XML_SCHEMA_TYPE_SEQUENCE: |
| 5832 | TODO break; |
| 5833 | case XML_SCHEMA_TYPE_CHOICE: |
| 5834 | TODO break; |
| 5835 | case XML_SCHEMA_TYPE_ALL: |
| 5836 | TODO break; |
| 5837 | case XML_SCHEMA_TYPE_SIMPLE_CONTENT: |
| 5838 | TODO break; |
| 5839 | case XML_SCHEMA_TYPE_COMPLEX_CONTENT: |
| 5840 | TODO break; |
| 5841 | case XML_SCHEMA_TYPE_UR: |
| 5842 | TODO break; |
| 5843 | case XML_SCHEMA_TYPE_RESTRICTION: |
| 5844 | /*xmlSchemaValidateRestrictionType(ctxt, node); */ |
| 5845 | TODO break; |
| 5846 | case XML_SCHEMA_TYPE_EXTENSION: |
| 5847 | TODO break; |
| 5848 | case XML_SCHEMA_TYPE_ATTRIBUTE: |
| 5849 | TODO break; |
| 5850 | case XML_SCHEMA_TYPE_GROUP: |
| 5851 | TODO break; |
| 5852 | case XML_SCHEMA_TYPE_NOTATION: |
| 5853 | TODO break; |
| 5854 | case XML_SCHEMA_TYPE_LIST: |
| 5855 | TODO break; |
| 5856 | case XML_SCHEMA_TYPE_UNION: |
| 5857 | TODO break; |
| 5858 | case XML_SCHEMA_FACET_MININCLUSIVE: |
| 5859 | TODO break; |
| 5860 | case XML_SCHEMA_FACET_MINEXCLUSIVE: |
| 5861 | TODO break; |
| 5862 | case XML_SCHEMA_FACET_MAXINCLUSIVE: |
| 5863 | TODO break; |
| 5864 | case XML_SCHEMA_FACET_MAXEXCLUSIVE: |
| 5865 | TODO break; |
| 5866 | case XML_SCHEMA_FACET_TOTALDIGITS: |
| 5867 | TODO break; |
| 5868 | case XML_SCHEMA_FACET_FRACTIONDIGITS: |
| 5869 | TODO break; |
| 5870 | case XML_SCHEMA_FACET_PATTERN: |
| 5871 | TODO break; |
| 5872 | case XML_SCHEMA_FACET_ENUMERATION: |
| 5873 | TODO break; |
| 5874 | case XML_SCHEMA_FACET_WHITESPACE: |
| 5875 | TODO break; |
| 5876 | case XML_SCHEMA_FACET_LENGTH: |
| 5877 | TODO break; |
| 5878 | case XML_SCHEMA_FACET_MAXLENGTH: |
| 5879 | TODO break; |
| 5880 | case XML_SCHEMA_FACET_MINLENGTH: |
| 5881 | TODO break; |
| 5882 | case XML_SCHEMA_TYPE_ATTRIBUTEGROUP: |
| 5883 | TODO break; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5884 | } |
| 5885 | xmlSchemaValidateAttributes(ctxt, node, type->attributes); |
| 5886 | |
| 5887 | if (ctxt->node == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5888 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5889 | ctxt->node = ctxt->node->next; |
| 5890 | ctxt->type = type->next; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5891 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5892 | } |
| 5893 | |
| 5894 | /** |
| 5895 | * xmlSchemaValidateType: |
| 5896 | * @ctxt: a schema validation context |
| 5897 | * @elem: an element |
| 5898 | * @type: the list of type declarations |
| 5899 | * |
| 5900 | * Validate the content of an element against the types. |
| 5901 | * |
| 5902 | * Returns 0 if the element is schemas valid, a positive error code |
| 5903 | * number otherwise and -1 in case of internal or API error. |
| 5904 | */ |
| 5905 | static int |
| 5906 | xmlSchemaValidateType(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5907 | xmlSchemaElementPtr elemDecl, xmlSchemaTypePtr type) |
| 5908 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5909 | xmlChar *nil; |
| 5910 | |
Daniel Veillard | 2db8c12 | 2003-07-08 12:16:59 +0000 | [diff] [blame] | 5911 | if ((elem == NULL) || (type == NULL) || (elemDecl == NULL)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5912 | return (0); |
Daniel Veillard | 2db8c12 | 2003-07-08 12:16:59 +0000 | [diff] [blame] | 5913 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5914 | /* |
| 5915 | * 3.3.4 : 2 |
| 5916 | */ |
| 5917 | if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5918 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_ISABSTRACT, "Element %s is abstract\n", elem->name, NULL); |
| 5919 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5920 | } |
| 5921 | /* |
| 5922 | * 3.3.4: 3 |
| 5923 | */ |
| 5924 | nil = xmlGetNsProp(elem, BAD_CAST "nil", xmlSchemaInstanceNs); |
| 5925 | if (elemDecl->flags & XML_SCHEMAS_ELEM_NILLABLE) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5926 | /* 3.3.4: 3.2 */ |
| 5927 | if (xmlStrEqual(nil, BAD_CAST "true")) { |
| 5928 | if (elem->children != NULL) { |
| 5929 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_NOTEMPTY, "Element %s is not empty\n", elem->name, NULL); |
| 5930 | return (ctxt->err); |
| 5931 | } |
| 5932 | if ((elemDecl->flags & XML_SCHEMAS_ELEM_FIXED) && |
| 5933 | (elemDecl->value != NULL)) { |
| 5934 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_HAVEDEFAULT, "Empty element %s cannot get a fixed value\n", elem->name, NULL); |
| 5935 | return (ctxt->err); |
| 5936 | } |
| 5937 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5938 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5939 | /* 3.3.4: 3.1 */ |
| 5940 | if (nil != NULL) { |
| 5941 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_NOTNILLABLE, "Element %s with xs:nil but not nillable\n", elem->name, NULL); |
| 5942 | xmlFree(nil); |
| 5943 | return (ctxt->err); |
| 5944 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5945 | } |
| 5946 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5947 | /* TODO 3.3.4: 4 if the element carries xs:type */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5948 | |
| 5949 | ctxt->type = elemDecl->subtypes; |
| 5950 | ctxt->node = elem->children; |
| 5951 | xmlSchemaValidateContent(ctxt, elem); |
| 5952 | xmlSchemaValidateAttributes(ctxt, elem, elemDecl->attributes); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5953 | |
| 5954 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5955 | } |
| 5956 | |
| 5957 | |
| 5958 | /** |
| 5959 | * xmlSchemaValidateAttributes: |
| 5960 | * @ctxt: a schema validation context |
| 5961 | * @elem: an element |
| 5962 | * @attributes: the list of attribute declarations |
| 5963 | * |
| 5964 | * Validate the attributes of an element. |
| 5965 | * |
| 5966 | * Returns 0 if the element is schemas valid, a positive error code |
| 5967 | * number otherwise and -1 in case of internal or API error. |
| 5968 | */ |
| 5969 | static int |
| 5970 | xmlSchemaValidateAttributes(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5971 | xmlSchemaAttributePtr attributes) |
| 5972 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5973 | int i, ret; |
| 5974 | xmlAttrPtr attr; |
| 5975 | xmlChar *value; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 5976 | xmlSchemaAttributeGroupPtr group = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5977 | |
| 5978 | if (attributes == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5979 | return (0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5980 | while (attributes != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5981 | /* |
| 5982 | * Handle attribute groups |
| 5983 | */ |
| 5984 | if (attributes->type == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) { |
| 5985 | group = (xmlSchemaAttributeGroupPtr) attributes; |
| 5986 | xmlSchemaValidateAttributes(ctxt, elem, group->attributes); |
| 5987 | attributes = group->next; |
| 5988 | continue; |
| 5989 | } |
| 5990 | for (i = ctxt->attrBase; i < ctxt->attrNr; i++) { |
| 5991 | attr = ctxt->attr[i].attr; |
| 5992 | if (attr == NULL) |
| 5993 | continue; |
| 5994 | if (attributes->ref != NULL) { |
| 5995 | if (!xmlStrEqual(attr->name, attributes->ref)) |
| 5996 | continue; |
| 5997 | if (attr->ns != NULL) { |
| 5998 | if ((attributes->refNs == NULL) || |
| 5999 | (!xmlStrEqual(attr->ns->href, attributes->refNs))) |
| 6000 | continue; |
| 6001 | } else if (attributes->refNs != NULL) { |
| 6002 | continue; |
| 6003 | } |
| 6004 | } else { |
| 6005 | if (!xmlStrEqual(attr->name, attributes->name)) |
| 6006 | continue; |
| 6007 | /* |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6008 | * handle the namespaces checks here |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6009 | */ |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6010 | if (attr->ns == NULL) { |
| 6011 | /* |
| 6012 | * accept an unqualified attribute only if the declaration |
| 6013 | * is unqualified or if the schemas allowed it. |
| 6014 | */ |
| 6015 | if ((attributes->targetNamespace != NULL) && |
| 6016 | ((attributes->flags & XML_SCHEMAS_ATTR_NSDEFAULT) == 0)) |
| 6017 | continue; |
| 6018 | } else { |
| 6019 | if (attributes->targetNamespace == NULL) |
| 6020 | continue; |
| 6021 | if (!xmlStrEqual(attributes->targetNamespace, |
| 6022 | attr->ns->href)) |
| 6023 | continue; |
| 6024 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6025 | } |
| 6026 | ctxt->cur = (xmlNodePtr) attributes; |
| 6027 | if (attributes->subtypes == NULL) { |
| 6028 | xmlSchemaVErr(ctxt, (xmlNodePtr) attr, XML_SCHEMAS_ERR_INTERNAL, "Internal error: attribute %s type not resolved\n", attr->name, NULL); |
| 6029 | continue; |
| 6030 | } |
| 6031 | value = xmlNodeListGetString(elem->doc, attr->children, 1); |
| 6032 | ret = xmlSchemaValidateSimpleValue(ctxt, attributes->subtypes, |
| 6033 | value); |
| 6034 | if (ret != 0) { |
| 6035 | xmlSchemaVErr(ctxt, (xmlNodePtr) attr, XML_SCHEMAS_ERR_ATTRINVALID, "attribute %s on %s does not match type\n", attr->name, elem->name); |
| 6036 | } else { |
| 6037 | ctxt->attr[i].state = XML_SCHEMAS_ATTR_CHECKED; |
| 6038 | } |
| 6039 | if (value != NULL) { |
| 6040 | xmlFree(value); |
| 6041 | } |
| 6042 | } |
| 6043 | attributes = attributes->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6044 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6045 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6046 | } |
| 6047 | |
| 6048 | /** |
| 6049 | * xmlSchemaValidateElement: |
| 6050 | * @ctxt: a schema validation context |
| 6051 | * @elem: an element |
| 6052 | * |
| 6053 | * Validate an element in a tree |
| 6054 | * |
| 6055 | * Returns 0 if the element is schemas valid, a positive error code |
| 6056 | * number otherwise and -1 in case of internal or API error. |
| 6057 | */ |
| 6058 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6059 | xmlSchemaValidateElement(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem) |
| 6060 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6061 | xmlSchemaElementPtr elemDecl; |
| 6062 | int ret, attrBase; |
| 6063 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6064 | if (elem->ns != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6065 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6066 | elem->name, elem->ns->href, NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6067 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6068 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6069 | elem->name, NULL, NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6070 | } |
| 6071 | /* |
| 6072 | * special case whe elementFormDefault is unqualified for top-level elem. |
| 6073 | */ |
| 6074 | if ((elemDecl == NULL) && (elem->ns != NULL) && |
| 6075 | (elem->parent != NULL) && (elem->parent->type != XML_ELEMENT_NODE) && |
| 6076 | (xmlStrEqual(ctxt->schema->targetNamespace, elem->ns->href)) && |
| 6077 | ((ctxt->schema->flags & XML_SCHEMAS_QUALIF_ELEM) == 0)) { |
| 6078 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6079 | elem->name, NULL, NULL); |
| 6080 | } |
| 6081 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6082 | /* |
| 6083 | * 3.3.4 : 1 |
| 6084 | */ |
| 6085 | if (elemDecl == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6086 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_UNDECLAREDELEM, "Element %s not declared\n", elem->name, NULL); |
| 6087 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6088 | } |
| 6089 | if (elemDecl->subtypes == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6090 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_NOTYPE, "Element %s has no type\n", elem->name, NULL); |
| 6091 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6092 | } |
| 6093 | /* |
| 6094 | * Verify the attributes |
| 6095 | */ |
| 6096 | attrBase = ctxt->attrBase; |
| 6097 | ctxt->attrBase = ctxt->attrNr; |
| 6098 | xmlSchemaRegisterAttributes(ctxt, elem->properties); |
| 6099 | xmlSchemaValidateAttributes(ctxt, elem, elemDecl->attributes); |
| 6100 | /* |
| 6101 | * Verify the element content recursively |
| 6102 | */ |
| 6103 | if (elemDecl->contModel != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6104 | ctxt->regexp = xmlRegNewExecCtxt(elemDecl->contModel, |
| 6105 | (xmlRegExecCallbacks) |
| 6106 | xmlSchemaValidateCallback, ctxt); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6107 | #ifdef DEBUG_AUTOMATA |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6108 | xmlGenericError(xmlGenericErrorContext, "====> %s\n", elem->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6109 | #endif |
| 6110 | } |
| 6111 | xmlSchemaValidateType(ctxt, elem, elemDecl, elemDecl->subtypes); |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 6112 | if (elemDecl->contModel != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6113 | ret = xmlRegExecPushString(ctxt->regexp, NULL, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6114 | #ifdef DEBUG_AUTOMATA |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6115 | xmlGenericError(xmlGenericErrorContext, |
| 6116 | "====> %s : %d\n", elem->name, ret); |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 6117 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6118 | if (ret == 0) { |
| 6119 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_ELEMCONT, "Element %s content check failed\n", elem->name, NULL); |
| 6120 | } else if (ret < 0) { |
| 6121 | 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] | 6122 | #ifdef DEBUG_CONTENT |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6123 | } else { |
| 6124 | xmlGenericError(xmlGenericErrorContext, |
| 6125 | "Element %s content check succeeded\n", |
| 6126 | elem->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6127 | |
| 6128 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6129 | } |
| 6130 | xmlRegFreeExecCtxt(ctxt->regexp); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6131 | } |
| 6132 | /* |
| 6133 | * Verify that all attributes were Schemas-validated |
| 6134 | */ |
| 6135 | xmlSchemaCheckAttributes(ctxt, elem); |
| 6136 | ctxt->attrNr = ctxt->attrBase; |
| 6137 | ctxt->attrBase = attrBase; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6138 | |
| 6139 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6140 | } |
| 6141 | |
| 6142 | /** |
| 6143 | * xmlSchemaValidateDocument: |
| 6144 | * @ctxt: a schema validation context |
| 6145 | * @doc: a parsed document tree |
| 6146 | * |
| 6147 | * Validate a document tree in memory. |
| 6148 | * |
| 6149 | * Returns 0 if the document is schemas valid, a positive error code |
| 6150 | * number otherwise and -1 in case of internal or API error. |
| 6151 | */ |
| 6152 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6153 | xmlSchemaValidateDocument(xmlSchemaValidCtxtPtr ctxt, xmlDocPtr doc) |
| 6154 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6155 | xmlNodePtr root; |
| 6156 | xmlSchemaElementPtr elemDecl; |
| 6157 | |
| 6158 | root = xmlDocGetRootElement(doc); |
| 6159 | if (root == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6160 | xmlSchemaVErr(ctxt, (xmlNodePtr) doc, XML_SCHEMAS_ERR_NOROOT, "document has no root\n", NULL, NULL); |
| 6161 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6162 | } |
| 6163 | if (root->ns != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6164 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6165 | root->name, root->ns->href, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6166 | else |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6167 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6168 | root->name, NULL, NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6169 | /* |
| 6170 | * special case whe elementFormDefault is unqualified for top-level elem. |
| 6171 | */ |
| 6172 | if ((elemDecl == NULL) && (root->ns != NULL) && |
| 6173 | (xmlStrEqual(ctxt->schema->targetNamespace, root->ns->href)) && |
| 6174 | ((ctxt->schema->flags & XML_SCHEMAS_QUALIF_ELEM) == 0)) { |
| 6175 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6176 | root->name, NULL, NULL); |
| 6177 | } |
| 6178 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6179 | if (elemDecl == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6180 | 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] | 6181 | } else if ((elemDecl->flags & XML_SCHEMAS_ELEM_TOPLEVEL) == 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6182 | 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] | 6183 | } |
| 6184 | /* |
| 6185 | * Okay, start the recursive validation |
| 6186 | */ |
| 6187 | xmlSchemaValidateElement(ctxt, root); |
| 6188 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6189 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6190 | } |
| 6191 | |
| 6192 | /************************************************************************ |
| 6193 | * * |
| 6194 | * SAX Validation code * |
| 6195 | * * |
| 6196 | ************************************************************************/ |
| 6197 | |
| 6198 | /************************************************************************ |
| 6199 | * * |
| 6200 | * Validation interfaces * |
| 6201 | * * |
| 6202 | ************************************************************************/ |
| 6203 | |
| 6204 | /** |
| 6205 | * xmlSchemaNewValidCtxt: |
| 6206 | * @schema: a precompiled XML Schemas |
| 6207 | * |
| 6208 | * Create an XML Schemas validation context based on the given schema |
| 6209 | * |
| 6210 | * Returns the validation context or NULL in case of error |
| 6211 | */ |
| 6212 | xmlSchemaValidCtxtPtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6213 | xmlSchemaNewValidCtxt(xmlSchemaPtr schema) |
| 6214 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6215 | xmlSchemaValidCtxtPtr ret; |
| 6216 | |
| 6217 | ret = (xmlSchemaValidCtxtPtr) xmlMalloc(sizeof(xmlSchemaValidCtxt)); |
| 6218 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6219 | xmlSchemaVErrMemory(NULL, "allocating validation context", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6220 | return (NULL); |
| 6221 | } |
| 6222 | memset(ret, 0, sizeof(xmlSchemaValidCtxt)); |
| 6223 | ret->schema = schema; |
| 6224 | ret->attrNr = 0; |
| 6225 | ret->attrMax = 10; |
| 6226 | ret->attr = (xmlSchemaAttrStatePtr) xmlMalloc(ret->attrMax * |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6227 | sizeof |
| 6228 | (xmlSchemaAttrState)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6229 | if (ret->attr == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6230 | xmlSchemaVErrMemory(NULL, "allocating validation context", NULL); |
| 6231 | free(ret); |
| 6232 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6233 | } |
| 6234 | memset(ret->attr, 0, ret->attrMax * sizeof(xmlSchemaAttrState)); |
| 6235 | return (ret); |
| 6236 | } |
| 6237 | |
| 6238 | /** |
| 6239 | * xmlSchemaFreeValidCtxt: |
| 6240 | * @ctxt: the schema validation context |
| 6241 | * |
| 6242 | * Free the resources associated to the schema validation context |
| 6243 | */ |
| 6244 | void |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6245 | xmlSchemaFreeValidCtxt(xmlSchemaValidCtxtPtr ctxt) |
| 6246 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6247 | if (ctxt == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6248 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6249 | if (ctxt->attr != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6250 | xmlFree(ctxt->attr); |
Daniel Veillard | 88c5891 | 2002-04-23 07:12:20 +0000 | [diff] [blame] | 6251 | if (ctxt->value != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6252 | xmlSchemaFreeValue(ctxt->value); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6253 | xmlFree(ctxt); |
| 6254 | } |
| 6255 | |
| 6256 | /** |
| 6257 | * xmlSchemaSetValidErrors: |
| 6258 | * @ctxt: a schema validation context |
| 6259 | * @err: the error function |
| 6260 | * @warn: the warning function |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 6261 | * @ctx: the functions context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6262 | * |
| 6263 | * Set the error and warning callback informations |
| 6264 | */ |
| 6265 | void |
| 6266 | xmlSchemaSetValidErrors(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6267 | xmlSchemaValidityErrorFunc err, |
| 6268 | xmlSchemaValidityWarningFunc warn, void *ctx) |
| 6269 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6270 | if (ctxt == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6271 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6272 | ctxt->error = err; |
| 6273 | ctxt->warning = warn; |
| 6274 | ctxt->userData = ctx; |
| 6275 | } |
| 6276 | |
| 6277 | /** |
| 6278 | * xmlSchemaValidateDoc: |
| 6279 | * @ctxt: a schema validation context |
| 6280 | * @doc: a parsed document tree |
| 6281 | * |
| 6282 | * Validate a document tree in memory. |
| 6283 | * |
| 6284 | * Returns 0 if the document is schemas valid, a positive error code |
| 6285 | * number otherwise and -1 in case of internal or API error. |
| 6286 | */ |
| 6287 | int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6288 | xmlSchemaValidateDoc(xmlSchemaValidCtxtPtr ctxt, xmlDocPtr doc) |
| 6289 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6290 | int ret; |
| 6291 | |
| 6292 | if ((ctxt == NULL) || (doc == NULL)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6293 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6294 | |
| 6295 | ctxt->doc = doc; |
| 6296 | ret = xmlSchemaValidateDocument(ctxt, doc); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6297 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6298 | } |
| 6299 | |
| 6300 | /** |
| 6301 | * xmlSchemaValidateStream: |
| 6302 | * @ctxt: a schema validation context |
| 6303 | * @input: the input to use for reading the data |
| 6304 | * @enc: an optional encoding information |
| 6305 | * @sax: a SAX handler for the resulting events |
| 6306 | * @user_data: the context to provide to the SAX handler. |
| 6307 | * |
| 6308 | * Validate a document tree in memory. |
| 6309 | * |
| 6310 | * Returns 0 if the document is schemas valid, a positive error code |
| 6311 | * number otherwise and -1 in case of internal or API error. |
| 6312 | */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6313 | int |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6314 | xmlSchemaValidateStream(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6315 | xmlParserInputBufferPtr input, xmlCharEncoding enc, |
| 6316 | xmlSAXHandlerPtr sax, void *user_data) |
| 6317 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6318 | if ((ctxt == NULL) || (input == NULL)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6319 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6320 | ctxt->input = input; |
| 6321 | ctxt->enc = enc; |
| 6322 | ctxt->sax = sax; |
| 6323 | ctxt->user_data = user_data; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6324 | TODO return (0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6325 | } |
| 6326 | |
| 6327 | #endif /* LIBXML_SCHEMAS_ENABLED */ |