blob: db30dc9ffbe3d5fcecf9b4225ee7ca09e1002dea [file] [log] [blame]
Daniel Veillard4255d502002-04-16 15:50:10 +00001/*
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
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010/*
Daniel Veillardb0f397e2003-12-23 23:30:53 +000011 * 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.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015 * - if we don't intend to use the schema for schemas, we
Daniel Veillard01fa6152004-06-29 17:04:39 +000016 * need to validate all schema attributes (ref, type, name)
17 * against their types.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018 * - Eliminate item creation for: ??
19 *
20 * NOTES:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021 * - Elimated item creation for: <restriction>, <extension>,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000022 * <simpleContent>, <complexContent>, <list>, <union>
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023 *
Daniel Veillardb0f397e2003-12-23 23:30:53 +000024 */
Daniel Veillard4255d502002-04-16 15:50:10 +000025#define IN_LIBXML
26#include "libxml.h"
27
28#ifdef LIBXML_SCHEMAS_ENABLED
29
30#include <string.h>
31#include <libxml/xmlmemory.h>
32#include <libxml/parser.h>
33#include <libxml/parserInternals.h>
34#include <libxml/hash.h>
Daniel Veillard5a872412002-05-22 06:40:27 +000035#include <libxml/uri.h>
Daniel Veillard4255d502002-04-16 15:50:10 +000036#include <libxml/xmlschemas.h>
37#include <libxml/schemasInternals.h>
38#include <libxml/xmlschemastypes.h>
39#include <libxml/xmlautomata.h>
40#include <libxml/xmlregexp.h>
Daniel Veillardbe9c6322003-11-22 20:37:51 +000041#include <libxml/dict.h>
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000042#include <libxml/encoding.h>
43#include <libxml/xmlIO.h>
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000044#ifdef LIBXML_PATTERN_ENABLED
45#include <libxml/pattern.h>
46#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000047#ifdef LIBXML_READER_ENABLED
48#include <libxml/xmlreader.h>
49#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000050
Daniel Veillarda84c0b32003-06-02 16:58:46 +000051/* #define DEBUG 1 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000052
Daniel Veillard82bbbd42003-05-11 20:16:09 +000053/* #define DEBUG_CONTENT 1 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000054
Daniel Veillard82bbbd42003-05-11 20:16:09 +000055/* #define DEBUG_TYPE 1 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000056
Daniel Veillard118aed72002-09-24 14:13:13 +000057/* #define DEBUG_CONTENT_REGEXP 1 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000058
Daniel Veillard4255d502002-04-16 15:50:10 +000059/* #define DEBUG_AUTOMATA 1 */
60
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000061#define DEBUG_ATTR_VALIDATION 0
Daniel Veillardc0826a72004-08-10 14:17:33 +000062
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000063/* #define DEBUG_IDC 1 */
64
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000065/* #define DEBUG_INCLUDES 1 */
66
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000067/* #define ENABLE_PARTICLE_RESTRICTION 1 */
68
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000069#define DUMP_CONTENT_MODEL
70
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000071#define XML_SCHEMA_SAX_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000072
73#ifdef LIBXML_READER_ENABLED
74/* #define XML_SCHEMA_READER_ENABLED */
75#endif
Daniel Veillardc0826a72004-08-10 14:17:33 +000076
Daniel Veillard4255d502002-04-16 15:50:10 +000077#define UNBOUNDED (1 << 30)
78#define TODO \
79 xmlGenericError(xmlGenericErrorContext, \
80 "Unimplemented block at %s:%d\n", \
81 __FILE__, __LINE__);
82
William M. Brack2f2a6632004-08-20 23:09:47 +000083#define XML_SCHEMAS_NO_NAMESPACE (const xmlChar *) "##"
Daniel Veillardc0826a72004-08-10 14:17:33 +000084
Daniel Veillard4255d502002-04-16 15:50:10 +000085/*
86 * The XML Schemas namespaces
87 */
88static const xmlChar *xmlSchemaNs = (const xmlChar *)
89 "http://www.w3.org/2001/XMLSchema";
90
91static const xmlChar *xmlSchemaInstanceNs = (const xmlChar *)
92 "http://www.w3.org/2001/XMLSchema-instance";
93
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000094static const xmlChar *xmlNamespaceNs = (const xmlChar *)
95 "http://www.w3.org/2000/xmlns/";
96
Daniel Veillardc0826a72004-08-10 14:17:33 +000097static const xmlChar *xmlSchemaElemDesElemDecl = (const xmlChar *)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000098 "element decl.";
Daniel Veillardc0826a72004-08-10 14:17:33 +000099static const xmlChar *xmlSchemaElemDesAttrDecl = (const xmlChar *)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000100 "attribute decl.";
Daniel Veillardc0826a72004-08-10 14:17:33 +0000101static const xmlChar *xmlSchemaElemDesAttrRef = (const xmlChar *)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000102 "attribute use";
Daniel Veillardc0826a72004-08-10 14:17:33 +0000103static const xmlChar *xmlSchemaElemDesCT = (const xmlChar *)
Kasimier T. Buchcik8b418172004-11-17 13:14:27 +0000104 "complex type";
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000105static const xmlChar *xmlSchemaElemModelGrDef = (const xmlChar *)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000106 "model group";
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000107#if 0
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000108static const xmlChar *xmlSchemaElemModelGrRef = (const xmlChar *)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000109 "model group ref.";
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000110#endif
Daniel Veillardc0826a72004-08-10 14:17:33 +0000111
Daniel Veillard4255d502002-04-16 15:50:10 +0000112#define IS_SCHEMA(node, type) \
113 ((node != NULL) && (node->ns != NULL) && \
114 (xmlStrEqual(node->name, (const xmlChar *) type)) && \
115 (xmlStrEqual(node->ns->href, xmlSchemaNs)))
116
Daniel Veillardc0826a72004-08-10 14:17:33 +0000117#define FREE_AND_NULL(str) \
118 if (str != NULL) { \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000119 xmlFree((xmlChar *) str); \
Daniel Veillardc0826a72004-08-10 14:17:33 +0000120 str = NULL; \
121 }
122
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000123#define IS_ANYTYPE(item) \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000124 ((item->type == XML_SCHEMA_TYPE_BASIC) && \
125 (item->builtInType == XML_SCHEMAS_ANYTYPE))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000126
127#define IS_COMPLEX_TYPE(item) \
128 ((item->type == XML_SCHEMA_TYPE_COMPLEX) || \
129 (item->builtInType == XML_SCHEMAS_ANYTYPE))
130
131#define IS_SIMPLE_TYPE(item) \
132 ((item->type == XML_SCHEMA_TYPE_SIMPLE) || \
133 ((item->type == XML_SCHEMA_TYPE_BASIC) && \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000134 (item->builtInType != XML_SCHEMAS_ANYTYPE)))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000135
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000136#define IS_ANY_SIMPLE_TYPE(item) \
137 ((item->type == XML_SCHEMA_TYPE_BASIC) && \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000138 (item->builtInType == XML_SCHEMAS_ANYSIMPLETYPE))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000139
140#define IS_NOT_TYPEFIXED(item) \
141 ((item->type != XML_SCHEMA_TYPE_BASIC) && \
142 ((item->flags & XML_SCHEMAS_TYPE_INTERNAL_RESOLVED) == 0))
143
144#define HAS_COMPLEX_CONTENT(item) \
145 ((item->contentType == XML_SCHEMA_CONTENT_MIXED) || \
146 (item->contentType == XML_SCHEMA_CONTENT_EMPTY) || \
147 (item->contentType == XML_SCHEMA_CONTENT_ELEMENTS))
148
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +0000149#define HAS_SIMPLE_CONTENT(item) \
150 ((item->contentType == XML_SCHEMA_CONTENT_SIMPLE) || \
151 (item->contentType == XML_SCHEMA_CONTENT_BASIC))
152
153#define HAS_MIXED_CONTENT(item) (item->contentType == XML_SCHEMA_CONTENT_MIXED)
154
155#define IS_PARTICLE_EMPTIABLE(item) \
156 (xmlSchemaIsParticleEmptiable((xmlSchemaParticlePtr) item->subtypes))
157
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000158#define GET_NODE(item) xmlSchemaGetComponentNode((xmlSchemaBasicItemPtr) item)
159
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000160#define GET_LIST_ITEM_TYPE(item) item->subtypes
161
162#define VARIETY_ATOMIC(item) (item->flags & XML_SCHEMAS_TYPE_VARIETY_ATOMIC)
163#define VARIETY_LIST(item) (item->flags & XML_SCHEMAS_TYPE_VARIETY_LIST)
164#define VARIETY_UNION(item) (item->flags & XML_SCHEMAS_TYPE_VARIETY_UNION)
165
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000166#define IS_MODEL_GROUP(item) \
167 ((item->type == XML_SCHEMA_TYPE_SEQUENCE) || \
168 (item->type == XML_SCHEMA_TYPE_CHOICE) || \
169 (item->type == XML_SCHEMA_TYPE_ALL))
170
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000171#define INODE_NILLED(item) (item->flags & XML_SCHEMA_ELEM_INFO_NILLED)
172
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +0000173#define ELEM_TYPE(item) item->subtypes
174
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +0000175#define GET_PARTICLE(item) (xmlSchemaParticlePtr) item->subtypes;
176
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +0000177#define SUBST_GROUP_AFF(item) item->refDecl
178
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000179#if 0
180#define WXS_GET_NEXT(item) xmlSchemaGetNextComponent((xmlSchemaBasicItemPtr) item)
181#endif
182
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +0000183#define SUBSET_RESTRICTION 1<<0
184#define SUBSET_EXTENSION 1<<1
185#define SUBSET_SUBSTITUTION 1<<2
186#define SUBSET_LIST 1<<3
187#define SUBSET_UNION 1<<4
188
Daniel Veillard4255d502002-04-16 15:50:10 +0000189#define XML_SCHEMAS_PARSE_ERROR 1
190
Daniel Veillardbd2904b2003-11-25 15:38:59 +0000191#define SCHEMAS_PARSE_OPTIONS XML_PARSE_NOENT
192
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000193typedef struct _xmlSchemaNodeInfo xmlSchemaNodeInfo;
194typedef xmlSchemaNodeInfo *xmlSchemaNodeInfoPtr;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000195
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000196
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000197typedef struct _xmlSchemaItemList xmlSchemaAssemble;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000198typedef xmlSchemaAssemble *xmlSchemaAssemblePtr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000199
200typedef struct _xmlSchemaItemList xmlSchemaItemList;
201typedef xmlSchemaItemList *xmlSchemaItemListPtr;
202
203struct _xmlSchemaItemList {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000204 void **items; /* used for dynamic addition of schemata */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000205 int nbItems; /* used for dynamic addition of schemata */
206 int sizeItems; /* used for dynamic addition of schemata */
207};
208
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000209typedef struct _xmlSchemaAbstractCtxt xmlSchemaAbstractCtxt;
210typedef xmlSchemaAbstractCtxt *xmlSchemaAbstractCtxtPtr;
211struct _xmlSchemaAbstractCtxt {
212 int type;
213};
214
215#define XML_SCHEMA_CTXT_PARSER 1
216#define XML_SCHEMA_CTXT_VALIDATOR 2
217
Daniel Veillard4255d502002-04-16 15:50:10 +0000218struct _xmlSchemaParserCtxt {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000219 int type;
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000220 void *userData; /* user specific data block */
221 xmlSchemaValidityErrorFunc error; /* the callback in case of errors */
222 xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillarde19fc232002-04-22 16:01:24 +0000223 xmlSchemaValidError err;
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000224 int nberrors;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000225 xmlStructuredErrorFunc serror;
Daniel Veillard4255d502002-04-16 15:50:10 +0000226
Daniel Veillardbe9c6322003-11-22 20:37:51 +0000227 xmlSchemaPtr topschema; /* The main schema */
228 xmlHashTablePtr namespaces; /* Hash table of namespaces to schemas */
229
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000230 xmlSchemaPtr schema; /* The schema in use */
Daniel Veillardbe9c6322003-11-22 20:37:51 +0000231 const xmlChar *container; /* the current element, group, ... */
Daniel Veillard4255d502002-04-16 15:50:10 +0000232 int counter;
233
Daniel Veillardbe9c6322003-11-22 20:37:51 +0000234 const xmlChar *URL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000235 xmlDocPtr doc;
Daniel Veillarddda22c12004-01-24 08:31:30 +0000236 int preserve; /* Whether the doc should be freed */
Daniel Veillard4255d502002-04-16 15:50:10 +0000237
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000238 const char *buffer;
239 int size;
Daniel Veillard6045c902002-10-09 21:13:59 +0000240
Daniel Veillard4255d502002-04-16 15:50:10 +0000241 /*
242 * Used to build complex element content models
243 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000244 xmlAutomataPtr am;
Daniel Veillard4255d502002-04-16 15:50:10 +0000245 xmlAutomataStatePtr start;
246 xmlAutomataStatePtr end;
247 xmlAutomataStatePtr state;
Daniel Veillardbe9c6322003-11-22 20:37:51 +0000248
249 xmlDictPtr dict; /* dictionnary for interned string names */
Daniel Veillardb0f397e2003-12-23 23:30:53 +0000250 int includes; /* the inclusion level, 0 for root or imports */
Daniel Veillard01fa6152004-06-29 17:04:39 +0000251 xmlSchemaTypePtr ctxtType; /* The current context simple/complex type */
252 xmlSchemaTypePtr parentItem; /* The current parent schema item */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000253 xmlSchemaAssemblePtr assemble;
254 int options;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000255 xmlSchemaValidCtxtPtr vctxt;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +0000256 const xmlChar **localImports; /* list of locally imported namespaces */
257 int sizeLocalImports;
258 int nbLocalImports;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +0000259 xmlHashTablePtr substGroups;
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +0000260 int isS4S;
Daniel Veillard4255d502002-04-16 15:50:10 +0000261};
262
Daniel Veillard4255d502002-04-16 15:50:10 +0000263#define XML_SCHEMAS_ATTR_UNKNOWN 1
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000264#define XML_SCHEMAS_ATTR_ASSESSED 2
Daniel Veillard3646d642004-06-02 19:19:14 +0000265#define XML_SCHEMAS_ATTR_PROHIBITED 3
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000266#define XML_SCHEMAS_ATTR_ERR_MISSING 4
Daniel Veillard3646d642004-06-02 19:19:14 +0000267#define XML_SCHEMAS_ATTR_INVALID_VALUE 5
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000268#define XML_SCHEMAS_ATTR_ERR_NO_TYPE 6
269#define XML_SCHEMAS_ATTR_ERR_FIXED_VALUE 7
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000270#define XML_SCHEMAS_ATTR_DEFAULT 8
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +0000271#define XML_SCHEMAS_ATTR_VALIDATE_VALUE 9
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000272#define XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL 10
273#define XML_SCHEMAS_ATTR_HAS_ATTR_USE 11
274#define XML_SCHEMAS_ATTR_HAS_ATTR_DECL 12
275#define XML_SCHEMAS_ATTR_WILD_SKIP 13
276#define XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL 14
277#define XML_SCHEMAS_ATTR_ERR_WILD_DUPLICATE_ID 15
278#define XML_SCHEMAS_ATTR_ERR_WILD_AND_USE_ID 16
279#define XML_SCHEMAS_ATTR_META 17
Daniel Veillard4255d502002-04-16 15:50:10 +0000280
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000281/**
282 * xmlSchemaBasicItem:
283 *
284 * The abstract base type for schema components.
285 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000286typedef struct _xmlSchemaBasicItem xmlSchemaBasicItem;
287typedef xmlSchemaBasicItem *xmlSchemaBasicItemPtr;
288struct _xmlSchemaBasicItem {
289 xmlSchemaTypeType type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000290};
291
292/**
293 * xmlSchemaAnnotItem:
294 *
295 * The abstract base type for annotated schema components.
296 * (Extends xmlSchemaBasicItem)
297 */
298typedef struct _xmlSchemaAnnotItem xmlSchemaAnnotItem;
299typedef xmlSchemaAnnotItem *xmlSchemaAnnotItemPtr;
300struct _xmlSchemaAnnotItem {
301 xmlSchemaTypeType type;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000302 xmlSchemaAnnotPtr annot;
303};
304
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000305/**
306 * xmlSchemaTreeItem:
307 *
308 * The abstract base type for tree-like structured schema components.
309 * (Extends xmlSchemaAnnotItem)
310 */
311typedef struct _xmlSchemaTreeItem xmlSchemaTreeItem;
312typedef xmlSchemaTreeItem *xmlSchemaTreeItemPtr;
313struct _xmlSchemaTreeItem {
314 xmlSchemaTypeType type;
315 xmlSchemaAnnotPtr annot;
316 xmlSchemaTreeItemPtr next;
317 xmlSchemaTreeItemPtr children;
318};
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000319
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000320/**
321 * xmlSchemaQNameRef:
322 *
323 * A component reference item (not a schema component)
324 * (Extends xmlSchemaBasicItem)
325 */
326typedef struct _xmlSchemaQNameRef xmlSchemaQNameRef;
327typedef xmlSchemaQNameRef *xmlSchemaQNameRefPtr;
328struct _xmlSchemaQNameRef {
329 xmlSchemaTypeType type;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000330 xmlSchemaBasicItemPtr item;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000331 xmlSchemaTypeType itemType;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000332 const xmlChar *name;
333 const xmlChar *targetNamespace;
334};
335
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000336/**
337 * xmlSchemaParticle:
338 *
339 * A particle component.
340 * (Extends xmlSchemaTreeItem)
341 */
342typedef struct _xmlSchemaParticle xmlSchemaParticle;
343typedef xmlSchemaParticle *xmlSchemaParticlePtr;
344struct _xmlSchemaParticle {
345 xmlSchemaTypeType type;
346 xmlSchemaAnnotPtr annot;
347 xmlSchemaTreeItemPtr next; /* next particle (OR "element decl" OR "wildcard") */
348 xmlSchemaTreeItemPtr children; /* the "term" ("model group" OR "group definition") */
349 int minOccurs;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000350 int maxOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000351 xmlNodePtr node;
352};
353
354/**
355 * xmlSchemaModelGroup:
356 *
357 * A model group component.
358 * (Extends xmlSchemaTreeItem)
359 */
360typedef struct _xmlSchemaModelGroup xmlSchemaModelGroup;
361typedef xmlSchemaModelGroup *xmlSchemaModelGroupPtr;
362struct _xmlSchemaModelGroup {
363 xmlSchemaTypeType type; /* XML_SCHEMA_TYPE_SEQUENCE, XML_SCHEMA_TYPE_CHOICE, XML_SCHEMA_TYPE_ALL */
364 xmlSchemaAnnotPtr annot;
365 xmlSchemaTreeItemPtr next; /* not used */
366 xmlSchemaTreeItemPtr children; /* first particle (OR "element decl" OR "wildcard") */
367 xmlNodePtr node;
368};
369
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +0000370#define XML_SCHEMA_MODEL_GROUP_DEF_MARKED 1<<0
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000371/**
372 * xmlSchemaModelGroupDef:
373 *
374 * A model group definition component.
375 * (Extends xmlSchemaTreeItem)
376 */
377typedef struct _xmlSchemaModelGroupDef xmlSchemaModelGroupDef;
378typedef xmlSchemaModelGroupDef *xmlSchemaModelGroupDefPtr;
379struct _xmlSchemaModelGroupDef {
380 xmlSchemaTypeType type; /* XML_SCHEMA_TYPE_GROUP */
381 xmlSchemaAnnotPtr annot;
382 xmlSchemaTreeItemPtr next; /* not used */
383 xmlSchemaTreeItemPtr children; /* the "model group" */
384 const xmlChar *name;
385 const xmlChar *targetNamespace;
386 xmlNodePtr node;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +0000387 int flags;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000388};
389
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000390typedef struct _xmlSchemaIDC xmlSchemaIDC;
391typedef xmlSchemaIDC *xmlSchemaIDCPtr;
392
393/**
394 * xmlSchemaIDCSelect:
395 *
396 * The identity-constraint "field" and "selector" item, holding the
397 * XPath expression.
398 */
399typedef struct _xmlSchemaIDCSelect xmlSchemaIDCSelect;
400typedef xmlSchemaIDCSelect *xmlSchemaIDCSelectPtr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000401struct _xmlSchemaIDCSelect {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000402 xmlSchemaIDCSelectPtr next;
403 xmlSchemaIDCPtr idc;
404 int index; /* an index position if significant for IDC key-sequences */
405 const xmlChar *xpath; /* the XPath expression */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +0000406 void *xpathComp; /* the compiled XPath expression */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000407};
408
409/**
410 * xmlSchemaIDC:
411 *
412 * The identity-constraint definition component.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000413 * (Extends xmlSchemaAnnotItem)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000414 */
415
416struct _xmlSchemaIDC {
417 xmlSchemaTypeType type;
418 xmlSchemaAnnotPtr annot;
419 xmlSchemaIDCPtr next;
420 xmlNodePtr node;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000421 const xmlChar *name;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000422 const xmlChar *targetNamespace;
423 xmlSchemaIDCSelectPtr selector;
424 xmlSchemaIDCSelectPtr fields;
425 int nbFields;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000426 xmlSchemaQNameRefPtr ref;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000427};
428
429/**
430 * xmlSchemaIDCAug:
431 *
432 * The augmented IDC information used for validation.
433 */
434typedef struct _xmlSchemaIDCAug xmlSchemaIDCAug;
435typedef xmlSchemaIDCAug *xmlSchemaIDCAugPtr;
436struct _xmlSchemaIDCAug {
437 xmlSchemaIDCAugPtr next; /* next in a list */
438 xmlSchemaIDCPtr def; /* the IDC definition */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000439 int bubbleDepth; /* the lowest tree level to which IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000440 tables need to be bubbled upwards */
441};
442
443/**
444 * xmlSchemaPSVIIDCKeySequence:
445 *
446 * The key sequence of a node table item.
447 */
448typedef struct _xmlSchemaPSVIIDCKey xmlSchemaPSVIIDCKey;
449typedef xmlSchemaPSVIIDCKey *xmlSchemaPSVIIDCKeyPtr;
450struct _xmlSchemaPSVIIDCKey {
451 xmlSchemaTypePtr type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000452 xmlSchemaValPtr val;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000453};
454
455/**
456 * xmlSchemaPSVIIDCNode:
457 *
458 * The node table item of a node table.
459 */
460typedef struct _xmlSchemaPSVIIDCNode xmlSchemaPSVIIDCNode;
461typedef xmlSchemaPSVIIDCNode *xmlSchemaPSVIIDCNodePtr;
462struct _xmlSchemaPSVIIDCNode {
463 xmlNodePtr node;
464 xmlSchemaPSVIIDCKeyPtr *keys;
465};
466
467/**
468 * xmlSchemaPSVIIDCBinding:
469 *
470 * The identity-constraint binding item of the [identity-constraint table].
471 */
472typedef struct _xmlSchemaPSVIIDCBinding xmlSchemaPSVIIDCBinding;
473typedef xmlSchemaPSVIIDCBinding *xmlSchemaPSVIIDCBindingPtr;
474struct _xmlSchemaPSVIIDCBinding {
475 xmlSchemaPSVIIDCBindingPtr next; /* next binding of a specific node */
476 xmlSchemaIDCPtr definition; /* the IDC definition */
477 xmlSchemaPSVIIDCNodePtr *nodeTable; /* array of key-sequences */
478 int nbNodes; /* number of entries in the node table */
479 int sizeNodes; /* size of the node table */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000480 int nbDupls; /* number of already identified duplicates in the node
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000481 table */
482 /* int nbKeys; number of keys in each key-sequence */
483};
484
485#define XPATH_STATE_OBJ_TYPE_IDC_SELECTOR 1
486#define XPATH_STATE_OBJ_TYPE_IDC_FIELD 2
487
488#define XPATH_STATE_OBJ_MATCHES -2
489#define XPATH_STATE_OBJ_BLOCKED -3
490
491typedef struct _xmlSchemaIDCMatcher xmlSchemaIDCMatcher;
492typedef xmlSchemaIDCMatcher *xmlSchemaIDCMatcherPtr;
493
494/**
495 * xmlSchemaIDCStateObj:
496 *
497 * The state object used to evaluate XPath expressions.
498 */
499typedef struct _xmlSchemaIDCStateObj xmlSchemaIDCStateObj;
500typedef xmlSchemaIDCStateObj *xmlSchemaIDCStateObjPtr;
501struct _xmlSchemaIDCStateObj {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000502 int type;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000503 xmlSchemaIDCStateObjPtr next; /* next if in a list */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +0000504 int depth; /* depth of creation */
505 int *history; /* list of (depth, state-id) tuples */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000506 int nbHistory;
507 int sizeHistory;
508 xmlSchemaIDCMatcherPtr matcher; /* the correspondent field/selector
509 matcher */
510 xmlSchemaIDCSelectPtr sel;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +0000511 void *xpathCtxt;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000512};
513
514#define IDC_MATCHER 0
515
516/**
517 * xmlSchemaIDCMatcher:
518 *
519 * Used to IDC selectors (and fields) successively.
520 */
521struct _xmlSchemaIDCMatcher {
522 int type;
523 int depth; /* the tree depth at creation time */
524 xmlSchemaIDCMatcherPtr next; /* next in the list */
525 xmlSchemaIDCAugPtr aidc; /* the augmented IDC item */
526 xmlSchemaPSVIIDCKeyPtr **keySeqs; /* the key-sequences of the target
527 elements */
528 int sizeKeySeqs;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000529 int targetDepth;
530};
531
532/*
533* Element info flags.
534*/
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000535#define XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES 1<<0
536#define XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES 1<<1
537#define XML_SCHEMA_ELEM_INFO_NILLED 1<<2
538#define XML_SCHEMA_ELEM_INFO_LOCAL_TYPE 1<<3
539
540#define XML_SCHEMA_NODE_INFO_VALUE_NEEDED 1<<4
541#define XML_SCHEMA_ELEM_INFO_EMPTY 1<<5
542#define XML_SCHEMA_ELEM_INFO_HAS_CONTENT 1<<6
543
544#define XML_SCHEMA_ELEM_INFO_HAS_ELEM_CONTENT 1<<7
545#define XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT 1<<8
546#define XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED 1<<9
547#define XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE 1<<10
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000548
549/**
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +0000550 * xmlSchemaNodeInfo:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000551 *
552 * Holds information of an element node.
553 */
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +0000554struct _xmlSchemaNodeInfo {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000555 xmlNodePtr node;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000556 int nodeType;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000557 const xmlChar *localName;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000558 const xmlChar *nsName;
559 const xmlChar *value;
560 xmlSchemaValPtr val; /* the pre-computed value if any */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000561 xmlSchemaTypePtr typeDef; /* the complex/simple type definition if any */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000562 int flags; /* combination of node info flags */
563 int valNeeded;
564 int normVal;
565
566 xmlSchemaElementPtr decl; /* the element/attribute declaration */
567 int depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000568 xmlSchemaPSVIIDCBindingPtr idcTable; /* the table of PSVI IDC bindings
569 for the scope element*/
570 xmlSchemaIDCMatcherPtr idcMatchers; /* the IDC matchers for the scope
571 element */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000572 xmlRegExecCtxtPtr regexCtxt;
573
574 const xmlChar **nsBindings; /* Namespace bindings on this element */
575 int nbNsBindings;
576 int sizeNsBindings;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000577};
578
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000579/*
580* @metaType values of xmlSchemaAttrInfo.
581*/
582#define XML_SCHEMA_ATTR_INFO_META_XSI_TYPE 1
583#define XML_SCHEMA_ATTR_INFO_META_XSI_NIL 2
584#define XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC 3
585#define XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC 4
586#define XML_SCHEMA_ATTR_INFO_META_XMLNS 5
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000587
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000588typedef struct _xmlSchemaAttrInfo xmlSchemaAttrInfo;
589typedef xmlSchemaAttrInfo *xmlSchemaAttrInfoPtr;
590struct _xmlSchemaAttrInfo {
591 xmlNodePtr node;
592 int nodeType;
593 const xmlChar *localName;
594 const xmlChar *nsName;
595 const xmlChar *value;
596 xmlSchemaValPtr val; /* the pre-computed value if any */
597 xmlSchemaTypePtr typeDef; /* the complex/simple type definition if any */
598 int flags; /* combination of node info flags */
599
600 xmlSchemaAttributePtr decl; /* the attribute declaration */
601 xmlSchemaAttributePtr use; /* the attribute use */
602 int state;
603 int metaType;
604 const xmlChar *vcValue; /* the value constraint value */
605 xmlSchemaNodeInfoPtr parent;
606};
607
608
609#define XML_SCHEMA_VALID_CTXT_FLAG_STREAM 1
Daniel Veillard4255d502002-04-16 15:50:10 +0000610/**
611 * xmlSchemaValidCtxt:
612 *
613 * A Schemas validation context
614 */
Daniel Veillard4255d502002-04-16 15:50:10 +0000615struct _xmlSchemaValidCtxt {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000616 int type;
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000617 void *userData; /* user specific data block */
618 xmlSchemaValidityErrorFunc error; /* the callback in case of errors */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000619 xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000620 xmlStructuredErrorFunc serror;
Daniel Veillard4255d502002-04-16 15:50:10 +0000621
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000622 xmlSchemaPtr schema; /* The schema in use */
623 xmlDocPtr doc;
Daniel Veillard4255d502002-04-16 15:50:10 +0000624 xmlParserInputBufferPtr input;
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000625 xmlCharEncoding enc;
626 xmlSAXHandlerPtr sax;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000627 xmlParserCtxtPtr parserCtxt;
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000628 void *user_data;
Daniel Veillard4255d502002-04-16 15:50:10 +0000629
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000630 int err;
631 int nberrors;
Daniel Veillard4255d502002-04-16 15:50:10 +0000632
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000633 xmlNodePtr node;
634 xmlNodePtr cur;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000635 /* xmlSchemaTypePtr type; */
Daniel Veillard4255d502002-04-16 15:50:10 +0000636
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000637 xmlRegExecCtxtPtr regexp;
638 xmlSchemaValPtr value;
Daniel Veillard4255d502002-04-16 15:50:10 +0000639
Daniel Veillardc0826a72004-08-10 14:17:33 +0000640 int valueWS;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000641 int options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000642 xmlNodePtr validationRoot;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000643 xmlSchemaParserCtxtPtr pctxt;
644 int xsiAssemble;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000645
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000646 int depth;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +0000647 xmlSchemaNodeInfoPtr *elemInfos; /* array of element informations */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000648 int sizeElemInfos;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000649 xmlSchemaNodeInfoPtr inode; /* the current element information */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000650
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000651 xmlSchemaIDCAugPtr aidcs; /* a list of augmented IDC informations */
652
653 xmlSchemaIDCStateObjPtr xpathStates; /* first active state object. */
654 xmlSchemaIDCStateObjPtr xpathStatePool; /* first stored state object. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000655
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000656 xmlSchemaPSVIIDCNodePtr *idcNodes; /* list of all IDC node-table entries*/
657 int nbIdcNodes;
658 int sizeIdcNodes;
659
660 xmlSchemaPSVIIDCKeyPtr *idcKeys; /* list of all IDC node-table entries */
661 int nbIdcKeys;
662 int sizeIdcKeys;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000663
664 int flags;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000665
666 xmlDictPtr dict;
667
Daniel Veillard39e5c892005-07-03 22:48:50 +0000668#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000669 xmlTextReaderPtr reader;
Daniel Veillard39e5c892005-07-03 22:48:50 +0000670#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000671
672 xmlSchemaAttrInfoPtr *attrInfos;
673 int nbAttrInfos;
674 int sizeAttrInfos;
675
676 int skipDepth;
Daniel Veillard4255d502002-04-16 15:50:10 +0000677};
678
Daniel Veillard1d913862003-11-21 00:28:39 +0000679/*
680 * These are the entries in the schemas importSchemas hash table
681 */
682typedef struct _xmlSchemaImport xmlSchemaImport;
683typedef xmlSchemaImport *xmlSchemaImportPtr;
684struct _xmlSchemaImport {
685 const xmlChar *schemaLocation;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000686 xmlSchemaPtr schema; /* not used any more */
William M. Brack2f2a6632004-08-20 23:09:47 +0000687 xmlDocPtr doc;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000688 int isMain;
Daniel Veillard1d913862003-11-21 00:28:39 +0000689};
Daniel Veillard4255d502002-04-16 15:50:10 +0000690
Daniel Veillardbd2904b2003-11-25 15:38:59 +0000691/*
692 * These are the entries associated to includes in a schemas
693 */
694typedef struct _xmlSchemaInclude xmlSchemaInclude;
695typedef xmlSchemaInclude *xmlSchemaIncludePtr;
696struct _xmlSchemaInclude {
697 xmlSchemaIncludePtr next;
Daniel Veillardbd2904b2003-11-25 15:38:59 +0000698 const xmlChar *schemaLocation;
699 xmlDocPtr doc;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +0000700 const xmlChar *origTargetNamespace;
701 const xmlChar *targetNamespace;
Daniel Veillardbd2904b2003-11-25 15:38:59 +0000702};
703
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +0000704/**
705 * xmlSchemaSubstGroup:
706 *
707 *
708 */
709typedef struct _xmlSchemaSubstGroup xmlSchemaSubstGroup;
710typedef xmlSchemaSubstGroup *xmlSchemaSubstGroupPtr;
711struct _xmlSchemaSubstGroup {
712 xmlSchemaElementPtr head;
713 xmlSchemaItemListPtr members;
714};
715
Daniel Veillard4255d502002-04-16 15:50:10 +0000716/************************************************************************
717 * *
718 * Some predeclarations *
719 * *
720 ************************************************************************/
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000721
Daniel Veillardbd2904b2003-11-25 15:38:59 +0000722static int xmlSchemaParseInclude(xmlSchemaParserCtxtPtr ctxt,
723 xmlSchemaPtr schema,
724 xmlNodePtr node);
Daniel Veillard01fa6152004-06-29 17:04:39 +0000725static void
726xmlSchemaTypeFixup(xmlSchemaTypePtr typeDecl,
727 xmlSchemaParserCtxtPtr ctxt, const xmlChar * name);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +0000728static const xmlChar *
Daniel Veillard01fa6152004-06-29 17:04:39 +0000729xmlSchemaFacetTypeToString(xmlSchemaTypeType type);
730static int
William M. Brack2f2a6632004-08-20 23:09:47 +0000731xmlSchemaParseImport(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
732 xmlNodePtr node);
Kasimier T. Buchcikb8841242004-11-15 12:11:56 +0000733static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000734xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
735 xmlSchemaParserCtxtPtr ctxt);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +0000736static void
737xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000738static xmlSchemaWhitespaceValueType
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +0000739xmlSchemaGetWhiteSpaceFacetValue(xmlSchemaTypePtr type);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000740static xmlSchemaTreeItemPtr
741xmlSchemaParseModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
742 xmlNodePtr node, xmlSchemaTypeType type,
743 int withParticle);
744static const xmlChar *
745xmlSchemaCompTypeToString(xmlSchemaTypeType type);
746static xmlSchemaTypeLinkPtr
747xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000748static void
749xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
750 const char *funcName,
751 const char *message);
752static int
753xmlSchemaCheckCOSSTDerivedOK(xmlSchemaTypePtr type,
754 xmlSchemaTypePtr baseType,
755 int subset);
756static void
757xmlSchemaCheckElementDeclComponent(xmlSchemaElementPtr elemDecl,
758 xmlSchemaParserCtxtPtr ctxt,
759 const xmlChar * name ATTRIBUTE_UNUSED);
William M. Brack87640d52004-04-17 14:58:15 +0000760
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000761/************************************************************************
762 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000763 * Helper functions *
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000764 * *
765 ************************************************************************/
766
767/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000768 * xmlSchemaCompTypeToString:
769 * @type: the type of the schema item
770 *
771 * Returns the component name of a schema item.
772 */
773static const xmlChar *
774xmlSchemaCompTypeToString(xmlSchemaTypeType type)
775{
776 switch (type) {
777 case XML_SCHEMA_TYPE_SIMPLE:
778 return(BAD_CAST "simple type definition");
779 case XML_SCHEMA_TYPE_COMPLEX:
780 return(BAD_CAST "complex type definition");
781 case XML_SCHEMA_TYPE_ELEMENT:
782 return(BAD_CAST "element declaration");
783 case XML_SCHEMA_TYPE_ATTRIBUTE:
784 return(BAD_CAST "attribute declaration");
785 case XML_SCHEMA_TYPE_GROUP:
786 return(BAD_CAST "model group definition");
787 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
788 return(BAD_CAST "attribute group definition");
789 case XML_SCHEMA_TYPE_NOTATION:
790 return(BAD_CAST "notation declaration");
791 case XML_SCHEMA_TYPE_SEQUENCE:
792 return(BAD_CAST "model group (sequence)");
793 case XML_SCHEMA_TYPE_CHOICE:
794 return(BAD_CAST "model group (choice)");
795 case XML_SCHEMA_TYPE_ALL:
796 return(BAD_CAST "model group (all)");
797 case XML_SCHEMA_TYPE_PARTICLE:
798 return(BAD_CAST "particle");
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +0000799 case XML_SCHEMA_TYPE_IDC_UNIQUE:
800 return(BAD_CAST "IDC (unique)");
801 case XML_SCHEMA_TYPE_IDC_KEY:
802 return(BAD_CAST "IDC (key)");
803 case XML_SCHEMA_TYPE_IDC_KEYREF:
804 return(BAD_CAST "IDC (keyref)");
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +0000805 case XML_SCHEMA_TYPE_ANY:
806 return(BAD_CAST "wildcard (any)");
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +0000807 case XML_SCHEMA_EXTRA_QNAMEREF:
808 return(BAD_CAST "[helper component] QName reference");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000809 default:
810 return(BAD_CAST "Not a schema component");
811 }
812}
813
814/**
815 * xmlSchemaGetComponentNode:
816 * @item: a schema component
817 *
818 * Returns node associated with the schema component.
819 * NOTE that such a node need not be available; plus, a component's
820 * node need not to reflect the component directly, since there is no
821 * one-to-one relationship between the XML Schema representation and
822 * the component representation.
823 */
824static xmlNodePtr
825xmlSchemaGetComponentNode(xmlSchemaBasicItemPtr item)
826{
827 switch (item->type) {
828 case XML_SCHEMA_TYPE_ELEMENT:
829 return (((xmlSchemaElementPtr) item)->node);
830 case XML_SCHEMA_TYPE_ATTRIBUTE:
831 return (((xmlSchemaAttributePtr) item)->node);
832 case XML_SCHEMA_TYPE_COMPLEX:
833 case XML_SCHEMA_TYPE_SIMPLE:
834 return (((xmlSchemaTypePtr) item)->node);
835 case XML_SCHEMA_TYPE_ANY:
836 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
837 return (((xmlSchemaWildcardPtr) item)->node);
838 case XML_SCHEMA_TYPE_PARTICLE:
839 return (((xmlSchemaParticlePtr) item)->node);
840 case XML_SCHEMA_TYPE_SEQUENCE:
841 case XML_SCHEMA_TYPE_CHOICE:
842 case XML_SCHEMA_TYPE_ALL:
843 return (((xmlSchemaModelGroupPtr) item)->node);
844 case XML_SCHEMA_TYPE_GROUP:
845 return (((xmlSchemaModelGroupDefPtr) item)->node);
846 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
847 return (((xmlSchemaAttributeGroupPtr) item)->node);
848 case XML_SCHEMA_TYPE_IDC_UNIQUE:
849 case XML_SCHEMA_TYPE_IDC_KEY:
850 case XML_SCHEMA_TYPE_IDC_KEYREF:
851 return (((xmlSchemaIDCPtr) item)->node);
852 default:
853 return (NULL);
854 }
855}
856
857#if 0
858/**
859 * xmlSchemaGetNextComponent:
860 * @item: a schema component
861 *
862 * Returns the next sibling of the schema component.
863 */
864static xmlSchemaBasicItemPtr
865xmlSchemaGetNextComponent(xmlSchemaBasicItemPtr item)
866{
867 switch (item->type) {
868 case XML_SCHEMA_TYPE_ELEMENT:
869 return ((xmlSchemaBasicItemPtr) ((xmlSchemaElementPtr) item)->next);
870 case XML_SCHEMA_TYPE_ATTRIBUTE:
871 return ((xmlSchemaBasicItemPtr) ((xmlSchemaAttributePtr) item)->next);
872 case XML_SCHEMA_TYPE_COMPLEX:
873 case XML_SCHEMA_TYPE_SIMPLE:
874 return ((xmlSchemaBasicItemPtr) ((xmlSchemaTypePtr) item)->next);
875 case XML_SCHEMA_TYPE_ANY:
876 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
877 return (NULL);
878 case XML_SCHEMA_TYPE_PARTICLE:
879 return ((xmlSchemaBasicItemPtr) ((xmlSchemaParticlePtr) item)->next);
880 case XML_SCHEMA_TYPE_SEQUENCE:
881 case XML_SCHEMA_TYPE_CHOICE:
882 case XML_SCHEMA_TYPE_ALL:
883 return (NULL);
884 case XML_SCHEMA_TYPE_GROUP:
885 return (NULL);
886 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
887 return ((xmlSchemaBasicItemPtr) ((xmlSchemaAttributeGroupPtr) item)->next);
888 case XML_SCHEMA_TYPE_IDC_UNIQUE:
889 case XML_SCHEMA_TYPE_IDC_KEY:
890 case XML_SCHEMA_TYPE_IDC_KEYREF:
891 return ((xmlSchemaBasicItemPtr) ((xmlSchemaIDCPtr) item)->next);
892 default:
893 return (NULL);
894 }
895}
896#endif
897
898/**
899 * xmlSchemaGetAttrName:
900 * @attr: the attribute declaration/use
901 *
902 * Returns the name of the attribute; if the attribute
903 * is a reference, the name of the referenced global type will be returned.
904 */
905static const xmlChar *
906xmlSchemaGetAttrName(xmlSchemaAttributePtr attr)
907{
908 if (attr->ref != NULL)
909 return(attr->ref);
910 else
911 return(attr->name);
912}
913
914/**
915 * xmlSchemaGetAttrTargetNsURI:
916 * @type: the type (element or attribute)
917 *
918 * Returns the target namespace URI of the type; if the type is a reference,
919 * the target namespace of the referenced type will be returned.
920 */
921static const xmlChar *
922xmlSchemaGetAttrTargetNsURI(xmlSchemaAttributePtr attr)
923{
924 if (attr->ref != NULL)
925 return (attr->refNs);
926 else
927 return(attr->targetNamespace);
928}
929
930/**
931 * xmlSchemaFormatQName:
932 * @buf: the string buffer
933 * @namespaceName: the namespace name
934 * @localName: the local name
935 *
936 * Returns the given QName in the format "{namespaceName}localName" or
937 * just "localName" if @namespaceName is NULL.
938 *
939 * Returns the localName if @namespaceName is NULL, a formatted
940 * string otherwise.
941 */
942static const xmlChar*
943xmlSchemaFormatQName(xmlChar **buf,
944 const xmlChar *namespaceName,
945 const xmlChar *localName)
946{
947 FREE_AND_NULL(*buf)
948 if (namespaceName == NULL)
949 return(localName);
950
951 *buf = xmlStrdup(BAD_CAST "{");
952 *buf = xmlStrcat(*buf, namespaceName);
953 *buf = xmlStrcat(*buf, BAD_CAST "}");
954 *buf = xmlStrcat(*buf, localName);
955
956 return ((const xmlChar *) *buf);
957}
958
959static const xmlChar*
960xmlSchemaFormatQNameNs(xmlChar **buf, xmlNsPtr ns, const xmlChar *localName)
961{
962 if (ns != NULL)
963 return (xmlSchemaFormatQName(buf, ns->href, localName));
964 else
965 return (xmlSchemaFormatQName(buf, NULL, localName));
966}
967
968static const xmlChar *
969xmlSchemaGetComponentName(xmlSchemaBasicItemPtr item)
970{
971 switch (item->type) {
972 case XML_SCHEMA_TYPE_ELEMENT:
973 return (((xmlSchemaElementPtr) item)->name);
974 case XML_SCHEMA_TYPE_ATTRIBUTE:
975 return (((xmlSchemaAttributePtr) item)->name);
976 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
977 return (((xmlSchemaAttributeGroupPtr) item)->name);
978 case XML_SCHEMA_TYPE_BASIC:
979 case XML_SCHEMA_TYPE_SIMPLE:
980 case XML_SCHEMA_TYPE_COMPLEX:
981 return (((xmlSchemaTypePtr) item)->name);
982 case XML_SCHEMA_TYPE_GROUP:
983 return (((xmlSchemaModelGroupDefPtr) item)->name);
984 case XML_SCHEMA_TYPE_IDC_KEY:
985 case XML_SCHEMA_TYPE_IDC_UNIQUE:
986 case XML_SCHEMA_TYPE_IDC_KEYREF:
987 return (((xmlSchemaIDCPtr) item)->name);
988 default:
989 /*
990 * Other components cannot have names.
991 */
992 break;
993 }
994 return (NULL);
995}
996
997static const xmlChar *
998xmlSchemaGetComponentTargetNs(xmlSchemaBasicItemPtr item)
999{
1000 switch (item->type) {
1001 case XML_SCHEMA_TYPE_ELEMENT:
1002 return (((xmlSchemaElementPtr) item)->targetNamespace);
1003 case XML_SCHEMA_TYPE_ATTRIBUTE:
1004 return (((xmlSchemaAttributePtr) item)->targetNamespace);
1005 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1006 return (((xmlSchemaAttributeGroupPtr) item)->targetNamespace);
1007 case XML_SCHEMA_TYPE_BASIC:
1008 return (BAD_CAST "http://www.w3.org/2001/XMLSchema");
1009 case XML_SCHEMA_TYPE_SIMPLE:
1010 case XML_SCHEMA_TYPE_COMPLEX:
1011 return (((xmlSchemaTypePtr) item)->targetNamespace);
1012 case XML_SCHEMA_TYPE_GROUP:
1013 return (((xmlSchemaModelGroupDefPtr) item)->targetNamespace);
1014 case XML_SCHEMA_TYPE_IDC_KEY:
1015 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1016 case XML_SCHEMA_TYPE_IDC_KEYREF:
1017 return (((xmlSchemaIDCPtr) item)->targetNamespace);
1018 default:
1019 /*
1020 * Other components cannot have names.
1021 */
1022 break;
1023 }
1024 return (NULL);
1025}
1026
1027static const xmlChar*
1028xmlSchemaGetComponentQName(xmlChar **buf,
1029 void *item)
1030{
1031 return (xmlSchemaFormatQName(buf,
1032 xmlSchemaGetComponentTargetNs((xmlSchemaBasicItemPtr) item),
1033 xmlSchemaGetComponentName((xmlSchemaBasicItemPtr) item)));
1034}
1035
1036/**
1037 * xmlSchemaWildcardPCToString:
1038 * @pc: the type of processContents
1039 *
1040 * Returns a string representation of the type of
1041 * processContents.
1042 */
1043static const xmlChar *
1044xmlSchemaWildcardPCToString(int pc)
1045{
1046 switch (pc) {
1047 case XML_SCHEMAS_ANY_SKIP:
1048 return (BAD_CAST "skip");
1049 case XML_SCHEMAS_ANY_LAX:
1050 return (BAD_CAST "lax");
1051 case XML_SCHEMAS_ANY_STRICT:
1052 return (BAD_CAST "strict");
1053 default:
1054 return (BAD_CAST "invalid process contents");
1055 }
1056}
1057
1058/**
1059 * xmlSchemaGetCanonValueWhtspExt:
1060 * @val: the precomputed value
1061 * @retValue: the returned value
1062 * @ws: the whitespace type of the value
1063 *
1064 * Get a the cononical representation of the value.
1065 * The caller has to free the returned retValue.
1066 *
1067 * Returns 0 if the value could be built and -1 in case of
1068 * API errors or if the value type is not supported yet.
1069 */
1070static int
1071xmlSchemaGetCanonValueWhtspExt(xmlSchemaValPtr val,
1072 xmlSchemaWhitespaceValueType ws,
1073 xmlChar **retValue)
1074{
1075 int list;
1076 xmlSchemaValType valType;
1077 const xmlChar *value, *value2 = NULL;
1078
1079
1080 if ((retValue == NULL) || (val == NULL))
1081 return (-1);
1082 list = xmlSchemaValueGetNext(val) ? 1 : 0;
1083 *retValue = NULL;
1084 do {
1085 value = NULL;
1086 valType = xmlSchemaGetValType(val);
1087 switch (valType) {
1088 case XML_SCHEMAS_STRING:
1089 case XML_SCHEMAS_NORMSTRING:
1090 case XML_SCHEMAS_ANYSIMPLETYPE:
1091 value = xmlSchemaValueGetAsString(val);
1092 if (value != NULL) {
1093 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
1094 value2 = xmlSchemaCollapseString(value);
1095 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
1096 value2 = xmlSchemaWhiteSpaceReplace(value);
1097 if (value2 != NULL)
1098 value = value2;
1099 }
1100 break;
1101 default:
1102 if (xmlSchemaGetCanonValue(val, &value2) == -1) {
1103 if (value2 != NULL)
1104 xmlFree((xmlChar *) value2);
1105 goto internal_error;
1106 }
1107 value = value2;
1108 }
1109 if (*retValue == NULL)
1110 if (value == NULL) {
1111 if (! list)
1112 *retValue = xmlStrdup(BAD_CAST "");
1113 } else
1114 *retValue = xmlStrdup(value);
1115 else if (value != NULL) {
1116 /* List. */
1117 *retValue = xmlStrcat((xmlChar *) *retValue, BAD_CAST " ");
1118 *retValue = xmlStrcat((xmlChar *) *retValue, value);
1119 }
1120 FREE_AND_NULL(value2)
1121 val = xmlSchemaValueGetNext(val);
1122 } while (val != NULL);
1123
1124 return (0);
1125internal_error:
1126 if (*retValue != NULL)
1127 xmlFree((xmlChar *) (*retValue));
1128 if (value2 != NULL)
1129 xmlFree((xmlChar *) value2);
1130 return (-1);
1131}
1132
1133/**
1134 * xmlSchemaFormatItemForReport:
1135 * @buf: the string buffer
1136 * @itemDes: the designation of the item
1137 * @itemName: the name of the item
1138 * @item: the item as an object
1139 * @itemNode: the node of the item
1140 * @local: the local name
1141 * @parsing: if the function is used during the parse
1142 *
1143 * Returns a representation of the given item used
1144 * for error reports.
1145 *
1146 * The following order is used to build the resulting
1147 * designation if the arguments are not NULL:
1148 * 1a. If itemDes not NULL -> itemDes
1149 * 1b. If (itemDes not NULL) and (itemName not NULL)
1150 * -> itemDes + itemName
1151 * 2. If the preceding was NULL and (item not NULL) -> item
1152 * 3. If the preceding was NULL and (itemNode not NULL) -> itemNode
1153 *
1154 * If the itemNode is an attribute node, the name of the attribute
1155 * will be appended to the result.
1156 *
1157 * Returns the formatted string and sets @buf to the resulting value.
1158 */
1159static xmlChar*
1160xmlSchemaFormatItemForReport(xmlChar **buf,
1161 const xmlChar *itemDes,
1162 xmlSchemaTypePtr item,
1163 xmlNodePtr itemNode)
1164{
1165 xmlChar *str = NULL;
1166 int named = 1;
1167
1168 if (*buf != NULL) {
1169 xmlFree(*buf);
1170 *buf = NULL;
1171 }
1172
1173 if (itemDes != NULL) {
1174 *buf = xmlStrdup(itemDes);
1175 } else if (item != NULL) {
1176 switch (item->type) {
1177 case XML_SCHEMA_TYPE_BASIC:
1178 if (VARIETY_ATOMIC(item))
1179 *buf = xmlStrdup(BAD_CAST "atomic type 'xs:");
1180 else if (VARIETY_LIST(item))
1181 *buf = xmlStrdup(BAD_CAST "list type 'xs:");
1182 else if (VARIETY_UNION(item))
1183 *buf = xmlStrdup(BAD_CAST "union type 'xs:");
1184 else
1185 *buf = xmlStrdup(BAD_CAST "simple type 'xs:");
1186 *buf = xmlStrcat(*buf, item->name);
1187 *buf = xmlStrcat(*buf, BAD_CAST "'");
1188 break;
1189 case XML_SCHEMA_TYPE_SIMPLE:
1190 if (item->flags & XML_SCHEMAS_TYPE_GLOBAL) {
1191 *buf = xmlStrdup(BAD_CAST"");
1192 } else {
1193 *buf = xmlStrdup(BAD_CAST "local ");
1194 }
1195 if (VARIETY_ATOMIC(item))
1196 *buf = xmlStrcat(*buf, BAD_CAST "atomic type");
1197 else if (VARIETY_LIST(item))
1198 *buf = xmlStrcat(*buf, BAD_CAST "list type");
1199 else if (VARIETY_UNION(item))
1200 *buf = xmlStrcat(*buf, BAD_CAST "union type");
1201 else
1202 *buf = xmlStrcat(*buf, BAD_CAST "simple type");
1203 if (item->flags & XML_SCHEMAS_TYPE_GLOBAL) {
1204 *buf = xmlStrcat(*buf, BAD_CAST " '");
1205 *buf = xmlStrcat(*buf, item->name);
1206 *buf = xmlStrcat(*buf, BAD_CAST "'");
1207 }
1208 break;
1209 case XML_SCHEMA_TYPE_COMPLEX:
1210 if (item->flags & XML_SCHEMAS_TYPE_GLOBAL)
1211 *buf = xmlStrdup(BAD_CAST "");
1212 else
1213 *buf = xmlStrdup(BAD_CAST "local ");
1214 *buf = xmlStrcat(*buf, BAD_CAST "complex type");
1215 if (item->flags & XML_SCHEMAS_TYPE_GLOBAL) {
1216 *buf = xmlStrcat(*buf, BAD_CAST " '");
1217 *buf = xmlStrcat(*buf, item->name);
1218 *buf = xmlStrcat(*buf, BAD_CAST "'");
1219 }
1220 break;
1221 case XML_SCHEMA_TYPE_ATTRIBUTE: {
1222 xmlSchemaAttributePtr attr;
1223
1224 attr = (xmlSchemaAttributePtr) item;
1225 if ((attr->flags & XML_SCHEMAS_ATTR_GLOBAL) ||
1226 (attr->ref == NULL)) {
1227 *buf = xmlStrdup(xmlSchemaElemDesAttrDecl);
1228 *buf = xmlStrcat(*buf, BAD_CAST " '");
1229 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1230 attr->targetNamespace, attr->name));
1231 FREE_AND_NULL(str)
1232 *buf = xmlStrcat(*buf, BAD_CAST "'");
1233 } else {
1234 *buf = xmlStrdup(xmlSchemaElemDesAttrRef);
1235 *buf = xmlStrcat(*buf, BAD_CAST " '");
1236 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1237 attr->refNs, attr->ref));
1238 FREE_AND_NULL(str)
1239 *buf = xmlStrcat(*buf, BAD_CAST "'");
1240 }
1241 }
1242 break;
1243 case XML_SCHEMA_TYPE_ELEMENT: {
1244 xmlSchemaElementPtr elem;
1245
1246 elem = (xmlSchemaElementPtr) item;
1247 if ((elem->flags & XML_SCHEMAS_ELEM_GLOBAL) ||
1248 (elem->ref == NULL)) {
1249 *buf = xmlStrdup(xmlSchemaElemDesElemDecl);
1250 *buf = xmlStrcat(*buf, BAD_CAST " '");
1251 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1252 elem->targetNamespace, elem->name));
1253 *buf = xmlStrcat(*buf, BAD_CAST "'");
1254 }
1255 }
1256 break;
1257 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1258 case XML_SCHEMA_TYPE_IDC_KEY:
1259 case XML_SCHEMA_TYPE_IDC_KEYREF:
1260 if (item->type == XML_SCHEMA_TYPE_IDC_UNIQUE)
1261 *buf = xmlStrdup(BAD_CAST "unique '");
1262 else if (item->type == XML_SCHEMA_TYPE_IDC_KEY)
1263 *buf = xmlStrdup(BAD_CAST "key '");
1264 else
1265 *buf = xmlStrdup(BAD_CAST "keyRef '");
1266 *buf = xmlStrcat(*buf, ((xmlSchemaIDCPtr) item)->name);
1267 *buf = xmlStrcat(*buf, BAD_CAST "'");
1268 break;
1269 case XML_SCHEMA_TYPE_ANY:
1270 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1271 *buf = xmlStrdup(xmlSchemaWildcardPCToString(
1272 ((xmlSchemaWildcardPtr) item)->processContents));
1273 *buf = xmlStrcat(*buf, BAD_CAST " wildcard");
1274 break;
1275 case XML_SCHEMA_FACET_MININCLUSIVE:
1276 case XML_SCHEMA_FACET_MINEXCLUSIVE:
1277 case XML_SCHEMA_FACET_MAXINCLUSIVE:
1278 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
1279 case XML_SCHEMA_FACET_TOTALDIGITS:
1280 case XML_SCHEMA_FACET_FRACTIONDIGITS:
1281 case XML_SCHEMA_FACET_PATTERN:
1282 case XML_SCHEMA_FACET_ENUMERATION:
1283 case XML_SCHEMA_FACET_WHITESPACE:
1284 case XML_SCHEMA_FACET_LENGTH:
1285 case XML_SCHEMA_FACET_MAXLENGTH:
1286 case XML_SCHEMA_FACET_MINLENGTH:
1287 *buf = xmlStrdup(BAD_CAST "facet '");
1288 *buf = xmlStrcat(*buf, xmlSchemaFacetTypeToString(item->type));
1289 *buf = xmlStrcat(*buf, BAD_CAST "'");
1290 break;
1291 case XML_SCHEMA_TYPE_NOTATION:
1292 *buf = xmlStrdup(BAD_CAST "notation");
1293 break;
1294 case XML_SCHEMA_TYPE_GROUP: {
1295 *buf = xmlStrdup(xmlSchemaElemModelGrDef);
1296 *buf = xmlStrcat(*buf, BAD_CAST " '");
1297 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1298 ((xmlSchemaModelGroupDefPtr) item)->targetNamespace,
1299 ((xmlSchemaModelGroupDefPtr) item)->name));
1300 *buf = xmlStrcat(*buf, BAD_CAST "'");
1301 FREE_AND_NULL(str)
1302 }
1303 break;
1304 case XML_SCHEMA_TYPE_SEQUENCE:
1305 case XML_SCHEMA_TYPE_CHOICE:
1306 case XML_SCHEMA_TYPE_ALL:
1307 case XML_SCHEMA_TYPE_PARTICLE:
1308 *buf = xmlStrdup(xmlSchemaCompTypeToString(item->type));
1309 break;
1310 default:
1311 named = 0;
1312 }
1313 } else
1314 named = 0;
1315
1316 if ((named == 0) && (itemNode != NULL)) {
1317 xmlNodePtr elem;
1318
1319 if (itemNode->type == XML_ATTRIBUTE_NODE)
1320 elem = itemNode->parent;
1321 else
1322 elem = itemNode;
1323 *buf = xmlStrdup(BAD_CAST "Element '");
1324 if (elem->ns != NULL) {
1325 *buf = xmlStrcat(*buf,
1326 xmlSchemaFormatQName(&str, elem->ns->href, elem->name));
1327 FREE_AND_NULL(str)
1328 } else
1329 *buf = xmlStrcat(*buf, elem->name);
1330 *buf = xmlStrcat(*buf, BAD_CAST "'");
1331
1332 }
1333 if ((itemNode != NULL) && (itemNode->type == XML_ATTRIBUTE_NODE)) {
1334 *buf = xmlStrcat(*buf, BAD_CAST ", attribute '");
1335 if (itemNode->ns != NULL) {
1336 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1337 itemNode->ns->href, itemNode->name));
1338 FREE_AND_NULL(str)
1339 } else
1340 *buf = xmlStrcat(*buf, itemNode->name);
1341 *buf = xmlStrcat(*buf, BAD_CAST "'");
1342 }
1343 FREE_AND_NULL(str)
1344
1345 return (*buf);
1346}
1347
1348/**
1349 * xmlSchemaFormatFacetEnumSet:
1350 * @buf: the string buffer
1351 * @type: the type holding the enumeration facets
1352 *
1353 * Builds a string consisting of all enumeration elements.
1354 *
1355 * Returns a string of all enumeration elements.
1356 */
1357static const xmlChar *
1358xmlSchemaFormatFacetEnumSet(xmlSchemaAbstractCtxtPtr actxt,
1359 xmlChar **buf, xmlSchemaTypePtr type)
1360{
1361 xmlSchemaFacetPtr facet;
1362 xmlSchemaWhitespaceValueType ws;
1363 xmlChar *value = NULL;
1364 int res;
1365
1366 if (*buf != NULL)
1367 xmlFree(*buf);
1368 *buf = NULL;
1369
1370 do {
1371 /*
1372 * Use the whitespace type of the base type.
1373 */
1374 ws = xmlSchemaGetWhiteSpaceFacetValue(type->baseType);
1375 for (facet = type->facets; facet != NULL; facet = facet->next) {
1376 if (facet->type != XML_SCHEMA_FACET_ENUMERATION)
1377 continue;
1378 res = xmlSchemaGetCanonValueWhtspExt(facet->val,
1379 ws, &value);
1380 if (res == -1) {
1381 xmlSchemaInternalErr(actxt,
1382 "xmlSchemaFormatFacetEnumSet",
1383 "compute the canonical lexical representation");
1384 if (*buf != NULL)
1385 xmlFree(*buf);
1386 *buf = NULL;
1387 return (NULL);
1388 }
1389 if (*buf == NULL)
1390 *buf = xmlStrdup(BAD_CAST "'");
1391 else
1392 *buf = xmlStrcat(*buf, BAD_CAST ", '");
1393 *buf = xmlStrcat(*buf, BAD_CAST value);
1394 *buf = xmlStrcat(*buf, BAD_CAST "'");
1395 if (value != NULL) {
1396 xmlFree((xmlChar *)value);
1397 value = NULL;
1398 }
1399 }
1400 type = type->baseType;
1401 } while ((type != NULL) && (type->type != XML_SCHEMA_TYPE_BASIC));
1402
1403 return ((const xmlChar *) *buf);
1404}
1405
1406/************************************************************************
1407 * *
1408 * Error functions *
1409 * *
1410 ************************************************************************/
1411
1412#if 0
1413static void
1414xmlSchemaErrMemory(const char *msg)
1415{
1416 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, NULL, NULL,
1417 msg);
1418}
1419#endif
1420
1421/**
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001422 * xmlSchemaPErrMemory:
1423 * @node: a context node
1424 * @extra: extra informations
1425 *
1426 * Handle an out of memory condition
1427 */
1428static void
1429xmlSchemaPErrMemory(xmlSchemaParserCtxtPtr ctxt,
1430 const char *extra, xmlNodePtr node)
1431{
1432 if (ctxt != NULL)
1433 ctxt->nberrors++;
1434 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, node, NULL,
1435 extra);
1436}
1437
1438/**
1439 * xmlSchemaPErr:
1440 * @ctxt: the parsing context
1441 * @node: the context node
1442 * @error: the error code
1443 * @msg: the error message
1444 * @str1: extra data
1445 * @str2: extra data
1446 *
1447 * Handle a parser error
1448 */
1449static void
1450xmlSchemaPErr(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
1451 const char *msg, const xmlChar * str1, const xmlChar * str2)
1452{
1453 xmlGenericErrorFunc channel = NULL;
Daniel Veillard659e71e2003-10-10 14:10:40 +00001454 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001455 void *data = NULL;
1456
1457 if (ctxt != NULL) {
1458 ctxt->nberrors++;
1459 channel = ctxt->error;
1460 data = ctxt->userData;
Daniel Veillard659e71e2003-10-10 14:10:40 +00001461 schannel = ctxt->serror;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001462 }
Daniel Veillard659e71e2003-10-10 14:10:40 +00001463 __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001464 error, XML_ERR_ERROR, NULL, 0,
1465 (const char *) str1, (const char *) str2, NULL, 0, 0,
1466 msg, str1, str2);
1467}
1468
1469/**
1470 * xmlSchemaPErr2:
1471 * @ctxt: the parsing context
1472 * @node: the context node
1473 * @node: the current child
1474 * @error: the error code
1475 * @msg: the error message
1476 * @str1: extra data
1477 * @str2: extra data
1478 *
1479 * Handle a parser error
1480 */
1481static void
1482xmlSchemaPErr2(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
1483 xmlNodePtr child, int error,
1484 const char *msg, const xmlChar * str1, const xmlChar * str2)
1485{
1486 if (child != NULL)
1487 xmlSchemaPErr(ctxt, child, error, msg, str1, str2);
1488 else
1489 xmlSchemaPErr(ctxt, node, error, msg, str1, str2);
1490}
1491
Daniel Veillard01fa6152004-06-29 17:04:39 +00001492
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001493/**
Daniel Veillard3646d642004-06-02 19:19:14 +00001494 * xmlSchemaPErrExt:
1495 * @ctxt: the parsing context
1496 * @node: the context node
1497 * @error: the error code
1498 * @strData1: extra data
1499 * @strData2: extra data
1500 * @strData3: extra data
1501 * @msg: the message
1502 * @str1: extra parameter for the message display
1503 * @str2: extra parameter for the message display
1504 * @str3: extra parameter for the message display
1505 * @str4: extra parameter for the message display
1506 * @str5: extra parameter for the message display
1507 *
1508 * Handle a parser error
1509 */
1510static void
1511xmlSchemaPErrExt(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
1512 const xmlChar * strData1, const xmlChar * strData2,
1513 const xmlChar * strData3, const char *msg, const xmlChar * str1,
1514 const xmlChar * str2, const xmlChar * str3, const xmlChar * str4,
1515 const xmlChar * str5)
1516{
1517
1518 xmlGenericErrorFunc channel = NULL;
1519 xmlStructuredErrorFunc schannel = NULL;
1520 void *data = NULL;
1521
1522 if (ctxt != NULL) {
1523 ctxt->nberrors++;
1524 channel = ctxt->error;
1525 data = ctxt->userData;
1526 schannel = ctxt->serror;
1527 }
1528 __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
1529 error, XML_ERR_ERROR, NULL, 0,
1530 (const char *) strData1, (const char *) strData2,
William M. Brack803812b2004-06-03 02:11:24 +00001531 (const char *) strData3, 0, 0, msg, str1, str2,
1532 str3, str4, str5);
Daniel Veillard3646d642004-06-02 19:19:14 +00001533}
Daniel Veillard01fa6152004-06-29 17:04:39 +00001534
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001535/************************************************************************
1536 * *
1537 * Allround error functions *
1538 * *
1539 ************************************************************************/
Daniel Veillard3646d642004-06-02 19:19:14 +00001540
1541/**
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001542 * xmlSchemaVTypeErrMemory:
1543 * @node: a context node
1544 * @extra: extra informations
1545 *
1546 * Handle an out of memory condition
1547 */
1548static void
1549xmlSchemaVErrMemory(xmlSchemaValidCtxtPtr ctxt,
1550 const char *extra, xmlNodePtr node)
1551{
1552 if (ctxt != NULL) {
1553 ctxt->nberrors++;
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00001554 ctxt->err = XML_SCHEMAV_INTERNAL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001555 }
1556 __xmlSimpleError(XML_FROM_SCHEMASV, XML_ERR_NO_MEMORY, node, NULL,
1557 extra);
1558}
1559
1560/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001561 * xmlSchemaErr3:
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001562 * @ctxt: the validation context
1563 * @node: the context node
1564 * @error: the error code
1565 * @msg: the error message
1566 * @str1: extra data
1567 * @str2: extra data
1568 * @str3: extra data
1569 *
1570 * Handle a validation error
1571 */
1572static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001573xmlSchemaErr3(xmlSchemaAbstractCtxtPtr ctxt,
1574 int error, xmlNodePtr node, const char *msg,
1575 const xmlChar *str1, const xmlChar *str2, const xmlChar *str3)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001576{
Daniel Veillard659e71e2003-10-10 14:10:40 +00001577 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001578 xmlGenericErrorFunc channel = NULL;
1579 void *data = NULL;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00001580
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001581 if (ctxt != NULL) {
1582 if (ctxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
1583 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctxt;
Daniel Veillard971771e2005-07-09 17:32:57 +00001584 int line = 0;
1585 const char *file = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001586
1587 vctxt->nberrors++;
1588 vctxt->err = error;
1589 channel = vctxt->error;
1590 schannel = vctxt->serror;
1591 data = vctxt->userData;
1592 if ((node == NULL) && (vctxt->depth >= 0) &&
Daniel Veillard971771e2005-07-09 17:32:57 +00001593 (vctxt->inode != NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001594 node = vctxt->inode->node;
Daniel Veillard971771e2005-07-09 17:32:57 +00001595 }
1596 if ((node == NULL) && (vctxt->parserCtxt != NULL) &&
1597 (vctxt->parserCtxt->input != NULL)) {
1598 file = vctxt->parserCtxt->input->filename;
1599 line = vctxt->parserCtxt->input->line;
1600 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001601 __xmlRaiseError(schannel, channel, data, ctxt,
1602 node, XML_FROM_SCHEMASV,
Daniel Veillard971771e2005-07-09 17:32:57 +00001603 error, XML_ERR_ERROR, file, line,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001604 (const char *) str1, (const char *) str2,
1605 (const char *) str3, 0, 0, msg, str1, str2, str3);
1606
1607 } else if (ctxt->type == XML_SCHEMA_CTXT_PARSER) {
1608 xmlSchemaParserCtxtPtr pctxt = (xmlSchemaParserCtxtPtr) ctxt;
1609
1610 pctxt->nberrors++;
1611 pctxt->err = error;
1612 channel = pctxt->error;
1613 schannel = pctxt->serror;
1614 data = pctxt->userData;
1615 __xmlRaiseError(schannel, channel, data, ctxt,
1616 node, XML_FROM_SCHEMASP,
1617 error, XML_ERR_ERROR, NULL, 0,
1618 (const char *) str1, (const char *) str2,
1619 (const char *) str3, 0, 0, msg, str1, str2, str3);
1620 } else {
1621 TODO
1622 }
1623 }
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00001624}
1625
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001626static void
1627xmlSchemaErr(xmlSchemaAbstractCtxtPtr actxt,
1628 int error, xmlNodePtr node, const char *msg,
1629 const xmlChar *str1, const xmlChar *str2)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001630{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001631 xmlSchemaErr3(actxt, error, node, msg, str1, str2, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001632}
1633
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001634static xmlChar *
1635xmlSchemaFormatNodeForError(xmlChar ** msg,
1636 xmlSchemaAbstractCtxtPtr actxt,
1637 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00001638{
1639 xmlChar *str = NULL;
1640
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001641 if (node != NULL) {
1642 /*
1643 * Work on tree nodes.
1644 */
1645 if (node->type == XML_ATTRIBUTE_NODE) {
1646 xmlNodePtr elem = node->parent;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00001647
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001648 *msg = xmlStrdup(BAD_CAST "Element '");
1649 if (elem->ns != NULL)
1650 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
1651 elem->ns->href, elem->name));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00001652 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001653 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
1654 NULL, elem->name));
1655 FREE_AND_NULL(str);
1656 *msg = xmlStrcat(*msg, BAD_CAST "', ");
1657 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
1658 } else {
1659 *msg = xmlStrdup(BAD_CAST "Element '");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00001660 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001661 if (node->ns != NULL)
1662 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
1663 node->ns->href, node->name));
Daniel Veillardc0826a72004-08-10 14:17:33 +00001664 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001665 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
1666 NULL, node->name));
1667 FREE_AND_NULL(str);
1668 *msg = xmlStrcat(*msg, BAD_CAST "': ");
1669 } else if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
1670 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) actxt;
1671 /*
1672 * Work on node infos.
1673 */
1674 if (vctxt->inode->nodeType == XML_ATTRIBUTE_NODE) {
1675 xmlSchemaNodeInfoPtr ielem =
1676 vctxt->elemInfos[vctxt->depth];
1677
1678 *msg = xmlStrdup(BAD_CAST "Element '");
1679 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
1680 ielem->nsName, ielem->localName));
1681 FREE_AND_NULL(str);
1682 *msg = xmlStrcat(*msg, BAD_CAST "', ");
1683 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
1684 } else {
1685 *msg = xmlStrdup(BAD_CAST "Element '");
1686 }
1687 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
1688 vctxt->inode->nsName, vctxt->inode->localName));
1689 FREE_AND_NULL(str);
1690 *msg = xmlStrcat(*msg, BAD_CAST "': ");
1691 } else {
1692 TODO
1693 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00001694 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001695 /*
1696 * VAL TODO: The output of the given schema component is currently
1697 * disabled.
1698 */
1699#if 0
1700 if ((type != NULL) && (xmlSchemaIsGlobalItem(type))) {
1701 *msg = xmlStrcat(*msg, BAD_CAST " [");
1702 *msg = xmlStrcat(*msg, xmlSchemaFormatItemForReport(&str,
1703 NULL, type, NULL, 0));
1704 FREE_AND_NULL(str)
1705 *msg = xmlStrcat(*msg, BAD_CAST "]");
Daniel Veillardc0826a72004-08-10 14:17:33 +00001706 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001707#endif
1708 return (*msg);
Daniel Veillardc0826a72004-08-10 14:17:33 +00001709}
1710
Daniel Veillardc0826a72004-08-10 14:17:33 +00001711static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001712xmlSchemaInternalErr2(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001713 const char *funcName,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001714 const char *message,
1715 const xmlChar *str1,
1716 const xmlChar *str2)
Daniel Veillardc0826a72004-08-10 14:17:33 +00001717{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001718 xmlChar *msg = NULL;
1719
1720 msg = xmlStrdup(BAD_CAST "Internal error: ");
1721 msg = xmlStrcat(msg, BAD_CAST funcName);
1722 msg = xmlStrcat(msg, BAD_CAST ", ");
1723 msg = xmlStrcat(msg, BAD_CAST message);
1724 msg = xmlStrcat(msg, BAD_CAST ".\n");
1725
1726 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR)
1727 xmlSchemaErr(actxt, XML_SCHEMAV_INTERNAL, NULL,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001728 (const char *) msg, str1, str2);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001729
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001730 else if (actxt->type == XML_SCHEMA_CTXT_PARSER)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001731 xmlSchemaErr(actxt, XML_SCHEMAP_INTERNAL, NULL,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001732 (const char *) msg, str1, str2);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001733
1734 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00001735}
1736
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001737static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001738xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
1739 const char *funcName,
1740 const char *message)
1741{
1742 xmlSchemaInternalErr2(actxt, funcName, message, NULL, NULL);
1743}
1744
1745static void
1746xmlSchemaPInternalErr(xmlSchemaParserCtxtPtr pctxt,
1747 const char *funcName,
1748 const char *message,
1749 const xmlChar *str1,
1750 const xmlChar *str2)
1751{
1752 xmlSchemaInternalErr2((xmlSchemaAbstractCtxtPtr) pctxt, funcName, message,
1753 str1, str2);
1754}
1755
1756static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001757xmlSchemaCustomErr(xmlSchemaAbstractCtxtPtr actxt,
1758 xmlParserErrors error,
1759 xmlNodePtr node,
1760 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
1761 const char *message,
1762 const xmlChar *str1,
1763 const xmlChar *str2)
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00001764{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001765 xmlChar *msg = NULL;
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00001766
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001767 xmlSchemaFormatNodeForError(&msg, actxt, node);
1768 msg = xmlStrcat(msg, (const xmlChar *) message);
1769 msg = xmlStrcat(msg, BAD_CAST ".\n");
1770 xmlSchemaErr(actxt, error, node,
1771 (const char *) msg, str1, str2);
1772 FREE_AND_NULL(msg)
1773}
1774
1775static int
1776xmlSchemaEvalErrorNodeType(xmlSchemaAbstractCtxtPtr actxt,
1777 xmlNodePtr node)
1778{
1779 if (node != NULL)
1780 return (node->type);
1781 if ((actxt->type == XML_SCHEMA_CTXT_VALIDATOR) &&
1782 (((xmlSchemaValidCtxtPtr) actxt)->inode != NULL))
1783 return ( ((xmlSchemaValidCtxtPtr) actxt)->inode->nodeType);
1784 return (-1);
1785}
1786
1787static int
1788xmlSchemaIsGlobalItem(xmlSchemaTypePtr item)
1789{
1790 switch (item->type) {
1791 case XML_SCHEMA_TYPE_COMPLEX:
1792 case XML_SCHEMA_TYPE_SIMPLE:
1793 if (item->flags & XML_SCHEMAS_TYPE_GLOBAL)
1794 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00001795 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001796 case XML_SCHEMA_TYPE_GROUP:
1797 return (1);
1798 case XML_SCHEMA_TYPE_ELEMENT:
1799 if ( ((xmlSchemaElementPtr) item)->flags &
1800 XML_SCHEMAS_ELEM_GLOBAL)
1801 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00001802 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001803 case XML_SCHEMA_TYPE_ATTRIBUTE:
1804 if ( ((xmlSchemaAttributePtr) item)->flags &
1805 XML_SCHEMAS_ATTR_GLOBAL)
1806 return(1);
1807 break;
1808 /* Note that attribute groups are always global. */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00001809 default:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001810 return(1);
1811 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00001812 return (0);
1813}
1814
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001815static void
1816xmlSchemaSimpleTypeErr(xmlSchemaAbstractCtxtPtr actxt,
1817 xmlParserErrors error,
1818 xmlNodePtr node,
1819 const xmlChar *value,
1820 xmlSchemaTypePtr type,
1821 int displayValue)
1822{
1823 xmlChar *msg = NULL;
1824
1825 xmlSchemaFormatNodeForError(&msg, actxt, node);
1826
1827 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
1828 XML_ATTRIBUTE_NODE))
1829 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
1830 else
1831 msg = xmlStrcat(msg, BAD_CAST "The character content is not a valid "
1832 "value of ");
1833
1834 if (! xmlSchemaIsGlobalItem(type))
1835 msg = xmlStrcat(msg, BAD_CAST "the local ");
1836 else
1837 msg = xmlStrcat(msg, BAD_CAST "the ");
1838
1839 if (VARIETY_ATOMIC(type))
1840 msg = xmlStrcat(msg, BAD_CAST "atomic type");
1841 else if (VARIETY_LIST(type))
1842 msg = xmlStrcat(msg, BAD_CAST "list type");
1843 else if (VARIETY_UNION(type))
1844 msg = xmlStrcat(msg, BAD_CAST "union type");
1845
1846 if (xmlSchemaIsGlobalItem(type)) {
1847 xmlChar *str = NULL;
1848 msg = xmlStrcat(msg, BAD_CAST " '");
1849 if (type->builtInType != 0) {
1850 msg = xmlStrcat(msg, BAD_CAST "xs:");
1851 msg = xmlStrcat(msg, type->name);
1852 } else
1853 msg = xmlStrcat(msg,
1854 xmlSchemaFormatQName(&str,
1855 type->targetNamespace, type->name));
1856 msg = xmlStrcat(msg, BAD_CAST "'");
1857 FREE_AND_NULL(str);
1858 }
1859 msg = xmlStrcat(msg, BAD_CAST ".\n");
1860 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
1861 XML_ATTRIBUTE_NODE))
1862 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
1863 else
1864 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
1865 FREE_AND_NULL(msg)
1866}
1867
Daniel Veillardc0826a72004-08-10 14:17:33 +00001868static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001869xmlSchemaFormatErrorNodeQName(xmlChar ** str,
1870 xmlSchemaNodeInfoPtr ni,
1871 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00001872{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001873 if (node != NULL) {
1874 if (node->ns != NULL)
1875 return (xmlSchemaFormatQName(str, node->ns->href, node->name));
1876 else
1877 return (xmlSchemaFormatQName(str, NULL, node->name));
1878 } else if (ni != NULL)
1879 return (xmlSchemaFormatQName(str, ni->nsName, ni->localName));
1880 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00001881}
1882
Daniel Veillardc0826a72004-08-10 14:17:33 +00001883static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001884xmlSchemaIllegalAttrErr(xmlSchemaAbstractCtxtPtr actxt,
1885 xmlParserErrors error,
1886 xmlSchemaAttrInfoPtr ni,
1887 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00001888{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001889 xmlChar *msg = NULL, *str = NULL;
1890
1891 xmlSchemaFormatNodeForError(&msg, actxt, node);
1892 msg = xmlStrcat(msg, BAD_CAST "The attribute '%s' is not allowed.\n");
1893 xmlSchemaErr(actxt, error, node, (const char *) msg,
1894 xmlSchemaFormatErrorNodeQName(&str, (xmlSchemaNodeInfoPtr) ni, node),
1895 NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00001896 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001897 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00001898}
1899
Daniel Veillardc0826a72004-08-10 14:17:33 +00001900static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001901xmlSchemaComplexTypeErr(xmlSchemaAbstractCtxtPtr actxt,
1902 xmlParserErrors error,
1903 xmlNodePtr node,
1904 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00001905 const char *message,
1906 int nbval,
1907 int nbneg,
1908 xmlChar **values)
1909{
1910 xmlChar *str = NULL, *msg = NULL;
1911 xmlChar *localName, *nsName;
1912 const xmlChar *cur, *end;
Daniel Veillard77005e62005-07-19 16:26:18 +00001913 int i, is_not;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00001914
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001915 xmlSchemaFormatNodeForError(&msg, actxt, node);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00001916 msg = xmlStrcat(msg, (const xmlChar *) message);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001917 msg = xmlStrcat(msg, BAD_CAST ".");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00001918 /*
1919 * Note that is does not make sense to report that we have a
1920 * wildcard here, since the wildcard might be unfolded into
1921 * multiple transitions.
1922 */
1923 if (nbval + nbneg > 0) {
1924 if (nbval + nbneg > 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001925 str = xmlStrdup(BAD_CAST " Expected is one of ( ");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00001926 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001927 str = xmlStrdup(BAD_CAST " Expected is ( ");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00001928 nsName = NULL;
1929
1930 for (i = 0; i < nbval + nbneg; i++) {
1931 cur = values[i];
Daniel Veillard77005e62005-07-19 16:26:18 +00001932 if (cur == NULL)
1933 continue;
1934 if ((cur[0] == 'n') && (cur[1] == 'o') && (cur[2] == 't') &&
1935 (cur[3] == ' ')) {
1936 is_not = 1;
1937 cur += 4;
1938 str = xmlStrcat(str, BAD_CAST "not ");
1939 } else {
1940 is_not = 0;
1941 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00001942 /*
1943 * Get the local name.
1944 */
1945 localName = NULL;
1946
1947 end = cur;
1948 if (*end == '*') {
1949 localName = xmlStrdup(BAD_CAST "*");
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00001950 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00001951 } else {
1952 while ((*end != 0) && (*end != '|'))
1953 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00001954 localName = xmlStrncat(localName, BAD_CAST cur, end - cur);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00001955 }
1956 if (*end != 0) {
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00001957 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00001958 /*
1959 * Skip "*|*" if they come with negated expressions, since
1960 * they represent the same negated wildcard.
1961 */
1962 if ((nbneg == 0) || (*end != '*') || (*localName != '*')) {
1963 /*
1964 * Get the namespace name.
1965 */
1966 cur = end;
1967 if (*end == '*') {
1968 nsName = xmlStrdup(BAD_CAST "{*}");
1969 } else {
1970 while (*end != 0)
1971 end++;
1972
1973 if (i >= nbval)
1974 nsName = xmlStrdup(BAD_CAST "{##other:");
1975 else
1976 nsName = xmlStrdup(BAD_CAST "{");
1977
1978 nsName = xmlStrncat(nsName, BAD_CAST cur, end - cur);
1979 nsName = xmlStrcat(nsName, BAD_CAST "}");
1980 }
1981 str = xmlStrcat(str, BAD_CAST nsName);
1982 FREE_AND_NULL(nsName)
1983 } else {
1984 FREE_AND_NULL(localName);
1985 continue;
1986 }
1987 }
1988 str = xmlStrcat(str, BAD_CAST localName);
1989 FREE_AND_NULL(localName);
1990
1991 if (i < nbval + nbneg -1)
1992 str = xmlStrcat(str, BAD_CAST ", ");
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00001993 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001994 str = xmlStrcat(str, BAD_CAST " ).\n");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00001995 msg = xmlStrcat(msg, BAD_CAST str);
1996 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001997 } else
1998 msg = xmlStrcat(msg, BAD_CAST "\n");
1999 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002000 xmlFree(msg);
2001}
2002
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002003static void
2004xmlSchemaFacetErr(xmlSchemaAbstractCtxtPtr actxt,
2005 xmlParserErrors error,
2006 xmlNodePtr node,
2007 const xmlChar *value,
2008 unsigned long length,
2009 xmlSchemaTypePtr type,
2010 xmlSchemaFacetPtr facet,
2011 const char *message,
2012 const xmlChar *str1,
2013 const xmlChar *str2)
2014{
2015 xmlChar *str = NULL, *msg = NULL;
2016 xmlSchemaTypeType facetType;
2017 int nodeType = xmlSchemaEvalErrorNodeType(actxt, node);
2018
2019 xmlSchemaFormatNodeForError(&msg, actxt, node);
2020 if (error == XML_SCHEMAV_CVC_ENUMERATION_VALID) {
2021 facetType = XML_SCHEMA_FACET_ENUMERATION;
2022 /*
2023 * If enumerations are validated, one must not expect the
2024 * facet to be given.
2025 */
2026 } else
2027 facetType = facet->type;
2028 msg = xmlStrcat(msg, BAD_CAST "[");
2029 msg = xmlStrcat(msg, BAD_CAST "facet '");
2030 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facetType));
2031 msg = xmlStrcat(msg, BAD_CAST "'] ");
2032 if (message == NULL) {
2033 /*
2034 * Use a default message.
2035 */
2036 if ((facetType == XML_SCHEMA_FACET_LENGTH) ||
2037 (facetType == XML_SCHEMA_FACET_MINLENGTH) ||
2038 (facetType == XML_SCHEMA_FACET_MAXLENGTH)) {
2039
2040 char len[25], actLen[25];
2041
2042 /* FIXME, TODO: What is the max expected string length of the
2043 * this value?
2044 */
2045 if (nodeType == XML_ATTRIBUTE_NODE)
2046 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has a length of '%s'; ");
2047 else
2048 msg = xmlStrcat(msg, BAD_CAST "The value has a length of '%s'; ");
2049
2050 snprintf(len, 24, "%lu", xmlSchemaGetFacetValueAsULong(facet));
2051 snprintf(actLen, 24, "%lu", length);
2052
2053 if (facetType == XML_SCHEMA_FACET_LENGTH)
2054 msg = xmlStrcat(msg,
2055 BAD_CAST "this differs from the allowed length of '%s'.\n");
2056 else if (facetType == XML_SCHEMA_FACET_MAXLENGTH)
2057 msg = xmlStrcat(msg,
2058 BAD_CAST "this exceeds the allowed maximum length of '%s'.\n");
2059 else if (facetType == XML_SCHEMA_FACET_MINLENGTH)
2060 msg = xmlStrcat(msg,
2061 BAD_CAST "this underruns the allowed minimum length of '%s'.\n");
2062
2063 if (nodeType == XML_ATTRIBUTE_NODE)
2064 xmlSchemaErr3(actxt, error, node, (const char *) msg,
2065 value, (const xmlChar *) actLen, (const xmlChar *) len);
2066 else
2067 xmlSchemaErr(actxt, error, node, (const char *) msg,
2068 (const xmlChar *) actLen, (const xmlChar *) len);
2069
2070 } else if (facetType == XML_SCHEMA_FACET_ENUMERATION) {
2071 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not an element "
2072 "of the set {%s}.\n");
2073 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2074 xmlSchemaFormatFacetEnumSet(actxt, &str, type));
2075 } else if (facetType == XML_SCHEMA_FACET_PATTERN) {
2076 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not accepted "
2077 "by the pattern '%s'.\n");
2078 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2079 facet->value);
2080 } else if (facetType == XML_SCHEMA_FACET_MININCLUSIVE) {
2081 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is less than the "
2082 "minimum value allowed ('%s').\n");
2083 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2084 facet->value);
2085 } else if (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) {
2086 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is greater than the "
2087 "maximum value allowed ('%s').\n");
2088 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2089 facet->value);
2090 } else if (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE) {
2091 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be less than "
2092 "'%s'.\n");
2093 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2094 facet->value);
2095 } else if (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) {
2096 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be more than "
2097 "'%s'.\n");
2098 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2099 facet->value);
2100 } else if (facetType == XML_SCHEMA_FACET_TOTALDIGITS) {
2101 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more "
2102 "digits than are allowed ('%s').\n");
2103 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2104 facet->value);
2105 } else if (facetType == XML_SCHEMA_FACET_FRACTIONDIGITS) {
2106 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more fractional "
2107 "digits than are allowed ('%s').\n");
2108 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2109 facet->value);
2110 } else if (nodeType == XML_ATTRIBUTE_NODE) {
2111 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not facet-valid.\n");
2112 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2113 } else {
2114 msg = xmlStrcat(msg, BAD_CAST "The value is not facet-valid.\n");
2115 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2116 }
2117 } else {
2118 msg = xmlStrcat(msg, (const xmlChar *) message);
2119 msg = xmlStrcat(msg, BAD_CAST ".\n");
2120 xmlSchemaErr(actxt, error, node, (const char *) msg, str1, str2);
2121 }
2122 FREE_AND_NULL(str)
2123 xmlFree(msg);
2124}
2125
2126#define VERROR(err, type, msg) \
2127 xmlSchemaCustomErr((xmlSchemaAbstractCtxtPtr) vctxt, err, NULL, type, msg, NULL, NULL);
2128
2129#define VERROR_INT(func, msg) xmlSchemaInternalErr((xmlSchemaAbstractCtxtPtr) vctxt, func, msg);
2130
2131#define PERROR_INT(func, msg) xmlSchemaInternalErr((xmlSchemaAbstractCtxtPtr) pctxt, func, msg);
2132
2133#define AERROR_INT(func, msg) xmlSchemaInternalErr(actxt, func, msg);
2134
2135
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002136/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00002137 * xmlSchemaPMissingAttrErr:
2138 * @ctxt: the schema validation context
2139 * @ownerDes: the designation of the owner
2140 * @ownerName: the name of the owner
2141 * @ownerItem: the owner as a schema object
2142 * @ownerElem: the owner as an element node
2143 * @node: the parent element node of the missing attribute node
2144 * @type: the corresponding type of the attribute node
2145 *
2146 * Reports an illegal attribute.
2147 */
2148static void
2149xmlSchemaPMissingAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002150 xmlParserErrors error,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002151 xmlSchemaTypePtr ownerItem,
2152 xmlNodePtr ownerElem,
2153 const char *name,
2154 const char *message)
2155{
2156 xmlChar *des = NULL;
2157
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002158 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
2159
Daniel Veillardc0826a72004-08-10 14:17:33 +00002160 if (message != NULL)
2161 xmlSchemaPErr(ctxt, ownerElem, error, "%s: %s.\n", BAD_CAST des, BAD_CAST message);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002162 else
2163 xmlSchemaPErr(ctxt, ownerElem, error,
2164 "%s: The attribute '%s' is required but missing.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00002165 BAD_CAST des, BAD_CAST name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002166 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002167}
2168
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002169
Daniel Veillardc0826a72004-08-10 14:17:33 +00002170/**
2171 * xmlSchemaPResCompAttrErr:
2172 * @ctxt: the schema validation context
2173 * @error: the error code
2174 * @ownerDes: the designation of the owner
2175 * @ownerItem: the owner as a schema object
2176 * @ownerElem: the owner as an element node
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002177 * @name: the name of the attribute holding the QName
Daniel Veillardc0826a72004-08-10 14:17:33 +00002178 * @refName: the referenced local name
2179 * @refURI: the referenced namespace URI
2180 * @message: optional message
2181 *
2182 * Used to report QName attribute values that failed to resolve
2183 * to schema components.
2184 */
2185static void
2186xmlSchemaPResCompAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002187 xmlParserErrors error,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002188 xmlSchemaTypePtr ownerItem,
2189 xmlNodePtr ownerElem,
2190 const char *name,
2191 const xmlChar *refName,
2192 const xmlChar *refURI,
2193 xmlSchemaTypeType refType,
2194 const char *refTypeStr)
2195{
2196 xmlChar *des = NULL, *strA = NULL;
2197
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002198 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002199 if (refTypeStr == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002200 refTypeStr = (const char *) xmlSchemaCompTypeToString(refType);
2201 xmlSchemaPErrExt(ctxt, ownerElem, error,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002202 NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002203 "%s, attribute '%s': The QName value '%s' does not resolve to a(n) "
2204 "%s.\n", BAD_CAST des, BAD_CAST name,
2205 xmlSchemaFormatQName(&strA, refURI, refName),
Daniel Veillardc0826a72004-08-10 14:17:33 +00002206 BAD_CAST refTypeStr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002207 FREE_AND_NULL(des)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002208 FREE_AND_NULL(strA)
2209}
2210
William M. Brack2f2a6632004-08-20 23:09:47 +00002211/**
2212 * xmlSchemaPCustomAttrErr:
2213 * @ctxt: the schema parser context
2214 * @error: the error code
2215 * @ownerDes: the designation of the owner
2216 * @ownerItem: the owner as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002217 * @attr: the illegal attribute node
William M. Brack2f2a6632004-08-20 23:09:47 +00002218 *
2219 * Reports an illegal attribute during the parse.
2220 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002221static void
2222xmlSchemaPCustomAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002223 xmlParserErrors error,
William M. Brack2f2a6632004-08-20 23:09:47 +00002224 xmlChar **ownerDes,
2225 xmlSchemaTypePtr ownerItem,
2226 xmlAttrPtr attr,
2227 const char *msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002228{
2229 xmlChar *des = NULL;
2230
2231 if (ownerDes == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002232 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002233 else if (*ownerDes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002234 xmlSchemaFormatItemForReport(ownerDes, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002235 des = *ownerDes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002236 } else
2237 des = *ownerDes;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002238 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002239 "%s, attribute '%s': %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00002240 BAD_CAST des, attr->name, (const xmlChar *) msg, NULL, NULL);
2241 if (ownerDes == NULL)
2242 FREE_AND_NULL(des);
2243}
2244
2245/**
2246 * xmlSchemaPIllegalAttrErr:
William M. Brack2f2a6632004-08-20 23:09:47 +00002247 * @ctxt: the schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00002248 * @error: the error code
William M. Brack2f2a6632004-08-20 23:09:47 +00002249 * @ownerDes: the designation of the attribute's owner
2250 * @ownerItem: the attribute's owner item
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002251 * @attr: the illegal attribute node
Daniel Veillardc0826a72004-08-10 14:17:33 +00002252 *
William M. Brack2f2a6632004-08-20 23:09:47 +00002253 * Reports an illegal attribute during the parse.
Daniel Veillardc0826a72004-08-10 14:17:33 +00002254 */
2255static void
2256xmlSchemaPIllegalAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002257 xmlParserErrors error,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002258 xmlChar **ownerDes,
2259 xmlSchemaTypePtr ownerItem,
2260 xmlAttrPtr attr)
2261{
2262 xmlChar *des = NULL, *strA = NULL;
2263
2264 if (ownerDes == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002265 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002266 else if (*ownerDes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002267 xmlSchemaFormatItemForReport(ownerDes, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002268 des = *ownerDes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002269 } else
2270 des = *ownerDes;
2271 xmlSchemaPErr(ctxt, (xmlNodePtr) attr, error,
2272 "%s: The attribute '%s' is not allowed.\n", BAD_CAST des,
2273 xmlSchemaFormatQNameNs(&strA, attr->ns, attr->name));
Daniel Veillardc0826a72004-08-10 14:17:33 +00002274 if (ownerDes == NULL)
2275 FREE_AND_NULL(des);
2276 FREE_AND_NULL(strA);
2277}
2278
William M. Brack2f2a6632004-08-20 23:09:47 +00002279/**
2280 * xmlSchemaPAquireDes:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002281 * @des: the first designation
William M. Brack2f2a6632004-08-20 23:09:47 +00002282 * @itemDes: the second designation
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002283 * @item: the schema item
William M. Brack2f2a6632004-08-20 23:09:47 +00002284 * @itemElem: the node of the schema item
2285 *
2286 * Creates a designation for an item.
2287 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002288static void
2289xmlSchemaPAquireDes(xmlChar **des,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002290 xmlChar **itemDes,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002291 xmlSchemaTypePtr item,
2292 xmlNodePtr itemElem)
2293{
2294 if (itemDes == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002295 xmlSchemaFormatItemForReport(des, NULL, item, itemElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002296 else if (*itemDes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002297 xmlSchemaFormatItemForReport(itemDes, NULL, item, itemElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002298 *des = *itemDes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002299 } else
2300 *des = *itemDes;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002301}
2302
William M. Brack2f2a6632004-08-20 23:09:47 +00002303/**
2304 * xmlSchemaPCustomErr:
2305 * @ctxt: the schema parser context
2306 * @error: the error code
2307 * @itemDes: the designation of the schema item
2308 * @item: the schema item
2309 * @itemElem: the node of the schema item
2310 * @message: the error message
2311 * @str1: an optional param for the error message
2312 * @str2: an optional param for the error message
2313 * @str3: an optional param for the error message
2314 *
2315 * Reports an error during parsing.
2316 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002317static void
2318xmlSchemaPCustomErrExt(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002319 xmlParserErrors error,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002320 xmlChar **itemDes,
2321 xmlSchemaTypePtr item,
2322 xmlNodePtr itemElem,
2323 const char *message,
2324 const xmlChar *str1,
2325 const xmlChar *str2,
2326 const xmlChar *str3)
2327{
2328 xmlChar *des = NULL, *msg = NULL;
2329
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002330 xmlSchemaPAquireDes(&des, itemDes, item, itemElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002331 msg = xmlStrdup(BAD_CAST "%s: ");
2332 msg = xmlStrcat(msg, (const xmlChar *) message);
2333 msg = xmlStrcat(msg, BAD_CAST ".\n");
2334 if ((itemElem == NULL) && (item != NULL))
2335 itemElem = item->node;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002336 xmlSchemaPErrExt(ctxt, itemElem, error, NULL, NULL, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002337 (const char *) msg, BAD_CAST des, str1, str2, str3, NULL);
2338 if (itemDes == NULL)
2339 FREE_AND_NULL(des);
2340 FREE_AND_NULL(msg);
2341}
2342
William M. Brack2f2a6632004-08-20 23:09:47 +00002343/**
2344 * xmlSchemaPCustomErr:
2345 * @ctxt: the schema parser context
2346 * @error: the error code
2347 * @itemDes: the designation of the schema item
2348 * @item: the schema item
2349 * @itemElem: the node of the schema item
2350 * @message: the error message
2351 * @str1: the optional param for the error message
2352 *
2353 * Reports an error during parsing.
2354 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002355static void
2356xmlSchemaPCustomErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002357 xmlParserErrors error,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002358 xmlChar **itemDes,
2359 xmlSchemaTypePtr item,
2360 xmlNodePtr itemElem,
2361 const char *message,
2362 const xmlChar *str1)
2363{
2364 xmlSchemaPCustomErrExt(ctxt, error, itemDes, item, itemElem, message,
2365 str1, NULL, NULL);
2366}
2367
William M. Brack2f2a6632004-08-20 23:09:47 +00002368/**
2369 * xmlSchemaPAttrUseErr:
2370 * @ctxt: the schema parser context
2371 * @error: the error code
2372 * @itemDes: the designation of the schema type
2373 * @item: the schema type
2374 * @itemElem: the node of the schema type
2375 * @attr: the invalid schema attribute
2376 * @message: the error message
2377 * @str1: the optional param for the error message
2378 *
2379 * Reports an attribute use error during parsing.
2380 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002381static void
2382xmlSchemaPAttrUseErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002383 xmlParserErrors error,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002384 xmlSchemaTypePtr item,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002385 const xmlSchemaAttributePtr attr,
2386 const char *message,
2387 const xmlChar *str1)
2388{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002389 xmlChar *str = NULL, *msg = NULL;
2390 xmlSchemaFormatItemForReport(&msg, NULL, item, NULL);
2391 msg = xmlStrcat(msg, BAD_CAST ", ");
2392 msg = xmlStrcat(msg,
2393 BAD_CAST xmlSchemaFormatItemForReport(&str, NULL,
2394 (xmlSchemaTypePtr) attr, NULL));
2395 FREE_AND_NULL(str);
2396 msg = xmlStrcat(msg, BAD_CAST ": ");
Daniel Veillardc0826a72004-08-10 14:17:33 +00002397 msg = xmlStrcat(msg, (const xmlChar *) message);
2398 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002399 xmlSchemaPErr(ctxt, attr->node, error,
2400 (const char *) msg, str1, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002401 xmlFree(msg);
2402}
2403
William M. Brack2f2a6632004-08-20 23:09:47 +00002404/**
2405 * xmlSchemaPIllegalFacetAtomicErr:
2406 * @ctxt: the schema parser context
2407 * @error: the error code
2408 * @itemDes: the designation of the type
2409 * @item: the schema type
2410 * @baseItem: the base type of type
2411 * @facet: the illegal facet
2412 *
2413 * Reports an illegal facet for atomic simple types.
2414 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002415static void
2416xmlSchemaPIllegalFacetAtomicErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002417 xmlParserErrors error,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002418 xmlChar **itemDes,
2419 xmlSchemaTypePtr item,
2420 xmlSchemaTypePtr baseItem,
2421 xmlSchemaFacetPtr facet)
2422{
2423 xmlChar *des = NULL, *strT = NULL;
2424
2425 xmlSchemaPAquireDes(&des, itemDes, item, item->node);
2426 xmlSchemaPErrExt(ctxt, item->node, error, NULL, NULL, NULL,
2427 "%s: The facet '%s' is not allowed on types derived from the "
2428 "type %s.\n",
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00002429 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002430 xmlSchemaFormatItemForReport(&strT, NULL, baseItem, NULL),
Daniel Veillardc0826a72004-08-10 14:17:33 +00002431 NULL, NULL);
2432 if (itemDes == NULL)
2433 FREE_AND_NULL(des);
2434 FREE_AND_NULL(strT);
2435}
2436
William M. Brack2f2a6632004-08-20 23:09:47 +00002437/**
2438 * xmlSchemaPIllegalFacetListUnionErr:
2439 * @ctxt: the schema parser context
2440 * @error: the error code
2441 * @itemDes: the designation of the schema item involved
2442 * @item: the schema item involved
2443 * @facet: the illegal facet
2444 *
2445 * Reports an illegal facet for <list> and <union>.
2446 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002447static void
2448xmlSchemaPIllegalFacetListUnionErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002449 xmlParserErrors error,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002450 xmlChar **itemDes,
2451 xmlSchemaTypePtr item,
2452 xmlSchemaFacetPtr facet)
2453{
2454 xmlChar *des = NULL, *strT = NULL;
2455
2456 xmlSchemaPAquireDes(&des, itemDes, item, item->node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002457 xmlSchemaPErr(ctxt, item->node, error,
2458 "%s: The facet '%s' is not allowed.\n",
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00002459 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type));
Daniel Veillardc0826a72004-08-10 14:17:33 +00002460 if (itemDes == NULL)
2461 FREE_AND_NULL(des);
2462 FREE_AND_NULL(strT);
2463}
2464
2465/**
2466 * xmlSchemaPMutualExclAttrErr:
2467 * @ctxt: the schema validation context
2468 * @error: the error code
2469 * @elemDes: the designation of the parent element node
2470 * @attr: the bad attribute node
2471 * @type: the corresponding type of the attribute node
2472 *
2473 * Reports an illegal attribute.
2474 */
2475static void
2476xmlSchemaPMutualExclAttrErr(xmlSchemaParserCtxtPtr ctxt,
2477 xmlParserErrors error,
2478 xmlChar **ownerDes,
2479 xmlSchemaTypePtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002480 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002481 const char *name1,
2482 const char *name2)
2483{
2484 xmlChar *des = NULL;
2485
2486 if (ownerDes == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002487 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002488 else if (*ownerDes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002489 xmlSchemaFormatItemForReport(ownerDes, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002490 des = *ownerDes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002491 } else
2492 des = *ownerDes;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002493 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002494 "%s: The attributes '%s' and '%s' are mutually exclusive.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00002495 BAD_CAST des, BAD_CAST name1, BAD_CAST name2, NULL, NULL);
2496 if (ownerDes == NULL)
2497 FREE_AND_NULL(des)
2498}
2499
2500/**
2501 * xmlSchemaPSimpleTypeErr:
2502 * @ctxt: the schema validation context
2503 * @error: the error code
William M. Brack2f2a6632004-08-20 23:09:47 +00002504 * @type: the type specifier
Daniel Veillardc0826a72004-08-10 14:17:33 +00002505 * @ownerDes: the designation of the owner
2506 * @ownerItem: the schema object if existent
2507 * @node: the validated node
2508 * @value: the validated value
2509 *
2510 * Reports a simple type validation error.
2511 * TODO: Should this report the value of an element as well?
2512 */
2513static void
2514xmlSchemaPSimpleTypeErr(xmlSchemaParserCtxtPtr ctxt,
2515 xmlParserErrors error,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002516 xmlSchemaTypePtr ownerItem ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002517 xmlNodePtr node,
William M. Brack2f2a6632004-08-20 23:09:47 +00002518 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002519 const char *expected,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002520 const xmlChar *value,
2521 const char *message,
2522 const xmlChar *str1,
2523 const xmlChar *str2)
2524{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002525 xmlChar *msg = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002526
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002527 xmlSchemaFormatNodeForError(&msg, (xmlSchemaAbstractCtxtPtr) ctxt, node);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002528 if (message == NULL) {
2529 /*
2530 * Use default messages.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002531 */
2532 if (type != NULL) {
2533 if (node->type == XML_ATTRIBUTE_NODE)
2534 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
2535 else
2536 msg = xmlStrcat(msg, BAD_CAST "The character content is not a "
2537 "valid value of ");
2538 if (! xmlSchemaIsGlobalItem(type))
2539 msg = xmlStrcat(msg, BAD_CAST "the local ");
2540 else
2541 msg = xmlStrcat(msg, BAD_CAST "the ");
2542
2543 if (VARIETY_ATOMIC(type))
2544 msg = xmlStrcat(msg, BAD_CAST "atomic type");
2545 else if (VARIETY_LIST(type))
2546 msg = xmlStrcat(msg, BAD_CAST "list type");
2547 else if (VARIETY_UNION(type))
2548 msg = xmlStrcat(msg, BAD_CAST "union type");
2549
2550 if (xmlSchemaIsGlobalItem(type)) {
2551 xmlChar *str = NULL;
2552 msg = xmlStrcat(msg, BAD_CAST " '");
2553 if (type->builtInType != 0) {
2554 msg = xmlStrcat(msg, BAD_CAST "xs:");
2555 msg = xmlStrcat(msg, type->name);
2556 } else
2557 msg = xmlStrcat(msg,
2558 xmlSchemaFormatQName(&str,
2559 type->targetNamespace, type->name));
2560 msg = xmlStrcat(msg, BAD_CAST "'.");
2561 FREE_AND_NULL(str);
2562 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00002563 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002564 if (node->type == XML_ATTRIBUTE_NODE)
2565 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not valid.");
2566 else
2567 msg = xmlStrcat(msg, BAD_CAST "The character content is not "
2568 "valid.");
2569 }
2570 if (expected) {
2571 msg = xmlStrcat(msg, BAD_CAST " Expected is '");
2572 msg = xmlStrcat(msg, BAD_CAST expected);
2573 msg = xmlStrcat(msg, BAD_CAST "'.\n");
2574 } else
2575 msg = xmlStrcat(msg, BAD_CAST "\n");
Daniel Veillardc0826a72004-08-10 14:17:33 +00002576 if (node->type == XML_ATTRIBUTE_NODE)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002577 xmlSchemaPErr(ctxt, node, error, (const char *) msg, value, NULL);
2578 else
2579 xmlSchemaPErr(ctxt, node, error, (const char *) msg, NULL, NULL);
2580 } else {
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +00002581 msg = xmlStrcat(msg, BAD_CAST message);
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +00002582 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002583 xmlSchemaPErrExt(ctxt, node, error, NULL, NULL, NULL,
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +00002584 (const char*) msg, str1, str2, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002585 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002586 /* Cleanup. */
2587 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002588}
2589
William M. Brack2f2a6632004-08-20 23:09:47 +00002590/**
2591 * xmlSchemaPContentErr:
2592 * @ctxt: the schema parser context
2593 * @error: the error code
2594 * @onwerDes: the designation of the holder of the content
2595 * @ownerItem: the owner item of the holder of the content
2596 * @ownerElem: the node of the holder of the content
2597 * @child: the invalid child node
2598 * @message: the optional error message
2599 * @content: the optional string describing the correct content
2600 *
2601 * Reports an error concerning the content of a schema element.
2602 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002603static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002604xmlSchemaPContentErr(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002605 xmlParserErrors error,
2606 xmlChar **ownerDes,
2607 xmlSchemaTypePtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002608 xmlNodePtr ownerElem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002609 xmlNodePtr child,
2610 const char *message,
2611 const char *content)
2612{
2613 xmlChar *des = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002614
Daniel Veillardc0826a72004-08-10 14:17:33 +00002615 if (ownerDes == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002616 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002617 else if (*ownerDes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002618 xmlSchemaFormatItemForReport(ownerDes, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002619 des = *ownerDes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002620 } else
2621 des = *ownerDes;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002622 if (message != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002623 xmlSchemaPErr2(ctxt, ownerElem, child, error,
2624 "%s: %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00002625 BAD_CAST des, BAD_CAST message);
2626 else {
2627 if (content != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002628 xmlSchemaPErr2(ctxt, ownerElem, child, error,
2629 "%s: The content is not valid. Expected is %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00002630 BAD_CAST des, BAD_CAST content);
2631 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002632 xmlSchemaPErr2(ctxt, ownerElem, child, error,
2633 "%s: The content is not valid.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00002634 BAD_CAST des, NULL);
2635 }
2636 }
2637 if (ownerDes == NULL)
2638 FREE_AND_NULL(des)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002639}
2640
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002641/************************************************************************
2642 * *
2643 * Streamable error functions *
2644 * *
2645 ************************************************************************/
Kasimier T. Buchcik8b418172004-11-17 13:14:27 +00002646
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00002647
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00002648
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00002649
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002650/************************************************************************
2651 * *
2652 * Validation helper functions *
2653 * *
2654 ************************************************************************/
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00002655
Daniel Veillardc0826a72004-08-10 14:17:33 +00002656
Daniel Veillard4255d502002-04-16 15:50:10 +00002657/************************************************************************
2658 * *
2659 * Allocation functions *
2660 * *
2661 ************************************************************************/
2662
2663/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00002664 * xmlSchemaNewSchemaForParserCtxt:
William M. Brack08171912003-12-29 02:52:11 +00002665 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +00002666 *
2667 * Allocate a new Schema structure.
2668 *
2669 * Returns the newly allocated structure or NULL in case or error
2670 */
2671static xmlSchemaPtr
2672xmlSchemaNewSchema(xmlSchemaParserCtxtPtr ctxt)
2673{
2674 xmlSchemaPtr ret;
2675
2676 ret = (xmlSchemaPtr) xmlMalloc(sizeof(xmlSchema));
2677 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002678 xmlSchemaPErrMemory(ctxt, "allocating schema", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00002679 return (NULL);
2680 }
2681 memset(ret, 0, sizeof(xmlSchema));
Daniel Veillardbe9c6322003-11-22 20:37:51 +00002682 ret->dict = ctxt->dict;
Daniel Veillard500a1de2004-03-22 15:22:58 +00002683 xmlDictReference(ret->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00002684
2685 return (ret);
2686}
2687
2688/**
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00002689 * xmlSchemaNewSchema:
2690 * @ctxt: a schema validation context
2691 *
2692 * Allocate a new Schema structure.
2693 *
2694 * Returns the newly allocated structure or NULL in case or error
2695 */
2696static xmlSchemaAssemblePtr
2697xmlSchemaNewAssemble(void)
2698{
2699 xmlSchemaAssemblePtr ret;
2700
2701 ret = (xmlSchemaAssemblePtr) xmlMalloc(sizeof(xmlSchemaAssemble));
2702 if (ret == NULL) {
2703 /* xmlSchemaPErrMemory(ctxt, "allocating assemble info", NULL); */
2704 return (NULL);
2705 }
2706 memset(ret, 0, sizeof(xmlSchemaAssemble));
2707 ret->items = NULL;
2708 return (ret);
2709}
2710
2711/**
Daniel Veillard4255d502002-04-16 15:50:10 +00002712 * xmlSchemaNewFacet:
Daniel Veillard4255d502002-04-16 15:50:10 +00002713 *
2714 * Allocate a new Facet structure.
2715 *
2716 * Returns the newly allocated structure or NULL in case or error
2717 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002718xmlSchemaFacetPtr
2719xmlSchemaNewFacet(void)
Daniel Veillard4255d502002-04-16 15:50:10 +00002720{
2721 xmlSchemaFacetPtr ret;
2722
2723 ret = (xmlSchemaFacetPtr) xmlMalloc(sizeof(xmlSchemaFacet));
2724 if (ret == NULL) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002725 return (NULL);
2726 }
2727 memset(ret, 0, sizeof(xmlSchemaFacet));
2728
2729 return (ret);
2730}
2731
2732/**
2733 * xmlSchemaNewAnnot:
William M. Brack08171912003-12-29 02:52:11 +00002734 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +00002735 * @node: a node
2736 *
2737 * Allocate a new annotation structure.
2738 *
2739 * Returns the newly allocated structure or NULL in case or error
2740 */
2741static xmlSchemaAnnotPtr
2742xmlSchemaNewAnnot(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
2743{
2744 xmlSchemaAnnotPtr ret;
2745
2746 ret = (xmlSchemaAnnotPtr) xmlMalloc(sizeof(xmlSchemaAnnot));
2747 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002748 xmlSchemaPErrMemory(ctxt, "allocating annotation", node);
Daniel Veillard4255d502002-04-16 15:50:10 +00002749 return (NULL);
2750 }
2751 memset(ret, 0, sizeof(xmlSchemaAnnot));
2752 ret->content = node;
2753 return (ret);
2754}
2755
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00002756static xmlSchemaItemListPtr
2757xmlSchemaNewItemList(void)
2758{
2759 xmlSchemaItemListPtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002760
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00002761 ret = xmlMalloc(sizeof(xmlSchemaItemList));
2762 if (ret == NULL) {
2763 xmlSchemaPErrMemory(NULL,
2764 "allocating an item list structure", NULL);
2765 return (NULL);
2766 }
2767 memset(ret, 0, sizeof(xmlSchemaItemList));
2768 return (ret);
2769}
2770
2771/**
2772 * xmlSchemaAddElementSubstitutionMember:
2773 * @pctxt: a schema parser context
2774 * @head: the head of the substitution group
2775 * @member: the new member of the substitution group
2776 *
2777 * Allocate a new annotation structure.
2778 *
2779 * Returns the newly allocated structure or NULL in case or error
2780 */
2781static int
2782xmlSchemaAddElementSubstitutionMember(xmlSchemaParserCtxtPtr pctxt,
2783 xmlSchemaElementPtr head,
2784 xmlSchemaElementPtr member)
2785{
2786 xmlSchemaSubstGroupPtr substGroup;
2787
2788 if (pctxt == NULL)
2789 return (-1);
2790
2791 if (pctxt->substGroups == NULL) {
2792 pctxt->substGroups = xmlHashCreateDict(10, pctxt->dict);
2793 if (pctxt->substGroups == NULL)
2794 return (-1);
2795 }
2796 substGroup = xmlHashLookup2(pctxt->substGroups, head->name,
2797 head->targetNamespace);
2798 if (substGroup == NULL) {
2799 int res;
2800
2801 substGroup = (xmlSchemaSubstGroupPtr) xmlMalloc(sizeof(xmlSchemaSubstGroup));
2802 if (substGroup == NULL) {
2803 xmlSchemaPErrMemory(NULL,
2804 "xmlSchemaAddElementSubstitution, allocating a substitution "
2805 "group container",
2806 NULL);
2807 return (-1);
2808 }
2809 substGroup->members = xmlSchemaNewItemList();
2810 if (substGroup->members == NULL) {
2811 xmlFree(substGroup);
2812 return (-1);
2813 }
2814 substGroup->head = head;
2815
2816 res = xmlHashAddEntry2(pctxt->substGroups,
2817 head->name, head->targetNamespace, substGroup);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002818 if (res != 0) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00002819 xmlFree(substGroup->members);
2820 xmlFree(substGroup);
2821 xmlSchemaPErr(pctxt, member->node,
2822 XML_SCHEMAP_INTERNAL,
2823 "Internal error: xmlSchemaAddElementSubstitution, "
2824 "failed to add a new substitution group container for "
2825 "'%s'.\n", head->name, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002826 return (-1);
2827 }
2828 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00002829 if (substGroup->members->items == NULL) {
2830 substGroup->members->items = (void **) xmlMalloc(
2831 5 * sizeof(xmlSchemaElementPtr));
2832 if (substGroup->members->items == NULL) {
2833 xmlSchemaPErrMemory(NULL,
2834 "allocating list of substitution group members", NULL);
2835 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002836 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00002837 substGroup->members->sizeItems = 5;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002838 } else if (substGroup->members->sizeItems <=
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00002839 substGroup->members->nbItems) {
2840 substGroup->members->sizeItems *= 2;
2841 substGroup->members->items = (void **) xmlRealloc(
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002842 substGroup->members->items,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00002843 substGroup->members->sizeItems * sizeof(xmlSchemaElementPtr));
2844 if (substGroup->members->items == NULL) {
2845 xmlSchemaPErrMemory(NULL,
2846 "re-allocating list of substitution group members", NULL);
2847 substGroup->members->sizeItems = 0;
2848 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002849 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00002850 }
2851 ((xmlSchemaElementPtr *) substGroup->members->items)
2852 [substGroup->members->nbItems++] = (void *) member;
2853 return (0);
2854}
2855
2856/**
2857 * xmlSchemaGetElementSubstitutionGroup:
2858 * @pctxt: a schema parser context
2859 * @head: the head of the substitution group
2860 * @member: the new member of the substitution group
2861 *
2862 * Allocate a new annotation structure.
2863 *
2864 * Returns the newly allocated structure or NULL in case or error
2865 */
2866static xmlSchemaSubstGroupPtr
2867xmlSchemaGetElementSubstitutionGroup(xmlSchemaParserCtxtPtr pctxt,
2868 xmlSchemaElementPtr head)
2869{
2870 if (pctxt == NULL)
2871 return (NULL);
2872
2873 if (pctxt->substGroups == NULL)
2874 return (NULL);
2875
2876 return ((xmlSchemaSubstGroupPtr) xmlHashLookup2(pctxt->substGroups,
2877 head->name, head->targetNamespace));
2878}
2879
2880/**
2881 * xmlSchemaFreeItemList:
2882 * @annot: a schema type structure
2883 *
2884 * Deallocate a annotation structure
2885 */
2886static void
2887xmlSchemaFreeItemList(xmlSchemaItemListPtr list)
2888{
2889 if (list == NULL)
2890 return;
2891 if (list->items != NULL)
2892 xmlFree(list->items);
2893 xmlFree(list);
2894}
2895
Daniel Veillard4255d502002-04-16 15:50:10 +00002896/**
Daniel Veillardfdc91562002-07-01 21:52:03 +00002897 * xmlSchemaFreeAnnot:
2898 * @annot: a schema type structure
2899 *
2900 * Deallocate a annotation structure
2901 */
2902static void
2903xmlSchemaFreeAnnot(xmlSchemaAnnotPtr annot)
2904{
2905 if (annot == NULL)
2906 return;
2907 xmlFree(annot);
2908}
2909
2910/**
Daniel Veillard1d913862003-11-21 00:28:39 +00002911 * xmlSchemaFreeImport:
2912 * @import: a schema import structure
2913 *
2914 * Deallocate an import structure
2915 */
2916static void
2917xmlSchemaFreeImport(xmlSchemaImportPtr import)
2918{
2919 if (import == NULL)
2920 return;
2921
2922 xmlSchemaFree(import->schema);
William M. Brack2f2a6632004-08-20 23:09:47 +00002923 xmlFreeDoc(import->doc);
Daniel Veillard1d913862003-11-21 00:28:39 +00002924 xmlFree(import);
2925}
2926
2927/**
Daniel Veillardbd2904b2003-11-25 15:38:59 +00002928 * xmlSchemaFreeInclude:
2929 * @include: a schema include structure
2930 *
2931 * Deallocate an include structure
2932 */
2933static void
2934xmlSchemaFreeInclude(xmlSchemaIncludePtr include)
2935{
2936 if (include == NULL)
2937 return;
2938
2939 xmlFreeDoc(include->doc);
2940 xmlFree(include);
2941}
2942
2943/**
2944 * xmlSchemaFreeIncludeList:
2945 * @includes: a schema include list
2946 *
2947 * Deallocate an include structure
2948 */
2949static void
2950xmlSchemaFreeIncludeList(xmlSchemaIncludePtr includes)
2951{
2952 xmlSchemaIncludePtr next;
2953
2954 while (includes != NULL) {
2955 next = includes->next;
2956 xmlSchemaFreeInclude(includes);
2957 includes = next;
2958 }
2959}
2960
2961/**
Daniel Veillard4255d502002-04-16 15:50:10 +00002962 * xmlSchemaFreeNotation:
2963 * @schema: a schema notation structure
2964 *
2965 * Deallocate a Schema Notation structure.
2966 */
2967static void
2968xmlSchemaFreeNotation(xmlSchemaNotationPtr nota)
2969{
2970 if (nota == NULL)
2971 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00002972 xmlFree(nota);
2973}
2974
2975/**
2976 * xmlSchemaFreeAttribute:
2977 * @schema: a schema attribute structure
2978 *
2979 * Deallocate a Schema Attribute structure.
2980 */
2981static void
2982xmlSchemaFreeAttribute(xmlSchemaAttributePtr attr)
2983{
2984 if (attr == NULL)
2985 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002986 if (attr->annot != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002987 xmlSchemaFreeAnnot(attr->annot);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00002988 if (attr->defVal != NULL)
2989 xmlSchemaFreeValue(attr->defVal);
Daniel Veillard4255d502002-04-16 15:50:10 +00002990 xmlFree(attr);
2991}
2992
2993/**
Daniel Veillard3646d642004-06-02 19:19:14 +00002994 * xmlSchemaFreeWildcardNsSet:
2995 * set: a schema wildcard namespace
2996 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00002997 * Deallocates a list of wildcard constraint structures.
Daniel Veillard3646d642004-06-02 19:19:14 +00002998 */
2999static void
3000xmlSchemaFreeWildcardNsSet(xmlSchemaWildcardNsPtr set)
3001{
3002 xmlSchemaWildcardNsPtr next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003003
Daniel Veillard3646d642004-06-02 19:19:14 +00003004 while (set != NULL) {
3005 next = set->next;
3006 xmlFree(set);
3007 set = next;
3008 }
3009}
3010
3011/**
3012 * xmlSchemaFreeWildcard:
Daniel Veillard01fa6152004-06-29 17:04:39 +00003013 * @wildcard: a wildcard structure
Daniel Veillard3646d642004-06-02 19:19:14 +00003014 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00003015 * Deallocates a wildcard structure.
Daniel Veillard3646d642004-06-02 19:19:14 +00003016 */
Daniel Veillard01fa6152004-06-29 17:04:39 +00003017void
Daniel Veillard3646d642004-06-02 19:19:14 +00003018xmlSchemaFreeWildcard(xmlSchemaWildcardPtr wildcard)
3019{
3020 if (wildcard == NULL)
3021 return;
3022 if (wildcard->annot != NULL)
3023 xmlSchemaFreeAnnot(wildcard->annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003024 if (wildcard->nsSet != NULL)
3025 xmlSchemaFreeWildcardNsSet(wildcard->nsSet);
3026 if (wildcard->negNsSet != NULL)
3027 xmlFree(wildcard->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +00003028 xmlFree(wildcard);
3029}
3030
3031/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003032 * xmlSchemaFreeAttributeGroup:
3033 * @schema: a schema attribute group structure
3034 *
3035 * Deallocate a Schema Attribute Group structure.
3036 */
3037static void
3038xmlSchemaFreeAttributeGroup(xmlSchemaAttributeGroupPtr attr)
3039{
3040 if (attr == NULL)
3041 return;
Daniel Veillard3646d642004-06-02 19:19:14 +00003042 if (attr->annot != NULL)
3043 xmlSchemaFreeAnnot(attr->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003044 xmlFree(attr);
3045}
3046
3047/**
Daniel Veillard3646d642004-06-02 19:19:14 +00003048 * xmlSchemaFreeAttributeUseList:
Daniel Veillard01fa6152004-06-29 17:04:39 +00003049 * @attrUse: an attribute link
Daniel Veillard3646d642004-06-02 19:19:14 +00003050 *
3051 * Deallocate a list of schema attribute uses.
3052 */
3053static void
3054xmlSchemaFreeAttributeUseList(xmlSchemaAttributeLinkPtr attrUse)
3055{
3056 xmlSchemaAttributeLinkPtr next;
3057
3058 while (attrUse != NULL) {
3059 next = attrUse->next;
3060 xmlFree(attrUse);
3061 attrUse = next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003062 }
Daniel Veillard3646d642004-06-02 19:19:14 +00003063}
3064
3065/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003066 * xmlSchemaFreeQNameRef:
3067 * @item: a QName reference structure
3068 *
3069 * Deallocatea a QName reference structure.
3070 */
3071static void
3072xmlSchemaFreeQNameRef(xmlSchemaQNameRefPtr item)
3073{
3074 xmlFree(item);
3075}
3076
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003077/**
3078 * xmlSchemaFreeQNameRef:
3079 * @item: a QName reference structure
3080 *
3081 * Deallocatea a QName reference structure.
3082 */
3083static void
3084xmlSchemaFreeSubstGroup(xmlSchemaSubstGroupPtr item)
3085{
3086 if (item == NULL)
3087 return;
3088 if (item->members != NULL)
3089 xmlSchemaFreeItemList(item->members);
3090 xmlFree(item);
3091}
3092
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003093static int
3094xmlSchemaAddVolatile(xmlSchemaPtr schema,
3095 xmlSchemaBasicItemPtr item)
3096{
3097 xmlSchemaItemListPtr list;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003098
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003099 if (schema->volatiles == NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003100 schema->volatiles = (void *) xmlSchemaNewItemList();
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003101 if (schema->volatiles == NULL) {
3102 xmlSchemaPErrMemory(NULL,
3103 "allocating list of volatiles", NULL);
3104 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003105 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003106 }
3107 list = (xmlSchemaItemListPtr) schema->volatiles;
3108 if (list->items == NULL) {
3109 list->items = (void **) xmlMalloc(
3110 20 * sizeof(xmlSchemaBasicItemPtr));
3111 if (list->items == NULL) {
3112 xmlSchemaPErrMemory(NULL,
3113 "allocating new volatile item buffer", NULL);
3114 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003115 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003116 list->sizeItems = 20;
3117 } else if (list->sizeItems <= list->nbItems) {
3118 list->sizeItems *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003119 list->items = (void **) xmlRealloc(list->items,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003120 list->sizeItems * sizeof(xmlSchemaTypePtr));
3121 if (list->items == NULL) {
3122 xmlSchemaPErrMemory(NULL,
3123 "growing volatile item buffer", NULL);
3124 list->sizeItems = 0;
3125 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003126 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003127 }
3128 ((xmlSchemaBasicItemPtr *) list->items)[list->nbItems++] = (void *) item;
3129 return (0);
3130}
3131
3132/**
Daniel Veillard01fa6152004-06-29 17:04:39 +00003133 * xmlSchemaFreeTypeLinkList:
3134 * @alink: a type link
3135 *
3136 * Deallocate a list of types.
3137 */
3138static void
3139xmlSchemaFreeTypeLinkList(xmlSchemaTypeLinkPtr link)
3140{
3141 xmlSchemaTypeLinkPtr next;
3142
3143 while (link != NULL) {
3144 next = link->next;
3145 xmlFree(link);
3146 link = next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003147 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003148}
3149
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003150static void
3151xmlSchemaFreeIDCStateObjList(xmlSchemaIDCStateObjPtr sto)
3152{
3153 xmlSchemaIDCStateObjPtr next;
3154 while (sto != NULL) {
3155 next = sto->next;
3156 if (sto->history != NULL)
3157 xmlFree(sto->history);
3158 if (sto->xpathCtxt != NULL)
3159 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
3160 xmlFree(sto);
3161 sto = next;
3162 }
3163}
3164
3165/**
3166 * xmlSchemaFreeIDC:
3167 * @idc: a identity-constraint definition
3168 *
3169 * Deallocates an identity-constraint definition.
3170 */
3171static void
3172xmlSchemaFreeIDC(xmlSchemaIDCPtr idcDef)
3173{
3174 xmlSchemaIDCSelectPtr cur, prev;
3175
3176 if (idcDef == NULL)
3177 return;
3178 if (idcDef->annot != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003179 xmlSchemaFreeAnnot(idcDef->annot);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003180 /* Selector */
3181 if (idcDef->selector != NULL) {
3182 if (idcDef->selector->xpathComp != NULL)
3183 xmlFreePattern((xmlPatternPtr) idcDef->selector->xpathComp);
3184 xmlFree(idcDef->selector);
3185 }
3186 /* Fields */
3187 if (idcDef->fields != NULL) {
3188 cur = idcDef->fields;
3189 do {
3190 prev = cur;
3191 cur = cur->next;
3192 if (prev->xpathComp != NULL)
3193 xmlFreePattern((xmlPatternPtr) prev->xpathComp);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003194 xmlFree(prev);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003195 } while (cur != NULL);
3196 }
3197 xmlFree(idcDef);
3198}
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003199
Daniel Veillard01fa6152004-06-29 17:04:39 +00003200/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003201 * xmlSchemaFreeElement:
3202 * @schema: a schema element structure
3203 *
3204 * Deallocate a Schema Element structure.
3205 */
3206static void
3207xmlSchemaFreeElement(xmlSchemaElementPtr elem)
3208{
3209 if (elem == NULL)
3210 return;
Daniel Veillard32370232002-10-16 14:08:14 +00003211 if (elem->annot != NULL)
3212 xmlSchemaFreeAnnot(elem->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003213 if (elem->contModel != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003214 xmlRegFreeRegexp(elem->contModel);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003215 if (elem->defVal != NULL)
3216 xmlSchemaFreeValue(elem->defVal);
Daniel Veillard4255d502002-04-16 15:50:10 +00003217 xmlFree(elem);
3218}
3219
3220/**
3221 * xmlSchemaFreeFacet:
3222 * @facet: a schema facet structure
3223 *
3224 * Deallocate a Schema Facet structure.
3225 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003226void
Daniel Veillard4255d502002-04-16 15:50:10 +00003227xmlSchemaFreeFacet(xmlSchemaFacetPtr facet)
3228{
3229 if (facet == NULL)
3230 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003231 if (facet->val != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003232 xmlSchemaFreeValue(facet->val);
Daniel Veillard4255d502002-04-16 15:50:10 +00003233 if (facet->regexp != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003234 xmlRegFreeRegexp(facet->regexp);
Daniel Veillardfdc91562002-07-01 21:52:03 +00003235 if (facet->annot != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003236 xmlSchemaFreeAnnot(facet->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003237 xmlFree(facet);
3238}
3239
3240/**
3241 * xmlSchemaFreeType:
3242 * @type: a schema type structure
3243 *
3244 * Deallocate a Schema Type structure.
3245 */
3246void
3247xmlSchemaFreeType(xmlSchemaTypePtr type)
3248{
3249 if (type == NULL)
3250 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003251 if (type->annot != NULL)
Daniel Veillard32370232002-10-16 14:08:14 +00003252 xmlSchemaFreeAnnot(type->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003253 if (type->facets != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003254 xmlSchemaFacetPtr facet, next;
Daniel Veillard4255d502002-04-16 15:50:10 +00003255
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003256 facet = type->facets;
3257 while (facet != NULL) {
3258 next = facet->next;
3259 xmlSchemaFreeFacet(facet);
3260 facet = next;
3261 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003262 }
Daniel Veillard3646d642004-06-02 19:19:14 +00003263 if (type->type != XML_SCHEMA_TYPE_BASIC) {
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +00003264 /*
3265 * TODO: Why is this restricted to non built-in types?
3266 */
Daniel Veillard3646d642004-06-02 19:19:14 +00003267 if (type->attributeUses != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003268 xmlSchemaFreeAttributeUseList(type->attributeUses);
Daniel Veillard3646d642004-06-02 19:19:14 +00003269 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003270 if (type->memberTypes != NULL)
3271 xmlSchemaFreeTypeLinkList(type->memberTypes);
3272 if (type->facetSet != NULL) {
3273 xmlSchemaFacetLinkPtr next, link;
3274
3275 link = type->facetSet;
3276 do {
3277 next = link->next;
3278 xmlFree(link);
3279 link = next;
3280 } while (link != NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003281 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00003282 if (type->contModel != NULL)
3283 xmlRegFreeRegexp(type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +00003284 xmlFree(type);
3285}
3286
3287/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003288 * xmlSchemaFreeModelGroupDef:
3289 * @item: a schema model group definition
3290 *
3291 * Deallocates a schema model group definition.
3292 */
3293static void
3294xmlSchemaFreeModelGroupDef(xmlSchemaModelGroupDefPtr item)
3295{
3296 if (item->annot != NULL)
3297 xmlSchemaFreeAnnot(item->annot);
3298 xmlFree(item);
3299}
3300
3301/**
3302 * xmlSchemaFreeModelGroup:
3303 * @item: a schema model group
3304 *
3305 * Deallocates a schema model group structure.
3306 */
3307static void
3308xmlSchemaFreeModelGroup(xmlSchemaModelGroupPtr item)
3309{
3310 if (item->annot != NULL)
3311 xmlSchemaFreeAnnot(item->annot);
3312 xmlFree(item);
3313}
3314
3315/**
3316 * xmlSchemaFreeParticle:
3317 * @type: a schema type structure
3318 *
3319 * Deallocate a Schema Type structure.
3320 */
3321static void
3322xmlSchemaFreeParticle(xmlSchemaParticlePtr item)
3323{
3324 if (item->annot != NULL)
3325 xmlSchemaFreeAnnot(item->annot);
3326 xmlFree(item);
3327}
3328
3329/**
3330 * xmlSchemaFreeMiscComponents:
3331 * @item: a schema component
3332 *
3333 * Deallocates misc. schema component structures.
3334 */
3335static void
3336xmlSchemaFreeMiscComponents(xmlSchemaTreeItemPtr item)
3337{
3338 if (item == NULL)
3339 return;
3340 switch (item->type) {
3341 case XML_SCHEMA_TYPE_PARTICLE:
3342 xmlSchemaFreeParticle((xmlSchemaParticlePtr) item);
3343 return;
3344 case XML_SCHEMA_TYPE_SEQUENCE:
3345 case XML_SCHEMA_TYPE_CHOICE:
3346 case XML_SCHEMA_TYPE_ALL:
3347 xmlSchemaFreeModelGroup((xmlSchemaModelGroupPtr) item);
3348 return;
3349 case XML_SCHEMA_TYPE_ANY:
3350 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
3351 xmlSchemaFreeWildcard((xmlSchemaWildcardPtr) item);
3352 break;
3353 default:
3354 /* TODO: This should never be hit. */
3355 TODO
3356 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003357 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003358}
3359
3360static void
3361xmlSchemaFreeVolatiles(xmlSchemaPtr schema)
3362{
3363 if (schema->volatiles == NULL)
3364 return;
3365 {
3366 xmlSchemaItemListPtr list = (xmlSchemaItemListPtr) schema->volatiles;
3367 xmlSchemaTreeItemPtr item;
3368 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003369
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003370 for (i = 0; i < list->nbItems; i++) {
3371 if (list->items[i] != NULL) {
3372 item = (xmlSchemaTreeItemPtr) list->items[i];
3373 switch (item->type) {
3374 case XML_SCHEMA_EXTRA_QNAMEREF:
3375 xmlSchemaFreeQNameRef((xmlSchemaQNameRefPtr) item);
3376 break;
3377 default:
3378 xmlSchemaFreeMiscComponents(item);
3379 }
3380 }
3381 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003382 xmlSchemaFreeItemList(list);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003383 }
3384}
3385/**
Daniel Veillardb0f397e2003-12-23 23:30:53 +00003386 * xmlSchemaFreeTypeList:
3387 * @type: a schema type structure
3388 *
3389 * Deallocate a Schema Type structure.
3390 */
3391static void
3392xmlSchemaFreeTypeList(xmlSchemaTypePtr type)
3393{
3394 xmlSchemaTypePtr next;
3395
3396 while (type != NULL) {
3397 next = type->redef;
3398 xmlSchemaFreeType(type);
3399 type = next;
3400 }
3401}
3402
3403/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003404 * xmlSchemaFree:
3405 * @schema: a schema structure
3406 *
3407 * Deallocate a Schema structure.
3408 */
3409void
3410xmlSchemaFree(xmlSchemaPtr schema)
3411{
3412 if (schema == NULL)
3413 return;
3414
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003415 if (schema->volatiles != NULL)
3416 xmlSchemaFreeVolatiles(schema);
Daniel Veillard4255d502002-04-16 15:50:10 +00003417 if (schema->notaDecl != NULL)
3418 xmlHashFree(schema->notaDecl,
3419 (xmlHashDeallocator) xmlSchemaFreeNotation);
3420 if (schema->attrDecl != NULL)
3421 xmlHashFree(schema->attrDecl,
3422 (xmlHashDeallocator) xmlSchemaFreeAttribute);
3423 if (schema->attrgrpDecl != NULL)
3424 xmlHashFree(schema->attrgrpDecl,
3425 (xmlHashDeallocator) xmlSchemaFreeAttributeGroup);
3426 if (schema->elemDecl != NULL)
3427 xmlHashFree(schema->elemDecl,
3428 (xmlHashDeallocator) xmlSchemaFreeElement);
3429 if (schema->typeDecl != NULL)
3430 xmlHashFree(schema->typeDecl,
Daniel Veillardb0f397e2003-12-23 23:30:53 +00003431 (xmlHashDeallocator) xmlSchemaFreeTypeList);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00003432 if (schema->groupDecl != NULL)
3433 xmlHashFree(schema->groupDecl,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003434 (xmlHashDeallocator) xmlSchemaFreeModelGroupDef);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003435 if (schema->idcDef != NULL)
3436 xmlHashFree(schema->idcDef,
3437 (xmlHashDeallocator) xmlSchemaFreeIDC);
Daniel Veillard1d913862003-11-21 00:28:39 +00003438 if (schema->schemasImports != NULL)
3439 xmlHashFree(schema->schemasImports,
3440 (xmlHashDeallocator) xmlSchemaFreeImport);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00003441 if (schema->includes != NULL) {
3442 xmlSchemaFreeIncludeList((xmlSchemaIncludePtr) schema->includes);
3443 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003444 if (schema->annot != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003445 xmlSchemaFreeAnnot(schema->annot);
Daniel Veillarddda22c12004-01-24 08:31:30 +00003446 if (schema->doc != NULL && !schema->preserve)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003447 xmlFreeDoc(schema->doc);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003448 xmlDictFree(schema->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00003449 xmlFree(schema);
3450}
3451
3452/************************************************************************
3453 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00003454 * Debug functions *
3455 * *
3456 ************************************************************************/
3457
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003458#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003459
Daniel Veillard4255d502002-04-16 15:50:10 +00003460/**
3461 * xmlSchemaElementDump:
3462 * @elem: an element
3463 * @output: the file output
3464 *
3465 * Dump the element
3466 */
3467static void
3468xmlSchemaElementDump(xmlSchemaElementPtr elem, FILE * output,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003469 const xmlChar * name ATTRIBUTE_UNUSED,
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003470 const xmlChar * namespace ATTRIBUTE_UNUSED,
3471 const xmlChar * context ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00003472{
3473 if (elem == NULL)
3474 return;
3475
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003476 if (elem->flags & XML_SCHEMAS_ELEM_REF) {
3477 fprintf(output, "Particle: %s", name);
3478 fprintf(output, ", term element: %s", elem->ref);
3479 if (elem->refNs != NULL)
3480 fprintf(output, " ns %s", elem->refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003481 } else {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003482 fprintf(output, "Element");
3483 if (elem->flags & XML_SCHEMAS_ELEM_GLOBAL)
3484 fprintf(output, " (global)");
3485 fprintf(output, ": %s ", elem->name);
3486 if (namespace != NULL)
3487 fprintf(output, "ns %s", namespace);
3488 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003489 fprintf(output, "\n");
3490 if ((elem->minOccurs != 1) || (elem->maxOccurs != 1)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003491 fprintf(output, " min %d ", elem->minOccurs);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003492 if (elem->maxOccurs >= UNBOUNDED)
3493 fprintf(output, "max: unbounded\n");
3494 else if (elem->maxOccurs != 1)
3495 fprintf(output, "max: %d\n", elem->maxOccurs);
3496 else
3497 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00003498 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003499 /*
3500 * Misc other properties.
3501 */
3502 if ((elem->flags & XML_SCHEMAS_ELEM_NILLABLE) ||
3503 (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT) ||
3504 (elem->flags & XML_SCHEMAS_ELEM_FIXED) ||
3505 (elem->flags & XML_SCHEMAS_ELEM_DEFAULT) ||
3506 (elem->id != NULL)) {
3507 fprintf(output, " props: ");
3508 if (elem->flags & XML_SCHEMAS_ELEM_FIXED)
3509 fprintf(output, "[fixed] ");
3510 if (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)
3511 fprintf(output, "[default] ");
3512 if (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT)
3513 fprintf(output, "[abstract] ");
3514 if (elem->flags & XML_SCHEMAS_ELEM_NILLABLE)
3515 fprintf(output, "[nillable] ");
3516 if (elem->id != NULL)
3517 fprintf(output, "[id: '%s'] ", elem->id);
3518 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00003519 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003520 /*
3521 * Default/fixed value.
3522 */
Daniel Veillard4255d502002-04-16 15:50:10 +00003523 if (elem->value != NULL)
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003524 fprintf(output, " value: '%s'\n", elem->value);
3525 /*
3526 * Type.
3527 */
3528 if (elem->namedType != NULL) {
3529 fprintf(output, " type: %s ", elem->namedType);
3530 if (elem->namedTypeNs != NULL)
3531 fprintf(output, "ns %s\n", elem->namedTypeNs);
3532 else
3533 fprintf(output, "\n");
3534 }
3535 /*
3536 * Substitution group.
3537 */
3538 if (elem->substGroup != NULL) {
3539 fprintf(output, " substitutionGroup: %s ", elem->substGroup);
3540 if (elem->substGroupNs != NULL)
3541 fprintf(output, "ns %s\n", elem->substGroupNs);
3542 else
3543 fprintf(output, "\n");
3544 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003545}
3546
3547/**
3548 * xmlSchemaAnnotDump:
3549 * @output: the file output
3550 * @annot: a annotation
3551 *
3552 * Dump the annotation
3553 */
3554static void
3555xmlSchemaAnnotDump(FILE * output, xmlSchemaAnnotPtr annot)
3556{
3557 xmlChar *content;
3558
3559 if (annot == NULL)
3560 return;
3561
3562 content = xmlNodeGetContent(annot->content);
3563 if (content != NULL) {
3564 fprintf(output, " Annot: %s\n", content);
3565 xmlFree(content);
3566 } else
3567 fprintf(output, " Annot: empty\n");
3568}
3569
3570/**
3571 * xmlSchemaTypeDump:
3572 * @output: the file output
3573 * @type: a type structure
3574 *
3575 * Dump a SchemaType structure
3576 */
3577static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003578xmlSchemaContentModelDump(xmlSchemaParticlePtr particle, FILE * output, int depth)
3579{
3580 xmlChar *str = NULL;
3581 xmlSchemaTreeItemPtr term;
3582 char shift[100];
3583 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003584
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003585 if (particle == NULL)
3586 return;
3587 for (i = 0;((i < depth) && (i < 25));i++)
3588 shift[2 * i] = shift[2 * i + 1] = ' ';
3589 shift[2 * i] = shift[2 * i + 1] = 0;
3590 fprintf(output, shift);
3591 if (particle->children == NULL) {
3592 fprintf(output, "MISSING particle term\n");
3593 return;
3594 }
3595 term = particle->children;
3596 switch (term->type) {
3597 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003598 fprintf(output, "ELEM '%s'", xmlSchemaFormatQName(&str,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003599 ((xmlSchemaElementPtr)term)->targetNamespace,
3600 ((xmlSchemaElementPtr)term)->name));
3601 break;
3602 case XML_SCHEMA_TYPE_SEQUENCE:
3603 fprintf(output, "SEQUENCE");
3604 break;
3605 case XML_SCHEMA_TYPE_CHOICE:
3606 fprintf(output, "CHOICE");
3607 break;
3608 case XML_SCHEMA_TYPE_ALL:
3609 fprintf(output, "ALL");
3610 break;
3611 case XML_SCHEMA_TYPE_ANY:
3612 fprintf(output, "ANY");
3613 break;
3614 default:
3615 fprintf(output, "UNKNOWN\n");
3616 return;
3617 }
3618 if (particle->minOccurs != 1)
3619 fprintf(output, " min: %d", particle->minOccurs);
3620 if (particle->maxOccurs >= UNBOUNDED)
3621 fprintf(output, " max: unbounded");
3622 else if (particle->maxOccurs != 1)
3623 fprintf(output, " max: %d", particle->maxOccurs);
3624 fprintf(output, "\n");
3625 if (((term->type == XML_SCHEMA_TYPE_SEQUENCE) ||
3626 (term->type == XML_SCHEMA_TYPE_CHOICE) ||
3627 (term->type == XML_SCHEMA_TYPE_ALL)) &&
3628 (term->children != NULL)) {
3629 xmlSchemaContentModelDump((xmlSchemaParticlePtr) term->children,
3630 output, depth +1);
3631 }
3632 if (particle->next != NULL)
3633 xmlSchemaContentModelDump((xmlSchemaParticlePtr) particle->next,
3634 output, depth);
3635}
3636/**
3637 * xmlSchemaTypeDump:
3638 * @output: the file output
3639 * @type: a type structure
3640 *
3641 * Dump a SchemaType structure
3642 */
3643static void
Daniel Veillard4255d502002-04-16 15:50:10 +00003644xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output)
3645{
3646 if (type == NULL) {
3647 fprintf(output, "Type: NULL\n");
3648 return;
3649 }
3650 fprintf(output, "Type: ");
3651 if (type->name != NULL)
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003652 fprintf(output, "%s ", type->name);
Daniel Veillard4255d502002-04-16 15:50:10 +00003653 else
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003654 fprintf(output, "no name ");
3655 if (type->targetNamespace != NULL)
3656 fprintf(output, "ns %s ", type->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00003657 switch (type->type) {
3658 case XML_SCHEMA_TYPE_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003659 fprintf(output, "[basic] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00003660 break;
3661 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003662 fprintf(output, "[simple] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00003663 break;
3664 case XML_SCHEMA_TYPE_COMPLEX:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003665 fprintf(output, "[complex] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00003666 break;
3667 case XML_SCHEMA_TYPE_SEQUENCE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003668 fprintf(output, "[sequence] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00003669 break;
3670 case XML_SCHEMA_TYPE_CHOICE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003671 fprintf(output, "[choice] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00003672 break;
3673 case XML_SCHEMA_TYPE_ALL:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003674 fprintf(output, "[all] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00003675 break;
3676 case XML_SCHEMA_TYPE_UR:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003677 fprintf(output, "[ur] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00003678 break;
3679 case XML_SCHEMA_TYPE_RESTRICTION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003680 fprintf(output, "[restriction] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00003681 break;
3682 case XML_SCHEMA_TYPE_EXTENSION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003683 fprintf(output, "[extension] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00003684 break;
3685 default:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003686 fprintf(output, "[unknown type %d] ", type->type);
Daniel Veillard4255d502002-04-16 15:50:10 +00003687 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003688 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003689 fprintf(output, "content: ");
Daniel Veillard4255d502002-04-16 15:50:10 +00003690 switch (type->contentType) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003691 case XML_SCHEMA_CONTENT_UNKNOWN:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003692 fprintf(output, "[unknown] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003693 break;
3694 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003695 fprintf(output, "[empty] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003696 break;
3697 case XML_SCHEMA_CONTENT_ELEMENTS:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003698 fprintf(output, "[element] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003699 break;
3700 case XML_SCHEMA_CONTENT_MIXED:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003701 fprintf(output, "[mixed] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003702 break;
3703 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00003704 /* not used. */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003705 break;
3706 case XML_SCHEMA_CONTENT_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003707 fprintf(output, "[basic] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003708 break;
3709 case XML_SCHEMA_CONTENT_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003710 fprintf(output, "[simple] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003711 break;
3712 case XML_SCHEMA_CONTENT_ANY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003713 fprintf(output, "[any] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003714 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00003715 }
3716 fprintf(output, "\n");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00003717 if (type->base != NULL) {
3718 fprintf(output, " base type: %s", type->base);
3719 if (type->baseNs != NULL)
3720 fprintf(output, " ns %s\n", type->baseNs);
3721 else
3722 fprintf(output, "\n");
3723 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003724 if (type->annot != NULL)
3725 xmlSchemaAnnotDump(output, type->annot);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003726#ifdef DUMP_CONTENT_MODEL
3727 if ((type->type == XML_SCHEMA_TYPE_COMPLEX) &&
3728 (type->subtypes != NULL)) {
3729 xmlSchemaContentModelDump((xmlSchemaParticlePtr) type->subtypes,
3730 output, 1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003731 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003732#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003733}
3734
3735/**
3736 * xmlSchemaDump:
3737 * @output: the file output
3738 * @schema: a schema structure
3739 *
3740 * Dump a Schema structure.
3741 */
3742void
3743xmlSchemaDump(FILE * output, xmlSchemaPtr schema)
3744{
Daniel Veillardce682bc2004-11-05 17:22:25 +00003745 if (output == NULL)
3746 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003747 if (schema == NULL) {
3748 fprintf(output, "Schemas: NULL\n");
3749 return;
3750 }
3751 fprintf(output, "Schemas: ");
3752 if (schema->name != NULL)
3753 fprintf(output, "%s, ", schema->name);
3754 else
3755 fprintf(output, "no name, ");
3756 if (schema->targetNamespace != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00003757 fprintf(output, "%s", (const char *) schema->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00003758 else
3759 fprintf(output, "no target namespace");
3760 fprintf(output, "\n");
3761 if (schema->annot != NULL)
3762 xmlSchemaAnnotDump(output, schema->annot);
3763
3764 xmlHashScan(schema->typeDecl, (xmlHashScanner) xmlSchemaTypeDump,
3765 output);
3766 xmlHashScanFull(schema->elemDecl,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003767 (xmlHashScannerFull) xmlSchemaElementDump, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00003768}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00003769
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00003770#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00003771/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003772 * xmlSchemaDebugDumpIDCTable:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00003773 * @vctxt: the WXS validation context
3774 *
3775 * Displays the current IDC table for debug purposes.
3776 */
3777static void
3778xmlSchemaDebugDumpIDCTable(FILE * output,
3779 const xmlChar *namespaceName,
3780 const xmlChar *localName,
3781 xmlSchemaPSVIIDCBindingPtr bind)
3782{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003783 xmlChar *str = NULL, *value;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00003784 xmlSchemaPSVIIDCNodePtr tab;
3785 xmlSchemaPSVIIDCKeyPtr key;
3786 int i, j, res;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003787
3788 fprintf(output, "IDC: TABLES on %s\n",
3789 xmlSchemaFormatQName(&str, namespaceName, localName));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00003790 FREE_AND_NULL(str)
3791
3792 if (bind == NULL)
3793 return;
3794 do {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003795 fprintf(output, "IDC: BINDING %s\n",
3796 xmlSchemaFormatQName(&str, bind->definition->targetNamespace,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00003797 bind->definition->name));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003798 FREE_AND_NULL(str)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00003799 for (i = 0; i < bind->nbNodes; i++) {
3800 tab = bind->nodeTable[i];
3801 fprintf(output, " ( ");
3802 for (j = 0; j < bind->definition->nbFields; j++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003803 key = tab->keys[j];
3804 if ((key != NULL) && (key->val != NULL)) {
3805 res = xmlSchemaGetCanonValue(key->val, &value);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003806 if (res >= 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00003807 fprintf(output, "\"%s\" ", value);
3808 else
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00003809 fprintf(output, "CANON-VALUE-FAILED ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003810 if (res == 0)
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003811 FREE_AND_NULL(value)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00003812 } else if (key != NULL)
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00003813 fprintf(output, "(no val), ");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00003814 else
3815 fprintf(output, "(key missing), ");
3816 }
3817 fprintf(output, ")\n");
3818 }
3819 bind = bind->next;
3820 } while (bind != NULL);
3821}
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00003822#endif /* DEBUG_IDC */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003823#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard4255d502002-04-16 15:50:10 +00003824
3825/************************************************************************
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003826 * *
3827 * Utilities *
3828 * *
3829 ************************************************************************/
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003830
Daniel Veillardc0826a72004-08-10 14:17:33 +00003831/**
3832 * xmlSchemaGetPropNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003833 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00003834 * @name: the name of the attribute
3835 *
3836 * Seeks an attribute with a name of @name in
3837 * no namespace.
3838 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003839 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00003840 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00003841static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003842xmlSchemaGetPropNode(xmlNodePtr node, const char *name)
Daniel Veillard01fa6152004-06-29 17:04:39 +00003843{
3844 xmlAttrPtr prop;
3845
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003846 if ((node == NULL) || (name == NULL))
Daniel Veillardc0826a72004-08-10 14:17:33 +00003847 return(NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +00003848 prop = node->properties;
3849 while (prop != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003850 if ((prop->ns == NULL) && xmlStrEqual(prop->name, BAD_CAST name))
Daniel Veillardc0826a72004-08-10 14:17:33 +00003851 return(prop);
3852 prop = prop->next;
3853 }
3854 return (NULL);
3855}
3856
3857/**
3858 * xmlSchemaGetPropNodeNs:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003859 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00003860 * @uri: the uri
3861 * @name: the name of the attribute
3862 *
3863 * Seeks an attribute with a local name of @name and
3864 * a namespace URI of @uri.
3865 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003866 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00003867 */
3868static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003869xmlSchemaGetPropNodeNs(xmlNodePtr node, const char *uri, const char *name)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003870{
3871 xmlAttrPtr prop;
3872
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003873 if ((node == NULL) || (name == NULL))
Daniel Veillardc0826a72004-08-10 14:17:33 +00003874 return(NULL);
3875 prop = node->properties;
3876 while (prop != NULL) {
3877 if ((prop->ns != NULL) &&
3878 xmlStrEqual(prop->name, BAD_CAST name) &&
3879 xmlStrEqual(prop->ns->href, BAD_CAST uri))
Daniel Veillard01fa6152004-06-29 17:04:39 +00003880 return(prop);
3881 prop = prop->next;
3882 }
3883 return (NULL);
3884}
3885
3886static const xmlChar *
3887xmlSchemaGetNodeContent(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
3888{
3889 xmlChar *val;
3890 const xmlChar *ret;
3891
3892 val = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003893 if (val == NULL)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00003894 val = xmlStrdup((xmlChar *)"");
Daniel Veillard01fa6152004-06-29 17:04:39 +00003895 ret = xmlDictLookup(ctxt->dict, val, -1);
3896 xmlFree(val);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003897 return(ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +00003898}
3899
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003900/**
3901 * xmlSchemaGetProp:
3902 * @ctxt: the parser context
3903 * @node: the node
3904 * @name: the property name
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003905 *
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003906 * Read a attribute value and internalize the string
3907 *
3908 * Returns the string or NULL if not present.
3909 */
3910static const xmlChar *
3911xmlSchemaGetProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
3912 const char *name)
3913{
3914 xmlChar *val;
3915 const xmlChar *ret;
3916
3917 val = xmlGetProp(node, BAD_CAST name);
3918 if (val == NULL)
3919 return(NULL);
3920 ret = xmlDictLookup(ctxt->dict, val, -1);
3921 xmlFree(val);
3922 return(ret);
3923}
3924
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003925/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +00003926 * *
3927 * Parsing functions *
3928 * *
3929 ************************************************************************/
3930
3931/**
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003932 * xmlSchemaGetElem:
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00003933 * @schema: the schema context
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003934 * @name: the element name
3935 * @ns: the element namespace
3936 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00003937 * Lookup a global element declaration in the schema.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003938 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00003939 * Returns the element declaration or NULL if not found.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003940 */
3941static xmlSchemaElementPtr
3942xmlSchemaGetElem(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00003943 const xmlChar * namespace)
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003944{
3945 xmlSchemaElementPtr ret;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003946
3947 if ((name == NULL) || (schema == NULL))
3948 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003949
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003950 ret = xmlHashLookup2(schema->elemDecl, name, namespace);
Daniel Veillardf2a12832003-11-24 13:04:35 +00003951 if ((ret != NULL) &&
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00003952 (ret->flags & XML_SCHEMAS_ELEM_GLOBAL)) {
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003953 return (ret);
Daniel Veillard3646d642004-06-02 19:19:14 +00003954 } else
3955 ret = NULL;
William M. Bracke7091952004-05-11 15:09:58 +00003956 /*
3957 * This one was removed, since top level element declarations have
3958 * the target namespace specified in targetNamespace of the <schema>
3959 * information element, even if elementFormDefault is "unqualified".
3960 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003961
William M. Bracke7091952004-05-11 15:09:58 +00003962 /* else if ((schema->flags & XML_SCHEMAS_QUALIF_ELEM) == 0) {
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003963 if (xmlStrEqual(namespace, schema->targetNamespace))
3964 ret = xmlHashLookup2(schema->elemDecl, name, NULL);
3965 else
3966 ret = xmlHashLookup2(schema->elemDecl, name, namespace);
Daniel Veillardf2a12832003-11-24 13:04:35 +00003967 if ((ret != NULL) &&
3968 ((level == 0) || (ret->flags & XML_SCHEMAS_ELEM_TOPLEVEL))) {
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003969 return (ret);
Daniel Veillardf2a12832003-11-24 13:04:35 +00003970 }
William M. Bracke7091952004-05-11 15:09:58 +00003971 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003972
William M. Brack2f2a6632004-08-20 23:09:47 +00003973 /*
3974 * Removed since imported components will be hold by the main schema only.
3975 *
Daniel Veillard3646d642004-06-02 19:19:14 +00003976 if (namespace == NULL)
William M. Brack2f2a6632004-08-20 23:09:47 +00003977 import = xmlHashLookup(schema->schemasImports, XML_SCHEMAS_NO_NAMESPACE);
Daniel Veillard3646d642004-06-02 19:19:14 +00003978 else
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003979 import = xmlHashLookup(schema->schemasImports, namespace);
Daniel Veillard3646d642004-06-02 19:19:14 +00003980 if (import != NULL) {
Daniel Veillardf2a12832003-11-24 13:04:35 +00003981 ret = xmlSchemaGetElem(import->schema, name, namespace, level + 1);
Daniel Veillard3646d642004-06-02 19:19:14 +00003982 if ((ret != NULL) && (ret->flags & XML_SCHEMAS_ELEM_GLOBAL)) {
3983 return (ret);
3984 } else
3985 ret = NULL;
3986 }
William M. Brack2f2a6632004-08-20 23:09:47 +00003987 */
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003988#ifdef DEBUG
3989 if (ret == NULL) {
3990 if (namespace == NULL)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003991 fprintf(stderr, "Unable to lookup element decl. %s", name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003992 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003993 fprintf(stderr, "Unable to lookup element decl. %s:%s", name,
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003994 namespace);
3995 }
3996#endif
3997 return (ret);
3998}
3999
4000/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004001 * xmlSchemaGetType:
4002 * @schema: the schemas context
4003 * @name: the type name
4004 * @ns: the type namespace
4005 *
4006 * Lookup a type in the schemas or the predefined types
4007 *
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004008 * Returns the group definition or NULL if not found.
Daniel Veillard4255d502002-04-16 15:50:10 +00004009 */
4010static xmlSchemaTypePtr
4011xmlSchemaGetType(xmlSchemaPtr schema, const xmlChar * name,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004012 const xmlChar * namespace)
4013{
Daniel Veillard4255d502002-04-16 15:50:10 +00004014 xmlSchemaTypePtr ret;
4015
4016 if (name == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004017 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004018 if (schema != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004019 ret = xmlHashLookup2(schema->typeDecl, name, namespace);
Daniel Veillard3646d642004-06-02 19:19:14 +00004020 if ((ret != NULL) && (ret->flags & XML_SCHEMAS_TYPE_GLOBAL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004021 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00004022 }
4023 ret = xmlSchemaGetPredefinedType(name, namespace);
Daniel Veillard1d913862003-11-21 00:28:39 +00004024 if (ret != NULL)
4025 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +00004026 /*
4027 * Removed, since the imported components will be grafted on the
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004028 * main schema only.
Daniel Veillard3646d642004-06-02 19:19:14 +00004029 if (namespace == NULL)
William M. Brack2f2a6632004-08-20 23:09:47 +00004030 import = xmlHashLookup(schema->schemasImports, XML_SCHEMAS_NO_NAMESPACE);
Daniel Veillard3646d642004-06-02 19:19:14 +00004031 else
Daniel Veillard1d913862003-11-21 00:28:39 +00004032 import = xmlHashLookup(schema->schemasImports, namespace);
Daniel Veillard3646d642004-06-02 19:19:14 +00004033 if (import != NULL) {
Daniel Veillard1d913862003-11-21 00:28:39 +00004034 ret = xmlSchemaGetType(import->schema, name, namespace);
Daniel Veillard3646d642004-06-02 19:19:14 +00004035 if ((ret != NULL) && (ret->flags & XML_SCHEMAS_TYPE_GLOBAL)) {
4036 return (ret);
4037 } else
4038 ret = NULL;
4039 }
William M. Brack2f2a6632004-08-20 23:09:47 +00004040 */
Daniel Veillard4255d502002-04-16 15:50:10 +00004041#ifdef DEBUG
4042 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004043 if (namespace == NULL)
4044 fprintf(stderr, "Unable to lookup type %s", name);
4045 else
4046 fprintf(stderr, "Unable to lookup type %s:%s", name,
4047 namespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004048 }
4049#endif
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004050 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00004051}
4052
Daniel Veillard3646d642004-06-02 19:19:14 +00004053/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004054 * xmlSchemaGetAttributeDecl:
4055 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004056 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004057 * @ns: the target namespace of the attribute
Daniel Veillard3646d642004-06-02 19:19:14 +00004058 *
4059 * Lookup a an attribute in the schema or imported schemas
4060 *
4061 * Returns the attribute declaration or NULL if not found.
4062 */
4063static xmlSchemaAttributePtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004064xmlSchemaGetAttributeDecl(xmlSchemaPtr schema, const xmlChar * name,
Daniel Veillard3646d642004-06-02 19:19:14 +00004065 const xmlChar * namespace)
4066{
4067 xmlSchemaAttributePtr ret;
Daniel Veillard3646d642004-06-02 19:19:14 +00004068
4069 if ((name == NULL) || (schema == NULL))
4070 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004071
4072
Daniel Veillard3646d642004-06-02 19:19:14 +00004073 ret = xmlHashLookup2(schema->attrDecl, name, namespace);
4074 if ((ret != NULL) && (ret->flags & XML_SCHEMAS_ATTR_GLOBAL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004075 return (ret);
Daniel Veillard3646d642004-06-02 19:19:14 +00004076 else
4077 ret = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00004078 /*
4079 * Removed, since imported components will be hold by the main schema only.
4080 *
Daniel Veillard3646d642004-06-02 19:19:14 +00004081 if (namespace == NULL)
William M. Brack2f2a6632004-08-20 23:09:47 +00004082 import = xmlHashLookup(schema->schemasImports, XML_SCHEMAS_NO_NAMESPACE);
Daniel Veillard3646d642004-06-02 19:19:14 +00004083 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004084 import = xmlHashLookup(schema->schemasImports, namespace);
Daniel Veillard3646d642004-06-02 19:19:14 +00004085 if (import != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004086 ret = xmlSchemaGetAttributeDecl(import->schema, name, namespace);
Daniel Veillard3646d642004-06-02 19:19:14 +00004087 if ((ret != NULL) && (ret->flags & XML_SCHEMAS_ATTR_GLOBAL)) {
4088 return (ret);
4089 } else
4090 ret = NULL;
4091 }
William M. Brack2f2a6632004-08-20 23:09:47 +00004092 */
Daniel Veillard3646d642004-06-02 19:19:14 +00004093#ifdef DEBUG
4094 if (ret == NULL) {
4095 if (namespace == NULL)
4096 fprintf(stderr, "Unable to lookup attribute %s", name);
4097 else
4098 fprintf(stderr, "Unable to lookup attribute %s:%s", name,
4099 namespace);
4100 }
4101#endif
4102 return (ret);
4103}
4104
4105/**
4106 * xmlSchemaGetAttributeGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004107 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004108 * @name: the name of the attribute group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004109 * @ns: the target namespace of the attribute group
Daniel Veillard3646d642004-06-02 19:19:14 +00004110 *
4111 * Lookup a an attribute group in the schema or imported schemas
4112 *
4113 * Returns the attribute group definition or NULL if not found.
4114 */
4115static xmlSchemaAttributeGroupPtr
4116xmlSchemaGetAttributeGroup(xmlSchemaPtr schema, const xmlChar * name,
4117 const xmlChar * namespace)
4118{
4119 xmlSchemaAttributeGroupPtr ret;
Daniel Veillard3646d642004-06-02 19:19:14 +00004120
4121 if ((name == NULL) || (schema == NULL))
4122 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004123
4124
Daniel Veillard3646d642004-06-02 19:19:14 +00004125 ret = xmlHashLookup2(schema->attrgrpDecl, name, namespace);
4126 if ((ret != NULL) && (ret->flags & XML_SCHEMAS_ATTRGROUP_GLOBAL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004127 return (ret);
Daniel Veillard3646d642004-06-02 19:19:14 +00004128 else
4129 ret = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00004130 /*
4131 * Removed since imported components will be hold by the main schema only.
4132 *
Daniel Veillard3646d642004-06-02 19:19:14 +00004133 if (namespace == NULL)
William M. Brack2f2a6632004-08-20 23:09:47 +00004134 import = xmlHashLookup(schema->schemasImports, XML_SCHEMAS_NO_NAMESPACE);
Daniel Veillard3646d642004-06-02 19:19:14 +00004135 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004136 import = xmlHashLookup(schema->schemasImports, namespace);
Daniel Veillard3646d642004-06-02 19:19:14 +00004137 if (import != NULL) {
4138 ret = xmlSchemaGetAttributeGroup(import->schema, name, namespace);
4139 if ((ret != NULL) && (ret->flags & XML_SCHEMAS_ATTRGROUP_GLOBAL))
4140 return (ret);
4141 else
4142 ret = NULL;
4143 }
William M. Brack2f2a6632004-08-20 23:09:47 +00004144 */
Daniel Veillard3646d642004-06-02 19:19:14 +00004145#ifdef DEBUG
4146 if (ret == NULL) {
4147 if (namespace == NULL)
4148 fprintf(stderr, "Unable to lookup attribute group %s", name);
4149 else
4150 fprintf(stderr, "Unable to lookup attribute group %s:%s", name,
4151 namespace);
4152 }
4153#endif
4154 return (ret);
4155}
4156
4157/**
4158 * xmlSchemaGetGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004159 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004160 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004161 * @ns: the target namespace of the group
Daniel Veillard3646d642004-06-02 19:19:14 +00004162 *
4163 * Lookup a group in the schema or imported schemas
4164 *
4165 * Returns the group definition or NULL if not found.
4166 */
4167static xmlSchemaTypePtr
4168xmlSchemaGetGroup(xmlSchemaPtr schema, const xmlChar * name,
4169 const xmlChar * namespace)
4170{
4171 xmlSchemaTypePtr ret;
Daniel Veillard3646d642004-06-02 19:19:14 +00004172
4173 if ((name == NULL) || (schema == NULL))
4174 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004175
Daniel Veillard3646d642004-06-02 19:19:14 +00004176 ret = xmlHashLookup2(schema->groupDecl, name, namespace);
William M. Brack2f2a6632004-08-20 23:09:47 +00004177 /*
4178 * Removed since imported components will be hold by the main schema only.
4179 *
Daniel Veillard3646d642004-06-02 19:19:14 +00004180 if (namespace == NULL)
William M. Brack2f2a6632004-08-20 23:09:47 +00004181 import = xmlHashLookup(schema->schemasImports, XML_SCHEMAS_NO_NAMESPACE);
Daniel Veillard3646d642004-06-02 19:19:14 +00004182 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004183 import = xmlHashLookup(schema->schemasImports, namespace);
Daniel Veillard3646d642004-06-02 19:19:14 +00004184 if (import != NULL) {
4185 ret = xmlSchemaGetGroup(import->schema, name, namespace);
4186 if ((ret != NULL) && (ret->flags & XML_SCHEMAS_TYPE_GLOBAL))
4187 return (ret);
4188 else
4189 ret = NULL;
4190 }
William M. Brack2f2a6632004-08-20 23:09:47 +00004191 */
Daniel Veillard3646d642004-06-02 19:19:14 +00004192#ifdef DEBUG
4193 if (ret == NULL) {
4194 if (namespace == NULL)
4195 fprintf(stderr, "Unable to lookup group %s", name);
4196 else
4197 fprintf(stderr, "Unable to lookup group %s:%s", name,
4198 namespace);
4199 }
4200#endif
4201 return (ret);
4202}
4203
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004204/**
4205 * xmlSchemaGetNamedComponent:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004206 * @schema: the schema
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004207 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004208 * @ns: the target namespace of the group
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004209 *
4210 * Lookup a group in the schema or imported schemas
4211 *
4212 * Returns the group definition or NULL if not found.
4213 */
4214static xmlSchemaTreeItemPtr
4215xmlSchemaGetNamedComponent(xmlSchemaPtr schema,
4216 xmlSchemaTypeType itemType,
4217 const xmlChar *name,
4218 const xmlChar *targetNs)
4219{
4220 switch (itemType) {
4221 case XML_SCHEMA_TYPE_GROUP:
4222 return ((xmlSchemaTreeItemPtr) xmlSchemaGetGroup(schema,
4223 name, targetNs));
4224 case XML_SCHEMA_TYPE_ELEMENT:
4225 return ((xmlSchemaTreeItemPtr) xmlSchemaGetElem(schema,
4226 name, targetNs));
4227 default:
4228 return (NULL);
4229 }
4230}
4231
Daniel Veillard4255d502002-04-16 15:50:10 +00004232/************************************************************************
4233 * *
4234 * Parsing functions *
4235 * *
4236 ************************************************************************/
4237
4238#define IS_BLANK_NODE(n) \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004239 (((n)->type == XML_TEXT_NODE) && (xmlSchemaIsBlank((n)->content, -1)))
Daniel Veillard4255d502002-04-16 15:50:10 +00004240
4241/**
4242 * xmlSchemaIsBlank:
4243 * @str: a string
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004244 * @len: the length of the string or -1
Daniel Veillard4255d502002-04-16 15:50:10 +00004245 *
4246 * Check if a string is ignorable
4247 *
4248 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
4249 */
4250static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004251xmlSchemaIsBlank(xmlChar * str, int len)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004252{
Daniel Veillard4255d502002-04-16 15:50:10 +00004253 if (str == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004254 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004255 if (len < 0) {
4256 while (*str != 0) {
4257 if (!(IS_BLANK_CH(*str)))
4258 return (0);
4259 str++;
4260 }
4261 } else while ((*str != 0) && (len != 0)) {
4262 if (!(IS_BLANK_CH(*str)))
4263 return (0);
4264 str++;
4265 len--;
Daniel Veillard4255d502002-04-16 15:50:10 +00004266 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004267
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004268 return (1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004269}
4270
4271/**
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004272 * xmlSchemaAddAssembledItem:
4273 * @ctxt: a schema parser context
4274 * @schema: the schema being built
4275 * @item: the item
4276 *
4277 * Add a item to the schema's list of current items.
4278 * This is used if the schema was already constructed and
4279 * new schemata need to be added to it.
4280 * *WARNING* this interface is highly subject to change.
4281 *
4282 * Returns 0 if suceeds and -1 if an internal error occurs.
4283 */
4284static int
4285xmlSchemaAddAssembledItem(xmlSchemaParserCtxtPtr ctxt,
4286 xmlSchemaTypePtr item)
4287{
4288 static int growSize = 100;
4289 xmlSchemaAssemblePtr ass;
4290
4291 ass = ctxt->assemble;
4292 if (ass->sizeItems < 0) {
4293 /* If disabled. */
4294 return (0);
4295 }
4296 if (ass->sizeItems <= 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00004297 ass->items = (void **) xmlMalloc(growSize * sizeof(xmlSchemaTypePtr));
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004298 if (ass->items == NULL) {
4299 xmlSchemaPErrMemory(ctxt,
4300 "allocating new item buffer", NULL);
4301 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004302 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004303 ass->sizeItems = growSize;
4304 } else if (ass->sizeItems <= ass->nbItems) {
4305 ass->sizeItems *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004306 ass->items = (void **) xmlRealloc(ass->items,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004307 ass->sizeItems * sizeof(xmlSchemaTypePtr));
4308 if (ass->items == NULL) {
4309 xmlSchemaPErrMemory(ctxt,
4310 "growing item buffer", NULL);
4311 ass->sizeItems = 0;
4312 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004313 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004314 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00004315 /* ass->items[ass->nbItems++] = (void *) item; */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004316 ((xmlSchemaTypePtr *) ass->items)[ass->nbItems++] = (void *) item;
4317 return (0);
4318}
4319
4320/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004321 * xmlSchemaAddNotation:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004322 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004323 * @schema: the schema being built
4324 * @name: the item name
4325 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00004326 * Add an XML schema annotation declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00004327 * *WARNING* this interface is highly subject to change
4328 *
4329 * Returns the new struture or NULL in case of error
4330 */
4331static xmlSchemaNotationPtr
4332xmlSchemaAddNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00004333 const xmlChar *name)
Daniel Veillard4255d502002-04-16 15:50:10 +00004334{
4335 xmlSchemaNotationPtr ret = NULL;
4336 int val;
4337
4338 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
4339 return (NULL);
4340
4341 if (schema->notaDecl == NULL)
Daniel Veillardb9e2f262005-01-23 23:48:34 +00004342 schema->notaDecl = xmlHashCreateDict(10, ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00004343 if (schema->notaDecl == NULL)
4344 return (NULL);
4345
4346 ret = (xmlSchemaNotationPtr) xmlMalloc(sizeof(xmlSchemaNotation));
4347 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004348 xmlSchemaPErrMemory(ctxt, "add annotation", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004349 return (NULL);
4350 }
4351 memset(ret, 0, sizeof(xmlSchemaNotation));
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004352 ret->name = xmlDictLookup(ctxt->dict, name, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004353 val = xmlHashAddEntry2(schema->notaDecl, name, schema->targetNamespace,
4354 ret);
4355 if (val != 0) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00004356 /*
4357 * TODO: This should never happen, since a unique name will be computed.
4358 * If it fails, then an other internal error must have occured.
4359 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004360 xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc,
4361 XML_SCHEMAP_REDEFINED_NOTATION,
Daniel Veillardc0826a72004-08-10 14:17:33 +00004362 "Annotation declaration '%s' is already declared.\n",
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004363 name, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004364 xmlFree(ret);
4365 return (NULL);
4366 }
4367 return (ret);
4368}
4369
4370
4371/**
4372 * xmlSchemaAddAttribute:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004373 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004374 * @schema: the schema being built
4375 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004376 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00004377 *
4378 * Add an XML schema Attrribute declaration
4379 * *WARNING* this interface is highly subject to change
4380 *
4381 * Returns the new struture or NULL in case of error
4382 */
4383static xmlSchemaAttributePtr
4384xmlSchemaAddAttribute(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
William M. Brack2f2a6632004-08-20 23:09:47 +00004385 const xmlChar * name, const xmlChar * namespace,
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004386 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00004387{
4388 xmlSchemaAttributePtr ret = NULL;
4389 int val;
4390
4391 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
4392 return (NULL);
4393
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004394#ifdef DEBUG
4395 fprintf(stderr, "Adding attribute %s\n", name);
4396 if (namespace != NULL)
4397 fprintf(stderr, " target namespace %s\n", namespace);
4398#endif
4399
Daniel Veillard4255d502002-04-16 15:50:10 +00004400 if (schema->attrDecl == NULL)
Daniel Veillardb9e2f262005-01-23 23:48:34 +00004401 schema->attrDecl = xmlHashCreateDict(10, ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00004402 if (schema->attrDecl == NULL)
4403 return (NULL);
4404
4405 ret = (xmlSchemaAttributePtr) xmlMalloc(sizeof(xmlSchemaAttribute));
4406 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004407 xmlSchemaPErrMemory(ctxt, "allocating attribute", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004408 return (NULL);
4409 }
4410 memset(ret, 0, sizeof(xmlSchemaAttribute));
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004411 ret->name = xmlDictLookup(ctxt->dict, name, -1);
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00004412 ret->targetNamespace = namespace;
Daniel Veillard4255d502002-04-16 15:50:10 +00004413 val = xmlHashAddEntry3(schema->attrDecl, name,
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00004414 namespace, ctxt->container, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00004415 if (val != 0) {
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004416 if (topLevel) {
4417 xmlSchemaPCustomErr(ctxt,
4418 XML_SCHEMAP_REDEFINED_ATTR,
4419 NULL, NULL, node,
4420 "A global attribute declaration with the name '%s' does "
4421 "already exist", name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004422 xmlFree(ret);
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004423 return (NULL);
4424 } else {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004425 char buf[30];
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004426 /*
4427 * Using the ctxt->container for xmlHashAddEntry3 is ambigious
4428 * in the scenario:
4429 * 1. multiple top-level complex types have different target
4430 * namespaces but have the SAME NAME; this can happen if
4431 * schemata are imported
4432 * 2. those complex types contain attributes with an equal name
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004433 * 3. those attributes are in no namespace
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004434 * We will compute a new context string.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004435 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004436 snprintf(buf, 29, "#aCont%d", ctxt->counter++ + 1);
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004437 val = xmlHashAddEntry3(schema->attrDecl, name,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00004438 namespace, xmlDictLookup(ctxt->dict, BAD_CAST buf, -1), ret);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004439
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004440 if (val != 0) {
4441 xmlSchemaPCustomErr(ctxt,
4442 XML_SCHEMAP_INTERNAL,
4443 NULL, NULL, node,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004444 "Internal error: xmlSchemaAddAttribute, "
4445 "a dublicate attribute declaration with the name '%s' "
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004446 "could not be added to the hash.", name);
4447 xmlFree(ret);
4448 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004449 }
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004450 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004451 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004452 if (ctxt->assemble != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004453 xmlSchemaAddAssembledItem(ctxt, (xmlSchemaTypePtr) ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00004454 return (ret);
4455}
4456
4457/**
4458 * xmlSchemaAddAttributeGroup:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004459 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004460 * @schema: the schema being built
4461 * @name: the item name
4462 *
4463 * Add an XML schema Attrribute Group declaration
4464 *
4465 * Returns the new struture or NULL in case of error
4466 */
4467static xmlSchemaAttributeGroupPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004468xmlSchemaAddAttributeGroup(xmlSchemaParserCtxtPtr ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00004469 xmlSchemaPtr schema, const xmlChar * name,
4470 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00004471{
4472 xmlSchemaAttributeGroupPtr ret = NULL;
4473 int val;
4474
4475 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
4476 return (NULL);
4477
4478 if (schema->attrgrpDecl == NULL)
Daniel Veillardb9e2f262005-01-23 23:48:34 +00004479 schema->attrgrpDecl = xmlHashCreateDict(10, ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00004480 if (schema->attrgrpDecl == NULL)
4481 return (NULL);
4482
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004483 ret =
4484 (xmlSchemaAttributeGroupPtr)
4485 xmlMalloc(sizeof(xmlSchemaAttributeGroup));
Daniel Veillard4255d502002-04-16 15:50:10 +00004486 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004487 xmlSchemaPErrMemory(ctxt, "allocating attribute group", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004488 return (NULL);
4489 }
4490 memset(ret, 0, sizeof(xmlSchemaAttributeGroup));
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004491 ret->name = xmlDictLookup(ctxt->dict, name, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004492 val = xmlHashAddEntry3(schema->attrgrpDecl, name,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004493 schema->targetNamespace, ctxt->container, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00004494 if (val != 0) {
William M. Brack2f2a6632004-08-20 23:09:47 +00004495 xmlSchemaPCustomErr(ctxt,
4496 XML_SCHEMAP_REDEFINED_ATTRGROUP,
4497 NULL, NULL, node,
4498 "A global attribute group definition with the name '%s' does already exist", name);
Daniel Veillard4255d502002-04-16 15:50:10 +00004499 xmlFree(ret);
4500 return (NULL);
4501 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004502 if (ctxt->assemble != NULL)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004503 xmlSchemaAddAssembledItem(ctxt, (xmlSchemaTypePtr) ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00004504 return (ret);
4505}
4506
4507/**
4508 * xmlSchemaAddElement:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004509 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004510 * @schema: the schema being built
4511 * @name: the type name
4512 * @namespace: the type namespace
4513 *
4514 * Add an XML schema Element declaration
4515 * *WARNING* this interface is highly subject to change
4516 *
4517 * Returns the new struture or NULL in case of error
4518 */
4519static xmlSchemaElementPtr
4520xmlSchemaAddElement(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
William M. Brack2f2a6632004-08-20 23:09:47 +00004521 const xmlChar * name, const xmlChar * namespace,
4522 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00004523{
4524 xmlSchemaElementPtr ret = NULL;
4525 int val;
4526
4527 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
4528 return (NULL);
4529
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004530#ifdef DEBUG
4531 fprintf(stderr, "Adding element %s\n", name);
4532 if (namespace != NULL)
4533 fprintf(stderr, " target namespace %s\n", namespace);
4534#endif
4535
Daniel Veillard4255d502002-04-16 15:50:10 +00004536 if (schema->elemDecl == NULL)
Daniel Veillardb9e2f262005-01-23 23:48:34 +00004537 schema->elemDecl = xmlHashCreateDict(10, ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00004538 if (schema->elemDecl == NULL)
4539 return (NULL);
4540
4541 ret = (xmlSchemaElementPtr) xmlMalloc(sizeof(xmlSchemaElement));
4542 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004543 xmlSchemaPErrMemory(ctxt, "allocating element", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004544 return (NULL);
4545 }
4546 memset(ret, 0, sizeof(xmlSchemaElement));
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004547 ret->name = xmlDictLookup(ctxt->dict, name, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004548 val = xmlHashAddEntry3(schema->elemDecl, name,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004549 namespace, ctxt->container, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00004550 if (val != 0) {
William M. Brack2f2a6632004-08-20 23:09:47 +00004551 if (topLevel) {
4552 xmlSchemaPCustomErr(ctxt,
4553 XML_SCHEMAP_REDEFINED_ELEMENT,
4554 NULL, NULL, node,
4555 "A global element declaration with the name '%s' does "
4556 "already exist", name);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004557 xmlFree(ret);
4558 return (NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +00004559 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004560 char buf[30];
William M. Brack2f2a6632004-08-20 23:09:47 +00004561
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004562 snprintf(buf, 29, "#eCont%d", ctxt->counter++ + 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004563 val = xmlHashAddEntry3(schema->elemDecl, name,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004564 namespace, (xmlChar *) buf, ret);
William M. Brack2f2a6632004-08-20 23:09:47 +00004565 if (val != 0) {
4566 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004567 XML_SCHEMAP_INTERNAL,
William M. Brack2f2a6632004-08-20 23:09:47 +00004568 NULL, NULL, node,
4569 "Internal error: xmlSchemaAddElement, "
4570 "a dublicate element declaration with the name '%s' "
4571 "could not be added to the hash.", name);
4572 xmlFree(ret);
4573 return (NULL);
4574 }
4575 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004576
Daniel Veillard4255d502002-04-16 15:50:10 +00004577 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004578 if (ctxt->assemble != NULL)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004579 xmlSchemaAddAssembledItem(ctxt, (xmlSchemaTypePtr) ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00004580 return (ret);
4581}
4582
4583/**
4584 * xmlSchemaAddType:
Daniel Veillard01fa6152004-06-29 17:04:39 +00004585 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004586 * @schema: the schema being built
4587 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004588 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00004589 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00004590 * Add an XML schema item
Daniel Veillard4255d502002-04-16 15:50:10 +00004591 * *WARNING* this interface is highly subject to change
4592 *
4593 * Returns the new struture or NULL in case of error
4594 */
4595static xmlSchemaTypePtr
4596xmlSchemaAddType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
William M. Brack2f2a6632004-08-20 23:09:47 +00004597 const xmlChar * name, const xmlChar * namespace,
4598 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00004599{
4600 xmlSchemaTypePtr ret = NULL;
4601 int val;
4602
4603 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
4604 return (NULL);
4605
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004606#ifdef DEBUG
4607 fprintf(stderr, "Adding type %s\n", name);
4608 if (namespace != NULL)
4609 fprintf(stderr, " target namespace %s\n", namespace);
4610#endif
4611
Daniel Veillard4255d502002-04-16 15:50:10 +00004612 if (schema->typeDecl == NULL)
Daniel Veillardb9e2f262005-01-23 23:48:34 +00004613 schema->typeDecl = xmlHashCreateDict(10, ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00004614 if (schema->typeDecl == NULL)
4615 return (NULL);
4616
4617 ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
4618 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004619 xmlSchemaPErrMemory(ctxt, "allocating type", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004620 return (NULL);
4621 }
4622 memset(ret, 0, sizeof(xmlSchemaType));
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004623 ret->name = xmlDictLookup(ctxt->dict, name, -1);
Daniel Veillardb0f397e2003-12-23 23:30:53 +00004624 ret->redef = NULL;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004625 val = xmlHashAddEntry2(schema->typeDecl, name, namespace, ret);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004626 if (val != 0) {
4627 if (ctxt->includes == 0) {
William M. Brack2f2a6632004-08-20 23:09:47 +00004628 xmlSchemaPCustomErr(ctxt,
4629 XML_SCHEMAP_REDEFINED_TYPE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004630 NULL, NULL, node,
4631 "A global type definition with the name '%s' does already exist", name);
Daniel Veillardb0f397e2003-12-23 23:30:53 +00004632 xmlFree(ret);
4633 return (NULL);
4634 } else {
4635 xmlSchemaTypePtr prev;
4636
4637 prev = xmlHashLookup2(schema->typeDecl, name, namespace);
4638 if (prev == NULL) {
4639 xmlSchemaPErr(ctxt, (xmlNodePtr) ctxt->doc,
Daniel Veillardc0826a72004-08-10 14:17:33 +00004640 XML_ERR_INTERNAL_ERROR,
4641 "Internal error: xmlSchemaAddType, on type "
4642 "'%s'.\n",
4643 name, NULL);
Daniel Veillardb0f397e2003-12-23 23:30:53 +00004644 xmlFree(ret);
4645 return (NULL);
4646 }
4647 ret->redef = prev->redef;
4648 prev->redef = ret;
4649 }
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004650 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004651 ret->node = node;
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004652 ret->minOccurs = 1;
4653 ret->maxOccurs = 1;
Daniel Veillard3646d642004-06-02 19:19:14 +00004654 ret->attributeUses = NULL;
4655 ret->attributeWildcard = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004656 if (ctxt->assemble != NULL)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004657 xmlSchemaAddAssembledItem(ctxt,ret);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004658 return (ret);
4659}
4660
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004661static xmlSchemaQNameRefPtr
4662xmlSchemaNewQNameRef(xmlSchemaPtr schema,
4663 xmlSchemaTypeType refType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004664 const xmlChar *refName,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004665 const xmlChar *refNs)
4666{
4667 xmlSchemaQNameRefPtr ret;
4668
4669 ret = (xmlSchemaQNameRefPtr)
4670 xmlMalloc(sizeof(xmlSchemaQNameRef));
4671 if (ret == NULL) {
4672 xmlSchemaPErrMemory(NULL, "allocating QName reference item",
4673 NULL);
4674 return (NULL);
4675 }
4676 ret->type = XML_SCHEMA_EXTRA_QNAMEREF;
4677 ret->name = refName;
4678 ret->targetNamespace = refNs;
4679 ret->item = NULL;
4680 ret->itemType = refType;
4681 /*
4682 * Store the reference item in the schema.
4683 */
4684 xmlSchemaAddVolatile(schema, (xmlSchemaBasicItemPtr) ret);
4685 return (ret);
4686}
4687
4688/**
4689 * xmlSchemaAddModelGroup:
4690 * @ctxt: a schema parser context
4691 * @schema: the schema being built
4692 * @type: the "compositor" type of the model group
4693 * @container: the internal component name
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004694 * @node: the node in the schema doc
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004695 *
4696 * Adds a schema model group
4697 * *WARNING* this interface is highly subject to change
4698 *
4699 * Returns the new struture or NULL in case of error
4700 */
4701static xmlSchemaModelGroupPtr
4702xmlSchemaAddModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
4703 xmlSchemaTypeType type, const xmlChar **container,
4704 xmlNodePtr node)
4705{
4706 xmlSchemaModelGroupPtr ret = NULL;
4707 xmlChar buf[30];
4708
4709 if ((ctxt == NULL) || (schema == NULL))
4710 return (NULL);
4711
4712#ifdef DEBUG
4713 fprintf(stderr, "Adding model group component\n");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004714#endif
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004715 ret = (xmlSchemaModelGroupPtr)
4716 xmlMalloc(sizeof(xmlSchemaModelGroup));
4717 if (ret == NULL) {
4718 xmlSchemaPErrMemory(ctxt, "allocating model group component",
4719 NULL);
4720 return (NULL);
4721 }
4722 ret->type = type;
4723 ret->annot = NULL;
4724 ret->node = node;
4725 ret->children = NULL;
4726 ret->next = NULL;
4727 if (type == XML_SCHEMA_TYPE_SEQUENCE) {
4728 if (container != NULL)
4729 snprintf((char *) buf, 29, "#seq%d", ctxt->counter++ + 1);
4730 } else if (type == XML_SCHEMA_TYPE_CHOICE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004731 if (container != NULL)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004732 snprintf((char *) buf, 29, "#cho%d", ctxt->counter++ + 1);
4733 } else {
4734 if (container != NULL)
4735 snprintf((char *) buf, 29, "#all%d", ctxt->counter++ + 1);
4736 }
4737 if (container != NULL)
4738 *container = xmlDictLookup(ctxt->dict, BAD_CAST buf, -1);
4739 /*
4740 * Add to volatile items.
4741 * TODO: this should be changed someday.
4742 */
4743 if (xmlSchemaAddVolatile(schema, (xmlSchemaBasicItemPtr) ret) != 0) {
4744 xmlFree(ret);
4745 return (NULL);
4746 }
4747 return (ret);
4748}
4749
4750
4751/**
4752 * xmlSchemaAddParticle:
4753 * @ctxt: a schema parser context
4754 * @schema: the schema being built
4755 * @node: the corresponding node in the schema doc
4756 * @min: the minOccurs
4757 * @max: the maxOccurs
4758 *
4759 * Adds an XML schema particle component.
4760 * *WARNING* this interface is highly subject to change
4761 *
4762 * Returns the new struture or NULL in case of error
4763 */
4764static xmlSchemaParticlePtr
4765xmlSchemaAddParticle(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
4766 xmlNodePtr node, int min, int max)
4767{
4768 xmlSchemaParticlePtr ret = NULL;
4769 if ((ctxt == NULL) || (schema == NULL))
4770 return (NULL);
4771
4772#ifdef DEBUG
4773 fprintf(stderr, "Adding particle component\n");
4774#endif
4775 ret = (xmlSchemaParticlePtr)
4776 xmlMalloc(sizeof(xmlSchemaParticle));
4777 if (ret == NULL) {
4778 xmlSchemaPErrMemory(ctxt, "allocating particle component",
4779 NULL);
4780 return (NULL);
4781 }
4782 ret->type = XML_SCHEMA_TYPE_PARTICLE;
4783 ret->annot = NULL;
4784 ret->node = node;
4785 ret->minOccurs = min;
4786 ret->maxOccurs = max;
4787 ret->next = NULL;
4788 ret->children = NULL;
4789
4790 if (xmlSchemaAddVolatile(schema, (xmlSchemaBasicItemPtr) ret) != 0) {
4791 xmlFree(ret);
4792 return (NULL);
4793 }
4794 return (ret);
4795}
4796
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004797/**
4798 * xmlSchemaAddGroup:
4799 * @ctxt: a schema validation context
4800 * @schema: the schema being built
4801 * @name: the group name
4802 *
4803 * Add an XML schema Group definition
4804 *
4805 * Returns the new struture or NULL in case of error
4806 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004807static xmlSchemaModelGroupDefPtr
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004808xmlSchemaAddGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00004809 const xmlChar *name, const xmlChar *namespaceName,
4810 xmlNodePtr node)
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004811{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004812 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004813 int val;
4814
4815 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
4816 return (NULL);
4817
4818 if (schema->groupDecl == NULL)
Daniel Veillardb9e2f262005-01-23 23:48:34 +00004819 schema->groupDecl = xmlHashCreateDict(10, ctxt->dict);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004820 if (schema->groupDecl == NULL)
4821 return (NULL);
4822
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004823 ret = (xmlSchemaModelGroupDefPtr) xmlMalloc(sizeof(xmlSchemaModelGroupDef));
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004824 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004825 xmlSchemaPErrMemory(ctxt, "adding group", NULL);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004826 return (NULL);
4827 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004828 memset(ret, 0, sizeof(xmlSchemaModelGroupDef));
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004829 ret->name = xmlDictLookup(ctxt->dict, name, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004830 ret->type = XML_SCHEMA_TYPE_GROUP;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004831 ret->node = node;
4832 ret->targetNamespace = namespaceName;
4833 val = xmlHashAddEntry2(schema->groupDecl, ret->name, namespaceName, ret);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004834 if (val != 0) {
William M. Brack2f2a6632004-08-20 23:09:47 +00004835 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00004836 XML_SCHEMAP_REDEFINED_GROUP,
4837 NULL, NULL, node,
4838 "A global model group definition with the name '%s' does already "
4839 "exist", name);
Daniel Veillard4255d502002-04-16 15:50:10 +00004840 xmlFree(ret);
4841 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004842 }
4843 if (ctxt->assemble != NULL)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004844 xmlSchemaAddAssembledItem(ctxt, (xmlSchemaTypePtr) ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00004845 return (ret);
4846}
4847
Daniel Veillard3646d642004-06-02 19:19:14 +00004848/**
4849 * xmlSchemaNewWildcardNs:
4850 * @ctxt: a schema validation context
4851 *
4852 * Creates a new wildcard namespace constraint.
4853 *
4854 * Returns the new struture or NULL in case of error
4855 */
4856static xmlSchemaWildcardNsPtr
4857xmlSchemaNewWildcardNsConstraint(xmlSchemaParserCtxtPtr ctxt)
4858{
4859 xmlSchemaWildcardNsPtr ret;
4860
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004861 ret = (xmlSchemaWildcardNsPtr)
Daniel Veillard3646d642004-06-02 19:19:14 +00004862 xmlMalloc(sizeof(xmlSchemaWildcardNs));
4863 if (ret == NULL) {
4864 xmlSchemaPErrMemory(ctxt, "creating wildcard namespace constraint", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004865 return (NULL);
Daniel Veillard3646d642004-06-02 19:19:14 +00004866 }
4867 ret->value = NULL;
4868 ret->next = NULL;
4869 return (ret);
4870}
4871
4872/**
4873 * xmlSchemaAddWildcard:
4874 * @ctxt: a schema validation context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004875 * @schema: a schema
4876 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004877 * Adds a wildcard.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004878 * It corresponds to a xsd:anyAttribute and xsd:any.
Daniel Veillard3646d642004-06-02 19:19:14 +00004879 *
4880 * Returns the new struture or NULL in case of error
4881 */
4882static xmlSchemaWildcardPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004883xmlSchemaAddWildcard(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
4884 xmlSchemaTypeType type, xmlNodePtr node)
Daniel Veillard3646d642004-06-02 19:19:14 +00004885{
4886 xmlSchemaWildcardPtr ret = NULL;
4887
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004888 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard3646d642004-06-02 19:19:14 +00004889 return (NULL);
4890
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004891#ifdef DEBUG
4892 fprintf(stderr, "Adding wildcard component\n");
4893#endif
4894
Daniel Veillard3646d642004-06-02 19:19:14 +00004895 ret = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
4896 if (ret == NULL) {
4897 xmlSchemaPErrMemory(ctxt, "adding wildcard", NULL);
4898 return (NULL);
4899 }
4900 memset(ret, 0, sizeof(xmlSchemaWildcard));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004901 ret->type = type;
Daniel Veillard3646d642004-06-02 19:19:14 +00004902 ret->minOccurs = 1;
4903 ret->maxOccurs = 1;
4904
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004905 if (xmlSchemaAddVolatile(schema, (xmlSchemaBasicItemPtr) ret) != 0) {
4906 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_INTERNAL, NULL, NULL, node,
4907 "Failed to add a wildcard component to the list", NULL);
4908 xmlFree(ret);
4909 return (NULL);
4910 }
Daniel Veillard3646d642004-06-02 19:19:14 +00004911 return (ret);
4912}
4913
Daniel Veillard4255d502002-04-16 15:50:10 +00004914/************************************************************************
4915 * *
4916 * Utilities for parsing *
4917 * *
4918 ************************************************************************/
4919
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +00004920#if 0
Daniel Veillard4255d502002-04-16 15:50:10 +00004921/**
4922 * xmlGetQNameProp:
4923 * @ctxt: a schema validation context
4924 * @node: a subtree containing XML Schema informations
4925 * @name: the attribute name
4926 * @namespace: the result namespace if any
4927 *
4928 * Extract a QName Attribute value
4929 *
4930 * Returns the NCName or NULL if not found, and also update @namespace
4931 * with the namespace URI
4932 */
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004933static const xmlChar *
Daniel Veillard4255d502002-04-16 15:50:10 +00004934xmlGetQNameProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004935 const char *name, const xmlChar ** namespace)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004936{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004937 const xmlChar *val;
Daniel Veillard4255d502002-04-16 15:50:10 +00004938 xmlNsPtr ns;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004939 const xmlChar *ret, *prefix;
4940 int len;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +00004941 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00004942
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004943 *namespace = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +00004944 attr = xmlSchemaGetPropNode(node, name);
4945 if (attr == NULL)
4946 return (NULL);
4947 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00004948
Daniel Veillard4255d502002-04-16 15:50:10 +00004949 if (val == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004950 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004951
Daniel Veillardba0153a2004-04-01 10:42:31 +00004952 if (!strchr((char *) val, ':')) {
Daniel Veillardebcdebd2004-03-05 00:15:50 +00004953 ns = xmlSearchNs(node->doc, node, 0);
4954 if (ns) {
4955 *namespace = xmlDictLookup(ctxt->dict, ns->href, -1);
4956 return (val);
4957 }
4958 }
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004959 ret = xmlSplitQName3(val, &len);
4960 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004961 return (val);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004962 }
4963 ret = xmlDictLookup(ctxt->dict, ret, -1);
4964 prefix = xmlDictLookup(ctxt->dict, val, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004965
4966 ns = xmlSearchNs(node->doc, node, prefix);
4967 if (ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004968 xmlSchemaPSimpleTypeErr(ctxt, XML_SCHEMAP_PREFIX_UNDEFINED,
4969 NULL, NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +00004970 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), NULL, val,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004971 "The QName value '%s' has no corresponding namespace "
4972 "declaration in scope", val, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004973 } else {
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004974 *namespace = xmlDictLookup(ctxt->dict, ns->href, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004975 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004976 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00004977}
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +00004978#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00004979
4980/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00004981 * xmlSchemaPValAttrNodeQNameValue:
4982 * @ctxt: a schema parser context
4983 * @schema: the schema context
4984 * @ownerDes: the designation of the parent element
4985 * @ownerItem: the parent as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004986 * @value: the QName value
Daniel Veillardc0826a72004-08-10 14:17:33 +00004987 * @local: the resulting local part if found, the attribute value otherwise
4988 * @uri: the resulting namespace URI if found
4989 *
4990 * Extracts the local name and the URI of a QName value and validates it.
4991 * This one is intended to be used on attribute values that
4992 * should resolve to schema components.
4993 *
4994 * Returns 0, in case the QName is valid, a positive error code
4995 * if not valid and -1 if an internal error occurs.
4996 */
4997static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004998xmlSchemaPValAttrNodeQNameValue(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00004999 xmlSchemaPtr schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005000 xmlChar **ownerDes ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005001 xmlSchemaTypePtr ownerItem,
5002 xmlAttrPtr attr,
5003 const xmlChar *value,
5004 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005005 const xmlChar **local)
5006{
5007 const xmlChar *pref;
5008 xmlNsPtr ns;
5009 int len, ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005010
Daniel Veillardc0826a72004-08-10 14:17:33 +00005011 *uri = NULL;
5012 *local = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00005013 ret = xmlValidateQName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005014 if (ret > 0) {
5015 xmlSchemaPSimpleTypeErr(ctxt,
5016 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5017 ownerItem, (xmlNodePtr) attr,
5018 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
5019 NULL, value, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005020 *local = value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005021 return (ctxt->err);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005022 } else if (ret < 0)
5023 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005024
5025 if (!strchr((char *) value, ':')) {
Daniel Veillard24505b02005-07-28 23:49:35 +00005026 ns = xmlSearchNs(attr->doc, attr->parent, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005027 if (ns)
5028 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
5029 else if (schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) {
5030 /*
5031 * This one takes care of included schemas with no
5032 * target namespace.
5033 */
5034 *uri = schema->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005035 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005036 *local = xmlDictLookup(ctxt->dict, value, -1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005037 return (0);
5038 }
5039 /*
5040 * At this point xmlSplitQName3 has to return a local name.
5041 */
5042 *local = xmlSplitQName3(value, &len);
5043 *local = xmlDictLookup(ctxt->dict, *local, -1);
5044 pref = xmlDictLookup(ctxt->dict, value, len);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005045 ns = xmlSearchNs(attr->doc, attr->parent, pref);
5046 if (ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005047 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005048 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005049 ownerItem, (xmlNodePtr) attr,
5050 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), NULL, value,
5051 "The value '%s' of simple type 'xs:QName' has no "
5052 "corresponding namespace declaration in scope", value, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005053 return (ctxt->err);
5054 } else {
5055 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005056 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00005057 return (0);
5058}
5059
5060/**
5061 * xmlSchemaPValAttrNodeQName:
5062 * @ctxt: a schema parser context
5063 * @schema: the schema context
5064 * @ownerDes: the designation of the owner element
5065 * @ownerItem: the owner as a schema object
5066 * @attr: the attribute node
5067 * @local: the resulting local part if found, the attribute value otherwise
5068 * @uri: the resulting namespace URI if found
5069 *
5070 * Extracts and validates the QName of an attribute value.
5071 * This one is intended to be used on attribute values that
5072 * should resolve to schema components.
5073 *
5074 * Returns 0, in case the QName is valid, a positive error code
5075 * if not valid and -1 if an internal error occurs.
5076 */
5077static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005078xmlSchemaPValAttrNodeQName(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005079 xmlSchemaPtr schema,
5080 xmlChar **ownerDes,
5081 xmlSchemaTypePtr ownerItem,
5082 xmlAttrPtr attr,
5083 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005084 const xmlChar **local)
5085{
5086 const xmlChar *value;
5087
5088 value = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005089 return (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
5090 ownerDes, ownerItem, attr, value, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005091}
5092
5093/**
5094 * xmlSchemaPValAttrQName:
5095 * @ctxt: a schema parser context
5096 * @schema: the schema context
5097 * @ownerDes: the designation of the parent element
5098 * @ownerItem: the owner as a schema object
5099 * @ownerElem: the parent node of the attribute
5100 * @name: the name of the attribute
5101 * @local: the resulting local part if found, the attribute value otherwise
5102 * @uri: the resulting namespace URI if found
5103 *
5104 * Extracts and validates the QName of an attribute value.
5105 *
5106 * Returns 0, in case the QName is valid, a positive error code
5107 * if not valid and -1 if an internal error occurs.
5108 */
5109static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005110xmlSchemaPValAttrQName(xmlSchemaParserCtxtPtr ctxt,
5111 xmlSchemaPtr schema,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005112 xmlChar **ownerDes,
5113 xmlSchemaTypePtr ownerItem,
5114 xmlNodePtr ownerElem,
5115 const char *name,
5116 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005117 const xmlChar **local)
5118{
5119 xmlAttrPtr attr;
5120
5121 attr = xmlSchemaGetPropNode(ownerElem, name);
5122 if (attr == NULL) {
5123 *local = NULL;
5124 *uri = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005125 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005126 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005127 return (xmlSchemaPValAttrNodeQName(ctxt, schema,
5128 ownerDes, ownerItem, attr, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005129}
5130
5131/**
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005132 * xmlSchemaPValAttrID:
5133 * @ctxt: a schema parser context
5134 * @schema: the schema context
5135 * @ownerDes: the designation of the parent element
5136 * @ownerItem: the owner as a schema object
5137 * @ownerElem: the parent node of the attribute
5138 * @name: the name of the attribute
5139 *
5140 * Extracts and validates the ID of an attribute value.
5141 *
5142 * Returns 0, in case the ID is valid, a positive error code
5143 * if not valid and -1 if an internal error occurs.
5144 */
5145static int
5146xmlSchemaPValAttrID(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005147 xmlChar **ownerDes ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005148 xmlSchemaTypePtr ownerItem,
5149 xmlNodePtr ownerElem,
5150 const xmlChar *name)
5151{
5152 int ret;
5153 xmlChar *value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005154 xmlAttrPtr attr;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005155
5156 value = xmlGetNoNsProp(ownerElem, name);
5157 if (value == NULL)
5158 return (0);
5159
5160 attr = xmlSchemaGetPropNode(ownerElem, (const char *) name);
5161 if (attr == NULL)
5162 return (-1);
5163
5164 ret = xmlValidateNCName(BAD_CAST value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005165 if (ret == 0) {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005166 /*
5167 * NOTE: the IDness might have already be declared in the DTD
5168 */
5169 if (attr->atype != XML_ATTRIBUTE_ID) {
5170 xmlIDPtr res;
5171 xmlChar *strip;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005172
5173 /*
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005174 * TODO: Use xmlSchemaStrip here; it's not exported at this
5175 * moment.
5176 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005177 strip = xmlSchemaCollapseString(BAD_CAST value);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005178 if (strip != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005179 value = strip;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005180 res = xmlAddID(NULL, ownerElem->doc, BAD_CAST value, attr);
5181 if (res == NULL) {
5182 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005183 xmlSchemaPSimpleTypeErr(ctxt,
5184 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5185 ownerItem, (xmlNodePtr) attr,
5186 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5187 NULL, NULL, "Duplicate value '%s' of simple "
5188 "type 'xs:ID'", BAD_CAST value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005189 } else
5190 attr->atype = XML_ATTRIBUTE_ID;
5191 if (strip != NULL)
5192 xmlFree(strip);
5193 }
5194 } else if (ret > 0) {
5195 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005196 xmlSchemaPSimpleTypeErr(ctxt,
5197 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5198 ownerItem, (xmlNodePtr) attr,
5199 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5200 NULL, NULL, "The value '%s' of simple type 'xs:ID' is "
5201 "not a valid 'xs:NCName'",
5202 BAD_CAST value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005203 }
5204 xmlFree(value);
5205
5206 return (ret);
5207}
5208
5209/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005210 * xmlGetMaxOccurs:
5211 * @ctxt: a schema validation context
5212 * @node: a subtree containing XML Schema informations
5213 *
5214 * Get the maxOccurs property
5215 *
5216 * Returns the default if not found, or the value
5217 */
5218static int
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005219xmlGetMaxOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
5220 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005221{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005222 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00005223 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005224 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00005225
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005226 attr = xmlSchemaGetPropNode(node, "maxOccurs");
5227 if (attr == NULL)
5228 return (def);
5229 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00005230
5231 if (xmlStrEqual(val, (const xmlChar *) "unbounded")) {
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005232 if (max != UNBOUNDED) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005233 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005234 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5235 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005236 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005237 val, NULL, NULL, NULL);
5238 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005239 } else
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005240 return (UNBOUNDED); /* encoding it with -1 might be another option */
Daniel Veillard4255d502002-04-16 15:50:10 +00005241 }
5242
5243 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00005244 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005245 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005246 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005247 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005248 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5249 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005250 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005251 val, NULL, NULL, NULL);
5252 return (def);
5253 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005254 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005255 ret = ret * 10 + (*cur - '0');
5256 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00005257 }
William M. Brack76e95df2003-10-18 16:20:14 +00005258 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005259 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005260 /*
5261 * TODO: Restrict the maximal value to Integer.
5262 */
5263 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005264 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005265 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5266 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005267 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005268 val, NULL, NULL, NULL);
5269 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00005270 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005271 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005272}
5273
5274/**
5275 * xmlGetMinOccurs:
5276 * @ctxt: a schema validation context
5277 * @node: a subtree containing XML Schema informations
5278 *
5279 * Get the minOccurs property
5280 *
5281 * Returns the default if not found, or the value
5282 */
5283static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005284xmlGetMinOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005285 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005286{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005287 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00005288 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005289 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00005290
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005291 attr = xmlSchemaGetPropNode(node, "minOccurs");
5292 if (attr == NULL)
5293 return (def);
5294 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00005295 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00005296 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005297 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005298 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005299 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005300 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5301 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005302 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005303 val, NULL, NULL, NULL);
5304 return (def);
5305 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005306 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005307 ret = ret * 10 + (*cur - '0');
5308 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00005309 }
William M. Brack76e95df2003-10-18 16:20:14 +00005310 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005311 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005312 /*
5313 * TODO: Restrict the maximal value to Integer.
5314 */
5315 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005316 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005317 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5318 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005319 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005320 val, NULL, NULL, NULL);
5321 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00005322 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005323 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005324}
5325
5326/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005327 * xmlSchemaPGetBoolNodeValue:
5328 * @ctxt: a schema validation context
5329 * @ownerDes: owner designation
5330 * @ownerItem: the owner as a schema item
5331 * @node: the node holding the value
5332 *
5333 * Converts a boolean string value into 1 or 0.
5334 *
5335 * Returns 0 or 1.
5336 */
5337static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005338xmlSchemaPGetBoolNodeValue(xmlSchemaParserCtxtPtr ctxt,
5339 xmlChar **ownerDes ATTRIBUTE_UNUSED,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005340 xmlSchemaTypePtr ownerItem,
5341 xmlNodePtr node)
5342{
5343 xmlChar *value = NULL;
5344 int res = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005345
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005346 value = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005347 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005348 * 3.2.2.1 Lexical representation
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005349 * An instance of a datatype that is defined as ·boolean·
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005350 * can have the following legal literals {true, false, 1, 0}.
5351 */
5352 if (xmlStrEqual(BAD_CAST value, BAD_CAST "true"))
5353 res = 1;
5354 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "false"))
5355 res = 0;
5356 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "1"))
5357 res = 1;
5358 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005359 res = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005360 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005361 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005362 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005363 ownerItem, node,
5364 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
5365 NULL, BAD_CAST value,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005366 NULL, NULL, NULL);
5367 }
5368 if (value != NULL)
5369 xmlFree(value);
5370 return (res);
5371}
5372
5373/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005374 * xmlGetBooleanProp:
5375 * @ctxt: a schema validation context
5376 * @node: a subtree containing XML Schema informations
5377 * @name: the attribute name
5378 * @def: the default value
5379 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00005380 * Evaluate if a boolean property is set
Daniel Veillard4255d502002-04-16 15:50:10 +00005381 *
5382 * Returns the default if not found, 0 if found to be false,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005383 * 1 if found to be true
Daniel Veillard4255d502002-04-16 15:50:10 +00005384 */
5385static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005386xmlGetBooleanProp(xmlSchemaParserCtxtPtr ctxt,
5387 xmlChar **ownerDes ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005388 xmlSchemaTypePtr ownerItem,
5389 xmlNodePtr node,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005390 const char *name, int def)
5391{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005392 const xmlChar *val;
Daniel Veillard4255d502002-04-16 15:50:10 +00005393
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005394 val = xmlSchemaGetProp(ctxt, node, name);
Daniel Veillard4255d502002-04-16 15:50:10 +00005395 if (val == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005396 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005397 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00005398 * 3.2.2.1 Lexical representation
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005399 * An instance of a datatype that is defined as ·boolean·
Daniel Veillardc0826a72004-08-10 14:17:33 +00005400 * can have the following legal literals {true, false, 1, 0}.
5401 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005402 if (xmlStrEqual(val, BAD_CAST "true"))
5403 def = 1;
5404 else if (xmlStrEqual(val, BAD_CAST "false"))
5405 def = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00005406 else if (xmlStrEqual(val, BAD_CAST "1"))
5407 def = 1;
5408 else if (xmlStrEqual(val, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005409 def = 0;
5410 else {
5411 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005412 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005413 ownerItem,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00005414 (xmlNodePtr) xmlSchemaGetPropNode(node, name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005415 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
5416 NULL, val, NULL, NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005417 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005418 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00005419}
5420
5421/************************************************************************
5422 * *
5423 * Shema extraction from an Infoset *
5424 * *
5425 ************************************************************************/
5426static xmlSchemaTypePtr xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr
5427 ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00005428 xmlNodePtr node,
5429 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005430static xmlSchemaTypePtr xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr
5431 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00005432 xmlSchemaPtr schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005433 xmlNodePtr node,
Daniel Veillard3646d642004-06-02 19:19:14 +00005434 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005435static xmlSchemaTypePtr xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr
5436 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00005437 xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005438 xmlNodePtr node,
5439 xmlSchemaTypeType parentType);
Daniel Veillard4255d502002-04-16 15:50:10 +00005440static xmlSchemaAttributePtr xmlSchemaParseAttribute(xmlSchemaParserCtxtPtr
5441 ctxt,
5442 xmlSchemaPtr schema,
William M. Bracke7091952004-05-11 15:09:58 +00005443 xmlNodePtr node,
5444 int topLevel);
Daniel Veillard4255d502002-04-16 15:50:10 +00005445static xmlSchemaAttributeGroupPtr
5446xmlSchemaParseAttributeGroup(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +00005447 xmlSchemaPtr schema, xmlNodePtr node,
5448 int topLevel);
Daniel Veillard4255d502002-04-16 15:50:10 +00005449static xmlSchemaTypePtr xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt,
5450 xmlSchemaPtr schema,
5451 xmlNodePtr node);
Daniel Veillard3646d642004-06-02 19:19:14 +00005452static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005453xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
5454 xmlSchemaPtr schema, xmlNodePtr node);
Daniel Veillard4255d502002-04-16 15:50:10 +00005455
5456/**
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005457 * xmlSchemaPValAttrNodeValue:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005458 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00005459 * @ctxt: a schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00005460 * @ownerDes: the designation of the parent element
5461 * @ownerItem: the schema object owner if existent
5462 * @attr: the schema attribute node being validated
5463 * @value: the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005464 * @type: the built-in type to be validated against
Daniel Veillard01fa6152004-06-29 17:04:39 +00005465 *
5466 * Validates a value against the given built-in type.
5467 * This one is intended to be used internally for validation
5468 * of schema attribute values during parsing of the schema.
5469 *
5470 * Returns 0 if the value is valid, a positive error code
5471 * number otherwise and -1 in case of an internal or API error.
5472 */
5473static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005474xmlSchemaPValAttrNodeValue(xmlSchemaParserCtxtPtr pctxt,
5475 xmlChar **ownerDes ATTRIBUTE_UNUSED,
5476 xmlSchemaTypePtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005477 xmlAttrPtr attr,
5478 const xmlChar *value,
5479 xmlSchemaTypePtr type)
Daniel Veillard01fa6152004-06-29 17:04:39 +00005480{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005481
5482 int ret = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00005483
5484 /*
5485 * NOTE: Should we move this to xmlschematypes.c? Hmm, but this
5486 * one is really meant to be used internally, so better not.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005487 */
5488 if ((pctxt == NULL) || (type == NULL) || (attr == NULL))
Daniel Veillard01fa6152004-06-29 17:04:39 +00005489 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005490 if (type->type != XML_SCHEMA_TYPE_BASIC) {
5491 PERROR_INT("xmlSchemaPValAttrNodeValue",
5492 "the given type is not a built-in type");
5493 return (-1);
5494 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00005495 switch (type->builtInType) {
5496 case XML_SCHEMAS_NCNAME:
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00005497 case XML_SCHEMAS_QNAME:
5498 case XML_SCHEMAS_ANYURI:
5499 case XML_SCHEMAS_TOKEN:
5500 case XML_SCHEMAS_LANGUAGE:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005501 ret = xmlSchemaValPredefTypeNode(type, value, NULL,
5502 (xmlNodePtr) attr);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00005503 break;
Daniel Veillard01fa6152004-06-29 17:04:39 +00005504 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005505 PERROR_INT("xmlSchemaPValAttrNodeValue",
5506 "validation using the given type is not supported");
Daniel Veillard01fa6152004-06-29 17:04:39 +00005507 return (-1);
5508 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005509 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00005510 /*
5511 * TODO: Should we use the S4S error codes instead?
5512 */
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00005513 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005514 PERROR_INT("xmlSchemaPValAttrNodeValue",
5515 "failed to validate a schema attribute value");
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00005516 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005517 } else if (ret > 0) {
5518 if (VARIETY_LIST(type))
5519 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
5520 else
5521 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
5522 xmlSchemaPSimpleTypeErr(pctxt,
5523 ret, ownerItem, (xmlNodePtr) attr,
5524 type, NULL, value, NULL, NULL, NULL);
5525 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00005526 return (ret);
5527}
5528
5529/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00005530 * xmlSchemaPValAttrNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005531 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00005532 * @ctxt: a schema parser context
5533 * @ownerDes: the designation of the parent element
5534 * @ownerItem: the schema object owner if existent
5535 * @attr: the schema attribute node being validated
5536 * @type: the built-in type to be validated against
5537 * @value: the resulting value if any
5538 *
5539 * Extracts and validates a value against the given built-in type.
5540 * This one is intended to be used internally for validation
5541 * of schema attribute values during parsing of the schema.
5542 *
5543 * Returns 0 if the value is valid, a positive error code
5544 * number otherwise and -1 in case of an internal or API error.
5545 */
5546static int
5547xmlSchemaPValAttrNode(xmlSchemaParserCtxtPtr ctxt,
5548 xmlChar **ownerDes,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005549 xmlSchemaTypePtr ownerItem,
5550 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005551 xmlSchemaTypePtr type,
5552 const xmlChar **value)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005553{
Daniel Veillardc0826a72004-08-10 14:17:33 +00005554 const xmlChar *val;
5555
5556 if ((ctxt == NULL) || (type == NULL) || (attr == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005557 return (-1);
5558
Daniel Veillardc0826a72004-08-10 14:17:33 +00005559 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
5560 if (value != NULL)
5561 *value = val;
5562
5563 return (xmlSchemaPValAttrNodeValue(ctxt, ownerDes, ownerItem, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005564 val, type));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005565}
5566
5567/**
5568 * xmlSchemaPValAttr:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005569 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00005570 * @ctxt: a schema parser context
5571 * @node: the element node of the attribute
5572 * @ownerDes: the designation of the parent element
5573 * @ownerItem: the schema object owner if existent
5574 * @ownerElem: the owner element node
5575 * @name: the name of the schema attribute node
5576 * @type: the built-in type to be validated against
5577 * @value: the resulting value if any
5578 *
5579 * Extracts and validates a value against the given built-in type.
5580 * This one is intended to be used internally for validation
5581 * of schema attribute values during parsing of the schema.
5582 *
5583 * Returns 0 if the value is valid, a positive error code
5584 * number otherwise and -1 in case of an internal or API error.
5585 */
5586static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005587xmlSchemaPValAttr(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005588 xmlChar **ownerDes,
5589 xmlSchemaTypePtr ownerItem,
5590 xmlNodePtr ownerElem,
5591 const char *name,
5592 xmlSchemaTypePtr type,
5593 const xmlChar **value)
5594{
5595 xmlAttrPtr attr;
5596
5597 if ((ctxt == NULL) || (type == NULL)) {
5598 if (value != NULL)
5599 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005600 return (-1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005601 }
5602 if (type->type != XML_SCHEMA_TYPE_BASIC) {
5603 if (value != NULL)
5604 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005605 xmlSchemaPErr(ctxt, ownerElem,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00005606 XML_SCHEMAP_INTERNAL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005607 "Internal error: xmlSchemaPValAttr, the given "
5608 "type '%s' is not a built-in type.\n",
5609 type->name, NULL);
5610 return (-1);
5611 }
5612 attr = xmlSchemaGetPropNode(ownerElem, name);
5613 if (attr == NULL) {
5614 if (value != NULL)
5615 *value = NULL;
5616 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005617 }
5618 return (xmlSchemaPValAttrNode(ctxt, ownerDes, ownerItem, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005619 type, value));
5620}
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00005621
5622static int
5623xmlSchemaCheckReference(xmlSchemaParserCtxtPtr pctxt,
5624 xmlSchemaPtr schema,
5625 xmlNodePtr node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005626 xmlSchemaBasicItemPtr item,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00005627 const xmlChar *namespaceName)
5628{
5629 if (xmlStrEqual(schema->targetNamespace, namespaceName))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005630 return (1);
5631 if (xmlStrEqual(xmlSchemaNs, namespaceName))
5632 return (1);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00005633 if (pctxt->localImports != NULL) {
5634 int i;
5635 for (i = 0; i < pctxt->nbLocalImports; i++)
5636 if (xmlStrEqual(namespaceName, pctxt->localImports[i]))
5637 return (1);
5638 }
5639 if (namespaceName == NULL)
5640 xmlSchemaPCustomErr(pctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005641 NULL, (xmlSchemaTypePtr) item, node,
5642 "References from this schema to components in no "
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00005643 "namespace are not valid, since not indicated by an import "
5644 "statement", NULL);
5645 else
5646 xmlSchemaPCustomErr(pctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005647 NULL, (xmlSchemaTypePtr) item, node,
5648 "References from this schema to components in the "
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00005649 "namespace '%s' are not valid, since not indicated by an import "
5650 "statement", namespaceName);
5651 return (0);
5652}
5653
Daniel Veillardc0826a72004-08-10 14:17:33 +00005654/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005655 * xmlSchemaParseAttrDecls:
5656 * @ctxt: a schema validation context
5657 * @schema: the schema being built
5658 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005659 * @type: the hosting type where the attributes will be anchored
Daniel Veillard4255d502002-04-16 15:50:10 +00005660 *
5661 * parse a XML schema attrDecls declaration corresponding to
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005662 * <!ENTITY % attrDecls
Daniel Veillard4255d502002-04-16 15:50:10 +00005663 * '((%attribute;| %attributeGroup;)*,(%anyAttribute;)?)'>
5664 */
5665static xmlNodePtr
5666xmlSchemaParseAttrDecls(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5667 xmlNodePtr child, xmlSchemaTypePtr type)
5668{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005669 xmlSchemaAttributePtr lastattr = NULL, attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00005670
Daniel Veillard4255d502002-04-16 15:50:10 +00005671 while ((IS_SCHEMA(child, "attribute")) ||
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005672 (IS_SCHEMA(child, "attributeGroup"))) {
5673 attr = NULL;
5674 if (IS_SCHEMA(child, "attribute")) {
William M. Bracke7091952004-05-11 15:09:58 +00005675 attr = xmlSchemaParseAttribute(ctxt, schema, child, 0);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005676 } else if (IS_SCHEMA(child, "attributeGroup")) {
5677 attr = (xmlSchemaAttributePtr)
Daniel Veillard3646d642004-06-02 19:19:14 +00005678 xmlSchemaParseAttributeGroup(ctxt, schema, child, 0);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005679 }
5680 if (attr != NULL) {
5681 if (lastattr == NULL) {
Daniel Veillard3646d642004-06-02 19:19:14 +00005682 if (type->type == XML_SCHEMA_TYPE_ATTRIBUTEGROUP)
5683 ((xmlSchemaAttributeGroupPtr) type)->attributes = attr;
5684 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005685 type->attributes = attr;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005686 lastattr = attr;
5687 } else {
5688 lastattr->next = attr;
5689 lastattr = attr;
5690 }
5691 }
5692 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005693 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005694 return (child);
Daniel Veillard4255d502002-04-16 15:50:10 +00005695}
5696
5697/**
5698 * xmlSchemaParseAnnotation:
5699 * @ctxt: a schema validation context
5700 * @schema: the schema being built
5701 * @node: a subtree containing XML Schema informations
5702 *
5703 * parse a XML schema Attrribute declaration
5704 * *WARNING* this interface is highly subject to change
5705 *
William M. Bracke7091952004-05-11 15:09:58 +00005706 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00005707 * 1 in case of success.
5708 */
5709static xmlSchemaAnnotPtr
5710xmlSchemaParseAnnotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5711 xmlNodePtr node)
5712{
5713 xmlSchemaAnnotPtr ret;
Daniel Veillardc0826a72004-08-10 14:17:33 +00005714 xmlNodePtr child = NULL;
5715 xmlAttrPtr attr;
5716 int barked = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00005717
Daniel Veillardc0826a72004-08-10 14:17:33 +00005718 /*
5719 * INFO: S4S completed.
5720 */
5721 /*
5722 * id = ID
5723 * {any attributes with non-schema namespace . . .}>
5724 * Content: (appinfo | documentation)*
5725 */
Daniel Veillard4255d502002-04-16 15:50:10 +00005726 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
5727 return (NULL);
5728 ret = xmlSchemaNewAnnot(ctxt, node);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005729 attr = node->properties;
5730 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005731 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00005732 (!xmlStrEqual(attr->name, BAD_CAST "id"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005733 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00005734 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005735
5736 xmlSchemaPIllegalAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005737 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
5738 NULL, NULL, attr);
5739 }
5740 attr = attr->next;
5741 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005742 xmlSchemaPValAttrID(ctxt, NULL, NULL, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00005743 /*
5744 * And now for the children...
5745 */
5746 child = node->children;
5747 while (child != NULL) {
5748 if (IS_SCHEMA(child, "appinfo")) {
5749 /* TODO: make available the content of "appinfo". */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005750 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00005751 * source = anyURI
5752 * {any attributes with non-schema namespace . . .}>
5753 * Content: ({any})*
5754 */
5755 attr = child->properties;
5756 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005757 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00005758 (!xmlStrEqual(attr->name, BAD_CAST "source"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005759 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00005760 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005761
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005762 xmlSchemaPIllegalAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005763 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
5764 NULL, NULL, attr);
5765 }
5766 attr = attr->next;
5767 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005768 xmlSchemaPValAttr(ctxt, NULL, NULL, child, "source",
5769 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005770 child = child->next;
5771 } else if (IS_SCHEMA(child, "documentation")) {
5772 /* TODO: make available the content of "documentation". */
5773 /*
5774 * source = anyURI
5775 * {any attributes with non-schema namespace . . .}>
5776 * Content: ({any})*
5777 */
5778 attr = child->properties;
5779 while (attr != NULL) {
5780 if (attr->ns == NULL) {
5781 if (!xmlStrEqual(attr->name, BAD_CAST "source")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005782 xmlSchemaPIllegalAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005783 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
5784 NULL, NULL, attr);
5785 }
5786 } else {
5787 if (xmlStrEqual(attr->ns->href, xmlSchemaNs) ||
5788 (xmlStrEqual(attr->name, BAD_CAST "lang") &&
5789 (!xmlStrEqual(attr->ns->href, XML_XML_NAMESPACE)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005790
5791 xmlSchemaPIllegalAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005792 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
5793 NULL, NULL, attr);
5794 }
5795 }
5796 attr = attr->next;
5797 }
5798 /*
5799 * Attribute "xml:lang".
5800 */
5801 attr = xmlSchemaGetPropNodeNs(child, (const char *) XML_XML_NAMESPACE, "lang");
5802 if (attr != NULL)
5803 xmlSchemaPValAttrNode(ctxt, NULL, NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005804 xmlSchemaGetBuiltInType(XML_SCHEMAS_LANGUAGE), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005805 child = child->next;
5806 } else {
5807 if (!barked)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005808 xmlSchemaPContentErr(ctxt,
5809 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005810 NULL, NULL, node, child, NULL, "(appinfo | documentation)*");
5811 barked = 1;
5812 child = child->next;
5813 }
5814 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005815
Daniel Veillard4255d502002-04-16 15:50:10 +00005816 return (ret);
5817}
5818
5819/**
5820 * xmlSchemaParseFacet:
5821 * @ctxt: a schema validation context
5822 * @schema: the schema being built
5823 * @node: a subtree containing XML Schema informations
5824 *
5825 * parse a XML schema Facet declaration
5826 * *WARNING* this interface is highly subject to change
5827 *
5828 * Returns the new type structure or NULL in case of error
5829 */
5830static xmlSchemaFacetPtr
5831xmlSchemaParseFacet(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005832 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00005833{
5834 xmlSchemaFacetPtr facet;
5835 xmlNodePtr child = NULL;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005836 const xmlChar *value;
Daniel Veillard4255d502002-04-16 15:50:10 +00005837
5838 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
5839 return (NULL);
5840
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00005841 facet = xmlSchemaNewFacet();
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005842 if (facet == NULL) {
5843 xmlSchemaPErrMemory(ctxt, "allocating facet", node);
5844 return (NULL);
5845 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005846 facet->node = node;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005847 value = xmlSchemaGetProp(ctxt, node, "value");
Daniel Veillard4255d502002-04-16 15:50:10 +00005848 if (value == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005849 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_FACET_NO_VALUE,
5850 "Facet %s has no value\n", node->name, NULL);
5851 xmlSchemaFreeFacet(facet);
Daniel Veillard4255d502002-04-16 15:50:10 +00005852 return (NULL);
5853 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005854 if (IS_SCHEMA(node, "minInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005855 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005856 } else if (IS_SCHEMA(node, "minExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005857 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005858 } else if (IS_SCHEMA(node, "maxInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005859 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005860 } else if (IS_SCHEMA(node, "maxExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005861 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005862 } else if (IS_SCHEMA(node, "totalDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005863 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005864 } else if (IS_SCHEMA(node, "fractionDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005865 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005866 } else if (IS_SCHEMA(node, "pattern")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005867 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005868 } else if (IS_SCHEMA(node, "enumeration")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005869 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005870 } else if (IS_SCHEMA(node, "whiteSpace")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005871 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005872 } else if (IS_SCHEMA(node, "length")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005873 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005874 } else if (IS_SCHEMA(node, "maxLength")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005875 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
5876 } else if (IS_SCHEMA(node, "minLength")) {
5877 facet->type = XML_SCHEMA_FACET_MINLENGTH;
5878 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005879 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_TYPE,
5880 "Unknown facet type %s\n", node->name, NULL);
5881 xmlSchemaFreeFacet(facet);
5882 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005883 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005884 xmlSchemaPValAttrID(ctxt, NULL,
5885 (xmlSchemaTypePtr) facet, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +00005886 facet->value = value;
Daniel Veillard01fa6152004-06-29 17:04:39 +00005887 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
5888 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
5889 const xmlChar *fixed;
5890
5891 fixed = xmlSchemaGetProp(ctxt, node, "fixed");
5892 if (fixed != NULL) {
5893 if (xmlStrEqual(fixed, BAD_CAST "true"))
5894 facet->fixed = 1;
5895 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005896 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005897 child = node->children;
5898
5899 if (IS_SCHEMA(child, "annotation")) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005900 facet->annot = xmlSchemaParseAnnotation(ctxt, schema, child);
5901 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00005902 }
5903 if (child != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005904 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_CHILD,
5905 "Facet %s has unexpected child content\n",
5906 node->name, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005907 }
5908 return (facet);
5909}
5910
5911/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00005912 * xmlSchemaParseWildcardNs:
5913 * @ctxt: a schema parser context
5914 * @wildc: the wildcard, already created
5915 * @node: a subtree containing XML Schema informations
5916 *
5917 * Parses the attribute "processContents" and "namespace"
5918 * of a xsd:anyAttribute and xsd:any.
5919 * *WARNING* this interface is highly subject to change
5920 *
5921 * Returns 0 if everything goes fine, a positive error code
5922 * if something is not valid and -1 if an internal error occurs.
5923 */
5924static int
5925xmlSchemaParseWildcardNs(xmlSchemaParserCtxtPtr ctxt,
5926 xmlSchemaPtr schema,
5927 xmlSchemaWildcardPtr wildc,
5928 xmlNodePtr node)
5929{
5930 const xmlChar *pc, *ns, *dictnsItem;
5931 int ret = 0;
5932 xmlChar *nsItem;
5933 xmlSchemaWildcardNsPtr tmp, lastNs = NULL;
5934 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005935
Daniel Veillardc0826a72004-08-10 14:17:33 +00005936 pc = xmlSchemaGetProp(ctxt, node, "processContents");
5937 if ((pc == NULL)
5938 || (xmlStrEqual(pc, (const xmlChar *) "strict"))) {
5939 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
5940 } else if (xmlStrEqual(pc, (const xmlChar *) "skip")) {
5941 wildc->processContents = XML_SCHEMAS_ANY_SKIP;
5942 } else if (xmlStrEqual(pc, (const xmlChar *) "lax")) {
5943 wildc->processContents = XML_SCHEMAS_ANY_LAX;
5944 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005945 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005946 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005947 NULL, node,
5948 NULL, "(strict | skip | lax)", pc,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005949 NULL, NULL, NULL);
5950 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005951 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00005952 }
5953 /*
5954 * Build the namespace constraints.
5955 */
5956 attr = xmlSchemaGetPropNode(node, "namespace");
5957 ns = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00005958 if ((attr == NULL) || (xmlStrEqual(ns, BAD_CAST "##any")))
Daniel Veillardc0826a72004-08-10 14:17:33 +00005959 wildc->any = 1;
5960 else if (xmlStrEqual(ns, BAD_CAST "##other")) {
5961 wildc->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005962 if (wildc->negNsSet == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00005963 return (-1);
5964 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005965 wildc->negNsSet->value = schema->targetNamespace;
5966 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00005967 const xmlChar *end, *cur;
5968
5969 cur = ns;
5970 do {
5971 while (IS_BLANK_CH(*cur))
5972 cur++;
5973 end = cur;
5974 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
5975 end++;
5976 if (end == cur)
5977 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005978 nsItem = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005979 if ((xmlStrEqual(nsItem, BAD_CAST "##other")) ||
5980 (xmlStrEqual(nsItem, BAD_CAST "##any"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005981 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005982 XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005983 NULL, (xmlNodePtr) attr,
5984 NULL,
5985 "((##any | ##other) | List of (xs:anyURI | "
5986 "(##targetNamespace | ##local)))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00005987 nsItem, NULL, NULL, NULL);
5988 ret = XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER;
5989 } else {
5990 if (xmlStrEqual(nsItem, BAD_CAST "##targetNamespace")) {
5991 dictnsItem = schema->targetNamespace;
5992 } else if (xmlStrEqual(nsItem, BAD_CAST "##local")) {
5993 dictnsItem = NULL;
5994 } else {
5995 /*
5996 * Validate the item (anyURI).
5997 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005998 xmlSchemaPValAttrNodeValue(ctxt, NULL, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005999 nsItem, xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI));
6000 dictnsItem = xmlDictLookup(ctxt->dict, nsItem, -1);
6001 }
6002 /*
6003 * Avoid dublicate namespaces.
6004 */
6005 tmp = wildc->nsSet;
6006 while (tmp != NULL) {
6007 if (dictnsItem == tmp->value)
6008 break;
6009 tmp = tmp->next;
6010 }
6011 if (tmp == NULL) {
6012 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
6013 if (tmp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006014 xmlFree(nsItem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006015 return (-1);
6016 }
6017 tmp->value = dictnsItem;
6018 tmp->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006019 if (wildc->nsSet == NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00006020 wildc->nsSet = tmp;
6021 else
6022 lastNs->next = tmp;
6023 lastNs = tmp;
6024 }
6025
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006026 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006027 xmlFree(nsItem);
6028 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006029 } while (*cur != 0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006030 }
6031 return (ret);
6032}
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006033
6034static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006035xmlSchemaPCheckParticleCorrect_2(xmlSchemaParserCtxtPtr ctxt,
6036 xmlSchemaParticlePtr item ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006037 xmlNodePtr node,
6038 int minOccurs,
6039 int maxOccurs) {
6040
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006041 if ((maxOccurs == 0) && ( minOccurs == 0))
6042 return (0);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006043 if (maxOccurs != UNBOUNDED) {
6044 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006045 * TODO: Maybe we should better not create the particle,
6046 * if min/max is invalid, since it could confuse the build of the
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006047 * content model.
6048 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006049 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006050 * 3.9.6 Schema Component Constraint: Particle Correct
6051 *
6052 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006053 if (maxOccurs < 1) {
6054 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006055 * 2.2 {max occurs} must be greater than or equal to 1.
6056 */
6057 xmlSchemaPCustomAttrErr(ctxt,
6058 XML_SCHEMAP_P_PROPS_CORRECT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006059 NULL, NULL,
6060 xmlSchemaGetPropNode(node, "maxOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006061 "The value must be greater than or equal to 1");
6062 return (XML_SCHEMAP_P_PROPS_CORRECT_2_2);
6063 } else if (minOccurs > maxOccurs) {
6064 /*
6065 * 2.1 {min occurs} must not be greater than {max occurs}.
6066 */
6067 xmlSchemaPCustomAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006068 XML_SCHEMAP_P_PROPS_CORRECT_2_1,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006069 NULL, NULL,
6070 xmlSchemaGetPropNode(node, "minOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006071 "The value must not be greater than the value of 'maxOccurs'");
6072 return (XML_SCHEMAP_P_PROPS_CORRECT_2_1);
6073 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006074 }
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006075 return (0);
6076}
6077
Daniel Veillardc0826a72004-08-10 14:17:33 +00006078/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006079 * xmlSchemaParseAny:
6080 * @ctxt: a schema validation context
6081 * @schema: the schema being built
6082 * @node: a subtree containing XML Schema informations
6083 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006084 * Parsea a XML schema <any> element. A particle and wildcard
6085 * will be created (except if minOccurs==maxOccurs==0, in this case
6086 * nothing will be created).
Daniel Veillard4255d502002-04-16 15:50:10 +00006087 * *WARNING* this interface is highly subject to change
6088 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006089 * Returns the particle or NULL in case of error or if minOccurs==maxOccurs==0
Daniel Veillard4255d502002-04-16 15:50:10 +00006090 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006091static xmlSchemaParticlePtr
Daniel Veillard4255d502002-04-16 15:50:10 +00006092xmlSchemaParseAny(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6093 xmlNodePtr node)
6094{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006095 xmlSchemaParticlePtr particle;
Daniel Veillard4255d502002-04-16 15:50:10 +00006096 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006097 xmlSchemaWildcardPtr wild;
6098 int min, max;
6099 xmlAttrPtr attr;
6100 xmlSchemaAnnotPtr annot = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00006101
6102 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6103 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006104 /*
6105 * Check for illegal attributes.
6106 */
6107 attr = node->properties;
6108 while (attr != NULL) {
6109 if (attr->ns == NULL) {
6110 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
6111 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
6112 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
6113 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
6114 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006115 xmlSchemaPIllegalAttrErr(ctxt,
6116 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
6117 NULL, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006118 }
6119 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006120 xmlSchemaPIllegalAttrErr(ctxt,
6121 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
6122 NULL, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006123 }
6124 attr = attr->next;
6125 }
6126 xmlSchemaPValAttrID(ctxt, NULL, NULL, node, BAD_CAST "id");
6127 /*
6128 * minOccurs/maxOccurs.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006129 */
6130 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
6131 "(xs:nonNegativeInteger | unbounded)");
6132 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1,
6133 "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006134 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
6135 /*
6136 * Create & parse the wildcard.
6137 */
6138 wild = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY, node);
6139 if (wild == NULL)
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006140 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006141 xmlSchemaParseWildcardNs(ctxt, schema, wild, node);
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006142 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006143 * And now for the children...
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006144 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006145 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00006146 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006147 annot = xmlSchemaParseAnnotation(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006148 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006149 }
6150 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006151 xmlSchemaPContentErr(ctxt,
6152 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006153 NULL, NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006154 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006155 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006156 /*
6157 * No component if minOccurs==maxOccurs==0.
6158 */
6159 if ((min == 0) && (max == 0)) {
6160 /* Don't free the wildcard, since it's already on the list. */
6161 return (NULL);
6162 }
6163 /*
6164 * Create the particle.
6165 */
6166 particle = xmlSchemaAddParticle(ctxt, schema, node, min, max);
6167 if (particle == NULL)
6168 return (NULL);
6169 particle->annot = annot;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006170 wild->minOccurs = min;
6171 wild->maxOccurs = max;
6172 particle->children = (xmlSchemaTreeItemPtr) wild;
Daniel Veillard4255d502002-04-16 15:50:10 +00006173
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006174 return (particle);
Daniel Veillard4255d502002-04-16 15:50:10 +00006175}
6176
6177/**
6178 * xmlSchemaParseNotation:
6179 * @ctxt: a schema validation context
6180 * @schema: the schema being built
6181 * @node: a subtree containing XML Schema informations
6182 *
6183 * parse a XML schema Notation declaration
6184 *
6185 * Returns the new structure or NULL in case of error
6186 */
6187static xmlSchemaNotationPtr
6188xmlSchemaParseNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006189 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006190{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006191 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00006192 xmlSchemaNotationPtr ret;
6193 xmlNodePtr child = NULL;
6194
6195 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6196 return (NULL);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006197 name = xmlSchemaGetProp(ctxt, node, "name");
Daniel Veillard4255d502002-04-16 15:50:10 +00006198 if (name == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006199 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_NOTATION_NO_NAME,
6200 "Notation has no name\n", NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006201 return (NULL);
6202 }
6203 ret = xmlSchemaAddNotation(ctxt, schema, name);
6204 if (ret == NULL) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006205 return (NULL);
6206 }
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006207 ret->targetNamespace = schema->targetNamespace;
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00006208
6209 xmlSchemaPValAttrID(ctxt, NULL, (xmlSchemaTypePtr) ret,
6210 node, BAD_CAST "id");
6211
6212 if (IS_SCHEMA(child, "annotation")) {
6213 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child);
6214 child = child->next;
6215 }
6216
Daniel Veillard4255d502002-04-16 15:50:10 +00006217 child = node->children;
6218 if (IS_SCHEMA(child, "annotation")) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006219 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child);
6220 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006221 }
6222 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006223 xmlSchemaPContentErr(ctxt,
6224 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006225 NULL, NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006226 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006227 }
6228
6229 return (ret);
6230}
6231
6232/**
6233 * xmlSchemaParseAnyAttribute:
6234 * @ctxt: a schema validation context
6235 * @schema: the schema being built
6236 * @node: a subtree containing XML Schema informations
6237 *
6238 * parse a XML schema AnyAttrribute declaration
6239 * *WARNING* this interface is highly subject to change
6240 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006241 * Returns a wildcard or NULL.
Daniel Veillard4255d502002-04-16 15:50:10 +00006242 */
Daniel Veillard3646d642004-06-02 19:19:14 +00006243static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006244xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
6245 xmlSchemaPtr schema, xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006246{
Daniel Veillard3646d642004-06-02 19:19:14 +00006247 xmlSchemaWildcardPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00006248 xmlNodePtr child = NULL;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006249 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00006250
6251 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6252 return (NULL);
6253
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006254 ret = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
6255 node);
Daniel Veillard4255d502002-04-16 15:50:10 +00006256 if (ret == NULL) {
6257 return (NULL);
6258 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006259 /*
6260 * Check for illegal attributes.
6261 */
6262 attr = node->properties;
6263 while (attr != NULL) {
6264 if (attr->ns == NULL) {
6265 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
6266 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
6267 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006268 xmlSchemaPIllegalAttrErr(ctxt,
6269 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
6270 NULL, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006271 }
6272 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006273 xmlSchemaPIllegalAttrErr(ctxt,
6274 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
6275 NULL, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006276 }
6277 attr = attr->next;
6278 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00006279 xmlSchemaPValAttrID(ctxt, NULL, (xmlSchemaTypePtr) ret,
6280 node, BAD_CAST "id");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006281 /*
6282 * Parse the namespace list.
6283 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006284 if (xmlSchemaParseWildcardNs(ctxt, schema, ret, node) != 0)
Daniel Veillardc0826a72004-08-10 14:17:33 +00006285 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006286 /*
6287 * And now for the children...
6288 */
Daniel Veillard4255d502002-04-16 15:50:10 +00006289 child = node->children;
6290 if (IS_SCHEMA(child, "annotation")) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006291 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child);
6292 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006293 }
6294 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006295 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006296 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006297 NULL, NULL, node, child,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006298 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006299 }
6300
6301 return (ret);
6302}
6303
6304
6305/**
6306 * xmlSchemaParseAttribute:
6307 * @ctxt: a schema validation context
6308 * @schema: the schema being built
6309 * @node: a subtree containing XML Schema informations
6310 *
6311 * parse a XML schema Attrribute declaration
6312 * *WARNING* this interface is highly subject to change
6313 *
William M. Bracke7091952004-05-11 15:09:58 +00006314 * Returns the attribute declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00006315 */
6316static xmlSchemaAttributePtr
6317xmlSchemaParseAttribute(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
William M. Bracke7091952004-05-11 15:09:58 +00006318 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00006319{
Daniel Veillardc0826a72004-08-10 14:17:33 +00006320 const xmlChar *name, *attrValue;
6321 xmlChar *repName = NULL; /* The reported designation. */
Daniel Veillard4255d502002-04-16 15:50:10 +00006322 xmlSchemaAttributePtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006323 xmlNodePtr child = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006324 xmlAttrPtr attr, nameAttr;
6325 int isRef = 0;
William M. Bracke7091952004-05-11 15:09:58 +00006326
6327 /*
6328 * Note that the w3c spec assumes the schema to be validated with schema
6329 * for schemas beforehand.
6330 *
6331 * 3.2.3 Constraints on XML Representations of Attribute Declarations
William M. Bracke7091952004-05-11 15:09:58 +00006332 */
Daniel Veillard4255d502002-04-16 15:50:10 +00006333
6334 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6335 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006336 attr = xmlSchemaGetPropNode(node, "ref");
6337 nameAttr = xmlSchemaGetPropNode(node, "name");
6338
6339 if ((attr == NULL) && (nameAttr == NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006340 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006341 * 3.2.3 : 3.1
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006342 * One of ref or name must be present, but not both
Daniel Veillardc0826a72004-08-10 14:17:33 +00006343 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006344 xmlSchemaPMissingAttrErr(ctxt, XML_SCHEMAP_SRC_ATTRIBUTE_3_1,
6345 NULL, node, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006346 "One of the attributes 'ref' or 'name' must be present");
6347 return (NULL);
6348 }
6349 if ((topLevel) || (attr == NULL)) {
6350 if (nameAttr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006351 xmlSchemaPMissingAttrErr(ctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
6352 NULL, node, "name", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00006353 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006354 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006355 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006356 isRef = 1;
6357
Daniel Veillardc0826a72004-08-10 14:17:33 +00006358 if (isRef) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006359 char buf[50];
6360 const xmlChar *refNs = NULL, *ref = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006361
6362 /*
6363 * Parse as attribute reference.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006364 */
6365 if (xmlSchemaPValAttrNodeQName(ctxt, schema,
6366 (xmlChar **) &xmlSchemaElemDesAttrRef, NULL, attr, &refNs,
6367 &ref) != 0) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006368 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006369 }
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00006370 snprintf(buf, 49, "#aRef%d", ctxt->counter++ + 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006371 name = (const xmlChar *) buf;
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00006372 ret = xmlSchemaAddAttribute(ctxt, schema, name, NULL, node, 0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006373 if (ret == NULL) {
6374 if (repName != NULL)
6375 xmlFree(repName);
6376 return (NULL);
William M. Bracke7091952004-05-11 15:09:58 +00006377 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006378 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE;
6379 ret->node = node;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006380 ret->refNs = refNs;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006381 ret->ref = ref;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006382 xmlSchemaCheckReference(ctxt, schema, node, (xmlSchemaBasicItemPtr) ret,
6383 refNs);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006384 /*
6385 xmlSchemaFormatTypeRep(&repName, (xmlSchemaTypePtr) ret, NULL, NULL);
6386 */
6387 if (nameAttr != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006388 xmlSchemaPMutualExclAttrErr(ctxt, XML_SCHEMAP_SRC_ATTRIBUTE_3_1,
6389 &repName, (xmlSchemaTypePtr) ret, nameAttr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006390 "ref", "name");
6391 /*
6392 * Check for illegal attributes.
6393 */
6394 attr = node->properties;
6395 while (attr != NULL) {
6396 if (attr->ns == NULL) {
6397 if (xmlStrEqual(attr->name, BAD_CAST "type") ||
6398 xmlStrEqual(attr->name, BAD_CAST "form")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006399 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006400 * 3.2.3 : 3.2
6401 * If ref is present, then all of <simpleType>,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006402 * form and type must be absent.
Daniel Veillardc0826a72004-08-10 14:17:33 +00006403 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006404 xmlSchemaPIllegalAttrErr(ctxt,
6405 XML_SCHEMAP_SRC_ATTRIBUTE_3_2, &repName,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006406 (xmlSchemaTypePtr) ret, attr);
6407 } else if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
6408 (!xmlStrEqual(attr->name, BAD_CAST "use")) &&
6409 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006410 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
6411 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006412 (!xmlStrEqual(attr->name, BAD_CAST "default"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006413 xmlSchemaPIllegalAttrErr(ctxt,
6414 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
6415 &repName, (xmlSchemaTypePtr) ret, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006416 }
6417 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006418 xmlSchemaPIllegalAttrErr(ctxt,
6419 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
6420 &repName, (xmlSchemaTypePtr) ret, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006421 }
6422 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006423 }
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006424 } else {
William M. Bracke7091952004-05-11 15:09:58 +00006425 const xmlChar *ns = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006426
Daniel Veillardc0826a72004-08-10 14:17:33 +00006427 /*
6428 * Parse as attribute declaration.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006429 */
6430 if (xmlSchemaPValAttrNode(ctxt,
6431 (xmlChar **) &xmlSchemaElemDesAttrDecl, NULL, nameAttr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006432 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
6433 return (NULL);
William M. Bracke7091952004-05-11 15:09:58 +00006434 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006435 /*
6436 xmlSchemaFormatTypeRep(&repName, NULL, xmlSchemaElemDesAttrDecl, name);
6437 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006438 /*
6439 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
6440 * TODO: Move this to the component layer.
Daniel Veillardc0826a72004-08-10 14:17:33 +00006441 */
6442 if (xmlStrEqual(name, BAD_CAST "xmlns")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006443 xmlSchemaPSimpleTypeErr(ctxt,
6444 XML_SCHEMAP_NO_XMLNS,
6445 NULL, (xmlNodePtr) nameAttr,
6446 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
6447 "The value of type 'xs:NCName' must not match 'xmlns'",
6448 NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006449 if (repName != NULL)
6450 xmlFree(repName);
6451 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006452 }
6453 /*
6454 * Evaluate the target namespace
6455 */
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006456 if (topLevel) {
6457 ns = schema->targetNamespace;
6458 } else {
6459 attr = xmlSchemaGetPropNode(node, "form");
6460 if (attr != NULL) {
6461 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
6462 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
6463 ns = schema->targetNamespace;
6464 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006465 xmlSchemaPSimpleTypeErr(ctxt,
6466 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6467 NULL, (xmlNodePtr) attr,
6468 NULL, "(qualified | unqualified)",
6469 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006470 }
6471 } else if (schema->flags & XML_SCHEMAS_QUALIF_ATTR)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006472 ns = schema->targetNamespace;
6473 }
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00006474 ret = xmlSchemaAddAttribute(ctxt, schema, name, ns, node, topLevel);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006475 if (ret == NULL) {
6476 if (repName != NULL)
6477 xmlFree(repName);
Daniel Veillard3646d642004-06-02 19:19:14 +00006478 return (NULL);
William M. Bracke7091952004-05-11 15:09:58 +00006479 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006480 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006481 ret->node = node;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006482 if (topLevel)
6483 ret->flags |= XML_SCHEMAS_ATTR_GLOBAL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006484 /*
6485 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
6486 * TODO: Move this to the component layer.
6487 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00006488 if (xmlStrEqual(ret->targetNamespace, xmlSchemaInstanceNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006489 xmlSchemaPCustomErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006490 XML_SCHEMAP_NO_XSI,
6491 &repName, (xmlSchemaTypePtr) ret, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006492 "The target namespace must not match '%s'",
6493 xmlSchemaInstanceNs);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006494 }
6495 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006496 * Check for illegal attributes.
6497 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00006498 attr = node->properties;
6499 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006500 if (attr->ns == NULL) {
6501 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
6502 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
6503 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006504 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
6505 (!xmlStrEqual(attr->name, BAD_CAST "type"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006506 if ((topLevel) ||
Daniel Veillardc0826a72004-08-10 14:17:33 +00006507 ((!xmlStrEqual(attr->name, BAD_CAST "form")) &&
6508 (!xmlStrEqual(attr->name, BAD_CAST "use")))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006509 xmlSchemaPIllegalAttrErr(ctxt,
6510 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
6511 &repName, (xmlSchemaTypePtr) ret, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006512 }
6513 }
6514 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006515 xmlSchemaPIllegalAttrErr(ctxt, XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
6516 &repName, (xmlSchemaTypePtr) ret, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006517 }
6518 attr = attr->next;
6519 }
6520 xmlSchemaPValAttrQName(ctxt, schema, &repName, (xmlSchemaTypePtr) ret,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006521 node, "type", &ret->typeNs, &ret->typeName);
6522 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00006523 xmlSchemaPValAttrID(ctxt, NULL, (xmlSchemaTypePtr) ret,
6524 node, BAD_CAST "id");
William M. Bracke7091952004-05-11 15:09:58 +00006525 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006526 * Attribute "fixed".
William M. Bracke7091952004-05-11 15:09:58 +00006527 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00006528 ret->defValue = xmlSchemaGetProp(ctxt, node, "fixed");
6529 if (ret->defValue != NULL)
6530 ret->flags |= XML_SCHEMAS_ATTR_FIXED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006531 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006532 * Attribute "default".
6533 */
6534 attr = xmlSchemaGetPropNode(node, "default");
6535 if (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006536 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006537 * 3.2.3 : 1
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006538 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00006539 */
6540 if (ret->flags & XML_SCHEMAS_ATTR_FIXED) {
6541 xmlSchemaPMutualExclAttrErr(ctxt, XML_SCHEMAP_SRC_ATTRIBUTE_1,
6542 &repName, (xmlSchemaTypePtr) ret, attr, "default", "fixed");
6543 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006544 ret->defValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
6545 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006546 if (topLevel == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006547 /*
6548 * Attribute "use".
Daniel Veillardc0826a72004-08-10 14:17:33 +00006549 */
6550 attr = xmlSchemaGetPropNode(node, "use");
6551 if (attr != NULL) {
6552 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
6553 if (xmlStrEqual(attrValue, BAD_CAST "optional"))
6554 ret->occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
6555 else if (xmlStrEqual(attrValue, BAD_CAST "prohibited"))
6556 ret->occurs = XML_SCHEMAS_ATTR_USE_PROHIBITED;
6557 else if (xmlStrEqual(attrValue, BAD_CAST "required"))
6558 ret->occurs = XML_SCHEMAS_ATTR_USE_REQUIRED;
6559 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006560 xmlSchemaPSimpleTypeErr(ctxt,
6561 XML_SCHEMAP_INVALID_ATTR_USE,
6562 (xmlSchemaTypePtr) ret, (xmlNodePtr) attr,
6563 NULL, "(optional | prohibited | required)",
6564 attrValue, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006565 } else
6566 ret->occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006567 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006568 * 3.2.3 : 2
6569 * If default and use are both present, use must have
6570 * the actual value optional.
6571 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006572 if ((ret->occurs != XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
6573 (ret->defValue != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006574 ((ret->flags & XML_SCHEMAS_ATTR_FIXED) == 0)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006575 xmlSchemaPSimpleTypeErr(ctxt,
6576 XML_SCHEMAP_SRC_ATTRIBUTE_2,
6577 (xmlSchemaTypePtr) ret, (xmlNodePtr) attr,
6578 NULL, "(optional | prohibited | required)", NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006579 "The value must be 'optional' if the attribute "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006580 "'default' is present as well", NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006581 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006582 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006583 /*
6584 * And now for the children...
6585 */
Daniel Veillard4255d502002-04-16 15:50:10 +00006586 child = node->children;
6587 if (IS_SCHEMA(child, "annotation")) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006588 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child);
6589 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006590 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006591 if (isRef) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006592 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006593 if (IS_SCHEMA(child, "simpleType"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006594 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006595 * 3.2.3 : 3.2
6596 * If ref is present, then all of <simpleType>,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006597 * form and type must be absent.
Daniel Veillardc0826a72004-08-10 14:17:33 +00006598 */
6599 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_SRC_ATTRIBUTE_3_2,
6600 &repName, (xmlSchemaTypePtr) ret, node, child, NULL,
6601 "(annotation?)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006602 else
Daniel Veillardc0826a72004-08-10 14:17:33 +00006603 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
6604 &repName, (xmlSchemaTypePtr) ret, node, child, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006605 "(annotation?)");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006606 }
6607 } else {
6608 if (IS_SCHEMA(child, "simpleType")) {
6609 if (ret->typeName != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006610 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006611 * 3.2.3 : 4
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006612 * type and <simpleType> must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00006613 */
6614 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
6615 &repName, (xmlSchemaTypePtr) ret, node, child,
6616 "The attribute 'type' and the <simpleType> child "
6617 "are mutually exclusive", NULL);
6618 } else
6619 ret->subtypes = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
6620 child = child->next;
6621 }
6622 if (child != NULL)
6623 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
6624 &repName, (xmlSchemaTypePtr) ret, node, child, NULL,
6625 "(annotation?, simpleType?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006626 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006627 /*
6628 * Cleanup.
6629 */
6630 if (repName != NULL)
6631 xmlFree(repName);
Daniel Veillard4255d502002-04-16 15:50:10 +00006632 return (ret);
6633}
6634
6635/**
6636 * xmlSchemaParseAttributeGroup:
6637 * @ctxt: a schema validation context
6638 * @schema: the schema being built
6639 * @node: a subtree containing XML Schema informations
6640 *
6641 * parse a XML schema Attribute Group declaration
6642 * *WARNING* this interface is highly subject to change
6643 *
6644 * Returns the attribute group or NULL in case of error.
6645 */
6646static xmlSchemaAttributeGroupPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006647xmlSchemaParseAttributeGroup(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +00006648 xmlSchemaPtr schema, xmlNodePtr node,
6649 int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00006650{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006651 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00006652 xmlSchemaAttributeGroupPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00006653 xmlNodePtr child = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006654 const xmlChar *oldcontainer;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006655 xmlAttrPtr attr, nameAttr;
Daniel Veillard4255d502002-04-16 15:50:10 +00006656
6657 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6658 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006659
6660 nameAttr = xmlSchemaGetPropNode(node, "name");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006661 attr = xmlSchemaGetPropNode(node, "ref");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006662 if ((topLevel) || (attr == NULL)) {
6663 /*
6664 * Parse as an attribute group definition.
6665 * Note that those are allowed at top level only.
6666 */
6667 if (nameAttr == NULL) {
6668 xmlSchemaPMissingAttrErr(ctxt,
6669 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006670 NULL, node, "name", NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006671 return (NULL);
6672 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006673 /* REDUNDANT: name = xmlSchemaGetNodeContent(ctxt,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00006674 * (xmlNodePtr) nameAttr);
6675 */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006676 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006677 * The name is crucial, exit if invalid.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006678 */
6679 if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006680 NULL, NULL, nameAttr,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006681 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
6682 return (NULL);
6683 }
6684 ret = xmlSchemaAddAttributeGroup(ctxt, schema, name, node);
6685 if (ret == NULL)
6686 return (NULL);
6687 ret->type = XML_SCHEMA_TYPE_ATTRIBUTEGROUP;
6688 ret->flags |= XML_SCHEMAS_ATTRGROUP_GLOBAL;
6689 ret->node = node;
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006690 ret->targetNamespace = schema->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006691 } else {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006692 char buf[50];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006693 const xmlChar *refNs = NULL, *ref = NULL;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006694
6695 /*
6696 * Parse as an attribute group definition reference.
6697 */
6698 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006699 xmlSchemaPMissingAttrErr(ctxt,
6700 XML_SCHEMAP_S4S_ATTR_MISSING,
6701 NULL, node, "ref", NULL);
6702 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006703 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006704 NULL, NULL, attr, &refNs,&ref);
6705
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00006706 snprintf(buf, 49, "#agRef%d", ctxt->counter++ + 1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006707 name = (const xmlChar *) buf;
6708 if (name == NULL) {
6709 xmlSchemaPErrMemory(ctxt, "creating internal name for an "
6710 "attribute group definition reference", node);
6711 return (NULL);
6712 }
6713 ret = xmlSchemaAddAttributeGroup(ctxt, schema, name, node);
6714 if (ret == NULL)
6715 return (NULL);
6716 ret->type = XML_SCHEMA_TYPE_ATTRIBUTEGROUP;
6717 ret->ref = ref;
6718 ret->refNs = refNs;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006719 ret->node = node;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006720 xmlSchemaCheckReference(ctxt, schema, node,
6721 (xmlSchemaBasicItemPtr) ret, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006722 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006723 /*
6724 * Check for illegal attributes.
6725 */
6726 attr = node->properties;
6727 while (attr != NULL) {
6728 if (attr->ns == NULL) {
6729 if ((((topLevel == 0) && (!xmlStrEqual(attr->name, BAD_CAST "ref"))) ||
6730 (topLevel && (!xmlStrEqual(attr->name, BAD_CAST "name")))) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006731 (!xmlStrEqual(attr->name, BAD_CAST "id")))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006732 {
6733 xmlSchemaPIllegalAttrErr(ctxt,
6734 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
6735 NULL, NULL, attr);
6736 }
6737 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
6738 xmlSchemaPIllegalAttrErr(ctxt,
6739 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
6740 NULL, NULL, attr);
6741 }
6742 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006743 }
6744 /* TODO: Validate "id" ? */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006745 /*
6746 * And now for the children...
6747 */
Daniel Veillard4255d502002-04-16 15:50:10 +00006748 oldcontainer = ctxt->container;
Daniel Veillard4255d502002-04-16 15:50:10 +00006749 ctxt->container = name;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006750 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00006751 if (IS_SCHEMA(child, "annotation")) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006752 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child);
6753 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006754 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006755 if (topLevel) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006756 child = xmlSchemaParseAttrDecls(ctxt, schema, child, (xmlSchemaTypePtr) ret);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006757 if (IS_SCHEMA(child, "anyAttribute")) {
6758 ret->attributeWildcard = xmlSchemaParseAnyAttribute(ctxt, schema, child);
6759 child = child->next;
6760 }
6761 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006762 if (child != NULL) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006763 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006764 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
6765 NULL, NULL, node, child, NULL,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006766 "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006767 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006768 ctxt->container = oldcontainer;
6769 return (ret);
6770}
6771
6772/**
William M. Brack2f2a6632004-08-20 23:09:47 +00006773 * xmlSchemaPValAttrFormDefault:
6774 * @value: the value
6775 * @flags: the flags to be modified
6776 * @flagQualified: the specific flag for "qualified"
6777 *
6778 * Returns 0 if the value is valid, 1 otherwise.
6779 */
6780static int
6781xmlSchemaPValAttrFormDefault(const xmlChar *value,
6782 int *flags,
6783 int flagQualified)
6784{
6785 if (xmlStrEqual(value, BAD_CAST "qualified")) {
6786 if ((*flags & flagQualified) == 0)
6787 *flags |= flagQualified;
6788 } else if (!xmlStrEqual(value, BAD_CAST "unqualified"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006789 return (1);
6790
William M. Brack2f2a6632004-08-20 23:09:47 +00006791 return (0);
6792}
6793
6794/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006795 * xmlSchemaPValAttrBlockFinal:
6796 * @value: the value
6797 * @flags: the flags to be modified
6798 * @flagAll: the specific flag for "#all"
6799 * @flagExtension: the specific flag for "extension"
6800 * @flagRestriction: the specific flag for "restriction"
6801 * @flagSubstitution: the specific flag for "substitution"
6802 * @flagList: the specific flag for "list"
6803 * @flagUnion: the specific flag for "union"
6804 *
6805 * Validates the value of the attribute "final" and "block". The value
6806 * is converted into the specified flag values and returned in @flags.
6807 *
6808 * Returns 0 if the value is valid, 1 otherwise.
6809 */
6810
6811static int
6812xmlSchemaPValAttrBlockFinal(const xmlChar *value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006813 int *flags,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006814 int flagAll,
6815 int flagExtension,
6816 int flagRestriction,
6817 int flagSubstitution,
6818 int flagList,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006819 int flagUnion)
Daniel Veillardc0826a72004-08-10 14:17:33 +00006820{
6821 int ret = 0;
6822
6823 /*
6824 * TODO: This does not check for dublicate entries.
6825 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00006826 if ((flags == NULL) || (value == NULL))
6827 return (-1);
6828 if (value[0] == 0)
6829 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006830 if (xmlStrEqual(value, BAD_CAST "#all")) {
6831 if (flagAll != -1)
6832 *flags |= flagAll;
6833 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006834 if (flagExtension != -1)
6835 *flags |= flagExtension;
6836 if (flagRestriction != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00006837 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006838 if (flagSubstitution != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00006839 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006840 if (flagList != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00006841 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006842 if (flagUnion != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00006843 *flags |= flagUnion;
6844 }
6845 } else {
6846 const xmlChar *end, *cur = value;
6847 xmlChar *item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006848
Daniel Veillardc0826a72004-08-10 14:17:33 +00006849 do {
6850 while (IS_BLANK_CH(*cur))
6851 cur++;
6852 end = cur;
6853 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
6854 end++;
6855 if (end == cur)
6856 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006857 item = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006858 if (xmlStrEqual(item, BAD_CAST "extension")) {
6859 if (flagExtension != -1) {
6860 if ((*flags & flagExtension) == 0)
6861 *flags |= flagExtension;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006862 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00006863 ret = 1;
6864 } else if (xmlStrEqual(item, BAD_CAST "restriction")) {
6865 if (flagRestriction != -1) {
6866 if ((*flags & flagRestriction) == 0)
6867 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006868 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00006869 ret = 1;
6870 } else if (xmlStrEqual(item, BAD_CAST "substitution")) {
6871 if (flagSubstitution != -1) {
6872 if ((*flags & flagSubstitution) == 0)
6873 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006874 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00006875 ret = 1;
6876 } else if (xmlStrEqual(item, BAD_CAST "list")) {
6877 if (flagList != -1) {
6878 if ((*flags & flagList) == 0)
6879 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006880 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00006881 ret = 1;
6882 } else if (xmlStrEqual(item, BAD_CAST "union")) {
6883 if (flagUnion != -1) {
6884 if ((*flags & flagUnion) == 0)
6885 *flags |= flagUnion;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006886 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00006887 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006888 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00006889 ret = 1;
6890 if (item != NULL)
6891 xmlFree(item);
6892 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006893 } while ((ret == 0) && (*cur != 0));
6894 }
6895
Daniel Veillardc0826a72004-08-10 14:17:33 +00006896 return (ret);
6897}
6898
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00006899static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006900xmlSchemaCheckCSelectorXPath(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00006901 xmlSchemaIDCPtr idc,
6902 xmlSchemaIDCSelectPtr selector,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00006903 xmlAttrPtr attr,
6904 int isField)
6905{
6906 xmlNodePtr node;
6907
6908 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006909 * c-selector-xpath:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00006910 * Schema Component Constraint: Selector Value OK
6911 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006912 * TODO: 1 The {selector} must be a valid XPath expression, as defined
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00006913 * in [XPath].
6914 */
6915 if (selector == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006916 xmlSchemaPErr(ctxt, idc->node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00006917 XML_SCHEMAP_INTERNAL,
6918 "Internal error: xmlSchemaCheckCSelectorXPath, "
6919 "the selector is not specified.\n", NULL, NULL);
6920 return (-1);
6921 }
6922 if (attr == NULL)
6923 node = idc->node;
6924 else
6925 node = (xmlNodePtr) attr;
6926 if (selector->xpath == NULL) {
6927 xmlSchemaPCustomErr(ctxt,
6928 /* TODO: Adjust error code. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006929 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6930 NULL, NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00006931 "The XPath expression of the selector is not valid", NULL);
6932 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
6933 } else {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00006934 const xmlChar **nsArray = NULL;
6935 xmlNsPtr *nsList = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00006936 /*
6937 * Compile the XPath expression.
6938 */
6939 /*
6940 * TODO: We need the array of in-scope namespaces for compilation.
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00006941 * TODO: Call xmlPatterncompile with different options for selector/
6942 * field.
6943 */
6944 nsList = xmlGetNsList(attr->doc, attr->parent);
6945 /*
6946 * Build an array of prefixes and namespaces.
6947 */
6948 if (nsList != NULL) {
6949 int i, count = 0;
6950 xmlNsPtr ns;
6951
6952 for (i = 0; nsList[i] != NULL; i++)
6953 count++;
6954
6955 nsArray = (const xmlChar **) xmlMalloc(
6956 (count * 2 + 1) * sizeof(const xmlChar *));
6957 if (nsArray == NULL) {
6958 xmlSchemaPErrMemory(ctxt, "allocating a namespace array",
6959 NULL);
6960 return (-1);
6961 }
6962 for (i = 0; i < count; i++) {
6963 ns = nsList[i];
6964 nsArray[2 * i] = nsList[i]->href;
6965 nsArray[2 * i + 1] = nsList[i]->prefix;
6966 }
6967 nsArray[count * 2] = NULL;
6968 xmlFree(nsList);
6969 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006970 /*
6971 * TODO: Differentiate between "selector" and "field".
6972 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00006973 if (isField)
6974 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00006975 NULL, XML_PATTERN_XSFIELD, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00006976 else
6977 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00006978 NULL, XML_PATTERN_XSSEL, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00006979 if (nsArray != NULL)
6980 xmlFree((xmlChar **) nsArray);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006981
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00006982 if (selector->xpathComp == NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00006983 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00006984 /* TODO: Adjust error code? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006985 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6986 NULL, NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00006987 "The XPath expression '%s' could not be "
6988 "compiled", selector->xpath);
6989 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006990 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00006991 }
6992 return (0);
6993}
6994
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006995#define ADD_ANNOTATION(annot) \
6996 xmlSchemaAnnotPtr cur = item->annot; \
6997 if (item->annot == NULL) { \
6998 item->annot = annot; \
6999 return (annot); \
7000 } \
7001 cur = item->annot; \
7002 if (cur->next != NULL) { \
7003 cur = cur->next; \
7004 } \
7005 cur->next = annot;
7006
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007007/**
7008 * xmlSchemaAssignAnnotation:
7009 * @item: the schema component
7010 * @annot: the annotation
7011 *
7012 * Adds the annotation to the given schema component.
7013 *
7014 * Returns the given annotaion.
7015 */
7016static xmlSchemaAnnotPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007017xmlSchemaAddAnnotation(xmlSchemaAnnotItemPtr annItem,
7018 xmlSchemaAnnotPtr annot)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007019{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007020 if ((annItem == NULL) || (annot == NULL))
7021 return (NULL);
7022 switch (annItem->type) {
7023 case XML_SCHEMA_TYPE_ELEMENT: {
7024 xmlSchemaElementPtr item = (xmlSchemaElementPtr) annItem;
7025 ADD_ANNOTATION(annot)
7026 }
7027 break;
7028 case XML_SCHEMA_TYPE_ATTRIBUTE: {
7029 xmlSchemaAttributePtr item = (xmlSchemaAttributePtr) annItem;
7030 ADD_ANNOTATION(annot)
7031 }
7032 break;
7033 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
7034 case XML_SCHEMA_TYPE_ANY: {
7035 xmlSchemaWildcardPtr item = (xmlSchemaWildcardPtr) annItem;
7036 ADD_ANNOTATION(annot)
7037 }
7038 break;
7039 case XML_SCHEMA_TYPE_PARTICLE:
7040 case XML_SCHEMA_TYPE_IDC_KEY:
7041 case XML_SCHEMA_TYPE_IDC_KEYREF:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007042 case XML_SCHEMA_TYPE_IDC_UNIQUE: {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007043 xmlSchemaAnnotItemPtr item = (xmlSchemaAnnotItemPtr) annItem;
7044 ADD_ANNOTATION(annot)
7045 }
7046 break;
7047 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP: {
7048 xmlSchemaAttributeGroupPtr item =
7049 (xmlSchemaAttributeGroupPtr) annItem;
7050 ADD_ANNOTATION(annot)
7051 }
7052 break;
7053 case XML_SCHEMA_TYPE_NOTATION: {
7054 xmlSchemaNotationPtr item = (xmlSchemaNotationPtr) annItem;
7055 ADD_ANNOTATION(annot)
7056 }
7057 break;
7058 case XML_SCHEMA_FACET_MININCLUSIVE:
7059 case XML_SCHEMA_FACET_MINEXCLUSIVE:
7060 case XML_SCHEMA_FACET_MAXINCLUSIVE:
7061 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
7062 case XML_SCHEMA_FACET_TOTALDIGITS:
7063 case XML_SCHEMA_FACET_FRACTIONDIGITS:
7064 case XML_SCHEMA_FACET_PATTERN:
7065 case XML_SCHEMA_FACET_ENUMERATION:
7066 case XML_SCHEMA_FACET_WHITESPACE:
7067 case XML_SCHEMA_FACET_LENGTH:
7068 case XML_SCHEMA_FACET_MAXLENGTH:
7069 case XML_SCHEMA_FACET_MINLENGTH: {
7070 xmlSchemaFacetPtr item = (xmlSchemaFacetPtr) annItem;
7071 ADD_ANNOTATION(annot)
7072 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007073 break;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007074 case XML_SCHEMA_TYPE_SIMPLE:
7075 case XML_SCHEMA_TYPE_COMPLEX: {
7076 xmlSchemaTypePtr item = (xmlSchemaTypePtr) annItem;
7077 ADD_ANNOTATION(annot)
7078 }
7079 break;
7080 case XML_SCHEMA_TYPE_GROUP: {
7081 xmlSchemaModelGroupDefPtr item = (xmlSchemaModelGroupDefPtr) annItem;
7082 ADD_ANNOTATION(annot)
7083 }
7084 break;
7085 case XML_SCHEMA_TYPE_SEQUENCE:
7086 case XML_SCHEMA_TYPE_CHOICE:
7087 case XML_SCHEMA_TYPE_ALL: {
7088 xmlSchemaModelGroupPtr item = (xmlSchemaModelGroupPtr) annItem;
7089 ADD_ANNOTATION(annot)
7090 }
7091 break;
7092 default:
7093 xmlSchemaPCustomErr(NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007094 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007095 NULL, NULL, NULL,
7096 "Internal error: xmlSchemaAddAnnotation, "
7097 "The item is not a annotated schema component", NULL);
7098 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007099 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007100 return (annot);
7101}
7102
7103/**
7104 * xmlSchemaParseIDCSelectorAndField:
7105 * @ctxt: a schema validation context
7106 * @schema: the schema being built
7107 * @node: a subtree containing XML Schema informations
7108 *
7109 * Parses a XML Schema identity-contraint definition's
7110 * <selector> and <field> elements.
7111 *
7112 * Returns the parsed identity-constraint definition.
7113 */
7114static xmlSchemaIDCSelectPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007115xmlSchemaParseIDCSelectorAndField(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007116 xmlSchemaPtr schema,
7117 xmlSchemaIDCPtr idc,
7118 xmlNodePtr node,
7119 int isField)
7120{
7121 xmlSchemaIDCSelectPtr item;
7122 xmlNodePtr child = NULL;
7123 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007124
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007125 /*
7126 * Check for illegal attributes.
7127 */
7128 attr = node->properties;
7129 while (attr != NULL) {
7130 if (attr->ns == NULL) {
7131 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
7132 (!xmlStrEqual(attr->name, BAD_CAST "xpath"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007133 xmlSchemaPIllegalAttrErr(ctxt,
7134 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
7135 NULL, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007136 }
7137 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007138 xmlSchemaPIllegalAttrErr(ctxt,
7139 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
7140 NULL, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007141 }
7142 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007143 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007144 /*
7145 * Create the item.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007146 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007147 item = (xmlSchemaIDCSelectPtr) xmlMalloc(sizeof(xmlSchemaIDCSelect));
7148 if (item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007149 xmlSchemaPErrMemory(ctxt,
7150 "allocating a 'selector' of an identity-constraint definition",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007151 NULL);
7152 return (NULL);
7153 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007154 memset(item, 0, sizeof(xmlSchemaIDCSelect));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007155 /*
7156 * Attribute "xpath" (mandatory).
7157 */
7158 attr = xmlSchemaGetPropNode(node, "xpath");
7159 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007160 xmlSchemaPMissingAttrErr(ctxt,
7161 XML_SCHEMAP_S4S_ATTR_MISSING,
7162 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007163 "name", NULL);
7164 } else {
7165 item->xpath = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
7166 /*
7167 * URGENT TODO: "field"s have an other syntax than "selector"s.
7168 */
7169
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007170 if (xmlSchemaCheckCSelectorXPath(ctxt, idc, item, attr,
7171 isField) == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007172 xmlSchemaPErr(ctxt,
7173 (xmlNodePtr) attr,
7174 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007175 "Internal error: xmlSchemaParseIDCSelectorAndField, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007176 "validating the XPath expression of a IDC selector.\n",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007177 NULL, NULL);
7178 }
7179
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007180 }
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00007181 xmlSchemaPValAttrID(ctxt, NULL, NULL, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007182 /*
7183 * And now for the children...
7184 */
7185 child = node->children;
7186 if (IS_SCHEMA(child, "annotation")) {
7187 /*
7188 * Add the annotation to the parent IDC.
7189 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007190 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) idc,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007191 xmlSchemaParseAnnotation(ctxt, schema, child));
7192 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007193 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007194 if (child != NULL) {
7195 xmlSchemaPContentErr(ctxt,
7196 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007197 NULL, NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007198 NULL, "(annotation?)");
7199 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007200
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007201 return (item);
7202}
7203
7204/**
7205 * xmlSchemaParseIDC:
7206 * @ctxt: a schema validation context
7207 * @schema: the schema being built
7208 * @node: a subtree containing XML Schema informations
7209 *
7210 * Parses a XML Schema identity-contraint definition.
7211 *
7212 * Returns the parsed identity-constraint definition.
7213 */
7214static xmlSchemaIDCPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007215xmlSchemaParseIDC(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007216 xmlSchemaPtr schema,
7217 xmlNodePtr node,
7218 xmlSchemaTypeType idcCategory,
7219 const xmlChar *targetNamespace)
7220{
7221 xmlSchemaIDCPtr item = NULL;
7222 xmlNodePtr child = NULL;
7223 xmlAttrPtr attr;
7224 const xmlChar *name = NULL;
7225 xmlSchemaIDCSelectPtr field = NULL, lastField = NULL;
7226 int resAdd;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007227
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007228 /*
7229 * Check for illegal attributes.
7230 */
7231 attr = node->properties;
7232 while (attr != NULL) {
7233 if (attr->ns == NULL) {
7234 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
7235 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7236 ((idcCategory != XML_SCHEMA_TYPE_IDC_KEYREF) ||
7237 (!xmlStrEqual(attr->name, BAD_CAST "refer")))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007238 xmlSchemaPIllegalAttrErr(ctxt,
7239 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
7240 NULL, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007241 }
7242 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007243 xmlSchemaPIllegalAttrErr(ctxt,
7244 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
7245 NULL, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007246 }
7247 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007248 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007249 /*
7250 * Attribute "name" (mandatory).
7251 */
7252 attr = xmlSchemaGetPropNode(node, "name");
7253 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007254 xmlSchemaPMissingAttrErr(ctxt,
7255 XML_SCHEMAP_S4S_ATTR_MISSING,
7256 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007257 "name", NULL);
7258 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007259 } else if (xmlSchemaPValAttrNode(ctxt,
7260 NULL, NULL, attr,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007261 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7262 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007263 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007264 /*
7265 * Create the component.
7266 */
7267 if (schema->idcDef == NULL)
7268 schema->idcDef = xmlHashCreateDict(10, ctxt->dict);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007269 if (schema->idcDef == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007270 return (NULL);
7271
7272 item = (xmlSchemaIDCPtr) xmlMalloc(sizeof(xmlSchemaIDC));
7273 if (item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007274 xmlSchemaPErrMemory(ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007275 "allocating an identity-constraint definition", NULL);
7276 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007277 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007278 /*
7279 * Add the IDC to the list of IDCs on the schema component.
7280 */
7281 resAdd = xmlHashAddEntry2(schema->idcDef, name, targetNamespace, item);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007282 if (resAdd != 0) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007283 xmlSchemaPCustomErrExt(ctxt,
7284 XML_SCHEMAP_REDEFINED_TYPE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007285 NULL, NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007286 "An identity-constraint definition with the name '%s' "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007287 "and targetNamespace '%s' does already exist",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007288 name, targetNamespace, NULL);
7289 xmlFree(item);
7290 return (NULL);
7291 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007292 memset(item, 0, sizeof(xmlSchemaIDC));
7293 item->name = name;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007294 item->type = idcCategory;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007295 item->node = node;
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +00007296 if (ctxt->assemble != NULL)
7297 xmlSchemaAddAssembledItem(ctxt, (xmlSchemaTypePtr) item);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007298 /*
7299 * The target namespace of the parent element declaration.
7300 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007301 item->targetNamespace = targetNamespace;
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00007302 xmlSchemaPValAttrID(ctxt, NULL, (xmlSchemaTypePtr) item,
7303 node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007304 if (idcCategory == XML_SCHEMA_TYPE_IDC_KEYREF) {
7305 /*
7306 * Attribute "refer" (mandatory).
7307 */
7308 attr = xmlSchemaGetPropNode(node, "refer");
7309 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007310 xmlSchemaPMissingAttrErr(ctxt,
7311 XML_SCHEMAP_S4S_ATTR_MISSING,
7312 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007313 "refer", NULL);
7314 } else {
7315 /*
7316 * Create a reference item.
7317 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007318 item->ref = xmlSchemaNewQNameRef(schema, XML_SCHEMA_TYPE_IDC_KEY,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007319 NULL, NULL);
7320 if (item->ref == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007321 return (NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007322 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007323 NULL, NULL, attr,
7324 &(item->ref->targetNamespace),
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007325 &(item->ref->name));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007326 xmlSchemaCheckReference(ctxt, schema, node,
7327 (xmlSchemaBasicItemPtr) item,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00007328 item->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007329 }
7330 }
7331 /*
7332 * And now for the children...
7333 */
7334 child = node->children;
7335 if (IS_SCHEMA(child, "annotation")) {
7336 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child);
7337 child = child->next;
7338 }
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00007339 if (child == NULL) {
7340 xmlSchemaPContentErr(ctxt,
7341 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007342 NULL, NULL, node, child,
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00007343 "A child element is missing",
7344 "(annotation?, (selector, field+))");
7345 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007346 /*
7347 * Child element <selector>.
7348 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007349 if (IS_SCHEMA(child, "selector")) {
7350 item->selector = xmlSchemaParseIDCSelectorAndField(ctxt, schema,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007351 item, child, 0);
7352 child = child->next;
7353 /*
7354 * Child elements <field>.
7355 */
7356 if (IS_SCHEMA(child, "field")) {
7357 do {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007358 field = xmlSchemaParseIDCSelectorAndField(ctxt, schema,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007359 item, child, 1);
7360 if (field != NULL) {
7361 field->index = item->nbFields;
7362 item->nbFields++;
7363 if (lastField != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007364 lastField->next = field;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007365 else
7366 item->fields = field;
7367 lastField = field;
7368 }
7369 child = child->next;
7370 } while (IS_SCHEMA(child, "field"));
7371 } else {
7372 xmlSchemaPContentErr(ctxt,
7373 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007374 NULL, NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007375 NULL, "(annotation?, (selector, field+))");
7376 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007377 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007378 if (child != NULL) {
7379 xmlSchemaPContentErr(ctxt,
7380 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007381 NULL, NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007382 NULL, "(annotation?, (selector, field+))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007383 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007384
7385 return (item);
7386}
7387
Daniel Veillardc0826a72004-08-10 14:17:33 +00007388/**
Daniel Veillard4255d502002-04-16 15:50:10 +00007389 * xmlSchemaParseElement:
7390 * @ctxt: a schema validation context
7391 * @schema: the schema being built
7392 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007393 * @topLevel: indicates if this is global declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00007394 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007395 * Parses a XML schema element declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00007396 * *WARNING* this interface is highly subject to change
7397 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007398 * Returns the element declaration or a particle; NULL in case
7399 * of an error or if the particle has minOccurs==maxOccurs==0.
Daniel Veillard4255d502002-04-16 15:50:10 +00007400 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007401static xmlSchemaBasicItemPtr
Daniel Veillard4255d502002-04-16 15:50:10 +00007402xmlSchemaParseElement(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00007403 xmlNodePtr node, int topLevel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007404{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007405 xmlSchemaElementPtr decl = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007406 xmlSchemaParticlePtr particle = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007407 xmlSchemaAnnotPtr annot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007408 xmlNodePtr child = NULL;
7409 xmlAttrPtr attr, nameAttr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007410 int min, max, isRef = 0;
7411 xmlChar *des = NULL;
William M. Bracke7091952004-05-11 15:09:58 +00007412
7413 /* 3.3.3 Constraints on XML Representations of Element Declarations */
7414 /* TODO: Complete implementation of 3.3.6 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007415
Daniel Veillard4255d502002-04-16 15:50:10 +00007416 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007417 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007418 /*
7419 * If we get a "ref" attribute on a local <element> we will assume it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007420 * a reference - even if there's a "name" attribute; this seems to be more
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007421 * robust.
7422 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00007423 nameAttr = xmlSchemaGetPropNode(node, "name");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007424 attr = xmlSchemaGetPropNode(node, "ref");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007425 if ((topLevel) || (attr == NULL)) {
7426 if (nameAttr == NULL) {
7427 xmlSchemaPMissingAttrErr(ctxt,
7428 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007429 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007430 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007431 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007432 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007433 isRef = 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +00007434
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007435 xmlSchemaPValAttrID(ctxt, NULL, NULL, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007436 child = node->children;
7437 if (IS_SCHEMA(child, "annotation")) {
7438 annot = xmlSchemaParseAnnotation(ctxt, schema, child);
7439 child = child->next;
7440 }
7441 /*
7442 * Skip particle part if a global declaration.
7443 */
7444 if (topLevel)
7445 goto declaration_part;
7446 /*
7447 * The particle part ==================================================
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007448 */
7449 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
7450 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1, "(xs:nonNegativeInteger | unbounded)");
7451 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007452 particle = xmlSchemaAddParticle(ctxt, schema, node, min, max);
7453 if (particle == NULL)
7454 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007455
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007456 /* ret->flags |= XML_SCHEMAS_ELEM_REF; */
7457
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007458 if (isRef) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007459 const xmlChar *refNs = NULL, *ref = NULL;
7460 xmlSchemaQNameRefPtr refer = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00007461 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007462 * The reference part =============================================
Daniel Veillardc0826a72004-08-10 14:17:33 +00007463 */
7464 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007465 NULL, NULL, attr, &refNs, &ref);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007466 xmlSchemaCheckReference(ctxt, schema, node, NULL, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007467 /*
7468 * SPEC (3.3.3 : 2.1) "One of ref or name must be present, but not both"
Daniel Veillardc0826a72004-08-10 14:17:33 +00007469 */
7470 if (nameAttr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007471 xmlSchemaPMutualExclAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007472 XML_SCHEMAP_SRC_ELEMENT_2_1,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007473 NULL, NULL, nameAttr, "ref", "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007474 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007475 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007476 * Check for illegal attributes.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007477 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00007478 attr = node->properties;
7479 while (attr != NULL) {
7480 if (attr->ns == NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00007481 if (xmlStrEqual(attr->name, BAD_CAST "ref") ||
7482 xmlStrEqual(attr->name, BAD_CAST "name") ||
7483 xmlStrEqual(attr->name, BAD_CAST "id") ||
7484 xmlStrEqual(attr->name, BAD_CAST "maxOccurs") ||
7485 xmlStrEqual(attr->name, BAD_CAST "minOccurs"))
7486 {
7487 attr = attr->next;
7488 continue;
7489 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007490 /* SPEC (3.3.3 : 2.2) */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007491 xmlSchemaPCustomAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007492 XML_SCHEMAP_SRC_ELEMENT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007493 NULL, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007494 "Only the attributes 'minOccurs', 'maxOccurs' and "
7495 "'id' are allowed in addition to 'ref'");
7496 break;
7497 }
7498 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7499 xmlSchemaPIllegalAttrErr(ctxt,
7500 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007501 NULL, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007502 }
7503 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007504 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00007505 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007506 * No children except <annotation> expected.
Daniel Veillardc0826a72004-08-10 14:17:33 +00007507 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007508 if (child != NULL) {
7509 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7510 NULL, NULL, node, child, NULL, "(annotation?)");
7511 }
7512 if ((min == 0) && (max == 0))
7513 goto return_null;
7514 /*
7515 * Create the reference item.
7516 */
7517 refer = xmlSchemaNewQNameRef(schema, XML_SCHEMA_TYPE_ELEMENT,
7518 ref, refNs);
7519 if (refer == NULL)
7520 goto return_null;
7521 particle->children = (xmlSchemaTreeItemPtr) refer;
7522 particle->annot = annot;
7523 /*
7524 * Add to assembled items; the reference need to be resolved.
7525 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007526 if (ctxt->assemble != NULL)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007527 xmlSchemaAddAssembledItem(ctxt, (xmlSchemaTypePtr) particle);
7528
7529 return ((xmlSchemaBasicItemPtr) particle);
7530 }
7531 /*
7532 * The declaration part ===============================================
7533 */
7534declaration_part:
7535 {
7536 const xmlChar *ns = NULL, *fixed, *name, *oldcontainer, *attrValue;
7537 xmlSchemaIDCPtr curIDC = NULL, lastIDC = NULL;
7538
7539 if (xmlSchemaPValAttrNode(ctxt, NULL, NULL, nameAttr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007540 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007541 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007542 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007543 * Evaluate the target namespace.
7544 */
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00007545 if (topLevel) {
7546 ns = schema->targetNamespace;
7547 } else {
7548 attr = xmlSchemaGetPropNode(node, "form");
7549 if (attr != NULL) {
7550 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
7551 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
William M. Bracke7091952004-05-11 15:09:58 +00007552 ns = schema->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00007553 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007554 xmlSchemaPSimpleTypeErr(ctxt,
7555 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
7556 NULL, (xmlNodePtr) attr,
7557 NULL, "(qualified | unqualified)",
7558 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00007559 }
7560 } else if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007561 ns = schema->targetNamespace;
7562 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007563 decl = xmlSchemaAddElement(ctxt, schema, name, ns, node, topLevel);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007564 if (decl == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007565 goto return_null;
William M. Bracke7091952004-05-11 15:09:58 +00007566 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007567 decl->type = XML_SCHEMA_TYPE_ELEMENT;
7568 decl->node = node;
7569 decl->targetNamespace = ns;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007570 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007571 * Check for illegal attributes.
7572 */
William M. Bracke7091952004-05-11 15:09:58 +00007573 attr = node->properties;
7574 while (attr != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00007575 if (attr->ns == NULL) {
7576 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7577 (!xmlStrEqual(attr->name, BAD_CAST "type")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007578 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00007579 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007580 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00007581 (!xmlStrEqual(attr->name, BAD_CAST "block")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007582 (!xmlStrEqual(attr->name, BAD_CAST "nillable")))
7583 {
7584 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00007585 if ((!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00007586 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007587 (!xmlStrEqual(attr->name, BAD_CAST "form")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00007588 {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00007589 xmlSchemaPIllegalAttrErr(ctxt,
7590 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
7591 NULL, (xmlSchemaTypePtr) decl, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007592 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007593 } else if ((!xmlStrEqual(attr->name, BAD_CAST "final")) &&
7594 (!xmlStrEqual(attr->name, BAD_CAST "abstract")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00007595 (!xmlStrEqual(attr->name, BAD_CAST "substitutionGroup"))) {
7596
7597 xmlSchemaPIllegalAttrErr(ctxt,
7598 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007599 NULL, (xmlSchemaTypePtr) decl, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007600 }
7601 }
7602 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007603
Daniel Veillardc0826a72004-08-10 14:17:33 +00007604 xmlSchemaPIllegalAttrErr(ctxt,
7605 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007606 NULL, (xmlSchemaTypePtr) decl, attr);
William M. Bracke7091952004-05-11 15:09:58 +00007607 }
7608 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007609 }
William M. Bracke7091952004-05-11 15:09:58 +00007610 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007611 * Extract/validate attributes.
7612 */
7613 if (topLevel) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007614 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007615 * Process top attributes of global element declarations here.
7616 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007617 decl->flags |= XML_SCHEMAS_ELEM_GLOBAL;
7618 decl->flags |= XML_SCHEMAS_ELEM_TOPLEVEL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007619 xmlSchemaPValAttrQName(ctxt, schema, NULL,
7620 (xmlSchemaTypePtr) decl, node, "substitutionGroup",
7621 &(decl->substGroupNs), &(decl->substGroup));
7622 if (xmlGetBooleanProp(ctxt, NULL, (xmlSchemaTypePtr) decl,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007623 node, "abstract", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007624 decl->flags |= XML_SCHEMAS_ELEM_ABSTRACT;
Daniel Veillardc0826a72004-08-10 14:17:33 +00007625 /*
7626 * Attribute "final".
7627 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007628 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007629 if (attr == NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00007630 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
7631 decl->flags |= XML_SCHEMAS_ELEM_FINAL_EXTENSION;
7632 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
7633 decl->flags |= XML_SCHEMAS_ELEM_FINAL_RESTRICTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00007634 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007635 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
7636 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00007637 -1,
7638 XML_SCHEMAS_ELEM_FINAL_EXTENSION,
7639 XML_SCHEMAS_ELEM_FINAL_RESTRICTION, -1, -1, -1) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007640 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007641 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007642 (xmlSchemaTypePtr) decl, (xmlNodePtr) attr,
7643 NULL, "(#all | List of (extension | restriction))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00007644 attrValue, NULL, NULL, NULL);
7645 }
7646 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007647 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00007648 /*
7649 * Attribute "block".
7650 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007651 attr = xmlSchemaGetPropNode(node, "block");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007652 if (attr == NULL) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00007653 /*
7654 * Apply default "block" values.
7655 */
7656 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
7657 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_RESTRICTION;
7658 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
7659 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_EXTENSION;
7660 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
7661 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00007662 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007663 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
7664 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00007665 -1,
7666 XML_SCHEMAS_ELEM_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007667 XML_SCHEMAS_ELEM_BLOCK_RESTRICTION,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007668 XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION, -1, -1) != 0) {
7669 xmlSchemaPSimpleTypeErr(ctxt,
7670 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007671 (xmlSchemaTypePtr) decl, (xmlNodePtr) attr,
William M. Brack2f2a6632004-08-20 23:09:47 +00007672 NULL, "(#all | List of (extension | "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007673 "restriction | substitution))", attrValue,
7674 NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007675 }
7676 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007677 if (xmlGetBooleanProp(ctxt, NULL, (xmlSchemaTypePtr) decl,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007678 node, "nillable", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007679 decl->flags |= XML_SCHEMAS_ELEM_NILLABLE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007680
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007681 attr = xmlSchemaGetPropNode(node, "type");
7682 if (attr != NULL) {
7683 xmlSchemaPValAttrNodeQName(ctxt, schema,
7684 NULL, (xmlSchemaTypePtr) decl, attr,
7685 &(decl->namedTypeNs), &(decl->namedType));
7686 xmlSchemaCheckReference(ctxt, schema, node,
7687 (xmlSchemaBasicItemPtr) decl, decl->namedTypeNs);
7688 }
7689 decl->value = xmlSchemaGetProp(ctxt, node, "default");
7690 attr = xmlSchemaGetPropNode(node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007691 if (attr != NULL) {
7692 fixed = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007693 if (decl->value != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007694 /*
7695 * 3.3.3 : 1
7696 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00007697 */
7698 xmlSchemaPMutualExclAttrErr(ctxt,
7699 XML_SCHEMAP_SRC_ELEMENT_1,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007700 NULL, (xmlSchemaTypePtr) decl, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007701 "default", "fixed");
7702 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007703 decl->flags |= XML_SCHEMAS_ELEM_FIXED;
7704 decl->value = fixed;
Daniel Veillardc0826a72004-08-10 14:17:33 +00007705 }
William M. Bracke7091952004-05-11 15:09:58 +00007706 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007707 /*
7708 * And now for the children...
7709 */
7710 oldcontainer = ctxt->container;
7711 ctxt->container = decl->name;
William M. Bracke7091952004-05-11 15:09:58 +00007712 if (IS_SCHEMA(child, "complexType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007713 /*
7714 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00007715 * "type" and either <simpleType> or <complexType> are mutually
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007716 * exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00007717 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007718 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00007719 xmlSchemaPContentErr(ctxt,
7720 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007721 NULL, (xmlSchemaTypePtr) decl, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007722 "The attribute 'type' and the <complexType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007723 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00007724 } else
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00007725 ELEM_TYPE(decl) = xmlSchemaParseComplexType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00007726 child = child->next;
7727 } else if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007728 /*
7729 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00007730 * "type" and either <simpleType> or <complexType> are
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007731 * mutually exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00007732 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007733 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00007734 xmlSchemaPContentErr(ctxt,
7735 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007736 NULL, (xmlSchemaTypePtr) decl, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007737 "The attribute 'type' and the <simpleType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007738 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00007739 } else
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00007740 ELEM_TYPE(decl) = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00007741 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007742 }
William M. Bracke7091952004-05-11 15:09:58 +00007743 while ((IS_SCHEMA(child, "unique")) ||
Daniel Veillardc0826a72004-08-10 14:17:33 +00007744 (IS_SCHEMA(child, "key")) || (IS_SCHEMA(child, "keyref"))) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007745 if (IS_SCHEMA(child, "unique")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007746 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007747 XML_SCHEMA_TYPE_IDC_UNIQUE, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007748 } else if (IS_SCHEMA(child, "key")) {
7749 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007750 XML_SCHEMA_TYPE_IDC_KEY, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007751 } else if (IS_SCHEMA(child, "keyref")) {
7752 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007753 XML_SCHEMA_TYPE_IDC_KEYREF, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007754 }
7755 if (lastIDC != NULL)
7756 lastIDC->next = curIDC;
7757 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007758 decl->idcs = (void *) curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007759 lastIDC = curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007760 child = child->next;
William M. Bracke7091952004-05-11 15:09:58 +00007761 }
7762 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00007763 xmlSchemaPContentErr(ctxt,
7764 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007765 NULL, (xmlSchemaTypePtr) decl, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007766 NULL, "(annotation?, ((simpleType | complexType)?, "
7767 "(unique | key | keyref)*))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007768 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007769 ctxt->container = oldcontainer;
7770 decl->annot = annot;
Daniel Veillardc0826a72004-08-10 14:17:33 +00007771 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00007772 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007773 * NOTE: Element Declaration Representation OK 4. will be checked at a
Daniel Veillardc0826a72004-08-10 14:17:33 +00007774 * different layer.
7775 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007776 FREE_AND_NULL(des)
7777 if (topLevel)
7778 return ((xmlSchemaBasicItemPtr) decl);
7779 else {
7780 particle->children = (xmlSchemaTreeItemPtr) decl;
7781 return ((xmlSchemaBasicItemPtr) particle);
7782 }
7783
7784return_null:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007785 FREE_AND_NULL(des);
7786 if (annot != NULL) {
7787 if (particle != NULL)
7788 particle->annot = NULL;
7789 if (decl != NULL)
7790 decl->annot = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007791 xmlSchemaFreeAnnot(annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007792 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007793 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00007794}
7795
7796/**
7797 * xmlSchemaParseUnion:
7798 * @ctxt: a schema validation context
7799 * @schema: the schema being built
7800 * @node: a subtree containing XML Schema informations
7801 *
7802 * parse a XML schema Union definition
7803 * *WARNING* this interface is highly subject to change
7804 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007805 * Returns -1 in case of internal error, 0 in case of success and a positive
7806 * error code otherwise.
Daniel Veillard4255d502002-04-16 15:50:10 +00007807 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007808static int
Daniel Veillard4255d502002-04-16 15:50:10 +00007809xmlSchemaParseUnion(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007810 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00007811{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007812 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00007813 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00007814 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007815 const xmlChar *cur = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00007816
7817 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007818 return (-1);
7819 /* Not a component, don't create it. */
7820 type = ctxt->ctxtType;
7821 /*
7822 * Mark the simple type as being of variety "union".
7823 */
7824 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007825 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00007826 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
7827 * then the ·simple ur-type definition·."
7828 */
7829 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00007830 /*
7831 * Check for illegal attributes.
7832 */
7833 attr = node->properties;
7834 while (attr != NULL) {
7835 if (attr->ns == NULL) {
7836 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
7837 (!xmlStrEqual(attr->name, BAD_CAST "memberTypes"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007838 xmlSchemaPIllegalAttrErr(ctxt,
7839 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
7840 NULL, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00007841 }
7842 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007843 xmlSchemaPIllegalAttrErr(ctxt,
7844 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
7845 NULL, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00007846 }
7847 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007848 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00007849 xmlSchemaPValAttrID(ctxt, NULL, NULL, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +00007850 /*
7851 * Attribute "memberTypes". This is a list of QNames.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007852 * TODO: Check the value to contain anything.
William M. Brack2f2a6632004-08-20 23:09:47 +00007853 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007854 attr = xmlSchemaGetPropNode(node, "memberTypes");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007855 if (attr != NULL) {
7856 const xmlChar *end;
7857 xmlChar *tmp;
7858 const xmlChar *localName, *nsName;
7859 xmlSchemaTypeLinkPtr link, lastLink = NULL;
7860 xmlSchemaQNameRefPtr ref;
7861
7862 cur = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007863 type->ref = cur;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007864 do {
7865 while (IS_BLANK_CH(*cur))
7866 cur++;
7867 end = cur;
7868 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
7869 end++;
7870 if (end == cur)
7871 break;
7872 tmp = xmlStrndup(cur, end - cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007873 if (xmlSchemaPValAttrNodeQNameValue(ctxt, schema, NULL,
7874 NULL, attr, BAD_CAST tmp, &nsName, &localName) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007875 /*
7876 * Create the member type link.
7877 */
7878 link = (xmlSchemaTypeLinkPtr)
7879 xmlMalloc(sizeof(xmlSchemaTypeLink));
7880 if (link == NULL) {
7881 xmlSchemaPErrMemory(ctxt, "xmlSchemaParseUnion, "
7882 "allocating a type link", NULL);
7883 return (-1);
7884 }
7885 link->type = NULL;
7886 link->next = NULL;
7887 if (lastLink == NULL)
7888 type->memberTypes = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007889 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007890 lastLink->next = link;
7891 lastLink = link;
7892 /*
7893 * Create a reference item.
7894 */
7895 ref = xmlSchemaNewQNameRef(schema, XML_SCHEMA_TYPE_SIMPLE,
7896 localName, nsName);
7897 if (ref == NULL) {
7898 FREE_AND_NULL(tmp)
7899 return (-1);
7900 }
7901 /*
7902 * Assign the reference to the link, it will be resolved
7903 * later during fixup of the union simple type.
7904 */
7905 link->type = (xmlSchemaTypePtr) ref;
7906 }
7907 FREE_AND_NULL(tmp)
7908 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007909 } while (*cur != 0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007910
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007911 }
William M. Brack2f2a6632004-08-20 23:09:47 +00007912 /*
7913 * And now for the children...
7914 */
Daniel Veillard4255d502002-04-16 15:50:10 +00007915 child = node->children;
7916 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007917 /*
7918 * Add the annotation to the simple type ancestor.
7919 */
7920 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
7921 xmlSchemaParseAnnotation(ctxt, schema, child));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007922 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007923 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007924 if (IS_SCHEMA(child, "simpleType")) {
7925 xmlSchemaTypePtr subtype, last = NULL;
7926
7927 /*
7928 * Anchor the member types in the "subtypes" field of the
7929 * simple type.
7930 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007931 while (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007932 subtype = (xmlSchemaTypePtr)
7933 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
7934 if (subtype != NULL) {
7935 if (last == NULL) {
7936 type->subtypes = subtype;
7937 last = subtype;
7938 } else {
7939 last->next = subtype;
7940 last = subtype;
7941 }
7942 last->next = NULL;
7943 }
7944 child = child->next;
7945 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007946 }
7947 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00007948 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007949 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007950 NULL, NULL, node, child, NULL, "(annotation?, simpleType*)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007951 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007952 if ((attr == NULL) && (type->subtypes == NULL)) {
7953 /*
7954 * src-union-memberTypes-or-simpleTypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007955 * Either the memberTypes [attribute] of the <union> element must
7956 * be non-empty or there must be at least one simpleType [child].
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007957 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007958 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007959 XML_SCHEMAP_SRC_UNION_MEMBERTYPES_OR_SIMPLETYPES,
7960 NULL, NULL, node,
7961 "Either the attribute 'memberTypes' or "
7962 "at least one <simpleType> child must be present", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007963 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007964 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00007965}
7966
7967/**
7968 * xmlSchemaParseList:
7969 * @ctxt: a schema validation context
7970 * @schema: the schema being built
7971 * @node: a subtree containing XML Schema informations
7972 *
7973 * parse a XML schema List definition
7974 * *WARNING* this interface is highly subject to change
7975 *
William M. Bracke7091952004-05-11 15:09:58 +00007976 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00007977 * 1 in case of success.
7978 */
7979static xmlSchemaTypePtr
7980xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007981 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00007982{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007983 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00007984 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00007985 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00007986
7987 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
7988 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007989 /* Not a component, don't create it. */
7990 type = ctxt->ctxtType;
7991 /*
7992 * Mark the type as being of variety "list".
7993 */
7994 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007995 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00007996 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
7997 * then the ·simple ur-type definition·."
7998 */
7999 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00008000 /*
8001 * Check for illegal attributes.
8002 */
8003 attr = node->properties;
8004 while (attr != NULL) {
8005 if (attr->ns == NULL) {
8006 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8007 (!xmlStrEqual(attr->name, BAD_CAST "itemType"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008008 xmlSchemaPIllegalAttrErr(ctxt,
8009 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
8010 NULL, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008011 }
8012 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008013 xmlSchemaPIllegalAttrErr(ctxt,
8014 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
8015 NULL, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008016 }
8017 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008018 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008019
8020 xmlSchemaPValAttrID(ctxt, NULL, NULL, node, BAD_CAST "id");
8021
William M. Brack2f2a6632004-08-20 23:09:47 +00008022 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008023 * Attribute "itemType". NOTE that we will use the "ref" and "refNs"
8024 * fields for holding the reference to the itemType.
William M. Brack2f2a6632004-08-20 23:09:47 +00008025 */
8026 xmlSchemaPValAttrQName(ctxt, schema, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008027 node, "itemType", &(type->refNs), &(type->ref));
William M. Brack2f2a6632004-08-20 23:09:47 +00008028 /*
8029 * And now for the children...
8030 */
Daniel Veillard4255d502002-04-16 15:50:10 +00008031 child = node->children;
8032 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008033 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
8034 xmlSchemaParseAnnotation(ctxt, schema, child));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008035 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008036 }
William M. Brack2f2a6632004-08-20 23:09:47 +00008037 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008038 /*
8039 * src-list-itemType-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008040 * Either the itemType [attribute] or the <simpleType> [child] of
8041 * the <list> element must be present, but not both.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008042 */
8043 if (type->ref != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008044 xmlSchemaPCustomErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00008045 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008046 NULL, NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +00008047 "The attribute 'itemType' and the <simpleType> child "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008048 "are mutually exclusive", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008049 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008050 type->subtypes = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Brack2f2a6632004-08-20 23:09:47 +00008051 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008052 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008053 } else if (type->ref == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008054 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008055 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008056 NULL, NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008057 "Either the attribute 'itemType' or the <simpleType> child "
8058 "must be present", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00008059 }
8060 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00008061 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008062 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008063 NULL, NULL, node, child, NULL, "(annotation?, simpleType?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00008064 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008065 if ((type->ref == NULL) &&
8066 (type->subtypes == NULL) &&
8067 (xmlSchemaGetPropNode(node, "itemType") == NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008068 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008069 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008070 NULL, NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008071 "Either the attribute 'itemType' or the <simpleType> child "
8072 "must be present", NULL);
8073 }
8074 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00008075}
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008076
Daniel Veillard4255d502002-04-16 15:50:10 +00008077/**
8078 * xmlSchemaParseSimpleType:
8079 * @ctxt: a schema validation context
8080 * @schema: the schema being built
8081 * @node: a subtree containing XML Schema informations
8082 *
8083 * parse a XML schema Simple Type definition
8084 * *WARNING* this interface is highly subject to change
8085 *
William M. Bracke7091952004-05-11 15:09:58 +00008086 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillardc0826a72004-08-10 14:17:33 +00008087 * 1 in case of success.
Daniel Veillard4255d502002-04-16 15:50:10 +00008088 */
8089static xmlSchemaTypePtr
8090xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00008091 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00008092{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008093 xmlSchemaTypePtr type, oldCtxtType, oldParentItem;
Daniel Veillard4255d502002-04-16 15:50:10 +00008094 xmlNodePtr child = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008095 const xmlChar *attrValue = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008096 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00008097
8098 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
8099 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008100
Daniel Veillardc0826a72004-08-10 14:17:33 +00008101 if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008102 attr = xmlSchemaGetPropNode(node, "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008103 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008104 xmlSchemaPMissingAttrErr(ctxt,
8105 XML_SCHEMAP_S4S_ATTR_MISSING,
8106 NULL, node,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008107 "name", NULL);
8108 return (NULL);
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00008109 } else {
8110 if (xmlSchemaPValAttrNode(ctxt,
8111 NULL, NULL, attr,
8112 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0)
8113 return (NULL);
8114 /*
8115 * Skip built-in types.
8116 */
8117 if (ctxt->isS4S) {
8118 xmlSchemaTypePtr biType;
8119
8120 biType = xmlSchemaGetPredefinedType(attrValue, xmlSchemaNs);
8121 if (biType != NULL)
8122 return (biType);
8123 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00008124 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008125 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008126
Daniel Veillardc0826a72004-08-10 14:17:33 +00008127 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008128 char buf[40];
Daniel Veillardc0826a72004-08-10 14:17:33 +00008129
Daniel Veillard01fa6152004-06-29 17:04:39 +00008130 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008131 * Parse as local simple type definition.
Daniel Veillard01fa6152004-06-29 17:04:39 +00008132 */
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00008133 snprintf(buf, 39, "#ST%d", ctxt->counter++ + 1);
William M. Brack2f2a6632004-08-20 23:09:47 +00008134 type = xmlSchemaAddType(ctxt, schema, (const xmlChar *)buf, NULL, node);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008135 if (type == NULL)
8136 return (NULL);
8137 type->node = node;
8138 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008139 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008140 /*
8141 * Check for illegal attributes.
8142 */
8143 attr = node->properties;
8144 while (attr != NULL) {
8145 if (attr->ns == NULL) {
8146 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008147 xmlSchemaPIllegalAttrErr(ctxt,
8148 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
8149 NULL, type, attr);
Daniel Veillard01fa6152004-06-29 17:04:39 +00008150 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008151 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008152 xmlSchemaPIllegalAttrErr(ctxt,
8153 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
8154 NULL, type, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008155 }
8156 attr = attr->next;
Daniel Veillard01fa6152004-06-29 17:04:39 +00008157 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008158 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008159 /*
8160 * Parse as global simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008161 *
8162 * Note that attrValue is the value of the attribute "name" here.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008163 */
William M. Brack2f2a6632004-08-20 23:09:47 +00008164 type = xmlSchemaAddType(ctxt, schema, attrValue, schema->targetNamespace, node);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008165 if (type == NULL)
8166 return (NULL);
8167 type->node = node;
8168 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008169 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008170 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
8171 /*
8172 * Check for illegal attributes.
8173 */
8174 attr = node->properties;
8175 while (attr != NULL) {
8176 if (attr->ns == NULL) {
8177 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8178 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008179 (!xmlStrEqual(attr->name, BAD_CAST "final"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008180 xmlSchemaPIllegalAttrErr(ctxt,
8181 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
8182 NULL, type, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008183 }
8184 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008185 xmlSchemaPIllegalAttrErr(ctxt,
8186 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
8187 NULL, type, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008188 }
8189 attr = attr->next;
8190 }
8191 /*
8192 * Attribute "final".
8193 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008194 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008195 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008196 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
8197 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
8198 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
8199 type->flags |= XML_SCHEMAS_TYPE_FINAL_LIST;
8200 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
8201 type->flags |= XML_SCHEMAS_TYPE_FINAL_UNION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008202 } else {
8203 attrValue = xmlSchemaGetProp(ctxt, node, "final");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008204 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
8205 -1, -1, XML_SCHEMAS_TYPE_FINAL_RESTRICTION, -1,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008206 XML_SCHEMAS_TYPE_FINAL_LIST,
8207 XML_SCHEMAS_TYPE_FINAL_UNION) != 0) {
8208
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008209 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008210 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008211 type, (xmlNodePtr) attr,
8212 NULL, "(#all | List of (list | union | restriction)",
Daniel Veillardc0826a72004-08-10 14:17:33 +00008213 attrValue, NULL, NULL, NULL);
8214 }
8215 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008216 }
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00008217 type->targetNamespace = schema->targetNamespace;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00008218 xmlSchemaPValAttrID(ctxt, NULL, type, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008219 /*
8220 * And now for the children...
8221 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008222 oldCtxtType = ctxt->ctxtType;
8223 oldParentItem = ctxt->parentItem;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008224 ctxt->ctxtType = type;
8225 ctxt->parentItem = type;
Daniel Veillard4255d502002-04-16 15:50:10 +00008226 child = node->children;
8227 if (IS_SCHEMA(child, "annotation")) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008228 type->annot = xmlSchemaParseAnnotation(ctxt, schema, child);
8229 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00008230 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008231 if (child == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008232 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_MISSING,
8233 NULL, type, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008234 "(annotation?, (restriction | list | union))");
8235 } else if (IS_SCHEMA(child, "restriction")) {
8236 xmlSchemaParseRestriction(ctxt, schema, child,
8237 XML_SCHEMA_TYPE_SIMPLE);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008238 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008239 } else if (IS_SCHEMA(child, "list")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008240 xmlSchemaParseList(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008241 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008242 } else if (IS_SCHEMA(child, "union")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008243 xmlSchemaParseUnion(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008244 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00008245 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008246 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008247 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
8248 NULL, type, node, child, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008249 "(annotation?, (restriction | list | union))");
Daniel Veillard4255d502002-04-16 15:50:10 +00008250 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008251 ctxt->parentItem = oldParentItem;
8252 ctxt->ctxtType = oldCtxtType;
Daniel Veillard01fa6152004-06-29 17:04:39 +00008253
Daniel Veillard4255d502002-04-16 15:50:10 +00008254 return (type);
8255}
8256
Daniel Veillard4255d502002-04-16 15:50:10 +00008257/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008258 * xmlSchemaParseModelGroupDefRef:
Daniel Veillard4255d502002-04-16 15:50:10 +00008259 * @ctxt: a schema validation context
8260 * @schema: the schema being built
8261 * @node: a subtree containing XML Schema informations
8262 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008263 * Parses a XML schema particle (reference to a model group definition).
Daniel Veillard4255d502002-04-16 15:50:10 +00008264 * *WARNING* this interface is highly subject to change
8265 *
William M. Bracke7091952004-05-11 15:09:58 +00008266 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00008267 * 1 in case of success.
8268 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008269static xmlSchemaTreeItemPtr
8270xmlSchemaParseModelGroupDefRef(xmlSchemaParserCtxtPtr ctxt,
8271 xmlSchemaPtr schema,
8272 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00008273{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008274 xmlSchemaParticlePtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008275 xmlNodePtr child = NULL;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00008276 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008277 const xmlChar *ref = NULL, *refNs = NULL;
8278 int min, max;
8279
8280 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008281 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008282
8283 attr = xmlSchemaGetPropNode(node, "ref");
8284 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008285 xmlSchemaPMissingAttrErr(ctxt,
8286 XML_SCHEMAP_S4S_ATTR_MISSING,
8287 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008288 "ref", NULL);
8289 return (NULL);
8290 } else if (xmlSchemaPValAttrNodeQName(ctxt, schema, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008291 attr, &refNs, &ref) != 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008292 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008293 }
8294 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008295 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008296 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008297 /*
8298 * Check for illegal attributes.
8299 */
8300 attr = node->properties;
8301 while (attr != NULL) {
8302 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008303 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008304 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8305 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
8306 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008307 xmlSchemaPIllegalAttrErr(ctxt,
8308 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
8309 NULL, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008310 }
8311 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008312 xmlSchemaPIllegalAttrErr(ctxt,
8313 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
8314 NULL, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008315 }
8316 attr = attr->next;
8317 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008318 xmlSchemaPValAttrID(ctxt, NULL, NULL, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008319 item = xmlSchemaAddParticle(ctxt, schema, node, min, max);
8320 if (item == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008321 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008322 /*
8323 * Create a reference item as the term; it will be substituted for
8324 * the model group after the reference has been resolved.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008325 */
8326 item->children = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008327 xmlSchemaNewQNameRef(schema, XML_SCHEMA_TYPE_GROUP, ref, refNs);
8328 xmlSchemaCheckReference(ctxt, schema, node, (xmlSchemaBasicItemPtr) item, refNs);
8329 xmlSchemaPCheckParticleCorrect_2(ctxt, item, node, min, max);
8330 /*
8331 * And now for the children...
8332 */
8333 child = node->children;
8334 /* TODO: Is annotation even allowed for a model group reference? */
8335 if (IS_SCHEMA(child, "annotation")) {
8336 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008337 * TODO: What to do exactly with the annotation?
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008338 */
8339 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child);
8340 child = child->next;
8341 }
8342 if (child != NULL) {
8343 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008344 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008345 NULL, NULL, node, child, NULL,
8346 "(annotation?)");
8347 }
8348 /*
8349 * Corresponds to no component at all if minOccurs==maxOccurs==0.
8350 */
8351 if ((min == 0) && (max == 0))
8352 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008353 if (ctxt->assemble != NULL)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008354 xmlSchemaAddAssembledItem(ctxt, (xmlSchemaTypePtr) item);
8355 return ((xmlSchemaTreeItemPtr) item);
8356}
8357
8358/**
8359 * xmlSchemaParseModelGroupDefinition:
8360 * @ctxt: a schema validation context
8361 * @schema: the schema being built
8362 * @node: a subtree containing XML Schema informations
8363 *
8364 * Parses a XML schema model group definition.
8365 * *WARNING* this interface is highly subject to change
8366 *
8367 * Returns -1 in case of error, 0 if the declaration is improper and
8368 * 1 in case of success.
8369 */
8370static xmlSchemaModelGroupDefPtr
8371xmlSchemaParseModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
8372 xmlSchemaPtr schema,
8373 xmlNodePtr node)
8374{
8375 xmlSchemaModelGroupDefPtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008376 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008377 xmlAttrPtr attr;
8378 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00008379
8380 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008381 return (NULL);
8382
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008383 attr = xmlSchemaGetPropNode(node, "name");
8384 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008385 xmlSchemaPMissingAttrErr(ctxt,
8386 XML_SCHEMAP_S4S_ATTR_MISSING,
8387 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008388 "name", NULL);
8389 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008390 } else if (xmlSchemaPValAttrNode(ctxt,
8391 NULL, NULL, attr,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008392 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
8393 return (NULL);
8394 }
8395 item = xmlSchemaAddGroup(ctxt, schema, name, schema->targetNamespace, node);
8396 if (item == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008397 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008398 /*
8399 * Check for illegal attributes.
8400 */
8401 attr = node->properties;
8402 while (attr != NULL) {
8403 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008404 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008405 (!xmlStrEqual(attr->name, BAD_CAST "id"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008406 xmlSchemaPIllegalAttrErr(ctxt,
8407 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
8408 NULL, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00008409 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008410 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008411 xmlSchemaPIllegalAttrErr(ctxt,
8412 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
8413 NULL, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00008414 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008415 attr = attr->next;
8416 }
8417 xmlSchemaPValAttrID(ctxt, NULL, NULL, node, BAD_CAST "id");
8418 /*
8419 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008420 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008421 child = node->children;
8422 if (IS_SCHEMA(child, "annotation")) {
8423 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child);
8424 child = child->next;
8425 }
8426 if (IS_SCHEMA(child, "all")) {
8427 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
8428 XML_SCHEMA_TYPE_ALL, 0);
8429 child = child->next;
8430 } else if (IS_SCHEMA(child, "choice")) {
8431 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
8432 XML_SCHEMA_TYPE_CHOICE, 0);
8433 child = child->next;
8434 } else if (IS_SCHEMA(child, "sequence")) {
8435 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
8436 XML_SCHEMA_TYPE_SEQUENCE, 0);
8437 child = child->next;
8438 }
8439 if (child != NULL) {
8440 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008441 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
8442 NULL, NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008443 "(annotation?, (all | choice | sequence)?)");
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00008444 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008445
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00008446 return (item);
Daniel Veillard4255d502002-04-16 15:50:10 +00008447}
8448
8449/**
Daniel Veillardbd2904b2003-11-25 15:38:59 +00008450 * xmlSchemaCleanupDoc:
8451 * @ctxt: a schema validation context
8452 * @node: the root of the document.
8453 *
8454 * removes unwanted nodes in a schemas document tree
8455 */
8456static void
8457xmlSchemaCleanupDoc(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr root)
8458{
8459 xmlNodePtr delete, cur;
8460
8461 if ((ctxt == NULL) || (root == NULL)) return;
8462
8463 /*
8464 * Remove all the blank text nodes
8465 */
8466 delete = NULL;
8467 cur = root;
8468 while (cur != NULL) {
8469 if (delete != NULL) {
8470 xmlUnlinkNode(delete);
8471 xmlFreeNode(delete);
8472 delete = NULL;
8473 }
8474 if (cur->type == XML_TEXT_NODE) {
8475 if (IS_BLANK_NODE(cur)) {
8476 if (xmlNodeGetSpacePreserve(cur) != 1) {
8477 delete = cur;
8478 }
8479 }
8480 } else if ((cur->type != XML_ELEMENT_NODE) &&
8481 (cur->type != XML_CDATA_SECTION_NODE)) {
8482 delete = cur;
8483 goto skip_children;
8484 }
8485
8486 /*
8487 * Skip to next node
8488 */
8489 if (cur->children != NULL) {
8490 if ((cur->children->type != XML_ENTITY_DECL) &&
8491 (cur->children->type != XML_ENTITY_REF_NODE) &&
8492 (cur->children->type != XML_ENTITY_NODE)) {
8493 cur = cur->children;
8494 continue;
8495 }
8496 }
8497 skip_children:
8498 if (cur->next != NULL) {
8499 cur = cur->next;
8500 continue;
8501 }
8502
8503 do {
8504 cur = cur->parent;
8505 if (cur == NULL)
8506 break;
8507 if (cur == root) {
8508 cur = NULL;
8509 break;
8510 }
8511 if (cur->next != NULL) {
8512 cur = cur->next;
8513 break;
8514 }
8515 } while (cur != NULL);
8516 }
8517 if (delete != NULL) {
8518 xmlUnlinkNode(delete);
8519 xmlFreeNode(delete);
8520 delete = NULL;
8521 }
8522}
8523
William M. Brack2f2a6632004-08-20 23:09:47 +00008524
8525/**
8526 * xmlSchemaImportSchema
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008527 *
William M. Brack2f2a6632004-08-20 23:09:47 +00008528 * @ctxt: a schema validation context
8529 * @schemaLocation: an URI defining where to find the imported schema
8530 *
8531 * import a XML schema
8532 * *WARNING* this interface is highly subject to change
8533 *
8534 * Returns -1 in case of error and 1 in case of success.
8535 */
8536#if 0
8537static xmlSchemaImportPtr
8538xmlSchemaImportSchema(xmlSchemaParserCtxtPtr ctxt,
8539 const xmlChar *schemaLocation)
8540{
8541 xmlSchemaImportPtr import;
8542 xmlSchemaParserCtxtPtr newctxt;
8543
8544 newctxt = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt));
8545 if (newctxt == NULL) {
8546 xmlSchemaPErrMemory(ctxt, "allocating schema parser context",
8547 NULL);
8548 return (NULL);
8549 }
8550 memset(newctxt, 0, sizeof(xmlSchemaParserCtxt));
8551 /* Keep the same dictionnary for parsing, really */
8552 xmlDictReference(ctxt->dict);
8553 newctxt->dict = ctxt->dict;
8554 newctxt->includes = 0;
8555 newctxt->URL = xmlDictLookup(newctxt->dict, schemaLocation, -1);
8556
8557 xmlSchemaSetParserErrors(newctxt, ctxt->error, ctxt->warning,
8558 ctxt->userData);
8559
8560 import = (xmlSchemaImport*) xmlMalloc(sizeof(xmlSchemaImport));
8561 if (import == NULL) {
8562 xmlSchemaPErrMemory(NULL, "allocating imported schema",
8563 NULL);
8564 xmlSchemaFreeParserCtxt(newctxt);
8565 return (NULL);
8566 }
8567
8568 memset(import, 0, sizeof(xmlSchemaImport));
8569 import->schemaLocation = xmlDictLookup(ctxt->dict, schemaLocation, -1);
8570 import->schema = xmlSchemaParse(newctxt);
8571
8572 if (import->schema == NULL) {
8573 /* FIXME use another error enum here ? */
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00008574 xmlSchemaPErr(ctxt, NULL, XML_SCHEMAP_INTERNAL,
William M. Brack2f2a6632004-08-20 23:09:47 +00008575 "Failed to import schema from location \"%s\".\n",
8576 schemaLocation, NULL);
8577
8578 xmlSchemaFreeParserCtxt(newctxt);
8579 /* The schemaLocation is held by the dictionary.
8580 if (import->schemaLocation != NULL)
8581 xmlFree((xmlChar *)import->schemaLocation);
8582 */
8583 xmlFree(import);
8584 return NULL;
8585 }
8586
8587 xmlSchemaFreeParserCtxt(newctxt);
8588 return import;
8589}
8590#endif
8591
8592static void
8593xmlSchemaClearSchemaDefaults(xmlSchemaPtr schema)
8594{
8595 if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
8596 schema->flags ^= XML_SCHEMAS_QUALIF_ELEM;
8597
8598 if (schema->flags & XML_SCHEMAS_QUALIF_ATTR)
8599 schema->flags ^= XML_SCHEMAS_QUALIF_ATTR;
8600
8601 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
8602 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_EXTENSION;
8603 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
8604 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION;
8605 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
8606 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_LIST;
8607 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
8608 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_UNION;
8609
8610 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
8611 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION;
8612 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
8613 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION;
8614 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
8615 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION;
8616}
8617
8618static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008619xmlSchemaParseSchemaDefaults(xmlSchemaParserCtxtPtr ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00008620 xmlSchemaPtr schema,
8621 xmlNodePtr node)
8622{
8623 xmlAttrPtr attr;
8624 const xmlChar *val;
8625
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00008626 xmlSchemaPValAttrID(ctxt, NULL, NULL, node, BAD_CAST "id");
8627 if (schema->version == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008628 xmlSchemaPValAttr(ctxt, NULL, NULL, node, "version",
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00008629 xmlSchemaGetBuiltInType(XML_SCHEMAS_TOKEN), &(schema->version));
8630 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008631 xmlSchemaPValAttr(ctxt, NULL, NULL, node, "version",
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00008632 xmlSchemaGetBuiltInType(XML_SCHEMAS_TOKEN), NULL);
8633
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008634 attr = xmlSchemaGetPropNode(node, "elementFormDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00008635 if (attr != NULL) {
8636 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008637 if (xmlSchemaPValAttrFormDefault(val, &schema->flags,
William M. Brack2f2a6632004-08-20 23:09:47 +00008638 XML_SCHEMAS_QUALIF_ELEM) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008639 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00008640 XML_SCHEMAP_ELEMFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008641 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00008642 "(qualified | unqualified)", val, NULL, NULL, NULL);
8643 }
8644 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008645
8646 attr = xmlSchemaGetPropNode(node, "attributeFormDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00008647 if (attr != NULL) {
8648 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008649 if (xmlSchemaPValAttrFormDefault(val, &schema->flags,
William M. Brack2f2a6632004-08-20 23:09:47 +00008650 XML_SCHEMAS_QUALIF_ATTR) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008651 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00008652 XML_SCHEMAP_ATTRFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008653 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00008654 "(qualified | unqualified)", val, NULL, NULL, NULL);
8655 }
8656 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008657
8658 attr = xmlSchemaGetPropNode(node, "finalDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00008659 if (attr != NULL) {
8660 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8661 if (xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
8662 XML_SCHEMAS_FINAL_DEFAULT_EXTENSION,
8663 XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION,
8664 -1,
8665 XML_SCHEMAS_FINAL_DEFAULT_LIST,
8666 XML_SCHEMAS_FINAL_DEFAULT_UNION) != 0) {
8667 xmlSchemaPSimpleTypeErr(ctxt,
8668 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008669 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00008670 "(#all | List of (extension | restriction | list | union))",
8671 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008672 }
William M. Brack2f2a6632004-08-20 23:09:47 +00008673 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008674
8675 attr = xmlSchemaGetPropNode(node, "blockDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00008676 if (attr != NULL) {
8677 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8678 if (xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
8679 XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION,
8680 XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION,
8681 XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION, -1, -1) != 0) {
8682 xmlSchemaPSimpleTypeErr(ctxt,
8683 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008684 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00008685 "(#all | List of (extension | restriction | substitution))",
8686 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008687 }
William M. Brack2f2a6632004-08-20 23:09:47 +00008688 }
8689}
8690
Daniel Veillardbd2904b2003-11-25 15:38:59 +00008691/**
8692 * xmlSchemaParseSchemaTopLevel:
8693 * @ctxt: a schema validation context
8694 * @schema: the schemas
8695 * @nodes: the list of top level nodes
8696 *
8697 * Returns the internal XML Schema structure built from the resource or
8698 * NULL in case of error
8699 */
8700static void
8701xmlSchemaParseSchemaTopLevel(xmlSchemaParserCtxtPtr ctxt,
8702 xmlSchemaPtr schema, xmlNodePtr nodes)
8703{
8704 xmlNodePtr child;
8705 xmlSchemaAnnotPtr annot;
8706
8707 if ((ctxt == NULL) || (schema == NULL) || (nodes == NULL))
8708 return;
8709
8710 child = nodes;
8711 while ((IS_SCHEMA(child, "include")) ||
8712 (IS_SCHEMA(child, "import")) ||
8713 (IS_SCHEMA(child, "redefine")) ||
8714 (IS_SCHEMA(child, "annotation"))) {
8715 if (IS_SCHEMA(child, "annotation")) {
8716 annot = xmlSchemaParseAnnotation(ctxt, schema, child);
8717 if (schema->annot == NULL)
8718 schema->annot = annot;
8719 else
8720 xmlSchemaFreeAnnot(annot);
8721 } else if (IS_SCHEMA(child, "import")) {
8722 xmlSchemaParseImport(ctxt, schema, child);
8723 } else if (IS_SCHEMA(child, "include")) {
Daniel Veillardb0f397e2003-12-23 23:30:53 +00008724 ctxt->includes++;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00008725 xmlSchemaParseInclude(ctxt, schema, child);
Daniel Veillardb0f397e2003-12-23 23:30:53 +00008726 ctxt->includes--;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00008727 } else if (IS_SCHEMA(child, "redefine")) {
8728 TODO
8729 }
8730 child = child->next;
8731 }
8732 while (child != NULL) {
8733 if (IS_SCHEMA(child, "complexType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00008734 xmlSchemaParseComplexType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00008735 child = child->next;
8736 } else if (IS_SCHEMA(child, "simpleType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00008737 xmlSchemaParseSimpleType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00008738 child = child->next;
8739 } else if (IS_SCHEMA(child, "element")) {
8740 xmlSchemaParseElement(ctxt, schema, child, 1);
8741 child = child->next;
8742 } else if (IS_SCHEMA(child, "attribute")) {
William M. Bracke7091952004-05-11 15:09:58 +00008743 xmlSchemaParseAttribute(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00008744 child = child->next;
8745 } else if (IS_SCHEMA(child, "attributeGroup")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00008746 xmlSchemaParseAttributeGroup(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00008747 child = child->next;
8748 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008749 xmlSchemaParseModelGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00008750 child = child->next;
8751 } else if (IS_SCHEMA(child, "notation")) {
8752 xmlSchemaParseNotation(ctxt, schema, child);
8753 child = child->next;
8754 } else {
8755 xmlSchemaPErr2(ctxt, NULL, child,
8756 XML_SCHEMAP_UNKNOWN_SCHEMAS_CHILD,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008757 "Unexpected element \"%s\" as child of <schema>.\n",
Daniel Veillardbd2904b2003-11-25 15:38:59 +00008758 child->name, NULL);
8759 child = child->next;
8760 }
8761 while (IS_SCHEMA(child, "annotation")) {
8762 annot = xmlSchemaParseAnnotation(ctxt, schema, child);
8763 if (schema->annot == NULL)
8764 schema->annot = annot;
8765 else
8766 xmlSchemaFreeAnnot(annot);
8767 child = child->next;
8768 }
8769 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00008770 ctxt->parentItem = NULL;
8771 ctxt->ctxtType = NULL;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00008772}
8773
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008774static xmlSchemaImportPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008775xmlSchemaAddImport(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008776 xmlHashTablePtr *imports,
8777 const xmlChar *nsName)
8778{
8779 xmlSchemaImportPtr ret;
8780
8781 if (*imports == NULL) {
Daniel Veillardb9e2f262005-01-23 23:48:34 +00008782 *imports = xmlHashCreateDict(10, ctxt->dict);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008783 if (*imports == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008784 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008785 XML_SCHEMAP_FAILED_BUILD_IMPORT,
8786 NULL, NULL, (xmlNodePtr) ctxt->doc,
8787 "Internal error: failed to build the import table",
8788 NULL);
8789 return (NULL);
8790 }
8791 }
8792 ret = (xmlSchemaImport*) xmlMalloc(sizeof(xmlSchemaImport));
8793 if (ret == NULL) {
8794 xmlSchemaPErrMemory(NULL, "allocating import struct", NULL);
8795 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008796 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008797 memset(ret, 0, sizeof(xmlSchemaImport));
8798 if (nsName == NULL)
8799 nsName = XML_SCHEMAS_NO_NAMESPACE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008800 xmlHashAddEntry(*imports, nsName, ret);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008801
8802 return (ret);
8803}
8804
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008805/**
8806 * xmlSchemaNewParserCtxtUseDict:
8807 * @URL: the location of the schema
8808 * @dict: the dictionary to be used
8809 *
8810 * Create an XML Schemas parse context for that file/resource expected
8811 * to contain an XML Schemas file.
8812 *
8813 * Returns the parser context or NULL in case of error
8814 */
8815static xmlSchemaParserCtxtPtr
8816xmlSchemaNewParserCtxtUseDict(const char *URL, xmlDictPtr dict)
8817{
8818 xmlSchemaParserCtxtPtr ret;
8819 /*
8820 if (URL == NULL)
8821 return (NULL);
8822 */
8823
8824 ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt));
8825 if (ret == NULL) {
8826 xmlSchemaPErrMemory(NULL, "allocating schema parser context",
8827 NULL);
8828 return (NULL);
8829 }
8830 memset(ret, 0, sizeof(xmlSchemaParserCtxt));
8831 ret->dict = dict;
8832 xmlDictReference(dict);
8833 if (URL != NULL)
8834 ret->URL = xmlDictLookup(dict, (const xmlChar *) URL, -1);
8835 ret->includes = 0;
8836 return (ret);
8837}
8838
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008839static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008840xmlSchemaCreatePCtxtOnVCtxt(xmlSchemaValidCtxtPtr vctxt)
8841{
8842 if (vctxt->pctxt == NULL) {
8843 if (vctxt->schema != NULL)
8844 vctxt->pctxt = xmlSchemaNewParserCtxtUseDict("*", vctxt->schema->dict);
8845 else
8846 vctxt->pctxt = xmlSchemaNewParserCtxt("*");
8847 if (vctxt->pctxt == NULL) {
8848 VERROR_INT("xmlSchemaCreatePCtxtOnVCtxt",
8849 "failed to create a temp. parser context");
8850 return (-1);
8851 }
8852 /* TODO: Pass user data. */
8853 xmlSchemaSetParserErrors(vctxt->pctxt, vctxt->error, vctxt->warning, NULL);
8854 }
8855 return (0);
8856}
8857
8858static int
8859xmlSchemaAcquireSchemaDoc(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008860 xmlSchemaPtr schema,
8861 xmlNodePtr node,
8862 const xmlChar *nsName,
8863 const xmlChar *location,
8864 xmlDocPtr *doc,
8865 const xmlChar **targetNamespace,
8866 int absolute)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008867{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008868 xmlSchemaParserCtxtPtr pctxt;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008869 xmlParserCtxtPtr parserCtxt;
8870 xmlSchemaImportPtr import;
8871 const xmlChar *ns;
8872 xmlNodePtr root;
8873
8874 /*
8875 * NOTE: This will be used for <import>, <xsi:schemaLocation> and
8876 * <xsi:noNamespaceSchemaLocation>.
8877 */
8878 *doc = NULL;
8879 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008880 * Given that the schemaLocation [attribute] is only a hint, it is open
8881 * to applications to ignore all but the first <import> for a given
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008882 * namespace, regardless of the ·actual value· of schemaLocation, but
8883 * such a strategy risks missing useful information when new
8884 * schemaLocations are offered.
8885 *
8886 * XSV (ver 2.5-2) does use the first <import> which resolves to a valid schema.
8887 * Xerces-J (ver 2.5.1) ignores all but the first given <import> - regardless if
8888 * valid or not.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008889 * We will follow XSV here.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008890 */
8891 if (location == NULL) {
8892 /*
8893 * Schema Document Location Strategy:
8894 *
8895 * 3 Based on the namespace name, identify an existing schema document,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008896 * either as a resource which is an XML document or a <schema> element
8897 * information item, in some local schema repository;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008898 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008899 * 5 Attempt to resolve the namespace name to locate such a resource.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008900 *
8901 * NOTE: Those stategies are not supported, so we will skip.
8902 */
8903 return (0);
8904 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008905 if (nsName == NULL)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008906 ns = XML_SCHEMAS_NO_NAMESPACE;
8907 else
8908 ns = nsName;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008909
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008910 import = xmlHashLookup(schema->schemasImports, ns);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008911 if (import != NULL) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008912 /*
8913 * There was a valid resource for the specified namespace already
8914 * defined, so skip.
8915 * TODO: This might be changed someday to allow import of
8916 * components from multiple documents for a single target namespace.
8917 */
8918 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008919 }
8920 if (actxt->type == XML_SCHEMA_CTXT_PARSER)
8921 pctxt = (xmlSchemaParserCtxtPtr) actxt;
8922 else {
8923 xmlSchemaCreatePCtxtOnVCtxt((xmlSchemaValidCtxtPtr) actxt);
8924 pctxt = ((xmlSchemaValidCtxtPtr) actxt)->pctxt;
8925 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008926 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008927 * Schema Document Location Strategy:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008928 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008929 * 2 Based on the location URI, identify an existing schema document,
8930 * either as a resource which is an XML document or a <schema> element
8931 * information item, in some local schema repository;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008932 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008933 * 4 Attempt to resolve the location URI, to locate a resource on the
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008934 * web which is or contains or references a <schema> element;
8935 * TODO: Hmm, I don't know if the reference stuff in 4. will work.
8936 *
8937 */
8938 if ((absolute == 0) && (node != NULL)) {
8939 xmlChar *base, *URI;
8940
8941 base = xmlNodeGetBase(node->doc, node);
8942 if (base == NULL) {
8943 URI = xmlBuildURI(location, node->doc->URL);
8944 } else {
8945 URI = xmlBuildURI(location, base);
8946 xmlFree(base);
8947 }
8948 if (URI != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008949 location = xmlDictLookup(pctxt->dict, URI, -1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008950 xmlFree(URI);
8951 }
8952 }
8953 parserCtxt = xmlNewParserCtxt();
8954 if (parserCtxt == NULL) {
8955 xmlSchemaPErrMemory(NULL, "xmlSchemaParseImport: "
8956 "allocating a parser context", NULL);
8957 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008958 }
8959 if ((pctxt->dict != NULL) && (parserCtxt->dict != NULL)) {
Kasimier T. Buchcik5dd55d92004-11-26 12:35:21 +00008960 xmlDictFree(parserCtxt->dict);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008961 parserCtxt->dict = pctxt->dict;
Kasimier T. Buchcik5dd55d92004-11-26 12:35:21 +00008962 xmlDictReference(parserCtxt->dict);
8963 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008964 *doc = xmlCtxtReadFile(parserCtxt, (const char *) location,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008965 NULL, SCHEMAS_PARSE_OPTIONS);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008966 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008967 * 2.1 The referent is (a fragment of) a resource which is an
8968 * XML document (see clause 1.1), which in turn corresponds to
8969 * a <schema> element information item in a well-formed information
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008970 * set, which in turn corresponds to a valid schema.
8971 * TODO: What to do with the "fragment" stuff?
8972 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008973 * 2.2 The referent is a <schema> element information item in
8974 * a well-formed information set, which in turn corresponds
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008975 * to a valid schema.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008976 * NOTE: 2.2 won't apply, since only XML documents will be processed
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008977 * here.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008978 */
8979 if (*doc == NULL) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008980 xmlErrorPtr lerr;
8981 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008982 * It is *not* an error for the application schema reference
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008983 * strategy to fail.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008984 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008985 * If the doc is NULL and the parser error is an IO error we
8986 * will assume that the resource could not be located or accessed.
8987 *
8988 * TODO: Try to find specific error codes to react only on
8989 * localisation failures.
8990 *
8991 * TODO, FIXME: Check the spec: is a namespace added to the imported
8992 * namespaces, even if the schemaLocation did not provide
8993 * a resource? I guess so, since omitting the "schemaLocation"
8994 * attribute, imports a namespace as well.
8995 */
8996 lerr = xmlGetLastError();
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008997 if ((lerr != NULL) && (lerr->domain == XML_FROM_IO)) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00008998 xmlFreeParserCtxt(parserCtxt);
8999 return(0);
9000 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009001 xmlSchemaCustomErr(actxt,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009002 XML_SCHEMAP_SRC_IMPORT_2_1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009003 node, NULL,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009004 "Failed to parse the resource '%s' for import",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009005 location, NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009006 xmlFreeParserCtxt(parserCtxt);
9007 return(XML_SCHEMAP_SRC_IMPORT_2_1);
9008 }
9009 xmlFreeParserCtxt(parserCtxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009010
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009011 root = xmlDocGetRootElement(*doc);
9012 if (root == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009013 xmlSchemaCustomErr(actxt,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009014 XML_SCHEMAP_SRC_IMPORT_2_1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009015 node, NULL,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009016 "The XML document '%s' to be imported has no document "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009017 "element", location, NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009018 xmlFreeDoc(*doc);
9019 *doc = NULL;
9020 return (XML_SCHEMAP_SRC_IMPORT_2_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009021 }
9022
9023 xmlSchemaCleanupDoc(pctxt, root);
9024
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009025 if (!IS_SCHEMA(root, "schema")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009026 xmlSchemaCustomErr(actxt,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009027 XML_SCHEMAP_SRC_IMPORT_2_1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009028 node, NULL,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009029 "The XML document '%s' to be imported is not a XML schema document",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009030 location, NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009031 xmlFreeDoc(*doc);
9032 *doc = NULL;
9033 return (XML_SCHEMAP_SRC_IMPORT_2_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009034 }
9035 *targetNamespace = xmlSchemaGetProp(pctxt, root, "targetNamespace");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009036 /*
9037 * Schema Representation Constraint: Import Constraints and Semantics
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009038 */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009039 if (nsName == NULL) {
9040 if (*targetNamespace != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009041 xmlSchemaCustomErr(actxt,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009042 XML_SCHEMAP_SRC_IMPORT_3_2,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009043 node, NULL,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009044 "The XML schema to be imported is not expected "
9045 "to have a target namespace; this differs from "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009046 "its target namespace of '%s'", *targetNamespace, NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009047 xmlFreeDoc(*doc);
9048 *doc = NULL;
9049 return (XML_SCHEMAP_SRC_IMPORT_3_2);
9050 }
9051 } else {
9052 if (*targetNamespace == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009053 xmlSchemaCustomErr(actxt,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009054 XML_SCHEMAP_SRC_IMPORT_3_1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009055 node, NULL,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009056 "The XML schema to be imported is expected to have a target "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009057 "namespace of '%s'", nsName, NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009058 xmlFreeDoc(*doc);
9059 *doc = NULL;
9060 return (XML_SCHEMAP_SRC_IMPORT_3_1);
9061 } else if (!xmlStrEqual(*targetNamespace, nsName)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009062 xmlSchemaCustomErr(actxt,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009063 XML_SCHEMAP_SRC_IMPORT_3_1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009064 node, NULL,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009065 "The XML schema to be imported is expected to have a "
9066 "target namespace of '%s'; this differs from "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009067 "its target namespace of '%s'",
9068 nsName, *targetNamespace);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009069 xmlFreeDoc(*doc);
9070 *doc = NULL;
9071 return (XML_SCHEMAP_SRC_IMPORT_3_1);
9072 }
9073 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009074 import = xmlSchemaAddImport(pctxt, &(schema->schemasImports), nsName);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009075 if (import == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009076 AERROR_INT("xmlSchemaAcquireSchemaDoc",
9077 "failed to build import table");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009078 xmlFreeDoc(*doc);
9079 *doc = NULL;
9080 return (-1);
9081 }
9082 import->schemaLocation = location;
9083 import->doc = *doc;
9084 return (0);
9085}
William M. Brack2f2a6632004-08-20 23:09:47 +00009086
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009087static void
9088xmlSchemaParseForImpInc(xmlSchemaParserCtxtPtr pctxt,
9089 xmlSchemaPtr schema,
9090 const xmlChar *targetNamespace,
9091 xmlNodePtr node)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009092{
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009093 const xmlChar *oldURL, **oldLocImps, *oldTNS;
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009094 int oldFlags, oldNumLocImps, oldSizeLocImps, oldIsS4S;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009095
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009096 /*
9097 * Save and reset the context & schema.
9098 */
9099 oldURL = pctxt->URL;
9100 /* TODO: Is using the doc->URL here correct? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009101 pctxt->URL = node->doc->URL;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009102 oldLocImps = pctxt->localImports;
9103 pctxt->localImports = NULL;
9104 oldNumLocImps = pctxt->nbLocalImports;
9105 pctxt->nbLocalImports = 0;
9106 oldSizeLocImps = pctxt->sizeLocalImports;
9107 pctxt->sizeLocalImports = 0;
9108 oldFlags = schema->flags;
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009109 oldIsS4S = pctxt->isS4S;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009110 xmlSchemaClearSchemaDefaults(schema);
9111 oldTNS = schema->targetNamespace;
9112 schema->targetNamespace = targetNamespace;
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009113 if ((targetNamespace != NULL) &&
9114 xmlStrEqual(targetNamespace, xmlSchemaNs)) {
9115 /*
9116 * We are parsing the schema for schema!
9117 */
9118 pctxt->isS4S = 1;
9119 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009120 /*
9121 * Parse the schema.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009122 */
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009123 xmlSchemaParseSchemaDefaults(pctxt, schema, node);
9124 xmlSchemaParseSchemaTopLevel(pctxt, schema, node->children);
9125 /*
9126 * Restore the context & schema.
9127 */
9128 schema->flags = oldFlags;
9129 schema->targetNamespace = oldTNS;
9130 if (pctxt->localImports != NULL)
9131 xmlFree((xmlChar *) pctxt->localImports);
9132 pctxt->localImports = oldLocImps;
9133 pctxt->nbLocalImports = oldNumLocImps;
9134 pctxt->sizeLocalImports = oldSizeLocImps;
9135 pctxt->URL = oldURL;
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009136 pctxt->isS4S = oldIsS4S;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009137}
9138
William M. Brack2f2a6632004-08-20 23:09:47 +00009139/**
9140 * xmlSchemaParseImport:
9141 * @ctxt: a schema validation context
9142 * @schema: the schema being built
9143 * @node: a subtree containing XML Schema informations
9144 *
9145 * parse a XML schema Import definition
9146 * *WARNING* this interface is highly subject to change
9147 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009148 * Returns 0 in case of success, a positive error code if
9149 * not valid and -1 in case of an internal error.
William M. Brack2f2a6632004-08-20 23:09:47 +00009150 */
9151static int
9152xmlSchemaParseImport(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
9153 xmlNodePtr node)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009154{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009155 xmlNodePtr child;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009156 const xmlChar *namespaceName = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00009157 const xmlChar *schemaLocation = NULL;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009158 const xmlChar *targetNamespace;
William M. Brack2f2a6632004-08-20 23:09:47 +00009159 xmlAttrPtr attr;
9160 xmlDocPtr doc;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009161 int ret = 0;
William M. Brack2f2a6632004-08-20 23:09:47 +00009162
9163 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
9164 return (-1);
9165
9166 /*
9167 * Check for illegal attributes.
9168 */
9169 attr = node->properties;
9170 while (attr != NULL) {
9171 if (attr->ns == NULL) {
9172 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9173 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
9174 (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009175 xmlSchemaPIllegalAttrErr(ctxt,
9176 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
9177 NULL, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00009178 }
9179 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009180 xmlSchemaPIllegalAttrErr(ctxt,
9181 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
9182 NULL, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00009183 }
9184 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009185 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009186 /*
9187 * Extract and validate attributes.
9188 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009189 if (xmlSchemaPValAttr(ctxt, NULL, NULL, node,
9190 "namespace", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009191 &namespaceName) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009192 xmlSchemaPSimpleTypeErr(ctxt,
9193 XML_SCHEMAP_IMPORT_NAMESPACE_NOT_URI,
9194 NULL, node,
9195 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009196 NULL, namespaceName, NULL, NULL, NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +00009197 return (XML_SCHEMAP_IMPORT_NAMESPACE_NOT_URI);
9198 }
9199
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009200 if (xmlSchemaPValAttr(ctxt, NULL, NULL, node,
9201 "schemaLocation", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
William M. Brack2f2a6632004-08-20 23:09:47 +00009202 &schemaLocation) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009203 xmlSchemaPSimpleTypeErr(ctxt,
9204 XML_SCHEMAP_IMPORT_SCHEMA_NOT_URI,
9205 NULL, node,
9206 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009207 NULL, namespaceName, NULL, NULL, NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +00009208 return (XML_SCHEMAP_IMPORT_SCHEMA_NOT_URI);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009209 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009210 /*
9211 * And now for the children...
9212 */
9213 child = node->children;
9214 if (IS_SCHEMA(child, "annotation")) {
9215 /*
9216 * the annotation here is simply discarded ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009217 * TODO: really?
William M. Brack2f2a6632004-08-20 23:09:47 +00009218 */
9219 child = child->next;
9220 }
9221 if (child != NULL) {
9222 xmlSchemaPContentErr(ctxt,
9223 XML_SCHEMAP_UNKNOWN_IMPORT_CHILD,
9224 NULL, NULL, node, child, NULL,
9225 "(annotation?)");
9226 }
9227 /*
9228 * Apply additional constraints.
9229 */
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009230 if (namespaceName != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009231 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009232 * 1.1 If the namespace [attribute] is present, then its ·actual value·
9233 * must not match the ·actual value· of the enclosing <schema>'s
William M. Brack2f2a6632004-08-20 23:09:47 +00009234 * targetNamespace [attribute].
9235 */
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009236 if (xmlStrEqual(schema->targetNamespace, namespaceName)) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009237 xmlSchemaPCustomErr(ctxt,
9238 XML_SCHEMAP_SRC_IMPORT_1_1,
9239 NULL, NULL, node,
9240 "The value of the attribute 'namespace' must not match "
9241 "the target namespace '%s' of the importing schema",
9242 schema->targetNamespace);
9243 return (XML_SCHEMAP_SRC_IMPORT_1_1);
9244 }
9245 } else {
9246 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009247 * 1.2 If the namespace [attribute] is not present, then the enclosing
William M. Brack2f2a6632004-08-20 23:09:47 +00009248 * <schema> must have a targetNamespace [attribute].
9249 */
9250 if (schema->targetNamespace == NULL) {
9251 xmlSchemaPCustomErr(ctxt,
9252 XML_SCHEMAP_SRC_IMPORT_1_2,
9253 NULL, NULL, node,
9254 "The attribute 'namespace' must be existent if "
9255 "the importing schema has no target namespace",
9256 NULL);
9257 return (XML_SCHEMAP_SRC_IMPORT_1_2);
9258 }
9259 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009260 /*
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009261 * Add the namespace to the list of locally imported namespace.
9262 */
9263 if (ctxt->localImports == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009264 ctxt->localImports = (const xmlChar **) xmlMalloc(10 *
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009265 sizeof(const xmlChar*));
9266 ctxt->sizeLocalImports = 10;
9267 ctxt->nbLocalImports = 0;
9268 } else if (ctxt->sizeLocalImports <= ctxt->nbLocalImports) {
9269 ctxt->sizeLocalImports *= 2;
9270 ctxt->localImports = (const xmlChar **) xmlRealloc(
9271 (xmlChar **) ctxt->localImports,
9272 ctxt->sizeLocalImports * sizeof(const xmlChar*));
9273 }
9274 ctxt->localImports[ctxt->nbLocalImports++] = namespaceName;
9275 /*
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009276 * Locate and aquire the schema document.
William M. Brack2f2a6632004-08-20 23:09:47 +00009277 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009278 ret = xmlSchemaAcquireSchemaDoc((xmlSchemaAbstractCtxtPtr) ctxt,
9279 schema, node, namespaceName,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009280 schemaLocation, &doc, &targetNamespace, 0);
9281 if (ret != 0) {
9282 if (doc != NULL)
William M. Brack2f2a6632004-08-20 23:09:47 +00009283 xmlFreeDoc(doc);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009284 return (ret);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009285 } else if (doc != NULL) {
9286 xmlSchemaParseForImpInc(ctxt, schema, targetNamespace,
9287 xmlDocGetRootElement(doc));
William M. Brack2f2a6632004-08-20 23:09:47 +00009288 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009289
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009290 return (0);
William M. Brack2f2a6632004-08-20 23:09:47 +00009291}
9292
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009293/**
9294 * xmlSchemaParseInclude:
9295 * @ctxt: a schema validation context
9296 * @schema: the schema being built
9297 * @node: a subtree containing XML Schema informations
9298 *
9299 * parse a XML schema Include definition
9300 *
William M. Bracke7091952004-05-11 15:09:58 +00009301 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009302 * 1 in case of success.
9303 */
9304static int
9305xmlSchemaParseInclude(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
9306 xmlNodePtr node)
9307{
9308 xmlNodePtr child = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009309 const xmlChar *schemaLocation, *targetNamespace;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009310 xmlDocPtr doc = NULL;
9311 xmlNodePtr root = NULL;
Daniel Veillardcffc1c72005-03-12 18:54:55 +00009312 xmlSchemaIncludePtr include = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009313 int wasConvertingNs = 0;
William M. Brack2f2a6632004-08-20 23:09:47 +00009314 xmlAttrPtr attr;
Kasimier T. Buchcik5dd55d92004-11-26 12:35:21 +00009315 xmlParserCtxtPtr parserCtxt;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009316
9317
9318 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
9319 return (-1);
9320
9321 /*
William M. Brack2f2a6632004-08-20 23:09:47 +00009322 * Check for illegal attributes.
9323 */
9324 attr = node->properties;
9325 while (attr != NULL) {
9326 if (attr->ns == NULL) {
9327 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9328 (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009329 xmlSchemaPIllegalAttrErr(ctxt,
9330 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
9331 NULL, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00009332 }
9333 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009334 xmlSchemaPIllegalAttrErr(ctxt,
9335 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
9336 NULL, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00009337 }
9338 attr = attr->next;
9339 }
9340 /*
9341 * Extract and validate attributes.
9342 */
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009343 xmlSchemaPValAttrID(ctxt, NULL, NULL, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +00009344 /*
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009345 * Preliminary step, extract the URI-Reference for the include and
9346 * make an URI from the base.
9347 */
William M. Brack2f2a6632004-08-20 23:09:47 +00009348 attr = xmlSchemaGetPropNode(node, "schemaLocation");
9349 if (attr != NULL) {
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009350 xmlChar *base = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00009351 xmlChar *uri = NULL;
9352
9353 if (xmlSchemaPValAttrNode(ctxt, NULL, NULL, attr,
9354 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), &schemaLocation) != 0)
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009355 goto exit_invalid;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009356 base = xmlNodeGetBase(node->doc, node);
9357 if (base == NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009358 uri = xmlBuildURI(schemaLocation, node->doc->URL);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009359 } else {
William M. Brack2f2a6632004-08-20 23:09:47 +00009360 uri = xmlBuildURI(schemaLocation, base);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009361 xmlFree(base);
9362 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009363 if (uri == NULL) {
9364 xmlSchemaPErr(ctxt,
9365 node,
9366 XML_SCHEMAP_INTERNAL,
9367 "Internal error: xmlSchemaParseInclude, "
9368 "could not build an URI from the schemaLocation.\n",
9369 NULL, NULL);
9370 goto exit_failure;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009371 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009372 schemaLocation = xmlDictLookup(ctxt->dict, uri, -1);
9373 xmlFree(uri);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009374 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009375 xmlSchemaPMissingAttrErr(ctxt,
9376 XML_SCHEMAP_INCLUDE_SCHEMA_NO_URI,
9377 NULL, node, "schemaLocation", NULL);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009378 goto exit_invalid;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009379 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009380 /*
9381 * And now for the children...
9382 */
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009383 child = node->children;
9384 while (IS_SCHEMA(child, "annotation")) {
9385 /*
9386 * the annotations here are simply discarded ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009387 * TODO: really?
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009388 */
9389 child = child->next;
9390 }
9391 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009392 xmlSchemaPContentErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009393 XML_SCHEMAP_UNKNOWN_INCLUDE_CHILD,
9394 NULL, NULL, node, child, NULL,
9395 "(annotation?)");
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009396 }
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009397 /*
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009398 * Report self-inclusion.
9399 */
9400 if (xmlStrEqual(schemaLocation, ctxt->URL)) {
9401 xmlSchemaPCustomErr(ctxt,
9402 XML_SCHEMAP_SRC_INCLUDE,
9403 NULL, NULL, node,
9404 "The schema document '%s' cannot include itself.",
9405 schemaLocation);
9406 return (XML_SCHEMAP_SRC_INCLUDE);
9407 }
9408 /*
9409 * Check if this one was already processed to avoid incorrect
9410 * duplicate component errors and infinite circular inclusion.
9411 */
9412 include = schema->includes;
9413 while (include != NULL) {
9414 if (xmlStrEqual(include->schemaLocation, schemaLocation)) {
9415 targetNamespace = include->origTargetNamespace;
9416 if (targetNamespace == NULL) {
9417 /*
9418 * Chameleon include: skip only if it was build for
9419 * the targetNamespace of the including schema.
9420 */
9421 if (xmlStrEqual(schema->targetNamespace,
9422 include->targetNamespace)) {
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009423 goto check_targetNamespace;
9424 }
9425 } else {
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009426 goto check_targetNamespace;
9427 }
9428 }
9429 include = include->next;
9430 }
9431 /*
9432 * First step is to parse the input document into an DOM/Infoset
9433 * TODO: Use xmlCtxtReadFile to share the dictionary?
Kasimier T. Buchcik5dd55d92004-11-26 12:35:21 +00009434 */
9435 parserCtxt = xmlNewParserCtxt();
9436 if (parserCtxt == NULL) {
9437 xmlSchemaPErrMemory(NULL, "xmlSchemaParseInclude: "
9438 "allocating a parser context", NULL);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009439 goto exit_failure;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009440 }
9441
Kasimier T. Buchcik5dd55d92004-11-26 12:35:21 +00009442 if ((ctxt->dict != NULL) && (parserCtxt->dict != NULL)) {
9443 xmlDictFree(parserCtxt->dict);
9444 parserCtxt->dict = ctxt->dict;
9445 xmlDictReference(parserCtxt->dict);
9446 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009447
9448 doc = xmlCtxtReadFile(parserCtxt, (const char *) schemaLocation,
Kasimier T. Buchcik5dd55d92004-11-26 12:35:21 +00009449 NULL, SCHEMAS_PARSE_OPTIONS);
9450 xmlFreeParserCtxt(parserCtxt);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009451 if (doc == NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009452 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009453 * TODO: It is not an error for the ·actual value· of the
9454 * schemaLocation [attribute] to fail to resolve it all, in which
9455 * case no corresponding inclusion is performed.
William M. Brack2f2a6632004-08-20 23:09:47 +00009456 * So do we need a warning report here?
9457 */
9458 xmlSchemaPCustomErr(ctxt,
9459 XML_SCHEMAP_FAILED_LOAD,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009460 NULL, NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +00009461 "Failed to load the document '%s' for inclusion", schemaLocation);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009462 goto exit_invalid;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009463 }
9464
9465 /*
9466 * Then extract the root of the schema
9467 */
9468 root = xmlDocGetRootElement(doc);
9469 if (root == NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009470 xmlSchemaPCustomErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009471 XML_SCHEMAP_NOROOT,
William M. Brack2f2a6632004-08-20 23:09:47 +00009472 NULL, NULL, node,
9473 "The included document '%s' has no document "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009474 "element", schemaLocation);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009475 goto exit_invalid;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009476 }
9477
9478 /*
9479 * Remove all the blank text nodes
9480 */
9481 xmlSchemaCleanupDoc(ctxt, root);
9482
9483 /*
9484 * Check the schemas top level element
9485 */
9486 if (!IS_SCHEMA(root, "schema")) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009487 xmlSchemaPCustomErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009488 XML_SCHEMAP_NOT_SCHEMA,
William M. Brack2f2a6632004-08-20 23:09:47 +00009489 NULL, NULL, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009490 "The document '%s' to be included is not a schema document",
William M. Brack2f2a6632004-08-20 23:09:47 +00009491 schemaLocation);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009492 goto exit_invalid;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009493 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009494
William M. Brack2f2a6632004-08-20 23:09:47 +00009495 targetNamespace = xmlSchemaGetProp(ctxt, root, "targetNamespace");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009496 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009497 * 2.1 SII has a targetNamespace [attribute], and its ·actual
9498 * value· is identical to the ·actual value· of the targetNamespace
Daniel Veillardc0826a72004-08-10 14:17:33 +00009499 * [attribute] of SIIÂ’ (which must have such an [attribute]).
9500 */
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009501check_targetNamespace:
Daniel Veillardc0826a72004-08-10 14:17:33 +00009502 if (targetNamespace != NULL) {
9503 if (schema->targetNamespace == NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009504 xmlSchemaPCustomErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009505 XML_SCHEMAP_SRC_INCLUDE,
William M. Brack2f2a6632004-08-20 23:09:47 +00009506 NULL, NULL, node,
9507 "The target namespace of the included schema "
9508 "'%s' has to be absent, since the including schema "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009509 "has no target namespace",
9510 schemaLocation);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009511 goto exit_invalid;
William M. Brack2f2a6632004-08-20 23:09:47 +00009512 } else if (!xmlStrEqual(targetNamespace, schema->targetNamespace)) {
9513 xmlSchemaPCustomErrExt(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009514 XML_SCHEMAP_SRC_INCLUDE,
William M. Brack2f2a6632004-08-20 23:09:47 +00009515 NULL, NULL, node,
9516 "The target namespace '%s' of the included schema '%s' "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009517 "differs from '%s' of the including schema",
William M. Brack2f2a6632004-08-20 23:09:47 +00009518 targetNamespace, schemaLocation, schema->targetNamespace);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009519 goto exit_invalid;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009520 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009521 } else if (schema->targetNamespace != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00009522 if ((schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009523 schema->flags |= XML_SCHEMAS_INCLUDING_CONVERT_NS;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009524 } else
9525 wasConvertingNs = 1;
9526 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009527
9528 if (include != NULL)
9529 goto exit;
9530
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009531 /*
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009532 * URGENT TODO: If the schema is a chameleon-include then copy the
9533 * components into the including schema and modify the targetNamespace
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009534 * of those components, do nothing otherwise.
9535 * NOTE: This is currently worked-around by compiling the chameleon
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009536 * for every destinct including targetNamespace; thus not performant at
9537 * the moment.
9538 * TODO: Check when the namespace in wildcards for chameleons needs
9539 * to be converted: before we built wildcard intersections or after.
9540 */
9541 /*
9542 * Register the include.
9543 */
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009544 include = (xmlSchemaIncludePtr) xmlMalloc(sizeof(xmlSchemaInclude));
9545 if (include == NULL) {
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009546 xmlSchemaPErrMemory(ctxt, "allocating include entry", NULL);
9547 goto exit_failure;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009548 }
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009549 memset(include, 0, sizeof(xmlSchemaInclude));
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009550 include->next = schema->includes;
9551 schema->includes = include;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009552 /*
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009553 * TODO: Use the resolved URI for the this location, since it might
9554 * differ if using filenames/URIs simultaneosly.
Daniel Veillardc0826a72004-08-10 14:17:33 +00009555 */
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009556 include->schemaLocation = schemaLocation;
9557 include->doc = doc;
9558 /*
9559 * In case of chameleons, the original target namespace will differ
9560 * from the resulting namespace.
9561 */
9562 include->origTargetNamespace = targetNamespace;
9563 include->targetNamespace = schema->targetNamespace;
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +00009564#ifdef DEBUG_INCLUDES
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009565 if (targetNamespace != schema->targetNamespace)
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +00009566 xmlGenericError(xmlGenericErrorContext,
9567 "INCLUDING CHAMELEON '%s'\n orig TNS '%s'\n"
9568 " into TNS '%s'\n", schemaLocation,
9569 targetNamespace, schema->targetNamespace);
9570 else
9571 xmlGenericError(xmlGenericErrorContext,
9572 "INCLUDING '%s'\n orig-TNS '%s'\n", schemaLocation,
9573 targetNamespace);
9574#endif
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009575 /*
9576 * Compile the included schema.
9577 */
9578 xmlSchemaParseForImpInc(ctxt, schema, schema->targetNamespace, root);
9579
9580exit:
Daniel Veillardc0826a72004-08-10 14:17:33 +00009581 /*
9582 * Remove the converting flag.
9583 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009584 if ((wasConvertingNs == 0) &&
William M. Brack2f2a6632004-08-20 23:09:47 +00009585 (schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS))
Daniel Veillardc0826a72004-08-10 14:17:33 +00009586 schema->flags ^= XML_SCHEMAS_INCLUDING_CONVERT_NS;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009587 return (1);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009588
9589exit_invalid:
9590 if (doc != NULL) {
9591 if (include != NULL)
9592 include->doc = NULL;
9593 xmlFreeDoc(doc);
9594 }
9595 return (ctxt->err);
9596
9597exit_failure:
9598 if (doc != NULL) {
9599 if (include != NULL)
9600 include->doc = NULL;
9601 xmlFreeDoc(doc);
9602 }
9603 return (-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009604}
9605
9606/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009607 * xmlSchemaParseModelGroup:
Daniel Veillard4255d502002-04-16 15:50:10 +00009608 * @ctxt: a schema validation context
9609 * @schema: the schema being built
9610 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009611 * @type: the "compositor" type
9612 * @particleNeeded: if a a model group with a particle
Daniel Veillard4255d502002-04-16 15:50:10 +00009613 *
9614 * parse a XML schema Sequence definition
9615 * *WARNING* this interface is highly subject to change
9616 *
William M. Bracke7091952004-05-11 15:09:58 +00009617 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00009618 * 1 in case of success.
9619 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009620static xmlSchemaTreeItemPtr
9621xmlSchemaParseModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
9622 xmlNodePtr node, xmlSchemaTypeType type,
9623 int withParticle)
Daniel Veillard4255d502002-04-16 15:50:10 +00009624{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009625 xmlSchemaModelGroupPtr item;
9626 xmlSchemaParticlePtr particle = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00009627 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00009628 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009629 const xmlChar *oldcontainer, *container;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00009630 int min = 0, max = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00009631
9632 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009633 return (NULL);
9634 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009635 * Create a model group with the given compositor.
William M. Brack2f2a6632004-08-20 23:09:47 +00009636 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009637 item = xmlSchemaAddModelGroup(ctxt, schema, type, &container, node);
9638 if (item == NULL)
9639 return (NULL);
9640
9641 if (withParticle) {
9642 if (type == XML_SCHEMA_TYPE_ALL) {
9643 min = xmlGetMinOccurs(ctxt, node, 0, 1, 1, "(0 | 1)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009644 max = xmlGetMaxOccurs(ctxt, node, 1, 1, 1, "1");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009645 } else {
9646 /* choice + sequence */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009647 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
9648 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
9649 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00009650 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009651 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
9652 /*
9653 * Create a particle
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009654 */
9655 particle = xmlSchemaAddParticle(ctxt, schema, node, min, max);
9656 if (particle == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009657 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009658 particle->children = (xmlSchemaTreeItemPtr) item;
9659 /*
9660 * Check for illegal attributes.
9661 */
9662 attr = node->properties;
9663 while (attr != NULL) {
9664 if (attr->ns == NULL) {
9665 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9666 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
9667 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009668 xmlSchemaPIllegalAttrErr(ctxt,
9669 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
9670 NULL, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009671 }
9672 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009673 xmlSchemaPIllegalAttrErr(ctxt,
9674 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
9675 NULL, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00009676 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009677 attr = attr->next;
William M. Brack2f2a6632004-08-20 23:09:47 +00009678 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009679 } else {
9680 /*
9681 * Check for illegal attributes.
9682 */
9683 attr = node->properties;
9684 while (attr != NULL) {
9685 if (attr->ns == NULL) {
9686 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009687 xmlSchemaPIllegalAttrErr(ctxt,
9688 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
9689 NULL, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009690 }
9691 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009692 xmlSchemaPIllegalAttrErr(ctxt,
9693 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
9694 NULL, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009695 }
9696 attr = attr->next;
9697 }
9698
William M. Brack2f2a6632004-08-20 23:09:47 +00009699 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009700
William M. Brack2f2a6632004-08-20 23:09:47 +00009701 /*
9702 * Extract and validate attributes.
9703 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009704 xmlSchemaPValAttrID(ctxt, NULL, NULL, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +00009705 /*
9706 * And now for the children...
9707 */
Daniel Veillard4255d502002-04-16 15:50:10 +00009708 child = node->children;
9709 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009710 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009711 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009712 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009713 oldcontainer = ctxt->container;
9714 ctxt->container = container;
9715 if (type == XML_SCHEMA_TYPE_ALL) {
9716 xmlSchemaParticlePtr part, last = NULL;
9717
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009718 while (IS_SCHEMA(child, "element")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009719 part = (xmlSchemaParticlePtr) xmlSchemaParseElement(ctxt,
9720 schema, child, 0);
9721 if (part != NULL) {
9722 if (part->minOccurs > 1)
9723 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_INVALID_MINOCCURS,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009724 NULL, NULL, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009725 "Invalid value for minOccurs (must be 0 or 1)", NULL);
9726 if (part->maxOccurs > 1)
9727 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_INVALID_MAXOCCURS,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009728 NULL, NULL, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009729 "Invalid value for maxOccurs (must be 0 or 1)",
9730 NULL);
9731 if (last == NULL)
9732 item->children = (xmlSchemaTreeItemPtr) part;
9733 else
9734 last->next = (xmlSchemaTreeItemPtr) part;
9735 last = part;
9736 }
9737 child = child->next;
9738 }
9739 if (child != NULL) {
9740 xmlSchemaPContentErr(ctxt,
9741 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
9742 NULL, NULL, node, child, NULL,
9743 "(annotation?, (annotation?, element*)");
9744 }
9745 } else {
9746 /* choice + sequence */
9747 xmlSchemaTreeItemPtr part = NULL, last = NULL;
9748
9749 while ((IS_SCHEMA(child, "element")) ||
9750 (IS_SCHEMA(child, "group")) ||
9751 (IS_SCHEMA(child, "any")) ||
9752 (IS_SCHEMA(child, "choice")) ||
9753 (IS_SCHEMA(child, "sequence"))) {
9754
9755 if (IS_SCHEMA(child, "element")) {
9756 part = (xmlSchemaTreeItemPtr)
9757 xmlSchemaParseElement(ctxt, schema, child, 0);
9758 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009759 part =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009760 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
9761 } else if (IS_SCHEMA(child, "any")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009762 part = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009763 xmlSchemaParseAny(ctxt, schema, child);
9764 } else if (IS_SCHEMA(child, "choice")) {
9765 part = xmlSchemaParseModelGroup(ctxt, schema, child,
9766 XML_SCHEMA_TYPE_CHOICE, 1);
9767 } else if (IS_SCHEMA(child, "sequence")) {
9768 part = xmlSchemaParseModelGroup(ctxt, schema, child,
9769 XML_SCHEMA_TYPE_SEQUENCE, 1);
9770 }
9771 if (part != NULL) {
9772 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009773 item->children = part;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009774 else
9775 last->next = part;
9776 last = part;
9777 }
9778 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009779 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009780 if (child != NULL) {
9781 xmlSchemaPContentErr(ctxt,
9782 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
9783 NULL, NULL, node, child, NULL,
9784 "(annotation?, (element | group | choice | sequence | any)*)");
9785 }
Daniel Veillard4255d502002-04-16 15:50:10 +00009786 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009787 ctxt->container = oldcontainer;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00009788 if (withParticle) {
9789 if ((min == 0) && (max == 0))
9790 return (NULL);
9791 else
9792 return ((xmlSchemaTreeItemPtr) particle);
9793 } else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009794 return ((xmlSchemaTreeItemPtr) item);
Daniel Veillard4255d502002-04-16 15:50:10 +00009795}
9796
9797/**
9798 * xmlSchemaParseRestriction:
9799 * @ctxt: a schema validation context
9800 * @schema: the schema being built
9801 * @node: a subtree containing XML Schema informations
Daniel Veillard4255d502002-04-16 15:50:10 +00009802 *
9803 * parse a XML schema Restriction definition
9804 * *WARNING* this interface is highly subject to change
9805 *
9806 * Returns the type definition or NULL in case of error
9807 */
9808static xmlSchemaTypePtr
9809xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009810 xmlNodePtr node, xmlSchemaTypeType parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +00009811{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009812 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00009813 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009814 char buf[30];
9815 const xmlChar *oldcontainer, *container;
William M. Brack2f2a6632004-08-20 23:09:47 +00009816 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00009817
9818 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
9819 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009820 /* Not a component, don't create it. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009821 type = ctxt->ctxtType;
9822 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009823
9824 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009825 * TODO: Is the container needed at all? the anonymous
9826 * items inside should generate unique names already.
9827 */
9828 snprintf(buf, 29, "#restr%d", ctxt->counter++ + 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009829 container = xmlDictLookup(ctxt->dict, BAD_CAST buf, -1);
William M. Brack2f2a6632004-08-20 23:09:47 +00009830 /*
9831 * Check for illegal attributes.
9832 */
9833 attr = node->properties;
9834 while (attr != NULL) {
9835 if (attr->ns == NULL) {
9836 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9837 (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009838 xmlSchemaPIllegalAttrErr(ctxt,
9839 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
9840 NULL, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00009841 }
9842 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009843 xmlSchemaPIllegalAttrErr(ctxt,
9844 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
9845 NULL, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00009846 }
9847 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009848 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009849 /*
9850 * Extract and validate attributes.
9851 */
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00009852 xmlSchemaPValAttrID(ctxt, NULL, NULL, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +00009853 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009854 * Attribute "base" - mandatory if inside a complex type.
William M. Brack2f2a6632004-08-20 23:09:47 +00009855 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009856 /*
9857 * SPEC (1.2) "otherwise (<restriction> has no <simpleType> "
9858 * among its [children]), the simple type definition which is
9859 * the {content type} of the type definition ·resolved· to by
9860 * the ·actual value· of the base [attribute]"
9861 */
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +00009862 if ((xmlSchemaPValAttrQName(ctxt, schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009863 NULL, NULL, node, "base",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009864 &(type->baseNs), &(type->base)) == 0) &&
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +00009865 (type->base == NULL) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009866 (type->type == XML_SCHEMA_TYPE_COMPLEX)) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009867 xmlSchemaPMissingAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009868 XML_SCHEMAP_S4S_ATTR_MISSING,
9869 type, node, "base", NULL);
9870 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009871 /*
9872 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009873 */
Daniel Veillard4255d502002-04-16 15:50:10 +00009874 child = node->children;
9875 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009876 /*
9877 * Add the annotation to the simple type ancestor.
9878 */
9879 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
9880 xmlSchemaParseAnnotation(ctxt, schema, child));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009881 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009882 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009883 oldcontainer = ctxt->container;
9884 ctxt->container = container;
9885 if (parentType == XML_SCHEMA_TYPE_SIMPLE) {
9886 /*
9887 * Corresponds to <simpleType><restriction><simpleType>.
9888 */
William M. Brack2f2a6632004-08-20 23:09:47 +00009889 if (IS_SCHEMA(child, "simpleType")) {
9890 if (type->base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009891 /*
William M. Brack2f2a6632004-08-20 23:09:47 +00009892 * src-restriction-base-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009893 * Either the base [attribute] or the simpleType [child] of the
9894 * <restriction> element must be present, but not both.
William M. Brack2f2a6632004-08-20 23:09:47 +00009895 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009896 xmlSchemaPContentErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009897 XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009898 NULL, NULL, node, child,
William M. Brack2f2a6632004-08-20 23:09:47 +00009899 "The attribute 'base' and the <simpleType> child are "
9900 "mutually exclusive", NULL);
9901 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009902 type->baseType = (xmlSchemaTypePtr)
William M. Brack2f2a6632004-08-20 23:09:47 +00009903 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009904 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009905 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009906 } else if (type->base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009907 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009908 XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
9909 NULL, NULL, node, child,
9910 "Either the attribute 'base' or a <simpleType> child "
9911 "must be present", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +00009912 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009913 } else if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
9914 /*
9915 * Corresponds to <complexType><complexContent><restriction>...
9916 * followed by:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009917 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009918 * Model groups <all>, <choice> and <sequence>.
9919 */
9920 if (IS_SCHEMA(child, "all")) {
9921 type->subtypes = (xmlSchemaTypePtr)
9922 xmlSchemaParseModelGroup(ctxt, schema, child,
9923 XML_SCHEMA_TYPE_ALL, 1);
Daniel Veillard01fa6152004-06-29 17:04:39 +00009924 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009925 } else if (IS_SCHEMA(child, "choice")) {
9926 type->subtypes = (xmlSchemaTypePtr)
9927 xmlSchemaParseModelGroup(ctxt,
9928 schema, child, XML_SCHEMA_TYPE_CHOICE, 1);
9929 child = child->next;
9930 } else if (IS_SCHEMA(child, "sequence")) {
9931 type->subtypes = (xmlSchemaTypePtr)
9932 xmlSchemaParseModelGroup(ctxt, schema, child,
9933 XML_SCHEMA_TYPE_SEQUENCE, 1);
9934 child = child->next;
9935 /*
9936 * Model group reference <group>.
9937 */
9938 } else if (IS_SCHEMA(child, "group")) {
9939 type->subtypes = (xmlSchemaTypePtr)
9940 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
9941 child = child->next;
9942 }
9943 } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009944 /*
9945 * Corresponds to <complexType><simpleContent><restriction>...
9946 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009947 * "1.1 the simple type definition corresponding to the <simpleType>
9948 * among the [children] of <restriction> if there is one;"
9949 */
9950 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009951 /*
9952 * We will store the to-be-restricted simple type in
9953 * type->contentTypeDef *temporarily*.
9954 */
9955 type->contentTypeDef = (xmlSchemaTypePtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009956 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009957 if ( type->contentTypeDef == NULL)
9958 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009959 child = child->next;
9960 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009961 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009962
9963 if ((parentType == XML_SCHEMA_TYPE_SIMPLE) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009964 (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009965 xmlSchemaFacetPtr facet, lastfacet = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009966 /*
9967 * Corresponds to <complexType><simpleContent><restriction>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009968 * <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009969 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009970
Daniel Veillard01fa6152004-06-29 17:04:39 +00009971 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009972 * Add the facets to the simple type ancestor.
Daniel Veillard01fa6152004-06-29 17:04:39 +00009973 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00009974 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009975 * TODO: Datatypes: 4.1.3 Constraints on XML Representation of
9976 * Simple Type Definition Schema Representation Constraint:
Daniel Veillardc0826a72004-08-10 14:17:33 +00009977 * *Single Facet Value*
9978 */
Daniel Veillard01fa6152004-06-29 17:04:39 +00009979 while ((IS_SCHEMA(child, "minInclusive")) ||
9980 (IS_SCHEMA(child, "minExclusive")) ||
9981 (IS_SCHEMA(child, "maxInclusive")) ||
9982 (IS_SCHEMA(child, "maxExclusive")) ||
9983 (IS_SCHEMA(child, "totalDigits")) ||
9984 (IS_SCHEMA(child, "fractionDigits")) ||
9985 (IS_SCHEMA(child, "pattern")) ||
9986 (IS_SCHEMA(child, "enumeration")) ||
9987 (IS_SCHEMA(child, "whiteSpace")) ||
9988 (IS_SCHEMA(child, "length")) ||
9989 (IS_SCHEMA(child, "maxLength")) ||
9990 (IS_SCHEMA(child, "minLength"))) {
9991 facet = xmlSchemaParseFacet(ctxt, schema, child);
9992 if (facet != NULL) {
9993 if (lastfacet == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009994 type->facets = facet;
Daniel Veillard01fa6152004-06-29 17:04:39 +00009995 else
9996 lastfacet->next = facet;
9997 lastfacet = facet;
9998 lastfacet->next = NULL;
9999 }
10000 child = child->next;
10001 }
10002 /*
10003 * Create links for derivation and validation.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010004 */
10005 if (type->facets != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000010006 xmlSchemaFacetLinkPtr facetLink, lastFacetLink = NULL;
10007
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010008 facet = type->facets;
10009 do {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010010 facetLink = (xmlSchemaFacetLinkPtr)
10011 xmlMalloc(sizeof(xmlSchemaFacetLink));
Daniel Veillard01fa6152004-06-29 17:04:39 +000010012 if (facetLink == NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +000010013 xmlSchemaPErrMemory(ctxt, "allocating a facet link", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000010014 xmlFree(facetLink);
10015 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010016 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000010017 facetLink->facet = facet;
10018 facetLink->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010019 if (lastFacetLink == NULL)
10020 type->facetSet = facetLink;
Daniel Veillard01fa6152004-06-29 17:04:39 +000010021 else
10022 lastFacetLink->next = facetLink;
10023 lastFacetLink = facetLink;
10024 facet = facet->next;
10025 } while (facet != NULL);
10026 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010027 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010028 if (type->type == XML_SCHEMA_TYPE_COMPLEX) {
10029 /*
10030 * Attribute uses/declarations.
10031 */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010032 child = xmlSchemaParseAttrDecls(ctxt, schema, child, type);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010033 /*
10034 * Attribute wildcard.
10035 */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010036 if (IS_SCHEMA(child, "anyAttribute")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010037 type->attributeWildcard =
10038 xmlSchemaParseAnyAttribute(ctxt, schema, child);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010039 child = child->next;
10040 }
10041 }
Daniel Veillard4255d502002-04-16 15:50:10 +000010042 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010043 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
10044 xmlSchemaPContentErr(ctxt,
10045 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010046 NULL, NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000010047 "annotation?, (group | all | choice | sequence)?, "
10048 "((attribute | attributeGroup)*, anyAttribute?))");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010049 } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010050 xmlSchemaPContentErr(ctxt,
10051 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010052 NULL, NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000010053 "(annotation?, (simpleType?, (minExclusive | minInclusive | "
10054 "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
10055 "length | minLength | maxLength | enumeration | whiteSpace | "
10056 "pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?))");
10057 } else {
10058 /* Simple type */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010059 xmlSchemaPContentErr(ctxt,
10060 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010061 NULL, NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000010062 "(annotation?, (simpleType?, (minExclusive | minInclusive | "
10063 "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
10064 "length | minLength | maxLength | enumeration | whiteSpace | "
10065 "pattern)*))");
10066 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010067 }
Daniel Veillard4255d502002-04-16 15:50:10 +000010068 ctxt->container = oldcontainer;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010069 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000010070}
10071
10072/**
10073 * xmlSchemaParseExtension:
10074 * @ctxt: a schema validation context
10075 * @schema: the schema being built
10076 * @node: a subtree containing XML Schema informations
10077 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010078 * Parses an <extension>, which is found inside a
10079 * <simpleContent> or <complexContent>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010080 * *WARNING* this interface is highly subject to change.
Daniel Veillard4255d502002-04-16 15:50:10 +000010081 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010082 * TODO: Returns the type definition or NULL in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +000010083 */
10084static xmlSchemaTypePtr
10085xmlSchemaParseExtension(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010086 xmlNodePtr node, xmlSchemaTypeType parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +000010087{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010088 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000010089 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010090 char buf[30];
10091 const xmlChar *oldcontainer, *container;
10092 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000010093
10094 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
10095 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010096 /* Not a component, don't create it. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010097 type = ctxt->ctxtType;
10098 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION;
Daniel Veillard4255d502002-04-16 15:50:10 +000010099
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010100 snprintf(buf, 29, "#ext%d", ctxt->counter++ + 1);
10101 container = xmlDictLookup(ctxt->dict, BAD_CAST buf, -1);
10102 /*
10103 * Check for illegal attributes.
10104 */
10105 attr = node->properties;
10106 while (attr != NULL) {
10107 if (attr->ns == NULL) {
10108 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
10109 (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010110 xmlSchemaPIllegalAttrErr(ctxt,
10111 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
10112 NULL, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010113 }
10114 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010115 xmlSchemaPIllegalAttrErr(ctxt,
10116 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
10117 NULL, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010118 }
10119 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010120 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000010121
10122 xmlSchemaPValAttrID(ctxt, NULL, NULL, node, BAD_CAST "id");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010123
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000010124 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010125 * Attribute "base" - mandatory.
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000010126 */
10127 if ((xmlSchemaPValAttrQName(ctxt, schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010128 NULL, NULL, node, "base", &(type->baseNs), &(type->base)) == 0) &&
10129 (type->base == NULL)) {
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000010130 xmlSchemaPMissingAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010131 XML_SCHEMAP_S4S_ATTR_MISSING,
10132 NULL, node, "base", NULL);
10133 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010134 /*
10135 * And now for the children...
10136 */
Daniel Veillard4255d502002-04-16 15:50:10 +000010137 child = node->children;
10138 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010139 /*
10140 * Add the annotation to the type ancestor.
10141 */
10142 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
10143 xmlSchemaParseAnnotation(ctxt, schema, child));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010144 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000010145 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010146 oldcontainer = ctxt->container;
10147 ctxt->container = container;
10148 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
10149 /*
10150 * Corresponds to <complexType><complexContent><extension>... and:
10151 *
10152 * Model groups <all>, <choice>, <sequence> and <group>.
10153 */
10154 if (IS_SCHEMA(child, "all")) {
10155 type->subtypes = (xmlSchemaTypePtr)
10156 xmlSchemaParseModelGroup(ctxt, schema,
10157 child, XML_SCHEMA_TYPE_ALL, 1);
10158 child = child->next;
10159 } else if (IS_SCHEMA(child, "choice")) {
10160 type->subtypes = (xmlSchemaTypePtr)
10161 xmlSchemaParseModelGroup(ctxt, schema,
10162 child, XML_SCHEMA_TYPE_CHOICE, 1);
10163 child = child->next;
10164 } else if (IS_SCHEMA(child, "sequence")) {
10165 type->subtypes = (xmlSchemaTypePtr)
10166 xmlSchemaParseModelGroup(ctxt, schema,
10167 child, XML_SCHEMA_TYPE_SEQUENCE, 1);
10168 child = child->next;
10169 } else if (IS_SCHEMA(child, "group")) {
10170 type->subtypes = (xmlSchemaTypePtr)
10171 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
10172 child = child->next;
10173 }
Daniel Veillard4255d502002-04-16 15:50:10 +000010174 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010175 if (child != NULL) {
10176 /*
10177 * Attribute uses/declarations.
10178 */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010179 child = xmlSchemaParseAttrDecls(ctxt, schema, child, type);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010180 /*
10181 * Attribute wildcard.
10182 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010183 if (IS_SCHEMA(child, "anyAttribute")) {
10184 ctxt->ctxtType->attributeWildcard =
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010185 xmlSchemaParseAnyAttribute(ctxt, schema, child);
10186 child = child->next;
10187 }
10188 }
Daniel Veillard4255d502002-04-16 15:50:10 +000010189 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010190 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
10191 /* Complex content extension. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010192 xmlSchemaPContentErr(ctxt,
10193 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010194 NULL, NULL, node, child, NULL,
10195 "(annotation?, ((group | all | choice | sequence)?, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010196 "((attribute | attributeGroup)*, anyAttribute?)))");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010197 } else {
10198 /* Simple content extension. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010199 xmlSchemaPContentErr(ctxt,
10200 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010201 NULL, NULL, node, child, NULL,
10202 "(annotation?, ((attribute | attributeGroup)*, "
10203 "anyAttribute?))");
10204 }
Daniel Veillard4255d502002-04-16 15:50:10 +000010205 }
10206 ctxt->container = oldcontainer;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010207 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000010208}
10209
10210/**
10211 * xmlSchemaParseSimpleContent:
10212 * @ctxt: a schema validation context
10213 * @schema: the schema being built
10214 * @node: a subtree containing XML Schema informations
10215 *
10216 * parse a XML schema SimpleContent definition
10217 * *WARNING* this interface is highly subject to change
10218 *
10219 * Returns the type definition or NULL in case of error
10220 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010221static int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010222xmlSchemaParseSimpleContent(xmlSchemaParserCtxtPtr ctxt,
10223 xmlSchemaPtr schema, xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +000010224{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010225 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000010226 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010227 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000010228
10229 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010230 return (-1);
10231 /* Not a component, don't create it. */
10232 type = ctxt->ctxtType;
10233 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
10234 /*
10235 * Check for illegal attributes.
10236 */
10237 attr = node->properties;
10238 while (attr != NULL) {
10239 if (attr->ns == NULL) {
10240 if ((!xmlStrEqual(attr->name, BAD_CAST "id"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010241 xmlSchemaPIllegalAttrErr(ctxt,
10242 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
10243 NULL, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010244 }
10245 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010246 xmlSchemaPIllegalAttrErr(ctxt,
10247 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
10248 NULL, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010249 }
10250 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010251 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000010252
10253 xmlSchemaPValAttrID(ctxt, NULL, NULL, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +000010254
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010255 /*
10256 * And now for the children...
10257 */
Daniel Veillard4255d502002-04-16 15:50:10 +000010258 child = node->children;
10259 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010260 /*
10261 * Add the annotation to the complex type ancestor.
10262 */
10263 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
10264 xmlSchemaParseAnnotation(ctxt, schema, child));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010265 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000010266 }
Daniel Veillard4255d502002-04-16 15:50:10 +000010267 if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010268 xmlSchemaParseRestriction(ctxt, schema, child,
10269 XML_SCHEMA_TYPE_SIMPLE_CONTENT);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010270 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000010271 } else if (IS_SCHEMA(child, "extension")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010272 xmlSchemaParseExtension(ctxt, schema, child,
10273 XML_SCHEMA_TYPE_SIMPLE_CONTENT);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010274 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000010275 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010276 if (child != NULL) {
10277 xmlSchemaPContentErr(ctxt,
10278 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010279 NULL, NULL, node, child, NULL,
10280 "(annotation?, (restriction | extension))");
Daniel Veillard4255d502002-04-16 15:50:10 +000010281 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010282 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000010283}
10284
10285/**
10286 * xmlSchemaParseComplexContent:
10287 * @ctxt: a schema validation context
10288 * @schema: the schema being built
10289 * @node: a subtree containing XML Schema informations
10290 *
10291 * parse a XML schema ComplexContent definition
10292 * *WARNING* this interface is highly subject to change
10293 *
10294 * Returns the type definition or NULL in case of error
10295 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010296static int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010297xmlSchemaParseComplexContent(xmlSchemaParserCtxtPtr ctxt,
10298 xmlSchemaPtr schema, xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +000010299{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010300 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000010301 xmlNodePtr child = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010302 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000010303
10304 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010305 return (-1);
10306 /* Not a component, don't create it. */
10307 type = ctxt->ctxtType;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010308 /*
10309 * Check for illegal attributes.
10310 */
10311 attr = node->properties;
10312 while (attr != NULL) {
10313 if (attr->ns == NULL) {
10314 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010315 (!xmlStrEqual(attr->name, BAD_CAST "mixed")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010316 {
10317 xmlSchemaPIllegalAttrErr(ctxt,
10318 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
10319 NULL, NULL, attr);
10320 }
10321 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
10322 xmlSchemaPIllegalAttrErr(ctxt,
10323 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
10324 NULL, NULL, attr);
10325 }
10326 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010327 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000010328
10329 xmlSchemaPValAttrID(ctxt, NULL, NULL, node, BAD_CAST "id");
10330
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010331 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010332 * Set the 'mixed' on the complex type ancestor.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010333 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010334 if (xmlGetBooleanProp(ctxt, NULL, NULL, node, "mixed", 0)) {
10335 if ((type->flags & XML_SCHEMAS_TYPE_MIXED) == 0)
10336 type->flags |= XML_SCHEMAS_TYPE_MIXED;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010337 }
Daniel Veillard4255d502002-04-16 15:50:10 +000010338 child = node->children;
10339 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010340 /*
10341 * Add the annotation to the complex type ancestor.
10342 */
10343 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
10344 xmlSchemaParseAnnotation(ctxt, schema, child));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010345 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000010346 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010347 if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010348 xmlSchemaParseRestriction(ctxt, schema, child,
10349 XML_SCHEMA_TYPE_COMPLEX_CONTENT);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010350 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000010351 } else if (IS_SCHEMA(child, "extension")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010352 xmlSchemaParseExtension(ctxt, schema, child,
10353 XML_SCHEMA_TYPE_COMPLEX_CONTENT);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010354 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000010355 }
Daniel Veillard4255d502002-04-16 15:50:10 +000010356 if (child != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010357 xmlSchemaPContentErr(ctxt,
10358 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
10359 NULL, NULL, node, child,
10360 NULL, "(annotation?, (restriction | extension))");
Daniel Veillard4255d502002-04-16 15:50:10 +000010361 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010362 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000010363}
10364
10365/**
10366 * xmlSchemaParseComplexType:
10367 * @ctxt: a schema validation context
10368 * @schema: the schema being built
10369 * @node: a subtree containing XML Schema informations
10370 *
10371 * parse a XML schema Complex Type definition
10372 * *WARNING* this interface is highly subject to change
10373 *
10374 * Returns the type definition or NULL in case of error
10375 */
10376static xmlSchemaTypePtr
10377xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +000010378 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +000010379{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010380 xmlSchemaTypePtr type, ctxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +000010381 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010382 const xmlChar *oldcontainer, *name = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010383 xmlAttrPtr attr;
10384 const xmlChar *attrValue;
10385 xmlChar *des = NULL; /* The reported designation. */
Daniel Veillard1a380b82004-10-21 16:00:06 +000010386 char buf[40];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010387 int final = 0, block = 0;
Daniel Veillard1a380b82004-10-21 16:00:06 +000010388
Daniel Veillard4255d502002-04-16 15:50:10 +000010389
10390 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
10391 return (NULL);
10392
Daniel Veillard01fa6152004-06-29 17:04:39 +000010393 ctxtType = ctxt->ctxtType;
10394
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010395 if (topLevel) {
10396 attr = xmlSchemaGetPropNode(node, "name");
10397 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010398 xmlSchemaPMissingAttrErr(ctxt,
10399 XML_SCHEMAP_S4S_ATTR_MISSING, NULL, node, "name", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010400 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010401 } else if (xmlSchemaPValAttrNode(ctxt,
10402 (xmlChar **) &xmlSchemaElemDesCT, NULL, attr,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010403 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
10404 return (NULL);
10405 }
10406 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010407
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010408 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010409 /*
10410 * Parse as local complex type definition.
10411 */
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +000010412 snprintf(buf, 39, "#CT%d", ctxt->counter++ + 1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010413 type = xmlSchemaAddType(ctxt, schema, (const xmlChar *)buf, NULL, node);
10414 if (type == NULL)
10415 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010416 name = type->name;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010417 type->node = node;
10418 type->type = XML_SCHEMA_TYPE_COMPLEX;
10419 /*
10420 * TODO: We need the target namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010421 */
10422 } else {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010423 /*
10424 * Parse as global complex type definition.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010425 */
William M. Brack2f2a6632004-08-20 23:09:47 +000010426 type = xmlSchemaAddType(ctxt, schema, name, schema->targetNamespace, node);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010427 if (type == NULL)
10428 return (NULL);
10429 type->node = node;
10430 type->type = XML_SCHEMA_TYPE_COMPLEX;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010431 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +000010432 }
10433 type->targetNamespace = schema->targetNamespace;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010434 /*
10435 * Handle attributes.
10436 */
10437 attr = node->properties;
10438 while (attr != NULL) {
10439 if (attr->ns == NULL) {
10440 if (xmlStrEqual(attr->name, BAD_CAST "id")) {
10441 /*
10442 * Attribute "id".
10443 */
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000010444 xmlSchemaPValAttrID(ctxt, NULL, type, node,
10445 BAD_CAST "id");
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010446 } else if (xmlStrEqual(attr->name, BAD_CAST "mixed")) {
10447 /*
10448 * Attribute "mixed".
10449 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010450 if (xmlSchemaPGetBoolNodeValue(ctxt, &des, type,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010451 (xmlNodePtr) attr))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010452 type->flags |= XML_SCHEMAS_TYPE_MIXED;
10453 } else if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010454 /*
10455 * Attributes of global complex type definitions.
10456 */
10457 if (xmlStrEqual(attr->name, BAD_CAST "name")) {
10458 /* Pass. */
10459 } else if (xmlStrEqual(attr->name, BAD_CAST "abstract")) {
10460 /*
10461 * Attribute "abstract".
10462 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010463 if (xmlSchemaPGetBoolNodeValue(ctxt, &des, type,
10464 (xmlNodePtr) attr))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010465 type->flags |= XML_SCHEMAS_TYPE_ABSTRACT;
10466 } else if (xmlStrEqual(attr->name, BAD_CAST "final")) {
10467 /*
10468 * Attribute "final".
10469 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010470 attrValue = xmlSchemaGetNodeContent(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010471 (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010472 if (xmlSchemaPValAttrBlockFinal(attrValue,
10473 &(type->flags),
10474 -1,
10475 XML_SCHEMAS_TYPE_FINAL_EXTENSION,
10476 XML_SCHEMAS_TYPE_FINAL_RESTRICTION,
10477 -1, -1, -1) != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010478 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010479 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010480 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010481 type, (xmlNodePtr) attr, NULL,
10482 "(#all | List of (extension | restriction))",
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010483 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010484 } else
10485 final = 1;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010486 } else if (xmlStrEqual(attr->name, BAD_CAST "block")) {
10487 /*
10488 * Attribute "block".
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010489 */
10490 attrValue = xmlSchemaGetNodeContent(ctxt,
10491 (xmlNodePtr) attr);
10492 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010493 -1,
10494 XML_SCHEMAS_TYPE_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010495 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010496 -1, -1, -1) != 0) {
10497 xmlSchemaPSimpleTypeErr(ctxt,
10498 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010499 type, (xmlNodePtr) attr, NULL,
10500 "(#all | List of (extension | restriction)) ",
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010501 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010502 } else
10503 block = 1;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010504 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010505 xmlSchemaPIllegalAttrErr(ctxt,
10506 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010507 &des, type, attr);
10508 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010509 } else {
10510 xmlSchemaPIllegalAttrErr(ctxt,
10511 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010512 &des, type, attr);
10513 }
10514 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010515 xmlSchemaPIllegalAttrErr(ctxt,
10516 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED,
10517 &des, type, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010518 }
10519 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010520 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010521 if (! block) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000010522 /*
10523 * Apply default "block" values.
10524 */
10525 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
10526 type->flags |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
10527 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
10528 type->flags |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
10529 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010530 if (! final) {
10531 /*
10532 * Apply default "block" values.
10533 */
10534 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
10535 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
10536 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
10537 type->flags |= XML_SCHEMAS_TYPE_FINAL_EXTENSION;
10538 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010539 /*
10540 * And now for the children...
10541 */
10542 oldcontainer = ctxt->container;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010543 ctxt->container = name;
Daniel Veillard4255d502002-04-16 15:50:10 +000010544 child = node->children;
10545 if (IS_SCHEMA(child, "annotation")) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010546 type->annot = xmlSchemaParseAnnotation(ctxt, schema, child);
10547 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000010548 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000010549 ctxt->ctxtType = type;
Daniel Veillard4255d502002-04-16 15:50:10 +000010550 if (IS_SCHEMA(child, "simpleContent")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010551 /*
10552 * 3.4.3 : 2.2
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010553 * Specifying mixed='true' when the <simpleContent>
10554 * alternative is chosen has no effect
10555 */
William M. Bracke7091952004-05-11 15:09:58 +000010556 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
10557 type->flags ^= XML_SCHEMAS_TYPE_MIXED;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010558 xmlSchemaParseSimpleContent(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010559 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000010560 } else if (IS_SCHEMA(child, "complexContent")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010561 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
10562 xmlSchemaParseComplexContent(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010563 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000010564 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010565 /*
10566 * SPEC
10567 * "...the third alternative (neither <simpleContent> nor
10568 * <complexContent>) is chosen. This case is understood as shorthand
10569 * for complex content restricting the ·ur-type definition·, and the
10570 * details of the mappings should be modified as necessary.
10571 */
10572 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
10573 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010574 /*
10575 * Parse model groups.
10576 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010577 if (IS_SCHEMA(child, "all")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010578 type->subtypes = (xmlSchemaTypePtr)
10579 xmlSchemaParseModelGroup(ctxt, schema, child,
10580 XML_SCHEMA_TYPE_ALL, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010581 child = child->next;
10582 } else if (IS_SCHEMA(child, "choice")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010583 type->subtypes = (xmlSchemaTypePtr)
10584 xmlSchemaParseModelGroup(ctxt, schema, child,
10585 XML_SCHEMA_TYPE_CHOICE, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010586 child = child->next;
10587 } else if (IS_SCHEMA(child, "sequence")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010588 type->subtypes = (xmlSchemaTypePtr)
10589 xmlSchemaParseModelGroup(ctxt, schema, child,
10590 XML_SCHEMA_TYPE_SEQUENCE, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010591 child = child->next;
10592 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010593 type->subtypes = (xmlSchemaTypePtr)
10594 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010595 child = child->next;
10596 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010597 /*
10598 * Parse attribute decls/refs.
10599 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010600 child = xmlSchemaParseAttrDecls(ctxt, schema, child, type);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010601 /*
10602 * Parse attribute wildcard.
10603 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010604 if (IS_SCHEMA(child, "anyAttribute")) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010605 type->attributeWildcard = xmlSchemaParseAnyAttribute(ctxt, schema, child);
10606 child = child->next;
10607 }
Daniel Veillard4255d502002-04-16 15:50:10 +000010608 }
10609 if (child != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010610 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010611 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010612 &des, type, node, child,
10613 NULL, "(annotation?, (simpleContent | complexContent | "
10614 "((group | all | choice | sequence)?, ((attribute | "
10615 "attributeGroup)*, anyAttribute?))))");
Daniel Veillard4255d502002-04-16 15:50:10 +000010616 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010617 FREE_AND_NULL(des);
Daniel Veillard4255d502002-04-16 15:50:10 +000010618 ctxt->container = oldcontainer;
Daniel Veillard01fa6152004-06-29 17:04:39 +000010619 ctxt->ctxtType = ctxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +000010620 return (type);
10621}
10622
Daniel Veillard4255d502002-04-16 15:50:10 +000010623/**
10624 * xmlSchemaParseSchema:
10625 * @ctxt: a schema validation context
10626 * @node: a subtree containing XML Schema informations
10627 *
10628 * parse a XML schema definition from a node set
10629 * *WARNING* this interface is highly subject to change
10630 *
10631 * Returns the internal XML Schema structure built from the resource or
10632 * NULL in case of error
10633 */
10634static xmlSchemaPtr
10635xmlSchemaParseSchema(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
10636{
10637 xmlSchemaPtr schema = NULL;
Daniel Veillardbe9c6322003-11-22 20:37:51 +000010638 const xmlChar *val;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000010639 int nberrors;
Daniel Veillardc0826a72004-08-10 14:17:33 +000010640 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000010641
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010642 /*
10643 * This one is called by xmlSchemaParse only and is used if
10644 * the schema to be parsed was specified via the API; i.e. not
10645 * automatically by the validated instance document.
10646 */
Daniel Veillard4255d502002-04-16 15:50:10 +000010647 if ((ctxt == NULL) || (node == NULL))
10648 return (NULL);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000010649 nberrors = ctxt->nberrors;
10650 ctxt->nberrors = 0;
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000010651 ctxt->isS4S = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000010652 if (IS_SCHEMA(node, "schema")) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010653 xmlSchemaImportPtr import;
10654
Daniel Veillard4255d502002-04-16 15:50:10 +000010655 schema = xmlSchemaNewSchema(ctxt);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010656 if (schema == NULL)
10657 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010658 attr = xmlSchemaGetPropNode(node, "targetNamespace");
Daniel Veillardc0826a72004-08-10 14:17:33 +000010659 if (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010660 xmlSchemaPValAttrNode(ctxt, NULL, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +000010661 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), &val);
10662 /*
10663 * TODO: Should we proceed with an invalid target namespace?
10664 */
Daniel Veillardbe9c6322003-11-22 20:37:51 +000010665 schema->targetNamespace = xmlDictLookup(ctxt->dict, val, -1);
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000010666 if (xmlStrEqual(schema->targetNamespace, xmlSchemaNs)) {
10667 /*
10668 * We are parsing the schema for schema!
10669 */
10670 ctxt->isS4S = 1;
10671 }
Daniel Veillardbe9c6322003-11-22 20:37:51 +000010672 } else {
10673 schema->targetNamespace = NULL;
10674 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010675 /*
10676 * Add the current ns name and location to the import table;
10677 * this is needed to have a consistent mechanism, regardless
10678 * if all schemata are constructed dynamically fired by the
10679 * instance or if the schema to be used was specified via
10680 * the API.
10681 */
10682 import = xmlSchemaAddImport(ctxt, &(schema->schemasImports),
10683 schema->targetNamespace);
10684 if (import == NULL) {
10685 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_FAILED_BUILD_IMPORT,
10686 NULL, NULL, (xmlNodePtr) ctxt->doc,
10687 "Internal error: xmlSchemaParseSchema, "
10688 "failed to add an import entry", NULL);
10689 xmlSchemaFree(schema);
10690 schema = NULL;
10691 return (NULL);
10692 }
10693 import->schemaLocation = ctxt->URL;
10694 /*
10695 * NOTE: We won't set the doc here, otherwise it will be freed
10696 * if the import struct is freed.
10697 * import->doc = ctxt->doc;
10698 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010699 xmlSchemaParseSchemaDefaults(ctxt, schema, node);
Daniel Veillardbd2904b2003-11-25 15:38:59 +000010700 xmlSchemaParseSchemaTopLevel(ctxt, schema, node->children);
10701 } else {
10702 xmlDocPtr doc;
10703
10704 doc = node->doc;
10705
10706 if ((doc != NULL) && (doc->URL != NULL)) {
10707 xmlSchemaPErr(ctxt, (xmlNodePtr) doc,
10708 XML_SCHEMAP_NOT_SCHEMA,
Daniel Veillardc0826a72004-08-10 14:17:33 +000010709 "The file \"%s\" is not a XML schema.\n", doc->URL, NULL);
Daniel Veillardbd2904b2003-11-25 15:38:59 +000010710 } else {
10711 xmlSchemaPErr(ctxt, (xmlNodePtr) doc,
10712 XML_SCHEMAP_NOT_SCHEMA,
Daniel Veillardc0826a72004-08-10 14:17:33 +000010713 "The file is not a XML schema.\n", NULL, NULL);
Daniel Veillardbd2904b2003-11-25 15:38:59 +000010714 }
10715 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000010716 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000010717 if (ctxt->nberrors != 0) {
10718 if (schema != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010719 xmlSchemaFree(schema);
10720 schema = NULL;
10721 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000010722 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010723 if (schema != NULL)
10724 schema->counter = ctxt->counter;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000010725 ctxt->nberrors = nberrors;
Daniel Veillard4255d502002-04-16 15:50:10 +000010726#ifdef DEBUG
10727 if (schema == NULL)
10728 xmlGenericError(xmlGenericErrorContext,
10729 "xmlSchemaParse() failed\n");
10730#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000010731 return (schema);
10732}
10733
10734/************************************************************************
10735 * *
10736 * Validating using Schemas *
10737 * *
10738 ************************************************************************/
10739
10740/************************************************************************
10741 * *
10742 * Reading/Writing Schemas *
10743 * *
10744 ************************************************************************/
10745
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010746#if 0 /* Will be enabled if it is clear what options are needed. */
10747/**
10748 * xmlSchemaParserCtxtSetOptions:
10749 * @ctxt: a schema parser context
10750 * @options: a combination of xmlSchemaParserOption
10751 *
10752 * Sets the options to be used during the parse.
10753 *
10754 * Returns 0 in case of success, -1 in case of an
10755 * API error.
10756 */
10757static int
10758xmlSchemaParserCtxtSetOptions(xmlSchemaParserCtxtPtr ctxt,
10759 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010760
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010761{
10762 int i;
10763
10764 if (ctxt == NULL)
10765 return (-1);
10766 /*
10767 * WARNING: Change the start value if adding to the
10768 * xmlSchemaParseOption.
10769 */
10770 for (i = 1; i < (int) sizeof(int) * 8; i++) {
10771 if (options & 1<<i) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010772 return (-1);
10773 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010774 }
10775 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010776 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010777}
10778
10779/**
10780 * xmlSchemaValidCtxtGetOptions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010781 * @ctxt: a schema parser context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010782 *
10783 * Returns the option combination of the parser context.
10784 */
10785static int
10786xmlSchemaParserCtxtGetOptions(xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010787
10788{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010789 if (ctxt == NULL)
10790 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010791 else
10792 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010793}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010794#endif
10795
Daniel Veillard4255d502002-04-16 15:50:10 +000010796/**
10797 * xmlSchemaNewParserCtxt:
10798 * @URL: the location of the schema
10799 *
10800 * Create an XML Schemas parse context for that file/resource expected
10801 * to contain an XML Schemas file.
10802 *
10803 * Returns the parser context or NULL in case of error
10804 */
10805xmlSchemaParserCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010806xmlSchemaNewParserCtxt(const char *URL)
10807{
Daniel Veillard4255d502002-04-16 15:50:10 +000010808 xmlSchemaParserCtxtPtr ret;
10809
10810 if (URL == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010811 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000010812
10813 ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt));
10814 if (ret == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000010815 xmlSchemaPErrMemory(NULL, "allocating schema parser context",
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010816 NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000010817 return (NULL);
10818 }
10819 memset(ret, 0, sizeof(xmlSchemaParserCtxt));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010820 ret->type = XML_SCHEMA_CTXT_PARSER;
Daniel Veillardbe9c6322003-11-22 20:37:51 +000010821 ret->dict = xmlDictCreate();
10822 ret->URL = xmlDictLookup(ret->dict, (const xmlChar *) URL, -1);
Daniel Veillardb0f397e2003-12-23 23:30:53 +000010823 ret->includes = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000010824 return (ret);
10825}
10826
10827/**
Daniel Veillard6045c902002-10-09 21:13:59 +000010828 * xmlSchemaNewMemParserCtxt:
10829 * @buffer: a pointer to a char array containing the schemas
10830 * @size: the size of the array
10831 *
10832 * Create an XML Schemas parse context for that memory buffer expected
10833 * to contain an XML Schemas file.
10834 *
10835 * Returns the parser context or NULL in case of error
10836 */
10837xmlSchemaParserCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010838xmlSchemaNewMemParserCtxt(const char *buffer, int size)
10839{
Daniel Veillard6045c902002-10-09 21:13:59 +000010840 xmlSchemaParserCtxtPtr ret;
10841
10842 if ((buffer == NULL) || (size <= 0))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010843 return (NULL);
Daniel Veillard6045c902002-10-09 21:13:59 +000010844
10845 ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt));
10846 if (ret == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000010847 xmlSchemaPErrMemory(NULL, "allocating schema parser context",
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010848 NULL);
Daniel Veillard6045c902002-10-09 21:13:59 +000010849 return (NULL);
10850 }
10851 memset(ret, 0, sizeof(xmlSchemaParserCtxt));
10852 ret->buffer = buffer;
10853 ret->size = size;
William M. Brackcf9eadf2003-12-25 13:24:05 +000010854 ret->dict = xmlDictCreate();
Daniel Veillard6045c902002-10-09 21:13:59 +000010855 return (ret);
10856}
10857
10858/**
Daniel Veillard9d751502003-10-29 13:21:47 +000010859 * xmlSchemaNewDocParserCtxt:
10860 * @doc: a preparsed document tree
10861 *
10862 * Create an XML Schemas parse context for that document.
10863 * NB. The document may be modified during the parsing process.
10864 *
10865 * Returns the parser context or NULL in case of error
10866 */
10867xmlSchemaParserCtxtPtr
10868xmlSchemaNewDocParserCtxt(xmlDocPtr doc)
10869{
10870 xmlSchemaParserCtxtPtr ret;
10871
10872 if (doc == NULL)
10873 return (NULL);
10874
10875 ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt));
10876 if (ret == NULL) {
10877 xmlSchemaPErrMemory(NULL, "allocating schema parser context",
10878 NULL);
10879 return (NULL);
10880 }
10881 memset(ret, 0, sizeof(xmlSchemaParserCtxt));
10882 ret->doc = doc;
William M. Brackcf9eadf2003-12-25 13:24:05 +000010883 ret->dict = xmlDictCreate();
Daniel Veillarddda22c12004-01-24 08:31:30 +000010884 /* The application has responsibility for the document */
10885 ret->preserve = 1;
Daniel Veillard9d751502003-10-29 13:21:47 +000010886
10887 return (ret);
10888}
10889
10890/**
Daniel Veillard4255d502002-04-16 15:50:10 +000010891 * xmlSchemaFreeParserCtxt:
10892 * @ctxt: the schema parser context
10893 *
10894 * Free the resources associated to the schema parser context
10895 */
10896void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010897xmlSchemaFreeParserCtxt(xmlSchemaParserCtxtPtr ctxt)
10898{
Daniel Veillard4255d502002-04-16 15:50:10 +000010899 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010900 return;
Daniel Veillarddda22c12004-01-24 08:31:30 +000010901 if (ctxt->doc != NULL && !ctxt->preserve)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010902 xmlFreeDoc(ctxt->doc);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010903 if (ctxt->assemble != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010904 xmlFree((xmlSchemaTypePtr *) ctxt->assemble->items);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010905 xmlFree(ctxt->assemble);
10906 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000010907 if (ctxt->vctxt != NULL) {
10908 xmlSchemaFreeValidCtxt(ctxt->vctxt);
10909 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010910 if (ctxt->localImports != NULL)
10911 xmlFree((xmlChar *) ctxt->localImports);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000010912 if (ctxt->substGroups != NULL)
10913 xmlHashFree(ctxt->substGroups,
10914 (xmlHashDeallocator) xmlSchemaFreeSubstGroup);
Daniel Veillardbe9c6322003-11-22 20:37:51 +000010915 xmlDictFree(ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +000010916 xmlFree(ctxt);
10917}
10918
10919/************************************************************************
10920 * *
10921 * Building the content models *
10922 * *
10923 ************************************************************************/
Daniel Veillardd0c9c322003-10-10 00:49:42 +000010924
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000010925static void
10926xmlSchemaBuildContentModelForSubstGroup(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillarda980bef2005-07-18 21:34:03 +000010927 xmlSchemaParticlePtr particle, int counter, xmlAutomataStatePtr end)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000010928{
Daniel Veillarda980bef2005-07-18 21:34:03 +000010929 xmlAutomataStatePtr start, tmp;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000010930 xmlSchemaElementPtr elemDecl, member;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000010931 xmlSchemaSubstGroupPtr substGroup;
10932 int i;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000010933
10934 elemDecl = (xmlSchemaElementPtr) particle->children;
10935 /*
10936 * Wrap the substitution group with a CHOICE.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010937 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000010938 start = pctxt->state;
Daniel Veillarda980bef2005-07-18 21:34:03 +000010939 if (end == NULL)
10940 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000010941 substGroup = xmlSchemaGetElementSubstitutionGroup(pctxt, elemDecl);
10942 if (substGroup == NULL) {
10943 xmlSchemaPErr(pctxt, GET_NODE(particle),
10944 XML_SCHEMAP_INTERNAL,
10945 "Internal error: xmlSchemaBuildContentModelForSubstGroup, "
10946 "declaration is marked having a subst. group but none "
10947 "available.\n", elemDecl->name, NULL);
10948 return;
10949 }
Daniel Veillarda980bef2005-07-18 21:34:03 +000010950 if (counter >= 0) {
10951 /*
10952 * NOTE that we put the declaration in, even if it's abstract,
10953 */
10954 tmp = xmlAutomataNewCountedTrans(pctxt->am, start, NULL, counter);
10955 xmlAutomataNewTransition2(pctxt->am, tmp, end,
10956 elemDecl->name, elemDecl->targetNamespace, elemDecl);
10957 /*
10958 * Add subst. group members.
10959 */
10960 for (i = 0; i < substGroup->members->nbItems; i++) {
10961 member = (xmlSchemaElementPtr) substGroup->members->items[i];
10962 xmlAutomataNewTransition2(pctxt->am, tmp, end,
10963 member->name, member->targetNamespace, member);
10964 }
10965 } else if (particle->maxOccurs == 1) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000010966 /*
10967 * NOTE that we put the declaration in, even if it's abstract,
10968 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010969 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000010970 xmlAutomataNewTransition2(pctxt->am,
10971 start, NULL,
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000010972 elemDecl->name, elemDecl->targetNamespace, elemDecl), end);
10973 /*
10974 * Add subst. group members.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010975 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000010976 for (i = 0; i < substGroup->members->nbItems; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010977 member = (xmlSchemaElementPtr) substGroup->members->items[i];
Daniel Veillarda980bef2005-07-18 21:34:03 +000010978 tmp = xmlAutomataNewOnceTrans2(pctxt->am, start, NULL,
10979 member->name, member->targetNamespace,
10980 1, 1, member);
10981 xmlAutomataNewEpsilon(pctxt->am, tmp, end);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000010982 }
10983 } else {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000010984 xmlAutomataStatePtr hop;
10985 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
10986 UNBOUNDED : particle->maxOccurs - 1;
10987 int minOccurs = particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
10988
10989 counter =
10990 xmlAutomataNewCounter(pctxt->am, minOccurs,
10991 maxOccurs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010992 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000010993
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010994 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000010995 xmlAutomataNewTransition2(pctxt->am,
10996 start, NULL,
10997 elemDecl->name, elemDecl->targetNamespace, elemDecl),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000010998 hop);
10999 /*
Daniel Veillarda980bef2005-07-18 21:34:03 +000011000 * Add subst. group members.
11001 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000011002 for (i = 0; i < substGroup->members->nbItems; i++) {
11003 member = (xmlSchemaElementPtr) substGroup->members->items[i];
11004 xmlAutomataNewEpsilon(pctxt->am,
11005 xmlAutomataNewTransition2(pctxt->am,
11006 start, NULL,
11007 member->name, member->targetNamespace, member),
11008 hop);
11009 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011010 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
11011 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
11012 }
11013 if (particle->minOccurs == 0)
11014 xmlAutomataNewEpsilon(pctxt->am, start, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011015 pctxt->state = end;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011016}
11017
11018static void
11019xmlSchemaBuildContentModelForElement(xmlSchemaParserCtxtPtr ctxt,
11020 xmlSchemaParticlePtr particle)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011021{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000011022 if (((xmlSchemaElementPtr) particle->children)->flags &
11023 XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011024 /*
11025 * Substitution groups.
11026 */
Daniel Veillarda980bef2005-07-18 21:34:03 +000011027 xmlSchemaBuildContentModelForSubstGroup(ctxt, particle, -1, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011028 } else {
11029 xmlSchemaElementPtr elemDecl;
11030 xmlAutomataStatePtr start;
11031
11032 elemDecl = (xmlSchemaElementPtr) particle->children;
11033
11034 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011035 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011036 if (particle->maxOccurs == 1) {
11037 start = ctxt->state;
11038 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
Daniel Veillarda980bef2005-07-18 21:34:03 +000011039 elemDecl->name, elemDecl->targetNamespace, elemDecl);
11040 } else if ((particle->maxOccurs >= UNBOUNDED) &&
11041 (particle->minOccurs < 2)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011042 /* Special case. */
11043 start = ctxt->state;
11044 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
11045 elemDecl->name, elemDecl->targetNamespace, elemDecl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011046 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, start);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011047 } else {
11048 int counter;
11049 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
11050 UNBOUNDED : particle->maxOccurs - 1;
11051 int minOccurs = particle->minOccurs < 1 ?
11052 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011053
11054 start = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011055 counter = xmlAutomataNewCounter(ctxt->am, minOccurs, maxOccurs);
11056 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
11057 elemDecl->name, elemDecl->targetNamespace, elemDecl);
11058 xmlAutomataNewCountedTrans(ctxt->am, ctxt->state, start, counter);
11059 ctxt->state = xmlAutomataNewCounterTrans(ctxt->am, ctxt->state,
11060 NULL, counter);
11061 }
11062 if (particle->minOccurs == 0)
11063 xmlAutomataNewEpsilon(ctxt->am, start, ctxt->state);
11064 }
11065}
11066
Daniel Veillard4255d502002-04-16 15:50:10 +000011067/**
11068 * xmlSchemaBuildAContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000011069 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011070 * @particle: the particle component
11071 * @name: the complex type's name whose content is being built
Daniel Veillard4255d502002-04-16 15:50:10 +000011072 *
11073 * Generate the automata sequence needed for that type
11074 */
11075static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011076xmlSchemaBuildAContentModel(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011077 xmlSchemaParticlePtr particle,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011078 const xmlChar * name)
11079{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011080 if (particle == NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011081 PERROR_INT("xmlSchemaBuildAContentModel", "particle is NULL");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011082 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000011083 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011084 if (particle->children == NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011085 PERROR_INT("xmlSchemaBuildAContentModel", "no term on particle");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011086 return;
11087 }
11088
11089 switch (particle->children->type) {
11090 case XML_SCHEMA_TYPE_ANY: {
Daniel Veillardc0826a72004-08-10 14:17:33 +000011091 xmlAutomataStatePtr start, end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011092 xmlSchemaWildcardPtr wild;
Daniel Veillardc0826a72004-08-10 14:17:33 +000011093 xmlSchemaWildcardNsPtr ns;
Daniel Veillard32370232002-10-16 14:08:14 +000011094
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011095 wild = (xmlSchemaWildcardPtr) particle->children;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011096
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011097 start = pctxt->state;
11098 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011099
11100 if (particle->maxOccurs == 1) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000011101 if (wild->any == 1) {
11102 /*
11103 * We need to add both transitions:
11104 *
11105 * 1. the {"*", "*"} for elements in a namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011106 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011107 pctxt->state =
11108 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011109 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011110 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000011111 /*
11112 * 2. the {"*"} for elements in no namespace.
11113 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011114 pctxt->state =
11115 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011116 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011117 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000011118
11119 } else if (wild->nsSet != NULL) {
11120 ns = wild->nsSet;
11121 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011122 pctxt->state = start;
11123 pctxt->state = xmlAutomataNewTransition2(pctxt->am,
11124 pctxt->state, NULL, BAD_CAST "*", ns->value, wild);
11125 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000011126 ns = ns->next;
11127 } while (ns != NULL);
11128
11129 } else if (wild->negNsSet != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011130
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +000011131 /*
11132 * Lead nodes with the negated namespace to the sink-state
11133 * {"*", "##other"}.
11134 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011135 pctxt->state = xmlAutomataNewTransition2(pctxt->am, start, NULL,
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +000011136 BAD_CAST "*", wild->negNsSet->value, wild);
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +000011137 /*
11138 * Open a door for nodes with any other namespace
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011139 * {"*", "*"}
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +000011140 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011141 pctxt->state = xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011142 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011143 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011144 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000011145 } else {
11146 int counter;
11147 xmlAutomataStatePtr hop;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011148 int maxOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011149 particle->maxOccurs == UNBOUNDED ? UNBOUNDED : particle->maxOccurs - 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +000011150 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011151 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011152
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011153 counter = xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
11154 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011155 if (wild->any == 1) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011156 pctxt->state =
11157 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011158 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011159 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
11160 pctxt->state =
11161 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011162 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011163 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011164 } else if (wild->nsSet != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000011165 ns = wild->nsSet;
11166 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011167 pctxt->state =
11168 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011169 start, NULL, BAD_CAST "*", ns->value, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011170 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Daniel Veillardc0826a72004-08-10 14:17:33 +000011171 ns = ns->next;
11172 } while (ns != NULL);
11173
11174 } else if (wild->negNsSet != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011175 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
Daniel Veillard9efc4762005-07-19 14:33:55 +000011176 start, hop, BAD_CAST "*", wild->negNsSet->value,
11177 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011178 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011179 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
11180 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardc0826a72004-08-10 14:17:33 +000011181 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011182 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011183 xmlAutomataNewEpsilon(pctxt->am, start, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011184 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011185 pctxt->state = end;
Daniel Veillardc0826a72004-08-10 14:17:33 +000011186 break;
11187 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011188 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011189 xmlSchemaBuildContentModelForElement(pctxt, particle);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011190 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011191 case XML_SCHEMA_TYPE_SEQUENCE:{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011192 xmlSchemaTreeItemPtr sub;
Daniel Veillardb39bc392002-10-26 19:29:51 +000011193
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011194 /*
11195 * If max and min occurances are default (1) then
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011196 * simply iterate over the particles of the <sequence>.
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011197 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011198 if ((particle->minOccurs == 1) && (particle->maxOccurs == 1)) {
11199 sub = particle->children->children;
11200 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011201 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011202 (xmlSchemaParticlePtr) sub, name);
11203 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011204 }
11205 } else {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011206 xmlAutomataStatePtr oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000011207
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011208 if (particle->maxOccurs >= UNBOUNDED) {
11209 if (particle->minOccurs > 1) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011210 xmlAutomataStatePtr tmp;
11211 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000011212
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011213 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011214 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011215 oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000011216
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011217 counter = xmlAutomataNewCounter(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011218 particle->minOccurs - 1, UNBOUNDED);
Daniel Veillardb39bc392002-10-26 19:29:51 +000011219
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011220 sub = particle->children->children;
11221 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011222 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011223 (xmlSchemaParticlePtr) sub, name);
11224 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011225 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011226 tmp = pctxt->state;
11227 xmlAutomataNewCountedTrans(pctxt->am, tmp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011228 oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011229 pctxt->state =
11230 xmlAutomataNewCounterTrans(pctxt->am, tmp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011231 NULL, counter);
Daniel Veillardb39bc392002-10-26 19:29:51 +000011232
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011233 } else {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011234 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Daniel Veillardafc05b62005-07-17 06:11:19 +000011235 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011236 oldstate = pctxt->state;
Daniel Veillardafc05b62005-07-17 06:11:19 +000011237
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011238 sub = particle->children->children;
11239 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011240 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011241 (xmlSchemaParticlePtr) sub, name);
11242 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011243 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011244 xmlAutomataNewEpsilon(pctxt->am, pctxt->state,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011245 oldstate);
Daniel Veillardafc05b62005-07-17 06:11:19 +000011246 /*
11247 * epsilon needed to block previous trans from
11248 * being allowed to enter back from another
11249 * construct
11250 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011251 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
11252 pctxt->state, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011253 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011254 xmlAutomataNewEpsilon(pctxt->am,
11255 oldstate, pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011256 }
11257 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011258 } else if ((particle->maxOccurs > 1)
11259 || (particle->minOccurs > 1)) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011260 xmlAutomataStatePtr tmp;
11261 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000011262
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011263 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011264 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011265 oldstate = pctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +000011266
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011267 counter = xmlAutomataNewCounter(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011268 particle->minOccurs - 1,
11269 particle->maxOccurs - 1);
Daniel Veillard4255d502002-04-16 15:50:10 +000011270
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011271 sub = particle->children->children;
11272 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011273 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011274 (xmlSchemaParticlePtr) sub, name);
11275 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011276 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011277 tmp = pctxt->state;
11278 xmlAutomataNewCountedTrans(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011279 tmp, oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011280 pctxt->state =
11281 xmlAutomataNewCounterTrans(pctxt->am, tmp, NULL,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011282 counter);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011283 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011284 xmlAutomataNewEpsilon(pctxt->am,
11285 oldstate, pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011286 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011287 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011288 sub = particle->children->children;
11289 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011290 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011291 (xmlSchemaParticlePtr) sub, name);
11292 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011293 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011294 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011295 xmlAutomataNewEpsilon(pctxt->am, oldstate,
11296 pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011297 }
11298 }
11299 }
11300 break;
11301 }
11302 case XML_SCHEMA_TYPE_CHOICE:{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011303 xmlSchemaTreeItemPtr sub;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011304 xmlAutomataStatePtr start, end;
Daniel Veillardb509f152002-04-17 16:28:10 +000011305
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011306 start = pctxt->state;
11307 end = xmlAutomataNewState(pctxt->am);
Daniel Veillard7646b182002-04-20 06:41:40 +000011308
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011309 /*
11310 * iterate over the subtypes and remerge the end with an
11311 * epsilon transition
11312 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011313 if (particle->maxOccurs == 1) {
11314 sub = particle->children->children;
11315 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011316 pctxt->state = start;
11317 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011318 (xmlSchemaParticlePtr) sub, name);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011319 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011320 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011321 }
11322 } else {
11323 int counter;
Daniel Veillardafc05b62005-07-17 06:11:19 +000011324 xmlAutomataStatePtr hop, base;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011325 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
11326 UNBOUNDED : particle->maxOccurs - 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011327 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011328 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Daniel Veillard7646b182002-04-20 06:41:40 +000011329
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011330 /*
11331 * use a counter to keep track of the number of transtions
11332 * which went through the choice.
11333 */
11334 counter =
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011335 xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
11336 hop = xmlAutomataNewState(pctxt->am);
11337 base = xmlAutomataNewState(pctxt->am);
Daniel Veillard6231e842002-04-18 11:54:04 +000011338
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011339 sub = particle->children->children;
11340 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011341 pctxt->state = base;
11342 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011343 (xmlSchemaParticlePtr) sub, name);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011344 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011345 sub = sub->next;
Daniel Veillarda980bef2005-07-18 21:34:03 +000011346 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011347 xmlAutomataNewEpsilon(pctxt->am, start, base);
11348 xmlAutomataNewCountedTrans(pctxt->am, hop, base, counter);
11349 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011350 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011351 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011352 xmlAutomataNewEpsilon(pctxt->am, start, end);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011353 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011354 pctxt->state = end;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011355 break;
11356 }
11357 case XML_SCHEMA_TYPE_ALL:{
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +000011358 xmlAutomataStatePtr start;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011359 xmlSchemaParticlePtr sub;
11360 xmlSchemaElementPtr elemDecl;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011361 int lax;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011362
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011363 sub = (xmlSchemaParticlePtr) particle->children->children;
11364 if (sub == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011365 break;
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011366 start = pctxt->state;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011367 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011368 pctxt->state = start;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011369
11370 elemDecl = (xmlSchemaElementPtr) sub->children;
11371 if (elemDecl == NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011372 PERROR_INT("xmlSchemaBuildAContentModel",
11373 "<element> particle has no term");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011374 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011375 };
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011376 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011377 * NOTE: The {max occurs} of all the particles in the
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +000011378 * {particles} of the group must be 0 or 1; this is
11379 * already ensured during the parse of the content of
11380 * <all>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011381 */
Daniel Veillarda980bef2005-07-18 21:34:03 +000011382 if (elemDecl->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
11383 int counter;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011384
Daniel Veillarda980bef2005-07-18 21:34:03 +000011385 /*
11386 * This is an abstract group, we need to share
11387 * the same counter for all the element transitions
11388 * derived from the group
11389 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011390 counter = xmlAutomataNewCounter(pctxt->am,
Daniel Veillarda980bef2005-07-18 21:34:03 +000011391 sub->minOccurs, sub->maxOccurs);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011392 xmlSchemaBuildContentModelForSubstGroup(pctxt,
11393 sub, counter, pctxt->state);
Daniel Veillarda980bef2005-07-18 21:34:03 +000011394 } else {
11395 if ((sub->minOccurs == 1) &&
11396 (sub->maxOccurs == 1)) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011397 xmlAutomataNewOnceTrans2(pctxt->am, pctxt->state,
11398 pctxt->state,
Daniel Veillarda980bef2005-07-18 21:34:03 +000011399 elemDecl->name,
11400 elemDecl->targetNamespace,
11401 1, 1, elemDecl);
11402 } else if ((sub->minOccurs == 0) &&
11403 (sub->maxOccurs == 1)) {
11404
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011405 xmlAutomataNewCountTrans2(pctxt->am, pctxt->state,
11406 pctxt->state,
Daniel Veillarda980bef2005-07-18 21:34:03 +000011407 elemDecl->name,
11408 elemDecl->targetNamespace,
11409 0,
11410 1,
11411 elemDecl);
11412 }
11413 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011414 sub = (xmlSchemaParticlePtr) sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011415 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011416 lax = particle->minOccurs == 0;
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011417 pctxt->state =
11418 xmlAutomataNewAllTrans(pctxt->am, pctxt->state, NULL, lax);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011419 break;
11420 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011421 case XML_SCHEMA_TYPE_GROUP:
11422 /*
11423 * If we hit a model group definition, then this means that
11424 * it was empty, thus was not substituted for the containing
11425 * model group. Just do nothing in this case.
11426 */
11427 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011428 default:
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011429 xmlSchemaPInternalErr(pctxt, "xmlSchemaBuildAContentModel",
11430 "found unexpected term of type '%s' in content model of complex "
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +000011431 "type '%s'",
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000011432 xmlSchemaCompTypeToString(particle->children->type), name);
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +000011433 xmlGenericError(xmlGenericErrorContext,
11434 "Unexpected type: %d\n", particle->children->type);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011435 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000011436 }
11437}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011438
Daniel Veillard4255d502002-04-16 15:50:10 +000011439/**
11440 * xmlSchemaBuildContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000011441 * @ctxt: the schema parser context
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011442 * @type: the complex type definition
Daniel Veillarda84c0b32003-06-02 16:58:46 +000011443 * @name: the element name
Daniel Veillard4255d502002-04-16 15:50:10 +000011444 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011445 * Builds the content model of the complex type.
Daniel Veillard4255d502002-04-16 15:50:10 +000011446 */
11447static void
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011448xmlSchemaBuildContentModel(xmlSchemaTypePtr type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011449 xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011450 const xmlChar * name)
11451{
Daniel Veillard4255d502002-04-16 15:50:10 +000011452 xmlAutomataStatePtr start;
11453
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011454 if ((type->type != XML_SCHEMA_TYPE_COMPLEX) ||
11455 (type->contModel != NULL) ||
11456 ((type->contentType != XML_SCHEMA_CONTENT_ELEMENTS) &&
11457 (type->contentType != XML_SCHEMA_CONTENT_MIXED)))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011458 return;
Daniel Veillarddecd64d2002-04-18 14:41:51 +000011459
11460#ifdef DEBUG_CONTENT
11461 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011462 "Building content model for %s\n", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000011463#endif
11464
Daniel Veillard4255d502002-04-16 15:50:10 +000011465 ctxt->am = xmlNewAutomata();
11466 if (ctxt->am == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011467 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011468 "Cannot create automata for complex type %s\n", name);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011469 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000011470 }
11471 start = ctxt->state = xmlAutomataGetInitState(ctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011472 xmlSchemaBuildAContentModel(ctxt, (xmlSchemaParticlePtr) type->subtypes, name);
Daniel Veillard4255d502002-04-16 15:50:10 +000011473 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011474 type->contModel = xmlAutomataCompile(ctxt->am);
11475 if (type->contModel == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011476 xmlSchemaPCustomErr(ctxt,
11477 XML_SCHEMAP_INTERNAL,
11478 NULL, type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000011479 "Failed to compile the content model", NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011480 } else if (xmlRegexpIsDeterminist(type->contModel) != 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011481 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000011482 XML_SCHEMAP_NOT_DETERMINISTIC,
11483 /* XML_SCHEMAS_ERR_NOTDETERMINIST, */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011484 NULL, type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000011485 "The content model is not determinist", NULL);
Daniel Veillarde19fc232002-04-22 16:01:24 +000011486 } else {
Daniel Veillard118aed72002-09-24 14:13:13 +000011487#ifdef DEBUG_CONTENT_REGEXP
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011488 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011489 "Content model of %s:\n", type->name);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011490 xmlRegexpPrint(stderr, type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +000011491#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +000011492 }
Daniel Veillarda84c0b32003-06-02 16:58:46 +000011493 ctxt->state = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000011494 xmlFreeAutomata(ctxt->am);
11495 ctxt->am = NULL;
11496}
11497
11498/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011499 * xmlSchemaElementFixup:
Daniel Veillard4255d502002-04-16 15:50:10 +000011500 * @elem: the schema element context
11501 * @ctxt: the schema parser context
11502 *
Daniel Veillardc0826a72004-08-10 14:17:33 +000011503 * Resolves the references of an element declaration
11504 * or particle, which has an element declaration as it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011505 * term.
Daniel Veillard4255d502002-04-16 15:50:10 +000011506 */
11507static void
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011508xmlSchemaElementFixup(xmlSchemaElementPtr elemDecl,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011509 xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000011510 const xmlChar * name ATTRIBUTE_UNUSED,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011511 const xmlChar * context ATTRIBUTE_UNUSED,
11512 const xmlChar * namespace ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +000011513{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011514 if ((ctxt == NULL) || (elemDecl == NULL) ||
11515 ((elemDecl != NULL) &&
11516 (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_RESOLVED)))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011517 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011518 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_RESOLVED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011519
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011520 if ((elemDecl->subtypes == NULL) && (elemDecl->namedType != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011521 xmlSchemaTypePtr type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011522
11523 /* (type definition) ... otherwise the type definition ·resolved·
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011524 * to by the ·actual value· of the type [attribute] ...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011525 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011526 type = xmlSchemaGetType(ctxt->schema, elemDecl->namedType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011527 elemDecl->namedTypeNs);
11528 if (type == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000011529 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000011530 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011531 (xmlSchemaTypePtr) elemDecl, elemDecl->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011532 "type", elemDecl->namedType, elemDecl->namedTypeNs,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011533 XML_SCHEMA_TYPE_BASIC, "type definition");
11534 } else
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011535 elemDecl->subtypes = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011536 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011537 if (elemDecl->substGroup != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011538 xmlSchemaElementPtr substHead;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011539
Daniel Veillardc0826a72004-08-10 14:17:33 +000011540 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011541 * FIXME TODO: Do we need a new field in _xmlSchemaElement for
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011542 * substitutionGroup?
Daniel Veillard01fa6152004-06-29 17:04:39 +000011543 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011544 substHead = xmlSchemaGetElem(ctxt->schema, elemDecl->substGroup,
11545 elemDecl->substGroupNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011546 if (substHead == NULL) {
11547 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000011548 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011549 (xmlSchemaTypePtr) elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011550 "substitutionGroup", elemDecl->substGroup,
11551 elemDecl->substGroupNs, XML_SCHEMA_TYPE_ELEMENT, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011552 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011553 xmlSchemaElementFixup(substHead, ctxt, NULL, NULL, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011554 /*
11555 * Set the "substitution group affiliation".
11556 * NOTE that now we use the "refDecl" field for this.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011557 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011558 elemDecl->refDecl = substHead;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011559 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011560 * (type definition)...otherwise the {type definition} of the
11561 * element declaration ·resolved· to by the ·actual value· of
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011562 * the substitutionGroup [attribute], if present
11563 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011564 if (elemDecl->subtypes == NULL)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011565 elemDecl->subtypes = substHead->subtypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011566 }
11567 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011568 if ((elemDecl->subtypes == NULL) && (elemDecl->namedType == NULL) &&
11569 (elemDecl->substGroup == NULL))
11570 elemDecl->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
William M. Bracke7091952004-05-11 15:09:58 +000011571}
11572
11573/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011574 * xmlSchemaResolveUnionMemberTypes:
William M. Bracke7091952004-05-11 15:09:58 +000011575 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011576 * @type: the schema simple type definition
William M. Bracke7091952004-05-11 15:09:58 +000011577 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011578 * Checks and builds the "member type definitions" property of the union
11579 * simple type. This handles part (1), part (2) is done in
11580 * xmlSchemaFinishMemberTypeDefinitionsProperty()
11581 *
Daniel Veillard01fa6152004-06-29 17:04:39 +000011582 * Returns -1 in case of an internal error, 0 otherwise.
William M. Bracke7091952004-05-11 15:09:58 +000011583 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000011584static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011585xmlSchemaResolveUnionMemberTypes(xmlSchemaParserCtxtPtr ctxt,
11586 xmlSchemaTypePtr type)
Daniel Veillard377e1a92004-04-16 16:30:05 +000011587{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011588
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011589 xmlSchemaTypeLinkPtr link, lastLink, newLink;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011590 xmlSchemaTypePtr memberType;
Daniel Veillard377e1a92004-04-16 16:30:05 +000011591
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011592 /*
11593 * SPEC (1) "If the <union> alternative is chosen, then [Definition:]
11594 * define the explicit members as the type definitions ·resolved·
11595 * to by the items in the ·actual value· of the memberTypes [attribute],
11596 * if any, followed by the type definitions corresponding to the
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011597 * <simpleType>s among the [children] of <union>, if any."
Daniel Veillard01fa6152004-06-29 17:04:39 +000011598 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011599 /*
11600 * Resolve references.
11601 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011602 link = type->memberTypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011603 lastLink = NULL;
11604 while (link != NULL) {
11605 const xmlChar *name, *nsName;
Daniel Veillardc0826a72004-08-10 14:17:33 +000011606
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011607 name = ((xmlSchemaQNameRefPtr) link->type)->name;
11608 nsName = ((xmlSchemaQNameRefPtr) link->type)->targetNamespace;
11609
11610 memberType = xmlSchemaGetType(ctxt->schema, name, nsName);
11611 if ((memberType == NULL) || (! IS_SIMPLE_TYPE(memberType))) {
11612 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011613 type, type->node, "memberTypes",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011614 name, nsName, XML_SCHEMA_TYPE_SIMPLE, NULL);
11615 /*
11616 * Remove the member type link.
11617 */
11618 if (lastLink == NULL)
11619 type->memberTypes = link->next;
11620 else
11621 lastLink->next = link->next;
11622 newLink = link;
11623 link = link->next;
11624 xmlFree(newLink);
11625 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011626 link->type = memberType;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011627 lastLink = link;
11628 link = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011629 }
11630 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000011631 /*
11632 * Add local simple types,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011633 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000011634 memberType = type->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011635 while (memberType != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000011636 link = (xmlSchemaTypeLinkPtr) xmlMalloc(sizeof(xmlSchemaTypeLink));
11637 if (link == NULL) {
11638 xmlSchemaPErrMemory(ctxt, "allocating a type link", NULL);
11639 return (-1);
11640 }
11641 link->type = memberType;
11642 link->next = NULL;
11643 if (lastLink == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011644 type->memberTypes = link;
11645 else
Daniel Veillard01fa6152004-06-29 17:04:39 +000011646 lastLink->next = link;
11647 lastLink = link;
11648 memberType = memberType->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011649 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000011650 return (0);
Daniel Veillard377e1a92004-04-16 16:30:05 +000011651}
11652
Daniel Veillard4255d502002-04-16 15:50:10 +000011653/**
Daniel Veillard3646d642004-06-02 19:19:14 +000011654 * xmlSchemaIsDerivedFromBuiltInType:
11655 * @ctxt: the schema parser context
11656 * @type: the type definition
11657 * @valType: the value type
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011658 *
Daniel Veillard3646d642004-06-02 19:19:14 +000011659 *
11660 * Returns 1 if the type has the given value type, or
11661 * is derived from such a type.
11662 */
William M. Brack803812b2004-06-03 02:11:24 +000011663static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011664xmlSchemaIsDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
Daniel Veillard3646d642004-06-02 19:19:14 +000011665{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011666 if (type == NULL)
11667 return (0);
11668 if (IS_COMPLEX_TYPE(type))
11669 return (0);
11670 if (type->type == XML_SCHEMA_TYPE_BASIC) {
11671 if (type->builtInType == valType)
11672 return(1);
11673 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
11674 (type->builtInType == XML_SCHEMAS_ANYTYPE))
11675 return (0);
11676 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
11677 } else
11678 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
Daniel Veillard3646d642004-06-02 19:19:14 +000011679
11680 return (0);
11681}
11682
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011683#if 0
11684/**
11685 * xmlSchemaIsDerivedFromBuiltInType:
11686 * @ctxt: the schema parser context
11687 * @type: the type definition
11688 * @valType: the value type
11689 *
11690 *
11691 * Returns 1 if the type has the given value type, or
11692 * is derived from such a type.
11693 */
11694static int
11695xmlSchemaIsUserDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
11696{
11697 if (type == NULL)
11698 return (0);
11699 if (IS_COMPLEX_TYPE(type))
11700 return (0);
11701 if (type->type == XML_SCHEMA_TYPE_BASIC) {
11702 if (type->builtInType == valType)
11703 return(1);
11704 return (0);
11705 } else
11706 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
11707
11708 return (0);
11709}
11710#endif
11711
11712static xmlSchemaTypePtr
11713xmlSchemaQueryBuiltInType(xmlSchemaTypePtr type)
11714{
11715 if (type == NULL)
11716 return (NULL);
11717 if (IS_COMPLEX_TYPE(type))
11718 return (NULL);
11719 if (type->type == XML_SCHEMA_TYPE_BASIC)
11720 return(type);
11721 else
11722 return(xmlSchemaQueryBuiltInType(type->subtypes));
11723
11724 return (NULL);
11725}
11726
Daniel Veillard3646d642004-06-02 19:19:14 +000011727/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000011728 * xmlSchemaGetPrimitiveType:
Daniel Veillard01fa6152004-06-29 17:04:39 +000011729 * @type: the simpleType definition
11730 *
11731 * Returns the primitive type of the given type or
11732 * NULL in case of error.
11733 */
11734static xmlSchemaTypePtr
11735xmlSchemaGetPrimitiveType(xmlSchemaTypePtr type)
11736{
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000011737
Daniel Veillard01fa6152004-06-29 17:04:39 +000011738 while (type != NULL) {
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000011739 /*
11740 * Note that anySimpleType is actually not a primitive type
11741 * but we need that here.
11742 */
11743 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
11744 (type->flags & XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE))
Daniel Veillard01fa6152004-06-29 17:04:39 +000011745 return (type);
11746 type = type->baseType;
11747 }
11748
11749 return (NULL);
11750}
11751
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000011752#if 0
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000011753/**
11754 * xmlSchemaGetBuiltInTypeAncestor:
11755 * @type: the simpleType definition
11756 *
11757 * Returns the primitive type of the given type or
11758 * NULL in case of error.
11759 */
11760static xmlSchemaTypePtr
11761xmlSchemaGetBuiltInTypeAncestor(xmlSchemaTypePtr type)
11762{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011763 if (VARIETY_LIST(type) || VARIETY_UNION(type))
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000011764 return (0);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000011765 while (type != NULL) {
11766 if (type->type == XML_SCHEMA_TYPE_BASIC)
11767 return (type);
11768 type = type->baseType;
11769 }
11770
11771 return (NULL);
11772}
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000011773#endif
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000011774
Daniel Veillard01fa6152004-06-29 17:04:39 +000011775/**
Daniel Veillard3646d642004-06-02 19:19:14 +000011776 * xmlSchemaBuildAttributeUsesOwned:
11777 * @ctxt: the schema parser context
11778 * @type: the complex type definition
11779 * @cur: the attribute declaration list
11780 * @lastUse: the top of the attribute use list
11781 *
11782 * Builds the attribute uses list on the given complex type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011783 * This one is supposed to be called by
Daniel Veillard3646d642004-06-02 19:19:14 +000011784 * xmlSchemaBuildAttributeValidation only.
11785 */
11786static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011787xmlSchemaBuildAttributeUsesOwned(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000011788 xmlSchemaAttributePtr cur,
11789 xmlSchemaAttributeLinkPtr *uses,
11790 xmlSchemaAttributeLinkPtr *lastUse)
11791{
11792 xmlSchemaAttributeLinkPtr tmp;
11793 while (cur != NULL) {
11794 if (cur->type == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011795 /*
11796 * W3C: "2 The {attribute uses} of the attribute groups ·resolved·
11797 * to by the ·actual value·s of the ref [attribute] of the
Daniel Veillard3646d642004-06-02 19:19:14 +000011798 * <attributeGroup> [children], if any."
11799 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011800 if (xmlSchemaBuildAttributeUsesOwned(ctxt,
11801 ((xmlSchemaAttributeGroupPtr) cur)->attributes, uses,
Daniel Veillard3646d642004-06-02 19:19:14 +000011802 lastUse) == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011803 return (-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000011804 }
11805 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011806 /* W3C: "1 The set of attribute uses corresponding to the
Daniel Veillard3646d642004-06-02 19:19:14 +000011807 * <attribute> [children], if any."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011808 */
11809 tmp = (xmlSchemaAttributeLinkPtr)
Daniel Veillard3646d642004-06-02 19:19:14 +000011810 xmlMalloc(sizeof(xmlSchemaAttributeLink));
11811 if (tmp == NULL) {
11812 xmlSchemaPErrMemory(ctxt, "building attribute uses", NULL);
11813 return (-1);
11814 }
11815 tmp->attr = cur;
11816 tmp->next = NULL;
11817 if (*uses == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011818 *uses = tmp;
11819 else
Daniel Veillard3646d642004-06-02 19:19:14 +000011820 (*lastUse)->next = tmp;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011821 *lastUse = tmp;
11822 }
Daniel Veillard3646d642004-06-02 19:19:14 +000011823 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011824 }
Daniel Veillard3646d642004-06-02 19:19:14 +000011825 return (0);
11826}
11827
Daniel Veillard50355f02004-06-08 17:52:16 +000011828/**
11829 * xmlSchemaCloneWildcardNsConstraints:
11830 * @ctxt: the schema parser context
11831 * @dest: the destination wildcard
11832 * @source: the source wildcard
11833 *
11834 * Clones the namespace constraints of source
11835 * and assignes them to dest.
11836 * Returns -1 on internal error, 0 otherwise.
11837 */
Daniel Veillard3646d642004-06-02 19:19:14 +000011838static int
11839xmlSchemaCloneWildcardNsConstraints(xmlSchemaParserCtxtPtr ctxt,
11840 xmlSchemaWildcardPtr *dest,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011841 xmlSchemaWildcardPtr source)
Daniel Veillard3646d642004-06-02 19:19:14 +000011842{
11843 xmlSchemaWildcardNsPtr cur, tmp, last;
11844
11845 if ((source == NULL) || (*dest == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011846 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000011847 (*dest)->any = source->any;
11848 cur = source->nsSet;
11849 last = NULL;
11850 while (cur != NULL) {
11851 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
11852 if (tmp == NULL)
11853 return(-1);
11854 tmp->value = cur->value;
11855 if (last == NULL)
11856 (*dest)->nsSet = tmp;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011857 else
Daniel Veillard3646d642004-06-02 19:19:14 +000011858 last->next = tmp;
11859 last = tmp;
11860 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011861 }
Daniel Veillard3646d642004-06-02 19:19:14 +000011862 if ((*dest)->negNsSet != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011863 xmlSchemaFreeWildcardNsSet((*dest)->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +000011864 if (source->negNsSet != NULL) {
11865 (*dest)->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
11866 if ((*dest)->negNsSet == NULL)
11867 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011868 (*dest)->negNsSet->value = source->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000011869 } else
11870 (*dest)->negNsSet = NULL;
11871 return(0);
11872}
11873
Daniel Veillard50355f02004-06-08 17:52:16 +000011874/**
11875 * xmlSchemaUnionWildcards:
11876 * @ctxt: the schema parser context
11877 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011878 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000011879 *
11880 * Unions the namespace constraints of the given wildcards.
11881 * @completeWild will hold the resulting union.
11882 * Returns a positive error code on failure, -1 in case of an
11883 * internal error, 0 otherwise.
11884 */
Daniel Veillard3646d642004-06-02 19:19:14 +000011885static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011886xmlSchemaUnionWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000011887 xmlSchemaWildcardPtr completeWild,
11888 xmlSchemaWildcardPtr curWild)
11889{
11890 xmlSchemaWildcardNsPtr cur, curB, tmp;
11891
11892 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011893 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000011894 * value.
11895 */
11896 if ((completeWild->any == curWild->any) &&
11897 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
11898 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011899
Daniel Veillard3646d642004-06-02 19:19:14 +000011900 if ((completeWild->negNsSet == NULL) ||
11901 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011902
Daniel Veillard3646d642004-06-02 19:19:14 +000011903 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000011904 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011905
11906 /*
11907 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000011908 */
11909 cur = completeWild->nsSet;
11910 while (cur != NULL) {
11911 found = 0;
11912 curB = curWild->nsSet;
11913 while (curB != NULL) {
11914 if (cur->value == curB->value) {
11915 found = 1;
11916 break;
11917 }
11918 curB = curB->next;
11919 }
11920 if (!found)
11921 break;
11922 cur = cur->next;
11923 }
11924 if (found)
11925 return(0);
11926 } else
11927 return(0);
11928 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011929 }
Daniel Veillard3646d642004-06-02 19:19:14 +000011930 /*
11931 * 2 If either O1 or O2 is any, then any must be the value
11932 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011933 if (completeWild->any != curWild->any) {
Daniel Veillard3646d642004-06-02 19:19:14 +000011934 if (completeWild->any == 0) {
11935 completeWild->any = 1;
11936 if (completeWild->nsSet != NULL) {
11937 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
11938 completeWild->nsSet = NULL;
11939 }
11940 if (completeWild->negNsSet != NULL) {
11941 xmlFree(completeWild->negNsSet);
11942 completeWild->negNsSet = NULL;
11943 }
11944 }
Daniel Veillard50355f02004-06-08 17:52:16 +000011945 return (0);
Daniel Veillard3646d642004-06-02 19:19:14 +000011946 }
11947 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011948 * 3 If both O1 and O2 are sets of (namespace names or ·absent·),
Daniel Veillard3646d642004-06-02 19:19:14 +000011949 * then the union of those sets must be the value.
11950 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011951 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000011952 int found;
11953 xmlSchemaWildcardNsPtr start;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011954
Daniel Veillard3646d642004-06-02 19:19:14 +000011955 cur = curWild->nsSet;
11956 start = completeWild->nsSet;
11957 while (cur != NULL) {
11958 found = 0;
11959 curB = start;
11960 while (curB != NULL) {
11961 if (cur->value == curB->value) {
11962 found = 1;
11963 break;
11964 }
11965 curB = curB->next;
11966 }
11967 if (!found) {
11968 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011969 if (tmp == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000011970 return (-1);
11971 tmp->value = cur->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011972 tmp->next = completeWild->nsSet;
Daniel Veillard3646d642004-06-02 19:19:14 +000011973 completeWild->nsSet = tmp;
11974 }
11975 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011976 }
11977
Daniel Veillard3646d642004-06-02 19:19:14 +000011978 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011979 }
Daniel Veillard3646d642004-06-02 19:19:14 +000011980 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011981 * 4 If the two are negations of different values (namespace names
Daniel Veillard3646d642004-06-02 19:19:14 +000011982 * or ·absent·), then a pair of not and ·absent· must be the value.
11983 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011984 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000011985 (curWild->negNsSet != NULL) &&
11986 (completeWild->negNsSet->value != curWild->negNsSet->value)) {
11987 completeWild->negNsSet->value = NULL;
Daniel Veillard50355f02004-06-08 17:52:16 +000011988
11989 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000011990 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011991 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000011992 * 5.
11993 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011994 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000011995 (completeWild->negNsSet->value != NULL) &&
11996 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011997 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000011998 (curWild->negNsSet->value != NULL) &&
11999 (completeWild->nsSet != NULL))) {
12000
12001 int nsFound, absentFound = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012002
Daniel Veillard3646d642004-06-02 19:19:14 +000012003 if (completeWild->nsSet != NULL) {
12004 cur = completeWild->nsSet;
12005 curB = curWild->negNsSet;
12006 } else {
12007 cur = curWild->nsSet;
12008 curB = completeWild->negNsSet;
12009 }
12010 nsFound = 0;
12011 while (cur != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012012 if (cur->value == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000012013 absentFound = 1;
12014 else if (cur->value == curB->value)
12015 nsFound = 1;
12016 if (nsFound && absentFound)
12017 break;
12018 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012019 }
Daniel Veillard3646d642004-06-02 19:19:14 +000012020
12021 if (nsFound && absentFound) {
12022 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012023 * 5.1 If the set S includes both the negated namespace
Daniel Veillard3646d642004-06-02 19:19:14 +000012024 * name and ·absent·, then any must be the value.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012025 */
Daniel Veillard3646d642004-06-02 19:19:14 +000012026 completeWild->any = 1;
12027 if (completeWild->nsSet != NULL) {
12028 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
12029 completeWild->nsSet = NULL;
12030 }
12031 if (completeWild->negNsSet != NULL) {
12032 xmlFree(completeWild->negNsSet);
12033 completeWild->negNsSet = NULL;
12034 }
12035 } else if (nsFound && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012036 /*
12037 * 5.2 If the set S includes the negated namespace name
12038 * but not ·absent·, then a pair of not and ·absent· must
Daniel Veillard3646d642004-06-02 19:19:14 +000012039 * be the value.
12040 */
12041 if (completeWild->nsSet != NULL) {
12042 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
12043 completeWild->nsSet = NULL;
12044 }
12045 if (completeWild->negNsSet == NULL) {
12046 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
12047 if (completeWild->negNsSet == NULL)
12048 return (-1);
12049 }
12050 completeWild->negNsSet->value = NULL;
12051 } else if ((!nsFound) && absentFound) {
12052 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012053 * 5.3 If the set S includes ·absent· but not the negated
Daniel Veillard3646d642004-06-02 19:19:14 +000012054 * namespace name, then the union is not expressible.
12055 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012056 xmlSchemaPErr(ctxt, completeWild->node,
Daniel Veillard3646d642004-06-02 19:19:14 +000012057 XML_SCHEMAP_UNION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000012058 "The union of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012059 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000012060 return(XML_SCHEMAP_UNION_NOT_EXPRESSIBLE);
Daniel Veillard3646d642004-06-02 19:19:14 +000012061 } else if ((!nsFound) && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012062 /*
12063 * 5.4 If the set S does not include either the negated namespace
12064 * name or ·absent·, then whichever of O1 or O2 is a pair of not
Daniel Veillard3646d642004-06-02 19:19:14 +000012065 * and a namespace name must be the value.
12066 */
12067 if (completeWild->negNsSet == NULL) {
12068 if (completeWild->nsSet != NULL) {
12069 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
12070 completeWild->nsSet = NULL;
12071 }
12072 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
12073 if (completeWild->negNsSet == NULL)
12074 return (-1);
12075 completeWild->negNsSet->value = curWild->negNsSet->value;
12076 }
12077 }
12078 return (0);
12079 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012080 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000012081 * 6.
12082 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012083 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000012084 (completeWild->negNsSet->value == NULL) &&
12085 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012086 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000012087 (curWild->negNsSet->value == NULL) &&
12088 (completeWild->nsSet != NULL))) {
12089
12090 if (completeWild->nsSet != NULL) {
12091 cur = completeWild->nsSet;
12092 } else {
12093 cur = curWild->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012094 }
Daniel Veillard3646d642004-06-02 19:19:14 +000012095 while (cur != NULL) {
12096 if (cur->value == NULL) {
12097 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012098 * 6.1 If the set S includes ·absent·, then any must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000012099 * value.
12100 */
12101 completeWild->any = 1;
12102 if (completeWild->nsSet != NULL) {
12103 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
12104 completeWild->nsSet = NULL;
12105 }
12106 if (completeWild->negNsSet != NULL) {
12107 xmlFree(completeWild->negNsSet);
12108 completeWild->negNsSet = NULL;
12109 }
12110 return (0);
12111 }
12112 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012113 }
Daniel Veillard3646d642004-06-02 19:19:14 +000012114 if (completeWild->negNsSet == NULL) {
12115 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012116 * 6.2 If the set S does not include ·absent·, then a pair of not
Daniel Veillard3646d642004-06-02 19:19:14 +000012117 * and ·absent· must be the value.
12118 */
12119 if (completeWild->nsSet != NULL) {
12120 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
12121 completeWild->nsSet = NULL;
12122 }
12123 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
12124 if (completeWild->negNsSet == NULL)
12125 return (-1);
12126 completeWild->negNsSet->value = NULL;
12127 }
12128 return (0);
12129 }
12130 return (0);
12131
12132}
12133
Daniel Veillard50355f02004-06-08 17:52:16 +000012134/**
12135 * xmlSchemaIntersectWildcards:
12136 * @ctxt: the schema parser context
12137 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012138 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000012139 *
12140 * Intersects the namespace constraints of the given wildcards.
12141 * @completeWild will hold the resulting intersection.
12142 * Returns a positive error code on failure, -1 in case of an
12143 * internal error, 0 otherwise.
12144 */
Daniel Veillard3646d642004-06-02 19:19:14 +000012145static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012146xmlSchemaIntersectWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000012147 xmlSchemaWildcardPtr completeWild,
12148 xmlSchemaWildcardPtr curWild)
12149{
William M. Brack803812b2004-06-03 02:11:24 +000012150 xmlSchemaWildcardNsPtr cur, curB, prev, tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000012151
12152 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012153 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000012154 * value.
12155 */
12156 if ((completeWild->any == curWild->any) &&
12157 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
12158 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012159
Daniel Veillard3646d642004-06-02 19:19:14 +000012160 if ((completeWild->negNsSet == NULL) ||
12161 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012162
Daniel Veillard3646d642004-06-02 19:19:14 +000012163 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000012164 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012165
12166 /*
12167 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000012168 */
12169 cur = completeWild->nsSet;
12170 while (cur != NULL) {
12171 found = 0;
12172 curB = curWild->nsSet;
12173 while (curB != NULL) {
12174 if (cur->value == curB->value) {
12175 found = 1;
12176 break;
12177 }
12178 curB = curB->next;
12179 }
12180 if (!found)
12181 break;
12182 cur = cur->next;
12183 }
12184 if (found)
12185 return(0);
12186 } else
12187 return(0);
12188 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012189 }
Daniel Veillard3646d642004-06-02 19:19:14 +000012190 /*
12191 * 2 If either O1 or O2 is any, then the other must be the value.
12192 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012193 if ((completeWild->any != curWild->any) && (completeWild->any)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000012194 if (xmlSchemaCloneWildcardNsConstraints(ctxt, &completeWild, curWild) == -1)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012195 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000012196 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012197 }
Daniel Veillard3646d642004-06-02 19:19:14 +000012198 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012199 * 3 If either O1 or O2 is a pair of not and a value (a namespace
12200 * name or ·absent·) and the other is a set of (namespace names or
12201 * ·absent·), then that set, minus the negated value if it was in
Daniel Veillard3646d642004-06-02 19:19:14 +000012202 * the set, minus ·absent· if it was in the set, must be the value.
12203 */
12204 if (((completeWild->negNsSet != NULL) && (curWild->nsSet != NULL)) ||
12205 ((curWild->negNsSet != NULL) && (completeWild->nsSet != NULL))) {
12206 const xmlChar *neg;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012207
Daniel Veillard3646d642004-06-02 19:19:14 +000012208 if (completeWild->nsSet == NULL) {
12209 neg = completeWild->negNsSet->value;
12210 if (xmlSchemaCloneWildcardNsConstraints(ctxt, &completeWild, curWild) == -1)
12211 return(-1);
12212 } else
12213 neg = curWild->negNsSet->value;
12214 /*
12215 * Remove absent and negated.
12216 */
12217 prev = NULL;
12218 cur = completeWild->nsSet;
12219 while (cur != NULL) {
12220 if (cur->value == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012221 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000012222 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012223 else
Daniel Veillard3646d642004-06-02 19:19:14 +000012224 prev->next = cur->next;
12225 xmlFree(cur);
12226 break;
12227 }
12228 prev = cur;
12229 cur = cur->next;
12230 }
12231 if (neg != NULL) {
12232 prev = NULL;
12233 cur = completeWild->nsSet;
12234 while (cur != NULL) {
12235 if (cur->value == neg) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012236 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000012237 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012238 else
Daniel Veillard3646d642004-06-02 19:19:14 +000012239 prev->next = cur->next;
12240 xmlFree(cur);
12241 break;
12242 }
12243 prev = cur;
12244 cur = cur->next;
12245 }
12246 }
12247
12248 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012249 }
Daniel Veillard3646d642004-06-02 19:19:14 +000012250 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012251 * 4 If both O1 and O2 are sets of (namespace names or ·absent·),
Daniel Veillard3646d642004-06-02 19:19:14 +000012252 * then the intersection of those sets must be the value.
12253 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012254 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000012255 int found;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012256
Daniel Veillard3646d642004-06-02 19:19:14 +000012257 cur = completeWild->nsSet;
12258 prev = NULL;
12259 while (cur != NULL) {
12260 found = 0;
12261 curB = curWild->nsSet;
12262 while (curB != NULL) {
12263 if (cur->value == curB->value) {
12264 found = 1;
12265 break;
12266 }
12267 curB = curB->next;
12268 }
12269 if (!found) {
12270 if (prev == NULL)
12271 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012272 else
Daniel Veillard3646d642004-06-02 19:19:14 +000012273 prev->next = cur->next;
12274 tmp = cur->next;
12275 xmlFree(cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012276 cur = tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000012277 continue;
12278 }
12279 prev = cur;
12280 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012281 }
12282
Daniel Veillard3646d642004-06-02 19:19:14 +000012283 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012284 }
12285 /* 5 If the two are negations of different namespace names,
Daniel Veillard3646d642004-06-02 19:19:14 +000012286 * then the intersection is not expressible
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012287 */
12288 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000012289 (curWild->negNsSet != NULL) &&
12290 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012291 (completeWild->negNsSet->value != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000012292 (curWild->negNsSet->value != NULL)) {
12293
12294 xmlSchemaPErr(ctxt, completeWild->node, XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000012295 "The intersection of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012296 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000012297 return(XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012298 }
12299 /*
12300 * 6 If the one is a negation of a namespace name and the other
12301 * is a negation of ·absent·, then the one which is the negation
Daniel Veillard3646d642004-06-02 19:19:14 +000012302 * of a namespace name must be the value.
12303 */
12304 if ((completeWild->negNsSet != NULL) && (curWild->negNsSet != NULL) &&
12305 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012306 (completeWild->negNsSet->value == NULL)) {
12307 completeWild->negNsSet->value = curWild->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000012308 }
12309 return(0);
12310}
12311
Daniel Veillard50355f02004-06-08 17:52:16 +000012312/**
12313 * xmlSchemaIsWildcardNsConstraintSubset:
12314 * @ctxt: the schema parser context
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000012315 * @sub: the first wildcard
12316 * @super: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000012317 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000012318 * Schema Component Constraint: Wildcard Subset (cos-ns-subset)
12319 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012320 * Returns 0 if the namespace constraint of @sub is an intensional
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000012321 * subset of @super, 1 otherwise.
Daniel Veillard50355f02004-06-08 17:52:16 +000012322 */
12323static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000012324xmlSchemaCheckCOSNSSubset(xmlSchemaWildcardPtr sub,
12325 xmlSchemaWildcardPtr super)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012326{
Daniel Veillard50355f02004-06-08 17:52:16 +000012327 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012328 * 1 super must be any.
Daniel Veillard50355f02004-06-08 17:52:16 +000012329 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000012330 if (super->any)
12331 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000012332 /*
12333 * 2.1 sub must be a pair of not and a namespace name or ·absent·.
12334 * 2.2 super must be a pair of not and the same value.
12335 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000012336 if ((sub->negNsSet != NULL) &&
12337 (super->negNsSet != NULL) &&
12338 (sub->negNsSet->value == sub->negNsSet->value))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012339 return (0);
12340 /*
12341 * 3.1 sub must be a set whose members are either namespace names or ·absent·.
Daniel Veillard50355f02004-06-08 17:52:16 +000012342 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000012343 if (sub->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000012344 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012345 * 3.2.1 super must be the same set or a superset thereof.
Daniel Veillard50355f02004-06-08 17:52:16 +000012346 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000012347 if (super->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000012348 xmlSchemaWildcardNsPtr cur, curB;
12349 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012350
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000012351 cur = sub->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000012352 while (cur != NULL) {
12353 found = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000012354 curB = super->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000012355 while (curB != NULL) {
12356 if (cur->value == curB->value) {
12357 found = 1;
12358 break;
12359 }
12360 curB = curB->next;
12361 }
12362 if (!found)
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000012363 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000012364 cur = cur->next;
12365 }
12366 if (found)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012367 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000012368 } else if (super->negNsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000012369 xmlSchemaWildcardNsPtr cur;
12370 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012371 * 3.2.2 super must be a pair of not and a namespace name or
12372 * ·absent· and that value must not be in sub's set.
Daniel Veillard50355f02004-06-08 17:52:16 +000012373 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000012374 cur = sub->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012375 while (cur != NULL) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000012376 if (cur->value == super->negNsSet->value)
12377 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000012378 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012379 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000012380 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000012381 }
12382 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000012383 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000012384}
12385
12386/**
12387 * xmlSchemaBuildCompleteAttributeWildcard:
12388 * @ctxt: the schema parser context
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012389 * @attrs: the attribute list
Daniel Veillard50355f02004-06-08 17:52:16 +000012390 * @completeWild: the resulting complete wildcard
12391 *
12392 * Returns -1 in case of an internal error, 0 otherwise.
12393 */
12394static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012395xmlSchemaBuildCompleteAttributeWildcard(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard50355f02004-06-08 17:52:16 +000012396 xmlSchemaAttributePtr attrs,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012397 xmlSchemaWildcardPtr *completeWild)
12398{
Daniel Veillard3646d642004-06-02 19:19:14 +000012399 while (attrs != NULL) {
12400 if (attrs->type == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) {
12401 xmlSchemaAttributeGroupPtr group;
12402
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012403 group = (xmlSchemaAttributeGroupPtr) attrs;
12404 /*
12405 * Handle attribute group references.
12406 */
12407 if (group->ref != NULL) {
12408 if (group->refItem == NULL) {
12409 /*
12410 * TODO: Should we raise a warning here?
12411 */
12412 /*
12413 * The referenced attribute group definition could not
12414 * be resolved beforehand, so skip.
12415 */
12416 attrs = attrs->next;
12417 continue;
12418 } else
12419 group = group->refItem;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012420 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012421 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012422 * For every attribute group definition, an intersected wildcard
12423 * will be created (assumed that a wildcard exists on the
12424 * particular attr. gr. def. or on any contained attr. gr. def
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012425 * at all).
12426 * The flag XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED ensures
12427 * that the intersection will be performed only once.
12428 */
Daniel Veillard3646d642004-06-02 19:19:14 +000012429 if ((group->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED) == 0) {
12430 if (group->attributes != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012431 if (xmlSchemaBuildCompleteAttributeWildcard(ctxt,
Daniel Veillard50355f02004-06-08 17:52:16 +000012432 group->attributes, &group->attributeWildcard) == -1)
12433 return (-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000012434 }
12435 group->flags |= XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012436 }
12437 if (group->attributeWildcard != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000012438 if (*completeWild == NULL) {
Daniel Veillard3646d642004-06-02 19:19:14 +000012439 /*
12440 * Copy the first encountered wildcard as context, except for the annotation.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012441 *
12442 * Although the complete wildcard might not correspond to any
12443 * node in the schema, we will save this context node.
Daniel Veillard3646d642004-06-02 19:19:14 +000012444 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012445 *completeWild = xmlSchemaAddWildcard(ctxt, ctxt->schema,
12446 XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
12447 group->attributeWildcard->node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012448 if (xmlSchemaCloneWildcardNsConstraints(ctxt,
Daniel Veillard50355f02004-06-08 17:52:16 +000012449 completeWild, group->attributeWildcard) == -1)
12450 return (-1);
12451 (*completeWild)->processContents = group->attributeWildcard->processContents;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012452 (*completeWild)->node = group->attributeWildcard->node;
12453 } else if (xmlSchemaIntersectWildcards(ctxt, *completeWild, group->attributeWildcard) == -1)
Daniel Veillard50355f02004-06-08 17:52:16 +000012454 return (-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000012455 }
12456 }
12457 attrs = attrs->next;
12458 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012459
12460 return (0);
Daniel Veillard3646d642004-06-02 19:19:14 +000012461}
12462
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012463static int
12464xmlSchemaGetEffectiveValueConstraint(xmlSchemaAttributePtr item,
12465 int *fixed,
12466 const xmlChar **value,
12467 xmlSchemaValPtr *val)
12468{
12469 *fixed = 0;
12470 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012471 if (val != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012472 *val = NULL;
12473
12474 if (item->defValue == NULL)
12475 item = item->refDecl;
12476
12477 if (item == NULL)
12478 return (0);
12479
12480 if (item->defValue != NULL) {
12481 *value = item->defValue;
12482 if (val != 0)
12483 *val = item->defVal;
12484 if (item->flags & XML_SCHEMAS_ATTR_FIXED)
12485 *fixed = 1;
12486 return (1);
12487 }
12488 return (0);
12489}
Daniel Veillard3646d642004-06-02 19:19:14 +000012490/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012491 * xmlSchemaCheckCVCWildcardNamespace:
Daniel Veillard3646d642004-06-02 19:19:14 +000012492 * @wild: the wildcard
12493 * @ns: the namespace
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012494 *
12495 * Validation Rule: Wildcard allows Namespace Name
12496 * (cvc-wildcard-namespace)
12497 *
Daniel Veillard3646d642004-06-02 19:19:14 +000012498 *
12499 * Returns 1 if the given namespace matches the wildcard,
12500 * 0 otherwise.
12501 */
12502static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012503xmlSchemaCheckCVCWildcardNamespace(xmlSchemaWildcardPtr wild,
12504 const xmlChar* ns)
Daniel Veillard3646d642004-06-02 19:19:14 +000012505{
12506 if (wild == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012507 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000012508
12509 if (wild->any)
12510 return(1);
12511 else if (wild->nsSet != NULL) {
12512 xmlSchemaWildcardNsPtr cur;
12513
12514 cur = wild->nsSet;
12515 while (cur != NULL) {
12516 if (xmlStrEqual(cur->value, ns))
12517 return(1);
12518 cur = cur->next;
12519 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012520 } else if ((wild->negNsSet != NULL) && (ns != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000012521 (!xmlStrEqual(wild->negNsSet->value, ns)))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012522 return(1);
12523
Daniel Veillard3646d642004-06-02 19:19:14 +000012524 return(0);
12525}
12526
12527/**
12528 * xmlSchemaBuildAttributeValidation:
12529 * @ctxt: the schema parser context
12530 * @type: the complex type definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012531 *
Daniel Veillard3646d642004-06-02 19:19:14 +000012532 *
12533 * Builds the wildcard and the attribute uses on the given complex type.
12534 * Returns -1 if an internal error occurs, 0 otherwise.
12535 */
12536static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012537xmlSchemaBuildAttributeValidation(xmlSchemaParserCtxtPtr pctxt,
12538 xmlSchemaTypePtr type)
Daniel Veillard3646d642004-06-02 19:19:14 +000012539{
12540 xmlSchemaTypePtr baseType = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012541 xmlSchemaAttributeLinkPtr cur, base, tmp, id = NULL,
12542 prev = NULL, uses = NULL, lastUse = NULL, lastBaseUse = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000012543 xmlSchemaAttributePtr attrs;
Daniel Veillard01fa6152004-06-29 17:04:39 +000012544 xmlSchemaTypePtr anyType;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012545 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000012546 int err = 0;
Daniel Veillard3646d642004-06-02 19:19:14 +000012547
Daniel Veillard01fa6152004-06-29 17:04:39 +000012548 anyType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012549 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000012550 * Complex Type Definition with complex content Schema Component.
12551 *
12552 * Attribute uses.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012553 * TODO: Add checks for absent referenced attribute declarations and
12554 * simple types.
Daniel Veillard3646d642004-06-02 19:19:14 +000012555 */
12556 if (type->attributeUses != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012557 PERROR_INT("xmlSchemaBuildAttributeValidation",
12558 "attribute uses already builded");
Daniel Veillard3646d642004-06-02 19:19:14 +000012559 return (-1);
12560 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000012561 if (type->baseType == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012562 PERROR_INT("xmlSchemaBuildAttributeValidation",
12563 "no base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000012564 return (-1);
12565 }
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012566 baseType = type->baseType;
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012567 /*
12568 * Inherit the attribute uses of the base type.
12569 */
12570 /*
12571 * NOTE: It is allowed to "extend" the anyType complex type.
12572 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012573 if (! IS_ANYTYPE(baseType)) {
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012574 if (baseType != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012575 for (cur = baseType->attributeUses; cur != NULL;
12576 cur = cur->next) {
12577 tmp = (xmlSchemaAttributeLinkPtr)
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012578 xmlMalloc(sizeof(xmlSchemaAttributeLink));
12579 if (tmp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012580 xmlSchemaPErrMemory(pctxt,
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012581 "building attribute uses of complexType", NULL);
12582 return (-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000012583 }
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012584 tmp->attr = cur->attr;
12585 tmp->next = NULL;
12586 if (type->attributeUses == NULL) {
12587 type->attributeUses = tmp;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012588 } else
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012589 lastBaseUse->next = tmp;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012590 lastBaseUse = tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000012591 }
12592 }
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012593 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000012594 attrs = type->attributes;
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012595 /*
12596 * Handle attribute wildcards.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012597 */
12598 err = xmlSchemaBuildCompleteAttributeWildcard(pctxt,
12599 attrs, &type->attributeWildcard);
Daniel Veillard01fa6152004-06-29 17:04:39 +000012600 /*
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012601 * NOTE: During the parse time, the wildcard is created on the complexType
12602 * directly, if encountered in a <restriction> or <extension> element.
Daniel Veillard01fa6152004-06-29 17:04:39 +000012603 */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012604 if (err == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012605 PERROR_INT("xmlSchemaBuildAttributeValidation",
12606 "failed to build an intersected attribute wildcard");
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000012607 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012608 }
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012609
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012610 if ((type->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION) &&
12611 ((IS_ANYTYPE(baseType)) ||
12612 ((baseType != NULL) &&
12613 (baseType->type == XML_SCHEMA_TYPE_COMPLEX) &&
12614 (baseType->attributeWildcard != NULL)))) {
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012615 if (type->attributeWildcard != NULL) {
12616 /*
12617 * Union the complete wildcard with the base wildcard.
12618 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012619 if (xmlSchemaUnionWildcards(pctxt, type->attributeWildcard,
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012620 baseType->attributeWildcard) == -1)
12621 return (-1);
12622 } else {
12623 /*
12624 * Just inherit the wildcard.
12625 */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012626 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012627 * NOTE: This is the only case where an attribute
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012628 * wildcard is shared.
12629 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012630 type->attributeWildcard = baseType->attributeWildcard;
Daniel Veillard50355f02004-06-08 17:52:16 +000012631 }
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012632 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012633
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012634 if (type->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION) {
12635 if (type->attributeWildcard != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012636 /*
12637 * Derivation Valid (Restriction, Complex)
12638 * 4.1 The {base type definition} must also have one.
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012639 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012640 if (baseType->attributeWildcard == NULL) {
12641 xmlSchemaPCustomErr(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000012642 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012643 NULL, type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000012644 "The type has an attribute wildcard, "
12645 "but the base type %s does not have one",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012646 xmlSchemaFormatItemForReport(&str, NULL, baseType, NULL));
Daniel Veillardc0826a72004-08-10 14:17:33 +000012647 FREE_AND_NULL(str)
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012648 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000012649 } else if (xmlSchemaCheckCOSNSSubset(
12650 type->attributeWildcard, baseType->attributeWildcard)) {
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012651 /* 4.2 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012652 xmlSchemaPCustomErr(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000012653 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_2,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012654 NULL, type, NULL,
12655 "The attribute wildcard is not a valid "
Daniel Veillardc0826a72004-08-10 14:17:33 +000012656 "subset of the wildcard in the base type %s",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012657 xmlSchemaFormatItemForReport(&str, NULL, baseType, NULL));
12658 FREE_AND_NULL(str)
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012659 return (1);
12660 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012661 /* 4.3 Unless the {base type definition} is the ·ur-type
12662 * definition·, the complex type definition's {attribute
12663 * wildcard}'s {process contents} must be identical to or
12664 * stronger than the {base type definition}'s {attribute
12665 * wildcard}'s {process contents}, where strict is stronger
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012666 * than lax is stronger than skip.
12667 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012668 if ((! IS_ANYTYPE(baseType)) &&
12669 (type->attributeWildcard->processContents <
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012670 baseType->attributeWildcard->processContents)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012671 xmlSchemaPCustomErr(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000012672 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_3,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012673 NULL, type, NULL,
12674 "The 'process contents' of the attribute wildcard is "
12675 "weaker than the one in the base type %s",
12676 xmlSchemaFormatItemForReport(&str, NULL, baseType, NULL));
Daniel Veillardc0826a72004-08-10 14:17:33 +000012677 FREE_AND_NULL(str)
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012678 return (1);
12679 }
12680 }
12681 } else if (type->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION) {
12682 /*
12683 * Derivation Valid (Extension)
12684 * At this point the type and the base have both, either
12685 * no wildcard or a wildcard.
12686 */
12687 if ((baseType->attributeWildcard != NULL) &&
12688 (baseType->attributeWildcard != type->attributeWildcard)) {
12689 /* 1.3 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000012690 if (xmlSchemaCheckCOSNSSubset(
12691 baseType->attributeWildcard, type->attributeWildcard)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012692 xmlSchemaPCustomErr(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000012693 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012694 NULL, type, NULL,
12695 "The attribute wildcard is not a valid "
Daniel Veillardc0826a72004-08-10 14:17:33 +000012696 "superset of the one in the base type %s",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012697 xmlSchemaFormatItemForReport(&str, NULL, baseType, NULL));
12698 FREE_AND_NULL(str)
12699 return (1);
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012700 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012701 }
12702 }
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000012703
Daniel Veillard3646d642004-06-02 19:19:14 +000012704 /*
12705 * Gather attribute uses defined by this type.
12706 */
12707 if (attrs != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012708 if (xmlSchemaBuildAttributeUsesOwned(pctxt, attrs,
Daniel Veillard3646d642004-06-02 19:19:14 +000012709 &uses, &lastUse) == -1) {
12710 return (-1);
12711 }
12712 }
12713 /* 3.4.6 -> Complex Type Definition Properties Correct 4.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012714 * "Two distinct attribute declarations in the {attribute uses} must
Daniel Veillard3646d642004-06-02 19:19:14 +000012715 * not have identical {name}s and {target namespace}s."
12716 *
12717 * For "extension" this is done further down.
12718 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012719 if ((uses != NULL) && ((type->flags &
12720 XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION) == 0)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000012721 cur = uses;
12722 while (cur != NULL) {
12723 tmp = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012724 while (tmp != NULL) {
12725 if ((xmlStrEqual(xmlSchemaGetAttrName(cur->attr),
Daniel Veillardc0826a72004-08-10 14:17:33 +000012726 xmlSchemaGetAttrName(tmp->attr))) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012727 (xmlStrEqual(xmlSchemaGetAttrTargetNsURI(cur->attr),
Daniel Veillardc0826a72004-08-10 14:17:33 +000012728 xmlSchemaGetAttrTargetNsURI(tmp->attr)))) {
12729
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012730 xmlSchemaPAttrUseErr(pctxt,
12731 XML_SCHEMAP_CT_PROPS_CORRECT_4,
12732 type, cur->attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +000012733 "Duplicate attribute use %s specified",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012734 xmlSchemaFormatQName(&str,
12735 xmlSchemaGetAttrTargetNsURI(tmp->attr),
12736 xmlSchemaGetAttrName(tmp->attr)));
12737 FREE_AND_NULL(str)
Daniel Veillard3646d642004-06-02 19:19:14 +000012738 break;
12739 }
12740 tmp = tmp->next;
12741 }
12742 cur = cur->next;
12743 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012744 }
12745 if (type->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION) {
Daniel Veillard3646d642004-06-02 19:19:14 +000012746 /*
12747 * Derive by restriction.
12748 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012749 if (IS_ANYTYPE(baseType)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000012750 type->attributeUses = uses;
12751 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012752 int found, valid;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012753 const xmlChar *bEffValue;
12754 int effFixed;
Daniel Veillard3646d642004-06-02 19:19:14 +000012755
12756 cur = uses;
12757 while (cur != NULL) {
12758 found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012759 valid = 1;
Daniel Veillard3646d642004-06-02 19:19:14 +000012760 base = type->attributeUses;
12761 while (base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012762 if (xmlStrEqual(xmlSchemaGetAttrName(cur->attr),
Daniel Veillardc0826a72004-08-10 14:17:33 +000012763 xmlSchemaGetAttrName(base->attr)) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012764 xmlStrEqual(xmlSchemaGetAttrTargetNsURI(cur->attr),
Daniel Veillardc0826a72004-08-10 14:17:33 +000012765 xmlSchemaGetAttrTargetNsURI(base->attr))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012766
12767 found = 1;
Daniel Veillard3646d642004-06-02 19:19:14 +000012768
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012769 if ((cur->attr->occurs ==
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000012770 XML_SCHEMAS_ATTR_USE_PROHIBITED) &&
12771 (base->attr->occurs ==
12772 XML_SCHEMAS_ATTR_USE_OPTIONAL)) {
12773 /*
12774 * NOOP.
12775 */
12776 } else if ((cur->attr->occurs ==
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012777 XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
12778 (base->attr->occurs ==
12779 XML_SCHEMAS_ATTR_USE_REQUIRED)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000012780 /*
12781 * derivation-ok-restriction 2.1.1
Daniel Veillard3646d642004-06-02 19:19:14 +000012782 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012783 xmlSchemaPAttrUseErr(pctxt,
12784 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_1,
12785 type, cur->attr,
12786 "The 'optional' use is inconsistent with a "
12787 "matching 'required' use of the base type",
12788 NULL);
12789 } else if ((cur->attr->occurs ==
12790 XML_SCHEMAS_ATTR_USE_PROHIBITED) &&
12791 (base->attr->occurs ==
12792 XML_SCHEMAS_ATTR_USE_REQUIRED)) {
12793 /*
12794 * derivation-ok-restriction 3
12795 */
12796 xmlSchemaPCustomErr(pctxt,
12797 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_3,
12798 NULL, type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000012799 "A matching attribute use for the 'required' "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012800 "attribute use '%s' of the base type is "
12801 "missing",
12802 xmlSchemaFormatQName(&str,
12803 xmlSchemaGetAttrTargetNsURI(base->attr),
12804 xmlSchemaGetAttrName(base->attr)));
Daniel Veillardc0826a72004-08-10 14:17:33 +000012805 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012806 } else if (xmlSchemaCheckCOSSTDerivedOK(
12807 cur->attr->subtypes, base->attr->subtypes, 0) != 0) {
12808
12809 /*
12810 * SPEC (2.1.2) "R's {attribute declaration}'s
12811 * {type definition} must be validly derived from
12812 * B's {type definition} given the empty set as
12813 * defined in Type Derivation OK (Simple) (§3.14.6)."
12814 */
12815 xmlSchemaPAttrUseErr(pctxt,
12816 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_2,
12817 type, cur->attr,
12818 "The attribute declaration's type "
12819 "definition is not validly derived from "
12820 "the corresponding definition in the "
12821 "base type", NULL);
Daniel Veillard3646d642004-06-02 19:19:14 +000012822 } else {
12823 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012824 * 2.1.3 [Definition:] Let the effective value
12825 * constraint of an attribute use be its {value
12826 * constraint}, if present, otherwise its {attribute
12827 * declaration}'s {value constraint} .
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012828 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012829 xmlSchemaGetEffectiveValueConstraint(base->attr,
Daniel Veillard24505b02005-07-28 23:49:35 +000012830 &effFixed, &bEffValue, NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012831 /*
12832 * 2.1.3 ... one of the following must be true
12833 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012834 * 2.1.3.1 B's ·effective value constraint· is
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012835 * ·absent· or default.
12836 */
12837 if ((bEffValue != NULL) &&
12838 (effFixed == 1)) {
12839 const xmlChar *rEffValue = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012840
12841 xmlSchemaGetEffectiveValueConstraint(base->attr,
Daniel Veillard24505b02005-07-28 23:49:35 +000012842 &effFixed, &rEffValue, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012843 /*
12844 * 2.1.3.2 R's ·effective value constraint· is
12845 * fixed with the same string as B's.
12846 * TODO: Compare the computed values.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012847 */
12848 if ((effFixed == 0) ||
12849 (! xmlStrEqual(rEffValue, bEffValue))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012850 xmlSchemaPAttrUseErr(pctxt,
12851 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_3,
12852 type, cur->attr,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012853 "The effective value constraint of the "
12854 "attribute use is inconsistent with "
12855 "its correspondent of the base type",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012856 NULL);
12857 } else {
12858 /*
12859 * Override the attribute use.
12860 */
12861 base->attr = cur->attr;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012862 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012863 } else
12864 base->attr = cur->attr;
Daniel Veillard3646d642004-06-02 19:19:14 +000012865 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012866
Daniel Veillard3646d642004-06-02 19:19:14 +000012867 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012868 }
Daniel Veillard3646d642004-06-02 19:19:14 +000012869 base = base->next;
12870 }
Daniel Veillard3646d642004-06-02 19:19:14 +000012871
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012872 if ((!found) && (cur->attr->occurs !=
12873 XML_SCHEMAS_ATTR_USE_PROHIBITED)) {
12874 /*
12875 * derivation-ok-restriction 2.2
12876 */
12877 if ((baseType->attributeWildcard == NULL) ||
12878 (xmlSchemaCheckCVCWildcardNamespace(
12879 baseType->attributeWildcard,
12880 cur->attr->targetNamespace) != 1)) {
12881 xmlSchemaPAttrUseErr(pctxt,
12882 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_2,
12883 type, cur->attr,
12884 "Neither a matching attribute use, "
12885 "nor a matching wildcard in the base type does exist",
12886 NULL);
12887 } else {
12888 /*
12889 * Add the attribute use.
12890 *
12891 * Note that this may lead to funny derivation error reports, if
12892 * multiple equal attribute uses exist; but this is not
12893 * allowed anyway, and it will be reported beforehand.
12894 */
12895 tmp = cur;
12896 if (prev != NULL)
12897 prev->next = cur->next;
12898 else
12899 uses = cur->next;
12900 cur = cur->next;
12901 tmp->next = NULL;
12902 if (type->attributeUses == NULL) {
12903 type->attributeUses = tmp;
12904 } else
12905 lastBaseUse->next = tmp;
12906 lastBaseUse = tmp;
12907
12908 continue;
Daniel Veillard3646d642004-06-02 19:19:14 +000012909 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012910 }
12911 prev = cur;
Daniel Veillard3646d642004-06-02 19:19:14 +000012912 cur = cur->next;
12913 }
12914 if (uses != NULL)
12915 xmlSchemaFreeAttributeUseList(uses);
12916 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012917 } else if (type->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION) {
Daniel Veillard3646d642004-06-02 19:19:14 +000012918 /*
12919 * The spec allows only appending, and not other kinds of extensions.
12920 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012921 * This ensures: Schema Component Constraint: Derivation Valid (Extension) : 1.2
Daniel Veillard3646d642004-06-02 19:19:14 +000012922 */
12923 if (uses != NULL) {
12924 if (type->attributeUses == NULL) {
12925 type->attributeUses = uses;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012926 } else
Daniel Veillard3646d642004-06-02 19:19:14 +000012927 lastBaseUse->next = uses;
Daniel Veillard50355f02004-06-08 17:52:16 +000012928 }
Daniel Veillard3646d642004-06-02 19:19:14 +000012929 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012930 PERROR_INT("xmlSchemaBuildAttributeValidation",
12931 "no derivation method");
12932 return (-1);
Daniel Veillard50355f02004-06-08 17:52:16 +000012933 }
Daniel Veillard3646d642004-06-02 19:19:14 +000012934 /*
12935 * 3.4.6 -> Complex Type Definition Properties Correct
12936 */
12937 if (type->attributeUses != NULL) {
12938 cur = type->attributeUses;
12939 prev = NULL;
12940 while (cur != NULL) {
12941 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012942 * 4. Two distinct attribute declarations in the {attribute uses} must
Daniel Veillard3646d642004-06-02 19:19:14 +000012943 * not have identical {name}s and {target namespace}s.
Daniel Veillard50355f02004-06-08 17:52:16 +000012944 *
Daniel Veillard3646d642004-06-02 19:19:14 +000012945 * Note that this was already done for "restriction" and types derived from
12946 * the ur-type.
Daniel Veillard50355f02004-06-08 17:52:16 +000012947 */
Daniel Veillard3646d642004-06-02 19:19:14 +000012948 if (type->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION) {
12949 tmp = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012950 while (tmp != NULL) {
12951 if ((xmlStrEqual(xmlSchemaGetAttrName(cur->attr),
Daniel Veillardc0826a72004-08-10 14:17:33 +000012952 xmlSchemaGetAttrName(tmp->attr))) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012953 (xmlStrEqual(xmlSchemaGetAttrTargetNsURI(cur->attr ),
Daniel Veillardc0826a72004-08-10 14:17:33 +000012954 xmlSchemaGetAttrTargetNsURI(tmp->attr)))) {
Daniel Veillard3646d642004-06-02 19:19:14 +000012955
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012956 xmlSchemaPAttrUseErr(pctxt,
12957 XML_SCHEMAP_CT_PROPS_CORRECT_4,
12958 type, tmp->attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +000012959 "Duplicate attribute use specified", NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000012960 break;
12961 }
Daniel Veillard3646d642004-06-02 19:19:14 +000012962 tmp = tmp->next;
Daniel Veillard50355f02004-06-08 17:52:16 +000012963 }
12964 }
Daniel Veillard3646d642004-06-02 19:19:14 +000012965 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012966 * 5. Two distinct attribute declarations in the {attribute uses} must
Daniel Veillard3646d642004-06-02 19:19:14 +000012967 * not have {type definition}s which are or are derived from ID.
12968 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012969 if ((cur->attr->subtypes != NULL) &&
12970 (xmlSchemaIsDerivedFromBuiltInType(cur->attr->subtypes,
12971 XML_SCHEMAS_ID))) {
Daniel Veillard3646d642004-06-02 19:19:14 +000012972 if (id != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012973 xmlSchemaPAttrUseErr(pctxt,
12974 XML_SCHEMAP_CT_PROPS_CORRECT_5,
12975 type, cur->attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +000012976 "There must not exist more than one attribute use, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012977 "declared of type 'ID' or derived from it",
Daniel Veillardc0826a72004-08-10 14:17:33 +000012978 NULL);
12979 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012980 }
Daniel Veillard3646d642004-06-02 19:19:14 +000012981 id = cur;
12982 }
12983 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012984 * Remove "prohibited" attribute uses. The reason this is done at this late
Daniel Veillard3646d642004-06-02 19:19:14 +000012985 * stage is to be able to catch dublicate attribute uses. So we had to keep
12986 * prohibited uses in the list as well.
12987 */
12988 if (cur->attr->occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED) {
12989 tmp = cur;
12990 if (prev == NULL)
12991 type->attributeUses = cur->next;
12992 else
12993 prev->next = cur->next;
12994 cur = cur->next;
Daniel Veillard50355f02004-06-08 17:52:16 +000012995 xmlFree(tmp);
Daniel Veillard3646d642004-06-02 19:19:14 +000012996 } else {
12997 prev = cur;
12998 cur = cur->next;
12999 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013000 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013001 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013002 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013003 * TODO: This check should be removed if we are 100% sure of
13004 * the base type attribute uses already being built.
13005 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013006 if ((baseType != NULL) && (! IS_ANYTYPE(baseType)) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013007 (baseType->type == XML_SCHEMA_TYPE_COMPLEX) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013008 (IS_NOT_TYPEFIXED(baseType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013009 PERROR_INT("xmlSchemaBuildAttributeValidation",
13010 "attribute uses not builded on base type");
13011 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013012 return (0);
13013}
13014
13015/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000013016 * xmlSchemaTypeFinalContains:
13017 * @schema: the schema
13018 * @type: the type definition
13019 * @final: the final
13020 *
13021 * Evaluates if a type definition contains the given "final".
13022 * This does take "finalDefault" into account as well.
13023 *
13024 * Returns 1 if the type does containt the given "final",
13025 * 0 otherwise.
13026 */
13027static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013028xmlSchemaTypeFinalContains(xmlSchemaTypePtr type, int final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000013029{
Daniel Veillard01fa6152004-06-29 17:04:39 +000013030 if (type == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013031 return (0);
13032 if (type->flags & final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000013033 return (1);
13034 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013035 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000013036}
13037
13038/**
13039 * xmlSchemaGetUnionSimpleTypeMemberTypes:
13040 * @type: the Union Simple Type
13041 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013042 * Returns a list of member types of @type if existing,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013043 * returns NULL otherwise.
13044 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000013045static xmlSchemaTypeLinkPtr
Daniel Veillard01fa6152004-06-29 17:04:39 +000013046xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type)
13047{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013048 while ((type != NULL) && (type->type == XML_SCHEMA_TYPE_SIMPLE)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000013049 if (type->memberTypes != NULL)
13050 return (type->memberTypes);
13051 else
13052 type = type->baseType;
13053 }
13054 return (NULL);
13055}
13056
13057/**
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013058 * xmlSchemaGetParticleTotalRangeMin:
13059 * @particle: the particle
13060 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013061 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013062 * (all and sequence) + (choice)
13063 *
13064 * Returns the minimun Effective Total Range.
13065 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013066static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013067xmlSchemaGetParticleTotalRangeMin(xmlSchemaParticlePtr particle)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013068{
13069 if ((particle->children == NULL) ||
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013070 (particle->minOccurs == 0))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013071 return (0);
13072 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013073 int min = -1, cur;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013074 xmlSchemaParticlePtr part =
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013075 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013076
13077 if (part == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013078 return (0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013079 while (part != NULL) {
13080 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
13081 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013082 cur = part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013083 else
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013084 cur = xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013085 if (cur == 0)
13086 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013087 if ((min > cur) || (min == -1))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013088 min = cur;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013089 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013090 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013091 return (particle->minOccurs * min);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013092 } else {
13093 /* <all> and <sequence> */
13094 int sum = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013095 xmlSchemaParticlePtr part =
13096 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013097
13098 if (part == NULL)
13099 return (0);
13100 do {
13101 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
13102 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013103 sum += part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013104 else
13105 sum += xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013106 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013107 } while (part != NULL);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013108 return (particle->minOccurs * sum);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013109 }
13110}
13111
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013112/**
13113 * xmlSchemaGetParticleTotalRangeMax:
13114 * @particle: the particle
13115 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013116 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013117 * (all and sequence) + (choice)
13118 *
13119 * Returns the maximum Effective Total Range.
13120 */
13121static int
13122xmlSchemaGetParticleTotalRangeMax(xmlSchemaParticlePtr particle)
13123{
13124 if ((particle->children == NULL) ||
13125 (particle->children->children == NULL))
13126 return (0);
13127 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
13128 int max = -1, cur;
13129 xmlSchemaParticlePtr part =
13130 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013131
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013132 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
13133 if (part->children == NULL)
13134 continue;
13135 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
13136 (part->children->type == XML_SCHEMA_TYPE_ANY))
13137 cur = part->maxOccurs;
13138 else
13139 cur = xmlSchemaGetParticleTotalRangeMax(part);
13140 if (cur == UNBOUNDED)
13141 return (UNBOUNDED);
13142 if ((max < cur) || (max == -1))
13143 max = cur;
13144 }
13145 /* TODO: Handle overflows? */
13146 return (particle->maxOccurs * max);
13147 } else {
13148 /* <all> and <sequence> */
13149 int sum = 0, cur;
13150 xmlSchemaParticlePtr part =
13151 (xmlSchemaParticlePtr) particle->children->children;
13152
13153 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
13154 if (part->children == NULL)
13155 continue;
13156 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
13157 (part->children->type == XML_SCHEMA_TYPE_ANY))
13158 cur = part->maxOccurs;
13159 else
13160 cur = xmlSchemaGetParticleTotalRangeMax(part);
13161 if (cur == UNBOUNDED)
13162 return (UNBOUNDED);
13163 if ((cur > 0) && (particle->maxOccurs == UNBOUNDED))
13164 return (UNBOUNDED);
13165 sum += cur;
13166 }
13167 /* TODO: Handle overflows? */
13168 return (particle->maxOccurs * sum);
13169 }
13170}
13171
13172/**
13173 * xmlSchemaIsParticleEmptiable:
13174 * @particle: the particle
13175 *
13176 * Schema Component Constraint: Particle Emptiable
13177 * Checks whether the given particle is emptiable.
13178 *
13179 * Returns 1 if emptiable, 0 otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013180 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013181static int
13182xmlSchemaIsParticleEmptiable(xmlSchemaParticlePtr particle)
13183{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013184 /*
13185 * SPEC (1) "Its {min occurs} is 0."
13186 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013187 if ((particle == NULL) || (particle->minOccurs == 0) ||
13188 (particle->children == NULL))
13189 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013190 /*
13191 * SPEC (2) "Its {term} is a group and the minimum part of the
13192 * effective total range of that group, [...] is 0."
13193 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013194 if (IS_MODEL_GROUP(particle->children)) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013195 if (xmlSchemaGetParticleTotalRangeMin(particle) == 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013196 return (1);
13197 }
13198 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000013199}
13200
13201/**
13202 * xmlSchemaCheckCOSSTDerivedOK:
13203 * @type: the derived simple type definition
13204 * @baseType: the base type definition
13205 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013206 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000013207 * Type Derivation OK (Simple) (cos-st-derived-OK)
13208 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013209 * Checks wheter @type can be validly
Daniel Veillard01fa6152004-06-29 17:04:39 +000013210 * derived from @baseType.
13211 *
13212 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013213 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013214static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013215xmlSchemaCheckCOSSTDerivedOK(xmlSchemaTypePtr type,
13216 xmlSchemaTypePtr baseType,
13217 int subset)
13218{
Daniel Veillard01fa6152004-06-29 17:04:39 +000013219 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000013220 * 1 They are the same type definition.
13221 * TODO: The identy check might have to be more complex than this.
13222 */
13223 if (type == baseType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013224 return (0);
13225 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000013226 * 2.1 restriction is not in the subset, or in the {final}
13227 * of its own {base type definition};
13228 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013229 if ((subset & SUBSET_RESTRICTION) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013230 (xmlSchemaTypeFinalContains(type->baseType,
13231 XML_SCHEMAS_TYPE_FINAL_RESTRICTION))) {
13232 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000013233 }
13234 /* 2.2 */
13235 if (type->baseType == baseType) {
13236 /*
13237 * 2.2.1 D's ·base type definition· is B.
13238 */
13239 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013240 }
13241 /*
13242 * 2.2.2 D's ·base type definition· is not the ·ur-type definition·
13243 * and is validly derived from B given the subset, as defined by this
13244 * constraint.
Daniel Veillard01fa6152004-06-29 17:04:39 +000013245 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013246 if ((! IS_ANYTYPE(type->baseType)) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013247 (xmlSchemaCheckCOSSTDerivedOK(type->baseType,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013248 baseType, subset) == 0)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013249 return (0);
13250 }
13251 /*
13252 * 2.2.3 D's {variety} is list or union and B is the ·simple ur-type
Daniel Veillard01fa6152004-06-29 17:04:39 +000013253 * definition·.
13254 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013255 if (IS_ANY_SIMPLE_TYPE(baseType) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013256 (VARIETY_LIST(type) || VARIETY_UNION(type))) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000013257 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013258 }
13259 /*
13260 * 2.2.4 B's {variety} is union and D is validly derived from a type
13261 * definition in B's {member type definitions} given the subset, as
Daniel Veillard01fa6152004-06-29 17:04:39 +000013262 * defined by this constraint.
13263 *
13264 * NOTE: This seems not to involve built-in types, since there is no
13265 * built-in Union Simple Type.
13266 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013267 if (VARIETY_UNION(baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000013268 xmlSchemaTypeLinkPtr cur;
13269
13270 cur = baseType->memberTypes;
13271 while (cur != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013272 if (xmlSchemaCheckCOSSTDerivedOK(type, cur->type, subset) == 0)
Daniel Veillard01fa6152004-06-29 17:04:39 +000013273 return (0);
13274 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013275 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013276 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013277
Daniel Veillard01fa6152004-06-29 17:04:39 +000013278 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_2);
13279}
13280
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013281/**
13282 * xmlSchemaCheckTypeDefCircularInternal:
13283 * @pctxt: the schema parser context
13284 * @ctxtType: the type definition
13285 * @ancestor: an ancestor of @ctxtType
13286 *
13287 * Checks st-props-correct (2) + ct-props-correct (3).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013288 * Circular type definitions are not allowed.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013289 *
13290 * Returns XML_SCHEMAP_ST_PROPS_CORRECT_2 if the given type is
13291 * circular, 0 otherwise.
13292 */
13293static int
13294xmlSchemaCheckTypeDefCircularInternal(xmlSchemaParserCtxtPtr pctxt,
13295 xmlSchemaTypePtr ctxtType,
13296 xmlSchemaTypePtr ancestor)
13297{
13298 int ret;
13299
13300 if ((ancestor == NULL) || (ancestor->type == XML_SCHEMA_TYPE_BASIC))
13301 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013302
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013303 if (ctxtType == ancestor) {
13304 xmlSchemaPCustomErr(pctxt,
13305 XML_SCHEMAP_ST_PROPS_CORRECT_2,
13306 NULL, ctxtType, GET_NODE(ctxtType),
13307 "The definition is circular", NULL);
13308 return (XML_SCHEMAP_ST_PROPS_CORRECT_2);
13309 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013310 if (ancestor->flags & XML_SCHEMAS_TYPE_MARKED) {
13311 /*
13312 * Avoid inifinite recursion on circular types not yet checked.
13313 */
13314 return (0);
13315 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013316 ancestor->flags |= XML_SCHEMAS_TYPE_MARKED;
13317 ret = xmlSchemaCheckTypeDefCircularInternal(pctxt, ctxtType,
13318 ancestor->baseType);
13319 ancestor->flags ^= XML_SCHEMAS_TYPE_MARKED;
13320 return (ret);
13321}
13322
13323/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013324 * xmlSchemaCheckTypeDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013325 * @item: the complex/simple type definition
13326 * @ctxt: the parser context
13327 * @name: the name
13328 *
13329 * Checks for circular type definitions.
13330 */
13331static void
13332xmlSchemaCheckTypeDefCircular(xmlSchemaTypePtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013333 xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013334 const xmlChar * name ATTRIBUTE_UNUSED)
13335{
13336 if ((item == NULL) ||
13337 ((item->type != XML_SCHEMA_TYPE_COMPLEX) &&
13338 (item->type != XML_SCHEMA_TYPE_SIMPLE)))
13339 return;
13340 xmlSchemaCheckTypeDefCircularInternal(ctxt, item, item->baseType);
13341
13342}
Daniel Veillard01fa6152004-06-29 17:04:39 +000013343
13344/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013345 * xmlSchemaResolveTypeDefs:
13346 * @item: the complex/simple type definition
13347 * @ctxt: the parser context
13348 * @name: the name
13349 *
13350 * Checks for circular type definitions.
13351 */
13352static void
13353xmlSchemaResolveTypeDefs(xmlSchemaTypePtr typeDef,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013354 xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013355 const xmlChar * name ATTRIBUTE_UNUSED)
13356{
13357 if (typeDef == NULL)
13358 return;
13359
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013360 /*
13361 * Resolve the base type.
13362 */
13363 if (typeDef->baseType == NULL) {
13364 typeDef->baseType = xmlSchemaGetType(ctxt->schema,
13365 typeDef->base, typeDef->baseNs);
13366 if (typeDef->baseType == NULL) {
13367 xmlSchemaPResCompAttrErr(ctxt,
13368 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013369 typeDef, typeDef->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013370 "base", typeDef->base, typeDef->baseNs,
13371 XML_SCHEMA_TYPE_SIMPLE, NULL);
13372 return;
13373 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013374 }
13375 if (IS_SIMPLE_TYPE(typeDef)) {
13376 if (VARIETY_UNION(typeDef)) {
13377 /*
13378 * Resolve the memberTypes.
13379 */
13380 xmlSchemaResolveUnionMemberTypes(ctxt, typeDef);
13381 return;
13382 } else if (VARIETY_LIST(typeDef)) {
13383 /*
13384 * Resolve the itemType.
13385 */
13386 if ((typeDef->subtypes == NULL) && (typeDef->ref != NULL)) {
13387 typeDef->subtypes = xmlSchemaGetType(ctxt->schema,
13388 typeDef->ref, typeDef->refNs);
13389 if ((typeDef->subtypes == NULL) ||
13390 (! IS_SIMPLE_TYPE(typeDef->subtypes))) {
13391 typeDef->subtypes = NULL;
13392 xmlSchemaPResCompAttrErr(ctxt,
13393 XML_SCHEMAP_SRC_RESOLVE,
13394 typeDef, typeDef->node,
13395 "itemType", typeDef->ref, typeDef->refNs,
13396 XML_SCHEMA_TYPE_SIMPLE, NULL);
13397 }
13398 }
13399 return;
13400 }
13401 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013402}
13403
13404
13405
13406/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000013407 * xmlSchemaCheckSTPropsCorrect:
13408 * @ctxt: the schema parser context
13409 * @type: the simple type definition
13410 *
13411 * Checks st-props-correct.
13412 *
13413 * Returns 0 if the properties are correct,
13414 * if not, a positive error code and -1 on internal
13415 * errors.
13416 */
13417static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013418xmlSchemaCheckSTPropsCorrect(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013419 xmlSchemaTypePtr type)
13420{
13421 xmlSchemaTypePtr baseType = type->baseType, anySimpleType,
13422 anyType;
Daniel Veillardc0826a72004-08-10 14:17:33 +000013423 xmlChar *str = NULL;
Daniel Veillard01fa6152004-06-29 17:04:39 +000013424
Daniel Veillardc0826a72004-08-10 14:17:33 +000013425 /* STATE: error funcs converted. */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013426 /*
13427 * Schema Component Constraint: Simple Type Definition Properties Correct
13428 *
13429 * NOTE: This is somehow redundant, since we actually built a simple type
13430 * to have all the needed information; this acts as an self test.
13431 */
13432 anySimpleType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
13433 anyType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013434 /* Base type: If the datatype has been ·derived· by ·restriction·
13435 * then the Simple Type Definition component from which it is ·derived·,
13436 * otherwise the Simple Type Definition for anySimpleType (§4.1.6).
Daniel Veillard01fa6152004-06-29 17:04:39 +000013437 */
13438 if (baseType == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013439 /*
13440 * TODO: Think about: "modulo the impact of Missing
13441 * Sub-components (§5.3)."
13442 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000013443 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013444 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013445 NULL, type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013446 "No base type existent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000013447 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013448
Daniel Veillard01fa6152004-06-29 17:04:39 +000013449 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013450 if (! IS_SIMPLE_TYPE(baseType)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000013451 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013452 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013453 NULL, type, NULL,
13454 "The base type '%s' is not a simple type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013455 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013456 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000013457 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
13458 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013459 if ( (VARIETY_LIST(type) || VARIETY_UNION(type)) &&
13460 ((type->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION) == 0) &&
13461 (! IS_ANY_SIMPLE_TYPE(baseType))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000013462 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013463 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013464 NULL, type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013465 "A type, derived by list or union, must have"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013466 "the simple ur-type definition as base type, not '%s'",
13467 xmlSchemaGetComponentQName(&str, baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000013468 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000013469 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
13470 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013471 /*
13472 * Variety: One of {atomic, list, union}.
Daniel Veillard01fa6152004-06-29 17:04:39 +000013473 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013474 if ((! VARIETY_ATOMIC(type)) && (! VARIETY_UNION(type)) &&
13475 (! VARIETY_LIST(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000013476 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013477 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013478 NULL, type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013479 "The variety is absent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000013480 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
13481 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000013482 /* TODO: Finish this. Hmm, is this finished? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013483
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013484 /*
13485 * 3 The {final} of the {base type definition} must not contain restriction.
13486 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013487 if (xmlSchemaTypeFinalContains(baseType,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013488 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
13489 xmlSchemaPCustomErr(ctxt,
13490 XML_SCHEMAP_ST_PROPS_CORRECT_3,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013491 NULL, type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013492 "The 'final' of its base type '%s' must not contain "
13493 "'restriction'",
13494 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013495 FREE_AND_NULL(str)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013496 return (XML_SCHEMAP_ST_PROPS_CORRECT_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013497 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013498
13499 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013500 * 2 All simple type definitions must be derived ultimately from the ·simple
13501 * ur-type definition (so· circular definitions are disallowed). That is, it
13502 * must be possible to reach a built-in primitive datatype or the ·simple
13503 * ur-type definition· by repeatedly following the {base type definition}.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013504 *
13505 * NOTE: this is done in xmlSchemaCheckTypeDefCircular().
Daniel Veillard01fa6152004-06-29 17:04:39 +000013506 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013507 return (0);
13508}
13509
13510/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013511 * xmlSchemaCheckCOSSTRestricts:
Daniel Veillard01fa6152004-06-29 17:04:39 +000013512 * @ctxt: the schema parser context
13513 * @type: the simple type definition
13514 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000013515 * Schema Component Constraint:
13516 * Derivation Valid (Restriction, Simple) (cos-st-restricts)
13517
13518 * Checks if the given @type (simpleType) is derived validly by restriction.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013519 * STATUS:
Daniel Veillard01fa6152004-06-29 17:04:39 +000013520 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013521 * Returns -1 on internal errors, 0 if the type is validly derived,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013522 * a positive error code otherwise.
13523 */
13524static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013525xmlSchemaCheckCOSSTRestricts(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013526 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013527{
Daniel Veillardc0826a72004-08-10 14:17:33 +000013528 xmlChar *str = NULL;
13529
Daniel Veillard01fa6152004-06-29 17:04:39 +000013530 if (type->type != XML_SCHEMA_TYPE_SIMPLE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013531 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
13532 "given type is not a user-derived simpleType");
Daniel Veillard01fa6152004-06-29 17:04:39 +000013533 return (-1);
13534 }
13535
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013536 if (VARIETY_ATOMIC(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000013537 xmlSchemaTypePtr primitive;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013538 /*
13539 * 1.1 The {base type definition} must be an atomic simple
Daniel Veillard01fa6152004-06-29 17:04:39 +000013540 * type definition or a built-in primitive datatype.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013541 */
13542 if (! VARIETY_ATOMIC(type->baseType)) {
13543 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013544 XML_SCHEMAP_COS_ST_RESTRICTS_1_1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013545 NULL, type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013546 "The base type '%s' is not an atomic simple type",
13547 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000013548 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000013549 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_1);
13550 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013551 /* 1.2 The {final} of the {base type definition} must not contain
Daniel Veillard01fa6152004-06-29 17:04:39 +000013552 * restriction.
13553 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000013554 /* OPTIMIZE TODO : This is already done in xmlSchemaCheckStPropsCorrect */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013555 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013556 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013557 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013558 XML_SCHEMAP_COS_ST_RESTRICTS_1_2,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013559 NULL, type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013560 "The final of its base type '%s' must not contain 'restriction'",
13561 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000013562 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000013563 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_2);
13564 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013565
13566 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000013567 * 1.3.1 DF must be an allowed constraining facet for the {primitive
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013568 * type definition}, as specified in the appropriate subsection of 3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000013569 * Primitive datatypes.
13570 */
13571 if (type->facets != NULL) {
13572 xmlSchemaFacetPtr facet;
13573 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013574
Daniel Veillard01fa6152004-06-29 17:04:39 +000013575 primitive = xmlSchemaGetPrimitiveType(type);
13576 if (primitive == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013577 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
13578 "failed to get primitive type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000013579 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013580 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013581 facet = type->facets;
13582 do {
13583 if (xmlSchemaIsBuiltInTypeFacet(primitive, facet->type) == 0) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000013584 ok = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013585 xmlSchemaPIllegalFacetAtomicErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013586 XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013587 NULL, type, primitive, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000013588 }
13589 facet = facet->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013590 } while (facet != NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000013591 if (ok == 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013592 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000013593 }
13594 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000013595 * SPEC (1.3.2) "If there is a facet of the same kind in the {facets}
13596 * of the {base type definition} (call this BF),then the DF's {value}
13597 * must be a valid restriction of BF's {value} as defined in
13598 * [XML Schemas: Datatypes]."
13599 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013600 * NOTE (1.3.2) Facet derivation constraints are currently handled in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000013601 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000013602 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013603 } else if (VARIETY_LIST(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000013604 xmlSchemaTypePtr itemType = NULL;
13605
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013606 itemType = type->subtypes;
13607 if ((itemType == NULL) || (! IS_SIMPLE_TYPE(itemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013608 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
13609 "failed to evaluate the item type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000013610 return (-1);
13611 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013612 if (IS_NOT_TYPEFIXED(itemType))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013613 xmlSchemaTypeFixup(itemType, pctxt, NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000013614 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013615 * 2.1 The {item type definition} must have a {variety} of atomic or
13616 * union (in which case all the {member type definitions}
Daniel Veillard01fa6152004-06-29 17:04:39 +000013617 * must be atomic).
13618 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013619 if ((! VARIETY_ATOMIC(itemType)) &&
13620 (! VARIETY_UNION(itemType))) {
13621 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013622 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013623 NULL, type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013624 "The item type '%s' does not have a variety of atomic or union",
13625 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013626 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000013627 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013628 } else if (VARIETY_UNION(itemType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000013629 xmlSchemaTypeLinkPtr member;
13630
13631 member = itemType->memberTypes;
13632 while (member != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013633 if (! VARIETY_ATOMIC(member->type)) {
13634 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013635 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013636 NULL, type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013637 "The item type is a union type, but the "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013638 "member type '%s' of this item type is not atomic",
13639 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013640 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000013641 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
13642 }
13643 member = member->next;
13644 }
13645 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013646
13647 if (IS_ANY_SIMPLE_TYPE(type->baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000013648 xmlSchemaFacetPtr facet;
13649 /*
13650 * This is the case if we have: <simpleType><list ..
13651 */
13652 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013653 * 2.3.1
13654 * 2.3.1.1 The {final} of the {item type definition} must not
Daniel Veillard01fa6152004-06-29 17:04:39 +000013655 * contain list.
13656 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013657 if (xmlSchemaTypeFinalContains(itemType,
13658 XML_SCHEMAS_TYPE_FINAL_LIST)) {
13659 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013660 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013661 NULL, type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013662 "The final of its item type '%s' must not contain 'list'",
13663 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013664 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000013665 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1);
13666 }
13667 /*
13668 * 2.3.1.2 The {facets} must only contain the whiteSpace
13669 * facet component.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013670 * OPTIMIZE TODO: the S4S already disallows any facet
13671 * to be specified.
Daniel Veillard01fa6152004-06-29 17:04:39 +000013672 */
13673 if (type->facets != NULL) {
13674 facet = type->facets;
13675 do {
Daniel Veillardc0826a72004-08-10 14:17:33 +000013676 if (facet->type != XML_SCHEMA_FACET_WHITESPACE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013677 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013678 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013679 NULL, type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000013680 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2);
13681 }
13682 facet = facet->next;
13683 } while (facet != NULL);
13684 }
13685 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013686 * MAYBE TODO: (Hmm, not really) Datatypes states:
13687 * A ·list· datatype can be ·derived· from an ·atomic· datatype
13688 * whose ·lexical space· allows space (such as string or anyURI)or
13689 * a ·union· datatype any of whose {member type definitions}'s
Daniel Veillard01fa6152004-06-29 17:04:39 +000013690 * ·lexical space· allows space.
13691 */
13692 } else {
13693 /*
13694 * This is the case if we have: <simpleType><restriction ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013695 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000013696 */
13697 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013698 * 2.3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000013699 * 2.3.2.1 The {base type definition} must have a {variety} of list.
13700 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013701 if (! VARIETY_LIST(type->baseType)) {
13702 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013703 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013704 NULL, type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013705 "The base type '%s' must be a list type",
13706 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013707 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000013708 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1);
13709 }
13710 /*
13711 * 2.3.2.2 The {final} of the {base type definition} must not
13712 * contain restriction.
13713 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013714 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013715 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013716 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013717 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013718 NULL, type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013719 "The 'final' of the base type '%s' must not contain 'restriction'",
13720 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013721 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000013722 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2);
13723 }
13724 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013725 * 2.3.2.3 The {item type definition} must be validly derived
Daniel Veillard01fa6152004-06-29 17:04:39 +000013726 * from the {base type definition}'s {item type definition} given
13727 * the empty set, as defined in Type Derivation OK (Simple) (§3.14.6).
13728 */
13729 {
13730 xmlSchemaTypePtr baseItemType;
13731
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013732 baseItemType = type->baseType->subtypes;
13733 if ((baseItemType == NULL) || (! IS_SIMPLE_TYPE(baseItemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013734 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
13735 "failed to eval the item type of a base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000013736 return (-1);
13737 }
13738 if ((itemType != baseItemType) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013739 (xmlSchemaCheckCOSSTDerivedOK(itemType,
13740 baseItemType, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000013741 xmlChar *strBIT = NULL, *strBT = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013742 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013743 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013744 NULL, type, NULL,
13745 "The item type '%s' is not validly derived from "
13746 "the item type '%s' of the base type '%s'",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013747 xmlSchemaGetComponentQName(&str, itemType),
13748 xmlSchemaGetComponentQName(&strBIT, baseItemType),
13749 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000013750
13751 FREE_AND_NULL(str)
13752 FREE_AND_NULL(strBIT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013753 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000013754 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3);
13755 }
13756 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013757
Daniel Veillard01fa6152004-06-29 17:04:39 +000013758 if (type->facets != NULL) {
13759 xmlSchemaFacetPtr facet;
13760 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013761 /*
13762 * 2.3.2.4 Only length, minLength, maxLength, whiteSpace, pattern
Daniel Veillard01fa6152004-06-29 17:04:39 +000013763 * and enumeration facet components are allowed among the {facets}.
13764 */
13765 facet = type->facets;
13766 do {
13767 switch (facet->type) {
13768 case XML_SCHEMA_FACET_LENGTH:
13769 case XML_SCHEMA_FACET_MINLENGTH:
13770 case XML_SCHEMA_FACET_MAXLENGTH:
13771 case XML_SCHEMA_FACET_WHITESPACE:
13772 /*
13773 * TODO: 2.5.1.2 List datatypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013774 * The value of ·whiteSpace· is fixed to the value collapse.
Daniel Veillard01fa6152004-06-29 17:04:39 +000013775 */
13776 case XML_SCHEMA_FACET_PATTERN:
13777 case XML_SCHEMA_FACET_ENUMERATION:
13778 break;
13779 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013780 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013781 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013782 NULL, type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000013783 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013784 * We could return, but it's nicer to report all
Daniel Veillard01fa6152004-06-29 17:04:39 +000013785 * invalid facets.
13786 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013787 ok = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000013788 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013789 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013790 facet = facet->next;
13791 } while (facet != NULL);
13792 if (ok == 0)
13793 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4);
13794 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000013795 * SPEC (2.3.2.5) (same as 1.3.2)
13796 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013797 * NOTE (2.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000013798 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000013799 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013800 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013801 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013802 } else if (VARIETY_UNION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000013803 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013804 * 3.1 The {member type definitions} must all have {variety} of
Daniel Veillard01fa6152004-06-29 17:04:39 +000013805 * atomic or list.
13806 */
13807 xmlSchemaTypeLinkPtr member;
13808
13809 member = type->memberTypes;
13810 while (member != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013811 if (IS_NOT_TYPEFIXED(member->type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013812 xmlSchemaTypeFixup(member->type, pctxt, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013813
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013814 if ((! VARIETY_ATOMIC(member->type)) &&
13815 (! VARIETY_LIST(member->type))) {
13816 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013817 XML_SCHEMAP_COS_ST_RESTRICTS_3_1,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013818 NULL, type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013819 "The member type '%s' is neither an atomic, nor a list type",
13820 xmlSchemaGetComponentQName(&str, member->type));
Daniel Veillardc0826a72004-08-10 14:17:33 +000013821 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000013822 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_1);
13823 }
13824 member = member->next;
13825 }
13826 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013827 * 3.3.1 If the {base type definition} is the ·simple ur-type
13828 * definition·
Daniel Veillard01fa6152004-06-29 17:04:39 +000013829 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013830 if (type->baseType->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000013831 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013832 * 3.3.1.1 All of the {member type definitions} must have a
Daniel Veillard01fa6152004-06-29 17:04:39 +000013833 * {final} which does not contain union.
13834 */
13835 member = type->memberTypes;
13836 while (member != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013837 if (xmlSchemaTypeFinalContains(member->type,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013838 XML_SCHEMAS_TYPE_FINAL_UNION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013839 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013840 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013841 NULL, type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013842 "The 'final' of member type '%s' contains 'union'",
13843 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013844 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000013845 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1);
13846 }
13847 member = member->next;
13848 }
13849 /*
13850 * 3.3.1.2 The {facets} must be empty.
13851 */
13852 if (type->facetSet != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013853 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013854 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013855 NULL, type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013856 "No facets allowed", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000013857 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2);
13858 }
13859 } else {
13860 /*
13861 * 3.3.2.1 The {base type definition} must have a {variety} of union.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013862 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000013863 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013864 if (! VARIETY_UNION(type->baseType)) {
13865 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013866 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013867 NULL, type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013868 "The base type '%s' is not a union type",
13869 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013870 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000013871 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1);
13872 }
13873 /*
13874 * 3.3.2.2 The {final} of the {base type definition} must not contain restriction.
13875 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013876 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013877 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013878 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013879 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013880 NULL, type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013881 "The 'final' of its base type '%s' must not contain 'restriction'",
13882 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013883 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000013884 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2);
13885 }
13886 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013887 * 3.3.2.3 The {member type definitions}, in order, must be validly
13888 * derived from the corresponding type definitions in the {base
13889 * type definition}'s {member type definitions} given the empty set,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013890 * as defined in Type Derivation OK (Simple) (§3.14.6).
13891 */
13892 {
13893 xmlSchemaTypeLinkPtr baseMember;
13894
13895 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013896 * OPTIMIZE: if the type is restricting, it has no local defined
13897 * member types and inherits the member types of the base type;
Daniel Veillard01fa6152004-06-29 17:04:39 +000013898 * thus a check for equality can be skipped.
13899 */
13900 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000013901 * Even worse: I cannot see a scenario where a restricting
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013902 * union simple type can have other member types as the member
Daniel Veillard01fa6152004-06-29 17:04:39 +000013903 * types of it's base type. This check seems not necessary with
13904 * respect to the derivation process in libxml2.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000013905 * But necessary if constructing types with an API.
Daniel Veillard01fa6152004-06-29 17:04:39 +000013906 */
13907 if (type->memberTypes != NULL) {
13908 member = type->memberTypes;
13909 baseMember = xmlSchemaGetUnionSimpleTypeMemberTypes(type->baseType);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013910 if ((member == NULL) && (baseMember != NULL)) {
13911 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
13912 "different number of member types in base");
13913 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013914 while (member != NULL) {
13915 if (baseMember == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013916 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
13917 "different number of member types in base");
Daniel Veillard01fa6152004-06-29 17:04:39 +000013918 }
13919 if ((member->type != baseMember->type) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013920 (xmlSchemaCheckCOSSTDerivedOK(
13921 member->type, baseMember->type, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000013922 xmlChar *strBMT = NULL, *strBT = NULL;
13923
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013924 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013925 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3,
13926 NULL, type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013927 "The member type %s is not validly "
13928 "derived from its corresponding member "
13929 "type %s of the base type %s",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013930 xmlSchemaGetComponentQName(&str, member->type),
13931 xmlSchemaGetComponentQName(&strBMT, baseMember->type),
13932 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000013933 FREE_AND_NULL(str)
13934 FREE_AND_NULL(strBMT)
13935 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000013936 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013937 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013938 member = member->next;
13939 baseMember = baseMember->next;
13940 }
13941 }
13942 }
13943 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013944 * 3.3.2.4 Only pattern and enumeration facet components are
Daniel Veillard01fa6152004-06-29 17:04:39 +000013945 * allowed among the {facets}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013946 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013947 if (type->facets != NULL) {
13948 xmlSchemaFacetPtr facet;
13949 int ok = 1;
13950
13951 facet = type->facets;
13952 do {
13953 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
13954 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013955 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013956 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013957 NULL, type, facet);
13958 ok = 0;
13959 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013960 facet = facet->next;
13961 } while (facet != NULL);
13962 if (ok == 0)
13963 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013964
Daniel Veillard01fa6152004-06-29 17:04:39 +000013965 }
13966 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000013967 * SPEC (3.3.2.5) (same as 1.3.2)
13968 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013969 * NOTE (3.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000013970 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000013971 */
13972 }
13973 }
13974
13975 return (0);
13976}
13977
13978/**
13979 * xmlSchemaCheckSRCSimpleType:
13980 * @ctxt: the schema parser context
13981 * @type: the simple type definition
13982 *
13983 * Checks crc-simple-type constraints.
13984 *
13985 * Returns 0 if the constraints are satisfied,
13986 * if not a positive error code and -1 on internal
13987 * errors.
13988 */
13989static int
13990xmlSchemaCheckSRCSimpleType(xmlSchemaParserCtxtPtr ctxt,
13991 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013992{
13993 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000013994 * src-simple-type.1 The corresponding simple type definition, if any,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013995 * must satisfy the conditions set out in Constraints on Simple Type
13996 * Definition Schema Components (§3.14.6).
Daniel Veillard01fa6152004-06-29 17:04:39 +000013997 */
13998 if ((xmlSchemaCheckSTPropsCorrect(ctxt, type) != 0) ||
13999 (xmlSchemaCheckCOSSTRestricts(ctxt, type) != 0)) {
14000 /*
14001 * TODO: Removed this, since it got annoying to get an
14002 * extra error report, if anything failed until now.
14003 * Enable this if needed.
14004 */
14005 /*
14006 xmlSchemaPErr(ctxt, type->node,
14007 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Daniel Veillardc0826a72004-08-10 14:17:33 +000014008 "Simple type '%s' does not satisfy the constraints "
Daniel Veillard01fa6152004-06-29 17:04:39 +000014009 "on simple type definitions.\n",
14010 type->name, NULL);
14011 */
14012 return (XML_SCHEMAP_SRC_SIMPLE_TYPE_1);
14013 }
14014
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014015 if (type->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014016 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014017 * src-simple-type.2 If the <restriction> alternative is chosen,
14018 * either it must have a base [attribute] or a <simpleType> among its
Daniel Veillard01fa6152004-06-29 17:04:39 +000014019 * [children], but not both.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014020 */
William M. Brack2f2a6632004-08-20 23:09:47 +000014021 /*
14022 * XML_SCHEMAP_SRC_SIMPLE_TYPE_2
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014023 * NOTE: This is checked in the parse function of <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014024 */
14025 } else if (VARIETY_LIST(type)) {
14026 /* src-simple-type.3 If the <list> alternative is chosen, either it must have
14027 * an itemType [attribute] or a <simpleType> among its [children],
Daniel Veillard01fa6152004-06-29 17:04:39 +000014028 * but not both.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014029 *
14030 * REMOVED: This is checked in the parse function of <list>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014031 */
14032 } else if (VARIETY_UNION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014033 xmlSchemaTypeLinkPtr member;
14034 xmlSchemaTypePtr ancestor, anySimpleType;
14035
14036 anySimpleType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
14037
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014038 /* src-simple-type.4 Circular union type definition is disallowed. That is, if
14039 * the <union> alternative is chosen, there must not be any entries
14040 * in the memberTypes [attribute] at any depth which resolve to the
Daniel Veillard01fa6152004-06-29 17:04:39 +000014041 * component corresponding to the <simpleType>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014042 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014043 member = type->memberTypes;
14044 while (member != NULL) {
14045 ancestor = member->type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014046 while ((ancestor != NULL) && (ancestor->type != XML_SCHEMA_TYPE_BASIC)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014047 if (ancestor == type) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014048 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000014049 XML_SCHEMAP_SRC_SIMPLE_TYPE_4,
Daniel Veillardc0826a72004-08-10 14:17:33 +000014050 NULL, type, NULL,
14051 "The definition is circular", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014052 return (XML_SCHEMAP_SRC_SIMPLE_TYPE_4);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014053 }
14054 if (IS_NOT_TYPEFIXED(ancestor))
14055 xmlSchemaTypeFixup(ancestor, ctxt, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014056 if (VARIETY_LIST(ancestor)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014057 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +000014058 * TODO, FIXME: Although a list simple type must not have a union ST
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014059 * type as item type, which in turn has a list ST as member
14060 * type, we will assume this here as well, since this check
Daniel Veillard01fa6152004-06-29 17:04:39 +000014061 * was not yet performed.
14062 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014063 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014064
Daniel Veillard01fa6152004-06-29 17:04:39 +000014065 ancestor = ancestor->baseType;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014066 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014067 member = member->next;
14068 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014069 }
14070
14071 return (0);
14072}
14073
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014074static int
14075xmlSchemaCreateVCtxtOnPCtxt(xmlSchemaParserCtxtPtr ctxt)
14076{
14077 if (ctxt->vctxt == NULL) {
14078 ctxt->vctxt = xmlSchemaNewValidCtxt(NULL);
14079 if (ctxt->vctxt == NULL) {
14080 xmlSchemaPErr(ctxt, NULL,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000014081 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014082 "Internal error: xmlSchemaCreateVCtxtOnPCtxt, "
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014083 "failed to create a temp. validation context.\n",
14084 NULL, NULL);
14085 return (-1);
14086 }
14087 /* TODO: Pass user data. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014088 xmlSchemaSetValidErrors(ctxt->vctxt, ctxt->error, ctxt->warning, NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014089 }
14090 return (0);
14091}
14092
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014093static int
14094xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
14095 xmlNodePtr node,
14096 xmlSchemaTypePtr type,
14097 const xmlChar *value,
14098 xmlSchemaValPtr *retVal,
14099 int fireErrors,
14100 int normalize,
14101 int isNormalized);
14102
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014103/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014104 * xmlSchemaParseCheckCOSValidDefault:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014105 * @pctxt: the schema parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014106 * @type: the simple type definition
14107 * @value: the default value
14108 * @node: an optional node (the holder of the value)
14109 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014110 * Schema Component Constraint: Element Default Valid (Immediate)
14111 * (cos-valid-default)
14112 * This will be used by the parser only. For the validator there's
14113 * an other version.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014114 *
14115 * Returns 0 if the constraints are satisfied,
14116 * if not, a positive error code and -1 on internal
14117 * errors.
14118 */
14119static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014120xmlSchemaParseCheckCOSValidDefault(xmlSchemaParserCtxtPtr pctxt,
14121 xmlNodePtr node,
14122 xmlSchemaTypePtr type,
14123 const xmlChar *value,
14124 xmlSchemaValPtr *val)
14125{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014126 int ret = 0;
14127
14128 /*
14129 * cos-valid-default:
14130 * Schema Component Constraint: Element Default Valid (Immediate)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014131 * For a string to be a valid default with respect to a type
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014132 * definition the appropriate case among the following must be true:
14133 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014134 if IS_COMPLEX_TYPE(type) {
William M. Brack2f2a6632004-08-20 23:09:47 +000014135 /*
14136 * Complex type.
14137 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014138 * SPEC (2.1) "its {content type} must be a simple type definition
14139 * or mixed."
14140 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014141 * type}'s particle must be ·emptiable· as defined by
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014142 * Particle Emptiable (§3.9.6)."
William M. Brack2f2a6632004-08-20 23:09:47 +000014143 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014144 if ((! HAS_SIMPLE_CONTENT(type)) &&
14145 ((! HAS_MIXED_CONTENT(type)) || (! IS_PARTICLE_EMPTIABLE(type)))) {
14146 /* NOTE that this covers (2.2.2) as well. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014147 xmlSchemaPCustomErr(pctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014148 XML_SCHEMAP_COS_VALID_DEFAULT_2_1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014149 NULL, type, type->node,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014150 "For a string to be a valid default, the type definition "
14151 "must be a simple type or a complex type with mixed content "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014152 "and a particle emptiable", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014153 return(XML_SCHEMAP_COS_VALID_DEFAULT_2_1);
14154 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014155 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014156 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014157 * 1 If the type definition is a simple type definition, then the string
14158 * must be ·valid· with respect to that definition as defined by String
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014159 * Valid (§3.14.4).
14160 *
14161 * AND
14162 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014163 * 2.2.1 If the {content type} is a simple type definition, then the
14164 * string must be ·valid· with respect to that simple type definition
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014165 * as defined by String Valid (§3.14.4).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014166 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014167 if (IS_SIMPLE_TYPE(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014168 ret = xmlSchemaVCheckCVCSimpleType((xmlSchemaAbstractCtxtPtr) pctxt, node,
14169 type, value, val, 1, 1, 0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014170 else if (HAS_SIMPLE_CONTENT(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014171 ret = xmlSchemaVCheckCVCSimpleType((xmlSchemaAbstractCtxtPtr) pctxt, node,
14172 type->contentTypeDef, value, val, 1, 1, 0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014173 else
14174 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014175
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014176 if (ret < 0) {
14177 PERROR_INT("xmlSchemaParseCheckCOSValidDefault",
14178 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014179 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014180
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014181 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +000014182}
14183
14184/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014185 * xmlSchemaCheckCTPropsCorrect:
William M. Brack2f2a6632004-08-20 23:09:47 +000014186 * @ctxt: the schema parser context
14187 * @type: the complex type definition
14188 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014189 *.(4.6) Constraints on Complex Type Definition Schema Components
14190 * Schema Component Constraint:
14191 * Complex Type Definition Properties Correct (ct-props-correct)
14192 * STATUS: (seems) complete
William M. Brack2f2a6632004-08-20 23:09:47 +000014193 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014194 * Returns 0 if the constraints are satisfied, a positive
14195 * error code if not and -1 if an internal error occured.
William M. Brack2f2a6632004-08-20 23:09:47 +000014196 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014197static int
14198xmlSchemaCheckCTPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
14199 xmlSchemaTypePtr type)
William M. Brack2f2a6632004-08-20 23:09:47 +000014200{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014201 /*
14202 * TODO: Correct the error code; XML_SCHEMAP_SRC_CT_1 is used temporarily.
14203 *
14204 * SPEC (1) "The values of the properties of a complex type definition must
14205 * be as described in the property tableau in The Complex Type Definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014206 * Schema Component (§3.4.1), modulo the impact of Missing
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014207 * Sub-components (§5.3)."
14208 */
14209 if ((type->baseType != NULL) &&
14210 (IS_SIMPLE_TYPE(type->baseType)) &&
14211 ((type->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION) == 0)) {
14212 /*
14213 * SPEC (2) "If the {base type definition} is a simple type definition,
14214 * the {derivation method} must be extension."
14215 */
14216 xmlSchemaPCustomErr(pctxt,
14217 XML_SCHEMAP_SRC_CT_1,
14218 NULL, type, NULL,
14219 "If the base type is a simple type, the derivation method must be "
14220 "'extension'", NULL);
14221 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014222 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014223 /*
14224 * SPEC (3) "Circular definitions are disallowed, except for the ·ur-type
14225 * definition·. That is, it must be possible to reach the ·ur-type
14226 * definition by repeatedly following the {base type definition}."
14227 *
14228 * NOTE (3) is done in xmlSchemaCheckTypeDefCircular().
14229 *
14230 * SPEC (4) "Two distinct attribute declarations in the {attribute uses}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014231 * must not have identical {name}s and {target namespace}s."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014232 * SPEC (5) "Two distinct attribute declarations in the {attribute uses}
14233 * must not have {type definition}s which are or are derived from ID."
14234 *
14235 * NOTE (4) and (5) are done in xmlSchemaBuildAttributeValidation().
14236 */
14237 return (0);
William M. Brack2f2a6632004-08-20 23:09:47 +000014238}
14239
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014240static int
14241xmlSchemaAreEqualTypes(xmlSchemaTypePtr typeA,
14242 xmlSchemaTypePtr typeB)
14243{
14244 /*
14245 * TODO: This should implement component-identity
14246 * in the future.
14247 */
14248 if ((typeA == NULL) || (typeB == NULL))
14249 return (0);
14250 return (typeA == typeB);
14251}
14252
14253/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000014254 * xmlSchemaCheckCOSCTDerivedOK:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014255 * @ctxt: the schema parser context
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000014256 * @type: the to-be derived complex type definition
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014257 * @baseType: the base complex type definition
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000014258 * @set: the given set
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014259 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014260 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014261 * Type Derivation OK (Complex) (cos-ct-derived-ok)
14262 *
14263 * STATUS: completed
14264 *
14265 * Returns 0 if the constraints are satisfied, or 1
14266 * if not.
14267 */
14268static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014269xmlSchemaCheckCOSCTDerivedOK(xmlSchemaTypePtr type,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014270 xmlSchemaTypePtr baseType,
14271 int set)
14272{
14273 int equal = xmlSchemaAreEqualTypes(type, baseType);
14274 /* TODO: Error codes. */
14275 /*
14276 * SPEC "For a complex type definition (call it D, for derived)
14277 * to be validly derived from a type definition (call this
14278 * B, for base) given a subset of {extension, restriction}
14279 * all of the following must be true:"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014280 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014281 if (! equal) {
14282 /*
14283 * SPEC (1) "If B and D are not the same type definition, then the
14284 * {derivation method} of D must not be in the subset."
14285 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014286 if (((set & SUBSET_EXTENSION) &&
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014287 (type->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION)) ||
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014288 ((set & SUBSET_RESTRICTION) &&
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014289 (type->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION)))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014290 return (1);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014291 } else {
14292 /*
14293 * SPEC (2.1) "B and D must be the same type definition."
14294 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014295 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014296 }
14297 /*
14298 * SPEC (2.2) "B must be D's {base type definition}."
14299 */
14300 if (type->baseType == baseType)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014301 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014302 /*
14303 * SPEC (2.3.1) "D's {base type definition} must not be the ·ur-type
14304 * definition·."
14305 */
14306 if (IS_ANYTYPE(type->baseType))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014307 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014308
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014309 if (IS_COMPLEX_TYPE(type->baseType)) {
14310 /*
14311 * SPEC (2.3.2.1) "If D's {base type definition} is complex, then it
14312 * must be validly derived from B given the subset as defined by this
14313 * constraint."
14314 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014315 return (xmlSchemaCheckCOSCTDerivedOK(type->baseType,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014316 baseType, set));
14317 } else {
14318 /*
14319 * SPEC (2.3.2.2) "If D's {base type definition} is simple, then it
14320 * must be validly derived from B given the subset as defined in Type
14321 * Derivation OK (Simple) (§3.14.6).
14322 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014323 return (xmlSchemaCheckCOSSTDerivedOK(type->baseType, baseType, set));
14324 }
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014325}
14326
14327/**
14328 * xmlSchemaCheckCOSDerivedOK:
14329 * @type: the derived simple type definition
14330 * @baseType: the base type definition
14331 *
14332 * Calls:
14333 * Type Derivation OK (Simple) AND Type Derivation OK (Complex)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014334 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014335 * Checks wheter @type can be validly derived from @baseType.
14336 *
14337 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014338 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014339static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014340xmlSchemaCheckCOSDerivedOK(xmlSchemaTypePtr type,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014341 xmlSchemaTypePtr baseType,
14342 int set)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014343{
14344 if (IS_SIMPLE_TYPE(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014345 return (xmlSchemaCheckCOSSTDerivedOK(type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014346 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014347 return (xmlSchemaCheckCOSCTDerivedOK(type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014348}
14349
William M. Brack2f2a6632004-08-20 23:09:47 +000014350/**
14351 * xmlSchemaCheckCOSCTExtends:
14352 * @ctxt: the schema parser context
14353 * @type: the complex type definition
14354 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014355 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014356 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014357 * Derivation Valid (Extension) (cos-ct-extends)
14358 *
14359 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014360 * missing:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014361 * (1.5)
14362 * (1.4.3.2.2.2) "Particle Valid (Extension)", which is not really needed.
William M. Brack2f2a6632004-08-20 23:09:47 +000014363 *
14364 * Returns 0 if the constraints are satisfied, a positive
14365 * error code if not and -1 if an internal error occured.
14366 */
14367static int
14368xmlSchemaCheckCOSCTExtends(xmlSchemaParserCtxtPtr ctxt,
14369 xmlSchemaTypePtr type)
14370{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014371 xmlSchemaTypePtr base = type->baseType;
14372 /*
14373 * TODO: Correct the error code; XML_SCHEMAP_COS_CT_EXTENDS_1_1 is used
14374 * temporarily only.
William M. Brack2f2a6632004-08-20 23:09:47 +000014375 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014376 /*
14377 * SPEC (1) "If the {base type definition} is a complex type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014378 * then all of the following must be true:"
14379 */
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +000014380 if (IS_COMPLEX_TYPE(base)) {
William M. Brack2f2a6632004-08-20 23:09:47 +000014381 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014382 * SPEC (1.1) "The {final} of the {base type definition} must not
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014383 * contain extension."
William M. Brack2f2a6632004-08-20 23:09:47 +000014384 */
14385 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
14386 xmlSchemaPCustomErr(ctxt,
14387 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
14388 NULL, type, NULL,
14389 "The 'final' of the base type definition "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014390 "contains 'extension'", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +000014391 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
14392 }
14393 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014394 * SPEC (1.2) "Its {attribute uses} must be a subset of the {attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014395 * uses}
14396 * of the complex type definition itself, that is, for every attribute
14397 * use in the {attribute uses} of the {base type definition}, there
14398 * must be an attribute use in the {attribute uses} of the complex
14399 * type definition itself whose {attribute declaration} has the same
14400 * {name}, {target namespace} and {type definition} as its attribute
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014401 * declaration"
William M. Brack2f2a6632004-08-20 23:09:47 +000014402 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014403 * NOTE (1.2): This will be already satisfied by the way the attribute
14404 * uses are extended in xmlSchemaBuildAttributeValidation(); thus this
14405 * check is not needed.
William M. Brack2f2a6632004-08-20 23:09:47 +000014406 */
14407
14408 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014409 * SPEC (1.3) "If it has an {attribute wildcard}, the complex type
14410 * definition must also have one, and the base type definition's
14411 * {attribute wildcard}'s {namespace constraint} must be a subset
14412 * of the complex type definition's {attribute wildcard}'s {namespace
14413 * constraint}, as defined by Wildcard Subset (§3.10.6)."
William M. Brack2f2a6632004-08-20 23:09:47 +000014414 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014415 * NOTE (1.3) This is already checked in
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014416 * xmlSchemaBuildAttributeValidation; thus this check is not needed.
William M. Brack2f2a6632004-08-20 23:09:47 +000014417 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014418 * SPEC (1.4) "One of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000014419 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014420 if ((type->contentTypeDef != NULL) &&
14421 (type->contentTypeDef == base->contentTypeDef)) {
14422 /*
14423 * SPEC (1.4.1) "The {content type} of the {base type definition}
14424 * and the {content type} of the complex type definition itself
14425 * must be the same simple type definition"
14426 * PASS
14427 */
14428 } else if ((type->contentType == XML_SCHEMA_CONTENT_EMPTY) &&
14429 (base->contentType == XML_SCHEMA_CONTENT_EMPTY) ) {
14430 /*
14431 * SPEC (1.4.2) "The {content type} of both the {base type
14432 * definition} and the complex type definition itself must
14433 * be empty."
14434 * PASS
14435 */
14436 } else {
14437 /*
14438 * SPEC (1.4.3) "All of the following must be true:"
14439 */
14440 if (type->subtypes == NULL) {
14441 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014442 * SPEC 1.4.3.1 The {content type} of the complex type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014443 * definition itself must specify a particle.
14444 */
14445 xmlSchemaPCustomErr(ctxt,
14446 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
14447 NULL, type, NULL,
14448 "The content type must specify a particle", NULL);
14449 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
14450 }
14451 /*
14452 * SPEC (1.4.3.2) "One of the following must be true:"
14453 */
14454 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
14455 /*
14456 * SPEC (1.4.3.2.1) "The {content type} of the {base type
14457 * definition} must be empty.
14458 * PASS
14459 */
14460 } else {
14461 /*
14462 * SPEC (1.4.3.2.2) "All of the following must be true:"
14463 */
14464 if ((type->contentType != base->contentType) ||
14465 ((type->contentType != XML_SCHEMA_CONTENT_MIXED) &&
14466 (type->contentType != XML_SCHEMA_CONTENT_ELEMENTS))) {
14467 /*
14468 * SPEC (1.4.3.2.2.1) "Both {content type}s must be mixed
14469 * or both must be element-only."
14470 */
14471 xmlSchemaPCustomErr(ctxt,
14472 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
14473 NULL, type, NULL,
14474 "The content type of both, the type and its base "
14475 "type, must either 'mixed' or 'element-only'", NULL);
14476 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014477 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014478 /*
14479 * FUTURE TODO SPEC (1.4.3.2.2.2) "The particle of the
14480 * complex type definition must be a ·valid extension·
14481 * of the {base type definition}'s particle, as defined
14482 * in Particle Valid (Extension) (§3.9.6)."
14483 *
14484 * NOTE that we won't check "Particle Valid (Extension)",
14485 * since it is ensured by the derivation process in
14486 * xmlSchemaTypeFixup(). We need to implement this when heading
14487 * for a construction API
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014488 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014489 }
14490 /*
14491 * TODO (1.5)
14492 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014493 }
William M. Brack2f2a6632004-08-20 23:09:47 +000014494 } else {
14495 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014496 * SPEC (2) "If the {base type definition} is a simple type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014497 * then all of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000014498 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014499 if (type->contentTypeDef != base) {
14500 /*
14501 * SPEC (2.1) "The {content type} must be the same simple type
14502 * definition."
14503 */
14504 xmlSchemaPCustomErr(ctxt,
14505 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
14506 NULL, type, NULL,
14507 "The content type must be the simple base type", NULL);
14508 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
14509 }
14510 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
14511 /*
14512 * SPEC (2.2) "The {final} of the {base type definition} must not
14513 * contain extension"
14514 * NOTE that this is the same as (1.1).
14515 */
14516 xmlSchemaPCustomErr(ctxt,
14517 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
14518 NULL, type, NULL,
14519 "The 'final' of the base type definition "
14520 "contains 'extension'", NULL);
14521 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014522 }
William M. Brack2f2a6632004-08-20 23:09:47 +000014523 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014524 return (0);
William M. Brack2f2a6632004-08-20 23:09:47 +000014525}
14526
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014527/**
14528 * xmlSchemaCheckDerivationOKRestriction:
14529 * @ctxt: the schema parser context
14530 * @type: the complex type definition
14531 *
14532 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014533 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014534 * Derivation Valid (Restriction, Complex) (derivation-ok-restriction)
14535 *
14536 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014537 * missing:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014538 * (5.4.2), (5.2.2.1)
14539 *
14540 * Returns 0 if the constraints are satisfied, a positive
14541 * error code if not and -1 if an internal error occured.
14542 */
14543static int
14544xmlSchemaCheckDerivationOKRestriction(xmlSchemaParserCtxtPtr ctxt,
14545 xmlSchemaTypePtr type)
14546{
14547 xmlSchemaTypePtr base;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014548
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014549 /*
14550 * TODO: Correct the error code; XML_SCHEMAP_COS_CT_EXTENDS_1_1 is used
14551 * temporarily only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014552 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014553 base = type->baseType;
14554 if (base->flags & XML_SCHEMAS_TYPE_FINAL_RESTRICTION) {
14555 /*
14556 * SPEC (1) "The {base type definition} must be a complex type
14557 * definition whose {final} does not contain restriction."
14558 */
14559 xmlSchemaPCustomErr(ctxt,
14560 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
14561 NULL, type, NULL,
14562 "The 'final' of the base type definition "
14563 "contains 'restriction'", NULL);
14564 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
14565 }
14566 /*
14567 * NOTE (3) and (4) are done in xmlSchemaBuildAttributeValidation().
14568 *
14569 * SPEC (5) "One of the following must be true:"
14570 */
14571 if (base->builtInType == XML_SCHEMAS_ANYTYPE) {
14572 /*
14573 * SPEC (5.1) "The {base type definition} must be the
14574 * ·ur-type definition·."
14575 * PASS
14576 */
14577 } else if ((type->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
14578 (type->contentType == XML_SCHEMA_CONTENT_BASIC)) {
14579 /*
14580 * SPEC (5.2.1) "The {content type} of the complex type definition
14581 * must be a simple type definition"
14582 *
14583 * SPEC (5.2.2) "One of the following must be true:"
14584 */
14585 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
14586 (base->contentType == XML_SCHEMA_CONTENT_BASIC)) {
14587 /*
14588 * SPEC (5.2.2.1) "The {content type} of the {base type
14589 * definition} must be a simple type definition from which
14590 * the {content type} is validly derived given the empty
14591 * set as defined in Type Derivation OK (Simple) (§3.14.6)."
14592 * URGENT TODO
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014593 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014594 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
14595 (xmlSchemaIsParticleEmptiable(
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014596 (xmlSchemaParticlePtr) base->subtypes))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014597 /*
14598 * SPEC (5.2.2.2) "The {base type definition} must be mixed
14599 * and have a particle which is ·emptiable· as defined in
14600 * Particle Emptiable (§3.9.6)."
14601 * PASS
14602 */
14603 } else {
14604 xmlSchemaPCustomErr(ctxt,
14605 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
14606 NULL, type, NULL,
14607 "The content type of the base type must be either "
14608 "a simple type or 'mixed' and an emptiable particle", NULL);
14609 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
14610 }
14611 } else if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
14612 /*
14613 * SPEC (5.3.1) "The {content type} of the complex type itself must
14614 * be empty"
14615 */
14616 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
14617 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014618 * SPEC (5.3.2.1) "The {content type} of the {base type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014619 * definition} must also be empty."
14620 * PASS
14621 */
14622 } else if (((base->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
14623 (base->contentType == XML_SCHEMA_CONTENT_MIXED)) &&
14624 xmlSchemaIsParticleEmptiable(
14625 (xmlSchemaParticlePtr) base->subtypes)) {
14626 /*
14627 * SPEC (5.3.2.2) "The {content type} of the {base type
14628 * definition} must be elementOnly or mixed and have a particle
14629 * which is ·emptiable· as defined in Particle Emptiable (§3.9.6)."
14630 * PASS
14631 */
14632 } else {
14633 xmlSchemaPCustomErr(ctxt,
14634 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
14635 NULL, type, NULL,
14636 "The content type of the base type must be either "
14637 "empty or 'mixed' (or 'elements-only') and an emptiable "
14638 "particle", NULL);
14639 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
14640 }
14641 } else if ((type->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014642 HAS_MIXED_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014643 /*
14644 * SPEC (5.4.1.1) "The {content type} of the complex type definition
14645 * itself must be element-only"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014646 */
14647 if (HAS_MIXED_CONTENT(type) && (! HAS_MIXED_CONTENT(base))) {
14648 /*
14649 * SPEC (5.4.1.2) "The {content type} of the complex type
14650 * definition itself and of the {base type definition} must be
14651 * mixed"
14652 */
14653 xmlSchemaPCustomErr(ctxt,
14654 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
14655 NULL, type, NULL,
14656 "If the content type is 'mixed', then the content type of the "
14657 "base type must also be 'mixed'", NULL);
14658 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
14659 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014660 /*
14661 * SPEC (5.4.2) "The particle of the complex type definition itself
14662 * must be a ·valid restriction· of the particle of the {content
14663 * type} of the {base type definition} as defined in Particle Valid
14664 * (Restriction) (§3.9.6).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014665 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014666 * URGENT TODO: (5.4.2)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014667 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014668 } else {
14669 xmlSchemaPCustomErr(ctxt,
14670 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
14671 NULL, type, NULL,
14672 "The type is not a valid restriction of its base type", NULL);
14673 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
14674 }
14675 return (0);
14676}
14677
14678/**
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014679 * xmlSchemaCheckCTComponent:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014680 * @ctxt: the schema parser context
14681 * @type: the complex type definition
14682 *
14683 * (3.4.6) Constraints on Complex Type Definition Schema Components
14684 *
14685 * Returns 0 if the constraints are satisfied, a positive
14686 * error code if not and -1 if an internal error occured.
14687 */
14688static int
14689xmlSchemaCheckCTComponent(xmlSchemaParserCtxtPtr ctxt,
14690 xmlSchemaTypePtr type)
14691{
14692 int ret;
14693 /*
14694 * Complex Type Definition Properties Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014695 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014696 ret = xmlSchemaCheckCTPropsCorrect(ctxt, type);
14697 if (ret != 0)
14698 return (ret);
14699 if (type->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION)
14700 ret = xmlSchemaCheckCOSCTExtends(ctxt, type);
14701 else
14702 ret = xmlSchemaCheckDerivationOKRestriction(ctxt, type);
14703 return (ret);
14704}
14705
14706/**
14707 * xmlSchemaCheckSRCCT:
14708 * @ctxt: the schema parser context
14709 * @type: the complex type definition
14710 *
14711 * (3.4.3) Constraints on XML Representations of Complex Type Definitions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014712 * Schema Representation Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014713 * Complex Type Definition Representation OK (src-ct)
14714 *
14715 * Returns 0 if the constraints are satisfied, a positive
14716 * error code if not and -1 if an internal error occured.
14717 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014718static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014719xmlSchemaCheckSRCCT(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014720 xmlSchemaTypePtr type)
14721{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014722 xmlSchemaTypePtr base;
14723 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014724
14725 /*
14726 * TODO: Adjust the error codes here, as I used
14727 * XML_SCHEMAP_SRC_CT_1 only yet.
14728 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014729 base = type->baseType;
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014730 if (! HAS_SIMPLE_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014731 /*
14732 * 1 If the <complexContent> alternative is chosen, the type definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014733 * ·resolved· to by the ·actual value· of the base [attribute]
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014734 * must be a complex type definition;
14735 */
14736 if (! IS_COMPLEX_TYPE(base)) {
14737 xmlChar *str = NULL;
14738 xmlSchemaPCustomErr(ctxt,
14739 XML_SCHEMAP_SRC_CT_1,
14740 NULL, type, type->node,
14741 "If using <complexContent>, the base type is expected to be "
14742 "a complex type. The base type '%s' is a simple type",
14743 xmlSchemaFormatQName(&str, base->targetNamespace,
14744 base->name));
14745 FREE_AND_NULL(str)
14746 return (XML_SCHEMAP_SRC_CT_1);
14747 }
14748 } else {
14749 /*
14750 * SPEC
14751 * 2 If the <simpleContent> alternative is chosen, all of the
14752 * following must be true:
14753 * 2.1 The type definition ·resolved· to by the ·actual value· of the
14754 * base [attribute] must be one of the following:
14755 */
14756 if (IS_SIMPLE_TYPE(base)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014757 if ((type->flags &
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014758 XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION) == 0) {
14759 xmlChar *str = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014760 /*
14761 * 2.1.3 only if the <extension> alternative is also
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014762 * chosen, a simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014763 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014764 /* TODO: Change error code to ..._SRC_CT_2_1_3. */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014765 xmlSchemaPCustomErr(ctxt,
14766 XML_SCHEMAP_SRC_CT_1,
14767 NULL, type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014768 "If using <simpleContent> and <restriction>, the base "
14769 "type must be a complex type. The base type '%s' is "
14770 "a simple type",
14771 xmlSchemaFormatQName(&str, base->targetNamespace,
14772 base->name));
14773 FREE_AND_NULL(str)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014774 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014775 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014776 } else {
14777 /* Base type is a complex type. */
14778 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
14779 (base->contentType == XML_SCHEMA_CONTENT_BASIC)) {
14780 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014781 * 2.1.1 a complex type definition whose {content type} is a
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014782 * simple type definition;
14783 * PASS
14784 */
14785 if (base->contentTypeDef == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014786 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014787 NULL, type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014788 "Internal error: xmlSchemaCheckSRCCT, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014789 "'%s', base type has no content type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014790 type->name);
14791 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014792 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014793 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
14794 (type->flags &
14795 XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION)) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014796
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014797 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014798 * 2.1.2 only if the <restriction> alternative is also
14799 * chosen, a complex type definition whose {content type}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014800 * is mixed and a particle emptiable.
14801 */
14802 if (! xmlSchemaIsParticleEmptiable(
14803 (xmlSchemaParticlePtr) base->subtypes)) {
14804 ret = XML_SCHEMAP_SRC_CT_1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014805 } else
14806 /*
14807 * Attention: at this point the <simpleType> child is in
14808 * ->contentTypeDef (put there during parsing).
14809 */
14810 if (type->contentTypeDef == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014811 xmlChar *str = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014812 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014813 * 2.2 If clause 2.1.2 above is satisfied, then there
14814 * must be a <simpleType> among the [children] of
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014815 * <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014816 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014817 /* TODO: Change error code to ..._SRC_CT_2_2. */
14818 xmlSchemaPCustomErr(ctxt,
14819 XML_SCHEMAP_SRC_CT_1,
14820 NULL, type, NULL,
14821 "A <simpleType> is expected among the children "
14822 "of <restriction>, if <simpleContent> is used and "
14823 "the base type '%s' is a complex type",
14824 xmlSchemaFormatQName(&str, base->targetNamespace,
14825 base->name));
14826 FREE_AND_NULL(str)
14827 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014828 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014829 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014830 ret = XML_SCHEMAP_SRC_CT_1;
14831 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014832 }
14833 if (ret > 0) {
14834 xmlChar *str = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014835 if (type->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014836 xmlSchemaPCustomErr(ctxt,
14837 XML_SCHEMAP_SRC_CT_1,
14838 NULL, type, NULL,
14839 "If <simpleContent> and <restriction> is used, the "
14840 "base type must be a simple type or a complex type with "
14841 "mixed content and particle emptiable. The base type "
14842 "'%s' is none of those",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014843 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014844 base->name));
14845 } else {
14846 xmlSchemaPCustomErr(ctxt,
14847 XML_SCHEMAP_SRC_CT_1,
14848 NULL, type, NULL,
14849 "If <simpleContent> and <extension> is used, the "
14850 "base type must be a simple type. The base type '%s' "
14851 "is a complex type",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014852 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014853 base->name));
14854 }
14855 FREE_AND_NULL(str)
14856 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014857 }
14858 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014859 * SPEC (3) "The corresponding complex type definition component must
14860 * satisfy the conditions set out in Constraints on Complex Type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014861 * Definition Schema Components (§3.4.6);"
14862 * NOTE (3) will be done in xmlSchemaTypeFixup().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014863 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014864 /*
14865 * SPEC (4) If clause 2.2.1 or clause 2.2.2 in the correspondence specification
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014866 * above for {attribute wildcard} is satisfied, the intensional
14867 * intersection must be expressible, as defined in Attribute Wildcard
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014868 * Intersection (§3.10.6).
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014869 * NOTE (4) is done in xmlSchemaBuildAttributeValidation().
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014870 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014871 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014872}
William M. Brack2f2a6632004-08-20 23:09:47 +000014873
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014874#ifdef ENABLE_PARTICLE_RESTRICTION
14875/**
14876 * xmlSchemaCheckParticleRangeOK:
14877 * @ctxt: the schema parser context
14878 * @type: the complex type definition
14879 *
14880 * (3.9.6) Constraints on Particle Schema Components
14881 * Schema Component Constraint:
14882 * Occurrence Range OK (range-ok)
14883 *
14884 * STATUS: complete
14885 *
14886 * Returns 0 if the constraints are satisfied, a positive
14887 * error code if not and -1 if an internal error occured.
14888 */
14889static int
14890xmlSchemaCheckParticleRangeOK(int rmin, int rmax,
14891 int bmin, int bmax)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014892{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014893 if (rmin < bmin)
14894 return (1);
14895 if ((bmax != UNBOUNDED) &&
14896 (rmax > bmax))
14897 return (1);
14898 return (0);
14899}
14900
14901/**
14902 * xmlSchemaCheckRCaseNameAndTypeOK:
14903 * @ctxt: the schema parser context
14904 * @r: the restricting element declaration particle
14905 * @b: the base element declaration particle
14906 *
14907 * (3.9.6) Constraints on Particle Schema Components
14908 * Schema Component Constraint:
14909 * Particle Restriction OK (Elt:Elt -- NameAndTypeOK)
14910 * (rcase-NameAndTypeOK)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014911 *
14912 * STATUS:
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014913 * MISSING (3.2.3)
14914 * CLARIFY: (3.2.2)
14915 *
14916 * Returns 0 if the constraints are satisfied, a positive
14917 * error code if not and -1 if an internal error occured.
14918 */
14919static int
14920xmlSchemaCheckRCaseNameAndTypeOK(xmlSchemaParserCtxtPtr ctxt,
14921 xmlSchemaParticlePtr r,
14922 xmlSchemaParticlePtr b)
14923{
14924 xmlSchemaElementPtr elemR, elemB;
14925
14926 /* TODO: Error codes (rcase-NameAndTypeOK). */
14927 elemR = (xmlSchemaElementPtr) r->children;
14928 elemB = (xmlSchemaElementPtr) b->children;
14929 /*
14930 * SPEC (1) "The declarations' {name}s and {target namespace}s are
14931 * the same."
14932 */
14933 if ((elemR != elemB) &&
14934 ((! xmlStrEqual(elemR->name, elemB->name)) ||
14935 (! xmlStrEqual(elemR->targetNamespace, elemB->targetNamespace))))
14936 return (1);
14937 /*
14938 * SPEC (2) "R's occurrence range is a valid restriction of B's
14939 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
14940 */
14941 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
14942 b->minOccurs, b->maxOccurs) != 0)
14943 return (1);
14944 /*
14945 * SPEC (3.1) "Both B's declaration's {scope} and R's declaration's
14946 * {scope} are global."
14947 */
14948 if (elemR == elemB)
14949 return (0);
14950 /*
14951 * SPEC (3.2.1) "Either B's {nillable} is true or R's {nillable} is false."
14952 */
14953 if (((elemB->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) &&
14954 (elemR->flags & XML_SCHEMAS_ELEM_NILLABLE))
14955 return (1);
14956 /*
14957 * SPEC (3.2.2) "either B's declaration's {value constraint} is absent,
14958 * or is not fixed, or R's declaration's {value constraint} is fixed
14959 * with the same value."
14960 */
14961 if ((elemB->value != NULL) && (elemB->flags & XML_SCHEMAS_ELEM_FIXED) &&
14962 ((elemR->value == NULL) ||
14963 ((elemR->flags & XML_SCHEMAS_ELEM_FIXED) == 0) ||
14964 /* TODO: Equality of the initial value or normalized or canonical? */
14965 (! xmlStrEqual(elemR->value, elemB->value))))
14966 return (1);
14967 /*
14968 * TODO: SPEC (3.2.3) "R's declaration's {identity-constraint
14969 * definitions} is a subset of B's declaration's {identity-constraint
14970 * definitions}, if any."
14971 */
14972 if (elemB->idcs != NULL) {
14973 /* TODO */
14974 }
14975 /*
14976 * SPEC (3.2.4) "R's declaration's {disallowed substitutions} is a
14977 * superset of B's declaration's {disallowed substitutions}."
14978 */
14979 if (((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) &&
14980 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) == 0)) ||
14981 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) &&
14982 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) == 0)) ||
14983 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) &&
14984 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) == 0)))
14985 return (1);
14986 /*
14987 * SPEC (3.2.5) "R's {type definition} is validly derived given
14988 * {extension, list, union} from B's {type definition}"
14989 *
14990 * BADSPEC TODO: What's the point of adding "list" and "union" to the
14991 * set, if the corresponding constraints handle "restriction" and
14992 * "extension" only?
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014993 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014994 */
14995 {
14996 int set = 0;
14997
14998 set |= SUBSET_EXTENSION;
14999 set |= SUBSET_LIST;
15000 set |= SUBSET_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015001 if (xmlSchemaCheckCOSDerivedOK(elemR->subtypes,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000015002 elemB->subtypes, set) != 0)
15003 return (1);
15004 }
15005 return (0);
15006}
15007
15008/**
15009 * xmlSchemaCheckRCaseNSCompat:
15010 * @ctxt: the schema parser context
15011 * @r: the restricting element declaration particle
15012 * @b: the base wildcard particle
15013 *
15014 * (3.9.6) Constraints on Particle Schema Components
15015 * Schema Component Constraint:
15016 * Particle Derivation OK (Elt:Any -- NSCompat)
15017 * (rcase-NSCompat)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015018 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000015019 * STATUS: complete
15020 *
15021 * Returns 0 if the constraints are satisfied, a positive
15022 * error code if not and -1 if an internal error occured.
15023 */
15024static int
15025xmlSchemaCheckRCaseNSCompat(xmlSchemaParserCtxtPtr ctxt,
15026 xmlSchemaParticlePtr r,
15027 xmlSchemaParticlePtr b)
15028{
15029 /* TODO:Error codes (rcase-NSCompat). */
15030 /*
15031 * SPEC "For an element declaration particle to be a ·valid restriction·
15032 * of a wildcard particle all of the following must be true:"
15033 *
15034 * SPEC (1) "The element declaration's {target namespace} is ·valid·
15035 * with respect to the wildcard's {namespace constraint} as defined by
15036 * Wildcard allows Namespace Name (§3.10.4)."
15037 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015038 if (xmlSchemaCheckCVCWildcardNamespace((xmlSchemaWildcardPtr) b->children,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000015039 ((xmlSchemaElementPtr) r->children)->targetNamespace) != 0)
15040 return (1);
15041 /*
15042 * SPEC (2) "R's occurrence range is a valid restriction of B's
15043 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
15044 */
15045 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
15046 b->minOccurs, b->maxOccurs) != 0)
15047 return (1);
15048
15049 return (0);
15050}
15051
15052/**
15053 * xmlSchemaCheckRCaseRecurseAsIfGroup:
15054 * @ctxt: the schema parser context
15055 * @r: the restricting element declaration particle
15056 * @b: the base model group particle
15057 *
15058 * (3.9.6) Constraints on Particle Schema Components
15059 * Schema Component Constraint:
15060 * Particle Derivation OK (Elt:All/Choice/Sequence -- RecurseAsIfGroup)
15061 * (rcase-RecurseAsIfGroup)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015062 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000015063 * STATUS: TODO
15064 *
15065 * Returns 0 if the constraints are satisfied, a positive
15066 * error code if not and -1 if an internal error occured.
15067 */
15068static int
15069xmlSchemaCheckRCaseRecurseAsIfGroup(xmlSchemaParserCtxtPtr ctxt,
15070 xmlSchemaParticlePtr r,
15071 xmlSchemaParticlePtr b)
15072{
15073 /* TODO: Error codes (rcase-RecurseAsIfGroup). */
15074 TODO
15075 return (0);
15076}
15077
15078/**
15079 * xmlSchemaCheckRCaseNSSubset:
15080 * @ctxt: the schema parser context
15081 * @r: the restricting wildcard particle
15082 * @b: the base wildcard particle
15083 *
15084 * (3.9.6) Constraints on Particle Schema Components
15085 * Schema Component Constraint:
15086 * Particle Derivation OK (Any:Any -- NSSubset)
15087 * (rcase-NSSubset)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015088 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000015089 * STATUS: complete
15090 *
15091 * Returns 0 if the constraints are satisfied, a positive
15092 * error code if not and -1 if an internal error occured.
15093 */
15094static int
15095xmlSchemaCheckRCaseNSSubset(xmlSchemaParserCtxtPtr ctxt,
15096 xmlSchemaParticlePtr r,
15097 xmlSchemaParticlePtr b,
15098 int isAnyTypeBase)
15099{
15100 /* TODO: Error codes (rcase-NSSubset). */
15101 /*
15102 * SPEC (1) "R's occurrence range is a valid restriction of B's
15103 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
15104 */
15105 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
15106 b->minOccurs, b->maxOccurs))
15107 return (1);
15108 /*
15109 * SPEC (2) "R's {namespace constraint} must be an intensional subset
15110 * of B's {namespace constraint} as defined by Wildcard Subset (§3.10.6)."
15111 */
15112 if (xmlSchemaCheckCOSNSSubset((xmlSchemaWildcardPtr) r->children,
15113 (xmlSchemaWildcardPtr) b->children))
15114 return (1);
15115 /*
15116 * SPEC (3) "Unless B is the content model wildcard of the ·ur-type
15117 * definition·, R's {process contents} must be identical to or stronger
15118 * than B's {process contents}, where strict is stronger than lax is
15119 * stronger than skip."
15120 */
15121 if (! isAnyTypeBase) {
15122 if ( ((xmlSchemaWildcardPtr) r->children)->processContents <
15123 ((xmlSchemaWildcardPtr) b->children)->processContents)
15124 return (1);
15125 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015126
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000015127 return (0);
15128}
15129
15130/**
15131 * xmlSchemaCheckCOSParticleRestrict:
15132 * @ctxt: the schema parser context
15133 * @type: the complex type definition
15134 *
15135 * (3.9.6) Constraints on Particle Schema Components
15136 * Schema Component Constraint:
15137 * Particle Valid (Restriction) (cos-particle-restrict)
15138 *
15139 * STATUS: TODO
15140 *
15141 * Returns 0 if the constraints are satisfied, a positive
15142 * error code if not and -1 if an internal error occured.
15143 */
15144static int
15145xmlSchemaCheckCOSParticleRestrict(xmlSchemaParserCtxtPtr ctxt,
15146 xmlSchemaParticlePtr r,
15147 xmlSchemaParticlePtr b)
15148{
15149 int ret = 0;
15150
15151 /*part = GET_PARTICLE(type);
15152 basePart = GET_PARTICLE(base);
15153 */
15154
15155 TODO
15156
15157 /*
15158 * SPEC (1) "They are the same particle."
15159 */
15160 if (r == b)
15161 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015162
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000015163
15164 return (0);
15165}
15166
15167/**
15168 * xmlSchemaCheckRCaseNSRecurseCheckCardinality:
15169 * @ctxt: the schema parser context
15170 * @r: the model group particle
15171 * @b: the base wildcard particle
15172 *
15173 * (3.9.6) Constraints on Particle Schema Components
15174 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015175 * Particle Derivation OK (All/Choice/Sequence:Any --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000015176 * NSRecurseCheckCardinality)
15177 * (rcase-NSRecurseCheckCardinality)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015178 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000015179 * STATUS: TODO: subst-groups
15180 *
15181 * Returns 0 if the constraints are satisfied, a positive
15182 * error code if not and -1 if an internal error occured.
15183 */
15184static int
15185xmlSchemaCheckRCaseNSRecurseCheckCardinality(xmlSchemaParserCtxtPtr ctxt,
15186 xmlSchemaParticlePtr r,
15187 xmlSchemaParticlePtr b)
15188{
15189 xmlSchemaParticlePtr part;
15190 /* TODO: Error codes (rcase-NSRecurseCheckCardinality). */
15191 if ((r->children == NULL) || (r->children->children == NULL))
15192 return (-1);
15193 /*
15194 * SPEC "For a group particle to be a ·valid restriction· of a
15195 * wildcard particle..."
15196 *
15197 * SPEC (1) "Every member of the {particles} of the group is a ·valid
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015198 * restriction· of the wildcard as defined by
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000015199 * Particle Valid (Restriction) (§3.9.6)."
15200 */
15201 part = (xmlSchemaParticlePtr) r->children->children;
15202 do {
15203 if (xmlSchemaCheckCOSParticleRestrict(ctxt, part, b))
15204 return (1);
15205 part = (xmlSchemaParticlePtr) part->next;
15206 } while (part != NULL);
15207 /*
15208 * SPEC (2) "The effective total range of the group [...] is a
15209 * valid restriction of B's occurrence range as defined by
15210 * Occurrence Range OK (§3.9.6)."
15211 */
15212 if (xmlSchemaCheckParticleRangeOK(
15213 xmlSchemaGetParticleTotalRangeMin(r),
15214 xmlSchemaGetParticleTotalRangeMax(r),
15215 b->minOccurs, b->maxOccurs) != 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015216 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000015217 return (0);
15218}
15219
15220/**
15221 * xmlSchemaCheckRCaseRecurse:
15222 * @ctxt: the schema parser context
15223 * @r: the <all> or <sequence> model group particle
15224 * @b: the base <all> or <sequence> model group particle
15225 *
15226 * (3.9.6) Constraints on Particle Schema Components
15227 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015228 * Particle Derivation OK (All:All,Sequence:Sequence --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000015229 Recurse)
15230 * (rcase-Recurse)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015231 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000015232 * STATUS: ?
15233 * TODO: subst-groups
15234 *
15235 * Returns 0 if the constraints are satisfied, a positive
15236 * error code if not and -1 if an internal error occured.
15237 */
15238static int
15239xmlSchemaCheckRCaseRecurse(xmlSchemaParserCtxtPtr ctxt,
15240 xmlSchemaParticlePtr r,
15241 xmlSchemaParticlePtr b)
15242{
15243 /* xmlSchemaParticlePtr part; */
15244 /* TODO: Error codes (rcase-Recurse). */
15245 if ((r->children == NULL) || (b->children == NULL) ||
15246 (r->children->type != b->children->type))
15247 return (-1);
15248 /*
15249 * SPEC "For an all or sequence group particle to be a ·valid
15250 * restriction· of another group particle with the same {compositor}..."
15251 *
15252 * SPEC (1) "R's occurrence range is a valid restriction of B's
15253 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
15254 */
15255 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
15256 b->minOccurs, b->maxOccurs))
15257 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015258
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000015259
15260 return (0);
15261}
15262
15263#endif
15264
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015265#define FACET_RESTR_MUTUAL_ERR(fac1, fac2) \
15266 xmlSchemaPCustomErrExt(pctxt, \
15267 XML_SCHEMAP_INVALID_FACET_VALUE, \
15268 NULL, (xmlSchemaTypePtr) fac1, fac1->node, \
15269 "It is an error for both '%s' and '%s' to be specified on the "\
15270 "same type definition", \
15271 BAD_CAST xmlSchemaFacetTypeToString(fac1->type), \
15272 BAD_CAST xmlSchemaFacetTypeToString(fac2->type), NULL);
15273
15274#define FACET_RESTR_ERR(fac1, msg) \
15275 xmlSchemaPCustomErr(pctxt, \
15276 XML_SCHEMAP_INVALID_FACET_VALUE, \
15277 NULL, (xmlSchemaTypePtr) fac1, fac1->node, \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015278 msg, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015279
15280#define FACET_RESTR_FIXED_ERR(fac) \
15281 xmlSchemaPCustomErr(pctxt, \
15282 XML_SCHEMAP_INVALID_FACET_VALUE, \
15283 NULL, (xmlSchemaTypePtr) fac, fac->node, \
15284 "The base type's facet is 'fixed', thus the value must not " \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015285 "differ", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015286
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000015287static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015288xmlSchemaDeriveFacetErr(xmlSchemaParserCtxtPtr pctxt,
15289 xmlSchemaFacetPtr facet1,
15290 xmlSchemaFacetPtr facet2,
15291 int lessGreater,
15292 int orEqual,
15293 int ofBase)
15294{
15295 xmlChar *msg = NULL;
15296
15297 msg = xmlStrdup(BAD_CAST "'");
15298 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet1->type));
15299 msg = xmlStrcat(msg, BAD_CAST "' has to be");
15300 if (lessGreater == 0)
15301 msg = xmlStrcat(msg, BAD_CAST " equal to");
15302 if (lessGreater == 1)
15303 msg = xmlStrcat(msg, BAD_CAST " greater than");
15304 else
15305 msg = xmlStrcat(msg, BAD_CAST " less than");
15306
15307 if (orEqual)
15308 msg = xmlStrcat(msg, BAD_CAST " or equal to");
15309 msg = xmlStrcat(msg, BAD_CAST " '");
15310 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet2->type));
15311 if (ofBase)
15312 msg = xmlStrcat(msg, BAD_CAST "' of the base type");
15313 else
15314 msg = xmlStrcat(msg, BAD_CAST "'");
15315
15316 xmlSchemaPCustomErr(pctxt,
15317 XML_SCHEMAP_INVALID_FACET_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015318 NULL, (xmlSchemaTypePtr) facet1, facet1->node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015319 (const char *) msg, NULL);
15320
15321 if (msg != NULL)
15322 xmlFree(msg);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000015323}
15324
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015325static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015326xmlSchemaDeriveAndValidateFacets(xmlSchemaParserCtxtPtr pctxt,
15327 xmlSchemaTypePtr type)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015328{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015329 xmlSchemaTypePtr base = type->baseType;
15330 xmlSchemaFacetLinkPtr link, cur, last = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015331 xmlSchemaFacetPtr facet, bfacet,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015332 flength = NULL, ftotdig = NULL, ffracdig = NULL,
15333 fmaxlen = NULL, fminlen = NULL, /* facets of the current type */
15334 fmininc = NULL, fmaxinc = NULL,
15335 fminexc = NULL, fmaxexc = NULL,
15336 bflength = NULL, bftotdig = NULL, bffracdig = NULL,
15337 bfmaxlen = NULL, bfminlen = NULL, /* facets of the base type */
15338 bfmininc = NULL, bfmaxinc = NULL,
15339 bfminexc = NULL, bfmaxexc = NULL;
15340 int res, err = 0, fixedErr;
15341 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015342 * 3 The {facets} of R are the union of S and the {facets}
15343 * of B, eliminating duplicates. To eliminate duplicates,
15344 * when a facet of the same kind occurs in both S and the
15345 * {facets} of B, the one in the {facets} of B is not
15346 * included, with the exception of enumeration and pattern
15347 * facets, for which multiple occurrences with distinct values
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015348 * are allowed.
15349 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015350
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015351 if ((type->facetSet == NULL) && (base->facetSet == NULL))
15352 return (0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015353
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015354 last = type->facetSet;
15355 if (last != NULL)
15356 while (last->next != NULL)
15357 last = last->next;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015358
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015359 for (cur = type->facetSet; cur != NULL; cur = cur->next) {
15360 facet = cur->facet;
15361 switch (facet->type) {
15362 case XML_SCHEMA_FACET_LENGTH:
15363 flength = facet; break;
15364 case XML_SCHEMA_FACET_MINLENGTH:
15365 fminlen = facet; break;
15366 case XML_SCHEMA_FACET_MININCLUSIVE:
15367 fmininc = facet; break;
15368 case XML_SCHEMA_FACET_MINEXCLUSIVE:
15369 fminexc = facet; break;
15370 case XML_SCHEMA_FACET_MAXLENGTH:
15371 fmaxlen = facet; break;
15372 case XML_SCHEMA_FACET_MAXINCLUSIVE:
15373 fmaxinc = facet; break;
15374 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
15375 fmaxexc = facet; break;
15376 case XML_SCHEMA_FACET_TOTALDIGITS:
15377 ftotdig = facet; break;
15378 case XML_SCHEMA_FACET_FRACTIONDIGITS:
15379 ffracdig = facet; break;
15380 default:
15381 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015382 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015383 }
15384 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
15385 facet = cur->facet;
15386 switch (facet->type) {
15387 case XML_SCHEMA_FACET_LENGTH:
15388 bflength = facet; break;
15389 case XML_SCHEMA_FACET_MINLENGTH:
15390 bfminlen = facet; break;
15391 case XML_SCHEMA_FACET_MININCLUSIVE:
15392 bfmininc = facet; break;
15393 case XML_SCHEMA_FACET_MINEXCLUSIVE:
15394 bfminexc = facet; break;
15395 case XML_SCHEMA_FACET_MAXLENGTH:
15396 bfmaxlen = facet; break;
15397 case XML_SCHEMA_FACET_MAXINCLUSIVE:
15398 bfmaxinc = facet; break;
15399 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
15400 bfmaxexc = facet; break;
15401 case XML_SCHEMA_FACET_TOTALDIGITS:
15402 bftotdig = facet; break;
15403 case XML_SCHEMA_FACET_FRACTIONDIGITS:
15404 bffracdig = facet; break;
15405 default:
15406 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015407 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015408 }
15409 err = 0;
15410 /*
15411 * length and minLength or maxLength (2.2) + (3.2)
15412 */
15413 if (flength && (fminlen || fmaxlen)) {
15414 FACET_RESTR_ERR(flength, "It is an error for both 'length' and "
15415 "either of 'minLength' or 'maxLength' to be specified on "
15416 "the same type definition")
15417 }
15418 /*
15419 * Mutual exclusions in the same derivation step.
15420 */
15421 if ((fmaxinc) && (fmaxexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015422 /*
15423 * SCC "maxInclusive and maxExclusive"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015424 */
15425 FACET_RESTR_MUTUAL_ERR(fmaxinc, fmaxexc)
15426 }
15427 if ((fmininc) && (fminexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015428 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015429 * SCC "minInclusive and minExclusive"
15430 */
15431 FACET_RESTR_MUTUAL_ERR(fmininc, fminexc)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015432 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015433
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015434 if (flength && bflength) {
15435 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015436 * SCC "length valid restriction"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015437 * The values have to be equal.
15438 */
15439 res = xmlSchemaCompareValues(flength->val, bflength->val);
15440 if (res == -2)
15441 goto internal_error;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015442 if (res != 0)
15443 xmlSchemaDeriveFacetErr(pctxt, flength, bflength, 0, 0, 1);
15444 if ((res != 0) && (bflength->fixed)) {
15445 FACET_RESTR_FIXED_ERR(flength)
15446 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015447
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015448 }
15449 if (fminlen && bfminlen) {
15450 /*
15451 * SCC "minLength valid restriction"
15452 * minLength >= BASE minLength
15453 */
15454 res = xmlSchemaCompareValues(fminlen->val, bfminlen->val);
15455 if (res == -2)
15456 goto internal_error;
15457 if (res == -1)
15458 xmlSchemaDeriveFacetErr(pctxt, fminlen, bfminlen, 1, 1, 1);
15459 if ((res != 0) && (bfminlen->fixed)) {
15460 FACET_RESTR_FIXED_ERR(fminlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015461 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015462 }
15463 if (fmaxlen && bfmaxlen) {
15464 /*
15465 * SCC "maxLength valid restriction"
15466 * maxLength <= BASE minLength
15467 */
15468 res = xmlSchemaCompareValues(fmaxlen->val, bfmaxlen->val);
15469 if (res == -2)
15470 goto internal_error;
15471 if (res == 1)
15472 xmlSchemaDeriveFacetErr(pctxt, fmaxlen, bfmaxlen, -1, 1, 1);
15473 if ((res != 0) && (bfmaxlen->fixed)) {
15474 FACET_RESTR_FIXED_ERR(fmaxlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015475 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015476 }
15477 /*
15478 * SCC "length and minLength or maxLength"
15479 */
15480 if (! flength)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015481 flength = bflength;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015482 if (flength) {
15483 if (! fminlen)
15484 flength = bflength;
15485 if (fminlen) {
15486 /* (1.1) length >= minLength */
15487 res = xmlSchemaCompareValues(flength->val, fminlen->val);
15488 if (res == -2)
15489 goto internal_error;
15490 if (res == -1)
15491 xmlSchemaDeriveFacetErr(pctxt, flength, fminlen, 1, 1, 0);
15492 }
15493 if (! fmaxlen)
15494 fmaxlen = bfmaxlen;
15495 if (fmaxlen) {
15496 /* (2.1) length <= maxLength */
15497 res = xmlSchemaCompareValues(flength->val, fmaxlen->val);
15498 if (res == -2)
15499 goto internal_error;
15500 if (res == 1)
15501 xmlSchemaDeriveFacetErr(pctxt, flength, fmaxlen, -1, 1, 0);
15502 }
15503 }
15504 if (fmaxinc) {
15505 /*
15506 * "maxInclusive"
15507 */
15508 if (fmininc) {
15509 /* SCC "maxInclusive >= minInclusive" */
15510 res = xmlSchemaCompareValues(fmaxinc->val, fmininc->val);
15511 if (res == -2)
15512 goto internal_error;
15513 if (res == -1) {
15514 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, fmininc, 1, 1, 0);
15515 }
15516 }
15517 /*
15518 * SCC "maxInclusive valid restriction"
15519 */
15520 if (bfmaxinc) {
15521 /* maxInclusive <= BASE maxInclusive */
15522 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxinc->val);
15523 if (res == -2)
15524 goto internal_error;
15525 if (res == 1)
15526 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxinc, -1, 1, 1);
15527 if ((res != 0) && (bfmaxinc->fixed)) {
15528 FACET_RESTR_FIXED_ERR(fmaxinc)
15529 }
15530 }
15531 if (bfmaxexc) {
15532 /* maxInclusive < BASE maxExclusive */
15533 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxexc->val);
15534 if (res == -2)
15535 goto internal_error;
15536 if (res != -1) {
15537 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxexc, -1, 0, 1);
15538 }
15539 }
15540 if (bfmininc) {
15541 /* maxInclusive >= BASE minInclusive */
15542 res = xmlSchemaCompareValues(fmaxinc->val, bfmininc->val);
15543 if (res == -2)
15544 goto internal_error;
15545 if (res == -1) {
15546 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmininc, 1, 1, 1);
15547 }
15548 }
15549 if (bfminexc) {
15550 /* maxInclusive > BASE minExclusive */
15551 res = xmlSchemaCompareValues(fmaxinc->val, bfminexc->val);
15552 if (res == -2)
15553 goto internal_error;
15554 if (res != 1) {
15555 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfminexc, 1, 0, 1);
15556 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015557 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015558 }
15559 if (fmaxexc) {
15560 /*
15561 * "maxExclusive >= minExclusive"
15562 */
15563 if (fminexc) {
15564 res = xmlSchemaCompareValues(fmaxexc->val, fminexc->val);
15565 if (res == -2)
15566 goto internal_error;
15567 if (res == -1) {
15568 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, fminexc, 1, 1, 0);
15569 }
15570 }
15571 /*
15572 * "maxExclusive valid restriction"
15573 */
15574 if (bfmaxexc) {
15575 /* maxExclusive <= BASE maxExclusive */
15576 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxexc->val);
15577 if (res == -2)
15578 goto internal_error;
15579 if (res == 1) {
15580 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxexc, -1, 1, 1);
15581 }
15582 if ((res != 0) && (bfmaxexc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015583 FACET_RESTR_FIXED_ERR(fmaxexc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015584 }
15585 }
15586 if (bfmaxinc) {
15587 /* maxExclusive <= BASE maxInclusive */
15588 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxinc->val);
15589 if (res == -2)
15590 goto internal_error;
15591 if (res == 1) {
15592 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxinc, -1, 1, 1);
15593 }
15594 }
15595 if (bfmininc) {
15596 /* maxExclusive > BASE minInclusive */
15597 res = xmlSchemaCompareValues(fmaxexc->val, bfmininc->val);
15598 if (res == -2)
15599 goto internal_error;
15600 if (res != 1) {
15601 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmininc, 1, 0, 1);
15602 }
15603 }
15604 if (bfminexc) {
15605 /* maxExclusive > BASE minExclusive */
15606 res = xmlSchemaCompareValues(fmaxexc->val, bfminexc->val);
15607 if (res == -2)
15608 goto internal_error;
15609 if (res != 1) {
15610 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfminexc, 1, 0, 1);
15611 }
15612 }
15613 }
15614 if (fminexc) {
15615 /*
15616 * "minExclusive < maxInclusive"
15617 */
15618 if (fmaxinc) {
15619 res = xmlSchemaCompareValues(fminexc->val, fmaxinc->val);
15620 if (res == -2)
15621 goto internal_error;
15622 if (res != -1) {
15623 xmlSchemaDeriveFacetErr(pctxt, fminexc, fmaxinc, -1, 0, 0);
15624 }
15625 }
15626 /*
15627 * "minExclusive valid restriction"
15628 */
15629 if (bfminexc) {
15630 /* minExclusive >= BASE minExclusive */
15631 res = xmlSchemaCompareValues(fminexc->val, bfminexc->val);
15632 if (res == -2)
15633 goto internal_error;
15634 if (res == -1) {
15635 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfminexc, 1, 1, 1);
15636 }
15637 if ((res != 0) && (bfminexc->fixed)) {
15638 FACET_RESTR_FIXED_ERR(fminexc)
15639 }
15640 }
15641 if (bfmaxinc) {
15642 /* minExclusive <= BASE maxInclusive */
15643 res = xmlSchemaCompareValues(fminexc->val, bfmaxinc->val);
15644 if (res == -2)
15645 goto internal_error;
15646 if (res == 1) {
15647 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxinc, -1, 1, 1);
15648 }
15649 }
15650 if (bfmininc) {
15651 /* minExclusive >= BASE minInclusive */
15652 res = xmlSchemaCompareValues(fminexc->val, bfmininc->val);
15653 if (res == -2)
15654 goto internal_error;
15655 if (res == -1) {
15656 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmininc, 1, 1, 1);
15657 }
15658 }
15659 if (bfmaxexc) {
15660 /* minExclusive < BASE maxExclusive */
15661 res = xmlSchemaCompareValues(fminexc->val, bfmaxexc->val);
15662 if (res == -2)
15663 goto internal_error;
15664 if (res != -1) {
15665 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxexc, -1, 0, 1);
15666 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015667 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015668 }
15669 if (fmininc) {
15670 /*
15671 * "minInclusive < maxExclusive"
15672 */
15673 if (fmaxexc) {
15674 res = xmlSchemaCompareValues(fmininc->val, fmaxexc->val);
15675 if (res == -2)
15676 goto internal_error;
15677 if (res != -1) {
15678 xmlSchemaDeriveFacetErr(pctxt, fmininc, fmaxexc, -1, 0, 0);
15679 }
15680 }
15681 /*
15682 * "minExclusive valid restriction"
15683 */
15684 if (bfmininc) {
15685 /* minInclusive >= BASE minInclusive */
15686 res = xmlSchemaCompareValues(fmininc->val, bfmininc->val);
15687 if (res == -2)
15688 goto internal_error;
15689 if (res == -1) {
15690 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmininc, 1, 1, 1);
15691 }
15692 if ((res != 0) && (bfmininc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015693 FACET_RESTR_FIXED_ERR(fmininc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015694 }
15695 }
15696 if (bfmaxinc) {
15697 /* minInclusive <= BASE maxInclusive */
15698 res = xmlSchemaCompareValues(fmininc->val, bfmaxinc->val);
15699 if (res == -2)
15700 goto internal_error;
Daniel Veillard0a119eb2005-07-20 13:46:00 +000015701 if (res == 1) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015702 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxinc, -1, 1, 1);
15703 }
15704 }
15705 if (bfminexc) {
15706 /* minInclusive > BASE minExclusive */
15707 res = xmlSchemaCompareValues(fmininc->val, bfminexc->val);
15708 if (res == -2)
15709 goto internal_error;
15710 if (res != 1)
15711 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfminexc, 1, 0, 1);
15712 }
15713 if (bfmaxexc) {
15714 /* minInclusive < BASE maxExclusive */
15715 res = xmlSchemaCompareValues(fmininc->val, bfmaxexc->val);
15716 if (res == -2)
15717 goto internal_error;
15718 if (res != -1)
15719 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxexc, -1, 0, 1);
15720 }
15721 }
15722 if (ftotdig && bftotdig) {
15723 /*
15724 * SCC " totalDigits valid restriction"
15725 * totalDigits <= BASE totalDigits
15726 */
15727 res = xmlSchemaCompareValues(ftotdig->val, bftotdig->val);
15728 if (res == -2)
15729 goto internal_error;
15730 if (res == 1)
15731 xmlSchemaDeriveFacetErr(pctxt, ftotdig, bftotdig,
15732 -1, 1, 1);
15733 if ((res != 0) && (bftotdig->fixed)) {
15734 FACET_RESTR_FIXED_ERR(ftotdig)
15735 }
15736 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015737 if (ffracdig && bffracdig) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015738 /*
15739 * SCC "fractionDigits valid restriction"
15740 * fractionDigits <= BASE fractionDigits
15741 */
15742 res = xmlSchemaCompareValues(ffracdig->val, bffracdig->val);
15743 if (res == -2)
15744 goto internal_error;
15745 if (res == 1)
15746 xmlSchemaDeriveFacetErr(pctxt, ffracdig, bffracdig,
15747 -1, 1, 1);
15748 if ((res != 0) && (bffracdig->fixed)) {
15749 FACET_RESTR_FIXED_ERR(ffracdig)
15750 }
15751 }
15752 /*
15753 * SCC "fractionDigits less than or equal to totalDigits"
15754 */
15755 if (! ftotdig)
15756 ftotdig = bftotdig;
15757 if (! ffracdig)
15758 ffracdig = bffracdig;
15759 if (ftotdig && ffracdig) {
15760 res = xmlSchemaCompareValues(ffracdig->val, ftotdig->val);
15761 if (res == -2)
15762 goto internal_error;
15763 if (res == 1)
15764 xmlSchemaDeriveFacetErr(pctxt, ffracdig, ftotdig,
15765 -1, 1, 0);
15766 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015767 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015768 * *Enumerations* won' be added here, since only the first set
15769 * of enumerations in the ancestor-or-self axis is used
15770 * for validation, plus we need to use the base type of those
15771 * enumerations for whitespace.
15772 *
15773 * *Patterns*: won't be add here, since they are ORed at
15774 * type level and ANDed at ancestor level. This will
15775 * happed during validation by walking the base axis
15776 * of the type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015777 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015778 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
15779 bfacet = cur->facet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015780 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015781 * Special handling of enumerations and patterns.
15782 * TODO: hmm, they should not appear in the set, so remove this.
15783 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015784 if ((bfacet->type == XML_SCHEMA_FACET_PATTERN) ||
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015785 (bfacet->type == XML_SCHEMA_FACET_ENUMERATION))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015786 continue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015787 /*
15788 * Search for a duplicate facet in the current type.
15789 */
15790 link = type->facetSet;
15791 err = 0;
15792 fixedErr = 0;
15793 while (link != NULL) {
15794 facet = link->facet;
15795 if (facet->type == bfacet->type) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015796 switch (facet->type) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015797 case XML_SCHEMA_FACET_WHITESPACE:
15798 /*
15799 * The whitespace must be stronger.
15800 */
15801 if (facet->whitespace < bfacet->whitespace) {
15802 FACET_RESTR_ERR(flength,
15803 "The 'whitespace' value has to be equal to "
15804 "or stronger than the 'whitespace' value of "
15805 "the base type")
15806 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015807 if ((bfacet->fixed) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015808 (facet->whitespace != bfacet->whitespace)) {
15809 FACET_RESTR_FIXED_ERR(facet)
15810 }
15811 break;
15812 default:
15813 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015814 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015815 /* Duplicate found. */
15816 break;
15817 }
15818 link = link->next;
15819 }
15820 /*
15821 * If no duplicate was found: add the base types's facet
15822 * to the set.
15823 */
15824 if (link == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015825 link = (xmlSchemaFacetLinkPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015826 xmlMalloc(sizeof(xmlSchemaFacetLink));
15827 if (link == NULL) {
15828 xmlSchemaPErrMemory(pctxt,
15829 "deriving facets, creating a facet link", NULL);
15830 return (-1);
15831 }
15832 link->facet = cur->facet;
15833 link->next = NULL;
15834 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015835 type->facetSet = link;
15836 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015837 last->next = link;
15838 last = link;
15839 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015840
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015841 }
15842
15843 return (0);
15844internal_error:
15845 xmlSchemaPCustomErr(pctxt,
15846 XML_SCHEMAP_INVALID_FACET_VALUE,
15847 NULL, type, NULL,
15848 "Internal error: xmlSchemaDeriveAndValidateFacets", NULL);
15849 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015850}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015851
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015852static int
15853xmlSchemaFinishMemberTypeDefinitionsProperty(xmlSchemaParserCtxtPtr pctxt,
15854 xmlSchemaTypePtr type)
15855{
15856 xmlSchemaTypeLinkPtr link, lastLink, prevLink, subLink, newLink;
15857 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015858 * The actual value is then formed by replacing any union type
15859 * definition in the ·explicit members· with the members of their
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015860 * {member type definitions}, in order.
15861 */
15862 link = type->memberTypes;
15863 while (link != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015864
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015865 if (IS_NOT_TYPEFIXED(link->type))
15866 xmlSchemaTypeFixup(link->type, pctxt, NULL);
15867
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015868 if (VARIETY_UNION(link->type)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015869 subLink = xmlSchemaGetUnionSimpleTypeMemberTypes(link->type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015870 if (subLink != NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015871 link->type = subLink->type;
15872 if (subLink->next != NULL) {
15873 lastLink = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015874 subLink = subLink->next;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015875 prevLink = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015876 while (subLink != NULL) {
15877 newLink = (xmlSchemaTypeLinkPtr)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015878 xmlMalloc(sizeof(xmlSchemaTypeLink));
15879 if (newLink == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015880 xmlSchemaPErrMemory(pctxt, "allocating a type link",
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015881 NULL);
15882 return (-1);
15883 }
15884 newLink->type = subLink->type;
15885 prevLink->next = newLink;
15886 prevLink = newLink;
15887 newLink->next = lastLink;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015888
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015889 subLink = subLink->next;
15890 }
15891 }
15892 }
15893 }
15894 link = link->next;
15895 }
15896 return (0);
15897}
15898
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015899static void
15900xmlSchemaTypeFixupOptimFacets(xmlSchemaTypePtr type)
15901{
15902 int has = 0, needVal = 0, normVal = 0;
15903
15904 has = (type->baseType->flags & XML_SCHEMAS_TYPE_HAS_FACETS) ? 1 : 0;
15905 if (has) {
15906 needVal = (type->baseType->flags &
15907 XML_SCHEMAS_TYPE_FACETSNEEDVALUE) ? 1 : 0;
15908 normVal = (type->baseType->flags &
15909 XML_SCHEMAS_TYPE_NORMVALUENEEDED) ? 1 : 0;
15910 }
15911 if (type->facets != NULL) {
15912 xmlSchemaFacetPtr fac;
15913
15914 for (fac = type->facets; fac != NULL; fac = fac->next) {
15915 switch (fac->type) {
15916 case XML_SCHEMA_FACET_WHITESPACE:
15917 break;
15918 case XML_SCHEMA_FACET_PATTERN:
15919 normVal = 1;
15920 has = 1;
15921 break;
15922 case XML_SCHEMA_FACET_ENUMERATION:
15923 needVal = 1;
15924 normVal = 1;
15925 has = 1;
15926 break;
15927 default:
15928 has = 1;
15929 break;
15930 }
15931 }
15932 }
15933 if (normVal)
15934 type->flags |= XML_SCHEMAS_TYPE_NORMVALUENEEDED;
15935 if (needVal)
15936 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
15937 if (has)
15938 type->flags |= XML_SCHEMAS_TYPE_HAS_FACETS;
15939
15940 if (has && (! needVal) && VARIETY_ATOMIC(type)) {
15941 xmlSchemaTypePtr prim = xmlSchemaGetPrimitiveType(type);
15942 /*
15943 * OPTIMIZE VAL TODO: Some facets need a computed value.
15944 */
15945 if ((prim->builtInType != XML_SCHEMAS_ANYSIMPLETYPE) &&
15946 (prim->builtInType != XML_SCHEMAS_STRING)) {
15947 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
15948 }
15949 }
15950}
15951
15952static int
15953xmlSchemaTypeFixupWhitespace(xmlSchemaTypePtr type)
15954{
15955
15956
15957 /*
15958 * Evaluate the whitespace-facet value.
15959 */
15960 if (VARIETY_LIST(type)) {
15961 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
15962 return (0);
15963 } else if (VARIETY_UNION(type))
15964 return (0);
15965
15966 if (type->facetSet != NULL) {
15967 xmlSchemaFacetLinkPtr lin;
15968
15969 for (lin = type->facetSet; lin != NULL; lin = lin->next) {
15970 if (lin->facet->type == XML_SCHEMA_FACET_WHITESPACE) {
15971 switch (lin->facet->whitespace) {
15972 case XML_SCHEMAS_FACET_PRESERVE:
15973 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
15974 break;
15975 case XML_SCHEMAS_FACET_REPLACE:
15976 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
15977 break;
15978 case XML_SCHEMAS_FACET_COLLAPSE:
15979 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
15980 break;
15981 default:
15982 return (-1);
15983 }
15984 return (0);
15985 }
15986 }
15987 }
15988 /*
15989 * For all ·atomic· datatypes other than string (and types ·derived·
15990 * by ·restriction· from it) the value of whiteSpace is fixed to
15991 * collapse
15992 */
15993 {
15994 xmlSchemaTypePtr anc;
15995
15996 for (anc = type->baseType; anc != NULL &&
15997 anc->builtInType != XML_SCHEMAS_ANYTYPE;
15998 anc = anc->baseType) {
15999
16000 if (anc->type == XML_SCHEMA_TYPE_BASIC) {
16001 if (anc->builtInType == XML_SCHEMAS_NORMSTRING) {
16002 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
16003
16004 } else if ((anc->builtInType == XML_SCHEMAS_STRING) ||
16005 (anc->builtInType == XML_SCHEMAS_ANYSIMPLETYPE)) {
16006 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
16007
16008 } else
16009 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
16010 break;
16011 }
16012 }
16013 return (0);
16014 }
16015 return (0);
16016}
16017
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016018/**
Daniel Veillard4255d502002-04-16 15:50:10 +000016019 * xmlSchemaTypeFixup:
16020 * @typeDecl: the schema type definition
16021 * @ctxt: the schema parser context
16022 *
16023 * Fixes the content model of the type.
16024 */
16025static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016026xmlSchemaTypeFixup(xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016027 xmlSchemaParserCtxtPtr pctxt, const xmlChar * name)
Daniel Veillard4255d502002-04-16 15:50:10 +000016028{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016029 if (type == NULL)
Daniel Veillard82bbbd42003-05-11 20:16:09 +000016030 return;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016031 if ((type->type != XML_SCHEMA_TYPE_COMPLEX) &&
16032 (type->type != XML_SCHEMA_TYPE_SIMPLE))
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000016033 return;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016034 if (! IS_NOT_TYPEFIXED(type))
16035 return;
16036 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
Daniel Veillard4255d502002-04-16 15:50:10 +000016037 if (name == NULL)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016038 name = type->name;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000016039
16040 if (type->baseType == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016041 xmlSchemaPCustomErr(pctxt,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000016042 XML_SCHEMAP_INTERNAL,
16043 NULL, type, NULL,
16044 "Internal error: xmlSchemaTypeFixup, "
16045 "baseType is missing on '%s'", type->name);
16046 return;
16047 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016048
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016049 if (type->type == XML_SCHEMA_TYPE_COMPLEX) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000016050 xmlSchemaTypePtr baseType = type->baseType;
Daniel Veillard01fa6152004-06-29 17:04:39 +000016051
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016052 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000016053 * Type-fix the base type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016054 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016055 if (IS_NOT_TYPEFIXED(baseType))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016056 xmlSchemaTypeFixup(baseType, pctxt, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016057 if (baseType->flags & XML_SCHEMAS_TYPE_INTERNAL_INVALID) {
16058 /*
16059 * Skip fixup if the base type is invalid.
16060 * TODO: Generate a warning!
16061 */
16062 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016063 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016064 /*
16065 * This basically checks if the base type can be derived.
16066 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016067 if (xmlSchemaCheckSRCCT(pctxt, type) != 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016068 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
16069 return;
16070 }
16071 /*
16072 * Fixup the content type.
16073 */
16074 if (type->contentType == XML_SCHEMA_CONTENT_SIMPLE) {
16075 /*
16076 * Corresponds to <complexType><simpleContent>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016077 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016078 if ((IS_COMPLEX_TYPE(baseType)) &&
16079 (baseType->contentTypeDef != NULL) &&
16080 (type->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016081 xmlSchemaTypePtr contentBase, content;
16082 char buf[30];
16083 const xmlChar *tmpname;
16084 /*
16085 * SPEC (1) If <restriction> + base type is <complexType>,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016086 * "whose own {content type} is a simple type..."
Daniel Veillard01fa6152004-06-29 17:04:39 +000016087 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016088 if (type->contentTypeDef != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016089 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016090 * SPEC (1.1) "the simple type definition corresponding to the
16091 * <simpleType> among the [children] of <restriction> if there
16092 * is one;"
16093 * Note that this "<simpleType> among the [children]" was put
16094 * into ->contentTypeDef during parsing.
Daniel Veillard01fa6152004-06-29 17:04:39 +000016095 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016096 contentBase = type->contentTypeDef;
16097 type->contentTypeDef = NULL;
16098 } else {
16099 /*
16100 * (1.2) "...otherwise (<restriction> has no <simpleType>
16101 * among its [children]), the simple type definition which
16102 * is the {content type} of the ... base type."
16103 */
16104 contentBase = baseType->contentTypeDef;
Daniel Veillard01fa6152004-06-29 17:04:39 +000016105 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016106 /*
16107 * SPEC
16108 * "... a simple type definition which restricts the simple
16109 * type definition identified in clause 1.1 or clause 1.2
16110 * with a set of facet components"
16111 *
16112 * Create the anonymous simple type, which will be the content
16113 * type of the complex type.
16114 */
16115 snprintf(buf, 29, "#scST%d", ++(pctxt->counter));
16116 tmpname = xmlDictLookup(pctxt->dict, BAD_CAST buf, -1);
16117 content = xmlSchemaAddType(pctxt,
16118 pctxt->schema, tmpname, tmpname, type->node);
16119 if (content == NULL)
16120 return;
16121 /*
16122 * We will use the same node as for the <complexType>
16123 * to have it somehow anchored in the schema doc.
16124 */
16125 content->node = type->node;
16126 content->type = XML_SCHEMA_TYPE_SIMPLE;
16127 content->contentType = XML_SCHEMA_CONTENT_SIMPLE;
16128 content->baseType = contentBase;
16129 /*
16130 * Move the facets, previously anchored on the complexType.
16131 */
16132 content->facets = type->facets;
16133 type->facets = NULL;
16134 content->facetSet = type->facetSet;
16135 type->facetSet = NULL;
16136
16137 type->contentTypeDef = content;
16138 if (IS_NOT_TYPEFIXED(contentBase))
16139 xmlSchemaTypeFixup(contentBase, pctxt, NULL);
16140 xmlSchemaTypeFixup(content, pctxt, NULL);
16141
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016142 } else if ((IS_COMPLEX_TYPE(baseType)) &&
16143 (baseType->contentType == XML_SCHEMA_CONTENT_MIXED) &&
16144 (type->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION)) {
16145 /*
16146 * SPEC (2) If <restriction> + base is a mixed <complexType> with
16147 * an emptiable particle, then a simple type definition which
16148 * restricts the <restriction>'s <simpleType> child.
Daniel Veillard01fa6152004-06-29 17:04:39 +000016149 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016150 if ((type->contentTypeDef == NULL) ||
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016151 (type->contentTypeDef->baseType == NULL)) {
16152 /*
16153 * TODO: Check if this ever happens.
16154 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016155 xmlSchemaPCustomErr(pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016156 XML_SCHEMAP_INTERNAL,
16157 NULL, type, NULL,
16158 "Internal error: xmlSchemaTypeFixup, "
16159 "complex type '%s': the <simpleContent><restriction> "
16160 "is missing a <simpleType> child, but was not catched "
16161 "by xmlSchemaCheckSRCCT()", type->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016162 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016163 } else if ((IS_COMPLEX_TYPE(baseType)) &&
16164 (type->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION)) {
16165 /*
16166 * SPEC (3) If <extension> + base is <complexType> with
16167 * <simpleType> content, "...then the {content type} of that
16168 * complex type definition"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016169 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016170 if (baseType->contentTypeDef == NULL) {
16171 /*
16172 * TODO: Check if this ever happens. xmlSchemaCheckSRCCT
16173 * should have catched this already.
16174 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016175 xmlSchemaPCustomErr(pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016176 XML_SCHEMAP_INTERNAL,
16177 NULL, type, NULL,
16178 "Internal error: xmlSchemaTypeFixup, "
16179 "complex type '%s': the <extension>ed base type is "
16180 "a complex type with no simple content type",
16181 type->name);
16182 }
16183 type->contentTypeDef = baseType->contentTypeDef;
16184 } else if ((IS_SIMPLE_TYPE(baseType)) &&
16185 (type->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION)) {
16186 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016187 * SPEC (4) <extension> + base is <simpleType>
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016188 * "... then that simple type definition"
16189 */
16190 type->contentTypeDef = baseType;
16191 } else {
16192 /*
16193 * TODO: Check if this ever happens.
16194 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016195 xmlSchemaPCustomErr(pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016196 XML_SCHEMAP_INTERNAL,
16197 NULL, type, NULL,
16198 "Internal error: xmlSchemaTypeFixup, "
16199 "complex type '%s' with <simpleContent>: unhandled "
16200 "derivation case", type->name);
16201 }
16202 } else {
16203 int dummySequence = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016204 xmlSchemaParticlePtr particle =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016205 (xmlSchemaParticlePtr) type->subtypes;
16206 /*
16207 * Corresponds to <complexType><complexContent>...
16208 *
16209 * NOTE that the effective mixed was already set during parsing of
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016210 * <complexType> and <complexContent>; its flag value is
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016211 * XML_SCHEMAS_TYPE_MIXED.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016212 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016213 * Compute the "effective content":
16214 * (2.1.1) + (2.1.2) + (2.1.3)
16215 */
16216 if ((particle == NULL) ||
16217 ((particle->type == XML_SCHEMA_TYPE_PARTICLE) &&
16218 ((particle->children->type == XML_SCHEMA_TYPE_ALL) ||
16219 (particle->children->type == XML_SCHEMA_TYPE_SEQUENCE) ||
16220 ((particle->children->type == XML_SCHEMA_TYPE_CHOICE) &&
16221 (particle->minOccurs == 0))) &&
16222 ( ((xmlSchemaTreeItemPtr) particle->children)->children == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016223 if (type->flags & XML_SCHEMAS_TYPE_MIXED) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016224 /*
16225 * SPEC (2.1.4) "If the ·effective mixed· is true, then
16226 * a particle whose properties are as follows:..."
16227 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016228 * Empty sequence model group with
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016229 * minOccurs/maxOccurs = 1 (i.e. a "particle emptiable").
16230 * NOTE that we sill assign it the <complexType> node to
16231 * somehow anchor it in the doc.
16232 */
16233 if ((particle == NULL) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016234 (particle->children->type != XML_SCHEMA_TYPE_SEQUENCE)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000016235 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016236 * Create the particle.
Daniel Veillard01fa6152004-06-29 17:04:39 +000016237 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016238 particle = xmlSchemaAddParticle(pctxt, pctxt->schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016239 type->node, 1, 1);
16240 if (particle == NULL)
16241 return;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000016242 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016243 * Create the model group.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000016244 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016245 particle->children = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016246 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016247 XML_SCHEMA_TYPE_SEQUENCE, NULL, type->node);
16248 if (particle->children == NULL)
16249 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016250
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016251 type->subtypes = (xmlSchemaTypePtr) particle;
Daniel Veillard01fa6152004-06-29 17:04:39 +000016252 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016253 dummySequence = 1;
16254 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
16255 } else {
16256 /*
16257 * SPEC (2.1.5) "otherwise empty"
16258 */
16259 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016260 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016261 } else {
Daniel Veillard01fa6152004-06-29 17:04:39 +000016262 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016263 * SPEC (2.2) "otherwise the particle corresponding to the
16264 * <all>, <choice>, <group> or <sequence> among the
16265 * [children]."
Daniel Veillard01fa6152004-06-29 17:04:39 +000016266 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016267 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
16268 }
16269 /*
16270 * Compute the "content type".
16271 */
16272 if (type->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION) {
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016273 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016274 * SPEC (3.1) "If <restriction>..."
16275 * (3.1.1) + (3.1.2) */
16276 if (type->contentType != XML_SCHEMA_CONTENT_EMPTY) {
16277 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
16278 type->contentType = XML_SCHEMA_CONTENT_MIXED;
16279 }
16280 } else {
16281 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016282 * SPEC (3.2) "If <extension>..."
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016283 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016284 if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16285 /*
16286 * SPEC (3.2.1)
16287 */
16288 type->contentType = baseType->contentType;
16289 type->subtypes = baseType->subtypes;
16290 /*
16291 * NOTE that the effective mixed is ignored here.
16292 */
16293 } else if (baseType->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16294 /*
16295 * SPEC (3.2.2)
16296 */
16297 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
16298 type->contentType = XML_SCHEMA_CONTENT_MIXED;
16299 } else {
16300 /*
16301 * SPEC (3.2.3)
16302 */
16303 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
16304 type->contentType = XML_SCHEMA_CONTENT_MIXED;
16305 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016306 * "A model group whose {compositor} is sequence and whose
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016307 * {particles} are..."
16308 */
16309 if (! dummySequence) {
16310 xmlSchemaTreeItemPtr effectiveContent =
16311 (xmlSchemaTreeItemPtr) type->subtypes;
16312 /*
16313 * Create the particle.
16314 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016315 particle = xmlSchemaAddParticle(pctxt, pctxt->schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016316 type->node, 1, 1);
16317 if (particle == NULL)
16318 return;
16319 /*
16320 * Create the "sequence" model group.
16321 */
16322 particle->children = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016323 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016324 XML_SCHEMA_TYPE_SEQUENCE, NULL, type->node);
16325 if (particle->children == NULL)
16326 return;
16327 type->subtypes = (xmlSchemaTypePtr) particle;
16328 /*
16329 * SPEC "the particle of the {content type} of
16330 * the ... base ..."
16331 * Create a duplicate of the base type's particle
16332 * and assign its "term" to it.
16333 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016334 particle->children->children =
16335 (xmlSchemaTreeItemPtr) xmlSchemaAddParticle(pctxt,
16336 pctxt->schema, type->node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016337 ((xmlSchemaParticlePtr) type->subtypes)->minOccurs,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016338 ((xmlSchemaParticlePtr) type->subtypes)->maxOccurs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016339 if (particle->children->children == NULL)
16340 return;
16341 particle = (xmlSchemaParticlePtr)
16342 particle->children->children;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016343 particle->children =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016344 ((xmlSchemaParticlePtr) baseType->subtypes)->children;
16345 /*
16346 * SPEC "followed by the ·effective content·."
16347 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016348 particle->next = effectiveContent;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016349 } else {
16350 /*
16351 * This is the case when there is already an empty
16352 * <sequence> with minOccurs==maxOccurs==1.
16353 * Just add the base types's content type.
16354 * NOTE that, although we miss to add an intermediate
16355 * <sequence>, this should produce no difference to
16356 * neither the regex compilation of the content model,
16357 * nor to the complex type contraints.
16358 */
16359 particle->children->children =
16360 (xmlSchemaTreeItemPtr) baseType->subtypes;
16361 }
16362 }
16363 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016364 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016365 /*
16366 * Apply the complex type component constraints; this will not
16367 * check attributes, since this is done in
16368 * xmlSchemaBuildAttributeValidation().
16369 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016370 if (xmlSchemaCheckCTComponent(pctxt, type) != 0)
16371 return;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016372 /*
16373 * Inherit & check constraints for attributes.
16374 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016375 xmlSchemaBuildAttributeValidation(pctxt, type);
16376 } else if (type->type == XML_SCHEMA_TYPE_SIMPLE) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016377 /*
16378 * Simple Type Definition Schema Component
16379 */
16380 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016381 if (VARIETY_LIST(type)) {
16382 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016383 * Corresponds to <simpleType><list>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016384 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016385 if (type->subtypes == NULL) {
16386 /*
16387 * This one is really needed, so get out.
16388 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016389 PERROR_INT("xmlSchemaTypeFixup",
16390 "list type has no item-type assigned");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016391 return;
16392 }
16393 if (IS_NOT_TYPEFIXED(type->subtypes))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016394 xmlSchemaTypeFixup(type->subtypes, pctxt, NULL);
16395 } else if (VARIETY_UNION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016396 /*
16397 * Corresponds to <simpleType><union>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016398 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016399 if (type->memberTypes == NULL) {
16400 /*
16401 * This one is really needed, so get out.
16402 */
16403 return;
16404 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016405 if (xmlSchemaFinishMemberTypeDefinitionsProperty(pctxt, type) == -1)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000016406 return;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016407 } else {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000016408 xmlSchemaTypePtr baseType = type->baseType;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016409 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016410 * Corresponds to <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016411 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016412 if (IS_NOT_TYPEFIXED(baseType))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016413 xmlSchemaTypeFixup(baseType, pctxt, NULL);
16414 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016415 * Variety
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016416 * If the <restriction> alternative is chosen, then the
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016417 * {variety} of the {base type definition}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016418 */
16419 if (VARIETY_ATOMIC(baseType))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016420 type->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016421 else if (VARIETY_LIST(baseType)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016422 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
16423 /*
16424 * Inherit the itemType.
16425 */
16426 type->subtypes = baseType->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016427 } else if (VARIETY_UNION(baseType)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016428 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
16429 /*
16430 * NOTE that we won't assign the memberTypes of the base,
16431 * since this will make trouble when freeing them; we will
16432 * use a lookup function to access them instead.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016433 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016434 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016435 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016436 /*
16437 * Check constraints.
16438 *
16439 * TODO: Split this somehow, we need to know first if we can derive
16440 * from the base type at all!
16441 */
16442 if (type->baseType != NULL) {
16443 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016444 * Schema Component Constraint: Simple Type Restriction
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016445 * (Facets)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016446 * NOTE: Satisfaction of 1 and 2 arise from the fixup
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016447 * applied beforehand.
16448 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016449 xmlSchemaCheckSRCSimpleType(pctxt, type);
16450 xmlSchemaCheckFacetValues(type, pctxt);
16451 if ((type->facetSet != NULL) ||
16452 (type->baseType->facetSet != NULL))
16453 xmlSchemaDeriveAndValidateFacets(pctxt, type);
16454 /*
16455 * Whitespace value.
16456 */
16457 xmlSchemaTypeFixupWhitespace(type);
16458 xmlSchemaTypeFixupOptimFacets(type);
16459 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000016460 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016461
Daniel Veillard8651f532002-04-17 09:06:27 +000016462#ifdef DEBUG_TYPE
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016463 if (type->node != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000016464 xmlGenericError(xmlGenericErrorContext,
16465 "Type of %s : %s:%d :", name,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016466 type->node->doc->URL,
16467 xmlGetLineNo(type->node));
Daniel Veillarddecd64d2002-04-18 14:41:51 +000016468 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000016469 xmlGenericError(xmlGenericErrorContext, "Type of %s :", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000016470 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016471 if ((IS_SIMPLE_TYPE(type)) || (IS_COMPLEX_TYPE(type))) {
16472 switch (type->contentType) {
16473 case XML_SCHEMA_CONTENT_SIMPLE:
16474 xmlGenericError(xmlGenericErrorContext, "simple\n");
16475 break;
16476 case XML_SCHEMA_CONTENT_ELEMENTS:
16477 xmlGenericError(xmlGenericErrorContext, "elements\n");
16478 break;
16479 case XML_SCHEMA_CONTENT_UNKNOWN:
16480 xmlGenericError(xmlGenericErrorContext, "unknown !!!\n");
16481 break;
16482 case XML_SCHEMA_CONTENT_EMPTY:
16483 xmlGenericError(xmlGenericErrorContext, "empty\n");
16484 break;
16485 case XML_SCHEMA_CONTENT_MIXED:
16486 if (xmlSchemaIsParticleEmptiable((xmlSchemaParticlePtr)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016487 type->subtypes))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016488 xmlGenericError(xmlGenericErrorContext,
16489 "mixed as emptiable particle\n");
16490 else
16491 xmlGenericError(xmlGenericErrorContext, "mixed\n");
16492 break;
16493 /* Removed, since not used. */
16494 /*
16495 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
16496 xmlGenericError(xmlGenericErrorContext, "mixed or elems\n");
16497 break;
16498 */
16499 case XML_SCHEMA_CONTENT_BASIC:
16500 xmlGenericError(xmlGenericErrorContext, "basic\n");
16501 break;
16502 default:
16503 xmlGenericError(xmlGenericErrorContext,
16504 "not registered !!!\n");
16505 break;
16506 }
Daniel Veillard8651f532002-04-17 09:06:27 +000016507 }
16508#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000016509}
16510
16511/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000016512 * xmlSchemaCheckFacet:
16513 * @facet: the facet
16514 * @typeDecl: the schema type definition
Daniel Veillard81562d22005-06-15 13:27:56 +000016515 * @pctxt: the schema parser context or NULL
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016516 * @name: the optional name of the type
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000016517 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016518 * Checks and computes the values of facets.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000016519 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016520 * Returns 0 if valid, a positive error code if not valid and
16521 * -1 in case of an internal or API error.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000016522 */
16523int
16524xmlSchemaCheckFacet(xmlSchemaFacetPtr facet,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000016525 xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016526 xmlSchemaParserCtxtPtr pctxt,
16527 const xmlChar * name ATTRIBUTE_UNUSED)
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000016528{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016529 int ret = 0, ctxtGiven;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016530
Daniel Veillardce682bc2004-11-05 17:22:25 +000016531 if ((facet == NULL) || (typeDecl == NULL))
16532 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016533 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016534 * TODO: will the parser context be given if used from
16535 * the relaxNG module?
16536 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016537 if (pctxt == NULL)
16538 ctxtGiven = 0;
16539 else
16540 ctxtGiven = 1;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000016541
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000016542 switch (facet->type) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000016543 case XML_SCHEMA_FACET_MININCLUSIVE:
16544 case XML_SCHEMA_FACET_MINEXCLUSIVE:
16545 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Daniel Veillardf4b05d02004-07-05 13:10:37 +000016546 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
16547 case XML_SCHEMA_FACET_ENUMERATION: {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000016548 /*
16549 * Okay we need to validate the value
16550 * at that point.
16551 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000016552 xmlSchemaTypePtr base;
Daniel Veillardf4b05d02004-07-05 13:10:37 +000016553
16554 /* 4.3.5.5 Constraints on enumeration Schema Components
16555 * Schema Component Constraint: enumeration valid restriction
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016556 * It is an ·error· if any member of {value} is not in the
16557 * ·value space· of {base type definition}.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000016558 *
16559 * minInclusive, maxInclusive, minExclusive, maxExclusive:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016560 * The value ·must· be in the
16561 * ·value space· of the ·base type·.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000016562 */
16563 /*
16564 * This function is intended to deliver a compiled value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000016565 * on the facet. In this implementation of XML Schemata the
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016566 * type holding a facet, won't be a built-in type.
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000016567 * Thus to ensure that other API
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016568 * calls (relaxng) do work, if the given type is a built-in
Daniel Veillardf4b05d02004-07-05 13:10:37 +000016569 * type, we will assume that the given built-in type *is
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016570 * already* the base type.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000016571 */
16572 if (typeDecl->type != XML_SCHEMA_TYPE_BASIC) {
16573 base = typeDecl->baseType;
16574 if (base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016575 PERROR_INT("xmlSchemaCheckFacet",
16576 "a type user derived type has no base type");
Daniel Veillardf4b05d02004-07-05 13:10:37 +000016577 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016578 }
Daniel Veillardf4b05d02004-07-05 13:10:37 +000016579 } else
16580 base = typeDecl;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016581
16582 if (! ctxtGiven) {
16583 /*
16584 * A context is needed if called from RelaxNG.
16585 */
16586 pctxt = xmlSchemaNewParserCtxt("*");
16587 if (pctxt == NULL)
16588 return (-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000016589 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000016590 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016591 * NOTE: This call does not check the content nodes,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000016592 * since they are not available:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016593 * facet->node is just the node holding the facet
16594 * definition, *not* the attribute holding the *value*
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000016595 * of the facet.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016596 */
16597 ret = xmlSchemaVCheckCVCSimpleType(
16598 (xmlSchemaAbstractCtxtPtr) pctxt, facet->node, base,
16599 facet->value, &(facet->val), 1, 1, 0);
16600 if (ret != 0) {
16601 if (ret < 0) {
16602 /* No error message for RelaxNG. */
16603 if (ctxtGiven) {
16604 xmlSchemaCustomErr((xmlSchemaAbstractCtxtPtr) pctxt,
16605 XML_SCHEMAP_INTERNAL, facet->node, NULL,
16606 "Internal error: xmlSchemaCheckFacet, "
16607 "failed to validate the value '%s' of the "
16608 "facet '%s' against the base type",
16609 facet->value, xmlSchemaFacetTypeToString(facet->type));
16610 }
16611 goto internal_error;
16612 }
16613 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
16614 /* No error message for RelaxNG. */
16615 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000016616 xmlChar *str = NULL;
16617
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016618 xmlSchemaCustomErr((xmlSchemaAbstractCtxtPtr) pctxt,
16619 ret, facet->node, (xmlSchemaTypePtr) facet,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000016620 "The value '%s' of the facet does not validate "
16621 "against the base type '%s'",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016622 facet->value,
16623 xmlSchemaFormatQName(&str,
16624 base->targetNamespace, base->name));
16625 FREE_AND_NULL(str);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000016626 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016627 goto exit;
16628 } else if (facet->val == NULL) {
16629 if (ctxtGiven) {
16630 PERROR_INT("xmlSchemaCheckFacet",
16631 "value was not computed");
16632 }
16633 TODO
16634 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000016635 break;
16636 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000016637 case XML_SCHEMA_FACET_PATTERN:
16638 facet->regexp = xmlRegexpCompile(facet->value);
16639 if (facet->regexp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016640 ret = XML_SCHEMAP_REGEXP_INVALID;
16641 /* No error message for RelaxNG. */
16642 if (ctxtGiven) {
16643 xmlSchemaCustomErr((xmlSchemaAbstractCtxtPtr) pctxt,
16644 ret, facet->node, typeDecl,
16645 "The value '%s' of the facet 'pattern' is not a "
16646 "valid regular expression",
16647 facet->value, NULL);
16648 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000016649 }
16650 break;
16651 case XML_SCHEMA_FACET_TOTALDIGITS:
16652 case XML_SCHEMA_FACET_FRACTIONDIGITS:
16653 case XML_SCHEMA_FACET_LENGTH:
16654 case XML_SCHEMA_FACET_MAXLENGTH:
16655 case XML_SCHEMA_FACET_MINLENGTH:{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016656 ret = xmlSchemaValidatePredefinedType(
16657 xmlSchemaGetBuiltInType(XML_SCHEMAS_NNINTEGER),
16658 facet->value, &(facet->val));
16659 if (ret != 0) {
16660 if (ret < 0) {
16661 /* No error message for RelaxNG. */
16662 if (ctxtGiven) {
16663 PERROR_INT("xmlSchemaCheckFacet",
16664 "validating facet value");
16665 }
16666 goto internal_error;
16667 }
16668 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
16669 /* No error message for RelaxNG. */
16670 if (ctxtGiven) {
16671 /* error code */
16672 xmlSchemaCustomErr((xmlSchemaAbstractCtxtPtr) pctxt,
16673 ret, facet->node, typeDecl,
16674 "The value '%s' of the facet '%s' is not a valid "
16675 "'nonNegativeInteger'",
16676 facet->value,
16677 xmlSchemaFacetTypeToString(facet->type));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000016678 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000016679 }
16680 break;
16681 }
16682 case XML_SCHEMA_FACET_WHITESPACE:{
16683 if (xmlStrEqual(facet->value, BAD_CAST "preserve")) {
16684 facet->whitespace = XML_SCHEMAS_FACET_PRESERVE;
16685 } else if (xmlStrEqual(facet->value, BAD_CAST "replace")) {
16686 facet->whitespace = XML_SCHEMAS_FACET_REPLACE;
16687 } else if (xmlStrEqual(facet->value, BAD_CAST "collapse")) {
16688 facet->whitespace = XML_SCHEMAS_FACET_COLLAPSE;
16689 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016690 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
16691 /* No error message for RelaxNG. */
16692 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000016693 /* error was previously: XML_SCHEMAP_INVALID_WHITE_SPACE */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016694 xmlSchemaCustomErr((xmlSchemaAbstractCtxtPtr) pctxt,
16695 ret, facet->node, typeDecl,
16696 "The value '%s' of the facet 'whitespace' is not "
16697 "valid", facet->value, NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000016698 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000016699 }
16700 }
16701 default:
16702 break;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000016703 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016704exit:
16705 if ((! ctxtGiven) && (pctxt != NULL))
16706 xmlSchemaFreeParserCtxt(pctxt);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000016707 return (ret);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016708internal_error:
16709 if ((! ctxtGiven) && (pctxt != NULL))
16710 xmlSchemaFreeParserCtxt(pctxt);
16711 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000016712}
16713
16714/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016715 * xmlSchemaCheckFacetValues:
Daniel Veillard4255d502002-04-16 15:50:10 +000016716 * @typeDecl: the schema type definition
16717 * @ctxt: the schema parser context
16718 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016719 * Checks the default values types, especially for facets
Daniel Veillard4255d502002-04-16 15:50:10 +000016720 */
16721static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016722xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
16723 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000016724{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016725 const xmlChar *name = typeDecl->name;
Daniel Veillard01fa6152004-06-29 17:04:39 +000016726 /*
16727 * NOTE: It is intended to use the facets list, instead
16728 * of facetSet.
16729 */
16730 if (typeDecl->facets != NULL) {
16731 xmlSchemaFacetPtr facet = typeDecl->facets;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016732
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000016733 /*
16734 * Temporarily assign the "schema" to the validation context
16735 * of the parser context. This is needed for NOTATION validation.
16736 */
16737 if (ctxt->vctxt == NULL) {
16738 if (xmlSchemaCreateVCtxtOnPCtxt(ctxt) == -1)
16739 return;
16740 }
16741 ctxt->vctxt->schema = ctxt->schema;
16742
Daniel Veillard01fa6152004-06-29 17:04:39 +000016743 while (facet != NULL) {
16744 xmlSchemaCheckFacet(facet, typeDecl, ctxt, name);
16745 facet = facet->next;
16746 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000016747
16748 ctxt->vctxt->schema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016749 }
Daniel Veillard4255d502002-04-16 15:50:10 +000016750}
16751
16752/**
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016753 * xmlSchemaGetCircModelGrDefRef:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016754 * @ctxtMGroup: the searched model group
16755 * @selfMGroup: the second searched model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016756 * @particle: the first particle
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016757 *
16758 * This one is intended to be used by
16759 * xmlSchemaCheckGroupDefCircular only.
16760 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016761 * Returns the particle with the circular model group definition reference,
16762 * otherwise NULL.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016763 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016764static xmlSchemaTreeItemPtr
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000016765xmlSchemaGetCircModelGrDefRef(xmlSchemaModelGroupDefPtr groupDef,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016766 xmlSchemaTreeItemPtr particle)
16767{
16768 xmlSchemaTreeItemPtr circ = NULL;
16769 xmlSchemaTreeItemPtr term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000016770 xmlSchemaModelGroupDefPtr gdef;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016771
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000016772 for (; particle != NULL; particle = particle->next) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016773 term = particle->children;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000016774 if (term == NULL)
16775 continue;
16776 switch (term->type) {
16777 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016778 gdef = (xmlSchemaModelGroupDefPtr) term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000016779 if (gdef == groupDef)
16780 return (particle);
16781 /*
16782 * Mark this model group definition to avoid infinite
16783 * recursion on circular references not yet examined.
16784 */
16785 if (gdef->flags & XML_SCHEMA_MODEL_GROUP_DEF_MARKED)
16786 continue;
16787 if (gdef->children != NULL) {
16788 gdef->flags |= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
16789 circ = xmlSchemaGetCircModelGrDefRef(groupDef,
16790 gdef->children->children);
16791 gdef->flags ^= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
16792 if (circ != NULL)
16793 return (circ);
16794 }
16795 break;
16796 case XML_SCHEMA_TYPE_SEQUENCE:
16797 case XML_SCHEMA_TYPE_CHOICE:
16798 case XML_SCHEMA_TYPE_ALL:
16799 circ = xmlSchemaGetCircModelGrDefRef(groupDef, term->children);
16800 if (circ != NULL)
16801 return (circ);
16802 break;
16803 default:
16804 break;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016805 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016806 }
16807 return (NULL);
16808}
16809
16810/**
16811 * xmlSchemaCheckGroupDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016812 * @item: the model group definition
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016813 * @ctxt: the parser context
16814 * @name: the name
16815 *
16816 * Checks for circular references to model group definitions.
16817 */
16818static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016819xmlSchemaCheckGroupDefCircular(xmlSchemaModelGroupDefPtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016820 xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000016821 const xmlChar * name ATTRIBUTE_UNUSED)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016822{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016823 /*
16824 * Schema Component Constraint: Model Group Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016825 * 2 Circular groups are disallowed. That is, within the {particles}
16826 * of a group there must not be at any depth a particle whose {term}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016827 * is the group itself.
16828 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016829 if ((item == NULL) ||
16830 (item->type != XML_SCHEMA_TYPE_GROUP) ||
16831 (item->children == NULL))
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016832 return;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016833 {
16834 xmlSchemaTreeItemPtr circ;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016835
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000016836 circ = xmlSchemaGetCircModelGrDefRef(item, item->children->children);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016837 if (circ != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016838 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016839 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016840 * TODO: The error report is not adequate: this constraint
16841 * is defined for model groups but not definitions, but since
16842 * there cannot be any circular model groups without a model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016843 * definition (if not using a construction API), we check those
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016844 * defintions only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016845 */
16846 xmlSchemaPCustomErr(ctxt,
16847 XML_SCHEMAP_MG_PROPS_CORRECT_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016848 NULL, NULL, GET_NODE(circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016849 "Circular reference to the model group definition '%s' "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016850 "defined", xmlSchemaFormatQName(&str,
16851 item->targetNamespace, item->name));
16852 FREE_AND_NULL(str)
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016853 /*
16854 * NOTE: We will cut the reference to avoid further
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016855 * confusion of the processor. This is a fatal error.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016856 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016857 circ->children = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016858 }
16859 }
16860}
16861
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000016862/**
16863 * xmlSchemaGroupDefTermFixup:
16864 * @item: the particle with a model group definition as term
16865 * @ctxt: the parser context
16866 * @name: the name
16867 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016868 * Checks cos-all-limited.
16869 *
16870 * Assigns the model group of model group definitions to the "term"
16871 * of the referencing particle.
16872 * In xmlSchemaMiscRefFixup the model group definitions was assigned
16873 * to the "term", since needed for the circularity check.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000016874 */
16875static void
16876xmlSchemaGroupDefTermFixup(xmlSchemaParticlePtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016877 xmlSchemaParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000016878 const xmlChar * name ATTRIBUTE_UNUSED)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016879{
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000016880 if ((item == NULL) ||
16881 (item->type != XML_SCHEMA_TYPE_PARTICLE) ||
16882 (item->children == NULL) ||
16883 (item->children->type != XML_SCHEMA_TYPE_GROUP) ||
16884 (item->children->children == NULL))
16885 return;
16886 item->children = item->children->children;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016887 /*
16888 * TODO: Not nice, but we will anchor cos-all-limited here.
16889 */
16890 if ((item->children->type == XML_SCHEMA_TYPE_ALL) &&
16891 (item->maxOccurs != 1)) {
16892 /*
16893 * SPEC (1.2) "the {term} property of a particle with
16894 * {max occurs}=1which is part of a pair which constitutes the
16895 * {content type} of a complex type definition."
16896 */
16897 xmlSchemaPCustomErr(ctxt,
16898 XML_SCHEMAP_SRC_ATTRIBUTE_GROUP_3,
16899 NULL, (xmlSchemaTypePtr) item, item->node,
16900 "The particle's 'maxOccurs' must be 1, since an xs:all model "
16901 "group is its term", NULL);
16902 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000016903}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016904
16905/**
16906 * xmlSchemaGetCircAttrGrRef:
16907 * @ctxtGr: the searched attribute group
16908 * @attr: the current attribute list to be processed
16909 *
16910 * This one is intended to be used by
16911 * xmlSchemaCheckSRCAttributeGroupCircular only.
16912 *
16913 * Returns the circular attribute grou reference, otherwise NULL.
16914 */
16915static xmlSchemaAttributeGroupPtr
16916xmlSchemaGetCircAttrGrRef(xmlSchemaAttributeGroupPtr ctxtGr,
16917 xmlSchemaAttributePtr attr)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016918{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016919 xmlSchemaAttributeGroupPtr circ = NULL, gr;
16920 int marked;
16921 /*
16922 * We will search for an attribute group reference which
16923 * references the context attribute group.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016924 */
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016925 while (attr != NULL) {
16926 marked = 0;
16927 if (attr->type == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) {
16928 gr = (xmlSchemaAttributeGroupPtr) attr;
16929 if (gr->refItem != NULL) {
16930 if (gr->refItem == ctxtGr)
16931 return (gr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016932 else if (gr->refItem->flags &
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016933 XML_SCHEMAS_ATTRGROUP_MARKED) {
16934 attr = attr->next;
16935 continue;
16936 } else {
16937 /*
16938 * Mark as visited to avoid infinite recursion on
16939 * circular references not yet examined.
16940 */
16941 gr->refItem->flags |= XML_SCHEMAS_ATTRGROUP_MARKED;
16942 marked = 1;
16943 }
16944 }
16945 if (gr->attributes != NULL)
16946 circ = xmlSchemaGetCircAttrGrRef(ctxtGr, gr->attributes);
16947 /*
16948 * Unmark the visited group's attributes.
16949 */
16950 if (marked)
16951 gr->refItem->flags ^= XML_SCHEMAS_ATTRGROUP_MARKED;
16952 if (circ != NULL)
16953 return (circ);
16954 }
16955 attr = attr->next;
16956 }
16957 return (NULL);
16958}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016959
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016960/**
16961 * xmlSchemaCheckSRCAttributeGroupCircular:
16962 * attrGr: the attribute group definition
16963 * @ctxt: the parser context
16964 * @name: the name
16965 *
16966 * Checks for circular references of attribute groups.
16967 */
16968static void
16969xmlSchemaCheckAttributeGroupCircular(xmlSchemaAttributeGroupPtr attrGr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016970 xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016971 const xmlChar * name ATTRIBUTE_UNUSED)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016972{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016973 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016974 * Schema Representation Constraint:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016975 * Attribute Group Definition Representation OK
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016976 * 3 Circular group reference is disallowed outside <redefine>.
16977 * That is, unless this element information item's parent is
16978 * <redefine>, then among the [children], if any, there must
16979 * not be an <attributeGroup> with ref [attribute] which resolves
16980 * to the component corresponding to this <attributeGroup>. Indirect
16981 * circularity is also ruled out. That is, when QName resolution
16982 * (Schema Document) (§3.15.3) is applied to a ·QName· arising from
16983 * any <attributeGroup>s with a ref [attribute] among the [children],
16984 * it must not be the case that a ·QName· is encountered at any depth
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016985 * which resolves to the component corresponding to this <attributeGroup>.
16986 */
16987 /*
16988 * Only global components can be referenced.
16989 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016990 if (((attrGr->flags & XML_SCHEMAS_ATTRGROUP_GLOBAL) == 0) ||
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016991 (attrGr->attributes == NULL))
16992 return;
16993 else {
16994 xmlSchemaAttributeGroupPtr circ;
16995
16996 circ = xmlSchemaGetCircAttrGrRef(attrGr, attrGr->attributes);
16997 if (circ != NULL) {
16998 /*
16999 * TODO: Report the referenced attr group as QName.
17000 */
17001 xmlSchemaPCustomErr(ctxt,
17002 XML_SCHEMAP_SRC_ATTRIBUTE_GROUP_3,
17003 NULL, NULL, circ->node,
17004 "Circular reference to the attribute group '%s' "
17005 "defined", attrGr->name);
17006 /*
17007 * NOTE: We will cut the reference to avoid further
17008 * confusion of the processor.
17009 * BADSPEC: The spec should define how to process in this case.
17010 */
17011 circ->attributes = NULL;
17012 circ->refItem = NULL;
17013 }
17014 }
17015}
17016
17017/**
Daniel Veillard13e04c62002-04-23 17:51:29 +000017018 * xmlSchemaAttrGrpFixup:
17019 * @attrgrpDecl: the schema attribute definition
17020 * @ctxt: the schema parser context
17021 * @name: the attribute name
17022 *
17023 * Fixes finish doing the computations on the attributes definitions
17024 */
17025static void
Daniel Veillard3646d642004-06-02 19:19:14 +000017026xmlSchemaAttrGrpFixup(xmlSchemaAttributeGroupPtr attrgrp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017027 xmlSchemaParserCtxtPtr ctxt, const xmlChar * name)
Daniel Veillard13e04c62002-04-23 17:51:29 +000017028{
17029 if (name == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000017030 name = attrgrp->name;
17031 if (attrgrp->attributes != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017032 return;
Daniel Veillard3646d642004-06-02 19:19:14 +000017033 if (attrgrp->ref != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017034 xmlSchemaAttributeGroupPtr ref;
Daniel Veillard13e04c62002-04-23 17:51:29 +000017035
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017036 ref = xmlSchemaGetAttributeGroup(ctxt->schema, attrgrp->ref,
Daniel Veillardc0826a72004-08-10 14:17:33 +000017037 attrgrp->refNs);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017038 if (ref == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017039 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000017040 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017041 (xmlSchemaTypePtr) attrgrp, attrgrp->node,
17042 "ref", attrgrp->ref, attrgrp->refNs,
Daniel Veillardc0826a72004-08-10 14:17:33 +000017043 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017044 return;
17045 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017046 attrgrp->refItem = ref;
17047 /*
17048 * Check for self reference!
17049 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017050 xmlSchemaAttrGrpFixup(ref, ctxt, NULL);
Daniel Veillard3646d642004-06-02 19:19:14 +000017051 attrgrp->attributes = ref->attributes;
17052 attrgrp->attributeWildcard = ref->attributeWildcard;
17053 }
Daniel Veillard13e04c62002-04-23 17:51:29 +000017054}
17055
17056/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017057 * xmlSchemaAttrCheckValConstr:
17058 * @item: an schema attribute declaration/use
17059 * @ctxt: a schema parser context
17060 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017061 *
17062 *
17063 * Schema Component Constraint: Attribute Declaration Properties Correct
17064 * (a-props-correct)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017065 * Validates the value constraints of an attribute declaration/use.
Daniel Veillard4255d502002-04-16 15:50:10 +000017066 *
17067 * Fixes finish doing the computations on the attributes definitions
17068 */
17069static void
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017070xmlSchemaCheckAttrValConstr(xmlSchemaAttributePtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017071 xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017072 const xmlChar * name ATTRIBUTE_UNUSED)
17073{
17074
17075 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017076 * 2 if there is a {value constraint}, the canonical lexical
17077 * representation of its value must be ·valid· with respect
17078 * to the {type definition} as defined in String Valid (§3.14.4).
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017079 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017080 if (item->defValue != NULL) {
17081 int ret;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017082
17083 if (item->subtypes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017084 PERROR_INT("xmlSchemaCheckAttrValConstr",
17085 "type is missing");
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017086 return;
17087 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017088 ret = xmlSchemaVCheckCVCSimpleType((xmlSchemaAbstractCtxtPtr) pctxt,
17089 item->node, item->subtypes, item->defValue, &(item->defVal),
17090 1, 1, 0);
17091 if (ret != 0) {
17092 if (ret < 0) {
17093 PERROR_INT("xmlSchemaAttrCheckValConstr",
17094 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017095 return;
17096 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017097 ret = XML_SCHEMAP_A_PROPS_CORRECT_2;
17098 xmlSchemaCustomErr((xmlSchemaAbstractCtxtPtr) pctxt,
17099 ret, item->node, (xmlSchemaTypePtr) item,
17100 "The value of the value constraint is not valid", NULL, NULL);
17101 return;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017102 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017103 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017104}
17105
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000017106static xmlSchemaElementPtr
17107xmlSchemaCheckSubstGroupCircular(xmlSchemaElementPtr elemDecl,
17108 xmlSchemaElementPtr ancestor)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017109{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000017110 xmlSchemaElementPtr ret;
17111
17112 if (SUBST_GROUP_AFF(ancestor) == NULL)
17113 return (NULL);
17114 if (SUBST_GROUP_AFF(ancestor) == elemDecl)
17115 return (ancestor);
17116
17117 if (SUBST_GROUP_AFF(ancestor)->flags & XML_SCHEMAS_ELEM_CIRCULAR)
17118 return (NULL);
17119 SUBST_GROUP_AFF(ancestor)->flags |= XML_SCHEMAS_ELEM_CIRCULAR;
17120 ret = xmlSchemaCheckSubstGroupCircular(elemDecl,
17121 SUBST_GROUP_AFF(ancestor));
17122 SUBST_GROUP_AFF(ancestor)->flags ^= XML_SCHEMAS_ELEM_CIRCULAR;
17123
17124 return (ret);
17125}
17126
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017127/**
17128 * xmlSchemaCheckElemPropsCorrect:
17129 * @ctxt: a schema parser context
17130 * @decl: the element declaration
17131 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017132 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017133 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017134 * Element Declaration Properties Correct (e-props-correct)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017135 *
17136 * STATUS:
17137 * missing: (6)
17138 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017139static int
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017140xmlSchemaCheckElemPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
17141 xmlSchemaElementPtr elemDecl)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017142{
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017143 int ret = 0;
17144 xmlSchemaTypePtr typeDef = ELEM_TYPE(elemDecl);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017145 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017146 * SPEC (1) "The values of the properties of an element declaration
17147 * must be as described in the property tableau in The Element
17148 * Declaration Schema Component (§3.3.1), modulo the impact of Missing
17149 * Sub-components (§5.3)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017150 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017151 if (SUBST_GROUP_AFF(elemDecl) != NULL) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000017152 xmlSchemaElementPtr head = SUBST_GROUP_AFF(elemDecl), circ;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017153
17154 xmlSchemaCheckElementDeclComponent(head, pctxt, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017155 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017156 * SPEC (3) "If there is a non-·absent· {substitution group
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017157 * affiliation}, then {scope} must be global."
17158 */
17159 if ((elemDecl->flags & XML_SCHEMAS_ELEM_GLOBAL) == 0) {
17160 xmlSchemaPCustomErr(pctxt,
17161 XML_SCHEMAP_E_PROPS_CORRECT_3,
17162 NULL, (xmlSchemaTypePtr) elemDecl, elemDecl->node,
17163 "Only global element declarations can have a "
17164 "substitution group affiliation", NULL);
17165 ret = XML_SCHEMAP_E_PROPS_CORRECT_3;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000017166 }
17167 /*
17168 * TODO: SPEC (6) "Circular substitution groups are disallowed.
17169 * That is, it must not be possible to return to an element declaration
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017170 * by repeatedly following the {substitution group affiliation}
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000017171 * property."
17172 */
17173 if (head == elemDecl)
17174 circ = head;
17175 else if (SUBST_GROUP_AFF(head) != NULL)
17176 circ = xmlSchemaCheckSubstGroupCircular(head, head);
17177 else
17178 circ = NULL;
17179 if (circ != NULL) {
17180 xmlChar *strA = NULL, *strB = NULL;
17181
17182 xmlSchemaPCustomErrExt(pctxt,
17183 XML_SCHEMAP_E_PROPS_CORRECT_6,
17184 NULL, (xmlSchemaTypePtr) circ, circ->node,
17185 "The element declaration '%s' defines a circular "
17186 "substitution group to element declaration '%s'",
17187 xmlSchemaGetComponentQName(&strA, circ),
17188 xmlSchemaGetComponentQName(&strB, head),
17189 NULL);
17190 FREE_AND_NULL(strA)
17191 FREE_AND_NULL(strB)
17192 ret = XML_SCHEMAP_E_PROPS_CORRECT_6;
17193 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017194 /*
17195 * SPEC (4) "If there is a {substitution group affiliation},
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017196 * the {type definition}
17197 * of the element declaration must be validly derived from the {type
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017198 * definition} of the {substitution group affiliation}, given the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017199 * of the {substitution group exclusions} of the {substitution group
17200 * affiliation}, as defined in Type Derivation OK (Complex) (§3.4.6)
17201 * (if the {type definition} is complex) or as defined in
17202 * Type Derivation OK (Simple) (§3.14.6) (if the {type definition} is
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017203 * simple)."
17204 *
17205 * NOTE: {substitution group exclusions} means the values of the
17206 * attribute "final".
17207 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000017208
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017209 if (typeDef != ELEM_TYPE(SUBST_GROUP_AFF(elemDecl))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017210 int set = 0;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017211
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017212 if (head->flags & XML_SCHEMAS_ELEM_FINAL_EXTENSION)
17213 set |= SUBSET_EXTENSION;
17214 if (head->flags & XML_SCHEMAS_ELEM_FINAL_RESTRICTION)
17215 set |= SUBSET_RESTRICTION;
17216
17217 if (xmlSchemaCheckCOSDerivedOK(typeDef,
17218 ELEM_TYPE(head), set) != 0) {
17219 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
17220
17221 ret = XML_SCHEMAP_E_PROPS_CORRECT_4;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017222 xmlSchemaPCustomErrExt(pctxt,
17223 XML_SCHEMAP_E_PROPS_CORRECT_4,
17224 NULL, (xmlSchemaTypePtr) elemDecl, elemDecl->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017225 "The type definition '%s' was "
17226 "either rejected by the substitution group "
17227 "affiliation '%s', or not validly derived from its type "
17228 "definition '%s'",
17229 xmlSchemaGetComponentQName(&strA, typeDef),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000017230 xmlSchemaGetComponentQName(&strB, head),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017231 xmlSchemaGetComponentQName(&strC, ELEM_TYPE(head)));
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017232 FREE_AND_NULL(strA)
17233 FREE_AND_NULL(strB)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017234 FREE_AND_NULL(strC)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017235 }
17236 }
17237 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017238 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017239 * SPEC (5) "If the {type definition} or {type definition}'s
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017240 * {content type}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017241 * is or is derived from ID then there must not be a {value constraint}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017242 * Note: The use of ID as a type definition for elements goes beyond
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017243 * XML 1.0, and should be avoided if backwards compatibility is desired"
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017244 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017245 if ((elemDecl->value != NULL) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017246 ((IS_SIMPLE_TYPE(typeDef) &&
17247 xmlSchemaIsDerivedFromBuiltInType(typeDef, XML_SCHEMAS_ID)) ||
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017248 (IS_COMPLEX_TYPE(typeDef) &&
17249 HAS_SIMPLE_CONTENT(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017250 xmlSchemaIsDerivedFromBuiltInType(typeDef->contentTypeDef,
17251 XML_SCHEMAS_ID)))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017252
17253 ret = XML_SCHEMAP_E_PROPS_CORRECT_5;
17254 xmlSchemaPCustomErr(pctxt,
17255 XML_SCHEMAP_E_PROPS_CORRECT_5,
17256 NULL, (xmlSchemaTypePtr) elemDecl, elemDecl->node,
17257 "The type definition (or type definition's content type) is or "
17258 "is derived from ID; value constraints are not allowed in "
17259 "conjunction with such a type definition", NULL);
17260 } else if (elemDecl->value != NULL) {
17261 int vcret;
17262 xmlNodePtr node = NULL;
17263
17264 /*
17265 * SPEC (2) "If there is a {value constraint}, the canonical lexical
17266 * representation of its value must be ·valid· with respect to the
17267 * {type definition} as defined in Element Default Valid (Immediate)
17268 * (§3.3.6)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017269 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017270 if (typeDef == NULL) {
17271 xmlSchemaPErr(pctxt, elemDecl->node,
17272 XML_SCHEMAP_INTERNAL,
17273 "Internal error: xmlSchemaCheckElemPropsCorrect, "
17274 "type is missing... skipping validation of "
17275 "the value constraint", NULL, NULL);
17276 return (-1);
17277 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017278 if (elemDecl->node != NULL) {
17279 if (elemDecl->flags & XML_SCHEMAS_ELEM_FIXED)
17280 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
17281 BAD_CAST "fixed");
17282 else
17283 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
17284 BAD_CAST "default");
17285 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017286 vcret = xmlSchemaParseCheckCOSValidDefault(pctxt, node,
17287 typeDef, elemDecl->value, &(elemDecl->defVal));
17288 if (vcret != 0) {
17289 if (vcret < 0) {
17290 PERROR_INT("xmlSchemaElemCheckValConstr",
17291 "failed to validate the value constraint of an "
17292 "element declaration");
17293 return (-1);
17294 }
17295 return (vcret);
17296 }
17297 }
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000017298
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017299 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017300}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017301
17302/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000017303 * xmlSchemaCheckElemSubstGroup:
17304 * @ctxt: a schema parser context
17305 * @decl: the element declaration
17306 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017307 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000017308 * Schema Component Constraint:
17309 * Substitution Group (cos-equiv-class)
17310 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017311 * In Libxml2 the subst. groups will be precomputed, in terms of that
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000017312 * a list will be built for each subst. group head, holding all direct
17313 * referents to this head.
17314 * NOTE that this function needs:
17315 * 1. circular subst. groups to be checked beforehand
17316 * 2. the declaration's type to be derived from the head's type
17317 *
17318 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017319 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000017320 */
17321static void
17322xmlSchemaCheckElemSubstGroup(xmlSchemaParserCtxtPtr ctxt,
17323 xmlSchemaElementPtr elemDecl)
17324{
17325 if ((SUBST_GROUP_AFF(elemDecl) == NULL) ||
17326 /* SPEC (1) "Its {abstract} is false." */
17327 (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT))
17328 return;
17329 {
17330 xmlSchemaElementPtr head;
17331 xmlSchemaTypePtr headType, type;
17332 int set, methSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017333 /*
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000017334 * SPEC (2) "It is validly substitutable for HEAD subject to HEAD's
17335 * {disallowed substitutions} as the blocking constraint, as defined in
17336 * Substitution Group OK (Transitive) (§3.3.6)."
17337 */
17338 for (head = SUBST_GROUP_AFF(elemDecl); head != NULL;
17339 head = SUBST_GROUP_AFF(head)) {
17340 set = 0;
17341 methSet = 0;
17342 /*
17343 * The blocking constraints.
17344 */
17345 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION)
17346 continue;
17347 headType = head->subtypes;
17348 type = elemDecl->subtypes;
17349 if (headType == type)
17350 goto add_member;
17351 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION)
17352 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
17353 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION)
17354 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
17355 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017356 * SPEC: Substitution Group OK (Transitive) (2.3)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000017357 * "The set of all {derivation method}s involved in the
17358 * derivation of D's {type definition} from C's {type definition}
17359 * does not intersect with the union of the blocking constraint,
17360 * C's {prohibited substitutions} (if C is complex, otherwise the
17361 * empty set) and the {prohibited substitutions} (respectively the
17362 * empty set) of any intermediate {type definition}s in the
17363 * derivation of D's {type definition} from C's {type definition}."
17364 */
17365 /*
17366 * OPTIMIZE TODO: Optimize this a bit, since, if traversing the
17367 * subst.head axis, the methSet does not need to be computed for
17368 * the full depth over and over.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017369 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000017370 /*
17371 * The set of all {derivation method}s involved in the derivation
17372 */
17373 while ((type != NULL) && (type != headType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017374 if ((type->flags &
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000017375 XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION) &&
17376 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
17377 methSet |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017378
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000017379 if ((type->flags &
17380 XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION) &&
17381 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
17382 methSet |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
17383
17384 type = type->baseType;
17385 }
17386 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017387 * The {prohibited substitutions} of all intermediate types +
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000017388 * the head's type.
17389 */
17390 type = elemDecl->subtypes->baseType;
17391 while (type != NULL) {
17392 if (IS_COMPLEX_TYPE(type)) {
17393 if ((type->flags &
17394 XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
17395 ((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) == 0))
17396 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
17397 if ((type->flags &
17398 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
17399 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
17400 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
17401 } else
17402 break;
17403 if (type == headType)
17404 break;
17405 type = type->baseType;
17406 }
17407 if ((set != 0) &&
17408 (((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
17409 (methSet & XML_SCHEMAS_TYPE_BLOCK_EXTENSION)) ||
17410 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
17411 (methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION)))) {
17412 continue;
17413 }
17414add_member:
17415 xmlSchemaAddElementSubstitutionMember(ctxt, head, elemDecl);
17416 if ((head->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) == 0)
17417 head->flags |= XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD;
17418 }
17419 }
17420}
17421
17422/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017423 * xmlSchemaCheckElementDeclComponent
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017424 * @item: an schema element declaration/particle
17425 * @ctxt: a schema parser context
17426 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017427 *
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017428 * Validates the value constraints of an element declaration.
17429 *
17430 * Fixes finish doing the computations on the element declarations.
17431 */
17432static void
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017433xmlSchemaCheckElementDeclComponent(xmlSchemaElementPtr elemDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017434 xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017435 const xmlChar * name ATTRIBUTE_UNUSED)
17436{
17437 if (elemDecl == NULL)
17438 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017439 if (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_CHECKED)
17440 return;
17441 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_CHECKED;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000017442 if (xmlSchemaCheckElemPropsCorrect(ctxt, elemDecl) == 0)
17443 xmlSchemaCheckElemSubstGroup(ctxt, elemDecl);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017444}
17445
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017446/**
17447 * xmlSchemaMiscRefFixup:
17448 * @item: an schema component
17449 * @ctxt: a schema parser context
17450 * @name: the internal name of the component
17451 *
17452 * Resolves references of misc. schema components.
17453 */
17454static void
17455xmlSchemaMiscRefFixup(xmlSchemaTreeItemPtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017456 xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017457 const xmlChar * name ATTRIBUTE_UNUSED)
17458{
17459 if (item->type == XML_SCHEMA_TYPE_PARTICLE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017460 if ((item->children != NULL) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017461 (item->children->type == XML_SCHEMA_EXTRA_QNAMEREF)) {
17462 xmlSchemaQNameRefPtr ref = (xmlSchemaQNameRefPtr) item->children;
17463 xmlSchemaTreeItemPtr refItem;
17464 /*
17465 * Resolve the reference.
17466 */
17467 item->children = NULL;
17468 refItem = xmlSchemaGetNamedComponent(ctxt->schema,
17469 ref->itemType, ref->name, ref->targetNamespace);
17470 if (refItem == NULL) {
17471 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017472 NULL, GET_NODE(item), "ref", ref->name,
17473 ref->targetNamespace, ref->itemType, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017474 } else {
17475 if (refItem->type == XML_SCHEMA_TYPE_GROUP) {
17476 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017477 * NOTE that we will assign the model group definition
17478 * itself to the "term" of the particle. This will ease
17479 * the check for circular model group definitions. After
17480 * that the "term" will be assigned the model group of the
17481 * model group definition.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017482 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017483 item->children = refItem;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017484 } else
17485 item->children = refItem;
17486 }
17487 }
17488 }
17489}
17490
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017491static int
17492xmlSchemaAreValuesEqual(xmlSchemaValPtr x,
17493 xmlSchemaValPtr y)
17494{
17495 xmlSchemaTypePtr tx, ty, ptx, pty;
17496 int ret;
17497
17498 while (x != NULL) {
17499 /* Same types. */
17500 tx = xmlSchemaGetBuiltInType(xmlSchemaGetValType(x));
17501 ty = xmlSchemaGetBuiltInType(xmlSchemaGetValType(y));
17502 ptx = xmlSchemaGetPrimitiveType(tx);
17503 pty = xmlSchemaGetPrimitiveType(ty);
17504 /*
17505 * (1) if a datatype T' is ·derived· by ·restriction· from an
17506 * atomic datatype T then the ·value space· of T' is a subset of
17507 * the ·value space· of T. */
17508 /*
17509 * (2) if datatypes T' and T'' are ·derived· by ·restriction·
17510 * from a common atomic ancestor T then the ·value space·s of T'
17511 * and T'' may overlap.
17512 */
17513 if (ptx != pty)
17514 return(0);
17515 /*
17516 * We assume computed values to be normalized, so do a fast
17517 * string comparison for string based types.
17518 */
17519 if ((ptx->builtInType == XML_SCHEMAS_STRING) ||
17520 IS_ANY_SIMPLE_TYPE(ptx)) {
17521 if (! xmlStrEqual(
17522 xmlSchemaValueGetAsString(x),
17523 xmlSchemaValueGetAsString(y)))
17524 return (0);
17525 } else {
17526 ret = xmlSchemaCompareValuesWhtsp(
17527 x, XML_SCHEMA_WHITESPACE_PRESERVE,
17528 y, XML_SCHEMA_WHITESPACE_PRESERVE);
17529 if (ret == -2)
17530 return(-1);
17531 if (ret != 0)
17532 return(0);
17533 }
17534 /*
17535 * Lists.
17536 */
17537 x = xmlSchemaValueGetNext(x);
17538 if (x != NULL) {
17539 y = xmlSchemaValueGetNext(y);
17540 if (y == NULL)
17541 return (0);
17542 } else if (xmlSchemaValueGetNext(y) != NULL)
17543 return (0);
17544 else
17545 return (1);
17546 }
17547 return (0);
17548}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017549
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017550/**
17551 * xmlSchemaAttrFixup:
17552 * @item: an schema attribute declaration/use.
17553 * @ctxt: a schema parser context
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017554 * @name: the name of the attribute
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017555 *
17556 * Fixes finish doing the computations on attribute declarations/uses.
17557 */
17558static void
17559xmlSchemaAttrFixup(xmlSchemaAttributePtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017560 xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017561 const xmlChar * name ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +000017562{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017563 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +000017564 * TODO: If including this is done twice (!) for every attribute.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017565 * -> Hmm, check if this is still done.
Daniel Veillardc0826a72004-08-10 14:17:33 +000017566 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000017567 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017568 * The simple type definition corresponding to the <simpleType> element
17569 * information item in the [children], if present, otherwise the simple
17570 * type definition ·resolved· to by the ·actual value· of the type
Daniel Veillard01fa6152004-06-29 17:04:39 +000017571 * [attribute], if present, otherwise the ·simple ur-type definition·.
17572 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017573 if (item->flags & XML_SCHEMAS_ATTR_INTERNAL_RESOLVED)
Daniel Veillardc0826a72004-08-10 14:17:33 +000017574 return;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017575 item->flags |= XML_SCHEMAS_ATTR_INTERNAL_RESOLVED;
17576 if (item->subtypes != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017577 return;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017578 if (item->typeName != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017579 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000017580
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017581 type = xmlSchemaGetType(ctxt->schema, item->typeName,
17582 item->typeNs);
17583 if ((type == NULL) || (! IS_SIMPLE_TYPE(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000017584 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000017585 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017586 (xmlSchemaTypePtr) item, item->node,
17587 "type", item->typeName, item->typeNs,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000017588 XML_SCHEMA_TYPE_SIMPLE, NULL);
17589 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017590 item->subtypes = type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017591
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017592 } else if (item->ref != NULL) {
17593 xmlSchemaAttributePtr decl;
Daniel Veillard4255d502002-04-16 15:50:10 +000017594
Daniel Veillardc0826a72004-08-10 14:17:33 +000017595 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017596 * We have an attribute use here; assign the referenced
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017597 * attribute declaration.
17598 */
17599 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +000017600 * TODO: Evaluate, what errors could occur if the declaration is not
17601 * found. It might be possible that the "typefixup" might crash if
17602 * no ref declaration was found.
17603 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017604 decl = xmlSchemaGetAttributeDecl(ctxt->schema, item->ref, item->refNs);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017605 if (decl == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000017606 xmlSchemaPResCompAttrErr(ctxt,
17607 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017608 (xmlSchemaTypePtr) item, item->node,
17609 "ref", item->ref, item->refNs,
Daniel Veillardc0826a72004-08-10 14:17:33 +000017610 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017611 return;
17612 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017613 item->refDecl = decl;
17614 xmlSchemaAttrFixup(decl, ctxt, NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017615 item->subtypes = decl->subtypes;
17616 /*
17617 * Attribute Use Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017618 * au-props-correct.2: If the {attribute declaration} has a fixed
17619 * {value constraint}, then if the attribute use itself has a
17620 * {value constraint}, it must also be fixed and its value must match
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017621 * that of the {attribute declaration}'s {value constraint}.
17622 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017623 if ((decl->flags & XML_SCHEMAS_ATTR_FIXED) &&
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017624 (item->defValue != NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017625 if ((item->flags & XML_SCHEMAS_ATTR_FIXED) == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017626 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017627 XML_SCHEMAP_AU_PROPS_CORRECT_2,
17628 NULL, NULL, item->node,
17629 "The attribute declaration has a 'fixed' value constraint "
17630 ", thus it must be 'fixed' in attribute use as well",
17631 NULL);
17632 } else {
17633 if (! xmlSchemaAreValuesEqual(item->defVal, decl->defVal)) {
17634 xmlSchemaPCustomErr(ctxt,
17635 XML_SCHEMAP_AU_PROPS_CORRECT_2,
17636 NULL, NULL, item->node,
17637 "The 'fixed' value constraint of the attribute use "
17638 "must match the attribute declaration's value "
17639 "constraint '%s'",
17640 decl->defValue);
17641 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017642 }
17643 /*
17644 * FUTURE: One should change the values of the attr. use
17645 * if ever validation should be attempted even if the
17646 * schema itself was not fully valid.
17647 */
17648 }
Daniel Veillard3646d642004-06-02 19:19:14 +000017649 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017650 item->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
17651 }
Daniel Veillard4255d502002-04-16 15:50:10 +000017652}
17653
17654/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000017655 * xmlSchemaResolveIDCKeyRef:
17656 * @idc: the identity-constraint definition
17657 * @ctxt: the schema parser context
17658 * @name: the attribute name
17659 *
17660 * Resolve keyRef references to key/unique IDCs.
17661 */
17662static void
17663xmlSchemaResolveIDCKeyRef(xmlSchemaIDCPtr idc,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000017664 xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +000017665 const xmlChar * name ATTRIBUTE_UNUSED)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017666{
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000017667 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF)
17668 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017669 if (idc->ref->name != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000017670 idc->ref->item = (xmlSchemaBasicItemPtr) xmlHashLookup2(
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000017671 pctxt->schema->idcDef,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017672 idc->ref->name,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000017673 idc->ref->targetNamespace);
17674 if (idc->ref->item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017675 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000017676 * TODO: It is actually not an error to fail to resolve.
17677 */
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000017678 xmlSchemaPResCompAttrErr(pctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000017679 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017680 (xmlSchemaTypePtr) idc, idc->node,
17681 "refer", idc->ref->name,
17682 idc->ref->targetNamespace,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000017683 XML_SCHEMA_TYPE_IDC_KEYREF, NULL);
17684 return;
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000017685 } else {
17686 if (idc->nbFields !=
17687 ((xmlSchemaIDCPtr) idc->ref->item)->nbFields) {
17688 xmlChar *str = NULL;
17689 xmlSchemaIDCPtr refer;
17690
17691 refer = (xmlSchemaIDCPtr) idc->ref->item;
17692 /*
17693 * SPEC c-props-correct(2)
17694 * "If the {identity-constraint category} is keyref,
17695 * the cardinality of the {fields} must equal that of
17696 * the {fields} of the {referenced key}.
17697 */
17698 xmlSchemaPCustomErr(pctxt,
17699 XML_SCHEMAP_C_PROPS_CORRECT,
17700 NULL, (xmlSchemaTypePtr) idc, idc->node,
17701 "The cardinality of the keyref differs from the "
17702 "cardinality of the referenced key '%s'",
17703 xmlSchemaFormatQName(&str, refer->targetNamespace,
17704 refer->name)
17705 );
17706 FREE_AND_NULL(str)
17707 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017708 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000017709 }
17710}
17711
17712/**
Daniel Veillard4255d502002-04-16 15:50:10 +000017713 * xmlSchemaParse:
17714 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000017715 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000017716 * parse a schema definition resource and build an internal
Daniel Veillard4255d502002-04-16 15:50:10 +000017717 * XML Shema struture which can be used to validate instances.
17718 * *WARNING* this interface is highly subject to change
17719 *
17720 * Returns the internal XML Schema structure built from the resource or
17721 * NULL in case of error
17722 */
17723xmlSchemaPtr
17724xmlSchemaParse(xmlSchemaParserCtxtPtr ctxt)
17725{
17726 xmlSchemaPtr ret = NULL;
17727 xmlDocPtr doc;
Daniel Veillardbd2904b2003-11-25 15:38:59 +000017728 xmlNodePtr root;
Daniel Veillarddda22c12004-01-24 08:31:30 +000017729 int preserve = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000017730
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000017731 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017732 * This one is used if the schema to be parsed was specified via
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000017733 * the API; i.e. not automatically by the validated instance document.
17734 */
17735
Daniel Veillard4255d502002-04-16 15:50:10 +000017736 xmlSchemaInitTypes();
17737
Daniel Veillard6045c902002-10-09 21:13:59 +000017738 if (ctxt == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +000017739 return (NULL);
17740
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000017741 ctxt->nberrors = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000017742 ctxt->counter = 0;
17743 ctxt->container = NULL;
17744
17745 /*
17746 * First step is to parse the input document into an DOM/Infoset
17747 */
Daniel Veillard6045c902002-10-09 21:13:59 +000017748 if (ctxt->URL != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017749 doc = xmlReadFile((const char *) ctxt->URL, NULL,
Daniel Veillardbd2904b2003-11-25 15:38:59 +000017750 SCHEMAS_PARSE_OPTIONS);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017751 if (doc == NULL) {
17752 xmlSchemaPErr(ctxt, NULL,
17753 XML_SCHEMAP_FAILED_LOAD,
Daniel Veillardc0826a72004-08-10 14:17:33 +000017754 "xmlSchemaParse: could not load '%s'.\n",
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017755 ctxt->URL, NULL);
17756 return (NULL);
17757 }
Daniel Veillard6045c902002-10-09 21:13:59 +000017758 } else if (ctxt->buffer != NULL) {
Daniel Veillardbd2904b2003-11-25 15:38:59 +000017759 doc = xmlReadMemory(ctxt->buffer, ctxt->size, NULL, NULL,
17760 SCHEMAS_PARSE_OPTIONS);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017761 if (doc == NULL) {
17762 xmlSchemaPErr(ctxt, NULL,
17763 XML_SCHEMAP_FAILED_PARSE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000017764 "xmlSchemaParse: could not parse.\n",
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017765 NULL, NULL);
17766 return (NULL);
17767 }
17768 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
Daniel Veillard65765282004-01-08 16:59:30 +000017769 ctxt->URL = xmlDictLookup(ctxt->dict, BAD_CAST "in_memory_buffer", -1);
Daniel Veillard9d751502003-10-29 13:21:47 +000017770 } else if (ctxt->doc != NULL) {
17771 doc = ctxt->doc;
Daniel Veillarddda22c12004-01-24 08:31:30 +000017772 preserve = 1;
Daniel Veillard6045c902002-10-09 21:13:59 +000017773 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017774 xmlSchemaPErr(ctxt, NULL,
17775 XML_SCHEMAP_NOTHING_TO_PARSE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000017776 "xmlSchemaParse: could not parse.\n",
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017777 NULL, NULL);
17778 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000017779 }
17780
17781 /*
17782 * Then extract the root and Schema parse it
17783 */
17784 root = xmlDocGetRootElement(doc);
17785 if (root == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017786 xmlSchemaPErr(ctxt, (xmlNodePtr) doc,
17787 XML_SCHEMAP_NOROOT,
Daniel Veillardc0826a72004-08-10 14:17:33 +000017788 "The schema has no document element.\n", NULL, NULL);
Daniel Veillarddda22c12004-01-24 08:31:30 +000017789 if (!preserve) {
17790 xmlFreeDoc(doc);
17791 }
Daniel Veillard4255d502002-04-16 15:50:10 +000017792 return (NULL);
17793 }
17794
17795 /*
17796 * Remove all the blank text nodes
17797 */
Daniel Veillardbd2904b2003-11-25 15:38:59 +000017798 xmlSchemaCleanupDoc(ctxt, root);
Daniel Veillard4255d502002-04-16 15:50:10 +000017799
17800 /*
17801 * Then do the parsing for good
17802 */
17803 ret = xmlSchemaParseSchema(ctxt, root);
Daniel Veillard1d913862003-11-21 00:28:39 +000017804 if (ret == NULL) {
Daniel Veillarddda22c12004-01-24 08:31:30 +000017805 if (!preserve) {
17806 xmlFreeDoc(doc);
17807 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017808 return (NULL);
Daniel Veillard1d913862003-11-21 00:28:39 +000017809 }
Daniel Veillard4255d502002-04-16 15:50:10 +000017810 ret->doc = doc;
Daniel Veillarddda22c12004-01-24 08:31:30 +000017811 ret->preserve = preserve;
Daniel Veillard4255d502002-04-16 15:50:10 +000017812 ctxt->schema = ret;
Daniel Veillardc0826a72004-08-10 14:17:33 +000017813 ctxt->ctxtType = NULL;
17814 ctxt->parentItem = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017815
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017816 /*
17817 * Resolve base types of simple/complex types.
17818 */
17819 xmlHashScan(ret->typeDecl, (xmlHashScanner) xmlSchemaResolveTypeDefs, ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017820
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017821 if (ctxt->nberrors != 0)
17822 goto exit;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017823
17824 if (ret->volatiles != NULL) {
17825 xmlSchemaItemListPtr list = (xmlSchemaItemListPtr) ret->volatiles;
17826 int i;
17827 xmlSchemaTreeItemPtr item;
17828
17829 for (i = 0; i < list->nbItems; i++) {
17830 item = (xmlSchemaTreeItemPtr) list->items[i];
17831 if (item->type == XML_SCHEMA_TYPE_PARTICLE)
17832 xmlSchemaMiscRefFixup(item, ctxt, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017833 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017834 }
Daniel Veillard4255d502002-04-16 15:50:10 +000017835 /*
Daniel Veillardf2a12832003-11-24 13:04:35 +000017836 * Then fixup all attributes declarations
17837 */
17838 xmlHashScan(ret->attrDecl, (xmlHashScanner) xmlSchemaAttrFixup, ctxt);
Daniel Veillardf2a12832003-11-24 13:04:35 +000017839 /*
17840 * Then fixup all attributes group declarations
17841 */
17842 xmlHashScan(ret->attrgrpDecl, (xmlHashScanner) xmlSchemaAttrGrpFixup,
17843 ctxt);
Daniel Veillardf2a12832003-11-24 13:04:35 +000017844 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000017845 * Resolve identity-constraint keyRefs.
17846 */
17847 xmlHashScan(ret->idcDef, (xmlHashScanner) xmlSchemaResolveIDCKeyRef, ctxt);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017848 /*
17849 * Check type defnitions for circular references.
17850 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017851 xmlHashScan(ret->typeDecl, (xmlHashScanner)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017852 xmlSchemaCheckTypeDefCircular, ctxt);
17853 /*
17854 * Check model groups defnitions for circular references.
17855 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017856 xmlHashScan(ret->groupDecl, (xmlHashScanner)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017857 xmlSchemaCheckGroupDefCircular, ctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017858 /*
17859 * Set the "term" of particles pointing to model group definitions
17860 * to the contained model group.
17861 */
17862 if (ret->volatiles != NULL) {
17863 xmlSchemaItemListPtr list = (xmlSchemaItemListPtr) ret->volatiles;
17864 int i;
17865 xmlSchemaParticlePtr item;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000017866
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017867 for (i = 0; i < list->nbItems; i++) {
17868 item = (xmlSchemaParticlePtr) list->items[i];
17869 if (item->type == XML_SCHEMA_TYPE_PARTICLE)
17870 xmlSchemaGroupDefTermFixup(item, ctxt, NULL);
17871 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017872 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000017873 /*
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017874 * Check attribute groups for circular references.
17875 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017876 xmlHashScan(ret->attrgrpDecl, (xmlHashScanner)
17877 xmlSchemaCheckAttributeGroupCircular, ctxt);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017878 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +000017879 * Then fix references of element declaration; apply constraints.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017880 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000017881 xmlHashScanFull(ret->elemDecl,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017882 (xmlHashScannerFull) xmlSchemaElementFixup, ctxt);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000017883 /*
17884 * We will stop here if the schema was not valid to avoid internal errors
17885 * on missing sub-components. This is not conforming to the spec, since it
17886 * allows missing components, but it might make further processing crash.
17887 * So see it as a very strict handling, which might be made more lax in the
17888 * future.
17889 */
17890 if (ctxt->nberrors != 0)
17891 goto exit;
17892 /*
17893 * Then fixup all types properties
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017894 */
17895 xmlHashScan(ret->typeDecl, (xmlHashScanner) xmlSchemaTypeFixup, ctxt);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017896 /*
17897 * Validate the value constraint of attribute declarations/uses.
17898 */
17899 xmlHashScan(ret->attrDecl, (xmlHashScanner) xmlSchemaCheckAttrValConstr, ctxt);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017900 /*
17901 * Validate the value constraint of element declarations.
17902 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017903 xmlHashScan(ret->elemDecl, (xmlHashScanner) xmlSchemaCheckElementDeclComponent, ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017904
17905 if (ctxt->nberrors != 0)
17906 goto exit;
17907
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000017908 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017909 * TODO: cos-element-consistent, cos-all-limited
17910 *
17911 * Then build the content model for all complex types
17912 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000017913 xmlHashScan(ret->typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017914 (xmlHashScanner) xmlSchemaBuildContentModel, ctxt);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017915
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000017916exit:
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000017917 if (ctxt->nberrors != 0) {
17918 xmlSchemaFree(ret);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017919 ret = NULL;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000017920 }
Daniel Veillard4255d502002-04-16 15:50:10 +000017921 return (ret);
17922}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017923
Daniel Veillard4255d502002-04-16 15:50:10 +000017924/**
Daniel Veillard01c13b52002-12-10 15:19:08 +000017925 * xmlSchemaSetParserErrors:
Daniel Veillard4255d502002-04-16 15:50:10 +000017926 * @ctxt: a schema validation context
Daniel Veillard01c13b52002-12-10 15:19:08 +000017927 * @err: the error callback
17928 * @warn: the warning callback
17929 * @ctx: contextual data for the callbacks
Daniel Veillard4255d502002-04-16 15:50:10 +000017930 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000017931 * Set the callback functions used to handle errors for a validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000017932 */
17933void
17934xmlSchemaSetParserErrors(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017935 xmlSchemaValidityErrorFunc err,
17936 xmlSchemaValidityWarningFunc warn, void *ctx)
17937{
Daniel Veillard4255d502002-04-16 15:50:10 +000017938 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017939 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000017940 ctxt->error = err;
17941 ctxt->warning = warn;
17942 ctxt->userData = ctx;
17943}
17944
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000017945/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000017946 * xmlSchemaGetParserErrors:
17947 * @ctxt: a XMl-Schema parser context
17948 * @err: the error callback result
17949 * @warn: the warning callback result
17950 * @ctx: contextual data for the callbacks result
17951 *
17952 * Get the callback information used to handle errors for a parser context
17953 *
17954 * Returns -1 in case of failure, 0 otherwise
17955 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017956int
Daniel Veillard259f0df2004-08-18 09:13:18 +000017957xmlSchemaGetParserErrors(xmlSchemaParserCtxtPtr ctxt,
17958 xmlSchemaValidityErrorFunc * err,
17959 xmlSchemaValidityWarningFunc * warn, void **ctx)
17960{
17961 if (ctxt == NULL)
17962 return(-1);
17963 if (err != NULL)
17964 *err = ctxt->error;
17965 if (warn != NULL)
17966 *warn = ctxt->warning;
17967 if (ctx != NULL)
17968 *ctx = ctxt->userData;
17969 return(0);
17970}
17971
17972/**
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000017973 * xmlSchemaFacetTypeToString:
17974 * @type: the facet type
17975 *
17976 * Convert the xmlSchemaTypeType to a char string.
17977 *
17978 * Returns the char string representation of the facet type if the
17979 * type is a facet and an "Internal Error" string otherwise.
17980 */
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000017981static const xmlChar *
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000017982xmlSchemaFacetTypeToString(xmlSchemaTypeType type)
17983{
17984 switch (type) {
17985 case XML_SCHEMA_FACET_PATTERN:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000017986 return (BAD_CAST "pattern");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017987 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000017988 return (BAD_CAST "maxExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017989 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000017990 return (BAD_CAST "maxInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017991 case XML_SCHEMA_FACET_MINEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000017992 return (BAD_CAST "minExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017993 case XML_SCHEMA_FACET_MININCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000017994 return (BAD_CAST "minInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017995 case XML_SCHEMA_FACET_WHITESPACE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000017996 return (BAD_CAST "whiteSpace");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017997 case XML_SCHEMA_FACET_ENUMERATION:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000017998 return (BAD_CAST "enumeration");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017999 case XML_SCHEMA_FACET_LENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018000 return (BAD_CAST "length");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018001 case XML_SCHEMA_FACET_MAXLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018002 return (BAD_CAST "maxLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018003 case XML_SCHEMA_FACET_MINLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018004 return (BAD_CAST "minLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018005 case XML_SCHEMA_FACET_TOTALDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018006 return (BAD_CAST "totalDigits");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018007 case XML_SCHEMA_FACET_FRACTIONDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018008 return (BAD_CAST "fractionDigits");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000018009 default:
18010 break;
18011 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018012 return (BAD_CAST "Internal Error");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000018013}
18014
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018015static xmlSchemaWhitespaceValueType
Daniel Veillardc0826a72004-08-10 14:17:33 +000018016xmlSchemaGetWhiteSpaceFacetValue(xmlSchemaTypePtr type)
18017{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018018 /*
18019 * The normalization type can be changed only for types which are derived
Daniel Veillardc0826a72004-08-10 14:17:33 +000018020 * from xsd:string.
18021 */
18022 if (type->type == XML_SCHEMA_TYPE_BASIC) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018023 /*
18024 * Note that we assume a whitespace of preserve for anySimpleType.
18025 */
18026 if ((type->builtInType == XML_SCHEMAS_STRING) ||
18027 (type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE))
18028 return(XML_SCHEMA_WHITESPACE_PRESERVE);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +000018029 else if (type->builtInType == XML_SCHEMAS_NORMSTRING)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018030 return(XML_SCHEMA_WHITESPACE_REPLACE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000018031 else {
18032 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018033 * For all ·atomic· datatypes other than string (and types ·derived·
18034 * by ·restriction· from it) the value of whiteSpace is fixed to
Daniel Veillardc0826a72004-08-10 14:17:33 +000018035 * collapse
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018036 * Note that this includes built-in list datatypes.
Daniel Veillardc0826a72004-08-10 14:17:33 +000018037 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018038 return(XML_SCHEMA_WHITESPACE_COLLAPSE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000018039 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018040 } else if (VARIETY_LIST(type)) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018041 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018042 * For list types the facet "whiteSpace" is fixed to "collapse".
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018043 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018044 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
18045 } else if (VARIETY_UNION(type)) {
18046 return (XML_SCHEMA_WHITESPACE_UNKNOWN);
18047 } else if (VARIETY_ATOMIC(type)) {
18048 if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE)
18049 return (XML_SCHEMA_WHITESPACE_PRESERVE);
18050 else if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_REPLACE)
18051 return (XML_SCHEMA_WHITESPACE_REPLACE);
18052 else
18053 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018054 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018055 return (-1);
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000018056}
18057
Daniel Veillard4255d502002-04-16 15:50:10 +000018058/************************************************************************
18059 * *
18060 * Simple type validation *
18061 * *
18062 ************************************************************************/
Daniel Veillard377e1a92004-04-16 16:30:05 +000018063
Daniel Veillard4255d502002-04-16 15:50:10 +000018064
18065/************************************************************************
18066 * *
18067 * DOM Validation code *
18068 * *
18069 ************************************************************************/
18070
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018071static void
18072xmlSchemaPostSchemaAssembleFixup(xmlSchemaParserCtxtPtr ctxt)
18073{
18074 int i, nbItems;
18075 xmlSchemaTypePtr item, *items;
18076
18077
18078 /*
18079 * During the Assemble of the schema ctxt->curItems has
18080 * been filled with the relevant new items. Fix those up.
18081 */
18082 nbItems = ctxt->assemble->nbItems;
18083 items = (xmlSchemaTypePtr *) ctxt->assemble->items;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018084
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018085 for (i = 0; i < nbItems; i++) {
18086 item = items[i];
18087 switch (item->type) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018088 case XML_SCHEMA_TYPE_COMPLEX:
18089 case XML_SCHEMA_TYPE_SIMPLE:
18090 xmlSchemaResolveTypeDefs(item, ctxt, NULL);
18091 break;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018092 case XML_SCHEMA_TYPE_ATTRIBUTE:
18093 xmlSchemaAttrFixup((xmlSchemaAttributePtr) item, ctxt, NULL);
18094 break;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018095 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018096 xmlSchemaAttrGrpFixup((xmlSchemaAttributeGroupPtr) item,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018097 ctxt, NULL);
18098 break;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018099 case XML_SCHEMA_TYPE_PARTICLE:
18100 xmlSchemaMiscRefFixup((xmlSchemaTreeItemPtr) item, ctxt, NULL);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000018101 break;
18102 case XML_SCHEMA_TYPE_IDC_KEY:
18103 case XML_SCHEMA_TYPE_IDC_UNIQUE:
18104 case XML_SCHEMA_TYPE_IDC_KEYREF:
18105 xmlSchemaResolveIDCKeyRef((xmlSchemaIDCPtr) item, ctxt, NULL);
18106 break;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018107 default:
18108 break;
18109 }
18110 }
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000018111 if (ctxt->nberrors != 0)
18112 return;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018113 /*
18114 * Circularity checks.
18115 */
18116 for (i = 0; i < nbItems; i++) {
18117 item = items[i];
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018118 switch (item->type) {
18119 case XML_SCHEMA_TYPE_COMPLEX:
18120 case XML_SCHEMA_TYPE_SIMPLE:
18121 xmlSchemaCheckTypeDefCircular(
18122 (xmlSchemaTypePtr) item, ctxt, NULL);
18123 break;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018124 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018125 xmlSchemaCheckGroupDefCircular(
18126 (xmlSchemaModelGroupDefPtr) item, ctxt, NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018127 break;
18128 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
18129 xmlSchemaCheckAttributeGroupCircular(
18130 (xmlSchemaAttributeGroupPtr) item, ctxt, NULL);
18131 break;
18132 default:
18133 break;
18134 }
18135 }
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000018136 if (ctxt->nberrors != 0)
18137 return;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018138 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018139 * Set the "term" of particles pointing to model group definitions
18140 * to the contained model group.
18141 */
18142 for (i = 0; i < nbItems; i++) {
18143 item = items[i];
18144 if ((item->type == XML_SCHEMA_TYPE_PARTICLE) &&
18145 (((xmlSchemaParticlePtr) item)->children != NULL) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018146 (((xmlSchemaParticlePtr) item)->children->type ==
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018147 XML_SCHEMA_TYPE_GROUP)) {
18148 xmlSchemaGroupDefTermFixup((xmlSchemaParticlePtr) item,
18149 ctxt, NULL);
18150 }
18151 }
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000018152 if (ctxt->nberrors != 0)
18153 return;
18154 for (i = 0; i < nbItems; i++) {
18155 item = items[i];
18156 switch (item->type) {
18157 case XML_SCHEMA_TYPE_ELEMENT:
18158 xmlSchemaElementFixup((xmlSchemaElementPtr) item, ctxt,
18159 NULL, NULL, NULL);
18160 break;
18161 default:
18162 break;
18163 }
18164 }
18165 if (ctxt->nberrors != 0)
18166 return;
18167
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018168 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018169 * Fixup for simple/complex types.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018170 */
18171 for (i = 0; i < nbItems; i++) {
18172 item = items[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018173 switch (item->type) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018174 case XML_SCHEMA_TYPE_SIMPLE:
18175 case XML_SCHEMA_TYPE_COMPLEX:
18176 xmlSchemaTypeFixup(item, ctxt, NULL);
18177 break;
18178 default:
18179 break;
18180 }
18181 }
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000018182 if (ctxt->nberrors != 0)
18183 return;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018184 /*
18185 * Validate value contraint values.
18186 */
18187 for (i = 0; i < nbItems; i++) {
18188 item = items[i];
18189 switch (item->type) {
18190 case XML_SCHEMA_TYPE_ATTRIBUTE:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018191 xmlSchemaCheckAttrValConstr((xmlSchemaAttributePtr) item,
18192 ctxt, NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018193 break;
18194 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018195 xmlSchemaCheckElementDeclComponent((xmlSchemaElementPtr) item,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018196 ctxt, NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018197 break;
18198 default:
18199 break;
18200 }
18201 }
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000018202 if (ctxt->nberrors != 0)
18203 return;
18204 /*
18205 * Build the content model for complex types.
18206 */
18207 for (i = 0; i < nbItems; i++) {
18208 item = items[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018209 switch (item->type) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000018210 case XML_SCHEMA_TYPE_COMPLEX:
18211 xmlSchemaBuildContentModel(item, ctxt, NULL);
18212 break;
18213 default:
18214 break;
18215 }
18216 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018217}
18218
18219/**
18220 * xmlSchemaAssembleByLocation:
18221 * @pctxt: a schema parser context
18222 * @vctxt: a schema validation context
18223 * @schema: the existing schema
18224 * @node: the node that fired the assembling
18225 * @nsName: the namespace name of the new schema
18226 * @location: the location of the schema
18227 *
18228 * Expands an existing schema by an additional schema.
18229 *
18230 * Returns 0 if the new schema is correct, a positive error code
18231 * number otherwise and -1 in case of an internal or API error.
18232 */
18233static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018234xmlSchemaAssembleByLocation(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018235 xmlSchemaPtr schema,
18236 xmlNodePtr node,
18237 const xmlChar *nsName,
18238 const xmlChar *location)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018239{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018240 const xmlChar *targetNs, *oldtns;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018241 xmlDocPtr doc, olddoc;
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000018242 int oldflags, ret = 0, oldIsS4S;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018243 xmlNodePtr docElem;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018244 xmlSchemaParserCtxtPtr pctxt;
18245
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018246 /*
18247 * This should be used:
18248 * 1. on <import>(s)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018249 * 2. if requested by the validated instance
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018250 * 3. if requested via the API
18251 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018252 if ((vctxt == NULL) || (schema == NULL))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018253 return (-1);
18254 /*
18255 * Create a temporary parser context.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018256 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018257 if ((vctxt->pctxt == NULL) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018258 (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
18259 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018260 pctxt = vctxt->pctxt;
18261 /*
18262 * Set the counter to produce unique names for anonymous items.
18263 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018264 pctxt->counter = schema->counter;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018265 /*
18266 * Acquire the schema document.
18267 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018268 ret = xmlSchemaAcquireSchemaDoc((xmlSchemaAbstractCtxtPtr) vctxt, schema,
18269 node, nsName, location, &doc, &targetNs, 0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018270 if (ret != 0) {
18271 if (doc != NULL)
18272 xmlFreeDoc(doc);
18273 } else if (doc != NULL) {
18274 docElem = xmlDocGetRootElement(doc);
18275 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018276 * Create new assemble info.
18277 */
18278 if (pctxt->assemble == NULL) {
18279 pctxt->assemble = xmlSchemaNewAssemble();
18280 if (pctxt->assemble == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018281 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018282 "Memory error: xmlSchemaAssembleByLocation, "
18283 "allocating assemble info", NULL);
18284 xmlFreeDoc(doc);
18285 return (-1);
18286 }
18287 }
18288 /*
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018289 * Save and reset the context & schema.
18290 */
18291 oldflags = schema->flags;
18292 oldtns = schema->targetNamespace;
18293 olddoc = schema->doc;
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000018294 oldIsS4S = vctxt->pctxt->isS4S;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018295
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018296 xmlSchemaClearSchemaDefaults(schema);
18297 schema->targetNamespace = targetNs;
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000018298 if ((targetNs != NULL) &&
18299 xmlStrEqual(targetNs, xmlSchemaNs)) {
18300 /*
18301 * We are parsing the schema for schema!
18302 */
18303 vctxt->pctxt->isS4S = 1;
18304 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018305 /* schema->nbCurItems = 0; */
18306 pctxt->schema = schema;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018307 pctxt->ctxtType = NULL;
18308 pctxt->parentItem = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018309
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000018310 xmlSchemaParseSchemaDefaults(pctxt, schema, docElem);
18311 if (pctxt->nberrors != 0) {
18312 vctxt->nberrors += pctxt->nberrors;
18313 goto finally;
18314 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018315 xmlSchemaParseSchemaTopLevel(pctxt, schema, docElem->children);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000018316 if (pctxt->nberrors != 0) {
18317 vctxt->nberrors += pctxt->nberrors;
18318 goto finally;
18319 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018320 xmlSchemaPostSchemaAssembleFixup(pctxt);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000018321 if (pctxt->nberrors != 0)
18322 vctxt->nberrors += pctxt->nberrors;
18323finally:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018324 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018325 * Set the counter of items.
18326 */
18327 schema->counter = pctxt->counter;
18328 /*
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018329 * Free the list of assembled components.
18330 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018331 pctxt->assemble->nbItems = 0;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018332 /*
18333 * Restore the context & schema.
18334 */
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000018335 vctxt->pctxt->isS4S = oldIsS4S;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018336 schema->flags = oldflags;
18337 schema->targetNamespace = oldtns;
18338 schema->doc = olddoc;
18339 ret = pctxt->err;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018340 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018341 return (ret);
18342}
18343
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018344static xmlSchemaAttrInfoPtr
18345xmlSchemaGetMetaAttrInfo(xmlSchemaValidCtxtPtr vctxt,
18346 int metaType)
18347{
18348 if (vctxt->nbAttrInfos == 0)
18349 return (NULL);
18350 {
18351 int i;
18352 xmlSchemaAttrInfoPtr iattr;
18353
18354 for (i = 0; i < vctxt->nbAttrInfos; i++) {
18355 iattr = vctxt->attrInfos[i];
18356 if (iattr->metaType == metaType)
18357 return (iattr);
18358 }
18359
18360 }
18361 return (NULL);
18362}
18363
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018364/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018365 * xmlSchemaAssembleByXSI:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018366 * @vctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018367 *
18368 * Expands an existing schema by an additional schema using
18369 * the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute
18370 * of an instance. If xsi:noNamespaceSchemaLocation is used, @noNamespace
18371 * must be set to 1.
18372 *
18373 * Returns 0 if the new schema is correct, a positive error code
18374 * number otherwise and -1 in case of an internal or API error.
18375 */
18376static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018377xmlSchemaAssembleByXSI(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018378{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018379 const xmlChar *cur, *end;
18380 const xmlChar *nsname = NULL, *location;
18381 int count = 0;
18382 int ret = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018383 xmlSchemaAttrInfoPtr iattr;
18384
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018385 /*
18386 * Parse the value; we will assume an even number of values
18387 * to be given (this is how Xerces and XSV work).
18388 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018389 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
18390 XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC);
18391 if (iattr == NULL)
18392 xmlSchemaGetMetaAttrInfo(vctxt,
18393 XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC);
18394 if (iattr == NULL)
18395 return (0);
18396 cur = iattr->value;
18397 do {
18398 if (iattr->metaType == XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018399 /*
18400 * Get the namespace name.
18401 */
18402 while (IS_BLANK_CH(*cur))
18403 cur++;
18404 end = cur;
18405 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
18406 end++;
18407 if (end == cur)
18408 break;
18409 count++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018410 nsname = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018411 cur = end;
18412 }
18413 /*
18414 * Get the URI.
18415 */
18416 while (IS_BLANK_CH(*cur))
18417 cur++;
18418 end = cur;
18419 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
18420 end++;
18421 if (end == cur)
18422 break;
18423 count++;
18424 location = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018425 cur = end;
18426 ret = xmlSchemaAssembleByLocation(vctxt, vctxt->schema,
18427 iattr->node, nsname, location);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018428 if (ret == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018429 VERROR_INT("xmlSchemaAssembleByXSI",
18430 "assembling schemata");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018431 return (-1);
18432 }
18433 } while (*cur != 0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018434 return (ret);
18435}
18436
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018437#define VAL_CREATE_DICT if (vctxt->dict == NULL) vctxt->dict = xmlDictCreate();
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018438
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018439static const xmlChar *
18440xmlSchemaLookupNamespace(xmlSchemaValidCtxtPtr vctxt,
18441 const xmlChar *prefix)
18442{
18443 if (vctxt->sax != NULL) {
18444 int i, j;
18445 xmlSchemaNodeInfoPtr inode;
18446
18447 for (i = vctxt->depth; i >= 0; i--) {
18448 if (vctxt->elemInfos[i]->nbNsBindings != 0) {
18449 inode = vctxt->elemInfos[i];
18450 for (j = 0; j < inode->nbNsBindings * 2; j += 2) {
18451 if (((prefix == NULL) &&
18452 (inode->nsBindings[j] == NULL)) ||
18453 ((prefix != NULL) && xmlStrEqual(prefix,
18454 inode->nsBindings[j]))) {
18455
18456 /*
18457 * Note that the namespace bindings are already
18458 * in a string dict.
18459 */
18460 return (inode->nsBindings[j+1]);
18461 }
18462 }
18463 }
18464 }
18465 return (NULL);
Daniel Veillard39e5c892005-07-03 22:48:50 +000018466#ifdef LIBXML_WRITER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018467 } else if (vctxt->reader != NULL) {
18468 xmlChar *nsName;
18469
18470 nsName = xmlTextReaderLookupNamespace(vctxt->reader, prefix);
18471 if (nsName != NULL) {
18472 const xmlChar *ret;
18473
18474 VAL_CREATE_DICT;
18475 ret = xmlDictLookup(vctxt->dict, nsName, -1);
18476 xmlFree(nsName);
18477 return (ret);
18478 } else
18479 return (NULL);
Daniel Veillard39e5c892005-07-03 22:48:50 +000018480#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018481 } else {
18482 xmlNsPtr ns;
18483
18484 if ((vctxt->inode->node == NULL) ||
18485 (vctxt->inode->node->doc == NULL)) {
18486 VERROR_INT("xmlSchemaLookupNamespace",
18487 "no node or node's doc avaliable");
18488 return (NULL);
18489 }
18490 ns = xmlSearchNs(vctxt->inode->node->doc,
18491 vctxt->inode->node, prefix);
18492 if (ns != NULL)
18493 return (ns->href);
18494 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018495 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018496}
18497
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018498/*
18499* This one works on the schema of the validation context.
18500*/
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018501static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018502xmlSchemaValidateNotation(xmlSchemaValidCtxtPtr vctxt,
18503 xmlSchemaPtr schema,
18504 xmlNodePtr node,
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018505 const xmlChar *value,
18506 xmlSchemaValPtr *val,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018507 int valNeeded)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018508{
18509 int ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018510
18511 if (vctxt && (vctxt->schema == NULL)) {
18512 VERROR_INT("xmlSchemaValidateNotation",
18513 "a schema is needed on the validation context");
18514 return (-1);
18515 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018516 ret = xmlValidateQName(value, 1);
18517 if (ret != 0)
18518 return (ret);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018519 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018520 xmlChar *localName = NULL;
18521 xmlChar *prefix = NULL;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018522
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018523 localName = xmlSplitQName2(value, &prefix);
18524 if (prefix != NULL) {
18525 const xmlChar *nsName = NULL;
18526
18527 if (vctxt != NULL)
18528 nsName = xmlSchemaLookupNamespace(vctxt, BAD_CAST prefix);
18529 else if (node != NULL) {
18530 xmlNsPtr ns = xmlSearchNs(node->doc, node, prefix);
18531 if (ns != NULL)
18532 nsName = ns->href;
18533 } else {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018534 xmlFree(prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018535 xmlFree(localName);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018536 return (1);
18537 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018538 if (nsName == NULL) {
18539 xmlFree(prefix);
18540 xmlFree(localName);
18541 return (1);
18542 }
18543 if (xmlHashLookup2(schema->notaDecl, localName,
18544 nsName) != NULL) {
18545 if (valNeeded && (val != NULL)) {
18546 (*val) = xmlSchemaNewNOTATIONValue(BAD_CAST localName,
18547 BAD_CAST xmlStrdup(nsName));
18548 if (*val == NULL)
18549 ret = -1;
18550 }
18551 } else
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018552 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018553 xmlFree(prefix);
18554 xmlFree(localName);
18555 } else {
18556 if (xmlHashLookup2(schema->notaDecl, value, NULL) != NULL) {
18557 if (valNeeded && (val != NULL)) {
18558 (*val) = xmlSchemaNewNOTATIONValue(
18559 BAD_CAST xmlStrdup(value), NULL);
18560 if (*val == NULL)
18561 ret = -1;
18562 }
18563 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018564 return (1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018565 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018566 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018567 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018568}
18569
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018570/************************************************************************
18571 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018572 * Validation of identity-constraints (IDC) *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018573 * *
18574 ************************************************************************/
18575
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018576/**
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000018577 * xmlSchemaAugmentIDC:
18578 * @idcDef: the IDC definition
18579 *
18580 * Creates an augmented IDC definition item.
18581 *
18582 * Returns the item, or NULL on internal errors.
18583 */
18584static void
18585xmlSchemaAugmentIDC(xmlSchemaIDCPtr idcDef,
18586 xmlSchemaValidCtxtPtr vctxt)
18587{
18588 xmlSchemaIDCAugPtr aidc;
18589
18590 aidc = (xmlSchemaIDCAugPtr) xmlMalloc(sizeof(xmlSchemaIDCAug));
18591 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018592 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000018593 "xmlSchemaAugmentIDC: allocating an augmented IDC definition",
18594 NULL);
18595 return;
18596 }
18597 aidc->bubbleDepth = -1;
18598 aidc->def = idcDef;
18599 aidc->next = NULL;
18600 if (vctxt->aidcs == NULL)
18601 vctxt->aidcs = aidc;
18602 else {
18603 aidc->next = vctxt->aidcs;
18604 vctxt->aidcs = aidc;
18605 }
18606}
18607
18608/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018609 * xmlSchemaIDCNewBinding:
18610 * @idcDef: the IDC definition of this binding
18611 *
18612 * Creates a new IDC binding.
18613 *
18614 * Returns the new binding in case of succeeded, NULL on internal errors.
18615 */
18616static xmlSchemaPSVIIDCBindingPtr
18617xmlSchemaIDCNewBinding(xmlSchemaIDCPtr idcDef)
18618{
18619 xmlSchemaPSVIIDCBindingPtr ret;
18620
18621 ret = (xmlSchemaPSVIIDCBindingPtr) xmlMalloc(
18622 sizeof(xmlSchemaPSVIIDCBinding));
18623 if (ret == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018624 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018625 "allocating a PSVI IDC binding item", NULL);
18626 return (NULL);
18627 }
18628 memset(ret, 0, sizeof(xmlSchemaPSVIIDCBinding));
18629 ret->definition = idcDef;
18630 return (ret);
18631}
18632
18633/**
18634 * xmlSchemaIDCStoreNodeTableItem:
18635 * @vctxt: the WXS validation context
18636 * @item: the IDC node table item
18637 *
18638 * The validation context is used to store an IDC node table items.
18639 * They are stored to avoid copying them if IDC node-tables are merged
18640 * with corresponding parent IDC node-tables (bubbling).
18641 *
18642 * Returns 0 if succeeded, -1 on internal errors.
18643 */
18644static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018645xmlSchemaIDCStoreNodeTableItem(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018646 xmlSchemaPSVIIDCNodePtr item)
18647{
18648 /*
18649 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018650 */
18651 if (vctxt->idcNodes == NULL) {
18652 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018653 xmlMalloc(20 * sizeof(xmlSchemaPSVIIDCNodePtr));
18654 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018655 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018656 "allocating the IDC node table item list", NULL);
18657 return (-1);
18658 }
18659 vctxt->sizeIdcNodes = 20;
18660 } else if (vctxt->sizeIdcNodes <= vctxt->nbIdcNodes) {
18661 vctxt->sizeIdcNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018662 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
18663 xmlRealloc(vctxt->idcNodes, vctxt->sizeIdcNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018664 sizeof(xmlSchemaPSVIIDCNodePtr));
18665 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018666 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018667 "re-allocating the IDC node table item list", NULL);
18668 return (-1);
18669 }
18670 }
18671 vctxt->idcNodes[vctxt->nbIdcNodes++] = item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018672
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018673 return (0);
18674}
18675
18676/**
18677 * xmlSchemaIDCStoreKey:
18678 * @vctxt: the WXS validation context
18679 * @item: the IDC key
18680 *
18681 * The validation context is used to store an IDC key.
18682 *
18683 * Returns 0 if succeeded, -1 on internal errors.
18684 */
18685static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018686xmlSchemaIDCStoreKey(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018687 xmlSchemaPSVIIDCKeyPtr key)
18688{
18689 /*
18690 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018691 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018692 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018693 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018694 xmlMalloc(40 * sizeof(xmlSchemaPSVIIDCKeyPtr));
18695 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018696 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018697 "allocating the IDC key storage list", NULL);
18698 return (-1);
18699 }
18700 vctxt->sizeIdcKeys = 40;
18701 } else if (vctxt->sizeIdcKeys <= vctxt->nbIdcKeys) {
18702 vctxt->sizeIdcKeys *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018703 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
18704 xmlRealloc(vctxt->idcKeys, vctxt->sizeIdcKeys *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018705 sizeof(xmlSchemaPSVIIDCKeyPtr));
18706 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018707 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018708 "re-allocating the IDC key storage list", NULL);
18709 return (-1);
18710 }
18711 }
18712 vctxt->idcKeys[vctxt->nbIdcKeys++] = key;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018713
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018714 return (0);
18715}
18716
18717/**
18718 * xmlSchemaIDCAppendNodeTableItem:
18719 * @bind: the IDC binding
18720 * @ntItem: the node-table item
18721 *
18722 * Appends the IDC node-table item to the binding.
18723 *
18724 * Returns 0 on success and -1 on internal errors.
18725 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018726static int
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018727xmlSchemaIDCAppendNodeTableItem(xmlSchemaPSVIIDCBindingPtr bind,
18728 xmlSchemaPSVIIDCNodePtr ntItem)
18729{
18730 if (bind->nodeTable == NULL) {
18731 bind->sizeNodes = 10;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018732 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018733 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
18734 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018735 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018736 "allocating an array of IDC node-table items", NULL);
18737 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018738 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018739 } else if (bind->sizeNodes <= bind->nbNodes) {
18740 bind->sizeNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018741 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
18742 xmlRealloc(bind->nodeTable, bind->sizeNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018743 sizeof(xmlSchemaPSVIIDCNodePtr));
18744 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018745 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018746 "re-allocating an array of IDC node-table items", NULL);
18747 return(-1);
18748 }
18749 }
18750 bind->nodeTable[bind->nbNodes++] = ntItem;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018751 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018752}
18753
18754/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018755 * xmlSchemaIDCAquireBinding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018756 * @vctxt: the WXS validation context
18757 * @matcher: the IDC matcher
18758 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018759 * Looks up an PSVI IDC binding, for the IDC definition and
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018760 * of the given matcher. If none found, a new one is created
18761 * and added to the IDC table.
18762 *
18763 * Returns an IDC binding or NULL on internal errors.
18764 */
18765static xmlSchemaPSVIIDCBindingPtr
18766xmlSchemaIDCAquireBinding(xmlSchemaValidCtxtPtr vctxt,
18767 xmlSchemaIDCMatcherPtr matcher)
18768{
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000018769 xmlSchemaNodeInfoPtr info;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018770
18771 info = vctxt->elemInfos[matcher->depth];
18772
18773 if (info->idcTable == NULL) {
18774 info->idcTable = xmlSchemaIDCNewBinding(matcher->aidc->def);
18775 if (info->idcTable == NULL)
18776 return (NULL);
18777 return(info->idcTable);
18778 } else {
18779 xmlSchemaPSVIIDCBindingPtr bind = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018780
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018781 bind = info->idcTable;
18782 do {
18783 if (bind->definition == matcher->aidc->def)
18784 return(bind);
18785 if (bind->next == NULL) {
18786 bind->next = xmlSchemaIDCNewBinding(matcher->aidc->def);
18787 if (bind->next == NULL)
18788 return (NULL);
18789 return(bind->next);
18790 }
18791 bind = bind->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018792 } while (bind != NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018793 }
18794 return (NULL);
18795}
18796
18797/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018798 * xmlSchemaIDCFreeKey:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018799 * @key: the IDC key
18800 *
18801 * Frees an IDC key together with its compiled value.
18802 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018803static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018804xmlSchemaIDCFreeKey(xmlSchemaPSVIIDCKeyPtr key)
18805{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018806 if (key->val != NULL)
18807 xmlSchemaFreeValue(key->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018808 xmlFree(key);
18809}
18810
18811/**
18812 * xmlSchemaIDCFreeBinding:
18813 *
18814 * Frees an IDC binding. Note that the node table-items
18815 * are not freed.
18816 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000018817static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018818xmlSchemaIDCFreeBinding(xmlSchemaPSVIIDCBindingPtr bind)
18819{
18820 if (bind->nodeTable != NULL) {
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000018821 if (bind->definition->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
18822 int i;
18823 /*
18824 * Node-table items for keyrefs are not stored globally
18825 * to the validation context, since they are not bubbled.
18826 * We need to free them here.
18827 */
18828 for (i = 0; i < bind->nbNodes; i++) {
18829 xmlFree(bind->nodeTable[i]->keys);
18830 xmlFree(bind->nodeTable[i]);
18831 }
18832 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018833 xmlFree(bind->nodeTable);
18834 }
18835 xmlFree(bind);
18836}
18837
18838/**
18839 * xmlSchemaIDCFreeIDCTable:
18840 * @bind: the first IDC binding in the list
18841 *
18842 * Frees an IDC table, i.e. all the IDC bindings in the list.
18843 */
18844static void
18845xmlSchemaIDCFreeIDCTable(xmlSchemaPSVIIDCBindingPtr bind)
18846{
18847 xmlSchemaPSVIIDCBindingPtr prev;
18848
18849 while (bind != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018850 prev = bind;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018851 bind = bind->next;
18852 xmlSchemaIDCFreeBinding(prev);
18853 }
18854}
18855
18856/**
18857 * xmlSchemaIDCFreeMatcherList:
18858 * @matcher: the first IDC matcher in the list
18859 *
18860 * Frees a list of IDC matchers.
18861 */
18862static void
18863xmlSchemaIDCFreeMatcherList(xmlSchemaIDCMatcherPtr matcher)
18864{
18865 xmlSchemaIDCMatcherPtr next;
18866
18867 while (matcher != NULL) {
18868 next = matcher->next;
18869 if (matcher->keySeqs != NULL) {
18870 int i;
18871 for (i = 0; i < matcher->sizeKeySeqs; i++)
18872 if (matcher->keySeqs[i] != NULL)
18873 xmlFree(matcher->keySeqs[i]);
18874 xmlFree(matcher->keySeqs);
18875 }
18876 xmlFree(matcher);
18877 matcher = next;
18878 }
18879}
18880
18881/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018882 * xmlSchemaIDCAddStateObject:
18883 * @vctxt: the WXS validation context
18884 * @matcher: the IDC matcher
18885 * @sel: the XPath information
18886 * @parent: the parent "selector" state object if any
18887 * @type: "selector" or "field"
18888 *
18889 * Creates/reuses and activates state objects for the given
18890 * XPath information; if the XPath expression consists of unions,
18891 * multiple state objects are created for every unioned expression.
18892 *
18893 * Returns 0 on success and -1 on internal errors.
18894 */
18895static int
18896xmlSchemaIDCAddStateObject(xmlSchemaValidCtxtPtr vctxt,
18897 xmlSchemaIDCMatcherPtr matcher,
18898 xmlSchemaIDCSelectPtr sel,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018899 int type)
18900{
18901 xmlSchemaIDCStateObjPtr sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018902
18903 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000018904 * Reuse the state objects from the pool.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018905 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000018906 if (vctxt->xpathStatePool != NULL) {
18907 sto = vctxt->xpathStatePool;
18908 vctxt->xpathStatePool = sto->next;
18909 sto->next = NULL;
18910 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018911 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000018912 * Create a new state object.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018913 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000018914 sto = (xmlSchemaIDCStateObjPtr) xmlMalloc(sizeof(xmlSchemaIDCStateObj));
18915 if (sto == NULL) {
18916 xmlSchemaVErrMemory(NULL,
18917 "allocating an IDC state object", NULL);
18918 return (-1);
18919 }
18920 memset(sto, 0, sizeof(xmlSchemaIDCStateObj));
18921 }
18922 /*
18923 * Add to global list.
18924 */
18925 if (vctxt->xpathStates != NULL)
18926 sto->next = vctxt->xpathStates;
18927 vctxt->xpathStates = sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018928
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000018929 /*
18930 * Free the old xpath validation context.
18931 */
18932 if (sto->xpathCtxt != NULL)
18933 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
18934
18935 /*
18936 * Create a new XPath (pattern) validation context.
18937 */
18938 sto->xpathCtxt = (void *) xmlPatternGetStreamCtxt(
18939 (xmlPatternPtr) sel->xpathComp);
18940 if (sto->xpathCtxt == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018941 VERROR_INT("xmlSchemaIDCAddStateObject",
18942 "failed to create an XPath validation context");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000018943 return (-1);
18944 }
18945 sto->type = type;
18946 sto->depth = vctxt->depth;
18947 sto->matcher = matcher;
18948 sto->sel = sel;
18949 sto->nbHistory = 0;
18950
18951#if DEBUG_IDC
18952 xmlGenericError(xmlGenericErrorContext, "IDC: STO push '%s'\n",
18953 sto->sel->xpath);
18954#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018955 return (0);
18956}
18957
18958/**
18959 * xmlSchemaXPathEvaluate:
18960 * @vctxt: the WXS validation context
18961 * @nodeType: the nodeType of the current node
18962 *
18963 * Evaluates all active XPath state objects.
18964 *
18965 * Returns the number of IC "field" state objects which resolved to
18966 * this node, 0 if none resolved and -1 on internal errors.
18967 */
18968static int
18969xmlSchemaXPathEvaluate(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018970 xmlElementType nodeType)
18971{
18972 xmlSchemaIDCStateObjPtr sto, head = NULL, first;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000018973 int res, resolved = 0, depth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018974
18975 if (vctxt->xpathStates == NULL)
18976 return (0);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000018977
18978 if (nodeType == XML_ATTRIBUTE_NODE)
18979 depth++;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018980#if DEBUG_IDC
18981 {
18982 xmlChar *str = NULL;
18983 xmlGenericError(xmlGenericErrorContext,
18984 "IDC: EVAL on %s, depth %d, type %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018985 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
18986 vctxt->inode->localName), depth, nodeType);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018987 FREE_AND_NULL(str)
18988 }
18989#endif
18990 /*
18991 * Process all active XPath state objects.
18992 */
18993 first = vctxt->xpathStates;
18994 sto = first;
18995 while (sto != head) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018996#if DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000018997 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000018998 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] selector '%s'\n",
18999 sto->matcher->aidc->def->name, sto->sel->xpath);
19000 else
19001 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] field '%s'\n",
19002 sto->matcher->aidc->def->name, sto->sel->xpath);
19003#endif
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000019004 if (nodeType == XML_ELEMENT_NODE)
19005 res = xmlStreamPush((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019006 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000019007 else
19008 res = xmlStreamPushAttr((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019009 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000019010
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019011 if (res == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019012 VERROR_INT("xmlSchemaXPathEvaluate",
19013 "calling xmlStreamPush()");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019014 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019015 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019016 if (res == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019017 goto next_sto;
19018 /*
19019 * Full match.
19020 */
19021#if DEBUG_IDC
19022 xmlGenericError(xmlGenericErrorContext, "IDC: "
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019023 "MATCH\n");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019024#endif
19025 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019026 * Register a match in the state object history.
19027 */
19028 if (sto->history == NULL) {
19029 sto->history = (int *) xmlMalloc(5 * sizeof(int));
19030 if (sto->history == NULL) {
19031 xmlSchemaVErrMemory(NULL,
19032 "allocating the state object history", NULL);
19033 return(-1);
19034 }
19035 sto->sizeHistory = 10;
19036 } else if (sto->sizeHistory <= sto->nbHistory) {
19037 sto->sizeHistory *= 2;
19038 sto->history = (int *) xmlRealloc(sto->history,
19039 sto->sizeHistory * sizeof(int));
19040 if (sto->history == NULL) {
19041 xmlSchemaVErrMemory(NULL,
19042 "re-allocating the state object history", NULL);
19043 return(-1);
19044 }
19045 }
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000019046 sto->history[sto->nbHistory++] = depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019047
19048#ifdef DEBUG_IDC
19049 xmlGenericError(xmlGenericErrorContext, "IDC: push match '%d'\n",
19050 vctxt->depth);
19051#endif
19052
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019053 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
19054 xmlSchemaIDCSelectPtr sel;
19055 /*
19056 * Activate state objects for the IDC fields of
19057 * the IDC selector.
19058 */
19059#if DEBUG_IDC
19060 xmlGenericError(xmlGenericErrorContext, "IDC: "
19061 "activating field states\n");
19062#endif
19063 sel = sto->matcher->aidc->def->fields;
19064 while (sel != NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019065 if (xmlSchemaIDCAddStateObject(vctxt, sto->matcher,
19066 sel, XPATH_STATE_OBJ_TYPE_IDC_FIELD) == -1)
19067 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019068 sel = sel->next;
19069 }
19070 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
19071 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000019072 * An IDC key node was found by the IDC field.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019073 */
19074#if DEBUG_IDC
19075 xmlGenericError(xmlGenericErrorContext,
19076 "IDC: key found\n");
19077#endif
19078 /*
19079 * Notify that the character value of this node is
19080 * needed.
19081 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019082 if (resolved == 0) {
19083 if ((vctxt->inode->flags &
19084 XML_SCHEMA_NODE_INFO_VALUE_NEEDED) == 0)
19085 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
19086 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019087 resolved++;
19088 }
19089next_sto:
19090 if (sto->next == NULL) {
19091 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019092 * Evaluate field state objects created on this node as well.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019093 */
19094 head = first;
19095 sto = vctxt->xpathStates;
19096 } else
19097 sto = sto->next;
19098 }
19099 return (resolved);
19100}
19101
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000019102static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019103xmlSchemaFormatIDCKeySequence(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000019104 xmlChar **buf,
19105 xmlSchemaPSVIIDCKeyPtr *seq,
19106 int count)
19107{
19108 int i, res;
19109 const xmlChar *value = NULL;
19110
19111 *buf = xmlStrdup(BAD_CAST "[");
19112 for (i = 0; i < count; i++) {
19113 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019114 res = xmlSchemaGetCanonValueWhtsp(seq[i]->val, &value,
19115 xmlSchemaGetWhiteSpaceFacetValue(seq[i]->type));
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000019116 if (res == 0)
19117 *buf = xmlStrcat(*buf, value);
19118 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019119 VERROR_INT("xmlSchemaFormatIDCKeySequence",
19120 "failed to compute a canonical value");
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000019121 *buf = xmlStrcat(*buf, BAD_CAST "???");
19122 }
19123 if (i < count -1)
19124 *buf = xmlStrcat(*buf, BAD_CAST "', ");
19125 else
19126 *buf = xmlStrcat(*buf, BAD_CAST "'");
19127 if (value != NULL) {
19128 xmlFree((xmlChar *) value);
19129 value = NULL;
19130 }
19131 }
19132 *buf = xmlStrcat(*buf, BAD_CAST "]");
19133
19134 return (BAD_CAST *buf);
19135}
19136
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019137/**
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000019138 * xmlSchemaXPathProcessHistory:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019139 * @vctxt: the WXS validation context
19140 * @type: the simple/complex type of the current node if any at all
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019141 * @val: the precompiled value
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019142 *
19143 * Processes and pops the history items of the IDC state objects.
19144 * IDC key-sequences are validated/created on IDC bindings.
19145 *
19146 * Returns 0 on success and -1 on internal errors.
19147 */
19148static int
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000019149xmlSchemaXPathProcessHistory(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000019150 int depth)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019151{
19152 xmlSchemaIDCStateObjPtr sto, nextsto;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019153 int res, matchDepth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019154 xmlSchemaPSVIIDCKeyPtr key = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019155 xmlSchemaTypePtr type = vctxt->inode->typeDef;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019156
19157 if (vctxt->xpathStates == NULL)
19158 return (0);
19159 sto = vctxt->xpathStates;
19160
19161#if DEBUG_IDC
19162 {
19163 xmlChar *str = NULL;
19164 xmlGenericError(xmlGenericErrorContext,
19165 "IDC: BACK on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019166 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
19167 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019168 FREE_AND_NULL(str)
19169 }
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000019170#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019171 /*
19172 * Evaluate the state objects.
19173 */
19174 while (sto != NULL) {
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000019175 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
19176 if (res == -1) {
19177 VERROR_INT("xmlSchemaXPathProcessHistory",
19178 "calling xmlStreamPop()");
19179 return (-1);
19180 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019181#if DEBUG_IDC
19182 xmlGenericError(xmlGenericErrorContext, "IDC: stream pop '%s'\n",
19183 sto->sel->xpath);
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000019184#endif
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019185 if (sto->nbHistory == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019186 goto deregister_check;
19187
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019188 matchDepth = sto->history[sto->nbHistory -1];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019189
19190 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019191 * Only matches at the current depth are of interest.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019192 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000019193 if (matchDepth != depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019194 sto = sto->next;
19195 continue;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000019196 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019197 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
19198 if (! IS_SIMPLE_TYPE(type)) {
19199 /*
19200 * Not qualified if the field resolves to a node of non
19201 * simple type.
19202 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019203 xmlSchemaCustomErr((xmlSchemaAbstractCtxtPtr) vctxt,
19204 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019205 (xmlSchemaTypePtr) sto->matcher->aidc->def,
19206 "The field '%s' does evaluate to a node of "
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000019207 "non-simple type", sto->sel->xpath, NULL);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019208
19209 sto->nbHistory--;
19210 goto deregister_check;
19211 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019212 if ((key == NULL) && (vctxt->inode->val == NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019213 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019214 * Failed to provide the normalized value; maybe
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019215 * the value was invalid.
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000019216 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019217 VERROR(XML_SCHEMAV_CVC_IDC,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000019218 (xmlSchemaTypePtr) sto->matcher->aidc->def,
19219 "Warning: No precomputed value available, the value "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019220 "was either invalid or something strange happend");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019221 sto->nbHistory--;
19222 goto deregister_check;
19223 } else {
19224 xmlSchemaIDCMatcherPtr matcher = sto->matcher;
19225 xmlSchemaPSVIIDCKeyPtr *keySeq;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000019226 int pos, idx;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019227
19228 /*
19229 * The key will be anchored on the matcher's list of
19230 * key-sequences. The position in this list is determined
19231 * by the target node's depth relative to the matcher's
19232 * depth of creation (i.e. the depth of the scope element).
19233 */
19234 pos = sto->depth - matcher->depth;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000019235 idx = sto->sel->index;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019236
19237 /*
19238 * Create/grow the array of key-sequences.
19239 */
19240 if (matcher->keySeqs == NULL) {
19241 if (pos > 9)
19242 matcher->sizeKeySeqs = pos * 2;
19243 else
19244 matcher->sizeKeySeqs = 10;
19245 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
19246 xmlMalloc(matcher->sizeKeySeqs *
19247 sizeof(xmlSchemaPSVIIDCKeyPtr *));
19248 if (matcher->keySeqs == NULL) {
19249 xmlSchemaVErrMemory(NULL,
19250 "allocating an array of key-sequences",
19251 NULL);
19252 return(-1);
19253 }
19254 memset(matcher->keySeqs, 0,
19255 matcher->sizeKeySeqs *
19256 sizeof(xmlSchemaPSVIIDCKeyPtr *));
19257 } else if (pos >= matcher->sizeKeySeqs) {
19258 int i = matcher->sizeKeySeqs;
19259
19260 matcher->sizeKeySeqs *= 2;
19261 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
19262 xmlRealloc(matcher->keySeqs,
19263 matcher->sizeKeySeqs *
19264 sizeof(xmlSchemaPSVIIDCKeyPtr *));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019265 if (matcher->keySeqs == NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019266 xmlSchemaVErrMemory(NULL,
19267 "reallocating an array of key-sequences",
19268 NULL);
19269 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019270 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019271 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019272 * The array needs to be NULLed.
19273 * TODO: Use memset?
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019274 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019275 for (; i < matcher->sizeKeySeqs; i++)
19276 matcher->keySeqs[i] = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019277 }
19278
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019279 /*
19280 * Get/create the key-sequence.
19281 */
19282 keySeq = matcher->keySeqs[pos];
19283 if (keySeq == NULL) {
19284 goto create_sequence;
19285 } else {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000019286 if (keySeq[idx] != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019287 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019288 * cvc-identity-constraint:
19289 * 3 For each node in the ·target node set· all
19290 * of the {fields}, with that node as the context
19291 * node, evaluate to either an empty node-set or
19292 * a node-set with exactly one member, which must
19293 * have a simple type.
19294 *
19295 * The key was already set; report an error.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019296 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019297 xmlSchemaCustomErr((xmlSchemaAbstractCtxtPtr) vctxt,
19298 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000019299 (xmlSchemaTypePtr) matcher->aidc->def,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019300 "The field '%s' evaluates to a node-set "
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000019301 "with more than one member",
19302 sto->sel->xpath, NULL);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019303 sto->nbHistory--;
19304 goto deregister_check;
19305 } else {
19306 goto create_key;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019307 }
19308 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019309
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019310create_sequence:
19311 /*
19312 * Create a key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019313 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019314 keySeq = (xmlSchemaPSVIIDCKeyPtr *) xmlMalloc(
19315 matcher->aidc->def->nbFields *
19316 sizeof(xmlSchemaPSVIIDCKeyPtr));
19317 if (keySeq == NULL) {
19318 xmlSchemaVErrMemory(NULL,
19319 "allocating an IDC key-sequence", NULL);
19320 return(-1);
19321 }
19322 memset(keySeq, 0, matcher->aidc->def->nbFields *
19323 sizeof(xmlSchemaPSVIIDCKeyPtr));
19324 matcher->keySeqs[pos] = keySeq;
19325create_key:
19326 /*
19327 * Created a key once per node only.
19328 */
19329 if (key == NULL) {
19330 key = (xmlSchemaPSVIIDCKeyPtr) xmlMalloc(
19331 sizeof(xmlSchemaPSVIIDCKey));
19332 if (key == NULL) {
19333 xmlSchemaVErrMemory(NULL,
19334 "allocating a IDC key", NULL);
19335 xmlFree(keySeq);
19336 matcher->keySeqs[pos] = NULL;
19337 return(-1);
19338 }
19339 /*
19340 * Consume the compiled value.
19341 */
19342 key->type = type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019343 key->val = vctxt->inode->val;
19344 vctxt->inode->val = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019345 /*
19346 * Store the key in a global list.
19347 */
19348 if (xmlSchemaIDCStoreKey(vctxt, key) == -1) {
19349 xmlSchemaIDCFreeKey(key);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019350 return (-1);
19351 }
19352 }
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000019353 keySeq[idx] = key;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019354 }
19355 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019356
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019357 xmlSchemaPSVIIDCKeyPtr **keySeq = NULL;
19358 xmlSchemaPSVIIDCBindingPtr bind;
19359 xmlSchemaPSVIIDCNodePtr ntItem;
19360 xmlSchemaIDCMatcherPtr matcher;
19361 xmlSchemaIDCPtr idc;
19362 int pos, i, j, nbKeys;
19363 /*
19364 * Here we have the following scenario:
19365 * An IDC 'selector' state object resolved to a target node,
19366 * during the time this target node was in the
19367 * ancestor-or-self axis, the 'field' state object(s) looked
19368 * out for matching nodes to create a key-sequence for this
19369 * target node. Now we are back to this target node and need
19370 * to put the key-sequence, together with the target node
19371 * itself, into the node-table of the corresponding IDC
19372 * binding.
19373 */
19374 matcher = sto->matcher;
19375 idc = matcher->aidc->def;
19376 nbKeys = idc->nbFields;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000019377 pos = depth - matcher->depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019378 /*
19379 * Check if the matcher has any key-sequences at all, plus
19380 * if it has a key-sequence for the current target node.
19381 */
19382 if ((matcher->keySeqs == NULL) ||
19383 (matcher->sizeKeySeqs <= pos)) {
19384 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
19385 goto selector_key_error;
19386 else
19387 goto selector_leave;
19388 }
19389
19390 keySeq = &(matcher->keySeqs[pos]);
19391 if (*keySeq == NULL) {
19392 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
19393 goto selector_key_error;
19394 else
19395 goto selector_leave;
19396 }
19397
19398 for (i = 0; i < nbKeys; i++) {
19399 if ((*keySeq)[i] == NULL) {
19400 /*
19401 * Not qualified, if not all fields did resolve.
19402 */
19403 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY) {
19404 /*
19405 * All fields of a "key" IDC must resolve.
19406 */
19407 goto selector_key_error;
19408 }
19409 goto selector_leave;
19410 }
19411 }
19412 /*
19413 * All fields did resolve.
19414 */
19415
19416 /*
19417 * 4.1 If the {identity-constraint category} is unique(/key),
19418 * then no two members of the ·qualified node set· have
19419 * ·key-sequences· whose members are pairwise equal, as
19420 * defined by Equal in [XML Schemas: Datatypes].
19421 *
19422 * Get the IDC binding from the matcher and check for
19423 * duplicate key-sequences.
19424 */
19425 bind = xmlSchemaIDCAquireBinding(vctxt, matcher);
19426 if ((idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) &&
19427 (bind->nbNodes != 0)) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000019428 xmlSchemaPSVIIDCKeyPtr ckey, bkey, *bkeySeq;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019429
19430 i = 0;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000019431 res = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019432 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019433 * Compare the key-sequences, key by key.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019434 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019435 do {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000019436 bkeySeq = bind->nodeTable[i]->keys;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019437 for (j = 0; j < nbKeys; j++) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000019438 ckey = (*keySeq)[j];
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019439 bkey = bkeySeq[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019440 res = xmlSchemaAreValuesEqual(ckey->val, bkey->val);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019441 if (res == -1) {
19442 return (-1);
19443 } else if (res == 0)
19444 break;
19445 }
19446 if (res == 1) {
19447 /*
19448 * Duplicate found.
19449 */
19450 break;
19451 }
19452 i++;
19453 } while (i < bind->nbNodes);
19454 if (i != bind->nbNodes) {
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000019455 xmlChar *str = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019456 /*
19457 * TODO: Try to report the key-sequence.
19458 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019459 xmlSchemaCustomErr((xmlSchemaAbstractCtxtPtr) vctxt,
19460 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019461 (xmlSchemaTypePtr) idc,
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000019462 "Duplicate key-sequence %s",
19463 xmlSchemaFormatIDCKeySequence(vctxt, &str,
19464 (*keySeq), nbKeys), NULL);
19465 FREE_AND_NULL(str)
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019466 goto selector_leave;
19467 }
19468 }
19469 /*
19470 * Add a node-table item to the IDC binding.
19471 */
19472 ntItem = (xmlSchemaPSVIIDCNodePtr) xmlMalloc(
19473 sizeof(xmlSchemaPSVIIDCNode));
19474 if (ntItem == NULL) {
19475 xmlSchemaVErrMemory(NULL,
19476 "allocating an IDC node-table item", NULL);
19477 xmlFree(*keySeq);
19478 *keySeq = NULL;
19479 return(-1);
19480 }
19481 memset(ntItem, 0, sizeof(xmlSchemaPSVIIDCNode));
19482
19483 /*
19484 * Store the node-table item on global list.
19485 */
19486 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) {
19487 if (xmlSchemaIDCStoreNodeTableItem(vctxt, ntItem) == -1) {
19488 xmlFree(ntItem);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019489 xmlFree(*keySeq);
19490 *keySeq = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019491 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019492 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019493 }
19494 /*
19495 * Init the node-table item. Consume the key-sequence.
19496 */
19497 ntItem->node = vctxt->node;
19498 ntItem->keys = *keySeq;
19499 *keySeq = NULL;
19500 if (xmlSchemaIDCAppendNodeTableItem(bind, ntItem) == -1) {
19501 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
19502 /*
19503 * Free the item, since keyref items won't be
19504 * put on a global list.
19505 */
19506 xmlFree(ntItem->keys);
19507 xmlFree(ntItem);
19508 }
19509 return (-1);
19510 }
19511
19512 goto selector_leave;
19513selector_key_error:
19514 /*
19515 * 4.2.1 (KEY) The ·target node set· and the
19516 * ·qualified node set· are equal, that is, every
19517 * member of the ·target node set· is also a member
19518 * of the ·qualified node set· and vice versa.
19519 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019520 VERROR(XML_SCHEMAV_CVC_IDC, (xmlSchemaTypePtr) idc,
19521 "All 'key' fields must evaluate to a node");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019522selector_leave:
19523 /*
19524 * Free the key-sequence if not added to the IDC table.
19525 */
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000019526 if ((keySeq != NULL) && (*keySeq != NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019527 xmlFree(*keySeq);
19528 *keySeq = NULL;
19529 }
19530 } /* if selector */
19531
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019532 sto->nbHistory--;
19533
19534deregister_check:
19535 /*
19536 * Deregister state objects if they reach the depth of creation.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019537 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000019538 if ((sto->nbHistory == 0) && (sto->depth == depth)) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019539#if DEBUG_IDC
19540 xmlGenericError(xmlGenericErrorContext, "IDC: STO pop '%s'\n",
19541 sto->sel->xpath);
19542#endif
19543 if (vctxt->xpathStates != sto) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019544 VERROR_INT("xmlSchemaXPathProcessHistory",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019545 "The state object to be removed is not the first "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019546 "in the list");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019547 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019548 nextsto = sto->next;
19549 /*
19550 * Unlink from the list of active XPath state objects.
19551 */
19552 vctxt->xpathStates = sto->next;
19553 sto->next = vctxt->xpathStatePool;
19554 /*
19555 * Link it to the pool of reusable state objects.
19556 */
19557 vctxt->xpathStatePool = sto;
19558 sto = nextsto;
19559 } else
19560 sto = sto->next;
19561 } /* while (sto != NULL) */
19562 return (0);
19563}
19564
19565/**
19566 * xmlSchemaIDCRegisterMatchers:
19567 * @vctxt: the WXS validation context
19568 * @elemDecl: the element declaration
19569 *
19570 * Creates helper objects to evaluate IDC selectors/fields
19571 * successively.
19572 *
19573 * Returns 0 if OK and -1 on internal errors.
19574 */
19575static int
19576xmlSchemaIDCRegisterMatchers(xmlSchemaValidCtxtPtr vctxt,
19577 xmlSchemaElementPtr elemDecl)
19578{
19579 xmlSchemaIDCMatcherPtr matcher, last = NULL;
19580 xmlSchemaIDCPtr idc, refIdc;
19581 xmlSchemaIDCAugPtr aidc;
19582
19583 idc = (xmlSchemaIDCPtr) elemDecl->idcs;
19584 if (idc == NULL)
19585 return (0);
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000019586
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019587#if DEBUG_IDC
19588 {
19589 xmlChar *str = NULL;
19590 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019591 "IDC: REGISTER on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019592 (char *) xmlSchemaFormatQName(&str, vctxt->inode->nsName,
19593 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019594 FREE_AND_NULL(str)
19595 }
19596#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019597 if (vctxt->inode->idcMatchers != NULL) {
19598 VERROR_INT("xmlSchemaIDCRegisterMatchers",
19599 "The chain of IDC matchers is expected to be empty");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019600 return (-1);
19601 }
19602 do {
19603 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
19604 /*
19605 * Since IDCs bubbles are expensive we need to know the
19606 * depth at which the bubbles should stop; this will be
19607 * the depth of the top-most keyref IDC. If no keyref
19608 * references a key/unique IDC, the bubbleDepth will
19609 * be -1, indicating that no bubbles are needed.
19610 */
19611 refIdc = (xmlSchemaIDCPtr) idc->ref->item;
19612 if (refIdc != NULL) {
19613 /*
19614 * Lookup the augmented IDC.
19615 */
19616 aidc = vctxt->aidcs;
19617 while (aidc != NULL) {
19618 if (aidc->def == refIdc)
19619 break;
19620 aidc = aidc->next;
19621 }
19622 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019623 VERROR_INT("xmlSchemaIDCRegisterMatchers",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019624 "Could not find an augmented IDC item for an IDC "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019625 "definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019626 return (-1);
19627 }
19628 if ((aidc->bubbleDepth == -1) ||
19629 (vctxt->depth < aidc->bubbleDepth))
19630 aidc->bubbleDepth = vctxt->depth;
19631 }
19632 }
19633 /*
19634 * Lookup the augmented IDC item for the IDC definition.
19635 */
19636 aidc = vctxt->aidcs;
19637 while (aidc != NULL) {
19638 if (aidc->def == idc)
19639 break;
19640 aidc = aidc->next;
19641 }
19642 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019643 VERROR_INT("xmlSchemaIDCRegisterMatchers",
19644 "Could not find an augmented IDC item for an IDC definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019645 return (-1);
19646 }
19647 /*
19648 * Create an IDC matcher for every IDC definition.
19649 */
19650 matcher = (xmlSchemaIDCMatcherPtr)
19651 xmlMalloc(sizeof(xmlSchemaIDCMatcher));
19652 if (matcher == NULL) {
19653 xmlSchemaVErrMemory(vctxt,
19654 "allocating an IDC matcher", NULL);
19655 return (-1);
19656 }
19657 memset(matcher, 0, sizeof(xmlSchemaIDCMatcher));
19658 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019659 vctxt->inode->idcMatchers = matcher;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019660 else
19661 last->next = matcher;
19662 last = matcher;
19663
19664 matcher->type = IDC_MATCHER;
19665 matcher->depth = vctxt->depth;
19666 matcher->aidc = aidc;
19667#if DEBUG_IDC
19668 xmlGenericError(xmlGenericErrorContext, "IDC: register matcher\n");
19669#endif
19670 /*
19671 * Init the automaton state object.
19672 */
19673 if (xmlSchemaIDCAddStateObject(vctxt, matcher,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019674 idc->selector, XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) == -1)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019675 return (-1);
19676
19677 idc = idc->next;
19678 } while (idc != NULL);
19679 return (0);
19680}
19681
19682/**
19683 * xmlSchemaBubbleIDCNodeTables:
19684 * @depth: the current tree depth
19685 *
19686 * Merges IDC bindings of an element at @depth into the corresponding IDC
19687 * bindings of its parent element. If a duplicate note-table entry is found,
19688 * both, the parent node-table entry and child entry are discarded from the
19689 * node-table of the parent.
19690 *
19691 * Returns 0 if OK and -1 on internal errors.
19692 */
19693static int
19694xmlSchemaBubbleIDCNodeTables(xmlSchemaValidCtxtPtr vctxt)
19695{
19696 xmlSchemaPSVIIDCBindingPtr bind; /* IDC bindings of the current node. */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000019697 xmlSchemaPSVIIDCBindingPtr *parTable, parBind = NULL, lastParBind = NULL; /* parent IDC bindings. */
19698 xmlSchemaPSVIIDCNodePtr node, parNode = NULL; /* node-table entries. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019699 xmlSchemaPSVIIDCKeyPtr key, parKey; /* keys of in a key-sequence. */
19700 xmlSchemaIDCAugPtr aidc;
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000019701 int i, j, k, ret = 0, oldNum, newDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019702 int duplTop;
19703
19704 /*
19705 * The node table has the following sections:
19706 *
19707 * O --> old node-table entries (first)
19708 * O
19709 * + --> new node-table entries
19710 * +
19711 * % --> new duplicate node-table entries
19712 * %
19713 * # --> old duplicate node-table entries
19714 * # (last)
19715 *
19716 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019717 bind = vctxt->inode->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019718 if (bind == NULL) {
19719 /* Fine, no table, no bubbles. */
19720 return (0);
19721 }
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000019722
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019723 parTable = &(vctxt->elemInfos[vctxt->depth -1]->idcTable);
19724 /*
19725 * Walk all bindings; create new or add to existing bindings.
19726 * Remove duplicate key-sequences.
19727 */
19728start_binding:
19729 while (bind != NULL) {
19730 /*
19731 * Skip keyref IDCs.
19732 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019733 if (bind->definition->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
19734 bind = bind->next;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019735 continue;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019736 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019737 /*
19738 * Check if the key/unique IDC table needs to be bubbled.
19739 */
19740 aidc = vctxt->aidcs;
19741 do {
19742 if (aidc->def == bind->definition) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019743 if ((aidc->bubbleDepth == -1) ||
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000019744 (aidc->bubbleDepth >= vctxt->depth)) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019745 bind = bind->next;
19746 goto start_binding;
19747 }
19748 break;
19749 }
19750 aidc = aidc->next;
19751 } while (aidc != NULL);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019752
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019753 if (parTable != NULL)
19754 parBind = *parTable;
19755 while (parBind != NULL) {
19756 /*
19757 * Search a matching parent binding for the
19758 * IDC definition.
19759 */
19760 if (parBind->definition == bind->definition) {
19761
19762 /*
19763 * Compare every node-table entry of the child node,
19764 * i.e. the key-sequence within, ...
19765 */
19766 oldNum = parBind->nbNodes; /* Skip newly added items. */
19767 duplTop = oldNum + parBind->nbDupls;
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000019768 newDupls = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019769
19770 for (i = 0; i < bind->nbNodes; i++) {
19771 node = bind->nodeTable[i];
19772 if (node == NULL)
19773 continue;
19774 /*
19775 * ...with every key-sequence of the parent node, already
19776 * evaluated to be a duplicate key-sequence.
19777 */
19778 if (parBind->nbDupls != 0) {
19779 j = bind->nbNodes + newDupls;
19780 while (j < duplTop) {
19781 parNode = parBind->nodeTable[j];
19782 for (k = 0; k < bind->definition->nbFields; k++) {
19783 key = node->keys[k];
19784 parKey = parNode->keys[k];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019785 ret = xmlSchemaAreValuesEqual(key->val,
19786 parKey->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019787 if (ret == -1) {
19788 /* TODO: Internal error */
19789 return(-1);
19790 } else if (ret == 0)
19791 break;
19792
19793 }
19794 if (ret == 1)
19795 /* Duplicate found. */
19796 break;
19797 j++;
19798 }
19799 if (j != duplTop) {
19800 /* Duplicate found. */
19801 continue;
19802 }
19803 }
19804 /*
19805 * ... and with every key-sequence of the parent node.
19806 */
19807 j = 0;
19808 while (j < oldNum) {
19809 parNode = parBind->nodeTable[j];
19810 /*
19811 * Compare key by key.
19812 */
19813 for (k = 0; k < parBind->definition->nbFields; k++) {
19814 key = node->keys[k];
19815 parKey = parNode->keys[k];
19816
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019817 ret = xmlSchemaAreValuesEqual(key->val,
19818 parKey->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019819 if (ret == -1) {
19820 /* TODO: Internal error */
19821 } else if (ret == 0)
19822 break;
19823
19824 }
19825 if (ret == 1)
19826 /*
19827 * The key-sequences are equal.
19828 */
19829 break;
19830 j++;
19831 }
19832 if (j != oldNum) {
19833 /*
19834 * Handle duplicates.
19835 */
19836 newDupls++;
19837 oldNum--;
19838 parBind->nbNodes--;
19839 /*
19840 * Move last old item to pos of duplicate.
19841 */
19842 parBind->nodeTable[j] =
19843 parBind->nodeTable[oldNum];
19844
19845 if (parBind->nbNodes != oldNum) {
19846 /*
19847 * If new items exist, move last new item to
19848 * last of old items.
19849 */
19850 parBind->nodeTable[oldNum] =
19851 parBind->nodeTable[parBind->nbNodes];
19852 }
19853 /*
19854 * Move duplicate to last pos of new/old items.
19855 */
19856 parBind->nodeTable[parBind->nbNodes] = parNode;
19857
19858 } else {
19859 /*
19860 * Add the node-table entry (node and key-sequence) of
19861 * the child node to the node table of the parent node.
19862 */
19863 if (parBind->nodeTable == NULL) {
19864 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000019865 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019866 if (parBind->nodeTable == NULL) {
19867 xmlSchemaVErrMemory(NULL,
19868 "allocating IDC list of node-table items", NULL);
19869 return(-1);
19870 }
19871 parBind->sizeNodes = 1;
19872 } else if (duplTop >= parBind->sizeNodes) {
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000019873 parBind->sizeNodes *= 2;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019874 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
19875 xmlRealloc(parBind->nodeTable, parBind->sizeNodes *
19876 sizeof(xmlSchemaPSVIIDCNodePtr));
19877 if (parBind->nodeTable == NULL) {
19878 xmlSchemaVErrMemory(NULL,
19879 "re-allocating IDC list of node-table items", NULL);
19880 return(-1);
19881 }
19882 }
19883
19884 /*
19885 * Move first old duplicate to last position
19886 * of old duplicates +1.
19887 */
19888 if (parBind->nbDupls != 0) {
19889 parBind->nodeTable[duplTop] =
19890 parBind->nodeTable[parBind->nbNodes + newDupls];
19891 }
19892 /*
19893 * Move first new duplicate to last position of
19894 * new duplicates +1.
19895 */
19896 if (newDupls != 0) {
19897 parBind->nodeTable[parBind->nbNodes + newDupls] =
19898 parBind->nodeTable[parBind->nbNodes];
19899 }
19900 /*
19901 * Append the new node-table entry to the 'new node-table
19902 * entries' section.
19903 */
19904 parBind->nodeTable[parBind->nbNodes] = node;
19905 parBind->nbNodes++;
19906 duplTop++;
19907 }
19908 }
19909 parBind->nbDupls += newDupls;
19910 break;
19911 }
19912 if (parBind->next == NULL)
19913 lastParBind = parBind;
19914 parBind = parBind->next;
19915 }
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000019916 if ((parBind == NULL) && (bind->nbNodes != 0)) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019917 /*
19918 * No binding for the IDC was found: create a new one and
19919 * copy all node-tables.
19920 */
19921 parBind = xmlSchemaIDCNewBinding(bind->definition);
19922 if (parBind == NULL)
19923 return(-1);
19924
19925 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
19926 xmlMalloc(bind->nbNodes * sizeof(xmlSchemaPSVIIDCNodePtr));
19927 if (parBind->nodeTable == NULL) {
19928 xmlSchemaVErrMemory(NULL,
19929 "allocating an array of IDC node-table items", NULL);
19930 xmlSchemaIDCFreeBinding(parBind);
19931 return(-1);
19932 }
19933 parBind->sizeNodes = bind->nbNodes;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000019934 parBind->nbNodes = bind->nbNodes;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019935 memcpy(parBind->nodeTable, bind->nodeTable,
19936 bind->nbNodes * sizeof(xmlSchemaPSVIIDCNodePtr));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019937 if (*parTable == NULL)
19938 *parTable = parBind;
19939 else
19940 lastParBind->next = parBind;
19941 }
19942 bind = bind->next;
19943 }
19944 return (0);
19945}
19946
19947/**
19948 * xmlSchemaCheckCVCIDCKeyRef:
19949 * @vctxt: the WXS validation context
19950 * @elemDecl: the element declaration
19951 *
19952 * Check the cvc-idc-keyref constraints.
19953 */
19954static int
19955xmlSchemaCheckCVCIDCKeyRef(xmlSchemaValidCtxtPtr vctxt)
19956{
19957 xmlSchemaPSVIIDCBindingPtr refbind, bind;
19958
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019959 refbind = vctxt->inode->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019960 /*
19961 * Find a keyref.
19962 */
19963 while (refbind != NULL) {
19964 if (refbind->definition->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
19965 int i, j, k, res;
19966 xmlSchemaPSVIIDCKeyPtr *refKeys, *keys;
19967 xmlSchemaPSVIIDCKeyPtr refKey, key;
19968
19969 /*
19970 * Find the referred key/unique.
19971 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019972 bind = vctxt->inode->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019973 do {
19974 if ((xmlSchemaIDCPtr) refbind->definition->ref->item ==
19975 bind->definition)
19976 break;
19977 bind = bind->next;
19978 } while (bind != NULL);
19979
19980 /*
19981 * Search for a matching key-sequences.
19982 */
19983 for (i = 0; i < refbind->nbNodes; i++) {
19984 res = 0;
19985 if (bind != NULL) {
19986 refKeys = refbind->nodeTable[i]->keys;
19987 for (j = 0; j < bind->nbNodes; j++) {
19988 keys = bind->nodeTable[j]->keys;
19989 for (k = 0; k < bind->definition->nbFields; k++) {
19990 refKey = refKeys[k];
19991 key = keys[k];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019992 res = xmlSchemaAreValuesEqual(key->val,
19993 refKey->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000019994 if (res == 0)
19995 break;
19996 else if (res == -1) {
19997 return (-1);
19998 }
19999 }
20000 if (res == 1) {
20001 /*
20002 * Match found.
20003 */
20004 break;
20005 }
20006 }
20007 }
20008 if (res == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020009 xmlChar *str = NULL, *strB = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020010 /* TODO: Report the key-sequence. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020011 xmlSchemaCustomErr((xmlSchemaAbstractCtxtPtr) vctxt,
20012 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020013 (xmlSchemaTypePtr) refbind->definition,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020014 "No match found for key-sequence %s of key "
20015 "reference '%s'",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000020016 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020017 refbind->nodeTable[i]->keys,
20018 refbind->definition->nbFields),
20019 xmlSchemaFormatQName(&strB,
20020 refbind->definition->targetNamespace,
20021 refbind->definition->name));
20022 FREE_AND_NULL(str);
20023 FREE_AND_NULL(strB);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020024 }
20025 }
20026 }
20027 refbind = refbind->next;
20028 }
20029 return (0);
20030}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020031
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020032/************************************************************************
20033 * *
20034 * XML Reader validation code *
20035 * *
20036 ************************************************************************/
20037
20038static xmlSchemaAttrInfoPtr
20039xmlSchemaGetFreshAttrInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020040{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020041 xmlSchemaAttrInfoPtr iattr;
20042 /*
20043 * Grow/create list of attribute infos.
20044 */
20045 if (vctxt->attrInfos == NULL) {
20046 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
20047 xmlMalloc(sizeof(xmlSchemaAttrInfoPtr));
20048 vctxt->sizeAttrInfos = 1;
20049 if (vctxt->attrInfos == NULL) {
20050 xmlSchemaVErrMemory(vctxt,
20051 "allocating attribute info list", NULL);
20052 return (NULL);
20053 }
20054 } else if (vctxt->sizeAttrInfos <= vctxt->nbAttrInfos) {
20055 vctxt->sizeAttrInfos++;
20056 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
20057 xmlRealloc(vctxt->attrInfos,
20058 vctxt->sizeAttrInfos * sizeof(xmlSchemaAttrInfoPtr));
20059 if (vctxt->attrInfos == NULL) {
20060 xmlSchemaVErrMemory(vctxt,
20061 "re-allocating attribute info list", NULL);
20062 return (NULL);
20063 }
20064 } else {
20065 iattr = vctxt->attrInfos[vctxt->nbAttrInfos++];
20066 if (iattr->localName != NULL) {
20067 VERROR_INT("xmlSchemaGetFreshAttrInfo",
20068 "attr info not cleared");
20069 return (NULL);
20070 }
20071 iattr->nodeType = XML_ATTRIBUTE_NODE;
20072 return (iattr);
20073 }
20074 /*
20075 * Create an attribute info.
20076 */
20077 iattr = (xmlSchemaAttrInfoPtr)
20078 xmlMalloc(sizeof(xmlSchemaAttrInfo));
20079 if (iattr == NULL) {
20080 xmlSchemaVErrMemory(vctxt, "creating new attribute info", NULL);
20081 return (NULL);
20082 }
20083 memset(iattr, 0, sizeof(xmlSchemaAttrInfo));
20084 iattr->nodeType = XML_ATTRIBUTE_NODE;
20085 vctxt->attrInfos[vctxt->nbAttrInfos++] = iattr;
20086
20087 return (iattr);
20088}
20089
20090static int
20091xmlSchemaValidatorPushAttribute(xmlSchemaValidCtxtPtr vctxt,
20092 xmlNodePtr attrNode,
20093 const xmlChar *localName,
20094 const xmlChar *nsName,
20095 int ownedNames,
20096 xmlChar *value,
20097 int ownedValue)
20098{
20099 xmlSchemaAttrInfoPtr attr;
20100
20101 attr = xmlSchemaGetFreshAttrInfo(vctxt);
20102 if (attr == NULL) {
20103 VERROR_INT("xmlSchemaPushAttribute",
20104 "calling xmlSchemaGetFreshAttrInfo()");
20105 return (-1);
20106 }
20107 attr->node = attrNode;
20108 attr->state = XML_SCHEMAS_ATTR_UNKNOWN;
20109 attr->localName = localName;
20110 attr->nsName = nsName;
20111 if (ownedNames)
20112 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
20113 /*
20114 * Evaluate if it's an XSI attribute.
20115 */
20116 if (nsName != NULL) {
20117 if (xmlStrEqual(localName, BAD_CAST "nil")) {
20118 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
20119 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NIL;
20120 }
20121 } else if (xmlStrEqual(localName, BAD_CAST "type")) {
20122 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
20123 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_TYPE;
20124 }
20125 } else if (xmlStrEqual(localName, BAD_CAST "schemaLocation")) {
20126 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
20127 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC;
20128 }
20129 } else if (xmlStrEqual(localName, BAD_CAST "noNamespaceSchemaLocation")) {
20130 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
20131 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC;
20132 }
20133 } else if (xmlStrEqual(attr->nsName, xmlNamespaceNs)) {
20134 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XMLNS;
20135 }
20136 }
20137 attr->value = value;
20138 if (ownedValue)
20139 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
20140 if (attr->metaType != 0)
20141 attr->state = XML_SCHEMAS_ATTR_META;
20142 return (0);
20143}
20144
20145static void
20146xmlSchemaClearElemInfo(xmlSchemaNodeInfoPtr ielem)
20147{
20148 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
20149 FREE_AND_NULL(ielem->localName);
20150 FREE_AND_NULL(ielem->nsName);
20151 } else {
20152 ielem->localName = NULL;
20153 ielem->nsName = NULL;
20154 }
20155 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
20156 FREE_AND_NULL(ielem->value);
20157 } else {
20158 ielem->value = NULL;
20159 }
20160 if (ielem->val != NULL) {
20161 xmlSchemaFreeValue(ielem->val);
20162 ielem->val = NULL;
20163 }
20164 if (ielem->idcMatchers != NULL) {
20165 xmlSchemaIDCFreeMatcherList(ielem->idcMatchers);
20166 ielem->idcMatchers = NULL;
20167 }
20168 if (ielem->idcTable != NULL) {
20169 xmlSchemaIDCFreeIDCTable(ielem->idcTable);
20170 ielem->idcTable = NULL;
20171 }
20172 if (ielem->regexCtxt != NULL) {
20173 xmlRegFreeExecCtxt(ielem->regexCtxt);
20174 ielem->regexCtxt = NULL;
20175 }
20176 if (ielem->nsBindings != NULL) {
20177 xmlFree((xmlChar **)ielem->nsBindings);
20178 ielem->nsBindings = NULL;
20179 ielem->nbNsBindings = 0;
20180 ielem->sizeNsBindings = 0;
20181 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020182}
20183
20184/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020185 * xmlSchemaGetFreshElemInfo:
20186 * @vctxt: the schema validation context
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020187 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020188 * Creates/reuses and initializes the element info item for
20189 * the currect tree depth.
20190 *
20191 * Returns the element info item or NULL on API or internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020192 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020193static xmlSchemaNodeInfoPtr
20194xmlSchemaGetFreshElemInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020195{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020196 xmlSchemaNodeInfoPtr info = NULL;
20197
20198 if (vctxt->depth > vctxt->sizeElemInfos) {
20199 VERROR_INT("xmlSchemaGetFreshElemInfo",
20200 "inconsistent depth encountered");
20201 return (NULL);
20202 }
20203 if (vctxt->elemInfos == NULL) {
20204 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
20205 xmlMalloc(10 * sizeof(xmlSchemaNodeInfoPtr));
20206 if (vctxt->elemInfos == NULL) {
20207 xmlSchemaVErrMemory(vctxt,
20208 "allocating the element info array", NULL);
20209 return (NULL);
20210 }
20211 memset(vctxt->elemInfos, 0, 10 * sizeof(xmlSchemaNodeInfoPtr));
20212 vctxt->sizeElemInfos = 10;
20213 } else if (vctxt->sizeElemInfos <= vctxt->depth) {
20214 int i = vctxt->sizeElemInfos;
20215
20216 vctxt->sizeElemInfos *= 2;
20217 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
20218 xmlRealloc(vctxt->elemInfos, vctxt->sizeElemInfos *
20219 sizeof(xmlSchemaNodeInfoPtr));
20220 if (vctxt->elemInfos == NULL) {
20221 xmlSchemaVErrMemory(vctxt,
20222 "re-allocating the element info array", NULL);
20223 return (NULL);
20224 }
20225 /*
20226 * We need the new memory to be NULLed.
20227 * TODO: Use memset instead?
20228 */
20229 for (; i < vctxt->sizeElemInfos; i++)
20230 vctxt->elemInfos[i] = NULL;
20231 } else
20232 info = vctxt->elemInfos[vctxt->depth];
20233
20234 if (info == NULL) {
20235 info = (xmlSchemaNodeInfoPtr)
20236 xmlMalloc(sizeof(xmlSchemaNodeInfo));
20237 if (info == NULL) {
20238 xmlSchemaVErrMemory(vctxt,
20239 "allocating an element info", NULL);
20240 return (NULL);
20241 }
20242 vctxt->elemInfos[vctxt->depth] = info;
20243 } else {
20244 if (info->localName != NULL) {
20245 VERROR_INT("xmlSchemaGetFreshElemInfo",
20246 "elem info has not been cleared");
20247 return (NULL);
20248 }
20249 }
20250 memset(info, 0, sizeof(xmlSchemaNodeInfo));
20251 info->nodeType = XML_ELEMENT_NODE;
20252 info->depth = vctxt->depth;
20253
20254 return (info);
20255}
20256
20257#define ACTIVATE_ATTRIBUTE(item) vctxt->inode = (xmlSchemaNodeInfoPtr) item;
20258#define ACTIVATE_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth];
20259#define ACTIVATE_PARENT_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth -1];
20260
20261static int
20262xmlSchemaValidateFacets(xmlSchemaAbstractCtxtPtr actxt,
20263 xmlNodePtr node,
20264 xmlSchemaTypePtr type,
20265 xmlSchemaValType valType,
20266 const xmlChar * value,
20267 xmlSchemaValPtr val,
20268 unsigned long length,
20269 int fireErrors)
20270{
20271 int ret, error = 0;
20272
20273 xmlSchemaTypePtr tmpType;
20274 xmlSchemaFacetLinkPtr facetLink;
20275 xmlSchemaFacetPtr facet;
20276 unsigned long len = 0;
20277 xmlSchemaWhitespaceValueType ws;
20278
20279 /*
20280 * In Libxml2, derived built-in types have currently no explicit facets.
20281 */
20282 if (type->type == XML_SCHEMA_TYPE_BASIC)
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000020283 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020284
20285 /*
20286 * NOTE: Do not jump away, if the facetSet of the given type is
20287 * empty: until now, "pattern" and "enumeration" facets of the
20288 * *base types* need to be checked as well.
20289 */
20290 if (type->facetSet == NULL)
20291 goto pattern_and_enum;
20292
20293 if (! VARIETY_ATOMIC(type)) {
20294 if (VARIETY_LIST(type))
20295 goto variety_list;
20296 else
20297 goto pattern_and_enum;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020298 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020299 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020300 * Whitespace handling is only of importance for string-based
20301 * types.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020302 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020303 tmpType = xmlSchemaGetPrimitiveType(type);
20304 if ((tmpType->builtInType == XML_SCHEMAS_STRING) ||
20305 IS_ANY_SIMPLE_TYPE(tmpType)) {
20306 ws = xmlSchemaGetWhiteSpaceFacetValue(type);
20307 } else
20308 ws = XML_SCHEMA_WHITESPACE_COLLAPSE;
20309 /*
20310 * If the value was not computed (for string or
20311 * anySimpleType based types), then use the provided
20312 * type.
20313 */
20314 if (val == NULL)
20315 valType = valType;
20316 else
20317 valType = xmlSchemaGetValType(val);
20318
20319 ret = 0;
20320 for (facetLink = type->facetSet; facetLink != NULL;
20321 facetLink = facetLink->next) {
20322 /*
20323 * Skip the pattern "whiteSpace": it is used to
20324 * format the character content beforehand.
20325 */
20326 switch (facetLink->facet->type) {
20327 case XML_SCHEMA_FACET_WHITESPACE:
20328 case XML_SCHEMA_FACET_PATTERN:
20329 case XML_SCHEMA_FACET_ENUMERATION:
20330 continue;
20331 case XML_SCHEMA_FACET_LENGTH:
20332 case XML_SCHEMA_FACET_MINLENGTH:
20333 case XML_SCHEMA_FACET_MAXLENGTH:
20334 ret = xmlSchemaValidateLengthFacetWhtsp(facetLink->facet,
20335 valType, value, val, &len, ws);
20336 break;
20337 default:
20338 ret = xmlSchemaValidateFacetWhtsp(facetLink->facet, ws,
20339 valType, value, val, ws);
20340 break;
20341 }
20342 if (ret < 0) {
20343 AERROR_INT("xmlSchemaValidateFacets",
20344 "validating against a atomic type facet");
20345 return (-1);
20346 } else if (ret > 0) {
20347 if (fireErrors)
20348 xmlSchemaFacetErr(actxt, ret, node,
20349 value, len, type, facetLink->facet, NULL, NULL, NULL);
20350 else
20351 return (ret);
20352 if (error == 0)
20353 error = ret;
20354 }
20355 ret = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020356 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020357
20358variety_list:
20359 if (! VARIETY_LIST(type))
20360 goto pattern_and_enum;
20361 /*
20362 * "length", "minLength" and "maxLength" of list types.
20363 */
20364 ret = 0;
20365 for (facetLink = type->facetSet; facetLink != NULL;
20366 facetLink = facetLink->next) {
20367
20368 switch (facetLink->facet->type) {
20369 case XML_SCHEMA_FACET_LENGTH:
20370 case XML_SCHEMA_FACET_MINLENGTH:
20371 case XML_SCHEMA_FACET_MAXLENGTH:
20372 ret = xmlSchemaValidateListSimpleTypeFacet(facetLink->facet,
20373 value, length, NULL);
20374 break;
20375 default:
20376 continue;
20377 }
20378 if (ret < 0) {
20379 AERROR_INT("xmlSchemaValidateFacets",
20380 "validating against a list type facet");
20381 return (-1);
20382 } else if (ret > 0) {
20383 if (fireErrors)
20384 xmlSchemaFacetErr(actxt, ret, node,
20385 value, length, type, facetLink->facet, NULL, NULL, NULL);
20386 else
20387 return (ret);
20388 if (error == 0)
20389 error = ret;
20390 }
20391 ret = 0;
20392 }
20393
20394pattern_and_enum:
20395 if (error >= 0) {
20396 int found = 0;
20397 /*
20398 * Process enumerations. Facet values are in the value space
20399 * of the defining type's base type. This seems to be a bug in the
20400 * XML Schema 1.0 spec. Use the whitespace type of the base type.
20401 * Only the first set of enumerations in the ancestor-or-self axis
20402 * is used for validation.
20403 */
20404 ret = 0;
20405 tmpType = type;
20406 do {
20407 for (facet = tmpType->facets; facet != NULL; facet = facet->next) {
20408 if (facet->type != XML_SCHEMA_FACET_ENUMERATION)
20409 continue;
20410 found = 1;
20411 ret = xmlSchemaAreValuesEqual(facet->val, val);
20412 if (ret == 1)
20413 break;
20414 else if (ret < 0) {
20415 AERROR_INT("xmlSchemaValidateFacets",
20416 "validating against an enumeration facet");
20417 return (-1);
20418 }
20419 }
20420 if (ret != 0)
20421 break;
20422 tmpType = tmpType->baseType;
20423 } while ((tmpType != NULL) &&
20424 (tmpType->type != XML_SCHEMA_TYPE_BASIC));
20425 if (found && (ret == 0)) {
20426 ret = XML_SCHEMAV_CVC_ENUMERATION_VALID;
20427 if (fireErrors) {
20428 xmlSchemaFacetErr(actxt, ret, node,
20429 value, 0, type, NULL, NULL, NULL, NULL);
20430 } else
20431 return (ret);
20432 if (error == 0)
20433 error = ret;
20434 }
20435 }
20436
20437 if (error >= 0) {
20438 int found;
20439 /*
20440 * Process patters. Pattern facets are ORed at type level
20441 * and ANDed if derived. Walk the base type axis.
20442 */
20443 tmpType = type;
20444 facet = NULL;
20445 do {
20446 found = 0;
20447 for (facetLink = tmpType->facetSet; facetLink != NULL;
20448 facetLink = facetLink->next) {
20449 if (facetLink->facet->type != XML_SCHEMA_FACET_PATTERN)
20450 continue;
20451 found = 1;
20452 /*
20453 * NOTE that for patterns, @value needs to be the
20454 * normalized vaule.
20455 */
20456 ret = xmlRegexpExec(facetLink->facet->regexp, value);
20457 if (ret == 1)
20458 break;
20459 else if (ret < 0) {
20460 AERROR_INT("xmlSchemaValidateFacets",
20461 "validating against a pattern facet");
20462 return (-1);
20463 } else {
20464 /*
20465 * Save the last non-validating facet.
20466 */
20467 facet = facetLink->facet;
20468 }
20469 }
20470 if (found && (ret != 1)) {
20471 ret = XML_SCHEMAV_CVC_PATTERN_VALID;
20472 if (fireErrors) {
20473 xmlSchemaFacetErr(actxt, ret, node,
20474 value, 0, type, facet, NULL, NULL, NULL);
20475 } else
20476 return (ret);
20477 if (error == 0)
20478 error = ret;
20479 break;
20480 }
20481 tmpType = tmpType->baseType;
20482 } while ((tmpType != NULL) && (tmpType->type != XML_SCHEMA_TYPE_BASIC));
20483 }
20484
20485 return (error);
20486}
20487
20488static xmlChar *
20489xmlSchemaNormalizeValue(xmlSchemaTypePtr type,
20490 const xmlChar *value)
20491{
20492 switch (xmlSchemaGetWhiteSpaceFacetValue(type)) {
20493 case XML_SCHEMA_WHITESPACE_COLLAPSE:
20494 return (xmlSchemaCollapseString(value));
20495 case XML_SCHEMA_WHITESPACE_REPLACE:
20496 return (xmlSchemaWhiteSpaceReplace(value));
20497 default:
20498 return (NULL);
20499 }
20500}
20501
20502static int
20503xmlSchemaValidateQName(xmlSchemaValidCtxtPtr vctxt,
20504 const xmlChar *value,
20505 xmlSchemaValPtr *val,
20506 int valNeeded)
20507{
20508 int ret;
20509 const xmlChar *nsName;
20510 xmlChar *local, *prefix = NULL;
20511
20512 ret = xmlValidateQName(value, 1);
20513 if (ret != 0) {
20514 if (ret == -1) {
20515 VERROR_INT("xmlSchemaValidateQName",
20516 "calling xmlValidateQName()");
20517 return (-1);
20518 }
20519 return( XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1);
20520 }
20521 /*
20522 * NOTE: xmlSplitQName2 will always return a duplicated
20523 * strings.
20524 */
20525 local = xmlSplitQName2(value, &prefix);
20526 if (local == NULL)
20527 local = xmlStrdup(value);
20528 /*
20529 * OPTIMIZE TODO: Use flags for:
20530 * - is there any namespace binding?
20531 * - is there a default namespace?
20532 */
20533 nsName = xmlSchemaLookupNamespace(vctxt, prefix);
20534
20535 if (prefix != NULL) {
20536 xmlFree(prefix);
20537 /*
20538 * A namespace must be found if the prefix is
20539 * NOT NULL.
20540 */
20541 if (nsName == NULL) {
20542 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
20543 xmlSchemaCustomErr((xmlSchemaAbstractCtxtPtr) vctxt, ret, NULL,
20544 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
20545 "The QName value '%s' has no "
20546 "corresponding namespace declaration in "
20547 "scope", value, NULL);
20548 if (local != NULL)
20549 xmlFree(local);
20550 return (ret);
20551 }
20552 }
20553 if (valNeeded && val) {
20554 if (nsName != NULL)
20555 *val = xmlSchemaNewQNameValue(
20556 BAD_CAST xmlStrdup(nsName), BAD_CAST local);
20557 else
20558 *val = xmlSchemaNewQNameValue(NULL,
20559 BAD_CAST local);
20560 } else
20561 xmlFree(local);
20562 return (0);
20563}
20564
20565/*
20566* cvc-simple-type
20567*/
20568static int
20569xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
20570 xmlNodePtr node,
20571 xmlSchemaTypePtr type,
20572 const xmlChar *value,
20573 xmlSchemaValPtr *retVal,
20574 int fireErrors,
20575 int normalize,
20576 int isNormalized)
20577{
20578 int ret = 0, valNeeded = (retVal) ? 1 : 0;
20579 xmlSchemaValPtr val = NULL;
20580 xmlSchemaWhitespaceValueType ws;
20581 xmlChar *normValue = NULL;
20582
20583#define NORMALIZE(atype) \
20584 if ((! isNormalized) && \
20585 (normalize || (type->flags & XML_SCHEMAS_TYPE_NORMVALUENEEDED))) { \
20586 normValue = xmlSchemaNormalizeValue(atype, value); \
20587 if (normValue != NULL) \
20588 value = normValue; \
20589 isNormalized = 1; \
20590 }
20591
20592 if ((retVal != NULL) && (*retVal != NULL)) {
20593 xmlSchemaFreeValue(*retVal);
20594 *retVal = NULL;
20595 }
20596 /*
20597 * 3.14.4 Simple Type Definition Validation Rules
20598 * Validation Rule: String Valid
20599 */
20600 /*
20601 * 1 It is schema-valid with respect to that definition as defined
20602 * by Datatype Valid in [XML Schemas: Datatypes].
20603 */
20604 /*
20605 * 2.1 If The definition is ENTITY or is validly derived from ENTITY given
20606 * the empty set, as defined in Type Derivation OK (Simple) (§3.14.6), then
20607 * the string must be a ·declared entity name·.
20608 */
20609 /*
20610 * 2.2 If The definition is ENTITIES or is validly derived from ENTITIES
20611 * given the empty set, as defined in Type Derivation OK (Simple) (§3.14.6),
20612 * then every whitespace-delimited substring of the string must be a ·declared
20613 * entity name·.
20614 */
20615 /*
20616 * 2.3 otherwise no further condition applies.
20617 */
20618 if ((! valNeeded) && (type->flags & XML_SCHEMAS_TYPE_FACETSNEEDVALUE))
20619 valNeeded = 1;
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000020620 if (value == NULL)
20621 value = BAD_CAST "";
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020622 if (IS_ANY_SIMPLE_TYPE(type) || VARIETY_ATOMIC(type)) {
20623 xmlSchemaTypePtr biType; /* The built-in type. */
20624 /*
20625 * SPEC (1.2.1) "if {variety} is ·atomic· then the string must ·match·
20626 * a literal in the ·lexical space· of {base type definition}"
20627 */
20628 /*
20629 * Whitespace-normalize.
20630 */
20631 NORMALIZE(type);
20632 if (type->type != XML_SCHEMA_TYPE_BASIC) {
20633 /*
20634 * Get the built-in type.
20635 */
20636 biType = type->baseType;
20637 while ((biType != NULL) &&
20638 (biType->type != XML_SCHEMA_TYPE_BASIC))
20639 biType = biType->baseType;
20640
20641 if (biType == NULL) {
20642 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
20643 "could not get the built-in type");
20644 goto internal_error;
20645 }
20646 } else
20647 biType = type;
20648 /*
20649 * NOTATIONs need to be processed here, since they need
20650 * to lookup in the hashtable of NOTATION declarations of the schema.
20651 */
20652 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
20653 switch (biType->builtInType) {
20654 case XML_SCHEMAS_NOTATION:
20655 ret = xmlSchemaValidateNotation(
20656 (xmlSchemaValidCtxtPtr) actxt,
20657 ((xmlSchemaValidCtxtPtr) actxt)->schema,
20658 NULL, value, &val, valNeeded);
20659 break;
20660 case XML_SCHEMAS_QNAME:
20661 ret = xmlSchemaValidateQName((xmlSchemaValidCtxtPtr) actxt,
20662 value, &val, valNeeded);
20663 break;
20664 default:
20665 ws = xmlSchemaGetWhiteSpaceFacetValue(type);
20666 if (valNeeded)
20667 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
20668 value, &val, NULL);
20669 else
20670 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
20671 value, NULL, NULL);
20672 break;
20673 }
20674 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
20675 switch (biType->builtInType) {
20676 case XML_SCHEMAS_NOTATION:
20677 ret = xmlSchemaValidateNotation(NULL,
20678 ((xmlSchemaParserCtxtPtr) actxt)->schema, node,
20679 value, &val, valNeeded);
20680 break;
20681 default:
20682 ws = xmlSchemaGetWhiteSpaceFacetValue(type);
20683 if (valNeeded)
20684 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
20685 value, &val, node);
20686 else
20687 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
20688 value, NULL, node);
20689 break;
20690 }
20691 } else {
20692 /*
20693 * Validation via a public API is not implemented yet.
20694 */
20695 TODO
20696 goto internal_error;
20697 }
20698 if (ret != 0) {
20699 if (ret < 0) {
20700 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
20701 "validating against a built-in type");
20702 goto internal_error;
20703 }
20704 if (VARIETY_LIST(type))
20705 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
20706 else
20707 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
20708 }
20709 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
20710 /*
20711 * Check facets.
20712 */
20713 ret = xmlSchemaValidateFacets(actxt, node, type,
20714 (xmlSchemaValType) biType->builtInType, value, val,
20715 0, fireErrors);
20716 if (ret != 0) {
20717 if (ret < 0) {
20718 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
20719 "validating facets of atomic simple type");
20720 goto internal_error;
20721 }
20722 if (VARIETY_LIST(type))
20723 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
20724 else
20725 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
20726 }
20727 }
20728 if (fireErrors && (ret > 0))
20729 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
20730 } else if (VARIETY_LIST(type)) {
20731
20732 xmlSchemaTypePtr itemType;
20733 const xmlChar *cur, *end;
20734 xmlChar *tmpValue = NULL;
20735 unsigned long len = 0;
20736 xmlSchemaValPtr prevVal = NULL, curVal = NULL;
20737 /* 1.2.2 if {variety} is ·list· then the string must be a sequence
20738 * of white space separated tokens, each of which ·match·es a literal
20739 * in the ·lexical space· of {item type definition}
20740 */
20741 /*
20742 * Note that XML_SCHEMAS_TYPE_NORMVALUENEEDED will be set if
20743 * the list type has an enum or pattern facet.
20744 */
20745 NORMALIZE(type);
20746 /*
20747 * VAL TODO: Optimize validation of empty values.
20748 * VAL TODO: We do not have computed values for lists.
20749 */
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000020750 itemType = GET_LIST_ITEM_TYPE(type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020751 cur = value;
20752 do {
20753 while (IS_BLANK_CH(*cur))
20754 cur++;
20755 end = cur;
20756 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
20757 end++;
20758 if (end == cur)
20759 break;
20760 tmpValue = xmlStrndup(cur, end - cur);
20761 len++;
20762
20763 if (valNeeded)
20764 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
20765 tmpValue, &curVal, fireErrors, 0, 1);
20766 else
20767 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
20768 tmpValue, NULL, fireErrors, 0, 1);
20769 FREE_AND_NULL(tmpValue);
20770 if (curVal != NULL) {
20771 /*
20772 * Add to list of computed values.
20773 */
20774 if (val == NULL)
20775 val = curVal;
20776 else
20777 xmlSchemaValueAppend(prevVal, curVal);
20778 prevVal = curVal;
20779 curVal = NULL;
20780 }
20781 if (ret != 0) {
20782 if (ret < 0) {
20783 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
20784 "validating an item of list simple type");
20785 goto internal_error;
20786 }
20787 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
20788 break;
20789 }
20790 cur = end;
20791 } while (*cur != 0);
20792 FREE_AND_NULL(tmpValue);
20793 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
20794 /*
20795 * Apply facets (pattern, enumeration).
20796 */
20797 ret = xmlSchemaValidateFacets(actxt, node, type,
20798 XML_SCHEMAS_UNKNOWN, value, val,
20799 len, fireErrors);
20800 if (ret != 0) {
20801 if (ret < 0) {
20802 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
20803 "validating facets of list simple type");
20804 goto internal_error;
20805 }
20806 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
20807 }
20808 }
20809 if (fireErrors && (ret > 0)) {
20810 /*
20811 * Report the normalized value.
20812 */
20813 normalize = 1;
20814 NORMALIZE(type);
20815 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
20816 }
20817 } else if (VARIETY_UNION(type)) {
20818 xmlSchemaTypeLinkPtr memberLink;
20819 /*
20820 * TODO: For all datatypes ·derived· by ·union· whiteSpace does
20821 * not apply directly; however, the normalization behavior of ·union·
20822 * types is controlled by the value of whiteSpace on that one of the
20823 * ·memberTypes· against which the ·union· is successfully validated.
20824 *
20825 * This means that the value is normalized by the first validating
20826 * member type, then the facets of the union type are applied. This
20827 * needs changing of the value!
20828 */
20829
20830 /*
20831 * 1.2.3 if {variety} is ·union· then the string must ·match· a
20832 * literal in the ·lexical space· of at least one member of
20833 * {member type definitions}
20834 */
20835 memberLink = xmlSchemaGetUnionSimpleTypeMemberTypes(type);
20836 if (memberLink == NULL) {
20837 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
20838 "union simple type has no member types");
20839 goto internal_error;
20840 }
20841 /*
20842 * Always normalize union type values, since we currently
20843 * cannot store the whitespace information with the value
20844 * itself; otherwise a later value-comparison would be
20845 * not possible.
20846 */
20847 while (memberLink != NULL) {
20848 if (valNeeded)
20849 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
20850 memberLink->type, value, &val, 0, 1, 0);
20851 else
20852 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
20853 memberLink->type, value, NULL, 0, 1, 0);
20854 if (ret <= 0)
20855 break;
20856 memberLink = memberLink->next;
20857 }
20858 if (ret != 0) {
20859 if (ret < 0) {
20860 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
20861 "validating members of union simple type");
20862 goto internal_error;
20863 }
20864 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
20865 }
20866 /*
20867 * Apply facets (pattern, enumeration).
20868 */
20869 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
20870 /*
20871 * The normalization behavior of ·union· types is controlled by
20872 * the value of whiteSpace on that one of the ·memberTypes·
20873 * against which the ·union· is successfully validated.
20874 */
20875 NORMALIZE(memberLink->type);
20876 ret = xmlSchemaValidateFacets(actxt, node, type,
20877 XML_SCHEMAS_UNKNOWN, value, val,
20878 0, fireErrors);
20879 if (ret != 0) {
20880 if (ret < 0) {
20881 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
20882 "validating facets of union simple type");
20883 goto internal_error;
20884 }
20885 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
20886 }
20887 }
20888 if (fireErrors && (ret > 0))
20889 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
20890 }
20891
20892 if (normValue != NULL)
20893 xmlFree(normValue);
20894 if (ret == 0) {
20895 if (retVal != NULL)
20896 *retVal = val;
20897 else if (val != NULL)
20898 xmlSchemaFreeValue(val);
20899 } else if (val != NULL)
20900 xmlSchemaFreeValue(val);
20901 return (ret);
20902internal_error:
20903 if (normValue != NULL)
20904 xmlFree(normValue);
20905 if (val != NULL)
20906 xmlSchemaFreeValue(val);
20907 return (-1);
20908}
20909
20910static int
20911xmlSchemaVExpandQName(xmlSchemaValidCtxtPtr vctxt,
20912 const xmlChar *value,
20913 const xmlChar **nsName,
20914 const xmlChar **localName)
20915{
20916 int ret = 0;
20917
20918 if ((nsName == NULL) || (localName == NULL))
20919 return (-1);
20920 *nsName = NULL;
20921 *localName = NULL;
20922
20923 ret = xmlValidateQName(value, 1);
20924 if (ret == -1)
20925 return (-1);
20926 if (ret > 0) {
20927 xmlSchemaSimpleTypeErr((xmlSchemaAbstractCtxtPtr) vctxt,
20928 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
20929 value, xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), 1);
20930 return (1);
20931 }
20932 {
20933 xmlChar *local = NULL;
20934 xmlChar *prefix;
20935
20936 /*
20937 * NOTE: xmlSplitQName2 will return a duplicated
20938 * string.
20939 */
20940 local = xmlSplitQName2(value, &prefix);
20941 VAL_CREATE_DICT;
20942 if (local == NULL)
20943 *localName = xmlDictLookup(vctxt->dict, value, -1);
20944 else {
20945 *localName = xmlDictLookup(vctxt->dict, local, -1);
20946 xmlFree(local);
20947 }
20948
20949 *nsName = xmlSchemaLookupNamespace(vctxt, prefix);
20950
20951 if (prefix != NULL) {
20952 xmlFree(prefix);
20953 /*
20954 * A namespace must be found if the prefix is NOT NULL.
20955 */
20956 if (*nsName == NULL) {
20957 xmlSchemaCustomErr((xmlSchemaAbstractCtxtPtr) vctxt,
20958 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
20959 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
20960 "The QName value '%s' has no "
20961 "corresponding namespace declaration in scope",
20962 value, NULL);
20963 return (2);
20964 }
20965 }
20966 }
20967 return (0);
20968}
20969
20970static int
20971xmlSchemaProcessXSIType(xmlSchemaValidCtxtPtr vctxt,
20972 xmlSchemaAttrInfoPtr iattr,
20973 xmlSchemaTypePtr *localType,
20974 xmlSchemaElementPtr elemDecl)
20975{
20976 int ret = 0;
20977 /*
20978 * cvc-elt (3.3.4) : (4)
20979 * AND
20980 * Schema-Validity Assessment (Element) (cvc-assess-elt)
20981 * (1.2.1.2.1) - (1.2.1.2.4)
20982 * Handle 'xsi:type'.
20983 */
20984 if (localType == NULL)
20985 return (-1);
20986 *localType = NULL;
20987 if (iattr == NULL)
20988 return (0);
20989 else {
20990 const xmlChar *nsName = NULL, *local = NULL;
20991 /*
20992 * TODO: We should report a *warning* that the type was overriden
20993 * by the instance.
20994 */
20995 ACTIVATE_ATTRIBUTE(iattr);
20996 /*
20997 * (cvc-elt) (3.3.4) : (4.1)
20998 * (cvc-assess-elt) (1.2.1.2.2)
20999 */
21000 ret = xmlSchemaVExpandQName(vctxt, iattr->value,
21001 &nsName, &local);
21002 if (ret != 0) {
21003 if (ret < 0) {
21004 VERROR_INT("xmlSchemaValidateElementByDeclaration",
21005 "calling xmlSchemaQNameExpand() to validate the "
21006 "attribute 'xsi:type'");
21007 goto internal_error;
21008 }
21009 goto exit;
21010 }
21011 /*
21012 * (cvc-elt) (3.3.4) : (4.2)
21013 * (cvc-assess-elt) (1.2.1.2.3)
21014 */
21015 *localType = xmlSchemaGetType(vctxt->schema, local, nsName);
21016 if (*localType == NULL) {
21017 xmlChar *str = NULL;
21018
21019 xmlSchemaCustomErr((xmlSchemaAbstractCtxtPtr) vctxt,
21020 XML_SCHEMAV_CVC_ELT_4_2, NULL,
21021 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
21022 "The QName value '%s' of the xsi:type attribute does not "
21023 "resolve to a type definition",
21024 xmlSchemaFormatQName(&str, nsName, local), NULL);
21025 FREE_AND_NULL(str);
21026 ret = vctxt->err;
21027 goto exit;
21028 }
21029 if (elemDecl != NULL) {
21030 int set = 0;
21031
21032 /*
21033 * SPEC cvc-elt (3.3.4) : (4.3) (Type Derivation OK)
21034 * "The ·local type definition· must be validly
21035 * derived from the {type definition} given the union of
21036 * the {disallowed substitutions} and the {type definition}'s
21037 * {prohibited substitutions}, as defined in
21038 * Type Derivation OK (Complex) (§3.4.6)
21039 * (if it is a complex type definition),
21040 * or given {disallowed substitutions} as defined in Type
21041 * Derivation OK (Simple) (§3.14.6) (if it is a simple type
21042 * definition)."
21043 *
21044 * {disallowed substitutions}: the "block" on the element decl.
21045 * {prohibited substitutions}: the "block" on the type def.
21046 */
21047 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) ||
21048 (elemDecl->subtypes->flags &
21049 XML_SCHEMAS_TYPE_BLOCK_EXTENSION))
21050 set |= SUBSET_EXTENSION;
21051
21052 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) ||
21053 (elemDecl->subtypes->flags &
21054 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION))
21055 set |= SUBSET_RESTRICTION;
21056
21057 if (xmlSchemaCheckCOSDerivedOK(*localType,
21058 elemDecl->subtypes, set) != 0) {
21059 xmlChar *str = NULL;
21060
21061 xmlSchemaCustomErr((xmlSchemaAbstractCtxtPtr) vctxt,
21062 XML_SCHEMAV_CVC_ELT_4_3, NULL, NULL,
21063 "The type definition '%s', specified by xsi:type, is "
21064 "blocked or not validly derived from the type definition "
21065 "of the element declaration",
21066 xmlSchemaFormatQName(&str,
21067 (*localType)->targetNamespace,
21068 (*localType)->name),
21069 NULL);
21070 FREE_AND_NULL(str);
21071 ret = vctxt->err;
21072 *localType = NULL;
21073 }
21074 }
21075 }
21076exit:
21077 ACTIVATE_ELEM;
21078 return (ret);
21079internal_error:
21080 ACTIVATE_ELEM;
21081 return (-1);
21082}
21083
21084static int
21085xmlSchemaValidateElemDecl(xmlSchemaValidCtxtPtr vctxt)
21086{
21087 xmlSchemaElementPtr elemDecl = vctxt->inode->decl;
21088 xmlSchemaTypePtr actualType = ELEM_TYPE(elemDecl);
21089
21090 /*
21091 * cvc-elt (3.3.4) : 1
21092 */
21093 if (elemDecl == NULL) {
21094 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL,
21095 "No matching declaration available");
21096 return (vctxt->err);
21097 }
21098 /*
21099 * cvc-elt (3.3.4) : 2
21100 */
21101 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT) {
21102 VERROR(XML_SCHEMAV_CVC_ELT_2, NULL,
21103 "The element declaration is abstract");
21104 return (vctxt->err);
21105 }
21106 if (actualType == NULL) {
21107 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
21108 "The type definition is absent");
21109 return (XML_SCHEMAV_CVC_TYPE_1);
21110 }
21111 if (vctxt->nbAttrInfos != 0) {
21112 int ret;
21113 xmlSchemaAttrInfoPtr iattr;
21114 /*
21115 * cvc-elt (3.3.4) : 3
21116 * Handle 'xsi:nil'.
21117 */
21118 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
21119 XML_SCHEMA_ATTR_INFO_META_XSI_NIL);
21120 if (iattr) {
21121 ACTIVATE_ATTRIBUTE(iattr);
21122 /*
21123 * Validate the value.
21124 */
21125 ret = xmlSchemaVCheckCVCSimpleType(
21126 (xmlSchemaAbstractCtxtPtr) vctxt, NULL,
21127 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
21128 iattr->value, &(iattr->val), 1, 0, 0);
21129 ACTIVATE_ELEM;
21130 if (ret < 0) {
21131 VERROR_INT("xmlSchemaValidateElemDecl",
21132 "calling xmlSchemaVCheckCVCSimpleType() to "
21133 "validate the attribute 'xsi:nil'");
21134 return (-1);
21135 }
21136 if (ret == 0) {
21137 if ((elemDecl->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) {
21138 /*
21139 * cvc-elt (3.3.4) : 3.1
21140 */
21141 VERROR(XML_SCHEMAV_CVC_ELT_3_1, NULL,
21142 "The element is not 'nillable'");
21143 /* Does not return an error on purpose. */
21144 } else {
21145 if (xmlSchemaValueGetAsBoolean(iattr->val)) {
21146 /*
21147 * cvc-elt (3.3.4) : 3.2.2
21148 */
21149 if ((elemDecl->flags & XML_SCHEMAS_ELEM_FIXED) &&
21150 (elemDecl->value != NULL)) {
21151 VERROR(XML_SCHEMAV_CVC_ELT_3_2_2, NULL,
21152 "The element cannot be 'nilled' because "
21153 "there is a fixed value constraint defined "
21154 "for it");
21155 /* Does not return an error on purpose. */
21156 } else
21157 vctxt->inode->flags |=
21158 XML_SCHEMA_ELEM_INFO_NILLED;
21159 }
21160 }
21161 }
21162 }
21163 /*
21164 * cvc-elt (3.3.4) : 4
21165 * Handle 'xsi:type'.
21166 */
21167 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
21168 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
21169 if (iattr) {
21170 xmlSchemaTypePtr localType = NULL;
21171
21172 ret = xmlSchemaProcessXSIType(vctxt, iattr, &localType,
21173 elemDecl);
21174 if (ret != 0) {
21175 if (ret == -1) {
21176 VERROR_INT("xmlSchemaValidateElemDecl",
21177 "calling xmlSchemaProcessXSIType() to "
21178 "process the attribute 'xsi:type'");
21179 return (-1);
21180 }
21181 /* Does not return an error on purpose. */
21182 }
21183 if (localType != NULL) {
21184 vctxt->inode->flags |= XML_SCHEMA_ELEM_INFO_LOCAL_TYPE;
21185 actualType = localType;
21186 }
21187 }
21188 }
21189 /*
21190 * IDC: Register identity-constraint XPath matchers.
21191 */
21192 if ((elemDecl->idcs != NULL) &&
21193 (xmlSchemaIDCRegisterMatchers(vctxt, elemDecl) == -1))
21194 return (-1);
21195 /*
21196 * No actual type definition.
21197 */
21198 if (actualType == NULL) {
21199 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
21200 "The type definition is absent");
21201 return (XML_SCHEMAV_CVC_TYPE_1);
21202 }
21203 /*
21204 * Remember the actual type definition.
21205 */
21206 vctxt->inode->typeDef = actualType;
21207
21208 return (0);
21209}
21210
21211static int
21212xmlSchemaVAttributesSimple(xmlSchemaValidCtxtPtr vctxt)
21213{
21214 xmlSchemaAttrInfoPtr iattr;
21215 int ret = 0, i;
21216
21217 /*
21218 * SPEC cvc-type (3.1.1)
21219 * "The attributes of must be empty, excepting those whose namespace
21220 * name is identical to http://www.w3.org/2001/XMLSchema-instance and
21221 * whose local name is one of type, nil, schemaLocation or
21222 * noNamespaceSchemaLocation."
21223 */
21224 if (vctxt->nbAttrInfos == 0)
21225 return (0);
21226 for (i = 0; i < vctxt->nbAttrInfos; i++) {
21227 iattr = vctxt->attrInfos[i];
21228 if (! iattr->metaType) {
21229 ACTIVATE_ATTRIBUTE(iattr)
21230 xmlSchemaIllegalAttrErr((xmlSchemaAbstractCtxtPtr) vctxt,
21231 XML_SCHEMAV_CVC_TYPE_3_1_1, iattr, NULL);
21232 ret = XML_SCHEMAV_CVC_TYPE_3_1_1;
21233 }
21234 }
21235 ACTIVATE_ELEM
21236 return (ret);
21237}
21238
21239/*
21240* Cleanup currently used attribute infos.
21241*/
21242static void
21243xmlSchemaClearAttrInfos(xmlSchemaValidCtxtPtr vctxt)
21244{
21245 int i;
21246 xmlSchemaAttrInfoPtr attr;
21247
21248 if (vctxt->nbAttrInfos == 0)
21249 return;
21250 for (i = 0; i < vctxt->nbAttrInfos; i++) {
21251 attr = vctxt->attrInfos[i];
21252 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
21253 if (attr->localName != NULL)
21254 xmlFree((xmlChar *) attr->localName);
21255 if (attr->nsName != NULL)
21256 xmlFree((xmlChar *) attr->nsName);
21257 }
21258 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
21259 if (attr->value != NULL)
21260 xmlFree((xmlChar *) attr->value);
21261 }
21262 if (attr->val != NULL) {
21263 xmlSchemaFreeValue(attr->val);
21264 attr->val = NULL;
21265 }
21266 memset(attr, 0, sizeof(xmlSchemaAttrInfo));
21267 }
21268 vctxt->nbAttrInfos = 0;
21269}
21270
21271/*
21272* 3.4.4 Complex Type Definition Validation Rules
21273* Element Locally Valid (Complex Type) (cvc-complex-type)
21274* 3.2.4 Attribute Declaration Validation Rules
21275* Validation Rule: Attribute Locally Valid (cvc-attribute)
21276* Attribute Locally Valid (Use) (cvc-au)
21277*
21278* Only "assessed" attribute information items will be visible to
21279* IDCs. I.e. not "lax" (without declaration) and "skip" wild attributes.
21280*/
21281static int
21282xmlSchemaVAttributesComplex(xmlSchemaValidCtxtPtr vctxt)
21283{
21284 xmlSchemaTypePtr type = vctxt->inode->typeDef;
21285 xmlSchemaAttributeLinkPtr attrUseLink;
21286 xmlSchemaAttributePtr attrUse = NULL, attrDecl = NULL;
21287 xmlSchemaAttrInfoPtr attr, tmpAttr;
21288 int i, found, nbAttrs;
21289 int xpathRes = 0, res, wildIDs = 0, fixed;
21290
21291 /*
21292 * SPEC (cvc-attribute)
21293 * (1) "The declaration must not be ·absent· (see Missing
21294 * Sub-components (§5.3) for how this can fail to be
21295 * the case)."
21296 * (2) "Its {type definition} must not be absent."
21297 *
21298 * NOTE (1) + (2): This is not handled here, since we currently do not
21299 * allow validation against schemas which have missing sub-components.
21300 *
21301 * SPEC (cvc-complex-type)
21302 * (3) "For each attribute information item in the element information
21303 * item's [attributes] excepting those whose [namespace name] is
21304 * identical to http://www.w3.org/2001/XMLSchema-instance and whose
21305 * [local name] is one of type, nil, schemaLocation or
21306 * noNamespaceSchemaLocation, the appropriate case among the following
21307 * must be true:
21308 *
21309 */
21310 nbAttrs = vctxt->nbAttrInfos;
21311 for (attrUseLink = type->attributeUses; attrUseLink != NULL;
21312 attrUseLink = attrUseLink->next) {
21313
21314 found = 0;
21315 attrUse = attrUseLink->attr;
21316 /*
21317 * VAL TODO: Implement a real "attribute use" component.
21318 */
21319 if (attrUse->refDecl != NULL)
21320 attrDecl = attrUse->refDecl;
21321 else
21322 attrDecl = attrUse;
21323 for (i = 0; i < nbAttrs; i++) {
21324 attr = vctxt->attrInfos[i];
21325 /*
21326 * SPEC (cvc-complex-type) (3)
21327 * Skip meta attributes.
21328 */
21329 if (attr->metaType)
21330 continue;
21331 if (attr->localName[0] != attrDecl->name[0])
21332 continue;
21333 if (!xmlStrEqual(attr->localName, attrDecl->name))
21334 continue;
21335 if (!xmlStrEqual(attr->nsName, attrDecl->targetNamespace))
21336 continue;
21337 found = 1;
21338 /*
21339 * SPEC (cvc-complex-type)
21340 * (3.1) "If there is among the {attribute uses} an attribute
21341 * use with an {attribute declaration} whose {name} matches
21342 * the attribute information item's [local name] and whose
21343 * {target namespace} is identical to the attribute information
21344 * item's [namespace name] (where an ·absent· {target namespace}
21345 * is taken to be identical to a [namespace name] with no value),
21346 * then the attribute information must be ·valid· with respect
21347 * to that attribute use as per Attribute Locally Valid (Use)
21348 * (§3.5.4). In this case the {attribute declaration} of that
21349 * attribute use is the ·context-determined declaration· for the
21350 * attribute information item with respect to Schema-Validity
21351 * Assessment (Attribute) (§3.2.4) and
21352 * Assessment Outcome (Attribute) (§3.2.5).
21353 */
21354 attr->state = XML_SCHEMAS_ATTR_ASSESSED;
21355 attr->use = attrUse;
21356 /*
21357 * Context-determined declaration.
21358 */
21359 attr->decl = attrDecl;
21360 attr->typeDef = attrDecl->subtypes;
21361 break;
21362 }
21363
21364 if (found)
21365 continue;
21366
21367 if (attrUse->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED) {
21368 /*
21369 * Handle non-existent, required attributes.
21370 *
21371 * SPEC (cvc-complex-type)
21372 * (4) "The {attribute declaration} of each attribute use in
21373 * the {attribute uses} whose {required} is true matches one
21374 * of the attribute information items in the element information
21375 * item's [attributes] as per clause 3.1 above."
21376 */
21377 tmpAttr = xmlSchemaGetFreshAttrInfo(vctxt);
21378 if (tmpAttr == NULL) {
21379 VERROR_INT(
21380 "xmlSchemaVAttributesComplex",
21381 "calling xmlSchemaGetFreshAttrInfo()");
21382 return (-1);
21383 }
21384 tmpAttr->state = XML_SCHEMAS_ATTR_ERR_MISSING;
21385 tmpAttr->use = attrUse;
21386 tmpAttr->decl = attrDecl;
21387 } else if ((attrUse->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
21388 ((attrUse->defValue != NULL) ||
21389 (attrDecl->defValue != NULL))) {
21390 /*
21391 * Handle non-existent, optional, default/fixed attributes.
21392 */
21393 tmpAttr = xmlSchemaGetFreshAttrInfo(vctxt);
21394 if (tmpAttr == NULL) {
21395 VERROR_INT(
21396 "xmlSchemaVAttributesComplex",
21397 "calling xmlSchemaGetFreshAttrInfo()");
21398 return (-1);
21399 }
21400 tmpAttr->state = XML_SCHEMAS_ATTR_DEFAULT;
21401 tmpAttr->use = attrUse;
21402 tmpAttr->decl = attrDecl;
21403 tmpAttr->typeDef = attrDecl->subtypes;
21404 tmpAttr->localName = attrDecl->name;
21405 tmpAttr->nsName = attrDecl->targetNamespace;
21406 }
21407 }
21408 if (vctxt->nbAttrInfos == 0)
21409 return (0);
21410 /*
21411 * Validate against the wildcard.
21412 */
21413 if (type->attributeWildcard != NULL) {
21414 /*
21415 * SPEC (cvc-complex-type)
21416 * (3.2.1) "There must be an {attribute wildcard}."
21417 */
21418 for (i = 0; i < nbAttrs; i++) {
21419 attr = vctxt->attrInfos[i];
21420 /*
21421 * SPEC (cvc-complex-type) (3)
21422 * Skip meta attributes.
21423 */
21424 if (attr->state != XML_SCHEMAS_ATTR_UNKNOWN)
21425 continue;
21426 /*
21427 * SPEC (cvc-complex-type)
21428 * (3.2.2) "The attribute information item must be ·valid· with
21429 * respect to it as defined in Item Valid (Wildcard) (§3.10.4)."
21430 *
21431 * SPEC Item Valid (Wildcard) (cvc-wildcard)
21432 * "... its [namespace name] must be ·valid· with respect to
21433 * the wildcard constraint, as defined in Wildcard allows
21434 * Namespace Name (§3.10.4)."
21435 */
21436 if (xmlSchemaCheckCVCWildcardNamespace(type->attributeWildcard,
21437 attr->nsName)) {
21438 /*
21439 * Handle processContents.
21440 *
21441 * SPEC (cvc-wildcard):
21442 * processContents | context-determined declaration:
21443 * "strict" "mustFind"
21444 * "lax" "none"
21445 * "skip" "skip"
21446 */
21447 if (type->attributeWildcard->processContents ==
21448 XML_SCHEMAS_ANY_SKIP) {
21449 /*
21450 * context-determined declaration = "skip"
21451 *
21452 * SPEC PSVI Assessment Outcome (Attribute)
21453 * [validity] = "notKnown"
21454 * [validation attempted] = "none"
21455 */
21456 attr->state = XML_SCHEMAS_ATTR_WILD_SKIP;
21457 continue;
21458 }
21459 /*
21460 * Find an attribute declaration.
21461 */
21462 attr->decl = xmlSchemaGetAttributeDecl(vctxt->schema,
21463 attr->localName, attr->nsName);
21464 if (attr->decl != NULL) {
21465 attr->state = XML_SCHEMAS_ATTR_ASSESSED;
21466 /*
21467 * SPEC (cvc-complex-type)
21468 * (5) "Let [Definition:] the wild IDs be the set of
21469 * all attribute information item to which clause 3.2
21470 * applied and whose ·validation· resulted in a
21471 * ·context-determined declaration· of mustFind or no
21472 * ·context-determined declaration· at all, and whose
21473 * [local name] and [namespace name] resolve (as
21474 * defined by QName resolution (Instance) (§3.15.4)) to
21475 * an attribute declaration whose {type definition} is
21476 * or is derived from ID. Then all of the following
21477 * must be true:"
21478 */
21479 attr->typeDef = attr->decl->subtypes;
21480 if (xmlSchemaIsDerivedFromBuiltInType(
21481 attr->typeDef, XML_SCHEMAS_ID)) {
21482 /*
21483 * SPEC (5.1) "There must be no more than one
21484 * item in ·wild IDs·."
21485 */
21486 if (wildIDs != 0) {
21487 /* VAL TODO */
21488 attr->state = XML_SCHEMAS_ATTR_ERR_WILD_DUPLICATE_ID;
21489 TODO
21490 continue;
21491 }
21492 wildIDs++;
21493 /*
21494 * SPEC (cvc-complex-type)
21495 * (5.2) "If ·wild IDs· is non-empty, there must not
21496 * be any attribute uses among the {attribute uses}
21497 * whose {attribute declaration}'s {type definition}
21498 * is or is derived from ID."
21499 */
21500 for (attrUseLink = type->attributeUses;
21501 attrUseLink != NULL;
21502 attrUseLink = attrUseLink->next) {
21503 if (xmlSchemaIsDerivedFromBuiltInType(
21504 attrUseLink->attr->subtypes,
21505 XML_SCHEMAS_ID)) {
21506 /* VAL TODO */
21507 attr->state = XML_SCHEMAS_ATTR_ERR_WILD_AND_USE_ID;
21508 TODO
21509 }
21510 }
21511 }
21512 } else if (type->attributeWildcard->processContents ==
21513 XML_SCHEMAS_ANY_LAX) {
21514 attr->state = XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL;
21515 /*
21516 * SPEC PSVI Assessment Outcome (Attribute)
21517 * [validity] = "notKnown"
21518 * [validation attempted] = "none"
21519 */
21520 } else {
21521 attr->state = XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL;
21522 }
21523 }
21524 }
21525 }
21526
21527
21528 if (vctxt->nbAttrInfos == 0)
21529 return (0);
21530
21531 /*
21532 * Validate values, create default attributes, evaluate IDCs.
21533 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021534 for (i = 0; i < vctxt->nbAttrInfos; i++) {
21535 attr = vctxt->attrInfos[i];
21536 /*
21537 * VAL TODO: Note that we won't try to resolve IDCs to
21538 * "lax" and "skip" validated attributes. Check what to
21539 * do in this case.
21540 */
21541 if ((attr->state != XML_SCHEMAS_ATTR_ASSESSED) &&
21542 (attr->state != XML_SCHEMAS_ATTR_DEFAULT))
21543 continue;
21544 /*
21545 * VAL TODO: What to do if the type definition is missing?
21546 */
21547 if (attr->typeDef == NULL) {
21548 attr->state = XML_SCHEMAS_ATTR_ERR_NO_TYPE;
21549 continue;
21550 }
21551
21552 ACTIVATE_ATTRIBUTE(attr);
Kasimier T. Buchcik828f6542005-06-09 11:23:39 +000021553 fixed = 0;
21554 xpathRes = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021555
21556 if (vctxt->xpathStates != NULL) {
21557 /*
21558 * Evaluate IDCs.
21559 */
21560 xpathRes = xmlSchemaXPathEvaluate(vctxt,
21561 XML_ATTRIBUTE_NODE);
21562 if (xpathRes == -1) {
21563 VERROR_INT("xmlSchemaVAttributesComplex",
21564 "calling xmlSchemaXPathEvaluate()");
21565 goto internal_error;
21566 }
21567 }
21568
21569 if (attr->state == XML_SCHEMAS_ATTR_DEFAULT) {
21570 /*
21571 * Default/fixed attributes.
21572 */
21573 if (xpathRes) {
21574 if (attr->use->defValue == NULL) {
21575 attr->value = (xmlChar *) attr->use->defValue;
21576 attr->val = attr->use->defVal;
21577 } else {
21578 attr->value = (xmlChar *) attr->decl->defValue;
21579 attr->val = attr->decl->defVal;
21580 }
21581 /*
21582 * IDCs will consume the precomputed default value,
21583 * so we need to clone it.
21584 */
21585 if (attr->val == NULL) {
21586 VERROR_INT("xmlSchemaVAttributesComplex",
21587 "default/fixed value on an attribute use was "
21588 "not precomputed");
21589 goto internal_error;
21590 }
21591 attr->val = xmlSchemaCopyValue(attr->val);
21592 if (attr->val == NULL) {
21593 VERROR_INT("xmlSchemaVAttributesComplex",
21594 "calling xmlSchemaCopyValue()");
21595 goto internal_error;
21596 }
21597 }
21598 /*
21599 * PSVI: Add the default attribute to the current element.
21600 * VAL TODO: Should we use the *normalized* value? This currently
21601 * uses the *initial* value.
21602 */
21603 if ((vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) &&
21604 (attr->node != NULL) && (attr->node->doc != NULL)) {
21605 xmlChar *normValue;
21606 const xmlChar *value;
21607
21608 value = attr->value;
21609 /*
21610 * Normalize the value.
21611 */
21612 normValue = xmlSchemaNormalizeValue(attr->typeDef,
21613 attr->value);
21614 if (normValue != NULL)
21615 value = BAD_CAST normValue;
21616
21617 if (attr->nsName == NULL) {
21618 if (xmlNewProp(attr->node->parent,
21619 attr->localName, value) == NULL) {
21620 VERROR_INT("xmlSchemaVAttributesComplex",
21621 "callling xmlNewProp()");
21622 if (normValue != NULL)
21623 xmlFree(normValue);
21624 goto internal_error;
21625 }
21626 } else {
21627 xmlNsPtr ns;
21628
21629 ns = xmlSearchNsByHref(attr->node->doc,
21630 attr->node->parent, attr->nsName);
21631 if (ns == NULL) {
21632 xmlChar prefix[12];
21633 int counter = 0;
21634
21635 /*
21636 * Create a namespace declaration on the validation
21637 * root node if no namespace declaration is in scope.
21638 */
21639 do {
21640 snprintf((char *) prefix, 12, "p%d", counter++);
21641 ns = xmlSearchNs(attr->node->doc,
21642 attr->node->parent, BAD_CAST prefix);
21643 if (counter > 1000) {
21644 VERROR_INT(
21645 "xmlSchemaVAttributesComplex",
21646 "could not compute a ns prefix for a "
21647 "default/fixed attribute");
21648 if (normValue != NULL)
21649 xmlFree(normValue);
21650 goto internal_error;
21651 }
21652 } while (ns != NULL);
21653 ns = xmlNewNs(vctxt->validationRoot,
21654 attr->nsName, BAD_CAST prefix);
21655 }
21656 xmlNewNsProp(attr->node->parent, ns,
21657 attr->localName, value);
21658 }
21659 if (normValue != NULL)
21660 xmlFree(normValue);
21661 }
21662 /*
21663 * Go directly to IDC evaluation.
21664 */
21665 goto eval_idcs;
21666 }
21667 /*
21668 * Validate the value.
21669 */
21670 if (vctxt->value != NULL) {
21671 /*
21672 * Free last computed value; just for safety reasons.
21673 */
21674 xmlSchemaFreeValue(vctxt->value);
21675 vctxt->value = NULL;
21676 }
21677 /*
21678 * Note that the attribute *use* can be unavailable, if
21679 * the attribute was a wild attribute.
21680 */
21681 if ((attr->decl->flags & XML_SCHEMAS_ATTR_FIXED) ||
21682 ((attr->use != NULL) &&
21683 (attr->use->flags & XML_SCHEMAS_ATTR_FIXED)))
21684 fixed = 1;
21685 else
21686 fixed = 0;
21687 /*
21688 * SPEC (cvc-attribute)
21689 * (3) "The item's ·normalized value· must be locally ·valid·
21690 * with respect to that {type definition} as per
21691 * String Valid (§3.14.4)."
21692 *
21693 * VAL TODO: Do we already have the
21694 * "normalized attribute value" here?
21695 */
21696 if (xpathRes || fixed) {
21697 attr->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
21698 /*
21699 * Request a computed value.
21700 */
21701 res = xmlSchemaVCheckCVCSimpleType(
21702 (xmlSchemaAbstractCtxtPtr) vctxt,
21703 attr->node, attr->typeDef, attr->value, &(attr->val),
21704 1, 1, 0);
21705 } else {
21706 res = xmlSchemaVCheckCVCSimpleType(
21707 (xmlSchemaAbstractCtxtPtr) vctxt,
21708 attr->node, attr->typeDef, attr->value, NULL,
21709 1, 0, 0);
21710 }
21711
21712 if (res != 0) {
21713 if (res == -1) {
21714 VERROR_INT("xmlSchemaVAttributesComplex",
21715 "calling xmlSchemaStreamValidateSimpleTypeValue()");
21716 goto internal_error;
21717 }
21718 attr->state = XML_SCHEMAS_ATTR_INVALID_VALUE;
21719 /*
21720 * SPEC PSVI Assessment Outcome (Attribute)
21721 * [validity] = "invalid"
21722 */
21723 goto eval_idcs;
21724 }
21725
21726 if (fixed) {
21727 int ws;
21728 /*
21729 * SPEC Attribute Locally Valid (Use) (cvc-au)
21730 * "For an attribute information item to be·valid·
21731 * with respect to an attribute use its *normalized*
21732 * value· must match the *canonical* lexical
21733 * representation of the attribute use's {value
21734 * constraint}value, if it is present and fixed."
21735 *
21736 * VAL TODO: The requirement for the *canonical* value
21737 * will be removed in XML Schema 1.1.
21738 */
21739 /*
21740 * SPEC Attribute Locally Valid (cvc-attribute)
21741 * (4) "The item's *actual* value· must match the *value* of
21742 * the {value constraint}, if it is present and fixed."
21743 */
21744 ws = xmlSchemaGetWhiteSpaceFacetValue(attr->typeDef);
21745 if (attr->val == NULL) {
21746 /* VAL TODO: A value was not precomputed. */
21747 TODO
21748 goto eval_idcs;
21749 }
21750 if ((attr->use != NULL) &&
21751 (attr->use->defValue != NULL)) {
21752 if (attr->use->defVal == NULL) {
21753 /* VAL TODO: A default value was not precomputed. */
21754 TODO
21755 goto eval_idcs;
21756 }
21757 attr->vcValue = attr->use->defValue;
21758 /*
21759 if (xmlSchemaCompareValuesWhtsp(attr->val,
21760 (xmlSchemaWhitespaceValueType) ws,
21761 attr->use->defVal,
21762 (xmlSchemaWhitespaceValueType) ws) != 0) {
21763 */
21764 if (! xmlSchemaAreValuesEqual(attr->val, attr->use->defVal))
21765 attr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
21766 } else {
Kasimier T. Buchcik828f6542005-06-09 11:23:39 +000021767 if (attr->decl->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021768 /* VAL TODO: A default value was not precomputed. */
21769 TODO
21770 goto eval_idcs;
21771 }
Kasimier T. Buchcik828f6542005-06-09 11:23:39 +000021772 attr->vcValue = attr->decl->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021773 /*
21774 if (xmlSchemaCompareValuesWhtsp(attr->val,
21775 (xmlSchemaWhitespaceValueType) ws,
21776 attrDecl->defVal,
21777 (xmlSchemaWhitespaceValueType) ws) != 0) {
21778 */
Kasimier T. Buchcik828f6542005-06-09 11:23:39 +000021779 if (! xmlSchemaAreValuesEqual(attr->val, attr->decl->defVal))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021780 attr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
21781 }
21782 /*
21783 * [validity] = "valid"
21784 */
21785 }
21786eval_idcs:
21787 /*
21788 * Evaluate IDCs.
21789 */
21790 if (xpathRes) {
21791 if (xmlSchemaXPathProcessHistory(vctxt,
21792 vctxt->depth +1) == -1) {
21793 VERROR_INT("xmlSchemaVAttributesComplex",
21794 "calling xmlSchemaXPathEvaluate()");
21795 goto internal_error;
21796 }
21797 }
21798 }
21799
21800 /*
21801 * Report errors.
21802 */
21803 for (i = 0; i < vctxt->nbAttrInfos; i++) {
21804 attr = vctxt->attrInfos[i];
21805 if ((attr->state == XML_SCHEMAS_ATTR_META) ||
21806 (attr->state == XML_SCHEMAS_ATTR_ASSESSED) ||
21807 (attr->state == XML_SCHEMAS_ATTR_WILD_SKIP) ||
21808 (attr->state == XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL))
21809 continue;
21810 ACTIVATE_ATTRIBUTE(attr);
21811 switch (attr->state) {
21812 case XML_SCHEMAS_ATTR_ERR_MISSING: {
21813 xmlChar *str = NULL;
21814 ACTIVATE_ELEM;
21815 xmlSchemaCustomErr((xmlSchemaAbstractCtxtPtr) vctxt,
21816 XML_SCHEMAV_CVC_COMPLEX_TYPE_4, NULL, NULL,
21817 "The attribute '%s' is required but missing",
21818 xmlSchemaFormatQName(&str,
21819 attr->decl->targetNamespace,
21820 attr->decl->name),
21821 NULL);
21822 FREE_AND_NULL(str)
21823 break;
21824 }
21825 case XML_SCHEMAS_ATTR_ERR_NO_TYPE:
21826 VERROR(XML_SCHEMAV_CVC_ATTRIBUTE_2, NULL,
21827 "The type definition is absent");
21828 break;
21829 case XML_SCHEMAS_ATTR_ERR_FIXED_VALUE:
21830 xmlSchemaCustomErr((xmlSchemaAbstractCtxtPtr) vctxt,
21831 XML_SCHEMAV_CVC_AU, NULL, NULL,
21832 "The value '%s' does not match the fixed "
21833 "value constraint '%s'",
21834 attr->value, attr->vcValue);
21835 break;
21836 case XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL:
21837 VERROR(XML_SCHEMAV_CVC_WILDCARD, NULL,
21838 "No matching global attribute declaration available, but "
21839 "demanded by the strict wildcard");
21840 break;
21841 case XML_SCHEMAS_ATTR_UNKNOWN:
21842 if (attr->metaType)
21843 break;
21844 /*
21845 * MAYBE VAL TODO: One might report different error messages
21846 * for the following errors.
21847 */
21848 if (type->attributeWildcard == NULL) {
21849 xmlSchemaIllegalAttrErr((xmlSchemaAbstractCtxtPtr) vctxt,
21850 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_1, attr, NULL);
21851 } else {
21852 xmlSchemaIllegalAttrErr((xmlSchemaAbstractCtxtPtr) vctxt,
21853 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_2, attr, NULL);
21854 }
21855 break;
21856 default:
21857 break;
21858 }
21859 }
21860
21861 ACTIVATE_ELEM;
21862 return (0);
21863internal_error:
21864 ACTIVATE_ELEM;
21865 return (-1);
21866}
21867
21868static int
21869xmlSchemaValidateElemWildcard(xmlSchemaValidCtxtPtr vctxt,
21870 int *skip)
21871{
21872 xmlSchemaWildcardPtr wild = (xmlSchemaWildcardPtr) vctxt->inode->decl;
21873 /*
21874 * The namespace of the element was already identified to be
21875 * matching the wildcard.
21876 */
21877 if ((skip == NULL) || (wild == NULL) ||
21878 (wild->type != XML_SCHEMA_TYPE_ANY)) {
21879 VERROR_INT("xmlSchemaValidateElemWildcard",
21880 "bad arguments");
21881 return (-1);
21882 }
21883 *skip = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021884 if (wild->processContents == XML_SCHEMAS_ANY_SKIP) {
21885 /*
21886 * URGENT VAL TODO: Either we need to position the stream to the
21887 * next sibling, or walk the whole subtree.
21888 */
21889 *skip = 1;
21890 return (0);
21891 }
21892 {
21893 xmlSchemaElementPtr decl = NULL;
21894
21895 decl = xmlHashLookup3(vctxt->schema->elemDecl,
21896 vctxt->inode->localName, vctxt->inode->nsName,
21897 NULL);
21898 if (decl != NULL) {
21899 vctxt->inode->decl = decl;
21900 return (0);
21901 }
21902 }
21903 if (wild->processContents == XML_SCHEMAS_ANY_STRICT) {
21904 /* VAL TODO: Change to proper error code. */
21905 VERROR(XML_SCHEMAV_CVC_ELT_1, (xmlSchemaTypePtr) wild,
21906 "No matching global element declaration available, but "
21907 "demanded by the strict wildcard");
21908 return (vctxt->err);
21909 }
21910 if (vctxt->nbAttrInfos != 0) {
21911 xmlSchemaAttrInfoPtr iattr;
21912 /*
21913 * SPEC Validation Rule: Schema-Validity Assessment (Element)
21914 * (1.2.1.2.1) - (1.2.1.2.3 )
21915 *
21916 * Use the xsi:type attribute for the type definition.
21917 */
21918 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
21919 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
21920 if (iattr != NULL) {
21921 if (xmlSchemaProcessXSIType(vctxt, iattr,
21922 &(vctxt->inode->typeDef), NULL) == -1) {
21923 VERROR_INT("xmlSchemaValidateElemWildcard",
21924 "calling xmlSchemaProcessXSIType() to "
21925 "process the attribute 'xsi:nil'");
21926 return (-1);
21927 }
21928 /*
21929 * Don't return an error on purpose.
21930 */
21931 return (0);
21932 }
21933 }
21934 /*
21935 * SPEC Validation Rule: Schema-Validity Assessment (Element)
21936 *
21937 * Fallback to "anyType".
21938 */
21939 vctxt->inode->typeDef =
21940 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
21941 return (0);
21942}
21943
21944/*
21945* xmlSchemaCheckCOSValidDefault:
21946*
21947* This will be called if: not nilled, no content and a default/fixed
21948* value is provided.
21949*/
21950
21951static int
21952xmlSchemaCheckCOSValidDefault(xmlSchemaValidCtxtPtr vctxt,
21953 const xmlChar *value,
21954 xmlSchemaValPtr *val)
21955{
21956 int ret = 0;
21957 xmlSchemaNodeInfoPtr inode = vctxt->inode;
21958
21959 /*
21960 * cos-valid-default:
21961 * Schema Component Constraint: Element Default Valid (Immediate)
21962 * For a string to be a valid default with respect to a type
21963 * definition the appropriate case among the following must be true:
21964 */
21965 if IS_COMPLEX_TYPE(inode->typeDef) {
21966 /*
21967 * Complex type.
21968 *
21969 * SPEC (2.1) "its {content type} must be a simple type definition
21970 * or mixed."
21971 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
21972 * type}'s particle must be ·emptiable· as defined by
21973 * Particle Emptiable (§3.9.6)."
21974 */
21975 if ((! HAS_SIMPLE_CONTENT(inode->typeDef)) &&
21976 ((! HAS_MIXED_CONTENT(inode->typeDef)) ||
21977 (! IS_PARTICLE_EMPTIABLE(inode->typeDef)))) {
21978 ret = XML_SCHEMAP_COS_VALID_DEFAULT_2_1;
21979 /* NOTE that this covers (2.2.2) as well. */
21980 VERROR(ret, NULL,
21981 "For a string to be a valid default, the type definition "
21982 "must be a simple type or a complex type with simple content "
21983 "or mixed content and a particle emptiable");
21984 return(ret);
21985 }
21986 }
21987 /*
21988 * 1 If the type definition is a simple type definition, then the string
21989 * must be ·valid· with respect to that definition as defined by String
21990 * Valid (§3.14.4).
21991 *
21992 * AND
21993 *
21994 * 2.2.1 If the {content type} is a simple type definition, then the
21995 * string must be ·valid· with respect to that simple type definition
21996 * as defined by String Valid (§3.14.4).
21997 */
21998 if (IS_SIMPLE_TYPE(inode->typeDef)) {
21999
22000 ret = xmlSchemaVCheckCVCSimpleType((xmlSchemaAbstractCtxtPtr) vctxt,
22001 NULL, inode->typeDef, value, val, 1, 1, 0);
22002
22003 } else if (HAS_SIMPLE_CONTENT(inode->typeDef)) {
22004
22005 ret = xmlSchemaVCheckCVCSimpleType((xmlSchemaAbstractCtxtPtr) vctxt,
22006 NULL, inode->typeDef->contentTypeDef, value, val, 1, 1, 0);
22007 }
22008 if (ret < 0) {
22009 VERROR_INT("xmlSchemaCheckCOSValidDefault",
22010 "calling xmlSchemaVCheckCVCSimpleType()");
22011 }
22012 return (ret);
22013}
22014
22015static void
22016xmlSchemaVContentModelCallback(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
22017 const xmlChar * name ATTRIBUTE_UNUSED,
22018 xmlSchemaElementPtr item,
22019 xmlSchemaNodeInfoPtr inode)
22020{
22021 inode->decl = item;
22022#ifdef DEBUG_CONTENT
22023 {
22024 xmlChar *str = NULL;
22025
22026 if (item->type == XML_SCHEMA_TYPE_ELEMENT) {
22027 xmlGenericError(xmlGenericErrorContext,
22028 "AUTOMATON callback for '%s' [declaration]\n",
22029 xmlSchemaFormatQName(&str,
22030 inode->localName, inode->nsName));
22031 } else {
22032 xmlGenericError(xmlGenericErrorContext,
22033 "AUTOMATON callback for '%s' [wildcard]\n",
22034 xmlSchemaFormatQName(&str,
22035 inode->localName, inode->nsName));
22036
22037 }
22038 FREE_AND_NULL(str)
22039 }
22040#endif
22041}
22042
22043static int
22044xmlSchemaValidatorPushElem(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000022045{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022046 vctxt->inode = xmlSchemaGetFreshElemInfo(vctxt);
22047 if (vctxt->inode == NULL) {
22048 VERROR_INT("xmlSchemaValidatorPushElem",
22049 "calling xmlSchemaGetFreshElemInfo()");
22050 return (-1);
22051 }
22052 vctxt->nbAttrInfos = 0;
22053 return (0);
22054}
22055
22056static int
22057xmlSchemaVCheckINodeDataType(xmlSchemaValidCtxtPtr vctxt,
22058 xmlSchemaNodeInfoPtr inode,
22059 xmlSchemaTypePtr type,
22060 const xmlChar *value)
22061{
22062 if (inode->flags & XML_SCHEMA_NODE_INFO_VALUE_NEEDED)
22063 return (xmlSchemaVCheckCVCSimpleType(
22064 (xmlSchemaAbstractCtxtPtr) vctxt, NULL,
22065 type, value, &(inode->val), 1, 1, 0));
22066 else
22067 return (xmlSchemaVCheckCVCSimpleType(
22068 (xmlSchemaAbstractCtxtPtr) vctxt, NULL,
22069 type, value, NULL, 1, 0, 0));
22070}
22071
22072
22073
22074/*
22075* Process END of element.
22076*/
22077static int
22078xmlSchemaValidatorPopElem(xmlSchemaValidCtxtPtr vctxt)
22079{
22080 int ret = 0;
22081 xmlSchemaNodeInfoPtr inode = vctxt->inode;
22082
22083 if (vctxt->nbAttrInfos != 0)
22084 xmlSchemaClearAttrInfos(vctxt);
22085 if (inode->flags & XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED) {
22086 /*
22087 * This element was not expected;
22088 * we will not validate child elements of broken parents.
22089 * Skip validation of all content of the parent.
22090 */
22091 vctxt->skipDepth = vctxt->depth -1;
22092 goto end_elem;
22093 }
22094 if ((inode->typeDef == NULL) ||
22095 (inode->flags & XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE)) {
22096 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000022097 * 1. the type definition might be missing if the element was
22098 * error prone
22099 * 2. it might be abstract.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022100 */
22101 goto end_elem;
22102 }
22103 /*
22104 * Check the content model.
22105 */
22106 if ((inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) ||
22107 (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)) {
22108
22109 /*
22110 * Workaround for "anyType".
22111 */
22112 if (inode->typeDef->builtInType == XML_SCHEMAS_ANYTYPE)
22113 goto character_content;
22114
22115 if ((inode->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) == 0) {
22116 xmlChar *values[10];
22117 int terminal, nbval = 10, nbneg;
22118
22119 if (inode->regexCtxt == NULL) {
22120 /*
22121 * Create the regex context.
22122 */
22123 inode->regexCtxt =
22124 xmlRegNewExecCtxt(inode->typeDef->contModel,
22125 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
22126 vctxt);
22127 if (inode->regexCtxt == NULL) {
22128 VERROR_INT("xmlSchemaValidatorPopElem",
22129 "failed to create a regex context");
22130 goto internal_error;
22131 }
22132#ifdef DEBUG_AUTOMATA
22133 xmlGenericError(xmlGenericErrorContext,
22134 "AUTOMATON create on '%s'\n", inode->localName);
22135#endif
22136 }
22137 /*
22138 * Get hold of the still expected content, since a further
22139 * call to xmlRegExecPushString() will loose this information.
22140 */
22141 xmlRegExecNextValues(inode->regexCtxt,
22142 &nbval, &nbneg, &values[0], &terminal);
22143 ret = xmlRegExecPushString(inode->regexCtxt, NULL, NULL);
22144 if (ret <= 0) {
22145 /*
22146 * Still missing something.
22147 */
22148 ret = 1;
22149 inode->flags |=
22150 XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
22151 xmlSchemaComplexTypeErr((xmlSchemaAbstractCtxtPtr) vctxt,
22152 XML_SCHEMAV_ELEMENT_CONTENT, NULL, NULL,
22153 "Missing child element(s)",
22154 nbval, nbneg, values);
22155#ifdef DEBUG_AUTOMATA
22156 xmlGenericError(xmlGenericErrorContext,
22157 "AUTOMATON missing ERROR on '%s'\n",
22158 inode->localName);
22159#endif
22160 } else {
22161 /*
22162 * Content model is satisfied.
22163 */
22164 ret = 0;
22165#ifdef DEBUG_AUTOMATA
22166 xmlGenericError(xmlGenericErrorContext,
22167 "AUTOMATON succeeded on '%s'\n",
22168 inode->localName);
22169#endif
22170 }
22171
22172 }
22173 }
22174 if (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)
22175 goto end_elem;
22176
22177character_content:
22178
22179 if (vctxt->value != NULL) {
22180 xmlSchemaFreeValue(vctxt->value);
22181 vctxt->value = NULL;
22182 }
22183 /*
22184 * Check character content.
22185 */
22186 if (inode->decl == NULL) {
22187 /*
22188 * Speedup if no declaration exists.
22189 */
22190 if (IS_SIMPLE_TYPE(inode->typeDef)) {
22191 ret = xmlSchemaVCheckINodeDataType(vctxt,
22192 inode, inode->typeDef, inode->value);
22193 } else if (HAS_SIMPLE_CONTENT(inode->typeDef)) {
22194 ret = xmlSchemaVCheckINodeDataType(vctxt,
22195 inode, inode->typeDef->contentTypeDef,
22196 inode->value);
22197 }
22198 if (ret < 0) {
22199 VERROR_INT("xmlSchemaValidatorPopElem",
22200 "calling xmlSchemaVCheckCVCSimpleType()");
22201 goto internal_error;
22202 }
22203 goto end_elem;
22204 }
22205 /*
22206 * cvc-elt (3.3.4) : 5
22207 * The appropriate case among the following must be true:
22208 */
22209 /*
22210 * cvc-elt (3.3.4) : 5.1
22211 * If the declaration has a {value constraint},
22212 * the item has neither element nor character [children] and
22213 * clause 3.2 has not applied, then all of the following must be true:
22214 */
22215 if ((inode->decl->value != NULL) &&
22216 (inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY) &&
22217 (! INODE_NILLED(inode))) {
22218 /*
22219 * cvc-elt (3.3.4) : 5.1.1
22220 * If the ·actual type definition· is a ·local type definition·
22221 * then the canonical lexical representation of the {value constraint}
22222 * value must be a valid default for the ·actual type definition· as
22223 * defined in Element Default Valid (Immediate) (§3.3.6).
22224 */
22225 /*
22226 * NOTE: 'local' above means types aquired by xsi:type.
22227 * NOTE: Although the *canonical* value is stated, it is not
22228 * relevant if canonical or not. Additionally XML Schema 1.1
22229 * will removed this requirement as well.
22230 */
22231 if (inode->flags & XML_SCHEMA_ELEM_INFO_LOCAL_TYPE) {
22232
22233 ret = xmlSchemaCheckCOSValidDefault(vctxt,
22234 inode->decl->value, &(inode->val));
22235 if (ret != 0) {
22236 if (ret < 0) {
22237 VERROR_INT("xmlSchemaValidatorPopElem",
22238 "calling xmlSchemaCheckCOSValidDefault()");
22239 goto internal_error;
22240 }
22241 goto end_elem;
22242 }
22243 /*
22244 * Stop here, to avoid redundant validation of the value
22245 * (see following).
22246 */
22247 goto default_psvi;
22248 }
22249 /*
22250 * cvc-elt (3.3.4) : 5.1.2
22251 * The element information item with the canonical lexical
22252 * representation of the {value constraint} value used as its
22253 * ·normalized value· must be ·valid· with respect to the
22254 * ·actual type definition· as defined by Element Locally Valid (Type)
22255 * (§3.3.4).
22256 */
22257 if (IS_SIMPLE_TYPE(inode->typeDef)) {
22258 ret = xmlSchemaVCheckINodeDataType(vctxt,
22259 inode, inode->typeDef, inode->decl->value);
22260 } else if (HAS_SIMPLE_CONTENT(inode->typeDef)) {
22261 ret = xmlSchemaVCheckINodeDataType(vctxt,
22262 inode, inode->typeDef->contentTypeDef,
22263 inode->decl->value);
22264 }
22265 if (ret != 0) {
22266 if (ret < 0) {
22267 VERROR_INT("xmlSchemaValidatorPopElem",
22268 "calling xmlSchemaVCheckCVCSimpleType()");
22269 goto internal_error;
22270 }
22271 goto end_elem;
22272 }
22273
22274default_psvi:
22275 /*
22276 * PSVI: Create a text node on the instance element.
22277 */
22278 if ((vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) &&
22279 (inode->node != NULL)) {
22280 xmlNodePtr textChild;
22281 xmlChar *normValue;
22282 /*
22283 * VAL TODO: Normalize the value.
22284 */
22285 normValue = xmlSchemaNormalizeValue(inode->typeDef,
22286 inode->decl->value);
22287 if (normValue != NULL) {
22288 textChild = xmlNewText(BAD_CAST normValue);
22289 xmlFree(normValue);
22290 } else
22291 textChild = xmlNewText(inode->decl->value);
22292 if (textChild == NULL) {
22293 VERROR_INT("xmlSchemaValidatorPopElem",
22294 "calling xmlNewText()");
22295 goto internal_error;
22296 } else
22297 xmlAddChild(inode->node, textChild);
22298 }
22299
22300 } else if (! INODE_NILLED(inode)) {
22301 /*
22302 * 5.2.1 The element information item must be ·valid· with respect
22303 * to the ·actual type definition· as defined by Element Locally
22304 * Valid (Type) (§3.3.4).
22305 */
22306 if (IS_SIMPLE_TYPE(inode->typeDef)) {
22307 /*
22308 * SPEC (cvc-type) (3.1)
22309 * "If the type definition is a simple type definition, ..."
22310 * (3.1.3) "If clause 3.2 of Element Locally Valid
22311 * (Element) (§3.3.4) did not apply, then the ·normalized value·
22312 * must be ·valid· with respect to the type definition as defined
22313 * by String Valid (§3.14.4).
22314 */
22315 ret = xmlSchemaVCheckINodeDataType(vctxt,
22316 inode, inode->typeDef, inode->value);
22317 } else if (HAS_SIMPLE_CONTENT(inode->typeDef)) {
22318 /*
22319 * SPEC (cvc-type) (3.2) "If the type definition is a complex type
22320 * definition, then the element information item must be
22321 * ·valid· with respect to the type definition as per
22322 * Element Locally Valid (Complex Type) (§3.4.4);"
22323 *
22324 * SPEC (cvc-complex-type) (2.2)
22325 * "If the {content type} is a simple type definition, ...
22326 * the ·normalized value· of the element information item is
22327 * ·valid· with respect to that simple type definition as
22328 * defined by String Valid (§3.14.4)."
22329 */
22330 ret = xmlSchemaVCheckINodeDataType(vctxt,
22331 inode, inode->typeDef->contentTypeDef, inode->value);
22332 }
22333 if (ret != 0) {
22334 if (ret < 0) {
22335 VERROR_INT("xmlSchemaValidatorPopElem",
22336 "calling xmlSchemaVCheckCVCSimpleType()");
22337 goto internal_error;
22338 }
22339 goto end_elem;
22340 }
22341 /*
22342 * 5.2.2 If there is a fixed {value constraint} and clause 3.2 has
22343 * not applied, all of the following must be true:
22344 */
22345 if ((inode->decl->value != NULL) &&
22346 (inode->decl->flags & XML_SCHEMAS_ELEM_FIXED)) {
22347
22348 /*
22349 * TODO: We will need a computed value, when comparison is
22350 * done on computed values.
22351 */
22352 /*
22353 * 5.2.2.1 The element information item must have no element
22354 * information item [children].
22355 */
22356 if (inode->flags &
22357 XML_SCHEMA_ELEM_INFO_HAS_ELEM_CONTENT) {
22358 ret = XML_SCHEMAV_CVC_ELT_5_2_2_1;
22359 VERROR(ret, NULL,
22360 "The content must not containt element nodes since "
22361 "there is a fixed value constraint");
22362 goto end_elem;
22363 } else {
22364 /*
22365 * 5.2.2.2 The appropriate case among the following must
22366 * be true:
22367 */
22368 if (HAS_MIXED_CONTENT(inode->typeDef)) {
22369 /*
22370 * 5.2.2.2.1 If the {content type} of the ·actual type
22371 * definition· is mixed, then the *initial value* of the
22372 * item must match the canonical lexical representation
22373 * of the {value constraint} value.
22374 *
22375 * ... the *initial value* of an element information
22376 * item is the string composed of, in order, the
22377 * [character code] of each character information item in
22378 * the [children] of that element information item.
22379 */
22380 if (! xmlStrEqual(inode->value, inode->decl->value)){
22381 /*
22382 * VAL TODO: Report invalid & expected values as well.
22383 * VAL TODO: Implement the canonical stuff.
22384 */
22385 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_1;
22386 xmlSchemaCustomErr((xmlSchemaAbstractCtxtPtr) vctxt,
22387 ret, NULL, NULL,
22388 "The initial value '%s' does not match the fixed "
22389 "value constraint '%s'",
22390 inode->value, inode->decl->value);
22391 goto end_elem;
22392 }
22393 } else if (HAS_SIMPLE_CONTENT(inode->typeDef)) {
22394 /*
22395 * 5.2.2.2.2 If the {content type} of the ·actual type
22396 * definition· is a simple type definition, then the
22397 * *actual value* of the item must match the canonical
22398 * lexical representation of the {value constraint} value.
22399 */
22400 /*
22401 * VAL TODO: *actual value* is the normalized value, impl.
22402 * this.
22403 * VAL TODO: Report invalid & expected values as well.
22404 * VAL TODO: Implement a comparison with the computed values.
22405 */
22406 if (! xmlStrEqual(inode->value,
22407 inode->decl->value)) {
22408 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_2;
22409 xmlSchemaCustomErr((xmlSchemaAbstractCtxtPtr) vctxt,
22410 ret, NULL, NULL,
22411 "The actual value '%s' does not match the fixed "
22412 "value constraint '%s'",
22413 inode->value,
22414 inode->decl->value);
22415 goto end_elem;
22416 }
22417 }
22418 }
22419 }
22420 }
22421
22422end_elem:
22423 if (vctxt->depth < 0) {
22424 /* TODO: raise error? */
22425 return (0);
22426 }
22427 if (vctxt->depth == vctxt->skipDepth)
22428 vctxt->skipDepth = -1;
22429 /*
22430 * Evaluate the history of XPath state objects.
22431 */
22432 if (xmlSchemaXPathProcessHistory(vctxt, vctxt->depth) == -1)
22433 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022434 /*
22435 * TODO: 6 The element information item must be ·valid· with respect to each of
22436 * the {identity-constraint definitions} as per Identity-constraint
22437 * Satisfied (§3.11.4).
22438 */
22439 /*
22440 * Validate IDC keyrefs.
22441 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022442 if (xmlSchemaCheckCVCIDCKeyRef(vctxt) == -1)
22443 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022444 /*
22445 * Merge/free the IDC table.
22446 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022447 if (inode->idcTable != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022448#ifdef DEBUG_IDC
22449 xmlSchemaDebugDumpIDCTable(stdout,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022450 inode->nsName,
22451 inode->localName,
22452 inode->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022453#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022454 if (vctxt->depth > 0) {
22455 /*
22456 * Merge the IDC node table with the table of the parent node.
22457 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022458 if (xmlSchemaBubbleIDCNodeTables(vctxt) == -1)
22459 goto internal_error;
22460 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022461 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022462 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022463 * Clear the current ielem.
22464 * VAL TODO: Don't free the PSVI IDC tables if they are
22465 * requested for the PSVI.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022466 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022467 xmlSchemaClearElemInfo(inode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022468 /*
22469 * Skip further processing if we are on the validation root.
22470 */
22471 if (vctxt->depth == 0) {
22472 vctxt->depth--;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022473 vctxt->inode = NULL;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022474 return (0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022475 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022476 /*
22477 * Reset the bubbleDepth if needed.
22478 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022479 if (vctxt->aidcs != NULL) {
22480 xmlSchemaIDCAugPtr aidc = vctxt->aidcs;
22481 do {
22482 if (aidc->bubbleDepth == vctxt->depth) {
22483 /*
22484 * A bubbleDepth of a key/unique IDC matches the current
22485 * depth, this means that we are leaving the scope of the
22486 * top-most keyref IDC.
22487 */
22488 aidc->bubbleDepth = -1;
22489 }
22490 aidc = aidc->next;
22491 } while (aidc != NULL);
22492 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022493 vctxt->depth--;
22494 vctxt->inode = vctxt->elemInfos[vctxt->depth];
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022495 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022496 * VAL TODO: 7 If the element information item is the ·validation root·, it must be
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000022497 * ·valid· per Validation Root Valid (ID/IDREF) (§3.3.4).
22498 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022499 return (ret);
22500
22501internal_error:
22502 vctxt->err = -1;
22503 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000022504}
22505
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022506/*
22507* 3.4.4 Complex Type Definition Validation Rules
22508* Validation Rule: Element Locally Valid (Complex Type) (cvc-complex-type)
22509*/
Daniel Veillardc0826a72004-08-10 14:17:33 +000022510static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022511xmlSchemaValidateChildElem(xmlSchemaValidCtxtPtr vctxt)
William M. Brack2f2a6632004-08-20 23:09:47 +000022512{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022513 xmlSchemaNodeInfoPtr pielem;
22514 xmlSchemaTypePtr ptype;
Daniel Veillard01fa6152004-06-29 17:04:39 +000022515 int ret = 0;
Daniel Veillard3646d642004-06-02 19:19:14 +000022516
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022517 if (vctxt->depth <= 0) {
22518 VERROR_INT("xmlSchemaValidateChildElem",
22519 "not intended for the validation root");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000022520 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022521 }
22522 pielem = vctxt->elemInfos[vctxt->depth -1];
22523 if (pielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
22524 pielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000022525 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022526 * Handle 'nilled' elements.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000022527 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022528 if (INODE_NILLED(pielem)) {
22529 /*
22530 * SPEC (cvc-elt) (3.3.4) : (3.2.1)
22531 */
22532 ACTIVATE_PARENT_ELEM;
22533 ret = XML_SCHEMAV_CVC_ELT_3_2_1;
22534 VERROR(ret, NULL,
22535 "Neither character nor element content is allowed, "
22536 "because the element was 'nilled'");
22537 ACTIVATE_ELEM;
22538 goto unexpected_elem;
22539 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000022540
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022541 ptype = pielem->typeDef;
22542
22543 if (ptype->builtInType == XML_SCHEMAS_ANYTYPE) {
22544 /*
22545 * Workaround for "anyType": we have currently no content model
22546 * assigned for "anyType", so handle it explicitely.
22547 * "anyType" has an unbounded, lax "any" wildcard.
22548 */
22549 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
22550 vctxt->inode->localName,
22551 vctxt->inode->nsName);
22552
22553 if (vctxt->inode->decl == NULL) {
22554 xmlSchemaAttrInfoPtr iattr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000022555 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022556 * Process "xsi:type".
22557 * SPEC (cvc-assess-elt) (1.2.1.2.1) - (1.2.1.2.3)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000022558 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022559 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
22560 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
22561 if (iattr != NULL) {
22562 ret = xmlSchemaProcessXSIType(vctxt, iattr,
22563 &(vctxt->inode->typeDef), NULL);
22564 if (ret != 0) {
22565 if (ret == -1) {
22566 VERROR_INT("xmlSchemaValidateChildElem",
22567 "calling xmlSchemaProcessXSIType() to "
22568 "process the attribute 'xsi:nil'");
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022569 return (-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000022570 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022571 return (ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +000022572 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022573 } else {
22574 /*
22575 * Fallback to "anyType".
22576 *
22577 * SPEC (cvc-assess-elt)
22578 * "If the item cannot be ·strictly assessed·, [...]
22579 * an element information item's schema validity may be laxly
22580 * assessed if its ·context-determined declaration· is not
22581 * skip by ·validating· with respect to the ·ur-type
22582 * definition· as per Element Locally Valid (Type) (§3.3.4)."
22583 */
22584 vctxt->inode->typeDef =
22585 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000022586 }
22587 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022588 return (0);
22589 }
22590
22591 switch (ptype->contentType) {
22592 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000022593 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022594 * SPEC (2.1) "If the {content type} is empty, then the
22595 * element information item has no character or element
22596 * information item [children]."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000022597 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022598 ACTIVATE_PARENT_ELEM
22599 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1;
22600 VERROR(ret, NULL,
22601 "Element content is not allowed, "
22602 "because the content type is empty");
22603 ACTIVATE_ELEM
22604 goto unexpected_elem;
22605 break;
22606
22607 case XML_SCHEMA_CONTENT_MIXED:
22608 case XML_SCHEMA_CONTENT_ELEMENTS: {
22609 xmlRegExecCtxtPtr regexCtxt;
22610 xmlChar *values[10];
22611 int terminal, nbval = 10, nbneg;
22612
22613 /* VAL TODO: Optimized "anyType" validation.*/
22614
22615 if (ptype->contModel == NULL) {
22616 VERROR_INT("xmlSchemaValidateChildElem",
22617 "type has elem content but no content model");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000022618 return (-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000022619 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022620 /*
22621 * Safety belf for evaluation if the cont. model was already
22622 * examined to be invalid.
22623 */
22624 if (pielem->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) {
22625 VERROR_INT("xmlSchemaValidateChildElem",
22626 "validating elem, but elem content is already invalid");
22627 return (-1);
22628 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +000022629
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022630 regexCtxt = pielem->regexCtxt;
22631 if (regexCtxt == NULL) {
22632 /*
22633 * Create the regex context.
22634 */
22635 regexCtxt = xmlRegNewExecCtxt(ptype->contModel,
22636 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
22637 vctxt);
22638 if (regexCtxt == NULL) {
22639 VERROR_INT("xmlSchemaValidateChildElem",
22640 "failed to create a regex context");
22641 return (-1);
22642 }
22643 pielem->regexCtxt = regexCtxt;
22644#ifdef DEBUG_AUTOMATA
22645 xmlGenericError(xmlGenericErrorContext, "AUTOMATA create on '%s'\n",
22646 pielem->localName);
22647#endif
22648 }
22649
22650 /*
22651 * SPEC (2.4) "If the {content type} is element-only or mixed,
22652 * then the sequence of the element information item's
22653 * element information item [children], if any, taken in
22654 * order, is ·valid· with respect to the {content type}'s
22655 * particle, as defined in Element Sequence Locally Valid
22656 * (Particle) (§3.9.4)."
22657 */
22658 ret = xmlRegExecPushString2(regexCtxt,
22659 vctxt->inode->localName,
22660 vctxt->inode->nsName,
22661 vctxt->inode);
22662#ifdef DEBUG_AUTOMATA
22663 if (ret < 0)
22664 xmlGenericError(xmlGenericErrorContext,
22665 "AUTOMATON push ERROR for '%s' on '%s'\n",
22666 vctxt->inode->localName, pielem->localName);
22667 else
22668 xmlGenericError(xmlGenericErrorContext,
22669 "AUTOMATON push OK for '%s' on '%s'\n",
22670 vctxt->inode->localName, pielem->localName);
22671#endif
22672 if (vctxt->err == XML_SCHEMAV_INTERNAL) {
22673 VERROR_INT("xmlSchemaValidateChildElem",
22674 "calling xmlRegExecPushString2()");
22675 return (-1);
22676 }
22677 if (ret < 0) {
22678 xmlRegExecErrInfo(regexCtxt, NULL, &nbval, &nbneg,
22679 &values[0], &terminal);
22680 xmlSchemaComplexTypeErr((xmlSchemaAbstractCtxtPtr) vctxt,
22681 XML_SCHEMAV_ELEMENT_CONTENT, NULL,NULL,
22682 "This element is not expected",
22683 nbval, nbneg, values);
22684 ret = vctxt->err;
22685 goto unexpected_elem;
22686 } else
22687 ret = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000022688 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000022689 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022690 case XML_SCHEMA_CONTENT_SIMPLE:
22691 case XML_SCHEMA_CONTENT_BASIC:
22692 ACTIVATE_PARENT_ELEM
22693 if (IS_COMPLEX_TYPE(ptype)) {
22694 /*
22695 * SPEC (cvc-complex-type) (2.2)
22696 * "If the {content type} is a simple type definition, then
22697 * the element information item has no element information
22698 * item [children], ..."
22699 */
22700 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_2;
22701 VERROR(ret, NULL, "Element content is not allowed, "
22702 "because the content type is a simple type definition");
22703 } else {
22704 /*
22705 * SPEC (cvc-type) (3.1.2) "The element information item must
22706 * have no element information item [children]."
22707 */
22708 ret = XML_SCHEMAV_CVC_TYPE_3_1_2;
22709 VERROR(ret, NULL, "Element content is not allowed, "
22710 "because the type definition is simple");
22711 }
22712 ACTIVATE_ELEM
22713 ret = vctxt->err;
22714 goto unexpected_elem;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000022715 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022716
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000022717 default:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000022718 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022719 }
22720 return (ret);
22721unexpected_elem:
22722 /*
22723 * Pop this element and set the skipDepth to skip
22724 * all further content of the parent element.
22725 */
22726 vctxt->skipDepth = vctxt->depth;
22727 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED;
22728 pielem->flags |= XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
22729 return (ret);
22730}
22731
22732#define XML_SCHEMA_PUSH_TEXT_PERSIST 1
22733#define XML_SCHEMA_PUSH_TEXT_CREATED 2
22734#define XML_SCHEMA_PUSH_TEXT_VOLATILE 3
22735
22736static int
22737xmlSchemaVPushText(xmlSchemaValidCtxtPtr vctxt,
22738 int nodeType, const xmlChar *value, int len,
22739 int mode, int *consumed)
22740{
22741 /*
22742 * Unfortunately we have to duplicate the text sometimes.
22743 * OPTIMIZE: Maybe we could skip it, if:
22744 * 1. content type is simple
22745 * 2. whitespace is "collapse"
22746 * 3. it consists of whitespace only
22747 *
22748 * Process character content.
22749 */
22750 if (consumed != NULL)
22751 *consumed = 0;
22752 if (INODE_NILLED(vctxt->inode)) {
22753 /*
22754 * SPEC cvc-elt (3.3.4 - 3.2.1)
22755 * "The element information item must have no character or
22756 * element information item [children]."
22757 */
22758 VERROR(XML_SCHEMAV_CVC_ELT_3_2_1, NULL,
22759 "Neither character nor element content is allowed "
22760 "because the element is 'nilled'");
22761 return (vctxt->err);
22762 }
22763 /*
22764 * SPEC (2.1) "If the {content type} is empty, then the
22765 * element information item has no character or element
22766 * information item [children]."
22767 */
22768 if (vctxt->inode->typeDef->contentType ==
22769 XML_SCHEMA_CONTENT_EMPTY) {
22770 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1, NULL,
22771 "Character content is not allowed, "
22772 "because the content type is empty");
22773 return (vctxt->err);
22774 }
22775
22776 if (vctxt->inode->typeDef->contentType ==
22777 XML_SCHEMA_CONTENT_ELEMENTS) {
22778 if ((nodeType != XML_TEXT_NODE) ||
22779 (! xmlSchemaIsBlank((xmlChar *) value, len))) {
22780 /*
22781 * SPEC cvc-complex-type (2.3)
22782 * "If the {content type} is element-only, then the
22783 * element information item has no character information
22784 * item [children] other than those whose [character
22785 * code] is defined as a white space in [XML 1.0 (Second
22786 * Edition)]."
22787 */
22788 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_3, NULL,
22789 "Character content other than whitespace is not allowed "
22790 "because the content type is 'element-only'");
22791 return (vctxt->err);
22792 }
22793 return (0);
22794 }
22795
22796 if ((value == NULL) || (value[0] == 0))
22797 return (0);
22798 /*
22799 * Save the value.
22800 * NOTE that even if the content type is *mixed*, we need the
22801 * *initial value* for default/fixed value constraints.
22802 */
22803 if ((vctxt->inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) &&
22804 ((vctxt->inode->decl == NULL) ||
22805 (vctxt->inode->decl->value == NULL)))
22806 return (0);
22807
22808 if (vctxt->inode->value == NULL) {
22809 /*
22810 * Set the value.
22811 */
22812 switch (mode) {
22813 case XML_SCHEMA_PUSH_TEXT_PERSIST:
22814 /*
22815 * When working on a tree.
22816 */
22817 vctxt->inode->value = value;
22818 break;
22819 case XML_SCHEMA_PUSH_TEXT_CREATED:
22820 /*
22821 * When working with the reader.
22822 * The value will be freed by the element info.
22823 */
22824 vctxt->inode->value = value;
22825 if (consumed != NULL)
22826 *consumed = 1;
22827 vctxt->inode->flags |=
22828 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
22829 break;
22830 case XML_SCHEMA_PUSH_TEXT_VOLATILE:
22831 /*
22832 * When working with SAX.
22833 * The value will be freed by the element info.
22834 */
22835 if (len != -1)
22836 vctxt->inode->value = BAD_CAST xmlStrndup(value, len);
22837 else
22838 vctxt->inode->value = BAD_CAST xmlStrdup(value);
22839 vctxt->inode->flags |=
22840 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
22841 break;
22842 default:
22843 break;
22844 }
22845 } else {
22846 /*
22847 * Concat the value.
22848 */
22849 if (vctxt->inode->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000022850 vctxt->inode->value = BAD_CAST xmlStrncat(
22851 (xmlChar *) vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022852 } else {
22853 vctxt->inode->value =
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000022854 BAD_CAST xmlStrncatNew(vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022855 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
22856 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000022857 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022858
22859 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000022860}
22861
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000022862static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022863xmlSchemaValidateElem(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000022864{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000022865 int ret = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000022866
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022867 if ((vctxt->skipDepth != -1) &&
22868 (vctxt->depth >= vctxt->skipDepth)) {
22869 VERROR_INT("xmlSchemaValidateElem",
22870 "in skip-state");
22871 goto internal_error;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000022872 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022873 if (vctxt->xsiAssemble) {
22874 if (xmlSchemaAssembleByXSI(vctxt) == -1)
22875 goto internal_error;
22876 }
22877 if (vctxt->depth > 0) {
22878 /*
22879 * Validate this element against the content model
22880 * of the parent.
22881 */
22882 ret = xmlSchemaValidateChildElem(vctxt);
22883 if (ret != 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000022884 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022885 VERROR_INT("xmlSchemaValidateElem",
22886 "calling xmlSchemaStreamValidateChildElement()");
22887 goto internal_error;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000022888 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022889 goto exit;
22890 }
22891 if (vctxt->depth == vctxt->skipDepth)
22892 goto exit;
22893 if ((vctxt->inode->decl == NULL) &&
22894 (vctxt->inode->typeDef == NULL)) {
22895 VERROR_INT("xmlSchemaValidateElem",
22896 "the child element was valid but neither the "
22897 "declaration nor the type was set");
22898 goto internal_error;
22899 }
22900 } else {
22901 /*
22902 * Get the declaration of the validation root.
22903 */
22904 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
22905 vctxt->inode->localName,
22906 vctxt->inode->nsName);
22907 if (vctxt->inode->decl == NULL) {
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000022908 ret = XML_SCHEMAV_CVC_ELT_1;
22909 VERROR(ret, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022910 "No matching global declaration available "
22911 "for the validation root");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022912 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000022913 }
22914 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000022915
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022916 if (vctxt->inode->decl == NULL)
22917 goto type_validation;
22918
22919 if (vctxt->inode->decl->type == XML_SCHEMA_TYPE_ANY) {
22920 int skip;
22921 /*
22922 * Wildcards.
22923 */
22924 ret = xmlSchemaValidateElemWildcard(vctxt, &skip);
22925 if (ret != 0) {
22926 if (ret < 0) {
22927 VERROR_INT("xmlSchemaValidateElem",
22928 "calling xmlSchemaValidateElemWildcard()");
22929 goto internal_error;
22930 }
22931 goto exit;
22932 }
22933 if (skip) {
22934 vctxt->skipDepth = vctxt->depth;
22935 goto exit;
22936 }
22937 /*
22938 * The declaration might be set by the wildcard validation,
22939 * when the processContents is "lax" or "strict".
22940 */
22941 if (vctxt->inode->decl->type != XML_SCHEMA_TYPE_ELEMENT) {
22942 /*
22943 * Clear the "decl" field to not confuse further processing.
22944 */
22945 vctxt->inode->decl = NULL;
22946 goto type_validation;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000022947 }
Daniel Veillard4255d502002-04-16 15:50:10 +000022948 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022949 /*
22950 * Validate against the declaration.
22951 */
22952 ret = xmlSchemaValidateElemDecl(vctxt);
22953 if (ret != 0) {
22954 if (ret < 0) {
22955 VERROR_INT("xmlSchemaValidateElem",
22956 "calling xmlSchemaValidateElemDecl()");
22957 goto internal_error;
22958 }
22959 goto exit;
22960 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000022961 /*
22962 * Validate against the type definition.
22963 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022964type_validation:
22965
22966 if (vctxt->inode->typeDef == NULL) {
22967 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
22968 ret = XML_SCHEMAV_CVC_TYPE_1;
22969 VERROR(ret, NULL,
22970 "The type definition is absent");
22971 goto exit;
22972 }
22973 if (vctxt->inode->typeDef->flags & XML_SCHEMAS_TYPE_ABSTRACT) {
22974 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
22975 ret = XML_SCHEMAV_CVC_TYPE_2;
22976 VERROR(ret, NULL,
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000022977 "The type definition is abstract");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022978 goto exit;
22979 }
22980 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000022981 * Evaluate IDCs. Do it here, since new IDC matchers are registered
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022982 * during validation against the declaration. This must be done
22983 * _before_ attribute validation.
22984 */
22985 ret = xmlSchemaXPathEvaluate(vctxt, XML_ELEMENT_NODE);
22986 if (ret == -1) {
22987 VERROR_INT("xmlSchemaValidateElem",
22988 "calling xmlSchemaXPathEvaluate()");
22989 goto internal_error;
22990 }
22991 /*
22992 * Validate attributes.
22993 */
22994 if (IS_COMPLEX_TYPE(vctxt->inode->typeDef)) {
22995 if ((vctxt->nbAttrInfos != 0) ||
22996 (vctxt->inode->typeDef->attributeUses != NULL)) {
22997
22998 ret = xmlSchemaVAttributesComplex(vctxt);
22999 }
23000 } else if (vctxt->nbAttrInfos != 0) {
23001
23002 ret = xmlSchemaVAttributesSimple(vctxt);
23003 }
23004 /*
23005 * Clear registered attributes.
23006 */
23007 if (vctxt->nbAttrInfos != 0)
23008 xmlSchemaClearAttrInfos(vctxt);
23009 if (ret == -1) {
23010 VERROR_INT("xmlSchemaValidateElem",
23011 "calling attributes validation");
23012 goto internal_error;
23013 }
23014 /*
23015 * Don't return an error if attributes are invalid on purpose.
23016 */
23017 ret = 0;
23018
23019exit:
23020 if (ret != 0)
23021 vctxt->skipDepth = vctxt->depth;
23022 return (ret);
23023internal_error:
23024 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000023025}
23026
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023027#ifdef XML_SCHEMA_READER_ENABLED
23028static int
23029xmlSchemaVReaderWalk(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000023030{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023031 const int WHTSP = 13, SIGN_WHTSP = 14, END_ELEM = 15;
23032 int depth, nodeType, ret = 0, consumed;
23033 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000023034
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023035 vctxt->depth = -1;
23036 ret = xmlTextReaderRead(vctxt->reader);
23037 /*
23038 * Move to the document element.
23039 */
23040 while (ret == 1) {
23041 nodeType = xmlTextReaderNodeType(vctxt->reader);
23042 if (nodeType == XML_ELEMENT_NODE)
23043 goto root_found;
23044 ret = xmlTextReaderRead(vctxt->reader);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000023045 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023046 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000023047
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023048root_found:
23049
23050 do {
23051 depth = xmlTextReaderDepth(vctxt->reader);
23052 nodeType = xmlTextReaderNodeType(vctxt->reader);
23053
23054 if (nodeType == XML_ELEMENT_NODE) {
23055
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000023056 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023057 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
23058 VERROR_INT("xmlSchemaVReaderWalk",
23059 "calling xmlSchemaValidatorPushElem()");
23060 goto internal_error;
23061 }
23062 ielem = vctxt->inode;
23063 ielem->localName = xmlTextReaderLocalName(vctxt->reader);
23064 ielem->nsName = xmlTextReaderNamespaceUri(vctxt->reader);
23065 ielem->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
23066 /*
23067 * Is the element empty?
23068 */
23069 ret = xmlTextReaderIsEmptyElement(vctxt->reader);
23070 if (ret == -1) {
23071 VERROR_INT("xmlSchemaVReaderWalk",
23072 "calling xmlTextReaderIsEmptyElement()");
23073 goto internal_error;
23074 }
23075 if (ret) {
23076 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
23077 }
23078 /*
23079 * Register attributes.
23080 */
23081 vctxt->nbAttrInfos = 0;
23082 ret = xmlTextReaderMoveToFirstAttribute(vctxt->reader);
23083 if (ret == -1) {
23084 VERROR_INT("xmlSchemaVReaderWalk",
23085 "calling xmlTextReaderMoveToFirstAttribute()");
23086 goto internal_error;
23087 }
23088 if (ret == 1) {
23089 do {
23090 /*
23091 * VAL TODO: How do we know that the reader works on a
23092 * node tree, to be able to pass a node here?
23093 */
23094 if (xmlSchemaValidatorPushAttribute(vctxt, NULL,
23095 (const xmlChar *) xmlTextReaderLocalName(vctxt->reader),
23096 xmlTextReaderNamespaceUri(vctxt->reader), 1,
23097 xmlTextReaderValue(vctxt->reader), 1) == -1) {
23098
23099 VERROR_INT("xmlSchemaVReaderWalk",
23100 "calling xmlSchemaValidatorPushAttribute()");
23101 goto internal_error;
23102 }
23103 ret = xmlTextReaderMoveToNextAttribute(vctxt->reader);
23104 if (ret == -1) {
23105 VERROR_INT("xmlSchemaVReaderWalk",
23106 "calling xmlTextReaderMoveToFirstAttribute()");
23107 goto internal_error;
23108 }
23109 } while (ret == 1);
23110 /*
23111 * Back to element position.
23112 */
23113 ret = xmlTextReaderMoveToElement(vctxt->reader);
23114 if (ret == -1) {
23115 VERROR_INT("xmlSchemaVReaderWalk",
23116 "calling xmlTextReaderMoveToElement()");
23117 goto internal_error;
23118 }
23119 }
23120 /*
23121 * Validate the element.
23122 */
23123 ret= xmlSchemaValidateElem(vctxt);
23124 if (ret != 0) {
23125 if (ret == -1) {
23126 VERROR_INT("xmlSchemaVReaderWalk",
23127 "calling xmlSchemaValidateElem()");
23128 goto internal_error;
23129 }
23130 goto exit;
23131 }
23132 if (vctxt->depth == vctxt->skipDepth) {
23133 int curDepth;
23134 /*
23135 * Skip all content.
23136 */
23137 if ((ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY) == 0) {
23138 ret = xmlTextReaderRead(vctxt->reader);
23139 curDepth = xmlTextReaderDepth(vctxt->reader);
23140 while ((ret == 1) && (curDepth != depth)) {
23141 ret = xmlTextReaderRead(vctxt->reader);
23142 curDepth = xmlTextReaderDepth(vctxt->reader);
23143 }
23144 if (ret < 0) {
23145 /*
23146 * VAL TODO: A reader error occured; what to do here?
23147 */
23148 ret = 1;
23149 goto exit;
23150 }
23151 }
23152 goto leave_elem;
23153 }
23154 /*
23155 * READER VAL TODO: Is an END_ELEM really never called
23156 * if the elem is empty?
23157 */
23158 if (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
23159 goto leave_elem;
23160 } else if (nodeType == END_ELEM) {
23161 /*
23162 * Process END of element.
23163 */
23164leave_elem:
23165 ret = xmlSchemaValidatorPopElem(vctxt);
23166 if (ret != 0) {
23167 if (ret < 0) {
23168 VERROR_INT("xmlSchemaVReaderWalk",
23169 "calling xmlSchemaValidatorPopElem()");
23170 goto internal_error;
23171 }
23172 goto exit;
23173 }
23174 if (vctxt->depth >= 0)
23175 ielem = vctxt->inode;
23176 else
23177 ielem = NULL;
23178 } else if ((nodeType == XML_TEXT_NODE) ||
23179 (nodeType == XML_CDATA_SECTION_NODE) ||
23180 (nodeType == WHTSP) ||
23181 (nodeType == SIGN_WHTSP)) {
23182 /*
23183 * Process character content.
23184 */
23185 xmlChar *value;
23186
23187 if ((nodeType == WHTSP) || (nodeType == SIGN_WHTSP))
23188 nodeType = XML_TEXT_NODE;
23189
23190 value = xmlTextReaderValue(vctxt->reader);
23191 ret = xmlSchemaVPushText(vctxt, nodeType, BAD_CAST value,
23192 -1, XML_SCHEMA_PUSH_TEXT_CREATED, &consumed);
23193 if (! consumed)
23194 xmlFree(value);
23195 if (ret == -1) {
23196 VERROR_INT("xmlSchemaVReaderWalk",
23197 "calling xmlSchemaVPushText()");
23198 goto internal_error;
23199 }
23200 } else if ((nodeType == XML_ENTITY_NODE) ||
23201 (nodeType == XML_ENTITY_REF_NODE)) {
23202 /*
23203 * VAL TODO: What to do with entities?
23204 */
23205 TODO
23206 }
23207 /*
23208 * Read next node.
23209 */
23210 ret = xmlTextReaderRead(vctxt->reader);
23211 } while (ret == 1);
23212
23213exit:
23214 return (ret);
23215internal_error:
23216 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000023217}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023218#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000023219
23220/************************************************************************
23221 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023222 * SAX validation handlers *
Daniel Veillard4255d502002-04-16 15:50:10 +000023223 * *
23224 ************************************************************************/
23225
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023226#ifdef XML_SCHEMA_SAX_ENABLED
23227/*
23228* Process text content.
23229*/
23230static void
23231xmlSchemaSAXHandleText(void *ctx,
23232 const xmlChar * ch,
23233 int len)
23234{
23235 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
23236
23237 if (vctxt->depth < 0)
23238 return;
23239 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
23240 return;
23241 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
23242 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
23243 if (xmlSchemaVPushText(vctxt, XML_TEXT_NODE, ch, len,
23244 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
23245 VERROR_INT("xmlSchemaSAXHandleCDataSection",
23246 "calling xmlSchemaVPushText()");
23247 vctxt->err = -1;
23248 xmlStopParser(vctxt->parserCtxt);
23249 }
23250}
23251
23252/*
23253* Process CDATA content.
23254*/
23255static void
23256xmlSchemaSAXHandleCDataSection(void *ctx,
23257 const xmlChar * ch,
23258 int len)
23259{
23260 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
23261
23262 if (vctxt->depth < 0)
23263 return;
23264 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
23265 return;
23266 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
23267 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
23268 if (xmlSchemaVPushText(vctxt, XML_CDATA_SECTION_NODE, ch, len,
23269 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
23270 VERROR_INT("xmlSchemaSAXHandleCDataSection",
23271 "calling xmlSchemaVPushText()");
23272 vctxt->err = -1;
23273 xmlStopParser(vctxt->parserCtxt);
23274 }
23275}
23276
23277static void
23278xmlSchemaSAXHandleReference(void *ctx ATTRIBUTE_UNUSED,
23279 const xmlChar * name ATTRIBUTE_UNUSED)
23280{
23281 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
23282
23283 if (vctxt->depth < 0)
23284 return;
23285 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
23286 return;
23287 /* SAX VAL TODO: What to do here? */
23288 TODO
23289}
23290
23291static void
23292xmlSchemaSAXHandleStartElementNs(void *ctx,
23293 const xmlChar * localname,
23294 const xmlChar * prefix ATTRIBUTE_UNUSED,
23295 const xmlChar * URI,
23296 int nb_namespaces,
23297 const xmlChar ** namespaces,
23298 int nb_attributes,
23299 int nb_defaulted ATTRIBUTE_UNUSED,
23300 const xmlChar ** attributes)
23301{
23302 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
23303 int ret;
23304 xmlSchemaNodeInfoPtr ielem;
23305 int i, j;
23306
23307 /*
23308 * SAX VAL TODO: What to do with nb_defaulted?
23309 */
23310 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000023311 * Skip elements if inside a "skip" wildcard or invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023312 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000023313 vctxt->depth++;
23314 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023315 return;
23316 /*
23317 * Push the element.
23318 */
23319 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
23320 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
23321 "calling xmlSchemaValidatorPushElem()");
23322 goto internal_error;
23323 }
23324 ielem = vctxt->inode;
23325 ielem->localName = localname;
23326 ielem->nsName = URI;
23327 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
23328 /*
23329 * Register namespaces on the elem info.
23330 */
23331 if (nb_namespaces != 0) {
23332 /*
23333 * Although the parser builds its own namespace list,
23334 * we have no access to it, so we'll use an own one.
23335 */
23336 for (i = 0, j = 0; i < nb_namespaces; i++, j += 2) {
23337 /*
23338 * Store prefix and namespace name.
23339 */
23340 if (ielem->nsBindings == NULL) {
23341 ielem->nsBindings =
23342 (const xmlChar **) xmlMalloc(10 *
23343 sizeof(const xmlChar *));
23344 if (ielem->nsBindings == NULL) {
23345 xmlSchemaVErrMemory(vctxt,
23346 "allocating namespace bindings for SAX validation",
23347 NULL);
23348 goto internal_error;
23349 }
23350 ielem->nbNsBindings = 0;
23351 ielem->sizeNsBindings = 5;
23352 } else if (ielem->sizeNsBindings <= ielem->nbNsBindings) {
23353 ielem->sizeNsBindings *= 2;
23354 ielem->nsBindings =
23355 (const xmlChar **) xmlRealloc(
23356 (void *) ielem->nsBindings,
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000023357 ielem->sizeNsBindings * 2 * sizeof(const xmlChar *));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023358 if (ielem->nsBindings == NULL) {
23359 xmlSchemaVErrMemory(vctxt,
23360 "re-allocating namespace bindings for SAX validation",
23361 NULL);
23362 goto internal_error;
23363 }
23364 }
23365
23366 ielem->nsBindings[ielem->nbNsBindings * 2] = namespaces[j];
23367 if (namespaces[j+1][0] == 0) {
23368 /*
23369 * Handle xmlns="".
23370 */
23371 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] = NULL;
23372 } else
23373 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] =
23374 namespaces[j+1];
23375 ielem->nbNsBindings++;
23376 }
23377 }
23378 /*
23379 * Register attributes.
23380 * SAX VAL TODO: We are not adding namespace declaration
23381 * attributes yet.
23382 */
23383 if (nb_attributes != 0) {
23384 xmlChar *value;
23385
23386 for (j = 0, i = 0; i < nb_attributes; i++, j += 5) {
23387 /*
23388 * Duplicate the value.
23389 */
23390 value = xmlStrndup(attributes[j+3],
23391 attributes[j+4] - attributes[j+3]);
23392 ret = xmlSchemaValidatorPushAttribute(vctxt,
23393 NULL, attributes[j], attributes[j+2], 0,
23394 value, 1);
23395 if (ret == -1) {
23396 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
23397 "calling xmlSchemaValidatorPushAttribute()");
23398 goto internal_error;
23399 }
23400 }
23401 }
23402 /*
23403 * Validate the element.
23404 */
23405 ret = xmlSchemaValidateElem(vctxt);
23406 if (ret != 0) {
23407 if (ret == -1) {
23408 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
23409 "calling xmlSchemaValidateElem()");
23410 goto internal_error;
23411 }
23412 goto exit;
23413 }
23414
23415exit:
23416 return;
23417internal_error:
23418 vctxt->err = -1;
23419 xmlStopParser(vctxt->parserCtxt);
23420 return;
23421}
23422
23423static void
23424xmlSchemaSAXHandleEndElementNs(void *ctx,
23425 const xmlChar * localname ATTRIBUTE_UNUSED,
23426 const xmlChar * prefix ATTRIBUTE_UNUSED,
23427 const xmlChar * URI ATTRIBUTE_UNUSED)
23428{
23429 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
23430 int res;
23431
23432 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000023433 * Skip elements if inside a "skip" wildcard or if invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023434 */
23435 if (vctxt->skipDepth != -1) {
23436 if (vctxt->depth > vctxt->skipDepth) {
23437 vctxt->depth--;
23438 return;
23439 } else
23440 vctxt->skipDepth = -1;
23441 }
23442 /*
23443 * SAX VAL TODO: Just a temporary check.
23444 */
23445 if ((!xmlStrEqual(vctxt->inode->localName, localname)) ||
23446 (!xmlStrEqual(vctxt->inode->nsName, URI))) {
23447 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
23448 "elem pop mismatch");
23449 }
23450 res = xmlSchemaValidatorPopElem(vctxt);
23451 if (res != 0) {
23452 if (res < 0) {
23453 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
23454 "calling xmlSchemaValidatorPopElem()");
23455 goto internal_error;
23456 }
23457 goto exit;
23458 }
23459exit:
23460 return;
23461internal_error:
23462 vctxt->err = -1;
23463 xmlStopParser(vctxt->parserCtxt);
23464 return;
23465}
23466#endif
23467
Daniel Veillard4255d502002-04-16 15:50:10 +000023468/************************************************************************
23469 * *
23470 * Validation interfaces *
23471 * *
23472 ************************************************************************/
23473
23474/**
23475 * xmlSchemaNewValidCtxt:
23476 * @schema: a precompiled XML Schemas
23477 *
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000023478 * Create an XML Schemas validation context based on the given schema.
Daniel Veillard4255d502002-04-16 15:50:10 +000023479 *
23480 * Returns the validation context or NULL in case of error
23481 */
23482xmlSchemaValidCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000023483xmlSchemaNewValidCtxt(xmlSchemaPtr schema)
23484{
Daniel Veillard4255d502002-04-16 15:50:10 +000023485 xmlSchemaValidCtxtPtr ret;
23486
23487 ret = (xmlSchemaValidCtxtPtr) xmlMalloc(sizeof(xmlSchemaValidCtxt));
23488 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000023489 xmlSchemaVErrMemory(NULL, "allocating validation context", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000023490 return (NULL);
23491 }
23492 memset(ret, 0, sizeof(xmlSchemaValidCtxt));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023493 ret->type = XML_SCHEMA_CTXT_VALIDATOR;
23494 ret->schema = schema;
Daniel Veillard4255d502002-04-16 15:50:10 +000023495 return (ret);
23496}
23497
23498/**
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000023499 * xmlSchemaClearValidCtxt:
23500 * @ctxt: the schema validation context
23501 *
23502 * Free the resources associated to the schema validation context;
23503 * leaves some fields alive intended for reuse of the context.
23504 */
23505static void
23506xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt)
23507{
23508 if (vctxt == NULL)
23509 return;
23510
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000023511 vctxt->flags = 0;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000023512 vctxt->validationRoot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023513 vctxt->doc = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000023514#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023515 vctxt->reader = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000023516#endif
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000023517 if (vctxt->value != NULL) {
23518 xmlSchemaFreeValue(vctxt->value);
23519 vctxt->value = NULL;
23520 }
23521 /*
23522 * Augmented IDC information.
23523 */
23524 if (vctxt->aidcs != NULL) {
23525 xmlSchemaIDCAugPtr cur = vctxt->aidcs, next;
23526 do {
23527 next = cur->next;
23528 xmlFree(cur);
23529 cur = next;
23530 } while (cur != NULL);
23531 vctxt->aidcs = NULL;
23532 }
23533 if (vctxt->idcNodes != NULL) {
23534 int i;
23535 xmlSchemaPSVIIDCNodePtr item;
23536
23537 for (i = 0; i < vctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023538 item = vctxt->idcNodes[i];
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000023539 xmlFree(item->keys);
23540 xmlFree(item);
23541 }
23542 xmlFree(vctxt->idcNodes);
23543 vctxt->idcNodes = NULL;
23544 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023545 /*
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000023546 * Note that we won't delete the XPath state pool here.
23547 */
23548 if (vctxt->xpathStates != NULL) {
23549 xmlSchemaFreeIDCStateObjList(vctxt->xpathStates);
23550 vctxt->xpathStates = NULL;
23551 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023552 /*
23553 * Attribute info.
23554 */
23555 if (vctxt->nbAttrInfos != 0) {
23556 xmlSchemaClearAttrInfos(vctxt);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000023557 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023558 /*
23559 * Element info.
23560 */
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000023561 if (vctxt->elemInfos != NULL) {
23562 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023563 xmlSchemaNodeInfoPtr ei;
23564
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000023565 for (i = 0; i < vctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023566 ei = vctxt->elemInfos[i];
23567 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000023568 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023569 xmlSchemaClearElemInfo(ei);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000023570 }
23571 }
23572}
23573
23574/**
Daniel Veillard4255d502002-04-16 15:50:10 +000023575 * xmlSchemaFreeValidCtxt:
23576 * @ctxt: the schema validation context
23577 *
23578 * Free the resources associated to the schema validation context
23579 */
23580void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000023581xmlSchemaFreeValidCtxt(xmlSchemaValidCtxtPtr ctxt)
23582{
Daniel Veillard4255d502002-04-16 15:50:10 +000023583 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000023584 return;
Daniel Veillard88c58912002-04-23 07:12:20 +000023585 if (ctxt->value != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000023586 xmlSchemaFreeValue(ctxt->value);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000023587 if (ctxt->pctxt != NULL)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000023588 xmlSchemaFreeParserCtxt(ctxt->pctxt);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023589 if (ctxt->idcNodes != NULL) {
23590 int i;
23591 xmlSchemaPSVIIDCNodePtr item;
23592
23593 for (i = 0; i < ctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023594 item = ctxt->idcNodes[i];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023595 xmlFree(item->keys);
23596 xmlFree(item);
23597 }
23598 xmlFree(ctxt->idcNodes);
23599 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023600 if (ctxt->idcKeys != NULL) {
23601 int i;
23602 for (i = 0; i < ctxt->nbIdcKeys; i++)
23603 xmlSchemaIDCFreeKey(ctxt->idcKeys[i]);
23604 xmlFree(ctxt->idcKeys);
23605 }
23606
23607 if (ctxt->xpathStates != NULL)
23608 xmlSchemaFreeIDCStateObjList(ctxt->xpathStates);
23609 if (ctxt->xpathStatePool != NULL)
23610 xmlSchemaFreeIDCStateObjList(ctxt->xpathStatePool);
23611
23612 /*
23613 * Augmented IDC information.
23614 */
23615 if (ctxt->aidcs != NULL) {
23616 xmlSchemaIDCAugPtr cur = ctxt->aidcs, next;
23617 do {
23618 next = cur->next;
23619 xmlFree(cur);
23620 cur = next;
23621 } while (cur != NULL);
23622 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023623 if (ctxt->attrInfos != NULL) {
23624 int i;
23625 xmlSchemaAttrInfoPtr attr;
23626
23627 /* Just a paranoid call to the cleanup. */
23628 if (ctxt->nbAttrInfos != 0)
23629 xmlSchemaClearAttrInfos(ctxt);
23630 for (i = 0; i < ctxt->sizeAttrInfos; i++) {
23631 attr = ctxt->attrInfos[i];
23632 xmlFree(attr);
23633 }
23634 xmlFree(ctxt->attrInfos);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000023635 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023636 if (ctxt->elemInfos != NULL) {
23637 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023638 xmlSchemaNodeInfoPtr ei;
23639
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023640 for (i = 0; i < ctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023641 ei = ctxt->elemInfos[i];
23642 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000023643 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023644 xmlSchemaClearElemInfo(ei);
23645 xmlFree(ei);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023646 }
23647 xmlFree(ctxt->elemInfos);
23648 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023649 if (ctxt->dict != NULL)
23650 xmlDictFree(ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +000023651 xmlFree(ctxt);
23652}
23653
23654/**
Daniel Veillardf10ae122005-07-10 19:03:16 +000023655 * xmlSchemaIsValid:
23656 * @ctxt: the schema validation context
23657 *
23658 * Check if any error was detected during validation.
23659 *
23660 * Returns 1 if valid so far, 0 if errors were detected, and -1 in case
23661 * of internal error.
23662 */
23663int
23664xmlSchemaIsValid(xmlSchemaValidCtxtPtr ctxt)
23665{
23666 if (ctxt == NULL)
23667 return(-1);
23668 return(ctxt->err == 0);
23669}
23670
23671/**
Daniel Veillard4255d502002-04-16 15:50:10 +000023672 * xmlSchemaSetValidErrors:
23673 * @ctxt: a schema validation context
23674 * @err: the error function
23675 * @warn: the warning function
Daniel Veillarda9b66d02002-12-11 14:23:49 +000023676 * @ctx: the functions context
Daniel Veillard4255d502002-04-16 15:50:10 +000023677 *
William M. Brack2f2a6632004-08-20 23:09:47 +000023678 * Set the error and warning callback informations
Daniel Veillard4255d502002-04-16 15:50:10 +000023679 */
23680void
23681xmlSchemaSetValidErrors(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000023682 xmlSchemaValidityErrorFunc err,
23683 xmlSchemaValidityWarningFunc warn, void *ctx)
23684{
Daniel Veillard4255d502002-04-16 15:50:10 +000023685 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000023686 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000023687 ctxt->error = err;
23688 ctxt->warning = warn;
23689 ctxt->userData = ctx;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000023690 if (ctxt->pctxt != NULL)
23691 xmlSchemaSetParserErrors(ctxt->pctxt, err, warn, ctx);
Daniel Veillard4255d502002-04-16 15:50:10 +000023692}
23693
23694/**
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000023695 * xmlSchemaSetValidStructuredErrors:
23696 * @ctxt: a schema validation context
23697 * @serror: the structured error function
23698 * @ctx: the functions context
23699 *
23700 * Set the structured error callback
23701 */
23702void
23703xmlSchemaSetValidStructuredErrors(xmlSchemaValidCtxtPtr ctxt,
23704 xmlStructuredErrorFunc serror, void *ctx)
23705{
23706 if (ctxt == NULL)
23707 return;
23708 ctxt->serror = serror;
23709 ctxt->error = NULL;
23710 ctxt->warning = NULL;
23711 ctxt->userData = ctx;
23712}
23713
23714/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000023715 * xmlSchemaGetValidErrors:
23716 * @ctxt: a XML-Schema validation context
23717 * @err: the error function result
23718 * @warn: the warning function result
23719 * @ctx: the functions context result
23720 *
23721 * Get the error and warning callback informations
23722 *
23723 * Returns -1 in case of error and 0 otherwise
23724 */
23725int
23726xmlSchemaGetValidErrors(xmlSchemaValidCtxtPtr ctxt,
23727 xmlSchemaValidityErrorFunc * err,
23728 xmlSchemaValidityWarningFunc * warn, void **ctx)
23729{
23730 if (ctxt == NULL)
23731 return (-1);
23732 if (err != NULL)
23733 *err = ctxt->error;
23734 if (warn != NULL)
23735 *warn = ctxt->warning;
23736 if (ctx != NULL)
23737 *ctx = ctxt->userData;
23738 return (0);
23739}
23740
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000023741
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000023742/**
Daniel Veillard6927b102004-10-27 17:29:04 +000023743 * xmlSchemaSetValidOptions:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000023744 * @ctxt: a schema validation context
23745 * @options: a combination of xmlSchemaValidOption
23746 *
23747 * Sets the options to be used during the validation.
23748 *
23749 * Returns 0 in case of success, -1 in case of an
23750 * API error.
23751 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000023752int
23753xmlSchemaSetValidOptions(xmlSchemaValidCtxtPtr ctxt,
23754 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023755
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000023756{
23757 int i;
23758
23759 if (ctxt == NULL)
23760 return (-1);
23761 /*
23762 * WARNING: Change the start value if adding to the
23763 * xmlSchemaValidOption.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000023764 * TODO: Is there an other, more easy to maintain,
23765 * way?
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000023766 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000023767 for (i = 1; i < (int) sizeof(int) * 8; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023768 if (options & 1<<i)
23769 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000023770 }
23771 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023772 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000023773}
23774
23775/**
Daniel Veillard6927b102004-10-27 17:29:04 +000023776 * xmlSchemaValidCtxtGetOptions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023777 * @ctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000023778 *
William M. Brack21e4ef22005-01-02 09:53:13 +000023779 * Get the validation context options.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023780 *
William M. Brack21e4ef22005-01-02 09:53:13 +000023781 * Returns the option combination or -1 on error.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000023782 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000023783int
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000023784xmlSchemaValidCtxtGetOptions(xmlSchemaValidCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023785
23786{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000023787 if (ctxt == NULL)
23788 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023789 else
23790 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000023791}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000023792
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023793static int
23794xmlSchemaVDocWalk(xmlSchemaValidCtxtPtr vctxt)
23795{
23796 xmlAttrPtr attr;
23797 int ret = 0;
23798 xmlSchemaNodeInfoPtr ielem = NULL;
23799 xmlNodePtr node, valRoot;
23800 const xmlChar *nsName;
23801
23802 /* DOC VAL TODO: Move this to the start function. */
23803 valRoot = xmlDocGetRootElement(vctxt->doc);
23804 if (valRoot == NULL) {
23805 /* VAL TODO: Error code? */
23806 VERROR(1, NULL, "The document has no document element");
23807 return (1);
23808 }
23809 vctxt->depth = -1;
23810 vctxt->validationRoot = valRoot;
23811 node = valRoot;
23812 while (node != NULL) {
23813 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
23814 goto next_sibling;
23815 if (node->type == XML_ELEMENT_NODE) {
23816
23817 /*
23818 * Init the node-info.
23819 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000023820 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023821 if (xmlSchemaValidatorPushElem(vctxt) == -1)
23822 goto internal_error;
23823 ielem = vctxt->inode;
23824 ielem->node = node;
23825 ielem->localName = node->name;
23826 if (node->ns != NULL)
23827 ielem->nsName = node->ns->href;
23828 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
23829 /*
23830 * Register attributes.
23831 * DOC VAL TODO: We do not register namespace declaration
23832 * attributes yet.
23833 */
23834 vctxt->nbAttrInfos = 0;
23835 if (node->properties != NULL) {
23836 attr = node->properties;
23837 do {
23838 if (attr->ns != NULL)
23839 nsName = attr->ns->href;
23840 else
23841 nsName = NULL;
23842 ret = xmlSchemaValidatorPushAttribute(vctxt,
23843 (xmlNodePtr) attr,
23844 attr->name, nsName, 0,
23845 xmlNodeListGetString(attr->doc, attr->children, 1), 1);
23846 if (ret == -1) {
23847 VERROR_INT("xmlSchemaDocWalk",
23848 "calling xmlSchemaValidatorPushAttribute()");
23849 goto internal_error;
23850 }
23851 attr = attr->next;
23852 } while (attr);
23853 }
23854 /*
23855 * Validate the element.
23856 */
23857 ret = xmlSchemaValidateElem(vctxt);
23858 if (ret != 0) {
23859 if (ret == -1) {
23860 VERROR_INT("xmlSchemaDocWalk",
23861 "calling xmlSchemaValidateElem()");
23862 goto internal_error;
23863 }
23864 /*
23865 * Don't stop validation; just skip the content
23866 * of this element.
23867 */
23868 goto leave_node;
23869 }
23870 if ((vctxt->skipDepth != -1) &&
23871 (vctxt->depth >= vctxt->skipDepth))
23872 goto leave_node;
23873 } else if ((node->type == XML_TEXT_NODE) ||
23874 (node->type == XML_CDATA_SECTION_NODE)) {
23875 /*
23876 * Process character content.
23877 */
23878 if (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
23879 ielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
23880 ret = xmlSchemaVPushText(vctxt, node->type, node->content,
23881 -1, XML_SCHEMA_PUSH_TEXT_PERSIST, NULL);
23882 if (ret < 0) {
23883 VERROR_INT("xmlSchemaVDocWalk",
23884 "calling xmlSchemaVPushText()");
23885 goto internal_error;
23886 }
23887 /*
23888 * DOC VAL TODO: Should we skip further validation of the
23889 * element content here?
23890 */
23891 } else if ((node->type == XML_ENTITY_NODE) ||
23892 (node->type == XML_ENTITY_REF_NODE)) {
23893 /*
23894 * DOC VAL TODO: What to do with entities?
23895 */
23896 TODO
23897 } else {
23898 goto leave_node;
23899 /*
23900 * DOC VAL TODO: XInclude nodes, etc.
23901 */
23902 }
23903 /*
23904 * Walk the doc.
23905 */
23906 if (node->children != NULL) {
23907 node = node->children;
23908 continue;
23909 }
23910leave_node:
23911 if (node->type == XML_ELEMENT_NODE) {
23912 /*
23913 * Leaving the scope of an element.
23914 */
23915 if (node != vctxt->inode->node) {
23916 VERROR_INT("xmlSchemaVDocWalk",
23917 "element position mismatch");
23918 goto internal_error;
23919 }
23920 ret = xmlSchemaValidatorPopElem(vctxt);
23921 if (ret != 0) {
23922 if (ret < 0) {
23923 VERROR_INT("xmlSchemaVDocWalk",
23924 "calling xmlSchemaValidatorPopElem()");
23925 goto internal_error;
23926 }
23927 }
23928 if (node == valRoot)
23929 goto exit;
23930 }
23931next_sibling:
23932 if (node->next != NULL)
23933 node = node->next;
23934 else {
23935 node = node->parent;
23936 goto leave_node;
23937 }
23938 }
23939
23940exit:
23941 return (ret);
23942internal_error:
23943 return (-1);
23944}
23945
23946static int
Daniel Veillardf10ae122005-07-10 19:03:16 +000023947xmlSchemaPreRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023948 /*
23949 * Some initialization.
23950 */
23951 vctxt->err = 0;
23952 vctxt->nberrors = 0;
23953 vctxt->depth = -1;
23954 vctxt->skipDepth = -1;
23955 /*
23956 * Create a schema + parser if necessary.
23957 */
23958 if (vctxt->schema == NULL) {
23959
23960 if ((vctxt->pctxt == NULL) &&
23961 (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
23962 return (-1);
23963
23964 vctxt->schema = xmlSchemaNewSchema(vctxt->pctxt);
23965 if (vctxt->schema == NULL) {
23966 VERROR_INT("xmlSchemaVStartValidation",
23967 "creating a schema");
23968 return (-1);
23969 }
23970 vctxt->xsiAssemble = 1;
23971 } else
23972 vctxt->xsiAssemble = 0;
23973 /*
23974 * Augment the IDC definitions.
23975 */
23976 if (vctxt->schema->idcDef != NULL) {
23977 xmlHashScan(vctxt->schema->idcDef,
23978 (xmlHashScanner) xmlSchemaAugmentIDC, vctxt);
23979 }
Daniel Veillardf10ae122005-07-10 19:03:16 +000023980 return(0);
23981}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023982
Daniel Veillardf10ae122005-07-10 19:03:16 +000023983static void
23984xmlSchemaPostRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023985 if (vctxt->xsiAssemble) {
23986 if (vctxt->schema != NULL) {
23987 xmlSchemaFree(vctxt->schema);
23988 vctxt->schema = NULL;
23989 }
23990 }
23991 xmlSchemaClearValidCtxt(vctxt);
Daniel Veillardf10ae122005-07-10 19:03:16 +000023992}
23993
23994static int
23995xmlSchemaVStart(xmlSchemaValidCtxtPtr vctxt)
23996{
23997 int ret = 0;
23998
23999 if (xmlSchemaPreRun(vctxt) < 0)
24000 return(-1);
24001
24002 if (vctxt->doc != NULL) {
24003 /*
24004 * Tree validation.
24005 */
24006 ret = xmlSchemaVDocWalk(vctxt);
24007#ifdef LIBXML_READER_ENABLED
24008 } else if (vctxt->reader != NULL) {
24009 /*
24010 * XML Reader validation.
24011 */
24012#ifdef XML_SCHEMA_READER_ENABLED
24013 ret = xmlSchemaVReaderWalk(vctxt);
24014#endif
24015#endif
24016 } else if ((vctxt->sax != NULL) && (vctxt->parserCtxt != NULL)) {
24017 /*
24018 * SAX validation.
24019 */
24020 ret = xmlParseDocument(vctxt->parserCtxt);
24021 } else {
24022 VERROR_INT("xmlSchemaVStartValidation",
24023 "no instance to validate");
24024 ret = -1;
24025 }
24026
24027 xmlSchemaPostRun(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024028 if (ret == 0)
24029 ret = vctxt->err;
24030 return (ret);
24031}
24032
24033/**
24034 * xmlSchemaValidateOneElement:
24035 * @ctxt: a schema validation context
24036 * @elem: an element node
24037 *
24038 * Validate a branch of a tree, starting with the given @elem.
24039 *
24040 * Returns 0 if the element and its subtree is valid, a positive error
24041 * code number otherwise and -1 in case of an internal or API error.
24042 */
24043int
24044xmlSchemaValidateOneElement(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem)
24045{
24046 if ((ctxt == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
24047 return (-1);
24048
24049 if (ctxt->schema == NULL)
24050 return (-1);
24051
24052 ctxt->doc = elem->doc;
24053 ctxt->node = elem;
24054 ctxt->validationRoot = elem;
24055 return(xmlSchemaVStart(ctxt));
24056}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000024057
Daniel Veillard259f0df2004-08-18 09:13:18 +000024058/**
Daniel Veillard4255d502002-04-16 15:50:10 +000024059 * xmlSchemaValidateDoc:
24060 * @ctxt: a schema validation context
24061 * @doc: a parsed document tree
24062 *
24063 * Validate a document tree in memory.
24064 *
24065 * Returns 0 if the document is schemas valid, a positive error code
24066 * number otherwise and -1 in case of internal or API error.
24067 */
24068int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000024069xmlSchemaValidateDoc(xmlSchemaValidCtxtPtr ctxt, xmlDocPtr doc)
24070{
Daniel Veillard4255d502002-04-16 15:50:10 +000024071 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000024072 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000024073
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024074 ctxt->doc = doc;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000024075 ctxt->node = xmlDocGetRootElement(doc);
24076 if (ctxt->node == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024077 xmlSchemaCustomErr((xmlSchemaAbstractCtxtPtr) ctxt,
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000024078 XML_SCHEMAV_DOCUMENT_ELEMENT_MISSING,
24079 (xmlNodePtr) doc, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024080 "The document has no document element", NULL, NULL);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000024081 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024082 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000024083 ctxt->validationRoot = ctxt->node;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024084 return (xmlSchemaVStart(ctxt));
Daniel Veillard4255d502002-04-16 15:50:10 +000024085}
24086
Daniel Veillardcdc82732005-07-08 15:04:06 +000024087
24088/************************************************************************
24089 * *
24090 * Function and data for SAX streaming API *
24091 * *
24092 ************************************************************************/
24093typedef struct _xmlSchemaSplitSAXData xmlSchemaSplitSAXData;
24094typedef xmlSchemaSplitSAXData *xmlSchemaSplitSAXDataPtr;
24095
24096struct _xmlSchemaSplitSAXData {
24097 xmlSAXHandlerPtr user_sax;
24098 void *user_data;
24099 xmlSchemaValidCtxtPtr ctxt;
24100 xmlSAXHandlerPtr schemas_sax;
24101};
24102
Daniel Veillard971771e2005-07-09 17:32:57 +000024103#define XML_SAX_PLUG_MAGIC 0xdc43ba21
24104
24105struct _xmlSchemaSAXPlug {
24106 unsigned int magic;
24107
24108 /* the original callbacks informations */
24109 xmlSAXHandlerPtr *user_sax_ptr;
24110 xmlSAXHandlerPtr user_sax;
24111 void **user_data_ptr;
24112 void *user_data;
24113
24114 /* the block plugged back and validation informations */
24115 xmlSAXHandler schemas_sax;
24116 xmlSchemaValidCtxtPtr ctxt;
24117};
24118
Daniel Veillardcdc82732005-07-08 15:04:06 +000024119/* All those functions just bounces to the user provided SAX handlers */
24120static void
24121internalSubsetSplit(void *ctx, const xmlChar *name,
24122 const xmlChar *ExternalID, const xmlChar *SystemID)
24123{
Daniel Veillard971771e2005-07-09 17:32:57 +000024124 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024125 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
24126 (ctxt->user_sax->internalSubset != NULL))
24127 ctxt->user_sax->internalSubset(ctxt->user_data, name, ExternalID,
24128 SystemID);
24129}
24130
24131static int
24132isStandaloneSplit(void *ctx)
24133{
Daniel Veillard971771e2005-07-09 17:32:57 +000024134 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024135 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
24136 (ctxt->user_sax->isStandalone != NULL))
24137 return(ctxt->user_sax->isStandalone(ctxt->user_data));
24138 return(0);
24139}
24140
24141static int
24142hasInternalSubsetSplit(void *ctx)
24143{
Daniel Veillard971771e2005-07-09 17:32:57 +000024144 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024145 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
24146 (ctxt->user_sax->hasInternalSubset != NULL))
24147 return(ctxt->user_sax->hasInternalSubset(ctxt->user_data));
24148 return(0);
24149}
24150
24151static int
24152hasExternalSubsetSplit(void *ctx)
24153{
Daniel Veillard971771e2005-07-09 17:32:57 +000024154 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024155 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
24156 (ctxt->user_sax->hasExternalSubset != NULL))
24157 return(ctxt->user_sax->hasExternalSubset(ctxt->user_data));
24158 return(0);
24159}
24160
24161static void
24162externalSubsetSplit(void *ctx, const xmlChar *name,
24163 const xmlChar *ExternalID, const xmlChar *SystemID)
24164{
Daniel Veillard971771e2005-07-09 17:32:57 +000024165 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024166 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
24167 (ctxt->user_sax->internalSubset != NULL))
24168 ctxt->user_sax->internalSubset(ctxt->user_data, name, ExternalID,
24169 SystemID);
24170}
24171
24172static xmlParserInputPtr
24173resolveEntitySplit(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
24174{
Daniel Veillard971771e2005-07-09 17:32:57 +000024175 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024176 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
24177 (ctxt->user_sax->resolveEntity != NULL))
24178 return(ctxt->user_sax->resolveEntity(ctxt->user_data, publicId,
24179 systemId));
24180 return(NULL);
24181}
24182
24183static xmlEntityPtr
24184getEntitySplit(void *ctx, const xmlChar *name)
24185{
Daniel Veillard971771e2005-07-09 17:32:57 +000024186 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024187 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
24188 (ctxt->user_sax->getEntity != NULL))
24189 return(ctxt->user_sax->getEntity(ctxt->user_data, name));
24190 return(NULL);
24191}
24192
24193static xmlEntityPtr
24194getParameterEntitySplit(void *ctx, const xmlChar *name)
24195{
Daniel Veillard971771e2005-07-09 17:32:57 +000024196 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024197 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
24198 (ctxt->user_sax->getParameterEntity != NULL))
24199 return(ctxt->user_sax->getParameterEntity(ctxt->user_data, name));
24200 return(NULL);
24201}
24202
24203
24204static void
24205entityDeclSplit(void *ctx, const xmlChar *name, int type,
24206 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
24207{
Daniel Veillard971771e2005-07-09 17:32:57 +000024208 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024209 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
24210 (ctxt->user_sax->entityDecl != NULL))
24211 ctxt->user_sax->entityDecl(ctxt->user_data, name, type, publicId,
24212 systemId, content);
24213}
24214
24215static void
24216attributeDeclSplit(void *ctx, const xmlChar * elem,
24217 const xmlChar * name, int type, int def,
24218 const xmlChar * defaultValue, xmlEnumerationPtr tree)
24219{
Daniel Veillard971771e2005-07-09 17:32:57 +000024220 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024221 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
24222 (ctxt->user_sax->attributeDecl != NULL)) {
24223 ctxt->user_sax->attributeDecl(ctxt->user_data, elem, name, type,
24224 def, defaultValue, tree);
24225 } else {
24226 xmlFreeEnumeration(tree);
24227 }
24228}
24229
24230static void
24231elementDeclSplit(void *ctx, const xmlChar *name, int type,
24232 xmlElementContentPtr content)
24233{
Daniel Veillard971771e2005-07-09 17:32:57 +000024234 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024235 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
24236 (ctxt->user_sax->elementDecl != NULL))
24237 ctxt->user_sax->elementDecl(ctxt->user_data, name, type, content);
24238}
24239
24240static void
24241notationDeclSplit(void *ctx, const xmlChar *name,
24242 const xmlChar *publicId, const xmlChar *systemId)
24243{
Daniel Veillard971771e2005-07-09 17:32:57 +000024244 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024245 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
24246 (ctxt->user_sax->notationDecl != NULL))
24247 ctxt->user_sax->notationDecl(ctxt->user_data, name, publicId,
24248 systemId);
24249}
24250
24251static void
24252unparsedEntityDeclSplit(void *ctx, const xmlChar *name,
24253 const xmlChar *publicId, const xmlChar *systemId,
24254 const xmlChar *notationName)
24255{
Daniel Veillard971771e2005-07-09 17:32:57 +000024256 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024257 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
24258 (ctxt->user_sax->unparsedEntityDecl != NULL))
24259 ctxt->user_sax->unparsedEntityDecl(ctxt->user_data, name, publicId,
24260 systemId, notationName);
24261}
24262
24263static void
24264setDocumentLocatorSplit(void *ctx, xmlSAXLocatorPtr loc)
24265{
Daniel Veillard971771e2005-07-09 17:32:57 +000024266 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024267 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
24268 (ctxt->user_sax->setDocumentLocator != NULL))
24269 ctxt->user_sax->setDocumentLocator(ctxt->user_data, loc);
24270}
24271
24272static void
24273startDocumentSplit(void *ctx)
24274{
Daniel Veillard971771e2005-07-09 17:32:57 +000024275 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024276 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
24277 (ctxt->user_sax->startDocument != NULL))
24278 ctxt->user_sax->startDocument(ctxt->user_data);
24279}
24280
24281static void
24282endDocumentSplit(void *ctx)
24283{
Daniel Veillard971771e2005-07-09 17:32:57 +000024284 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024285 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
24286 (ctxt->user_sax->endDocument != NULL))
24287 ctxt->user_sax->endDocument(ctxt->user_data);
24288}
24289
24290static void
24291processingInstructionSplit(void *ctx, const xmlChar *target,
24292 const xmlChar *data)
24293{
Daniel Veillard971771e2005-07-09 17:32:57 +000024294 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024295 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
24296 (ctxt->user_sax->processingInstruction != NULL))
24297 ctxt->user_sax->processingInstruction(ctxt->user_data, target, data);
24298}
24299
24300static void
24301commentSplit(void *ctx, const xmlChar *value)
24302{
Daniel Veillard971771e2005-07-09 17:32:57 +000024303 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024304 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
24305 (ctxt->user_sax->comment != NULL))
24306 ctxt->user_sax->comment(ctxt->user_data, value);
24307}
24308
24309/*
24310 * Varargs error callbacks to the user application, harder ...
24311 */
24312
Daniel Veillardffa3c742005-07-21 13:24:09 +000024313static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000024314warningSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000024315 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024316 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
24317 (ctxt->user_sax->warning != NULL)) {
24318 TODO
24319 }
24320}
Daniel Veillardffa3c742005-07-21 13:24:09 +000024321static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000024322errorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000024323 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024324 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
24325 (ctxt->user_sax->error != NULL)) {
24326 TODO
24327 }
24328}
Daniel Veillardffa3c742005-07-21 13:24:09 +000024329static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000024330fatalErrorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000024331 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024332 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
24333 (ctxt->user_sax->fatalError != NULL)) {
24334 TODO
24335 }
24336}
24337
24338/*
24339 * Those are function where both the user handler and the schemas handler
24340 * need to be called.
24341 */
24342static void
24343charactersSplit(void *ctx, const xmlChar *ch, int len)
24344{
Daniel Veillard971771e2005-07-09 17:32:57 +000024345 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024346 if (ctxt == NULL)
24347 return;
24348 if ((ctxt->user_sax != NULL) && (ctxt->user_sax->characters != NULL))
24349 ctxt->user_sax->characters(ctxt->user_data, ch, len);
24350 if (ctxt->ctxt != NULL)
24351 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
24352}
24353
24354static void
24355ignorableWhitespaceSplit(void *ctx, const xmlChar *ch, int len)
24356{
Daniel Veillard971771e2005-07-09 17:32:57 +000024357 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024358 if (ctxt == NULL)
24359 return;
24360 if ((ctxt->user_sax != NULL) &&
24361 (ctxt->user_sax->ignorableWhitespace != NULL))
24362 ctxt->user_sax->ignorableWhitespace(ctxt->user_data, ch, len);
24363 if (ctxt->ctxt != NULL)
24364 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
24365}
24366
24367static void
24368cdataBlockSplit(void *ctx, const xmlChar *value, int len)
24369{
Daniel Veillard971771e2005-07-09 17:32:57 +000024370 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024371 if (ctxt == NULL)
24372 return;
24373 if ((ctxt->user_sax != NULL) &&
24374 (ctxt->user_sax->ignorableWhitespace != NULL))
24375 ctxt->user_sax->ignorableWhitespace(ctxt->user_data, value, len);
24376 if (ctxt->ctxt != NULL)
24377 xmlSchemaSAXHandleCDataSection(ctxt->ctxt, value, len);
24378}
24379
24380static void
24381referenceSplit(void *ctx, const xmlChar *name)
24382{
Daniel Veillard971771e2005-07-09 17:32:57 +000024383 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024384 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
24385 (ctxt->user_sax->reference != NULL))
24386 ctxt->user_sax->reference(ctxt->user_data, name);
24387 if (ctxt->ctxt != NULL)
24388 xmlSchemaSAXHandleReference(ctxt->user_data, name);
24389}
24390
24391static void
24392startElementNsSplit(void *ctx, const xmlChar * localname,
24393 const xmlChar * prefix, const xmlChar * URI,
24394 int nb_namespaces, const xmlChar ** namespaces,
24395 int nb_attributes, int nb_defaulted,
24396 const xmlChar ** attributes) {
Daniel Veillard971771e2005-07-09 17:32:57 +000024397 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024398 if (ctxt == NULL)
24399 return;
24400 if ((ctxt->user_sax != NULL) &&
24401 (ctxt->user_sax->startElementNs != NULL))
24402 ctxt->user_sax->startElementNs(ctxt->user_data, localname, prefix,
24403 URI, nb_namespaces, namespaces,
24404 nb_attributes, nb_defaulted,
24405 attributes);
24406 if (ctxt->ctxt != NULL)
24407 xmlSchemaSAXHandleStartElementNs(ctxt->ctxt, localname, prefix,
24408 URI, nb_namespaces, namespaces,
24409 nb_attributes, nb_defaulted,
24410 attributes);
24411}
24412
24413static void
24414endElementNsSplit(void *ctx, const xmlChar * localname,
24415 const xmlChar * prefix, const xmlChar * URI) {
Daniel Veillard971771e2005-07-09 17:32:57 +000024416 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024417 if (ctxt == NULL)
24418 return;
24419 if ((ctxt->user_sax != NULL) &&
24420 (ctxt->user_sax->endElementNs != NULL))
24421 ctxt->user_sax->endElementNs(ctxt->user_data, localname, prefix, URI);
24422 if (ctxt->ctxt != NULL)
24423 xmlSchemaSAXHandleEndElementNs(ctxt->ctxt, localname, prefix, URI);
24424}
24425
Daniel Veillard4255d502002-04-16 15:50:10 +000024426/**
Daniel Veillard971771e2005-07-09 17:32:57 +000024427 * xmlSchemaSAXPlug:
24428 * @ctxt: a schema validation context
Daniel Veillard1f33c4d2005-07-10 21:38:31 +000024429 * @sax: a pointer to the original xmlSAXHandlerPtr
24430 * @user_data: a pointer to the original SAX user data pointer
Daniel Veillard971771e2005-07-09 17:32:57 +000024431 *
24432 * Plug a SAX based validation layer in a SAX parsing event flow.
24433 * The original @saxptr and @dataptr data are replaced by new pointers
24434 * but the calls to the original will be maintained.
24435 *
24436 * Returns a pointer to a data structure needed to unplug the validation layer
24437 * or NULL in case of errors.
24438 */
24439xmlSchemaSAXPlugPtr
24440xmlSchemaSAXPlug(xmlSchemaValidCtxtPtr ctxt,
24441 xmlSAXHandlerPtr *sax, void **user_data)
24442{
24443 xmlSchemaSAXPlugPtr ret;
24444 xmlSAXHandlerPtr old_sax;
24445
24446 if ((ctxt == NULL) || (sax == NULL) || (user_data == NULL))
24447 return(NULL);
24448
24449 /*
24450 * We only allow to plug into SAX2 event streams
24451 */
24452 old_sax = *sax;
24453 if ((old_sax != NULL) && (old_sax->initialized != XML_SAX2_MAGIC))
24454 return(NULL);
24455 if ((old_sax != NULL) &&
24456 (old_sax->startElementNs == NULL) && (old_sax->endElementNs == NULL) &&
24457 ((old_sax->startElement != NULL) || (old_sax->endElement != NULL)))
24458 return(NULL);
24459
24460 /*
24461 * everything seems right allocate the local data needed for that layer
24462 */
24463 ret = (xmlSchemaSAXPlugPtr) xmlMalloc(sizeof(xmlSchemaSAXPlugStruct));
24464 if (ret == NULL) {
24465 return(NULL);
24466 }
24467 memset(ret, 0, sizeof(xmlSchemaSAXPlugStruct));
24468 ret->magic = XML_SAX_PLUG_MAGIC;
24469 ret->schemas_sax.initialized = XML_SAX2_MAGIC;
24470 ret->ctxt = ctxt;
24471 ret->user_sax_ptr = sax;
24472 ret->user_sax = old_sax;
24473 if (old_sax == NULL) {
24474 /*
24475 * go direct, no need for the split block and functions.
24476 */
24477 ret->schemas_sax.startElementNs = xmlSchemaSAXHandleStartElementNs;
24478 ret->schemas_sax.endElementNs = xmlSchemaSAXHandleEndElementNs;
24479 /*
24480 * Note that we use the same text-function for both, to prevent
24481 * the parser from testing for ignorable whitespace.
24482 */
24483 ret->schemas_sax.ignorableWhitespace = xmlSchemaSAXHandleText;
24484 ret->schemas_sax.characters = xmlSchemaSAXHandleText;
24485
24486 ret->schemas_sax.cdataBlock = xmlSchemaSAXHandleCDataSection;
24487 ret->schemas_sax.reference = xmlSchemaSAXHandleReference;
24488
24489 ret->user_data = ctxt;
24490 *user_data = ctxt;
24491 } else {
24492 /*
24493 * for each callback unused by Schemas initialize it to the Split
24494 * routine only if non NULL in the user block, this can speed up
24495 * things at the SAX level.
24496 */
24497 if (old_sax->internalSubset != NULL)
24498 ret->schemas_sax.internalSubset = internalSubsetSplit;
24499 if (old_sax->isStandalone != NULL)
24500 ret->schemas_sax.isStandalone = isStandaloneSplit;
24501 if (old_sax->hasInternalSubset != NULL)
24502 ret->schemas_sax.hasInternalSubset = hasInternalSubsetSplit;
24503 if (old_sax->hasExternalSubset != NULL)
24504 ret->schemas_sax.hasExternalSubset = hasExternalSubsetSplit;
24505 if (old_sax->resolveEntity != NULL)
24506 ret->schemas_sax.resolveEntity = resolveEntitySplit;
24507 if (old_sax->getEntity != NULL)
24508 ret->schemas_sax.getEntity = getEntitySplit;
24509 if (old_sax->entityDecl != NULL)
24510 ret->schemas_sax.entityDecl = entityDeclSplit;
24511 if (old_sax->notationDecl != NULL)
24512 ret->schemas_sax.notationDecl = notationDeclSplit;
24513 if (old_sax->attributeDecl != NULL)
24514 ret->schemas_sax.attributeDecl = attributeDeclSplit;
24515 if (old_sax->elementDecl != NULL)
24516 ret->schemas_sax.elementDecl = elementDeclSplit;
24517 if (old_sax->unparsedEntityDecl != NULL)
24518 ret->schemas_sax.unparsedEntityDecl = unparsedEntityDeclSplit;
24519 if (old_sax->setDocumentLocator != NULL)
24520 ret->schemas_sax.setDocumentLocator = setDocumentLocatorSplit;
24521 if (old_sax->startDocument != NULL)
24522 ret->schemas_sax.startDocument = startDocumentSplit;
24523 if (old_sax->endDocument != NULL)
24524 ret->schemas_sax.endDocument = endDocumentSplit;
24525 if (old_sax->processingInstruction != NULL)
24526 ret->schemas_sax.processingInstruction = processingInstructionSplit;
24527 if (old_sax->comment != NULL)
24528 ret->schemas_sax.comment = commentSplit;
24529 if (old_sax->warning != NULL)
24530 ret->schemas_sax.warning = warningSplit;
24531 if (old_sax->error != NULL)
24532 ret->schemas_sax.error = errorSplit;
24533 if (old_sax->fatalError != NULL)
24534 ret->schemas_sax.fatalError = fatalErrorSplit;
24535 if (old_sax->getParameterEntity != NULL)
24536 ret->schemas_sax.getParameterEntity = getParameterEntitySplit;
24537 if (old_sax->externalSubset != NULL)
24538 ret->schemas_sax.externalSubset = externalSubsetSplit;
24539
24540 /*
24541 * the 6 schemas callback have to go to the splitter functions
24542 * Note that we use the same text-function for ignorableWhitespace
24543 * if possible, to prevent the parser from testing for ignorable
24544 * whitespace.
24545 */
24546 ret->schemas_sax.characters = charactersSplit;
24547 if ((old_sax->ignorableWhitespace != NULL) &&
24548 (old_sax->ignorableWhitespace != old_sax->characters))
24549 ret->schemas_sax.ignorableWhitespace = ignorableWhitespaceSplit;
24550 else
24551 ret->schemas_sax.ignorableWhitespace = charactersSplit;
24552 ret->schemas_sax.cdataBlock = cdataBlockSplit;
24553 ret->schemas_sax.reference = referenceSplit;
24554 ret->schemas_sax.startElementNs = startElementNsSplit;
24555 ret->schemas_sax.endElementNs = endElementNsSplit;
24556
24557 ret->user_data_ptr = user_data;
24558 ret->user_data = *user_data;
24559 *user_data = ret;
24560 }
24561
24562 /*
24563 * plug the pointers back.
24564 */
24565 *sax = &(ret->schemas_sax);
Daniel Veillardf10ae122005-07-10 19:03:16 +000024566 ctxt->sax = *sax;
24567 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
24568 xmlSchemaPreRun(ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000024569 return(ret);
24570}
24571
24572/**
24573 * xmlSchemaSAXUnplug:
24574 * @plug: a data structure returned by xmlSchemaSAXPlug
24575 *
24576 * Unplug a SAX based validation layer in a SAX parsing event flow.
24577 * The original pointers used in the call are restored.
24578 *
24579 * Returns 0 in case of success and -1 in case of failure.
24580 */
24581int
24582xmlSchemaSAXUnplug(xmlSchemaSAXPlugPtr plug)
24583{
24584 xmlSAXHandlerPtr *sax;
24585 void **user_data;
24586
24587 if ((plug == NULL) || (plug->magic != XML_SAX_PLUG_MAGIC))
24588 return(-1);
24589 plug->magic = 0;
24590
Daniel Veillardf10ae122005-07-10 19:03:16 +000024591 xmlSchemaPostRun(plug->ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000024592 /* restore the data */
24593 sax = plug->user_sax_ptr;
24594 *sax = plug->user_sax;
24595 if (plug->user_sax != NULL) {
24596 user_data = plug->user_data_ptr;
24597 *user_data = plug->user_data;
24598 }
24599
24600 /* free and return */
24601 xmlFree(plug);
24602 return(0);
24603}
24604
24605/**
Daniel Veillard4255d502002-04-16 15:50:10 +000024606 * xmlSchemaValidateStream:
24607 * @ctxt: a schema validation context
24608 * @input: the input to use for reading the data
24609 * @enc: an optional encoding information
24610 * @sax: a SAX handler for the resulting events
24611 * @user_data: the context to provide to the SAX handler.
24612 *
Daniel Veillardcdc82732005-07-08 15:04:06 +000024613 * Validate an input based on a flow of SAX event from the parser
24614 * and forward the events to the @sax handler with the provided @user_data
24615 * the user provided @sax handler must be a SAX2 one.
Daniel Veillard4255d502002-04-16 15:50:10 +000024616 *
24617 * Returns 0 if the document is schemas valid, a positive error code
24618 * number otherwise and -1 in case of internal or API error.
24619 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000024620int
Daniel Veillard4255d502002-04-16 15:50:10 +000024621xmlSchemaValidateStream(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000024622 xmlParserInputBufferPtr input, xmlCharEncoding enc,
24623 xmlSAXHandlerPtr sax, void *user_data)
24624{
Daniel Veillard971771e2005-07-09 17:32:57 +000024625 xmlSchemaSAXPlugPtr plug = NULL;
24626 xmlSAXHandlerPtr old_sax = NULL;
24627 xmlParserCtxtPtr pctxt = NULL;
24628 xmlParserInputPtr inputStream = NULL;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024629 int ret;
24630
Daniel Veillard4255d502002-04-16 15:50:10 +000024631 if ((ctxt == NULL) || (input == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000024632 return (-1);
Daniel Veillardcdc82732005-07-08 15:04:06 +000024633
Daniel Veillardcdc82732005-07-08 15:04:06 +000024634 /*
24635 * prepare the parser
24636 */
24637 pctxt = xmlNewParserCtxt();
24638 if (pctxt == NULL)
24639 return (-1);
24640 old_sax = pctxt->sax;
Daniel Veillard971771e2005-07-09 17:32:57 +000024641 pctxt->sax = sax;
24642 pctxt->userData = user_data;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024643#if 0
24644 if (options)
24645 xmlCtxtUseOptions(pctxt, options);
24646#endif
Kasimier T. Buchcik85aed6d2005-07-08 18:06:18 +000024647 pctxt->linenumbers = 1;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024648
Daniel Veillardf0af8ec2005-07-08 17:27:33 +000024649 inputStream = xmlNewIOInputStream(pctxt, input, enc);;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024650 if (inputStream == NULL) {
24651 ret = -1;
24652 goto done;
24653 }
24654 inputPush(pctxt, inputStream);
24655 ctxt->parserCtxt = pctxt;
24656 ctxt->input = input;
24657
24658 /*
Daniel Veillard971771e2005-07-09 17:32:57 +000024659 * Plug the validation and launch the parsing
Daniel Veillardcdc82732005-07-08 15:04:06 +000024660 */
Daniel Veillard971771e2005-07-09 17:32:57 +000024661 plug = xmlSchemaSAXPlug(ctxt, &(pctxt->sax), &(pctxt->userData));
24662 if (plug == NULL) {
24663 ret = -1;
24664 goto done;
24665 }
24666 ctxt->input = input;
24667 ctxt->enc = enc;
24668 ctxt->sax = pctxt->sax;
Daniel Veillardcdc82732005-07-08 15:04:06 +000024669 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
24670 ret = xmlSchemaVStart(ctxt);
24671
24672 if ((ret == 0) && (! ctxt->parserCtxt->wellFormed)) {
24673 ret = ctxt->parserCtxt->errNo;
24674 if (ret == 0)
24675 ret = 1;
24676 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000024677
24678done:
Daniel Veillard971771e2005-07-09 17:32:57 +000024679 ctxt->parserCtxt = NULL;
24680 ctxt->sax = NULL;
24681 ctxt->input = NULL;
24682 if (plug != NULL) {
24683 xmlSchemaSAXUnplug(plug);
24684 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000024685 /* cleanup */
Daniel Veillard971771e2005-07-09 17:32:57 +000024686 if (pctxt != NULL) {
24687 pctxt->sax = old_sax;
24688 xmlFreeParserCtxt(pctxt);
24689 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000024690 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +000024691}
24692
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024693/**
24694 * xmlSchemaValidateFile:
24695 * @ctxt: a schema validation context
Daniel Veillard81562d22005-06-15 13:27:56 +000024696 * @filename: the URI of the instance
24697 * @options: a future set of options, currently unused
24698 *
24699 * Do a schemas validation of the given resource, it will use the
24700 * SAX streamable validation internally.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024701 *
24702 * Returns 0 if the document is valid, a positive error code
24703 * number otherwise and -1 in case of an internal or API error.
24704 */
24705int
24706xmlSchemaValidateFile(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000024707 const char * filename,
24708 int options ATTRIBUTE_UNUSED)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024709{
Daniel Veillard81562d22005-06-15 13:27:56 +000024710#ifdef XML_SCHEMA_SAX_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024711 int ret;
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000024712 xmlParserInputBufferPtr input;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024713
24714 if ((ctxt == NULL) || (filename == NULL))
24715 return (-1);
24716
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000024717 input = xmlParserInputBufferCreateFilename(filename,
24718 XML_CHAR_ENCODING_NONE);
24719 if (input == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024720 return (-1);
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000024721 ret = xmlSchemaValidateStream(ctxt, input, XML_CHAR_ENCODING_NONE,
24722 NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024723 return (ret);
Daniel Veillard81562d22005-06-15 13:27:56 +000024724#else
24725 return (-1);
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000024726#endif /* XML_SCHEMA_SAX_ENABLED */
Daniel Veillard81562d22005-06-15 13:27:56 +000024727}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024728
Daniel Veillard5d4644e2005-04-01 13:11:58 +000024729#define bottom_xmlschemas
24730#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +000024731#endif /* LIBXML_SCHEMAS_ENABLED */