Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * schemas.c : implementation of the XML Schema handling and |
| 3 | * schema validity checking |
| 4 | * |
| 5 | * See Copyright for the status of this software. |
| 6 | * |
| 7 | * Daniel Veillard <veillard@redhat.com> |
| 8 | */ |
| 9 | |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 10 | /* |
| 11 | * TODO: |
| 12 | * - when types are redefined in includes, check that all |
| 13 | * types in the redef list are equal |
| 14 | * -> need a type equality operation. |
| 15 | */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 16 | #define IN_LIBXML |
| 17 | #include "libxml.h" |
| 18 | |
| 19 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <libxml/xmlmemory.h> |
| 23 | #include <libxml/parser.h> |
| 24 | #include <libxml/parserInternals.h> |
| 25 | #include <libxml/hash.h> |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 26 | #include <libxml/uri.h> |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 27 | |
| 28 | #include <libxml/xmlschemas.h> |
| 29 | #include <libxml/schemasInternals.h> |
| 30 | #include <libxml/xmlschemastypes.h> |
| 31 | #include <libxml/xmlautomata.h> |
| 32 | #include <libxml/xmlregexp.h> |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 33 | #include <libxml/dict.h> |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 34 | |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 35 | /* #define DEBUG 1 */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 36 | |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 37 | /* #define DEBUG_CONTENT 1 */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 38 | |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 39 | /* #define DEBUG_TYPE 1 */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 40 | |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 41 | /* #define DEBUG_CONTENT_REGEXP 1 */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 42 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 43 | /* #define DEBUG_AUTOMATA 1 */ |
| 44 | |
| 45 | #define UNBOUNDED (1 << 30) |
| 46 | #define TODO \ |
| 47 | xmlGenericError(xmlGenericErrorContext, \ |
| 48 | "Unimplemented block at %s:%d\n", \ |
| 49 | __FILE__, __LINE__); |
| 50 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 51 | #define XML_SCHEMAS_DEFAULT_NAMESPACE (const xmlChar *)"the default namespace" |
| 52 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 53 | /* |
| 54 | * The XML Schemas namespaces |
| 55 | */ |
| 56 | static const xmlChar *xmlSchemaNs = (const xmlChar *) |
| 57 | "http://www.w3.org/2001/XMLSchema"; |
| 58 | |
| 59 | static const xmlChar *xmlSchemaInstanceNs = (const xmlChar *) |
| 60 | "http://www.w3.org/2001/XMLSchema-instance"; |
| 61 | |
| 62 | #define IS_SCHEMA(node, type) \ |
| 63 | ((node != NULL) && (node->ns != NULL) && \ |
| 64 | (xmlStrEqual(node->name, (const xmlChar *) type)) && \ |
| 65 | (xmlStrEqual(node->ns->href, xmlSchemaNs))) |
| 66 | |
| 67 | #define XML_SCHEMAS_PARSE_ERROR 1 |
| 68 | |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 69 | #define SCHEMAS_PARSE_OPTIONS XML_PARSE_NOENT |
| 70 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 71 | struct _xmlSchemaParserCtxt { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 72 | void *userData; /* user specific data block */ |
| 73 | xmlSchemaValidityErrorFunc error; /* the callback in case of errors */ |
| 74 | xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */ |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 75 | xmlSchemaValidError err; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 76 | int nberrors; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 77 | xmlStructuredErrorFunc serror; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 78 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 79 | xmlSchemaPtr topschema; /* The main schema */ |
| 80 | xmlHashTablePtr namespaces; /* Hash table of namespaces to schemas */ |
| 81 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 82 | xmlSchemaPtr schema; /* The schema in use */ |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 83 | const xmlChar *container; /* the current element, group, ... */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 84 | int counter; |
| 85 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 86 | const xmlChar *URL; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 87 | xmlDocPtr doc; |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 88 | int preserve; /* Whether the doc should be freed */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 89 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 90 | const char *buffer; |
| 91 | int size; |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 92 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 93 | /* |
| 94 | * Used to build complex element content models |
| 95 | */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 96 | xmlAutomataPtr am; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 97 | xmlAutomataStatePtr start; |
| 98 | xmlAutomataStatePtr end; |
| 99 | xmlAutomataStatePtr state; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 100 | |
| 101 | xmlDictPtr dict; /* dictionnary for interned string names */ |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 102 | int includes; /* the inclusion level, 0 for root or imports */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | |
| 106 | #define XML_SCHEMAS_ATTR_UNKNOWN 1 |
| 107 | #define XML_SCHEMAS_ATTR_CHECKED 2 |
| 108 | |
| 109 | typedef struct _xmlSchemaAttrState xmlSchemaAttrState; |
| 110 | typedef xmlSchemaAttrState *xmlSchemaAttrStatePtr; |
| 111 | struct _xmlSchemaAttrState { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 112 | xmlAttrPtr attr; |
| 113 | int state; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | /** |
| 117 | * xmlSchemaValidCtxt: |
| 118 | * |
| 119 | * A Schemas validation context |
| 120 | */ |
| 121 | |
| 122 | struct _xmlSchemaValidCtxt { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 123 | void *userData; /* user specific data block */ |
| 124 | xmlSchemaValidityErrorFunc error; /* the callback in case of errors */ |
| 125 | xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */ |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 126 | xmlStructuredErrorFunc serror; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 127 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 128 | xmlSchemaPtr schema; /* The schema in use */ |
| 129 | xmlDocPtr doc; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 130 | xmlParserInputBufferPtr input; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 131 | xmlCharEncoding enc; |
| 132 | xmlSAXHandlerPtr sax; |
| 133 | void *user_data; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 134 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 135 | xmlDocPtr myDoc; |
| 136 | int err; |
| 137 | int nberrors; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 138 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 139 | xmlNodePtr node; |
| 140 | xmlNodePtr cur; |
| 141 | xmlSchemaTypePtr type; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 142 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 143 | xmlRegExecCtxtPtr regexp; |
| 144 | xmlSchemaValPtr value; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 145 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 146 | int attrNr; |
| 147 | int attrBase; |
| 148 | int attrMax; |
| 149 | xmlSchemaAttrStatePtr attr; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 152 | /* |
| 153 | * These are the entries in the schemas importSchemas hash table |
| 154 | */ |
| 155 | typedef struct _xmlSchemaImport xmlSchemaImport; |
| 156 | typedef xmlSchemaImport *xmlSchemaImportPtr; |
| 157 | struct _xmlSchemaImport { |
| 158 | const xmlChar *schemaLocation; |
| 159 | xmlSchemaPtr schema; |
| 160 | }; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 161 | |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 162 | /* |
| 163 | * These are the entries associated to includes in a schemas |
| 164 | */ |
| 165 | typedef struct _xmlSchemaInclude xmlSchemaInclude; |
| 166 | typedef xmlSchemaInclude *xmlSchemaIncludePtr; |
| 167 | struct _xmlSchemaInclude { |
| 168 | xmlSchemaIncludePtr next; |
| 169 | |
| 170 | const xmlChar *schemaLocation; |
| 171 | xmlDocPtr doc; |
| 172 | }; |
| 173 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 174 | /************************************************************************ |
| 175 | * * |
| 176 | * Some predeclarations * |
| 177 | * * |
| 178 | ************************************************************************/ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 179 | static int xmlSchemaValidateSimpleValue(xmlSchemaValidCtxtPtr ctxt, |
| 180 | xmlSchemaTypePtr type, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 181 | const xmlChar * value); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 182 | |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 183 | static int xmlSchemaParseInclude(xmlSchemaParserCtxtPtr ctxt, |
| 184 | xmlSchemaPtr schema, |
| 185 | xmlNodePtr node); |
William M. Brack | 87640d5 | 2004-04-17 14:58:15 +0000 | [diff] [blame] | 186 | static int |
| 187 | xmlSchemaValidateSimpleValueInternal(xmlSchemaValidCtxtPtr ctxt, |
| 188 | xmlSchemaTypePtr type, |
| 189 | const xmlChar * value, |
| 190 | int fireErrors); |
| 191 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 192 | /************************************************************************ |
| 193 | * * |
| 194 | * Datatype error handlers * |
| 195 | * * |
| 196 | ************************************************************************/ |
| 197 | |
| 198 | /** |
| 199 | * xmlSchemaPErrMemory: |
| 200 | * @node: a context node |
| 201 | * @extra: extra informations |
| 202 | * |
| 203 | * Handle an out of memory condition |
| 204 | */ |
| 205 | static void |
| 206 | xmlSchemaPErrMemory(xmlSchemaParserCtxtPtr ctxt, |
| 207 | const char *extra, xmlNodePtr node) |
| 208 | { |
| 209 | if (ctxt != NULL) |
| 210 | ctxt->nberrors++; |
| 211 | __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, node, NULL, |
| 212 | extra); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * xmlSchemaPErr: |
| 217 | * @ctxt: the parsing context |
| 218 | * @node: the context node |
| 219 | * @error: the error code |
| 220 | * @msg: the error message |
| 221 | * @str1: extra data |
| 222 | * @str2: extra data |
| 223 | * |
| 224 | * Handle a parser error |
| 225 | */ |
| 226 | static void |
| 227 | xmlSchemaPErr(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error, |
| 228 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 229 | { |
| 230 | xmlGenericErrorFunc channel = NULL; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 231 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 232 | void *data = NULL; |
| 233 | |
| 234 | if (ctxt != NULL) { |
| 235 | ctxt->nberrors++; |
| 236 | channel = ctxt->error; |
| 237 | data = ctxt->userData; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 238 | schannel = ctxt->serror; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 239 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 240 | __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 241 | error, XML_ERR_ERROR, NULL, 0, |
| 242 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 243 | msg, str1, str2); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * xmlSchemaPErr2: |
| 248 | * @ctxt: the parsing context |
| 249 | * @node: the context node |
| 250 | * @node: the current child |
| 251 | * @error: the error code |
| 252 | * @msg: the error message |
| 253 | * @str1: extra data |
| 254 | * @str2: extra data |
| 255 | * |
| 256 | * Handle a parser error |
| 257 | */ |
| 258 | static void |
| 259 | xmlSchemaPErr2(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, |
| 260 | xmlNodePtr child, int error, |
| 261 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 262 | { |
| 263 | if (child != NULL) |
| 264 | xmlSchemaPErr(ctxt, child, error, msg, str1, str2); |
| 265 | else |
| 266 | xmlSchemaPErr(ctxt, node, error, msg, str1, str2); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * xmlSchemaVTypeErrMemory: |
| 271 | * @node: a context node |
| 272 | * @extra: extra informations |
| 273 | * |
| 274 | * Handle an out of memory condition |
| 275 | */ |
| 276 | static void |
| 277 | xmlSchemaVErrMemory(xmlSchemaValidCtxtPtr ctxt, |
| 278 | const char *extra, xmlNodePtr node) |
| 279 | { |
| 280 | if (ctxt != NULL) { |
| 281 | ctxt->nberrors++; |
| 282 | ctxt->err = XML_SCHEMAS_ERR_INTERNAL; |
| 283 | } |
| 284 | __xmlSimpleError(XML_FROM_SCHEMASV, XML_ERR_NO_MEMORY, node, NULL, |
| 285 | extra); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * xmlSchemaVErr3: |
| 290 | * @ctxt: the validation context |
| 291 | * @node: the context node |
| 292 | * @error: the error code |
| 293 | * @msg: the error message |
| 294 | * @str1: extra data |
| 295 | * @str2: extra data |
| 296 | * @str3: extra data |
| 297 | * |
| 298 | * Handle a validation error |
| 299 | */ |
| 300 | static void |
| 301 | xmlSchemaVErr3(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node, int error, |
| 302 | const char *msg, const xmlChar *str1, const xmlChar *str2, |
| 303 | const xmlChar *str3) |
| 304 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 305 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 306 | xmlGenericErrorFunc channel = NULL; |
| 307 | void *data = NULL; |
| 308 | |
| 309 | if (ctxt != NULL) { |
| 310 | ctxt->nberrors++; |
| 311 | ctxt->err = error; |
| 312 | channel = ctxt->error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 313 | schannel = ctxt->serror; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 314 | data = ctxt->userData; |
| 315 | } |
| 316 | /* reajust to global error numbers */ |
| 317 | error += XML_SCHEMAV_NOROOT - XML_SCHEMAS_ERR_NOROOT; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 318 | __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASV, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 319 | error, XML_ERR_ERROR, NULL, 0, |
| 320 | (const char *) str1, (const char *) str2, |
| 321 | (const char *) str3, 0, 0, |
| 322 | msg, str1, str2, str3); |
| 323 | } |
| 324 | /** |
| 325 | * xmlSchemaVErr: |
| 326 | * @ctxt: the validation context |
| 327 | * @node: the context node |
| 328 | * @error: the error code |
| 329 | * @msg: the error message |
| 330 | * @str1: extra data |
| 331 | * @str2: extra data |
| 332 | * |
| 333 | * Handle a validation error |
| 334 | */ |
| 335 | static void |
| 336 | xmlSchemaVErr(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node, int error, |
| 337 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 338 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 339 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 340 | xmlGenericErrorFunc channel = NULL; |
| 341 | void *data = NULL; |
| 342 | |
| 343 | if (ctxt != NULL) { |
| 344 | ctxt->nberrors++; |
| 345 | ctxt->err = error; |
| 346 | channel = ctxt->error; |
| 347 | data = ctxt->userData; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 348 | schannel = ctxt->serror; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 349 | } |
| 350 | /* reajust to global error numbers */ |
| 351 | error += XML_SCHEMAV_NOROOT - XML_SCHEMAS_ERR_NOROOT; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 352 | __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASV, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 353 | error, XML_ERR_ERROR, NULL, 0, |
| 354 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 355 | msg, str1, str2); |
| 356 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 357 | |
| 358 | /************************************************************************ |
| 359 | * * |
| 360 | * Allocation functions * |
| 361 | * * |
| 362 | ************************************************************************/ |
| 363 | |
| 364 | /** |
| 365 | * xmlSchemaNewSchema: |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 366 | * @ctxt: a schema validation context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 367 | * |
| 368 | * Allocate a new Schema structure. |
| 369 | * |
| 370 | * Returns the newly allocated structure or NULL in case or error |
| 371 | */ |
| 372 | static xmlSchemaPtr |
| 373 | xmlSchemaNewSchema(xmlSchemaParserCtxtPtr ctxt) |
| 374 | { |
| 375 | xmlSchemaPtr ret; |
| 376 | |
| 377 | ret = (xmlSchemaPtr) xmlMalloc(sizeof(xmlSchema)); |
| 378 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 379 | xmlSchemaPErrMemory(ctxt, "allocating schema", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 380 | return (NULL); |
| 381 | } |
| 382 | memset(ret, 0, sizeof(xmlSchema)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 383 | ret->dict = ctxt->dict; |
Daniel Veillard | 500a1de | 2004-03-22 15:22:58 +0000 | [diff] [blame] | 384 | xmlDictReference(ret->dict); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 385 | |
| 386 | return (ret); |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * xmlSchemaNewFacet: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 391 | * |
| 392 | * Allocate a new Facet structure. |
| 393 | * |
| 394 | * Returns the newly allocated structure or NULL in case or error |
| 395 | */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 396 | xmlSchemaFacetPtr |
| 397 | xmlSchemaNewFacet(void) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 398 | { |
| 399 | xmlSchemaFacetPtr ret; |
| 400 | |
| 401 | ret = (xmlSchemaFacetPtr) xmlMalloc(sizeof(xmlSchemaFacet)); |
| 402 | if (ret == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 403 | return (NULL); |
| 404 | } |
| 405 | memset(ret, 0, sizeof(xmlSchemaFacet)); |
| 406 | |
| 407 | return (ret); |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * xmlSchemaNewAnnot: |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 412 | * @ctxt: a schema validation context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 413 | * @node: a node |
| 414 | * |
| 415 | * Allocate a new annotation structure. |
| 416 | * |
| 417 | * Returns the newly allocated structure or NULL in case or error |
| 418 | */ |
| 419 | static xmlSchemaAnnotPtr |
| 420 | xmlSchemaNewAnnot(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node) |
| 421 | { |
| 422 | xmlSchemaAnnotPtr ret; |
| 423 | |
| 424 | ret = (xmlSchemaAnnotPtr) xmlMalloc(sizeof(xmlSchemaAnnot)); |
| 425 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 426 | xmlSchemaPErrMemory(ctxt, "allocating annotation", node); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 427 | return (NULL); |
| 428 | } |
| 429 | memset(ret, 0, sizeof(xmlSchemaAnnot)); |
| 430 | ret->content = node; |
| 431 | return (ret); |
| 432 | } |
| 433 | |
| 434 | /** |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 435 | * xmlSchemaFreeAnnot: |
| 436 | * @annot: a schema type structure |
| 437 | * |
| 438 | * Deallocate a annotation structure |
| 439 | */ |
| 440 | static void |
| 441 | xmlSchemaFreeAnnot(xmlSchemaAnnotPtr annot) |
| 442 | { |
| 443 | if (annot == NULL) |
| 444 | return; |
| 445 | xmlFree(annot); |
| 446 | } |
| 447 | |
| 448 | /** |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 449 | * xmlSchemaFreeImport: |
| 450 | * @import: a schema import structure |
| 451 | * |
| 452 | * Deallocate an import structure |
| 453 | */ |
| 454 | static void |
| 455 | xmlSchemaFreeImport(xmlSchemaImportPtr import) |
| 456 | { |
| 457 | if (import == NULL) |
| 458 | return; |
| 459 | |
| 460 | xmlSchemaFree(import->schema); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 461 | xmlFree(import); |
| 462 | } |
| 463 | |
| 464 | /** |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 465 | * xmlSchemaFreeInclude: |
| 466 | * @include: a schema include structure |
| 467 | * |
| 468 | * Deallocate an include structure |
| 469 | */ |
| 470 | static void |
| 471 | xmlSchemaFreeInclude(xmlSchemaIncludePtr include) |
| 472 | { |
| 473 | if (include == NULL) |
| 474 | return; |
| 475 | |
| 476 | xmlFreeDoc(include->doc); |
| 477 | xmlFree(include); |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * xmlSchemaFreeIncludeList: |
| 482 | * @includes: a schema include list |
| 483 | * |
| 484 | * Deallocate an include structure |
| 485 | */ |
| 486 | static void |
| 487 | xmlSchemaFreeIncludeList(xmlSchemaIncludePtr includes) |
| 488 | { |
| 489 | xmlSchemaIncludePtr next; |
| 490 | |
| 491 | while (includes != NULL) { |
| 492 | next = includes->next; |
| 493 | xmlSchemaFreeInclude(includes); |
| 494 | includes = next; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 499 | * xmlSchemaFreeNotation: |
| 500 | * @schema: a schema notation structure |
| 501 | * |
| 502 | * Deallocate a Schema Notation structure. |
| 503 | */ |
| 504 | static void |
| 505 | xmlSchemaFreeNotation(xmlSchemaNotationPtr nota) |
| 506 | { |
| 507 | if (nota == NULL) |
| 508 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 509 | xmlFree(nota); |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * xmlSchemaFreeAttribute: |
| 514 | * @schema: a schema attribute structure |
| 515 | * |
| 516 | * Deallocate a Schema Attribute structure. |
| 517 | */ |
| 518 | static void |
| 519 | xmlSchemaFreeAttribute(xmlSchemaAttributePtr 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 | * xmlSchemaFreeAttributeGroup: |
| 528 | * @schema: a schema attribute group structure |
| 529 | * |
| 530 | * Deallocate a Schema Attribute Group structure. |
| 531 | */ |
| 532 | static void |
| 533 | xmlSchemaFreeAttributeGroup(xmlSchemaAttributeGroupPtr attr) |
| 534 | { |
| 535 | if (attr == NULL) |
| 536 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 537 | xmlFree(attr); |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * xmlSchemaFreeElement: |
| 542 | * @schema: a schema element structure |
| 543 | * |
| 544 | * Deallocate a Schema Element structure. |
| 545 | */ |
| 546 | static void |
| 547 | xmlSchemaFreeElement(xmlSchemaElementPtr elem) |
| 548 | { |
| 549 | if (elem == NULL) |
| 550 | return; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 551 | if (elem->annot != NULL) |
| 552 | xmlSchemaFreeAnnot(elem->annot); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 553 | if (elem->contModel != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 554 | xmlRegFreeRegexp(elem->contModel); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 555 | xmlFree(elem); |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * xmlSchemaFreeFacet: |
| 560 | * @facet: a schema facet structure |
| 561 | * |
| 562 | * Deallocate a Schema Facet structure. |
| 563 | */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 564 | void |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 565 | xmlSchemaFreeFacet(xmlSchemaFacetPtr facet) |
| 566 | { |
| 567 | if (facet == NULL) |
| 568 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 569 | if (facet->val != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 570 | xmlSchemaFreeValue(facet->val); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 571 | if (facet->regexp != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 572 | xmlRegFreeRegexp(facet->regexp); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 573 | if (facet->annot != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 574 | xmlSchemaFreeAnnot(facet->annot); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 575 | xmlFree(facet); |
| 576 | } |
| 577 | |
| 578 | /** |
| 579 | * xmlSchemaFreeType: |
| 580 | * @type: a schema type structure |
| 581 | * |
| 582 | * Deallocate a Schema Type structure. |
| 583 | */ |
| 584 | void |
| 585 | xmlSchemaFreeType(xmlSchemaTypePtr type) |
| 586 | { |
| 587 | if (type == NULL) |
| 588 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 589 | if (type->annot != NULL) |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 590 | xmlSchemaFreeAnnot(type->annot); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 591 | if (type->facets != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 592 | xmlSchemaFacetPtr facet, next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 593 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 594 | facet = type->facets; |
| 595 | while (facet != NULL) { |
| 596 | next = facet->next; |
| 597 | xmlSchemaFreeFacet(facet); |
| 598 | facet = next; |
| 599 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 600 | } |
| 601 | xmlFree(type); |
| 602 | } |
| 603 | |
| 604 | /** |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 605 | * xmlSchemaFreeTypeList: |
| 606 | * @type: a schema type structure |
| 607 | * |
| 608 | * Deallocate a Schema Type structure. |
| 609 | */ |
| 610 | static void |
| 611 | xmlSchemaFreeTypeList(xmlSchemaTypePtr type) |
| 612 | { |
| 613 | xmlSchemaTypePtr next; |
| 614 | |
| 615 | while (type != NULL) { |
| 616 | next = type->redef; |
| 617 | xmlSchemaFreeType(type); |
| 618 | type = next; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 623 | * xmlSchemaFree: |
| 624 | * @schema: a schema structure |
| 625 | * |
| 626 | * Deallocate a Schema structure. |
| 627 | */ |
| 628 | void |
| 629 | xmlSchemaFree(xmlSchemaPtr schema) |
| 630 | { |
| 631 | if (schema == NULL) |
| 632 | return; |
| 633 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 634 | if (schema->notaDecl != NULL) |
| 635 | xmlHashFree(schema->notaDecl, |
| 636 | (xmlHashDeallocator) xmlSchemaFreeNotation); |
| 637 | if (schema->attrDecl != NULL) |
| 638 | xmlHashFree(schema->attrDecl, |
| 639 | (xmlHashDeallocator) xmlSchemaFreeAttribute); |
| 640 | if (schema->attrgrpDecl != NULL) |
| 641 | xmlHashFree(schema->attrgrpDecl, |
| 642 | (xmlHashDeallocator) xmlSchemaFreeAttributeGroup); |
| 643 | if (schema->elemDecl != NULL) |
| 644 | xmlHashFree(schema->elemDecl, |
| 645 | (xmlHashDeallocator) xmlSchemaFreeElement); |
| 646 | if (schema->typeDecl != NULL) |
| 647 | xmlHashFree(schema->typeDecl, |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 648 | (xmlHashDeallocator) xmlSchemaFreeTypeList); |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 649 | if (schema->groupDecl != NULL) |
| 650 | xmlHashFree(schema->groupDecl, |
| 651 | (xmlHashDeallocator) xmlSchemaFreeType); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 652 | if (schema->schemasImports != NULL) |
| 653 | xmlHashFree(schema->schemasImports, |
| 654 | (xmlHashDeallocator) xmlSchemaFreeImport); |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 655 | if (schema->includes != NULL) { |
| 656 | xmlSchemaFreeIncludeList((xmlSchemaIncludePtr) schema->includes); |
| 657 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 658 | if (schema->annot != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 659 | xmlSchemaFreeAnnot(schema->annot); |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 660 | if (schema->doc != NULL && !schema->preserve) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 661 | xmlFreeDoc(schema->doc); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 662 | xmlDictFree(schema->dict); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 663 | |
| 664 | xmlFree(schema); |
| 665 | } |
| 666 | |
| 667 | /************************************************************************ |
| 668 | * * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 669 | * Debug functions * |
| 670 | * * |
| 671 | ************************************************************************/ |
| 672 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 673 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 674 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 675 | /** |
| 676 | * xmlSchemaElementDump: |
| 677 | * @elem: an element |
| 678 | * @output: the file output |
| 679 | * |
| 680 | * Dump the element |
| 681 | */ |
| 682 | static void |
| 683 | xmlSchemaElementDump(xmlSchemaElementPtr elem, FILE * output, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 684 | const xmlChar * name ATTRIBUTE_UNUSED, |
| 685 | const xmlChar * context ATTRIBUTE_UNUSED, |
| 686 | const xmlChar * namespace ATTRIBUTE_UNUSED) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 687 | { |
| 688 | if (elem == NULL) |
| 689 | return; |
| 690 | |
| 691 | fprintf(output, "Element "); |
| 692 | if (elem->flags & XML_SCHEMAS_ELEM_TOPLEVEL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 693 | fprintf(output, "toplevel "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 694 | fprintf(output, ": %s ", elem->name); |
| 695 | if (namespace != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 696 | fprintf(output, "namespace '%s' ", namespace); |
| 697 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 698 | if (elem->flags & XML_SCHEMAS_ELEM_NILLABLE) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 699 | fprintf(output, "nillable "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 700 | if (elem->flags & XML_SCHEMAS_ELEM_GLOBAL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 701 | fprintf(output, "global "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 702 | if (elem->flags & XML_SCHEMAS_ELEM_DEFAULT) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 703 | fprintf(output, "default "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 704 | if (elem->flags & XML_SCHEMAS_ELEM_FIXED) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 705 | fprintf(output, "fixed "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 706 | if (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 707 | fprintf(output, "abstract "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 708 | if (elem->flags & XML_SCHEMAS_ELEM_REF) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 709 | fprintf(output, "ref '%s' ", elem->ref); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 710 | if (elem->id != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 711 | fprintf(output, "id '%s' ", elem->id); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 712 | fprintf(output, "\n"); |
| 713 | if ((elem->minOccurs != 1) || (elem->maxOccurs != 1)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 714 | fprintf(output, " "); |
| 715 | if (elem->minOccurs != 1) |
| 716 | fprintf(output, "min: %d ", elem->minOccurs); |
| 717 | if (elem->maxOccurs >= UNBOUNDED) |
| 718 | fprintf(output, "max: unbounded\n"); |
| 719 | else if (elem->maxOccurs != 1) |
| 720 | fprintf(output, "max: %d\n", elem->maxOccurs); |
| 721 | else |
| 722 | fprintf(output, "\n"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 723 | } |
| 724 | if (elem->namedType != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 725 | fprintf(output, " type: %s", elem->namedType); |
| 726 | if (elem->namedTypeNs != NULL) |
| 727 | fprintf(output, " ns %s\n", elem->namedTypeNs); |
| 728 | else |
| 729 | fprintf(output, "\n"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 730 | } |
| 731 | if (elem->substGroup != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 732 | fprintf(output, " substitutionGroup: %s", elem->substGroup); |
| 733 | if (elem->substGroupNs != NULL) |
| 734 | fprintf(output, " ns %s\n", elem->substGroupNs); |
| 735 | else |
| 736 | fprintf(output, "\n"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 737 | } |
| 738 | if (elem->value != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 739 | fprintf(output, " default: %s", elem->value); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | /** |
| 743 | * xmlSchemaAnnotDump: |
| 744 | * @output: the file output |
| 745 | * @annot: a annotation |
| 746 | * |
| 747 | * Dump the annotation |
| 748 | */ |
| 749 | static void |
| 750 | xmlSchemaAnnotDump(FILE * output, xmlSchemaAnnotPtr annot) |
| 751 | { |
| 752 | xmlChar *content; |
| 753 | |
| 754 | if (annot == NULL) |
| 755 | return; |
| 756 | |
| 757 | content = xmlNodeGetContent(annot->content); |
| 758 | if (content != NULL) { |
| 759 | fprintf(output, " Annot: %s\n", content); |
| 760 | xmlFree(content); |
| 761 | } else |
| 762 | fprintf(output, " Annot: empty\n"); |
| 763 | } |
| 764 | |
| 765 | /** |
| 766 | * xmlSchemaTypeDump: |
| 767 | * @output: the file output |
| 768 | * @type: a type structure |
| 769 | * |
| 770 | * Dump a SchemaType structure |
| 771 | */ |
| 772 | static void |
| 773 | xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output) |
| 774 | { |
| 775 | if (type == NULL) { |
| 776 | fprintf(output, "Type: NULL\n"); |
| 777 | return; |
| 778 | } |
| 779 | fprintf(output, "Type: "); |
| 780 | if (type->name != NULL) |
| 781 | fprintf(output, "%s, ", type->name); |
| 782 | else |
| 783 | fprintf(output, "no name"); |
| 784 | switch (type->type) { |
| 785 | case XML_SCHEMA_TYPE_BASIC: |
| 786 | fprintf(output, "basic "); |
| 787 | break; |
| 788 | case XML_SCHEMA_TYPE_SIMPLE: |
| 789 | fprintf(output, "simple "); |
| 790 | break; |
| 791 | case XML_SCHEMA_TYPE_COMPLEX: |
| 792 | fprintf(output, "complex "); |
| 793 | break; |
| 794 | case XML_SCHEMA_TYPE_SEQUENCE: |
| 795 | fprintf(output, "sequence "); |
| 796 | break; |
| 797 | case XML_SCHEMA_TYPE_CHOICE: |
| 798 | fprintf(output, "choice "); |
| 799 | break; |
| 800 | case XML_SCHEMA_TYPE_ALL: |
| 801 | fprintf(output, "all "); |
| 802 | break; |
| 803 | case XML_SCHEMA_TYPE_UR: |
| 804 | fprintf(output, "ur "); |
| 805 | break; |
| 806 | case XML_SCHEMA_TYPE_RESTRICTION: |
| 807 | fprintf(output, "restriction "); |
| 808 | break; |
| 809 | case XML_SCHEMA_TYPE_EXTENSION: |
| 810 | fprintf(output, "extension "); |
| 811 | break; |
| 812 | default: |
| 813 | fprintf(output, "unknowntype%d ", type->type); |
| 814 | break; |
| 815 | } |
| 816 | if (type->base != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 817 | fprintf(output, "base %s, ", type->base); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 818 | } |
| 819 | switch (type->contentType) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 820 | case XML_SCHEMA_CONTENT_UNKNOWN: |
| 821 | fprintf(output, "unknown "); |
| 822 | break; |
| 823 | case XML_SCHEMA_CONTENT_EMPTY: |
| 824 | fprintf(output, "empty "); |
| 825 | break; |
| 826 | case XML_SCHEMA_CONTENT_ELEMENTS: |
| 827 | fprintf(output, "element "); |
| 828 | break; |
| 829 | case XML_SCHEMA_CONTENT_MIXED: |
| 830 | fprintf(output, "mixed "); |
| 831 | break; |
| 832 | case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS: |
| 833 | fprintf(output, "mixed_or_elems "); |
| 834 | break; |
| 835 | case XML_SCHEMA_CONTENT_BASIC: |
| 836 | fprintf(output, "basic "); |
| 837 | break; |
| 838 | case XML_SCHEMA_CONTENT_SIMPLE: |
| 839 | fprintf(output, "simple "); |
| 840 | break; |
| 841 | case XML_SCHEMA_CONTENT_ANY: |
| 842 | fprintf(output, "any "); |
| 843 | break; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 844 | } |
| 845 | fprintf(output, "\n"); |
| 846 | if ((type->minOccurs != 1) || (type->maxOccurs != 1)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 847 | fprintf(output, " "); |
| 848 | if (type->minOccurs != 1) |
| 849 | fprintf(output, "min: %d ", type->minOccurs); |
| 850 | if (type->maxOccurs >= UNBOUNDED) |
| 851 | fprintf(output, "max: unbounded\n"); |
| 852 | else if (type->maxOccurs != 1) |
| 853 | fprintf(output, "max: %d\n", type->maxOccurs); |
| 854 | else |
| 855 | fprintf(output, "\n"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 856 | } |
| 857 | if (type->annot != NULL) |
| 858 | xmlSchemaAnnotDump(output, type->annot); |
| 859 | if (type->subtypes != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 860 | xmlSchemaTypePtr sub = type->subtypes; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 861 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 862 | fprintf(output, " subtypes: "); |
| 863 | while (sub != NULL) { |
| 864 | fprintf(output, "%s ", sub->name); |
| 865 | sub = sub->next; |
| 866 | } |
| 867 | fprintf(output, "\n"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | } |
| 871 | |
| 872 | /** |
| 873 | * xmlSchemaDump: |
| 874 | * @output: the file output |
| 875 | * @schema: a schema structure |
| 876 | * |
| 877 | * Dump a Schema structure. |
| 878 | */ |
| 879 | void |
| 880 | xmlSchemaDump(FILE * output, xmlSchemaPtr schema) |
| 881 | { |
| 882 | if (schema == NULL) { |
| 883 | fprintf(output, "Schemas: NULL\n"); |
| 884 | return; |
| 885 | } |
| 886 | fprintf(output, "Schemas: "); |
| 887 | if (schema->name != NULL) |
| 888 | fprintf(output, "%s, ", schema->name); |
| 889 | else |
| 890 | fprintf(output, "no name, "); |
| 891 | if (schema->targetNamespace != NULL) |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 892 | fprintf(output, "%s", (const char *) schema->targetNamespace); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 893 | else |
| 894 | fprintf(output, "no target namespace"); |
| 895 | fprintf(output, "\n"); |
| 896 | if (schema->annot != NULL) |
| 897 | xmlSchemaAnnotDump(output, schema->annot); |
| 898 | |
| 899 | xmlHashScan(schema->typeDecl, (xmlHashScanner) xmlSchemaTypeDump, |
| 900 | output); |
| 901 | xmlHashScanFull(schema->elemDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 902 | (xmlHashScannerFull) xmlSchemaElementDump, output); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 903 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 904 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 905 | |
| 906 | /************************************************************************ |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 907 | * * |
| 908 | * Utilities * |
| 909 | * * |
| 910 | ************************************************************************/ |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 911 | |
| 912 | /** |
| 913 | * xmlSchemaGetProp: |
| 914 | * @ctxt: the parser context |
| 915 | * @node: the node |
| 916 | * @name: the property name |
| 917 | * |
| 918 | * Read a attribute value and internalize the string |
| 919 | * |
| 920 | * Returns the string or NULL if not present. |
| 921 | */ |
| 922 | static const xmlChar * |
| 923 | xmlSchemaGetProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, |
| 924 | const char *name) |
| 925 | { |
| 926 | xmlChar *val; |
| 927 | const xmlChar *ret; |
| 928 | |
| 929 | val = xmlGetProp(node, BAD_CAST name); |
| 930 | if (val == NULL) |
| 931 | return(NULL); |
| 932 | ret = xmlDictLookup(ctxt->dict, val, -1); |
| 933 | xmlFree(val); |
| 934 | return(ret); |
| 935 | } |
| 936 | |
| 937 | /** |
| 938 | * xmlSchemaGetNamespace: |
| 939 | * @ctxt: the parser context |
| 940 | * @schema: the schemas containing the declaration |
| 941 | * @node: the node |
| 942 | * @qname: the QName to analyze |
| 943 | * |
| 944 | * Find the namespace name for the given declaration. |
| 945 | * |
| 946 | * Returns the local name for that declaration, as well as the namespace name |
| 947 | */ |
| 948 | static const xmlChar * |
| 949 | xmlSchemaGetNamespace(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 950 | xmlNodePtr node, const xmlChar *qname, |
| 951 | const xmlChar **namespace) { |
| 952 | int len; |
| 953 | const xmlChar *name, *prefix, *def = NULL; |
| 954 | xmlNsPtr ns; |
| 955 | |
| 956 | *namespace = NULL; |
| 957 | |
| 958 | if (xmlStrEqual(node->name, BAD_CAST "element") || |
| 959 | xmlStrEqual(node->name, BAD_CAST "attribute") || |
| 960 | xmlStrEqual(node->name, BAD_CAST "simpleType") || |
| 961 | xmlStrEqual(node->name, BAD_CAST "complexType")) { |
| 962 | def = xmlSchemaGetProp(ctxt, node, "targetNamespace"); |
| 963 | } |
| 964 | |
| 965 | qname = xmlDictLookup(ctxt->dict, qname, -1); /* intern the string */ |
| 966 | name = xmlSplitQName3(qname, &len); |
| 967 | if (name == NULL) { |
| 968 | if (def == NULL) { |
| 969 | if (xmlStrEqual(node->name, BAD_CAST "element")) { |
| 970 | if (schema->flags & XML_SCHEMAS_QUALIF_ELEM) |
| 971 | *namespace = schema->targetNamespace; |
| 972 | } else if (xmlStrEqual(node->name, BAD_CAST "attribute")) { |
| 973 | if (schema->flags & XML_SCHEMAS_QUALIF_ATTR) |
| 974 | *namespace = schema->targetNamespace; |
| 975 | } else if ((xmlStrEqual(node->name, BAD_CAST "simpleType")) || |
| 976 | (xmlStrEqual(node->name, BAD_CAST "complexType"))) { |
| 977 | *namespace = schema->targetNamespace; |
| 978 | } |
| 979 | } else { |
| 980 | *namespace = def; |
| 981 | } |
| 982 | return(qname); |
| 983 | } |
| 984 | name = xmlDictLookup(ctxt->dict, name, -1); |
| 985 | prefix = xmlDictLookup(ctxt->dict, qname, len); |
| 986 | if (def != NULL) { |
| 987 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_DEF_AND_PREFIX, |
| 988 | "%s: presence of both prefix %s and targetNamespace\n", |
| 989 | node->name, prefix); |
| 990 | } |
| 991 | ns = xmlSearchNs(node->doc, node, prefix); |
| 992 | if (ns == NULL) { |
| 993 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_PREFIX_UNDEFINED, |
| 994 | "%s: the QName prefix %s is undefined\n", |
| 995 | node->name, prefix); |
| 996 | return(name); |
| 997 | } |
| 998 | *namespace = xmlDictLookup(ctxt->dict, ns->href, -1); |
| 999 | return(name); |
| 1000 | } |
| 1001 | |
| 1002 | /************************************************************************ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1003 | * * |
| 1004 | * Parsing functions * |
| 1005 | * * |
| 1006 | ************************************************************************/ |
| 1007 | |
| 1008 | /** |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1009 | * xmlSchemaGetElem: |
| 1010 | * @schema: the schemas context |
| 1011 | * @name: the element name |
| 1012 | * @ns: the element namespace |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1013 | * @level: how deep is the request |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1014 | * |
| 1015 | * Lookup a an element in the schemas or the accessible schemas |
| 1016 | * |
| 1017 | * Returns the element definition or NULL if not found. |
| 1018 | */ |
| 1019 | static xmlSchemaElementPtr |
| 1020 | xmlSchemaGetElem(xmlSchemaPtr schema, const xmlChar * name, |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1021 | const xmlChar * namespace, int level) |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1022 | { |
| 1023 | xmlSchemaElementPtr ret; |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1024 | xmlSchemaImportPtr import = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1025 | |
| 1026 | if ((name == NULL) || (schema == NULL)) |
| 1027 | return (NULL); |
| 1028 | |
| 1029 | if (namespace == NULL) { |
| 1030 | ret = xmlHashLookup2(schema->elemDecl, name, namespace); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1031 | if ((ret != NULL) && |
| 1032 | ((level == 0) || (ret->flags & XML_SCHEMAS_ELEM_TOPLEVEL))) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1033 | return (ret); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1034 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1035 | } else if ((schema->flags & XML_SCHEMAS_QUALIF_ELEM) == 0) { |
| 1036 | if (xmlStrEqual(namespace, schema->targetNamespace)) |
| 1037 | ret = xmlHashLookup2(schema->elemDecl, name, NULL); |
| 1038 | else |
| 1039 | ret = xmlHashLookup2(schema->elemDecl, name, namespace); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1040 | if ((ret != NULL) && |
| 1041 | ((level == 0) || (ret->flags & XML_SCHEMAS_ELEM_TOPLEVEL))) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1042 | return (ret); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1043 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1044 | } else { |
| 1045 | ret = xmlHashLookup2(schema->elemDecl, name, namespace); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1046 | if ((ret != NULL) && |
| 1047 | ((level == 0) || (ret->flags & XML_SCHEMAS_ELEM_TOPLEVEL))) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1048 | return (ret); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1049 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1050 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1051 | if (level > 0) |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1052 | import = xmlHashLookup(schema->schemasImports, namespace); |
| 1053 | if (import != NULL) |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1054 | ret = xmlSchemaGetElem(import->schema, name, namespace, level + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1055 | #ifdef DEBUG |
| 1056 | if (ret == NULL) { |
| 1057 | if (namespace == NULL) |
| 1058 | fprintf(stderr, "Unable to lookup type %s", name); |
| 1059 | else |
| 1060 | fprintf(stderr, "Unable to lookup type %s:%s", name, |
| 1061 | namespace); |
| 1062 | } |
| 1063 | #endif |
| 1064 | return (ret); |
| 1065 | } |
| 1066 | |
| 1067 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1068 | * xmlSchemaGetType: |
| 1069 | * @schema: the schemas context |
| 1070 | * @name: the type name |
| 1071 | * @ns: the type namespace |
| 1072 | * |
| 1073 | * Lookup a type in the schemas or the predefined types |
| 1074 | * |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 1075 | * Returns the group definition or NULL if not found. |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1076 | */ |
| 1077 | static xmlSchemaTypePtr |
| 1078 | xmlSchemaGetType(xmlSchemaPtr schema, const xmlChar * name, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1079 | const xmlChar * namespace) |
| 1080 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1081 | xmlSchemaTypePtr ret; |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 1082 | xmlSchemaImportPtr import; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1083 | |
| 1084 | if (name == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1085 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1086 | if (schema != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1087 | ret = xmlHashLookup2(schema->typeDecl, name, namespace); |
| 1088 | if (ret != NULL) |
| 1089 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1090 | } |
| 1091 | ret = xmlSchemaGetPredefinedType(name, namespace); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 1092 | if (ret != NULL) |
| 1093 | return (ret); |
| 1094 | import = xmlHashLookup(schema->schemasImports, namespace); |
| 1095 | if (import != NULL) |
| 1096 | ret = xmlSchemaGetType(import->schema, name, namespace); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1097 | #ifdef DEBUG |
| 1098 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1099 | if (namespace == NULL) |
| 1100 | fprintf(stderr, "Unable to lookup type %s", name); |
| 1101 | else |
| 1102 | fprintf(stderr, "Unable to lookup type %s:%s", name, |
| 1103 | namespace); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1104 | } |
| 1105 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1106 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | /************************************************************************ |
| 1110 | * * |
| 1111 | * Parsing functions * |
| 1112 | * * |
| 1113 | ************************************************************************/ |
| 1114 | |
| 1115 | #define IS_BLANK_NODE(n) \ |
| 1116 | (((n)->type == XML_TEXT_NODE) && (xmlSchemaIsBlank((n)->content))) |
| 1117 | |
| 1118 | /** |
| 1119 | * xmlSchemaIsBlank: |
| 1120 | * @str: a string |
| 1121 | * |
| 1122 | * Check if a string is ignorable |
| 1123 | * |
| 1124 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 1125 | */ |
| 1126 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1127 | xmlSchemaIsBlank(xmlChar * str) |
| 1128 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1129 | if (str == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1130 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1131 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1132 | if (!(IS_BLANK_CH(*str))) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1133 | return (0); |
| 1134 | str++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1135 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1136 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | /** |
| 1140 | * xmlSchemaAddNotation: |
| 1141 | * @ctxt: a schema validation context |
| 1142 | * @schema: the schema being built |
| 1143 | * @name: the item name |
| 1144 | * |
| 1145 | * Add an XML schema Attrribute declaration |
| 1146 | * *WARNING* this interface is highly subject to change |
| 1147 | * |
| 1148 | * Returns the new struture or NULL in case of error |
| 1149 | */ |
| 1150 | static xmlSchemaNotationPtr |
| 1151 | xmlSchemaAddNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1152 | const xmlChar * name) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1153 | { |
| 1154 | xmlSchemaNotationPtr ret = NULL; |
| 1155 | int val; |
| 1156 | |
| 1157 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1158 | return (NULL); |
| 1159 | |
| 1160 | if (schema->notaDecl == NULL) |
| 1161 | schema->notaDecl = xmlHashCreate(10); |
| 1162 | if (schema->notaDecl == NULL) |
| 1163 | return (NULL); |
| 1164 | |
| 1165 | ret = (xmlSchemaNotationPtr) xmlMalloc(sizeof(xmlSchemaNotation)); |
| 1166 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1167 | xmlSchemaPErrMemory(ctxt, "add annotation", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1168 | return (NULL); |
| 1169 | } |
| 1170 | memset(ret, 0, sizeof(xmlSchemaNotation)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1171 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1172 | val = xmlHashAddEntry2(schema->notaDecl, name, schema->targetNamespace, |
| 1173 | ret); |
| 1174 | if (val != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1175 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1176 | XML_SCHEMAP_REDEFINED_NOTATION, |
| 1177 | "Notation %s already defined\n", |
| 1178 | name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1179 | xmlFree(ret); |
| 1180 | return (NULL); |
| 1181 | } |
| 1182 | return (ret); |
| 1183 | } |
| 1184 | |
| 1185 | |
| 1186 | /** |
| 1187 | * xmlSchemaAddAttribute: |
| 1188 | * @ctxt: a schema validation context |
| 1189 | * @schema: the schema being built |
| 1190 | * @name: the item name |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1191 | * @namespace: the namespace |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1192 | * |
| 1193 | * Add an XML schema Attrribute declaration |
| 1194 | * *WARNING* this interface is highly subject to change |
| 1195 | * |
| 1196 | * Returns the new struture or NULL in case of error |
| 1197 | */ |
| 1198 | static xmlSchemaAttributePtr |
| 1199 | xmlSchemaAddAttribute(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1200 | const xmlChar * name, const xmlChar * namespace) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1201 | { |
| 1202 | xmlSchemaAttributePtr ret = NULL; |
| 1203 | int val; |
| 1204 | |
| 1205 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1206 | return (NULL); |
| 1207 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1208 | #ifdef DEBUG |
| 1209 | fprintf(stderr, "Adding attribute %s\n", name); |
| 1210 | if (namespace != NULL) |
| 1211 | fprintf(stderr, " target namespace %s\n", namespace); |
| 1212 | #endif |
| 1213 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1214 | if (schema->attrDecl == NULL) |
| 1215 | schema->attrDecl = xmlHashCreate(10); |
| 1216 | if (schema->attrDecl == NULL) |
| 1217 | return (NULL); |
| 1218 | |
| 1219 | ret = (xmlSchemaAttributePtr) xmlMalloc(sizeof(xmlSchemaAttribute)); |
| 1220 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1221 | xmlSchemaPErrMemory(ctxt, "allocating attribute", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1222 | return (NULL); |
| 1223 | } |
| 1224 | memset(ret, 0, sizeof(xmlSchemaAttribute)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1225 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
| 1226 | ret->targetNamespace = xmlDictLookup(ctxt->dict, namespace, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1227 | val = xmlHashAddEntry3(schema->attrDecl, name, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1228 | schema->targetNamespace, ctxt->container, ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1229 | if (val != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1230 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1231 | XML_SCHEMAP_REDEFINED_ATTR, |
| 1232 | "Attribute %s already defined\n", |
| 1233 | name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1234 | xmlFree(ret); |
| 1235 | return (NULL); |
| 1236 | } |
| 1237 | return (ret); |
| 1238 | } |
| 1239 | |
| 1240 | /** |
| 1241 | * xmlSchemaAddAttributeGroup: |
| 1242 | * @ctxt: a schema validation context |
| 1243 | * @schema: the schema being built |
| 1244 | * @name: the item name |
| 1245 | * |
| 1246 | * Add an XML schema Attrribute Group declaration |
| 1247 | * |
| 1248 | * Returns the new struture or NULL in case of error |
| 1249 | */ |
| 1250 | static xmlSchemaAttributeGroupPtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1251 | xmlSchemaAddAttributeGroup(xmlSchemaParserCtxtPtr ctxt, |
| 1252 | xmlSchemaPtr schema, const xmlChar * name) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1253 | { |
| 1254 | xmlSchemaAttributeGroupPtr ret = NULL; |
| 1255 | int val; |
| 1256 | |
| 1257 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1258 | return (NULL); |
| 1259 | |
| 1260 | if (schema->attrgrpDecl == NULL) |
| 1261 | schema->attrgrpDecl = xmlHashCreate(10); |
| 1262 | if (schema->attrgrpDecl == NULL) |
| 1263 | return (NULL); |
| 1264 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1265 | ret = |
| 1266 | (xmlSchemaAttributeGroupPtr) |
| 1267 | xmlMalloc(sizeof(xmlSchemaAttributeGroup)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1268 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1269 | xmlSchemaPErrMemory(ctxt, "allocating attribute group", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1270 | return (NULL); |
| 1271 | } |
| 1272 | memset(ret, 0, sizeof(xmlSchemaAttributeGroup)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1273 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1274 | val = xmlHashAddEntry3(schema->attrgrpDecl, name, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1275 | schema->targetNamespace, ctxt->container, ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1276 | if (val != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1277 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1278 | XML_SCHEMAP_REDEFINED_ATTRGROUP, |
| 1279 | "Attribute group %s already defined\n", |
| 1280 | name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1281 | xmlFree(ret); |
| 1282 | return (NULL); |
| 1283 | } |
| 1284 | return (ret); |
| 1285 | } |
| 1286 | |
| 1287 | /** |
| 1288 | * xmlSchemaAddElement: |
| 1289 | * @ctxt: a schema validation context |
| 1290 | * @schema: the schema being built |
| 1291 | * @name: the type name |
| 1292 | * @namespace: the type namespace |
| 1293 | * |
| 1294 | * Add an XML schema Element declaration |
| 1295 | * *WARNING* this interface is highly subject to change |
| 1296 | * |
| 1297 | * Returns the new struture or NULL in case of error |
| 1298 | */ |
| 1299 | static xmlSchemaElementPtr |
| 1300 | xmlSchemaAddElement(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 1301 | const xmlChar * name, const xmlChar * namespace) |
| 1302 | { |
| 1303 | xmlSchemaElementPtr ret = NULL; |
| 1304 | int val; |
| 1305 | |
| 1306 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1307 | return (NULL); |
| 1308 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1309 | #ifdef DEBUG |
| 1310 | fprintf(stderr, "Adding element %s\n", name); |
| 1311 | if (namespace != NULL) |
| 1312 | fprintf(stderr, " target namespace %s\n", namespace); |
| 1313 | #endif |
| 1314 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1315 | if (schema->elemDecl == NULL) |
| 1316 | schema->elemDecl = xmlHashCreate(10); |
| 1317 | if (schema->elemDecl == NULL) |
| 1318 | return (NULL); |
| 1319 | |
| 1320 | ret = (xmlSchemaElementPtr) xmlMalloc(sizeof(xmlSchemaElement)); |
| 1321 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1322 | xmlSchemaPErrMemory(ctxt, "allocating element", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1323 | return (NULL); |
| 1324 | } |
| 1325 | memset(ret, 0, sizeof(xmlSchemaElement)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1326 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
| 1327 | ret->targetNamespace = xmlDictLookup(ctxt->dict, namespace, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1328 | val = xmlHashAddEntry3(schema->elemDecl, name, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1329 | namespace, ctxt->container, ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1330 | if (val != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1331 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1332 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1333 | snprintf(buf, 99, "privatieelem %d", ctxt->counter++ + 1); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1334 | val = xmlHashAddEntry3(schema->elemDecl, name, (xmlChar *) buf, |
| 1335 | namespace, ret); |
| 1336 | if (val != 0) { |
| 1337 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1338 | XML_SCHEMAP_REDEFINED_ELEMENT, |
| 1339 | "Element %s already defined\n", |
| 1340 | name, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1341 | xmlFree(ret); |
| 1342 | return (NULL); |
| 1343 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1344 | } |
| 1345 | return (ret); |
| 1346 | } |
| 1347 | |
| 1348 | /** |
| 1349 | * xmlSchemaAddType: |
| 1350 | * @ctxt: a schema validation context |
| 1351 | * @schema: the schema being built |
| 1352 | * @name: the item name |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1353 | * @namespace: the namespace |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1354 | * |
| 1355 | * Add an XML schema Simple Type definition |
| 1356 | * *WARNING* this interface is highly subject to change |
| 1357 | * |
| 1358 | * Returns the new struture or NULL in case of error |
| 1359 | */ |
| 1360 | static xmlSchemaTypePtr |
| 1361 | xmlSchemaAddType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1362 | const xmlChar * name, const xmlChar * namespace) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1363 | { |
| 1364 | xmlSchemaTypePtr ret = NULL; |
| 1365 | int val; |
| 1366 | |
| 1367 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1368 | return (NULL); |
| 1369 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1370 | #ifdef DEBUG |
| 1371 | fprintf(stderr, "Adding type %s\n", name); |
| 1372 | if (namespace != NULL) |
| 1373 | fprintf(stderr, " target namespace %s\n", namespace); |
| 1374 | #endif |
| 1375 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1376 | if (schema->typeDecl == NULL) |
| 1377 | schema->typeDecl = xmlHashCreate(10); |
| 1378 | if (schema->typeDecl == NULL) |
| 1379 | return (NULL); |
| 1380 | |
| 1381 | ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType)); |
| 1382 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1383 | xmlSchemaPErrMemory(ctxt, "allocating type", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1384 | return (NULL); |
| 1385 | } |
| 1386 | memset(ret, 0, sizeof(xmlSchemaType)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1387 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 1388 | ret->redef = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1389 | val = xmlHashAddEntry2(schema->typeDecl, name, namespace, ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1390 | if (val != 0) { |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 1391 | if (ctxt->includes == 0) { |
| 1392 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1393 | XML_SCHEMAP_REDEFINED_TYPE, |
| 1394 | "Type %s already defined\n", |
| 1395 | name, NULL); |
| 1396 | xmlFree(ret); |
| 1397 | return (NULL); |
| 1398 | } else { |
| 1399 | xmlSchemaTypePtr prev; |
| 1400 | |
| 1401 | prev = xmlHashLookup2(schema->typeDecl, name, namespace); |
| 1402 | if (prev == NULL) { |
| 1403 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1404 | XML_ERR_INTERNAL_ERROR, |
| 1405 | "Internal error on type %s definition\n", |
| 1406 | name, NULL); |
| 1407 | xmlFree(ret); |
| 1408 | return (NULL); |
| 1409 | } |
| 1410 | ret->redef = prev->redef; |
| 1411 | prev->redef = ret; |
| 1412 | } |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 1413 | } |
| 1414 | ret->minOccurs = 1; |
| 1415 | ret->maxOccurs = 1; |
| 1416 | |
| 1417 | return (ret); |
| 1418 | } |
| 1419 | |
| 1420 | /** |
| 1421 | * xmlSchemaAddGroup: |
| 1422 | * @ctxt: a schema validation context |
| 1423 | * @schema: the schema being built |
| 1424 | * @name: the group name |
| 1425 | * |
| 1426 | * Add an XML schema Group definition |
| 1427 | * |
| 1428 | * Returns the new struture or NULL in case of error |
| 1429 | */ |
| 1430 | static xmlSchemaTypePtr |
| 1431 | xmlSchemaAddGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1432 | const xmlChar * name) |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 1433 | { |
| 1434 | xmlSchemaTypePtr ret = NULL; |
| 1435 | int val; |
| 1436 | |
| 1437 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1438 | return (NULL); |
| 1439 | |
| 1440 | if (schema->groupDecl == NULL) |
| 1441 | schema->groupDecl = xmlHashCreate(10); |
| 1442 | if (schema->groupDecl == NULL) |
| 1443 | return (NULL); |
| 1444 | |
| 1445 | ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType)); |
| 1446 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1447 | xmlSchemaPErrMemory(ctxt, "adding group", NULL); |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 1448 | return (NULL); |
| 1449 | } |
| 1450 | memset(ret, 0, sizeof(xmlSchemaType)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1451 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1452 | val = |
| 1453 | xmlHashAddEntry2(schema->groupDecl, name, schema->targetNamespace, |
| 1454 | ret); |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 1455 | if (val != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1456 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1457 | XML_SCHEMAP_REDEFINED_GROUP, |
| 1458 | "Group %s already defined\n", |
| 1459 | name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1460 | xmlFree(ret); |
| 1461 | return (NULL); |
| 1462 | } |
| 1463 | ret->minOccurs = 1; |
| 1464 | ret->maxOccurs = 1; |
| 1465 | |
| 1466 | return (ret); |
| 1467 | } |
| 1468 | |
| 1469 | /************************************************************************ |
| 1470 | * * |
| 1471 | * Utilities for parsing * |
| 1472 | * * |
| 1473 | ************************************************************************/ |
| 1474 | |
| 1475 | /** |
| 1476 | * xmlGetQNameProp: |
| 1477 | * @ctxt: a schema validation context |
| 1478 | * @node: a subtree containing XML Schema informations |
| 1479 | * @name: the attribute name |
| 1480 | * @namespace: the result namespace if any |
| 1481 | * |
| 1482 | * Extract a QName Attribute value |
| 1483 | * |
| 1484 | * Returns the NCName or NULL if not found, and also update @namespace |
| 1485 | * with the namespace URI |
| 1486 | */ |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1487 | static const xmlChar * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1488 | xmlGetQNameProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1489 | const char *name, const xmlChar ** namespace) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1490 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1491 | const xmlChar *val; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1492 | xmlNsPtr ns; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1493 | const xmlChar *ret, *prefix; |
| 1494 | int len; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1495 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1496 | *namespace = NULL; |
| 1497 | val = xmlSchemaGetProp(ctxt, node, name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1498 | if (val == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1499 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1500 | |
Daniel Veillard | ba0153a | 2004-04-01 10:42:31 +0000 | [diff] [blame] | 1501 | if (!strchr((char *) val, ':')) { |
Daniel Veillard | ebcdebd | 2004-03-05 00:15:50 +0000 | [diff] [blame] | 1502 | ns = xmlSearchNs(node->doc, node, 0); |
| 1503 | if (ns) { |
| 1504 | *namespace = xmlDictLookup(ctxt->dict, ns->href, -1); |
| 1505 | return (val); |
| 1506 | } |
| 1507 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1508 | ret = xmlSplitQName3(val, &len); |
| 1509 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1510 | return (val); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1511 | } |
| 1512 | ret = xmlDictLookup(ctxt->dict, ret, -1); |
| 1513 | prefix = xmlDictLookup(ctxt->dict, val, len); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1514 | |
| 1515 | ns = xmlSearchNs(node->doc, node, prefix); |
| 1516 | if (ns == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1517 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_PREFIX_UNDEFINED, |
| 1518 | "Attribute %s: the QName prefix %s is undefined\n", |
| 1519 | (const xmlChar *) name, prefix); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1520 | } else { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1521 | *namespace = xmlDictLookup(ctxt->dict, ns->href, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1522 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1523 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | /** |
| 1527 | * xmlGetMaxOccurs: |
| 1528 | * @ctxt: a schema validation context |
| 1529 | * @node: a subtree containing XML Schema informations |
| 1530 | * |
| 1531 | * Get the maxOccurs property |
| 1532 | * |
| 1533 | * Returns the default if not found, or the value |
| 1534 | */ |
| 1535 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1536 | xmlGetMaxOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node) |
| 1537 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1538 | const xmlChar *val, *cur; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1539 | int ret = 0; |
| 1540 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1541 | val = xmlSchemaGetProp(ctxt, node, "maxOccurs"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1542 | if (val == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1543 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1544 | |
| 1545 | if (xmlStrEqual(val, (const xmlChar *) "unbounded")) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1546 | return (UNBOUNDED); /* encoding it with -1 might be another option */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | cur = val; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1550 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1551 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1552 | while ((*cur >= '0') && (*cur <= '9')) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1553 | ret = ret * 10 + (*cur - '0'); |
| 1554 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1555 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1556 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1557 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1558 | if (*cur != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1559 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_INVALID_MAXOCCURS, |
| 1560 | "invalid value for maxOccurs: %s\n", val, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1561 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1562 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1563 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | /** |
| 1567 | * xmlGetMinOccurs: |
| 1568 | * @ctxt: a schema validation context |
| 1569 | * @node: a subtree containing XML Schema informations |
| 1570 | * |
| 1571 | * Get the minOccurs property |
| 1572 | * |
| 1573 | * Returns the default if not found, or the value |
| 1574 | */ |
| 1575 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1576 | xmlGetMinOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node) |
| 1577 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1578 | const xmlChar *val, *cur; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1579 | int ret = 0; |
| 1580 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1581 | val = xmlSchemaGetProp(ctxt, node, "minOccurs"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1582 | if (val == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1583 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1584 | |
| 1585 | cur = val; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1586 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1587 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1588 | while ((*cur >= '0') && (*cur <= '9')) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1589 | ret = ret * 10 + (*cur - '0'); |
| 1590 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1591 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1592 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1593 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1594 | if (*cur != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1595 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_INVALID_MINOCCURS, |
| 1596 | "invalid value for minOccurs: %s\n", val, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1597 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1598 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1599 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | /** |
| 1603 | * xmlGetBooleanProp: |
| 1604 | * @ctxt: a schema validation context |
| 1605 | * @node: a subtree containing XML Schema informations |
| 1606 | * @name: the attribute name |
| 1607 | * @def: the default value |
| 1608 | * |
| 1609 | * Get is a bolean property is set |
| 1610 | * |
| 1611 | * Returns the default if not found, 0 if found to be false, |
| 1612 | * 1 if found to be true |
| 1613 | */ |
| 1614 | static int |
| 1615 | xmlGetBooleanProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1616 | const char *name, int def) |
| 1617 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1618 | const xmlChar *val; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1619 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1620 | val = xmlSchemaGetProp(ctxt, node, name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1621 | if (val == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1622 | return (def); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1623 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1624 | if (xmlStrEqual(val, BAD_CAST "true")) |
| 1625 | def = 1; |
| 1626 | else if (xmlStrEqual(val, BAD_CAST "false")) |
| 1627 | def = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1628 | else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1629 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_INVALID_BOOLEAN, |
| 1630 | "Attribute %s: the value %s is not boolean\n", |
| 1631 | (const xmlChar *) name, val); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1632 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1633 | return (def); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | /************************************************************************ |
| 1637 | * * |
| 1638 | * Shema extraction from an Infoset * |
| 1639 | * * |
| 1640 | ************************************************************************/ |
| 1641 | static xmlSchemaTypePtr xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr |
| 1642 | ctxt, xmlSchemaPtr schema, |
| 1643 | xmlNodePtr node); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1644 | static xmlSchemaTypePtr xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr |
| 1645 | ctxt, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1646 | xmlSchemaPtr schema, |
| 1647 | xmlNodePtr node); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1648 | static xmlSchemaTypePtr xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr |
| 1649 | ctxt, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1650 | xmlSchemaPtr schema, |
| 1651 | xmlNodePtr node, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1652 | int simple); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1653 | static xmlSchemaTypePtr xmlSchemaParseSequence(xmlSchemaParserCtxtPtr ctxt, |
| 1654 | xmlSchemaPtr schema, |
| 1655 | xmlNodePtr node); |
| 1656 | static xmlSchemaTypePtr xmlSchemaParseAll(xmlSchemaParserCtxtPtr ctxt, |
| 1657 | xmlSchemaPtr schema, |
| 1658 | xmlNodePtr node); |
| 1659 | static xmlSchemaAttributePtr xmlSchemaParseAttribute(xmlSchemaParserCtxtPtr |
| 1660 | ctxt, |
| 1661 | xmlSchemaPtr schema, |
| 1662 | xmlNodePtr node); |
| 1663 | static xmlSchemaAttributeGroupPtr |
| 1664 | xmlSchemaParseAttributeGroup(xmlSchemaParserCtxtPtr ctxt, |
| 1665 | xmlSchemaPtr schema, xmlNodePtr node); |
| 1666 | static xmlSchemaTypePtr xmlSchemaParseChoice(xmlSchemaParserCtxtPtr ctxt, |
| 1667 | xmlSchemaPtr schema, |
| 1668 | xmlNodePtr node); |
| 1669 | static xmlSchemaTypePtr xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt, |
| 1670 | xmlSchemaPtr schema, |
| 1671 | xmlNodePtr node); |
| 1672 | static xmlSchemaAttributePtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1673 | xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt, |
| 1674 | xmlSchemaPtr schema, xmlNodePtr node); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1675 | |
| 1676 | /** |
| 1677 | * xmlSchemaParseAttrDecls: |
| 1678 | * @ctxt: a schema validation context |
| 1679 | * @schema: the schema being built |
| 1680 | * @node: a subtree containing XML Schema informations |
| 1681 | * @type: the hosting type |
| 1682 | * |
| 1683 | * parse a XML schema attrDecls declaration corresponding to |
| 1684 | * <!ENTITY % attrDecls |
| 1685 | * '((%attribute;| %attributeGroup;)*,(%anyAttribute;)?)'> |
| 1686 | */ |
| 1687 | static xmlNodePtr |
| 1688 | xmlSchemaParseAttrDecls(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 1689 | xmlNodePtr child, xmlSchemaTypePtr type) |
| 1690 | { |
| 1691 | xmlSchemaAttributePtr lastattr, attr; |
| 1692 | |
| 1693 | lastattr = NULL; |
| 1694 | while ((IS_SCHEMA(child, "attribute")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1695 | (IS_SCHEMA(child, "attributeGroup"))) { |
| 1696 | attr = NULL; |
| 1697 | if (IS_SCHEMA(child, "attribute")) { |
| 1698 | attr = xmlSchemaParseAttribute(ctxt, schema, child); |
| 1699 | } else if (IS_SCHEMA(child, "attributeGroup")) { |
| 1700 | attr = (xmlSchemaAttributePtr) |
| 1701 | xmlSchemaParseAttributeGroup(ctxt, schema, child); |
| 1702 | } |
| 1703 | if (attr != NULL) { |
| 1704 | if (lastattr == NULL) { |
| 1705 | type->attributes = attr; |
| 1706 | lastattr = attr; |
| 1707 | } else { |
| 1708 | lastattr->next = attr; |
| 1709 | lastattr = attr; |
| 1710 | } |
| 1711 | } |
| 1712 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1713 | } |
| 1714 | if (IS_SCHEMA(child, "anyAttribute")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1715 | attr = xmlSchemaParseAnyAttribute(ctxt, schema, child); |
| 1716 | if (attr != NULL) { |
| 1717 | if (lastattr == NULL) { |
| 1718 | type->attributes = attr; |
| 1719 | lastattr = attr; |
| 1720 | } else { |
| 1721 | lastattr->next = attr; |
| 1722 | lastattr = attr; |
| 1723 | } |
| 1724 | } |
| 1725 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1726 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1727 | return (child); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | /** |
| 1731 | * xmlSchemaParseAnnotation: |
| 1732 | * @ctxt: a schema validation context |
| 1733 | * @schema: the schema being built |
| 1734 | * @node: a subtree containing XML Schema informations |
| 1735 | * |
| 1736 | * parse a XML schema Attrribute declaration |
| 1737 | * *WARNING* this interface is highly subject to change |
| 1738 | * |
| 1739 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 1740 | * 1 in case of success. |
| 1741 | */ |
| 1742 | static xmlSchemaAnnotPtr |
| 1743 | xmlSchemaParseAnnotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 1744 | xmlNodePtr node) |
| 1745 | { |
| 1746 | xmlSchemaAnnotPtr ret; |
| 1747 | |
| 1748 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1749 | return (NULL); |
| 1750 | ret = xmlSchemaNewAnnot(ctxt, node); |
| 1751 | |
| 1752 | return (ret); |
| 1753 | } |
| 1754 | |
| 1755 | /** |
| 1756 | * xmlSchemaParseFacet: |
| 1757 | * @ctxt: a schema validation context |
| 1758 | * @schema: the schema being built |
| 1759 | * @node: a subtree containing XML Schema informations |
| 1760 | * |
| 1761 | * parse a XML schema Facet declaration |
| 1762 | * *WARNING* this interface is highly subject to change |
| 1763 | * |
| 1764 | * Returns the new type structure or NULL in case of error |
| 1765 | */ |
| 1766 | static xmlSchemaFacetPtr |
| 1767 | xmlSchemaParseFacet(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1768 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1769 | { |
| 1770 | xmlSchemaFacetPtr facet; |
| 1771 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1772 | const xmlChar *value; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1773 | |
| 1774 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1775 | return (NULL); |
| 1776 | |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 1777 | facet = xmlSchemaNewFacet(); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1778 | if (facet == NULL) { |
| 1779 | xmlSchemaPErrMemory(ctxt, "allocating facet", node); |
| 1780 | return (NULL); |
| 1781 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1782 | facet->node = node; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1783 | value = xmlSchemaGetProp(ctxt, node, "value"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1784 | if (value == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1785 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_FACET_NO_VALUE, |
| 1786 | "Facet %s has no value\n", node->name, NULL); |
| 1787 | xmlSchemaFreeFacet(facet); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1788 | return (NULL); |
| 1789 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1790 | if (IS_SCHEMA(node, "minInclusive")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1791 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1792 | } else if (IS_SCHEMA(node, "minExclusive")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1793 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1794 | } else if (IS_SCHEMA(node, "maxInclusive")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1795 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1796 | } else if (IS_SCHEMA(node, "maxExclusive")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1797 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1798 | } else if (IS_SCHEMA(node, "totalDigits")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1799 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1800 | } else if (IS_SCHEMA(node, "fractionDigits")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1801 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1802 | } else if (IS_SCHEMA(node, "pattern")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1803 | facet->type = XML_SCHEMA_FACET_PATTERN; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1804 | } else if (IS_SCHEMA(node, "enumeration")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1805 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1806 | } else if (IS_SCHEMA(node, "whiteSpace")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1807 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1808 | } else if (IS_SCHEMA(node, "length")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1809 | facet->type = XML_SCHEMA_FACET_LENGTH; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1810 | } else if (IS_SCHEMA(node, "maxLength")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1811 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
| 1812 | } else if (IS_SCHEMA(node, "minLength")) { |
| 1813 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
| 1814 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1815 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_TYPE, |
| 1816 | "Unknown facet type %s\n", node->name, NULL); |
| 1817 | xmlSchemaFreeFacet(facet); |
| 1818 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1819 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1820 | facet->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1821 | facet->value = value; |
| 1822 | child = node->children; |
| 1823 | |
| 1824 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1825 | facet->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 1826 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1827 | } |
| 1828 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1829 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_CHILD, |
| 1830 | "Facet %s has unexpected child content\n", |
| 1831 | node->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1832 | } |
| 1833 | return (facet); |
| 1834 | } |
| 1835 | |
| 1836 | /** |
| 1837 | * xmlSchemaParseAny: |
| 1838 | * @ctxt: a schema validation context |
| 1839 | * @schema: the schema being built |
| 1840 | * @node: a subtree containing XML Schema informations |
| 1841 | * |
| 1842 | * parse a XML schema Any declaration |
| 1843 | * *WARNING* this interface is highly subject to change |
| 1844 | * |
| 1845 | * Returns the new type structure or NULL in case of error |
| 1846 | */ |
| 1847 | static xmlSchemaTypePtr |
| 1848 | xmlSchemaParseAny(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 1849 | xmlNodePtr node) |
| 1850 | { |
| 1851 | xmlSchemaTypePtr type; |
| 1852 | xmlNodePtr child = NULL; |
| 1853 | xmlChar name[30]; |
| 1854 | |
| 1855 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1856 | return (NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1857 | snprintf((char *) name, 30, "any %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1858 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1859 | if (type == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1860 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1861 | type->node = node; |
| 1862 | type->type = XML_SCHEMA_TYPE_ANY; |
| 1863 | child = node->children; |
| 1864 | type->minOccurs = xmlGetMinOccurs(ctxt, node); |
| 1865 | type->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
| 1866 | |
| 1867 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1868 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 1869 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1870 | } |
| 1871 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1872 | xmlSchemaPErr2(ctxt, node, child, |
| 1873 | XML_SCHEMAP_UNKNOWN_SEQUENCE_CHILD, |
| 1874 | "Sequence %s has unexpected content\n", type->name, |
| 1875 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | return (type); |
| 1879 | } |
| 1880 | |
| 1881 | /** |
| 1882 | * xmlSchemaParseNotation: |
| 1883 | * @ctxt: a schema validation context |
| 1884 | * @schema: the schema being built |
| 1885 | * @node: a subtree containing XML Schema informations |
| 1886 | * |
| 1887 | * parse a XML schema Notation declaration |
| 1888 | * |
| 1889 | * Returns the new structure or NULL in case of error |
| 1890 | */ |
| 1891 | static xmlSchemaNotationPtr |
| 1892 | xmlSchemaParseNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1893 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1894 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1895 | const xmlChar *name; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1896 | xmlSchemaNotationPtr ret; |
| 1897 | xmlNodePtr child = NULL; |
| 1898 | |
| 1899 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1900 | return (NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1901 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1902 | if (name == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1903 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_NOTATION_NO_NAME, |
| 1904 | "Notation has no name\n", NULL, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1905 | return (NULL); |
| 1906 | } |
| 1907 | ret = xmlSchemaAddNotation(ctxt, schema, name); |
| 1908 | if (ret == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1909 | return (NULL); |
| 1910 | } |
| 1911 | child = node->children; |
| 1912 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1913 | ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 1914 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1915 | } |
| 1916 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1917 | xmlSchemaPErr2(ctxt, node, child, |
| 1918 | XML_SCHEMAP_UNKNOWN_NOTATION_CHILD, |
| 1919 | "notation %s has unexpected content\n", name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | return (ret); |
| 1923 | } |
| 1924 | |
| 1925 | /** |
| 1926 | * xmlSchemaParseAnyAttribute: |
| 1927 | * @ctxt: a schema validation context |
| 1928 | * @schema: the schema being built |
| 1929 | * @node: a subtree containing XML Schema informations |
| 1930 | * |
| 1931 | * parse a XML schema AnyAttrribute declaration |
| 1932 | * *WARNING* this interface is highly subject to change |
| 1933 | * |
| 1934 | * Returns an attribute def structure or NULL |
| 1935 | */ |
| 1936 | static xmlSchemaAttributePtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1937 | xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt, |
| 1938 | xmlSchemaPtr schema, xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1939 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1940 | const xmlChar *processContents; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1941 | xmlSchemaAttributePtr ret; |
| 1942 | xmlNodePtr child = NULL; |
| 1943 | char name[100]; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1944 | const xmlChar *local, *ns; |
| 1945 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1946 | |
| 1947 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1948 | return (NULL); |
| 1949 | |
| 1950 | snprintf(name, 99, "anyattr %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1951 | local = xmlSchemaGetNamespace(ctxt, schema, node, BAD_CAST "anyattr", &ns); |
| 1952 | ret = xmlSchemaAddAttribute(ctxt, schema, BAD_CAST name, ns); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1953 | if (ret == NULL) { |
| 1954 | return (NULL); |
| 1955 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1956 | ret->id = xmlSchemaGetProp(ctxt, node, "id"); |
| 1957 | processContents = xmlSchemaGetProp(ctxt, node, "processContents"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1958 | if ((processContents == NULL) |
| 1959 | || (xmlStrEqual(processContents, (const xmlChar *) "strict"))) { |
| 1960 | ret->occurs = XML_SCHEMAS_ANYATTR_STRICT; |
| 1961 | } else if (xmlStrEqual(processContents, (const xmlChar *) "skip")) { |
| 1962 | ret->occurs = XML_SCHEMAS_ANYATTR_SKIP; |
| 1963 | } else if (xmlStrEqual(processContents, (const xmlChar *) "lax")) { |
| 1964 | ret->occurs = XML_SCHEMAS_ANYATTR_LAX; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1965 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1966 | xmlSchemaPErr2(ctxt, node, child, |
| 1967 | XML_SCHEMAP_UNKNOWN_PROCESSCONTENT_CHILD, |
| 1968 | "anyAttribute has unexpected content for processContents: %s\n", |
| 1969 | processContents, NULL); |
| 1970 | ret->occurs = XML_SCHEMAS_ANYATTR_STRICT; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1971 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1972 | |
| 1973 | child = node->children; |
| 1974 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1975 | ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 1976 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1977 | } |
| 1978 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1979 | xmlSchemaPErr2(ctxt, node, child, |
| 1980 | XML_SCHEMAP_UNKNOWN_ANYATTRIBUTE_CHILD, |
| 1981 | "anyAttribute %s has unexpected content\n", |
| 1982 | (const xmlChar *) name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | return (ret); |
| 1986 | } |
| 1987 | |
| 1988 | |
| 1989 | /** |
| 1990 | * xmlSchemaParseAttribute: |
| 1991 | * @ctxt: a schema validation context |
| 1992 | * @schema: the schema being built |
| 1993 | * @node: a subtree containing XML Schema informations |
| 1994 | * |
| 1995 | * parse a XML schema Attrribute declaration |
| 1996 | * *WARNING* this interface is highly subject to change |
| 1997 | * |
| 1998 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 1999 | * 1 in case of success. |
| 2000 | */ |
| 2001 | static xmlSchemaAttributePtr |
| 2002 | xmlSchemaParseAttribute(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 2003 | xmlNodePtr node) |
| 2004 | { |
Daniel Veillard | c85d0fe | 2004-04-16 16:46:51 +0000 | [diff] [blame] | 2005 | const xmlChar *name, *refNs = NULL, *ref = NULL, *attrVal; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2006 | xmlSchemaAttributePtr ret; |
| 2007 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2008 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2009 | |
| 2010 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2011 | return (NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2012 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2013 | if (name == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2014 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2015 | ref = xmlGetQNameProp(ctxt, node, "ref", &refNs); |
| 2016 | if (ref == NULL) { |
| 2017 | xmlSchemaPErr2(ctxt, node, child, |
| 2018 | XML_SCHEMAP_ATTR_NONAME_NOREF, |
| 2019 | "Attribute has no name nor ref\n", NULL, NULL); |
| 2020 | return (NULL); |
| 2021 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2022 | snprintf(buf, 99, "anonattr %d", ctxt->counter++ + 1); |
| 2023 | name = (const xmlChar *) buf; |
| 2024 | ret = xmlSchemaAddAttribute(ctxt, schema, name, NULL); |
| 2025 | } else { |
| 2026 | const xmlChar *local, *ns; |
| 2027 | |
| 2028 | local = xmlSchemaGetNamespace(ctxt, schema, node, name, &ns); |
| 2029 | ret = xmlSchemaAddAttribute(ctxt, schema, local, ns); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2030 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2031 | if (ret == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2032 | return (NULL); |
| 2033 | } |
Daniel Veillard | c85d0fe | 2004-04-16 16:46:51 +0000 | [diff] [blame] | 2034 | |
| 2035 | /* Read the "use" attribute. */ |
| 2036 | attrVal = xmlSchemaGetProp(ctxt, node, "use"); |
| 2037 | if (attrVal != NULL) { |
| 2038 | if (xmlStrEqual(attrVal, BAD_CAST "optional")) |
| 2039 | ret->occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL; |
| 2040 | else if (xmlStrEqual(attrVal, BAD_CAST "prohibited")) |
| 2041 | ret->occurs = XML_SCHEMAS_ATTR_USE_PROHIBITED; |
| 2042 | else if (xmlStrEqual(attrVal, BAD_CAST "required")) |
| 2043 | ret->occurs = XML_SCHEMAS_ATTR_USE_REQUIRED; |
| 2044 | else |
| 2045 | xmlSchemaPErr(ctxt, node, |
| 2046 | XML_SCHEMAP_INVALID_ATTR_USE, |
| 2047 | "attribute %s has an invalid value for \"use\"\n", name, NULL); |
| 2048 | } else |
| 2049 | ret->occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL; |
| 2050 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2051 | ret->ref = ref; |
| 2052 | ret->refNs = refNs; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2053 | if ((ret->targetNamespace != NULL) && |
| 2054 | ((schema->flags & XML_SCHEMAS_QUALIF_ATTR) == 0) && |
| 2055 | (xmlStrEqual(ret->targetNamespace, schema->targetNamespace))) |
| 2056 | ret->flags |= XML_SCHEMAS_ATTR_NSDEFAULT; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2057 | ret->typeName = xmlGetQNameProp(ctxt, node, "type", &(ret->typeNs)); |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 2058 | ret->node = node; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2059 | child = node->children; |
| 2060 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2061 | ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2062 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2063 | } |
| 2064 | if (IS_SCHEMA(child, "simpleType")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2065 | ret->subtypes = xmlSchemaParseSimpleType(ctxt, schema, child); |
| 2066 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2067 | } |
| 2068 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2069 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_ATTR_CHILD, |
| 2070 | "attribute %s has unexpected content\n", name, |
| 2071 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | return (ret); |
| 2075 | } |
| 2076 | |
| 2077 | /** |
| 2078 | * xmlSchemaParseAttributeGroup: |
| 2079 | * @ctxt: a schema validation context |
| 2080 | * @schema: the schema being built |
| 2081 | * @node: a subtree containing XML Schema informations |
| 2082 | * |
| 2083 | * parse a XML schema Attribute Group declaration |
| 2084 | * *WARNING* this interface is highly subject to change |
| 2085 | * |
| 2086 | * Returns the attribute group or NULL in case of error. |
| 2087 | */ |
| 2088 | static xmlSchemaAttributeGroupPtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2089 | xmlSchemaParseAttributeGroup(xmlSchemaParserCtxtPtr ctxt, |
| 2090 | xmlSchemaPtr schema, xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2091 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2092 | const xmlChar *name, *refNs = NULL, *ref = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2093 | xmlSchemaAttributeGroupPtr ret; |
| 2094 | xmlSchemaAttributePtr last = NULL, attr; |
| 2095 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2096 | const xmlChar *oldcontainer; |
| 2097 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2098 | |
| 2099 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2100 | return (NULL); |
| 2101 | oldcontainer = ctxt->container; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2102 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2103 | if (name == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2104 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2105 | ref = xmlGetQNameProp(ctxt, node, "ref", &refNs); |
| 2106 | if (ref == NULL) { |
| 2107 | xmlSchemaPErr2(ctxt, node, child, |
| 2108 | XML_SCHEMAP_ATTRGRP_NONAME_NOREF, |
| 2109 | "AttributeGroup has no name nor ref\n", NULL, |
| 2110 | NULL); |
| 2111 | return (NULL); |
| 2112 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2113 | snprintf(buf, 99, "anonattrgroup %d", ctxt->counter++ + 1); |
| 2114 | name = (const xmlChar *) buf; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2115 | if (name == NULL) { |
| 2116 | xmlSchemaPErrMemory(ctxt, "creating attribute group", node); |
| 2117 | return (NULL); |
| 2118 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2119 | } |
| 2120 | ret = xmlSchemaAddAttributeGroup(ctxt, schema, name); |
| 2121 | if (ret == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2122 | return (NULL); |
| 2123 | } |
| 2124 | ret->ref = ref; |
| 2125 | ret->refNs = refNs; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 2126 | ret->type = XML_SCHEMA_TYPE_ATTRIBUTEGROUP; |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 2127 | ret->node = node; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2128 | child = node->children; |
| 2129 | ctxt->container = name; |
| 2130 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2131 | ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2132 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2133 | } |
| 2134 | while ((IS_SCHEMA(child, "attribute")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2135 | (IS_SCHEMA(child, "attributeGroup"))) { |
| 2136 | attr = NULL; |
| 2137 | if (IS_SCHEMA(child, "attribute")) { |
| 2138 | attr = xmlSchemaParseAttribute(ctxt, schema, child); |
| 2139 | } else if (IS_SCHEMA(child, "attributeGroup")) { |
| 2140 | attr = (xmlSchemaAttributePtr) |
| 2141 | xmlSchemaParseAttributeGroup(ctxt, schema, child); |
| 2142 | } |
| 2143 | if (attr != NULL) { |
| 2144 | if (last == NULL) { |
| 2145 | ret->attributes = attr; |
| 2146 | last = attr; |
| 2147 | } else { |
| 2148 | last->next = attr; |
| 2149 | last = attr; |
| 2150 | } |
| 2151 | } |
| 2152 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2153 | } |
| 2154 | if (IS_SCHEMA(child, "anyAttribute")) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2155 | TODO |
| 2156 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2157 | } |
| 2158 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2159 | xmlSchemaPErr2(ctxt, node, child, |
| 2160 | XML_SCHEMAP_UNKNOWN_ATTRGRP_CHILD, |
| 2161 | "attribute group %s has unexpected content\n", name, |
| 2162 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2163 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2164 | ctxt->container = oldcontainer; |
| 2165 | return (ret); |
| 2166 | } |
| 2167 | |
| 2168 | /** |
| 2169 | * xmlSchemaParseElement: |
| 2170 | * @ctxt: a schema validation context |
| 2171 | * @schema: the schema being built |
| 2172 | * @node: a subtree containing XML Schema informations |
| 2173 | * |
| 2174 | * parse a XML schema Element declaration |
| 2175 | * *WARNING* this interface is highly subject to change |
| 2176 | * |
| 2177 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2178 | * 1 in case of success. |
| 2179 | */ |
| 2180 | static xmlSchemaElementPtr |
| 2181 | xmlSchemaParseElement(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 2182 | xmlNodePtr node, int toplevel) |
| 2183 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2184 | const xmlChar *name, *fixed; |
| 2185 | const xmlChar *refNs = NULL, *ref = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2186 | xmlSchemaElementPtr ret; |
| 2187 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2188 | const xmlChar *oldcontainer; |
| 2189 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2190 | |
| 2191 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2192 | return (NULL); |
| 2193 | oldcontainer = ctxt->container; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2194 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2195 | if (name == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2196 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2197 | ref = xmlGetQNameProp(ctxt, node, "ref", &refNs); |
| 2198 | if (ref == NULL) { |
| 2199 | xmlSchemaPErr2(ctxt, node, child, |
| 2200 | XML_SCHEMAP_ELEM_NONAME_NOREF, |
| 2201 | "Element has no name nor ref\n", NULL, NULL); |
| 2202 | return (NULL); |
| 2203 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2204 | snprintf(buf, 99, "anonelem %d", ctxt->counter++ + 1); |
| 2205 | name = (const xmlChar *) buf; |
| 2206 | ret = xmlSchemaAddElement(ctxt, schema, name, NULL); |
| 2207 | } else { |
| 2208 | const xmlChar *local, *ns; |
| 2209 | |
| 2210 | local = xmlSchemaGetNamespace(ctxt, schema, node, name, &ns); |
| 2211 | ret = xmlSchemaAddElement(ctxt, schema, local, ns); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2212 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2213 | if (ret != NULL) |
| 2214 | ret->node = node; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2215 | if (ret == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2216 | return (NULL); |
| 2217 | } |
| 2218 | ret->type = XML_SCHEMA_TYPE_ELEMENT; |
| 2219 | ret->ref = ref; |
| 2220 | ret->refNs = refNs; |
| 2221 | if (ref != NULL) |
| 2222 | ret->flags |= XML_SCHEMAS_ELEM_REF; |
| 2223 | if (toplevel) |
| 2224 | ret->flags |= XML_SCHEMAS_ELEM_TOPLEVEL; |
| 2225 | if (xmlGetBooleanProp(ctxt, node, "nillable", 0)) |
| 2226 | ret->flags |= XML_SCHEMAS_ELEM_NILLABLE; |
| 2227 | if (xmlGetBooleanProp(ctxt, node, "abstract", 0)) |
| 2228 | ret->flags |= XML_SCHEMAS_ELEM_NILLABLE; |
| 2229 | ctxt->container = name; |
| 2230 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2231 | ret->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2232 | ret->namedType = |
| 2233 | xmlGetQNameProp(ctxt, node, "type", &(ret->namedTypeNs)); |
| 2234 | ret->substGroup = |
| 2235 | xmlGetQNameProp(ctxt, node, "substitutionGroup", |
| 2236 | &(ret->substGroupNs)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2237 | fixed = xmlSchemaGetProp(ctxt, node, "fixed"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2238 | ret->minOccurs = xmlGetMinOccurs(ctxt, node); |
| 2239 | ret->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2240 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2241 | ret->value = xmlSchemaGetProp(ctxt, node, "default"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2242 | if ((ret->value != NULL) && (fixed != NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2243 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_ELEM_DEFAULT_FIXED, |
| 2244 | "Element %s has both default and fixed\n", |
| 2245 | ret->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2246 | } else if (fixed != NULL) { |
| 2247 | ret->flags |= XML_SCHEMAS_ELEM_FIXED; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2248 | ret->value = fixed; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
| 2251 | child = node->children; |
| 2252 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2253 | ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2254 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2255 | } |
| 2256 | if (IS_SCHEMA(child, "complexType")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2257 | ret->subtypes = xmlSchemaParseComplexType(ctxt, schema, child); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2258 | child = child->next; |
| 2259 | } else if (IS_SCHEMA(child, "simpleType")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2260 | ret->subtypes = xmlSchemaParseSimpleType(ctxt, schema, child); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2261 | child = child->next; |
| 2262 | } |
| 2263 | while ((IS_SCHEMA(child, "unique")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2264 | (IS_SCHEMA(child, "key")) || (IS_SCHEMA(child, "keyref"))) { |
| 2265 | TODO child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2266 | } |
| 2267 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2268 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_ELEM_CHILD, |
| 2269 | "element %s has unexpected content\n", name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | ctxt->container = oldcontainer; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2273 | return (ret); |
| 2274 | } |
| 2275 | |
| 2276 | /** |
| 2277 | * xmlSchemaParseUnion: |
| 2278 | * @ctxt: a schema validation context |
| 2279 | * @schema: the schema being built |
| 2280 | * @node: a subtree containing XML Schema informations |
| 2281 | * |
| 2282 | * parse a XML schema Union definition |
| 2283 | * *WARNING* this interface is highly subject to change |
| 2284 | * |
| 2285 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2286 | * 1 in case of success. |
| 2287 | */ |
| 2288 | static xmlSchemaTypePtr |
| 2289 | xmlSchemaParseUnion(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2290 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2291 | { |
| 2292 | xmlSchemaTypePtr type, subtype, last = NULL; |
| 2293 | xmlNodePtr child = NULL; |
| 2294 | xmlChar name[30]; |
| 2295 | |
| 2296 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2297 | return (NULL); |
| 2298 | |
| 2299 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2300 | snprintf((char *) name, 30, "union %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2301 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2302 | if (type == NULL) |
| 2303 | return (NULL); |
| 2304 | type->node = node; |
Daniel Veillard | 377e1a9 | 2004-04-16 16:30:05 +0000 | [diff] [blame] | 2305 | type->type = XML_SCHEMA_TYPE_UNION; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2306 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
| 2307 | type->ref = xmlSchemaGetProp(ctxt, node, "memberTypes"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2308 | |
| 2309 | child = node->children; |
| 2310 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2311 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2312 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2313 | } |
| 2314 | while (IS_SCHEMA(child, "simpleType")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2315 | subtype = (xmlSchemaTypePtr) |
| 2316 | xmlSchemaParseSimpleType(ctxt, schema, child); |
| 2317 | if (subtype != NULL) { |
| 2318 | if (last == NULL) { |
| 2319 | type->subtypes = subtype; |
| 2320 | last = subtype; |
| 2321 | } else { |
| 2322 | last->next = subtype; |
| 2323 | last = subtype; |
| 2324 | } |
| 2325 | last->next = NULL; |
| 2326 | } |
| 2327 | child = child->next; |
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_UNION_CHILD, |
| 2331 | "Union %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 | } |
| 2336 | |
| 2337 | /** |
| 2338 | * xmlSchemaParseList: |
| 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 List 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 | xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2351 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2352 | { |
| 2353 | xmlSchemaTypePtr type, subtype; |
| 2354 | xmlNodePtr child = NULL; |
| 2355 | xmlChar name[30]; |
| 2356 | |
| 2357 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2358 | return (NULL); |
| 2359 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2360 | snprintf((char *) name, 30, "list %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2361 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2362 | if (type == NULL) |
| 2363 | return (NULL); |
| 2364 | type->node = node; |
| 2365 | type->type = XML_SCHEMA_TYPE_LIST; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2366 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2367 | type->ref = xmlGetQNameProp(ctxt, node, "ref", &(type->refNs)); |
| 2368 | |
| 2369 | child = node->children; |
| 2370 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2371 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2372 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2373 | } |
| 2374 | subtype = NULL; |
| 2375 | if (IS_SCHEMA(child, "simpleType")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2376 | subtype = (xmlSchemaTypePtr) |
| 2377 | xmlSchemaParseSimpleType(ctxt, schema, child); |
| 2378 | child = child->next; |
| 2379 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2380 | } |
| 2381 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2382 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_LIST_CHILD, |
| 2383 | "List %s has unexpected content\n", type->name, |
| 2384 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2385 | } |
| 2386 | return (type); |
| 2387 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2388 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2389 | /** |
| 2390 | * xmlSchemaParseSimpleType: |
| 2391 | * @ctxt: a schema validation context |
| 2392 | * @schema: the schema being built |
| 2393 | * @node: a subtree containing XML Schema informations |
| 2394 | * |
| 2395 | * parse a XML schema Simple Type definition |
| 2396 | * *WARNING* this interface is highly subject to change |
| 2397 | * |
| 2398 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2399 | * 1 in case of success. |
| 2400 | */ |
| 2401 | static xmlSchemaTypePtr |
| 2402 | xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 2403 | xmlNodePtr node) |
| 2404 | { |
| 2405 | xmlSchemaTypePtr type, subtype; |
| 2406 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2407 | const xmlChar *name; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2408 | |
| 2409 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2410 | return (NULL); |
| 2411 | |
| 2412 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2413 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2414 | if (name == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2415 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2416 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2417 | snprintf(buf, 99, "simpletype %d", ctxt->counter++ + 1); |
| 2418 | type = xmlSchemaAddType(ctxt, schema, (const xmlChar *)buf, NULL); |
| 2419 | } else { |
| 2420 | const xmlChar *local, *ns; |
| 2421 | |
| 2422 | local = xmlSchemaGetNamespace(ctxt, schema, node, name, &ns); |
| 2423 | type = xmlSchemaAddType(ctxt, schema, local, ns); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2424 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2425 | if (type == NULL) |
| 2426 | return (NULL); |
| 2427 | type->node = node; |
| 2428 | type->type = XML_SCHEMA_TYPE_SIMPLE; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2429 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2430 | |
| 2431 | child = node->children; |
| 2432 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2433 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2434 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2435 | } |
| 2436 | subtype = NULL; |
| 2437 | if (IS_SCHEMA(child, "restriction")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2438 | subtype = (xmlSchemaTypePtr) |
| 2439 | xmlSchemaParseRestriction(ctxt, schema, child, 1); |
| 2440 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2441 | } else if (IS_SCHEMA(child, "list")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2442 | subtype = (xmlSchemaTypePtr) |
| 2443 | xmlSchemaParseList(ctxt, schema, child); |
| 2444 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2445 | } else if (IS_SCHEMA(child, "union")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2446 | subtype = (xmlSchemaTypePtr) |
| 2447 | xmlSchemaParseUnion(ctxt, schema, child); |
| 2448 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2449 | } |
| 2450 | type->subtypes = subtype; |
| 2451 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2452 | xmlSchemaPErr2(ctxt, node, child, |
| 2453 | XML_SCHEMAP_UNKNOWN_SIMPLETYPE_CHILD, |
| 2454 | "SimpleType %s has unexpected content\n", |
| 2455 | type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
| 2458 | return (type); |
| 2459 | } |
| 2460 | |
| 2461 | |
| 2462 | /** |
| 2463 | * xmlSchemaParseGroup: |
| 2464 | * @ctxt: a schema validation context |
| 2465 | * @schema: the schema being built |
| 2466 | * @node: a subtree containing XML Schema informations |
| 2467 | * |
| 2468 | * parse a XML schema Group definition |
| 2469 | * *WARNING* this interface is highly subject to change |
| 2470 | * |
| 2471 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2472 | * 1 in case of success. |
| 2473 | */ |
| 2474 | static xmlSchemaTypePtr |
| 2475 | xmlSchemaParseGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2476 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2477 | { |
| 2478 | xmlSchemaTypePtr type, subtype; |
| 2479 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2480 | const xmlChar *name; |
| 2481 | const xmlChar *ref = NULL, *refNs = NULL; |
| 2482 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2483 | |
| 2484 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2485 | return (NULL); |
| 2486 | |
| 2487 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2488 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2489 | if (name == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2490 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2491 | ref = xmlGetQNameProp(ctxt, node, "ref", &refNs); |
| 2492 | if (ref == NULL) { |
| 2493 | xmlSchemaPErr2(ctxt, node, child, |
| 2494 | XML_SCHEMAP_GROUP_NONAME_NOREF, |
| 2495 | "Group has no name nor ref\n", NULL, NULL); |
| 2496 | return (NULL); |
| 2497 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2498 | if (refNs == NULL) |
| 2499 | refNs = schema->targetNamespace; |
| 2500 | snprintf(buf, 99, "anongroup %d", ctxt->counter++ + 1); |
| 2501 | name = (const xmlChar *) buf; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2502 | } |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 2503 | type = xmlSchemaAddGroup(ctxt, schema, name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2504 | if (type == NULL) |
| 2505 | return (NULL); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2506 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2507 | type->node = node; |
| 2508 | type->type = XML_SCHEMA_TYPE_GROUP; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2509 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2510 | type->ref = ref; |
| 2511 | type->refNs = refNs; |
| 2512 | type->minOccurs = xmlGetMinOccurs(ctxt, node); |
| 2513 | type->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
| 2514 | |
| 2515 | child = node->children; |
| 2516 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2517 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2518 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2519 | } |
| 2520 | subtype = NULL; |
| 2521 | if (IS_SCHEMA(child, "all")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2522 | subtype = (xmlSchemaTypePtr) |
| 2523 | xmlSchemaParseAll(ctxt, schema, child); |
| 2524 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2525 | } else if (IS_SCHEMA(child, "choice")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2526 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 2527 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2528 | } else if (IS_SCHEMA(child, "sequence")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2529 | subtype = (xmlSchemaTypePtr) |
| 2530 | xmlSchemaParseSequence(ctxt, schema, child); |
| 2531 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2532 | } |
| 2533 | if (subtype != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2534 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2535 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2536 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_GROUP_CHILD, |
| 2537 | "Group %s has unexpected content\n", type->name, |
| 2538 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
| 2541 | return (type); |
| 2542 | } |
| 2543 | |
| 2544 | /** |
| 2545 | * xmlSchemaParseAll: |
| 2546 | * @ctxt: a schema validation context |
| 2547 | * @schema: the schema being built |
| 2548 | * @node: a subtree containing XML Schema informations |
| 2549 | * |
| 2550 | * parse a XML schema All definition |
| 2551 | * *WARNING* this interface is highly subject to change |
| 2552 | * |
| 2553 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2554 | * 1 in case of success. |
| 2555 | */ |
| 2556 | static xmlSchemaTypePtr |
| 2557 | xmlSchemaParseAll(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2558 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2559 | { |
| 2560 | xmlSchemaTypePtr type, subtype, last = NULL; |
| 2561 | xmlNodePtr child = NULL; |
| 2562 | xmlChar name[30]; |
| 2563 | |
| 2564 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2565 | return (NULL); |
| 2566 | |
| 2567 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2568 | snprintf((char *) name, 30, "all%d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2569 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2570 | if (type == NULL) |
| 2571 | return (NULL); |
| 2572 | type->node = node; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 2573 | type->type = XML_SCHEMA_TYPE_ALL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2574 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2575 | type->minOccurs = xmlGetMinOccurs(ctxt, node); |
William M. Brack | b15351e | 2003-12-27 04:34:42 +0000 | [diff] [blame] | 2576 | if (type->minOccurs > 1) |
| 2577 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_INVALID_MINOCCURS, |
| 2578 | "invalid value for minOccurs (must be 0 or 1)\n", NULL, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2579 | type->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
William M. Brack | b15351e | 2003-12-27 04:34:42 +0000 | [diff] [blame] | 2580 | if (type->maxOccurs > 1) |
| 2581 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_INVALID_MAXOCCURS, |
| 2582 | "invalid value for maxOccurs (must be 0 or 1)\n", NULL, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2583 | |
| 2584 | child = node->children; |
| 2585 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2586 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2587 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2588 | } |
| 2589 | while (IS_SCHEMA(child, "element")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2590 | subtype = (xmlSchemaTypePtr) |
| 2591 | xmlSchemaParseElement(ctxt, schema, child, 0); |
| 2592 | if (subtype != NULL) { |
William M. Brack | b15351e | 2003-12-27 04:34:42 +0000 | [diff] [blame] | 2593 | if (subtype->minOccurs > 1) |
| 2594 | xmlSchemaPErr(ctxt, child, XML_SCHEMAP_INVALID_MINOCCURS, |
| 2595 | "invalid value for minOccurs (must be 0 or 1)\n", |
| 2596 | NULL, NULL); |
| 2597 | if (subtype->maxOccurs > 1) |
| 2598 | xmlSchemaPErr(ctxt, child, XML_SCHEMAP_INVALID_MAXOCCURS, |
| 2599 | "invalid value for maxOccurs (must be 0 or 1)\n", |
| 2600 | NULL, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2601 | if (last == NULL) { |
| 2602 | type->subtypes = subtype; |
| 2603 | last = subtype; |
| 2604 | } else { |
| 2605 | last->next = subtype; |
| 2606 | last = subtype; |
| 2607 | } |
| 2608 | last->next = NULL; |
| 2609 | } |
| 2610 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2611 | } |
| 2612 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2613 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_ALL_CHILD, |
| 2614 | "All %s has unexpected content\n", type->name, |
| 2615 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2616 | } |
| 2617 | |
| 2618 | return (type); |
| 2619 | } |
| 2620 | |
| 2621 | /** |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2622 | * xmlSchemaImportSchema |
| 2623 | * |
| 2624 | * @ctxt: a schema validation context |
| 2625 | * @schemaLocation: an URI defining where to find the imported schema |
| 2626 | * |
| 2627 | * import a XML schema |
| 2628 | * *WARNING* this interface is highly subject to change |
| 2629 | * |
| 2630 | * Returns -1 in case of error and 1 in case of success. |
| 2631 | */ |
| 2632 | static xmlSchemaImportPtr |
| 2633 | xmlSchemaImportSchema(xmlSchemaParserCtxtPtr ctxt, |
| 2634 | const xmlChar *schemaLocation) |
| 2635 | { |
| 2636 | xmlSchemaImportPtr import; |
| 2637 | xmlSchemaParserCtxtPtr newctxt; |
| 2638 | |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2639 | newctxt = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt)); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2640 | if (newctxt == NULL) { |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2641 | xmlSchemaPErrMemory(ctxt, "allocating schama parser context", |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2642 | NULL); |
| 2643 | return (NULL); |
| 2644 | } |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2645 | memset(newctxt, 0, sizeof(xmlSchemaParserCtxt)); |
| 2646 | /* Keep the same dictionnary for parsing, really */ |
| 2647 | xmlDictReference(ctxt->dict); |
| 2648 | newctxt->dict = ctxt->dict; |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 2649 | newctxt->includes = 0; |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2650 | newctxt->URL = xmlDictLookup(newctxt->dict, schemaLocation, -1); |
| 2651 | |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2652 | xmlSchemaSetParserErrors(newctxt, ctxt->error, ctxt->warning, |
| 2653 | ctxt->userData); |
| 2654 | |
| 2655 | import = (xmlSchemaImport*) xmlMalloc(sizeof(xmlSchemaImport)); |
| 2656 | if (import == NULL) { |
| 2657 | xmlSchemaPErrMemory(NULL, "allocating imported schema", |
| 2658 | NULL); |
| 2659 | xmlSchemaFreeParserCtxt(newctxt); |
| 2660 | return (NULL); |
| 2661 | } |
| 2662 | |
| 2663 | memset(import, 0, sizeof(xmlSchemaImport)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2664 | import->schemaLocation = xmlDictLookup(ctxt->dict, schemaLocation, -1); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2665 | import->schema = xmlSchemaParse(newctxt); |
| 2666 | |
| 2667 | if (import->schema == NULL) { |
| 2668 | /* FIXME use another error enum here ? */ |
| 2669 | xmlSchemaPErr(ctxt, NULL, XML_SCHEMAS_ERR_INTERNAL, |
| 2670 | "failed to import schema at location %s\n", |
| 2671 | schemaLocation, NULL); |
| 2672 | |
| 2673 | xmlSchemaFreeParserCtxt(newctxt); |
| 2674 | if (import->schemaLocation != NULL) |
| 2675 | xmlFree((xmlChar *)import->schemaLocation); |
| 2676 | xmlFree(import); |
| 2677 | return NULL; |
| 2678 | } |
| 2679 | |
| 2680 | xmlSchemaFreeParserCtxt(newctxt); |
| 2681 | return import; |
| 2682 | } |
| 2683 | |
| 2684 | |
| 2685 | /** |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2686 | * xmlSchemaParseImport: |
| 2687 | * @ctxt: a schema validation context |
| 2688 | * @schema: the schema being built |
| 2689 | * @node: a subtree containing XML Schema informations |
| 2690 | * |
| 2691 | * parse a XML schema Import definition |
| 2692 | * *WARNING* this interface is highly subject to change |
| 2693 | * |
| 2694 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2695 | * 1 in case of success. |
| 2696 | */ |
| 2697 | static int |
| 2698 | xmlSchemaParseImport(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2699 | xmlNodePtr node) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2700 | { |
| 2701 | xmlNodePtr child = NULL; |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2702 | xmlSchemaImportPtr import = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2703 | const xmlChar *namespace; |
| 2704 | const xmlChar *schemaLocation; |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2705 | const xmlChar *previous; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2706 | xmlURIPtr check; |
| 2707 | |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2708 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2709 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2710 | return (-1); |
| 2711 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2712 | namespace = xmlSchemaGetProp(ctxt, node, "namespace"); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2713 | if (namespace != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2714 | check = xmlParseURI((const char *) namespace); |
| 2715 | if (check == NULL) { |
| 2716 | xmlSchemaPErr2(ctxt, node, child, |
| 2717 | XML_SCHEMAP_IMPORT_NAMESPACE_NOT_URI, |
| 2718 | "Import namespace attribute is not an URI: %s\n", |
| 2719 | namespace, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2720 | return (-1); |
| 2721 | } else { |
| 2722 | xmlFreeURI(check); |
| 2723 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2724 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2725 | schemaLocation = xmlSchemaGetProp(ctxt, node, "schemaLocation"); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2726 | if (schemaLocation != NULL) { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2727 | xmlChar *base = NULL; |
| 2728 | xmlChar *URI = NULL; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2729 | check = xmlParseURI((const char *) schemaLocation); |
| 2730 | if (check == NULL) { |
| 2731 | xmlSchemaPErr2(ctxt, node, child, |
| 2732 | XML_SCHEMAP_IMPORT_SCHEMA_NOT_URI, |
| 2733 | "Import schemaLocation attribute is not an URI: %s\n", |
| 2734 | schemaLocation, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2735 | return (-1); |
| 2736 | } else { |
| 2737 | xmlFreeURI(check); |
| 2738 | } |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2739 | base = xmlNodeGetBase(node->doc, node); |
| 2740 | if (base == NULL) { |
| 2741 | URI = xmlBuildURI(schemaLocation, node->doc->URL); |
| 2742 | } else { |
| 2743 | URI = xmlBuildURI(schemaLocation, base); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2744 | xmlFree(base); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2745 | } |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2746 | if (URI != NULL) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2747 | schemaLocation = xmlDictLookup(ctxt->dict, URI, -1); |
| 2748 | xmlFree(URI); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2749 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2750 | } |
| 2751 | if (schema->schemasImports == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2752 | schema->schemasImports = xmlHashCreate(10); |
| 2753 | if (schema->schemasImports == NULL) { |
| 2754 | xmlSchemaPErr2(ctxt, node, child, |
| 2755 | XML_SCHEMAP_FAILED_BUILD_IMPORT, |
| 2756 | "Internal: failed to build import table\n", |
| 2757 | NULL, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2758 | return (-1); |
| 2759 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2760 | } |
| 2761 | if (namespace == NULL) { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2762 | import = xmlHashLookup(schema->schemasImports, |
| 2763 | XML_SCHEMAS_DEFAULT_NAMESPACE); |
| 2764 | if (import != NULL) |
| 2765 | previous = import->schemaLocation; |
| 2766 | else |
| 2767 | previous = NULL; |
| 2768 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2769 | if (schemaLocation != NULL) { |
| 2770 | if (previous != NULL) { |
| 2771 | if (!xmlStrEqual(schemaLocation, previous)) { |
| 2772 | xmlSchemaPErr2(ctxt, node, child, |
| 2773 | XML_SCHEMAP_IMPORT_REDEFINE_NSNAME, |
| 2774 | "Redefining import for default namespace with a different URI: %s\n", |
| 2775 | schemaLocation, NULL); |
| 2776 | } |
| 2777 | } else { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2778 | import = xmlSchemaImportSchema(ctxt, schemaLocation); |
| 2779 | if (import == NULL) { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2780 | return (-1); |
| 2781 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2782 | xmlHashAddEntry(schema->schemasImports, |
| 2783 | XML_SCHEMAS_DEFAULT_NAMESPACE, |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2784 | import); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2785 | } |
| 2786 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2787 | } else { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2788 | import = xmlHashLookup(schema->schemasImports, namespace); |
| 2789 | if (import != NULL) |
| 2790 | previous = import->schemaLocation; |
| 2791 | else |
| 2792 | previous = NULL; |
| 2793 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2794 | if (schemaLocation != NULL) { |
| 2795 | if (previous != NULL) { |
| 2796 | if (!xmlStrEqual(schemaLocation, previous)) { |
| 2797 | xmlSchemaPErr2(ctxt, node, child, |
| 2798 | XML_SCHEMAP_IMPORT_REDEFINE_NSNAME, |
| 2799 | "Redefining import for namespace %s with a different URI: %s\n", |
| 2800 | namespace, schemaLocation); |
| 2801 | } |
| 2802 | } else { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2803 | import = xmlSchemaImportSchema(ctxt, schemaLocation); |
| 2804 | if (import == NULL) { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2805 | return (-1); |
| 2806 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2807 | xmlHashAddEntry(schema->schemasImports, |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2808 | namespace, import); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2809 | } |
| 2810 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2811 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2812 | |
| 2813 | child = node->children; |
| 2814 | while (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2815 | /* |
| 2816 | * the annotations here are simply discarded ... |
| 2817 | */ |
| 2818 | child = child->next; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2819 | } |
| 2820 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2821 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_IMPORT_CHILD, |
| 2822 | "Import has unexpected content\n", NULL, NULL); |
| 2823 | return (-1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2824 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2825 | return (1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | /** |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2829 | * xmlSchemaCleanupDoc: |
| 2830 | * @ctxt: a schema validation context |
| 2831 | * @node: the root of the document. |
| 2832 | * |
| 2833 | * removes unwanted nodes in a schemas document tree |
| 2834 | */ |
| 2835 | static void |
| 2836 | xmlSchemaCleanupDoc(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr root) |
| 2837 | { |
| 2838 | xmlNodePtr delete, cur; |
| 2839 | |
| 2840 | if ((ctxt == NULL) || (root == NULL)) return; |
| 2841 | |
| 2842 | /* |
| 2843 | * Remove all the blank text nodes |
| 2844 | */ |
| 2845 | delete = NULL; |
| 2846 | cur = root; |
| 2847 | while (cur != NULL) { |
| 2848 | if (delete != NULL) { |
| 2849 | xmlUnlinkNode(delete); |
| 2850 | xmlFreeNode(delete); |
| 2851 | delete = NULL; |
| 2852 | } |
| 2853 | if (cur->type == XML_TEXT_NODE) { |
| 2854 | if (IS_BLANK_NODE(cur)) { |
| 2855 | if (xmlNodeGetSpacePreserve(cur) != 1) { |
| 2856 | delete = cur; |
| 2857 | } |
| 2858 | } |
| 2859 | } else if ((cur->type != XML_ELEMENT_NODE) && |
| 2860 | (cur->type != XML_CDATA_SECTION_NODE)) { |
| 2861 | delete = cur; |
| 2862 | goto skip_children; |
| 2863 | } |
| 2864 | |
| 2865 | /* |
| 2866 | * Skip to next node |
| 2867 | */ |
| 2868 | if (cur->children != NULL) { |
| 2869 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 2870 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 2871 | (cur->children->type != XML_ENTITY_NODE)) { |
| 2872 | cur = cur->children; |
| 2873 | continue; |
| 2874 | } |
| 2875 | } |
| 2876 | skip_children: |
| 2877 | if (cur->next != NULL) { |
| 2878 | cur = cur->next; |
| 2879 | continue; |
| 2880 | } |
| 2881 | |
| 2882 | do { |
| 2883 | cur = cur->parent; |
| 2884 | if (cur == NULL) |
| 2885 | break; |
| 2886 | if (cur == root) { |
| 2887 | cur = NULL; |
| 2888 | break; |
| 2889 | } |
| 2890 | if (cur->next != NULL) { |
| 2891 | cur = cur->next; |
| 2892 | break; |
| 2893 | } |
| 2894 | } while (cur != NULL); |
| 2895 | } |
| 2896 | if (delete != NULL) { |
| 2897 | xmlUnlinkNode(delete); |
| 2898 | xmlFreeNode(delete); |
| 2899 | delete = NULL; |
| 2900 | } |
| 2901 | } |
| 2902 | |
| 2903 | /** |
| 2904 | * xmlSchemaParseSchemaTopLevel: |
| 2905 | * @ctxt: a schema validation context |
| 2906 | * @schema: the schemas |
| 2907 | * @nodes: the list of top level nodes |
| 2908 | * |
| 2909 | * Returns the internal XML Schema structure built from the resource or |
| 2910 | * NULL in case of error |
| 2911 | */ |
| 2912 | static void |
| 2913 | xmlSchemaParseSchemaTopLevel(xmlSchemaParserCtxtPtr ctxt, |
| 2914 | xmlSchemaPtr schema, xmlNodePtr nodes) |
| 2915 | { |
| 2916 | xmlNodePtr child; |
| 2917 | xmlSchemaAnnotPtr annot; |
| 2918 | |
| 2919 | if ((ctxt == NULL) || (schema == NULL) || (nodes == NULL)) |
| 2920 | return; |
| 2921 | |
| 2922 | child = nodes; |
| 2923 | while ((IS_SCHEMA(child, "include")) || |
| 2924 | (IS_SCHEMA(child, "import")) || |
| 2925 | (IS_SCHEMA(child, "redefine")) || |
| 2926 | (IS_SCHEMA(child, "annotation"))) { |
| 2927 | if (IS_SCHEMA(child, "annotation")) { |
| 2928 | annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2929 | if (schema->annot == NULL) |
| 2930 | schema->annot = annot; |
| 2931 | else |
| 2932 | xmlSchemaFreeAnnot(annot); |
| 2933 | } else if (IS_SCHEMA(child, "import")) { |
| 2934 | xmlSchemaParseImport(ctxt, schema, child); |
| 2935 | } else if (IS_SCHEMA(child, "include")) { |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 2936 | ctxt->includes++; |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2937 | xmlSchemaParseInclude(ctxt, schema, child); |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 2938 | ctxt->includes--; |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2939 | } else if (IS_SCHEMA(child, "redefine")) { |
| 2940 | TODO |
| 2941 | } |
| 2942 | child = child->next; |
| 2943 | } |
| 2944 | while (child != NULL) { |
| 2945 | if (IS_SCHEMA(child, "complexType")) { |
| 2946 | xmlSchemaParseComplexType(ctxt, schema, child); |
| 2947 | child = child->next; |
| 2948 | } else if (IS_SCHEMA(child, "simpleType")) { |
| 2949 | xmlSchemaParseSimpleType(ctxt, schema, child); |
| 2950 | child = child->next; |
| 2951 | } else if (IS_SCHEMA(child, "element")) { |
| 2952 | xmlSchemaParseElement(ctxt, schema, child, 1); |
| 2953 | child = child->next; |
| 2954 | } else if (IS_SCHEMA(child, "attribute")) { |
| 2955 | xmlSchemaParseAttribute(ctxt, schema, child); |
| 2956 | child = child->next; |
| 2957 | } else if (IS_SCHEMA(child, "attributeGroup")) { |
| 2958 | xmlSchemaParseAttributeGroup(ctxt, schema, child); |
| 2959 | child = child->next; |
| 2960 | } else if (IS_SCHEMA(child, "group")) { |
| 2961 | xmlSchemaParseGroup(ctxt, schema, child); |
| 2962 | child = child->next; |
| 2963 | } else if (IS_SCHEMA(child, "notation")) { |
| 2964 | xmlSchemaParseNotation(ctxt, schema, child); |
| 2965 | child = child->next; |
| 2966 | } else { |
| 2967 | xmlSchemaPErr2(ctxt, NULL, child, |
| 2968 | XML_SCHEMAP_UNKNOWN_SCHEMAS_CHILD, |
| 2969 | "Schemas: unexpected element %s here \n", |
| 2970 | child->name, NULL); |
| 2971 | child = child->next; |
| 2972 | } |
| 2973 | while (IS_SCHEMA(child, "annotation")) { |
| 2974 | annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2975 | if (schema->annot == NULL) |
| 2976 | schema->annot = annot; |
| 2977 | else |
| 2978 | xmlSchemaFreeAnnot(annot); |
| 2979 | child = child->next; |
| 2980 | } |
| 2981 | } |
| 2982 | } |
| 2983 | |
| 2984 | /** |
| 2985 | * xmlSchemaParseInclude: |
| 2986 | * @ctxt: a schema validation context |
| 2987 | * @schema: the schema being built |
| 2988 | * @node: a subtree containing XML Schema informations |
| 2989 | * |
| 2990 | * parse a XML schema Include definition |
| 2991 | * |
| 2992 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2993 | * 1 in case of success. |
| 2994 | */ |
| 2995 | static int |
| 2996 | xmlSchemaParseInclude(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 2997 | xmlNodePtr node) |
| 2998 | { |
| 2999 | xmlNodePtr child = NULL; |
| 3000 | const xmlChar *schemaLocation; |
| 3001 | xmlURIPtr check; |
| 3002 | xmlDocPtr doc; |
| 3003 | xmlNodePtr root; |
| 3004 | xmlSchemaIncludePtr include; |
| 3005 | |
| 3006 | |
| 3007 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3008 | return (-1); |
| 3009 | |
| 3010 | /* |
| 3011 | * Preliminary step, extract the URI-Reference for the include and |
| 3012 | * make an URI from the base. |
| 3013 | */ |
| 3014 | schemaLocation = xmlSchemaGetProp(ctxt, node, "schemaLocation"); |
| 3015 | if (schemaLocation != NULL) { |
| 3016 | xmlChar *base = NULL; |
| 3017 | xmlChar *URI = NULL; |
| 3018 | check = xmlParseURI((const char *) schemaLocation); |
| 3019 | if (check == NULL) { |
| 3020 | xmlSchemaPErr2(ctxt, node, child, |
| 3021 | XML_SCHEMAP_INCLUDE_SCHEMA_NOT_URI, |
| 3022 | "Include schemaLocation attribute is not an URI: %s\n", |
| 3023 | schemaLocation, NULL); |
| 3024 | return (-1); |
| 3025 | } else { |
| 3026 | xmlFreeURI(check); |
| 3027 | } |
| 3028 | base = xmlNodeGetBase(node->doc, node); |
| 3029 | if (base == NULL) { |
| 3030 | URI = xmlBuildURI(schemaLocation, node->doc->URL); |
| 3031 | } else { |
| 3032 | URI = xmlBuildURI(schemaLocation, base); |
| 3033 | xmlFree(base); |
| 3034 | } |
| 3035 | if (URI != NULL) { |
| 3036 | schemaLocation = xmlDictLookup(ctxt->dict, URI, -1); |
| 3037 | xmlFree(URI); |
| 3038 | } |
| 3039 | } else { |
| 3040 | xmlSchemaPErr2(ctxt, node, child, |
| 3041 | XML_SCHEMAP_INCLUDE_SCHEMA_NO_URI, |
| 3042 | "Include schemaLocation attribute missing\n", |
| 3043 | NULL, NULL); |
| 3044 | return (-1); |
| 3045 | } |
| 3046 | |
| 3047 | child = node->children; |
| 3048 | while (IS_SCHEMA(child, "annotation")) { |
| 3049 | /* |
| 3050 | * the annotations here are simply discarded ... |
| 3051 | */ |
| 3052 | child = child->next; |
| 3053 | } |
| 3054 | if (child != NULL) { |
| 3055 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_INCLUDE_CHILD, |
| 3056 | "Include has unexpected content\n", NULL, NULL); |
| 3057 | return (-1); |
| 3058 | } |
| 3059 | |
| 3060 | /* |
| 3061 | * First step is to parse the input document into an DOM/Infoset |
| 3062 | */ |
| 3063 | doc = xmlReadFile((const char *) schemaLocation, NULL, |
| 3064 | SCHEMAS_PARSE_OPTIONS); |
| 3065 | if (doc == NULL) { |
| 3066 | xmlSchemaPErr(ctxt, NULL, |
| 3067 | XML_SCHEMAP_FAILED_LOAD, |
| 3068 | "xmlSchemaParse: could not load %s\n", |
| 3069 | ctxt->URL, NULL); |
| 3070 | return(-1); |
| 3071 | } |
| 3072 | |
| 3073 | /* |
| 3074 | * Then extract the root of the schema |
| 3075 | */ |
| 3076 | root = xmlDocGetRootElement(doc); |
| 3077 | if (root == NULL) { |
| 3078 | xmlSchemaPErr(ctxt, (xmlNodePtr) doc, |
| 3079 | XML_SCHEMAP_NOROOT, |
| 3080 | "schemas %s has no root", schemaLocation, NULL); |
| 3081 | xmlFreeDoc(doc); |
| 3082 | return (-1); |
| 3083 | } |
| 3084 | |
| 3085 | /* |
| 3086 | * Remove all the blank text nodes |
| 3087 | */ |
| 3088 | xmlSchemaCleanupDoc(ctxt, root); |
| 3089 | |
| 3090 | /* |
| 3091 | * Check the schemas top level element |
| 3092 | */ |
| 3093 | if (!IS_SCHEMA(root, "schema")) { |
| 3094 | xmlSchemaPErr(ctxt, (xmlNodePtr) doc, |
| 3095 | XML_SCHEMAP_NOT_SCHEMA, |
| 3096 | "File %s is not a schemas", schemaLocation, NULL); |
| 3097 | xmlFreeDoc(doc); |
| 3098 | return (-1); |
| 3099 | } |
| 3100 | |
| 3101 | /* |
| 3102 | * register the include |
| 3103 | */ |
| 3104 | include = (xmlSchemaIncludePtr) xmlMalloc(sizeof(xmlSchemaInclude)); |
| 3105 | if (include == NULL) { |
| 3106 | xmlSchemaPErrMemory(ctxt, "allocating included schema", NULL); |
| 3107 | xmlFreeDoc(doc); |
| 3108 | return (-1); |
| 3109 | } |
| 3110 | |
| 3111 | memset(include, 0, sizeof(xmlSchemaInclude)); |
| 3112 | include->schemaLocation = xmlDictLookup(ctxt->dict, schemaLocation, -1); |
| 3113 | include->doc = doc; |
| 3114 | include->next = schema->includes; |
| 3115 | schema->includes = include; |
| 3116 | |
| 3117 | |
| 3118 | /* |
| 3119 | * parse the declarations in the included file like if they |
| 3120 | * were in the original file. |
| 3121 | */ |
| 3122 | xmlSchemaParseSchemaTopLevel(ctxt, schema, root->children); |
| 3123 | |
| 3124 | return (1); |
| 3125 | } |
| 3126 | |
| 3127 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3128 | * xmlSchemaParseChoice: |
| 3129 | * @ctxt: a schema validation context |
| 3130 | * @schema: the schema being built |
| 3131 | * @node: a subtree containing XML Schema informations |
| 3132 | * |
| 3133 | * parse a XML schema Choice definition |
| 3134 | * *WARNING* this interface is highly subject to change |
| 3135 | * |
| 3136 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 3137 | * 1 in case of success. |
| 3138 | */ |
| 3139 | static xmlSchemaTypePtr |
| 3140 | xmlSchemaParseChoice(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3141 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3142 | { |
| 3143 | xmlSchemaTypePtr type, subtype, last = NULL; |
| 3144 | xmlNodePtr child = NULL; |
| 3145 | xmlChar name[30]; |
| 3146 | |
| 3147 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3148 | return (NULL); |
| 3149 | |
| 3150 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3151 | snprintf((char *) name, 30, "choice %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3152 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3153 | if (type == NULL) |
| 3154 | return (NULL); |
| 3155 | type->node = node; |
| 3156 | type->type = XML_SCHEMA_TYPE_CHOICE; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3157 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3158 | type->minOccurs = xmlGetMinOccurs(ctxt, node); |
| 3159 | type->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
| 3160 | |
| 3161 | child = node->children; |
| 3162 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3163 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3164 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3165 | } |
| 3166 | while ((IS_SCHEMA(child, "element")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3167 | (IS_SCHEMA(child, "group")) || |
| 3168 | (IS_SCHEMA(child, "any")) || |
| 3169 | (IS_SCHEMA(child, "choice")) || |
| 3170 | (IS_SCHEMA(child, "sequence"))) { |
| 3171 | subtype = NULL; |
| 3172 | if (IS_SCHEMA(child, "element")) { |
| 3173 | subtype = (xmlSchemaTypePtr) |
| 3174 | xmlSchemaParseElement(ctxt, schema, child, 0); |
| 3175 | } else if (IS_SCHEMA(child, "group")) { |
| 3176 | subtype = xmlSchemaParseGroup(ctxt, schema, child); |
| 3177 | } else if (IS_SCHEMA(child, "any")) { |
| 3178 | subtype = xmlSchemaParseAny(ctxt, schema, child); |
| 3179 | } else if (IS_SCHEMA(child, "sequence")) { |
| 3180 | subtype = xmlSchemaParseSequence(ctxt, schema, child); |
| 3181 | } else if (IS_SCHEMA(child, "choice")) { |
| 3182 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 3183 | } |
| 3184 | if (subtype != NULL) { |
| 3185 | if (last == NULL) { |
| 3186 | type->subtypes = subtype; |
| 3187 | last = subtype; |
| 3188 | } else { |
| 3189 | last->next = subtype; |
| 3190 | last = subtype; |
| 3191 | } |
| 3192 | last->next = NULL; |
| 3193 | } |
| 3194 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3195 | } |
| 3196 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3197 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_CHOICE_CHILD, |
| 3198 | "Choice %s has unexpected content\n", type->name, |
| 3199 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3200 | } |
| 3201 | |
| 3202 | return (type); |
| 3203 | } |
| 3204 | |
| 3205 | /** |
| 3206 | * xmlSchemaParseSequence: |
| 3207 | * @ctxt: a schema validation context |
| 3208 | * @schema: the schema being built |
| 3209 | * @node: a subtree containing XML Schema informations |
| 3210 | * |
| 3211 | * parse a XML schema Sequence definition |
| 3212 | * *WARNING* this interface is highly subject to change |
| 3213 | * |
| 3214 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 3215 | * 1 in case of success. |
| 3216 | */ |
| 3217 | static xmlSchemaTypePtr |
| 3218 | xmlSchemaParseSequence(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3219 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3220 | { |
| 3221 | xmlSchemaTypePtr type, subtype, last = NULL; |
| 3222 | xmlNodePtr child = NULL; |
| 3223 | xmlChar name[30]; |
| 3224 | |
| 3225 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3226 | return (NULL); |
| 3227 | |
| 3228 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3229 | snprintf((char *) name, 30, "sequence %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3230 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3231 | if (type == NULL) |
| 3232 | return (NULL); |
| 3233 | type->node = node; |
| 3234 | type->type = XML_SCHEMA_TYPE_SEQUENCE; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3235 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3236 | type->minOccurs = xmlGetMinOccurs(ctxt, node); |
| 3237 | type->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
| 3238 | |
| 3239 | child = node->children; |
| 3240 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3241 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3242 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3243 | } |
| 3244 | while ((IS_SCHEMA(child, "element")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3245 | (IS_SCHEMA(child, "group")) || |
| 3246 | (IS_SCHEMA(child, "any")) || |
| 3247 | (IS_SCHEMA(child, "choice")) || |
| 3248 | (IS_SCHEMA(child, "sequence"))) { |
| 3249 | subtype = NULL; |
| 3250 | if (IS_SCHEMA(child, "element")) { |
| 3251 | subtype = (xmlSchemaTypePtr) |
| 3252 | xmlSchemaParseElement(ctxt, schema, child, 0); |
| 3253 | } else if (IS_SCHEMA(child, "group")) { |
| 3254 | subtype = xmlSchemaParseGroup(ctxt, schema, child); |
| 3255 | } else if (IS_SCHEMA(child, "any")) { |
| 3256 | subtype = xmlSchemaParseAny(ctxt, schema, child); |
| 3257 | } else if (IS_SCHEMA(child, "choice")) { |
| 3258 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 3259 | } else if (IS_SCHEMA(child, "sequence")) { |
| 3260 | subtype = xmlSchemaParseSequence(ctxt, schema, child); |
| 3261 | } |
| 3262 | if (subtype != NULL) { |
| 3263 | if (last == NULL) { |
| 3264 | type->subtypes = subtype; |
| 3265 | last = subtype; |
| 3266 | } else { |
| 3267 | last->next = subtype; |
| 3268 | last = subtype; |
| 3269 | } |
| 3270 | last->next = NULL; |
| 3271 | } |
| 3272 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3273 | } |
| 3274 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3275 | xmlSchemaPErr2(ctxt, node, child, |
| 3276 | XML_SCHEMAP_UNKNOWN_SEQUENCE_CHILD, |
| 3277 | "Sequence %s has unexpected content\n", type->name, |
| 3278 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3279 | } |
| 3280 | |
| 3281 | return (type); |
| 3282 | } |
| 3283 | |
| 3284 | /** |
| 3285 | * xmlSchemaParseRestriction: |
| 3286 | * @ctxt: a schema validation context |
| 3287 | * @schema: the schema being built |
| 3288 | * @node: a subtree containing XML Schema informations |
| 3289 | * @simple: is that part of a simple type. |
| 3290 | * |
| 3291 | * parse a XML schema Restriction definition |
| 3292 | * *WARNING* this interface is highly subject to change |
| 3293 | * |
| 3294 | * Returns the type definition or NULL in case of error |
| 3295 | */ |
| 3296 | static xmlSchemaTypePtr |
| 3297 | xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 3298 | xmlNodePtr node, int simple) |
| 3299 | { |
| 3300 | xmlSchemaTypePtr type, subtype; |
| 3301 | xmlSchemaFacetPtr facet, lastfacet = NULL; |
| 3302 | xmlNodePtr child = NULL; |
| 3303 | xmlChar name[30]; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3304 | const xmlChar *oldcontainer; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3305 | |
| 3306 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3307 | return (NULL); |
| 3308 | |
| 3309 | oldcontainer = ctxt->container; |
| 3310 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3311 | snprintf((char *) name, 30, "restriction %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3312 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3313 | if (type == NULL) |
| 3314 | return (NULL); |
| 3315 | type->node = node; |
| 3316 | type->type = XML_SCHEMA_TYPE_RESTRICTION; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3317 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3318 | type->base = xmlGetQNameProp(ctxt, node, "base", &(type->baseNs)); |
| 3319 | if ((!simple) && (type->base == NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3320 | xmlSchemaPErr2(ctxt, node, child, |
| 3321 | XML_SCHEMAP_RESTRICTION_NONAME_NOREF, |
| 3322 | "Restriction %s has no base\n", type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3323 | } |
| 3324 | ctxt->container = name; |
| 3325 | |
| 3326 | child = node->children; |
| 3327 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3328 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3329 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3330 | } |
| 3331 | subtype = NULL; |
| 3332 | |
| 3333 | if (IS_SCHEMA(child, "all")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3334 | subtype = (xmlSchemaTypePtr) |
| 3335 | xmlSchemaParseAll(ctxt, schema, child); |
| 3336 | child = child->next; |
| 3337 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3338 | } else if (IS_SCHEMA(child, "choice")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3339 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 3340 | child = child->next; |
| 3341 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3342 | } else if (IS_SCHEMA(child, "sequence")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3343 | subtype = (xmlSchemaTypePtr) |
| 3344 | xmlSchemaParseSequence(ctxt, schema, child); |
| 3345 | child = child->next; |
| 3346 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3347 | } else if (IS_SCHEMA(child, "group")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3348 | subtype = (xmlSchemaTypePtr) |
| 3349 | xmlSchemaParseGroup(ctxt, schema, child); |
| 3350 | child = child->next; |
| 3351 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3352 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3353 | if (IS_SCHEMA(child, "simpleType")) { |
| 3354 | subtype = (xmlSchemaTypePtr) |
| 3355 | xmlSchemaParseSimpleType(ctxt, schema, child); |
| 3356 | child = child->next; |
| 3357 | type->baseType = subtype; |
| 3358 | } |
| 3359 | /* |
| 3360 | * Facets |
| 3361 | */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3362 | while ((IS_SCHEMA(child, "minInclusive")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3363 | (IS_SCHEMA(child, "minExclusive")) || |
| 3364 | (IS_SCHEMA(child, "maxInclusive")) || |
| 3365 | (IS_SCHEMA(child, "maxExclusive")) || |
| 3366 | (IS_SCHEMA(child, "totalDigits")) || |
| 3367 | (IS_SCHEMA(child, "fractionDigits")) || |
| 3368 | (IS_SCHEMA(child, "pattern")) || |
| 3369 | (IS_SCHEMA(child, "enumeration")) || |
| 3370 | (IS_SCHEMA(child, "whiteSpace")) || |
| 3371 | (IS_SCHEMA(child, "length")) || |
| 3372 | (IS_SCHEMA(child, "maxLength")) || |
| 3373 | (IS_SCHEMA(child, "minLength"))) { |
| 3374 | facet = xmlSchemaParseFacet(ctxt, schema, child); |
| 3375 | if (facet != NULL) { |
| 3376 | if (lastfacet == NULL) { |
| 3377 | type->facets = facet; |
| 3378 | lastfacet = facet; |
| 3379 | } else { |
| 3380 | lastfacet->next = facet; |
| 3381 | lastfacet = facet; |
| 3382 | } |
| 3383 | lastfacet->next = NULL; |
| 3384 | } |
| 3385 | child = child->next; |
| 3386 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3387 | } |
| 3388 | child = xmlSchemaParseAttrDecls(ctxt, schema, child, type); |
| 3389 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3390 | xmlSchemaPErr2(ctxt, node, child, |
| 3391 | XML_SCHEMAP_UNKNOWN_RESTRICTION_CHILD, |
| 3392 | "Restriction %s has unexpected content\n", |
| 3393 | type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3394 | } |
| 3395 | ctxt->container = oldcontainer; |
| 3396 | return (type); |
| 3397 | } |
| 3398 | |
| 3399 | /** |
| 3400 | * xmlSchemaParseExtension: |
| 3401 | * @ctxt: a schema validation context |
| 3402 | * @schema: the schema being built |
| 3403 | * @node: a subtree containing XML Schema informations |
| 3404 | * |
| 3405 | * parse a XML schema Extension definition |
| 3406 | * *WARNING* this interface is highly subject to change |
| 3407 | * |
| 3408 | * Returns the type definition or NULL in case of error |
| 3409 | */ |
| 3410 | static xmlSchemaTypePtr |
| 3411 | xmlSchemaParseExtension(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3412 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3413 | { |
| 3414 | xmlSchemaTypePtr type, subtype; |
| 3415 | xmlNodePtr child = NULL; |
| 3416 | xmlChar name[30]; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3417 | const xmlChar *oldcontainer; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3418 | |
| 3419 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3420 | return (NULL); |
| 3421 | |
| 3422 | oldcontainer = ctxt->container; |
| 3423 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3424 | snprintf((char *) name, 30, "extension %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3425 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3426 | if (type == NULL) |
| 3427 | return (NULL); |
| 3428 | type->node = node; |
| 3429 | type->type = XML_SCHEMA_TYPE_EXTENSION; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3430 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3431 | ctxt->container = name; |
| 3432 | |
| 3433 | type->base = xmlGetQNameProp(ctxt, node, "base", &(type->baseNs)); |
| 3434 | if (type->base == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3435 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_EXTENSION_NO_BASE, |
| 3436 | "Extension %s has no base\n", type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3437 | } |
| 3438 | child = node->children; |
| 3439 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3440 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3441 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3442 | } |
| 3443 | subtype = NULL; |
| 3444 | |
| 3445 | if (IS_SCHEMA(child, "all")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3446 | subtype = xmlSchemaParseAll(ctxt, schema, child); |
| 3447 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3448 | } else if (IS_SCHEMA(child, "choice")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3449 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 3450 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3451 | } else if (IS_SCHEMA(child, "sequence")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3452 | subtype = xmlSchemaParseSequence(ctxt, schema, child); |
| 3453 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3454 | } else if (IS_SCHEMA(child, "group")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3455 | subtype = xmlSchemaParseGroup(ctxt, schema, child); |
| 3456 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3457 | } |
| 3458 | if (subtype != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3459 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3460 | child = xmlSchemaParseAttrDecls(ctxt, schema, child, type); |
| 3461 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3462 | xmlSchemaPErr2(ctxt, node, child, |
| 3463 | XML_SCHEMAP_UNKNOWN_EXTENSION_CHILD, |
| 3464 | "Extension %s has unexpected content\n", type->name, |
| 3465 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3466 | } |
| 3467 | ctxt->container = oldcontainer; |
| 3468 | return (type); |
| 3469 | } |
| 3470 | |
| 3471 | /** |
| 3472 | * xmlSchemaParseSimpleContent: |
| 3473 | * @ctxt: a schema validation context |
| 3474 | * @schema: the schema being built |
| 3475 | * @node: a subtree containing XML Schema informations |
| 3476 | * |
| 3477 | * parse a XML schema SimpleContent definition |
| 3478 | * *WARNING* this interface is highly subject to change |
| 3479 | * |
| 3480 | * Returns the type definition or NULL in case of error |
| 3481 | */ |
| 3482 | static xmlSchemaTypePtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3483 | xmlSchemaParseSimpleContent(xmlSchemaParserCtxtPtr ctxt, |
| 3484 | xmlSchemaPtr schema, xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3485 | { |
| 3486 | xmlSchemaTypePtr type, subtype; |
| 3487 | xmlNodePtr child = NULL; |
| 3488 | xmlChar name[30]; |
| 3489 | |
| 3490 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3491 | return (NULL); |
| 3492 | |
| 3493 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3494 | snprintf((char *) name, 30, "complexContent %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3495 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3496 | if (type == NULL) |
| 3497 | return (NULL); |
| 3498 | type->node = node; |
| 3499 | type->type = XML_SCHEMA_TYPE_SIMPLE_CONTENT; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3500 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3501 | |
| 3502 | child = node->children; |
| 3503 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3504 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3505 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3506 | } |
| 3507 | subtype = NULL; |
| 3508 | if (IS_SCHEMA(child, "restriction")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3509 | subtype = (xmlSchemaTypePtr) |
| 3510 | xmlSchemaParseRestriction(ctxt, schema, child, 0); |
| 3511 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3512 | } else if (IS_SCHEMA(child, "extension")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3513 | subtype = (xmlSchemaTypePtr) |
| 3514 | xmlSchemaParseExtension(ctxt, schema, child); |
| 3515 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3516 | } |
| 3517 | type->subtypes = subtype; |
| 3518 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3519 | xmlSchemaPErr2(ctxt, node, child, |
| 3520 | XML_SCHEMAP_UNKNOWN_SIMPLECONTENT_CHILD, |
| 3521 | "SimpleContent %s has unexpected content\n", |
| 3522 | type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3523 | } |
| 3524 | return (type); |
| 3525 | } |
| 3526 | |
| 3527 | /** |
| 3528 | * xmlSchemaParseComplexContent: |
| 3529 | * @ctxt: a schema validation context |
| 3530 | * @schema: the schema being built |
| 3531 | * @node: a subtree containing XML Schema informations |
| 3532 | * |
| 3533 | * parse a XML schema ComplexContent definition |
| 3534 | * *WARNING* this interface is highly subject to change |
| 3535 | * |
| 3536 | * Returns the type definition or NULL in case of error |
| 3537 | */ |
| 3538 | static xmlSchemaTypePtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3539 | xmlSchemaParseComplexContent(xmlSchemaParserCtxtPtr ctxt, |
| 3540 | xmlSchemaPtr schema, xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3541 | { |
| 3542 | xmlSchemaTypePtr type, subtype; |
| 3543 | xmlNodePtr child = NULL; |
| 3544 | xmlChar name[30]; |
| 3545 | |
| 3546 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3547 | return (NULL); |
| 3548 | |
| 3549 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3550 | snprintf((char *) name, 30, "complexContent %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3551 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3552 | if (type == NULL) |
| 3553 | return (NULL); |
| 3554 | type->node = node; |
| 3555 | type->type = XML_SCHEMA_TYPE_COMPLEX_CONTENT; |
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 | |
| 3558 | child = node->children; |
| 3559 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3560 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3561 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3562 | } |
| 3563 | subtype = NULL; |
| 3564 | if (IS_SCHEMA(child, "restriction")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3565 | subtype = (xmlSchemaTypePtr) |
| 3566 | xmlSchemaParseRestriction(ctxt, schema, child, 0); |
| 3567 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3568 | } else if (IS_SCHEMA(child, "extension")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3569 | subtype = (xmlSchemaTypePtr) |
| 3570 | xmlSchemaParseExtension(ctxt, schema, child); |
| 3571 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3572 | } |
| 3573 | type->subtypes = subtype; |
| 3574 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3575 | xmlSchemaPErr2(ctxt, node, child, |
| 3576 | XML_SCHEMAP_UNKNOWN_COMPLEXCONTENT_CHILD, |
| 3577 | "ComplexContent %s has unexpected content\n", |
| 3578 | type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3579 | } |
| 3580 | return (type); |
| 3581 | } |
| 3582 | |
| 3583 | /** |
| 3584 | * xmlSchemaParseComplexType: |
| 3585 | * @ctxt: a schema validation context |
| 3586 | * @schema: the schema being built |
| 3587 | * @node: a subtree containing XML Schema informations |
| 3588 | * |
| 3589 | * parse a XML schema Complex Type definition |
| 3590 | * *WARNING* this interface is highly subject to change |
| 3591 | * |
| 3592 | * Returns the type definition or NULL in case of error |
| 3593 | */ |
| 3594 | static xmlSchemaTypePtr |
| 3595 | xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 3596 | xmlNodePtr node) |
| 3597 | { |
| 3598 | xmlSchemaTypePtr type, subtype; |
| 3599 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3600 | const xmlChar *name; |
| 3601 | const xmlChar *oldcontainer; |
Daniel Veillard | 1aefc86 | 2004-03-04 11:40:48 +0000 | [diff] [blame] | 3602 | const xmlChar *mixed; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3603 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3604 | |
| 3605 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3606 | return (NULL); |
| 3607 | |
| 3608 | oldcontainer = ctxt->container; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3609 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3610 | if (name == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3611 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3612 | snprintf(buf, 99, "anontype %d", ctxt->counter++ + 1); |
| 3613 | name = (const xmlChar *)buf; |
| 3614 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
| 3615 | } else { |
| 3616 | const xmlChar *local, *ns; |
| 3617 | |
| 3618 | local = xmlSchemaGetNamespace(ctxt, schema, node, name, &ns); |
| 3619 | type = xmlSchemaAddType(ctxt, schema, local, ns); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3620 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3621 | if (type == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3622 | return (NULL); |
| 3623 | } |
Daniel Veillard | 1aefc86 | 2004-03-04 11:40:48 +0000 | [diff] [blame] | 3624 | |
| 3625 | mixed = xmlSchemaGetProp(ctxt, node, "mixed"); |
| 3626 | if (mixed != NULL) |
| 3627 | type->flags |= XML_SCHEMAS_TYPE_MIXED; |
| 3628 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3629 | type->node = node; |
| 3630 | type->type = XML_SCHEMA_TYPE_COMPLEX; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3631 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3632 | ctxt->container = name; |
| 3633 | |
| 3634 | child = node->children; |
| 3635 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3636 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3637 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3638 | } |
| 3639 | if (IS_SCHEMA(child, "simpleContent")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3640 | type->subtypes = xmlSchemaParseSimpleContent(ctxt, schema, child); |
| 3641 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3642 | } else if (IS_SCHEMA(child, "complexContent")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3643 | type->subtypes = xmlSchemaParseComplexContent(ctxt, schema, child); |
| 3644 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3645 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3646 | subtype = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3647 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3648 | if (IS_SCHEMA(child, "all")) { |
| 3649 | subtype = xmlSchemaParseAll(ctxt, schema, child); |
| 3650 | child = child->next; |
| 3651 | } else if (IS_SCHEMA(child, "choice")) { |
| 3652 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 3653 | child = child->next; |
| 3654 | } else if (IS_SCHEMA(child, "sequence")) { |
| 3655 | subtype = xmlSchemaParseSequence(ctxt, schema, child); |
| 3656 | child = child->next; |
| 3657 | } else if (IS_SCHEMA(child, "group")) { |
| 3658 | subtype = xmlSchemaParseGroup(ctxt, schema, child); |
| 3659 | child = child->next; |
| 3660 | } |
| 3661 | if (subtype != NULL) |
| 3662 | type->subtypes = subtype; |
| 3663 | child = xmlSchemaParseAttrDecls(ctxt, schema, child, type); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3664 | } |
| 3665 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3666 | xmlSchemaPErr2(ctxt, node, child, |
| 3667 | XML_SCHEMAP_UNKNOWN_COMPLEXTYPE_CHILD, |
| 3668 | "ComplexType %s has unexpected content\n", |
| 3669 | type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3670 | } |
| 3671 | ctxt->container = oldcontainer; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3672 | return (type); |
| 3673 | } |
| 3674 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3675 | /** |
| 3676 | * xmlSchemaParseSchema: |
| 3677 | * @ctxt: a schema validation context |
| 3678 | * @node: a subtree containing XML Schema informations |
| 3679 | * |
| 3680 | * parse a XML schema definition from a node set |
| 3681 | * *WARNING* this interface is highly subject to change |
| 3682 | * |
| 3683 | * Returns the internal XML Schema structure built from the resource or |
| 3684 | * NULL in case of error |
| 3685 | */ |
| 3686 | static xmlSchemaPtr |
| 3687 | xmlSchemaParseSchema(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node) |
| 3688 | { |
| 3689 | xmlSchemaPtr schema = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3690 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3691 | const xmlChar *val; |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 3692 | int nberrors; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3693 | |
| 3694 | if ((ctxt == NULL) || (node == NULL)) |
| 3695 | return (NULL); |
| 3696 | |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 3697 | nberrors = ctxt->nberrors; |
| 3698 | ctxt->nberrors = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3699 | if (IS_SCHEMA(node, "schema")) { |
| 3700 | schema = xmlSchemaNewSchema(ctxt); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3701 | if (schema == NULL) |
| 3702 | return (NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3703 | val = xmlSchemaGetProp(ctxt, node, "targetNamespace"); |
| 3704 | if (val != NULL) { |
| 3705 | schema->targetNamespace = xmlDictLookup(ctxt->dict, val, -1); |
| 3706 | } else { |
| 3707 | schema->targetNamespace = NULL; |
| 3708 | } |
| 3709 | schema->id = xmlSchemaGetProp(ctxt, node, "id"); |
| 3710 | schema->version = xmlSchemaGetProp(ctxt, node, "version"); |
| 3711 | val = xmlSchemaGetProp(ctxt, node, "elementFormDefault"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3712 | if (val != NULL) { |
| 3713 | if (xmlStrEqual(val, BAD_CAST "qualified")) |
| 3714 | schema->flags |= XML_SCHEMAS_QUALIF_ELEM; |
| 3715 | else if (!xmlStrEqual(val, BAD_CAST "unqualified")) { |
| 3716 | xmlSchemaPErr2(ctxt, node, child, |
| 3717 | XML_SCHEMAP_ELEMFORMDEFAULT_VALUE, |
| 3718 | "Invalid value %s for elementFormDefault\n", |
| 3719 | val, NULL); |
| 3720 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3721 | } else { |
| 3722 | schema->flags |= XML_SCHEMAS_QUALIF_ELEM; |
| 3723 | } |
| 3724 | val = xmlSchemaGetProp(ctxt, node, "attributeFormDefault"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3725 | if (val != NULL) { |
| 3726 | if (xmlStrEqual(val, BAD_CAST "qualified")) |
| 3727 | schema->flags |= XML_SCHEMAS_QUALIF_ATTR; |
| 3728 | else if (!xmlStrEqual(val, BAD_CAST "unqualified")) { |
| 3729 | xmlSchemaPErr2(ctxt, node, child, |
| 3730 | XML_SCHEMAP_ATTRFORMDEFAULT_VALUE, |
| 3731 | "Invalid value %s for attributeFormDefault\n", |
| 3732 | val, NULL); |
| 3733 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3734 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3735 | |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 3736 | xmlSchemaParseSchemaTopLevel(ctxt, schema, node->children); |
| 3737 | } else { |
| 3738 | xmlDocPtr doc; |
| 3739 | |
| 3740 | doc = node->doc; |
| 3741 | |
| 3742 | if ((doc != NULL) && (doc->URL != NULL)) { |
| 3743 | xmlSchemaPErr(ctxt, (xmlNodePtr) doc, |
| 3744 | XML_SCHEMAP_NOT_SCHEMA, |
| 3745 | "File %s is not a schemas", doc->URL, NULL); |
| 3746 | } else { |
| 3747 | xmlSchemaPErr(ctxt, (xmlNodePtr) doc, |
| 3748 | XML_SCHEMAP_NOT_SCHEMA, |
| 3749 | "File is not a schemas", NULL, NULL); |
| 3750 | } |
| 3751 | return(NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3752 | } |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 3753 | if (ctxt->nberrors != 0) { |
| 3754 | if (schema != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3755 | xmlSchemaFree(schema); |
| 3756 | schema = NULL; |
| 3757 | } |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 3758 | } |
| 3759 | ctxt->nberrors = nberrors; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3760 | #ifdef DEBUG |
| 3761 | if (schema == NULL) |
| 3762 | xmlGenericError(xmlGenericErrorContext, |
| 3763 | "xmlSchemaParse() failed\n"); |
| 3764 | #endif |
| 3765 | |
| 3766 | return (schema); |
| 3767 | } |
| 3768 | |
| 3769 | /************************************************************************ |
| 3770 | * * |
| 3771 | * Validating using Schemas * |
| 3772 | * * |
| 3773 | ************************************************************************/ |
| 3774 | |
| 3775 | /************************************************************************ |
| 3776 | * * |
| 3777 | * Reading/Writing Schemas * |
| 3778 | * * |
| 3779 | ************************************************************************/ |
| 3780 | |
| 3781 | /** |
| 3782 | * xmlSchemaNewParserCtxt: |
| 3783 | * @URL: the location of the schema |
| 3784 | * |
| 3785 | * Create an XML Schemas parse context for that file/resource expected |
| 3786 | * to contain an XML Schemas file. |
| 3787 | * |
| 3788 | * Returns the parser context or NULL in case of error |
| 3789 | */ |
| 3790 | xmlSchemaParserCtxtPtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3791 | xmlSchemaNewParserCtxt(const char *URL) |
| 3792 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3793 | xmlSchemaParserCtxtPtr ret; |
| 3794 | |
| 3795 | if (URL == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3796 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3797 | |
| 3798 | ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt)); |
| 3799 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3800 | xmlSchemaPErrMemory(NULL, "allocating schama parser context", |
| 3801 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3802 | return (NULL); |
| 3803 | } |
| 3804 | memset(ret, 0, sizeof(xmlSchemaParserCtxt)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3805 | ret->dict = xmlDictCreate(); |
| 3806 | ret->URL = xmlDictLookup(ret->dict, (const xmlChar *) URL, -1); |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 3807 | ret->includes = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3808 | return (ret); |
| 3809 | } |
| 3810 | |
| 3811 | /** |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 3812 | * xmlSchemaNewMemParserCtxt: |
| 3813 | * @buffer: a pointer to a char array containing the schemas |
| 3814 | * @size: the size of the array |
| 3815 | * |
| 3816 | * Create an XML Schemas parse context for that memory buffer expected |
| 3817 | * to contain an XML Schemas file. |
| 3818 | * |
| 3819 | * Returns the parser context or NULL in case of error |
| 3820 | */ |
| 3821 | xmlSchemaParserCtxtPtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3822 | xmlSchemaNewMemParserCtxt(const char *buffer, int size) |
| 3823 | { |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 3824 | xmlSchemaParserCtxtPtr ret; |
| 3825 | |
| 3826 | if ((buffer == NULL) || (size <= 0)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3827 | return (NULL); |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 3828 | |
| 3829 | ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt)); |
| 3830 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3831 | xmlSchemaPErrMemory(NULL, "allocating schama parser context", |
| 3832 | NULL); |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 3833 | return (NULL); |
| 3834 | } |
| 3835 | memset(ret, 0, sizeof(xmlSchemaParserCtxt)); |
| 3836 | ret->buffer = buffer; |
| 3837 | ret->size = size; |
William M. Brack | cf9eadf | 2003-12-25 13:24:05 +0000 | [diff] [blame] | 3838 | ret->dict = xmlDictCreate(); |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 3839 | return (ret); |
| 3840 | } |
| 3841 | |
| 3842 | /** |
Daniel Veillard | 9d75150 | 2003-10-29 13:21:47 +0000 | [diff] [blame] | 3843 | * xmlSchemaNewDocParserCtxt: |
| 3844 | * @doc: a preparsed document tree |
| 3845 | * |
| 3846 | * Create an XML Schemas parse context for that document. |
| 3847 | * NB. The document may be modified during the parsing process. |
| 3848 | * |
| 3849 | * Returns the parser context or NULL in case of error |
| 3850 | */ |
| 3851 | xmlSchemaParserCtxtPtr |
| 3852 | xmlSchemaNewDocParserCtxt(xmlDocPtr doc) |
| 3853 | { |
| 3854 | xmlSchemaParserCtxtPtr ret; |
| 3855 | |
| 3856 | if (doc == NULL) |
| 3857 | return (NULL); |
| 3858 | |
| 3859 | ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt)); |
| 3860 | if (ret == NULL) { |
| 3861 | xmlSchemaPErrMemory(NULL, "allocating schema parser context", |
| 3862 | NULL); |
| 3863 | return (NULL); |
| 3864 | } |
| 3865 | memset(ret, 0, sizeof(xmlSchemaParserCtxt)); |
| 3866 | ret->doc = doc; |
William M. Brack | cf9eadf | 2003-12-25 13:24:05 +0000 | [diff] [blame] | 3867 | ret->dict = xmlDictCreate(); |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 3868 | /* The application has responsibility for the document */ |
| 3869 | ret->preserve = 1; |
Daniel Veillard | 9d75150 | 2003-10-29 13:21:47 +0000 | [diff] [blame] | 3870 | |
| 3871 | return (ret); |
| 3872 | } |
| 3873 | |
| 3874 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3875 | * xmlSchemaFreeParserCtxt: |
| 3876 | * @ctxt: the schema parser context |
| 3877 | * |
| 3878 | * Free the resources associated to the schema parser context |
| 3879 | */ |
| 3880 | void |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3881 | xmlSchemaFreeParserCtxt(xmlSchemaParserCtxtPtr ctxt) |
| 3882 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3883 | if (ctxt == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3884 | return; |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 3885 | if (ctxt->doc != NULL && !ctxt->preserve) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3886 | xmlFreeDoc(ctxt->doc); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3887 | xmlDictFree(ctxt->dict); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3888 | xmlFree(ctxt); |
| 3889 | } |
| 3890 | |
| 3891 | /************************************************************************ |
| 3892 | * * |
| 3893 | * Building the content models * |
| 3894 | * * |
| 3895 | ************************************************************************/ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3896 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3897 | /** |
| 3898 | * xmlSchemaBuildAContentModel: |
| 3899 | * @type: the schema type definition |
| 3900 | * @ctxt: the schema parser context |
| 3901 | * @name: the element name whose content is being built |
| 3902 | * |
| 3903 | * Generate the automata sequence needed for that type |
| 3904 | */ |
| 3905 | static void |
| 3906 | xmlSchemaBuildAContentModel(xmlSchemaTypePtr type, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3907 | xmlSchemaParserCtxtPtr ctxt, |
| 3908 | const xmlChar * name) |
| 3909 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3910 | if (type == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3911 | xmlGenericError(xmlGenericErrorContext, |
| 3912 | "Found unexpected type = NULL in %s content model\n", |
| 3913 | name); |
| 3914 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3915 | } |
| 3916 | switch (type->type) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3917 | case XML_SCHEMA_TYPE_ANY: |
| 3918 | /* TODO : handle the namespace too */ |
| 3919 | /* TODO : make that a specific transition type */ |
| 3920 | TODO ctxt->state = |
| 3921 | xmlAutomataNewTransition(ctxt->am, ctxt->state, NULL, |
| 3922 | BAD_CAST "*", NULL); |
| 3923 | break; |
| 3924 | case XML_SCHEMA_TYPE_ELEMENT:{ |
| 3925 | xmlSchemaElementPtr elem = (xmlSchemaElementPtr) type; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3926 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3927 | /* TODO : handle the namespace too */ |
| 3928 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3929 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3930 | if (elem->maxOccurs >= UNBOUNDED) { |
| 3931 | if (elem->minOccurs > 1) { |
| 3932 | xmlAutomataStatePtr tmp; |
| 3933 | int counter; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3934 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3935 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
| 3936 | oldstate, |
| 3937 | NULL); |
| 3938 | oldstate = ctxt->state; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3939 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3940 | counter = xmlAutomataNewCounter(ctxt->am, |
| 3941 | elem->minOccurs - |
| 3942 | 1, UNBOUNDED); |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3943 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3944 | if (elem->refDecl != NULL) { |
| 3945 | xmlSchemaBuildAContentModel((xmlSchemaTypePtr) |
| 3946 | elem->refDecl, |
| 3947 | ctxt, |
| 3948 | elem->refDecl-> |
| 3949 | name); |
| 3950 | } else { |
| 3951 | ctxt->state = |
| 3952 | xmlAutomataNewTransition(ctxt->am, |
| 3953 | ctxt->state, NULL, |
| 3954 | elem->name, type); |
| 3955 | } |
| 3956 | tmp = ctxt->state; |
| 3957 | xmlAutomataNewCountedTrans(ctxt->am, tmp, oldstate, |
| 3958 | counter); |
| 3959 | ctxt->state = |
| 3960 | xmlAutomataNewCounterTrans(ctxt->am, tmp, NULL, |
| 3961 | counter); |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3962 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3963 | } else { |
| 3964 | if (elem->refDecl != NULL) { |
| 3965 | xmlSchemaBuildAContentModel((xmlSchemaTypePtr) |
| 3966 | elem->refDecl, |
| 3967 | ctxt, |
| 3968 | elem->refDecl-> |
| 3969 | name); |
| 3970 | } else { |
| 3971 | ctxt->state = |
| 3972 | xmlAutomataNewTransition(ctxt->am, |
| 3973 | ctxt->state, NULL, |
| 3974 | elem->name, type); |
| 3975 | } |
| 3976 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
| 3977 | oldstate); |
| 3978 | if (elem->minOccurs == 0) { |
| 3979 | /* basically an elem* */ |
| 3980 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 3981 | ctxt->state); |
| 3982 | } |
| 3983 | } |
| 3984 | } else if ((elem->maxOccurs > 1) || (elem->minOccurs > 1)) { |
| 3985 | xmlAutomataStatePtr tmp; |
| 3986 | int counter; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3987 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3988 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
| 3989 | oldstate, NULL); |
| 3990 | oldstate = ctxt->state; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3991 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3992 | counter = xmlAutomataNewCounter(ctxt->am, |
| 3993 | elem->minOccurs - 1, |
| 3994 | elem->maxOccurs - 1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3995 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3996 | if (elem->refDecl != NULL) { |
| 3997 | xmlSchemaBuildAContentModel((xmlSchemaTypePtr) |
| 3998 | elem->refDecl, ctxt, |
| 3999 | elem->refDecl->name); |
| 4000 | } else { |
| 4001 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
| 4002 | ctxt->state, |
| 4003 | NULL, |
| 4004 | elem->name, |
| 4005 | type); |
| 4006 | } |
| 4007 | tmp = ctxt->state; |
| 4008 | xmlAutomataNewCountedTrans(ctxt->am, tmp, oldstate, |
| 4009 | counter); |
| 4010 | ctxt->state = xmlAutomataNewCounterTrans(ctxt->am, tmp, |
| 4011 | NULL, |
| 4012 | counter); |
| 4013 | if (elem->minOccurs == 0) { |
| 4014 | /* basically an elem? */ |
| 4015 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 4016 | ctxt->state); |
| 4017 | } |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4018 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4019 | } else { |
| 4020 | if (elem->refDecl != NULL) { |
| 4021 | xmlSchemaBuildAContentModel((xmlSchemaTypePtr) |
| 4022 | elem->refDecl, ctxt, |
| 4023 | elem->refDecl->name); |
| 4024 | } else { |
| 4025 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
| 4026 | ctxt->state, |
| 4027 | NULL, |
| 4028 | elem->name, |
| 4029 | type); |
| 4030 | } |
| 4031 | if (elem->minOccurs == 0) { |
| 4032 | /* basically an elem? */ |
| 4033 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 4034 | ctxt->state); |
| 4035 | } |
| 4036 | } |
| 4037 | break; |
| 4038 | } |
| 4039 | case XML_SCHEMA_TYPE_SEQUENCE:{ |
| 4040 | xmlSchemaTypePtr subtypes; |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4041 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4042 | /* |
| 4043 | * If max and min occurances are default (1) then |
| 4044 | * simply iterate over the subtypes |
| 4045 | */ |
| 4046 | if ((type->minOccurs == 1) && (type->maxOccurs == 1)) { |
| 4047 | subtypes = type->subtypes; |
| 4048 | while (subtypes != NULL) { |
| 4049 | xmlSchemaBuildAContentModel(subtypes, ctxt, name); |
| 4050 | subtypes = subtypes->next; |
| 4051 | } |
| 4052 | } else { |
| 4053 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4054 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4055 | if (type->maxOccurs >= UNBOUNDED) { |
| 4056 | if (type->minOccurs > 1) { |
| 4057 | xmlAutomataStatePtr tmp; |
| 4058 | int counter; |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4059 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4060 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
| 4061 | oldstate, |
| 4062 | NULL); |
| 4063 | oldstate = ctxt->state; |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4064 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4065 | counter = xmlAutomataNewCounter(ctxt->am, |
| 4066 | type-> |
| 4067 | minOccurs - 1, |
| 4068 | UNBOUNDED); |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4069 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4070 | subtypes = type->subtypes; |
| 4071 | while (subtypes != NULL) { |
| 4072 | xmlSchemaBuildAContentModel(subtypes, ctxt, |
| 4073 | name); |
| 4074 | subtypes = subtypes->next; |
| 4075 | } |
| 4076 | tmp = ctxt->state; |
| 4077 | xmlAutomataNewCountedTrans(ctxt->am, tmp, |
| 4078 | oldstate, counter); |
| 4079 | ctxt->state = |
| 4080 | xmlAutomataNewCounterTrans(ctxt->am, tmp, |
| 4081 | NULL, counter); |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4082 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4083 | } else { |
| 4084 | subtypes = type->subtypes; |
| 4085 | while (subtypes != NULL) { |
| 4086 | xmlSchemaBuildAContentModel(subtypes, ctxt, |
| 4087 | name); |
| 4088 | subtypes = subtypes->next; |
| 4089 | } |
| 4090 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
| 4091 | oldstate); |
| 4092 | if (type->minOccurs == 0) { |
| 4093 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 4094 | ctxt->state); |
| 4095 | } |
| 4096 | } |
| 4097 | } else if ((type->maxOccurs > 1) |
| 4098 | || (type->minOccurs > 1)) { |
| 4099 | xmlAutomataStatePtr tmp; |
| 4100 | int counter; |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4101 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4102 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
| 4103 | oldstate, |
| 4104 | NULL); |
| 4105 | oldstate = ctxt->state; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4106 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4107 | counter = xmlAutomataNewCounter(ctxt->am, |
| 4108 | type->minOccurs - |
| 4109 | 1, |
| 4110 | type->maxOccurs - |
| 4111 | 1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4112 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4113 | subtypes = type->subtypes; |
| 4114 | while (subtypes != NULL) { |
| 4115 | xmlSchemaBuildAContentModel(subtypes, ctxt, |
| 4116 | name); |
| 4117 | subtypes = subtypes->next; |
| 4118 | } |
| 4119 | tmp = ctxt->state; |
| 4120 | xmlAutomataNewCountedTrans(ctxt->am, tmp, oldstate, |
| 4121 | counter); |
| 4122 | ctxt->state = |
| 4123 | xmlAutomataNewCounterTrans(ctxt->am, tmp, NULL, |
| 4124 | counter); |
| 4125 | if (type->minOccurs == 0) { |
| 4126 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 4127 | ctxt->state); |
| 4128 | } |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 4129 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4130 | } else { |
| 4131 | subtypes = type->subtypes; |
| 4132 | while (subtypes != NULL) { |
| 4133 | xmlSchemaBuildAContentModel(subtypes, ctxt, |
| 4134 | name); |
| 4135 | subtypes = subtypes->next; |
| 4136 | } |
| 4137 | if (type->minOccurs == 0) { |
| 4138 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 4139 | ctxt->state); |
| 4140 | } |
| 4141 | } |
| 4142 | } |
| 4143 | break; |
| 4144 | } |
| 4145 | case XML_SCHEMA_TYPE_CHOICE:{ |
| 4146 | xmlSchemaTypePtr subtypes; |
| 4147 | xmlAutomataStatePtr start, end; |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 4148 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4149 | start = ctxt->state; |
| 4150 | end = xmlAutomataNewState(ctxt->am); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4151 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4152 | /* |
| 4153 | * iterate over the subtypes and remerge the end with an |
| 4154 | * epsilon transition |
| 4155 | */ |
| 4156 | if (type->maxOccurs == 1) { |
| 4157 | subtypes = type->subtypes; |
| 4158 | while (subtypes != NULL) { |
| 4159 | ctxt->state = start; |
| 4160 | xmlSchemaBuildAContentModel(subtypes, ctxt, name); |
| 4161 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, end); |
| 4162 | subtypes = subtypes->next; |
| 4163 | } |
| 4164 | } else { |
| 4165 | int counter; |
| 4166 | xmlAutomataStatePtr hop; |
| 4167 | int maxOccurs = type->maxOccurs == UNBOUNDED ? |
| 4168 | UNBOUNDED : type->maxOccurs - 1; |
| 4169 | int minOccurs = |
| 4170 | type->minOccurs < 1 ? 0 : type->minOccurs - 1; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4171 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4172 | /* |
| 4173 | * use a counter to keep track of the number of transtions |
| 4174 | * which went through the choice. |
| 4175 | */ |
| 4176 | counter = |
| 4177 | xmlAutomataNewCounter(ctxt->am, minOccurs, |
| 4178 | maxOccurs); |
| 4179 | hop = xmlAutomataNewState(ctxt->am); |
Daniel Veillard | 6231e84 | 2002-04-18 11:54:04 +0000 | [diff] [blame] | 4180 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4181 | subtypes = type->subtypes; |
| 4182 | while (subtypes != NULL) { |
| 4183 | ctxt->state = start; |
| 4184 | xmlSchemaBuildAContentModel(subtypes, ctxt, name); |
| 4185 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, hop); |
| 4186 | subtypes = subtypes->next; |
| 4187 | } |
| 4188 | xmlAutomataNewCountedTrans(ctxt->am, hop, start, |
| 4189 | counter); |
| 4190 | xmlAutomataNewCounterTrans(ctxt->am, hop, end, |
| 4191 | counter); |
| 4192 | } |
| 4193 | if (type->minOccurs == 0) { |
| 4194 | xmlAutomataNewEpsilon(ctxt->am, start, end); |
| 4195 | } |
| 4196 | ctxt->state = end; |
| 4197 | break; |
| 4198 | } |
| 4199 | case XML_SCHEMA_TYPE_ALL:{ |
| 4200 | xmlAutomataStatePtr start; |
| 4201 | xmlSchemaTypePtr subtypes; |
| 4202 | xmlSchemaElementPtr elem = (xmlSchemaElementPtr) type; |
| 4203 | int lax; |
| 4204 | |
| 4205 | subtypes = type->subtypes; |
| 4206 | if (subtypes == NULL) |
| 4207 | break; |
| 4208 | start = ctxt->state; |
| 4209 | while (subtypes != NULL) { |
| 4210 | ctxt->state = start; |
| 4211 | elem = (xmlSchemaElementPtr) subtypes; |
| 4212 | |
| 4213 | /* TODO : handle the namespace too */ |
| 4214 | if ((elem->minOccurs == 1) && (elem->maxOccurs == 1)) { |
| 4215 | xmlAutomataNewOnceTrans(ctxt->am, ctxt->state, |
| 4216 | ctxt->state, elem->name, 1, |
| 4217 | 1, subtypes); |
| 4218 | } else { |
| 4219 | xmlAutomataNewCountTrans(ctxt->am, ctxt->state, |
| 4220 | ctxt->state, elem->name, |
| 4221 | elem->minOccurs, |
| 4222 | elem->maxOccurs, |
| 4223 | subtypes); |
| 4224 | } |
| 4225 | subtypes = subtypes->next; |
| 4226 | } |
| 4227 | lax = type->minOccurs == 0; |
| 4228 | ctxt->state = |
| 4229 | xmlAutomataNewAllTrans(ctxt->am, ctxt->state, NULL, |
| 4230 | lax); |
| 4231 | break; |
| 4232 | } |
| 4233 | case XML_SCHEMA_TYPE_RESTRICTION: |
| 4234 | if (type->subtypes != NULL) |
| 4235 | xmlSchemaBuildAContentModel(type->subtypes, ctxt, name); |
| 4236 | break; |
| 4237 | case XML_SCHEMA_TYPE_EXTENSION: |
| 4238 | if (type->baseType != NULL) { |
| 4239 | xmlSchemaTypePtr subtypes; |
| 4240 | |
Daniel Veillard | f762755 | 2004-04-22 07:15:40 +0000 | [diff] [blame] | 4241 | if (type->recurse) { |
| 4242 | xmlSchemaPErr(ctxt, type->node, |
| 4243 | XML_SCHEMAP_UNKNOWN_BASE_TYPE, |
| 4244 | "Schemas: extension type %s is recursive\n", |
| 4245 | type->name, NULL); |
| 4246 | return; |
| 4247 | } |
| 4248 | type->recurse = 1; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4249 | xmlSchemaBuildAContentModel(type->baseType, ctxt, name); |
Daniel Veillard | f762755 | 2004-04-22 07:15:40 +0000 | [diff] [blame] | 4250 | type->recurse = 0; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4251 | subtypes = type->subtypes; |
| 4252 | while (subtypes != NULL) { |
| 4253 | xmlSchemaBuildAContentModel(subtypes, ctxt, name); |
| 4254 | subtypes = subtypes->next; |
| 4255 | } |
| 4256 | } else if (type->subtypes != NULL) |
| 4257 | xmlSchemaBuildAContentModel(type->subtypes, ctxt, name); |
| 4258 | break; |
| 4259 | case XML_SCHEMA_TYPE_GROUP: |
| 4260 | if (type->subtypes == NULL) { |
| 4261 | } |
| 4262 | case XML_SCHEMA_TYPE_COMPLEX: |
| 4263 | case XML_SCHEMA_TYPE_COMPLEX_CONTENT: |
| 4264 | if (type->subtypes != NULL) |
| 4265 | xmlSchemaBuildAContentModel(type->subtypes, ctxt, name); |
| 4266 | break; |
| 4267 | default: |
| 4268 | xmlGenericError(xmlGenericErrorContext, |
| 4269 | "Found unexpected type %d in %s content model\n", |
| 4270 | type->type, name); |
| 4271 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4272 | } |
| 4273 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4274 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4275 | /** |
| 4276 | * xmlSchemaBuildContentModel: |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4277 | * @elem: the element |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4278 | * @ctxt: the schema parser context |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4279 | * @name: the element name |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4280 | * |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4281 | * Builds the content model of the element. |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4282 | */ |
| 4283 | static void |
| 4284 | xmlSchemaBuildContentModel(xmlSchemaElementPtr elem, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4285 | xmlSchemaParserCtxtPtr ctxt, |
| 4286 | const xmlChar * name) |
| 4287 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4288 | xmlAutomataStatePtr start; |
| 4289 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4290 | if (elem->contModel != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4291 | return; |
Daniel Veillard | 88c5891 | 2002-04-23 07:12:20 +0000 | [diff] [blame] | 4292 | if (elem->subtypes == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4293 | elem->contentType = XML_SCHEMA_CONTENT_ANY; |
| 4294 | return; |
Daniel Veillard | 88c5891 | 2002-04-23 07:12:20 +0000 | [diff] [blame] | 4295 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4296 | if (elem->subtypes->type != XML_SCHEMA_TYPE_COMPLEX) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4297 | return; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 4298 | if ((elem->subtypes->contentType == XML_SCHEMA_CONTENT_BASIC) || |
| 4299 | (elem->subtypes->contentType == XML_SCHEMA_CONTENT_SIMPLE)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4300 | return; |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 4301 | |
| 4302 | #ifdef DEBUG_CONTENT |
| 4303 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4304 | "Building content model for %s\n", name); |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 4305 | #endif |
| 4306 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4307 | ctxt->am = xmlNewAutomata(); |
| 4308 | if (ctxt->am == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4309 | xmlGenericError(xmlGenericErrorContext, |
| 4310 | "Cannot create automata for elem %s\n", name); |
| 4311 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4312 | } |
| 4313 | start = ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 4314 | xmlSchemaBuildAContentModel(elem->subtypes, ctxt, name); |
| 4315 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
Daniel Veillard | 4402ab4 | 2002-09-12 16:02:56 +0000 | [diff] [blame] | 4316 | elem->contModel = xmlAutomataCompile(ctxt->am); |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4317 | if (elem->contModel == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4318 | xmlSchemaPErr(ctxt, elem->node, XML_SCHEMAS_ERR_INTERNAL, |
| 4319 | "failed to compile %s content model\n", name, NULL); |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4320 | } else if (xmlRegexpIsDeterminist(elem->contModel) != 1) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4321 | xmlSchemaPErr(ctxt, elem->node, XML_SCHEMAS_ERR_NOTDETERMINIST, |
| 4322 | "Content model of %s is not determinist:\n", name, |
| 4323 | NULL); |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 4324 | } else { |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 4325 | #ifdef DEBUG_CONTENT_REGEXP |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4326 | xmlGenericError(xmlGenericErrorContext, |
| 4327 | "Content model of %s:\n", name); |
| 4328 | xmlRegexpPrint(stderr, elem->contModel); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4329 | #endif |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 4330 | } |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4331 | ctxt->state = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4332 | xmlFreeAutomata(ctxt->am); |
| 4333 | ctxt->am = NULL; |
| 4334 | } |
| 4335 | |
| 4336 | /** |
| 4337 | * xmlSchemaRefFixupCallback: |
| 4338 | * @elem: the schema element context |
| 4339 | * @ctxt: the schema parser context |
| 4340 | * |
| 4341 | * Free the resources associated to the schema parser context |
| 4342 | */ |
| 4343 | static void |
| 4344 | xmlSchemaRefFixupCallback(xmlSchemaElementPtr elem, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4345 | xmlSchemaParserCtxtPtr ctxt, |
| 4346 | const xmlChar * name, |
| 4347 | const xmlChar * context ATTRIBUTE_UNUSED, |
| 4348 | const xmlChar * namespace ATTRIBUTE_UNUSED) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4349 | { |
| 4350 | if ((ctxt == NULL) || (elem == NULL)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4351 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4352 | if (elem->ref != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4353 | xmlSchemaElementPtr elemDecl; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4354 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4355 | if (elem->subtypes != NULL) { |
| 4356 | xmlSchemaPErr(ctxt, elem->node, |
| 4357 | XML_SCHEMAP_INVALID_REF_AND_SUBTYPE, |
| 4358 | "Schemas: element %s have both ref and subtype\n", |
| 4359 | name, NULL); |
| 4360 | return; |
| 4361 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 4362 | elemDecl = xmlSchemaGetElem(ctxt->schema, elem->ref, elem->refNs, 0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4363 | |
| 4364 | if (elemDecl == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4365 | xmlSchemaPErr(ctxt, elem->node, XML_SCHEMAP_UNKNOWN_REF, |
| 4366 | "Schemas: element %s ref to %s not found\n", |
| 4367 | name, elem->ref); |
| 4368 | return; |
| 4369 | } |
| 4370 | elem->refDecl = elemDecl; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4371 | } else if (elem->namedType != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4372 | xmlSchemaTypePtr typeDecl; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4373 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4374 | if (elem->subtypes != NULL) { |
| 4375 | xmlSchemaPErr(ctxt, elem->node, XML_SCHEMAP_TYPE_AND_SUBTYPE, |
| 4376 | "Schemas: element %s have both type and subtype\n", |
| 4377 | name, NULL); |
| 4378 | return; |
| 4379 | } |
| 4380 | typeDecl = xmlSchemaGetType(ctxt->schema, elem->namedType, |
| 4381 | elem->namedTypeNs); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4382 | |
| 4383 | if (typeDecl == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4384 | xmlSchemaPErr(ctxt, elem->node, XML_SCHEMAP_UNKNOWN_TYPE, |
| 4385 | "Schemas: element %s type %s not found\n", name, |
| 4386 | elem->namedType); |
| 4387 | return; |
| 4388 | } |
| 4389 | elem->subtypes = typeDecl; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4390 | } |
| 4391 | } |
| 4392 | |
Daniel Veillard | 377e1a9 | 2004-04-16 16:30:05 +0000 | [diff] [blame] | 4393 | static void |
| 4394 | xmlSchemaParseUnionRefCheck(xmlSchemaTypePtr typeDecl, |
| 4395 | xmlSchemaParserCtxtPtr ctxt) |
| 4396 | { |
| 4397 | const xmlChar *cur, *end, *prefix, *ncName, *namespace; |
| 4398 | xmlChar *tmp; |
| 4399 | xmlSchemaTypePtr subtype; |
| 4400 | xmlNsPtr ns; |
| 4401 | int len; |
| 4402 | |
| 4403 | if ((typeDecl->type != XML_SCHEMA_TYPE_UNION) || (typeDecl->ref == NULL)) |
| 4404 | return; |
| 4405 | |
| 4406 | cur = typeDecl->ref; |
| 4407 | do { |
| 4408 | while (IS_BLANK_CH(*cur)) |
| 4409 | cur++; |
| 4410 | end = cur; |
| 4411 | while ((*end != 0) && (!(IS_BLANK_CH(*end)))) |
| 4412 | end++; |
| 4413 | if (end == cur) |
| 4414 | break; |
| 4415 | tmp = xmlStrndup(cur, end - cur); |
| 4416 | ncName = xmlSplitQName3(tmp, &len); |
| 4417 | if (ncName != NULL) { |
| 4418 | prefix = xmlDictLookup(ctxt->dict, tmp, len); |
| 4419 | } else { |
| 4420 | prefix = NULL; |
| 4421 | ncName = tmp; |
| 4422 | } |
| 4423 | ns = xmlSearchNs(typeDecl->node->doc, typeDecl->node, prefix); |
| 4424 | if (ns == NULL) { |
| 4425 | xmlSchemaPErr(ctxt, typeDecl->node, XML_SCHEMAP_PREFIX_UNDEFINED, |
| 4426 | "Union %s: the namespace of member type %s is undefined\n", |
William M. Brack | 87640d5 | 2004-04-17 14:58:15 +0000 | [diff] [blame] | 4427 | typeDecl->name, (const xmlChar *) tmp); |
| 4428 | namespace = NULL; |
Daniel Veillard | 377e1a9 | 2004-04-16 16:30:05 +0000 | [diff] [blame] | 4429 | } else { |
| 4430 | namespace = xmlDictLookup(ctxt->dict, ns->href, -1); |
| 4431 | } |
| 4432 | /* Lookup the referenced type */ |
| 4433 | subtype = xmlSchemaGetType(ctxt->schema, ncName, namespace); |
| 4434 | if (subtype == NULL) { |
| 4435 | xmlSchemaPErr(ctxt, typeDecl->node, XML_SCHEMAP_UNKNOWN_MEMBER_TYPE, |
| 4436 | "Union %s references an unknown member type >%s<\n", |
William M. Brack | 87640d5 | 2004-04-17 14:58:15 +0000 | [diff] [blame] | 4437 | typeDecl->name, (const xmlChar *) tmp); |
Daniel Veillard | 377e1a9 | 2004-04-16 16:30:05 +0000 | [diff] [blame] | 4438 | } |
| 4439 | xmlFree(tmp); |
| 4440 | cur = end; |
| 4441 | } while (*cur != 0); |
| 4442 | |
| 4443 | } |
| 4444 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4445 | /** |
| 4446 | * xmlSchemaTypeFixup: |
| 4447 | * @typeDecl: the schema type definition |
| 4448 | * @ctxt: the schema parser context |
| 4449 | * |
| 4450 | * Fixes the content model of the type. |
| 4451 | */ |
| 4452 | static void |
| 4453 | xmlSchemaTypeFixup(xmlSchemaTypePtr typeDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4454 | xmlSchemaParserCtxtPtr ctxt, const xmlChar * name) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4455 | { |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 4456 | if (typeDecl == NULL) |
| 4457 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4458 | if (name == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4459 | name = typeDecl->name; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4460 | if (typeDecl->contentType == XML_SCHEMA_CONTENT_UNKNOWN) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4461 | switch (typeDecl->type) { |
| 4462 | case XML_SCHEMA_TYPE_SIMPLE_CONTENT:{ |
| 4463 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, NULL); |
| 4464 | if (typeDecl->subtypes != NULL) |
| 4465 | typeDecl->contentType = |
| 4466 | typeDecl->subtypes->contentType; |
| 4467 | break; |
| 4468 | } |
| 4469 | case XML_SCHEMA_TYPE_RESTRICTION:{ |
| 4470 | if (typeDecl->subtypes != NULL) |
| 4471 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4472 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4473 | if (typeDecl->base != NULL) { |
| 4474 | xmlSchemaTypePtr baseType; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4475 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4476 | baseType = |
| 4477 | xmlSchemaGetType(ctxt->schema, typeDecl->base, |
| 4478 | typeDecl->baseNs); |
| 4479 | if (baseType == NULL) { |
| 4480 | xmlSchemaPErr(ctxt, typeDecl->node, |
| 4481 | XML_SCHEMAP_UNKNOWN_BASE_TYPE, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4482 | "Schemas: type %s base type %s not found\n", |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4483 | name, typeDecl->base); |
| 4484 | } |
| 4485 | typeDecl->baseType = baseType; |
| 4486 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 4487 | if (typeDecl->subtypes == NULL) |
| 4488 | if (typeDecl->baseType != NULL) |
| 4489 | typeDecl->contentType = |
| 4490 | typeDecl->baseType->contentType; |
| 4491 | else |
| 4492 | /* 1.1.1 */ |
| 4493 | typeDecl->contentType = XML_SCHEMA_CONTENT_EMPTY; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4494 | else if ((typeDecl->subtypes->subtypes == NULL) && |
| 4495 | ((typeDecl->subtypes->type == |
| 4496 | XML_SCHEMA_TYPE_ALL) |
| 4497 | || (typeDecl->subtypes->type == |
| 4498 | XML_SCHEMA_TYPE_SEQUENCE))) |
| 4499 | /* 1.1.2 */ |
| 4500 | typeDecl->contentType = XML_SCHEMA_CONTENT_EMPTY; |
| 4501 | else if ((typeDecl->subtypes->type == |
| 4502 | XML_SCHEMA_TYPE_CHOICE) |
| 4503 | && (typeDecl->subtypes->subtypes == NULL)) |
| 4504 | /* 1.1.3 */ |
| 4505 | typeDecl->contentType = XML_SCHEMA_CONTENT_EMPTY; |
| 4506 | else { |
| 4507 | /* 1.2 and 2.X are applied at the other layer */ |
| 4508 | typeDecl->contentType = |
| 4509 | XML_SCHEMA_CONTENT_ELEMENTS; |
| 4510 | } |
| 4511 | break; |
| 4512 | } |
| 4513 | case XML_SCHEMA_TYPE_EXTENSION:{ |
| 4514 | xmlSchemaContentType explicitContentType; |
| 4515 | xmlSchemaTypePtr base; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4516 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4517 | if (typeDecl->base != NULL) { |
| 4518 | xmlSchemaTypePtr baseType; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4519 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4520 | baseType = |
| 4521 | xmlSchemaGetType(ctxt->schema, typeDecl->base, |
| 4522 | typeDecl->baseNs); |
| 4523 | if (baseType == NULL) { |
| 4524 | xmlSchemaPErr(ctxt, typeDecl->node, |
| 4525 | XML_SCHEMAP_UNKNOWN_BASE_TYPE, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4526 | "Schemas: type %s base type %s not found\n", |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4527 | name, typeDecl->base); |
| 4528 | } |
| 4529 | typeDecl->baseType = baseType; |
| 4530 | } |
| 4531 | if (typeDecl->subtypes != NULL) |
| 4532 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4533 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4534 | explicitContentType = XML_SCHEMA_CONTENT_ELEMENTS; |
| 4535 | if (typeDecl->subtypes == NULL) |
| 4536 | /* 1.1.1 */ |
| 4537 | explicitContentType = XML_SCHEMA_CONTENT_EMPTY; |
| 4538 | else if ((typeDecl->subtypes->subtypes == NULL) && |
| 4539 | ((typeDecl->subtypes->type == |
| 4540 | XML_SCHEMA_TYPE_ALL) |
| 4541 | || (typeDecl->subtypes->type == |
| 4542 | XML_SCHEMA_TYPE_SEQUENCE))) |
| 4543 | /* 1.1.2 */ |
| 4544 | explicitContentType = XML_SCHEMA_CONTENT_EMPTY; |
| 4545 | else if ((typeDecl->subtypes->type == |
| 4546 | XML_SCHEMA_TYPE_CHOICE) |
| 4547 | && (typeDecl->subtypes->subtypes == NULL)) |
| 4548 | /* 1.1.3 */ |
| 4549 | explicitContentType = XML_SCHEMA_CONTENT_EMPTY; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4550 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4551 | base = xmlSchemaGetType(ctxt->schema, typeDecl->base, |
| 4552 | typeDecl->baseNs); |
| 4553 | if (base == NULL) { |
| 4554 | xmlSchemaPErr(ctxt, typeDecl->node, |
| 4555 | XML_SCHEMAP_UNKNOWN_BASE_TYPE, |
| 4556 | "Schemas: base type %s of type %s not found\n", |
| 4557 | typeDecl->base, name); |
| 4558 | return; |
| 4559 | } |
Daniel Veillard | 2582a33 | 2004-04-18 19:49:46 +0000 | [diff] [blame] | 4560 | if (typeDecl->recurse) { |
| 4561 | xmlSchemaPErr(ctxt, typeDecl->node, |
| 4562 | XML_SCHEMAP_UNKNOWN_BASE_TYPE, |
| 4563 | "Schemas: extension type %s is recursive\n", |
| 4564 | name, NULL); |
| 4565 | return; |
| 4566 | } |
| 4567 | typeDecl->recurse = 1; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4568 | xmlSchemaTypeFixup(base, ctxt, NULL); |
Daniel Veillard | 2582a33 | 2004-04-18 19:49:46 +0000 | [diff] [blame] | 4569 | typeDecl->recurse = 0; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4570 | if (explicitContentType == XML_SCHEMA_CONTENT_EMPTY) { |
| 4571 | /* 2.1 */ |
| 4572 | typeDecl->contentType = base->contentType; |
| 4573 | } else if (base->contentType == |
| 4574 | XML_SCHEMA_CONTENT_EMPTY) { |
| 4575 | /* 2.2 imbitable ! */ |
| 4576 | typeDecl->contentType = |
| 4577 | XML_SCHEMA_CONTENT_ELEMENTS; |
| 4578 | } else { |
| 4579 | /* 2.3 imbitable pareil ! */ |
| 4580 | typeDecl->contentType = |
| 4581 | XML_SCHEMA_CONTENT_ELEMENTS; |
| 4582 | } |
| 4583 | break; |
| 4584 | } |
| 4585 | case XML_SCHEMA_TYPE_COMPLEX:{ |
| 4586 | if (typeDecl->subtypes == NULL) { |
| 4587 | typeDecl->contentType = XML_SCHEMA_CONTENT_EMPTY; |
Daniel Veillard | 1aefc86 | 2004-03-04 11:40:48 +0000 | [diff] [blame] | 4588 | |
| 4589 | if (typeDecl->flags & XML_SCHEMAS_TYPE_MIXED) |
| 4590 | typeDecl->contentType = |
| 4591 | XML_SCHEMA_CONTENT_MIXED; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4592 | } else { |
| 4593 | if (typeDecl->flags & XML_SCHEMAS_TYPE_MIXED) |
| 4594 | typeDecl->contentType = |
| 4595 | XML_SCHEMA_CONTENT_MIXED; |
| 4596 | else { |
| 4597 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, |
| 4598 | NULL); |
| 4599 | if (typeDecl->subtypes != NULL) |
| 4600 | typeDecl->contentType = |
| 4601 | typeDecl->subtypes->contentType; |
| 4602 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 4603 | if (typeDecl->attributes == NULL) |
| 4604 | typeDecl->attributes = |
| 4605 | typeDecl->subtypes->attributes; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4606 | } |
| 4607 | break; |
| 4608 | } |
| 4609 | case XML_SCHEMA_TYPE_COMPLEX_CONTENT:{ |
| 4610 | if (typeDecl->subtypes == NULL) { |
| 4611 | typeDecl->contentType = XML_SCHEMA_CONTENT_EMPTY; |
Daniel Veillard | 1aefc86 | 2004-03-04 11:40:48 +0000 | [diff] [blame] | 4612 | if (typeDecl->flags & XML_SCHEMAS_TYPE_MIXED) |
| 4613 | typeDecl->contentType = |
| 4614 | XML_SCHEMA_CONTENT_MIXED; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4615 | } else { |
| 4616 | if (typeDecl->flags & XML_SCHEMAS_TYPE_MIXED) |
| 4617 | typeDecl->contentType = |
| 4618 | XML_SCHEMA_CONTENT_MIXED; |
| 4619 | else { |
| 4620 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, |
| 4621 | NULL); |
| 4622 | if (typeDecl->subtypes != NULL) |
| 4623 | typeDecl->contentType = |
| 4624 | typeDecl->subtypes->contentType; |
| 4625 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 4626 | if (typeDecl->attributes == NULL) |
| 4627 | typeDecl->attributes = |
| 4628 | typeDecl->subtypes->attributes; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4629 | } |
| 4630 | break; |
| 4631 | } |
| 4632 | case XML_SCHEMA_TYPE_SEQUENCE: |
| 4633 | case XML_SCHEMA_TYPE_GROUP: |
| 4634 | case XML_SCHEMA_TYPE_ALL: |
| 4635 | case XML_SCHEMA_TYPE_CHOICE: |
| 4636 | typeDecl->contentType = XML_SCHEMA_CONTENT_ELEMENTS; |
| 4637 | break; |
| 4638 | case XML_SCHEMA_TYPE_BASIC: |
| 4639 | case XML_SCHEMA_TYPE_ANY: |
| 4640 | case XML_SCHEMA_TYPE_FACET: |
| 4641 | case XML_SCHEMA_TYPE_SIMPLE: |
| 4642 | case XML_SCHEMA_TYPE_UR: |
| 4643 | case XML_SCHEMA_TYPE_ELEMENT: |
| 4644 | case XML_SCHEMA_TYPE_ATTRIBUTE: |
| 4645 | case XML_SCHEMA_TYPE_ATTRIBUTEGROUP: |
| 4646 | case XML_SCHEMA_TYPE_NOTATION: |
| 4647 | case XML_SCHEMA_TYPE_LIST: |
| 4648 | case XML_SCHEMA_TYPE_UNION: |
Daniel Veillard | 377e1a9 | 2004-04-16 16:30:05 +0000 | [diff] [blame] | 4649 | xmlSchemaParseUnionRefCheck(typeDecl, ctxt); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4650 | case XML_SCHEMA_FACET_MININCLUSIVE: |
| 4651 | case XML_SCHEMA_FACET_MINEXCLUSIVE: |
| 4652 | case XML_SCHEMA_FACET_MAXINCLUSIVE: |
| 4653 | case XML_SCHEMA_FACET_MAXEXCLUSIVE: |
| 4654 | case XML_SCHEMA_FACET_TOTALDIGITS: |
| 4655 | case XML_SCHEMA_FACET_FRACTIONDIGITS: |
| 4656 | case XML_SCHEMA_FACET_PATTERN: |
| 4657 | case XML_SCHEMA_FACET_ENUMERATION: |
| 4658 | case XML_SCHEMA_FACET_WHITESPACE: |
| 4659 | case XML_SCHEMA_FACET_LENGTH: |
| 4660 | case XML_SCHEMA_FACET_MAXLENGTH: |
| 4661 | case XML_SCHEMA_FACET_MINLENGTH: |
| 4662 | typeDecl->contentType = XML_SCHEMA_CONTENT_SIMPLE; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 4663 | if (typeDecl->subtypes != NULL) |
| 4664 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4665 | break; |
| 4666 | } |
| 4667 | } |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4668 | #ifdef DEBUG_TYPE |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 4669 | if (typeDecl->node != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4670 | xmlGenericError(xmlGenericErrorContext, |
| 4671 | "Type of %s : %s:%d :", name, |
| 4672 | typeDecl->node->doc->URL, |
| 4673 | xmlGetLineNo(typeDecl->node)); |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 4674 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4675 | xmlGenericError(xmlGenericErrorContext, "Type of %s :", name); |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 4676 | } |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4677 | switch (typeDecl->contentType) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4678 | case XML_SCHEMA_CONTENT_SIMPLE: |
| 4679 | xmlGenericError(xmlGenericErrorContext, "simple\n"); |
| 4680 | break; |
| 4681 | case XML_SCHEMA_CONTENT_ELEMENTS: |
| 4682 | xmlGenericError(xmlGenericErrorContext, "elements\n"); |
| 4683 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4684 | case XML_SCHEMA_CONTENT_UNKNOWN: |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4685 | xmlGenericError(xmlGenericErrorContext, "unknown !!!\n"); |
| 4686 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4687 | case XML_SCHEMA_CONTENT_EMPTY: |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4688 | xmlGenericError(xmlGenericErrorContext, "empty\n"); |
| 4689 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4690 | case XML_SCHEMA_CONTENT_MIXED: |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4691 | xmlGenericError(xmlGenericErrorContext, "mixed\n"); |
| 4692 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4693 | case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS: |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4694 | xmlGenericError(xmlGenericErrorContext, "mixed or elems\n"); |
| 4695 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4696 | case XML_SCHEMA_CONTENT_BASIC: |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4697 | xmlGenericError(xmlGenericErrorContext, "basic\n"); |
| 4698 | break; |
| 4699 | default: |
| 4700 | xmlGenericError(xmlGenericErrorContext, |
| 4701 | "not registered !!!\n"); |
| 4702 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4703 | } |
| 4704 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4705 | } |
| 4706 | |
| 4707 | /** |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4708 | * xmlSchemaCheckFacet: |
| 4709 | * @facet: the facet |
| 4710 | * @typeDecl: the schema type definition |
| 4711 | * @ctxt: the schema parser context or NULL |
| 4712 | * @name: name of the type |
| 4713 | * |
| 4714 | * Checks the default values types, especially for facets |
| 4715 | * |
| 4716 | * Returns 0 if okay or -1 in cae of error |
| 4717 | */ |
| 4718 | int |
| 4719 | xmlSchemaCheckFacet(xmlSchemaFacetPtr facet, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4720 | xmlSchemaTypePtr typeDecl, |
| 4721 | xmlSchemaParserCtxtPtr ctxt, const xmlChar * name) |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4722 | { |
| 4723 | static xmlSchemaTypePtr nonNegativeIntegerType = NULL; |
| 4724 | int ret = 0; |
| 4725 | |
| 4726 | if (nonNegativeIntegerType == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4727 | nonNegativeIntegerType = |
| 4728 | xmlSchemaGetPredefinedType(BAD_CAST "nonNegativeInteger", |
| 4729 | xmlSchemaNs); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4730 | } |
| 4731 | switch (facet->type) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4732 | case XML_SCHEMA_FACET_MININCLUSIVE: |
| 4733 | case XML_SCHEMA_FACET_MINEXCLUSIVE: |
| 4734 | case XML_SCHEMA_FACET_MAXINCLUSIVE: |
| 4735 | case XML_SCHEMA_FACET_MAXEXCLUSIVE:{ |
| 4736 | /* |
| 4737 | * Okay we need to validate the value |
| 4738 | * at that point. |
| 4739 | */ |
| 4740 | xmlSchemaValidCtxtPtr vctxt; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4741 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4742 | vctxt = xmlSchemaNewValidCtxt(NULL); |
| 4743 | if (vctxt == NULL) |
| 4744 | break; |
| 4745 | xmlSchemaValidateSimpleValue(vctxt, typeDecl, |
| 4746 | facet->value); |
| 4747 | facet->val = vctxt->value; |
| 4748 | vctxt->value = NULL; |
| 4749 | if (facet->val == NULL) { |
| 4750 | /* error code */ |
| 4751 | if (ctxt != NULL) { |
| 4752 | xmlSchemaPErr(ctxt, facet->node, |
| 4753 | XML_SCHEMAP_INVALID_FACET, |
| 4754 | "Schemas: type %s facet value %s invalid\n", |
| 4755 | name, facet->value); |
| 4756 | } |
| 4757 | ret = -1; |
| 4758 | } |
| 4759 | xmlSchemaFreeValidCtxt(vctxt); |
| 4760 | break; |
| 4761 | } |
| 4762 | case XML_SCHEMA_FACET_ENUMERATION:{ |
| 4763 | /* |
| 4764 | * Okay we need to validate the value |
| 4765 | * at that point. |
| 4766 | */ |
| 4767 | xmlSchemaValidCtxtPtr vctxt; |
| 4768 | int tmp; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4769 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4770 | vctxt = xmlSchemaNewValidCtxt(NULL); |
| 4771 | if (vctxt == NULL) |
| 4772 | break; |
| 4773 | tmp = xmlSchemaValidateSimpleValue(vctxt, typeDecl, |
| 4774 | facet->value); |
| 4775 | if (tmp != 0) { |
| 4776 | if (ctxt != NULL) { |
| 4777 | xmlSchemaPErr(ctxt, facet->node, |
| 4778 | XML_SCHEMAP_INVALID_ENUM, |
| 4779 | "Schemas: type %s enumeration value %s invalid\n", |
| 4780 | name, facet->value); |
| 4781 | } |
| 4782 | ret = -1; |
| 4783 | } |
| 4784 | xmlSchemaFreeValidCtxt(vctxt); |
| 4785 | break; |
| 4786 | } |
| 4787 | case XML_SCHEMA_FACET_PATTERN: |
| 4788 | facet->regexp = xmlRegexpCompile(facet->value); |
| 4789 | if (facet->regexp == NULL) { |
| 4790 | xmlSchemaPErr(ctxt, typeDecl->node, |
| 4791 | XML_SCHEMAP_REGEXP_INVALID, |
| 4792 | "Schemas: type %s facet regexp %s invalid\n", |
| 4793 | name, facet->value); |
| 4794 | ret = -1; |
| 4795 | } |
| 4796 | break; |
| 4797 | case XML_SCHEMA_FACET_TOTALDIGITS: |
| 4798 | case XML_SCHEMA_FACET_FRACTIONDIGITS: |
| 4799 | case XML_SCHEMA_FACET_LENGTH: |
| 4800 | case XML_SCHEMA_FACET_MAXLENGTH: |
| 4801 | case XML_SCHEMA_FACET_MINLENGTH:{ |
| 4802 | int tmp; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4803 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4804 | tmp = |
| 4805 | xmlSchemaValidatePredefinedType(nonNegativeIntegerType, |
| 4806 | facet->value, |
| 4807 | &facet->val); |
| 4808 | if (tmp != 0) { |
| 4809 | /* error code */ |
| 4810 | if (ctxt != NULL) { |
| 4811 | xmlSchemaPErr(ctxt, facet->node, |
| 4812 | XML_SCHEMAP_INVALID_FACET_VALUE, |
| 4813 | "Schemas: type %s facet value %s invalid\n", |
| 4814 | name, facet->value); |
| 4815 | } |
| 4816 | ret = -1; |
| 4817 | } |
| 4818 | break; |
| 4819 | } |
| 4820 | case XML_SCHEMA_FACET_WHITESPACE:{ |
| 4821 | if (xmlStrEqual(facet->value, BAD_CAST "preserve")) { |
| 4822 | facet->whitespace = XML_SCHEMAS_FACET_PRESERVE; |
| 4823 | } else if (xmlStrEqual(facet->value, BAD_CAST "replace")) { |
| 4824 | facet->whitespace = XML_SCHEMAS_FACET_REPLACE; |
| 4825 | } else if (xmlStrEqual(facet->value, BAD_CAST "collapse")) { |
| 4826 | facet->whitespace = XML_SCHEMAS_FACET_COLLAPSE; |
| 4827 | } else { |
| 4828 | if (ctxt != NULL) { |
| 4829 | xmlSchemaPErr(ctxt, facet->node, |
| 4830 | XML_SCHEMAP_INVALID_WHITE_SPACE, |
| 4831 | "Schemas: type %s whiteSpace value %s invalid\n", |
| 4832 | name, facet->value); |
| 4833 | } |
| 4834 | ret = -1; |
| 4835 | } |
| 4836 | } |
| 4837 | default: |
| 4838 | break; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4839 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4840 | return (ret); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4841 | } |
| 4842 | |
| 4843 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4844 | * xmlSchemaCheckDefaults: |
| 4845 | * @typeDecl: the schema type definition |
| 4846 | * @ctxt: the schema parser context |
| 4847 | * |
| 4848 | * Checks the default values types, especially for facets |
| 4849 | */ |
| 4850 | static void |
| 4851 | xmlSchemaCheckDefaults(xmlSchemaTypePtr typeDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4852 | xmlSchemaParserCtxtPtr ctxt, const xmlChar * name) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4853 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4854 | if (name == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4855 | name = typeDecl->name; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4856 | if (typeDecl->type == XML_SCHEMA_TYPE_RESTRICTION) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4857 | if (typeDecl->facets != NULL) { |
| 4858 | xmlSchemaFacetPtr facet = typeDecl->facets; |
| 4859 | |
| 4860 | while (facet != NULL) { |
| 4861 | xmlSchemaCheckFacet(facet, typeDecl, ctxt, name); |
| 4862 | facet = facet->next; |
| 4863 | } |
| 4864 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4865 | } |
| 4866 | } |
| 4867 | |
| 4868 | /** |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4869 | * xmlSchemaAttrGrpFixup: |
| 4870 | * @attrgrpDecl: the schema attribute definition |
| 4871 | * @ctxt: the schema parser context |
| 4872 | * @name: the attribute name |
| 4873 | * |
| 4874 | * Fixes finish doing the computations on the attributes definitions |
| 4875 | */ |
| 4876 | static void |
| 4877 | xmlSchemaAttrGrpFixup(xmlSchemaAttributeGroupPtr attrgrpDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4878 | xmlSchemaParserCtxtPtr ctxt, const xmlChar * name) |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4879 | { |
| 4880 | if (name == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4881 | name = attrgrpDecl->name; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4882 | if (attrgrpDecl->attributes != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4883 | return; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4884 | if (attrgrpDecl->ref != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4885 | xmlSchemaAttributeGroupPtr ref; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4886 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4887 | ref = xmlHashLookup2(ctxt->schema->attrgrpDecl, attrgrpDecl->ref, |
| 4888 | attrgrpDecl->refNs); |
| 4889 | if (ref == NULL) { |
| 4890 | xmlSchemaPErr(ctxt, attrgrpDecl->node, |
| 4891 | XML_SCHEMAP_UNKNOWN_ATTRIBUTE_GROUP, |
| 4892 | "Schemas: attribute group %s reference %s not found\n", |
| 4893 | name, attrgrpDecl->ref); |
| 4894 | return; |
| 4895 | } |
| 4896 | xmlSchemaAttrGrpFixup(ref, ctxt, NULL); |
| 4897 | attrgrpDecl->attributes = ref->attributes; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4898 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4899 | xmlSchemaPErr(ctxt, attrgrpDecl->node, XML_SCHEMAP_NOATTR_NOREF, |
| 4900 | "Schemas: attribute %s has no attributes nor reference\n", |
| 4901 | name, NULL); |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4902 | } |
| 4903 | } |
| 4904 | |
| 4905 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4906 | * xmlSchemaAttrFixup: |
| 4907 | * @attrDecl: the schema attribute definition |
| 4908 | * @ctxt: the schema parser context |
| 4909 | * @name: the attribute name |
| 4910 | * |
| 4911 | * Fixes finish doing the computations on the attributes definitions |
| 4912 | */ |
| 4913 | static void |
| 4914 | xmlSchemaAttrFixup(xmlSchemaAttributePtr attrDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4915 | xmlSchemaParserCtxtPtr ctxt, const xmlChar * name) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4916 | { |
| 4917 | if (name == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4918 | name = attrDecl->name; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4919 | if (attrDecl->subtypes != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4920 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4921 | if (attrDecl->typeName != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4922 | xmlSchemaTypePtr type; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4923 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4924 | type = xmlSchemaGetType(ctxt->schema, attrDecl->typeName, |
| 4925 | attrDecl->typeNs); |
| 4926 | if (type == NULL) { |
| 4927 | xmlSchemaPErr(ctxt, attrDecl->node, XML_SCHEMAP_UNKNOWN_TYPE, |
| 4928 | "Schemas: attribute %s type %s not found\n", |
| 4929 | name, attrDecl->typeName); |
| 4930 | } |
| 4931 | attrDecl->subtypes = type; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4932 | } else if (attrDecl->ref != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4933 | xmlSchemaAttributePtr ref; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4934 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4935 | ref = xmlHashLookup2(ctxt->schema->attrDecl, attrDecl->ref, |
| 4936 | attrDecl->refNs); |
| 4937 | if (ref == NULL) { |
| 4938 | xmlSchemaPErr(ctxt, attrDecl->node, XML_SCHEMAP_UNKNOWN_REF, |
| 4939 | "Schemas: attribute %s reference %s not found\n", |
| 4940 | name, attrDecl->ref); |
| 4941 | return; |
| 4942 | } |
| 4943 | xmlSchemaAttrFixup(ref, ctxt, NULL); |
| 4944 | attrDecl->subtypes = ref->subtypes; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4945 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4946 | xmlSchemaPErr(ctxt, attrDecl->node, XML_SCHEMAP_NOTYPE_NOREF, |
| 4947 | "Schemas: attribute %s has no type nor reference\n", |
| 4948 | name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4949 | } |
| 4950 | } |
| 4951 | |
| 4952 | /** |
| 4953 | * xmlSchemaParse: |
| 4954 | * @ctxt: a schema validation context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4955 | * |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4956 | * parse a schema definition resource and build an internal |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4957 | * XML Shema struture which can be used to validate instances. |
| 4958 | * *WARNING* this interface is highly subject to change |
| 4959 | * |
| 4960 | * Returns the internal XML Schema structure built from the resource or |
| 4961 | * NULL in case of error |
| 4962 | */ |
| 4963 | xmlSchemaPtr |
| 4964 | xmlSchemaParse(xmlSchemaParserCtxtPtr ctxt) |
| 4965 | { |
| 4966 | xmlSchemaPtr ret = NULL; |
| 4967 | xmlDocPtr doc; |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 4968 | xmlNodePtr root; |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 4969 | int nberrors; |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 4970 | int preserve = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4971 | |
| 4972 | xmlSchemaInitTypes(); |
| 4973 | |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 4974 | if (ctxt == NULL) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4975 | return (NULL); |
| 4976 | |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 4977 | nberrors = ctxt->nberrors; |
| 4978 | ctxt->nberrors = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4979 | ctxt->counter = 0; |
| 4980 | ctxt->container = NULL; |
| 4981 | |
| 4982 | /* |
| 4983 | * First step is to parse the input document into an DOM/Infoset |
| 4984 | */ |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 4985 | if (ctxt->URL != NULL) { |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 4986 | doc = xmlReadFile((const char *) ctxt->URL, NULL, |
| 4987 | SCHEMAS_PARSE_OPTIONS); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4988 | if (doc == NULL) { |
| 4989 | xmlSchemaPErr(ctxt, NULL, |
| 4990 | XML_SCHEMAP_FAILED_LOAD, |
| 4991 | "xmlSchemaParse: could not load %s\n", |
| 4992 | ctxt->URL, NULL); |
| 4993 | return (NULL); |
| 4994 | } |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 4995 | } else if (ctxt->buffer != NULL) { |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 4996 | doc = xmlReadMemory(ctxt->buffer, ctxt->size, NULL, NULL, |
| 4997 | SCHEMAS_PARSE_OPTIONS); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4998 | if (doc == NULL) { |
| 4999 | xmlSchemaPErr(ctxt, NULL, |
| 5000 | XML_SCHEMAP_FAILED_PARSE, |
| 5001 | "xmlSchemaParse: could not parse\n", |
| 5002 | NULL, NULL); |
| 5003 | return (NULL); |
| 5004 | } |
| 5005 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
Daniel Veillard | 6576528 | 2004-01-08 16:59:30 +0000 | [diff] [blame] | 5006 | ctxt->URL = xmlDictLookup(ctxt->dict, BAD_CAST "in_memory_buffer", -1); |
Daniel Veillard | 9d75150 | 2003-10-29 13:21:47 +0000 | [diff] [blame] | 5007 | } else if (ctxt->doc != NULL) { |
| 5008 | doc = ctxt->doc; |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 5009 | preserve = 1; |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 5010 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5011 | xmlSchemaPErr(ctxt, NULL, |
| 5012 | XML_SCHEMAP_NOTHING_TO_PARSE, |
| 5013 | "xmlSchemaParse: could not parse\n", |
| 5014 | NULL, NULL); |
| 5015 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5016 | } |
| 5017 | |
| 5018 | /* |
| 5019 | * Then extract the root and Schema parse it |
| 5020 | */ |
| 5021 | root = xmlDocGetRootElement(doc); |
| 5022 | if (root == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5023 | xmlSchemaPErr(ctxt, (xmlNodePtr) doc, |
| 5024 | XML_SCHEMAP_NOROOT, |
| 5025 | "schemas has no root", NULL, NULL); |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 5026 | if (!preserve) { |
| 5027 | xmlFreeDoc(doc); |
| 5028 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5029 | return (NULL); |
| 5030 | } |
| 5031 | |
| 5032 | /* |
| 5033 | * Remove all the blank text nodes |
| 5034 | */ |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 5035 | xmlSchemaCleanupDoc(ctxt, root); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5036 | |
| 5037 | /* |
| 5038 | * Then do the parsing for good |
| 5039 | */ |
| 5040 | ret = xmlSchemaParseSchema(ctxt, root); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 5041 | if (ret == NULL) { |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 5042 | if (!preserve) { |
| 5043 | xmlFreeDoc(doc); |
| 5044 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5045 | return (NULL); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 5046 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5047 | ret->doc = doc; |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 5048 | ret->preserve = preserve; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5049 | |
| 5050 | /* |
| 5051 | * Then fix all the references. |
| 5052 | */ |
| 5053 | ctxt->schema = ret; |
| 5054 | xmlHashScanFull(ret->elemDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5055 | (xmlHashScannerFull) xmlSchemaRefFixupCallback, ctxt); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5056 | |
| 5057 | /* |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 5058 | * Then fixup all attributes declarations |
| 5059 | */ |
| 5060 | xmlHashScan(ret->attrDecl, (xmlHashScanner) xmlSchemaAttrFixup, ctxt); |
| 5061 | |
| 5062 | /* |
| 5063 | * Then fixup all attributes group declarations |
| 5064 | */ |
| 5065 | xmlHashScan(ret->attrgrpDecl, (xmlHashScanner) xmlSchemaAttrGrpFixup, |
| 5066 | ctxt); |
| 5067 | |
| 5068 | /* |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5069 | * Then fixup all types properties |
| 5070 | */ |
| 5071 | xmlHashScan(ret->typeDecl, (xmlHashScanner) xmlSchemaTypeFixup, ctxt); |
| 5072 | |
| 5073 | /* |
| 5074 | * Then build the content model for all elements |
| 5075 | */ |
| 5076 | xmlHashScan(ret->elemDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5077 | (xmlHashScanner) xmlSchemaBuildContentModel, ctxt); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5078 | |
| 5079 | /* |
| 5080 | * Then check the defaults part of the type like facets values |
| 5081 | */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5082 | xmlHashScan(ret->typeDecl, (xmlHashScanner) xmlSchemaCheckDefaults, |
| 5083 | ctxt); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5084 | |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 5085 | if (ctxt->nberrors != 0) { |
| 5086 | xmlSchemaFree(ret); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5087 | ret = NULL; |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 5088 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5089 | return (ret); |
| 5090 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5091 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5092 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5093 | * xmlSchemaSetParserErrors: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5094 | * @ctxt: a schema validation context |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5095 | * @err: the error callback |
| 5096 | * @warn: the warning callback |
| 5097 | * @ctx: contextual data for the callbacks |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5098 | * |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5099 | * Set the callback functions used to handle errors for a validation context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5100 | */ |
| 5101 | void |
| 5102 | xmlSchemaSetParserErrors(xmlSchemaParserCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5103 | xmlSchemaValidityErrorFunc err, |
| 5104 | xmlSchemaValidityWarningFunc warn, void *ctx) |
| 5105 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5106 | if (ctxt == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5107 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5108 | ctxt->error = err; |
| 5109 | ctxt->warning = warn; |
| 5110 | ctxt->userData = ctx; |
| 5111 | } |
| 5112 | |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5113 | /** |
| 5114 | * xmlSchemaFacetTypeToString: |
| 5115 | * @type: the facet type |
| 5116 | * |
| 5117 | * Convert the xmlSchemaTypeType to a char string. |
| 5118 | * |
| 5119 | * Returns the char string representation of the facet type if the |
| 5120 | * type is a facet and an "Internal Error" string otherwise. |
| 5121 | */ |
| 5122 | static const char * |
| 5123 | xmlSchemaFacetTypeToString(xmlSchemaTypeType type) |
| 5124 | { |
| 5125 | switch (type) { |
| 5126 | case XML_SCHEMA_FACET_PATTERN: |
| 5127 | return ("pattern"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5128 | case XML_SCHEMA_FACET_MAXEXCLUSIVE: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5129 | return ("maxExclusive"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5130 | case XML_SCHEMA_FACET_MAXINCLUSIVE: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5131 | return ("maxInclusive"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5132 | case XML_SCHEMA_FACET_MINEXCLUSIVE: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5133 | return ("minExclusive"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5134 | case XML_SCHEMA_FACET_MININCLUSIVE: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5135 | return ("minInclusive"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5136 | case XML_SCHEMA_FACET_WHITESPACE: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5137 | return ("whiteSpace"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5138 | case XML_SCHEMA_FACET_ENUMERATION: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5139 | return ("enumeration"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5140 | case XML_SCHEMA_FACET_LENGTH: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5141 | return ("length"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5142 | case XML_SCHEMA_FACET_MAXLENGTH: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5143 | return ("maxLength"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5144 | case XML_SCHEMA_FACET_MINLENGTH: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5145 | return ("minLength"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5146 | case XML_SCHEMA_FACET_TOTALDIGITS: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5147 | return ("totalDigits"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5148 | case XML_SCHEMA_FACET_FRACTIONDIGITS: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5149 | return ("fractionDigits"); |
| 5150 | default: |
| 5151 | break; |
| 5152 | } |
| 5153 | return ("Internal Error"); |
| 5154 | } |
| 5155 | |
| 5156 | /** |
Daniel Veillard | 377e1a9 | 2004-04-16 16:30:05 +0000 | [diff] [blame] | 5157 | * xmlSchemaValidateFacetsInternal: |
| 5158 | * @ctxt: a schema validation context |
| 5159 | * @base: the base type |
| 5160 | * @facets: the list of facets to check |
| 5161 | * @value: the lexical repr of the value to validate |
| 5162 | * @val: the precomputed value |
| 5163 | * @fireErrors: if 0, only internal errors will be fired; otherwise all errors will be fired. |
| 5164 | * |
| 5165 | * Check a value against all facet conditions |
| 5166 | * |
| 5167 | * Returns 0 if the element is schemas valid, a positive error code |
| 5168 | * number otherwise and -1 in case of internal or API error. |
| 5169 | */ |
| 5170 | static int |
| 5171 | xmlSchemaValidateFacetsInternal(xmlSchemaValidCtxtPtr ctxt, |
| 5172 | xmlSchemaTypePtr base, |
| 5173 | xmlSchemaFacetPtr facets, const xmlChar * value, int fireErrors) |
| 5174 | { |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5175 | int ret = 0; |
| 5176 | int tmp = 0; |
| 5177 | xmlSchemaTypeType type; |
| 5178 | xmlSchemaFacetPtr facet = facets; |
| 5179 | |
| 5180 | while (facet != NULL) { |
| 5181 | type = facet->type; |
| 5182 | if (type == XML_SCHEMA_FACET_ENUMERATION) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5183 | tmp = 1; |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5184 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5185 | while (facet != NULL) { |
| 5186 | tmp = |
| 5187 | xmlSchemaValidateFacet(base, facet, value, |
| 5188 | ctxt->value); |
| 5189 | if (tmp == 0) { |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5190 | return 0; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5191 | } |
| 5192 | facet = facet->next; |
| 5193 | } |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5194 | } else |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5195 | tmp = xmlSchemaValidateFacet(base, facet, value, ctxt->value); |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5196 | |
| 5197 | if (tmp != 0) { |
| 5198 | ret = tmp; |
Daniel Veillard | 377e1a9 | 2004-04-16 16:30:05 +0000 | [diff] [blame] | 5199 | if (fireErrors) |
| 5200 | 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] | 5201 | } |
| 5202 | if (facet != NULL) |
| 5203 | facet = facet->next; |
| 5204 | } |
| 5205 | return (ret); |
| 5206 | } |
| 5207 | |
William M. Brack | 87640d5 | 2004-04-17 14:58:15 +0000 | [diff] [blame] | 5208 | /** |
| 5209 | * xmlSchemaValidateFacets: |
| 5210 | * @ctxt: a schema validation context |
| 5211 | * @base: the base type |
| 5212 | * @facets: the list of facets to check |
| 5213 | * @value: the lexical repr of the value to validate |
| 5214 | * @val: the precomputed value |
| 5215 | * |
| 5216 | * Check a value against all facet conditions |
| 5217 | * |
| 5218 | * Returns 0 if the element is schemas valid, a positive error code |
| 5219 | * number otherwise and -1 in case of internal or API error. |
| 5220 | */ |
| 5221 | static int |
| 5222 | xmlSchemaValidateFacets(xmlSchemaValidCtxtPtr ctxt, |
| 5223 | xmlSchemaTypePtr base, |
| 5224 | xmlSchemaFacetPtr facets, const xmlChar * value) |
| 5225 | { |
| 5226 | return(xmlSchemaValidateFacetsInternal(ctxt, base, facets, value, 1)); |
| 5227 | } |
| 5228 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5229 | /************************************************************************ |
| 5230 | * * |
| 5231 | * Simple type validation * |
| 5232 | * * |
| 5233 | ************************************************************************/ |
| 5234 | |
| 5235 | /** |
Daniel Veillard | 377e1a9 | 2004-04-16 16:30:05 +0000 | [diff] [blame] | 5236 | * xmlSchemaValidateSimpleValueUnion: |
| 5237 | * @ctxt: a schema validation context |
| 5238 | * @type: the type declaration |
| 5239 | * @value: the value to validate |
| 5240 | * |
| 5241 | * Validates a value against a union. |
| 5242 | * |
| 5243 | * Returns 0 if the value is valid, a positive error code |
| 5244 | * number otherwise and -1 in case of internal or API error. |
| 5245 | */ |
| 5246 | static int |
| 5247 | xmlSchemaValidateSimpleValueUnion(xmlSchemaValidCtxtPtr ctxt, |
| 5248 | xmlSchemaTypePtr type, const xmlChar * value) |
| 5249 | { |
| 5250 | int ret = 0; |
| 5251 | const xmlChar *cur, *end, *prefix, *ncName; |
| 5252 | xmlChar *tmp; |
| 5253 | xmlSchemaTypePtr subtype; |
| 5254 | xmlNsPtr ns; |
| 5255 | int len; |
| 5256 | |
| 5257 | |
| 5258 | /* Process referenced memberTypes. */ |
| 5259 | cur = type->ref; |
| 5260 | do { |
| 5261 | while (IS_BLANK_CH(*cur)) |
| 5262 | cur++; |
| 5263 | end = cur; |
| 5264 | while ((*end != 0) && (!(IS_BLANK_CH(*end)))) |
| 5265 | end++; |
| 5266 | if (end == cur) |
| 5267 | break; |
| 5268 | tmp = xmlStrndup(cur, end - cur); |
| 5269 | ncName = xmlSplitQName3(tmp, &len); |
| 5270 | if (ncName != NULL) { |
| 5271 | prefix = xmlStrndup(tmp, len); |
| 5272 | /* prefix = xmlDictLookup(ctxt->doc->dict, tmp, len); */ |
| 5273 | } else { |
| 5274 | prefix = NULL; |
| 5275 | ncName = tmp; |
| 5276 | } |
| 5277 | /* We won't do additional checks here, since they have been performed during parsing. */ |
| 5278 | ns = xmlSearchNs(type->node->doc, type->node, prefix); |
| 5279 | /* namespace = xmlDictLookup(ctxt->doc->dict, ns->href, -1); */ |
| 5280 | subtype = xmlSchemaGetType(ctxt->schema, ncName, ns->href); |
| 5281 | if (tmp != NULL) |
| 5282 | xmlFree(tmp); |
| 5283 | if (prefix != NULL) |
William M. Brack | 87640d5 | 2004-04-17 14:58:15 +0000 | [diff] [blame] | 5284 | xmlFree((void *)prefix); |
Daniel Veillard | 377e1a9 | 2004-04-16 16:30:05 +0000 | [diff] [blame] | 5285 | ret = xmlSchemaValidateSimpleValueInternal(ctxt, subtype, value, 0); |
| 5286 | if ((ret == 0) || (ret == -1)) { |
| 5287 | return (ret); |
| 5288 | } |
| 5289 | cur = end; |
| 5290 | } while (*cur != 0); |
| 5291 | |
| 5292 | if (type->subtypes != NULL) { |
| 5293 | subtype = type->subtypes; |
| 5294 | do { |
| 5295 | ret = xmlSchemaValidateSimpleValueInternal(ctxt, subtype, value, 0); |
| 5296 | if ((ret == 0) || (ret == -1)) { |
| 5297 | return (ret); |
| 5298 | } |
| 5299 | subtype = subtype->next; |
| 5300 | } while (subtype != NULL); |
| 5301 | } |
| 5302 | return (ret); |
| 5303 | } |
| 5304 | |
| 5305 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5306 | * xmlSchemaValidateSimpleValue: |
| 5307 | * @ctxt: a schema validation context |
| 5308 | * @type: the type declaration |
| 5309 | * @value: the value to validate |
| 5310 | * |
| 5311 | * Validate a value against a simple type |
| 5312 | * |
| 5313 | * Returns 0 if the value is valid, a positive error code |
| 5314 | * number otherwise and -1 in case of internal or API error. |
| 5315 | */ |
| 5316 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5317 | xmlSchemaValidateSimpleValue(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5318 | xmlSchemaTypePtr type, const xmlChar * value) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5319 | { |
Daniel Veillard | 377e1a9 | 2004-04-16 16:30:05 +0000 | [diff] [blame] | 5320 | return (xmlSchemaValidateSimpleValueInternal(ctxt, type, value, 1)); |
| 5321 | } |
| 5322 | |
| 5323 | /** |
| 5324 | * xmlSchemaValidateSimpleValue: |
| 5325 | * @ctxt: a schema validation context |
| 5326 | * @type: the type declaration |
| 5327 | * @value: the value to validate |
| 5328 | * @fireErrors: if 0, only internal errors will be fired; otherwise all errors will be fired. |
| 5329 | * |
| 5330 | * Validate a value against a simple type |
| 5331 | * |
| 5332 | * Returns 0 if the value is valid, a positive error code |
| 5333 | * number otherwise and -1 in case of internal or API error. |
| 5334 | */ |
| 5335 | static int |
| 5336 | xmlSchemaValidateSimpleValueInternal(xmlSchemaValidCtxtPtr ctxt, |
| 5337 | xmlSchemaTypePtr type, const xmlChar * value, int fireErrors) |
| 5338 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5339 | int ret = 0; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5340 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5341 | /* |
| 5342 | * First normalize the value accordingly to Schema Datatype |
| 5343 | * 4.3.6 whiteSpace definition of the whiteSpace facet of type |
| 5344 | */ |
| 5345 | /* |
| 5346 | * Then check the normalized value against the lexical space of the |
| 5347 | * type. |
| 5348 | */ |
| 5349 | if (type->type == XML_SCHEMA_TYPE_BASIC) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5350 | if (ctxt->value != NULL) { |
| 5351 | xmlSchemaFreeValue(ctxt->value); |
| 5352 | ctxt->value = NULL; |
| 5353 | } |
| 5354 | ret = xmlSchemaValPredefTypeNode(type, value, &(ctxt->value), |
| 5355 | ctxt->cur); |
Daniel Veillard | 377e1a9 | 2004-04-16 16:30:05 +0000 | [diff] [blame] | 5356 | if ((fireErrors) && (ret != 0)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5357 | 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] | 5358 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5359 | } else if (type->type == XML_SCHEMA_TYPE_RESTRICTION) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5360 | xmlSchemaTypePtr base; |
| 5361 | xmlSchemaFacetPtr facet; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5362 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5363 | base = type->baseType; |
| 5364 | if (base != NULL) { |
Daniel Veillard | 377e1a9 | 2004-04-16 16:30:05 +0000 | [diff] [blame] | 5365 | ret = xmlSchemaValidateSimpleValueInternal(ctxt, base, value, fireErrors); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5366 | } else if (type->subtypes != NULL) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5367 | TODO |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5368 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5369 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5370 | /* |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 5371 | * Do not validate facets or attributes when working on |
| 5372 | * building the Schemas |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5373 | */ |
| 5374 | if (ctxt->schema != NULL) { |
| 5375 | if (ret == 0) { |
| 5376 | facet = type->facets; |
Daniel Veillard | 377e1a9 | 2004-04-16 16:30:05 +0000 | [diff] [blame] | 5377 | ret = xmlSchemaValidateFacetsInternal(ctxt, base, facet, value, fireErrors); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5378 | } |
| 5379 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5380 | } else if (type->type == XML_SCHEMA_TYPE_SIMPLE) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5381 | xmlSchemaTypePtr base; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5382 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5383 | base = type->subtypes; |
| 5384 | if (base != NULL) { |
Daniel Veillard | 377e1a9 | 2004-04-16 16:30:05 +0000 | [diff] [blame] | 5385 | ret = xmlSchemaValidateSimpleValueInternal(ctxt, base, value, fireErrors); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5386 | } else { |
| 5387 | TODO} |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5388 | } else if (type->type == XML_SCHEMA_TYPE_LIST) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5389 | xmlSchemaTypePtr base; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5390 | const xmlChar *cur, *end; |
| 5391 | xmlChar *tmp; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5392 | int ret2; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5393 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5394 | base = type->subtypes; |
| 5395 | if (base == NULL) { |
| 5396 | xmlSchemaVErr(ctxt, type->node, XML_SCHEMAS_ERR_INTERNAL, |
| 5397 | "Internal: List type %s has no base type\n", |
| 5398 | type->name, NULL); |
| 5399 | return (-1); |
| 5400 | } |
| 5401 | cur = value; |
| 5402 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5403 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5404 | cur++; |
| 5405 | end = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5406 | while ((*end != 0) && (!(IS_BLANK_CH(*end)))) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5407 | end++; |
| 5408 | if (end == cur) |
| 5409 | break; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5410 | tmp = xmlStrndup(cur, end - cur); |
Daniel Veillard | 377e1a9 | 2004-04-16 16:30:05 +0000 | [diff] [blame] | 5411 | ret2 = xmlSchemaValidateSimpleValueInternal(ctxt, base, tmp, fireErrors); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5412 | xmlFree(tmp); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5413 | if (ret2 != 0) |
| 5414 | ret = 1; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5415 | cur = end; |
| 5416 | } while (*cur != 0); |
Daniel Veillard | 377e1a9 | 2004-04-16 16:30:05 +0000 | [diff] [blame] | 5417 | } else if (type->type == XML_SCHEMA_TYPE_UNION) { |
| 5418 | ret = xmlSchemaValidateSimpleValueUnion(ctxt, type, value); |
| 5419 | if ((fireErrors) && (ret != 0)) { |
| 5420 | xmlSchemaVErr(ctxt, ctxt->cur, XML_SCHEMAS_ERR_VALUE, "Failed to validate type %s\n", type->name, NULL); |
| 5421 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5422 | } else { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5423 | TODO |
| 5424 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5425 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5426 | } |
| 5427 | |
| 5428 | /************************************************************************ |
| 5429 | * * |
| 5430 | * DOM Validation code * |
| 5431 | * * |
| 5432 | ************************************************************************/ |
| 5433 | |
| 5434 | static int xmlSchemaValidateContent(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5435 | xmlNodePtr node); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5436 | static int xmlSchemaValidateAttributes(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5437 | xmlNodePtr elem, |
| 5438 | xmlSchemaAttributePtr attributes); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5439 | static int xmlSchemaValidateType(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5440 | xmlNodePtr elem, |
| 5441 | xmlSchemaElementPtr elemDecl, |
| 5442 | xmlSchemaTypePtr type); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5443 | |
| 5444 | /** |
| 5445 | * xmlSchemaRegisterAttributes: |
| 5446 | * @ctxt: a schema validation context |
| 5447 | * @attrs: a list of attributes |
| 5448 | * |
| 5449 | * Register the list of attributes as the set to be validated on that element |
| 5450 | * |
| 5451 | * Returns -1 in case of error, 0 otherwise |
| 5452 | */ |
| 5453 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5454 | xmlSchemaRegisterAttributes(xmlSchemaValidCtxtPtr ctxt, xmlAttrPtr attrs) |
| 5455 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5456 | while (attrs != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5457 | if ((attrs->ns != NULL) && |
| 5458 | (xmlStrEqual(attrs->ns->href, xmlSchemaInstanceNs))) { |
| 5459 | attrs = attrs->next; |
| 5460 | continue; |
| 5461 | } |
| 5462 | if (ctxt->attrNr >= ctxt->attrMax) { |
| 5463 | xmlSchemaAttrStatePtr tmp; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5464 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5465 | ctxt->attrMax *= 2; |
| 5466 | tmp = (xmlSchemaAttrStatePtr) |
| 5467 | xmlRealloc(ctxt->attr, ctxt->attrMax * |
| 5468 | sizeof(xmlSchemaAttrState)); |
| 5469 | if (tmp == NULL) { |
| 5470 | xmlSchemaVErrMemory(ctxt, "registering attributes", NULL); |
| 5471 | ctxt->attrMax /= 2; |
| 5472 | return (-1); |
| 5473 | } |
| 5474 | ctxt->attr = tmp; |
| 5475 | } |
| 5476 | ctxt->attr[ctxt->attrNr].attr = attrs; |
| 5477 | ctxt->attr[ctxt->attrNr].state = XML_SCHEMAS_ATTR_UNKNOWN; |
| 5478 | ctxt->attrNr++; |
| 5479 | attrs = attrs->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5480 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5481 | return (0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5482 | } |
| 5483 | |
| 5484 | /** |
| 5485 | * xmlSchemaCheckAttributes: |
| 5486 | * @ctxt: a schema validation context |
| 5487 | * @node: the node carrying it. |
| 5488 | * |
| 5489 | * Check that the registered set of attributes on the current node |
| 5490 | * has been properly validated. |
| 5491 | * |
| 5492 | * Returns 0 if validity constraints are met, 1 otherwise. |
| 5493 | */ |
| 5494 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5495 | xmlSchemaCheckAttributes(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5496 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5497 | int ret = 0; |
| 5498 | int i; |
| 5499 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5500 | for (i = ctxt->attrBase; i < ctxt->attrNr; i++) { |
| 5501 | if (ctxt->attr[i].attr == NULL) |
| 5502 | break; |
| 5503 | if (ctxt->attr[i].state == XML_SCHEMAS_ATTR_UNKNOWN) { |
| 5504 | ret = 1; |
| 5505 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_ATTRUNKNOWN, "Attribute %s on %s is unknown\n", ctxt->attr[i].attr->name, node->name); |
| 5506 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5507 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5508 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5509 | } |
| 5510 | |
| 5511 | /** |
| 5512 | * xmlSchemaValidateSimpleContent: |
| 5513 | * @ctxt: a schema validation context |
| 5514 | * @elem: an element |
| 5515 | * @type: the type declaration |
| 5516 | * |
| 5517 | * Validate the content of an element expected to be a simple type |
| 5518 | * |
| 5519 | * Returns 0 if the element is schemas valid, a positive error code |
| 5520 | * number otherwise and -1 in case of internal or API error. |
| 5521 | */ |
| 5522 | static int |
| 5523 | xmlSchemaValidateSimpleContent(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5524 | xmlNodePtr node ATTRIBUTE_UNUSED) |
| 5525 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5526 | xmlNodePtr child; |
| 5527 | xmlSchemaTypePtr type, base; |
| 5528 | xmlChar *value; |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5529 | int ret = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5530 | |
| 5531 | child = ctxt->node; |
| 5532 | type = ctxt->type; |
| 5533 | |
| 5534 | /* |
| 5535 | * Validation Rule: Element Locally Valid (Type): 3.1.3 |
| 5536 | */ |
| 5537 | value = xmlNodeGetContent(child); |
| 5538 | /* xmlSchemaValidateSimpleValue(ctxt, type, value); */ |
| 5539 | switch (type->type) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5540 | case XML_SCHEMA_TYPE_RESTRICTION:{ |
| 5541 | xmlSchemaFacetPtr facet; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5542 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5543 | base = type->baseType; |
| 5544 | if (base != NULL) { |
| 5545 | ret = xmlSchemaValidateSimpleValue(ctxt, base, value); |
| 5546 | } else { |
| 5547 | TODO} |
| 5548 | if (ret == 0) { |
| 5549 | facet = type->facets; |
| 5550 | ret = |
| 5551 | xmlSchemaValidateFacets(ctxt, base, facet, value); |
| 5552 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 5553 | if ((ret == 0) && (type->attributes != NULL)) { |
| 5554 | ret = xmlSchemaValidateAttributes(ctxt, node, |
| 5555 | type->attributes); |
| 5556 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5557 | break; |
| 5558 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5559 | case XML_SCHEMA_TYPE_EXTENSION:{ |
| 5560 | TODO |
| 5561 | break; |
| 5562 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5563 | default: |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5564 | TODO |
| 5565 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5566 | if (value != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5567 | xmlFree(value); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5568 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5569 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5570 | } |
| 5571 | |
| 5572 | /** |
| 5573 | * xmlSchemaValidateCheckNodeList |
| 5574 | * @nodelist: the list of nodes |
| 5575 | * |
| 5576 | * Check the node list is only made of text nodes and entities pointing |
| 5577 | * to text nodes |
| 5578 | * |
| 5579 | * Returns 1 if true, 0 if false and -1 in case of error |
| 5580 | */ |
| 5581 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5582 | xmlSchemaValidateCheckNodeList(xmlNodePtr nodelist) |
| 5583 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5584 | while (nodelist != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5585 | if (nodelist->type == XML_ENTITY_REF_NODE) { |
| 5586 | TODO /* implement recursion in the entity content */ |
| 5587 | } |
| 5588 | if ((nodelist->type != XML_TEXT_NODE) && |
| 5589 | (nodelist->type != XML_COMMENT_NODE) && |
| 5590 | (nodelist->type != XML_PI_NODE) && |
| 5591 | (nodelist->type != XML_PI_NODE)) { |
| 5592 | return (0); |
| 5593 | } |
| 5594 | nodelist = nodelist->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5595 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5596 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5597 | } |
| 5598 | |
| 5599 | /** |
| 5600 | * xmlSchemaSkipIgnored: |
| 5601 | * @ctxt: a schema validation context |
| 5602 | * @type: the current type context |
| 5603 | * @node: the top node. |
| 5604 | * |
| 5605 | * Skip ignorable nodes in that context |
| 5606 | * |
| 5607 | * Returns the new sibling |
| 5608 | * number otherwise and -1 in case of internal or API error. |
| 5609 | */ |
| 5610 | static xmlNodePtr |
Daniel Veillard | dda8f1b | 2002-09-26 09:47:36 +0000 | [diff] [blame] | 5611 | xmlSchemaSkipIgnored(xmlSchemaValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5612 | xmlSchemaTypePtr type, xmlNodePtr node) |
| 5613 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5614 | int mixed = 0; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5615 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5616 | /* |
| 5617 | * TODO complete and handle entities |
| 5618 | */ |
| 5619 | mixed = ((type->contentType == XML_SCHEMA_CONTENT_MIXED) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5620 | (type->contentType == XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5621 | while ((node != NULL) && |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5622 | ((node->type == XML_COMMENT_NODE) || |
| 5623 | ((mixed == 1) && (node->type == XML_TEXT_NODE)) || |
| 5624 | (((type->contentType == XML_SCHEMA_CONTENT_ELEMENTS) && |
| 5625 | (node->type == XML_TEXT_NODE) && (IS_BLANK_NODE(node)))))) { |
| 5626 | node = node->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5627 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5628 | return (node); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5629 | } |
| 5630 | |
| 5631 | /** |
| 5632 | * xmlSchemaValidateCallback: |
| 5633 | * @ctxt: a schema validation context |
| 5634 | * @name: the name of the element detected (might be NULL) |
| 5635 | * @type: the type |
| 5636 | * |
| 5637 | * A transition has been made in the automata associated to an element |
| 5638 | * content model |
| 5639 | */ |
| 5640 | static void |
| 5641 | xmlSchemaValidateCallback(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5642 | const xmlChar * name ATTRIBUTE_UNUSED, |
| 5643 | xmlSchemaTypePtr type, xmlNodePtr node) |
| 5644 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5645 | xmlSchemaTypePtr oldtype = ctxt->type; |
| 5646 | xmlNodePtr oldnode = ctxt->node; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5647 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5648 | #ifdef DEBUG_CONTENT |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 5649 | xmlGenericError(xmlGenericErrorContext, |
| 5650 | "xmlSchemaValidateCallback: %s, %s, %s\n", |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5651 | name, type->name, node->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5652 | #endif |
| 5653 | ctxt->type = type; |
| 5654 | ctxt->node = node; |
| 5655 | xmlSchemaValidateContent(ctxt, node); |
| 5656 | ctxt->type = oldtype; |
| 5657 | ctxt->node = oldnode; |
| 5658 | } |
| 5659 | |
| 5660 | |
| 5661 | #if 0 |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5662 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5663 | /** |
| 5664 | * xmlSchemaValidateSimpleRestrictionType: |
| 5665 | * @ctxt: a schema validation context |
| 5666 | * @node: the top node. |
| 5667 | * |
| 5668 | * Validate the content of a restriction type. |
| 5669 | * |
| 5670 | * Returns 0 if the element is schemas valid, a positive error code |
| 5671 | * number otherwise and -1 in case of internal or API error. |
| 5672 | */ |
| 5673 | static int |
| 5674 | xmlSchemaValidateSimpleRestrictionType(xmlSchemaValidCtxtPtr ctxt, |
| 5675 | xmlNodePtr node) |
| 5676 | { |
| 5677 | xmlNodePtr child; |
| 5678 | xmlSchemaTypePtr type; |
| 5679 | int ret; |
| 5680 | |
| 5681 | child = ctxt->node; |
| 5682 | type = ctxt->type; |
| 5683 | |
| 5684 | if ((ctxt == NULL) || (type == NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5685 | 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] | 5686 | return (-1); |
| 5687 | } |
| 5688 | /* |
| 5689 | * Only text and text based entities references shall be found there |
| 5690 | */ |
| 5691 | ret = xmlSchemaValidateCheckNodeList(child); |
| 5692 | if (ret < 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5693 | 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] | 5694 | return (-1); |
| 5695 | } else if (ret == 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5696 | 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] | 5697 | return (-1); |
| 5698 | } |
| 5699 | ctxt->type = type->subtypes; |
| 5700 | xmlSchemaValidateContent(ctxt, node); |
| 5701 | ctxt->type = type; |
| 5702 | return (ret); |
| 5703 | } |
| 5704 | #endif |
| 5705 | |
| 5706 | /** |
| 5707 | * xmlSchemaValidateSimpleType: |
| 5708 | * @ctxt: a schema validation context |
| 5709 | * @node: the top node. |
| 5710 | * |
| 5711 | * Validate the content of an simple type. |
| 5712 | * |
| 5713 | * Returns 0 if the element is schemas valid, a positive error code |
| 5714 | * number otherwise and -1 in case of internal or API error. |
| 5715 | */ |
| 5716 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5717 | xmlSchemaValidateSimpleType(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5718 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5719 | xmlNodePtr child; |
| 5720 | xmlSchemaTypePtr type; |
| 5721 | xmlAttrPtr attr; |
| 5722 | int ret; |
| 5723 | |
| 5724 | child = ctxt->node; |
| 5725 | type = ctxt->type; |
| 5726 | |
| 5727 | if ((ctxt == NULL) || (type == NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5728 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INTERNAL, "Internal error: xmlSchemaValidateSimpleType %s\n", node->name, NULL); |
| 5729 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5730 | } |
| 5731 | /* |
| 5732 | * Only text and text based entities references shall be found there |
| 5733 | */ |
| 5734 | ret = xmlSchemaValidateCheckNodeList(child); |
| 5735 | if (ret < 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5736 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INTERNAL, "Internal error: xmlSchemaValidateSimpleType %s content\n", node->name, NULL); |
| 5737 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5738 | } else if (ret == 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5739 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_NOTSIMPLE, "Element %s content is not a simple type\n", node->name, NULL); |
| 5740 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5741 | } |
| 5742 | /* |
| 5743 | * Validation Rule: Element Locally Valid (Type): 3.1.1 |
| 5744 | */ |
| 5745 | attr = node->properties; |
| 5746 | while (attr != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5747 | if ((attr->ns == NULL) || |
| 5748 | (!xmlStrEqual(attr->ns->href, xmlSchemaInstanceNs)) || |
| 5749 | ((!xmlStrEqual(attr->name, BAD_CAST "type")) && |
| 5750 | (!xmlStrEqual(attr->name, BAD_CAST "nil")) && |
| 5751 | (!xmlStrEqual(attr->name, BAD_CAST "schemasLocation")) && |
| 5752 | (!xmlStrEqual |
| 5753 | (attr->name, BAD_CAST "noNamespaceSchemaLocation")))) { |
| 5754 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INVALIDATTR, "Element %s: attribute %s should not be present\n", node->name, attr->name); |
| 5755 | return (ctxt->err); |
| 5756 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5757 | } |
| 5758 | |
| 5759 | ctxt->type = type->subtypes; |
| 5760 | ret = xmlSchemaValidateSimpleContent(ctxt, node); |
| 5761 | ctxt->type = type; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5762 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5763 | } |
| 5764 | |
| 5765 | /** |
| 5766 | * xmlSchemaValidateElementType: |
| 5767 | * @ctxt: a schema validation context |
| 5768 | * @node: the top node. |
| 5769 | * |
| 5770 | * Validate the content of an element type. |
| 5771 | * Validation Rule: Element Locally Valid (Complex Type) |
| 5772 | * |
| 5773 | * Returns 0 if the element is schemas valid, a positive error code |
| 5774 | * number otherwise and -1 in case of internal or API error. |
| 5775 | */ |
| 5776 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5777 | xmlSchemaValidateElementType(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5778 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5779 | xmlNodePtr child; |
| 5780 | xmlSchemaTypePtr type; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5781 | xmlRegExecCtxtPtr oldregexp; /* cont model of the parent */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5782 | xmlSchemaElementPtr decl; |
| 5783 | int ret, attrBase; |
| 5784 | |
| 5785 | oldregexp = ctxt->regexp; |
| 5786 | |
| 5787 | child = ctxt->node; |
| 5788 | type = ctxt->type; |
| 5789 | |
| 5790 | if ((ctxt == NULL) || (type == NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5791 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INTERNAL, "Internal error: xmlSchemaValidateElementType\n", node->name, NULL); |
| 5792 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5793 | } |
| 5794 | if (child == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5795 | if (type->minOccurs > 0) { |
| 5796 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_MISSING, "Element %s: missing child %s\n", node->name, type->name); |
| 5797 | } |
| 5798 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5799 | } |
| 5800 | |
| 5801 | /* |
| 5802 | * Verify the element matches |
| 5803 | */ |
| 5804 | if (!xmlStrEqual(child->name, type->name)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5805 | xmlSchemaVErr3(ctxt, node, XML_SCHEMAS_ERR_WRONGELEM, "Element %s: missing child %s found %s\n", node->name, type->name, child->name); |
| 5806 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5807 | } |
| 5808 | /* |
| 5809 | * Verify the attributes |
| 5810 | */ |
| 5811 | attrBase = ctxt->attrBase; |
| 5812 | ctxt->attrBase = ctxt->attrNr; |
| 5813 | xmlSchemaRegisterAttributes(ctxt, child->properties); |
| 5814 | xmlSchemaValidateAttributes(ctxt, child, type->attributes); |
| 5815 | /* |
| 5816 | * Verify the element content recursively |
| 5817 | */ |
| 5818 | decl = (xmlSchemaElementPtr) type; |
| 5819 | oldregexp = ctxt->regexp; |
| 5820 | if (decl->contModel != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5821 | ctxt->regexp = xmlRegNewExecCtxt(decl->contModel, |
| 5822 | (xmlRegExecCallbacks) |
| 5823 | xmlSchemaValidateCallback, ctxt); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5824 | #ifdef DEBUG_AUTOMATA |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5825 | xmlGenericError(xmlGenericErrorContext, "====> %s\n", node->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5826 | #endif |
| 5827 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5828 | xmlSchemaValidateType(ctxt, child, (xmlSchemaElementPtr) type, |
| 5829 | type->subtypes); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5830 | |
| 5831 | if (decl->contModel != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5832 | ret = xmlRegExecPushString(ctxt->regexp, NULL, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5833 | #ifdef DEBUG_AUTOMATA |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5834 | xmlGenericError(xmlGenericErrorContext, |
| 5835 | "====> %s : %d\n", node->name, ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5836 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5837 | if (ret == 0) { |
| 5838 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_ELEMCONT, "Element %s content check failed\n", node->name, NULL); |
| 5839 | } else if (ret < 0) { |
| 5840 | 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] | 5841 | #ifdef DEBUG_CONTENT |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5842 | } else { |
| 5843 | xmlGenericError(xmlGenericErrorContext, |
| 5844 | "Element %s content check succeeded\n", |
| 5845 | node->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5846 | |
| 5847 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5848 | } |
| 5849 | xmlRegFreeExecCtxt(ctxt->regexp); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5850 | } |
| 5851 | /* |
| 5852 | * Verify that all attributes were Schemas-validated |
| 5853 | */ |
| 5854 | xmlSchemaCheckAttributes(ctxt, node); |
| 5855 | ctxt->attrNr = ctxt->attrBase; |
| 5856 | ctxt->attrBase = attrBase; |
| 5857 | |
| 5858 | ctxt->regexp = oldregexp; |
| 5859 | |
| 5860 | ctxt->node = child; |
| 5861 | ctxt->type = type; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5862 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5863 | } |
| 5864 | |
| 5865 | /** |
| 5866 | * xmlSchemaValidateBasicType: |
| 5867 | * @ctxt: a schema validation context |
| 5868 | * @node: the top node. |
| 5869 | * |
| 5870 | * Validate the content of an element expected to be a basic type type |
| 5871 | * |
| 5872 | * Returns 0 if the element is schemas valid, a positive error code |
| 5873 | * number otherwise and -1 in case of internal or API error. |
| 5874 | */ |
| 5875 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5876 | xmlSchemaValidateBasicType(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5877 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5878 | int ret; |
| 5879 | xmlNodePtr child, cur; |
| 5880 | xmlSchemaTypePtr type; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5881 | xmlChar *value; /* lexical representation */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5882 | |
| 5883 | child = ctxt->node; |
| 5884 | type = ctxt->type; |
| 5885 | |
| 5886 | if ((ctxt == NULL) || (type == NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5887 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INTERNAL, "Internal error: xmlSchemaValidateBasicType\n", node->name, NULL); |
| 5888 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5889 | } |
| 5890 | /* |
| 5891 | * First check the content model of the node. |
| 5892 | */ |
| 5893 | cur = child; |
| 5894 | while (cur != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5895 | switch (cur->type) { |
| 5896 | case XML_TEXT_NODE: |
| 5897 | case XML_CDATA_SECTION_NODE: |
| 5898 | case XML_PI_NODE: |
| 5899 | case XML_COMMENT_NODE: |
| 5900 | case XML_XINCLUDE_START: |
| 5901 | case XML_XINCLUDE_END: |
| 5902 | break; |
| 5903 | case XML_ENTITY_REF_NODE: |
| 5904 | case XML_ENTITY_NODE: |
| 5905 | TODO break; |
| 5906 | case XML_ELEMENT_NODE: |
| 5907 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INVALIDELEM, "Element %s: child %s should not be present\n", node->name, cur->name); |
| 5908 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5909 | case XML_ATTRIBUTE_NODE: |
| 5910 | case XML_DOCUMENT_NODE: |
| 5911 | case XML_DOCUMENT_TYPE_NODE: |
| 5912 | case XML_DOCUMENT_FRAG_NODE: |
| 5913 | case XML_NOTATION_NODE: |
| 5914 | case XML_HTML_DOCUMENT_NODE: |
| 5915 | case XML_DTD_NODE: |
| 5916 | case XML_ELEMENT_DECL: |
| 5917 | case XML_ATTRIBUTE_DECL: |
| 5918 | case XML_ENTITY_DECL: |
| 5919 | case XML_NAMESPACE_DECL: |
| 5920 | #ifdef LIBXML_DOCB_ENABLED |
| 5921 | case XML_DOCB_DOCUMENT_NODE: |
| 5922 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5923 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INVALIDELEM, "Element %s: node type of node unexpected here\n", node->name, NULL); |
| 5924 | return (ctxt->err); |
| 5925 | } |
| 5926 | cur = cur->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5927 | } |
| 5928 | if (child == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5929 | value = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5930 | else |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5931 | value = xmlNodeGetContent(child->parent); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5932 | |
| 5933 | if (ctxt->value != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5934 | xmlSchemaFreeValue(ctxt->value); |
| 5935 | ctxt->value = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5936 | } |
| 5937 | ret = xmlSchemaValidatePredefinedType(type, value, &(ctxt->value)); |
| 5938 | if (value != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5939 | xmlFree(value); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5940 | if (ret != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5941 | 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] | 5942 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5943 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5944 | } |
| 5945 | |
| 5946 | /** |
| 5947 | * xmlSchemaValidateComplexType: |
| 5948 | * @ctxt: a schema validation context |
| 5949 | * @node: the top node. |
| 5950 | * |
| 5951 | * Validate the content of an element expected to be a complex type type |
| 5952 | * xmlschema-1.html#cvc-complex-type |
| 5953 | * Validation Rule: Element Locally Valid (Complex Type) |
| 5954 | * |
| 5955 | * Returns 0 if the element is schemas valid, a positive error code |
| 5956 | * number otherwise and -1 in case of internal or API error. |
| 5957 | */ |
| 5958 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5959 | xmlSchemaValidateComplexType(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5960 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5961 | xmlNodePtr child; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 5962 | xmlSchemaTypePtr type, subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5963 | int ret; |
| 5964 | |
| 5965 | child = ctxt->node; |
| 5966 | type = ctxt->type; |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 5967 | ctxt->cur = node; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5968 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5969 | switch (type->contentType) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5970 | case XML_SCHEMA_CONTENT_EMPTY: |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5971 | if (type->baseType != NULL) { |
| 5972 | } else if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5973 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_NOTEMPTY, "Element %s is supposed to be empty\n", node->name, NULL); |
| 5974 | } |
| 5975 | if (type->attributes != NULL) { |
| 5976 | xmlSchemaValidateAttributes(ctxt, node, type->attributes); |
| 5977 | } |
| 5978 | subtype = type->subtypes; |
| 5979 | while (subtype != NULL) { |
| 5980 | ctxt->type = subtype; |
| 5981 | xmlSchemaValidateComplexType(ctxt, node); |
| 5982 | subtype = subtype->next; |
| 5983 | } |
| 5984 | break; |
| 5985 | case XML_SCHEMA_CONTENT_ELEMENTS: |
| 5986 | case XML_SCHEMA_CONTENT_MIXED: |
| 5987 | case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS: |
| 5988 | /* |
| 5989 | * Skip ignorable nodes in that context |
| 5990 | */ |
| 5991 | child = xmlSchemaSkipIgnored(ctxt, type, child); |
| 5992 | while (child != NULL) { |
| 5993 | if (child->type == XML_ELEMENT_NODE) { |
| 5994 | ret = xmlRegExecPushString(ctxt->regexp, |
| 5995 | child->name, child); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5996 | #ifdef DEBUG_AUTOMATA |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5997 | if (ret < 0) |
| 5998 | xmlGenericError(xmlGenericErrorContext, |
| 5999 | " --> %s Error\n", child->name); |
| 6000 | else |
| 6001 | xmlGenericError(xmlGenericErrorContext, |
| 6002 | " --> %s\n", child->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6003 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6004 | } |
| 6005 | child = child->next; |
| 6006 | /* |
| 6007 | * Skip ignorable nodes in that context |
| 6008 | */ |
| 6009 | child = xmlSchemaSkipIgnored(ctxt, type, child); |
| 6010 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 6011 | if (type->attributes != NULL) { |
| 6012 | xmlSchemaValidateAttributes(ctxt, node, type->attributes); |
| 6013 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6014 | break; |
| 6015 | case XML_SCHEMA_CONTENT_BASIC:{ |
| 6016 | if (type->subtypes != NULL) { |
| 6017 | ctxt->type = type->subtypes; |
| 6018 | xmlSchemaValidateComplexType(ctxt, node); |
| 6019 | } |
| 6020 | if (type->baseType != NULL) { |
| 6021 | ctxt->type = type->baseType; |
| 6022 | xmlSchemaValidateBasicType(ctxt, node); |
| 6023 | } |
| 6024 | if (type->attributes != NULL) { |
| 6025 | xmlSchemaValidateAttributes(ctxt, node, |
| 6026 | type->attributes); |
| 6027 | } |
| 6028 | ctxt->type = type; |
| 6029 | break; |
| 6030 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6031 | case XML_SCHEMA_CONTENT_SIMPLE:{ |
| 6032 | if (type->subtypes != NULL) { |
| 6033 | ctxt->type = type->subtypes; |
| 6034 | xmlSchemaValidateComplexType(ctxt, node); |
| 6035 | } |
| 6036 | if (type->baseType != NULL) { |
| 6037 | ctxt->type = type->baseType; |
| 6038 | xmlSchemaValidateComplexType(ctxt, node); |
| 6039 | } |
| 6040 | if (type->attributes != NULL) { |
| 6041 | xmlSchemaValidateAttributes(ctxt, node, |
| 6042 | type->attributes); |
| 6043 | } |
| 6044 | ctxt->type = type; |
| 6045 | break; |
| 6046 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6047 | default: |
| 6048 | TODO xmlGenericError(xmlGenericErrorContext, |
| 6049 | "unimplemented content type %d\n", |
| 6050 | type->contentType); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6051 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6052 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6053 | } |
| 6054 | |
| 6055 | /** |
| 6056 | * xmlSchemaValidateContent: |
| 6057 | * @ctxt: a schema validation context |
| 6058 | * @elem: an element |
| 6059 | * @type: the type declaration |
| 6060 | * |
| 6061 | * Validate the content of an element against the type. |
| 6062 | * |
| 6063 | * Returns 0 if the element is schemas valid, a positive error code |
| 6064 | * number otherwise and -1 in case of internal or API error. |
| 6065 | */ |
| 6066 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6067 | xmlSchemaValidateContent(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 6068 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6069 | xmlNodePtr child; |
| 6070 | xmlSchemaTypePtr type; |
| 6071 | |
| 6072 | child = ctxt->node; |
| 6073 | type = ctxt->type; |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 6074 | ctxt->cur = node; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6075 | |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 6076 | xmlSchemaValidateAttributes(ctxt, node, type->attributes); |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 6077 | ctxt->cur = node; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 6078 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6079 | switch (type->type) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6080 | case XML_SCHEMA_TYPE_ANY: |
| 6081 | /* Any type will do it, fine */ |
| 6082 | TODO /* handle recursivity */ |
| 6083 | break; |
| 6084 | case XML_SCHEMA_TYPE_COMPLEX: |
| 6085 | xmlSchemaValidateComplexType(ctxt, node); |
| 6086 | break; |
| 6087 | case XML_SCHEMA_TYPE_ELEMENT:{ |
| 6088 | xmlSchemaElementPtr decl = (xmlSchemaElementPtr) type; |
| 6089 | |
| 6090 | /* |
| 6091 | * Handle element reference here |
| 6092 | */ |
| 6093 | if (decl->ref != NULL) { |
| 6094 | if (decl->refDecl == NULL) { |
| 6095 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INTERNAL, "Internal error: element reference %s not resolved\n", decl->ref, NULL); |
| 6096 | return (-1); |
| 6097 | } |
| 6098 | ctxt->type = (xmlSchemaTypePtr) decl->refDecl; |
| 6099 | decl = decl->refDecl; |
| 6100 | } |
| 6101 | xmlSchemaValidateElementType(ctxt, node); |
| 6102 | ctxt->type = type; |
| 6103 | break; |
| 6104 | } |
| 6105 | case XML_SCHEMA_TYPE_BASIC: |
| 6106 | xmlSchemaValidateBasicType(ctxt, node); |
| 6107 | break; |
| 6108 | case XML_SCHEMA_TYPE_FACET: |
| 6109 | TODO break; |
| 6110 | case XML_SCHEMA_TYPE_SIMPLE: |
| 6111 | xmlSchemaValidateSimpleType(ctxt, node); |
| 6112 | break; |
| 6113 | case XML_SCHEMA_TYPE_SEQUENCE: |
| 6114 | TODO break; |
| 6115 | case XML_SCHEMA_TYPE_CHOICE: |
| 6116 | TODO break; |
| 6117 | case XML_SCHEMA_TYPE_ALL: |
| 6118 | TODO break; |
| 6119 | case XML_SCHEMA_TYPE_SIMPLE_CONTENT: |
| 6120 | TODO break; |
| 6121 | case XML_SCHEMA_TYPE_COMPLEX_CONTENT: |
| 6122 | TODO break; |
| 6123 | case XML_SCHEMA_TYPE_UR: |
| 6124 | TODO break; |
| 6125 | case XML_SCHEMA_TYPE_RESTRICTION: |
| 6126 | /*xmlSchemaValidateRestrictionType(ctxt, node); */ |
| 6127 | TODO break; |
| 6128 | case XML_SCHEMA_TYPE_EXTENSION: |
| 6129 | TODO break; |
| 6130 | case XML_SCHEMA_TYPE_ATTRIBUTE: |
| 6131 | TODO break; |
| 6132 | case XML_SCHEMA_TYPE_GROUP: |
| 6133 | TODO break; |
| 6134 | case XML_SCHEMA_TYPE_NOTATION: |
| 6135 | TODO break; |
| 6136 | case XML_SCHEMA_TYPE_LIST: |
| 6137 | TODO break; |
| 6138 | case XML_SCHEMA_TYPE_UNION: |
| 6139 | TODO break; |
| 6140 | case XML_SCHEMA_FACET_MININCLUSIVE: |
| 6141 | TODO break; |
| 6142 | case XML_SCHEMA_FACET_MINEXCLUSIVE: |
| 6143 | TODO break; |
| 6144 | case XML_SCHEMA_FACET_MAXINCLUSIVE: |
| 6145 | TODO break; |
| 6146 | case XML_SCHEMA_FACET_MAXEXCLUSIVE: |
| 6147 | TODO break; |
| 6148 | case XML_SCHEMA_FACET_TOTALDIGITS: |
| 6149 | TODO break; |
| 6150 | case XML_SCHEMA_FACET_FRACTIONDIGITS: |
| 6151 | TODO break; |
| 6152 | case XML_SCHEMA_FACET_PATTERN: |
| 6153 | TODO break; |
| 6154 | case XML_SCHEMA_FACET_ENUMERATION: |
| 6155 | TODO break; |
| 6156 | case XML_SCHEMA_FACET_WHITESPACE: |
| 6157 | TODO break; |
| 6158 | case XML_SCHEMA_FACET_LENGTH: |
| 6159 | TODO break; |
| 6160 | case XML_SCHEMA_FACET_MAXLENGTH: |
| 6161 | TODO break; |
| 6162 | case XML_SCHEMA_FACET_MINLENGTH: |
| 6163 | TODO break; |
| 6164 | case XML_SCHEMA_TYPE_ATTRIBUTEGROUP: |
| 6165 | TODO break; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6166 | } |
| 6167 | xmlSchemaValidateAttributes(ctxt, node, type->attributes); |
| 6168 | |
| 6169 | if (ctxt->node == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6170 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6171 | ctxt->node = ctxt->node->next; |
| 6172 | ctxt->type = type->next; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6173 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6174 | } |
| 6175 | |
| 6176 | /** |
| 6177 | * xmlSchemaValidateType: |
| 6178 | * @ctxt: a schema validation context |
| 6179 | * @elem: an element |
| 6180 | * @type: the list of type declarations |
| 6181 | * |
| 6182 | * Validate the content of an element against the types. |
| 6183 | * |
| 6184 | * Returns 0 if the element is schemas valid, a positive error code |
| 6185 | * number otherwise and -1 in case of internal or API error. |
| 6186 | */ |
| 6187 | static int |
| 6188 | xmlSchemaValidateType(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6189 | xmlSchemaElementPtr elemDecl, xmlSchemaTypePtr type) |
| 6190 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6191 | xmlChar *nil; |
| 6192 | |
Daniel Veillard | 2db8c12 | 2003-07-08 12:16:59 +0000 | [diff] [blame] | 6193 | if ((elem == NULL) || (type == NULL) || (elemDecl == NULL)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6194 | return (0); |
Daniel Veillard | 2db8c12 | 2003-07-08 12:16:59 +0000 | [diff] [blame] | 6195 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6196 | /* |
| 6197 | * 3.3.4 : 2 |
| 6198 | */ |
| 6199 | if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6200 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_ISABSTRACT, "Element %s is abstract\n", elem->name, NULL); |
| 6201 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6202 | } |
| 6203 | /* |
| 6204 | * 3.3.4: 3 |
| 6205 | */ |
| 6206 | nil = xmlGetNsProp(elem, BAD_CAST "nil", xmlSchemaInstanceNs); |
| 6207 | if (elemDecl->flags & XML_SCHEMAS_ELEM_NILLABLE) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6208 | /* 3.3.4: 3.2 */ |
| 6209 | if (xmlStrEqual(nil, BAD_CAST "true")) { |
| 6210 | if (elem->children != NULL) { |
| 6211 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_NOTEMPTY, "Element %s is not empty\n", elem->name, NULL); |
| 6212 | return (ctxt->err); |
| 6213 | } |
| 6214 | if ((elemDecl->flags & XML_SCHEMAS_ELEM_FIXED) && |
| 6215 | (elemDecl->value != NULL)) { |
| 6216 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_HAVEDEFAULT, "Empty element %s cannot get a fixed value\n", elem->name, NULL); |
| 6217 | return (ctxt->err); |
| 6218 | } |
| 6219 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6220 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6221 | /* 3.3.4: 3.1 */ |
| 6222 | if (nil != NULL) { |
| 6223 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_NOTNILLABLE, "Element %s with xs:nil but not nillable\n", elem->name, NULL); |
| 6224 | xmlFree(nil); |
| 6225 | return (ctxt->err); |
| 6226 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6227 | } |
| 6228 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6229 | /* TODO 3.3.4: 4 if the element carries xs:type */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6230 | |
| 6231 | ctxt->type = elemDecl->subtypes; |
| 6232 | ctxt->node = elem->children; |
| 6233 | xmlSchemaValidateContent(ctxt, elem); |
| 6234 | xmlSchemaValidateAttributes(ctxt, elem, elemDecl->attributes); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6235 | |
| 6236 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6237 | } |
| 6238 | |
| 6239 | |
| 6240 | /** |
| 6241 | * xmlSchemaValidateAttributes: |
| 6242 | * @ctxt: a schema validation context |
| 6243 | * @elem: an element |
| 6244 | * @attributes: the list of attribute declarations |
| 6245 | * |
| 6246 | * Validate the attributes of an element. |
| 6247 | * |
| 6248 | * Returns 0 if the element is schemas valid, a positive error code |
| 6249 | * number otherwise and -1 in case of internal or API error. |
| 6250 | */ |
| 6251 | static int |
| 6252 | xmlSchemaValidateAttributes(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6253 | xmlSchemaAttributePtr attributes) |
| 6254 | { |
William M. Brack | 87640d5 | 2004-04-17 14:58:15 +0000 | [diff] [blame] | 6255 | int i, ret; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6256 | xmlAttrPtr attr; |
| 6257 | xmlChar *value; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 6258 | xmlSchemaAttributeGroupPtr group = NULL; |
Daniel Veillard | c85d0fe | 2004-04-16 16:46:51 +0000 | [diff] [blame] | 6259 | int found; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6260 | |
| 6261 | if (attributes == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6262 | return (0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6263 | while (attributes != NULL) { |
Daniel Veillard | c85d0fe | 2004-04-16 16:46:51 +0000 | [diff] [blame] | 6264 | found = 0; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6265 | /* |
| 6266 | * Handle attribute groups |
| 6267 | */ |
| 6268 | if (attributes->type == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) { |
| 6269 | group = (xmlSchemaAttributeGroupPtr) attributes; |
| 6270 | xmlSchemaValidateAttributes(ctxt, elem, group->attributes); |
| 6271 | attributes = group->next; |
| 6272 | continue; |
| 6273 | } |
| 6274 | for (i = ctxt->attrBase; i < ctxt->attrNr; i++) { |
| 6275 | attr = ctxt->attr[i].attr; |
| 6276 | if (attr == NULL) |
| 6277 | continue; |
| 6278 | if (attributes->ref != NULL) { |
| 6279 | if (!xmlStrEqual(attr->name, attributes->ref)) |
| 6280 | continue; |
| 6281 | if (attr->ns != NULL) { |
| 6282 | if ((attributes->refNs == NULL) || |
| 6283 | (!xmlStrEqual(attr->ns->href, attributes->refNs))) |
| 6284 | continue; |
| 6285 | } else if (attributes->refNs != NULL) { |
| 6286 | continue; |
| 6287 | } |
| 6288 | } else { |
| 6289 | if (!xmlStrEqual(attr->name, attributes->name)) |
| 6290 | continue; |
| 6291 | /* |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6292 | * handle the namespaces checks here |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6293 | */ |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6294 | if (attr->ns == NULL) { |
| 6295 | /* |
| 6296 | * accept an unqualified attribute only if the declaration |
| 6297 | * is unqualified or if the schemas allowed it. |
| 6298 | */ |
| 6299 | if ((attributes->targetNamespace != NULL) && |
| 6300 | ((attributes->flags & XML_SCHEMAS_ATTR_NSDEFAULT) == 0)) |
| 6301 | continue; |
| 6302 | } else { |
| 6303 | if (attributes->targetNamespace == NULL) |
| 6304 | continue; |
| 6305 | if (!xmlStrEqual(attributes->targetNamespace, |
| 6306 | attr->ns->href)) |
| 6307 | continue; |
| 6308 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6309 | } |
Daniel Veillard | c85d0fe | 2004-04-16 16:46:51 +0000 | [diff] [blame] | 6310 | found = 1; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6311 | ctxt->cur = (xmlNodePtr) attributes; |
Daniel Veillard | c85d0fe | 2004-04-16 16:46:51 +0000 | [diff] [blame] | 6312 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6313 | if (attributes->subtypes == NULL) { |
| 6314 | xmlSchemaVErr(ctxt, (xmlNodePtr) attr, XML_SCHEMAS_ERR_INTERNAL, "Internal error: attribute %s type not resolved\n", attr->name, NULL); |
| 6315 | continue; |
| 6316 | } |
Daniel Veillard | c85d0fe | 2004-04-16 16:46:51 +0000 | [diff] [blame] | 6317 | |
| 6318 | if (attributes->occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED) { |
| 6319 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_INVALIDATTR, "attribute %s on %s is prohibited\n", attributes->name, elem->name); |
| 6320 | /* Setting the state to XML_SCHEMAS_ATTR_CHECKED seems not very logical but it |
| 6321 | surpresses the "attribute is unknown" error report. Please change this if you know better */ |
| 6322 | ctxt->attr[i].state = XML_SCHEMAS_ATTR_CHECKED; |
| 6323 | break; |
| 6324 | } |
| 6325 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6326 | value = xmlNodeListGetString(elem->doc, attr->children, 1); |
| 6327 | ret = xmlSchemaValidateSimpleValue(ctxt, attributes->subtypes, |
| 6328 | value); |
| 6329 | if (ret != 0) { |
| 6330 | xmlSchemaVErr(ctxt, (xmlNodePtr) attr, XML_SCHEMAS_ERR_ATTRINVALID, "attribute %s on %s does not match type\n", attr->name, elem->name); |
| 6331 | } else { |
| 6332 | ctxt->attr[i].state = XML_SCHEMAS_ATTR_CHECKED; |
| 6333 | } |
| 6334 | if (value != NULL) { |
| 6335 | xmlFree(value); |
| 6336 | } |
| 6337 | } |
Daniel Veillard | c85d0fe | 2004-04-16 16:46:51 +0000 | [diff] [blame] | 6338 | if ((!found) && (attributes->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED)) { |
| 6339 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_MISSING, "required attribute %s on %s is missing\n", attributes->name, elem->name); |
| 6340 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6341 | attributes = attributes->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6342 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6343 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6344 | } |
| 6345 | |
| 6346 | /** |
| 6347 | * xmlSchemaValidateElement: |
| 6348 | * @ctxt: a schema validation context |
| 6349 | * @elem: an element |
| 6350 | * |
| 6351 | * Validate an element in a tree |
| 6352 | * |
| 6353 | * Returns 0 if the element is schemas valid, a positive error code |
| 6354 | * number otherwise and -1 in case of internal or API error. |
| 6355 | */ |
| 6356 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6357 | xmlSchemaValidateElement(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem) |
| 6358 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6359 | xmlSchemaElementPtr elemDecl; |
| 6360 | int ret, attrBase; |
| 6361 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6362 | if (elem->ns != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6363 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6364 | elem->name, elem->ns->href, NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6365 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6366 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6367 | elem->name, NULL, NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6368 | } |
| 6369 | /* |
| 6370 | * special case whe elementFormDefault is unqualified for top-level elem. |
| 6371 | */ |
| 6372 | if ((elemDecl == NULL) && (elem->ns != NULL) && |
| 6373 | (elem->parent != NULL) && (elem->parent->type != XML_ELEMENT_NODE) && |
| 6374 | (xmlStrEqual(ctxt->schema->targetNamespace, elem->ns->href)) && |
| 6375 | ((ctxt->schema->flags & XML_SCHEMAS_QUALIF_ELEM) == 0)) { |
| 6376 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6377 | elem->name, NULL, NULL); |
| 6378 | } |
| 6379 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6380 | /* |
| 6381 | * 3.3.4 : 1 |
| 6382 | */ |
| 6383 | if (elemDecl == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6384 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_UNDECLAREDELEM, "Element %s not declared\n", elem->name, NULL); |
| 6385 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6386 | } |
| 6387 | if (elemDecl->subtypes == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6388 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_NOTYPE, "Element %s has no type\n", elem->name, NULL); |
| 6389 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6390 | } |
| 6391 | /* |
| 6392 | * Verify the attributes |
| 6393 | */ |
| 6394 | attrBase = ctxt->attrBase; |
| 6395 | ctxt->attrBase = ctxt->attrNr; |
| 6396 | xmlSchemaRegisterAttributes(ctxt, elem->properties); |
| 6397 | xmlSchemaValidateAttributes(ctxt, elem, elemDecl->attributes); |
| 6398 | /* |
| 6399 | * Verify the element content recursively |
| 6400 | */ |
| 6401 | if (elemDecl->contModel != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6402 | ctxt->regexp = xmlRegNewExecCtxt(elemDecl->contModel, |
| 6403 | (xmlRegExecCallbacks) |
| 6404 | xmlSchemaValidateCallback, ctxt); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6405 | #ifdef DEBUG_AUTOMATA |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6406 | xmlGenericError(xmlGenericErrorContext, "====> %s\n", elem->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6407 | #endif |
| 6408 | } |
| 6409 | xmlSchemaValidateType(ctxt, elem, elemDecl, elemDecl->subtypes); |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 6410 | if (elemDecl->contModel != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6411 | ret = xmlRegExecPushString(ctxt->regexp, NULL, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6412 | #ifdef DEBUG_AUTOMATA |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6413 | xmlGenericError(xmlGenericErrorContext, |
| 6414 | "====> %s : %d\n", elem->name, ret); |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 6415 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6416 | if (ret == 0) { |
| 6417 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_ELEMCONT, "Element %s content check failed\n", elem->name, NULL); |
| 6418 | } else if (ret < 0) { |
| 6419 | 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] | 6420 | #ifdef DEBUG_CONTENT |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6421 | } else { |
| 6422 | xmlGenericError(xmlGenericErrorContext, |
| 6423 | "Element %s content check succeeded\n", |
| 6424 | elem->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6425 | |
| 6426 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6427 | } |
| 6428 | xmlRegFreeExecCtxt(ctxt->regexp); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6429 | } |
| 6430 | /* |
| 6431 | * Verify that all attributes were Schemas-validated |
| 6432 | */ |
| 6433 | xmlSchemaCheckAttributes(ctxt, elem); |
| 6434 | ctxt->attrNr = ctxt->attrBase; |
| 6435 | ctxt->attrBase = attrBase; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6436 | |
| 6437 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6438 | } |
| 6439 | |
| 6440 | /** |
| 6441 | * xmlSchemaValidateDocument: |
| 6442 | * @ctxt: a schema validation context |
| 6443 | * @doc: a parsed document tree |
| 6444 | * |
| 6445 | * Validate a document tree in memory. |
| 6446 | * |
| 6447 | * Returns 0 if the document is schemas valid, a positive error code |
| 6448 | * number otherwise and -1 in case of internal or API error. |
| 6449 | */ |
| 6450 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6451 | xmlSchemaValidateDocument(xmlSchemaValidCtxtPtr ctxt, xmlDocPtr doc) |
| 6452 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6453 | xmlNodePtr root; |
| 6454 | xmlSchemaElementPtr elemDecl; |
| 6455 | |
| 6456 | root = xmlDocGetRootElement(doc); |
| 6457 | if (root == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6458 | xmlSchemaVErr(ctxt, (xmlNodePtr) doc, XML_SCHEMAS_ERR_NOROOT, "document has no root\n", NULL, NULL); |
| 6459 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6460 | } |
| 6461 | if (root->ns != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6462 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6463 | root->name, root->ns->href, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6464 | else |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6465 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6466 | root->name, NULL, NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6467 | /* |
| 6468 | * special case whe elementFormDefault is unqualified for top-level elem. |
| 6469 | */ |
| 6470 | if ((elemDecl == NULL) && (root->ns != NULL) && |
| 6471 | (xmlStrEqual(ctxt->schema->targetNamespace, root->ns->href)) && |
| 6472 | ((ctxt->schema->flags & XML_SCHEMAS_QUALIF_ELEM) == 0)) { |
| 6473 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6474 | root->name, NULL, NULL); |
| 6475 | } |
| 6476 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6477 | if (elemDecl == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6478 | 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] | 6479 | } else if ((elemDecl->flags & XML_SCHEMAS_ELEM_TOPLEVEL) == 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6480 | 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] | 6481 | } |
| 6482 | /* |
| 6483 | * Okay, start the recursive validation |
| 6484 | */ |
| 6485 | xmlSchemaValidateElement(ctxt, root); |
| 6486 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6487 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6488 | } |
| 6489 | |
| 6490 | /************************************************************************ |
| 6491 | * * |
| 6492 | * SAX Validation code * |
| 6493 | * * |
| 6494 | ************************************************************************/ |
| 6495 | |
| 6496 | /************************************************************************ |
| 6497 | * * |
| 6498 | * Validation interfaces * |
| 6499 | * * |
| 6500 | ************************************************************************/ |
| 6501 | |
| 6502 | /** |
| 6503 | * xmlSchemaNewValidCtxt: |
| 6504 | * @schema: a precompiled XML Schemas |
| 6505 | * |
| 6506 | * Create an XML Schemas validation context based on the given schema |
| 6507 | * |
| 6508 | * Returns the validation context or NULL in case of error |
| 6509 | */ |
| 6510 | xmlSchemaValidCtxtPtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6511 | xmlSchemaNewValidCtxt(xmlSchemaPtr schema) |
| 6512 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6513 | xmlSchemaValidCtxtPtr ret; |
| 6514 | |
| 6515 | ret = (xmlSchemaValidCtxtPtr) xmlMalloc(sizeof(xmlSchemaValidCtxt)); |
| 6516 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6517 | xmlSchemaVErrMemory(NULL, "allocating validation context", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6518 | return (NULL); |
| 6519 | } |
| 6520 | memset(ret, 0, sizeof(xmlSchemaValidCtxt)); |
| 6521 | ret->schema = schema; |
| 6522 | ret->attrNr = 0; |
| 6523 | ret->attrMax = 10; |
| 6524 | ret->attr = (xmlSchemaAttrStatePtr) xmlMalloc(ret->attrMax * |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6525 | sizeof |
| 6526 | (xmlSchemaAttrState)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6527 | if (ret->attr == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6528 | xmlSchemaVErrMemory(NULL, "allocating validation context", NULL); |
| 6529 | free(ret); |
| 6530 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6531 | } |
| 6532 | memset(ret->attr, 0, ret->attrMax * sizeof(xmlSchemaAttrState)); |
| 6533 | return (ret); |
| 6534 | } |
| 6535 | |
| 6536 | /** |
| 6537 | * xmlSchemaFreeValidCtxt: |
| 6538 | * @ctxt: the schema validation context |
| 6539 | * |
| 6540 | * Free the resources associated to the schema validation context |
| 6541 | */ |
| 6542 | void |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6543 | xmlSchemaFreeValidCtxt(xmlSchemaValidCtxtPtr ctxt) |
| 6544 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6545 | if (ctxt == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6546 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6547 | if (ctxt->attr != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6548 | xmlFree(ctxt->attr); |
Daniel Veillard | 88c5891 | 2002-04-23 07:12:20 +0000 | [diff] [blame] | 6549 | if (ctxt->value != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6550 | xmlSchemaFreeValue(ctxt->value); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6551 | xmlFree(ctxt); |
| 6552 | } |
| 6553 | |
| 6554 | /** |
| 6555 | * xmlSchemaSetValidErrors: |
| 6556 | * @ctxt: a schema validation context |
| 6557 | * @err: the error function |
| 6558 | * @warn: the warning function |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 6559 | * @ctx: the functions context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6560 | * |
| 6561 | * Set the error and warning callback informations |
| 6562 | */ |
| 6563 | void |
| 6564 | xmlSchemaSetValidErrors(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6565 | xmlSchemaValidityErrorFunc err, |
| 6566 | xmlSchemaValidityWarningFunc warn, void *ctx) |
| 6567 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6568 | if (ctxt == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6569 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6570 | ctxt->error = err; |
| 6571 | ctxt->warning = warn; |
| 6572 | ctxt->userData = ctx; |
| 6573 | } |
| 6574 | |
| 6575 | /** |
| 6576 | * xmlSchemaValidateDoc: |
| 6577 | * @ctxt: a schema validation context |
| 6578 | * @doc: a parsed document tree |
| 6579 | * |
| 6580 | * Validate a document tree in memory. |
| 6581 | * |
| 6582 | * Returns 0 if the document is schemas valid, a positive error code |
| 6583 | * number otherwise and -1 in case of internal or API error. |
| 6584 | */ |
| 6585 | int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6586 | xmlSchemaValidateDoc(xmlSchemaValidCtxtPtr ctxt, xmlDocPtr doc) |
| 6587 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6588 | int ret; |
| 6589 | |
| 6590 | if ((ctxt == NULL) || (doc == NULL)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6591 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6592 | |
| 6593 | ctxt->doc = doc; |
| 6594 | ret = xmlSchemaValidateDocument(ctxt, doc); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6595 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6596 | } |
| 6597 | |
| 6598 | /** |
| 6599 | * xmlSchemaValidateStream: |
| 6600 | * @ctxt: a schema validation context |
| 6601 | * @input: the input to use for reading the data |
| 6602 | * @enc: an optional encoding information |
| 6603 | * @sax: a SAX handler for the resulting events |
| 6604 | * @user_data: the context to provide to the SAX handler. |
| 6605 | * |
| 6606 | * Validate a document tree in memory. |
| 6607 | * |
| 6608 | * Returns 0 if the document is schemas valid, a positive error code |
| 6609 | * number otherwise and -1 in case of internal or API error. |
| 6610 | */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6611 | int |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6612 | xmlSchemaValidateStream(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6613 | xmlParserInputBufferPtr input, xmlCharEncoding enc, |
| 6614 | xmlSAXHandlerPtr sax, void *user_data) |
| 6615 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6616 | if ((ctxt == NULL) || (input == NULL)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6617 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6618 | ctxt->input = input; |
| 6619 | ctxt->enc = enc; |
| 6620 | ctxt->sax = sax; |
| 6621 | ctxt->user_data = user_data; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6622 | TODO return (0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6623 | } |
| 6624 | |
| 6625 | #endif /* LIBXML_SCHEMAS_ENABLED */ |