Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * schemas.c : implementation of the XML Schema handling and |
| 3 | * schema validity checking |
| 4 | * |
| 5 | * See Copyright for the status of this software. |
| 6 | * |
| 7 | * Daniel Veillard <veillard@redhat.com> |
| 8 | */ |
| 9 | |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 10 | /* |
| 11 | * TODO: |
| 12 | * - when types are redefined in includes, check that all |
| 13 | * types in the redef list are equal |
| 14 | * -> need a type equality operation. |
| 15 | */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 16 | #define IN_LIBXML |
| 17 | #include "libxml.h" |
| 18 | |
| 19 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <libxml/xmlmemory.h> |
| 23 | #include <libxml/parser.h> |
| 24 | #include <libxml/parserInternals.h> |
| 25 | #include <libxml/hash.h> |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 26 | #include <libxml/uri.h> |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 27 | |
| 28 | #include <libxml/xmlschemas.h> |
| 29 | #include <libxml/schemasInternals.h> |
| 30 | #include <libxml/xmlschemastypes.h> |
| 31 | #include <libxml/xmlautomata.h> |
| 32 | #include <libxml/xmlregexp.h> |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 33 | #include <libxml/dict.h> |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 34 | |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 35 | /* #define DEBUG 1 */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 36 | |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 37 | /* #define DEBUG_CONTENT 1 */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 38 | |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 39 | /* #define DEBUG_TYPE 1 */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 40 | |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 41 | /* #define DEBUG_CONTENT_REGEXP 1 */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 42 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 43 | /* #define DEBUG_AUTOMATA 1 */ |
| 44 | |
| 45 | #define UNBOUNDED (1 << 30) |
| 46 | #define TODO \ |
| 47 | xmlGenericError(xmlGenericErrorContext, \ |
| 48 | "Unimplemented block at %s:%d\n", \ |
| 49 | __FILE__, __LINE__); |
| 50 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 51 | #define XML_SCHEMAS_DEFAULT_NAMESPACE (const xmlChar *)"the default namespace" |
| 52 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 53 | /* |
| 54 | * The XML Schemas namespaces |
| 55 | */ |
| 56 | static const xmlChar *xmlSchemaNs = (const xmlChar *) |
| 57 | "http://www.w3.org/2001/XMLSchema"; |
| 58 | |
| 59 | static const xmlChar *xmlSchemaInstanceNs = (const xmlChar *) |
| 60 | "http://www.w3.org/2001/XMLSchema-instance"; |
| 61 | |
| 62 | #define IS_SCHEMA(node, type) \ |
| 63 | ((node != NULL) && (node->ns != NULL) && \ |
| 64 | (xmlStrEqual(node->name, (const xmlChar *) type)) && \ |
| 65 | (xmlStrEqual(node->ns->href, xmlSchemaNs))) |
| 66 | |
| 67 | #define XML_SCHEMAS_PARSE_ERROR 1 |
| 68 | |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 69 | #define SCHEMAS_PARSE_OPTIONS XML_PARSE_NOENT |
| 70 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 71 | struct _xmlSchemaParserCtxt { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 72 | void *userData; /* user specific data block */ |
| 73 | xmlSchemaValidityErrorFunc error; /* the callback in case of errors */ |
| 74 | xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */ |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 75 | xmlSchemaValidError err; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 76 | int nberrors; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 77 | xmlStructuredErrorFunc serror; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 78 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 79 | xmlSchemaPtr topschema; /* The main schema */ |
| 80 | xmlHashTablePtr namespaces; /* Hash table of namespaces to schemas */ |
| 81 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 82 | xmlSchemaPtr schema; /* The schema in use */ |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 83 | const xmlChar *container; /* the current element, group, ... */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 84 | int counter; |
| 85 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 86 | const xmlChar *URL; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 87 | xmlDocPtr doc; |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 88 | int preserve; /* Whether the doc should be freed */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 89 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 90 | const char *buffer; |
| 91 | int size; |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 92 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 93 | /* |
| 94 | * Used to build complex element content models |
| 95 | */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 96 | xmlAutomataPtr am; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 97 | xmlAutomataStatePtr start; |
| 98 | xmlAutomataStatePtr end; |
| 99 | xmlAutomataStatePtr state; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 100 | |
| 101 | xmlDictPtr dict; /* dictionnary for interned string names */ |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 102 | int includes; /* the inclusion level, 0 for root or imports */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | |
| 106 | #define XML_SCHEMAS_ATTR_UNKNOWN 1 |
| 107 | #define XML_SCHEMAS_ATTR_CHECKED 2 |
| 108 | |
| 109 | typedef struct _xmlSchemaAttrState xmlSchemaAttrState; |
| 110 | typedef xmlSchemaAttrState *xmlSchemaAttrStatePtr; |
| 111 | struct _xmlSchemaAttrState { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 112 | xmlAttrPtr attr; |
| 113 | int state; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | /** |
| 117 | * xmlSchemaValidCtxt: |
| 118 | * |
| 119 | * A Schemas validation context |
| 120 | */ |
| 121 | |
| 122 | struct _xmlSchemaValidCtxt { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 123 | void *userData; /* user specific data block */ |
| 124 | xmlSchemaValidityErrorFunc error; /* the callback in case of errors */ |
| 125 | xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */ |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 126 | xmlStructuredErrorFunc serror; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 127 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 128 | xmlSchemaPtr schema; /* The schema in use */ |
| 129 | xmlDocPtr doc; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 130 | xmlParserInputBufferPtr input; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 131 | xmlCharEncoding enc; |
| 132 | xmlSAXHandlerPtr sax; |
| 133 | void *user_data; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 134 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 135 | xmlDocPtr myDoc; |
| 136 | int err; |
| 137 | int nberrors; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 138 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 139 | xmlNodePtr node; |
| 140 | xmlNodePtr cur; |
| 141 | xmlSchemaTypePtr type; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 142 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 143 | xmlRegExecCtxtPtr regexp; |
| 144 | xmlSchemaValPtr value; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 145 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 146 | int attrNr; |
| 147 | int attrBase; |
| 148 | int attrMax; |
| 149 | xmlSchemaAttrStatePtr attr; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 152 | /* |
| 153 | * These are the entries in the schemas importSchemas hash table |
| 154 | */ |
| 155 | typedef struct _xmlSchemaImport xmlSchemaImport; |
| 156 | typedef xmlSchemaImport *xmlSchemaImportPtr; |
| 157 | struct _xmlSchemaImport { |
| 158 | const xmlChar *schemaLocation; |
| 159 | xmlSchemaPtr schema; |
| 160 | }; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 161 | |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 162 | /* |
| 163 | * These are the entries associated to includes in a schemas |
| 164 | */ |
| 165 | typedef struct _xmlSchemaInclude xmlSchemaInclude; |
| 166 | typedef xmlSchemaInclude *xmlSchemaIncludePtr; |
| 167 | struct _xmlSchemaInclude { |
| 168 | xmlSchemaIncludePtr next; |
| 169 | |
| 170 | const xmlChar *schemaLocation; |
| 171 | xmlDocPtr doc; |
| 172 | }; |
| 173 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 174 | /************************************************************************ |
| 175 | * * |
| 176 | * Some predeclarations * |
| 177 | * * |
| 178 | ************************************************************************/ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 179 | static int xmlSchemaValidateSimpleValue(xmlSchemaValidCtxtPtr ctxt, |
| 180 | xmlSchemaTypePtr type, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 181 | const xmlChar * value); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 182 | |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 183 | static int xmlSchemaParseInclude(xmlSchemaParserCtxtPtr ctxt, |
| 184 | xmlSchemaPtr schema, |
| 185 | xmlNodePtr node); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 186 | /************************************************************************ |
| 187 | * * |
| 188 | * Datatype error handlers * |
| 189 | * * |
| 190 | ************************************************************************/ |
| 191 | |
| 192 | /** |
| 193 | * xmlSchemaPErrMemory: |
| 194 | * @node: a context node |
| 195 | * @extra: extra informations |
| 196 | * |
| 197 | * Handle an out of memory condition |
| 198 | */ |
| 199 | static void |
| 200 | xmlSchemaPErrMemory(xmlSchemaParserCtxtPtr ctxt, |
| 201 | const char *extra, xmlNodePtr node) |
| 202 | { |
| 203 | if (ctxt != NULL) |
| 204 | ctxt->nberrors++; |
| 205 | __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, node, NULL, |
| 206 | extra); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * xmlSchemaPErr: |
| 211 | * @ctxt: the parsing context |
| 212 | * @node: the context node |
| 213 | * @error: the error code |
| 214 | * @msg: the error message |
| 215 | * @str1: extra data |
| 216 | * @str2: extra data |
| 217 | * |
| 218 | * Handle a parser error |
| 219 | */ |
| 220 | static void |
| 221 | xmlSchemaPErr(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error, |
| 222 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 223 | { |
| 224 | xmlGenericErrorFunc channel = NULL; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 225 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 226 | void *data = NULL; |
| 227 | |
| 228 | if (ctxt != NULL) { |
| 229 | ctxt->nberrors++; |
| 230 | channel = ctxt->error; |
| 231 | data = ctxt->userData; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 232 | schannel = ctxt->serror; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 233 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 234 | __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 235 | error, XML_ERR_ERROR, NULL, 0, |
| 236 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 237 | msg, str1, str2); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * xmlSchemaPErr2: |
| 242 | * @ctxt: the parsing context |
| 243 | * @node: the context node |
| 244 | * @node: the current child |
| 245 | * @error: the error code |
| 246 | * @msg: the error message |
| 247 | * @str1: extra data |
| 248 | * @str2: extra data |
| 249 | * |
| 250 | * Handle a parser error |
| 251 | */ |
| 252 | static void |
| 253 | xmlSchemaPErr2(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, |
| 254 | xmlNodePtr child, int error, |
| 255 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 256 | { |
| 257 | if (child != NULL) |
| 258 | xmlSchemaPErr(ctxt, child, error, msg, str1, str2); |
| 259 | else |
| 260 | xmlSchemaPErr(ctxt, node, error, msg, str1, str2); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * xmlSchemaVTypeErrMemory: |
| 265 | * @node: a context node |
| 266 | * @extra: extra informations |
| 267 | * |
| 268 | * Handle an out of memory condition |
| 269 | */ |
| 270 | static void |
| 271 | xmlSchemaVErrMemory(xmlSchemaValidCtxtPtr ctxt, |
| 272 | const char *extra, xmlNodePtr node) |
| 273 | { |
| 274 | if (ctxt != NULL) { |
| 275 | ctxt->nberrors++; |
| 276 | ctxt->err = XML_SCHEMAS_ERR_INTERNAL; |
| 277 | } |
| 278 | __xmlSimpleError(XML_FROM_SCHEMASV, XML_ERR_NO_MEMORY, node, NULL, |
| 279 | extra); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * xmlSchemaVErr3: |
| 284 | * @ctxt: the validation context |
| 285 | * @node: the context node |
| 286 | * @error: the error code |
| 287 | * @msg: the error message |
| 288 | * @str1: extra data |
| 289 | * @str2: extra data |
| 290 | * @str3: extra data |
| 291 | * |
| 292 | * Handle a validation error |
| 293 | */ |
| 294 | static void |
| 295 | xmlSchemaVErr3(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node, int error, |
| 296 | const char *msg, const xmlChar *str1, const xmlChar *str2, |
| 297 | const xmlChar *str3) |
| 298 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 299 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 300 | xmlGenericErrorFunc channel = NULL; |
| 301 | void *data = NULL; |
| 302 | |
| 303 | if (ctxt != NULL) { |
| 304 | ctxt->nberrors++; |
| 305 | ctxt->err = error; |
| 306 | channel = ctxt->error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 307 | schannel = ctxt->serror; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 308 | data = ctxt->userData; |
| 309 | } |
| 310 | /* reajust to global error numbers */ |
| 311 | error += XML_SCHEMAV_NOROOT - XML_SCHEMAS_ERR_NOROOT; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 312 | __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASV, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 313 | error, XML_ERR_ERROR, NULL, 0, |
| 314 | (const char *) str1, (const char *) str2, |
| 315 | (const char *) str3, 0, 0, |
| 316 | msg, str1, str2, str3); |
| 317 | } |
| 318 | /** |
| 319 | * xmlSchemaVErr: |
| 320 | * @ctxt: the validation context |
| 321 | * @node: the context node |
| 322 | * @error: the error code |
| 323 | * @msg: the error message |
| 324 | * @str1: extra data |
| 325 | * @str2: extra data |
| 326 | * |
| 327 | * Handle a validation error |
| 328 | */ |
| 329 | static void |
| 330 | xmlSchemaVErr(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node, int error, |
| 331 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 332 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 333 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 334 | xmlGenericErrorFunc channel = NULL; |
| 335 | void *data = NULL; |
| 336 | |
| 337 | if (ctxt != NULL) { |
| 338 | ctxt->nberrors++; |
| 339 | ctxt->err = error; |
| 340 | channel = ctxt->error; |
| 341 | data = ctxt->userData; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 342 | schannel = ctxt->serror; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 343 | } |
| 344 | /* reajust to global error numbers */ |
| 345 | error += XML_SCHEMAV_NOROOT - XML_SCHEMAS_ERR_NOROOT; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 346 | __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASV, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 347 | error, XML_ERR_ERROR, NULL, 0, |
| 348 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 349 | msg, str1, str2); |
| 350 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 351 | |
| 352 | /************************************************************************ |
| 353 | * * |
| 354 | * Allocation functions * |
| 355 | * * |
| 356 | ************************************************************************/ |
| 357 | |
| 358 | /** |
| 359 | * xmlSchemaNewSchema: |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 360 | * @ctxt: a schema validation context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 361 | * |
| 362 | * Allocate a new Schema structure. |
| 363 | * |
| 364 | * Returns the newly allocated structure or NULL in case or error |
| 365 | */ |
| 366 | static xmlSchemaPtr |
| 367 | xmlSchemaNewSchema(xmlSchemaParserCtxtPtr ctxt) |
| 368 | { |
| 369 | xmlSchemaPtr ret; |
| 370 | |
| 371 | ret = (xmlSchemaPtr) xmlMalloc(sizeof(xmlSchema)); |
| 372 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 373 | xmlSchemaPErrMemory(ctxt, "allocating schema", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 374 | return (NULL); |
| 375 | } |
| 376 | memset(ret, 0, sizeof(xmlSchema)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 377 | xmlDictReference(ctxt->dict); |
| 378 | ret->dict = ctxt->dict; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 379 | |
| 380 | return (ret); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * xmlSchemaNewFacet: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 385 | * |
| 386 | * Allocate a new Facet structure. |
| 387 | * |
| 388 | * Returns the newly allocated structure or NULL in case or error |
| 389 | */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 390 | xmlSchemaFacetPtr |
| 391 | xmlSchemaNewFacet(void) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 392 | { |
| 393 | xmlSchemaFacetPtr ret; |
| 394 | |
| 395 | ret = (xmlSchemaFacetPtr) xmlMalloc(sizeof(xmlSchemaFacet)); |
| 396 | if (ret == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 397 | return (NULL); |
| 398 | } |
| 399 | memset(ret, 0, sizeof(xmlSchemaFacet)); |
| 400 | |
| 401 | return (ret); |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * xmlSchemaNewAnnot: |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 406 | * @ctxt: a schema validation context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 407 | * @node: a node |
| 408 | * |
| 409 | * Allocate a new annotation structure. |
| 410 | * |
| 411 | * Returns the newly allocated structure or NULL in case or error |
| 412 | */ |
| 413 | static xmlSchemaAnnotPtr |
| 414 | xmlSchemaNewAnnot(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node) |
| 415 | { |
| 416 | xmlSchemaAnnotPtr ret; |
| 417 | |
| 418 | ret = (xmlSchemaAnnotPtr) xmlMalloc(sizeof(xmlSchemaAnnot)); |
| 419 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 420 | xmlSchemaPErrMemory(ctxt, "allocating annotation", node); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 421 | return (NULL); |
| 422 | } |
| 423 | memset(ret, 0, sizeof(xmlSchemaAnnot)); |
| 424 | ret->content = node; |
| 425 | return (ret); |
| 426 | } |
| 427 | |
| 428 | /** |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 429 | * xmlSchemaFreeAnnot: |
| 430 | * @annot: a schema type structure |
| 431 | * |
| 432 | * Deallocate a annotation structure |
| 433 | */ |
| 434 | static void |
| 435 | xmlSchemaFreeAnnot(xmlSchemaAnnotPtr annot) |
| 436 | { |
| 437 | if (annot == NULL) |
| 438 | return; |
| 439 | xmlFree(annot); |
| 440 | } |
| 441 | |
| 442 | /** |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 443 | * xmlSchemaFreeImport: |
| 444 | * @import: a schema import structure |
| 445 | * |
| 446 | * Deallocate an import structure |
| 447 | */ |
| 448 | static void |
| 449 | xmlSchemaFreeImport(xmlSchemaImportPtr import) |
| 450 | { |
| 451 | if (import == NULL) |
| 452 | return; |
| 453 | |
| 454 | xmlSchemaFree(import->schema); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 455 | xmlFree(import); |
| 456 | } |
| 457 | |
| 458 | /** |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 459 | * xmlSchemaFreeInclude: |
| 460 | * @include: a schema include structure |
| 461 | * |
| 462 | * Deallocate an include structure |
| 463 | */ |
| 464 | static void |
| 465 | xmlSchemaFreeInclude(xmlSchemaIncludePtr include) |
| 466 | { |
| 467 | if (include == NULL) |
| 468 | return; |
| 469 | |
| 470 | xmlFreeDoc(include->doc); |
| 471 | xmlFree(include); |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * xmlSchemaFreeIncludeList: |
| 476 | * @includes: a schema include list |
| 477 | * |
| 478 | * Deallocate an include structure |
| 479 | */ |
| 480 | static void |
| 481 | xmlSchemaFreeIncludeList(xmlSchemaIncludePtr includes) |
| 482 | { |
| 483 | xmlSchemaIncludePtr next; |
| 484 | |
| 485 | while (includes != NULL) { |
| 486 | next = includes->next; |
| 487 | xmlSchemaFreeInclude(includes); |
| 488 | includes = next; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 493 | * xmlSchemaFreeNotation: |
| 494 | * @schema: a schema notation structure |
| 495 | * |
| 496 | * Deallocate a Schema Notation structure. |
| 497 | */ |
| 498 | static void |
| 499 | xmlSchemaFreeNotation(xmlSchemaNotationPtr nota) |
| 500 | { |
| 501 | if (nota == NULL) |
| 502 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 503 | xmlFree(nota); |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * xmlSchemaFreeAttribute: |
| 508 | * @schema: a schema attribute structure |
| 509 | * |
| 510 | * Deallocate a Schema Attribute structure. |
| 511 | */ |
| 512 | static void |
| 513 | xmlSchemaFreeAttribute(xmlSchemaAttributePtr attr) |
| 514 | { |
| 515 | if (attr == NULL) |
| 516 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 517 | xmlFree(attr); |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * xmlSchemaFreeAttributeGroup: |
| 522 | * @schema: a schema attribute group structure |
| 523 | * |
| 524 | * Deallocate a Schema Attribute Group structure. |
| 525 | */ |
| 526 | static void |
| 527 | xmlSchemaFreeAttributeGroup(xmlSchemaAttributeGroupPtr attr) |
| 528 | { |
| 529 | if (attr == NULL) |
| 530 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 531 | xmlFree(attr); |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * xmlSchemaFreeElement: |
| 536 | * @schema: a schema element structure |
| 537 | * |
| 538 | * Deallocate a Schema Element structure. |
| 539 | */ |
| 540 | static void |
| 541 | xmlSchemaFreeElement(xmlSchemaElementPtr elem) |
| 542 | { |
| 543 | if (elem == NULL) |
| 544 | return; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 545 | if (elem->annot != NULL) |
| 546 | xmlSchemaFreeAnnot(elem->annot); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 547 | if (elem->contModel != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 548 | xmlRegFreeRegexp(elem->contModel); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 549 | xmlFree(elem); |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * xmlSchemaFreeFacet: |
| 554 | * @facet: a schema facet structure |
| 555 | * |
| 556 | * Deallocate a Schema Facet structure. |
| 557 | */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 558 | void |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 559 | xmlSchemaFreeFacet(xmlSchemaFacetPtr facet) |
| 560 | { |
| 561 | if (facet == NULL) |
| 562 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 563 | if (facet->val != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 564 | xmlSchemaFreeValue(facet->val); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 565 | if (facet->regexp != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 566 | xmlRegFreeRegexp(facet->regexp); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 567 | if (facet->annot != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 568 | xmlSchemaFreeAnnot(facet->annot); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 569 | xmlFree(facet); |
| 570 | } |
| 571 | |
| 572 | /** |
| 573 | * xmlSchemaFreeType: |
| 574 | * @type: a schema type structure |
| 575 | * |
| 576 | * Deallocate a Schema Type structure. |
| 577 | */ |
| 578 | void |
| 579 | xmlSchemaFreeType(xmlSchemaTypePtr type) |
| 580 | { |
| 581 | if (type == NULL) |
| 582 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 583 | if (type->annot != NULL) |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 584 | xmlSchemaFreeAnnot(type->annot); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 585 | if (type->facets != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 586 | xmlSchemaFacetPtr facet, next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 587 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 588 | facet = type->facets; |
| 589 | while (facet != NULL) { |
| 590 | next = facet->next; |
| 591 | xmlSchemaFreeFacet(facet); |
| 592 | facet = next; |
| 593 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 594 | } |
| 595 | xmlFree(type); |
| 596 | } |
| 597 | |
| 598 | /** |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 599 | * xmlSchemaFreeTypeList: |
| 600 | * @type: a schema type structure |
| 601 | * |
| 602 | * Deallocate a Schema Type structure. |
| 603 | */ |
| 604 | static void |
| 605 | xmlSchemaFreeTypeList(xmlSchemaTypePtr type) |
| 606 | { |
| 607 | xmlSchemaTypePtr next; |
| 608 | |
| 609 | while (type != NULL) { |
| 610 | next = type->redef; |
| 611 | xmlSchemaFreeType(type); |
| 612 | type = next; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 617 | * xmlSchemaFree: |
| 618 | * @schema: a schema structure |
| 619 | * |
| 620 | * Deallocate a Schema structure. |
| 621 | */ |
| 622 | void |
| 623 | xmlSchemaFree(xmlSchemaPtr schema) |
| 624 | { |
| 625 | if (schema == NULL) |
| 626 | return; |
| 627 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 628 | if (schema->notaDecl != NULL) |
| 629 | xmlHashFree(schema->notaDecl, |
| 630 | (xmlHashDeallocator) xmlSchemaFreeNotation); |
| 631 | if (schema->attrDecl != NULL) |
| 632 | xmlHashFree(schema->attrDecl, |
| 633 | (xmlHashDeallocator) xmlSchemaFreeAttribute); |
| 634 | if (schema->attrgrpDecl != NULL) |
| 635 | xmlHashFree(schema->attrgrpDecl, |
| 636 | (xmlHashDeallocator) xmlSchemaFreeAttributeGroup); |
| 637 | if (schema->elemDecl != NULL) |
| 638 | xmlHashFree(schema->elemDecl, |
| 639 | (xmlHashDeallocator) xmlSchemaFreeElement); |
| 640 | if (schema->typeDecl != NULL) |
| 641 | xmlHashFree(schema->typeDecl, |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 642 | (xmlHashDeallocator) xmlSchemaFreeTypeList); |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 643 | if (schema->groupDecl != NULL) |
| 644 | xmlHashFree(schema->groupDecl, |
| 645 | (xmlHashDeallocator) xmlSchemaFreeType); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 646 | if (schema->schemasImports != NULL) |
| 647 | xmlHashFree(schema->schemasImports, |
| 648 | (xmlHashDeallocator) xmlSchemaFreeImport); |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 649 | if (schema->includes != NULL) { |
| 650 | xmlSchemaFreeIncludeList((xmlSchemaIncludePtr) schema->includes); |
| 651 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 652 | if (schema->annot != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 653 | xmlSchemaFreeAnnot(schema->annot); |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 654 | if (schema->doc != NULL && !schema->preserve) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 655 | xmlFreeDoc(schema->doc); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 656 | xmlDictFree(schema->dict); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 657 | |
| 658 | xmlFree(schema); |
| 659 | } |
| 660 | |
| 661 | /************************************************************************ |
| 662 | * * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 663 | * Debug functions * |
| 664 | * * |
| 665 | ************************************************************************/ |
| 666 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 667 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 668 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 669 | /** |
| 670 | * xmlSchemaElementDump: |
| 671 | * @elem: an element |
| 672 | * @output: the file output |
| 673 | * |
| 674 | * Dump the element |
| 675 | */ |
| 676 | static void |
| 677 | xmlSchemaElementDump(xmlSchemaElementPtr elem, FILE * output, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 678 | const xmlChar * name ATTRIBUTE_UNUSED, |
| 679 | const xmlChar * context ATTRIBUTE_UNUSED, |
| 680 | const xmlChar * namespace ATTRIBUTE_UNUSED) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 681 | { |
| 682 | if (elem == NULL) |
| 683 | return; |
| 684 | |
| 685 | fprintf(output, "Element "); |
| 686 | if (elem->flags & XML_SCHEMAS_ELEM_TOPLEVEL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 687 | fprintf(output, "toplevel "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 688 | fprintf(output, ": %s ", elem->name); |
| 689 | if (namespace != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 690 | fprintf(output, "namespace '%s' ", namespace); |
| 691 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 692 | if (elem->flags & XML_SCHEMAS_ELEM_NILLABLE) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 693 | fprintf(output, "nillable "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 694 | if (elem->flags & XML_SCHEMAS_ELEM_GLOBAL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 695 | fprintf(output, "global "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 696 | if (elem->flags & XML_SCHEMAS_ELEM_DEFAULT) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 697 | fprintf(output, "default "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 698 | if (elem->flags & XML_SCHEMAS_ELEM_FIXED) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 699 | fprintf(output, "fixed "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 700 | if (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 701 | fprintf(output, "abstract "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 702 | if (elem->flags & XML_SCHEMAS_ELEM_REF) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 703 | fprintf(output, "ref '%s' ", elem->ref); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 704 | if (elem->id != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 705 | fprintf(output, "id '%s' ", elem->id); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 706 | fprintf(output, "\n"); |
| 707 | if ((elem->minOccurs != 1) || (elem->maxOccurs != 1)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 708 | fprintf(output, " "); |
| 709 | if (elem->minOccurs != 1) |
| 710 | fprintf(output, "min: %d ", elem->minOccurs); |
| 711 | if (elem->maxOccurs >= UNBOUNDED) |
| 712 | fprintf(output, "max: unbounded\n"); |
| 713 | else if (elem->maxOccurs != 1) |
| 714 | fprintf(output, "max: %d\n", elem->maxOccurs); |
| 715 | else |
| 716 | fprintf(output, "\n"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 717 | } |
| 718 | if (elem->namedType != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 719 | fprintf(output, " type: %s", elem->namedType); |
| 720 | if (elem->namedTypeNs != NULL) |
| 721 | fprintf(output, " ns %s\n", elem->namedTypeNs); |
| 722 | else |
| 723 | fprintf(output, "\n"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 724 | } |
| 725 | if (elem->substGroup != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 726 | fprintf(output, " substitutionGroup: %s", elem->substGroup); |
| 727 | if (elem->substGroupNs != NULL) |
| 728 | fprintf(output, " ns %s\n", elem->substGroupNs); |
| 729 | else |
| 730 | fprintf(output, "\n"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 731 | } |
| 732 | if (elem->value != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 733 | fprintf(output, " default: %s", elem->value); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | /** |
| 737 | * xmlSchemaAnnotDump: |
| 738 | * @output: the file output |
| 739 | * @annot: a annotation |
| 740 | * |
| 741 | * Dump the annotation |
| 742 | */ |
| 743 | static void |
| 744 | xmlSchemaAnnotDump(FILE * output, xmlSchemaAnnotPtr annot) |
| 745 | { |
| 746 | xmlChar *content; |
| 747 | |
| 748 | if (annot == NULL) |
| 749 | return; |
| 750 | |
| 751 | content = xmlNodeGetContent(annot->content); |
| 752 | if (content != NULL) { |
| 753 | fprintf(output, " Annot: %s\n", content); |
| 754 | xmlFree(content); |
| 755 | } else |
| 756 | fprintf(output, " Annot: empty\n"); |
| 757 | } |
| 758 | |
| 759 | /** |
| 760 | * xmlSchemaTypeDump: |
| 761 | * @output: the file output |
| 762 | * @type: a type structure |
| 763 | * |
| 764 | * Dump a SchemaType structure |
| 765 | */ |
| 766 | static void |
| 767 | xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output) |
| 768 | { |
| 769 | if (type == NULL) { |
| 770 | fprintf(output, "Type: NULL\n"); |
| 771 | return; |
| 772 | } |
| 773 | fprintf(output, "Type: "); |
| 774 | if (type->name != NULL) |
| 775 | fprintf(output, "%s, ", type->name); |
| 776 | else |
| 777 | fprintf(output, "no name"); |
| 778 | switch (type->type) { |
| 779 | case XML_SCHEMA_TYPE_BASIC: |
| 780 | fprintf(output, "basic "); |
| 781 | break; |
| 782 | case XML_SCHEMA_TYPE_SIMPLE: |
| 783 | fprintf(output, "simple "); |
| 784 | break; |
| 785 | case XML_SCHEMA_TYPE_COMPLEX: |
| 786 | fprintf(output, "complex "); |
| 787 | break; |
| 788 | case XML_SCHEMA_TYPE_SEQUENCE: |
| 789 | fprintf(output, "sequence "); |
| 790 | break; |
| 791 | case XML_SCHEMA_TYPE_CHOICE: |
| 792 | fprintf(output, "choice "); |
| 793 | break; |
| 794 | case XML_SCHEMA_TYPE_ALL: |
| 795 | fprintf(output, "all "); |
| 796 | break; |
| 797 | case XML_SCHEMA_TYPE_UR: |
| 798 | fprintf(output, "ur "); |
| 799 | break; |
| 800 | case XML_SCHEMA_TYPE_RESTRICTION: |
| 801 | fprintf(output, "restriction "); |
| 802 | break; |
| 803 | case XML_SCHEMA_TYPE_EXTENSION: |
| 804 | fprintf(output, "extension "); |
| 805 | break; |
| 806 | default: |
| 807 | fprintf(output, "unknowntype%d ", type->type); |
| 808 | break; |
| 809 | } |
| 810 | if (type->base != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 811 | fprintf(output, "base %s, ", type->base); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 812 | } |
| 813 | switch (type->contentType) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 814 | case XML_SCHEMA_CONTENT_UNKNOWN: |
| 815 | fprintf(output, "unknown "); |
| 816 | break; |
| 817 | case XML_SCHEMA_CONTENT_EMPTY: |
| 818 | fprintf(output, "empty "); |
| 819 | break; |
| 820 | case XML_SCHEMA_CONTENT_ELEMENTS: |
| 821 | fprintf(output, "element "); |
| 822 | break; |
| 823 | case XML_SCHEMA_CONTENT_MIXED: |
| 824 | fprintf(output, "mixed "); |
| 825 | break; |
| 826 | case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS: |
| 827 | fprintf(output, "mixed_or_elems "); |
| 828 | break; |
| 829 | case XML_SCHEMA_CONTENT_BASIC: |
| 830 | fprintf(output, "basic "); |
| 831 | break; |
| 832 | case XML_SCHEMA_CONTENT_SIMPLE: |
| 833 | fprintf(output, "simple "); |
| 834 | break; |
| 835 | case XML_SCHEMA_CONTENT_ANY: |
| 836 | fprintf(output, "any "); |
| 837 | break; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 838 | } |
| 839 | fprintf(output, "\n"); |
| 840 | if ((type->minOccurs != 1) || (type->maxOccurs != 1)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 841 | fprintf(output, " "); |
| 842 | if (type->minOccurs != 1) |
| 843 | fprintf(output, "min: %d ", type->minOccurs); |
| 844 | if (type->maxOccurs >= UNBOUNDED) |
| 845 | fprintf(output, "max: unbounded\n"); |
| 846 | else if (type->maxOccurs != 1) |
| 847 | fprintf(output, "max: %d\n", type->maxOccurs); |
| 848 | else |
| 849 | fprintf(output, "\n"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 850 | } |
| 851 | if (type->annot != NULL) |
| 852 | xmlSchemaAnnotDump(output, type->annot); |
| 853 | if (type->subtypes != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 854 | xmlSchemaTypePtr sub = type->subtypes; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 855 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 856 | fprintf(output, " subtypes: "); |
| 857 | while (sub != NULL) { |
| 858 | fprintf(output, "%s ", sub->name); |
| 859 | sub = sub->next; |
| 860 | } |
| 861 | fprintf(output, "\n"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | } |
| 865 | |
| 866 | /** |
| 867 | * xmlSchemaDump: |
| 868 | * @output: the file output |
| 869 | * @schema: a schema structure |
| 870 | * |
| 871 | * Dump a Schema structure. |
| 872 | */ |
| 873 | void |
| 874 | xmlSchemaDump(FILE * output, xmlSchemaPtr schema) |
| 875 | { |
| 876 | if (schema == NULL) { |
| 877 | fprintf(output, "Schemas: NULL\n"); |
| 878 | return; |
| 879 | } |
| 880 | fprintf(output, "Schemas: "); |
| 881 | if (schema->name != NULL) |
| 882 | fprintf(output, "%s, ", schema->name); |
| 883 | else |
| 884 | fprintf(output, "no name, "); |
| 885 | if (schema->targetNamespace != NULL) |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 886 | fprintf(output, "%s", (const char *) schema->targetNamespace); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 887 | else |
| 888 | fprintf(output, "no target namespace"); |
| 889 | fprintf(output, "\n"); |
| 890 | if (schema->annot != NULL) |
| 891 | xmlSchemaAnnotDump(output, schema->annot); |
| 892 | |
| 893 | xmlHashScan(schema->typeDecl, (xmlHashScanner) xmlSchemaTypeDump, |
| 894 | output); |
| 895 | xmlHashScanFull(schema->elemDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 896 | (xmlHashScannerFull) xmlSchemaElementDump, output); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 897 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 898 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 899 | |
| 900 | /************************************************************************ |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 901 | * * |
| 902 | * Utilities * |
| 903 | * * |
| 904 | ************************************************************************/ |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 905 | |
| 906 | /** |
| 907 | * xmlSchemaGetProp: |
| 908 | * @ctxt: the parser context |
| 909 | * @node: the node |
| 910 | * @name: the property name |
| 911 | * |
| 912 | * Read a attribute value and internalize the string |
| 913 | * |
| 914 | * Returns the string or NULL if not present. |
| 915 | */ |
| 916 | static const xmlChar * |
| 917 | xmlSchemaGetProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, |
| 918 | const char *name) |
| 919 | { |
| 920 | xmlChar *val; |
| 921 | const xmlChar *ret; |
| 922 | |
| 923 | val = xmlGetProp(node, BAD_CAST name); |
| 924 | if (val == NULL) |
| 925 | return(NULL); |
| 926 | ret = xmlDictLookup(ctxt->dict, val, -1); |
| 927 | xmlFree(val); |
| 928 | return(ret); |
| 929 | } |
| 930 | |
| 931 | /** |
| 932 | * xmlSchemaGetNamespace: |
| 933 | * @ctxt: the parser context |
| 934 | * @schema: the schemas containing the declaration |
| 935 | * @node: the node |
| 936 | * @qname: the QName to analyze |
| 937 | * |
| 938 | * Find the namespace name for the given declaration. |
| 939 | * |
| 940 | * Returns the local name for that declaration, as well as the namespace name |
| 941 | */ |
| 942 | static const xmlChar * |
| 943 | xmlSchemaGetNamespace(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 944 | xmlNodePtr node, const xmlChar *qname, |
| 945 | const xmlChar **namespace) { |
| 946 | int len; |
| 947 | const xmlChar *name, *prefix, *def = NULL; |
| 948 | xmlNsPtr ns; |
| 949 | |
| 950 | *namespace = NULL; |
| 951 | |
| 952 | if (xmlStrEqual(node->name, BAD_CAST "element") || |
| 953 | xmlStrEqual(node->name, BAD_CAST "attribute") || |
| 954 | xmlStrEqual(node->name, BAD_CAST "simpleType") || |
| 955 | xmlStrEqual(node->name, BAD_CAST "complexType")) { |
| 956 | def = xmlSchemaGetProp(ctxt, node, "targetNamespace"); |
| 957 | } |
| 958 | |
| 959 | qname = xmlDictLookup(ctxt->dict, qname, -1); /* intern the string */ |
| 960 | name = xmlSplitQName3(qname, &len); |
| 961 | if (name == NULL) { |
| 962 | if (def == NULL) { |
| 963 | if (xmlStrEqual(node->name, BAD_CAST "element")) { |
| 964 | if (schema->flags & XML_SCHEMAS_QUALIF_ELEM) |
| 965 | *namespace = schema->targetNamespace; |
| 966 | } else if (xmlStrEqual(node->name, BAD_CAST "attribute")) { |
| 967 | if (schema->flags & XML_SCHEMAS_QUALIF_ATTR) |
| 968 | *namespace = schema->targetNamespace; |
| 969 | } else if ((xmlStrEqual(node->name, BAD_CAST "simpleType")) || |
| 970 | (xmlStrEqual(node->name, BAD_CAST "complexType"))) { |
| 971 | *namespace = schema->targetNamespace; |
| 972 | } |
| 973 | } else { |
| 974 | *namespace = def; |
| 975 | } |
| 976 | return(qname); |
| 977 | } |
| 978 | name = xmlDictLookup(ctxt->dict, name, -1); |
| 979 | prefix = xmlDictLookup(ctxt->dict, qname, len); |
| 980 | if (def != NULL) { |
| 981 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_DEF_AND_PREFIX, |
| 982 | "%s: presence of both prefix %s and targetNamespace\n", |
| 983 | node->name, prefix); |
| 984 | } |
| 985 | ns = xmlSearchNs(node->doc, node, prefix); |
| 986 | if (ns == NULL) { |
| 987 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_PREFIX_UNDEFINED, |
| 988 | "%s: the QName prefix %s is undefined\n", |
| 989 | node->name, prefix); |
| 990 | return(name); |
| 991 | } |
| 992 | *namespace = xmlDictLookup(ctxt->dict, ns->href, -1); |
| 993 | return(name); |
| 994 | } |
| 995 | |
| 996 | /************************************************************************ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 997 | * * |
| 998 | * Parsing functions * |
| 999 | * * |
| 1000 | ************************************************************************/ |
| 1001 | |
| 1002 | /** |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1003 | * xmlSchemaGetElem: |
| 1004 | * @schema: the schemas context |
| 1005 | * @name: the element name |
| 1006 | * @ns: the element namespace |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1007 | * @level: how deep is the request |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1008 | * |
| 1009 | * Lookup a an element in the schemas or the accessible schemas |
| 1010 | * |
| 1011 | * Returns the element definition or NULL if not found. |
| 1012 | */ |
| 1013 | static xmlSchemaElementPtr |
| 1014 | xmlSchemaGetElem(xmlSchemaPtr schema, const xmlChar * name, |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1015 | const xmlChar * namespace, int level) |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1016 | { |
| 1017 | xmlSchemaElementPtr ret; |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1018 | xmlSchemaImportPtr import = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1019 | |
| 1020 | if ((name == NULL) || (schema == NULL)) |
| 1021 | return (NULL); |
| 1022 | |
| 1023 | if (namespace == NULL) { |
| 1024 | ret = xmlHashLookup2(schema->elemDecl, name, namespace); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1025 | if ((ret != NULL) && |
| 1026 | ((level == 0) || (ret->flags & XML_SCHEMAS_ELEM_TOPLEVEL))) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1027 | return (ret); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1028 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1029 | } else if ((schema->flags & XML_SCHEMAS_QUALIF_ELEM) == 0) { |
| 1030 | if (xmlStrEqual(namespace, schema->targetNamespace)) |
| 1031 | ret = xmlHashLookup2(schema->elemDecl, name, NULL); |
| 1032 | else |
| 1033 | ret = xmlHashLookup2(schema->elemDecl, name, namespace); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1034 | if ((ret != NULL) && |
| 1035 | ((level == 0) || (ret->flags & XML_SCHEMAS_ELEM_TOPLEVEL))) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1036 | return (ret); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1037 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1038 | } else { |
| 1039 | ret = xmlHashLookup2(schema->elemDecl, name, namespace); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1040 | if ((ret != NULL) && |
| 1041 | ((level == 0) || (ret->flags & XML_SCHEMAS_ELEM_TOPLEVEL))) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1042 | return (ret); |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1043 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1044 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1045 | if (level > 0) |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1046 | import = xmlHashLookup(schema->schemasImports, namespace); |
| 1047 | if (import != NULL) |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 1048 | ret = xmlSchemaGetElem(import->schema, name, namespace, level + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1049 | #ifdef DEBUG |
| 1050 | if (ret == NULL) { |
| 1051 | if (namespace == NULL) |
| 1052 | fprintf(stderr, "Unable to lookup type %s", name); |
| 1053 | else |
| 1054 | fprintf(stderr, "Unable to lookup type %s:%s", name, |
| 1055 | namespace); |
| 1056 | } |
| 1057 | #endif |
| 1058 | return (ret); |
| 1059 | } |
| 1060 | |
| 1061 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1062 | * xmlSchemaGetType: |
| 1063 | * @schema: the schemas context |
| 1064 | * @name: the type name |
| 1065 | * @ns: the type namespace |
| 1066 | * |
| 1067 | * Lookup a type in the schemas or the predefined types |
| 1068 | * |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 1069 | * Returns the group definition or NULL if not found. |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1070 | */ |
| 1071 | static xmlSchemaTypePtr |
| 1072 | xmlSchemaGetType(xmlSchemaPtr schema, const xmlChar * name, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1073 | const xmlChar * namespace) |
| 1074 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1075 | xmlSchemaTypePtr ret; |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 1076 | xmlSchemaImportPtr import; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1077 | |
| 1078 | if (name == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1079 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1080 | if (schema != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1081 | ret = xmlHashLookup2(schema->typeDecl, name, namespace); |
| 1082 | if (ret != NULL) |
| 1083 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1084 | } |
| 1085 | ret = xmlSchemaGetPredefinedType(name, namespace); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 1086 | if (ret != NULL) |
| 1087 | return (ret); |
| 1088 | import = xmlHashLookup(schema->schemasImports, namespace); |
| 1089 | if (import != NULL) |
| 1090 | ret = xmlSchemaGetType(import->schema, name, namespace); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1091 | #ifdef DEBUG |
| 1092 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1093 | if (namespace == NULL) |
| 1094 | fprintf(stderr, "Unable to lookup type %s", name); |
| 1095 | else |
| 1096 | fprintf(stderr, "Unable to lookup type %s:%s", name, |
| 1097 | namespace); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1098 | } |
| 1099 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1100 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | /************************************************************************ |
| 1104 | * * |
| 1105 | * Parsing functions * |
| 1106 | * * |
| 1107 | ************************************************************************/ |
| 1108 | |
| 1109 | #define IS_BLANK_NODE(n) \ |
| 1110 | (((n)->type == XML_TEXT_NODE) && (xmlSchemaIsBlank((n)->content))) |
| 1111 | |
| 1112 | /** |
| 1113 | * xmlSchemaIsBlank: |
| 1114 | * @str: a string |
| 1115 | * |
| 1116 | * Check if a string is ignorable |
| 1117 | * |
| 1118 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 1119 | */ |
| 1120 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1121 | xmlSchemaIsBlank(xmlChar * str) |
| 1122 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1123 | if (str == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1124 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1125 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1126 | if (!(IS_BLANK_CH(*str))) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1127 | return (0); |
| 1128 | str++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1129 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1130 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | /** |
| 1134 | * xmlSchemaAddNotation: |
| 1135 | * @ctxt: a schema validation context |
| 1136 | * @schema: the schema being built |
| 1137 | * @name: the item name |
| 1138 | * |
| 1139 | * Add an XML schema Attrribute declaration |
| 1140 | * *WARNING* this interface is highly subject to change |
| 1141 | * |
| 1142 | * Returns the new struture or NULL in case of error |
| 1143 | */ |
| 1144 | static xmlSchemaNotationPtr |
| 1145 | xmlSchemaAddNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1146 | const xmlChar * name) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1147 | { |
| 1148 | xmlSchemaNotationPtr ret = NULL; |
| 1149 | int val; |
| 1150 | |
| 1151 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1152 | return (NULL); |
| 1153 | |
| 1154 | if (schema->notaDecl == NULL) |
| 1155 | schema->notaDecl = xmlHashCreate(10); |
| 1156 | if (schema->notaDecl == NULL) |
| 1157 | return (NULL); |
| 1158 | |
| 1159 | ret = (xmlSchemaNotationPtr) xmlMalloc(sizeof(xmlSchemaNotation)); |
| 1160 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1161 | xmlSchemaPErrMemory(ctxt, "add annotation", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1162 | return (NULL); |
| 1163 | } |
| 1164 | memset(ret, 0, sizeof(xmlSchemaNotation)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1165 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1166 | val = xmlHashAddEntry2(schema->notaDecl, name, schema->targetNamespace, |
| 1167 | ret); |
| 1168 | if (val != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1169 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1170 | XML_SCHEMAP_REDEFINED_NOTATION, |
| 1171 | "Notation %s already defined\n", |
| 1172 | name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1173 | xmlFree(ret); |
| 1174 | return (NULL); |
| 1175 | } |
| 1176 | return (ret); |
| 1177 | } |
| 1178 | |
| 1179 | |
| 1180 | /** |
| 1181 | * xmlSchemaAddAttribute: |
| 1182 | * @ctxt: a schema validation context |
| 1183 | * @schema: the schema being built |
| 1184 | * @name: the item name |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1185 | * @namespace: the namespace |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1186 | * |
| 1187 | * Add an XML schema Attrribute declaration |
| 1188 | * *WARNING* this interface is highly subject to change |
| 1189 | * |
| 1190 | * Returns the new struture or NULL in case of error |
| 1191 | */ |
| 1192 | static xmlSchemaAttributePtr |
| 1193 | xmlSchemaAddAttribute(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1194 | const xmlChar * name, const xmlChar * namespace) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1195 | { |
| 1196 | xmlSchemaAttributePtr ret = NULL; |
| 1197 | int val; |
| 1198 | |
| 1199 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1200 | return (NULL); |
| 1201 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1202 | #ifdef DEBUG |
| 1203 | fprintf(stderr, "Adding attribute %s\n", name); |
| 1204 | if (namespace != NULL) |
| 1205 | fprintf(stderr, " target namespace %s\n", namespace); |
| 1206 | #endif |
| 1207 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1208 | if (schema->attrDecl == NULL) |
| 1209 | schema->attrDecl = xmlHashCreate(10); |
| 1210 | if (schema->attrDecl == NULL) |
| 1211 | return (NULL); |
| 1212 | |
| 1213 | ret = (xmlSchemaAttributePtr) xmlMalloc(sizeof(xmlSchemaAttribute)); |
| 1214 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1215 | xmlSchemaPErrMemory(ctxt, "allocating attribute", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1216 | return (NULL); |
| 1217 | } |
| 1218 | memset(ret, 0, sizeof(xmlSchemaAttribute)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1219 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
| 1220 | ret->targetNamespace = xmlDictLookup(ctxt->dict, namespace, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1221 | val = xmlHashAddEntry3(schema->attrDecl, name, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1222 | schema->targetNamespace, ctxt->container, ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1223 | if (val != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1224 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1225 | XML_SCHEMAP_REDEFINED_ATTR, |
| 1226 | "Attribute %s already defined\n", |
| 1227 | name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1228 | xmlFree(ret); |
| 1229 | return (NULL); |
| 1230 | } |
| 1231 | return (ret); |
| 1232 | } |
| 1233 | |
| 1234 | /** |
| 1235 | * xmlSchemaAddAttributeGroup: |
| 1236 | * @ctxt: a schema validation context |
| 1237 | * @schema: the schema being built |
| 1238 | * @name: the item name |
| 1239 | * |
| 1240 | * Add an XML schema Attrribute Group declaration |
| 1241 | * |
| 1242 | * Returns the new struture or NULL in case of error |
| 1243 | */ |
| 1244 | static xmlSchemaAttributeGroupPtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1245 | xmlSchemaAddAttributeGroup(xmlSchemaParserCtxtPtr ctxt, |
| 1246 | xmlSchemaPtr schema, const xmlChar * name) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1247 | { |
| 1248 | xmlSchemaAttributeGroupPtr ret = NULL; |
| 1249 | int val; |
| 1250 | |
| 1251 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1252 | return (NULL); |
| 1253 | |
| 1254 | if (schema->attrgrpDecl == NULL) |
| 1255 | schema->attrgrpDecl = xmlHashCreate(10); |
| 1256 | if (schema->attrgrpDecl == NULL) |
| 1257 | return (NULL); |
| 1258 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1259 | ret = |
| 1260 | (xmlSchemaAttributeGroupPtr) |
| 1261 | xmlMalloc(sizeof(xmlSchemaAttributeGroup)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1262 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1263 | xmlSchemaPErrMemory(ctxt, "allocating attribute group", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1264 | return (NULL); |
| 1265 | } |
| 1266 | memset(ret, 0, sizeof(xmlSchemaAttributeGroup)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1267 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1268 | val = xmlHashAddEntry3(schema->attrgrpDecl, name, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1269 | schema->targetNamespace, ctxt->container, ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1270 | if (val != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1271 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1272 | XML_SCHEMAP_REDEFINED_ATTRGROUP, |
| 1273 | "Attribute group %s already defined\n", |
| 1274 | name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1275 | xmlFree(ret); |
| 1276 | return (NULL); |
| 1277 | } |
| 1278 | return (ret); |
| 1279 | } |
| 1280 | |
| 1281 | /** |
| 1282 | * xmlSchemaAddElement: |
| 1283 | * @ctxt: a schema validation context |
| 1284 | * @schema: the schema being built |
| 1285 | * @name: the type name |
| 1286 | * @namespace: the type namespace |
| 1287 | * |
| 1288 | * Add an XML schema Element declaration |
| 1289 | * *WARNING* this interface is highly subject to change |
| 1290 | * |
| 1291 | * Returns the new struture or NULL in case of error |
| 1292 | */ |
| 1293 | static xmlSchemaElementPtr |
| 1294 | xmlSchemaAddElement(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 1295 | const xmlChar * name, const xmlChar * namespace) |
| 1296 | { |
| 1297 | xmlSchemaElementPtr ret = NULL; |
| 1298 | int val; |
| 1299 | |
| 1300 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1301 | return (NULL); |
| 1302 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1303 | #ifdef DEBUG |
| 1304 | fprintf(stderr, "Adding element %s\n", name); |
| 1305 | if (namespace != NULL) |
| 1306 | fprintf(stderr, " target namespace %s\n", namespace); |
| 1307 | #endif |
| 1308 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1309 | if (schema->elemDecl == NULL) |
| 1310 | schema->elemDecl = xmlHashCreate(10); |
| 1311 | if (schema->elemDecl == NULL) |
| 1312 | return (NULL); |
| 1313 | |
| 1314 | ret = (xmlSchemaElementPtr) xmlMalloc(sizeof(xmlSchemaElement)); |
| 1315 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1316 | xmlSchemaPErrMemory(ctxt, "allocating element", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1317 | return (NULL); |
| 1318 | } |
| 1319 | memset(ret, 0, sizeof(xmlSchemaElement)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1320 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
| 1321 | ret->targetNamespace = xmlDictLookup(ctxt->dict, namespace, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1322 | val = xmlHashAddEntry3(schema->elemDecl, name, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1323 | namespace, ctxt->container, ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1324 | if (val != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1325 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1326 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1327 | snprintf(buf, 99, "privatieelem %d", ctxt->counter++ + 1); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1328 | val = xmlHashAddEntry3(schema->elemDecl, name, (xmlChar *) buf, |
| 1329 | namespace, ret); |
| 1330 | if (val != 0) { |
| 1331 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1332 | XML_SCHEMAP_REDEFINED_ELEMENT, |
| 1333 | "Element %s already defined\n", |
| 1334 | name, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1335 | xmlFree(ret); |
| 1336 | return (NULL); |
| 1337 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1338 | } |
| 1339 | return (ret); |
| 1340 | } |
| 1341 | |
| 1342 | /** |
| 1343 | * xmlSchemaAddType: |
| 1344 | * @ctxt: a schema validation context |
| 1345 | * @schema: the schema being built |
| 1346 | * @name: the item name |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1347 | * @namespace: the namespace |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1348 | * |
| 1349 | * Add an XML schema Simple Type definition |
| 1350 | * *WARNING* this interface is highly subject to change |
| 1351 | * |
| 1352 | * Returns the new struture or NULL in case of error |
| 1353 | */ |
| 1354 | static xmlSchemaTypePtr |
| 1355 | xmlSchemaAddType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1356 | const xmlChar * name, const xmlChar * namespace) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1357 | { |
| 1358 | xmlSchemaTypePtr ret = NULL; |
| 1359 | int val; |
| 1360 | |
| 1361 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1362 | return (NULL); |
| 1363 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1364 | #ifdef DEBUG |
| 1365 | fprintf(stderr, "Adding type %s\n", name); |
| 1366 | if (namespace != NULL) |
| 1367 | fprintf(stderr, " target namespace %s\n", namespace); |
| 1368 | #endif |
| 1369 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1370 | if (schema->typeDecl == NULL) |
| 1371 | schema->typeDecl = xmlHashCreate(10); |
| 1372 | if (schema->typeDecl == NULL) |
| 1373 | return (NULL); |
| 1374 | |
| 1375 | ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType)); |
| 1376 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1377 | xmlSchemaPErrMemory(ctxt, "allocating type", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1378 | return (NULL); |
| 1379 | } |
| 1380 | memset(ret, 0, sizeof(xmlSchemaType)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1381 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 1382 | ret->redef = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1383 | val = xmlHashAddEntry2(schema->typeDecl, name, namespace, ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1384 | if (val != 0) { |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 1385 | if (ctxt->includes == 0) { |
| 1386 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1387 | XML_SCHEMAP_REDEFINED_TYPE, |
| 1388 | "Type %s already defined\n", |
| 1389 | name, NULL); |
| 1390 | xmlFree(ret); |
| 1391 | return (NULL); |
| 1392 | } else { |
| 1393 | xmlSchemaTypePtr prev; |
| 1394 | |
| 1395 | prev = xmlHashLookup2(schema->typeDecl, name, namespace); |
| 1396 | if (prev == NULL) { |
| 1397 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1398 | XML_ERR_INTERNAL_ERROR, |
| 1399 | "Internal error on type %s definition\n", |
| 1400 | name, NULL); |
| 1401 | xmlFree(ret); |
| 1402 | return (NULL); |
| 1403 | } |
| 1404 | ret->redef = prev->redef; |
| 1405 | prev->redef = ret; |
| 1406 | } |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 1407 | } |
| 1408 | ret->minOccurs = 1; |
| 1409 | ret->maxOccurs = 1; |
| 1410 | |
| 1411 | return (ret); |
| 1412 | } |
| 1413 | |
| 1414 | /** |
| 1415 | * xmlSchemaAddGroup: |
| 1416 | * @ctxt: a schema validation context |
| 1417 | * @schema: the schema being built |
| 1418 | * @name: the group name |
| 1419 | * |
| 1420 | * Add an XML schema Group definition |
| 1421 | * |
| 1422 | * Returns the new struture or NULL in case of error |
| 1423 | */ |
| 1424 | static xmlSchemaTypePtr |
| 1425 | xmlSchemaAddGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1426 | const xmlChar * name) |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 1427 | { |
| 1428 | xmlSchemaTypePtr ret = NULL; |
| 1429 | int val; |
| 1430 | |
| 1431 | if ((ctxt == NULL) || (schema == NULL) || (name == NULL)) |
| 1432 | return (NULL); |
| 1433 | |
| 1434 | if (schema->groupDecl == NULL) |
| 1435 | schema->groupDecl = xmlHashCreate(10); |
| 1436 | if (schema->groupDecl == NULL) |
| 1437 | return (NULL); |
| 1438 | |
| 1439 | ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType)); |
| 1440 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1441 | xmlSchemaPErrMemory(ctxt, "adding group", NULL); |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 1442 | return (NULL); |
| 1443 | } |
| 1444 | memset(ret, 0, sizeof(xmlSchemaType)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1445 | ret->name = xmlDictLookup(ctxt->dict, name, -1); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1446 | val = |
| 1447 | xmlHashAddEntry2(schema->groupDecl, name, schema->targetNamespace, |
| 1448 | ret); |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 1449 | if (val != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1450 | xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc, |
| 1451 | XML_SCHEMAP_REDEFINED_GROUP, |
| 1452 | "Group %s already defined\n", |
| 1453 | name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1454 | xmlFree(ret); |
| 1455 | return (NULL); |
| 1456 | } |
| 1457 | ret->minOccurs = 1; |
| 1458 | ret->maxOccurs = 1; |
| 1459 | |
| 1460 | return (ret); |
| 1461 | } |
| 1462 | |
| 1463 | /************************************************************************ |
| 1464 | * * |
| 1465 | * Utilities for parsing * |
| 1466 | * * |
| 1467 | ************************************************************************/ |
| 1468 | |
| 1469 | /** |
| 1470 | * xmlGetQNameProp: |
| 1471 | * @ctxt: a schema validation context |
| 1472 | * @node: a subtree containing XML Schema informations |
| 1473 | * @name: the attribute name |
| 1474 | * @namespace: the result namespace if any |
| 1475 | * |
| 1476 | * Extract a QName Attribute value |
| 1477 | * |
| 1478 | * Returns the NCName or NULL if not found, and also update @namespace |
| 1479 | * with the namespace URI |
| 1480 | */ |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1481 | static const xmlChar * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1482 | xmlGetQNameProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1483 | const char *name, const xmlChar ** namespace) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1484 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1485 | const xmlChar *val; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1486 | xmlNsPtr ns; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1487 | const xmlChar *ret, *prefix; |
| 1488 | int len; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1489 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1490 | *namespace = NULL; |
| 1491 | val = xmlSchemaGetProp(ctxt, node, name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1492 | if (val == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1493 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1494 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1495 | ret = xmlSplitQName3(val, &len); |
| 1496 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1497 | return (val); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1498 | } |
| 1499 | ret = xmlDictLookup(ctxt->dict, ret, -1); |
| 1500 | prefix = xmlDictLookup(ctxt->dict, val, len); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1501 | |
| 1502 | ns = xmlSearchNs(node->doc, node, prefix); |
| 1503 | if (ns == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1504 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_PREFIX_UNDEFINED, |
| 1505 | "Attribute %s: the QName prefix %s is undefined\n", |
| 1506 | (const xmlChar *) name, prefix); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1507 | } else { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1508 | *namespace = xmlDictLookup(ctxt->dict, ns->href, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1509 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1510 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | /** |
| 1514 | * xmlGetMaxOccurs: |
| 1515 | * @ctxt: a schema validation context |
| 1516 | * @node: a subtree containing XML Schema informations |
| 1517 | * |
| 1518 | * Get the maxOccurs property |
| 1519 | * |
| 1520 | * Returns the default if not found, or the value |
| 1521 | */ |
| 1522 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1523 | xmlGetMaxOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node) |
| 1524 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1525 | const xmlChar *val, *cur; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1526 | int ret = 0; |
| 1527 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1528 | val = xmlSchemaGetProp(ctxt, node, "maxOccurs"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1529 | if (val == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1530 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1531 | |
| 1532 | if (xmlStrEqual(val, (const xmlChar *) "unbounded")) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1533 | return (UNBOUNDED); /* encoding it with -1 might be another option */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | cur = val; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1537 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1538 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1539 | while ((*cur >= '0') && (*cur <= '9')) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1540 | ret = ret * 10 + (*cur - '0'); |
| 1541 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1542 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1543 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1544 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1545 | if (*cur != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1546 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_INVALID_MAXOCCURS, |
| 1547 | "invalid value for maxOccurs: %s\n", val, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1548 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1549 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1550 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | /** |
| 1554 | * xmlGetMinOccurs: |
| 1555 | * @ctxt: a schema validation context |
| 1556 | * @node: a subtree containing XML Schema informations |
| 1557 | * |
| 1558 | * Get the minOccurs property |
| 1559 | * |
| 1560 | * Returns the default if not found, or the value |
| 1561 | */ |
| 1562 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1563 | xmlGetMinOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node) |
| 1564 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1565 | const xmlChar *val, *cur; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1566 | int ret = 0; |
| 1567 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1568 | val = xmlSchemaGetProp(ctxt, node, "minOccurs"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1569 | if (val == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1570 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1571 | |
| 1572 | cur = val; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1573 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1574 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1575 | while ((*cur >= '0') && (*cur <= '9')) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1576 | ret = ret * 10 + (*cur - '0'); |
| 1577 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1578 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1579 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1580 | cur++; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1581 | if (*cur != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1582 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_INVALID_MINOCCURS, |
| 1583 | "invalid value for minOccurs: %s\n", val, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1584 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1585 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1586 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | /** |
| 1590 | * xmlGetBooleanProp: |
| 1591 | * @ctxt: a schema validation context |
| 1592 | * @node: a subtree containing XML Schema informations |
| 1593 | * @name: the attribute name |
| 1594 | * @def: the default value |
| 1595 | * |
| 1596 | * Get is a bolean property is set |
| 1597 | * |
| 1598 | * Returns the default if not found, 0 if found to be false, |
| 1599 | * 1 if found to be true |
| 1600 | */ |
| 1601 | static int |
| 1602 | xmlGetBooleanProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1603 | const char *name, int def) |
| 1604 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1605 | const xmlChar *val; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1606 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1607 | val = xmlSchemaGetProp(ctxt, node, name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1608 | if (val == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1609 | return (def); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1610 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1611 | if (xmlStrEqual(val, BAD_CAST "true")) |
| 1612 | def = 1; |
| 1613 | else if (xmlStrEqual(val, BAD_CAST "false")) |
| 1614 | def = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1615 | else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1616 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_INVALID_BOOLEAN, |
| 1617 | "Attribute %s: the value %s is not boolean\n", |
| 1618 | (const xmlChar *) name, val); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1619 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1620 | return (def); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | /************************************************************************ |
| 1624 | * * |
| 1625 | * Shema extraction from an Infoset * |
| 1626 | * * |
| 1627 | ************************************************************************/ |
| 1628 | static xmlSchemaTypePtr xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr |
| 1629 | ctxt, xmlSchemaPtr schema, |
| 1630 | xmlNodePtr node); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1631 | static xmlSchemaTypePtr xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr |
| 1632 | ctxt, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1633 | xmlSchemaPtr schema, |
| 1634 | xmlNodePtr node); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1635 | static xmlSchemaTypePtr xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr |
| 1636 | ctxt, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1637 | xmlSchemaPtr schema, |
| 1638 | xmlNodePtr node, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1639 | int simple); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1640 | static xmlSchemaTypePtr xmlSchemaParseSequence(xmlSchemaParserCtxtPtr ctxt, |
| 1641 | xmlSchemaPtr schema, |
| 1642 | xmlNodePtr node); |
| 1643 | static xmlSchemaTypePtr xmlSchemaParseAll(xmlSchemaParserCtxtPtr ctxt, |
| 1644 | xmlSchemaPtr schema, |
| 1645 | xmlNodePtr node); |
| 1646 | static xmlSchemaAttributePtr xmlSchemaParseAttribute(xmlSchemaParserCtxtPtr |
| 1647 | ctxt, |
| 1648 | xmlSchemaPtr schema, |
| 1649 | xmlNodePtr node); |
| 1650 | static xmlSchemaAttributeGroupPtr |
| 1651 | xmlSchemaParseAttributeGroup(xmlSchemaParserCtxtPtr ctxt, |
| 1652 | xmlSchemaPtr schema, xmlNodePtr node); |
| 1653 | static xmlSchemaTypePtr xmlSchemaParseChoice(xmlSchemaParserCtxtPtr ctxt, |
| 1654 | xmlSchemaPtr schema, |
| 1655 | xmlNodePtr node); |
| 1656 | static xmlSchemaTypePtr xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt, |
| 1657 | xmlSchemaPtr schema, |
| 1658 | xmlNodePtr node); |
| 1659 | static xmlSchemaAttributePtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1660 | xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt, |
| 1661 | xmlSchemaPtr schema, xmlNodePtr node); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1662 | |
| 1663 | /** |
| 1664 | * xmlSchemaParseAttrDecls: |
| 1665 | * @ctxt: a schema validation context |
| 1666 | * @schema: the schema being built |
| 1667 | * @node: a subtree containing XML Schema informations |
| 1668 | * @type: the hosting type |
| 1669 | * |
| 1670 | * parse a XML schema attrDecls declaration corresponding to |
| 1671 | * <!ENTITY % attrDecls |
| 1672 | * '((%attribute;| %attributeGroup;)*,(%anyAttribute;)?)'> |
| 1673 | */ |
| 1674 | static xmlNodePtr |
| 1675 | xmlSchemaParseAttrDecls(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 1676 | xmlNodePtr child, xmlSchemaTypePtr type) |
| 1677 | { |
| 1678 | xmlSchemaAttributePtr lastattr, attr; |
| 1679 | |
| 1680 | lastattr = NULL; |
| 1681 | while ((IS_SCHEMA(child, "attribute")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1682 | (IS_SCHEMA(child, "attributeGroup"))) { |
| 1683 | attr = NULL; |
| 1684 | if (IS_SCHEMA(child, "attribute")) { |
| 1685 | attr = xmlSchemaParseAttribute(ctxt, schema, child); |
| 1686 | } else if (IS_SCHEMA(child, "attributeGroup")) { |
| 1687 | attr = (xmlSchemaAttributePtr) |
| 1688 | xmlSchemaParseAttributeGroup(ctxt, schema, child); |
| 1689 | } |
| 1690 | if (attr != NULL) { |
| 1691 | if (lastattr == NULL) { |
| 1692 | type->attributes = attr; |
| 1693 | lastattr = attr; |
| 1694 | } else { |
| 1695 | lastattr->next = attr; |
| 1696 | lastattr = attr; |
| 1697 | } |
| 1698 | } |
| 1699 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1700 | } |
| 1701 | if (IS_SCHEMA(child, "anyAttribute")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1702 | attr = xmlSchemaParseAnyAttribute(ctxt, schema, child); |
| 1703 | if (attr != NULL) { |
| 1704 | if (lastattr == NULL) { |
| 1705 | type->attributes = attr; |
| 1706 | lastattr = attr; |
| 1707 | } else { |
| 1708 | lastattr->next = attr; |
| 1709 | lastattr = attr; |
| 1710 | } |
| 1711 | } |
| 1712 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1713 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1714 | return (child); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | /** |
| 1718 | * xmlSchemaParseAnnotation: |
| 1719 | * @ctxt: a schema validation context |
| 1720 | * @schema: the schema being built |
| 1721 | * @node: a subtree containing XML Schema informations |
| 1722 | * |
| 1723 | * parse a XML schema Attrribute declaration |
| 1724 | * *WARNING* this interface is highly subject to change |
| 1725 | * |
| 1726 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 1727 | * 1 in case of success. |
| 1728 | */ |
| 1729 | static xmlSchemaAnnotPtr |
| 1730 | xmlSchemaParseAnnotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 1731 | xmlNodePtr node) |
| 1732 | { |
| 1733 | xmlSchemaAnnotPtr ret; |
| 1734 | |
| 1735 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1736 | return (NULL); |
| 1737 | ret = xmlSchemaNewAnnot(ctxt, node); |
| 1738 | |
| 1739 | return (ret); |
| 1740 | } |
| 1741 | |
| 1742 | /** |
| 1743 | * xmlSchemaParseFacet: |
| 1744 | * @ctxt: a schema validation context |
| 1745 | * @schema: the schema being built |
| 1746 | * @node: a subtree containing XML Schema informations |
| 1747 | * |
| 1748 | * parse a XML schema Facet declaration |
| 1749 | * *WARNING* this interface is highly subject to change |
| 1750 | * |
| 1751 | * Returns the new type structure or NULL in case of error |
| 1752 | */ |
| 1753 | static xmlSchemaFacetPtr |
| 1754 | xmlSchemaParseFacet(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1755 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1756 | { |
| 1757 | xmlSchemaFacetPtr facet; |
| 1758 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1759 | const xmlChar *value; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1760 | |
| 1761 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1762 | return (NULL); |
| 1763 | |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 1764 | facet = xmlSchemaNewFacet(); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1765 | if (facet == NULL) { |
| 1766 | xmlSchemaPErrMemory(ctxt, "allocating facet", node); |
| 1767 | return (NULL); |
| 1768 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1769 | facet->node = node; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1770 | value = xmlSchemaGetProp(ctxt, node, "value"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1771 | if (value == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1772 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_FACET_NO_VALUE, |
| 1773 | "Facet %s has no value\n", node->name, NULL); |
| 1774 | xmlSchemaFreeFacet(facet); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1775 | return (NULL); |
| 1776 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1777 | if (IS_SCHEMA(node, "minInclusive")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1778 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1779 | } else if (IS_SCHEMA(node, "minExclusive")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1780 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1781 | } else if (IS_SCHEMA(node, "maxInclusive")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1782 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1783 | } else if (IS_SCHEMA(node, "maxExclusive")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1784 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1785 | } else if (IS_SCHEMA(node, "totalDigits")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1786 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1787 | } else if (IS_SCHEMA(node, "fractionDigits")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1788 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1789 | } else if (IS_SCHEMA(node, "pattern")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1790 | facet->type = XML_SCHEMA_FACET_PATTERN; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1791 | } else if (IS_SCHEMA(node, "enumeration")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1792 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1793 | } else if (IS_SCHEMA(node, "whiteSpace")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1794 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1795 | } else if (IS_SCHEMA(node, "length")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1796 | facet->type = XML_SCHEMA_FACET_LENGTH; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1797 | } else if (IS_SCHEMA(node, "maxLength")) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1798 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
| 1799 | } else if (IS_SCHEMA(node, "minLength")) { |
| 1800 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
| 1801 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1802 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_TYPE, |
| 1803 | "Unknown facet type %s\n", node->name, NULL); |
| 1804 | xmlSchemaFreeFacet(facet); |
| 1805 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1806 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1807 | facet->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1808 | facet->value = value; |
| 1809 | child = node->children; |
| 1810 | |
| 1811 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1812 | facet->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 1813 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1814 | } |
| 1815 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1816 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_CHILD, |
| 1817 | "Facet %s has unexpected child content\n", |
| 1818 | node->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1819 | } |
| 1820 | return (facet); |
| 1821 | } |
| 1822 | |
| 1823 | /** |
| 1824 | * xmlSchemaParseAny: |
| 1825 | * @ctxt: a schema validation context |
| 1826 | * @schema: the schema being built |
| 1827 | * @node: a subtree containing XML Schema informations |
| 1828 | * |
| 1829 | * parse a XML schema Any declaration |
| 1830 | * *WARNING* this interface is highly subject to change |
| 1831 | * |
| 1832 | * Returns the new type structure or NULL in case of error |
| 1833 | */ |
| 1834 | static xmlSchemaTypePtr |
| 1835 | xmlSchemaParseAny(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 1836 | xmlNodePtr node) |
| 1837 | { |
| 1838 | xmlSchemaTypePtr type; |
| 1839 | xmlNodePtr child = NULL; |
| 1840 | xmlChar name[30]; |
| 1841 | |
| 1842 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1843 | return (NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1844 | snprintf((char *) name, 30, "any %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1845 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1846 | if (type == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1847 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1848 | type->node = node; |
| 1849 | type->type = XML_SCHEMA_TYPE_ANY; |
| 1850 | child = node->children; |
| 1851 | type->minOccurs = xmlGetMinOccurs(ctxt, node); |
| 1852 | type->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
| 1853 | |
| 1854 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1855 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 1856 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1857 | } |
| 1858 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1859 | xmlSchemaPErr2(ctxt, node, child, |
| 1860 | XML_SCHEMAP_UNKNOWN_SEQUENCE_CHILD, |
| 1861 | "Sequence %s has unexpected content\n", type->name, |
| 1862 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | return (type); |
| 1866 | } |
| 1867 | |
| 1868 | /** |
| 1869 | * xmlSchemaParseNotation: |
| 1870 | * @ctxt: a schema validation context |
| 1871 | * @schema: the schema being built |
| 1872 | * @node: a subtree containing XML Schema informations |
| 1873 | * |
| 1874 | * parse a XML schema Notation declaration |
| 1875 | * |
| 1876 | * Returns the new structure or NULL in case of error |
| 1877 | */ |
| 1878 | static xmlSchemaNotationPtr |
| 1879 | xmlSchemaParseNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1880 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1881 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1882 | const xmlChar *name; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1883 | xmlSchemaNotationPtr ret; |
| 1884 | xmlNodePtr child = NULL; |
| 1885 | |
| 1886 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1887 | return (NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1888 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1889 | if (name == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1890 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_NOTATION_NO_NAME, |
| 1891 | "Notation has no name\n", NULL, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1892 | return (NULL); |
| 1893 | } |
| 1894 | ret = xmlSchemaAddNotation(ctxt, schema, name); |
| 1895 | if (ret == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1896 | return (NULL); |
| 1897 | } |
| 1898 | child = node->children; |
| 1899 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1900 | ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 1901 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1902 | } |
| 1903 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1904 | xmlSchemaPErr2(ctxt, node, child, |
| 1905 | XML_SCHEMAP_UNKNOWN_NOTATION_CHILD, |
| 1906 | "notation %s has unexpected content\n", name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | return (ret); |
| 1910 | } |
| 1911 | |
| 1912 | /** |
| 1913 | * xmlSchemaParseAnyAttribute: |
| 1914 | * @ctxt: a schema validation context |
| 1915 | * @schema: the schema being built |
| 1916 | * @node: a subtree containing XML Schema informations |
| 1917 | * |
| 1918 | * parse a XML schema AnyAttrribute declaration |
| 1919 | * *WARNING* this interface is highly subject to change |
| 1920 | * |
| 1921 | * Returns an attribute def structure or NULL |
| 1922 | */ |
| 1923 | static xmlSchemaAttributePtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1924 | xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt, |
| 1925 | xmlSchemaPtr schema, xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1926 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1927 | const xmlChar *processContents; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1928 | xmlSchemaAttributePtr ret; |
| 1929 | xmlNodePtr child = NULL; |
| 1930 | char name[100]; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1931 | const xmlChar *local, *ns; |
| 1932 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1933 | |
| 1934 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1935 | return (NULL); |
| 1936 | |
| 1937 | snprintf(name, 99, "anyattr %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1938 | local = xmlSchemaGetNamespace(ctxt, schema, node, BAD_CAST "anyattr", &ns); |
| 1939 | ret = xmlSchemaAddAttribute(ctxt, schema, BAD_CAST name, ns); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1940 | if (ret == NULL) { |
| 1941 | return (NULL); |
| 1942 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1943 | ret->id = xmlSchemaGetProp(ctxt, node, "id"); |
| 1944 | processContents = xmlSchemaGetProp(ctxt, node, "processContents"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1945 | if ((processContents == NULL) |
| 1946 | || (xmlStrEqual(processContents, (const xmlChar *) "strict"))) { |
| 1947 | ret->occurs = XML_SCHEMAS_ANYATTR_STRICT; |
| 1948 | } else if (xmlStrEqual(processContents, (const xmlChar *) "skip")) { |
| 1949 | ret->occurs = XML_SCHEMAS_ANYATTR_SKIP; |
| 1950 | } else if (xmlStrEqual(processContents, (const xmlChar *) "lax")) { |
| 1951 | ret->occurs = XML_SCHEMAS_ANYATTR_LAX; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1952 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1953 | xmlSchemaPErr2(ctxt, node, child, |
| 1954 | XML_SCHEMAP_UNKNOWN_PROCESSCONTENT_CHILD, |
| 1955 | "anyAttribute has unexpected content for processContents: %s\n", |
| 1956 | processContents, NULL); |
| 1957 | ret->occurs = XML_SCHEMAS_ANYATTR_STRICT; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1958 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1959 | |
| 1960 | child = node->children; |
| 1961 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1962 | ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 1963 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1964 | } |
| 1965 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 1966 | xmlSchemaPErr2(ctxt, node, child, |
| 1967 | XML_SCHEMAP_UNKNOWN_ANYATTRIBUTE_CHILD, |
| 1968 | "anyAttribute %s has unexpected content\n", |
| 1969 | (const xmlChar *) name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | return (ret); |
| 1973 | } |
| 1974 | |
| 1975 | |
| 1976 | /** |
| 1977 | * xmlSchemaParseAttribute: |
| 1978 | * @ctxt: a schema validation context |
| 1979 | * @schema: the schema being built |
| 1980 | * @node: a subtree containing XML Schema informations |
| 1981 | * |
| 1982 | * parse a XML schema Attrribute declaration |
| 1983 | * *WARNING* this interface is highly subject to change |
| 1984 | * |
| 1985 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 1986 | * 1 in case of success. |
| 1987 | */ |
| 1988 | static xmlSchemaAttributePtr |
| 1989 | xmlSchemaParseAttribute(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 1990 | xmlNodePtr node) |
| 1991 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1992 | const xmlChar *name, *refNs = NULL, *ref = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1993 | xmlSchemaAttributePtr ret; |
| 1994 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1995 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1996 | |
| 1997 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 1998 | return (NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 1999 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2000 | if (name == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2001 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2002 | ref = xmlGetQNameProp(ctxt, node, "ref", &refNs); |
| 2003 | if (ref == NULL) { |
| 2004 | xmlSchemaPErr2(ctxt, node, child, |
| 2005 | XML_SCHEMAP_ATTR_NONAME_NOREF, |
| 2006 | "Attribute has no name nor ref\n", NULL, NULL); |
| 2007 | return (NULL); |
| 2008 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2009 | if (refNs == NULL) |
| 2010 | refNs = schema->targetNamespace; |
| 2011 | snprintf(buf, 99, "anonattr %d", ctxt->counter++ + 1); |
| 2012 | name = (const xmlChar *) buf; |
| 2013 | ret = xmlSchemaAddAttribute(ctxt, schema, name, NULL); |
| 2014 | } else { |
| 2015 | const xmlChar *local, *ns; |
| 2016 | |
| 2017 | local = xmlSchemaGetNamespace(ctxt, schema, node, name, &ns); |
| 2018 | ret = xmlSchemaAddAttribute(ctxt, schema, local, ns); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2019 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2020 | if (ret == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2021 | return (NULL); |
| 2022 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2023 | ret->ref = ref; |
| 2024 | ret->refNs = refNs; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2025 | if ((ret->targetNamespace != NULL) && |
| 2026 | ((schema->flags & XML_SCHEMAS_QUALIF_ATTR) == 0) && |
| 2027 | (xmlStrEqual(ret->targetNamespace, schema->targetNamespace))) |
| 2028 | ret->flags |= XML_SCHEMAS_ATTR_NSDEFAULT; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2029 | ret->typeName = xmlGetQNameProp(ctxt, node, "type", &(ret->typeNs)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2030 | if ((ret->typeName != NULL) && (ret->typeNs == NULL)) |
| 2031 | ret->typeNs = schema->targetNamespace; |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 2032 | ret->node = node; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2033 | child = node->children; |
| 2034 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2035 | ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2036 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2037 | } |
| 2038 | if (IS_SCHEMA(child, "simpleType")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2039 | ret->subtypes = xmlSchemaParseSimpleType(ctxt, schema, child); |
| 2040 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2041 | } |
| 2042 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2043 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_ATTR_CHILD, |
| 2044 | "attribute %s has unexpected content\n", name, |
| 2045 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | return (ret); |
| 2049 | } |
| 2050 | |
| 2051 | /** |
| 2052 | * xmlSchemaParseAttributeGroup: |
| 2053 | * @ctxt: a schema validation context |
| 2054 | * @schema: the schema being built |
| 2055 | * @node: a subtree containing XML Schema informations |
| 2056 | * |
| 2057 | * parse a XML schema Attribute Group declaration |
| 2058 | * *WARNING* this interface is highly subject to change |
| 2059 | * |
| 2060 | * Returns the attribute group or NULL in case of error. |
| 2061 | */ |
| 2062 | static xmlSchemaAttributeGroupPtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2063 | xmlSchemaParseAttributeGroup(xmlSchemaParserCtxtPtr ctxt, |
| 2064 | xmlSchemaPtr schema, xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2065 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2066 | const xmlChar *name, *refNs = NULL, *ref = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2067 | xmlSchemaAttributeGroupPtr ret; |
| 2068 | xmlSchemaAttributePtr last = NULL, attr; |
| 2069 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2070 | const xmlChar *oldcontainer; |
| 2071 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2072 | |
| 2073 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2074 | return (NULL); |
| 2075 | oldcontainer = ctxt->container; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2076 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2077 | if (name == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2078 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2079 | ref = xmlGetQNameProp(ctxt, node, "ref", &refNs); |
| 2080 | if (ref == NULL) { |
| 2081 | xmlSchemaPErr2(ctxt, node, child, |
| 2082 | XML_SCHEMAP_ATTRGRP_NONAME_NOREF, |
| 2083 | "AttributeGroup has no name nor ref\n", NULL, |
| 2084 | NULL); |
| 2085 | return (NULL); |
| 2086 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2087 | if (refNs == NULL) |
| 2088 | refNs = schema->targetNamespace; |
| 2089 | snprintf(buf, 99, "anonattrgroup %d", ctxt->counter++ + 1); |
| 2090 | name = (const xmlChar *) buf; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2091 | if (name == NULL) { |
| 2092 | xmlSchemaPErrMemory(ctxt, "creating attribute group", node); |
| 2093 | return (NULL); |
| 2094 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2095 | } |
| 2096 | ret = xmlSchemaAddAttributeGroup(ctxt, schema, name); |
| 2097 | if (ret == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2098 | return (NULL); |
| 2099 | } |
| 2100 | ret->ref = ref; |
| 2101 | ret->refNs = refNs; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 2102 | ret->type = XML_SCHEMA_TYPE_ATTRIBUTEGROUP; |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 2103 | ret->node = node; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2104 | child = node->children; |
| 2105 | ctxt->container = name; |
| 2106 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2107 | ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2108 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2109 | } |
| 2110 | while ((IS_SCHEMA(child, "attribute")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2111 | (IS_SCHEMA(child, "attributeGroup"))) { |
| 2112 | attr = NULL; |
| 2113 | if (IS_SCHEMA(child, "attribute")) { |
| 2114 | attr = xmlSchemaParseAttribute(ctxt, schema, child); |
| 2115 | } else if (IS_SCHEMA(child, "attributeGroup")) { |
| 2116 | attr = (xmlSchemaAttributePtr) |
| 2117 | xmlSchemaParseAttributeGroup(ctxt, schema, child); |
| 2118 | } |
| 2119 | if (attr != NULL) { |
| 2120 | if (last == NULL) { |
| 2121 | ret->attributes = attr; |
| 2122 | last = attr; |
| 2123 | } else { |
| 2124 | last->next = attr; |
| 2125 | last = attr; |
| 2126 | } |
| 2127 | } |
| 2128 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2129 | } |
| 2130 | if (IS_SCHEMA(child, "anyAttribute")) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2131 | TODO |
| 2132 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2133 | } |
| 2134 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2135 | xmlSchemaPErr2(ctxt, node, child, |
| 2136 | XML_SCHEMAP_UNKNOWN_ATTRGRP_CHILD, |
| 2137 | "attribute group %s has unexpected content\n", name, |
| 2138 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2139 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2140 | ctxt->container = oldcontainer; |
| 2141 | return (ret); |
| 2142 | } |
| 2143 | |
| 2144 | /** |
| 2145 | * xmlSchemaParseElement: |
| 2146 | * @ctxt: a schema validation context |
| 2147 | * @schema: the schema being built |
| 2148 | * @node: a subtree containing XML Schema informations |
| 2149 | * |
| 2150 | * parse a XML schema Element declaration |
| 2151 | * *WARNING* this interface is highly subject to change |
| 2152 | * |
| 2153 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2154 | * 1 in case of success. |
| 2155 | */ |
| 2156 | static xmlSchemaElementPtr |
| 2157 | xmlSchemaParseElement(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 2158 | xmlNodePtr node, int toplevel) |
| 2159 | { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2160 | const xmlChar *name, *fixed; |
| 2161 | const xmlChar *refNs = NULL, *ref = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2162 | xmlSchemaElementPtr ret; |
| 2163 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2164 | const xmlChar *oldcontainer; |
| 2165 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2166 | |
| 2167 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2168 | return (NULL); |
| 2169 | oldcontainer = ctxt->container; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2170 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2171 | if (name == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2172 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2173 | ref = xmlGetQNameProp(ctxt, node, "ref", &refNs); |
| 2174 | if (ref == NULL) { |
| 2175 | xmlSchemaPErr2(ctxt, node, child, |
| 2176 | XML_SCHEMAP_ELEM_NONAME_NOREF, |
| 2177 | "Element has no name nor ref\n", NULL, NULL); |
| 2178 | return (NULL); |
| 2179 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2180 | if (refNs == NULL) |
| 2181 | refNs = schema->targetNamespace; |
| 2182 | snprintf(buf, 99, "anonelem %d", ctxt->counter++ + 1); |
| 2183 | name = (const xmlChar *) buf; |
| 2184 | ret = xmlSchemaAddElement(ctxt, schema, name, NULL); |
| 2185 | } else { |
| 2186 | const xmlChar *local, *ns; |
| 2187 | |
| 2188 | local = xmlSchemaGetNamespace(ctxt, schema, node, name, &ns); |
| 2189 | ret = xmlSchemaAddElement(ctxt, schema, local, ns); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2190 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2191 | if (ret != NULL) |
| 2192 | ret->node = node; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2193 | if (ret == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2194 | return (NULL); |
| 2195 | } |
| 2196 | ret->type = XML_SCHEMA_TYPE_ELEMENT; |
| 2197 | ret->ref = ref; |
| 2198 | ret->refNs = refNs; |
| 2199 | if (ref != NULL) |
| 2200 | ret->flags |= XML_SCHEMAS_ELEM_REF; |
| 2201 | if (toplevel) |
| 2202 | ret->flags |= XML_SCHEMAS_ELEM_TOPLEVEL; |
| 2203 | if (xmlGetBooleanProp(ctxt, node, "nillable", 0)) |
| 2204 | ret->flags |= XML_SCHEMAS_ELEM_NILLABLE; |
| 2205 | if (xmlGetBooleanProp(ctxt, node, "abstract", 0)) |
| 2206 | ret->flags |= XML_SCHEMAS_ELEM_NILLABLE; |
| 2207 | ctxt->container = name; |
| 2208 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2209 | ret->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2210 | ret->namedType = |
| 2211 | xmlGetQNameProp(ctxt, node, "type", &(ret->namedTypeNs)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2212 | if ((ret->namedType != NULL) && (ret->namedTypeNs == NULL)) |
| 2213 | ret->namedTypeNs = schema->targetNamespace; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2214 | ret->substGroup = |
| 2215 | xmlGetQNameProp(ctxt, node, "substitutionGroup", |
| 2216 | &(ret->substGroupNs)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2217 | fixed = xmlSchemaGetProp(ctxt, node, "fixed"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2218 | ret->minOccurs = xmlGetMinOccurs(ctxt, node); |
| 2219 | ret->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2220 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2221 | ret->value = xmlSchemaGetProp(ctxt, node, "default"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2222 | if ((ret->value != NULL) && (fixed != NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2223 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_ELEM_DEFAULT_FIXED, |
| 2224 | "Element %s has both default and fixed\n", |
| 2225 | ret->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2226 | } else if (fixed != NULL) { |
| 2227 | ret->flags |= XML_SCHEMAS_ELEM_FIXED; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2228 | ret->value = fixed; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
| 2231 | child = node->children; |
| 2232 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2233 | ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2234 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2235 | } |
| 2236 | if (IS_SCHEMA(child, "complexType")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2237 | ret->subtypes = xmlSchemaParseComplexType(ctxt, schema, child); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2238 | child = child->next; |
| 2239 | } else if (IS_SCHEMA(child, "simpleType")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2240 | ret->subtypes = xmlSchemaParseSimpleType(ctxt, schema, child); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2241 | child = child->next; |
| 2242 | } |
| 2243 | while ((IS_SCHEMA(child, "unique")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2244 | (IS_SCHEMA(child, "key")) || (IS_SCHEMA(child, "keyref"))) { |
| 2245 | TODO child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2246 | } |
| 2247 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2248 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_ELEM_CHILD, |
| 2249 | "element %s has unexpected content\n", name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | ctxt->container = oldcontainer; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2253 | return (ret); |
| 2254 | } |
| 2255 | |
| 2256 | /** |
| 2257 | * xmlSchemaParseUnion: |
| 2258 | * @ctxt: a schema validation context |
| 2259 | * @schema: the schema being built |
| 2260 | * @node: a subtree containing XML Schema informations |
| 2261 | * |
| 2262 | * parse a XML schema Union definition |
| 2263 | * *WARNING* this interface is highly subject to change |
| 2264 | * |
| 2265 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2266 | * 1 in case of success. |
| 2267 | */ |
| 2268 | static xmlSchemaTypePtr |
| 2269 | xmlSchemaParseUnion(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2270 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2271 | { |
| 2272 | xmlSchemaTypePtr type, subtype, last = NULL; |
| 2273 | xmlNodePtr child = NULL; |
| 2274 | xmlChar name[30]; |
| 2275 | |
| 2276 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2277 | return (NULL); |
| 2278 | |
| 2279 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2280 | snprintf((char *) name, 30, "union %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2281 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2282 | if (type == NULL) |
| 2283 | return (NULL); |
| 2284 | type->node = node; |
| 2285 | type->type = XML_SCHEMA_TYPE_LIST; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2286 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
| 2287 | type->ref = xmlSchemaGetProp(ctxt, node, "memberTypes"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2288 | |
| 2289 | child = node->children; |
| 2290 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2291 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2292 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2293 | } |
| 2294 | while (IS_SCHEMA(child, "simpleType")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2295 | subtype = (xmlSchemaTypePtr) |
| 2296 | xmlSchemaParseSimpleType(ctxt, schema, child); |
| 2297 | if (subtype != NULL) { |
| 2298 | if (last == NULL) { |
| 2299 | type->subtypes = subtype; |
| 2300 | last = subtype; |
| 2301 | } else { |
| 2302 | last->next = subtype; |
| 2303 | last = subtype; |
| 2304 | } |
| 2305 | last->next = NULL; |
| 2306 | } |
| 2307 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2308 | } |
| 2309 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2310 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_UNION_CHILD, |
| 2311 | "Union %s has unexpected content\n", type->name, |
| 2312 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2313 | } |
| 2314 | return (type); |
| 2315 | } |
| 2316 | |
| 2317 | /** |
| 2318 | * xmlSchemaParseList: |
| 2319 | * @ctxt: a schema validation context |
| 2320 | * @schema: the schema being built |
| 2321 | * @node: a subtree containing XML Schema informations |
| 2322 | * |
| 2323 | * parse a XML schema List definition |
| 2324 | * *WARNING* this interface is highly subject to change |
| 2325 | * |
| 2326 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2327 | * 1 in case of success. |
| 2328 | */ |
| 2329 | static xmlSchemaTypePtr |
| 2330 | xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2331 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2332 | { |
| 2333 | xmlSchemaTypePtr type, subtype; |
| 2334 | xmlNodePtr child = NULL; |
| 2335 | xmlChar name[30]; |
| 2336 | |
| 2337 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2338 | return (NULL); |
| 2339 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2340 | snprintf((char *) name, 30, "list %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2341 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2342 | if (type == NULL) |
| 2343 | return (NULL); |
| 2344 | type->node = node; |
| 2345 | type->type = XML_SCHEMA_TYPE_LIST; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2346 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2347 | type->ref = xmlGetQNameProp(ctxt, node, "ref", &(type->refNs)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2348 | if ((type->ref != NULL) && (type->refNs == NULL)) |
| 2349 | type->refNs = schema->targetNamespace; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2350 | |
| 2351 | child = node->children; |
| 2352 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2353 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2354 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2355 | } |
| 2356 | subtype = NULL; |
| 2357 | if (IS_SCHEMA(child, "simpleType")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2358 | subtype = (xmlSchemaTypePtr) |
| 2359 | xmlSchemaParseSimpleType(ctxt, schema, child); |
| 2360 | child = child->next; |
| 2361 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2362 | } |
| 2363 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2364 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_LIST_CHILD, |
| 2365 | "List %s has unexpected content\n", type->name, |
| 2366 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2367 | } |
| 2368 | return (type); |
| 2369 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2370 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2371 | /** |
| 2372 | * xmlSchemaParseSimpleType: |
| 2373 | * @ctxt: a schema validation context |
| 2374 | * @schema: the schema being built |
| 2375 | * @node: a subtree containing XML Schema informations |
| 2376 | * |
| 2377 | * parse a XML schema Simple Type definition |
| 2378 | * *WARNING* this interface is highly subject to change |
| 2379 | * |
| 2380 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2381 | * 1 in case of success. |
| 2382 | */ |
| 2383 | static xmlSchemaTypePtr |
| 2384 | xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 2385 | xmlNodePtr node) |
| 2386 | { |
| 2387 | xmlSchemaTypePtr type, subtype; |
| 2388 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2389 | const xmlChar *name; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2390 | |
| 2391 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2392 | return (NULL); |
| 2393 | |
| 2394 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2395 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2396 | if (name == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2397 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2398 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2399 | snprintf(buf, 99, "simpletype %d", ctxt->counter++ + 1); |
| 2400 | type = xmlSchemaAddType(ctxt, schema, (const xmlChar *)buf, NULL); |
| 2401 | } else { |
| 2402 | const xmlChar *local, *ns; |
| 2403 | |
| 2404 | local = xmlSchemaGetNamespace(ctxt, schema, node, name, &ns); |
| 2405 | type = xmlSchemaAddType(ctxt, schema, local, ns); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2406 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2407 | if (type == NULL) |
| 2408 | return (NULL); |
| 2409 | type->node = node; |
| 2410 | type->type = XML_SCHEMA_TYPE_SIMPLE; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2411 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2412 | |
| 2413 | child = node->children; |
| 2414 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2415 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2416 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2417 | } |
| 2418 | subtype = NULL; |
| 2419 | if (IS_SCHEMA(child, "restriction")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2420 | subtype = (xmlSchemaTypePtr) |
| 2421 | xmlSchemaParseRestriction(ctxt, schema, child, 1); |
| 2422 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2423 | } else if (IS_SCHEMA(child, "list")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2424 | subtype = (xmlSchemaTypePtr) |
| 2425 | xmlSchemaParseList(ctxt, schema, child); |
| 2426 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2427 | } else if (IS_SCHEMA(child, "union")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2428 | subtype = (xmlSchemaTypePtr) |
| 2429 | xmlSchemaParseUnion(ctxt, schema, child); |
| 2430 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2431 | } |
| 2432 | type->subtypes = subtype; |
| 2433 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2434 | xmlSchemaPErr2(ctxt, node, child, |
| 2435 | XML_SCHEMAP_UNKNOWN_SIMPLETYPE_CHILD, |
| 2436 | "SimpleType %s has unexpected content\n", |
| 2437 | type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2438 | } |
| 2439 | |
| 2440 | return (type); |
| 2441 | } |
| 2442 | |
| 2443 | |
| 2444 | /** |
| 2445 | * xmlSchemaParseGroup: |
| 2446 | * @ctxt: a schema validation context |
| 2447 | * @schema: the schema being built |
| 2448 | * @node: a subtree containing XML Schema informations |
| 2449 | * |
| 2450 | * parse a XML schema Group definition |
| 2451 | * *WARNING* this interface is highly subject to change |
| 2452 | * |
| 2453 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2454 | * 1 in case of success. |
| 2455 | */ |
| 2456 | static xmlSchemaTypePtr |
| 2457 | xmlSchemaParseGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2458 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2459 | { |
| 2460 | xmlSchemaTypePtr type, subtype; |
| 2461 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2462 | const xmlChar *name; |
| 2463 | const xmlChar *ref = NULL, *refNs = NULL; |
| 2464 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2465 | |
| 2466 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2467 | return (NULL); |
| 2468 | |
| 2469 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2470 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2471 | if (name == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2472 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2473 | ref = xmlGetQNameProp(ctxt, node, "ref", &refNs); |
| 2474 | if (ref == NULL) { |
| 2475 | xmlSchemaPErr2(ctxt, node, child, |
| 2476 | XML_SCHEMAP_GROUP_NONAME_NOREF, |
| 2477 | "Group has no name nor ref\n", NULL, NULL); |
| 2478 | return (NULL); |
| 2479 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2480 | if (refNs == NULL) |
| 2481 | refNs = schema->targetNamespace; |
| 2482 | snprintf(buf, 99, "anongroup %d", ctxt->counter++ + 1); |
| 2483 | name = (const xmlChar *) buf; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2484 | } |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 2485 | type = xmlSchemaAddGroup(ctxt, schema, name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2486 | if (type == NULL) |
| 2487 | return (NULL); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2488 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2489 | type->node = node; |
| 2490 | type->type = XML_SCHEMA_TYPE_GROUP; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2491 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2492 | type->ref = ref; |
| 2493 | type->refNs = refNs; |
| 2494 | type->minOccurs = xmlGetMinOccurs(ctxt, node); |
| 2495 | type->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
| 2496 | |
| 2497 | child = node->children; |
| 2498 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2499 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2500 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2501 | } |
| 2502 | subtype = NULL; |
| 2503 | if (IS_SCHEMA(child, "all")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2504 | subtype = (xmlSchemaTypePtr) |
| 2505 | xmlSchemaParseAll(ctxt, schema, child); |
| 2506 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2507 | } else if (IS_SCHEMA(child, "choice")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2508 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 2509 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2510 | } else if (IS_SCHEMA(child, "sequence")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2511 | subtype = (xmlSchemaTypePtr) |
| 2512 | xmlSchemaParseSequence(ctxt, schema, child); |
| 2513 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2514 | } |
| 2515 | if (subtype != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2516 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2517 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2518 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_GROUP_CHILD, |
| 2519 | "Group %s has unexpected content\n", type->name, |
| 2520 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2521 | } |
| 2522 | |
| 2523 | return (type); |
| 2524 | } |
| 2525 | |
| 2526 | /** |
| 2527 | * xmlSchemaParseAll: |
| 2528 | * @ctxt: a schema validation context |
| 2529 | * @schema: the schema being built |
| 2530 | * @node: a subtree containing XML Schema informations |
| 2531 | * |
| 2532 | * parse a XML schema All definition |
| 2533 | * *WARNING* this interface is highly subject to change |
| 2534 | * |
| 2535 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2536 | * 1 in case of success. |
| 2537 | */ |
| 2538 | static xmlSchemaTypePtr |
| 2539 | xmlSchemaParseAll(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2540 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2541 | { |
| 2542 | xmlSchemaTypePtr type, subtype, last = NULL; |
| 2543 | xmlNodePtr child = NULL; |
| 2544 | xmlChar name[30]; |
| 2545 | |
| 2546 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2547 | return (NULL); |
| 2548 | |
| 2549 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2550 | snprintf((char *) name, 30, "all%d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2551 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2552 | if (type == NULL) |
| 2553 | return (NULL); |
| 2554 | type->node = node; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 2555 | type->type = XML_SCHEMA_TYPE_ALL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2556 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2557 | type->minOccurs = xmlGetMinOccurs(ctxt, node); |
William M. Brack | b15351e | 2003-12-27 04:34:42 +0000 | [diff] [blame] | 2558 | if (type->minOccurs > 1) |
| 2559 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_INVALID_MINOCCURS, |
| 2560 | "invalid value for minOccurs (must be 0 or 1)\n", NULL, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2561 | type->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
William M. Brack | b15351e | 2003-12-27 04:34:42 +0000 | [diff] [blame] | 2562 | if (type->maxOccurs > 1) |
| 2563 | xmlSchemaPErr(ctxt, node, XML_SCHEMAP_INVALID_MAXOCCURS, |
| 2564 | "invalid value for maxOccurs (must be 0 or 1)\n", NULL, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2565 | |
| 2566 | child = node->children; |
| 2567 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2568 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2569 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2570 | } |
| 2571 | while (IS_SCHEMA(child, "element")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2572 | subtype = (xmlSchemaTypePtr) |
| 2573 | xmlSchemaParseElement(ctxt, schema, child, 0); |
| 2574 | if (subtype != NULL) { |
William M. Brack | b15351e | 2003-12-27 04:34:42 +0000 | [diff] [blame] | 2575 | if (subtype->minOccurs > 1) |
| 2576 | xmlSchemaPErr(ctxt, child, XML_SCHEMAP_INVALID_MINOCCURS, |
| 2577 | "invalid value for minOccurs (must be 0 or 1)\n", |
| 2578 | NULL, NULL); |
| 2579 | if (subtype->maxOccurs > 1) |
| 2580 | xmlSchemaPErr(ctxt, child, XML_SCHEMAP_INVALID_MAXOCCURS, |
| 2581 | "invalid value for maxOccurs (must be 0 or 1)\n", |
| 2582 | NULL, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2583 | if (last == NULL) { |
| 2584 | type->subtypes = subtype; |
| 2585 | last = subtype; |
| 2586 | } else { |
| 2587 | last->next = subtype; |
| 2588 | last = subtype; |
| 2589 | } |
| 2590 | last->next = NULL; |
| 2591 | } |
| 2592 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2593 | } |
| 2594 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2595 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_ALL_CHILD, |
| 2596 | "All %s has unexpected content\n", type->name, |
| 2597 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
| 2600 | return (type); |
| 2601 | } |
| 2602 | |
| 2603 | /** |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2604 | * xmlSchemaImportSchema |
| 2605 | * |
| 2606 | * @ctxt: a schema validation context |
| 2607 | * @schemaLocation: an URI defining where to find the imported schema |
| 2608 | * |
| 2609 | * import a XML schema |
| 2610 | * *WARNING* this interface is highly subject to change |
| 2611 | * |
| 2612 | * Returns -1 in case of error and 1 in case of success. |
| 2613 | */ |
| 2614 | static xmlSchemaImportPtr |
| 2615 | xmlSchemaImportSchema(xmlSchemaParserCtxtPtr ctxt, |
| 2616 | const xmlChar *schemaLocation) |
| 2617 | { |
| 2618 | xmlSchemaImportPtr import; |
| 2619 | xmlSchemaParserCtxtPtr newctxt; |
| 2620 | |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2621 | newctxt = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt)); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2622 | if (newctxt == NULL) { |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2623 | xmlSchemaPErrMemory(ctxt, "allocating schama parser context", |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2624 | NULL); |
| 2625 | return (NULL); |
| 2626 | } |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2627 | memset(newctxt, 0, sizeof(xmlSchemaParserCtxt)); |
| 2628 | /* Keep the same dictionnary for parsing, really */ |
| 2629 | xmlDictReference(ctxt->dict); |
| 2630 | newctxt->dict = ctxt->dict; |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 2631 | newctxt->includes = 0; |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2632 | newctxt->URL = xmlDictLookup(newctxt->dict, schemaLocation, -1); |
| 2633 | |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2634 | xmlSchemaSetParserErrors(newctxt, ctxt->error, ctxt->warning, |
| 2635 | ctxt->userData); |
| 2636 | |
| 2637 | import = (xmlSchemaImport*) xmlMalloc(sizeof(xmlSchemaImport)); |
| 2638 | if (import == NULL) { |
| 2639 | xmlSchemaPErrMemory(NULL, "allocating imported schema", |
| 2640 | NULL); |
| 2641 | xmlSchemaFreeParserCtxt(newctxt); |
| 2642 | return (NULL); |
| 2643 | } |
| 2644 | |
| 2645 | memset(import, 0, sizeof(xmlSchemaImport)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2646 | import->schemaLocation = xmlDictLookup(ctxt->dict, schemaLocation, -1); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2647 | import->schema = xmlSchemaParse(newctxt); |
| 2648 | |
| 2649 | if (import->schema == NULL) { |
| 2650 | /* FIXME use another error enum here ? */ |
| 2651 | xmlSchemaPErr(ctxt, NULL, XML_SCHEMAS_ERR_INTERNAL, |
| 2652 | "failed to import schema at location %s\n", |
| 2653 | schemaLocation, NULL); |
| 2654 | |
| 2655 | xmlSchemaFreeParserCtxt(newctxt); |
| 2656 | if (import->schemaLocation != NULL) |
| 2657 | xmlFree((xmlChar *)import->schemaLocation); |
| 2658 | xmlFree(import); |
| 2659 | return NULL; |
| 2660 | } |
| 2661 | |
| 2662 | xmlSchemaFreeParserCtxt(newctxt); |
| 2663 | return import; |
| 2664 | } |
| 2665 | |
| 2666 | |
| 2667 | /** |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2668 | * xmlSchemaParseImport: |
| 2669 | * @ctxt: a schema validation context |
| 2670 | * @schema: the schema being built |
| 2671 | * @node: a subtree containing XML Schema informations |
| 2672 | * |
| 2673 | * parse a XML schema Import definition |
| 2674 | * *WARNING* this interface is highly subject to change |
| 2675 | * |
| 2676 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2677 | * 1 in case of success. |
| 2678 | */ |
| 2679 | static int |
| 2680 | xmlSchemaParseImport(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2681 | xmlNodePtr node) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2682 | { |
| 2683 | xmlNodePtr child = NULL; |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2684 | xmlSchemaImportPtr import = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2685 | const xmlChar *namespace; |
| 2686 | const xmlChar *schemaLocation; |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2687 | const xmlChar *previous; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2688 | xmlURIPtr check; |
| 2689 | |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2690 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2691 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2692 | return (-1); |
| 2693 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2694 | namespace = xmlSchemaGetProp(ctxt, node, "namespace"); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2695 | if (namespace != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2696 | check = xmlParseURI((const char *) namespace); |
| 2697 | if (check == NULL) { |
| 2698 | xmlSchemaPErr2(ctxt, node, child, |
| 2699 | XML_SCHEMAP_IMPORT_NAMESPACE_NOT_URI, |
| 2700 | "Import namespace attribute is not an URI: %s\n", |
| 2701 | namespace, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2702 | return (-1); |
| 2703 | } else { |
| 2704 | xmlFreeURI(check); |
| 2705 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2706 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2707 | schemaLocation = xmlSchemaGetProp(ctxt, node, "schemaLocation"); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2708 | if (schemaLocation != NULL) { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2709 | xmlChar *base = NULL; |
| 2710 | xmlChar *URI = NULL; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2711 | check = xmlParseURI((const char *) schemaLocation); |
| 2712 | if (check == NULL) { |
| 2713 | xmlSchemaPErr2(ctxt, node, child, |
| 2714 | XML_SCHEMAP_IMPORT_SCHEMA_NOT_URI, |
| 2715 | "Import schemaLocation attribute is not an URI: %s\n", |
| 2716 | schemaLocation, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2717 | return (-1); |
| 2718 | } else { |
| 2719 | xmlFreeURI(check); |
| 2720 | } |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2721 | base = xmlNodeGetBase(node->doc, node); |
| 2722 | if (base == NULL) { |
| 2723 | URI = xmlBuildURI(schemaLocation, node->doc->URL); |
| 2724 | } else { |
| 2725 | URI = xmlBuildURI(schemaLocation, base); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2726 | xmlFree(base); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2727 | } |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2728 | if (URI != NULL) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 2729 | schemaLocation = xmlDictLookup(ctxt->dict, URI, -1); |
| 2730 | xmlFree(URI); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2731 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2732 | } |
| 2733 | if (schema->schemasImports == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2734 | schema->schemasImports = xmlHashCreate(10); |
| 2735 | if (schema->schemasImports == NULL) { |
| 2736 | xmlSchemaPErr2(ctxt, node, child, |
| 2737 | XML_SCHEMAP_FAILED_BUILD_IMPORT, |
| 2738 | "Internal: failed to build import table\n", |
| 2739 | NULL, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2740 | return (-1); |
| 2741 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2742 | } |
| 2743 | if (namespace == NULL) { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2744 | import = xmlHashLookup(schema->schemasImports, |
| 2745 | XML_SCHEMAS_DEFAULT_NAMESPACE); |
| 2746 | if (import != NULL) |
| 2747 | previous = import->schemaLocation; |
| 2748 | else |
| 2749 | previous = NULL; |
| 2750 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2751 | if (schemaLocation != NULL) { |
| 2752 | if (previous != NULL) { |
| 2753 | if (!xmlStrEqual(schemaLocation, previous)) { |
| 2754 | xmlSchemaPErr2(ctxt, node, child, |
| 2755 | XML_SCHEMAP_IMPORT_REDEFINE_NSNAME, |
| 2756 | "Redefining import for default namespace with a different URI: %s\n", |
| 2757 | schemaLocation, NULL); |
| 2758 | } |
| 2759 | } else { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2760 | import = xmlSchemaImportSchema(ctxt, schemaLocation); |
| 2761 | if (import == NULL) { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2762 | return (-1); |
| 2763 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2764 | xmlHashAddEntry(schema->schemasImports, |
| 2765 | XML_SCHEMAS_DEFAULT_NAMESPACE, |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2766 | import); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2767 | } |
| 2768 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2769 | } else { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2770 | import = xmlHashLookup(schema->schemasImports, namespace); |
| 2771 | if (import != NULL) |
| 2772 | previous = import->schemaLocation; |
| 2773 | else |
| 2774 | previous = NULL; |
| 2775 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2776 | if (schemaLocation != NULL) { |
| 2777 | if (previous != NULL) { |
| 2778 | if (!xmlStrEqual(schemaLocation, previous)) { |
| 2779 | xmlSchemaPErr2(ctxt, node, child, |
| 2780 | XML_SCHEMAP_IMPORT_REDEFINE_NSNAME, |
| 2781 | "Redefining import for namespace %s with a different URI: %s\n", |
| 2782 | namespace, schemaLocation); |
| 2783 | } |
| 2784 | } else { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2785 | import = xmlSchemaImportSchema(ctxt, schemaLocation); |
| 2786 | if (import == NULL) { |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2787 | return (-1); |
| 2788 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2789 | xmlHashAddEntry(schema->schemasImports, |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 2790 | namespace, import); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2791 | } |
| 2792 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2793 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2794 | |
| 2795 | child = node->children; |
| 2796 | while (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2797 | /* |
| 2798 | * the annotations here are simply discarded ... |
| 2799 | */ |
| 2800 | child = child->next; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2801 | } |
| 2802 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2803 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_IMPORT_CHILD, |
| 2804 | "Import has unexpected content\n", NULL, NULL); |
| 2805 | return (-1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2806 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2807 | return (1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | /** |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2811 | * xmlSchemaCleanupDoc: |
| 2812 | * @ctxt: a schema validation context |
| 2813 | * @node: the root of the document. |
| 2814 | * |
| 2815 | * removes unwanted nodes in a schemas document tree |
| 2816 | */ |
| 2817 | static void |
| 2818 | xmlSchemaCleanupDoc(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr root) |
| 2819 | { |
| 2820 | xmlNodePtr delete, cur; |
| 2821 | |
| 2822 | if ((ctxt == NULL) || (root == NULL)) return; |
| 2823 | |
| 2824 | /* |
| 2825 | * Remove all the blank text nodes |
| 2826 | */ |
| 2827 | delete = NULL; |
| 2828 | cur = root; |
| 2829 | while (cur != NULL) { |
| 2830 | if (delete != NULL) { |
| 2831 | xmlUnlinkNode(delete); |
| 2832 | xmlFreeNode(delete); |
| 2833 | delete = NULL; |
| 2834 | } |
| 2835 | if (cur->type == XML_TEXT_NODE) { |
| 2836 | if (IS_BLANK_NODE(cur)) { |
| 2837 | if (xmlNodeGetSpacePreserve(cur) != 1) { |
| 2838 | delete = cur; |
| 2839 | } |
| 2840 | } |
| 2841 | } else if ((cur->type != XML_ELEMENT_NODE) && |
| 2842 | (cur->type != XML_CDATA_SECTION_NODE)) { |
| 2843 | delete = cur; |
| 2844 | goto skip_children; |
| 2845 | } |
| 2846 | |
| 2847 | /* |
| 2848 | * Skip to next node |
| 2849 | */ |
| 2850 | if (cur->children != NULL) { |
| 2851 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 2852 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 2853 | (cur->children->type != XML_ENTITY_NODE)) { |
| 2854 | cur = cur->children; |
| 2855 | continue; |
| 2856 | } |
| 2857 | } |
| 2858 | skip_children: |
| 2859 | if (cur->next != NULL) { |
| 2860 | cur = cur->next; |
| 2861 | continue; |
| 2862 | } |
| 2863 | |
| 2864 | do { |
| 2865 | cur = cur->parent; |
| 2866 | if (cur == NULL) |
| 2867 | break; |
| 2868 | if (cur == root) { |
| 2869 | cur = NULL; |
| 2870 | break; |
| 2871 | } |
| 2872 | if (cur->next != NULL) { |
| 2873 | cur = cur->next; |
| 2874 | break; |
| 2875 | } |
| 2876 | } while (cur != NULL); |
| 2877 | } |
| 2878 | if (delete != NULL) { |
| 2879 | xmlUnlinkNode(delete); |
| 2880 | xmlFreeNode(delete); |
| 2881 | delete = NULL; |
| 2882 | } |
| 2883 | } |
| 2884 | |
| 2885 | /** |
| 2886 | * xmlSchemaParseSchemaTopLevel: |
| 2887 | * @ctxt: a schema validation context |
| 2888 | * @schema: the schemas |
| 2889 | * @nodes: the list of top level nodes |
| 2890 | * |
| 2891 | * Returns the internal XML Schema structure built from the resource or |
| 2892 | * NULL in case of error |
| 2893 | */ |
| 2894 | static void |
| 2895 | xmlSchemaParseSchemaTopLevel(xmlSchemaParserCtxtPtr ctxt, |
| 2896 | xmlSchemaPtr schema, xmlNodePtr nodes) |
| 2897 | { |
| 2898 | xmlNodePtr child; |
| 2899 | xmlSchemaAnnotPtr annot; |
| 2900 | |
| 2901 | if ((ctxt == NULL) || (schema == NULL) || (nodes == NULL)) |
| 2902 | return; |
| 2903 | |
| 2904 | child = nodes; |
| 2905 | while ((IS_SCHEMA(child, "include")) || |
| 2906 | (IS_SCHEMA(child, "import")) || |
| 2907 | (IS_SCHEMA(child, "redefine")) || |
| 2908 | (IS_SCHEMA(child, "annotation"))) { |
| 2909 | if (IS_SCHEMA(child, "annotation")) { |
| 2910 | annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2911 | if (schema->annot == NULL) |
| 2912 | schema->annot = annot; |
| 2913 | else |
| 2914 | xmlSchemaFreeAnnot(annot); |
| 2915 | } else if (IS_SCHEMA(child, "import")) { |
| 2916 | xmlSchemaParseImport(ctxt, schema, child); |
| 2917 | } else if (IS_SCHEMA(child, "include")) { |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 2918 | ctxt->includes++; |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2919 | xmlSchemaParseInclude(ctxt, schema, child); |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 2920 | ctxt->includes--; |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 2921 | } else if (IS_SCHEMA(child, "redefine")) { |
| 2922 | TODO |
| 2923 | } |
| 2924 | child = child->next; |
| 2925 | } |
| 2926 | while (child != NULL) { |
| 2927 | if (IS_SCHEMA(child, "complexType")) { |
| 2928 | xmlSchemaParseComplexType(ctxt, schema, child); |
| 2929 | child = child->next; |
| 2930 | } else if (IS_SCHEMA(child, "simpleType")) { |
| 2931 | xmlSchemaParseSimpleType(ctxt, schema, child); |
| 2932 | child = child->next; |
| 2933 | } else if (IS_SCHEMA(child, "element")) { |
| 2934 | xmlSchemaParseElement(ctxt, schema, child, 1); |
| 2935 | child = child->next; |
| 2936 | } else if (IS_SCHEMA(child, "attribute")) { |
| 2937 | xmlSchemaParseAttribute(ctxt, schema, child); |
| 2938 | child = child->next; |
| 2939 | } else if (IS_SCHEMA(child, "attributeGroup")) { |
| 2940 | xmlSchemaParseAttributeGroup(ctxt, schema, child); |
| 2941 | child = child->next; |
| 2942 | } else if (IS_SCHEMA(child, "group")) { |
| 2943 | xmlSchemaParseGroup(ctxt, schema, child); |
| 2944 | child = child->next; |
| 2945 | } else if (IS_SCHEMA(child, "notation")) { |
| 2946 | xmlSchemaParseNotation(ctxt, schema, child); |
| 2947 | child = child->next; |
| 2948 | } else { |
| 2949 | xmlSchemaPErr2(ctxt, NULL, child, |
| 2950 | XML_SCHEMAP_UNKNOWN_SCHEMAS_CHILD, |
| 2951 | "Schemas: unexpected element %s here \n", |
| 2952 | child->name, NULL); |
| 2953 | child = child->next; |
| 2954 | } |
| 2955 | while (IS_SCHEMA(child, "annotation")) { |
| 2956 | annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 2957 | if (schema->annot == NULL) |
| 2958 | schema->annot = annot; |
| 2959 | else |
| 2960 | xmlSchemaFreeAnnot(annot); |
| 2961 | child = child->next; |
| 2962 | } |
| 2963 | } |
| 2964 | } |
| 2965 | |
| 2966 | /** |
| 2967 | * xmlSchemaParseInclude: |
| 2968 | * @ctxt: a schema validation context |
| 2969 | * @schema: the schema being built |
| 2970 | * @node: a subtree containing XML Schema informations |
| 2971 | * |
| 2972 | * parse a XML schema Include definition |
| 2973 | * |
| 2974 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 2975 | * 1 in case of success. |
| 2976 | */ |
| 2977 | static int |
| 2978 | xmlSchemaParseInclude(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 2979 | xmlNodePtr node) |
| 2980 | { |
| 2981 | xmlNodePtr child = NULL; |
| 2982 | const xmlChar *schemaLocation; |
| 2983 | xmlURIPtr check; |
| 2984 | xmlDocPtr doc; |
| 2985 | xmlNodePtr root; |
| 2986 | xmlSchemaIncludePtr include; |
| 2987 | |
| 2988 | |
| 2989 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 2990 | return (-1); |
| 2991 | |
| 2992 | /* |
| 2993 | * Preliminary step, extract the URI-Reference for the include and |
| 2994 | * make an URI from the base. |
| 2995 | */ |
| 2996 | schemaLocation = xmlSchemaGetProp(ctxt, node, "schemaLocation"); |
| 2997 | if (schemaLocation != NULL) { |
| 2998 | xmlChar *base = NULL; |
| 2999 | xmlChar *URI = NULL; |
| 3000 | check = xmlParseURI((const char *) schemaLocation); |
| 3001 | if (check == NULL) { |
| 3002 | xmlSchemaPErr2(ctxt, node, child, |
| 3003 | XML_SCHEMAP_INCLUDE_SCHEMA_NOT_URI, |
| 3004 | "Include schemaLocation attribute is not an URI: %s\n", |
| 3005 | schemaLocation, NULL); |
| 3006 | return (-1); |
| 3007 | } else { |
| 3008 | xmlFreeURI(check); |
| 3009 | } |
| 3010 | base = xmlNodeGetBase(node->doc, node); |
| 3011 | if (base == NULL) { |
| 3012 | URI = xmlBuildURI(schemaLocation, node->doc->URL); |
| 3013 | } else { |
| 3014 | URI = xmlBuildURI(schemaLocation, base); |
| 3015 | xmlFree(base); |
| 3016 | } |
| 3017 | if (URI != NULL) { |
| 3018 | schemaLocation = xmlDictLookup(ctxt->dict, URI, -1); |
| 3019 | xmlFree(URI); |
| 3020 | } |
| 3021 | } else { |
| 3022 | xmlSchemaPErr2(ctxt, node, child, |
| 3023 | XML_SCHEMAP_INCLUDE_SCHEMA_NO_URI, |
| 3024 | "Include schemaLocation attribute missing\n", |
| 3025 | NULL, NULL); |
| 3026 | return (-1); |
| 3027 | } |
| 3028 | |
| 3029 | child = node->children; |
| 3030 | while (IS_SCHEMA(child, "annotation")) { |
| 3031 | /* |
| 3032 | * the annotations here are simply discarded ... |
| 3033 | */ |
| 3034 | child = child->next; |
| 3035 | } |
| 3036 | if (child != NULL) { |
| 3037 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_INCLUDE_CHILD, |
| 3038 | "Include has unexpected content\n", NULL, NULL); |
| 3039 | return (-1); |
| 3040 | } |
| 3041 | |
| 3042 | /* |
| 3043 | * First step is to parse the input document into an DOM/Infoset |
| 3044 | */ |
| 3045 | doc = xmlReadFile((const char *) schemaLocation, NULL, |
| 3046 | SCHEMAS_PARSE_OPTIONS); |
| 3047 | if (doc == NULL) { |
| 3048 | xmlSchemaPErr(ctxt, NULL, |
| 3049 | XML_SCHEMAP_FAILED_LOAD, |
| 3050 | "xmlSchemaParse: could not load %s\n", |
| 3051 | ctxt->URL, NULL); |
| 3052 | return(-1); |
| 3053 | } |
| 3054 | |
| 3055 | /* |
| 3056 | * Then extract the root of the schema |
| 3057 | */ |
| 3058 | root = xmlDocGetRootElement(doc); |
| 3059 | if (root == NULL) { |
| 3060 | xmlSchemaPErr(ctxt, (xmlNodePtr) doc, |
| 3061 | XML_SCHEMAP_NOROOT, |
| 3062 | "schemas %s has no root", schemaLocation, NULL); |
| 3063 | xmlFreeDoc(doc); |
| 3064 | return (-1); |
| 3065 | } |
| 3066 | |
| 3067 | /* |
| 3068 | * Remove all the blank text nodes |
| 3069 | */ |
| 3070 | xmlSchemaCleanupDoc(ctxt, root); |
| 3071 | |
| 3072 | /* |
| 3073 | * Check the schemas top level element |
| 3074 | */ |
| 3075 | if (!IS_SCHEMA(root, "schema")) { |
| 3076 | xmlSchemaPErr(ctxt, (xmlNodePtr) doc, |
| 3077 | XML_SCHEMAP_NOT_SCHEMA, |
| 3078 | "File %s is not a schemas", schemaLocation, NULL); |
| 3079 | xmlFreeDoc(doc); |
| 3080 | return (-1); |
| 3081 | } |
| 3082 | |
| 3083 | /* |
| 3084 | * register the include |
| 3085 | */ |
| 3086 | include = (xmlSchemaIncludePtr) xmlMalloc(sizeof(xmlSchemaInclude)); |
| 3087 | if (include == NULL) { |
| 3088 | xmlSchemaPErrMemory(ctxt, "allocating included schema", NULL); |
| 3089 | xmlFreeDoc(doc); |
| 3090 | return (-1); |
| 3091 | } |
| 3092 | |
| 3093 | memset(include, 0, sizeof(xmlSchemaInclude)); |
| 3094 | include->schemaLocation = xmlDictLookup(ctxt->dict, schemaLocation, -1); |
| 3095 | include->doc = doc; |
| 3096 | include->next = schema->includes; |
| 3097 | schema->includes = include; |
| 3098 | |
| 3099 | |
| 3100 | /* |
| 3101 | * parse the declarations in the included file like if they |
| 3102 | * were in the original file. |
| 3103 | */ |
| 3104 | xmlSchemaParseSchemaTopLevel(ctxt, schema, root->children); |
| 3105 | |
| 3106 | return (1); |
| 3107 | } |
| 3108 | |
| 3109 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3110 | * xmlSchemaParseChoice: |
| 3111 | * @ctxt: a schema validation context |
| 3112 | * @schema: the schema being built |
| 3113 | * @node: a subtree containing XML Schema informations |
| 3114 | * |
| 3115 | * parse a XML schema Choice definition |
| 3116 | * *WARNING* this interface is highly subject to change |
| 3117 | * |
| 3118 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 3119 | * 1 in case of success. |
| 3120 | */ |
| 3121 | static xmlSchemaTypePtr |
| 3122 | xmlSchemaParseChoice(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3123 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3124 | { |
| 3125 | xmlSchemaTypePtr type, subtype, last = NULL; |
| 3126 | xmlNodePtr child = NULL; |
| 3127 | xmlChar name[30]; |
| 3128 | |
| 3129 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3130 | return (NULL); |
| 3131 | |
| 3132 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3133 | snprintf((char *) name, 30, "choice %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3134 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3135 | if (type == NULL) |
| 3136 | return (NULL); |
| 3137 | type->node = node; |
| 3138 | type->type = XML_SCHEMA_TYPE_CHOICE; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3139 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3140 | type->minOccurs = xmlGetMinOccurs(ctxt, node); |
| 3141 | type->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
| 3142 | |
| 3143 | child = node->children; |
| 3144 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3145 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3146 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3147 | } |
| 3148 | while ((IS_SCHEMA(child, "element")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3149 | (IS_SCHEMA(child, "group")) || |
| 3150 | (IS_SCHEMA(child, "any")) || |
| 3151 | (IS_SCHEMA(child, "choice")) || |
| 3152 | (IS_SCHEMA(child, "sequence"))) { |
| 3153 | subtype = NULL; |
| 3154 | if (IS_SCHEMA(child, "element")) { |
| 3155 | subtype = (xmlSchemaTypePtr) |
| 3156 | xmlSchemaParseElement(ctxt, schema, child, 0); |
| 3157 | } else if (IS_SCHEMA(child, "group")) { |
| 3158 | subtype = xmlSchemaParseGroup(ctxt, schema, child); |
| 3159 | } else if (IS_SCHEMA(child, "any")) { |
| 3160 | subtype = xmlSchemaParseAny(ctxt, schema, child); |
| 3161 | } else if (IS_SCHEMA(child, "sequence")) { |
| 3162 | subtype = xmlSchemaParseSequence(ctxt, schema, child); |
| 3163 | } else if (IS_SCHEMA(child, "choice")) { |
| 3164 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 3165 | } |
| 3166 | if (subtype != NULL) { |
| 3167 | if (last == NULL) { |
| 3168 | type->subtypes = subtype; |
| 3169 | last = subtype; |
| 3170 | } else { |
| 3171 | last->next = subtype; |
| 3172 | last = subtype; |
| 3173 | } |
| 3174 | last->next = NULL; |
| 3175 | } |
| 3176 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3177 | } |
| 3178 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3179 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_CHOICE_CHILD, |
| 3180 | "Choice %s has unexpected content\n", type->name, |
| 3181 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3182 | } |
| 3183 | |
| 3184 | return (type); |
| 3185 | } |
| 3186 | |
| 3187 | /** |
| 3188 | * xmlSchemaParseSequence: |
| 3189 | * @ctxt: a schema validation context |
| 3190 | * @schema: the schema being built |
| 3191 | * @node: a subtree containing XML Schema informations |
| 3192 | * |
| 3193 | * parse a XML schema Sequence definition |
| 3194 | * *WARNING* this interface is highly subject to change |
| 3195 | * |
| 3196 | * Returns -1 in case of error, 0 if the declaration is inproper and |
| 3197 | * 1 in case of success. |
| 3198 | */ |
| 3199 | static xmlSchemaTypePtr |
| 3200 | xmlSchemaParseSequence(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3201 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3202 | { |
| 3203 | xmlSchemaTypePtr type, subtype, last = NULL; |
| 3204 | xmlNodePtr child = NULL; |
| 3205 | xmlChar name[30]; |
| 3206 | |
| 3207 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3208 | return (NULL); |
| 3209 | |
| 3210 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3211 | snprintf((char *) name, 30, "sequence %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3212 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3213 | if (type == NULL) |
| 3214 | return (NULL); |
| 3215 | type->node = node; |
| 3216 | type->type = XML_SCHEMA_TYPE_SEQUENCE; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3217 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3218 | type->minOccurs = xmlGetMinOccurs(ctxt, node); |
| 3219 | type->maxOccurs = xmlGetMaxOccurs(ctxt, node); |
| 3220 | |
| 3221 | child = node->children; |
| 3222 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3223 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3224 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3225 | } |
| 3226 | while ((IS_SCHEMA(child, "element")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3227 | (IS_SCHEMA(child, "group")) || |
| 3228 | (IS_SCHEMA(child, "any")) || |
| 3229 | (IS_SCHEMA(child, "choice")) || |
| 3230 | (IS_SCHEMA(child, "sequence"))) { |
| 3231 | subtype = NULL; |
| 3232 | if (IS_SCHEMA(child, "element")) { |
| 3233 | subtype = (xmlSchemaTypePtr) |
| 3234 | xmlSchemaParseElement(ctxt, schema, child, 0); |
| 3235 | } else if (IS_SCHEMA(child, "group")) { |
| 3236 | subtype = xmlSchemaParseGroup(ctxt, schema, child); |
| 3237 | } else if (IS_SCHEMA(child, "any")) { |
| 3238 | subtype = xmlSchemaParseAny(ctxt, schema, child); |
| 3239 | } else if (IS_SCHEMA(child, "choice")) { |
| 3240 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 3241 | } else if (IS_SCHEMA(child, "sequence")) { |
| 3242 | subtype = xmlSchemaParseSequence(ctxt, schema, child); |
| 3243 | } |
| 3244 | if (subtype != NULL) { |
| 3245 | if (last == NULL) { |
| 3246 | type->subtypes = subtype; |
| 3247 | last = subtype; |
| 3248 | } else { |
| 3249 | last->next = subtype; |
| 3250 | last = subtype; |
| 3251 | } |
| 3252 | last->next = NULL; |
| 3253 | } |
| 3254 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3255 | } |
| 3256 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3257 | xmlSchemaPErr2(ctxt, node, child, |
| 3258 | XML_SCHEMAP_UNKNOWN_SEQUENCE_CHILD, |
| 3259 | "Sequence %s has unexpected content\n", type->name, |
| 3260 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3261 | } |
| 3262 | |
| 3263 | return (type); |
| 3264 | } |
| 3265 | |
| 3266 | /** |
| 3267 | * xmlSchemaParseRestriction: |
| 3268 | * @ctxt: a schema validation context |
| 3269 | * @schema: the schema being built |
| 3270 | * @node: a subtree containing XML Schema informations |
| 3271 | * @simple: is that part of a simple type. |
| 3272 | * |
| 3273 | * parse a XML schema Restriction definition |
| 3274 | * *WARNING* this interface is highly subject to change |
| 3275 | * |
| 3276 | * Returns the type definition or NULL in case of error |
| 3277 | */ |
| 3278 | static xmlSchemaTypePtr |
| 3279 | xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 3280 | xmlNodePtr node, int simple) |
| 3281 | { |
| 3282 | xmlSchemaTypePtr type, subtype; |
| 3283 | xmlSchemaFacetPtr facet, lastfacet = NULL; |
| 3284 | xmlNodePtr child = NULL; |
| 3285 | xmlChar name[30]; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3286 | const xmlChar *oldcontainer; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3287 | |
| 3288 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3289 | return (NULL); |
| 3290 | |
| 3291 | oldcontainer = ctxt->container; |
| 3292 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3293 | snprintf((char *) name, 30, "restriction %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3294 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3295 | if (type == NULL) |
| 3296 | return (NULL); |
| 3297 | type->node = node; |
| 3298 | type->type = XML_SCHEMA_TYPE_RESTRICTION; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3299 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3300 | type->base = xmlGetQNameProp(ctxt, node, "base", &(type->baseNs)); |
| 3301 | if ((!simple) && (type->base == NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3302 | xmlSchemaPErr2(ctxt, node, child, |
| 3303 | XML_SCHEMAP_RESTRICTION_NONAME_NOREF, |
| 3304 | "Restriction %s has no base\n", type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3305 | } |
| 3306 | ctxt->container = name; |
| 3307 | |
| 3308 | child = node->children; |
| 3309 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3310 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3311 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3312 | } |
| 3313 | subtype = NULL; |
| 3314 | |
| 3315 | if (IS_SCHEMA(child, "all")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3316 | subtype = (xmlSchemaTypePtr) |
| 3317 | xmlSchemaParseAll(ctxt, schema, child); |
| 3318 | child = child->next; |
| 3319 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3320 | } else if (IS_SCHEMA(child, "choice")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3321 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 3322 | child = child->next; |
| 3323 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3324 | } else if (IS_SCHEMA(child, "sequence")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3325 | subtype = (xmlSchemaTypePtr) |
| 3326 | xmlSchemaParseSequence(ctxt, schema, child); |
| 3327 | child = child->next; |
| 3328 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3329 | } else if (IS_SCHEMA(child, "group")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3330 | subtype = (xmlSchemaTypePtr) |
| 3331 | xmlSchemaParseGroup(ctxt, schema, child); |
| 3332 | child = child->next; |
| 3333 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3334 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3335 | if (IS_SCHEMA(child, "simpleType")) { |
| 3336 | subtype = (xmlSchemaTypePtr) |
| 3337 | xmlSchemaParseSimpleType(ctxt, schema, child); |
| 3338 | child = child->next; |
| 3339 | type->baseType = subtype; |
| 3340 | } |
| 3341 | /* |
| 3342 | * Facets |
| 3343 | */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3344 | while ((IS_SCHEMA(child, "minInclusive")) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3345 | (IS_SCHEMA(child, "minExclusive")) || |
| 3346 | (IS_SCHEMA(child, "maxInclusive")) || |
| 3347 | (IS_SCHEMA(child, "maxExclusive")) || |
| 3348 | (IS_SCHEMA(child, "totalDigits")) || |
| 3349 | (IS_SCHEMA(child, "fractionDigits")) || |
| 3350 | (IS_SCHEMA(child, "pattern")) || |
| 3351 | (IS_SCHEMA(child, "enumeration")) || |
| 3352 | (IS_SCHEMA(child, "whiteSpace")) || |
| 3353 | (IS_SCHEMA(child, "length")) || |
| 3354 | (IS_SCHEMA(child, "maxLength")) || |
| 3355 | (IS_SCHEMA(child, "minLength"))) { |
| 3356 | facet = xmlSchemaParseFacet(ctxt, schema, child); |
| 3357 | if (facet != NULL) { |
| 3358 | if (lastfacet == NULL) { |
| 3359 | type->facets = facet; |
| 3360 | lastfacet = facet; |
| 3361 | } else { |
| 3362 | lastfacet->next = facet; |
| 3363 | lastfacet = facet; |
| 3364 | } |
| 3365 | lastfacet->next = NULL; |
| 3366 | } |
| 3367 | child = child->next; |
| 3368 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3369 | } |
| 3370 | child = xmlSchemaParseAttrDecls(ctxt, schema, child, type); |
| 3371 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3372 | xmlSchemaPErr2(ctxt, node, child, |
| 3373 | XML_SCHEMAP_UNKNOWN_RESTRICTION_CHILD, |
| 3374 | "Restriction %s has unexpected content\n", |
| 3375 | type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3376 | } |
| 3377 | ctxt->container = oldcontainer; |
| 3378 | return (type); |
| 3379 | } |
| 3380 | |
| 3381 | /** |
| 3382 | * xmlSchemaParseExtension: |
| 3383 | * @ctxt: a schema validation context |
| 3384 | * @schema: the schema being built |
| 3385 | * @node: a subtree containing XML Schema informations |
| 3386 | * |
| 3387 | * parse a XML schema Extension definition |
| 3388 | * *WARNING* this interface is highly subject to change |
| 3389 | * |
| 3390 | * Returns the type definition or NULL in case of error |
| 3391 | */ |
| 3392 | static xmlSchemaTypePtr |
| 3393 | xmlSchemaParseExtension(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3394 | xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3395 | { |
| 3396 | xmlSchemaTypePtr type, subtype; |
| 3397 | xmlNodePtr child = NULL; |
| 3398 | xmlChar name[30]; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3399 | const xmlChar *oldcontainer; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3400 | |
| 3401 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3402 | return (NULL); |
| 3403 | |
| 3404 | oldcontainer = ctxt->container; |
| 3405 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3406 | snprintf((char *) name, 30, "extension %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3407 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3408 | if (type == NULL) |
| 3409 | return (NULL); |
| 3410 | type->node = node; |
| 3411 | type->type = XML_SCHEMA_TYPE_EXTENSION; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3412 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3413 | ctxt->container = name; |
| 3414 | |
| 3415 | type->base = xmlGetQNameProp(ctxt, node, "base", &(type->baseNs)); |
| 3416 | if (type->base == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3417 | xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_EXTENSION_NO_BASE, |
| 3418 | "Extension %s has no base\n", type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3419 | } |
| 3420 | child = node->children; |
| 3421 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3422 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3423 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3424 | } |
| 3425 | subtype = NULL; |
| 3426 | |
| 3427 | if (IS_SCHEMA(child, "all")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3428 | subtype = xmlSchemaParseAll(ctxt, schema, child); |
| 3429 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3430 | } else if (IS_SCHEMA(child, "choice")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3431 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 3432 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3433 | } else if (IS_SCHEMA(child, "sequence")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3434 | subtype = xmlSchemaParseSequence(ctxt, schema, child); |
| 3435 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3436 | } else if (IS_SCHEMA(child, "group")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3437 | subtype = xmlSchemaParseGroup(ctxt, schema, child); |
| 3438 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3439 | } |
| 3440 | if (subtype != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3441 | type->subtypes = subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3442 | child = xmlSchemaParseAttrDecls(ctxt, schema, child, type); |
| 3443 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3444 | xmlSchemaPErr2(ctxt, node, child, |
| 3445 | XML_SCHEMAP_UNKNOWN_EXTENSION_CHILD, |
| 3446 | "Extension %s has unexpected content\n", type->name, |
| 3447 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3448 | } |
| 3449 | ctxt->container = oldcontainer; |
| 3450 | return (type); |
| 3451 | } |
| 3452 | |
| 3453 | /** |
| 3454 | * xmlSchemaParseSimpleContent: |
| 3455 | * @ctxt: a schema validation context |
| 3456 | * @schema: the schema being built |
| 3457 | * @node: a subtree containing XML Schema informations |
| 3458 | * |
| 3459 | * parse a XML schema SimpleContent definition |
| 3460 | * *WARNING* this interface is highly subject to change |
| 3461 | * |
| 3462 | * Returns the type definition or NULL in case of error |
| 3463 | */ |
| 3464 | static xmlSchemaTypePtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3465 | xmlSchemaParseSimpleContent(xmlSchemaParserCtxtPtr ctxt, |
| 3466 | xmlSchemaPtr schema, xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3467 | { |
| 3468 | xmlSchemaTypePtr type, subtype; |
| 3469 | xmlNodePtr child = NULL; |
| 3470 | xmlChar name[30]; |
| 3471 | |
| 3472 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3473 | return (NULL); |
| 3474 | |
| 3475 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3476 | snprintf((char *) name, 30, "complexContent %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3477 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3478 | if (type == NULL) |
| 3479 | return (NULL); |
| 3480 | type->node = node; |
| 3481 | type->type = XML_SCHEMA_TYPE_SIMPLE_CONTENT; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3482 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3483 | |
| 3484 | child = node->children; |
| 3485 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3486 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3487 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3488 | } |
| 3489 | subtype = NULL; |
| 3490 | if (IS_SCHEMA(child, "restriction")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3491 | subtype = (xmlSchemaTypePtr) |
| 3492 | xmlSchemaParseRestriction(ctxt, schema, child, 0); |
| 3493 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3494 | } else if (IS_SCHEMA(child, "extension")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3495 | subtype = (xmlSchemaTypePtr) |
| 3496 | xmlSchemaParseExtension(ctxt, schema, child); |
| 3497 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3498 | } |
| 3499 | type->subtypes = subtype; |
| 3500 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3501 | xmlSchemaPErr2(ctxt, node, child, |
| 3502 | XML_SCHEMAP_UNKNOWN_SIMPLECONTENT_CHILD, |
| 3503 | "SimpleContent %s has unexpected content\n", |
| 3504 | type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3505 | } |
| 3506 | return (type); |
| 3507 | } |
| 3508 | |
| 3509 | /** |
| 3510 | * xmlSchemaParseComplexContent: |
| 3511 | * @ctxt: a schema validation context |
| 3512 | * @schema: the schema being built |
| 3513 | * @node: a subtree containing XML Schema informations |
| 3514 | * |
| 3515 | * parse a XML schema ComplexContent definition |
| 3516 | * *WARNING* this interface is highly subject to change |
| 3517 | * |
| 3518 | * Returns the type definition or NULL in case of error |
| 3519 | */ |
| 3520 | static xmlSchemaTypePtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3521 | xmlSchemaParseComplexContent(xmlSchemaParserCtxtPtr ctxt, |
| 3522 | xmlSchemaPtr schema, xmlNodePtr node) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3523 | { |
| 3524 | xmlSchemaTypePtr type, subtype; |
| 3525 | xmlNodePtr child = NULL; |
| 3526 | xmlChar name[30]; |
| 3527 | |
| 3528 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3529 | return (NULL); |
| 3530 | |
| 3531 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3532 | snprintf((char *) name, 30, "complexContent %d", ctxt->counter++ + 1); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3533 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3534 | if (type == NULL) |
| 3535 | return (NULL); |
| 3536 | type->node = node; |
| 3537 | type->type = XML_SCHEMA_TYPE_COMPLEX_CONTENT; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3538 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3539 | |
| 3540 | child = node->children; |
| 3541 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3542 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3543 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3544 | } |
| 3545 | subtype = NULL; |
| 3546 | if (IS_SCHEMA(child, "restriction")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3547 | subtype = (xmlSchemaTypePtr) |
| 3548 | xmlSchemaParseRestriction(ctxt, schema, child, 0); |
| 3549 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3550 | } else if (IS_SCHEMA(child, "extension")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3551 | subtype = (xmlSchemaTypePtr) |
| 3552 | xmlSchemaParseExtension(ctxt, schema, child); |
| 3553 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3554 | } |
| 3555 | type->subtypes = subtype; |
| 3556 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3557 | xmlSchemaPErr2(ctxt, node, child, |
| 3558 | XML_SCHEMAP_UNKNOWN_COMPLEXCONTENT_CHILD, |
| 3559 | "ComplexContent %s has unexpected content\n", |
| 3560 | type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3561 | } |
| 3562 | return (type); |
| 3563 | } |
| 3564 | |
| 3565 | /** |
| 3566 | * xmlSchemaParseComplexType: |
| 3567 | * @ctxt: a schema validation context |
| 3568 | * @schema: the schema being built |
| 3569 | * @node: a subtree containing XML Schema informations |
| 3570 | * |
| 3571 | * parse a XML schema Complex Type definition |
| 3572 | * *WARNING* this interface is highly subject to change |
| 3573 | * |
| 3574 | * Returns the type definition or NULL in case of error |
| 3575 | */ |
| 3576 | static xmlSchemaTypePtr |
| 3577 | xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema, |
| 3578 | xmlNodePtr node) |
| 3579 | { |
| 3580 | xmlSchemaTypePtr type, subtype; |
| 3581 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3582 | const xmlChar *name; |
| 3583 | const xmlChar *oldcontainer; |
Daniel Veillard | 1aefc86 | 2004-03-04 11:40:48 +0000 | [diff] [blame^] | 3584 | const xmlChar *mixed; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3585 | char buf[100]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3586 | |
| 3587 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL)) |
| 3588 | return (NULL); |
| 3589 | |
| 3590 | oldcontainer = ctxt->container; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3591 | name = xmlSchemaGetProp(ctxt, node, "name"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3592 | if (name == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3593 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3594 | snprintf(buf, 99, "anontype %d", ctxt->counter++ + 1); |
| 3595 | name = (const xmlChar *)buf; |
| 3596 | type = xmlSchemaAddType(ctxt, schema, name, NULL); |
| 3597 | } else { |
| 3598 | const xmlChar *local, *ns; |
| 3599 | |
| 3600 | local = xmlSchemaGetNamespace(ctxt, schema, node, name, &ns); |
| 3601 | type = xmlSchemaAddType(ctxt, schema, local, ns); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3602 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3603 | if (type == NULL) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3604 | return (NULL); |
| 3605 | } |
Daniel Veillard | 1aefc86 | 2004-03-04 11:40:48 +0000 | [diff] [blame^] | 3606 | |
| 3607 | mixed = xmlSchemaGetProp(ctxt, node, "mixed"); |
| 3608 | if (mixed != NULL) |
| 3609 | type->flags |= XML_SCHEMAS_TYPE_MIXED; |
| 3610 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3611 | type->node = node; |
| 3612 | type->type = XML_SCHEMA_TYPE_COMPLEX; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3613 | type->id = xmlSchemaGetProp(ctxt, node, "id"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3614 | ctxt->container = name; |
| 3615 | |
| 3616 | child = node->children; |
| 3617 | if (IS_SCHEMA(child, "annotation")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3618 | type->annot = xmlSchemaParseAnnotation(ctxt, schema, child); |
| 3619 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3620 | } |
| 3621 | if (IS_SCHEMA(child, "simpleContent")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3622 | type->subtypes = xmlSchemaParseSimpleContent(ctxt, schema, child); |
| 3623 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3624 | } else if (IS_SCHEMA(child, "complexContent")) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3625 | type->subtypes = xmlSchemaParseComplexContent(ctxt, schema, child); |
| 3626 | child = child->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3627 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3628 | subtype = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3629 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3630 | if (IS_SCHEMA(child, "all")) { |
| 3631 | subtype = xmlSchemaParseAll(ctxt, schema, child); |
| 3632 | child = child->next; |
| 3633 | } else if (IS_SCHEMA(child, "choice")) { |
| 3634 | subtype = xmlSchemaParseChoice(ctxt, schema, child); |
| 3635 | child = child->next; |
| 3636 | } else if (IS_SCHEMA(child, "sequence")) { |
| 3637 | subtype = xmlSchemaParseSequence(ctxt, schema, child); |
| 3638 | child = child->next; |
| 3639 | } else if (IS_SCHEMA(child, "group")) { |
| 3640 | subtype = xmlSchemaParseGroup(ctxt, schema, child); |
| 3641 | child = child->next; |
| 3642 | } |
| 3643 | if (subtype != NULL) |
| 3644 | type->subtypes = subtype; |
| 3645 | child = xmlSchemaParseAttrDecls(ctxt, schema, child, type); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3646 | } |
| 3647 | if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3648 | xmlSchemaPErr2(ctxt, node, child, |
| 3649 | XML_SCHEMAP_UNKNOWN_COMPLEXTYPE_CHILD, |
| 3650 | "ComplexType %s has unexpected content\n", |
| 3651 | type->name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3652 | } |
| 3653 | ctxt->container = oldcontainer; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3654 | return (type); |
| 3655 | } |
| 3656 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3657 | /** |
| 3658 | * xmlSchemaParseSchema: |
| 3659 | * @ctxt: a schema validation context |
| 3660 | * @node: a subtree containing XML Schema informations |
| 3661 | * |
| 3662 | * parse a XML schema definition from a node set |
| 3663 | * *WARNING* this interface is highly subject to change |
| 3664 | * |
| 3665 | * Returns the internal XML Schema structure built from the resource or |
| 3666 | * NULL in case of error |
| 3667 | */ |
| 3668 | static xmlSchemaPtr |
| 3669 | xmlSchemaParseSchema(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node) |
| 3670 | { |
| 3671 | xmlSchemaPtr schema = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3672 | xmlNodePtr child = NULL; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3673 | const xmlChar *val; |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 3674 | int nberrors; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3675 | |
| 3676 | if ((ctxt == NULL) || (node == NULL)) |
| 3677 | return (NULL); |
| 3678 | |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 3679 | nberrors = ctxt->nberrors; |
| 3680 | ctxt->nberrors = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3681 | if (IS_SCHEMA(node, "schema")) { |
| 3682 | schema = xmlSchemaNewSchema(ctxt); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3683 | if (schema == NULL) |
| 3684 | return (NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3685 | val = xmlSchemaGetProp(ctxt, node, "targetNamespace"); |
| 3686 | if (val != NULL) { |
| 3687 | schema->targetNamespace = xmlDictLookup(ctxt->dict, val, -1); |
| 3688 | } else { |
| 3689 | schema->targetNamespace = NULL; |
| 3690 | } |
| 3691 | schema->id = xmlSchemaGetProp(ctxt, node, "id"); |
| 3692 | schema->version = xmlSchemaGetProp(ctxt, node, "version"); |
| 3693 | val = xmlSchemaGetProp(ctxt, node, "elementFormDefault"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3694 | if (val != NULL) { |
| 3695 | if (xmlStrEqual(val, BAD_CAST "qualified")) |
| 3696 | schema->flags |= XML_SCHEMAS_QUALIF_ELEM; |
| 3697 | else if (!xmlStrEqual(val, BAD_CAST "unqualified")) { |
| 3698 | xmlSchemaPErr2(ctxt, node, child, |
| 3699 | XML_SCHEMAP_ELEMFORMDEFAULT_VALUE, |
| 3700 | "Invalid value %s for elementFormDefault\n", |
| 3701 | val, NULL); |
| 3702 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3703 | } else { |
| 3704 | schema->flags |= XML_SCHEMAS_QUALIF_ELEM; |
| 3705 | } |
| 3706 | val = xmlSchemaGetProp(ctxt, node, "attributeFormDefault"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3707 | if (val != NULL) { |
| 3708 | if (xmlStrEqual(val, BAD_CAST "qualified")) |
| 3709 | schema->flags |= XML_SCHEMAS_QUALIF_ATTR; |
| 3710 | else if (!xmlStrEqual(val, BAD_CAST "unqualified")) { |
| 3711 | xmlSchemaPErr2(ctxt, node, child, |
| 3712 | XML_SCHEMAP_ATTRFORMDEFAULT_VALUE, |
| 3713 | "Invalid value %s for attributeFormDefault\n", |
| 3714 | val, NULL); |
| 3715 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3716 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3717 | |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 3718 | xmlSchemaParseSchemaTopLevel(ctxt, schema, node->children); |
| 3719 | } else { |
| 3720 | xmlDocPtr doc; |
| 3721 | |
| 3722 | doc = node->doc; |
| 3723 | |
| 3724 | if ((doc != NULL) && (doc->URL != NULL)) { |
| 3725 | xmlSchemaPErr(ctxt, (xmlNodePtr) doc, |
| 3726 | XML_SCHEMAP_NOT_SCHEMA, |
| 3727 | "File %s is not a schemas", doc->URL, NULL); |
| 3728 | } else { |
| 3729 | xmlSchemaPErr(ctxt, (xmlNodePtr) doc, |
| 3730 | XML_SCHEMAP_NOT_SCHEMA, |
| 3731 | "File is not a schemas", NULL, NULL); |
| 3732 | } |
| 3733 | return(NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3734 | } |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 3735 | if (ctxt->nberrors != 0) { |
| 3736 | if (schema != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3737 | xmlSchemaFree(schema); |
| 3738 | schema = NULL; |
| 3739 | } |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 3740 | } |
| 3741 | ctxt->nberrors = nberrors; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3742 | #ifdef DEBUG |
| 3743 | if (schema == NULL) |
| 3744 | xmlGenericError(xmlGenericErrorContext, |
| 3745 | "xmlSchemaParse() failed\n"); |
| 3746 | #endif |
| 3747 | |
| 3748 | return (schema); |
| 3749 | } |
| 3750 | |
| 3751 | /************************************************************************ |
| 3752 | * * |
| 3753 | * Validating using Schemas * |
| 3754 | * * |
| 3755 | ************************************************************************/ |
| 3756 | |
| 3757 | /************************************************************************ |
| 3758 | * * |
| 3759 | * Reading/Writing Schemas * |
| 3760 | * * |
| 3761 | ************************************************************************/ |
| 3762 | |
| 3763 | /** |
| 3764 | * xmlSchemaNewParserCtxt: |
| 3765 | * @URL: the location of the schema |
| 3766 | * |
| 3767 | * Create an XML Schemas parse context for that file/resource expected |
| 3768 | * to contain an XML Schemas file. |
| 3769 | * |
| 3770 | * Returns the parser context or NULL in case of error |
| 3771 | */ |
| 3772 | xmlSchemaParserCtxtPtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3773 | xmlSchemaNewParserCtxt(const char *URL) |
| 3774 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3775 | xmlSchemaParserCtxtPtr ret; |
| 3776 | |
| 3777 | if (URL == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3778 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3779 | |
| 3780 | ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt)); |
| 3781 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3782 | xmlSchemaPErrMemory(NULL, "allocating schama parser context", |
| 3783 | NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3784 | return (NULL); |
| 3785 | } |
| 3786 | memset(ret, 0, sizeof(xmlSchemaParserCtxt)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3787 | ret->dict = xmlDictCreate(); |
| 3788 | ret->URL = xmlDictLookup(ret->dict, (const xmlChar *) URL, -1); |
Daniel Veillard | b0f397e | 2003-12-23 23:30:53 +0000 | [diff] [blame] | 3789 | ret->includes = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3790 | return (ret); |
| 3791 | } |
| 3792 | |
| 3793 | /** |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 3794 | * xmlSchemaNewMemParserCtxt: |
| 3795 | * @buffer: a pointer to a char array containing the schemas |
| 3796 | * @size: the size of the array |
| 3797 | * |
| 3798 | * Create an XML Schemas parse context for that memory buffer expected |
| 3799 | * to contain an XML Schemas file. |
| 3800 | * |
| 3801 | * Returns the parser context or NULL in case of error |
| 3802 | */ |
| 3803 | xmlSchemaParserCtxtPtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3804 | xmlSchemaNewMemParserCtxt(const char *buffer, int size) |
| 3805 | { |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 3806 | xmlSchemaParserCtxtPtr ret; |
| 3807 | |
| 3808 | if ((buffer == NULL) || (size <= 0)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3809 | return (NULL); |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 3810 | |
| 3811 | ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt)); |
| 3812 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3813 | xmlSchemaPErrMemory(NULL, "allocating schama parser context", |
| 3814 | NULL); |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 3815 | return (NULL); |
| 3816 | } |
| 3817 | memset(ret, 0, sizeof(xmlSchemaParserCtxt)); |
| 3818 | ret->buffer = buffer; |
| 3819 | ret->size = size; |
William M. Brack | cf9eadf | 2003-12-25 13:24:05 +0000 | [diff] [blame] | 3820 | ret->dict = xmlDictCreate(); |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 3821 | return (ret); |
| 3822 | } |
| 3823 | |
| 3824 | /** |
Daniel Veillard | 9d75150 | 2003-10-29 13:21:47 +0000 | [diff] [blame] | 3825 | * xmlSchemaNewDocParserCtxt: |
| 3826 | * @doc: a preparsed document tree |
| 3827 | * |
| 3828 | * Create an XML Schemas parse context for that document. |
| 3829 | * NB. The document may be modified during the parsing process. |
| 3830 | * |
| 3831 | * Returns the parser context or NULL in case of error |
| 3832 | */ |
| 3833 | xmlSchemaParserCtxtPtr |
| 3834 | xmlSchemaNewDocParserCtxt(xmlDocPtr doc) |
| 3835 | { |
| 3836 | xmlSchemaParserCtxtPtr ret; |
| 3837 | |
| 3838 | if (doc == NULL) |
| 3839 | return (NULL); |
| 3840 | |
| 3841 | ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt)); |
| 3842 | if (ret == NULL) { |
| 3843 | xmlSchemaPErrMemory(NULL, "allocating schema parser context", |
| 3844 | NULL); |
| 3845 | return (NULL); |
| 3846 | } |
| 3847 | memset(ret, 0, sizeof(xmlSchemaParserCtxt)); |
| 3848 | ret->doc = doc; |
William M. Brack | cf9eadf | 2003-12-25 13:24:05 +0000 | [diff] [blame] | 3849 | ret->dict = xmlDictCreate(); |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 3850 | /* The application has responsibility for the document */ |
| 3851 | ret->preserve = 1; |
Daniel Veillard | 9d75150 | 2003-10-29 13:21:47 +0000 | [diff] [blame] | 3852 | |
| 3853 | return (ret); |
| 3854 | } |
| 3855 | |
| 3856 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3857 | * xmlSchemaFreeParserCtxt: |
| 3858 | * @ctxt: the schema parser context |
| 3859 | * |
| 3860 | * Free the resources associated to the schema parser context |
| 3861 | */ |
| 3862 | void |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3863 | xmlSchemaFreeParserCtxt(xmlSchemaParserCtxtPtr ctxt) |
| 3864 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3865 | if (ctxt == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3866 | return; |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 3867 | if (ctxt->doc != NULL && !ctxt->preserve) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3868 | xmlFreeDoc(ctxt->doc); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 3869 | xmlDictFree(ctxt->dict); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3870 | xmlFree(ctxt); |
| 3871 | } |
| 3872 | |
| 3873 | /************************************************************************ |
| 3874 | * * |
| 3875 | * Building the content models * |
| 3876 | * * |
| 3877 | ************************************************************************/ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3878 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3879 | /** |
| 3880 | * xmlSchemaBuildAContentModel: |
| 3881 | * @type: the schema type definition |
| 3882 | * @ctxt: the schema parser context |
| 3883 | * @name: the element name whose content is being built |
| 3884 | * |
| 3885 | * Generate the automata sequence needed for that type |
| 3886 | */ |
| 3887 | static void |
| 3888 | xmlSchemaBuildAContentModel(xmlSchemaTypePtr type, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3889 | xmlSchemaParserCtxtPtr ctxt, |
| 3890 | const xmlChar * name) |
| 3891 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3892 | if (type == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3893 | xmlGenericError(xmlGenericErrorContext, |
| 3894 | "Found unexpected type = NULL in %s content model\n", |
| 3895 | name); |
| 3896 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3897 | } |
| 3898 | switch (type->type) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3899 | case XML_SCHEMA_TYPE_ANY: |
| 3900 | /* TODO : handle the namespace too */ |
| 3901 | /* TODO : make that a specific transition type */ |
| 3902 | TODO ctxt->state = |
| 3903 | xmlAutomataNewTransition(ctxt->am, ctxt->state, NULL, |
| 3904 | BAD_CAST "*", NULL); |
| 3905 | break; |
| 3906 | case XML_SCHEMA_TYPE_ELEMENT:{ |
| 3907 | xmlSchemaElementPtr elem = (xmlSchemaElementPtr) type; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3908 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3909 | /* TODO : handle the namespace too */ |
| 3910 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3911 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3912 | if (elem->maxOccurs >= UNBOUNDED) { |
| 3913 | if (elem->minOccurs > 1) { |
| 3914 | xmlAutomataStatePtr tmp; |
| 3915 | int counter; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3916 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3917 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
| 3918 | oldstate, |
| 3919 | NULL); |
| 3920 | oldstate = ctxt->state; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3921 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3922 | counter = xmlAutomataNewCounter(ctxt->am, |
| 3923 | elem->minOccurs - |
| 3924 | 1, UNBOUNDED); |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3925 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3926 | if (elem->refDecl != NULL) { |
| 3927 | xmlSchemaBuildAContentModel((xmlSchemaTypePtr) |
| 3928 | elem->refDecl, |
| 3929 | ctxt, |
| 3930 | elem->refDecl-> |
| 3931 | name); |
| 3932 | } else { |
| 3933 | ctxt->state = |
| 3934 | xmlAutomataNewTransition(ctxt->am, |
| 3935 | ctxt->state, NULL, |
| 3936 | elem->name, type); |
| 3937 | } |
| 3938 | tmp = ctxt->state; |
| 3939 | xmlAutomataNewCountedTrans(ctxt->am, tmp, oldstate, |
| 3940 | counter); |
| 3941 | ctxt->state = |
| 3942 | xmlAutomataNewCounterTrans(ctxt->am, tmp, NULL, |
| 3943 | counter); |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3944 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3945 | } else { |
| 3946 | if (elem->refDecl != NULL) { |
| 3947 | xmlSchemaBuildAContentModel((xmlSchemaTypePtr) |
| 3948 | elem->refDecl, |
| 3949 | ctxt, |
| 3950 | elem->refDecl-> |
| 3951 | name); |
| 3952 | } else { |
| 3953 | ctxt->state = |
| 3954 | xmlAutomataNewTransition(ctxt->am, |
| 3955 | ctxt->state, NULL, |
| 3956 | elem->name, type); |
| 3957 | } |
| 3958 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
| 3959 | oldstate); |
| 3960 | if (elem->minOccurs == 0) { |
| 3961 | /* basically an elem* */ |
| 3962 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 3963 | ctxt->state); |
| 3964 | } |
| 3965 | } |
| 3966 | } else if ((elem->maxOccurs > 1) || (elem->minOccurs > 1)) { |
| 3967 | xmlAutomataStatePtr tmp; |
| 3968 | int counter; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3969 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3970 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
| 3971 | oldstate, NULL); |
| 3972 | oldstate = ctxt->state; |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3973 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3974 | counter = xmlAutomataNewCounter(ctxt->am, |
| 3975 | elem->minOccurs - 1, |
| 3976 | elem->maxOccurs - 1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3977 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3978 | if (elem->refDecl != NULL) { |
| 3979 | xmlSchemaBuildAContentModel((xmlSchemaTypePtr) |
| 3980 | elem->refDecl, ctxt, |
| 3981 | elem->refDecl->name); |
| 3982 | } else { |
| 3983 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
| 3984 | ctxt->state, |
| 3985 | NULL, |
| 3986 | elem->name, |
| 3987 | type); |
| 3988 | } |
| 3989 | tmp = ctxt->state; |
| 3990 | xmlAutomataNewCountedTrans(ctxt->am, tmp, oldstate, |
| 3991 | counter); |
| 3992 | ctxt->state = xmlAutomataNewCounterTrans(ctxt->am, tmp, |
| 3993 | NULL, |
| 3994 | counter); |
| 3995 | if (elem->minOccurs == 0) { |
| 3996 | /* basically an elem? */ |
| 3997 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 3998 | ctxt->state); |
| 3999 | } |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4000 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4001 | } else { |
| 4002 | if (elem->refDecl != NULL) { |
| 4003 | xmlSchemaBuildAContentModel((xmlSchemaTypePtr) |
| 4004 | elem->refDecl, ctxt, |
| 4005 | elem->refDecl->name); |
| 4006 | } else { |
| 4007 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
| 4008 | ctxt->state, |
| 4009 | NULL, |
| 4010 | elem->name, |
| 4011 | type); |
| 4012 | } |
| 4013 | if (elem->minOccurs == 0) { |
| 4014 | /* basically an elem? */ |
| 4015 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 4016 | ctxt->state); |
| 4017 | } |
| 4018 | } |
| 4019 | break; |
| 4020 | } |
| 4021 | case XML_SCHEMA_TYPE_SEQUENCE:{ |
| 4022 | xmlSchemaTypePtr subtypes; |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4023 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4024 | /* |
| 4025 | * If max and min occurances are default (1) then |
| 4026 | * simply iterate over the subtypes |
| 4027 | */ |
| 4028 | if ((type->minOccurs == 1) && (type->maxOccurs == 1)) { |
| 4029 | subtypes = type->subtypes; |
| 4030 | while (subtypes != NULL) { |
| 4031 | xmlSchemaBuildAContentModel(subtypes, ctxt, name); |
| 4032 | subtypes = subtypes->next; |
| 4033 | } |
| 4034 | } else { |
| 4035 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4036 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4037 | if (type->maxOccurs >= UNBOUNDED) { |
| 4038 | if (type->minOccurs > 1) { |
| 4039 | xmlAutomataStatePtr tmp; |
| 4040 | int counter; |
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 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
| 4043 | oldstate, |
| 4044 | NULL); |
| 4045 | oldstate = ctxt->state; |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4046 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4047 | counter = xmlAutomataNewCounter(ctxt->am, |
| 4048 | type-> |
| 4049 | minOccurs - 1, |
| 4050 | UNBOUNDED); |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4051 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4052 | subtypes = type->subtypes; |
| 4053 | while (subtypes != NULL) { |
| 4054 | xmlSchemaBuildAContentModel(subtypes, ctxt, |
| 4055 | name); |
| 4056 | subtypes = subtypes->next; |
| 4057 | } |
| 4058 | tmp = ctxt->state; |
| 4059 | xmlAutomataNewCountedTrans(ctxt->am, tmp, |
| 4060 | oldstate, counter); |
| 4061 | ctxt->state = |
| 4062 | xmlAutomataNewCounterTrans(ctxt->am, tmp, |
| 4063 | NULL, counter); |
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 | } else { |
| 4066 | subtypes = type->subtypes; |
| 4067 | while (subtypes != NULL) { |
| 4068 | xmlSchemaBuildAContentModel(subtypes, ctxt, |
| 4069 | name); |
| 4070 | subtypes = subtypes->next; |
| 4071 | } |
| 4072 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
| 4073 | oldstate); |
| 4074 | if (type->minOccurs == 0) { |
| 4075 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 4076 | ctxt->state); |
| 4077 | } |
| 4078 | } |
| 4079 | } else if ((type->maxOccurs > 1) |
| 4080 | || (type->minOccurs > 1)) { |
| 4081 | xmlAutomataStatePtr tmp; |
| 4082 | int counter; |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 4083 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4084 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
| 4085 | oldstate, |
| 4086 | NULL); |
| 4087 | oldstate = ctxt->state; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4088 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4089 | counter = xmlAutomataNewCounter(ctxt->am, |
| 4090 | type->minOccurs - |
| 4091 | 1, |
| 4092 | type->maxOccurs - |
| 4093 | 1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4094 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4095 | subtypes = type->subtypes; |
| 4096 | while (subtypes != NULL) { |
| 4097 | xmlSchemaBuildAContentModel(subtypes, ctxt, |
| 4098 | name); |
| 4099 | subtypes = subtypes->next; |
| 4100 | } |
| 4101 | tmp = ctxt->state; |
| 4102 | xmlAutomataNewCountedTrans(ctxt->am, tmp, oldstate, |
| 4103 | counter); |
| 4104 | ctxt->state = |
| 4105 | xmlAutomataNewCounterTrans(ctxt->am, tmp, NULL, |
| 4106 | counter); |
| 4107 | if (type->minOccurs == 0) { |
| 4108 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 4109 | ctxt->state); |
| 4110 | } |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 4111 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4112 | } else { |
| 4113 | subtypes = type->subtypes; |
| 4114 | while (subtypes != NULL) { |
| 4115 | xmlSchemaBuildAContentModel(subtypes, ctxt, |
| 4116 | name); |
| 4117 | subtypes = subtypes->next; |
| 4118 | } |
| 4119 | if (type->minOccurs == 0) { |
| 4120 | xmlAutomataNewEpsilon(ctxt->am, oldstate, |
| 4121 | ctxt->state); |
| 4122 | } |
| 4123 | } |
| 4124 | } |
| 4125 | break; |
| 4126 | } |
| 4127 | case XML_SCHEMA_TYPE_CHOICE:{ |
| 4128 | xmlSchemaTypePtr subtypes; |
| 4129 | xmlAutomataStatePtr start, end; |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 4130 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4131 | start = ctxt->state; |
| 4132 | end = xmlAutomataNewState(ctxt->am); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4133 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4134 | /* |
| 4135 | * iterate over the subtypes and remerge the end with an |
| 4136 | * epsilon transition |
| 4137 | */ |
| 4138 | if (type->maxOccurs == 1) { |
| 4139 | subtypes = type->subtypes; |
| 4140 | while (subtypes != NULL) { |
| 4141 | ctxt->state = start; |
| 4142 | xmlSchemaBuildAContentModel(subtypes, ctxt, name); |
| 4143 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, end); |
| 4144 | subtypes = subtypes->next; |
| 4145 | } |
| 4146 | } else { |
| 4147 | int counter; |
| 4148 | xmlAutomataStatePtr hop; |
| 4149 | int maxOccurs = type->maxOccurs == UNBOUNDED ? |
| 4150 | UNBOUNDED : type->maxOccurs - 1; |
| 4151 | int minOccurs = |
| 4152 | type->minOccurs < 1 ? 0 : type->minOccurs - 1; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4153 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4154 | /* |
| 4155 | * use a counter to keep track of the number of transtions |
| 4156 | * which went through the choice. |
| 4157 | */ |
| 4158 | counter = |
| 4159 | xmlAutomataNewCounter(ctxt->am, minOccurs, |
| 4160 | maxOccurs); |
| 4161 | hop = xmlAutomataNewState(ctxt->am); |
Daniel Veillard | 6231e84 | 2002-04-18 11:54:04 +0000 | [diff] [blame] | 4162 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4163 | subtypes = type->subtypes; |
| 4164 | while (subtypes != NULL) { |
| 4165 | ctxt->state = start; |
| 4166 | xmlSchemaBuildAContentModel(subtypes, ctxt, name); |
| 4167 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, hop); |
| 4168 | subtypes = subtypes->next; |
| 4169 | } |
| 4170 | xmlAutomataNewCountedTrans(ctxt->am, hop, start, |
| 4171 | counter); |
| 4172 | xmlAutomataNewCounterTrans(ctxt->am, hop, end, |
| 4173 | counter); |
| 4174 | } |
| 4175 | if (type->minOccurs == 0) { |
| 4176 | xmlAutomataNewEpsilon(ctxt->am, start, end); |
| 4177 | } |
| 4178 | ctxt->state = end; |
| 4179 | break; |
| 4180 | } |
| 4181 | case XML_SCHEMA_TYPE_ALL:{ |
| 4182 | xmlAutomataStatePtr start; |
| 4183 | xmlSchemaTypePtr subtypes; |
| 4184 | xmlSchemaElementPtr elem = (xmlSchemaElementPtr) type; |
| 4185 | int lax; |
| 4186 | |
| 4187 | subtypes = type->subtypes; |
| 4188 | if (subtypes == NULL) |
| 4189 | break; |
| 4190 | start = ctxt->state; |
| 4191 | while (subtypes != NULL) { |
| 4192 | ctxt->state = start; |
| 4193 | elem = (xmlSchemaElementPtr) subtypes; |
| 4194 | |
| 4195 | /* TODO : handle the namespace too */ |
| 4196 | if ((elem->minOccurs == 1) && (elem->maxOccurs == 1)) { |
| 4197 | xmlAutomataNewOnceTrans(ctxt->am, ctxt->state, |
| 4198 | ctxt->state, elem->name, 1, |
| 4199 | 1, subtypes); |
| 4200 | } else { |
| 4201 | xmlAutomataNewCountTrans(ctxt->am, ctxt->state, |
| 4202 | ctxt->state, elem->name, |
| 4203 | elem->minOccurs, |
| 4204 | elem->maxOccurs, |
| 4205 | subtypes); |
| 4206 | } |
| 4207 | subtypes = subtypes->next; |
| 4208 | } |
| 4209 | lax = type->minOccurs == 0; |
| 4210 | ctxt->state = |
| 4211 | xmlAutomataNewAllTrans(ctxt->am, ctxt->state, NULL, |
| 4212 | lax); |
| 4213 | break; |
| 4214 | } |
| 4215 | case XML_SCHEMA_TYPE_RESTRICTION: |
| 4216 | if (type->subtypes != NULL) |
| 4217 | xmlSchemaBuildAContentModel(type->subtypes, ctxt, name); |
| 4218 | break; |
| 4219 | case XML_SCHEMA_TYPE_EXTENSION: |
| 4220 | if (type->baseType != NULL) { |
| 4221 | xmlSchemaTypePtr subtypes; |
| 4222 | |
| 4223 | xmlSchemaBuildAContentModel(type->baseType, ctxt, name); |
| 4224 | subtypes = type->subtypes; |
| 4225 | while (subtypes != NULL) { |
| 4226 | xmlSchemaBuildAContentModel(subtypes, ctxt, name); |
| 4227 | subtypes = subtypes->next; |
| 4228 | } |
| 4229 | } else if (type->subtypes != NULL) |
| 4230 | xmlSchemaBuildAContentModel(type->subtypes, ctxt, name); |
| 4231 | break; |
| 4232 | case XML_SCHEMA_TYPE_GROUP: |
| 4233 | if (type->subtypes == NULL) { |
| 4234 | } |
| 4235 | case XML_SCHEMA_TYPE_COMPLEX: |
| 4236 | case XML_SCHEMA_TYPE_COMPLEX_CONTENT: |
| 4237 | if (type->subtypes != NULL) |
| 4238 | xmlSchemaBuildAContentModel(type->subtypes, ctxt, name); |
| 4239 | break; |
| 4240 | default: |
| 4241 | xmlGenericError(xmlGenericErrorContext, |
| 4242 | "Found unexpected type %d in %s content model\n", |
| 4243 | type->type, name); |
| 4244 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4245 | } |
| 4246 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4247 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4248 | /** |
| 4249 | * xmlSchemaBuildContentModel: |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4250 | * @elem: the element |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4251 | * @ctxt: the schema parser context |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4252 | * @name: the element name |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4253 | * |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4254 | * Builds the content model of the element. |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4255 | */ |
| 4256 | static void |
| 4257 | xmlSchemaBuildContentModel(xmlSchemaElementPtr elem, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4258 | xmlSchemaParserCtxtPtr ctxt, |
| 4259 | const xmlChar * name) |
| 4260 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4261 | xmlAutomataStatePtr start; |
| 4262 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4263 | if (elem->contModel != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4264 | return; |
Daniel Veillard | 88c5891 | 2002-04-23 07:12:20 +0000 | [diff] [blame] | 4265 | if (elem->subtypes == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4266 | elem->contentType = XML_SCHEMA_CONTENT_ANY; |
| 4267 | return; |
Daniel Veillard | 88c5891 | 2002-04-23 07:12:20 +0000 | [diff] [blame] | 4268 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4269 | if (elem->subtypes->type != XML_SCHEMA_TYPE_COMPLEX) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4270 | return; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 4271 | if ((elem->subtypes->contentType == XML_SCHEMA_CONTENT_BASIC) || |
| 4272 | (elem->subtypes->contentType == XML_SCHEMA_CONTENT_SIMPLE)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4273 | return; |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 4274 | |
| 4275 | #ifdef DEBUG_CONTENT |
| 4276 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4277 | "Building content model for %s\n", name); |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 4278 | #endif |
| 4279 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4280 | ctxt->am = xmlNewAutomata(); |
| 4281 | if (ctxt->am == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4282 | xmlGenericError(xmlGenericErrorContext, |
| 4283 | "Cannot create automata for elem %s\n", name); |
| 4284 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4285 | } |
| 4286 | start = ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 4287 | xmlSchemaBuildAContentModel(elem->subtypes, ctxt, name); |
| 4288 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
Daniel Veillard | 4402ab4 | 2002-09-12 16:02:56 +0000 | [diff] [blame] | 4289 | elem->contModel = xmlAutomataCompile(ctxt->am); |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4290 | if (elem->contModel == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4291 | xmlSchemaPErr(ctxt, elem->node, XML_SCHEMAS_ERR_INTERNAL, |
| 4292 | "failed to compile %s content model\n", name, NULL); |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4293 | } else if (xmlRegexpIsDeterminist(elem->contModel) != 1) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4294 | xmlSchemaPErr(ctxt, elem->node, XML_SCHEMAS_ERR_NOTDETERMINIST, |
| 4295 | "Content model of %s is not determinist:\n", name, |
| 4296 | NULL); |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 4297 | } else { |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 4298 | #ifdef DEBUG_CONTENT_REGEXP |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4299 | xmlGenericError(xmlGenericErrorContext, |
| 4300 | "Content model of %s:\n", name); |
| 4301 | xmlRegexpPrint(stderr, elem->contModel); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4302 | #endif |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 4303 | } |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 4304 | ctxt->state = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4305 | xmlFreeAutomata(ctxt->am); |
| 4306 | ctxt->am = NULL; |
| 4307 | } |
| 4308 | |
| 4309 | /** |
| 4310 | * xmlSchemaRefFixupCallback: |
| 4311 | * @elem: the schema element context |
| 4312 | * @ctxt: the schema parser context |
| 4313 | * |
| 4314 | * Free the resources associated to the schema parser context |
| 4315 | */ |
| 4316 | static void |
| 4317 | xmlSchemaRefFixupCallback(xmlSchemaElementPtr elem, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4318 | xmlSchemaParserCtxtPtr ctxt, |
| 4319 | const xmlChar * name, |
| 4320 | const xmlChar * context ATTRIBUTE_UNUSED, |
| 4321 | const xmlChar * namespace ATTRIBUTE_UNUSED) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4322 | { |
| 4323 | if ((ctxt == NULL) || (elem == NULL)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4324 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4325 | if (elem->ref != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4326 | xmlSchemaElementPtr elemDecl; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4327 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4328 | if (elem->subtypes != NULL) { |
| 4329 | xmlSchemaPErr(ctxt, elem->node, |
| 4330 | XML_SCHEMAP_INVALID_REF_AND_SUBTYPE, |
| 4331 | "Schemas: element %s have both ref and subtype\n", |
| 4332 | name, NULL); |
| 4333 | return; |
| 4334 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 4335 | elemDecl = xmlSchemaGetElem(ctxt->schema, elem->ref, elem->refNs, 0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4336 | |
| 4337 | if (elemDecl == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4338 | xmlSchemaPErr(ctxt, elem->node, XML_SCHEMAP_UNKNOWN_REF, |
| 4339 | "Schemas: element %s ref to %s not found\n", |
| 4340 | name, elem->ref); |
| 4341 | return; |
| 4342 | } |
| 4343 | elem->refDecl = elemDecl; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4344 | } else if (elem->namedType != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4345 | xmlSchemaTypePtr typeDecl; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4346 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4347 | if (elem->subtypes != NULL) { |
| 4348 | xmlSchemaPErr(ctxt, elem->node, XML_SCHEMAP_TYPE_AND_SUBTYPE, |
| 4349 | "Schemas: element %s have both type and subtype\n", |
| 4350 | name, NULL); |
| 4351 | return; |
| 4352 | } |
| 4353 | typeDecl = xmlSchemaGetType(ctxt->schema, elem->namedType, |
| 4354 | elem->namedTypeNs); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4355 | |
| 4356 | if (typeDecl == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4357 | xmlSchemaPErr(ctxt, elem->node, XML_SCHEMAP_UNKNOWN_TYPE, |
| 4358 | "Schemas: element %s type %s not found\n", name, |
| 4359 | elem->namedType); |
| 4360 | return; |
| 4361 | } |
| 4362 | elem->subtypes = typeDecl; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4363 | } |
| 4364 | } |
| 4365 | |
| 4366 | /** |
| 4367 | * xmlSchemaTypeFixup: |
| 4368 | * @typeDecl: the schema type definition |
| 4369 | * @ctxt: the schema parser context |
| 4370 | * |
| 4371 | * Fixes the content model of the type. |
| 4372 | */ |
| 4373 | static void |
| 4374 | xmlSchemaTypeFixup(xmlSchemaTypePtr typeDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4375 | xmlSchemaParserCtxtPtr ctxt, const xmlChar * name) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4376 | { |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 4377 | if (typeDecl == NULL) |
| 4378 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4379 | if (name == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4380 | name = typeDecl->name; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4381 | if (typeDecl->contentType == XML_SCHEMA_CONTENT_UNKNOWN) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4382 | switch (typeDecl->type) { |
| 4383 | case XML_SCHEMA_TYPE_SIMPLE_CONTENT:{ |
| 4384 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, NULL); |
| 4385 | if (typeDecl->subtypes != NULL) |
| 4386 | typeDecl->contentType = |
| 4387 | typeDecl->subtypes->contentType; |
| 4388 | break; |
| 4389 | } |
| 4390 | case XML_SCHEMA_TYPE_RESTRICTION:{ |
| 4391 | if (typeDecl->subtypes != NULL) |
| 4392 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4393 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4394 | if (typeDecl->base != NULL) { |
| 4395 | xmlSchemaTypePtr baseType; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4396 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4397 | baseType = |
| 4398 | xmlSchemaGetType(ctxt->schema, typeDecl->base, |
| 4399 | typeDecl->baseNs); |
| 4400 | if (baseType == NULL) { |
| 4401 | xmlSchemaPErr(ctxt, typeDecl->node, |
| 4402 | XML_SCHEMAP_UNKNOWN_BASE_TYPE, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4403 | "Schemas: type %s base type %s not found\n", |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4404 | name, typeDecl->base); |
| 4405 | } |
| 4406 | typeDecl->baseType = baseType; |
| 4407 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 4408 | if (typeDecl->subtypes == NULL) |
| 4409 | if (typeDecl->baseType != NULL) |
| 4410 | typeDecl->contentType = |
| 4411 | typeDecl->baseType->contentType; |
| 4412 | else |
| 4413 | /* 1.1.1 */ |
| 4414 | typeDecl->contentType = XML_SCHEMA_CONTENT_EMPTY; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4415 | else if ((typeDecl->subtypes->subtypes == NULL) && |
| 4416 | ((typeDecl->subtypes->type == |
| 4417 | XML_SCHEMA_TYPE_ALL) |
| 4418 | || (typeDecl->subtypes->type == |
| 4419 | XML_SCHEMA_TYPE_SEQUENCE))) |
| 4420 | /* 1.1.2 */ |
| 4421 | typeDecl->contentType = XML_SCHEMA_CONTENT_EMPTY; |
| 4422 | else if ((typeDecl->subtypes->type == |
| 4423 | XML_SCHEMA_TYPE_CHOICE) |
| 4424 | && (typeDecl->subtypes->subtypes == NULL)) |
| 4425 | /* 1.1.3 */ |
| 4426 | typeDecl->contentType = XML_SCHEMA_CONTENT_EMPTY; |
| 4427 | else { |
| 4428 | /* 1.2 and 2.X are applied at the other layer */ |
| 4429 | typeDecl->contentType = |
| 4430 | XML_SCHEMA_CONTENT_ELEMENTS; |
| 4431 | } |
| 4432 | break; |
| 4433 | } |
| 4434 | case XML_SCHEMA_TYPE_EXTENSION:{ |
| 4435 | xmlSchemaContentType explicitContentType; |
| 4436 | xmlSchemaTypePtr base; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4437 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4438 | if (typeDecl->base != NULL) { |
| 4439 | xmlSchemaTypePtr baseType; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4440 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4441 | baseType = |
| 4442 | xmlSchemaGetType(ctxt->schema, typeDecl->base, |
| 4443 | typeDecl->baseNs); |
| 4444 | if (baseType == NULL) { |
| 4445 | xmlSchemaPErr(ctxt, typeDecl->node, |
| 4446 | XML_SCHEMAP_UNKNOWN_BASE_TYPE, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4447 | "Schemas: type %s base type %s not found\n", |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4448 | name, typeDecl->base); |
| 4449 | } |
| 4450 | typeDecl->baseType = baseType; |
| 4451 | } |
| 4452 | if (typeDecl->subtypes != NULL) |
| 4453 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4454 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4455 | explicitContentType = XML_SCHEMA_CONTENT_ELEMENTS; |
| 4456 | if (typeDecl->subtypes == NULL) |
| 4457 | /* 1.1.1 */ |
| 4458 | explicitContentType = XML_SCHEMA_CONTENT_EMPTY; |
| 4459 | else if ((typeDecl->subtypes->subtypes == NULL) && |
| 4460 | ((typeDecl->subtypes->type == |
| 4461 | XML_SCHEMA_TYPE_ALL) |
| 4462 | || (typeDecl->subtypes->type == |
| 4463 | XML_SCHEMA_TYPE_SEQUENCE))) |
| 4464 | /* 1.1.2 */ |
| 4465 | explicitContentType = XML_SCHEMA_CONTENT_EMPTY; |
| 4466 | else if ((typeDecl->subtypes->type == |
| 4467 | XML_SCHEMA_TYPE_CHOICE) |
| 4468 | && (typeDecl->subtypes->subtypes == NULL)) |
| 4469 | /* 1.1.3 */ |
| 4470 | explicitContentType = XML_SCHEMA_CONTENT_EMPTY; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4471 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4472 | base = xmlSchemaGetType(ctxt->schema, typeDecl->base, |
| 4473 | typeDecl->baseNs); |
| 4474 | if (base == NULL) { |
| 4475 | xmlSchemaPErr(ctxt, typeDecl->node, |
| 4476 | XML_SCHEMAP_UNKNOWN_BASE_TYPE, |
| 4477 | "Schemas: base type %s of type %s not found\n", |
| 4478 | typeDecl->base, name); |
| 4479 | return; |
| 4480 | } |
| 4481 | xmlSchemaTypeFixup(base, ctxt, NULL); |
| 4482 | if (explicitContentType == XML_SCHEMA_CONTENT_EMPTY) { |
| 4483 | /* 2.1 */ |
| 4484 | typeDecl->contentType = base->contentType; |
| 4485 | } else if (base->contentType == |
| 4486 | XML_SCHEMA_CONTENT_EMPTY) { |
| 4487 | /* 2.2 imbitable ! */ |
| 4488 | typeDecl->contentType = |
| 4489 | XML_SCHEMA_CONTENT_ELEMENTS; |
| 4490 | } else { |
| 4491 | /* 2.3 imbitable pareil ! */ |
| 4492 | typeDecl->contentType = |
| 4493 | XML_SCHEMA_CONTENT_ELEMENTS; |
| 4494 | } |
| 4495 | break; |
| 4496 | } |
| 4497 | case XML_SCHEMA_TYPE_COMPLEX:{ |
| 4498 | if (typeDecl->subtypes == NULL) { |
| 4499 | typeDecl->contentType = XML_SCHEMA_CONTENT_EMPTY; |
Daniel Veillard | 1aefc86 | 2004-03-04 11:40:48 +0000 | [diff] [blame^] | 4500 | |
| 4501 | if (typeDecl->flags & XML_SCHEMAS_TYPE_MIXED) |
| 4502 | typeDecl->contentType = |
| 4503 | XML_SCHEMA_CONTENT_MIXED; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4504 | } else { |
| 4505 | if (typeDecl->flags & XML_SCHEMAS_TYPE_MIXED) |
| 4506 | typeDecl->contentType = |
| 4507 | XML_SCHEMA_CONTENT_MIXED; |
| 4508 | else { |
| 4509 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, |
| 4510 | NULL); |
| 4511 | if (typeDecl->subtypes != NULL) |
| 4512 | typeDecl->contentType = |
| 4513 | typeDecl->subtypes->contentType; |
| 4514 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 4515 | if (typeDecl->attributes == NULL) |
| 4516 | typeDecl->attributes = |
| 4517 | typeDecl->subtypes->attributes; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4518 | } |
| 4519 | break; |
| 4520 | } |
| 4521 | case XML_SCHEMA_TYPE_COMPLEX_CONTENT:{ |
| 4522 | if (typeDecl->subtypes == NULL) { |
| 4523 | typeDecl->contentType = XML_SCHEMA_CONTENT_EMPTY; |
Daniel Veillard | 1aefc86 | 2004-03-04 11:40:48 +0000 | [diff] [blame^] | 4524 | if (typeDecl->flags & XML_SCHEMAS_TYPE_MIXED) |
| 4525 | typeDecl->contentType = |
| 4526 | XML_SCHEMA_CONTENT_MIXED; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4527 | } else { |
| 4528 | if (typeDecl->flags & XML_SCHEMAS_TYPE_MIXED) |
| 4529 | typeDecl->contentType = |
| 4530 | XML_SCHEMA_CONTENT_MIXED; |
| 4531 | else { |
| 4532 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, |
| 4533 | NULL); |
| 4534 | if (typeDecl->subtypes != NULL) |
| 4535 | typeDecl->contentType = |
| 4536 | typeDecl->subtypes->contentType; |
| 4537 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 4538 | if (typeDecl->attributes == NULL) |
| 4539 | typeDecl->attributes = |
| 4540 | typeDecl->subtypes->attributes; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4541 | } |
| 4542 | break; |
| 4543 | } |
| 4544 | case XML_SCHEMA_TYPE_SEQUENCE: |
| 4545 | case XML_SCHEMA_TYPE_GROUP: |
| 4546 | case XML_SCHEMA_TYPE_ALL: |
| 4547 | case XML_SCHEMA_TYPE_CHOICE: |
| 4548 | typeDecl->contentType = XML_SCHEMA_CONTENT_ELEMENTS; |
| 4549 | break; |
| 4550 | case XML_SCHEMA_TYPE_BASIC: |
| 4551 | case XML_SCHEMA_TYPE_ANY: |
| 4552 | case XML_SCHEMA_TYPE_FACET: |
| 4553 | case XML_SCHEMA_TYPE_SIMPLE: |
| 4554 | case XML_SCHEMA_TYPE_UR: |
| 4555 | case XML_SCHEMA_TYPE_ELEMENT: |
| 4556 | case XML_SCHEMA_TYPE_ATTRIBUTE: |
| 4557 | case XML_SCHEMA_TYPE_ATTRIBUTEGROUP: |
| 4558 | case XML_SCHEMA_TYPE_NOTATION: |
| 4559 | case XML_SCHEMA_TYPE_LIST: |
| 4560 | case XML_SCHEMA_TYPE_UNION: |
| 4561 | case XML_SCHEMA_FACET_MININCLUSIVE: |
| 4562 | case XML_SCHEMA_FACET_MINEXCLUSIVE: |
| 4563 | case XML_SCHEMA_FACET_MAXINCLUSIVE: |
| 4564 | case XML_SCHEMA_FACET_MAXEXCLUSIVE: |
| 4565 | case XML_SCHEMA_FACET_TOTALDIGITS: |
| 4566 | case XML_SCHEMA_FACET_FRACTIONDIGITS: |
| 4567 | case XML_SCHEMA_FACET_PATTERN: |
| 4568 | case XML_SCHEMA_FACET_ENUMERATION: |
| 4569 | case XML_SCHEMA_FACET_WHITESPACE: |
| 4570 | case XML_SCHEMA_FACET_LENGTH: |
| 4571 | case XML_SCHEMA_FACET_MAXLENGTH: |
| 4572 | case XML_SCHEMA_FACET_MINLENGTH: |
| 4573 | typeDecl->contentType = XML_SCHEMA_CONTENT_SIMPLE; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 4574 | if (typeDecl->subtypes != NULL) |
| 4575 | xmlSchemaTypeFixup(typeDecl->subtypes, ctxt, NULL); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4576 | break; |
| 4577 | } |
| 4578 | } |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4579 | #ifdef DEBUG_TYPE |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 4580 | if (typeDecl->node != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4581 | xmlGenericError(xmlGenericErrorContext, |
| 4582 | "Type of %s : %s:%d :", name, |
| 4583 | typeDecl->node->doc->URL, |
| 4584 | xmlGetLineNo(typeDecl->node)); |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 4585 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4586 | xmlGenericError(xmlGenericErrorContext, "Type of %s :", name); |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 4587 | } |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4588 | switch (typeDecl->contentType) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4589 | case XML_SCHEMA_CONTENT_SIMPLE: |
| 4590 | xmlGenericError(xmlGenericErrorContext, "simple\n"); |
| 4591 | break; |
| 4592 | case XML_SCHEMA_CONTENT_ELEMENTS: |
| 4593 | xmlGenericError(xmlGenericErrorContext, "elements\n"); |
| 4594 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4595 | case XML_SCHEMA_CONTENT_UNKNOWN: |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4596 | xmlGenericError(xmlGenericErrorContext, "unknown !!!\n"); |
| 4597 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4598 | case XML_SCHEMA_CONTENT_EMPTY: |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4599 | xmlGenericError(xmlGenericErrorContext, "empty\n"); |
| 4600 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4601 | case XML_SCHEMA_CONTENT_MIXED: |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4602 | xmlGenericError(xmlGenericErrorContext, "mixed\n"); |
| 4603 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4604 | case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS: |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4605 | xmlGenericError(xmlGenericErrorContext, "mixed or elems\n"); |
| 4606 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4607 | case XML_SCHEMA_CONTENT_BASIC: |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4608 | xmlGenericError(xmlGenericErrorContext, "basic\n"); |
| 4609 | break; |
| 4610 | default: |
| 4611 | xmlGenericError(xmlGenericErrorContext, |
| 4612 | "not registered !!!\n"); |
| 4613 | break; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4614 | } |
| 4615 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4616 | } |
| 4617 | |
| 4618 | /** |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4619 | * xmlSchemaCheckFacet: |
| 4620 | * @facet: the facet |
| 4621 | * @typeDecl: the schema type definition |
| 4622 | * @ctxt: the schema parser context or NULL |
| 4623 | * @name: name of the type |
| 4624 | * |
| 4625 | * Checks the default values types, especially for facets |
| 4626 | * |
| 4627 | * Returns 0 if okay or -1 in cae of error |
| 4628 | */ |
| 4629 | int |
| 4630 | xmlSchemaCheckFacet(xmlSchemaFacetPtr facet, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4631 | xmlSchemaTypePtr typeDecl, |
| 4632 | xmlSchemaParserCtxtPtr ctxt, const xmlChar * name) |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4633 | { |
| 4634 | static xmlSchemaTypePtr nonNegativeIntegerType = NULL; |
| 4635 | int ret = 0; |
| 4636 | |
| 4637 | if (nonNegativeIntegerType == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4638 | nonNegativeIntegerType = |
| 4639 | xmlSchemaGetPredefinedType(BAD_CAST "nonNegativeInteger", |
| 4640 | xmlSchemaNs); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4641 | } |
| 4642 | switch (facet->type) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4643 | case XML_SCHEMA_FACET_MININCLUSIVE: |
| 4644 | case XML_SCHEMA_FACET_MINEXCLUSIVE: |
| 4645 | case XML_SCHEMA_FACET_MAXINCLUSIVE: |
| 4646 | case XML_SCHEMA_FACET_MAXEXCLUSIVE:{ |
| 4647 | /* |
| 4648 | * Okay we need to validate the value |
| 4649 | * at that point. |
| 4650 | */ |
| 4651 | xmlSchemaValidCtxtPtr vctxt; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4652 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4653 | vctxt = xmlSchemaNewValidCtxt(NULL); |
| 4654 | if (vctxt == NULL) |
| 4655 | break; |
| 4656 | xmlSchemaValidateSimpleValue(vctxt, typeDecl, |
| 4657 | facet->value); |
| 4658 | facet->val = vctxt->value; |
| 4659 | vctxt->value = NULL; |
| 4660 | if (facet->val == NULL) { |
| 4661 | /* error code */ |
| 4662 | if (ctxt != NULL) { |
| 4663 | xmlSchemaPErr(ctxt, facet->node, |
| 4664 | XML_SCHEMAP_INVALID_FACET, |
| 4665 | "Schemas: type %s facet value %s invalid\n", |
| 4666 | name, facet->value); |
| 4667 | } |
| 4668 | ret = -1; |
| 4669 | } |
| 4670 | xmlSchemaFreeValidCtxt(vctxt); |
| 4671 | break; |
| 4672 | } |
| 4673 | case XML_SCHEMA_FACET_ENUMERATION:{ |
| 4674 | /* |
| 4675 | * Okay we need to validate the value |
| 4676 | * at that point. |
| 4677 | */ |
| 4678 | xmlSchemaValidCtxtPtr vctxt; |
| 4679 | int tmp; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4680 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4681 | vctxt = xmlSchemaNewValidCtxt(NULL); |
| 4682 | if (vctxt == NULL) |
| 4683 | break; |
| 4684 | tmp = xmlSchemaValidateSimpleValue(vctxt, typeDecl, |
| 4685 | facet->value); |
| 4686 | if (tmp != 0) { |
| 4687 | if (ctxt != NULL) { |
| 4688 | xmlSchemaPErr(ctxt, facet->node, |
| 4689 | XML_SCHEMAP_INVALID_ENUM, |
| 4690 | "Schemas: type %s enumeration value %s invalid\n", |
| 4691 | name, facet->value); |
| 4692 | } |
| 4693 | ret = -1; |
| 4694 | } |
| 4695 | xmlSchemaFreeValidCtxt(vctxt); |
| 4696 | break; |
| 4697 | } |
| 4698 | case XML_SCHEMA_FACET_PATTERN: |
| 4699 | facet->regexp = xmlRegexpCompile(facet->value); |
| 4700 | if (facet->regexp == NULL) { |
| 4701 | xmlSchemaPErr(ctxt, typeDecl->node, |
| 4702 | XML_SCHEMAP_REGEXP_INVALID, |
| 4703 | "Schemas: type %s facet regexp %s invalid\n", |
| 4704 | name, facet->value); |
| 4705 | ret = -1; |
| 4706 | } |
| 4707 | break; |
| 4708 | case XML_SCHEMA_FACET_TOTALDIGITS: |
| 4709 | case XML_SCHEMA_FACET_FRACTIONDIGITS: |
| 4710 | case XML_SCHEMA_FACET_LENGTH: |
| 4711 | case XML_SCHEMA_FACET_MAXLENGTH: |
| 4712 | case XML_SCHEMA_FACET_MINLENGTH:{ |
| 4713 | int tmp; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4714 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4715 | tmp = |
| 4716 | xmlSchemaValidatePredefinedType(nonNegativeIntegerType, |
| 4717 | facet->value, |
| 4718 | &facet->val); |
| 4719 | if (tmp != 0) { |
| 4720 | /* error code */ |
| 4721 | if (ctxt != NULL) { |
| 4722 | xmlSchemaPErr(ctxt, facet->node, |
| 4723 | XML_SCHEMAP_INVALID_FACET_VALUE, |
| 4724 | "Schemas: type %s facet value %s invalid\n", |
| 4725 | name, facet->value); |
| 4726 | } |
| 4727 | ret = -1; |
| 4728 | } |
| 4729 | break; |
| 4730 | } |
| 4731 | case XML_SCHEMA_FACET_WHITESPACE:{ |
| 4732 | if (xmlStrEqual(facet->value, BAD_CAST "preserve")) { |
| 4733 | facet->whitespace = XML_SCHEMAS_FACET_PRESERVE; |
| 4734 | } else if (xmlStrEqual(facet->value, BAD_CAST "replace")) { |
| 4735 | facet->whitespace = XML_SCHEMAS_FACET_REPLACE; |
| 4736 | } else if (xmlStrEqual(facet->value, BAD_CAST "collapse")) { |
| 4737 | facet->whitespace = XML_SCHEMAS_FACET_COLLAPSE; |
| 4738 | } else { |
| 4739 | if (ctxt != NULL) { |
| 4740 | xmlSchemaPErr(ctxt, facet->node, |
| 4741 | XML_SCHEMAP_INVALID_WHITE_SPACE, |
| 4742 | "Schemas: type %s whiteSpace value %s invalid\n", |
| 4743 | name, facet->value); |
| 4744 | } |
| 4745 | ret = -1; |
| 4746 | } |
| 4747 | } |
| 4748 | default: |
| 4749 | break; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4750 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4751 | return (ret); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4752 | } |
| 4753 | |
| 4754 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4755 | * xmlSchemaCheckDefaults: |
| 4756 | * @typeDecl: the schema type definition |
| 4757 | * @ctxt: the schema parser context |
| 4758 | * |
| 4759 | * Checks the default values types, especially for facets |
| 4760 | */ |
| 4761 | static void |
| 4762 | xmlSchemaCheckDefaults(xmlSchemaTypePtr typeDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4763 | xmlSchemaParserCtxtPtr ctxt, const xmlChar * name) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4764 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4765 | if (name == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4766 | name = typeDecl->name; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4767 | if (typeDecl->type == XML_SCHEMA_TYPE_RESTRICTION) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4768 | if (typeDecl->facets != NULL) { |
| 4769 | xmlSchemaFacetPtr facet = typeDecl->facets; |
| 4770 | |
| 4771 | while (facet != NULL) { |
| 4772 | xmlSchemaCheckFacet(facet, typeDecl, ctxt, name); |
| 4773 | facet = facet->next; |
| 4774 | } |
| 4775 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4776 | } |
| 4777 | } |
| 4778 | |
| 4779 | /** |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4780 | * xmlSchemaAttrGrpFixup: |
| 4781 | * @attrgrpDecl: the schema attribute definition |
| 4782 | * @ctxt: the schema parser context |
| 4783 | * @name: the attribute name |
| 4784 | * |
| 4785 | * Fixes finish doing the computations on the attributes definitions |
| 4786 | */ |
| 4787 | static void |
| 4788 | xmlSchemaAttrGrpFixup(xmlSchemaAttributeGroupPtr attrgrpDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4789 | xmlSchemaParserCtxtPtr ctxt, const xmlChar * name) |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4790 | { |
| 4791 | if (name == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4792 | name = attrgrpDecl->name; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4793 | if (attrgrpDecl->attributes != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4794 | return; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4795 | if (attrgrpDecl->ref != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4796 | xmlSchemaAttributeGroupPtr ref; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4797 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4798 | ref = xmlHashLookup2(ctxt->schema->attrgrpDecl, attrgrpDecl->ref, |
| 4799 | attrgrpDecl->refNs); |
| 4800 | if (ref == NULL) { |
| 4801 | xmlSchemaPErr(ctxt, attrgrpDecl->node, |
| 4802 | XML_SCHEMAP_UNKNOWN_ATTRIBUTE_GROUP, |
| 4803 | "Schemas: attribute group %s reference %s not found\n", |
| 4804 | name, attrgrpDecl->ref); |
| 4805 | return; |
| 4806 | } |
| 4807 | xmlSchemaAttrGrpFixup(ref, ctxt, NULL); |
| 4808 | attrgrpDecl->attributes = ref->attributes; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4809 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4810 | xmlSchemaPErr(ctxt, attrgrpDecl->node, XML_SCHEMAP_NOATTR_NOREF, |
| 4811 | "Schemas: attribute %s has no attributes nor reference\n", |
| 4812 | name, NULL); |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 4813 | } |
| 4814 | } |
| 4815 | |
| 4816 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4817 | * xmlSchemaAttrFixup: |
| 4818 | * @attrDecl: the schema attribute definition |
| 4819 | * @ctxt: the schema parser context |
| 4820 | * @name: the attribute name |
| 4821 | * |
| 4822 | * Fixes finish doing the computations on the attributes definitions |
| 4823 | */ |
| 4824 | static void |
| 4825 | xmlSchemaAttrFixup(xmlSchemaAttributePtr attrDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4826 | xmlSchemaParserCtxtPtr ctxt, const xmlChar * name) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4827 | { |
| 4828 | if (name == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4829 | name = attrDecl->name; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4830 | if (attrDecl->subtypes != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4831 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4832 | if (attrDecl->typeName != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4833 | xmlSchemaTypePtr type; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4834 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4835 | type = xmlSchemaGetType(ctxt->schema, attrDecl->typeName, |
| 4836 | attrDecl->typeNs); |
| 4837 | if (type == NULL) { |
| 4838 | xmlSchemaPErr(ctxt, attrDecl->node, XML_SCHEMAP_UNKNOWN_TYPE, |
| 4839 | "Schemas: attribute %s type %s not found\n", |
| 4840 | name, attrDecl->typeName); |
| 4841 | } |
| 4842 | attrDecl->subtypes = type; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4843 | } else if (attrDecl->ref != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4844 | xmlSchemaAttributePtr ref; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4845 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4846 | ref = xmlHashLookup2(ctxt->schema->attrDecl, attrDecl->ref, |
| 4847 | attrDecl->refNs); |
| 4848 | if (ref == NULL) { |
| 4849 | xmlSchemaPErr(ctxt, attrDecl->node, XML_SCHEMAP_UNKNOWN_REF, |
| 4850 | "Schemas: attribute %s reference %s not found\n", |
| 4851 | name, attrDecl->ref); |
| 4852 | return; |
| 4853 | } |
| 4854 | xmlSchemaAttrFixup(ref, ctxt, NULL); |
| 4855 | attrDecl->subtypes = ref->subtypes; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4856 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4857 | xmlSchemaPErr(ctxt, attrDecl->node, XML_SCHEMAP_NOTYPE_NOREF, |
| 4858 | "Schemas: attribute %s has no type nor reference\n", |
| 4859 | name, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4860 | } |
| 4861 | } |
| 4862 | |
| 4863 | /** |
| 4864 | * xmlSchemaParse: |
| 4865 | * @ctxt: a schema validation context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4866 | * |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4867 | * parse a schema definition resource and build an internal |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4868 | * XML Shema struture which can be used to validate instances. |
| 4869 | * *WARNING* this interface is highly subject to change |
| 4870 | * |
| 4871 | * Returns the internal XML Schema structure built from the resource or |
| 4872 | * NULL in case of error |
| 4873 | */ |
| 4874 | xmlSchemaPtr |
| 4875 | xmlSchemaParse(xmlSchemaParserCtxtPtr ctxt) |
| 4876 | { |
| 4877 | xmlSchemaPtr ret = NULL; |
| 4878 | xmlDocPtr doc; |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 4879 | xmlNodePtr root; |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 4880 | int nberrors; |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 4881 | int preserve = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4882 | |
| 4883 | xmlSchemaInitTypes(); |
| 4884 | |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 4885 | if (ctxt == NULL) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4886 | return (NULL); |
| 4887 | |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 4888 | nberrors = ctxt->nberrors; |
| 4889 | ctxt->nberrors = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4890 | ctxt->counter = 0; |
| 4891 | ctxt->container = NULL; |
| 4892 | |
| 4893 | /* |
| 4894 | * First step is to parse the input document into an DOM/Infoset |
| 4895 | */ |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 4896 | if (ctxt->URL != NULL) { |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 4897 | doc = xmlReadFile((const char *) ctxt->URL, NULL, |
| 4898 | SCHEMAS_PARSE_OPTIONS); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4899 | if (doc == NULL) { |
| 4900 | xmlSchemaPErr(ctxt, NULL, |
| 4901 | XML_SCHEMAP_FAILED_LOAD, |
| 4902 | "xmlSchemaParse: could not load %s\n", |
| 4903 | ctxt->URL, NULL); |
| 4904 | return (NULL); |
| 4905 | } |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 4906 | } else if (ctxt->buffer != NULL) { |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 4907 | doc = xmlReadMemory(ctxt->buffer, ctxt->size, NULL, NULL, |
| 4908 | SCHEMAS_PARSE_OPTIONS); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4909 | if (doc == NULL) { |
| 4910 | xmlSchemaPErr(ctxt, NULL, |
| 4911 | XML_SCHEMAP_FAILED_PARSE, |
| 4912 | "xmlSchemaParse: could not parse\n", |
| 4913 | NULL, NULL); |
| 4914 | return (NULL); |
| 4915 | } |
| 4916 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
Daniel Veillard | 6576528 | 2004-01-08 16:59:30 +0000 | [diff] [blame] | 4917 | ctxt->URL = xmlDictLookup(ctxt->dict, BAD_CAST "in_memory_buffer", -1); |
Daniel Veillard | 9d75150 | 2003-10-29 13:21:47 +0000 | [diff] [blame] | 4918 | } else if (ctxt->doc != NULL) { |
| 4919 | doc = ctxt->doc; |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 4920 | preserve = 1; |
Daniel Veillard | 6045c90 | 2002-10-09 21:13:59 +0000 | [diff] [blame] | 4921 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4922 | xmlSchemaPErr(ctxt, NULL, |
| 4923 | XML_SCHEMAP_NOTHING_TO_PARSE, |
| 4924 | "xmlSchemaParse: could not parse\n", |
| 4925 | NULL, NULL); |
| 4926 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4927 | } |
| 4928 | |
| 4929 | /* |
| 4930 | * Then extract the root and Schema parse it |
| 4931 | */ |
| 4932 | root = xmlDocGetRootElement(doc); |
| 4933 | if (root == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4934 | xmlSchemaPErr(ctxt, (xmlNodePtr) doc, |
| 4935 | XML_SCHEMAP_NOROOT, |
| 4936 | "schemas has no root", NULL, NULL); |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 4937 | if (!preserve) { |
| 4938 | xmlFreeDoc(doc); |
| 4939 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4940 | return (NULL); |
| 4941 | } |
| 4942 | |
| 4943 | /* |
| 4944 | * Remove all the blank text nodes |
| 4945 | */ |
Daniel Veillard | bd2904b | 2003-11-25 15:38:59 +0000 | [diff] [blame] | 4946 | xmlSchemaCleanupDoc(ctxt, root); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4947 | |
| 4948 | /* |
| 4949 | * Then do the parsing for good |
| 4950 | */ |
| 4951 | ret = xmlSchemaParseSchema(ctxt, root); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 4952 | if (ret == NULL) { |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 4953 | if (!preserve) { |
| 4954 | xmlFreeDoc(doc); |
| 4955 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4956 | return (NULL); |
Daniel Veillard | 1d91386 | 2003-11-21 00:28:39 +0000 | [diff] [blame] | 4957 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4958 | ret->doc = doc; |
Daniel Veillard | dda22c1 | 2004-01-24 08:31:30 +0000 | [diff] [blame] | 4959 | ret->preserve = preserve; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4960 | |
| 4961 | /* |
| 4962 | * Then fix all the references. |
| 4963 | */ |
| 4964 | ctxt->schema = ret; |
| 4965 | xmlHashScanFull(ret->elemDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4966 | (xmlHashScannerFull) xmlSchemaRefFixupCallback, ctxt); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4967 | |
| 4968 | /* |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 4969 | * Then fixup all attributes declarations |
| 4970 | */ |
| 4971 | xmlHashScan(ret->attrDecl, (xmlHashScanner) xmlSchemaAttrFixup, ctxt); |
| 4972 | |
| 4973 | /* |
| 4974 | * Then fixup all attributes group declarations |
| 4975 | */ |
| 4976 | xmlHashScan(ret->attrgrpDecl, (xmlHashScanner) xmlSchemaAttrGrpFixup, |
| 4977 | ctxt); |
| 4978 | |
| 4979 | /* |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4980 | * Then fixup all types properties |
| 4981 | */ |
| 4982 | xmlHashScan(ret->typeDecl, (xmlHashScanner) xmlSchemaTypeFixup, ctxt); |
| 4983 | |
| 4984 | /* |
| 4985 | * Then build the content model for all elements |
| 4986 | */ |
| 4987 | xmlHashScan(ret->elemDecl, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4988 | (xmlHashScanner) xmlSchemaBuildContentModel, ctxt); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4989 | |
| 4990 | /* |
| 4991 | * Then check the defaults part of the type like facets values |
| 4992 | */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4993 | xmlHashScan(ret->typeDecl, (xmlHashScanner) xmlSchemaCheckDefaults, |
| 4994 | ctxt); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4995 | |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 4996 | if (ctxt->nberrors != 0) { |
| 4997 | xmlSchemaFree(ret); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 4998 | ret = NULL; |
Daniel Veillard | 75bb3bb | 2003-05-12 15:25:56 +0000 | [diff] [blame] | 4999 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5000 | return (ret); |
| 5001 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5002 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5003 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5004 | * xmlSchemaSetParserErrors: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5005 | * @ctxt: a schema validation context |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5006 | * @err: the error callback |
| 5007 | * @warn: the warning callback |
| 5008 | * @ctx: contextual data for the callbacks |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5009 | * |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5010 | * Set the callback functions used to handle errors for a validation context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5011 | */ |
| 5012 | void |
| 5013 | xmlSchemaSetParserErrors(xmlSchemaParserCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5014 | xmlSchemaValidityErrorFunc err, |
| 5015 | xmlSchemaValidityWarningFunc warn, void *ctx) |
| 5016 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5017 | if (ctxt == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5018 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5019 | ctxt->error = err; |
| 5020 | ctxt->warning = warn; |
| 5021 | ctxt->userData = ctx; |
| 5022 | } |
| 5023 | |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5024 | /** |
| 5025 | * xmlSchemaFacetTypeToString: |
| 5026 | * @type: the facet type |
| 5027 | * |
| 5028 | * Convert the xmlSchemaTypeType to a char string. |
| 5029 | * |
| 5030 | * Returns the char string representation of the facet type if the |
| 5031 | * type is a facet and an "Internal Error" string otherwise. |
| 5032 | */ |
| 5033 | static const char * |
| 5034 | xmlSchemaFacetTypeToString(xmlSchemaTypeType type) |
| 5035 | { |
| 5036 | switch (type) { |
| 5037 | case XML_SCHEMA_FACET_PATTERN: |
| 5038 | return ("pattern"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5039 | case XML_SCHEMA_FACET_MAXEXCLUSIVE: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5040 | return ("maxExclusive"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5041 | case XML_SCHEMA_FACET_MAXINCLUSIVE: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5042 | return ("maxInclusive"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5043 | case XML_SCHEMA_FACET_MINEXCLUSIVE: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5044 | return ("minExclusive"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5045 | case XML_SCHEMA_FACET_MININCLUSIVE: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5046 | return ("minInclusive"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5047 | case XML_SCHEMA_FACET_WHITESPACE: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5048 | return ("whiteSpace"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5049 | case XML_SCHEMA_FACET_ENUMERATION: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5050 | return ("enumeration"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5051 | case XML_SCHEMA_FACET_LENGTH: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5052 | return ("length"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5053 | case XML_SCHEMA_FACET_MAXLENGTH: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5054 | return ("maxLength"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5055 | case XML_SCHEMA_FACET_MINLENGTH: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5056 | return ("minLength"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5057 | case XML_SCHEMA_FACET_TOTALDIGITS: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5058 | return ("totalDigits"); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5059 | case XML_SCHEMA_FACET_FRACTIONDIGITS: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5060 | return ("fractionDigits"); |
| 5061 | default: |
| 5062 | break; |
| 5063 | } |
| 5064 | return ("Internal Error"); |
| 5065 | } |
| 5066 | |
| 5067 | /** |
| 5068 | * xmlSchemaValidateFacets: |
| 5069 | * @ctxt: a schema validation context |
| 5070 | * @base: the base type |
| 5071 | * @facets: the list of facets to check |
| 5072 | * @value: the lexical repr of the value to validate |
| 5073 | * @val: the precomputed value |
| 5074 | * |
| 5075 | * Check a value against all facet conditions |
| 5076 | * |
| 5077 | * Returns 0 if the element is schemas valid, a positive error code |
| 5078 | * number otherwise and -1 in case of internal or API error. |
| 5079 | */ |
| 5080 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5081 | xmlSchemaValidateFacets(xmlSchemaValidCtxtPtr ctxt, |
| 5082 | xmlSchemaTypePtr base, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5083 | xmlSchemaFacetPtr facets, const xmlChar * value) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5084 | { |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5085 | int ret = 0; |
| 5086 | int tmp = 0; |
| 5087 | xmlSchemaTypeType type; |
| 5088 | xmlSchemaFacetPtr facet = facets; |
| 5089 | |
| 5090 | while (facet != NULL) { |
| 5091 | type = facet->type; |
| 5092 | if (type == XML_SCHEMA_FACET_ENUMERATION) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5093 | tmp = 1; |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5094 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5095 | while (facet != NULL) { |
| 5096 | tmp = |
| 5097 | xmlSchemaValidateFacet(base, facet, value, |
| 5098 | ctxt->value); |
| 5099 | if (tmp == 0) { |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5100 | return 0; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5101 | } |
| 5102 | facet = facet->next; |
| 5103 | } |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5104 | } else |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5105 | tmp = xmlSchemaValidateFacet(base, facet, value, ctxt->value); |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5106 | |
| 5107 | if (tmp != 0) { |
| 5108 | ret = tmp; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5109 | 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] | 5110 | } |
| 5111 | if (facet != NULL) |
| 5112 | facet = facet->next; |
| 5113 | } |
| 5114 | return (ret); |
| 5115 | } |
| 5116 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5117 | /************************************************************************ |
| 5118 | * * |
| 5119 | * Simple type validation * |
| 5120 | * * |
| 5121 | ************************************************************************/ |
| 5122 | |
| 5123 | /** |
| 5124 | * xmlSchemaValidateSimpleValue: |
| 5125 | * @ctxt: a schema validation context |
| 5126 | * @type: the type declaration |
| 5127 | * @value: the value to validate |
| 5128 | * |
| 5129 | * Validate a value against a simple type |
| 5130 | * |
| 5131 | * Returns 0 if the value is valid, a positive error code |
| 5132 | * number otherwise and -1 in case of internal or API error. |
| 5133 | */ |
| 5134 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5135 | xmlSchemaValidateSimpleValue(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5136 | xmlSchemaTypePtr type, const xmlChar * value) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5137 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5138 | int ret = 0; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5139 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5140 | /* |
| 5141 | * First normalize the value accordingly to Schema Datatype |
| 5142 | * 4.3.6 whiteSpace definition of the whiteSpace facet of type |
| 5143 | */ |
| 5144 | /* |
| 5145 | * Then check the normalized value against the lexical space of the |
| 5146 | * type. |
| 5147 | */ |
| 5148 | if (type->type == XML_SCHEMA_TYPE_BASIC) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5149 | if (ctxt->value != NULL) { |
| 5150 | xmlSchemaFreeValue(ctxt->value); |
| 5151 | ctxt->value = NULL; |
| 5152 | } |
| 5153 | ret = xmlSchemaValPredefTypeNode(type, value, &(ctxt->value), |
| 5154 | ctxt->cur); |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5155 | if (ret != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5156 | 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] | 5157 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5158 | } else if (type->type == XML_SCHEMA_TYPE_RESTRICTION) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5159 | xmlSchemaTypePtr base; |
| 5160 | xmlSchemaFacetPtr facet; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5161 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5162 | base = type->baseType; |
| 5163 | if (base != NULL) { |
| 5164 | ret = xmlSchemaValidateSimpleValue(ctxt, base, value); |
| 5165 | } else if (type->subtypes != NULL) { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5166 | TODO |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5167 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5168 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5169 | /* |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 5170 | * Do not validate facets or attributes when working on |
| 5171 | * building the Schemas |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5172 | */ |
| 5173 | if (ctxt->schema != NULL) { |
| 5174 | if (ret == 0) { |
| 5175 | facet = type->facets; |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5176 | ret = xmlSchemaValidateFacets(ctxt, base, facet, value); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5177 | } |
| 5178 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5179 | } else if (type->type == XML_SCHEMA_TYPE_SIMPLE) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5180 | xmlSchemaTypePtr base; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5181 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5182 | base = type->subtypes; |
| 5183 | if (base != NULL) { |
| 5184 | ret = xmlSchemaValidateSimpleValue(ctxt, base, value); |
| 5185 | } else { |
| 5186 | TODO} |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5187 | } else if (type->type == XML_SCHEMA_TYPE_LIST) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5188 | xmlSchemaTypePtr base; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5189 | const xmlChar *cur, *end; |
| 5190 | xmlChar *tmp; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5191 | int ret2; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5192 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5193 | base = type->subtypes; |
| 5194 | if (base == NULL) { |
| 5195 | xmlSchemaVErr(ctxt, type->node, XML_SCHEMAS_ERR_INTERNAL, |
| 5196 | "Internal: List type %s has no base type\n", |
| 5197 | type->name, NULL); |
| 5198 | return (-1); |
| 5199 | } |
| 5200 | cur = value; |
| 5201 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5202 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5203 | cur++; |
| 5204 | end = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5205 | while ((*end != 0) && (!(IS_BLANK_CH(*end)))) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5206 | end++; |
| 5207 | if (end == cur) |
| 5208 | break; |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5209 | tmp = xmlStrndup(cur, end - cur); |
| 5210 | ret2 = xmlSchemaValidateSimpleValue(ctxt, base, tmp); |
| 5211 | xmlFree(tmp); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5212 | if (ret2 != 0) |
| 5213 | ret = 1; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5214 | cur = end; |
| 5215 | } while (*cur != 0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5216 | } else { |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5217 | TODO |
| 5218 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5219 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5220 | } |
| 5221 | |
| 5222 | /************************************************************************ |
| 5223 | * * |
| 5224 | * DOM Validation code * |
| 5225 | * * |
| 5226 | ************************************************************************/ |
| 5227 | |
| 5228 | static int xmlSchemaValidateContent(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5229 | xmlNodePtr node); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5230 | static int xmlSchemaValidateAttributes(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5231 | xmlNodePtr elem, |
| 5232 | xmlSchemaAttributePtr attributes); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5233 | static int xmlSchemaValidateType(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5234 | xmlNodePtr elem, |
| 5235 | xmlSchemaElementPtr elemDecl, |
| 5236 | xmlSchemaTypePtr type); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5237 | |
| 5238 | /** |
| 5239 | * xmlSchemaRegisterAttributes: |
| 5240 | * @ctxt: a schema validation context |
| 5241 | * @attrs: a list of attributes |
| 5242 | * |
| 5243 | * Register the list of attributes as the set to be validated on that element |
| 5244 | * |
| 5245 | * Returns -1 in case of error, 0 otherwise |
| 5246 | */ |
| 5247 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5248 | xmlSchemaRegisterAttributes(xmlSchemaValidCtxtPtr ctxt, xmlAttrPtr attrs) |
| 5249 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5250 | while (attrs != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5251 | if ((attrs->ns != NULL) && |
| 5252 | (xmlStrEqual(attrs->ns->href, xmlSchemaInstanceNs))) { |
| 5253 | attrs = attrs->next; |
| 5254 | continue; |
| 5255 | } |
| 5256 | if (ctxt->attrNr >= ctxt->attrMax) { |
| 5257 | xmlSchemaAttrStatePtr tmp; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5258 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5259 | ctxt->attrMax *= 2; |
| 5260 | tmp = (xmlSchemaAttrStatePtr) |
| 5261 | xmlRealloc(ctxt->attr, ctxt->attrMax * |
| 5262 | sizeof(xmlSchemaAttrState)); |
| 5263 | if (tmp == NULL) { |
| 5264 | xmlSchemaVErrMemory(ctxt, "registering attributes", NULL); |
| 5265 | ctxt->attrMax /= 2; |
| 5266 | return (-1); |
| 5267 | } |
| 5268 | ctxt->attr = tmp; |
| 5269 | } |
| 5270 | ctxt->attr[ctxt->attrNr].attr = attrs; |
| 5271 | ctxt->attr[ctxt->attrNr].state = XML_SCHEMAS_ATTR_UNKNOWN; |
| 5272 | ctxt->attrNr++; |
| 5273 | attrs = attrs->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5274 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5275 | return (0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5276 | } |
| 5277 | |
| 5278 | /** |
| 5279 | * xmlSchemaCheckAttributes: |
| 5280 | * @ctxt: a schema validation context |
| 5281 | * @node: the node carrying it. |
| 5282 | * |
| 5283 | * Check that the registered set of attributes on the current node |
| 5284 | * has been properly validated. |
| 5285 | * |
| 5286 | * Returns 0 if validity constraints are met, 1 otherwise. |
| 5287 | */ |
| 5288 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5289 | xmlSchemaCheckAttributes(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5290 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5291 | int ret = 0; |
| 5292 | int i; |
| 5293 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5294 | for (i = ctxt->attrBase; i < ctxt->attrNr; i++) { |
| 5295 | if (ctxt->attr[i].attr == NULL) |
| 5296 | break; |
| 5297 | if (ctxt->attr[i].state == XML_SCHEMAS_ATTR_UNKNOWN) { |
| 5298 | ret = 1; |
| 5299 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_ATTRUNKNOWN, "Attribute %s on %s is unknown\n", ctxt->attr[i].attr->name, node->name); |
| 5300 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5301 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5302 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5303 | } |
| 5304 | |
| 5305 | /** |
| 5306 | * xmlSchemaValidateSimpleContent: |
| 5307 | * @ctxt: a schema validation context |
| 5308 | * @elem: an element |
| 5309 | * @type: the type declaration |
| 5310 | * |
| 5311 | * Validate the content of an element expected to be a simple type |
| 5312 | * |
| 5313 | * Returns 0 if the element is schemas valid, a positive error code |
| 5314 | * number otherwise and -1 in case of internal or API error. |
| 5315 | */ |
| 5316 | static int |
| 5317 | xmlSchemaValidateSimpleContent(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5318 | xmlNodePtr node ATTRIBUTE_UNUSED) |
| 5319 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5320 | xmlNodePtr child; |
| 5321 | xmlSchemaTypePtr type, base; |
| 5322 | xmlChar *value; |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 5323 | int ret = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5324 | |
| 5325 | child = ctxt->node; |
| 5326 | type = ctxt->type; |
| 5327 | |
| 5328 | /* |
| 5329 | * Validation Rule: Element Locally Valid (Type): 3.1.3 |
| 5330 | */ |
| 5331 | value = xmlNodeGetContent(child); |
| 5332 | /* xmlSchemaValidateSimpleValue(ctxt, type, value); */ |
| 5333 | switch (type->type) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5334 | case XML_SCHEMA_TYPE_RESTRICTION:{ |
| 5335 | xmlSchemaFacetPtr facet; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5336 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5337 | base = type->baseType; |
| 5338 | if (base != NULL) { |
| 5339 | ret = xmlSchemaValidateSimpleValue(ctxt, base, value); |
| 5340 | } else { |
| 5341 | TODO} |
| 5342 | if (ret == 0) { |
| 5343 | facet = type->facets; |
| 5344 | ret = |
| 5345 | xmlSchemaValidateFacets(ctxt, base, facet, value); |
| 5346 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 5347 | if ((ret == 0) && (type->attributes != NULL)) { |
| 5348 | ret = xmlSchemaValidateAttributes(ctxt, node, |
| 5349 | type->attributes); |
| 5350 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5351 | break; |
| 5352 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5353 | case XML_SCHEMA_TYPE_EXTENSION:{ |
| 5354 | TODO |
| 5355 | break; |
| 5356 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5357 | default: |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5358 | TODO |
| 5359 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5360 | if (value != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5361 | xmlFree(value); |
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 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5364 | } |
| 5365 | |
| 5366 | /** |
| 5367 | * xmlSchemaValidateCheckNodeList |
| 5368 | * @nodelist: the list of nodes |
| 5369 | * |
| 5370 | * Check the node list is only made of text nodes and entities pointing |
| 5371 | * to text nodes |
| 5372 | * |
| 5373 | * Returns 1 if true, 0 if false and -1 in case of error |
| 5374 | */ |
| 5375 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5376 | xmlSchemaValidateCheckNodeList(xmlNodePtr nodelist) |
| 5377 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5378 | while (nodelist != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5379 | if (nodelist->type == XML_ENTITY_REF_NODE) { |
| 5380 | TODO /* implement recursion in the entity content */ |
| 5381 | } |
| 5382 | if ((nodelist->type != XML_TEXT_NODE) && |
| 5383 | (nodelist->type != XML_COMMENT_NODE) && |
| 5384 | (nodelist->type != XML_PI_NODE) && |
| 5385 | (nodelist->type != XML_PI_NODE)) { |
| 5386 | return (0); |
| 5387 | } |
| 5388 | nodelist = nodelist->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5389 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5390 | return (1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5391 | } |
| 5392 | |
| 5393 | /** |
| 5394 | * xmlSchemaSkipIgnored: |
| 5395 | * @ctxt: a schema validation context |
| 5396 | * @type: the current type context |
| 5397 | * @node: the top node. |
| 5398 | * |
| 5399 | * Skip ignorable nodes in that context |
| 5400 | * |
| 5401 | * Returns the new sibling |
| 5402 | * number otherwise and -1 in case of internal or API error. |
| 5403 | */ |
| 5404 | static xmlNodePtr |
Daniel Veillard | dda8f1b | 2002-09-26 09:47:36 +0000 | [diff] [blame] | 5405 | xmlSchemaSkipIgnored(xmlSchemaValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5406 | xmlSchemaTypePtr type, xmlNodePtr node) |
| 5407 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5408 | int mixed = 0; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5409 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5410 | /* |
| 5411 | * TODO complete and handle entities |
| 5412 | */ |
| 5413 | mixed = ((type->contentType == XML_SCHEMA_CONTENT_MIXED) || |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5414 | (type->contentType == XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5415 | while ((node != NULL) && |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5416 | ((node->type == XML_COMMENT_NODE) || |
| 5417 | ((mixed == 1) && (node->type == XML_TEXT_NODE)) || |
| 5418 | (((type->contentType == XML_SCHEMA_CONTENT_ELEMENTS) && |
| 5419 | (node->type == XML_TEXT_NODE) && (IS_BLANK_NODE(node)))))) { |
| 5420 | node = node->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5421 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5422 | return (node); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5423 | } |
| 5424 | |
| 5425 | /** |
| 5426 | * xmlSchemaValidateCallback: |
| 5427 | * @ctxt: a schema validation context |
| 5428 | * @name: the name of the element detected (might be NULL) |
| 5429 | * @type: the type |
| 5430 | * |
| 5431 | * A transition has been made in the automata associated to an element |
| 5432 | * content model |
| 5433 | */ |
| 5434 | static void |
| 5435 | xmlSchemaValidateCallback(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5436 | const xmlChar * name ATTRIBUTE_UNUSED, |
| 5437 | xmlSchemaTypePtr type, xmlNodePtr node) |
| 5438 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5439 | xmlSchemaTypePtr oldtype = ctxt->type; |
| 5440 | xmlNodePtr oldnode = ctxt->node; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5441 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5442 | #ifdef DEBUG_CONTENT |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 5443 | xmlGenericError(xmlGenericErrorContext, |
| 5444 | "xmlSchemaValidateCallback: %s, %s, %s\n", |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5445 | name, type->name, node->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5446 | #endif |
| 5447 | ctxt->type = type; |
| 5448 | ctxt->node = node; |
| 5449 | xmlSchemaValidateContent(ctxt, node); |
| 5450 | ctxt->type = oldtype; |
| 5451 | ctxt->node = oldnode; |
| 5452 | } |
| 5453 | |
| 5454 | |
| 5455 | #if 0 |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5456 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5457 | /** |
| 5458 | * xmlSchemaValidateSimpleRestrictionType: |
| 5459 | * @ctxt: a schema validation context |
| 5460 | * @node: the top node. |
| 5461 | * |
| 5462 | * Validate the content of a restriction type. |
| 5463 | * |
| 5464 | * Returns 0 if the element is schemas valid, a positive error code |
| 5465 | * number otherwise and -1 in case of internal or API error. |
| 5466 | */ |
| 5467 | static int |
| 5468 | xmlSchemaValidateSimpleRestrictionType(xmlSchemaValidCtxtPtr ctxt, |
| 5469 | xmlNodePtr node) |
| 5470 | { |
| 5471 | xmlNodePtr child; |
| 5472 | xmlSchemaTypePtr type; |
| 5473 | int ret; |
| 5474 | |
| 5475 | child = ctxt->node; |
| 5476 | type = ctxt->type; |
| 5477 | |
| 5478 | if ((ctxt == NULL) || (type == NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5479 | 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] | 5480 | return (-1); |
| 5481 | } |
| 5482 | /* |
| 5483 | * Only text and text based entities references shall be found there |
| 5484 | */ |
| 5485 | ret = xmlSchemaValidateCheckNodeList(child); |
| 5486 | if (ret < 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5487 | 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] | 5488 | return (-1); |
| 5489 | } else if (ret == 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5490 | 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] | 5491 | return (-1); |
| 5492 | } |
| 5493 | ctxt->type = type->subtypes; |
| 5494 | xmlSchemaValidateContent(ctxt, node); |
| 5495 | ctxt->type = type; |
| 5496 | return (ret); |
| 5497 | } |
| 5498 | #endif |
| 5499 | |
| 5500 | /** |
| 5501 | * xmlSchemaValidateSimpleType: |
| 5502 | * @ctxt: a schema validation context |
| 5503 | * @node: the top node. |
| 5504 | * |
| 5505 | * Validate the content of an simple type. |
| 5506 | * |
| 5507 | * Returns 0 if the element is schemas valid, a positive error code |
| 5508 | * number otherwise and -1 in case of internal or API error. |
| 5509 | */ |
| 5510 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5511 | xmlSchemaValidateSimpleType(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5512 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5513 | xmlNodePtr child; |
| 5514 | xmlSchemaTypePtr type; |
| 5515 | xmlAttrPtr attr; |
| 5516 | int ret; |
| 5517 | |
| 5518 | child = ctxt->node; |
| 5519 | type = ctxt->type; |
| 5520 | |
| 5521 | if ((ctxt == NULL) || (type == NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5522 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INTERNAL, "Internal error: xmlSchemaValidateSimpleType %s\n", node->name, NULL); |
| 5523 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5524 | } |
| 5525 | /* |
| 5526 | * Only text and text based entities references shall be found there |
| 5527 | */ |
| 5528 | ret = xmlSchemaValidateCheckNodeList(child); |
| 5529 | if (ret < 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5530 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INTERNAL, "Internal error: xmlSchemaValidateSimpleType %s content\n", node->name, NULL); |
| 5531 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5532 | } else if (ret == 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5533 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_NOTSIMPLE, "Element %s content is not a simple type\n", node->name, NULL); |
| 5534 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5535 | } |
| 5536 | /* |
| 5537 | * Validation Rule: Element Locally Valid (Type): 3.1.1 |
| 5538 | */ |
| 5539 | attr = node->properties; |
| 5540 | while (attr != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5541 | if ((attr->ns == NULL) || |
| 5542 | (!xmlStrEqual(attr->ns->href, xmlSchemaInstanceNs)) || |
| 5543 | ((!xmlStrEqual(attr->name, BAD_CAST "type")) && |
| 5544 | (!xmlStrEqual(attr->name, BAD_CAST "nil")) && |
| 5545 | (!xmlStrEqual(attr->name, BAD_CAST "schemasLocation")) && |
| 5546 | (!xmlStrEqual |
| 5547 | (attr->name, BAD_CAST "noNamespaceSchemaLocation")))) { |
| 5548 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INVALIDATTR, "Element %s: attribute %s should not be present\n", node->name, attr->name); |
| 5549 | return (ctxt->err); |
| 5550 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5551 | } |
| 5552 | |
| 5553 | ctxt->type = type->subtypes; |
| 5554 | ret = xmlSchemaValidateSimpleContent(ctxt, node); |
| 5555 | ctxt->type = type; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5556 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5557 | } |
| 5558 | |
| 5559 | /** |
| 5560 | * xmlSchemaValidateElementType: |
| 5561 | * @ctxt: a schema validation context |
| 5562 | * @node: the top node. |
| 5563 | * |
| 5564 | * Validate the content of an element type. |
| 5565 | * Validation Rule: Element Locally Valid (Complex Type) |
| 5566 | * |
| 5567 | * Returns 0 if the element is schemas valid, a positive error code |
| 5568 | * number otherwise and -1 in case of internal or API error. |
| 5569 | */ |
| 5570 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5571 | xmlSchemaValidateElementType(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5572 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5573 | xmlNodePtr child; |
| 5574 | xmlSchemaTypePtr type; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5575 | xmlRegExecCtxtPtr oldregexp; /* cont model of the parent */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5576 | xmlSchemaElementPtr decl; |
| 5577 | int ret, attrBase; |
| 5578 | |
| 5579 | oldregexp = ctxt->regexp; |
| 5580 | |
| 5581 | child = ctxt->node; |
| 5582 | type = ctxt->type; |
| 5583 | |
| 5584 | if ((ctxt == NULL) || (type == NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5585 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INTERNAL, "Internal error: xmlSchemaValidateElementType\n", node->name, NULL); |
| 5586 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5587 | } |
| 5588 | if (child == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5589 | if (type->minOccurs > 0) { |
| 5590 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_MISSING, "Element %s: missing child %s\n", node->name, type->name); |
| 5591 | } |
| 5592 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5593 | } |
| 5594 | |
| 5595 | /* |
| 5596 | * Verify the element matches |
| 5597 | */ |
| 5598 | if (!xmlStrEqual(child->name, type->name)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5599 | xmlSchemaVErr3(ctxt, node, XML_SCHEMAS_ERR_WRONGELEM, "Element %s: missing child %s found %s\n", node->name, type->name, child->name); |
| 5600 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5601 | } |
| 5602 | /* |
| 5603 | * Verify the attributes |
| 5604 | */ |
| 5605 | attrBase = ctxt->attrBase; |
| 5606 | ctxt->attrBase = ctxt->attrNr; |
| 5607 | xmlSchemaRegisterAttributes(ctxt, child->properties); |
| 5608 | xmlSchemaValidateAttributes(ctxt, child, type->attributes); |
| 5609 | /* |
| 5610 | * Verify the element content recursively |
| 5611 | */ |
| 5612 | decl = (xmlSchemaElementPtr) type; |
| 5613 | oldregexp = ctxt->regexp; |
| 5614 | if (decl->contModel != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5615 | ctxt->regexp = xmlRegNewExecCtxt(decl->contModel, |
| 5616 | (xmlRegExecCallbacks) |
| 5617 | xmlSchemaValidateCallback, ctxt); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5618 | #ifdef DEBUG_AUTOMATA |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5619 | xmlGenericError(xmlGenericErrorContext, "====> %s\n", node->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5620 | #endif |
| 5621 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5622 | xmlSchemaValidateType(ctxt, child, (xmlSchemaElementPtr) type, |
| 5623 | type->subtypes); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5624 | |
| 5625 | if (decl->contModel != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5626 | ret = xmlRegExecPushString(ctxt->regexp, NULL, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5627 | #ifdef DEBUG_AUTOMATA |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5628 | xmlGenericError(xmlGenericErrorContext, |
| 5629 | "====> %s : %d\n", node->name, ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5630 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5631 | if (ret == 0) { |
| 5632 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_ELEMCONT, "Element %s content check failed\n", node->name, NULL); |
| 5633 | } else if (ret < 0) { |
| 5634 | 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] | 5635 | #ifdef DEBUG_CONTENT |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5636 | } else { |
| 5637 | xmlGenericError(xmlGenericErrorContext, |
| 5638 | "Element %s content check succeeded\n", |
| 5639 | node->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5640 | |
| 5641 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5642 | } |
| 5643 | xmlRegFreeExecCtxt(ctxt->regexp); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5644 | } |
| 5645 | /* |
| 5646 | * Verify that all attributes were Schemas-validated |
| 5647 | */ |
| 5648 | xmlSchemaCheckAttributes(ctxt, node); |
| 5649 | ctxt->attrNr = ctxt->attrBase; |
| 5650 | ctxt->attrBase = attrBase; |
| 5651 | |
| 5652 | ctxt->regexp = oldregexp; |
| 5653 | |
| 5654 | ctxt->node = child; |
| 5655 | ctxt->type = type; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5656 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5657 | } |
| 5658 | |
| 5659 | /** |
| 5660 | * xmlSchemaValidateBasicType: |
| 5661 | * @ctxt: a schema validation context |
| 5662 | * @node: the top node. |
| 5663 | * |
| 5664 | * Validate the content of an element expected to be a basic type type |
| 5665 | * |
| 5666 | * Returns 0 if the element is schemas valid, a positive error code |
| 5667 | * number otherwise and -1 in case of internal or API error. |
| 5668 | */ |
| 5669 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5670 | xmlSchemaValidateBasicType(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5671 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5672 | int ret; |
| 5673 | xmlNodePtr child, cur; |
| 5674 | xmlSchemaTypePtr type; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5675 | xmlChar *value; /* lexical representation */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5676 | |
| 5677 | child = ctxt->node; |
| 5678 | type = ctxt->type; |
| 5679 | |
| 5680 | if ((ctxt == NULL) || (type == NULL)) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5681 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INTERNAL, "Internal error: xmlSchemaValidateBasicType\n", node->name, NULL); |
| 5682 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5683 | } |
| 5684 | /* |
| 5685 | * First check the content model of the node. |
| 5686 | */ |
| 5687 | cur = child; |
| 5688 | while (cur != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5689 | switch (cur->type) { |
| 5690 | case XML_TEXT_NODE: |
| 5691 | case XML_CDATA_SECTION_NODE: |
| 5692 | case XML_PI_NODE: |
| 5693 | case XML_COMMENT_NODE: |
| 5694 | case XML_XINCLUDE_START: |
| 5695 | case XML_XINCLUDE_END: |
| 5696 | break; |
| 5697 | case XML_ENTITY_REF_NODE: |
| 5698 | case XML_ENTITY_NODE: |
| 5699 | TODO break; |
| 5700 | case XML_ELEMENT_NODE: |
| 5701 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INVALIDELEM, "Element %s: child %s should not be present\n", node->name, cur->name); |
| 5702 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5703 | case XML_ATTRIBUTE_NODE: |
| 5704 | case XML_DOCUMENT_NODE: |
| 5705 | case XML_DOCUMENT_TYPE_NODE: |
| 5706 | case XML_DOCUMENT_FRAG_NODE: |
| 5707 | case XML_NOTATION_NODE: |
| 5708 | case XML_HTML_DOCUMENT_NODE: |
| 5709 | case XML_DTD_NODE: |
| 5710 | case XML_ELEMENT_DECL: |
| 5711 | case XML_ATTRIBUTE_DECL: |
| 5712 | case XML_ENTITY_DECL: |
| 5713 | case XML_NAMESPACE_DECL: |
| 5714 | #ifdef LIBXML_DOCB_ENABLED |
| 5715 | case XML_DOCB_DOCUMENT_NODE: |
| 5716 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5717 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INVALIDELEM, "Element %s: node type of node unexpected here\n", node->name, NULL); |
| 5718 | return (ctxt->err); |
| 5719 | } |
| 5720 | cur = cur->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5721 | } |
| 5722 | if (child == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5723 | value = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5724 | else |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5725 | value = xmlNodeGetContent(child->parent); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5726 | |
| 5727 | if (ctxt->value != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5728 | xmlSchemaFreeValue(ctxt->value); |
| 5729 | ctxt->value = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5730 | } |
| 5731 | ret = xmlSchemaValidatePredefinedType(type, value, &(ctxt->value)); |
| 5732 | if (value != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5733 | xmlFree(value); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5734 | if (ret != 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5735 | 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] | 5736 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5737 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5738 | } |
| 5739 | |
| 5740 | /** |
| 5741 | * xmlSchemaValidateComplexType: |
| 5742 | * @ctxt: a schema validation context |
| 5743 | * @node: the top node. |
| 5744 | * |
| 5745 | * Validate the content of an element expected to be a complex type type |
| 5746 | * xmlschema-1.html#cvc-complex-type |
| 5747 | * Validation Rule: Element Locally Valid (Complex Type) |
| 5748 | * |
| 5749 | * Returns 0 if the element is schemas valid, a positive error code |
| 5750 | * number otherwise and -1 in case of internal or API error. |
| 5751 | */ |
| 5752 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5753 | xmlSchemaValidateComplexType(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5754 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5755 | xmlNodePtr child; |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 5756 | xmlSchemaTypePtr type, subtype; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5757 | int ret; |
| 5758 | |
| 5759 | child = ctxt->node; |
| 5760 | type = ctxt->type; |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 5761 | ctxt->cur = node; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5762 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5763 | switch (type->contentType) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5764 | case XML_SCHEMA_CONTENT_EMPTY: |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5765 | if (type->baseType != NULL) { |
| 5766 | } else if (child != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5767 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_NOTEMPTY, "Element %s is supposed to be empty\n", node->name, NULL); |
| 5768 | } |
| 5769 | if (type->attributes != NULL) { |
| 5770 | xmlSchemaValidateAttributes(ctxt, node, type->attributes); |
| 5771 | } |
| 5772 | subtype = type->subtypes; |
| 5773 | while (subtype != NULL) { |
| 5774 | ctxt->type = subtype; |
| 5775 | xmlSchemaValidateComplexType(ctxt, node); |
| 5776 | subtype = subtype->next; |
| 5777 | } |
| 5778 | break; |
| 5779 | case XML_SCHEMA_CONTENT_ELEMENTS: |
| 5780 | case XML_SCHEMA_CONTENT_MIXED: |
| 5781 | case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS: |
| 5782 | /* |
| 5783 | * Skip ignorable nodes in that context |
| 5784 | */ |
| 5785 | child = xmlSchemaSkipIgnored(ctxt, type, child); |
| 5786 | while (child != NULL) { |
| 5787 | if (child->type == XML_ELEMENT_NODE) { |
| 5788 | ret = xmlRegExecPushString(ctxt->regexp, |
| 5789 | child->name, child); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5790 | #ifdef DEBUG_AUTOMATA |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5791 | if (ret < 0) |
| 5792 | xmlGenericError(xmlGenericErrorContext, |
| 5793 | " --> %s Error\n", child->name); |
| 5794 | else |
| 5795 | xmlGenericError(xmlGenericErrorContext, |
| 5796 | " --> %s\n", child->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5797 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5798 | } |
| 5799 | child = child->next; |
| 5800 | /* |
| 5801 | * Skip ignorable nodes in that context |
| 5802 | */ |
| 5803 | child = xmlSchemaSkipIgnored(ctxt, type, child); |
| 5804 | } |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 5805 | if (type->attributes != NULL) { |
| 5806 | xmlSchemaValidateAttributes(ctxt, node, type->attributes); |
| 5807 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5808 | break; |
| 5809 | case XML_SCHEMA_CONTENT_BASIC:{ |
| 5810 | if (type->subtypes != NULL) { |
| 5811 | ctxt->type = type->subtypes; |
| 5812 | xmlSchemaValidateComplexType(ctxt, node); |
| 5813 | } |
| 5814 | if (type->baseType != NULL) { |
| 5815 | ctxt->type = type->baseType; |
| 5816 | xmlSchemaValidateBasicType(ctxt, node); |
| 5817 | } |
| 5818 | if (type->attributes != NULL) { |
| 5819 | xmlSchemaValidateAttributes(ctxt, node, |
| 5820 | type->attributes); |
| 5821 | } |
| 5822 | ctxt->type = type; |
| 5823 | break; |
| 5824 | } |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 5825 | case XML_SCHEMA_CONTENT_SIMPLE:{ |
| 5826 | if (type->subtypes != NULL) { |
| 5827 | ctxt->type = type->subtypes; |
| 5828 | xmlSchemaValidateComplexType(ctxt, node); |
| 5829 | } |
| 5830 | if (type->baseType != NULL) { |
| 5831 | ctxt->type = type->baseType; |
| 5832 | xmlSchemaValidateComplexType(ctxt, node); |
| 5833 | } |
| 5834 | if (type->attributes != NULL) { |
| 5835 | xmlSchemaValidateAttributes(ctxt, node, |
| 5836 | type->attributes); |
| 5837 | } |
| 5838 | ctxt->type = type; |
| 5839 | break; |
| 5840 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5841 | default: |
| 5842 | TODO xmlGenericError(xmlGenericErrorContext, |
| 5843 | "unimplemented content type %d\n", |
| 5844 | type->contentType); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5845 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5846 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5847 | } |
| 5848 | |
| 5849 | /** |
| 5850 | * xmlSchemaValidateContent: |
| 5851 | * @ctxt: a schema validation context |
| 5852 | * @elem: an element |
| 5853 | * @type: the type declaration |
| 5854 | * |
| 5855 | * Validate the content of an element against the type. |
| 5856 | * |
| 5857 | * Returns 0 if the element is schemas valid, a positive error code |
| 5858 | * number otherwise and -1 in case of internal or API error. |
| 5859 | */ |
| 5860 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5861 | xmlSchemaValidateContent(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr node) |
| 5862 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5863 | xmlNodePtr child; |
| 5864 | xmlSchemaTypePtr type; |
| 5865 | |
| 5866 | child = ctxt->node; |
| 5867 | type = ctxt->type; |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 5868 | ctxt->cur = node; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5869 | |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 5870 | xmlSchemaValidateAttributes(ctxt, node, type->attributes); |
Daniel Veillard | 82bbbd4 | 2003-05-11 20:16:09 +0000 | [diff] [blame] | 5871 | ctxt->cur = node; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 5872 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5873 | switch (type->type) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5874 | case XML_SCHEMA_TYPE_ANY: |
| 5875 | /* Any type will do it, fine */ |
| 5876 | TODO /* handle recursivity */ |
| 5877 | break; |
| 5878 | case XML_SCHEMA_TYPE_COMPLEX: |
| 5879 | xmlSchemaValidateComplexType(ctxt, node); |
| 5880 | break; |
| 5881 | case XML_SCHEMA_TYPE_ELEMENT:{ |
| 5882 | xmlSchemaElementPtr decl = (xmlSchemaElementPtr) type; |
| 5883 | |
| 5884 | /* |
| 5885 | * Handle element reference here |
| 5886 | */ |
| 5887 | if (decl->ref != NULL) { |
| 5888 | if (decl->refDecl == NULL) { |
| 5889 | xmlSchemaVErr(ctxt, node, XML_SCHEMAS_ERR_INTERNAL, "Internal error: element reference %s not resolved\n", decl->ref, NULL); |
| 5890 | return (-1); |
| 5891 | } |
| 5892 | ctxt->type = (xmlSchemaTypePtr) decl->refDecl; |
| 5893 | decl = decl->refDecl; |
| 5894 | } |
| 5895 | xmlSchemaValidateElementType(ctxt, node); |
| 5896 | ctxt->type = type; |
| 5897 | break; |
| 5898 | } |
| 5899 | case XML_SCHEMA_TYPE_BASIC: |
| 5900 | xmlSchemaValidateBasicType(ctxt, node); |
| 5901 | break; |
| 5902 | case XML_SCHEMA_TYPE_FACET: |
| 5903 | TODO break; |
| 5904 | case XML_SCHEMA_TYPE_SIMPLE: |
| 5905 | xmlSchemaValidateSimpleType(ctxt, node); |
| 5906 | break; |
| 5907 | case XML_SCHEMA_TYPE_SEQUENCE: |
| 5908 | TODO break; |
| 5909 | case XML_SCHEMA_TYPE_CHOICE: |
| 5910 | TODO break; |
| 5911 | case XML_SCHEMA_TYPE_ALL: |
| 5912 | TODO break; |
| 5913 | case XML_SCHEMA_TYPE_SIMPLE_CONTENT: |
| 5914 | TODO break; |
| 5915 | case XML_SCHEMA_TYPE_COMPLEX_CONTENT: |
| 5916 | TODO break; |
| 5917 | case XML_SCHEMA_TYPE_UR: |
| 5918 | TODO break; |
| 5919 | case XML_SCHEMA_TYPE_RESTRICTION: |
| 5920 | /*xmlSchemaValidateRestrictionType(ctxt, node); */ |
| 5921 | TODO break; |
| 5922 | case XML_SCHEMA_TYPE_EXTENSION: |
| 5923 | TODO break; |
| 5924 | case XML_SCHEMA_TYPE_ATTRIBUTE: |
| 5925 | TODO break; |
| 5926 | case XML_SCHEMA_TYPE_GROUP: |
| 5927 | TODO break; |
| 5928 | case XML_SCHEMA_TYPE_NOTATION: |
| 5929 | TODO break; |
| 5930 | case XML_SCHEMA_TYPE_LIST: |
| 5931 | TODO break; |
| 5932 | case XML_SCHEMA_TYPE_UNION: |
| 5933 | TODO break; |
| 5934 | case XML_SCHEMA_FACET_MININCLUSIVE: |
| 5935 | TODO break; |
| 5936 | case XML_SCHEMA_FACET_MINEXCLUSIVE: |
| 5937 | TODO break; |
| 5938 | case XML_SCHEMA_FACET_MAXINCLUSIVE: |
| 5939 | TODO break; |
| 5940 | case XML_SCHEMA_FACET_MAXEXCLUSIVE: |
| 5941 | TODO break; |
| 5942 | case XML_SCHEMA_FACET_TOTALDIGITS: |
| 5943 | TODO break; |
| 5944 | case XML_SCHEMA_FACET_FRACTIONDIGITS: |
| 5945 | TODO break; |
| 5946 | case XML_SCHEMA_FACET_PATTERN: |
| 5947 | TODO break; |
| 5948 | case XML_SCHEMA_FACET_ENUMERATION: |
| 5949 | TODO break; |
| 5950 | case XML_SCHEMA_FACET_WHITESPACE: |
| 5951 | TODO break; |
| 5952 | case XML_SCHEMA_FACET_LENGTH: |
| 5953 | TODO break; |
| 5954 | case XML_SCHEMA_FACET_MAXLENGTH: |
| 5955 | TODO break; |
| 5956 | case XML_SCHEMA_FACET_MINLENGTH: |
| 5957 | TODO break; |
| 5958 | case XML_SCHEMA_TYPE_ATTRIBUTEGROUP: |
| 5959 | TODO break; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5960 | } |
| 5961 | xmlSchemaValidateAttributes(ctxt, node, type->attributes); |
| 5962 | |
| 5963 | if (ctxt->node == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5964 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5965 | ctxt->node = ctxt->node->next; |
| 5966 | ctxt->type = type->next; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5967 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5968 | } |
| 5969 | |
| 5970 | /** |
| 5971 | * xmlSchemaValidateType: |
| 5972 | * @ctxt: a schema validation context |
| 5973 | * @elem: an element |
| 5974 | * @type: the list of type declarations |
| 5975 | * |
| 5976 | * Validate the content of an element against the types. |
| 5977 | * |
| 5978 | * Returns 0 if the element is schemas valid, a positive error code |
| 5979 | * number otherwise and -1 in case of internal or API error. |
| 5980 | */ |
| 5981 | static int |
| 5982 | xmlSchemaValidateType(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5983 | xmlSchemaElementPtr elemDecl, xmlSchemaTypePtr type) |
| 5984 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5985 | xmlChar *nil; |
| 5986 | |
Daniel Veillard | 2db8c12 | 2003-07-08 12:16:59 +0000 | [diff] [blame] | 5987 | if ((elem == NULL) || (type == NULL) || (elemDecl == NULL)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5988 | return (0); |
Daniel Veillard | 2db8c12 | 2003-07-08 12:16:59 +0000 | [diff] [blame] | 5989 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5990 | /* |
| 5991 | * 3.3.4 : 2 |
| 5992 | */ |
| 5993 | if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 5994 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_ISABSTRACT, "Element %s is abstract\n", elem->name, NULL); |
| 5995 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5996 | } |
| 5997 | /* |
| 5998 | * 3.3.4: 3 |
| 5999 | */ |
| 6000 | nil = xmlGetNsProp(elem, BAD_CAST "nil", xmlSchemaInstanceNs); |
| 6001 | if (elemDecl->flags & XML_SCHEMAS_ELEM_NILLABLE) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6002 | /* 3.3.4: 3.2 */ |
| 6003 | if (xmlStrEqual(nil, BAD_CAST "true")) { |
| 6004 | if (elem->children != NULL) { |
| 6005 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_NOTEMPTY, "Element %s is not empty\n", elem->name, NULL); |
| 6006 | return (ctxt->err); |
| 6007 | } |
| 6008 | if ((elemDecl->flags & XML_SCHEMAS_ELEM_FIXED) && |
| 6009 | (elemDecl->value != NULL)) { |
| 6010 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_HAVEDEFAULT, "Empty element %s cannot get a fixed value\n", elem->name, NULL); |
| 6011 | return (ctxt->err); |
| 6012 | } |
| 6013 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6014 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6015 | /* 3.3.4: 3.1 */ |
| 6016 | if (nil != NULL) { |
| 6017 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_NOTNILLABLE, "Element %s with xs:nil but not nillable\n", elem->name, NULL); |
| 6018 | xmlFree(nil); |
| 6019 | return (ctxt->err); |
| 6020 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6021 | } |
| 6022 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6023 | /* TODO 3.3.4: 4 if the element carries xs:type */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6024 | |
| 6025 | ctxt->type = elemDecl->subtypes; |
| 6026 | ctxt->node = elem->children; |
| 6027 | xmlSchemaValidateContent(ctxt, elem); |
| 6028 | xmlSchemaValidateAttributes(ctxt, elem, elemDecl->attributes); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6029 | |
| 6030 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6031 | } |
| 6032 | |
| 6033 | |
| 6034 | /** |
| 6035 | * xmlSchemaValidateAttributes: |
| 6036 | * @ctxt: a schema validation context |
| 6037 | * @elem: an element |
| 6038 | * @attributes: the list of attribute declarations |
| 6039 | * |
| 6040 | * Validate the attributes of an element. |
| 6041 | * |
| 6042 | * Returns 0 if the element is schemas valid, a positive error code |
| 6043 | * number otherwise and -1 in case of internal or API error. |
| 6044 | */ |
| 6045 | static int |
| 6046 | xmlSchemaValidateAttributes(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6047 | xmlSchemaAttributePtr attributes) |
| 6048 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6049 | int i, ret; |
| 6050 | xmlAttrPtr attr; |
| 6051 | xmlChar *value; |
Daniel Veillard | 13e04c6 | 2002-04-23 17:51:29 +0000 | [diff] [blame] | 6052 | xmlSchemaAttributeGroupPtr group = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6053 | |
| 6054 | if (attributes == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6055 | return (0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6056 | while (attributes != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6057 | /* |
| 6058 | * Handle attribute groups |
| 6059 | */ |
| 6060 | if (attributes->type == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) { |
| 6061 | group = (xmlSchemaAttributeGroupPtr) attributes; |
| 6062 | xmlSchemaValidateAttributes(ctxt, elem, group->attributes); |
| 6063 | attributes = group->next; |
| 6064 | continue; |
| 6065 | } |
| 6066 | for (i = ctxt->attrBase; i < ctxt->attrNr; i++) { |
| 6067 | attr = ctxt->attr[i].attr; |
| 6068 | if (attr == NULL) |
| 6069 | continue; |
| 6070 | if (attributes->ref != NULL) { |
| 6071 | if (!xmlStrEqual(attr->name, attributes->ref)) |
| 6072 | continue; |
| 6073 | if (attr->ns != NULL) { |
| 6074 | if ((attributes->refNs == NULL) || |
| 6075 | (!xmlStrEqual(attr->ns->href, attributes->refNs))) |
| 6076 | continue; |
| 6077 | } else if (attributes->refNs != NULL) { |
| 6078 | continue; |
| 6079 | } |
| 6080 | } else { |
| 6081 | if (!xmlStrEqual(attr->name, attributes->name)) |
| 6082 | continue; |
| 6083 | /* |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6084 | * handle the namespaces checks here |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6085 | */ |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6086 | if (attr->ns == NULL) { |
| 6087 | /* |
| 6088 | * accept an unqualified attribute only if the declaration |
| 6089 | * is unqualified or if the schemas allowed it. |
| 6090 | */ |
| 6091 | if ((attributes->targetNamespace != NULL) && |
| 6092 | ((attributes->flags & XML_SCHEMAS_ATTR_NSDEFAULT) == 0)) |
| 6093 | continue; |
| 6094 | } else { |
| 6095 | if (attributes->targetNamespace == NULL) |
| 6096 | continue; |
| 6097 | if (!xmlStrEqual(attributes->targetNamespace, |
| 6098 | attr->ns->href)) |
| 6099 | continue; |
| 6100 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6101 | } |
| 6102 | ctxt->cur = (xmlNodePtr) attributes; |
| 6103 | if (attributes->subtypes == NULL) { |
| 6104 | xmlSchemaVErr(ctxt, (xmlNodePtr) attr, XML_SCHEMAS_ERR_INTERNAL, "Internal error: attribute %s type not resolved\n", attr->name, NULL); |
| 6105 | continue; |
| 6106 | } |
| 6107 | value = xmlNodeListGetString(elem->doc, attr->children, 1); |
| 6108 | ret = xmlSchemaValidateSimpleValue(ctxt, attributes->subtypes, |
| 6109 | value); |
| 6110 | if (ret != 0) { |
| 6111 | xmlSchemaVErr(ctxt, (xmlNodePtr) attr, XML_SCHEMAS_ERR_ATTRINVALID, "attribute %s on %s does not match type\n", attr->name, elem->name); |
| 6112 | } else { |
| 6113 | ctxt->attr[i].state = XML_SCHEMAS_ATTR_CHECKED; |
| 6114 | } |
| 6115 | if (value != NULL) { |
| 6116 | xmlFree(value); |
| 6117 | } |
| 6118 | } |
| 6119 | attributes = attributes->next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6120 | } |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6121 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6122 | } |
| 6123 | |
| 6124 | /** |
| 6125 | * xmlSchemaValidateElement: |
| 6126 | * @ctxt: a schema validation context |
| 6127 | * @elem: an element |
| 6128 | * |
| 6129 | * Validate an element in a tree |
| 6130 | * |
| 6131 | * Returns 0 if the element is schemas valid, a positive error code |
| 6132 | * number otherwise and -1 in case of internal or API error. |
| 6133 | */ |
| 6134 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6135 | xmlSchemaValidateElement(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem) |
| 6136 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6137 | xmlSchemaElementPtr elemDecl; |
| 6138 | int ret, attrBase; |
| 6139 | |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6140 | if (elem->ns != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6141 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6142 | elem->name, elem->ns->href, NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6143 | } else { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6144 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6145 | elem->name, NULL, NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6146 | } |
| 6147 | /* |
| 6148 | * special case whe elementFormDefault is unqualified for top-level elem. |
| 6149 | */ |
| 6150 | if ((elemDecl == NULL) && (elem->ns != NULL) && |
| 6151 | (elem->parent != NULL) && (elem->parent->type != XML_ELEMENT_NODE) && |
| 6152 | (xmlStrEqual(ctxt->schema->targetNamespace, elem->ns->href)) && |
| 6153 | ((ctxt->schema->flags & XML_SCHEMAS_QUALIF_ELEM) == 0)) { |
| 6154 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6155 | elem->name, NULL, NULL); |
| 6156 | } |
| 6157 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6158 | /* |
| 6159 | * 3.3.4 : 1 |
| 6160 | */ |
| 6161 | if (elemDecl == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6162 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_UNDECLAREDELEM, "Element %s not declared\n", elem->name, NULL); |
| 6163 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6164 | } |
| 6165 | if (elemDecl->subtypes == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6166 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_NOTYPE, "Element %s has no type\n", elem->name, NULL); |
| 6167 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6168 | } |
| 6169 | /* |
| 6170 | * Verify the attributes |
| 6171 | */ |
| 6172 | attrBase = ctxt->attrBase; |
| 6173 | ctxt->attrBase = ctxt->attrNr; |
| 6174 | xmlSchemaRegisterAttributes(ctxt, elem->properties); |
| 6175 | xmlSchemaValidateAttributes(ctxt, elem, elemDecl->attributes); |
| 6176 | /* |
| 6177 | * Verify the element content recursively |
| 6178 | */ |
| 6179 | if (elemDecl->contModel != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6180 | ctxt->regexp = xmlRegNewExecCtxt(elemDecl->contModel, |
| 6181 | (xmlRegExecCallbacks) |
| 6182 | xmlSchemaValidateCallback, ctxt); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6183 | #ifdef DEBUG_AUTOMATA |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6184 | xmlGenericError(xmlGenericErrorContext, "====> %s\n", elem->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6185 | #endif |
| 6186 | } |
| 6187 | xmlSchemaValidateType(ctxt, elem, elemDecl, elemDecl->subtypes); |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 6188 | if (elemDecl->contModel != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6189 | ret = xmlRegExecPushString(ctxt->regexp, NULL, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6190 | #ifdef DEBUG_AUTOMATA |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6191 | xmlGenericError(xmlGenericErrorContext, |
| 6192 | "====> %s : %d\n", elem->name, ret); |
Daniel Veillard | decd64d | 2002-04-18 14:41:51 +0000 | [diff] [blame] | 6193 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6194 | if (ret == 0) { |
| 6195 | xmlSchemaVErr(ctxt, elem, XML_SCHEMAS_ERR_ELEMCONT, "Element %s content check failed\n", elem->name, NULL); |
| 6196 | } else if (ret < 0) { |
| 6197 | 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] | 6198 | #ifdef DEBUG_CONTENT |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6199 | } else { |
| 6200 | xmlGenericError(xmlGenericErrorContext, |
| 6201 | "Element %s content check succeeded\n", |
| 6202 | elem->name); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6203 | |
| 6204 | #endif |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6205 | } |
| 6206 | xmlRegFreeExecCtxt(ctxt->regexp); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6207 | } |
| 6208 | /* |
| 6209 | * Verify that all attributes were Schemas-validated |
| 6210 | */ |
| 6211 | xmlSchemaCheckAttributes(ctxt, elem); |
| 6212 | ctxt->attrNr = ctxt->attrBase; |
| 6213 | ctxt->attrBase = attrBase; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6214 | |
| 6215 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6216 | } |
| 6217 | |
| 6218 | /** |
| 6219 | * xmlSchemaValidateDocument: |
| 6220 | * @ctxt: a schema validation context |
| 6221 | * @doc: a parsed document tree |
| 6222 | * |
| 6223 | * Validate a document tree in memory. |
| 6224 | * |
| 6225 | * Returns 0 if the document is schemas valid, a positive error code |
| 6226 | * number otherwise and -1 in case of internal or API error. |
| 6227 | */ |
| 6228 | static int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6229 | xmlSchemaValidateDocument(xmlSchemaValidCtxtPtr ctxt, xmlDocPtr doc) |
| 6230 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6231 | xmlNodePtr root; |
| 6232 | xmlSchemaElementPtr elemDecl; |
| 6233 | |
| 6234 | root = xmlDocGetRootElement(doc); |
| 6235 | if (root == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6236 | xmlSchemaVErr(ctxt, (xmlNodePtr) doc, XML_SCHEMAS_ERR_NOROOT, "document has no root\n", NULL, NULL); |
| 6237 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6238 | } |
| 6239 | if (root->ns != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6240 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6241 | root->name, root->ns->href, NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6242 | else |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6243 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6244 | root->name, NULL, NULL); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 6245 | /* |
| 6246 | * special case whe elementFormDefault is unqualified for top-level elem. |
| 6247 | */ |
| 6248 | if ((elemDecl == NULL) && (root->ns != NULL) && |
| 6249 | (xmlStrEqual(ctxt->schema->targetNamespace, root->ns->href)) && |
| 6250 | ((ctxt->schema->flags & XML_SCHEMAS_QUALIF_ELEM) == 0)) { |
| 6251 | elemDecl = xmlHashLookup3(ctxt->schema->elemDecl, |
| 6252 | root->name, NULL, NULL); |
| 6253 | } |
| 6254 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6255 | if (elemDecl == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6256 | 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] | 6257 | } else if ((elemDecl->flags & XML_SCHEMAS_ELEM_TOPLEVEL) == 0) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6258 | 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] | 6259 | } |
| 6260 | /* |
| 6261 | * Okay, start the recursive validation |
| 6262 | */ |
| 6263 | xmlSchemaValidateElement(ctxt, root); |
| 6264 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6265 | return (ctxt->err); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6266 | } |
| 6267 | |
| 6268 | /************************************************************************ |
| 6269 | * * |
| 6270 | * SAX Validation code * |
| 6271 | * * |
| 6272 | ************************************************************************/ |
| 6273 | |
| 6274 | /************************************************************************ |
| 6275 | * * |
| 6276 | * Validation interfaces * |
| 6277 | * * |
| 6278 | ************************************************************************/ |
| 6279 | |
| 6280 | /** |
| 6281 | * xmlSchemaNewValidCtxt: |
| 6282 | * @schema: a precompiled XML Schemas |
| 6283 | * |
| 6284 | * Create an XML Schemas validation context based on the given schema |
| 6285 | * |
| 6286 | * Returns the validation context or NULL in case of error |
| 6287 | */ |
| 6288 | xmlSchemaValidCtxtPtr |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6289 | xmlSchemaNewValidCtxt(xmlSchemaPtr schema) |
| 6290 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6291 | xmlSchemaValidCtxtPtr ret; |
| 6292 | |
| 6293 | ret = (xmlSchemaValidCtxtPtr) xmlMalloc(sizeof(xmlSchemaValidCtxt)); |
| 6294 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6295 | xmlSchemaVErrMemory(NULL, "allocating validation context", NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6296 | return (NULL); |
| 6297 | } |
| 6298 | memset(ret, 0, sizeof(xmlSchemaValidCtxt)); |
| 6299 | ret->schema = schema; |
| 6300 | ret->attrNr = 0; |
| 6301 | ret->attrMax = 10; |
| 6302 | ret->attr = (xmlSchemaAttrStatePtr) xmlMalloc(ret->attrMax * |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6303 | sizeof |
| 6304 | (xmlSchemaAttrState)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6305 | if (ret->attr == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6306 | xmlSchemaVErrMemory(NULL, "allocating validation context", NULL); |
| 6307 | free(ret); |
| 6308 | return (NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6309 | } |
| 6310 | memset(ret->attr, 0, ret->attrMax * sizeof(xmlSchemaAttrState)); |
| 6311 | return (ret); |
| 6312 | } |
| 6313 | |
| 6314 | /** |
| 6315 | * xmlSchemaFreeValidCtxt: |
| 6316 | * @ctxt: the schema validation context |
| 6317 | * |
| 6318 | * Free the resources associated to the schema validation context |
| 6319 | */ |
| 6320 | void |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6321 | xmlSchemaFreeValidCtxt(xmlSchemaValidCtxtPtr ctxt) |
| 6322 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6323 | if (ctxt == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6324 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6325 | if (ctxt->attr != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6326 | xmlFree(ctxt->attr); |
Daniel Veillard | 88c5891 | 2002-04-23 07:12:20 +0000 | [diff] [blame] | 6327 | if (ctxt->value != NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6328 | xmlSchemaFreeValue(ctxt->value); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6329 | xmlFree(ctxt); |
| 6330 | } |
| 6331 | |
| 6332 | /** |
| 6333 | * xmlSchemaSetValidErrors: |
| 6334 | * @ctxt: a schema validation context |
| 6335 | * @err: the error function |
| 6336 | * @warn: the warning function |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 6337 | * @ctx: the functions context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6338 | * |
| 6339 | * Set the error and warning callback informations |
| 6340 | */ |
| 6341 | void |
| 6342 | xmlSchemaSetValidErrors(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6343 | xmlSchemaValidityErrorFunc err, |
| 6344 | xmlSchemaValidityWarningFunc warn, void *ctx) |
| 6345 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6346 | if (ctxt == NULL) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6347 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6348 | ctxt->error = err; |
| 6349 | ctxt->warning = warn; |
| 6350 | ctxt->userData = ctx; |
| 6351 | } |
| 6352 | |
| 6353 | /** |
| 6354 | * xmlSchemaValidateDoc: |
| 6355 | * @ctxt: a schema validation context |
| 6356 | * @doc: a parsed document tree |
| 6357 | * |
| 6358 | * Validate a document tree in memory. |
| 6359 | * |
| 6360 | * Returns 0 if the document is schemas valid, a positive error code |
| 6361 | * number otherwise and -1 in case of internal or API error. |
| 6362 | */ |
| 6363 | int |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6364 | xmlSchemaValidateDoc(xmlSchemaValidCtxtPtr ctxt, xmlDocPtr doc) |
| 6365 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6366 | int ret; |
| 6367 | |
| 6368 | if ((ctxt == NULL) || (doc == NULL)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6369 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6370 | |
| 6371 | ctxt->doc = doc; |
| 6372 | ret = xmlSchemaValidateDocument(ctxt, doc); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6373 | return (ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6374 | } |
| 6375 | |
| 6376 | /** |
| 6377 | * xmlSchemaValidateStream: |
| 6378 | * @ctxt: a schema validation context |
| 6379 | * @input: the input to use for reading the data |
| 6380 | * @enc: an optional encoding information |
| 6381 | * @sax: a SAX handler for the resulting events |
| 6382 | * @user_data: the context to provide to the SAX handler. |
| 6383 | * |
| 6384 | * Validate a document tree in memory. |
| 6385 | * |
| 6386 | * Returns 0 if the document is schemas valid, a positive error code |
| 6387 | * number otherwise and -1 in case of internal or API error. |
| 6388 | */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6389 | int |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6390 | xmlSchemaValidateStream(xmlSchemaValidCtxtPtr ctxt, |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6391 | xmlParserInputBufferPtr input, xmlCharEncoding enc, |
| 6392 | xmlSAXHandlerPtr sax, void *user_data) |
| 6393 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6394 | if ((ctxt == NULL) || (input == NULL)) |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6395 | return (-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6396 | ctxt->input = input; |
| 6397 | ctxt->enc = enc; |
| 6398 | ctxt->sax = sax; |
| 6399 | ctxt->user_data = user_data; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 6400 | TODO return (0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6401 | } |
| 6402 | |
| 6403 | #endif /* LIBXML_SCHEMAS_ENABLED */ |